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