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