]> rtime.felk.cvut.cz Git - socketcan-devel.git/blob - kernel/2.6/drivers/net/can/sja1000/sja1000.c
d63b73630d24d8bc6192ebfe78e049b467b30428
[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 #ifdef CONFIG_CAN_DEBUG_DEVICES
81 #define DBG(args...)   ((debug > 0) ? printk(args) : 0)
82 /* logging in interrupt context! */
83 #define iDBG(args...)  ((debug > 1) ? printk(args) : 0)
84 #define iiDBG(args...) ((debug > 2) ? printk(args) : 0)
85 #else
86 #define DBG(args...)
87 #define iDBG(args...)
88 #define iiDBG(args...)
89 #endif
90
91 #ifdef CONFIG_CAN_DEBUG_DEVICES
92 static const char *ecc_errors[] = {
93         NULL,
94         NULL,
95         "ID.28 to ID.28",
96         "start of frame",
97         "bit SRTR",
98         "bit IDE",
99         "ID.20 to ID.18",
100         "ID.17 to ID.13",
101         "CRC sequence",
102         "reserved bit 0",
103         "data field",
104         "data length code",
105         "bit RTR",
106         "reserved bit 1",
107         "ID.4 to ID.0",
108         "ID.12 to ID.5",
109         NULL,
110         "active error flag",
111         "intermission",
112         "tolerate dominant bits",
113         NULL,
114         NULL,
115         "passive error flag",
116         "error delimiter",
117         "CRC delimiter",
118         "acknowledge slot",
119         "end of frame",
120         "acknowledge delimiter",
121         "overload flag",
122         NULL,
123         NULL,
124         NULL
125 };
126
127 static const char *ecc_types[] = {
128         "bit error",
129         "form error",
130         "stuff error",
131         "other type of error"
132 };
133 #endif
134
135 static struct can_bittiming_const sja1000_bittiming_const = {
136         .tseg1_min = 1,
137         .tseg1_max = 16,
138         .tseg2_min = 1,
139         .tseg2_max = 8,
140         .sjw_max = 4,
141         .brp_min = 1,
142         .brp_max = 64,
143         .brp_inc = 1,
144 };
145
146 static int debug;
147
148 module_param(debug, int, S_IRUGO | S_IWUSR);
149
150 MODULE_PARM_DESC(debug, "Set debug mask (default: 0)");
151
152 static int sja1000_probe_chip(struct net_device *dev)
153 {
154         struct sja1000_priv *priv = netdev_priv(dev);
155
156         if (dev->base_addr && (priv->read_reg(dev, 0) == 0xFF)) {
157                 printk(KERN_INFO "%s: probing @0x%lX failed\n",
158                        DRV_NAME, dev->base_addr);
159                 return 0;
160         }
161         return 1;
162 }
163
164 int set_reset_mode(struct net_device *dev)
165 {
166         struct sja1000_priv *priv = netdev_priv(dev);
167         unsigned char status = priv->read_reg(dev, REG_MOD);
168         int i;
169
170         /* disable interrupts */
171         priv->write_reg(dev, REG_IER, IRQ_OFF);
172
173         for (i = 0; i < 100; i++) {
174                 /* check reset bit */
175                 if (status & MOD_RM) {
176                         if (i > 1) {
177                                 iDBG(KERN_INFO "%s: %s looped %d times\n",
178                                      dev->name, __func__, i);
179                         }
180                         priv->can.state = CAN_STATE_STOPPED;
181                         return 0;
182                 }
183
184                 priv->write_reg(dev, REG_MOD, MOD_RM);  /* reset chip */
185                 status = priv->read_reg(dev, REG_MOD);
186                 udelay(10);
187         }
188
189         dev_err(ND2D(dev), "setting SJA1000 into reset mode failed!\n");
190         return 1;
191
192 }
193
194 static int set_normal_mode(struct net_device *dev)
195 {
196         struct sja1000_priv *priv = netdev_priv(dev);
197         unsigned char status = priv->read_reg(dev, REG_MOD);
198         int i;
199
200         for (i = 0; i < 100; i++) {
201                 /* check reset bit */
202                 if ((status & MOD_RM) == 0) {
203 #ifdef CONFIG_CAN_DEBUG_DEVICES
204                         if (i > 1) {
205                                 iDBG(KERN_INFO "%s: %s looped %d times\n",
206                                      dev->name, __func__, i);
207                         }
208 #endif
209                         priv->can.state = CAN_STATE_ACTIVE;
210                         /* enable all interrupts */
211                         priv->write_reg(dev, REG_IER, IRQ_ALL);
212
213                         return 0;
214                 }
215
216                 /* set chip to normal mode */
217                 priv->write_reg(dev, REG_MOD, 0x00);
218                 status = priv->read_reg(dev, REG_MOD);
219                 udelay(10);
220         }
221
222         dev_err(ND2D(dev), "setting SJA1000 into normal mode failed!\n");
223         return 1;
224
225 }
226
227 static void sja1000_start(struct net_device *dev)
228 {
229         struct sja1000_priv *priv = netdev_priv(dev);
230
231         iDBG(KERN_INFO "%s: %s()\n", dev->name, __func__);
232
233         /* leave reset mode */
234         if (priv->can.state != CAN_STATE_STOPPED)
235                 set_reset_mode(dev);
236
237         /* Clear error counters and error code capture */
238         priv->write_reg(dev, REG_TXERR, 0x0);
239         priv->write_reg(dev, REG_RXERR, 0x0);
240         priv->read_reg(dev, REG_ECC);
241
242         /* leave reset mode */
243         set_normal_mode(dev);
244 }
245
246 static int sja1000_set_mode(struct net_device *dev, enum can_mode mode)
247 {
248         struct sja1000_priv *priv = netdev_priv(dev);
249
250         switch (mode) {
251         case CAN_MODE_START:
252                 DBG("%s: CAN_MODE_START requested\n", __func__);
253                 if (!priv->open_time)
254                         return -EINVAL;
255
256                 sja1000_start(dev);
257                 if (netif_queue_stopped(dev))
258                         netif_wake_queue(dev);
259                 break;
260
261         default:
262                 return -EOPNOTSUPP;
263         }
264
265         return 0;
266 }
267
268 static int sja1000_get_state(struct net_device *dev, enum can_state *state)
269 {
270         struct sja1000_priv *priv = netdev_priv(dev);
271         u8 status;
272
273         /* FIXME: inspecting the status register to get the current state
274          * is not really necessary, because state changes are handled by
275          * in the ISR and the variable priv->can.state gets updated. The
276          * CAN devicde interface needs fixing!
277          */
278
279         spin_lock_irq(&priv->can.irq_lock);
280
281         if (priv->can.state == CAN_STATE_STOPPED) {
282                 *state =  CAN_STATE_STOPPED;
283         } else {
284                 status = priv->read_reg(dev, REG_SR);
285                 if (status & SR_BS)
286                         *state = CAN_STATE_BUS_OFF;
287                 else if (status & SR_ES) {
288                         if (priv->read_reg(dev, REG_TXERR) > 127 ||
289                             priv->read_reg(dev, REG_RXERR) > 127)
290                                 *state = CAN_STATE_BUS_PASSIVE;
291                         else
292                                 *state = CAN_STATE_BUS_WARNING;
293                 } else
294                         *state = CAN_STATE_ACTIVE;
295         }
296 #ifdef CONFIG_CAN_DEBUG_DEVICES
297         /* Check state */
298         if (*state != priv->can.state)
299                 dev_err(ND2D(dev),
300                         "Oops, state mismatch: hard %d != soft %d\n",
301                         *state, priv->can.state);
302 #endif
303         spin_unlock_irq(&priv->can.irq_lock);
304         return 0;
305 }
306
307 static int sja1000_set_bittiming(struct net_device *dev)
308 {
309         struct sja1000_priv *priv = netdev_priv(dev);
310         struct can_bittiming *bt = &priv->can.bittiming;
311         u8 btr0, btr1;
312
313         btr0 = ((bt->brp - 1) & 0x3f) | (((bt->sjw - 1) & 0x3) << 6);
314         btr1 = ((bt->prop_seg + bt->phase_seg1 - 1) & 0xf) |
315                 (((bt->phase_seg2 - 1) & 0x7) << 4) |
316           ((priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES) << 7);
317
318         dev_info(ND2D(dev), "BTR0=0x%02x BTR1=0x%02x\n", btr0, btr1);
319
320         priv->write_reg(dev, REG_BTR0, btr0);
321         priv->write_reg(dev, REG_BTR1, btr1);
322
323         return 0;
324 }
325
326 /*
327  * initialize SJA1000 chip:
328  *   - reset chip
329  *   - set output mode
330  *   - set baudrate
331  *   - enable interrupts
332  *   - start operating mode
333  */
334 static void chipset_init(struct net_device *dev)
335 {
336         struct sja1000_priv *priv = netdev_priv(dev);
337
338         /* set clock divider and output control register */
339         priv->write_reg(dev, REG_CDR, priv->cdr | CDR_PELICAN);
340
341         /* set acceptance filter (accept all) */
342         priv->write_reg(dev, REG_ACCC0, 0x00);
343         priv->write_reg(dev, REG_ACCC1, 0x00);
344         priv->write_reg(dev, REG_ACCC2, 0x00);
345         priv->write_reg(dev, REG_ACCC3, 0x00);
346
347         priv->write_reg(dev, REG_ACCM0, 0xFF);
348         priv->write_reg(dev, REG_ACCM1, 0xFF);
349         priv->write_reg(dev, REG_ACCM2, 0xFF);
350         priv->write_reg(dev, REG_ACCM3, 0xFF);
351
352         priv->write_reg(dev, REG_OCR, priv->ocr | OCR_MODE_NORMAL);
353 }
354
355 /*
356  * transmit a CAN message
357  * message layout in the sk_buff should be like this:
358  * xx xx xx xx   ff      ll   00 11 22 33 44 55 66 77
359  * [  can-id ] [flags] [len] [can data (up to 8 bytes]
360  */
361 static int sja1000_start_xmit(struct sk_buff *skb, struct net_device *dev)
362 {
363         struct sja1000_priv *priv = netdev_priv(dev);
364 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
365         struct net_device_stats *stats = can_get_stats(dev);
366 #else
367         struct net_device_stats *stats = &dev->stats;
368 #endif
369         struct can_frame *cf = (struct can_frame *)skb->data;
370         uint8_t fi;
371         uint8_t dlc;
372         canid_t id;
373         uint8_t dreg;
374         int i;
375
376         netif_stop_queue(dev);
377
378         fi = dlc = cf->can_dlc;
379         id = cf->can_id;
380
381         if (id & CAN_RTR_FLAG)
382                 fi |= FI_RTR;
383
384         if (id & CAN_EFF_FLAG) {
385                 fi |= FI_FF;
386                 dreg = EFF_BUF;
387                 priv->write_reg(dev, REG_FI, fi);
388                 priv->write_reg(dev, REG_ID1, (id & 0x1fe00000) >> (5 + 16));
389                 priv->write_reg(dev, REG_ID2, (id & 0x001fe000) >> (5 + 8));
390                 priv->write_reg(dev, REG_ID3, (id & 0x00001fe0) >> 5);
391                 priv->write_reg(dev, REG_ID4, (id & 0x0000001f) << 3);
392         } else {
393                 dreg = SFF_BUF;
394                 priv->write_reg(dev, REG_FI, fi);
395                 priv->write_reg(dev, REG_ID1, (id & 0x000007f8) >> 3);
396                 priv->write_reg(dev, REG_ID2, (id & 0x00000007) << 5);
397         }
398
399         for (i = 0; i < dlc; i++)
400                 priv->write_reg(dev, dreg++, cf->data[i]);
401
402         stats->tx_bytes += dlc;
403         dev->trans_start = jiffies;
404
405         can_put_echo_skb(skb, dev, 0);
406
407         priv->write_reg(dev, REG_CMR, CMD_TR);
408
409         return 0;
410 }
411
412 static void sja1000_rx(struct net_device *dev)
413 {
414         struct sja1000_priv *priv = netdev_priv(dev);
415 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
416         struct net_device_stats *stats = can_get_stats(dev);
417 #else
418         struct net_device_stats *stats = &dev->stats;
419 #endif
420         struct can_frame *cf;
421         struct sk_buff *skb;
422         uint8_t fi;
423         uint8_t dreg;
424         canid_t id;
425         uint8_t dlc;
426         int i;
427
428         skb = dev_alloc_skb(sizeof(struct can_frame));
429         if (skb == NULL)
430                 return;
431         skb->dev = dev;
432         skb->protocol = htons(ETH_P_CAN);
433
434         fi = priv->read_reg(dev, REG_FI);
435         dlc = fi & 0x0F;
436
437         if (fi & FI_FF) {
438                 /* extended frame format (EFF) */
439                 dreg = EFF_BUF;
440                 id = (priv->read_reg(dev, REG_ID1) << (5 + 16))
441                     | (priv->read_reg(dev, REG_ID2) << (5 + 8))
442                     | (priv->read_reg(dev, REG_ID3) << 5)
443                     | (priv->read_reg(dev, REG_ID4) >> 3);
444                 id |= CAN_EFF_FLAG;
445         } else {
446                 /* standard frame format (SFF) */
447                 dreg = SFF_BUF;
448                 id = (priv->read_reg(dev, REG_ID1) << 3)
449                     | (priv->read_reg(dev, REG_ID2) >> 5);
450         }
451
452         if (fi & FI_RTR)
453                 id |= CAN_RTR_FLAG;
454
455         cf = (struct can_frame *)skb_put(skb, sizeof(struct can_frame));
456         memset(cf, 0, sizeof(struct can_frame));
457         cf->can_id = id;
458         cf->can_dlc = dlc;
459         for (i = 0; i < dlc; i++)
460                 cf->data[i] = priv->read_reg(dev, dreg++);
461
462         while (i < 8)
463                 cf->data[i++] = 0;
464
465         /* release receive buffer */
466         priv->write_reg(dev, REG_CMR, CMD_RRB);
467
468         netif_rx(skb);
469
470         dev->last_rx = jiffies;
471         stats->rx_packets++;
472         stats->rx_bytes += dlc;
473 }
474
475 static int sja1000_err(struct net_device *dev,
476                        uint8_t isrc, uint8_t status, int n)
477 {
478         struct sja1000_priv *priv = netdev_priv(dev);
479 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
480         struct net_device_stats *stats = can_get_stats(dev);
481 #else
482         struct net_device_stats *stats = &dev->stats;
483 #endif
484         struct can_frame *cf;
485         struct sk_buff *skb;
486         enum can_state state = priv->can.state;
487         uint8_t ecc, alc;
488
489         skb = dev_alloc_skb(sizeof(struct can_frame));
490         if (skb == NULL)
491                 return -ENOMEM;
492         skb->dev = dev;
493         skb->protocol = htons(ETH_P_CAN);
494         cf = (struct can_frame *)skb_put(skb, sizeof(struct can_frame));
495         memset(cf, 0, sizeof(struct can_frame));
496         cf->can_id = CAN_ERR_FLAG;
497         cf->can_dlc = CAN_ERR_DLC;
498
499         if (isrc & IRQ_DOI) {
500                 /* data overrun interrupt */
501                 iiDBG(KERN_INFO "%s: data overrun isrc=0x%02X "
502                       "status=0x%02X\n", dev->name, isrc, status);
503                 iDBG(KERN_INFO "%s: DOI #%d#\n", dev->name, n);
504                 cf->can_id |= CAN_ERR_CRTL;
505                 cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
506                 priv->can.can_stats.data_overrun++;
507                 priv->write_reg(dev, REG_CMR, CMD_CDO); /* clear bit */
508         }
509
510         if (isrc & IRQ_EI) {
511                 /* error warning interrupt */
512                 iiDBG(KERN_INFO "%s: error warning isrc=0x%02X "
513                       "status=0x%02X\n", dev->name, isrc, status);
514                 iDBG(KERN_INFO "%s: EI #%d#\n", dev->name, n);
515                 priv->can.can_stats.error_warning++;
516
517                 if (status & SR_BS) {
518                         state = CAN_STATE_BUS_OFF;
519                         cf->can_id |= CAN_ERR_BUSOFF;
520                         can_bus_off(dev);
521                         iDBG(KERN_INFO "%s: BUS OFF\n", dev->name);
522                 } else if (status & SR_ES) {
523                         state = CAN_STATE_BUS_WARNING;
524                         iDBG(KERN_INFO "%s: error\n", dev->name);
525                 } else
526                         state = CAN_STATE_ACTIVE;
527         }
528         if (isrc & IRQ_BEI) {
529                 /* bus error interrupt */
530                 iiDBG(KERN_INFO "%s: bus error isrc=0x%02X "
531                       "status=0x%02X\n", dev->name, isrc, status);
532                 iDBG(KERN_INFO "%s: BEI #%d# [%d]\n", dev->name, n,
533                      priv->can.can_stats.bus_error);
534                 priv->can.can_stats.bus_error++;
535                 ecc = priv->read_reg(dev, REG_ECC);
536                 iDBG(KERN_INFO "%s: ECC = 0x%02X (%s, %s, %s)\n",
537                      dev->name, ecc,
538                      (ecc & ECC_DIR) ? "RX" : "TX",
539                      ecc_types[ecc >> ECC_ERR],
540                      ecc_errors[ecc & ECC_SEG]);
541
542                 cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
543
544                 switch (ecc & ECC_MASK) {
545                 case ECC_BIT:
546                         cf->data[2] |= CAN_ERR_PROT_BIT;
547                         break;
548                 case ECC_FORM:
549                         cf->data[2] |= CAN_ERR_PROT_FORM;
550                         break;
551                 case ECC_STUFF:
552                         cf->data[2] |= CAN_ERR_PROT_STUFF;
553                         break;
554                 default:
555                         cf->data[2] |= CAN_ERR_PROT_UNSPEC;
556                         cf->data[3] = ecc & ECC_SEG;
557                         break;
558                 }
559                 /* Error occured during transmission? */
560                 if ((ecc & ECC_DIR) == 0)
561                         cf->data[2] |= CAN_ERR_PROT_TX;
562         }
563         if (isrc & IRQ_EPI) {
564                 /* error passive interrupt */
565                 iiDBG(KERN_INFO "%s: error passive isrc=0x%02X"
566                       " status=0x%02X\n", dev->name, isrc, status);
567                 iDBG(KERN_INFO "%s: EPI #%d#\n", dev->name, n);
568                 priv->can.can_stats.error_passive++;
569                 if (status & SR_ES) {
570                         iDBG(KERN_INFO "%s: ERROR PASSIVE\n", dev->name);
571                         state = CAN_STATE_BUS_PASSIVE;
572                 } else {
573                         iDBG(KERN_INFO "%s: ERROR ACTIVE\n", dev->name);
574                         state = CAN_STATE_ACTIVE;
575                 }
576         }
577         if (isrc & IRQ_ALI) {
578                 /* arbitration lost interrupt */
579                 iiDBG(KERN_INFO "%s: error arbitration lost "
580                       "isrc=0x%02X status=0x%02X\n",
581                       dev->name, isrc, status);
582                 iDBG(KERN_INFO "%s: ALI #%d#\n", dev->name, n);
583                 alc = priv->read_reg(dev, REG_ALC);
584                 iDBG(KERN_INFO "%s: ALC = 0x%02X\n", dev->name, alc);
585                 priv->can.can_stats.arbitration_lost++;
586                 cf->can_id |= CAN_ERR_LOSTARB;
587                 cf->data[0] = alc & 0x1f;
588         }
589
590         if (state != priv->can.state && (state == CAN_STATE_BUS_WARNING ||
591                                          state == CAN_STATE_BUS_PASSIVE)) {
592                 uint8_t rxerr = priv->read_reg(dev, REG_RXERR);
593                 uint8_t txerr = priv->read_reg(dev, REG_TXERR);
594                 cf->can_id |= CAN_ERR_CRTL;
595                 if (state == CAN_STATE_BUS_WARNING)
596                         cf->data[1] = (txerr > rxerr) ?
597                                 CAN_ERR_CRTL_TX_WARNING :
598                                 CAN_ERR_CRTL_RX_WARNING;
599                 else
600                         cf->data[1] = (txerr > rxerr) ?
601                                 CAN_ERR_CRTL_TX_PASSIVE :
602                                 CAN_ERR_CRTL_RX_PASSIVE;
603         }
604
605         priv->can.state = state;
606
607         netif_rx(skb);
608
609         dev->last_rx = jiffies;
610         stats->rx_packets++;
611         stats->rx_bytes += cf->can_dlc;
612
613         return 0;
614 }
615
616 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
617 irqreturn_t sja1000_interrupt(int irq, void *dev_id, struct pt_regs *regs)
618 #else
619 irqreturn_t sja1000_interrupt(int irq, void *dev_id)
620 #endif
621 {
622         struct net_device *dev = (struct net_device *)dev_id;
623         struct sja1000_priv *priv = netdev_priv(dev);
624 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
625         struct net_device_stats *stats = can_get_stats(dev);
626 #else
627         struct net_device_stats *stats = &dev->stats;
628 #endif
629         uint8_t isrc, status;
630         int n = 0;
631
632         if (priv->pre_irq)
633                 priv->pre_irq(dev);
634
635         iiDBG(KERN_INFO "%s: interrupt\n", dev->name);
636
637         if (priv->can.state == CAN_STATE_STOPPED) {
638                 iiDBG(KERN_ERR "%s: %s: controller is in reset mode! "
639                       "MOD=0x%02X IER=0x%02X IR=0x%02X SR=0x%02X!\n",
640                       dev->name, __func__, priv->read_reg(dev, REG_MOD),
641                       priv->read_reg(dev, REG_IER), priv->read_reg(dev, REG_IR),
642                       priv->read_reg(dev, REG_SR));
643                 goto out;
644         }
645
646         while ((isrc = priv->read_reg(dev, REG_IR)) && (n < 20)) {
647                 n++;
648                 status = priv->read_reg(dev, REG_SR);
649
650                 if (isrc & IRQ_WUI) {
651                         /* wake-up interrupt */
652                         priv->can.can_stats.wakeup++;
653                 }
654                 if (isrc & IRQ_TI) {
655                         /* transmission complete interrupt */
656                         stats->tx_packets++;
657                         can_get_echo_skb(dev, 0);
658                         netif_wake_queue(dev);
659                 }
660                 if (isrc & IRQ_RI) {
661                         /* receive interrupt */
662                         while (status & SR_RBS) {
663                                 sja1000_rx(dev);
664                                 status = priv->read_reg(dev, REG_SR);
665                         }
666                 }
667                 if (isrc & (IRQ_DOI | IRQ_EI | IRQ_BEI | IRQ_EPI | IRQ_ALI)) {
668                         /* error interrupt */
669                         if (sja1000_err(dev, isrc, status, n))
670                                 break;
671                 }
672         }
673         if (n > 1)
674                 iDBG(KERN_INFO "%s: handled %d IRQs\n", dev->name, n);
675
676 out:
677         if (priv->post_irq)
678                 priv->post_irq(dev);
679
680         return (n) ? IRQ_HANDLED : IRQ_NONE;
681 }
682 EXPORT_SYMBOL_GPL(sja1000_interrupt);
683
684 static int sja1000_open(struct net_device *dev)
685 {
686         struct sja1000_priv *priv = netdev_priv(dev);
687         int err;
688
689         /* set chip into reset mode */
690         set_reset_mode(dev);
691
692         /* determine and set bittime */
693         err = can_set_bittiming(dev);
694         if (err)
695                 return err;
696
697         /* register interrupt handler, if not done by the device driver */
698         if (!(priv->flags & SJA1000_CUSTOM_IRQ_HANDLER)) {
699 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18)
700                 err = request_irq(dev->irq, &sja1000_interrupt, SA_SHIRQ,
701                                   dev->name, (void *)dev);
702 #else
703                 err = request_irq(dev->irq, &sja1000_interrupt, IRQF_SHARED,
704                                   dev->name, (void *)dev);
705 #endif
706                 if (err)
707                         return -EAGAIN;
708         }
709
710 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
711         /* clear statistics */
712         memset(&priv->can.net_stats, 0, sizeof(priv->can.net_stats));
713 #endif
714
715         /* init and start chi */
716         sja1000_start(dev);
717         priv->open_time = jiffies;
718
719         netif_start_queue(dev);
720
721         return 0;
722 }
723
724 static int sja1000_close(struct net_device *dev)
725 {
726         struct sja1000_priv *priv = netdev_priv(dev);
727
728         set_reset_mode(dev);
729         netif_stop_queue(dev);
730         priv->open_time = 0;
731         can_close_cleanup(dev);
732
733         if (!(priv->flags & SJA1000_CUSTOM_IRQ_HANDLER))
734                 free_irq(dev->irq, (void *)dev);
735
736         return 0;
737 }
738
739 struct net_device *alloc_sja1000dev(int sizeof_priv)
740 {
741         struct net_device *dev;
742         struct sja1000_priv *priv;
743
744         dev = alloc_candev(sizeof(struct sja1000_priv) + sizeof_priv);
745         if (!dev)
746                 return NULL;
747
748         priv = netdev_priv(dev);
749         priv->dev = dev;
750
751         if (sizeof_priv)
752                 priv->priv = (void *)priv + sizeof(struct sja1000_priv);
753
754         return dev;
755 }
756 EXPORT_SYMBOL_GPL(alloc_sja1000dev);
757
758 void free_sja1000dev(struct net_device *dev)
759 {
760         free_candev(dev);
761 }
762 EXPORT_SYMBOL(free_sja1000dev);
763
764 int register_sja1000dev(struct net_device *dev)
765 {
766         struct sja1000_priv *priv = netdev_priv(dev);
767         int err;
768
769         if (!sja1000_probe_chip(dev))
770                 return -ENODEV;
771
772         dev->flags |= IFF_ECHO; /* we support local echo */
773
774         dev->open = sja1000_open;
775         dev->stop = sja1000_close;
776
777         dev->hard_start_xmit = sja1000_start_xmit;
778
779         priv->can.bittiming_const = &sja1000_bittiming_const;
780         priv->can.do_set_bittiming = sja1000_set_bittiming;
781         priv->can.do_get_state = sja1000_get_state;
782         priv->can.do_set_mode = sja1000_set_mode;
783         priv->dev = dev;
784
785         err = register_candev(dev);
786         if (err) {
787                 printk(KERN_INFO
788                        "%s: registering netdev failed\n", DRV_NAME);
789                 free_netdev(dev);
790                 return err;
791         }
792
793         set_reset_mode(dev);
794         chipset_init(dev);
795         return 0;
796 }
797 EXPORT_SYMBOL(register_sja1000dev);
798
799 void unregister_sja1000dev(struct net_device *dev)
800 {
801         set_reset_mode(dev);
802         unregister_candev(dev);
803 }
804 EXPORT_SYMBOL(unregister_sja1000dev);
805
806 static __init int sja1000_init(void)
807 {
808         printk(KERN_INFO "%s CAN netdevice driver\n", DRV_NAME);
809
810         if (debug)
811                 printk(KERN_INFO "%s: debug level set to %d.\n",
812                        DRV_NAME, debug);
813
814         return 0;
815 }
816
817 module_init(sja1000_init);
818
819 static __exit void sja1000_exit(void)
820 {
821         printk(KERN_INFO "%s: driver removed\n", DRV_NAME);
822 }
823
824 module_exit(sja1000_exit);