xref: /linux/drivers/net/can/sja1000/sja1000.c (revision 3932b9ca55b0be314a36d3e84faff3e823c081f5)
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/netdevice.h>
56 #include <linux/if_arp.h>
57 #include <linux/if_ether.h>
58 #include <linux/skbuff.h>
59 #include <linux/delay.h>
60 
61 #include <linux/can/dev.h>
62 #include <linux/can/error.h>
63 #include <linux/can/led.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 	/* set clock divider and output control register */
188 	priv->write_reg(priv, SJA1000_CDR, priv->cdr | CDR_PELICAN);
189 
190 	/* set acceptance filter (accept all) */
191 	priv->write_reg(priv, SJA1000_ACCC0, 0x00);
192 	priv->write_reg(priv, SJA1000_ACCC1, 0x00);
193 	priv->write_reg(priv, SJA1000_ACCC2, 0x00);
194 	priv->write_reg(priv, SJA1000_ACCC3, 0x00);
195 
196 	priv->write_reg(priv, SJA1000_ACCM0, 0xFF);
197 	priv->write_reg(priv, SJA1000_ACCM1, 0xFF);
198 	priv->write_reg(priv, SJA1000_ACCM2, 0xFF);
199 	priv->write_reg(priv, SJA1000_ACCM3, 0xFF);
200 
201 	priv->write_reg(priv, SJA1000_OCR, priv->ocr | OCR_MODE_NORMAL);
202 }
203 
204 static void sja1000_start(struct net_device *dev)
205 {
206 	struct sja1000_priv *priv = netdev_priv(dev);
207 
208 	/* leave reset mode */
209 	if (priv->can.state != CAN_STATE_STOPPED)
210 		set_reset_mode(dev);
211 
212 	/* Initialize chip if uninitialized at this stage */
213 	if (!(priv->read_reg(priv, SJA1000_CDR) & CDR_PELICAN))
214 		chipset_init(dev);
215 
216 	/* Clear error counters and error code capture */
217 	priv->write_reg(priv, SJA1000_TXERR, 0x0);
218 	priv->write_reg(priv, SJA1000_RXERR, 0x0);
219 	priv->read_reg(priv, SJA1000_ECC);
220 
221 	/* leave reset mode */
222 	set_normal_mode(dev);
223 }
224 
225 static int sja1000_set_mode(struct net_device *dev, enum can_mode mode)
226 {
227 	switch (mode) {
228 	case CAN_MODE_START:
229 		sja1000_start(dev);
230 		if (netif_queue_stopped(dev))
231 			netif_wake_queue(dev);
232 		break;
233 
234 	default:
235 		return -EOPNOTSUPP;
236 	}
237 
238 	return 0;
239 }
240 
241 static int sja1000_set_bittiming(struct net_device *dev)
242 {
243 	struct sja1000_priv *priv = netdev_priv(dev);
244 	struct can_bittiming *bt = &priv->can.bittiming;
245 	u8 btr0, btr1;
246 
247 	btr0 = ((bt->brp - 1) & 0x3f) | (((bt->sjw - 1) & 0x3) << 6);
248 	btr1 = ((bt->prop_seg + bt->phase_seg1 - 1) & 0xf) |
249 		(((bt->phase_seg2 - 1) & 0x7) << 4);
250 	if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
251 		btr1 |= 0x80;
252 
253 	netdev_info(dev, "setting BTR0=0x%02x BTR1=0x%02x\n", btr0, btr1);
254 
255 	priv->write_reg(priv, SJA1000_BTR0, btr0);
256 	priv->write_reg(priv, SJA1000_BTR1, btr1);
257 
258 	return 0;
259 }
260 
261 static int sja1000_get_berr_counter(const struct net_device *dev,
262 				    struct can_berr_counter *bec)
263 {
264 	struct sja1000_priv *priv = netdev_priv(dev);
265 
266 	bec->txerr = priv->read_reg(priv, SJA1000_TXERR);
267 	bec->rxerr = priv->read_reg(priv, SJA1000_RXERR);
268 
269 	return 0;
270 }
271 
272 /*
273  * transmit a CAN message
274  * message layout in the sk_buff should be like this:
275  * xx xx xx xx	 ff	 ll   00 11 22 33 44 55 66 77
276  * [  can-id ] [flags] [len] [can data (up to 8 bytes]
277  */
278 static netdev_tx_t sja1000_start_xmit(struct sk_buff *skb,
279 					    struct net_device *dev)
280 {
281 	struct sja1000_priv *priv = netdev_priv(dev);
282 	struct can_frame *cf = (struct can_frame *)skb->data;
283 	uint8_t fi;
284 	uint8_t dlc;
285 	canid_t id;
286 	uint8_t dreg;
287 	u8 cmd_reg_val = 0x00;
288 	int i;
289 
290 	if (can_dropped_invalid_skb(dev, skb))
291 		return NETDEV_TX_OK;
292 
293 	netif_stop_queue(dev);
294 
295 	fi = dlc = cf->can_dlc;
296 	id = cf->can_id;
297 
298 	if (id & CAN_RTR_FLAG)
299 		fi |= SJA1000_FI_RTR;
300 
301 	if (id & CAN_EFF_FLAG) {
302 		fi |= SJA1000_FI_FF;
303 		dreg = SJA1000_EFF_BUF;
304 		priv->write_reg(priv, SJA1000_FI, fi);
305 		priv->write_reg(priv, SJA1000_ID1, (id & 0x1fe00000) >> 21);
306 		priv->write_reg(priv, SJA1000_ID2, (id & 0x001fe000) >> 13);
307 		priv->write_reg(priv, SJA1000_ID3, (id & 0x00001fe0) >> 5);
308 		priv->write_reg(priv, SJA1000_ID4, (id & 0x0000001f) << 3);
309 	} else {
310 		dreg = SJA1000_SFF_BUF;
311 		priv->write_reg(priv, SJA1000_FI, fi);
312 		priv->write_reg(priv, SJA1000_ID1, (id & 0x000007f8) >> 3);
313 		priv->write_reg(priv, SJA1000_ID2, (id & 0x00000007) << 5);
314 	}
315 
316 	for (i = 0; i < dlc; i++)
317 		priv->write_reg(priv, dreg++, cf->data[i]);
318 
319 	can_put_echo_skb(skb, dev, 0);
320 
321 	if (priv->can.ctrlmode & CAN_CTRLMODE_ONE_SHOT)
322 		cmd_reg_val |= CMD_AT;
323 
324 	if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK)
325 		cmd_reg_val |= CMD_SRR;
326 	else
327 		cmd_reg_val |= CMD_TR;
328 
329 	sja1000_write_cmdreg(priv, cmd_reg_val);
330 
331 	return NETDEV_TX_OK;
332 }
333 
334 static void sja1000_rx(struct net_device *dev)
335 {
336 	struct sja1000_priv *priv = netdev_priv(dev);
337 	struct net_device_stats *stats = &dev->stats;
338 	struct can_frame *cf;
339 	struct sk_buff *skb;
340 	uint8_t fi;
341 	uint8_t dreg;
342 	canid_t id;
343 	int i;
344 
345 	/* create zero'ed CAN frame buffer */
346 	skb = alloc_can_skb(dev, &cf);
347 	if (skb == NULL)
348 		return;
349 
350 	fi = priv->read_reg(priv, SJA1000_FI);
351 
352 	if (fi & SJA1000_FI_FF) {
353 		/* extended frame format (EFF) */
354 		dreg = SJA1000_EFF_BUF;
355 		id = (priv->read_reg(priv, SJA1000_ID1) << 21)
356 		    | (priv->read_reg(priv, SJA1000_ID2) << 13)
357 		    | (priv->read_reg(priv, SJA1000_ID3) << 5)
358 		    | (priv->read_reg(priv, SJA1000_ID4) >> 3);
359 		id |= CAN_EFF_FLAG;
360 	} else {
361 		/* standard frame format (SFF) */
362 		dreg = SJA1000_SFF_BUF;
363 		id = (priv->read_reg(priv, SJA1000_ID1) << 3)
364 		    | (priv->read_reg(priv, SJA1000_ID2) >> 5);
365 	}
366 
367 	cf->can_dlc = get_can_dlc(fi & 0x0F);
368 	if (fi & SJA1000_FI_RTR) {
369 		id |= CAN_RTR_FLAG;
370 	} else {
371 		for (i = 0; i < cf->can_dlc; i++)
372 			cf->data[i] = priv->read_reg(priv, dreg++);
373 	}
374 
375 	cf->can_id = id;
376 
377 	/* release receive buffer */
378 	sja1000_write_cmdreg(priv, CMD_RRB);
379 
380 	netif_rx(skb);
381 
382 	stats->rx_packets++;
383 	stats->rx_bytes += cf->can_dlc;
384 
385 	can_led_event(dev, CAN_LED_EVENT_RX);
386 }
387 
388 static int sja1000_err(struct net_device *dev, uint8_t isrc, uint8_t status)
389 {
390 	struct sja1000_priv *priv = netdev_priv(dev);
391 	struct net_device_stats *stats = &dev->stats;
392 	struct can_frame *cf;
393 	struct sk_buff *skb;
394 	enum can_state state = priv->can.state;
395 	uint8_t ecc, alc;
396 
397 	skb = alloc_can_err_skb(dev, &cf);
398 	if (skb == NULL)
399 		return -ENOMEM;
400 
401 	if (isrc & IRQ_DOI) {
402 		/* data overrun interrupt */
403 		netdev_dbg(dev, "data overrun interrupt\n");
404 		cf->can_id |= CAN_ERR_CRTL;
405 		cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
406 		stats->rx_over_errors++;
407 		stats->rx_errors++;
408 		sja1000_write_cmdreg(priv, CMD_CDO);	/* clear bit */
409 	}
410 
411 	if (isrc & IRQ_EI) {
412 		/* error warning interrupt */
413 		netdev_dbg(dev, "error warning interrupt\n");
414 
415 		if (status & SR_BS) {
416 			state = CAN_STATE_BUS_OFF;
417 			cf->can_id |= CAN_ERR_BUSOFF;
418 			can_bus_off(dev);
419 		} else if (status & SR_ES) {
420 			state = CAN_STATE_ERROR_WARNING;
421 		} else
422 			state = CAN_STATE_ERROR_ACTIVE;
423 	}
424 	if (isrc & IRQ_BEI) {
425 		/* bus error interrupt */
426 		priv->can.can_stats.bus_error++;
427 		stats->rx_errors++;
428 
429 		ecc = priv->read_reg(priv, SJA1000_ECC);
430 
431 		cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
432 
433 		switch (ecc & ECC_MASK) {
434 		case ECC_BIT:
435 			cf->data[2] |= CAN_ERR_PROT_BIT;
436 			break;
437 		case ECC_FORM:
438 			cf->data[2] |= CAN_ERR_PROT_FORM;
439 			break;
440 		case ECC_STUFF:
441 			cf->data[2] |= CAN_ERR_PROT_STUFF;
442 			break;
443 		default:
444 			cf->data[2] |= CAN_ERR_PROT_UNSPEC;
445 			cf->data[3] = ecc & ECC_SEG;
446 			break;
447 		}
448 		/* Error occurred during transmission? */
449 		if ((ecc & ECC_DIR) == 0)
450 			cf->data[2] |= CAN_ERR_PROT_TX;
451 	}
452 	if (isrc & IRQ_EPI) {
453 		/* error passive interrupt */
454 		netdev_dbg(dev, "error passive interrupt\n");
455 		if (status & SR_ES)
456 			state = CAN_STATE_ERROR_PASSIVE;
457 		else
458 			state = CAN_STATE_ERROR_ACTIVE;
459 	}
460 	if (isrc & IRQ_ALI) {
461 		/* arbitration lost interrupt */
462 		netdev_dbg(dev, "arbitration lost interrupt\n");
463 		alc = priv->read_reg(priv, SJA1000_ALC);
464 		priv->can.can_stats.arbitration_lost++;
465 		stats->tx_errors++;
466 		cf->can_id |= CAN_ERR_LOSTARB;
467 		cf->data[0] = alc & 0x1f;
468 	}
469 
470 	if (state != priv->can.state && (state == CAN_STATE_ERROR_WARNING ||
471 					 state == CAN_STATE_ERROR_PASSIVE)) {
472 		uint8_t rxerr = priv->read_reg(priv, SJA1000_RXERR);
473 		uint8_t txerr = priv->read_reg(priv, SJA1000_TXERR);
474 		cf->can_id |= CAN_ERR_CRTL;
475 		if (state == CAN_STATE_ERROR_WARNING) {
476 			priv->can.can_stats.error_warning++;
477 			cf->data[1] = (txerr > rxerr) ?
478 				CAN_ERR_CRTL_TX_WARNING :
479 				CAN_ERR_CRTL_RX_WARNING;
480 		} else {
481 			priv->can.can_stats.error_passive++;
482 			cf->data[1] = (txerr > rxerr) ?
483 				CAN_ERR_CRTL_TX_PASSIVE :
484 				CAN_ERR_CRTL_RX_PASSIVE;
485 		}
486 		cf->data[6] = txerr;
487 		cf->data[7] = rxerr;
488 	}
489 
490 	priv->can.state = state;
491 
492 	netif_rx(skb);
493 
494 	stats->rx_packets++;
495 	stats->rx_bytes += cf->can_dlc;
496 
497 	return 0;
498 }
499 
500 irqreturn_t sja1000_interrupt(int irq, void *dev_id)
501 {
502 	struct net_device *dev = (struct net_device *)dev_id;
503 	struct sja1000_priv *priv = netdev_priv(dev);
504 	struct net_device_stats *stats = &dev->stats;
505 	uint8_t isrc, status;
506 	int n = 0;
507 
508 	if (priv->pre_irq)
509 		priv->pre_irq(priv);
510 
511 	/* Shared interrupts and IRQ off? */
512 	if (priv->read_reg(priv, SJA1000_IER) == IRQ_OFF)
513 		goto out;
514 
515 	while ((isrc = priv->read_reg(priv, SJA1000_IR)) &&
516 	       (n < SJA1000_MAX_IRQ)) {
517 
518 		status = priv->read_reg(priv, SJA1000_SR);
519 		/* check for absent controller due to hw unplug */
520 		if (status == 0xFF && sja1000_is_absent(priv))
521 			goto out;
522 
523 		if (isrc & IRQ_WUI)
524 			netdev_warn(dev, "wakeup interrupt\n");
525 
526 		if (isrc & IRQ_TI) {
527 			/* transmission buffer released */
528 			if (priv->can.ctrlmode & CAN_CTRLMODE_ONE_SHOT &&
529 			    !(status & SR_TCS)) {
530 				stats->tx_errors++;
531 				can_free_echo_skb(dev, 0);
532 			} else {
533 				/* transmission complete */
534 				stats->tx_bytes +=
535 					priv->read_reg(priv, SJA1000_FI) & 0xf;
536 				stats->tx_packets++;
537 				can_get_echo_skb(dev, 0);
538 			}
539 			netif_wake_queue(dev);
540 			can_led_event(dev, CAN_LED_EVENT_TX);
541 		}
542 		if (isrc & IRQ_RI) {
543 			/* receive interrupt */
544 			while (status & SR_RBS) {
545 				sja1000_rx(dev);
546 				status = priv->read_reg(priv, SJA1000_SR);
547 				/* check for absent controller */
548 				if (status == 0xFF && sja1000_is_absent(priv))
549 					goto out;
550 			}
551 		}
552 		if (isrc & (IRQ_DOI | IRQ_EI | IRQ_BEI | IRQ_EPI | IRQ_ALI)) {
553 			/* error interrupt */
554 			if (sja1000_err(dev, isrc, status))
555 				break;
556 		}
557 		n++;
558 	}
559 out:
560 	if (priv->post_irq)
561 		priv->post_irq(priv);
562 
563 	if (n >= SJA1000_MAX_IRQ)
564 		netdev_dbg(dev, "%d messages handled in ISR", n);
565 
566 	return (n) ? IRQ_HANDLED : IRQ_NONE;
567 }
568 EXPORT_SYMBOL_GPL(sja1000_interrupt);
569 
570 static int sja1000_open(struct net_device *dev)
571 {
572 	struct sja1000_priv *priv = netdev_priv(dev);
573 	int err;
574 
575 	/* set chip into reset mode */
576 	set_reset_mode(dev);
577 
578 	/* common open */
579 	err = open_candev(dev);
580 	if (err)
581 		return err;
582 
583 	/* register interrupt handler, if not done by the device driver */
584 	if (!(priv->flags & SJA1000_CUSTOM_IRQ_HANDLER)) {
585 		err = request_irq(dev->irq, sja1000_interrupt, priv->irq_flags,
586 				  dev->name, (void *)dev);
587 		if (err) {
588 			close_candev(dev);
589 			return -EAGAIN;
590 		}
591 	}
592 
593 	/* init and start chi */
594 	sja1000_start(dev);
595 
596 	can_led_event(dev, CAN_LED_EVENT_OPEN);
597 
598 	netif_start_queue(dev);
599 
600 	return 0;
601 }
602 
603 static int sja1000_close(struct net_device *dev)
604 {
605 	struct sja1000_priv *priv = netdev_priv(dev);
606 
607 	netif_stop_queue(dev);
608 	set_reset_mode(dev);
609 
610 	if (!(priv->flags & SJA1000_CUSTOM_IRQ_HANDLER))
611 		free_irq(dev->irq, (void *)dev);
612 
613 	close_candev(dev);
614 
615 	can_led_event(dev, CAN_LED_EVENT_STOP);
616 
617 	return 0;
618 }
619 
620 struct net_device *alloc_sja1000dev(int sizeof_priv)
621 {
622 	struct net_device *dev;
623 	struct sja1000_priv *priv;
624 
625 	dev = alloc_candev(sizeof(struct sja1000_priv) + sizeof_priv,
626 		SJA1000_ECHO_SKB_MAX);
627 	if (!dev)
628 		return NULL;
629 
630 	priv = netdev_priv(dev);
631 
632 	priv->dev = dev;
633 	priv->can.bittiming_const = &sja1000_bittiming_const;
634 	priv->can.do_set_bittiming = sja1000_set_bittiming;
635 	priv->can.do_set_mode = sja1000_set_mode;
636 	priv->can.do_get_berr_counter = sja1000_get_berr_counter;
637 	priv->can.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK |
638 				       CAN_CTRLMODE_LISTENONLY |
639 				       CAN_CTRLMODE_3_SAMPLES |
640 				       CAN_CTRLMODE_ONE_SHOT |
641 				       CAN_CTRLMODE_BERR_REPORTING |
642 				       CAN_CTRLMODE_PRESUME_ACK;
643 
644 	spin_lock_init(&priv->cmdreg_lock);
645 
646 	if (sizeof_priv)
647 		priv->priv = (void *)priv + sizeof(struct sja1000_priv);
648 
649 	return dev;
650 }
651 EXPORT_SYMBOL_GPL(alloc_sja1000dev);
652 
653 void free_sja1000dev(struct net_device *dev)
654 {
655 	free_candev(dev);
656 }
657 EXPORT_SYMBOL_GPL(free_sja1000dev);
658 
659 static const struct net_device_ops sja1000_netdev_ops = {
660 	.ndo_open	= sja1000_open,
661 	.ndo_stop	= sja1000_close,
662 	.ndo_start_xmit	= sja1000_start_xmit,
663 	.ndo_change_mtu	= can_change_mtu,
664 };
665 
666 int register_sja1000dev(struct net_device *dev)
667 {
668 	int ret;
669 
670 	if (!sja1000_probe_chip(dev))
671 		return -ENODEV;
672 
673 	dev->flags |= IFF_ECHO;	/* we support local echo */
674 	dev->netdev_ops = &sja1000_netdev_ops;
675 
676 	set_reset_mode(dev);
677 	chipset_init(dev);
678 
679 	ret =  register_candev(dev);
680 
681 	if (!ret)
682 		devm_can_led_init(dev);
683 
684 	return ret;
685 }
686 EXPORT_SYMBOL_GPL(register_sja1000dev);
687 
688 void unregister_sja1000dev(struct net_device *dev)
689 {
690 	set_reset_mode(dev);
691 	unregister_candev(dev);
692 }
693 EXPORT_SYMBOL_GPL(unregister_sja1000dev);
694 
695 static __init int sja1000_init(void)
696 {
697 	printk(KERN_INFO "%s CAN netdevice driver\n", DRV_NAME);
698 
699 	return 0;
700 }
701 
702 module_init(sja1000_init);
703 
704 static __exit void sja1000_exit(void)
705 {
706 	printk(KERN_INFO "%s: driver removed\n", DRV_NAME);
707 }
708 
709 module_exit(sja1000_exit);
710