xref: /linux/drivers/net/can/sja1000/sja1000.c (revision 3e5c291c7942d0909a48bc5ec1b9bba136465166)
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 
64 #include "sja1000.h"
65 
66 #define DRV_NAME "sja1000"
67 
68 MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
69 MODULE_LICENSE("Dual BSD/GPL");
70 MODULE_DESCRIPTION(DRV_NAME "CAN netdevice driver");
71 
72 static const struct can_bittiming_const sja1000_bittiming_const = {
73 	.name = DRV_NAME,
74 	.tseg1_min = 1,
75 	.tseg1_max = 16,
76 	.tseg2_min = 1,
77 	.tseg2_max = 8,
78 	.sjw_max = 4,
79 	.brp_min = 1,
80 	.brp_max = 64,
81 	.brp_inc = 1,
82 };
83 
84 static void sja1000_write_cmdreg(struct sja1000_priv *priv, u8 val)
85 {
86 	unsigned long flags;
87 
88 	/*
89 	 * The command register needs some locking and time to settle
90 	 * the write_reg() operation - especially on SMP systems.
91 	 */
92 	spin_lock_irqsave(&priv->cmdreg_lock, flags);
93 	priv->write_reg(priv, SJA1000_CMR, val);
94 	priv->read_reg(priv, SJA1000_SR);
95 	spin_unlock_irqrestore(&priv->cmdreg_lock, flags);
96 }
97 
98 static int sja1000_is_absent(struct sja1000_priv *priv)
99 {
100 	return (priv->read_reg(priv, SJA1000_MOD) == 0xFF);
101 }
102 
103 static int sja1000_probe_chip(struct net_device *dev)
104 {
105 	struct sja1000_priv *priv = netdev_priv(dev);
106 
107 	if (priv->reg_base && sja1000_is_absent(priv)) {
108 		netdev_err(dev, "probing failed\n");
109 		return 0;
110 	}
111 	return -1;
112 }
113 
114 static void set_reset_mode(struct net_device *dev)
115 {
116 	struct sja1000_priv *priv = netdev_priv(dev);
117 	unsigned char status = priv->read_reg(priv, SJA1000_MOD);
118 	int i;
119 
120 	/* disable interrupts */
121 	priv->write_reg(priv, SJA1000_IER, IRQ_OFF);
122 
123 	for (i = 0; i < 100; i++) {
124 		/* check reset bit */
125 		if (status & MOD_RM) {
126 			priv->can.state = CAN_STATE_STOPPED;
127 			return;
128 		}
129 
130 		/* reset chip */
131 		priv->write_reg(priv, SJA1000_MOD, MOD_RM);
132 		udelay(10);
133 		status = priv->read_reg(priv, SJA1000_MOD);
134 	}
135 
136 	netdev_err(dev, "setting SJA1000 into reset mode failed!\n");
137 }
138 
139 static void set_normal_mode(struct net_device *dev)
140 {
141 	struct sja1000_priv *priv = netdev_priv(dev);
142 	unsigned char status = priv->read_reg(priv, SJA1000_MOD);
143 	u8 mod_reg_val = 0x00;
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 			mod_reg_val |= MOD_LOM;
162 		if (priv->can.ctrlmode & CAN_CTRLMODE_PRESUME_ACK)
163 			mod_reg_val |= MOD_STM;
164 		priv->write_reg(priv, SJA1000_MOD, mod_reg_val);
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 /*
175  * initialize SJA1000 chip:
176  *   - reset chip
177  *   - set output mode
178  *   - set baudrate
179  *   - enable interrupts
180  *   - start operating mode
181  */
182 static void chipset_init(struct net_device *dev)
183 {
184 	struct sja1000_priv *priv = netdev_priv(dev);
185 
186 	if (!(priv->flags & SJA1000_QUIRK_NO_CDR_REG))
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->flags & SJA1000_QUIRK_NO_CDR_REG ||
214 	      priv->read_reg(priv, SJA1000_CDR) & CDR_PELICAN))
215 		chipset_init(dev);
216 
217 	/* Clear error counters and error code capture */
218 	priv->write_reg(priv, SJA1000_TXERR, 0x0);
219 	priv->write_reg(priv, SJA1000_RXERR, 0x0);
220 	priv->read_reg(priv, SJA1000_ECC);
221 
222 	/* clear interrupt flags */
223 	priv->read_reg(priv, SJA1000_IR);
224 
225 	/* leave reset mode */
226 	set_normal_mode(dev);
227 }
228 
229 static int sja1000_set_mode(struct net_device *dev, enum can_mode mode)
230 {
231 	switch (mode) {
232 	case CAN_MODE_START:
233 		sja1000_start(dev);
234 		if (netif_queue_stopped(dev))
235 			netif_wake_queue(dev);
236 		break;
237 
238 	default:
239 		return -EOPNOTSUPP;
240 	}
241 
242 	return 0;
243 }
244 
245 static int sja1000_set_bittiming(struct net_device *dev)
246 {
247 	struct sja1000_priv *priv = netdev_priv(dev);
248 	struct can_bittiming *bt = &priv->can.bittiming;
249 	u8 btr0, btr1;
250 
251 	btr0 = ((bt->brp - 1) & 0x3f) | (((bt->sjw - 1) & 0x3) << 6);
252 	btr1 = ((bt->prop_seg + bt->phase_seg1 - 1) & 0xf) |
253 		(((bt->phase_seg2 - 1) & 0x7) << 4);
254 	if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
255 		btr1 |= 0x80;
256 
257 	netdev_info(dev, "setting BTR0=0x%02x BTR1=0x%02x\n", btr0, btr1);
258 
259 	priv->write_reg(priv, SJA1000_BTR0, btr0);
260 	priv->write_reg(priv, SJA1000_BTR1, btr1);
261 
262 	return 0;
263 }
264 
265 static int sja1000_get_berr_counter(const struct net_device *dev,
266 				    struct can_berr_counter *bec)
267 {
268 	struct sja1000_priv *priv = netdev_priv(dev);
269 
270 	bec->txerr = priv->read_reg(priv, SJA1000_TXERR);
271 	bec->rxerr = priv->read_reg(priv, SJA1000_RXERR);
272 
273 	return 0;
274 }
275 
276 /*
277  * transmit a CAN message
278  * message layout in the sk_buff should be like this:
279  * xx xx xx xx	 ff	 ll   00 11 22 33 44 55 66 77
280  * [  can-id ] [flags] [len] [can data (up to 8 bytes]
281  */
282 static netdev_tx_t sja1000_start_xmit(struct sk_buff *skb,
283 					    struct net_device *dev)
284 {
285 	struct sja1000_priv *priv = netdev_priv(dev);
286 	struct can_frame *cf = (struct can_frame *)skb->data;
287 	uint8_t fi;
288 	canid_t id;
289 	uint8_t dreg;
290 	u8 cmd_reg_val = 0x00;
291 	int i;
292 
293 	if (can_dropped_invalid_skb(dev, skb))
294 		return NETDEV_TX_OK;
295 
296 	netif_stop_queue(dev);
297 
298 	fi = can_get_cc_dlc(cf, priv->can.ctrlmode);
299 	id = cf->can_id;
300 
301 	if (id & CAN_RTR_FLAG)
302 		fi |= SJA1000_FI_RTR;
303 
304 	if (id & CAN_EFF_FLAG) {
305 		fi |= SJA1000_FI_FF;
306 		dreg = SJA1000_EFF_BUF;
307 		priv->write_reg(priv, SJA1000_FI, fi);
308 		priv->write_reg(priv, SJA1000_ID1, (id & 0x1fe00000) >> 21);
309 		priv->write_reg(priv, SJA1000_ID2, (id & 0x001fe000) >> 13);
310 		priv->write_reg(priv, SJA1000_ID3, (id & 0x00001fe0) >> 5);
311 		priv->write_reg(priv, SJA1000_ID4, (id & 0x0000001f) << 3);
312 	} else {
313 		dreg = SJA1000_SFF_BUF;
314 		priv->write_reg(priv, SJA1000_FI, fi);
315 		priv->write_reg(priv, SJA1000_ID1, (id & 0x000007f8) >> 3);
316 		priv->write_reg(priv, SJA1000_ID2, (id & 0x00000007) << 5);
317 	}
318 
319 	for (i = 0; i < cf->len; i++)
320 		priv->write_reg(priv, dreg++, cf->data[i]);
321 
322 	can_put_echo_skb(skb, dev, 0, 0);
323 
324 	if (priv->can.ctrlmode & CAN_CTRLMODE_ONE_SHOT)
325 		cmd_reg_val |= CMD_AT;
326 
327 	if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK)
328 		cmd_reg_val |= CMD_SRR;
329 	else
330 		cmd_reg_val |= CMD_TR;
331 
332 	sja1000_write_cmdreg(priv, cmd_reg_val);
333 
334 	return NETDEV_TX_OK;
335 }
336 
337 static void sja1000_rx(struct net_device *dev)
338 {
339 	struct sja1000_priv *priv = netdev_priv(dev);
340 	struct net_device_stats *stats = &dev->stats;
341 	struct can_frame *cf;
342 	struct sk_buff *skb;
343 	uint8_t fi;
344 	uint8_t dreg;
345 	canid_t id;
346 	int i;
347 
348 	/* create zero'ed CAN frame buffer */
349 	skb = alloc_can_skb(dev, &cf);
350 	if (skb == NULL)
351 		return;
352 
353 	fi = priv->read_reg(priv, SJA1000_FI);
354 
355 	if (fi & SJA1000_FI_FF) {
356 		/* extended frame format (EFF) */
357 		dreg = SJA1000_EFF_BUF;
358 		id = (priv->read_reg(priv, SJA1000_ID1) << 21)
359 		    | (priv->read_reg(priv, SJA1000_ID2) << 13)
360 		    | (priv->read_reg(priv, SJA1000_ID3) << 5)
361 		    | (priv->read_reg(priv, SJA1000_ID4) >> 3);
362 		id |= CAN_EFF_FLAG;
363 	} else {
364 		/* standard frame format (SFF) */
365 		dreg = SJA1000_SFF_BUF;
366 		id = (priv->read_reg(priv, SJA1000_ID1) << 3)
367 		    | (priv->read_reg(priv, SJA1000_ID2) >> 5);
368 	}
369 
370 	can_frame_set_cc_len(cf, fi & 0x0F, priv->can.ctrlmode);
371 	if (fi & SJA1000_FI_RTR) {
372 		id |= CAN_RTR_FLAG;
373 	} else {
374 		for (i = 0; i < cf->len; i++)
375 			cf->data[i] = priv->read_reg(priv, dreg++);
376 
377 		stats->rx_bytes += cf->len;
378 	}
379 	stats->rx_packets++;
380 
381 	cf->can_id = id;
382 
383 	/* release receive buffer */
384 	sja1000_write_cmdreg(priv, CMD_RRB);
385 
386 	netif_rx(skb);
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 	if (isrc & IRQ_DOI) {
408 		/* data overrun interrupt */
409 		netdev_dbg(dev, "data overrun interrupt\n");
410 		cf->can_id |= CAN_ERR_CRTL;
411 		cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
412 		stats->rx_over_errors++;
413 		stats->rx_errors++;
414 		sja1000_write_cmdreg(priv, CMD_CDO);	/* clear bit */
415 	}
416 
417 	if (isrc & IRQ_EI) {
418 		/* error warning interrupt */
419 		netdev_dbg(dev, "error warning interrupt\n");
420 
421 		if (status & SR_BS)
422 			state = CAN_STATE_BUS_OFF;
423 		else if (status & SR_ES)
424 			state = CAN_STATE_ERROR_WARNING;
425 		else
426 			state = CAN_STATE_ERROR_ACTIVE;
427 	}
428 	if (state != CAN_STATE_BUS_OFF) {
429 		cf->can_id |= CAN_ERR_CNT;
430 		cf->data[6] = txerr;
431 		cf->data[7] = rxerr;
432 	}
433 	if (isrc & IRQ_BEI) {
434 		/* bus error interrupt */
435 		priv->can.can_stats.bus_error++;
436 		stats->rx_errors++;
437 
438 		ecc = priv->read_reg(priv, SJA1000_ECC);
439 
440 		cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
441 
442 		/* set error type */
443 		switch (ecc & ECC_MASK) {
444 		case ECC_BIT:
445 			cf->data[2] |= CAN_ERR_PROT_BIT;
446 			break;
447 		case ECC_FORM:
448 			cf->data[2] |= CAN_ERR_PROT_FORM;
449 			break;
450 		case ECC_STUFF:
451 			cf->data[2] |= CAN_ERR_PROT_STUFF;
452 			break;
453 		default:
454 			break;
455 		}
456 
457 		/* set error location */
458 		cf->data[3] = ecc & ECC_SEG;
459 
460 		/* Error occurred during transmission? */
461 		if ((ecc & ECC_DIR) == 0)
462 			cf->data[2] |= CAN_ERR_PROT_TX;
463 	}
464 	if (isrc & IRQ_EPI) {
465 		/* error passive interrupt */
466 		netdev_dbg(dev, "error passive interrupt\n");
467 
468 		if (state == CAN_STATE_ERROR_PASSIVE)
469 			state = CAN_STATE_ERROR_WARNING;
470 		else
471 			state = CAN_STATE_ERROR_PASSIVE;
472 	}
473 	if (isrc & IRQ_ALI) {
474 		/* arbitration lost interrupt */
475 		netdev_dbg(dev, "arbitration lost interrupt\n");
476 		alc = priv->read_reg(priv, SJA1000_ALC);
477 		priv->can.can_stats.arbitration_lost++;
478 		cf->can_id |= CAN_ERR_LOSTARB;
479 		cf->data[0] = alc & 0x1f;
480 	}
481 
482 	if (state != priv->can.state) {
483 		tx_state = txerr >= rxerr ? state : 0;
484 		rx_state = txerr <= rxerr ? state : 0;
485 
486 		can_change_state(dev, cf, tx_state, rx_state);
487 
488 		if(state == CAN_STATE_BUS_OFF)
489 			can_bus_off(dev);
490 	}
491 
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 += can_get_echo_skb(dev, 0, NULL);
532 				stats->tx_packets++;
533 			}
534 			netif_wake_queue(dev);
535 		}
536 		if (isrc & IRQ_RI) {
537 			/* receive interrupt */
538 			while (status & SR_RBS) {
539 				sja1000_rx(dev);
540 				status = priv->read_reg(priv, SJA1000_SR);
541 				/* check for absent controller */
542 				if (status == 0xFF && sja1000_is_absent(priv))
543 					goto out;
544 			}
545 		}
546 		if (isrc & (IRQ_DOI | IRQ_EI | IRQ_BEI | IRQ_EPI | IRQ_ALI)) {
547 			/* error interrupt */
548 			if (sja1000_err(dev, isrc, status))
549 				break;
550 		}
551 		n++;
552 	}
553 out:
554 	if (priv->post_irq)
555 		priv->post_irq(priv);
556 
557 	if (n >= SJA1000_MAX_IRQ)
558 		netdev_dbg(dev, "%d messages handled in ISR", n);
559 
560 	return (n) ? IRQ_HANDLED : IRQ_NONE;
561 }
562 EXPORT_SYMBOL_GPL(sja1000_interrupt);
563 
564 static int sja1000_open(struct net_device *dev)
565 {
566 	struct sja1000_priv *priv = netdev_priv(dev);
567 	int err;
568 
569 	/* set chip into reset mode */
570 	set_reset_mode(dev);
571 
572 	/* common open */
573 	err = open_candev(dev);
574 	if (err)
575 		return err;
576 
577 	/* register interrupt handler, if not done by the device driver */
578 	if (!(priv->flags & SJA1000_CUSTOM_IRQ_HANDLER)) {
579 		err = request_irq(dev->irq, sja1000_interrupt, priv->irq_flags,
580 				  dev->name, (void *)dev);
581 		if (err) {
582 			close_candev(dev);
583 			return -EAGAIN;
584 		}
585 	}
586 
587 	/* init and start chi */
588 	sja1000_start(dev);
589 
590 	netif_start_queue(dev);
591 
592 	return 0;
593 }
594 
595 static int sja1000_close(struct net_device *dev)
596 {
597 	struct sja1000_priv *priv = netdev_priv(dev);
598 
599 	netif_stop_queue(dev);
600 	set_reset_mode(dev);
601 
602 	if (!(priv->flags & SJA1000_CUSTOM_IRQ_HANDLER))
603 		free_irq(dev->irq, (void *)dev);
604 
605 	close_candev(dev);
606 
607 	return 0;
608 }
609 
610 struct net_device *alloc_sja1000dev(int sizeof_priv)
611 {
612 	struct net_device *dev;
613 	struct sja1000_priv *priv;
614 
615 	dev = alloc_candev(sizeof(struct sja1000_priv) + sizeof_priv,
616 		SJA1000_ECHO_SKB_MAX);
617 	if (!dev)
618 		return NULL;
619 
620 	priv = netdev_priv(dev);
621 
622 	priv->dev = dev;
623 	priv->can.bittiming_const = &sja1000_bittiming_const;
624 	priv->can.do_set_bittiming = sja1000_set_bittiming;
625 	priv->can.do_set_mode = sja1000_set_mode;
626 	priv->can.do_get_berr_counter = sja1000_get_berr_counter;
627 	priv->can.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK |
628 				       CAN_CTRLMODE_LISTENONLY |
629 				       CAN_CTRLMODE_3_SAMPLES |
630 				       CAN_CTRLMODE_ONE_SHOT |
631 				       CAN_CTRLMODE_BERR_REPORTING |
632 				       CAN_CTRLMODE_PRESUME_ACK |
633 				       CAN_CTRLMODE_CC_LEN8_DLC;
634 
635 	spin_lock_init(&priv->cmdreg_lock);
636 
637 	if (sizeof_priv)
638 		priv->priv = (void *)priv + sizeof(struct sja1000_priv);
639 
640 	return dev;
641 }
642 EXPORT_SYMBOL_GPL(alloc_sja1000dev);
643 
644 void free_sja1000dev(struct net_device *dev)
645 {
646 	free_candev(dev);
647 }
648 EXPORT_SYMBOL_GPL(free_sja1000dev);
649 
650 static const struct net_device_ops sja1000_netdev_ops = {
651 	.ndo_open	= sja1000_open,
652 	.ndo_stop	= sja1000_close,
653 	.ndo_start_xmit	= sja1000_start_xmit,
654 	.ndo_change_mtu	= can_change_mtu,
655 };
656 
657 int register_sja1000dev(struct net_device *dev)
658 {
659 	int ret;
660 
661 	if (!sja1000_probe_chip(dev))
662 		return -ENODEV;
663 
664 	dev->flags |= IFF_ECHO;	/* we support local echo */
665 	dev->netdev_ops = &sja1000_netdev_ops;
666 
667 	set_reset_mode(dev);
668 	chipset_init(dev);
669 
670 	ret =  register_candev(dev);
671 
672 	return ret;
673 }
674 EXPORT_SYMBOL_GPL(register_sja1000dev);
675 
676 void unregister_sja1000dev(struct net_device *dev)
677 {
678 	set_reset_mode(dev);
679 	unregister_candev(dev);
680 }
681 EXPORT_SYMBOL_GPL(unregister_sja1000dev);
682 
683 static __init int sja1000_init(void)
684 {
685 	printk(KERN_INFO "%s CAN netdevice driver\n", DRV_NAME);
686 
687 	return 0;
688 }
689 
690 module_init(sja1000_init);
691 
692 static __exit void sja1000_exit(void)
693 {
694 	printk(KERN_INFO "%s: driver removed\n", DRV_NAME);
695 }
696 
697 module_exit(sja1000_exit);
698