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 
sja1000_write_cmdreg(struct sja1000_priv * priv,u8 val)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 
sja1000_is_absent(struct sja1000_priv * priv)98 static int sja1000_is_absent(struct sja1000_priv *priv)
99 {
100 	return (priv->read_reg(priv, SJA1000_MOD) == 0xFF);
101 }
102 
sja1000_probe_chip(struct net_device * dev)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 
set_reset_mode(struct net_device * dev)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 
set_normal_mode(struct net_device * dev)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  */
chipset_init(struct net_device * dev)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 
sja1000_start(struct net_device * dev)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 
sja1000_set_mode(struct net_device * dev,enum can_mode mode)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 
sja1000_set_bittiming(struct net_device * dev)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 
sja1000_get_berr_counter(const struct net_device * dev,struct can_berr_counter * bec)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  */
sja1000_start_xmit(struct sk_buff * skb,struct net_device * dev)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 
sja1000_rx(struct net_device * dev)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 
sja1000_err(struct net_device * dev,uint8_t isrc,uint8_t status)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 	if (isrc & IRQ_DOI) {
406 		/* data overrun interrupt */
407 		netdev_dbg(dev, "data overrun interrupt\n");
408 		cf->can_id |= CAN_ERR_CRTL;
409 		cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
410 		stats->rx_over_errors++;
411 		stats->rx_errors++;
412 		sja1000_write_cmdreg(priv, CMD_CDO);	/* clear bit */
413 	}
414 
415 	if (isrc & IRQ_EI) {
416 		/* error warning interrupt */
417 		netdev_dbg(dev, "error warning interrupt\n");
418 
419 		if (status & SR_BS)
420 			state = CAN_STATE_BUS_OFF;
421 		else if (status & SR_ES)
422 			state = CAN_STATE_ERROR_WARNING;
423 		else
424 			state = CAN_STATE_ERROR_ACTIVE;
425 	}
426 	if (state != CAN_STATE_BUS_OFF) {
427 		cf->data[6] = txerr;
428 		cf->data[7] = rxerr;
429 	}
430 	if (isrc & IRQ_BEI) {
431 		/* bus error interrupt */
432 		priv->can.can_stats.bus_error++;
433 		stats->rx_errors++;
434 
435 		ecc = priv->read_reg(priv, SJA1000_ECC);
436 
437 		cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
438 
439 		/* set error type */
440 		switch (ecc & ECC_MASK) {
441 		case ECC_BIT:
442 			cf->data[2] |= CAN_ERR_PROT_BIT;
443 			break;
444 		case ECC_FORM:
445 			cf->data[2] |= CAN_ERR_PROT_FORM;
446 			break;
447 		case ECC_STUFF:
448 			cf->data[2] |= CAN_ERR_PROT_STUFF;
449 			break;
450 		default:
451 			break;
452 		}
453 
454 		/* set error location */
455 		cf->data[3] = ecc & ECC_SEG;
456 
457 		/* Error occurred during transmission? */
458 		if ((ecc & ECC_DIR) == 0)
459 			cf->data[2] |= CAN_ERR_PROT_TX;
460 	}
461 	if (isrc & IRQ_EPI) {
462 		/* error passive interrupt */
463 		netdev_dbg(dev, "error passive interrupt\n");
464 
465 		if (state == CAN_STATE_ERROR_PASSIVE)
466 			state = CAN_STATE_ERROR_WARNING;
467 		else
468 			state = CAN_STATE_ERROR_PASSIVE;
469 	}
470 	if (isrc & IRQ_ALI) {
471 		/* arbitration lost interrupt */
472 		netdev_dbg(dev, "arbitration lost interrupt\n");
473 		alc = priv->read_reg(priv, SJA1000_ALC);
474 		priv->can.can_stats.arbitration_lost++;
475 		cf->can_id |= CAN_ERR_LOSTARB;
476 		cf->data[0] = alc & 0x1f;
477 	}
478 
479 	if (state != priv->can.state) {
480 		tx_state = txerr >= rxerr ? state : 0;
481 		rx_state = txerr <= rxerr ? state : 0;
482 
483 		can_change_state(dev, cf, tx_state, rx_state);
484 
485 		if(state == CAN_STATE_BUS_OFF)
486 			can_bus_off(dev);
487 	}
488 
489 	netif_rx(skb);
490 
491 	return 0;
492 }
493 
sja1000_interrupt(int irq,void * dev_id)494 irqreturn_t sja1000_interrupt(int irq, void *dev_id)
495 {
496 	struct net_device *dev = (struct net_device *)dev_id;
497 	struct sja1000_priv *priv = netdev_priv(dev);
498 	struct net_device_stats *stats = &dev->stats;
499 	uint8_t isrc, status;
500 	int n = 0;
501 
502 	if (priv->pre_irq)
503 		priv->pre_irq(priv);
504 
505 	/* Shared interrupts and IRQ off? */
506 	if (priv->read_reg(priv, SJA1000_IER) == IRQ_OFF)
507 		goto out;
508 
509 	while ((isrc = priv->read_reg(priv, SJA1000_IR)) &&
510 	       (n < SJA1000_MAX_IRQ)) {
511 
512 		status = priv->read_reg(priv, SJA1000_SR);
513 		/* check for absent controller due to hw unplug */
514 		if (status == 0xFF && sja1000_is_absent(priv))
515 			goto out;
516 
517 		if (isrc & IRQ_WUI)
518 			netdev_warn(dev, "wakeup interrupt\n");
519 
520 		if (isrc & IRQ_TI) {
521 			/* transmission buffer released */
522 			if (priv->can.ctrlmode & CAN_CTRLMODE_ONE_SHOT &&
523 			    !(status & SR_TCS)) {
524 				stats->tx_errors++;
525 				can_free_echo_skb(dev, 0, NULL);
526 			} else {
527 				/* transmission complete */
528 				stats->tx_bytes += can_get_echo_skb(dev, 0, NULL);
529 				stats->tx_packets++;
530 			}
531 			netif_wake_queue(dev);
532 		}
533 		if (isrc & IRQ_RI) {
534 			/* receive interrupt */
535 			while (status & SR_RBS) {
536 				sja1000_rx(dev);
537 				status = priv->read_reg(priv, SJA1000_SR);
538 				/* check for absent controller */
539 				if (status == 0xFF && sja1000_is_absent(priv))
540 					goto out;
541 			}
542 		}
543 		if (isrc & (IRQ_DOI | IRQ_EI | IRQ_BEI | IRQ_EPI | IRQ_ALI)) {
544 			/* error interrupt */
545 			if (sja1000_err(dev, isrc, status))
546 				break;
547 		}
548 		n++;
549 	}
550 out:
551 	if (priv->post_irq)
552 		priv->post_irq(priv);
553 
554 	if (n >= SJA1000_MAX_IRQ)
555 		netdev_dbg(dev, "%d messages handled in ISR", n);
556 
557 	return (n) ? IRQ_HANDLED : IRQ_NONE;
558 }
559 EXPORT_SYMBOL_GPL(sja1000_interrupt);
560 
sja1000_open(struct net_device * dev)561 static int sja1000_open(struct net_device *dev)
562 {
563 	struct sja1000_priv *priv = netdev_priv(dev);
564 	int err;
565 
566 	/* set chip into reset mode */
567 	set_reset_mode(dev);
568 
569 	/* common open */
570 	err = open_candev(dev);
571 	if (err)
572 		return err;
573 
574 	/* register interrupt handler, if not done by the device driver */
575 	if (!(priv->flags & SJA1000_CUSTOM_IRQ_HANDLER)) {
576 		err = request_irq(dev->irq, sja1000_interrupt, priv->irq_flags,
577 				  dev->name, (void *)dev);
578 		if (err) {
579 			close_candev(dev);
580 			return -EAGAIN;
581 		}
582 	}
583 
584 	/* init and start chi */
585 	sja1000_start(dev);
586 
587 	netif_start_queue(dev);
588 
589 	return 0;
590 }
591 
sja1000_close(struct net_device * dev)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 	return 0;
605 }
606 
alloc_sja1000dev(int sizeof_priv)607 struct net_device *alloc_sja1000dev(int sizeof_priv)
608 {
609 	struct net_device *dev;
610 	struct sja1000_priv *priv;
611 
612 	dev = alloc_candev(sizeof(struct sja1000_priv) + sizeof_priv,
613 		SJA1000_ECHO_SKB_MAX);
614 	if (!dev)
615 		return NULL;
616 
617 	priv = netdev_priv(dev);
618 
619 	priv->dev = dev;
620 	priv->can.bittiming_const = &sja1000_bittiming_const;
621 	priv->can.do_set_bittiming = sja1000_set_bittiming;
622 	priv->can.do_set_mode = sja1000_set_mode;
623 	priv->can.do_get_berr_counter = sja1000_get_berr_counter;
624 	priv->can.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK |
625 				       CAN_CTRLMODE_LISTENONLY |
626 				       CAN_CTRLMODE_3_SAMPLES |
627 				       CAN_CTRLMODE_ONE_SHOT |
628 				       CAN_CTRLMODE_BERR_REPORTING |
629 				       CAN_CTRLMODE_PRESUME_ACK |
630 				       CAN_CTRLMODE_CC_LEN8_DLC;
631 
632 	spin_lock_init(&priv->cmdreg_lock);
633 
634 	if (sizeof_priv)
635 		priv->priv = (void *)priv + sizeof(struct sja1000_priv);
636 
637 	return dev;
638 }
639 EXPORT_SYMBOL_GPL(alloc_sja1000dev);
640 
free_sja1000dev(struct net_device * dev)641 void free_sja1000dev(struct net_device *dev)
642 {
643 	free_candev(dev);
644 }
645 EXPORT_SYMBOL_GPL(free_sja1000dev);
646 
647 static const struct net_device_ops sja1000_netdev_ops = {
648 	.ndo_open	= sja1000_open,
649 	.ndo_stop	= sja1000_close,
650 	.ndo_start_xmit	= sja1000_start_xmit,
651 	.ndo_change_mtu	= can_change_mtu,
652 };
653 
register_sja1000dev(struct net_device * dev)654 int register_sja1000dev(struct net_device *dev)
655 {
656 	int ret;
657 
658 	if (!sja1000_probe_chip(dev))
659 		return -ENODEV;
660 
661 	dev->flags |= IFF_ECHO;	/* we support local echo */
662 	dev->netdev_ops = &sja1000_netdev_ops;
663 
664 	set_reset_mode(dev);
665 	chipset_init(dev);
666 
667 	ret =  register_candev(dev);
668 
669 	return ret;
670 }
671 EXPORT_SYMBOL_GPL(register_sja1000dev);
672 
unregister_sja1000dev(struct net_device * dev)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 
sja1000_init(void)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 
sja1000_exit(void)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