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