xref: /linux/drivers/net/can/sja1000/sja1000.c (revision e794b089cd6e65b9ed2b82e8dc21ea3aa49a3db2)
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  */
44 
45 #include <linux/module.h>
46 #include <linux/init.h>
47 #include <linux/kernel.h>
48 #include <linux/sched.h>
49 #include <linux/types.h>
50 #include <linux/fcntl.h>
51 #include <linux/interrupt.h>
52 #include <linux/ptrace.h>
53 #include <linux/string.h>
54 #include <linux/errno.h>
55 #include <linux/ethtool.h>
56 #include <linux/netdevice.h>
57 #include <linux/if_arp.h>
58 #include <linux/if_ether.h>
59 #include <linux/skbuff.h>
60 #include <linux/delay.h>
61 
62 #include <linux/can/dev.h>
63 #include <linux/can/error.h>
64 
65 #include "sja1000.h"
66 
67 #define DRV_NAME "sja1000"
68 
69 MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
70 MODULE_LICENSE("Dual BSD/GPL");
71 MODULE_DESCRIPTION(DRV_NAME "CAN netdevice driver");
72 
73 static const struct can_bittiming_const sja1000_bittiming_const = {
74 	.name = DRV_NAME,
75 	.tseg1_min = 1,
76 	.tseg1_max = 16,
77 	.tseg2_min = 1,
78 	.tseg2_max = 8,
79 	.sjw_max = 4,
80 	.brp_min = 1,
81 	.brp_max = 64,
82 	.brp_inc = 1,
83 };
84 
85 static void sja1000_write_cmdreg(struct sja1000_priv *priv, u8 val)
86 {
87 	unsigned long flags;
88 
89 	/*
90 	 * The command register needs some locking and time to settle
91 	 * the write_reg() operation - especially on SMP systems.
92 	 */
93 	spin_lock_irqsave(&priv->cmdreg_lock, flags);
94 	priv->write_reg(priv, SJA1000_CMR, val);
95 	priv->read_reg(priv, SJA1000_SR);
96 	spin_unlock_irqrestore(&priv->cmdreg_lock, flags);
97 }
98 
99 static int sja1000_is_absent(struct sja1000_priv *priv)
100 {
101 	return (priv->read_reg(priv, SJA1000_MOD) == 0xFF);
102 }
103 
104 static int sja1000_probe_chip(struct net_device *dev)
105 {
106 	struct sja1000_priv *priv = netdev_priv(dev);
107 
108 	if (priv->reg_base && sja1000_is_absent(priv)) {
109 		netdev_err(dev, "probing failed\n");
110 		return 0;
111 	}
112 	return -1;
113 }
114 
115 static void set_reset_mode(struct net_device *dev)
116 {
117 	struct sja1000_priv *priv = netdev_priv(dev);
118 	unsigned char status = priv->read_reg(priv, SJA1000_MOD);
119 	int i;
120 
121 	/* disable interrupts */
122 	priv->write_reg(priv, SJA1000_IER, IRQ_OFF);
123 
124 	for (i = 0; i < 100; i++) {
125 		/* check reset bit */
126 		if (status & MOD_RM) {
127 			priv->can.state = CAN_STATE_STOPPED;
128 			return;
129 		}
130 
131 		/* reset chip */
132 		priv->write_reg(priv, SJA1000_MOD, MOD_RM);
133 		udelay(10);
134 		status = priv->read_reg(priv, SJA1000_MOD);
135 	}
136 
137 	netdev_err(dev, "setting SJA1000 into reset mode failed!\n");
138 }
139 
140 static void set_normal_mode(struct net_device *dev)
141 {
142 	struct sja1000_priv *priv = netdev_priv(dev);
143 	unsigned char status = priv->read_reg(priv, SJA1000_MOD);
144 	u8 mod_reg_val = 0x00;
145 	int i;
146 
147 	for (i = 0; i < 100; i++) {
148 		/* check reset bit */
149 		if ((status & MOD_RM) == 0) {
150 			priv->can.state = CAN_STATE_ERROR_ACTIVE;
151 			/* enable interrupts */
152 			if (priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING)
153 				priv->write_reg(priv, SJA1000_IER, IRQ_ALL);
154 			else
155 				priv->write_reg(priv, SJA1000_IER,
156 						IRQ_ALL & ~IRQ_BEI);
157 			return;
158 		}
159 
160 		/* set chip to normal mode */
161 		if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
162 			mod_reg_val |= MOD_LOM;
163 		if (priv->can.ctrlmode & CAN_CTRLMODE_PRESUME_ACK)
164 			mod_reg_val |= MOD_STM;
165 		priv->write_reg(priv, SJA1000_MOD, mod_reg_val);
166 
167 		udelay(10);
168 
169 		status = priv->read_reg(priv, SJA1000_MOD);
170 	}
171 
172 	netdev_err(dev, "setting SJA1000 into normal mode failed!\n");
173 }
174 
175 /*
176  * initialize SJA1000 chip:
177  *   - reset chip
178  *   - set output mode
179  *   - set baudrate
180  *   - enable interrupts
181  *   - start operating mode
182  */
183 static void chipset_init(struct net_device *dev)
184 {
185 	struct sja1000_priv *priv = netdev_priv(dev);
186 
187 	if (!(priv->flags & SJA1000_QUIRK_NO_CDR_REG))
188 		/* set clock divider and output control register */
189 		priv->write_reg(priv, SJA1000_CDR, priv->cdr | CDR_PELICAN);
190 
191 	/* set acceptance filter (accept all) */
192 	priv->write_reg(priv, SJA1000_ACCC0, 0x00);
193 	priv->write_reg(priv, SJA1000_ACCC1, 0x00);
194 	priv->write_reg(priv, SJA1000_ACCC2, 0x00);
195 	priv->write_reg(priv, SJA1000_ACCC3, 0x00);
196 
197 	priv->write_reg(priv, SJA1000_ACCM0, 0xFF);
198 	priv->write_reg(priv, SJA1000_ACCM1, 0xFF);
199 	priv->write_reg(priv, SJA1000_ACCM2, 0xFF);
200 	priv->write_reg(priv, SJA1000_ACCM3, 0xFF);
201 
202 	priv->write_reg(priv, SJA1000_OCR, priv->ocr | OCR_MODE_NORMAL);
203 }
204 
205 static void sja1000_start(struct net_device *dev)
206 {
207 	struct sja1000_priv *priv = netdev_priv(dev);
208 
209 	/* enter reset mode */
210 	if (priv->can.state != CAN_STATE_STOPPED)
211 		set_reset_mode(dev);
212 
213 	/* Initialize chip if uninitialized at this stage */
214 	if (!(priv->flags & SJA1000_QUIRK_NO_CDR_REG ||
215 	      priv->read_reg(priv, SJA1000_CDR) & CDR_PELICAN))
216 		chipset_init(dev);
217 
218 	/* Clear error counters and error code capture */
219 	priv->write_reg(priv, SJA1000_TXERR, 0x0);
220 	priv->write_reg(priv, SJA1000_RXERR, 0x0);
221 	priv->read_reg(priv, SJA1000_ECC);
222 
223 	/* clear interrupt flags */
224 	priv->read_reg(priv, SJA1000_IR);
225 
226 	/* leave reset mode */
227 	set_normal_mode(dev);
228 }
229 
230 static int sja1000_set_mode(struct net_device *dev, enum can_mode mode)
231 {
232 	switch (mode) {
233 	case CAN_MODE_START:
234 		sja1000_start(dev);
235 		if (netif_queue_stopped(dev))
236 			netif_wake_queue(dev);
237 		break;
238 
239 	default:
240 		return -EOPNOTSUPP;
241 	}
242 
243 	return 0;
244 }
245 
246 static int sja1000_set_bittiming(struct net_device *dev)
247 {
248 	struct sja1000_priv *priv = netdev_priv(dev);
249 	struct can_bittiming *bt = &priv->can.bittiming;
250 	u8 btr0, btr1;
251 
252 	btr0 = ((bt->brp - 1) & 0x3f) | (((bt->sjw - 1) & 0x3) << 6);
253 	btr1 = ((bt->prop_seg + bt->phase_seg1 - 1) & 0xf) |
254 		(((bt->phase_seg2 - 1) & 0x7) << 4);
255 	if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
256 		btr1 |= 0x80;
257 
258 	netdev_info(dev, "setting BTR0=0x%02x BTR1=0x%02x\n", btr0, btr1);
259 
260 	priv->write_reg(priv, SJA1000_BTR0, btr0);
261 	priv->write_reg(priv, SJA1000_BTR1, btr1);
262 
263 	return 0;
264 }
265 
266 static int sja1000_get_berr_counter(const struct net_device *dev,
267 				    struct can_berr_counter *bec)
268 {
269 	struct sja1000_priv *priv = netdev_priv(dev);
270 
271 	bec->txerr = priv->read_reg(priv, SJA1000_TXERR);
272 	bec->rxerr = priv->read_reg(priv, SJA1000_RXERR);
273 
274 	return 0;
275 }
276 
277 /*
278  * transmit a CAN message
279  * message layout in the sk_buff should be like this:
280  * xx xx xx xx	 ff	 ll   00 11 22 33 44 55 66 77
281  * [  can-id ] [flags] [len] [can data (up to 8 bytes]
282  */
283 static netdev_tx_t sja1000_start_xmit(struct sk_buff *skb,
284 					    struct net_device *dev)
285 {
286 	struct sja1000_priv *priv = netdev_priv(dev);
287 	struct can_frame *cf = (struct can_frame *)skb->data;
288 	uint8_t fi;
289 	canid_t id;
290 	uint8_t dreg;
291 	u8 cmd_reg_val = 0x00;
292 	int i;
293 
294 	if (can_dev_dropped_skb(dev, skb))
295 		return NETDEV_TX_OK;
296 
297 	netif_stop_queue(dev);
298 
299 	fi = can_get_cc_dlc(cf, priv->can.ctrlmode);
300 	id = cf->can_id;
301 
302 	if (id & CAN_RTR_FLAG)
303 		fi |= SJA1000_FI_RTR;
304 
305 	if (id & CAN_EFF_FLAG) {
306 		fi |= SJA1000_FI_FF;
307 		dreg = SJA1000_EFF_BUF;
308 		priv->write_reg(priv, SJA1000_FI, fi);
309 		priv->write_reg(priv, SJA1000_ID1, (id & 0x1fe00000) >> 21);
310 		priv->write_reg(priv, SJA1000_ID2, (id & 0x001fe000) >> 13);
311 		priv->write_reg(priv, SJA1000_ID3, (id & 0x00001fe0) >> 5);
312 		priv->write_reg(priv, SJA1000_ID4, (id & 0x0000001f) << 3);
313 	} else {
314 		dreg = SJA1000_SFF_BUF;
315 		priv->write_reg(priv, SJA1000_FI, fi);
316 		priv->write_reg(priv, SJA1000_ID1, (id & 0x000007f8) >> 3);
317 		priv->write_reg(priv, SJA1000_ID2, (id & 0x00000007) << 5);
318 	}
319 
320 	for (i = 0; i < cf->len; i++)
321 		priv->write_reg(priv, dreg++, cf->data[i]);
322 
323 	can_put_echo_skb(skb, dev, 0, 0);
324 
325 	if (priv->can.ctrlmode & CAN_CTRLMODE_ONE_SHOT)
326 		cmd_reg_val |= CMD_AT;
327 
328 	if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK)
329 		cmd_reg_val |= CMD_SRR;
330 	else
331 		cmd_reg_val |= CMD_TR;
332 
333 	sja1000_write_cmdreg(priv, cmd_reg_val);
334 
335 	return NETDEV_TX_OK;
336 }
337 
338 static void sja1000_rx(struct net_device *dev)
339 {
340 	struct sja1000_priv *priv = netdev_priv(dev);
341 	struct net_device_stats *stats = &dev->stats;
342 	struct can_frame *cf;
343 	struct sk_buff *skb;
344 	uint8_t fi;
345 	uint8_t dreg;
346 	canid_t id;
347 	int i;
348 
349 	/* create zero'ed CAN frame buffer */
350 	skb = alloc_can_skb(dev, &cf);
351 	if (skb == NULL)
352 		return;
353 
354 	fi = priv->read_reg(priv, SJA1000_FI);
355 
356 	if (fi & SJA1000_FI_FF) {
357 		/* extended frame format (EFF) */
358 		dreg = SJA1000_EFF_BUF;
359 		id = (priv->read_reg(priv, SJA1000_ID1) << 21)
360 		    | (priv->read_reg(priv, SJA1000_ID2) << 13)
361 		    | (priv->read_reg(priv, SJA1000_ID3) << 5)
362 		    | (priv->read_reg(priv, SJA1000_ID4) >> 3);
363 		id |= CAN_EFF_FLAG;
364 	} else {
365 		/* standard frame format (SFF) */
366 		dreg = SJA1000_SFF_BUF;
367 		id = (priv->read_reg(priv, SJA1000_ID1) << 3)
368 		    | (priv->read_reg(priv, SJA1000_ID2) >> 5);
369 	}
370 
371 	can_frame_set_cc_len(cf, fi & 0x0F, priv->can.ctrlmode);
372 	if (fi & SJA1000_FI_RTR) {
373 		id |= CAN_RTR_FLAG;
374 	} else {
375 		for (i = 0; i < cf->len; i++)
376 			cf->data[i] = priv->read_reg(priv, dreg++);
377 
378 		stats->rx_bytes += cf->len;
379 	}
380 	stats->rx_packets++;
381 
382 	cf->can_id = id;
383 
384 	/* release receive buffer */
385 	sja1000_write_cmdreg(priv, CMD_RRB);
386 
387 	netif_rx(skb);
388 }
389 
390 static irqreturn_t sja1000_reset_interrupt(int irq, void *dev_id)
391 {
392 	struct net_device *dev = (struct net_device *)dev_id;
393 
394 	netdev_dbg(dev, "performing a soft reset upon overrun\n");
395 	sja1000_start(dev);
396 
397 	return IRQ_HANDLED;
398 }
399 
400 static int sja1000_err(struct net_device *dev, uint8_t isrc, uint8_t status)
401 {
402 	struct sja1000_priv *priv = netdev_priv(dev);
403 	struct net_device_stats *stats = &dev->stats;
404 	struct can_frame *cf;
405 	struct sk_buff *skb;
406 	enum can_state state = priv->can.state;
407 	enum can_state rx_state, tx_state;
408 	unsigned int rxerr, txerr;
409 	uint8_t ecc, alc;
410 	int ret = 0;
411 
412 	skb = alloc_can_err_skb(dev, &cf);
413 	if (skb == NULL)
414 		return -ENOMEM;
415 
416 	txerr = priv->read_reg(priv, SJA1000_TXERR);
417 	rxerr = priv->read_reg(priv, SJA1000_RXERR);
418 
419 	if (isrc & IRQ_DOI) {
420 		/* data overrun interrupt */
421 		netdev_dbg(dev, "data overrun interrupt\n");
422 		cf->can_id |= CAN_ERR_CRTL;
423 		cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
424 		stats->rx_over_errors++;
425 		stats->rx_errors++;
426 		sja1000_write_cmdreg(priv, CMD_CDO);	/* clear bit */
427 
428 		/* Some controllers needs additional handling upon overrun
429 		 * condition: the controller may sometimes be totally confused
430 		 * and refuse any new frame while its buffer is empty. The only
431 		 * way to re-sync the read vs. write buffer offsets is to
432 		 * stop any current handling and perform a reset.
433 		 */
434 		if (priv->flags & SJA1000_QUIRK_RESET_ON_OVERRUN)
435 			ret = IRQ_WAKE_THREAD;
436 	}
437 
438 	if (isrc & IRQ_EI) {
439 		/* error warning interrupt */
440 		netdev_dbg(dev, "error warning interrupt\n");
441 
442 		if (status & SR_BS)
443 			state = CAN_STATE_BUS_OFF;
444 		else if (status & SR_ES)
445 			state = CAN_STATE_ERROR_WARNING;
446 		else
447 			state = CAN_STATE_ERROR_ACTIVE;
448 	}
449 	if (state != CAN_STATE_BUS_OFF) {
450 		cf->can_id |= CAN_ERR_CNT;
451 		cf->data[6] = txerr;
452 		cf->data[7] = rxerr;
453 	}
454 	if (isrc & IRQ_BEI) {
455 		/* bus error interrupt */
456 		priv->can.can_stats.bus_error++;
457 		stats->rx_errors++;
458 
459 		ecc = priv->read_reg(priv, SJA1000_ECC);
460 
461 		cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
462 
463 		/* set error type */
464 		switch (ecc & ECC_MASK) {
465 		case ECC_BIT:
466 			cf->data[2] |= CAN_ERR_PROT_BIT;
467 			break;
468 		case ECC_FORM:
469 			cf->data[2] |= CAN_ERR_PROT_FORM;
470 			break;
471 		case ECC_STUFF:
472 			cf->data[2] |= CAN_ERR_PROT_STUFF;
473 			break;
474 		default:
475 			break;
476 		}
477 
478 		/* set error location */
479 		cf->data[3] = ecc & ECC_SEG;
480 
481 		/* Error occurred during transmission? */
482 		if ((ecc & ECC_DIR) == 0)
483 			cf->data[2] |= CAN_ERR_PROT_TX;
484 	}
485 	if (isrc & IRQ_EPI) {
486 		/* error passive interrupt */
487 		netdev_dbg(dev, "error passive interrupt\n");
488 
489 		if (state == CAN_STATE_ERROR_PASSIVE)
490 			state = CAN_STATE_ERROR_WARNING;
491 		else
492 			state = CAN_STATE_ERROR_PASSIVE;
493 	}
494 	if (isrc & IRQ_ALI) {
495 		/* arbitration lost interrupt */
496 		netdev_dbg(dev, "arbitration lost interrupt\n");
497 		alc = priv->read_reg(priv, SJA1000_ALC);
498 		priv->can.can_stats.arbitration_lost++;
499 		cf->can_id |= CAN_ERR_LOSTARB;
500 		cf->data[0] = alc & 0x1f;
501 	}
502 
503 	if (state != priv->can.state) {
504 		tx_state = txerr >= rxerr ? state : 0;
505 		rx_state = txerr <= rxerr ? state : 0;
506 
507 		can_change_state(dev, cf, tx_state, rx_state);
508 
509 		if(state == CAN_STATE_BUS_OFF)
510 			can_bus_off(dev);
511 	}
512 
513 	netif_rx(skb);
514 
515 	return ret;
516 }
517 
518 irqreturn_t sja1000_interrupt(int irq, void *dev_id)
519 {
520 	struct net_device *dev = (struct net_device *)dev_id;
521 	struct sja1000_priv *priv = netdev_priv(dev);
522 	struct net_device_stats *stats = &dev->stats;
523 	uint8_t isrc, status;
524 	irqreturn_t ret = 0;
525 	int n = 0, err;
526 
527 	if (priv->pre_irq)
528 		priv->pre_irq(priv);
529 
530 	/* Shared interrupts and IRQ off? */
531 	if (priv->read_reg(priv, SJA1000_IER) == IRQ_OFF)
532 		goto out;
533 
534 	while ((isrc = priv->read_reg(priv, SJA1000_IR)) &&
535 	       (n < SJA1000_MAX_IRQ)) {
536 
537 		status = priv->read_reg(priv, SJA1000_SR);
538 		/* check for absent controller due to hw unplug */
539 		if (status == 0xFF && sja1000_is_absent(priv))
540 			goto out;
541 
542 		if (isrc & IRQ_WUI)
543 			netdev_warn(dev, "wakeup interrupt\n");
544 
545 		if (isrc & IRQ_TI) {
546 			/* transmission buffer released */
547 			if (priv->can.ctrlmode & CAN_CTRLMODE_ONE_SHOT &&
548 			    !(status & SR_TCS)) {
549 				stats->tx_errors++;
550 				can_free_echo_skb(dev, 0, NULL);
551 			} else {
552 				/* transmission complete */
553 				stats->tx_bytes += can_get_echo_skb(dev, 0, NULL);
554 				stats->tx_packets++;
555 			}
556 			netif_wake_queue(dev);
557 		}
558 		if (isrc & IRQ_RI) {
559 			/* receive interrupt */
560 			while (status & SR_RBS) {
561 				sja1000_rx(dev);
562 				status = priv->read_reg(priv, SJA1000_SR);
563 				/* check for absent controller */
564 				if (status == 0xFF && sja1000_is_absent(priv))
565 					goto out;
566 			}
567 		}
568 		if (isrc & (IRQ_DOI | IRQ_EI | IRQ_BEI | IRQ_EPI | IRQ_ALI)) {
569 			/* error interrupt */
570 			err = sja1000_err(dev, isrc, status);
571 			if (err == IRQ_WAKE_THREAD)
572 				ret = err;
573 			if (err)
574 				break;
575 		}
576 		n++;
577 	}
578 out:
579 	if (!ret)
580 		ret = (n) ? IRQ_HANDLED : IRQ_NONE;
581 
582 	if (priv->post_irq)
583 		priv->post_irq(priv);
584 
585 	if (n >= SJA1000_MAX_IRQ)
586 		netdev_dbg(dev, "%d messages handled in ISR", n);
587 
588 	return ret;
589 }
590 EXPORT_SYMBOL_GPL(sja1000_interrupt);
591 
592 static int sja1000_open(struct net_device *dev)
593 {
594 	struct sja1000_priv *priv = netdev_priv(dev);
595 	int err;
596 
597 	/* set chip into reset mode */
598 	set_reset_mode(dev);
599 
600 	/* common open */
601 	err = open_candev(dev);
602 	if (err)
603 		return err;
604 
605 	/* register interrupt handler, if not done by the device driver */
606 	if (!(priv->flags & SJA1000_CUSTOM_IRQ_HANDLER)) {
607 		err = request_threaded_irq(dev->irq, sja1000_interrupt,
608 					   sja1000_reset_interrupt,
609 					   priv->irq_flags, dev->name, (void *)dev);
610 		if (err) {
611 			close_candev(dev);
612 			return -EAGAIN;
613 		}
614 	}
615 
616 	/* init and start chi */
617 	sja1000_start(dev);
618 
619 	netif_start_queue(dev);
620 
621 	return 0;
622 }
623 
624 static int sja1000_close(struct net_device *dev)
625 {
626 	struct sja1000_priv *priv = netdev_priv(dev);
627 
628 	netif_stop_queue(dev);
629 	set_reset_mode(dev);
630 
631 	if (!(priv->flags & SJA1000_CUSTOM_IRQ_HANDLER))
632 		free_irq(dev->irq, (void *)dev);
633 
634 	close_candev(dev);
635 
636 	return 0;
637 }
638 
639 struct net_device *alloc_sja1000dev(int sizeof_priv)
640 {
641 	struct net_device *dev;
642 	struct sja1000_priv *priv;
643 
644 	dev = alloc_candev(sizeof(struct sja1000_priv) + sizeof_priv,
645 		SJA1000_ECHO_SKB_MAX);
646 	if (!dev)
647 		return NULL;
648 
649 	priv = netdev_priv(dev);
650 
651 	priv->dev = dev;
652 	priv->can.bittiming_const = &sja1000_bittiming_const;
653 	priv->can.do_set_bittiming = sja1000_set_bittiming;
654 	priv->can.do_set_mode = sja1000_set_mode;
655 	priv->can.do_get_berr_counter = sja1000_get_berr_counter;
656 	priv->can.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK |
657 				       CAN_CTRLMODE_LISTENONLY |
658 				       CAN_CTRLMODE_3_SAMPLES |
659 				       CAN_CTRLMODE_ONE_SHOT |
660 				       CAN_CTRLMODE_BERR_REPORTING |
661 				       CAN_CTRLMODE_PRESUME_ACK |
662 				       CAN_CTRLMODE_CC_LEN8_DLC;
663 
664 	spin_lock_init(&priv->cmdreg_lock);
665 
666 	if (sizeof_priv)
667 		priv->priv = (void *)priv + sizeof(struct sja1000_priv);
668 
669 	return dev;
670 }
671 EXPORT_SYMBOL_GPL(alloc_sja1000dev);
672 
673 void free_sja1000dev(struct net_device *dev)
674 {
675 	free_candev(dev);
676 }
677 EXPORT_SYMBOL_GPL(free_sja1000dev);
678 
679 static const struct net_device_ops sja1000_netdev_ops = {
680 	.ndo_open	= sja1000_open,
681 	.ndo_stop	= sja1000_close,
682 	.ndo_start_xmit	= sja1000_start_xmit,
683 	.ndo_change_mtu	= can_change_mtu,
684 };
685 
686 static const struct ethtool_ops sja1000_ethtool_ops = {
687 	.get_ts_info = ethtool_op_get_ts_info,
688 };
689 
690 int register_sja1000dev(struct net_device *dev)
691 {
692 	if (!sja1000_probe_chip(dev))
693 		return -ENODEV;
694 
695 	dev->flags |= IFF_ECHO;	/* we support local echo */
696 	dev->netdev_ops = &sja1000_netdev_ops;
697 	dev->ethtool_ops = &sja1000_ethtool_ops;
698 
699 	set_reset_mode(dev);
700 	chipset_init(dev);
701 
702 	return register_candev(dev);
703 }
704 EXPORT_SYMBOL_GPL(register_sja1000dev);
705 
706 void unregister_sja1000dev(struct net_device *dev)
707 {
708 	set_reset_mode(dev);
709 	unregister_candev(dev);
710 }
711 EXPORT_SYMBOL_GPL(unregister_sja1000dev);
712 
713 static __init int sja1000_init(void)
714 {
715 	printk(KERN_INFO "%s CAN netdevice driver\n", DRV_NAME);
716 
717 	return 0;
718 }
719 
720 module_init(sja1000_init);
721 
722 static __exit void sja1000_exit(void)
723 {
724 	printk(KERN_INFO "%s: driver removed\n", DRV_NAME);
725 }
726 
727 module_exit(sja1000_exit);
728