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