]> rtime.felk.cvut.cz Git - socketcan-devel.git/blob - kernel/2.6/drivers/net/can/sja1000/sja1000.c
Use EXPORT_SYMBOL_GPL instead of EXPORT_SYMBOL for drivers
[socketcan-devel.git] / kernel / 2.6 / drivers / net / can / sja1000 / sja1000.c
1 /*
2  * sja1000.c -  Philips SJA1000 network device driver
3  *
4  * Copyright (c) 2003 Matthias Brukner, Trajet Gmbh, Rebenring 33,
5  * 38106 Braunschweig, GERMANY
6  *
7  * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of Volkswagen nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * Alternatively, provided that this notice is retained in full, this
23  * software may be distributed under the terms of the GNU General
24  * Public License ("GPL") version 2, in which case the provisions of the
25  * GPL apply INSTEAD OF those given above.
26  *
27  * The provided data structures and external interfaces from this code
28  * are not restricted to be used by modules with a GPL compatible license.
29  *
30  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
31  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
32  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
33  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
34  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
36  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
37  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
40  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
41  * DAMAGE.
42  *
43  * Send feedback to <socketcan-users@lists.berlios.de>
44  *
45  */
46
47 #include <linux/module.h>
48 #include <linux/init.h>
49 #include <linux/kernel.h>
50 #include <linux/version.h>
51 #include <linux/sched.h>
52 #include <linux/types.h>
53 #include <linux/fcntl.h>
54 #include <linux/interrupt.h>
55 #include <linux/ptrace.h>
56 #include <linux/string.h>
57 #include <linux/errno.h>
58 #include <linux/netdevice.h>
59 #include <linux/if_arp.h>
60 #include <linux/if_ether.h>
61 #include <linux/skbuff.h>
62 #include <linux/delay.h>
63
64 #include <linux/can.h>
65 #include <linux/can/dev.h>
66 #include <linux/can/error.h>
67 #include <linux/can/dev.h>
68
69 #include "sja1000.h"
70
71 #include <linux/can/version.h>  /* for RCSID. Removed by mkpatch script */
72 RCSID("$Id: sja1000.c 531 2007-10-19 07:38:29Z hartkopp $");
73
74 #define DRV_NAME "sja1000"
75
76 MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
77 MODULE_LICENSE("Dual BSD/GPL");
78 MODULE_DESCRIPTION(DRV_NAME " CAN netdevice driver");
79
80 static struct can_bittiming_const sja1000_bittiming_const = {
81         .tseg1_min = 1,
82         .tseg1_max = 16,
83         .tseg2_min = 1,
84         .tseg2_max = 8,
85         .sjw_max = 4,
86         .brp_min = 1,
87         .brp_max = 64,
88         .brp_inc = 1,
89 };
90
91 static int sja1000_probe_chip(struct net_device *dev)
92 {
93         struct sja1000_priv *priv = netdev_priv(dev);
94
95         if (dev->base_addr && (priv->read_reg(dev, 0) == 0xFF)) {
96                 printk(KERN_INFO "%s: probing @0x%lX failed\n",
97                        DRV_NAME, dev->base_addr);
98                 return 0;
99         }
100         return 1;
101 }
102
103 static int set_reset_mode(struct net_device *dev)
104 {
105         struct sja1000_priv *priv = netdev_priv(dev);
106         unsigned char status = priv->read_reg(dev, REG_MOD);
107         int i;
108
109         /* disable interrupts */
110         priv->write_reg(dev, REG_IER, IRQ_OFF);
111
112         for (i = 0; i < 100; i++) {
113                 /* check reset bit */
114                 if (status & MOD_RM) {
115                         priv->can.state = CAN_STATE_STOPPED;
116                         return 0;
117                 }
118
119                 priv->write_reg(dev, REG_MOD, MOD_RM);  /* reset chip */
120                 status = priv->read_reg(dev, REG_MOD);
121                 udelay(10);
122         }
123
124         dev_err(ND2D(dev), "setting SJA1000 into reset mode failed!\n");
125         return 1;
126
127 }
128
129 static int set_normal_mode(struct net_device *dev)
130 {
131         struct sja1000_priv *priv = netdev_priv(dev);
132         unsigned char status = priv->read_reg(dev, REG_MOD);
133         int i;
134
135         for (i = 0; i < 100; i++) {
136                 /* check reset bit */
137                 if ((status & MOD_RM) == 0) {
138                         priv->can.state = CAN_STATE_ACTIVE;
139                         /* enable all interrupts */
140                         priv->write_reg(dev, REG_IER, IRQ_ALL);
141
142                         return 0;
143                 }
144
145                 /* set chip to normal mode */
146                 priv->write_reg(dev, REG_MOD, 0x00);
147                 status = priv->read_reg(dev, REG_MOD);
148                 udelay(10);
149         }
150
151         dev_err(ND2D(dev), "setting SJA1000 into normal mode failed!\n");
152         return 1;
153
154 }
155
156 static void sja1000_start(struct net_device *dev)
157 {
158         struct sja1000_priv *priv = netdev_priv(dev);
159
160         /* leave reset mode */
161         if (priv->can.state != CAN_STATE_STOPPED)
162                 set_reset_mode(dev);
163
164         /* Clear error counters and error code capture */
165         priv->write_reg(dev, REG_TXERR, 0x0);
166         priv->write_reg(dev, REG_RXERR, 0x0);
167         priv->read_reg(dev, REG_ECC);
168
169         /* leave reset mode */
170         set_normal_mode(dev);
171 }
172
173 static int sja1000_set_mode(struct net_device *dev, enum can_mode mode)
174 {
175         struct sja1000_priv *priv = netdev_priv(dev);
176
177         switch (mode) {
178         case CAN_MODE_START:
179                 if (!priv->open_time)
180                         return -EINVAL;
181
182                 sja1000_start(dev);
183                 if (netif_queue_stopped(dev))
184                         netif_wake_queue(dev);
185                 break;
186
187         default:
188                 return -EOPNOTSUPP;
189         }
190
191         return 0;
192 }
193
194 static int sja1000_get_state(struct net_device *dev, enum can_state *state)
195 {
196         struct sja1000_priv *priv = netdev_priv(dev);
197         u8 status;
198
199         /* FIXME: inspecting the status register to get the current state
200          * is not really necessary, because state changes are handled by
201          * in the ISR and the variable priv->can.state gets updated. The
202          * CAN devicde interface needs fixing!
203          */
204
205         spin_lock_irq(&priv->can.irq_lock);
206
207         if (priv->can.state == CAN_STATE_STOPPED) {
208                 *state =  CAN_STATE_STOPPED;
209         } else {
210                 status = priv->read_reg(dev, REG_SR);
211                 if (status & SR_BS)
212                         *state = CAN_STATE_BUS_OFF;
213                 else if (status & SR_ES) {
214                         if (priv->read_reg(dev, REG_TXERR) > 127 ||
215                             priv->read_reg(dev, REG_RXERR) > 127)
216                                 *state = CAN_STATE_BUS_PASSIVE;
217                         else
218                                 *state = CAN_STATE_BUS_WARNING;
219                 } else
220                         *state = CAN_STATE_ACTIVE;
221         }
222         /* Check state */
223         if (*state != priv->can.state)
224                 dev_err(ND2D(dev),
225                         "Oops, state mismatch: hard %d != soft %d\n",
226                         *state, priv->can.state);
227         spin_unlock_irq(&priv->can.irq_lock);
228         return 0;
229 }
230
231 static int sja1000_set_bittiming(struct net_device *dev)
232 {
233         struct sja1000_priv *priv = netdev_priv(dev);
234         struct can_bittiming *bt = &priv->can.bittiming;
235         u8 btr0, btr1;
236
237         btr0 = ((bt->brp - 1) & 0x3f) | (((bt->sjw - 1) & 0x3) << 6);
238         btr1 = ((bt->prop_seg + bt->phase_seg1 - 1) & 0xf) |
239                 (((bt->phase_seg2 - 1) & 0x7) << 4) |
240           ((priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES) << 7);
241
242         dev_info(ND2D(dev), "setting BTR0=0x%02x BTR1=0x%02x\n", btr0, btr1);
243
244         priv->write_reg(dev, REG_BTR0, btr0);
245         priv->write_reg(dev, REG_BTR1, btr1);
246
247         return 0;
248 }
249
250 /*
251  * initialize SJA1000 chip:
252  *   - reset chip
253  *   - set output mode
254  *   - set baudrate
255  *   - enable interrupts
256  *   - start operating mode
257  */
258 static void chipset_init(struct net_device *dev)
259 {
260         struct sja1000_priv *priv = netdev_priv(dev);
261
262         /* set clock divider and output control register */
263         priv->write_reg(dev, REG_CDR, priv->cdr | CDR_PELICAN);
264
265         /* set acceptance filter (accept all) */
266         priv->write_reg(dev, REG_ACCC0, 0x00);
267         priv->write_reg(dev, REG_ACCC1, 0x00);
268         priv->write_reg(dev, REG_ACCC2, 0x00);
269         priv->write_reg(dev, REG_ACCC3, 0x00);
270
271         priv->write_reg(dev, REG_ACCM0, 0xFF);
272         priv->write_reg(dev, REG_ACCM1, 0xFF);
273         priv->write_reg(dev, REG_ACCM2, 0xFF);
274         priv->write_reg(dev, REG_ACCM3, 0xFF);
275
276         priv->write_reg(dev, REG_OCR, priv->ocr | OCR_MODE_NORMAL);
277 }
278
279 /*
280  * transmit a CAN message
281  * message layout in the sk_buff should be like this:
282  * xx xx xx xx   ff      ll   00 11 22 33 44 55 66 77
283  * [  can-id ] [flags] [len] [can data (up to 8 bytes]
284  */
285 static int sja1000_start_xmit(struct sk_buff *skb, struct net_device *dev)
286 {
287         struct sja1000_priv *priv = netdev_priv(dev);
288 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
289         struct net_device_stats *stats = can_get_stats(dev);
290 #else
291         struct net_device_stats *stats = &dev->stats;
292 #endif
293         struct can_frame *cf = (struct can_frame *)skb->data;
294         uint8_t fi;
295         uint8_t dlc;
296         canid_t id;
297         uint8_t dreg;
298         int i;
299
300         netif_stop_queue(dev);
301
302         fi = dlc = cf->can_dlc;
303         id = cf->can_id;
304
305         if (id & CAN_RTR_FLAG)
306                 fi |= FI_RTR;
307
308         if (id & CAN_EFF_FLAG) {
309                 fi |= FI_FF;
310                 dreg = EFF_BUF;
311                 priv->write_reg(dev, REG_FI, fi);
312                 priv->write_reg(dev, REG_ID1, (id & 0x1fe00000) >> (5 + 16));
313                 priv->write_reg(dev, REG_ID2, (id & 0x001fe000) >> (5 + 8));
314                 priv->write_reg(dev, REG_ID3, (id & 0x00001fe0) >> 5);
315                 priv->write_reg(dev, REG_ID4, (id & 0x0000001f) << 3);
316         } else {
317                 dreg = SFF_BUF;
318                 priv->write_reg(dev, REG_FI, fi);
319                 priv->write_reg(dev, REG_ID1, (id & 0x000007f8) >> 3);
320                 priv->write_reg(dev, REG_ID2, (id & 0x00000007) << 5);
321         }
322
323         for (i = 0; i < dlc; i++)
324                 priv->write_reg(dev, dreg++, cf->data[i]);
325
326         stats->tx_bytes += dlc;
327         dev->trans_start = jiffies;
328
329         can_put_echo_skb(skb, dev, 0);
330
331         priv->write_reg(dev, REG_CMR, CMD_TR);
332
333         return 0;
334 }
335
336 static void sja1000_rx(struct net_device *dev)
337 {
338         struct sja1000_priv *priv = netdev_priv(dev);
339 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
340         struct net_device_stats *stats = can_get_stats(dev);
341 #else
342         struct net_device_stats *stats = &dev->stats;
343 #endif
344         struct can_frame *cf;
345         struct sk_buff *skb;
346         uint8_t fi;
347         uint8_t dreg;
348         canid_t id;
349         uint8_t dlc;
350         int i;
351
352         skb = dev_alloc_skb(sizeof(struct can_frame));
353         if (skb == NULL)
354                 return;
355         skb->dev = dev;
356         skb->protocol = htons(ETH_P_CAN);
357
358         fi = priv->read_reg(dev, REG_FI);
359         dlc = fi & 0x0F;
360
361         if (fi & FI_FF) {
362                 /* extended frame format (EFF) */
363                 dreg = EFF_BUF;
364                 id = (priv->read_reg(dev, REG_ID1) << (5 + 16))
365                     | (priv->read_reg(dev, REG_ID2) << (5 + 8))
366                     | (priv->read_reg(dev, REG_ID3) << 5)
367                     | (priv->read_reg(dev, REG_ID4) >> 3);
368                 id |= CAN_EFF_FLAG;
369         } else {
370                 /* standard frame format (SFF) */
371                 dreg = SFF_BUF;
372                 id = (priv->read_reg(dev, REG_ID1) << 3)
373                     | (priv->read_reg(dev, REG_ID2) >> 5);
374         }
375
376         if (fi & FI_RTR)
377                 id |= CAN_RTR_FLAG;
378
379         cf = (struct can_frame *)skb_put(skb, sizeof(struct can_frame));
380         memset(cf, 0, sizeof(struct can_frame));
381         cf->can_id = id;
382         cf->can_dlc = dlc;
383         for (i = 0; i < dlc; i++)
384                 cf->data[i] = priv->read_reg(dev, dreg++);
385
386         while (i < 8)
387                 cf->data[i++] = 0;
388
389         /* release receive buffer */
390         priv->write_reg(dev, REG_CMR, CMD_RRB);
391
392         netif_rx(skb);
393
394         dev->last_rx = jiffies;
395         stats->rx_packets++;
396         stats->rx_bytes += dlc;
397 }
398
399 static int sja1000_err(struct net_device *dev, uint8_t isrc, uint8_t status)
400 {
401         struct sja1000_priv *priv = netdev_priv(dev);
402 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
403         struct net_device_stats *stats = can_get_stats(dev);
404 #else
405         struct net_device_stats *stats = &dev->stats;
406 #endif
407         struct can_frame *cf;
408         struct sk_buff *skb;
409         enum can_state state = priv->can.state;
410         uint8_t ecc, alc;
411
412         skb = dev_alloc_skb(sizeof(struct can_frame));
413         if (skb == NULL)
414                 return -ENOMEM;
415         skb->dev = dev;
416         skb->protocol = htons(ETH_P_CAN);
417         cf = (struct can_frame *)skb_put(skb, sizeof(struct can_frame));
418         memset(cf, 0, sizeof(struct can_frame));
419         cf->can_id = CAN_ERR_FLAG;
420         cf->can_dlc = CAN_ERR_DLC;
421
422         if (isrc & IRQ_DOI) {
423                 /* data overrun interrupt */
424                 dev_dbg(ND2D(dev), "data overrun interrupt\n");
425                 cf->can_id |= CAN_ERR_CRTL;
426                 cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
427                 priv->can.can_stats.data_overrun++;
428                 priv->write_reg(dev, REG_CMR, CMD_CDO); /* clear bit */
429         }
430
431         if (isrc & IRQ_EI) {
432                 /* error warning interrupt */
433                 priv->can.can_stats.error_warning++;
434                 dev_dbg(ND2D(dev), "error warning interrupt\n");
435
436                 if (status & SR_BS) {
437                         state = CAN_STATE_BUS_OFF;
438                         cf->can_id |= CAN_ERR_BUSOFF;
439                         can_bus_off(dev);
440                 } else if (status & SR_ES) {
441                         state = CAN_STATE_BUS_WARNING;
442                 } else
443                         state = CAN_STATE_ACTIVE;
444         }
445         if (isrc & IRQ_BEI) {
446                 /* bus error interrupt */
447                 priv->can.can_stats.bus_error++;
448                 ecc = priv->read_reg(dev, REG_ECC);
449
450                 cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
451
452                 switch (ecc & ECC_MASK) {
453                 case ECC_BIT:
454                         cf->data[2] |= CAN_ERR_PROT_BIT;
455                         break;
456                 case ECC_FORM:
457                         cf->data[2] |= CAN_ERR_PROT_FORM;
458                         break;
459                 case ECC_STUFF:
460                         cf->data[2] |= CAN_ERR_PROT_STUFF;
461                         break;
462                 default:
463                         cf->data[2] |= CAN_ERR_PROT_UNSPEC;
464                         cf->data[3] = ecc & ECC_SEG;
465                         break;
466                 }
467                 /* Error occured during transmission? */
468                 if ((ecc & ECC_DIR) == 0)
469                         cf->data[2] |= CAN_ERR_PROT_TX;
470         }
471         if (isrc & IRQ_EPI) {
472                 /* error passive interrupt */
473                 dev_dbg(ND2D(dev), "error passive interrupt\n");
474                 priv->can.can_stats.error_passive++;
475                 if (status & SR_ES)
476                         state = CAN_STATE_BUS_PASSIVE;
477                 else
478                         state = CAN_STATE_ACTIVE;
479         }
480         if (isrc & IRQ_ALI) {
481                 /* arbitration lost interrupt */
482                 dev_dbg(ND2D(dev), "arbitration lost interrupt\n");
483                 alc = priv->read_reg(dev, REG_ALC);
484                 priv->can.can_stats.arbitration_lost++;
485                 cf->can_id |= CAN_ERR_LOSTARB;
486                 cf->data[0] = alc & 0x1f;
487         }
488
489         if (state != priv->can.state && (state == CAN_STATE_BUS_WARNING ||
490                                          state == CAN_STATE_BUS_PASSIVE)) {
491                 uint8_t rxerr = priv->read_reg(dev, REG_RXERR);
492                 uint8_t txerr = priv->read_reg(dev, REG_TXERR);
493                 cf->can_id |= CAN_ERR_CRTL;
494                 if (state == CAN_STATE_BUS_WARNING)
495                         cf->data[1] = (txerr > rxerr) ?
496                                 CAN_ERR_CRTL_TX_WARNING :
497                                 CAN_ERR_CRTL_RX_WARNING;
498                 else
499                         cf->data[1] = (txerr > rxerr) ?
500                                 CAN_ERR_CRTL_TX_PASSIVE :
501                                 CAN_ERR_CRTL_RX_PASSIVE;
502         }
503
504         priv->can.state = state;
505
506         netif_rx(skb);
507
508         dev->last_rx = jiffies;
509         stats->rx_packets++;
510         stats->rx_bytes += cf->can_dlc;
511
512         return 0;
513 }
514
515 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
516 irqreturn_t sja1000_interrupt(int irq, void *dev_id, struct pt_regs *regs)
517 #else
518 irqreturn_t sja1000_interrupt(int irq, void *dev_id)
519 #endif
520 {
521         struct net_device *dev = (struct net_device *)dev_id;
522         struct sja1000_priv *priv = netdev_priv(dev);
523 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
524         struct net_device_stats *stats = can_get_stats(dev);
525 #else
526         struct net_device_stats *stats = &dev->stats;
527 #endif
528         uint8_t isrc, status;
529         int n = 0;
530
531         /* Shared interrupts and IRQ off? */
532         if (priv->read_reg(dev, REG_IER) == IRQ_OFF)
533                 return IRQ_NONE;
534
535         if (priv->pre_irq)
536                 priv->pre_irq(dev);
537
538         while ((isrc = priv->read_reg(dev, REG_IR)) && (n < SJA1000_MAX_IRQ)) {
539                 n++;
540                 status = priv->read_reg(dev, REG_SR);
541
542                 if (isrc & IRQ_WUI) {
543                         /* wake-up interrupt */
544                         priv->can.can_stats.wakeup++;
545                 }
546                 if (isrc & IRQ_TI) {
547                         /* transmission complete interrupt */
548                         stats->tx_packets++;
549                         can_get_echo_skb(dev, 0);
550                         netif_wake_queue(dev);
551                 }
552                 if (isrc & IRQ_RI) {
553                         /* receive interrupt */
554                         while (status & SR_RBS) {
555                                 sja1000_rx(dev);
556                                 status = priv->read_reg(dev, REG_SR);
557                         }
558                 }
559                 if (isrc & (IRQ_DOI | IRQ_EI | IRQ_BEI | IRQ_EPI | IRQ_ALI)) {
560                         /* error interrupt */
561                         if (sja1000_err(dev, isrc, status))
562                                 break;
563                 }
564         }
565
566         if (priv->post_irq)
567                 priv->post_irq(dev);
568
569         if (n >= SJA1000_MAX_IRQ)
570                 dev_dbg(ND2D(dev), "%d messages handled in ISR", n);
571
572         return (n) ? IRQ_HANDLED : IRQ_NONE;
573 }
574 EXPORT_SYMBOL_GPL(sja1000_interrupt);
575
576 static int sja1000_open(struct net_device *dev)
577 {
578         struct sja1000_priv *priv = netdev_priv(dev);
579         int err;
580
581         /* set chip into reset mode */
582         set_reset_mode(dev);
583
584         /* determine and set bittime */
585         err = can_set_bittiming(dev);
586         if (err)
587                 return err;
588
589         /* register interrupt handler, if not done by the device driver */
590         if (!(priv->flags & SJA1000_CUSTOM_IRQ_HANDLER)) {
591 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18)
592                 err = request_irq(dev->irq, &sja1000_interrupt, SA_SHIRQ,
593                                   dev->name, (void *)dev);
594 #else
595                 err = request_irq(dev->irq, &sja1000_interrupt, IRQF_SHARED,
596                                   dev->name, (void *)dev);
597 #endif
598                 if (err)
599                         return -EAGAIN;
600         }
601
602 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
603         /* clear statistics */
604         memset(&priv->can.net_stats, 0, sizeof(priv->can.net_stats));
605 #endif
606
607         /* init and start chi */
608         sja1000_start(dev);
609         priv->open_time = jiffies;
610
611         netif_start_queue(dev);
612
613         return 0;
614 }
615
616 static int sja1000_close(struct net_device *dev)
617 {
618         struct sja1000_priv *priv = netdev_priv(dev);
619
620         set_reset_mode(dev);
621         netif_stop_queue(dev);
622         priv->open_time = 0;
623         can_close_cleanup(dev);
624
625         if (!(priv->flags & SJA1000_CUSTOM_IRQ_HANDLER))
626                 free_irq(dev->irq, (void *)dev);
627
628         return 0;
629 }
630
631 struct net_device *alloc_sja1000dev(int sizeof_priv)
632 {
633         struct net_device *dev;
634         struct sja1000_priv *priv;
635
636         dev = alloc_candev(sizeof(struct sja1000_priv) + sizeof_priv);
637         if (!dev)
638                 return NULL;
639
640         priv = netdev_priv(dev);
641         priv->dev = dev;
642
643         if (sizeof_priv)
644                 priv->priv = (void *)priv + sizeof(struct sja1000_priv);
645
646         return dev;
647 }
648 EXPORT_SYMBOL_GPL(alloc_sja1000dev);
649
650 void free_sja1000dev(struct net_device *dev)
651 {
652         free_candev(dev);
653 }
654 EXPORT_SYMBOL_GPL(free_sja1000dev);
655
656 int register_sja1000dev(struct net_device *dev)
657 {
658         struct sja1000_priv *priv = netdev_priv(dev);
659         int err;
660
661         if (!sja1000_probe_chip(dev))
662                 return -ENODEV;
663
664         dev->flags |= IFF_ECHO; /* we support local echo */
665
666         dev->open = sja1000_open;
667         dev->stop = sja1000_close;
668
669         dev->hard_start_xmit = sja1000_start_xmit;
670
671         priv->can.bittiming_const = &sja1000_bittiming_const;
672         priv->can.do_set_bittiming = sja1000_set_bittiming;
673         priv->can.do_get_state = sja1000_get_state;
674         priv->can.do_set_mode = sja1000_set_mode;
675         priv->dev = dev;
676
677         err = register_candev(dev);
678         if (err) {
679                 printk(KERN_INFO
680                        "%s: registering netdev failed\n", DRV_NAME);
681                 free_netdev(dev);
682                 return err;
683         }
684
685         set_reset_mode(dev);
686         chipset_init(dev);
687         return 0;
688 }
689 EXPORT_SYMBOL_GPL(register_sja1000dev);
690
691 void unregister_sja1000dev(struct net_device *dev)
692 {
693         set_reset_mode(dev);
694         unregister_candev(dev);
695 }
696 EXPORT_SYMBOL_GPL(unregister_sja1000dev);
697
698 static __init int sja1000_init(void)
699 {
700         printk(KERN_INFO "%s CAN netdevice driver\n", DRV_NAME);
701
702         return 0;
703 }
704
705 module_init(sja1000_init);
706
707 static __exit void sja1000_exit(void)
708 {
709         printk(KERN_INFO "%s: driver removed\n", DRV_NAME);
710 }
711
712 module_exit(sja1000_exit);