]> rtime.felk.cvut.cz Git - socketcan-devel.git/blob - kernel/2.6/drivers/net/can/mscan/mscan.c
Renamed the GET_CAN_DLC() macro to lower-case get_can_dlc() and added some
[socketcan-devel.git] / kernel / 2.6 / drivers / net / can / mscan / mscan.c
1 /*
2  * CAN bus driver for the alone generic (as possible as) MSCAN controller.
3  *
4  * Copyright (C) 2005-2006 Andrey Volkov <avolkov@varma-el.com>,
5  *                         Varma Electronics Oy
6  * Copyright (C) 2008-2009 Wolfgang Grandegger <wg@grandegger.com>
7  * Copytight (C) 2008-2009 Pengutronix <kernel@pengutronix.de>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the version 2 of the GNU General Public License
11  * as published by the Free Software Foundation
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/interrupt.h>
26 #include <linux/delay.h>
27 #include <linux/netdevice.h>
28 #include <linux/if_arp.h>
29 #include <linux/if_ether.h>
30 #include <linux/list.h>
31 #include <socketcan/can.h>
32 #include <socketcan/can/dev.h>
33 #include <socketcan/can/error.h>
34 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
35 #include <linux/io.h>
36 #else
37 #include <asm/io.h>
38 #endif
39
40 #include "mscan.h"
41
42 #include <socketcan/can/version.h>      /* for RCSID. Removed by mkpatch script */
43 RCSID("$Id$");
44
45 static struct can_bittiming_const mscan_bittiming_const = {
46         .name = "mscan",
47         .tseg1_min = 4,
48         .tseg1_max = 16,
49         .tseg2_min = 2,
50         .tseg2_max = 8,
51         .sjw_max = 4,
52         .brp_min = 1,
53         .brp_max = 64,
54         .brp_inc = 1,
55 };
56
57 struct mscan_state {
58         u8 mode;
59         u8 canrier;
60         u8 cantier;
61 };
62
63 static enum can_state state_map[] = {
64         CAN_STATE_ERROR_ACTIVE,
65         CAN_STATE_ERROR_WARNING,
66         CAN_STATE_ERROR_PASSIVE,
67         CAN_STATE_BUS_OFF
68 };
69
70 static int mscan_set_mode(struct net_device *dev, u8 mode)
71 {
72         struct mscan_priv *priv = netdev_priv(dev);
73         struct mscan_regs *regs = (struct mscan_regs *)priv->reg_base;
74         int ret = 0;
75         int i;
76         u8 canctl1;
77
78         if (mode != MSCAN_NORMAL_MODE) {
79                 if (priv->tx_active) {
80                         /* Abort transfers before going to sleep */#
81                         out_8(&regs->cantarq, priv->tx_active);
82                         /* Suppress TX done interrupts */
83                         out_8(&regs->cantier, 0);
84                 }
85
86                 canctl1 = in_8(&regs->canctl1);
87                 if ((mode & MSCAN_SLPRQ) && !(canctl1 & MSCAN_SLPAK)) {
88                         setbits8(&regs->canctl0, MSCAN_SLPRQ);
89                         for (i = 0; i < MSCAN_SET_MODE_RETRIES; i++) {
90                                 if (in_8(&regs->canctl1) & MSCAN_SLPAK)
91                                         break;
92                                 udelay(100);
93                         }
94                         /*
95                          * The mscan controller will fail to enter sleep mode,
96                          * while there are irregular activities on bus, like
97                          * somebody keeps retransmitting. This behavior is
98                          * undocumented and seems to differ between mscan built
99                          * in mpc5200b and mpc5200. We proceed in that case,
100                          * since otherwise the slprq will be kept set and the
101                          * controller will get stuck. NOTE: INITRQ or CSWAI
102                          * will abort all active transmit actions, if still
103                          * any, at once.
104                          */
105                         if (i >= MSCAN_SET_MODE_RETRIES)
106                                 dev_dbg(ND2D(dev),
107                                         "device failed to enter sleep mode. "
108                                         "We proceed anyhow.\n");
109                         else
110                                 priv->can.state = CAN_STATE_SLEEPING;
111                 }
112
113                 if ((mode & MSCAN_INITRQ) && !(canctl1 & MSCAN_INITAK)) {
114                         setbits8(&regs->canctl0, MSCAN_INITRQ);
115                         for (i = 0; i < MSCAN_SET_MODE_RETRIES; i++) {
116                                 if (in_8(&regs->canctl1) & MSCAN_INITAK)
117                                         break;
118                         }
119                         if (i >= MSCAN_SET_MODE_RETRIES)
120                                 ret = -ENODEV;
121                 }
122                 if (!ret)
123                         priv->can.state = CAN_STATE_STOPPED;
124
125                 if (mode & MSCAN_CSWAI)
126                         setbits8(&regs->canctl0, MSCAN_CSWAI);
127
128         } else {
129                 canctl1 = in_8(&regs->canctl1);
130                 if (canctl1 & (MSCAN_SLPAK | MSCAN_INITAK)) {
131                         clrbits8(&regs->canctl0, MSCAN_SLPRQ | MSCAN_INITRQ);
132                         for (i = 0; i < MSCAN_SET_MODE_RETRIES; i++) {
133                                 canctl1 = in_8(&regs->canctl1);
134                                 if (!(canctl1 & (MSCAN_INITAK | MSCAN_SLPAK)))
135                                         break;
136                         }
137                         if (i >= MSCAN_SET_MODE_RETRIES)
138                                 ret = -ENODEV;
139                         else
140                                 priv->can.state = CAN_STATE_ERROR_ACTIVE;
141                 }
142         }
143         return ret;
144 }
145
146 static int mscan_start(struct net_device *dev)
147 {
148         struct mscan_priv *priv = netdev_priv(dev);
149         struct mscan_regs *regs = (struct mscan_regs *)priv->reg_base;
150         u8 canrflg;
151         int err;
152
153         out_8(&regs->canrier, 0);
154
155         INIT_LIST_HEAD(&priv->tx_head);
156         priv->prev_buf_id = 0;
157         priv->cur_pri = 0;
158         priv->tx_active = 0;
159         priv->shadow_canrier = 0;
160         priv->flags = 0;
161
162         err = mscan_set_mode(dev, MSCAN_NORMAL_MODE);
163         if (err)
164                 return err;
165
166         canrflg = in_8(&regs->canrflg);
167         priv->shadow_statflg = canrflg & MSCAN_STAT_MSK;
168         priv->can.state = state_map[max(MSCAN_STATE_RX(canrflg),
169                                     MSCAN_STATE_TX(canrflg))];
170         out_8(&regs->cantier, 0);
171
172         /* Enable receive interrupts. */
173         out_8(&regs->canrier, MSCAN_OVRIE | MSCAN_RXFIE | MSCAN_CSCIE |
174               MSCAN_RSTATE1 | MSCAN_RSTATE0 | MSCAN_TSTATE1 | MSCAN_TSTATE0);
175
176         return 0;
177 }
178
179 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,32)
180 static int mscan_start_xmit(struct sk_buff *skb, struct net_device *dev)
181 #else
182 static netdev_tx_t mscan_start_xmit(struct sk_buff *skb, struct net_device *dev)
183 #endif
184 {
185         struct can_frame *frame = (struct can_frame *)skb->data;
186         struct mscan_priv *priv = netdev_priv(dev);
187         struct mscan_regs *regs = (struct mscan_regs *)priv->reg_base;
188         int i, rtr, buf_id;
189         u32 can_id;
190
191         if (frame->can_dlc > 8)
192                 return -EINVAL;
193
194         out_8(&regs->cantier, 0);
195
196         i = ~priv->tx_active & MSCAN_TXE;
197         buf_id = ffs(i) - 1;
198         switch (hweight8(i)) {
199         case 0:
200                 netif_stop_queue(dev);
201                 dev_err(ND2D(dev), "Tx Ring full when queue awake!\n");
202                 return NETDEV_TX_BUSY;
203         case 1:
204                 /*
205                  * if buf_id < 3, then current frame will be send out of order,
206                  * since buffer with lower id have higher priority (hell..)
207                  */
208                 netif_stop_queue(dev);
209         case 2:
210                 if (buf_id < priv->prev_buf_id) {
211                         priv->cur_pri++;
212                         if (priv->cur_pri == 0xff) {
213                                 set_bit(F_TX_WAIT_ALL, &priv->flags);
214                                 netif_stop_queue(dev);
215                         }
216                 }
217                 set_bit(F_TX_PROGRESS, &priv->flags);
218                 break;
219         }
220         priv->prev_buf_id = buf_id;
221         out_8(&regs->cantbsel, i);
222
223         rtr = frame->can_id & CAN_RTR_FLAG;
224
225         /* RTR is always the lowest bit of interest, then IDs follow */
226         if (frame->can_id & CAN_EFF_FLAG) {
227                 can_id = (frame->can_id & CAN_EFF_MASK)
228                          << (MSCAN_EFF_RTR_SHIFT + 1);
229                 if (rtr)
230                         can_id |= 1 << MSCAN_EFF_RTR_SHIFT;
231                 out_be16(&regs->tx.idr3_2, can_id);
232
233                 can_id >>= 16;
234                 /* EFF_FLAGS are inbetween the IDs :( */
235                 can_id = (can_id & 0x7) | ((can_id << 2) & 0xffe0)
236                          | MSCAN_EFF_FLAGS;
237         } else {
238                 can_id = (frame->can_id & CAN_SFF_MASK)
239                          << (MSCAN_SFF_RTR_SHIFT + 1);
240                 if (rtr)
241                         can_id |= 1 << MSCAN_SFF_RTR_SHIFT;
242         }
243         out_be16(&regs->tx.idr1_0, can_id);
244
245         if (!rtr) {
246                 void __iomem *data = &regs->tx.dsr1_0;
247                 u16 *payload = (u16 *)frame->data;
248
249                 /* It is safe to write into dsr[dlc+1] */
250                 for (i = 0; i < (frame->can_dlc + 1) / 2; i++) {
251                         out_be16(data, *payload++);
252                         data += 2 + _MSCAN_RESERVED_DSR_SIZE;
253                 }
254         }
255
256         out_8(&regs->tx.dlr, frame->can_dlc);
257         out_8(&regs->tx.tbpr, priv->cur_pri);
258
259         /* Start transmission. */
260         out_8(&regs->cantflg, 1 << buf_id);
261
262         if (!test_bit(F_TX_PROGRESS, &priv->flags))
263                 dev->trans_start = jiffies;
264
265         list_add_tail(&priv->tx_queue[buf_id].list, &priv->tx_head);
266
267         can_put_echo_skb(skb, dev, buf_id);
268
269         /* Enable interrupt. */
270         priv->tx_active |= 1 << buf_id;
271         out_8(&regs->cantier, priv->tx_active);
272
273         return NETDEV_TX_OK;
274 }
275
276 /* This function returns the old state to see where we came from */
277 static enum can_state check_set_state(struct net_device *dev, u8 canrflg)
278 {
279         struct mscan_priv *priv = netdev_priv(dev);
280         enum can_state state, old_state = priv->can.state;
281
282         if (canrflg & MSCAN_CSCIF && old_state <= CAN_STATE_BUS_OFF) {
283                 state = state_map[max(MSCAN_STATE_RX(canrflg),
284                                       MSCAN_STATE_TX(canrflg))];
285                 priv->can.state = state;
286         }
287         return old_state;
288 }
289
290 static void mscan_get_rx_frame(struct net_device *dev, struct can_frame *frame)
291 {
292         struct mscan_priv *priv = netdev_priv(dev);
293         struct mscan_regs *regs = (struct mscan_regs *)priv->reg_base;
294         u32 can_id;
295         int i;
296
297         can_id = in_be16(&regs->rx.idr1_0);
298         if (can_id & (1 << 3)) {
299                 frame->can_id = CAN_EFF_FLAG;
300                 can_id = ((can_id << 16) | in_be16(&regs->rx.idr3_2));
301                 can_id = ((can_id & 0xffe00000) |
302                           ((can_id & 0x7ffff) << 2)) >> 2;
303         } else {
304                 can_id >>= 4;
305                 frame->can_id = 0;
306         }
307
308         frame->can_id |= can_id >> 1;
309         if (can_id & 1)
310                 frame->can_id |= CAN_RTR_FLAG;
311
312         frame->can_dlc = get_can_dlc(in_8(&regs->rx.dlr) & 0xf);
313
314         if (!(frame->can_id & CAN_RTR_FLAG)) {
315                 void __iomem *data = &regs->rx.dsr1_0;
316                 u16 *payload = (u16 *)frame->data;
317
318                 for (i = 0; i < (frame->can_dlc + 1) / 2; i++) {
319                         *payload++ = in_be16(data);
320                         data += 2 + _MSCAN_RESERVED_DSR_SIZE;
321                 }
322         }
323
324         out_8(&regs->canrflg, MSCAN_RXF);
325 }
326
327 static void mscan_get_err_frame(struct net_device *dev, struct can_frame *frame,
328                                 u8 canrflg)
329 {
330         struct mscan_priv *priv = netdev_priv(dev);
331         struct mscan_regs *regs = (struct mscan_regs *)priv->reg_base;
332 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
333         struct net_device_stats *stats = can_get_stats(dev);
334 #else
335         struct net_device_stats *stats = &dev->stats;
336 #endif
337         enum can_state old_state;
338
339         dev_dbg(ND2D(dev), "error interrupt (canrflg=%#x)\n", canrflg);
340         frame->can_id = CAN_ERR_FLAG;
341
342         if (canrflg & MSCAN_OVRIF) {
343                 frame->can_id |= CAN_ERR_CRTL;
344                 frame->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
345                 stats->rx_over_errors++;
346                 stats->rx_errors++;
347         } else {
348                 frame->data[1] = 0;
349         }
350
351         old_state = check_set_state(dev, canrflg);
352         /* State changed */
353         if (old_state != priv->can.state) {
354                 switch (priv->can.state) {
355                 case CAN_STATE_ERROR_WARNING:
356                         frame->can_id |= CAN_ERR_CRTL;
357                         priv->can.can_stats.error_warning++;
358                         if ((priv->shadow_statflg & MSCAN_RSTAT_MSK) <
359                             (canrflg & MSCAN_RSTAT_MSK))
360                                 frame->data[1] |= CAN_ERR_CRTL_RX_WARNING;
361                         if ((priv->shadow_statflg & MSCAN_TSTAT_MSK) <
362                             (canrflg & MSCAN_TSTAT_MSK))
363                                 frame->data[1] |= CAN_ERR_CRTL_TX_WARNING;
364                         break;
365                 case CAN_STATE_ERROR_PASSIVE:
366                         frame->can_id |= CAN_ERR_CRTL;
367                         priv->can.can_stats.error_passive++;
368                         frame->data[1] |= CAN_ERR_CRTL_RX_PASSIVE;
369                         break;
370                 case CAN_STATE_BUS_OFF:
371                         frame->can_id |= CAN_ERR_BUSOFF;
372                         /*
373                          * The MSCAN on the MPC5200 does recover from bus-off
374                          * automatically. To avoid that we stop the chip doing
375                          * a light-weight stop (we are in irq-context).
376                          */
377                         out_8(&regs->cantier, 0);
378                         out_8(&regs->canrier, 0);
379                         setbits8(&regs->canctl0, MSCAN_SLPRQ | MSCAN_INITRQ);
380                         can_bus_off(dev);
381                         break;
382                 default:
383                         break;
384                 }
385         }
386         priv->shadow_statflg = canrflg & MSCAN_STAT_MSK;
387         frame->can_dlc = CAN_ERR_DLC;
388         out_8(&regs->canrflg, MSCAN_ERR_IF);
389 }
390
391 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
392 static int mscan_rx_poll(struct napi_struct *napi, int quota)
393 #else
394 static int mscan_rx_poll(struct net_device *dev, int *budget)
395 #endif
396 {
397 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,28)
398         struct mscan_priv *priv = container_of(napi, struct mscan_priv, napi);
399         struct net_device *dev = napi->dev;
400 #elif LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
401         struct mscan_priv *priv = container_of(napi, struct mscan_priv, napi);
402         struct net_device *dev = priv->dev;
403 #else
404         struct mscan_priv *priv = netdev_priv(dev);
405         int quota = min(dev->quota, *budget);
406 #endif
407         struct mscan_regs *regs = (struct mscan_regs *)priv->reg_base;
408 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
409         struct net_device_stats *stats = can_get_stats(dev);
410 #else
411         struct net_device_stats *stats = &dev->stats;
412 #endif
413         int npackets = 0;
414         int ret = 1;
415         struct sk_buff *skb;
416         struct can_frame *frame;
417         u8 canrflg;
418
419         while (npackets < quota) {
420                 canrflg = in_8(&regs->canrflg);
421                 if (!(canrflg & (MSCAN_RXF | MSCAN_ERR_IF)))
422                         break;
423
424                 skb = alloc_can_skb(dev, &frame);
425                 if (!skb) {
426                         if (printk_ratelimit())
427                                 dev_notice(ND2D(dev), "packet dropped\n");
428                         stats->rx_dropped++;
429                         out_8(&regs->canrflg, canrflg);
430                         continue;
431                 }
432
433                 if (canrflg & MSCAN_RXF)
434                         mscan_get_rx_frame(dev, frame);
435                 else if (canrflg & MSCAN_ERR_IF)
436                         mscan_get_err_frame(dev, frame, canrflg);
437
438 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,32)
439                 dev->last_rx = jiffies;
440 #endif
441                 stats->rx_packets++;
442                 stats->rx_bytes += frame->can_dlc;
443                 npackets++;
444                 netif_receive_skb(skb);
445         }
446
447 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
448         *budget -= npackets;
449         dev->quota -= npackets;
450 #endif
451
452         if (!(in_8(&regs->canrflg) & (MSCAN_RXF | MSCAN_ERR_IF))) {
453 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,28)
454                 napi_complete(&priv->napi);
455 #elif LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
456                 netif_rx_complete(dev, &priv->napi);
457 #else
458                 netif_rx_complete(dev);
459 #endif
460                 clear_bit(F_RX_PROGRESS, &priv->flags);
461                 if (priv->can.state < CAN_STATE_BUS_OFF)
462                         out_8(&regs->canrier, priv->shadow_canrier);
463                 ret = 0;
464         }
465         return ret;
466 }
467
468 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
469 static irqreturn_t mscan_isr(int irq, void *dev_id, struct pt_regs *r)
470 #else
471 static irqreturn_t mscan_isr(int irq, void *dev_id)
472 #endif
473 {
474         struct net_device *dev = (struct net_device *)dev_id;
475         struct mscan_priv *priv = netdev_priv(dev);
476         struct mscan_regs *regs = (struct mscan_regs *)priv->reg_base;
477 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
478         struct net_device_stats *stats = can_get_stats(dev);
479 #else
480         struct net_device_stats *stats = &dev->stats;
481 #endif
482         u8 cantier, cantflg, canrflg;
483         irqreturn_t ret = IRQ_NONE;
484
485         cantier = in_8(&regs->cantier) & MSCAN_TXE;
486         cantflg = in_8(&regs->cantflg) & cantier;
487
488         if (cantier && cantflg) {
489                 struct list_head *tmp, *pos;
490
491                 list_for_each_safe(pos, tmp, &priv->tx_head) {
492                         struct tx_queue_entry *entry =
493                             list_entry(pos, struct tx_queue_entry, list);
494                         u8 mask = entry->mask;
495
496                         if (!(cantflg & mask))
497                                 continue;
498
499                         out_8(&regs->cantbsel, mask);
500                         stats->tx_bytes += in_8(&regs->tx.dlr);
501                         stats->tx_packets++;
502                         can_get_echo_skb(dev, entry->id);
503                         priv->tx_active &= ~mask;
504                         list_del(pos);
505                 }
506
507                 if (list_empty(&priv->tx_head)) {
508                         clear_bit(F_TX_WAIT_ALL, &priv->flags);
509                         clear_bit(F_TX_PROGRESS, &priv->flags);
510                         priv->cur_pri = 0;
511                 } else {
512                         dev->trans_start = jiffies;
513                 }
514
515                 if (!test_bit(F_TX_WAIT_ALL, &priv->flags))
516                         netif_wake_queue(dev);
517
518                 out_8(&regs->cantier, priv->tx_active);
519                 ret = IRQ_HANDLED;
520         }
521
522         canrflg = in_8(&regs->canrflg);
523         if ((canrflg & ~MSCAN_STAT_MSK) &&
524             !test_and_set_bit(F_RX_PROGRESS, &priv->flags)) {
525                 if (canrflg & ~MSCAN_STAT_MSK) {
526                         priv->shadow_canrier = in_8(&regs->canrier);
527                         out_8(&regs->canrier, 0);
528 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,28)
529                         napi_schedule(&priv->napi);
530 #elif LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
531                         netif_rx_schedule(dev, &priv->napi);
532 #else
533                         netif_rx_schedule(dev);
534 #endif
535                         ret = IRQ_HANDLED;
536                 } else {
537                         clear_bit(F_RX_PROGRESS, &priv->flags);
538                 }
539         }
540         return ret;
541 }
542
543 static int mscan_do_set_mode(struct net_device *dev, enum can_mode mode)
544 {
545         struct mscan_priv *priv = netdev_priv(dev);
546         int ret = 0;
547
548         if (!priv->open_time)
549                 return -EINVAL;
550
551         switch (mode) {
552         case CAN_MODE_START:
553                 if (priv->can.state <= CAN_STATE_BUS_OFF)
554                         mscan_set_mode(dev, MSCAN_INIT_MODE);
555                 ret = mscan_start(dev);
556                 if (ret)
557                         break;
558                 if (netif_queue_stopped(dev))
559                         netif_wake_queue(dev);
560                 break;
561
562         default:
563                 ret = -EOPNOTSUPP;
564                 break;
565         }
566         return ret;
567 }
568
569 static int mscan_do_set_bittiming(struct net_device *dev)
570 {
571         struct mscan_priv *priv = netdev_priv(dev);
572         struct mscan_regs *regs = (struct mscan_regs *)priv->reg_base;
573         struct can_bittiming *bt = &priv->can.bittiming;
574         u8 btr0, btr1;
575
576         btr0 = BTR0_SET_BRP(bt->brp) | BTR0_SET_SJW(bt->sjw);
577         btr1 = (BTR1_SET_TSEG1(bt->prop_seg + bt->phase_seg1) |
578                 BTR1_SET_TSEG2(bt->phase_seg2) |
579                 BTR1_SET_SAM(priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES));
580
581         dev_info(ND2D(dev), "setting BTR0=0x%02x BTR1=0x%02x\n",
582                 btr0, btr1);
583
584         out_8(&regs->canbtr0, btr0);
585         out_8(&regs->canbtr1, btr1);
586
587         return 0;
588 }
589
590 static int mscan_open(struct net_device *dev)
591 {
592         int ret;
593         struct mscan_priv *priv = netdev_priv(dev);
594         struct mscan_regs *regs = (struct mscan_regs *)priv->reg_base;
595
596         /* common open */
597         ret = open_candev(dev);
598         if (ret)
599                 return ret;
600
601 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
602         napi_enable(&priv->napi);
603 #endif
604
605         ret = request_irq(dev->irq, mscan_isr, 0, dev->name, dev);
606         if (ret < 0) {
607                 dev_err(ND2D(dev), "failed to attach interrupt\n");
608                 goto exit_napi_disable;
609         }
610
611         priv->open_time = jiffies;
612
613         clrbits8(&regs->canctl1, MSCAN_LISTEN);
614
615         ret = mscan_start(dev);
616         if (ret)
617                 goto exit_free_irq;
618
619         netif_start_queue(dev);
620
621         return 0;
622
623 exit_free_irq:
624         priv->open_time = 0;
625         free_irq(dev->irq, dev);
626 exit_napi_disable:
627 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
628         napi_disable(&priv->napi);
629 #endif
630         close_candev(dev);
631         return ret;
632 }
633
634 static int mscan_close(struct net_device *dev)
635 {
636         struct mscan_priv *priv = netdev_priv(dev);
637         struct mscan_regs *regs = (struct mscan_regs *)priv->reg_base;
638
639         netif_stop_queue(dev);
640 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
641         napi_disable(&priv->napi);
642 #endif
643
644         out_8(&regs->cantier, 0);
645         out_8(&regs->canrier, 0);
646         mscan_set_mode(dev, MSCAN_INIT_MODE);
647         close_candev(dev);
648         free_irq(dev->irq, dev);
649         priv->open_time = 0;
650
651         return 0;
652 }
653
654 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,28)
655 static const struct net_device_ops mscan_netdev_ops = {
656        .ndo_open               = mscan_open,
657        .ndo_stop               = mscan_close,
658        .ndo_start_xmit         = mscan_start_xmit,
659 };
660 #endif
661
662 int register_mscandev(struct net_device *dev, int clock_src)
663 {
664         struct mscan_priv *priv = netdev_priv(dev);
665         struct mscan_regs *regs = (struct mscan_regs *)priv->reg_base;
666         u8 ctl1;
667
668         ctl1 = in_8(&regs->canctl1);
669         if (clock_src)
670                 ctl1 |= MSCAN_CLKSRC;
671         else
672                 ctl1 &= ~MSCAN_CLKSRC;
673
674         ctl1 |= MSCAN_CANE;
675         out_8(&regs->canctl1, ctl1);
676         udelay(100);
677
678         /* acceptance mask/acceptance code (accept everything) */
679         out_be16(&regs->canidar1_0, 0);
680         out_be16(&regs->canidar3_2, 0);
681         out_be16(&regs->canidar5_4, 0);
682         out_be16(&regs->canidar7_6, 0);
683
684         out_be16(&regs->canidmr1_0, 0xffff);
685         out_be16(&regs->canidmr3_2, 0xffff);
686         out_be16(&regs->canidmr5_4, 0xffff);
687         out_be16(&regs->canidmr7_6, 0xffff);
688         /* Two 32 bit Acceptance Filters */
689         out_8(&regs->canidac, MSCAN_AF_32BIT);
690
691         mscan_set_mode(dev, MSCAN_INIT_MODE);
692
693         return register_candev(dev);
694 }
695
696 void unregister_mscandev(struct net_device *dev)
697 {
698         struct mscan_priv *priv = netdev_priv(dev);
699         struct mscan_regs *regs = (struct mscan_regs *)priv->reg_base;
700         mscan_set_mode(dev, MSCAN_INIT_MODE);
701         clrbits8(&regs->canctl1, MSCAN_CANE);
702         unregister_candev(dev);
703 }
704
705 struct net_device *alloc_mscandev(void)
706 {
707         struct net_device *dev;
708         struct mscan_priv *priv;
709         int i;
710
711         dev = alloc_candev(sizeof(struct mscan_priv), MSCAN_ECHO_SKB_MAX);
712         if (!dev)
713                 return NULL;
714         priv = netdev_priv(dev);
715
716 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,28)
717         dev->netdev_ops = &mscan_netdev_ops;
718 #else
719         dev->open = mscan_open;
720         dev->stop = mscan_close;
721         dev->hard_start_xmit = mscan_start_xmit;
722 #endif
723
724         dev->flags |= IFF_ECHO; /* we support local echo */
725
726 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,28)
727         netif_napi_add(dev, &priv->napi, mscan_rx_poll, 8);
728 #elif LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
729         priv->dev = dev;
730         netif_napi_add(dev, &priv->napi, mscan_rx_poll, 8);
731 #else
732         dev->poll = mscan_rx_poll;
733         dev->weight = 8;
734 #endif
735
736         priv->can.bittiming_const = &mscan_bittiming_const;
737         priv->can.do_set_bittiming = mscan_do_set_bittiming;
738         priv->can.do_set_mode = mscan_do_set_mode;
739
740         for (i = 0; i < TX_QUEUE_SIZE; i++) {
741                 priv->tx_queue[i].id = i;
742                 priv->tx_queue[i].mask = 1 << i;
743         }
744
745         return dev;
746 }
747
748 MODULE_AUTHOR("Andrey Volkov <avolkov@varma-el.com>");
749 MODULE_LICENSE("GPL v2");
750 MODULE_DESCRIPTION("CAN port driver for a MSCAN based chips");