• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * CAN driver for PEAK System USB adapters
3  * Derived from the PCAN project file driver/src/pcan_usb_core.c
4  *
5  * Copyright (C) 2003-2010 PEAK System-Technik GmbH
6  * Copyright (C) 2010-2012 Stephane Grosjean <s.grosjean@peak-system.com>
7  *
8  * Many thanks to Klaus Hitschler <klaus.hitschler@gmx.de>
9  *
10  * This program is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU General Public License as published
12  * by the Free Software Foundation; version 2 of the License.
13  *
14  * This program is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * General Public License for more details.
18  */
19 #include <linux/init.h>
20 #include <linux/signal.h>
21 #include <linux/slab.h>
22 #include <linux/module.h>
23 #include <linux/netdevice.h>
24 #include <linux/usb.h>
25 
26 #include <linux/can.h>
27 #include <linux/can/dev.h>
28 #include <linux/can/error.h>
29 
30 #include "pcan_usb_core.h"
31 
32 MODULE_AUTHOR("Stephane Grosjean <s.grosjean@peak-system.com>");
33 MODULE_DESCRIPTION("CAN driver for PEAK-System USB adapters");
34 MODULE_LICENSE("GPL v2");
35 
36 /* Table of devices that work with this driver */
37 static struct usb_device_id peak_usb_table[] = {
38 	{USB_DEVICE(PCAN_USB_VENDOR_ID, PCAN_USB_PRODUCT_ID)},
39 	{USB_DEVICE(PCAN_USB_VENDOR_ID, PCAN_USBPRO_PRODUCT_ID)},
40 	{} /* Terminating entry */
41 };
42 
43 MODULE_DEVICE_TABLE(usb, peak_usb_table);
44 
45 /* List of supported PCAN-USB adapters (NULL terminated list) */
46 static struct peak_usb_adapter *peak_usb_adapters_list[] = {
47 	&pcan_usb,
48 	&pcan_usb_pro,
49 	NULL,
50 };
51 
52 /*
53  * dump memory
54  */
55 #define DUMP_WIDTH	16
pcan_dump_mem(char * prompt,void * p,int l)56 void pcan_dump_mem(char *prompt, void *p, int l)
57 {
58 	pr_info("%s dumping %s (%d bytes):\n",
59 		PCAN_USB_DRIVER_NAME, prompt ? prompt : "memory", l);
60 	print_hex_dump(KERN_INFO, PCAN_USB_DRIVER_NAME " ", DUMP_PREFIX_NONE,
61 		       DUMP_WIDTH, 1, p, l, false);
62 }
63 
64 /*
65  * initialize a time_ref object with usb adapter own settings
66  */
peak_usb_init_time_ref(struct peak_time_ref * time_ref,struct peak_usb_adapter * adapter)67 void peak_usb_init_time_ref(struct peak_time_ref *time_ref,
68 			    struct peak_usb_adapter *adapter)
69 {
70 	if (time_ref) {
71 		memset(time_ref, 0, sizeof(struct peak_time_ref));
72 		time_ref->adapter = adapter;
73 	}
74 }
75 
peak_usb_add_us(struct timeval * tv,u32 delta_us)76 static void peak_usb_add_us(struct timeval *tv, u32 delta_us)
77 {
78 	/* number of s. to add to final time */
79 	u32 delta_s = delta_us / 1000000;
80 
81 	delta_us -= delta_s * 1000000;
82 
83 	tv->tv_usec += delta_us;
84 	if (tv->tv_usec >= 1000000) {
85 		tv->tv_usec -= 1000000;
86 		delta_s++;
87 	}
88 	tv->tv_sec += delta_s;
89 }
90 
91 /*
92  * sometimes, another now may be  more recent than current one...
93  */
peak_usb_update_ts_now(struct peak_time_ref * time_ref,u32 ts_now)94 void peak_usb_update_ts_now(struct peak_time_ref *time_ref, u32 ts_now)
95 {
96 	time_ref->ts_dev_2 = ts_now;
97 
98 	/* should wait at least two passes before computing */
99 	if (time_ref->tv_host.tv_sec > 0) {
100 		u32 delta_ts = time_ref->ts_dev_2 - time_ref->ts_dev_1;
101 
102 		if (time_ref->ts_dev_2 < time_ref->ts_dev_1)
103 			delta_ts &= (1 << time_ref->adapter->ts_used_bits) - 1;
104 
105 		time_ref->ts_total += delta_ts;
106 	}
107 }
108 
109 /*
110  * register device timestamp as now
111  */
peak_usb_set_ts_now(struct peak_time_ref * time_ref,u32 ts_now)112 void peak_usb_set_ts_now(struct peak_time_ref *time_ref, u32 ts_now)
113 {
114 	if (time_ref->tv_host_0.tv_sec == 0) {
115 		/* use monotonic clock to correctly compute further deltas */
116 		time_ref->tv_host_0 = ktime_to_timeval(ktime_get());
117 		time_ref->tv_host.tv_sec = 0;
118 	} else {
119 		/*
120 		 * delta_us should not be >= 2^32 => delta_s should be < 4294
121 		 * handle 32-bits wrapping here: if count of s. reaches 4200,
122 		 * reset counters and change time base
123 		 */
124 		if (time_ref->tv_host.tv_sec != 0) {
125 			u32 delta_s = time_ref->tv_host.tv_sec
126 						- time_ref->tv_host_0.tv_sec;
127 			if (delta_s > 4200) {
128 				time_ref->tv_host_0 = time_ref->tv_host;
129 				time_ref->ts_total = 0;
130 			}
131 		}
132 
133 		time_ref->tv_host = ktime_to_timeval(ktime_get());
134 		time_ref->tick_count++;
135 	}
136 
137 	time_ref->ts_dev_1 = time_ref->ts_dev_2;
138 	peak_usb_update_ts_now(time_ref, ts_now);
139 }
140 
141 /*
142  * compute timeval according to current ts and time_ref data
143  */
peak_usb_get_ts_tv(struct peak_time_ref * time_ref,u32 ts,struct timeval * tv)144 void peak_usb_get_ts_tv(struct peak_time_ref *time_ref, u32 ts,
145 			struct timeval *tv)
146 {
147 	/* protect from getting timeval before setting now */
148 	if (time_ref->tv_host.tv_sec > 0) {
149 		u64 delta_us;
150 
151 		delta_us = ts - time_ref->ts_dev_2;
152 		if (ts < time_ref->ts_dev_2)
153 			delta_us &= (1 << time_ref->adapter->ts_used_bits) - 1;
154 
155 		delta_us += time_ref->ts_total;
156 
157 		delta_us *= time_ref->adapter->us_per_ts_scale;
158 		delta_us >>= time_ref->adapter->us_per_ts_shift;
159 
160 		*tv = time_ref->tv_host_0;
161 		peak_usb_add_us(tv, (u32)delta_us);
162 	} else {
163 		*tv = ktime_to_timeval(ktime_get());
164 	}
165 }
166 
167 /*
168  * callback for bulk Rx urb
169  */
peak_usb_read_bulk_callback(struct urb * urb)170 static void peak_usb_read_bulk_callback(struct urb *urb)
171 {
172 	struct peak_usb_device *dev = urb->context;
173 	struct net_device *netdev;
174 	int err;
175 
176 	netdev = dev->netdev;
177 
178 	if (!netif_device_present(netdev))
179 		return;
180 
181 	/* check reception status */
182 	switch (urb->status) {
183 	case 0:
184 		/* success */
185 		break;
186 
187 	case -EILSEQ:
188 	case -ENOENT:
189 	case -ECONNRESET:
190 	case -ESHUTDOWN:
191 		return;
192 
193 	default:
194 		if (net_ratelimit())
195 			netdev_err(netdev,
196 				   "Rx urb aborted (%d)\n", urb->status);
197 		goto resubmit_urb;
198 	}
199 
200 	/* protect from any incoming empty msgs */
201 	if ((urb->actual_length > 0) && (dev->adapter->dev_decode_buf)) {
202 		/* handle these kinds of msgs only if _start callback called */
203 		if (dev->state & PCAN_USB_STATE_STARTED) {
204 			err = dev->adapter->dev_decode_buf(dev, urb);
205 			if (err)
206 				pcan_dump_mem("received usb message",
207 					      urb->transfer_buffer,
208 					      urb->transfer_buffer_length);
209 		}
210 	}
211 
212 resubmit_urb:
213 	usb_fill_bulk_urb(urb, dev->udev,
214 		usb_rcvbulkpipe(dev->udev, dev->ep_msg_in),
215 		urb->transfer_buffer, dev->adapter->rx_buffer_size,
216 		peak_usb_read_bulk_callback, dev);
217 
218 	usb_anchor_urb(urb, &dev->rx_submitted);
219 	err = usb_submit_urb(urb, GFP_ATOMIC);
220 	if (!err)
221 		return;
222 
223 	usb_unanchor_urb(urb);
224 
225 	if (err == -ENODEV)
226 		netif_device_detach(netdev);
227 	else
228 		netdev_err(netdev, "failed resubmitting read bulk urb: %d\n",
229 			   err);
230 }
231 
232 /*
233  * callback for bulk Tx urb
234  */
peak_usb_write_bulk_callback(struct urb * urb)235 static void peak_usb_write_bulk_callback(struct urb *urb)
236 {
237 	struct peak_tx_urb_context *context = urb->context;
238 	struct peak_usb_device *dev;
239 	struct net_device *netdev;
240 
241 	BUG_ON(!context);
242 
243 	dev = context->dev;
244 	netdev = dev->netdev;
245 
246 	atomic_dec(&dev->active_tx_urbs);
247 
248 	if (!netif_device_present(netdev))
249 		return;
250 
251 	/* check tx status */
252 	switch (urb->status) {
253 	case 0:
254 		/* transmission complete */
255 		netdev->stats.tx_packets++;
256 		netdev->stats.tx_bytes += context->dlc;
257 
258 		/* prevent tx timeout */
259 		netdev->trans_start = jiffies;
260 		break;
261 
262 	default:
263 		if (net_ratelimit())
264 			netdev_err(netdev, "Tx urb aborted (%d)\n",
265 				   urb->status);
266 	case -EPROTO:
267 	case -ENOENT:
268 	case -ECONNRESET:
269 	case -ESHUTDOWN:
270 
271 		break;
272 	}
273 
274 	/* should always release echo skb and corresponding context */
275 	can_get_echo_skb(netdev, context->echo_index);
276 	context->echo_index = PCAN_USB_MAX_TX_URBS;
277 
278 	/* do wakeup tx queue in case of success only */
279 	if (!urb->status)
280 		netif_wake_queue(netdev);
281 }
282 
283 /*
284  * called by netdev to send one skb on the CAN interface.
285  */
peak_usb_ndo_start_xmit(struct sk_buff * skb,struct net_device * netdev)286 static netdev_tx_t peak_usb_ndo_start_xmit(struct sk_buff *skb,
287 					   struct net_device *netdev)
288 {
289 	struct peak_usb_device *dev = netdev_priv(netdev);
290 	struct peak_tx_urb_context *context = NULL;
291 	struct net_device_stats *stats = &netdev->stats;
292 	struct can_frame *cf = (struct can_frame *)skb->data;
293 	struct urb *urb;
294 	u8 *obuf;
295 	int i, err;
296 	size_t size = dev->adapter->tx_buffer_size;
297 
298 	if (can_dropped_invalid_skb(netdev, skb))
299 		return NETDEV_TX_OK;
300 
301 	for (i = 0; i < PCAN_USB_MAX_TX_URBS; i++)
302 		if (dev->tx_contexts[i].echo_index == PCAN_USB_MAX_TX_URBS) {
303 			context = dev->tx_contexts + i;
304 			break;
305 		}
306 
307 	if (!context) {
308 		/* should not occur except during restart */
309 		return NETDEV_TX_BUSY;
310 	}
311 
312 	urb = context->urb;
313 	obuf = urb->transfer_buffer;
314 
315 	err = dev->adapter->dev_encode_msg(dev, skb, obuf, &size);
316 	if (err) {
317 		if (net_ratelimit())
318 			netdev_err(netdev, "packet dropped\n");
319 		dev_kfree_skb(skb);
320 		stats->tx_dropped++;
321 		return NETDEV_TX_OK;
322 	}
323 
324 	context->echo_index = i;
325 	context->dlc = cf->can_dlc;
326 
327 	usb_anchor_urb(urb, &dev->tx_submitted);
328 
329 	can_put_echo_skb(skb, netdev, context->echo_index);
330 
331 	atomic_inc(&dev->active_tx_urbs);
332 
333 	err = usb_submit_urb(urb, GFP_ATOMIC);
334 	if (err) {
335 		can_free_echo_skb(netdev, context->echo_index);
336 
337 		usb_unanchor_urb(urb);
338 
339 		/* this context is not used in fact */
340 		context->echo_index = PCAN_USB_MAX_TX_URBS;
341 
342 		atomic_dec(&dev->active_tx_urbs);
343 
344 		switch (err) {
345 		case -ENODEV:
346 			netif_device_detach(netdev);
347 			break;
348 		default:
349 			netdev_warn(netdev, "tx urb submitting failed err=%d\n",
350 				    err);
351 		case -ENOENT:
352 			/* cable unplugged */
353 			stats->tx_dropped++;
354 		}
355 	} else {
356 		netdev->trans_start = jiffies;
357 
358 		/* slow down tx path */
359 		if (atomic_read(&dev->active_tx_urbs) >= PCAN_USB_MAX_TX_URBS)
360 			netif_stop_queue(netdev);
361 	}
362 
363 	return NETDEV_TX_OK;
364 }
365 
366 /*
367  * start the CAN interface.
368  * Rx and Tx urbs are allocated here. Rx urbs are submitted here.
369  */
peak_usb_start(struct peak_usb_device * dev)370 static int peak_usb_start(struct peak_usb_device *dev)
371 {
372 	struct net_device *netdev = dev->netdev;
373 	int err, i;
374 
375 	for (i = 0; i < PCAN_USB_MAX_RX_URBS; i++) {
376 		struct urb *urb;
377 		u8 *buf;
378 
379 		/* create a URB, and a buffer for it, to receive usb messages */
380 		urb = usb_alloc_urb(0, GFP_KERNEL);
381 		if (!urb) {
382 			netdev_err(netdev, "No memory left for URBs\n");
383 			err = -ENOMEM;
384 			break;
385 		}
386 
387 		buf = kmalloc(dev->adapter->rx_buffer_size, GFP_KERNEL);
388 		if (!buf) {
389 			usb_free_urb(urb);
390 			err = -ENOMEM;
391 			break;
392 		}
393 
394 		usb_fill_bulk_urb(urb, dev->udev,
395 			usb_rcvbulkpipe(dev->udev, dev->ep_msg_in),
396 			buf, dev->adapter->rx_buffer_size,
397 			peak_usb_read_bulk_callback, dev);
398 
399 		/* ask last usb_free_urb() to also kfree() transfer_buffer */
400 		urb->transfer_flags |= URB_FREE_BUFFER;
401 		usb_anchor_urb(urb, &dev->rx_submitted);
402 
403 		err = usb_submit_urb(urb, GFP_KERNEL);
404 		if (err) {
405 			if (err == -ENODEV)
406 				netif_device_detach(dev->netdev);
407 
408 			usb_unanchor_urb(urb);
409 			kfree(buf);
410 			usb_free_urb(urb);
411 			break;
412 		}
413 
414 		/* drop reference, USB core will take care of freeing it */
415 		usb_free_urb(urb);
416 	}
417 
418 	/* did we submit any URBs? Warn if we was not able to submit all urbs */
419 	if (i < PCAN_USB_MAX_RX_URBS) {
420 		if (i == 0) {
421 			netdev_err(netdev, "couldn't setup any rx URB\n");
422 			return err;
423 		}
424 
425 		netdev_warn(netdev, "rx performance may be slow\n");
426 	}
427 
428 	/* pre-alloc tx buffers and corresponding urbs */
429 	for (i = 0; i < PCAN_USB_MAX_TX_URBS; i++) {
430 		struct peak_tx_urb_context *context;
431 		struct urb *urb;
432 		u8 *buf;
433 
434 		/* create a URB and a buffer for it, to transmit usb messages */
435 		urb = usb_alloc_urb(0, GFP_KERNEL);
436 		if (!urb) {
437 			netdev_err(netdev, "No memory left for URBs\n");
438 			err = -ENOMEM;
439 			break;
440 		}
441 
442 		buf = kmalloc(dev->adapter->tx_buffer_size, GFP_KERNEL);
443 		if (!buf) {
444 			usb_free_urb(urb);
445 			err = -ENOMEM;
446 			break;
447 		}
448 
449 		context = dev->tx_contexts + i;
450 		context->dev = dev;
451 		context->urb = urb;
452 
453 		usb_fill_bulk_urb(urb, dev->udev,
454 			usb_sndbulkpipe(dev->udev, dev->ep_msg_out),
455 			buf, dev->adapter->tx_buffer_size,
456 			peak_usb_write_bulk_callback, context);
457 
458 		/* ask last usb_free_urb() to also kfree() transfer_buffer */
459 		urb->transfer_flags |= URB_FREE_BUFFER;
460 	}
461 
462 	/* warn if we were not able to allocate enough tx contexts */
463 	if (i < PCAN_USB_MAX_TX_URBS) {
464 		if (i == 0) {
465 			netdev_err(netdev, "couldn't setup any tx URB\n");
466 			goto err_tx;
467 		}
468 
469 		netdev_warn(netdev, "tx performance may be slow\n");
470 	}
471 
472 	if (dev->adapter->dev_start) {
473 		err = dev->adapter->dev_start(dev);
474 		if (err)
475 			goto err_adapter;
476 	}
477 
478 	dev->state |= PCAN_USB_STATE_STARTED;
479 
480 	/* can set bus on now */
481 	if (dev->adapter->dev_set_bus) {
482 		err = dev->adapter->dev_set_bus(dev, 1);
483 		if (err)
484 			goto err_adapter;
485 	}
486 
487 	dev->can.state = CAN_STATE_ERROR_ACTIVE;
488 
489 	return 0;
490 
491 err_adapter:
492 	if (err == -ENODEV)
493 		netif_device_detach(dev->netdev);
494 
495 	netdev_warn(netdev, "couldn't submit control: %d\n", err);
496 
497 	for (i = 0; i < PCAN_USB_MAX_TX_URBS; i++) {
498 		usb_free_urb(dev->tx_contexts[i].urb);
499 		dev->tx_contexts[i].urb = NULL;
500 	}
501 err_tx:
502 	usb_kill_anchored_urbs(&dev->rx_submitted);
503 
504 	return err;
505 }
506 
507 /*
508  * called by netdev to open the corresponding CAN interface.
509  */
peak_usb_ndo_open(struct net_device * netdev)510 static int peak_usb_ndo_open(struct net_device *netdev)
511 {
512 	struct peak_usb_device *dev = netdev_priv(netdev);
513 	int err;
514 
515 	/* common open */
516 	err = open_candev(netdev);
517 	if (err)
518 		return err;
519 
520 	/* finally start device */
521 	err = peak_usb_start(dev);
522 	if (err) {
523 		netdev_err(netdev, "couldn't start device: %d\n", err);
524 		close_candev(netdev);
525 		return err;
526 	}
527 
528 	netif_start_queue(netdev);
529 
530 	return 0;
531 }
532 
533 /*
534  * unlink in-flight Rx and Tx urbs and free their memory.
535  */
peak_usb_unlink_all_urbs(struct peak_usb_device * dev)536 static void peak_usb_unlink_all_urbs(struct peak_usb_device *dev)
537 {
538 	int i;
539 
540 	/* free all Rx (submitted) urbs */
541 	usb_kill_anchored_urbs(&dev->rx_submitted);
542 
543 	/* free unsubmitted Tx urbs first */
544 	for (i = 0; i < PCAN_USB_MAX_TX_URBS; i++) {
545 		struct urb *urb = dev->tx_contexts[i].urb;
546 
547 		if (!urb ||
548 		    dev->tx_contexts[i].echo_index != PCAN_USB_MAX_TX_URBS) {
549 			/*
550 			 * this urb is already released or always submitted,
551 			 * let usb core free by itself
552 			 */
553 			continue;
554 		}
555 
556 		usb_free_urb(urb);
557 		dev->tx_contexts[i].urb = NULL;
558 	}
559 
560 	/* then free all submitted Tx urbs */
561 	usb_kill_anchored_urbs(&dev->tx_submitted);
562 	atomic_set(&dev->active_tx_urbs, 0);
563 }
564 
565 /*
566  * called by netdev to close the corresponding CAN interface.
567  */
peak_usb_ndo_stop(struct net_device * netdev)568 static int peak_usb_ndo_stop(struct net_device *netdev)
569 {
570 	struct peak_usb_device *dev = netdev_priv(netdev);
571 
572 	dev->state &= ~PCAN_USB_STATE_STARTED;
573 	netif_stop_queue(netdev);
574 
575 	/* unlink all pending urbs and free used memory */
576 	peak_usb_unlink_all_urbs(dev);
577 
578 	if (dev->adapter->dev_stop)
579 		dev->adapter->dev_stop(dev);
580 
581 	close_candev(netdev);
582 
583 	dev->can.state = CAN_STATE_STOPPED;
584 
585 	/* can set bus off now */
586 	if (dev->adapter->dev_set_bus) {
587 		int err = dev->adapter->dev_set_bus(dev, 0);
588 		if (err)
589 			return err;
590 	}
591 
592 	return 0;
593 }
594 
595 /*
596  * handle end of waiting for the device to reset
597  */
peak_usb_restart_complete(struct peak_usb_device * dev)598 void peak_usb_restart_complete(struct peak_usb_device *dev)
599 {
600 	/* finally MUST update can state */
601 	dev->can.state = CAN_STATE_ERROR_ACTIVE;
602 
603 	/* netdev queue can be awaken now */
604 	netif_wake_queue(dev->netdev);
605 }
606 
peak_usb_async_complete(struct urb * urb)607 void peak_usb_async_complete(struct urb *urb)
608 {
609 	kfree(urb->transfer_buffer);
610 	usb_free_urb(urb);
611 }
612 
613 /*
614  * device (auto-)restart mechanism runs in a timer context =>
615  * MUST handle restart with asynchronous usb transfers
616  */
peak_usb_restart(struct peak_usb_device * dev)617 static int peak_usb_restart(struct peak_usb_device *dev)
618 {
619 	struct urb *urb;
620 	int err;
621 	u8 *buf;
622 
623 	/*
624 	 * if device doesn't define any asynchronous restart handler, simply
625 	 * wake the netdev queue up
626 	 */
627 	if (!dev->adapter->dev_restart_async) {
628 		peak_usb_restart_complete(dev);
629 		return 0;
630 	}
631 
632 	/* first allocate a urb to handle the asynchronous steps */
633 	urb = usb_alloc_urb(0, GFP_ATOMIC);
634 	if (!urb) {
635 		netdev_err(dev->netdev, "no memory left for urb\n");
636 		return -ENOMEM;
637 	}
638 
639 	/* also allocate enough space for the commands to send */
640 	buf = kmalloc(PCAN_USB_MAX_CMD_LEN, GFP_ATOMIC);
641 	if (!buf) {
642 		usb_free_urb(urb);
643 		return -ENOMEM;
644 	}
645 
646 	/* call the device specific handler for the restart */
647 	err = dev->adapter->dev_restart_async(dev, urb, buf);
648 	if (!err)
649 		return 0;
650 
651 	kfree(buf);
652 	usb_free_urb(urb);
653 
654 	return err;
655 }
656 
657 /*
658  * candev callback used to change CAN mode.
659  * Warning: this is called from a timer context!
660  */
peak_usb_set_mode(struct net_device * netdev,enum can_mode mode)661 static int peak_usb_set_mode(struct net_device *netdev, enum can_mode mode)
662 {
663 	struct peak_usb_device *dev = netdev_priv(netdev);
664 	int err = 0;
665 
666 	switch (mode) {
667 	case CAN_MODE_START:
668 		err = peak_usb_restart(dev);
669 		if (err)
670 			netdev_err(netdev, "couldn't start device (err %d)\n",
671 				   err);
672 		break;
673 
674 	default:
675 		return -EOPNOTSUPP;
676 	}
677 
678 	return err;
679 }
680 
681 /*
682  * candev callback used to set device bitrate.
683  */
peak_usb_set_bittiming(struct net_device * netdev)684 static int peak_usb_set_bittiming(struct net_device *netdev)
685 {
686 	struct peak_usb_device *dev = netdev_priv(netdev);
687 	struct can_bittiming *bt = &dev->can.bittiming;
688 
689 	if (dev->adapter->dev_set_bittiming) {
690 		int err = dev->adapter->dev_set_bittiming(dev, bt);
691 
692 		if (err)
693 			netdev_info(netdev, "couldn't set bitrate (err %d)\n",
694 				err);
695 		return err;
696 	}
697 
698 	return 0;
699 }
700 
701 static const struct net_device_ops peak_usb_netdev_ops = {
702 	.ndo_open = peak_usb_ndo_open,
703 	.ndo_stop = peak_usb_ndo_stop,
704 	.ndo_start_xmit = peak_usb_ndo_start_xmit,
705 	.ndo_change_mtu = can_change_mtu,
706 };
707 
708 /*
709  * create one device which is attached to CAN controller #ctrl_idx of the
710  * usb adapter.
711  */
peak_usb_create_dev(struct peak_usb_adapter * peak_usb_adapter,struct usb_interface * intf,int ctrl_idx)712 static int peak_usb_create_dev(struct peak_usb_adapter *peak_usb_adapter,
713 			       struct usb_interface *intf, int ctrl_idx)
714 {
715 	struct usb_device *usb_dev = interface_to_usbdev(intf);
716 	int sizeof_candev = peak_usb_adapter->sizeof_dev_private;
717 	struct peak_usb_device *dev;
718 	struct net_device *netdev;
719 	int i, err;
720 	u16 tmp16;
721 
722 	if (sizeof_candev < sizeof(struct peak_usb_device))
723 		sizeof_candev = sizeof(struct peak_usb_device);
724 
725 	netdev = alloc_candev(sizeof_candev, PCAN_USB_MAX_TX_URBS);
726 	if (!netdev) {
727 		dev_err(&intf->dev, "%s: couldn't alloc candev\n",
728 			PCAN_USB_DRIVER_NAME);
729 		return -ENOMEM;
730 	}
731 
732 	dev = netdev_priv(netdev);
733 
734 	/* allocate a buffer large enough to send commands */
735 	dev->cmd_buf = kmalloc(PCAN_USB_MAX_CMD_LEN, GFP_KERNEL);
736 	if (!dev->cmd_buf) {
737 		err = -ENOMEM;
738 		goto lbl_free_candev;
739 	}
740 
741 	dev->udev = usb_dev;
742 	dev->netdev = netdev;
743 	dev->adapter = peak_usb_adapter;
744 	dev->ctrl_idx = ctrl_idx;
745 	dev->state = PCAN_USB_STATE_CONNECTED;
746 
747 	dev->ep_msg_in = peak_usb_adapter->ep_msg_in;
748 	dev->ep_msg_out = peak_usb_adapter->ep_msg_out[ctrl_idx];
749 
750 	dev->can.clock = peak_usb_adapter->clock;
751 	dev->can.bittiming_const = &peak_usb_adapter->bittiming_const;
752 	dev->can.do_set_bittiming = peak_usb_set_bittiming;
753 	dev->can.do_set_mode = peak_usb_set_mode;
754 	dev->can.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES |
755 				      CAN_CTRLMODE_LISTENONLY;
756 
757 	netdev->netdev_ops = &peak_usb_netdev_ops;
758 
759 	netdev->flags |= IFF_ECHO; /* we support local echo */
760 
761 	init_usb_anchor(&dev->rx_submitted);
762 
763 	init_usb_anchor(&dev->tx_submitted);
764 	atomic_set(&dev->active_tx_urbs, 0);
765 
766 	for (i = 0; i < PCAN_USB_MAX_TX_URBS; i++)
767 		dev->tx_contexts[i].echo_index = PCAN_USB_MAX_TX_URBS;
768 
769 	dev->prev_siblings = usb_get_intfdata(intf);
770 	usb_set_intfdata(intf, dev);
771 
772 	SET_NETDEV_DEV(netdev, &intf->dev);
773 	netdev->dev_id = ctrl_idx;
774 
775 	err = register_candev(netdev);
776 	if (err) {
777 		dev_err(&intf->dev, "couldn't register CAN device: %d\n", err);
778 		goto lbl_restore_intf_data;
779 	}
780 
781 	if (dev->prev_siblings)
782 		(dev->prev_siblings)->next_siblings = dev;
783 
784 	/* keep hw revision into the netdevice */
785 	tmp16 = le16_to_cpu(usb_dev->descriptor.bcdDevice);
786 	dev->device_rev = tmp16 >> 8;
787 
788 	if (dev->adapter->dev_init) {
789 		err = dev->adapter->dev_init(dev);
790 		if (err)
791 			goto lbl_unregister_candev;
792 	}
793 
794 	/* set bus off */
795 	if (dev->adapter->dev_set_bus) {
796 		err = dev->adapter->dev_set_bus(dev, 0);
797 		if (err)
798 			goto lbl_unregister_candev;
799 	}
800 
801 	/* get device number early */
802 	if (dev->adapter->dev_get_device_id)
803 		dev->adapter->dev_get_device_id(dev, &dev->device_number);
804 
805 	netdev_info(netdev, "attached to %s channel %u (device %u)\n",
806 			peak_usb_adapter->name, ctrl_idx, dev->device_number);
807 
808 	return 0;
809 
810 lbl_unregister_candev:
811 	unregister_candev(netdev);
812 
813 lbl_restore_intf_data:
814 	usb_set_intfdata(intf, dev->prev_siblings);
815 	kfree(dev->cmd_buf);
816 
817 lbl_free_candev:
818 	free_candev(netdev);
819 
820 	return err;
821 }
822 
823 /*
824  * called by the usb core when the device is unplugged from the system
825  */
peak_usb_disconnect(struct usb_interface * intf)826 static void peak_usb_disconnect(struct usb_interface *intf)
827 {
828 	struct peak_usb_device *dev;
829 
830 	/* unregister as many netdev devices as siblings */
831 	for (dev = usb_get_intfdata(intf); dev; dev = dev->prev_siblings) {
832 		struct net_device *netdev = dev->netdev;
833 		char name[IFNAMSIZ];
834 
835 		dev->state &= ~PCAN_USB_STATE_CONNECTED;
836 		strncpy(name, netdev->name, IFNAMSIZ);
837 
838 		unregister_netdev(netdev);
839 		free_candev(netdev);
840 
841 		kfree(dev->cmd_buf);
842 		dev->next_siblings = NULL;
843 		if (dev->adapter->dev_free)
844 			dev->adapter->dev_free(dev);
845 
846 		dev_info(&intf->dev, "%s removed\n", name);
847 	}
848 
849 	usb_set_intfdata(intf, NULL);
850 }
851 
852 /*
853  * probe function for new PEAK-System devices
854  */
peak_usb_probe(struct usb_interface * intf,const struct usb_device_id * id)855 static int peak_usb_probe(struct usb_interface *intf,
856 			  const struct usb_device_id *id)
857 {
858 	struct usb_device *usb_dev = interface_to_usbdev(intf);
859 	struct peak_usb_adapter *peak_usb_adapter, **pp;
860 	int i, err = -ENOMEM;
861 
862 	usb_dev = interface_to_usbdev(intf);
863 
864 	/* get corresponding PCAN-USB adapter */
865 	for (pp = peak_usb_adapters_list; *pp; pp++)
866 		if ((*pp)->device_id == usb_dev->descriptor.idProduct)
867 			break;
868 
869 	peak_usb_adapter = *pp;
870 	if (!peak_usb_adapter) {
871 		/* should never come except device_id bad usage in this file */
872 		pr_err("%s: didn't find device id. 0x%x in devices list\n",
873 			PCAN_USB_DRIVER_NAME, usb_dev->descriptor.idProduct);
874 		return -ENODEV;
875 	}
876 
877 	/* got corresponding adapter: check if it handles current interface */
878 	if (peak_usb_adapter->intf_probe) {
879 		err = peak_usb_adapter->intf_probe(intf);
880 		if (err)
881 			return err;
882 	}
883 
884 	for (i = 0; i < peak_usb_adapter->ctrl_count; i++) {
885 		err = peak_usb_create_dev(peak_usb_adapter, intf, i);
886 		if (err) {
887 			/* deregister already created devices */
888 			peak_usb_disconnect(intf);
889 			break;
890 		}
891 	}
892 
893 	return err;
894 }
895 
896 /* usb specific object needed to register this driver with the usb subsystem */
897 static struct usb_driver peak_usb_driver = {
898 	.name = PCAN_USB_DRIVER_NAME,
899 	.disconnect = peak_usb_disconnect,
900 	.probe = peak_usb_probe,
901 	.id_table = peak_usb_table,
902 };
903 
peak_usb_init(void)904 static int __init peak_usb_init(void)
905 {
906 	int err;
907 
908 	/* register this driver with the USB subsystem */
909 	err = usb_register(&peak_usb_driver);
910 	if (err)
911 		pr_err("%s: usb_register failed (err %d)\n",
912 			PCAN_USB_DRIVER_NAME, err);
913 
914 	return err;
915 }
916 
peak_usb_do_device_exit(struct device * d,void * arg)917 static int peak_usb_do_device_exit(struct device *d, void *arg)
918 {
919 	struct usb_interface *intf = to_usb_interface(d);
920 	struct peak_usb_device *dev;
921 
922 	/* stop as many netdev devices as siblings */
923 	for (dev = usb_get_intfdata(intf); dev; dev = dev->prev_siblings) {
924 		struct net_device *netdev = dev->netdev;
925 
926 		if (netif_device_present(netdev))
927 			if (dev->adapter->dev_exit)
928 				dev->adapter->dev_exit(dev);
929 	}
930 
931 	return 0;
932 }
933 
peak_usb_exit(void)934 static void __exit peak_usb_exit(void)
935 {
936 	int err;
937 
938 	/* last chance do send any synchronous commands here */
939 	err = driver_for_each_device(&peak_usb_driver.drvwrap.driver, NULL,
940 				     NULL, peak_usb_do_device_exit);
941 	if (err)
942 		pr_err("%s: failed to stop all can devices (err %d)\n",
943 			PCAN_USB_DRIVER_NAME, err);
944 
945 	/* deregister this driver with the USB subsystem */
946 	usb_deregister(&peak_usb_driver);
947 
948 	pr_info("%s: PCAN-USB interfaces driver unloaded\n",
949 		PCAN_USB_DRIVER_NAME);
950 }
951 
952 module_init(peak_usb_init);
953 module_exit(peak_usb_exit);
954