xref: /linux/drivers/net/can/usb/gs_usb.c (revision a4989fa91110508b64eea7ccde63d062113988ff)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* CAN driver for Geschwister Schneider USB/CAN devices
3  * and bytewerk.org candleLight USB CAN interfaces.
4  *
5  * Copyright (C) 2013-2016 Geschwister Schneider Technologie-,
6  * Entwicklungs- und Vertriebs UG (Haftungsbeschränkt).
7  * Copyright (C) 2016 Hubert Denkmair
8  *
9  * Many thanks to all socketcan devs!
10  */
11 
12 #include <linux/ethtool.h>
13 #include <linux/init.h>
14 #include <linux/signal.h>
15 #include <linux/module.h>
16 #include <linux/netdevice.h>
17 #include <linux/usb.h>
18 
19 #include <linux/can.h>
20 #include <linux/can/dev.h>
21 #include <linux/can/error.h>
22 
23 /* Device specific constants */
24 #define USB_GSUSB_1_VENDOR_ID      0x1d50
25 #define USB_GSUSB_1_PRODUCT_ID     0x606f
26 
27 #define USB_CANDLELIGHT_VENDOR_ID  0x1209
28 #define USB_CANDLELIGHT_PRODUCT_ID 0x2323
29 
30 #define GSUSB_ENDPOINT_IN          1
31 #define GSUSB_ENDPOINT_OUT         2
32 
33 /* Device specific constants */
34 enum gs_usb_breq {
35 	GS_USB_BREQ_HOST_FORMAT = 0,
36 	GS_USB_BREQ_BITTIMING,
37 	GS_USB_BREQ_MODE,
38 	GS_USB_BREQ_BERR,
39 	GS_USB_BREQ_BT_CONST,
40 	GS_USB_BREQ_DEVICE_CONFIG,
41 	GS_USB_BREQ_TIMESTAMP,
42 	GS_USB_BREQ_IDENTIFY,
43 };
44 
45 enum gs_can_mode {
46 	/* reset a channel. turns it off */
47 	GS_CAN_MODE_RESET = 0,
48 	/* starts a channel */
49 	GS_CAN_MODE_START
50 };
51 
52 enum gs_can_state {
53 	GS_CAN_STATE_ERROR_ACTIVE = 0,
54 	GS_CAN_STATE_ERROR_WARNING,
55 	GS_CAN_STATE_ERROR_PASSIVE,
56 	GS_CAN_STATE_BUS_OFF,
57 	GS_CAN_STATE_STOPPED,
58 	GS_CAN_STATE_SLEEPING
59 };
60 
61 enum gs_can_identify_mode {
62 	GS_CAN_IDENTIFY_OFF = 0,
63 	GS_CAN_IDENTIFY_ON
64 };
65 
66 /* data types passed between host and device */
67 struct gs_host_config {
68 	u32 byte_order;
69 } __packed;
70 /* All data exchanged between host and device is exchanged in host byte order,
71  * thanks to the struct gs_host_config byte_order member, which is sent first
72  * to indicate the desired byte order.
73  */
74 
75 struct gs_device_config {
76 	u8 reserved1;
77 	u8 reserved2;
78 	u8 reserved3;
79 	u8 icount;
80 	u32 sw_version;
81 	u32 hw_version;
82 } __packed;
83 
84 #define GS_CAN_MODE_NORMAL               0
85 #define GS_CAN_MODE_LISTEN_ONLY          BIT(0)
86 #define GS_CAN_MODE_LOOP_BACK            BIT(1)
87 #define GS_CAN_MODE_TRIPLE_SAMPLE        BIT(2)
88 #define GS_CAN_MODE_ONE_SHOT             BIT(3)
89 
90 struct gs_device_mode {
91 	u32 mode;
92 	u32 flags;
93 } __packed;
94 
95 struct gs_device_state {
96 	u32 state;
97 	u32 rxerr;
98 	u32 txerr;
99 } __packed;
100 
101 struct gs_device_bittiming {
102 	u32 prop_seg;
103 	u32 phase_seg1;
104 	u32 phase_seg2;
105 	u32 sjw;
106 	u32 brp;
107 } __packed;
108 
109 struct gs_identify_mode {
110 	u32 mode;
111 } __packed;
112 
113 #define GS_CAN_FEATURE_LISTEN_ONLY      BIT(0)
114 #define GS_CAN_FEATURE_LOOP_BACK        BIT(1)
115 #define GS_CAN_FEATURE_TRIPLE_SAMPLE    BIT(2)
116 #define GS_CAN_FEATURE_ONE_SHOT         BIT(3)
117 #define GS_CAN_FEATURE_HW_TIMESTAMP     BIT(4)
118 #define GS_CAN_FEATURE_IDENTIFY         BIT(5)
119 
120 struct gs_device_bt_const {
121 	u32 feature;
122 	u32 fclk_can;
123 	u32 tseg1_min;
124 	u32 tseg1_max;
125 	u32 tseg2_min;
126 	u32 tseg2_max;
127 	u32 sjw_max;
128 	u32 brp_min;
129 	u32 brp_max;
130 	u32 brp_inc;
131 } __packed;
132 
133 #define GS_CAN_FLAG_OVERFLOW 1
134 
135 struct gs_host_frame {
136 	u32 echo_id;
137 	u32 can_id;
138 
139 	u8 can_dlc;
140 	u8 channel;
141 	u8 flags;
142 	u8 reserved;
143 
144 	u8 data[8];
145 } __packed;
146 /* The GS USB devices make use of the same flags and masks as in
147  * linux/can.h and linux/can/error.h, and no additional mapping is necessary.
148  */
149 
150 /* Only send a max of GS_MAX_TX_URBS frames per channel at a time. */
151 #define GS_MAX_TX_URBS 10
152 /* Only launch a max of GS_MAX_RX_URBS usb requests at a time. */
153 #define GS_MAX_RX_URBS 30
154 /* Maximum number of interfaces the driver supports per device.
155  * Current hardware only supports 2 interfaces. The future may vary.
156  */
157 #define GS_MAX_INTF 2
158 
159 struct gs_tx_context {
160 	struct gs_can *dev;
161 	unsigned int echo_id;
162 };
163 
164 struct gs_can {
165 	struct can_priv can; /* must be the first member */
166 
167 	struct gs_usb *parent;
168 
169 	struct net_device *netdev;
170 	struct usb_device *udev;
171 	struct usb_interface *iface;
172 
173 	struct can_bittiming_const bt_const;
174 	unsigned int channel;	/* channel number */
175 
176 	/* This lock prevents a race condition between xmit and receive. */
177 	spinlock_t tx_ctx_lock;
178 	struct gs_tx_context tx_context[GS_MAX_TX_URBS];
179 
180 	struct usb_anchor tx_submitted;
181 	atomic_t active_tx_urbs;
182 };
183 
184 /* usb interface struct */
185 struct gs_usb {
186 	struct gs_can *canch[GS_MAX_INTF];
187 	struct usb_anchor rx_submitted;
188 	atomic_t active_channels;
189 	struct usb_device *udev;
190 };
191 
192 /* 'allocate' a tx context.
193  * returns a valid tx context or NULL if there is no space.
194  */
195 static struct gs_tx_context *gs_alloc_tx_context(struct gs_can *dev)
196 {
197 	int i = 0;
198 	unsigned long flags;
199 
200 	spin_lock_irqsave(&dev->tx_ctx_lock, flags);
201 
202 	for (; i < GS_MAX_TX_URBS; i++) {
203 		if (dev->tx_context[i].echo_id == GS_MAX_TX_URBS) {
204 			dev->tx_context[i].echo_id = i;
205 			spin_unlock_irqrestore(&dev->tx_ctx_lock, flags);
206 			return &dev->tx_context[i];
207 		}
208 	}
209 
210 	spin_unlock_irqrestore(&dev->tx_ctx_lock, flags);
211 	return NULL;
212 }
213 
214 /* releases a tx context
215  */
216 static void gs_free_tx_context(struct gs_tx_context *txc)
217 {
218 	txc->echo_id = GS_MAX_TX_URBS;
219 }
220 
221 /* Get a tx context by id.
222  */
223 static struct gs_tx_context *gs_get_tx_context(struct gs_can *dev,
224 					       unsigned int id)
225 {
226 	unsigned long flags;
227 
228 	if (id < GS_MAX_TX_URBS) {
229 		spin_lock_irqsave(&dev->tx_ctx_lock, flags);
230 		if (dev->tx_context[id].echo_id == id) {
231 			spin_unlock_irqrestore(&dev->tx_ctx_lock, flags);
232 			return &dev->tx_context[id];
233 		}
234 		spin_unlock_irqrestore(&dev->tx_ctx_lock, flags);
235 	}
236 	return NULL;
237 }
238 
239 static int gs_cmd_reset(struct gs_can *gsdev)
240 {
241 	struct gs_device_mode *dm;
242 	struct usb_interface *intf = gsdev->iface;
243 	int rc;
244 
245 	dm = kzalloc(sizeof(*dm), GFP_KERNEL);
246 	if (!dm)
247 		return -ENOMEM;
248 
249 	dm->mode = GS_CAN_MODE_RESET;
250 
251 	rc = usb_control_msg(interface_to_usbdev(intf),
252 			     usb_sndctrlpipe(interface_to_usbdev(intf), 0),
253 			     GS_USB_BREQ_MODE,
254 			     USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
255 			     gsdev->channel,
256 			     0,
257 			     dm,
258 			     sizeof(*dm),
259 			     1000);
260 
261 	kfree(dm);
262 
263 	return rc;
264 }
265 
266 static void gs_update_state(struct gs_can *dev, struct can_frame *cf)
267 {
268 	struct can_device_stats *can_stats = &dev->can.can_stats;
269 
270 	if (cf->can_id & CAN_ERR_RESTARTED) {
271 		dev->can.state = CAN_STATE_ERROR_ACTIVE;
272 		can_stats->restarts++;
273 	} else if (cf->can_id & CAN_ERR_BUSOFF) {
274 		dev->can.state = CAN_STATE_BUS_OFF;
275 		can_stats->bus_off++;
276 	} else if (cf->can_id & CAN_ERR_CRTL) {
277 		if ((cf->data[1] & CAN_ERR_CRTL_TX_WARNING) ||
278 		    (cf->data[1] & CAN_ERR_CRTL_RX_WARNING)) {
279 			dev->can.state = CAN_STATE_ERROR_WARNING;
280 			can_stats->error_warning++;
281 		} else if ((cf->data[1] & CAN_ERR_CRTL_TX_PASSIVE) ||
282 			   (cf->data[1] & CAN_ERR_CRTL_RX_PASSIVE)) {
283 			dev->can.state = CAN_STATE_ERROR_PASSIVE;
284 			can_stats->error_passive++;
285 		} else {
286 			dev->can.state = CAN_STATE_ERROR_ACTIVE;
287 		}
288 	}
289 }
290 
291 static void gs_usb_receive_bulk_callback(struct urb *urb)
292 {
293 	struct gs_usb *usbcan = urb->context;
294 	struct gs_can *dev;
295 	struct net_device *netdev;
296 	int rc;
297 	struct net_device_stats *stats;
298 	struct gs_host_frame *hf = urb->transfer_buffer;
299 	struct gs_tx_context *txc;
300 	struct can_frame *cf;
301 	struct sk_buff *skb;
302 
303 	BUG_ON(!usbcan);
304 
305 	switch (urb->status) {
306 	case 0: /* success */
307 		break;
308 	case -ENOENT:
309 	case -ESHUTDOWN:
310 		return;
311 	default:
312 		/* do not resubmit aborted urbs. eg: when device goes down */
313 		return;
314 	}
315 
316 	/* device reports out of range channel id */
317 	if (hf->channel >= GS_MAX_INTF)
318 		goto resubmit_urb;
319 
320 	dev = usbcan->canch[hf->channel];
321 
322 	netdev = dev->netdev;
323 	stats = &netdev->stats;
324 
325 	if (!netif_device_present(netdev))
326 		return;
327 
328 	if (hf->echo_id == -1) { /* normal rx */
329 		skb = alloc_can_skb(dev->netdev, &cf);
330 		if (!skb)
331 			return;
332 
333 		cf->can_id = hf->can_id;
334 
335 		can_frame_set_cc_len(cf, hf->can_dlc, dev->can.ctrlmode);
336 		memcpy(cf->data, hf->data, 8);
337 
338 		/* ERROR frames tell us information about the controller */
339 		if (hf->can_id & CAN_ERR_FLAG)
340 			gs_update_state(dev, cf);
341 
342 		netdev->stats.rx_packets++;
343 		netdev->stats.rx_bytes += hf->can_dlc;
344 
345 		netif_rx(skb);
346 	} else { /* echo_id == hf->echo_id */
347 		if (hf->echo_id >= GS_MAX_TX_URBS) {
348 			netdev_err(netdev,
349 				   "Unexpected out of range echo id %d\n",
350 				   hf->echo_id);
351 			goto resubmit_urb;
352 		}
353 
354 		netdev->stats.tx_packets++;
355 		netdev->stats.tx_bytes += hf->can_dlc;
356 
357 		txc = gs_get_tx_context(dev, hf->echo_id);
358 
359 		/* bad devices send bad echo_ids. */
360 		if (!txc) {
361 			netdev_err(netdev,
362 				   "Unexpected unused echo id %d\n",
363 				   hf->echo_id);
364 			goto resubmit_urb;
365 		}
366 
367 		can_get_echo_skb(netdev, hf->echo_id);
368 
369 		gs_free_tx_context(txc);
370 
371 		atomic_dec(&dev->active_tx_urbs);
372 
373 		netif_wake_queue(netdev);
374 	}
375 
376 	if (hf->flags & GS_CAN_FLAG_OVERFLOW) {
377 		skb = alloc_can_err_skb(netdev, &cf);
378 		if (!skb)
379 			goto resubmit_urb;
380 
381 		cf->can_id |= CAN_ERR_CRTL;
382 		cf->len = CAN_ERR_DLC;
383 		cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
384 		stats->rx_over_errors++;
385 		stats->rx_errors++;
386 		netif_rx(skb);
387 	}
388 
389  resubmit_urb:
390 	usb_fill_bulk_urb(urb,
391 			  usbcan->udev,
392 			  usb_rcvbulkpipe(usbcan->udev, GSUSB_ENDPOINT_IN),
393 			  hf,
394 			  sizeof(struct gs_host_frame),
395 			  gs_usb_receive_bulk_callback,
396 			  usbcan
397 			  );
398 
399 	rc = usb_submit_urb(urb, GFP_ATOMIC);
400 
401 	/* USB failure take down all interfaces */
402 	if (rc == -ENODEV) {
403 		for (rc = 0; rc < GS_MAX_INTF; rc++) {
404 			if (usbcan->canch[rc])
405 				netif_device_detach(usbcan->canch[rc]->netdev);
406 		}
407 	}
408 }
409 
410 static int gs_usb_set_bittiming(struct net_device *netdev)
411 {
412 	struct gs_can *dev = netdev_priv(netdev);
413 	struct can_bittiming *bt = &dev->can.bittiming;
414 	struct usb_interface *intf = dev->iface;
415 	int rc;
416 	struct gs_device_bittiming *dbt;
417 
418 	dbt = kmalloc(sizeof(*dbt), GFP_KERNEL);
419 	if (!dbt)
420 		return -ENOMEM;
421 
422 	dbt->prop_seg = bt->prop_seg;
423 	dbt->phase_seg1 = bt->phase_seg1;
424 	dbt->phase_seg2 = bt->phase_seg2;
425 	dbt->sjw = bt->sjw;
426 	dbt->brp = bt->brp;
427 
428 	/* request bit timings */
429 	rc = usb_control_msg(interface_to_usbdev(intf),
430 			     usb_sndctrlpipe(interface_to_usbdev(intf), 0),
431 			     GS_USB_BREQ_BITTIMING,
432 			     USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
433 			     dev->channel,
434 			     0,
435 			     dbt,
436 			     sizeof(*dbt),
437 			     1000);
438 
439 	kfree(dbt);
440 
441 	if (rc < 0)
442 		dev_err(netdev->dev.parent, "Couldn't set bittimings (err=%d)",
443 			rc);
444 
445 	return (rc > 0) ? 0 : rc;
446 }
447 
448 static void gs_usb_xmit_callback(struct urb *urb)
449 {
450 	struct gs_tx_context *txc = urb->context;
451 	struct gs_can *dev = txc->dev;
452 	struct net_device *netdev = dev->netdev;
453 
454 	if (urb->status)
455 		netdev_info(netdev, "usb xmit fail %d\n", txc->echo_id);
456 
457 	usb_free_coherent(urb->dev,
458 			  urb->transfer_buffer_length,
459 			  urb->transfer_buffer,
460 			  urb->transfer_dma);
461 }
462 
463 static netdev_tx_t gs_can_start_xmit(struct sk_buff *skb,
464 				     struct net_device *netdev)
465 {
466 	struct gs_can *dev = netdev_priv(netdev);
467 	struct net_device_stats *stats = &dev->netdev->stats;
468 	struct urb *urb;
469 	struct gs_host_frame *hf;
470 	struct can_frame *cf;
471 	int rc;
472 	unsigned int idx;
473 	struct gs_tx_context *txc;
474 
475 	if (can_dropped_invalid_skb(netdev, skb))
476 		return NETDEV_TX_OK;
477 
478 	/* find an empty context to keep track of transmission */
479 	txc = gs_alloc_tx_context(dev);
480 	if (!txc)
481 		return NETDEV_TX_BUSY;
482 
483 	/* create a URB, and a buffer for it */
484 	urb = usb_alloc_urb(0, GFP_ATOMIC);
485 	if (!urb)
486 		goto nomem_urb;
487 
488 	hf = usb_alloc_coherent(dev->udev, sizeof(*hf), GFP_ATOMIC,
489 				&urb->transfer_dma);
490 	if (!hf) {
491 		netdev_err(netdev, "No memory left for USB buffer\n");
492 		goto nomem_hf;
493 	}
494 
495 	idx = txc->echo_id;
496 
497 	if (idx >= GS_MAX_TX_URBS) {
498 		netdev_err(netdev, "Invalid tx context %d\n", idx);
499 		goto badidx;
500 	}
501 
502 	hf->echo_id = idx;
503 	hf->channel = dev->channel;
504 
505 	cf = (struct can_frame *)skb->data;
506 
507 	hf->can_id = cf->can_id;
508 	hf->can_dlc = can_get_cc_dlc(cf, dev->can.ctrlmode);
509 
510 	memcpy(hf->data, cf->data, cf->len);
511 
512 	usb_fill_bulk_urb(urb, dev->udev,
513 			  usb_sndbulkpipe(dev->udev, GSUSB_ENDPOINT_OUT),
514 			  hf,
515 			  sizeof(*hf),
516 			  gs_usb_xmit_callback,
517 			  txc);
518 
519 	urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
520 	usb_anchor_urb(urb, &dev->tx_submitted);
521 
522 	can_put_echo_skb(skb, netdev, idx);
523 
524 	atomic_inc(&dev->active_tx_urbs);
525 
526 	rc = usb_submit_urb(urb, GFP_ATOMIC);
527 	if (unlikely(rc)) {			/* usb send failed */
528 		atomic_dec(&dev->active_tx_urbs);
529 
530 		can_free_echo_skb(netdev, idx);
531 		gs_free_tx_context(txc);
532 
533 		usb_unanchor_urb(urb);
534 		usb_free_coherent(dev->udev,
535 				  sizeof(*hf),
536 				  hf,
537 				  urb->transfer_dma);
538 
539 		if (rc == -ENODEV) {
540 			netif_device_detach(netdev);
541 		} else {
542 			netdev_err(netdev, "usb_submit failed (err=%d)\n", rc);
543 			stats->tx_dropped++;
544 		}
545 	} else {
546 		/* Slow down tx path */
547 		if (atomic_read(&dev->active_tx_urbs) >= GS_MAX_TX_URBS)
548 			netif_stop_queue(netdev);
549 	}
550 
551 	/* let usb core take care of this urb */
552 	usb_free_urb(urb);
553 
554 	return NETDEV_TX_OK;
555 
556  badidx:
557 	usb_free_coherent(dev->udev,
558 			  sizeof(*hf),
559 			  hf,
560 			  urb->transfer_dma);
561  nomem_hf:
562 	usb_free_urb(urb);
563 
564  nomem_urb:
565 	gs_free_tx_context(txc);
566 	dev_kfree_skb(skb);
567 	stats->tx_dropped++;
568 	return NETDEV_TX_OK;
569 }
570 
571 static int gs_can_open(struct net_device *netdev)
572 {
573 	struct gs_can *dev = netdev_priv(netdev);
574 	struct gs_usb *parent = dev->parent;
575 	int rc, i;
576 	struct gs_device_mode *dm;
577 	u32 ctrlmode;
578 
579 	rc = open_candev(netdev);
580 	if (rc)
581 		return rc;
582 
583 	if (atomic_add_return(1, &parent->active_channels) == 1) {
584 		for (i = 0; i < GS_MAX_RX_URBS; i++) {
585 			struct urb *urb;
586 			u8 *buf;
587 
588 			/* alloc rx urb */
589 			urb = usb_alloc_urb(0, GFP_KERNEL);
590 			if (!urb)
591 				return -ENOMEM;
592 
593 			/* alloc rx buffer */
594 			buf = usb_alloc_coherent(dev->udev,
595 						 sizeof(struct gs_host_frame),
596 						 GFP_KERNEL,
597 						 &urb->transfer_dma);
598 			if (!buf) {
599 				netdev_err(netdev,
600 					   "No memory left for USB buffer\n");
601 				usb_free_urb(urb);
602 				return -ENOMEM;
603 			}
604 
605 			/* fill, anchor, and submit rx urb */
606 			usb_fill_bulk_urb(urb,
607 					  dev->udev,
608 					  usb_rcvbulkpipe(dev->udev,
609 							  GSUSB_ENDPOINT_IN),
610 					  buf,
611 					  sizeof(struct gs_host_frame),
612 					  gs_usb_receive_bulk_callback,
613 					  parent);
614 			urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
615 
616 			usb_anchor_urb(urb, &parent->rx_submitted);
617 
618 			rc = usb_submit_urb(urb, GFP_KERNEL);
619 			if (rc) {
620 				if (rc == -ENODEV)
621 					netif_device_detach(dev->netdev);
622 
623 				netdev_err(netdev,
624 					   "usb_submit failed (err=%d)\n",
625 					   rc);
626 
627 				usb_unanchor_urb(urb);
628 				usb_free_urb(urb);
629 				break;
630 			}
631 
632 			/* Drop reference,
633 			 * USB core will take care of freeing it
634 			 */
635 			usb_free_urb(urb);
636 		}
637 	}
638 
639 	dm = kmalloc(sizeof(*dm), GFP_KERNEL);
640 	if (!dm)
641 		return -ENOMEM;
642 
643 	/* flags */
644 	ctrlmode = dev->can.ctrlmode;
645 	dm->flags = 0;
646 
647 	if (ctrlmode & CAN_CTRLMODE_LOOPBACK)
648 		dm->flags |= GS_CAN_MODE_LOOP_BACK;
649 	else if (ctrlmode & CAN_CTRLMODE_LISTENONLY)
650 		dm->flags |= GS_CAN_MODE_LISTEN_ONLY;
651 
652 	/* Controller is not allowed to retry TX
653 	 * this mode is unavailable on atmels uc3c hardware
654 	 */
655 	if (ctrlmode & CAN_CTRLMODE_ONE_SHOT)
656 		dm->flags |= GS_CAN_MODE_ONE_SHOT;
657 
658 	if (ctrlmode & CAN_CTRLMODE_3_SAMPLES)
659 		dm->flags |= GS_CAN_MODE_TRIPLE_SAMPLE;
660 
661 	/* finally start device */
662 	dm->mode = GS_CAN_MODE_START;
663 	rc = usb_control_msg(interface_to_usbdev(dev->iface),
664 			     usb_sndctrlpipe(interface_to_usbdev(dev->iface), 0),
665 			     GS_USB_BREQ_MODE,
666 			     USB_DIR_OUT | USB_TYPE_VENDOR |
667 			     USB_RECIP_INTERFACE,
668 			     dev->channel,
669 			     0,
670 			     dm,
671 			     sizeof(*dm),
672 			     1000);
673 
674 	if (rc < 0) {
675 		netdev_err(netdev, "Couldn't start device (err=%d)\n", rc);
676 		kfree(dm);
677 		return rc;
678 	}
679 
680 	kfree(dm);
681 
682 	dev->can.state = CAN_STATE_ERROR_ACTIVE;
683 
684 	if (!(dev->can.ctrlmode & CAN_CTRLMODE_LISTENONLY))
685 		netif_start_queue(netdev);
686 
687 	return 0;
688 }
689 
690 static int gs_can_close(struct net_device *netdev)
691 {
692 	int rc;
693 	struct gs_can *dev = netdev_priv(netdev);
694 	struct gs_usb *parent = dev->parent;
695 
696 	netif_stop_queue(netdev);
697 
698 	/* Stop polling */
699 	if (atomic_dec_and_test(&parent->active_channels))
700 		usb_kill_anchored_urbs(&parent->rx_submitted);
701 
702 	/* Stop sending URBs */
703 	usb_kill_anchored_urbs(&dev->tx_submitted);
704 	atomic_set(&dev->active_tx_urbs, 0);
705 
706 	/* reset the device */
707 	rc = gs_cmd_reset(dev);
708 	if (rc < 0)
709 		netdev_warn(netdev, "Couldn't shutdown device (err=%d)", rc);
710 
711 	/* reset tx contexts */
712 	for (rc = 0; rc < GS_MAX_TX_URBS; rc++) {
713 		dev->tx_context[rc].dev = dev;
714 		dev->tx_context[rc].echo_id = GS_MAX_TX_URBS;
715 	}
716 
717 	/* close the netdev */
718 	close_candev(netdev);
719 
720 	return 0;
721 }
722 
723 static const struct net_device_ops gs_usb_netdev_ops = {
724 	.ndo_open = gs_can_open,
725 	.ndo_stop = gs_can_close,
726 	.ndo_start_xmit = gs_can_start_xmit,
727 	.ndo_change_mtu = can_change_mtu,
728 };
729 
730 static int gs_usb_set_identify(struct net_device *netdev, bool do_identify)
731 {
732 	struct gs_can *dev = netdev_priv(netdev);
733 	struct gs_identify_mode *imode;
734 	int rc;
735 
736 	imode = kmalloc(sizeof(*imode), GFP_KERNEL);
737 
738 	if (!imode)
739 		return -ENOMEM;
740 
741 	if (do_identify)
742 		imode->mode = GS_CAN_IDENTIFY_ON;
743 	else
744 		imode->mode = GS_CAN_IDENTIFY_OFF;
745 
746 	rc = usb_control_msg(interface_to_usbdev(dev->iface),
747 			     usb_sndctrlpipe(interface_to_usbdev(dev->iface),
748 					     0),
749 			     GS_USB_BREQ_IDENTIFY,
750 			     USB_DIR_OUT | USB_TYPE_VENDOR |
751 			     USB_RECIP_INTERFACE,
752 			     dev->channel,
753 			     0,
754 			     imode,
755 			     sizeof(*imode),
756 			     100);
757 
758 	kfree(imode);
759 
760 	return (rc > 0) ? 0 : rc;
761 }
762 
763 /* blink LED's for finding the this interface */
764 static int gs_usb_set_phys_id(struct net_device *dev,
765 			      enum ethtool_phys_id_state state)
766 {
767 	int rc = 0;
768 
769 	switch (state) {
770 	case ETHTOOL_ID_ACTIVE:
771 		rc = gs_usb_set_identify(dev, GS_CAN_IDENTIFY_ON);
772 		break;
773 	case ETHTOOL_ID_INACTIVE:
774 		rc = gs_usb_set_identify(dev, GS_CAN_IDENTIFY_OFF);
775 		break;
776 	default:
777 		break;
778 	}
779 
780 	return rc;
781 }
782 
783 static const struct ethtool_ops gs_usb_ethtool_ops = {
784 	.set_phys_id = gs_usb_set_phys_id,
785 };
786 
787 static struct gs_can *gs_make_candev(unsigned int channel,
788 				     struct usb_interface *intf,
789 				     struct gs_device_config *dconf)
790 {
791 	struct gs_can *dev;
792 	struct net_device *netdev;
793 	int rc;
794 	struct gs_device_bt_const *bt_const;
795 
796 	bt_const = kmalloc(sizeof(*bt_const), GFP_KERNEL);
797 	if (!bt_const)
798 		return ERR_PTR(-ENOMEM);
799 
800 	/* fetch bit timing constants */
801 	rc = usb_control_msg(interface_to_usbdev(intf),
802 			     usb_rcvctrlpipe(interface_to_usbdev(intf), 0),
803 			     GS_USB_BREQ_BT_CONST,
804 			     USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
805 			     channel,
806 			     0,
807 			     bt_const,
808 			     sizeof(*bt_const),
809 			     1000);
810 
811 	if (rc < 0) {
812 		dev_err(&intf->dev,
813 			"Couldn't get bit timing const for channel (err=%d)\n",
814 			rc);
815 		kfree(bt_const);
816 		return ERR_PTR(rc);
817 	}
818 
819 	/* create netdev */
820 	netdev = alloc_candev(sizeof(struct gs_can), GS_MAX_TX_URBS);
821 	if (!netdev) {
822 		dev_err(&intf->dev, "Couldn't allocate candev\n");
823 		kfree(bt_const);
824 		return ERR_PTR(-ENOMEM);
825 	}
826 
827 	dev = netdev_priv(netdev);
828 
829 	netdev->netdev_ops = &gs_usb_netdev_ops;
830 
831 	netdev->flags |= IFF_ECHO; /* we support full roundtrip echo */
832 
833 	/* dev setup */
834 	strcpy(dev->bt_const.name, "gs_usb");
835 	dev->bt_const.tseg1_min = bt_const->tseg1_min;
836 	dev->bt_const.tseg1_max = bt_const->tseg1_max;
837 	dev->bt_const.tseg2_min = bt_const->tseg2_min;
838 	dev->bt_const.tseg2_max = bt_const->tseg2_max;
839 	dev->bt_const.sjw_max = bt_const->sjw_max;
840 	dev->bt_const.brp_min = bt_const->brp_min;
841 	dev->bt_const.brp_max = bt_const->brp_max;
842 	dev->bt_const.brp_inc = bt_const->brp_inc;
843 
844 	dev->udev = interface_to_usbdev(intf);
845 	dev->iface = intf;
846 	dev->netdev = netdev;
847 	dev->channel = channel;
848 
849 	init_usb_anchor(&dev->tx_submitted);
850 	atomic_set(&dev->active_tx_urbs, 0);
851 	spin_lock_init(&dev->tx_ctx_lock);
852 	for (rc = 0; rc < GS_MAX_TX_URBS; rc++) {
853 		dev->tx_context[rc].dev = dev;
854 		dev->tx_context[rc].echo_id = GS_MAX_TX_URBS;
855 	}
856 
857 	/* can setup */
858 	dev->can.state = CAN_STATE_STOPPED;
859 	dev->can.clock.freq = bt_const->fclk_can;
860 	dev->can.bittiming_const = &dev->bt_const;
861 	dev->can.do_set_bittiming = gs_usb_set_bittiming;
862 
863 	dev->can.ctrlmode_supported = CAN_CTRLMODE_CC_LEN8_DLC;
864 
865 	if (bt_const->feature & GS_CAN_FEATURE_LISTEN_ONLY)
866 		dev->can.ctrlmode_supported |= CAN_CTRLMODE_LISTENONLY;
867 
868 	if (bt_const->feature & GS_CAN_FEATURE_LOOP_BACK)
869 		dev->can.ctrlmode_supported |= CAN_CTRLMODE_LOOPBACK;
870 
871 	if (bt_const->feature & GS_CAN_FEATURE_TRIPLE_SAMPLE)
872 		dev->can.ctrlmode_supported |= CAN_CTRLMODE_3_SAMPLES;
873 
874 	if (bt_const->feature & GS_CAN_FEATURE_ONE_SHOT)
875 		dev->can.ctrlmode_supported |= CAN_CTRLMODE_ONE_SHOT;
876 
877 	SET_NETDEV_DEV(netdev, &intf->dev);
878 
879 	if (dconf->sw_version > 1)
880 		if (bt_const->feature & GS_CAN_FEATURE_IDENTIFY)
881 			netdev->ethtool_ops = &gs_usb_ethtool_ops;
882 
883 	kfree(bt_const);
884 
885 	rc = register_candev(dev->netdev);
886 	if (rc) {
887 		free_candev(dev->netdev);
888 		dev_err(&intf->dev, "Couldn't register candev (err=%d)\n", rc);
889 		return ERR_PTR(rc);
890 	}
891 
892 	return dev;
893 }
894 
895 static void gs_destroy_candev(struct gs_can *dev)
896 {
897 	unregister_candev(dev->netdev);
898 	usb_kill_anchored_urbs(&dev->tx_submitted);
899 	free_candev(dev->netdev);
900 }
901 
902 static int gs_usb_probe(struct usb_interface *intf,
903 			const struct usb_device_id *id)
904 {
905 	struct gs_usb *dev;
906 	int rc = -ENOMEM;
907 	unsigned int icount, i;
908 	struct gs_host_config *hconf;
909 	struct gs_device_config *dconf;
910 
911 	hconf = kmalloc(sizeof(*hconf), GFP_KERNEL);
912 	if (!hconf)
913 		return -ENOMEM;
914 
915 	hconf->byte_order = 0x0000beef;
916 
917 	/* send host config */
918 	rc = usb_control_msg(interface_to_usbdev(intf),
919 			     usb_sndctrlpipe(interface_to_usbdev(intf), 0),
920 			     GS_USB_BREQ_HOST_FORMAT,
921 			     USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
922 			     1,
923 			     intf->cur_altsetting->desc.bInterfaceNumber,
924 			     hconf,
925 			     sizeof(*hconf),
926 			     1000);
927 
928 	kfree(hconf);
929 
930 	if (rc < 0) {
931 		dev_err(&intf->dev, "Couldn't send data format (err=%d)\n",
932 			rc);
933 		return rc;
934 	}
935 
936 	dconf = kmalloc(sizeof(*dconf), GFP_KERNEL);
937 	if (!dconf)
938 		return -ENOMEM;
939 
940 	/* read device config */
941 	rc = usb_control_msg(interface_to_usbdev(intf),
942 			     usb_rcvctrlpipe(interface_to_usbdev(intf), 0),
943 			     GS_USB_BREQ_DEVICE_CONFIG,
944 			     USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
945 			     1,
946 			     intf->cur_altsetting->desc.bInterfaceNumber,
947 			     dconf,
948 			     sizeof(*dconf),
949 			     1000);
950 	if (rc < 0) {
951 		dev_err(&intf->dev, "Couldn't get device config: (err=%d)\n",
952 			rc);
953 		kfree(dconf);
954 		return rc;
955 	}
956 
957 	icount = dconf->icount + 1;
958 	dev_info(&intf->dev, "Configuring for %d interfaces\n", icount);
959 
960 	if (icount > GS_MAX_INTF) {
961 		dev_err(&intf->dev,
962 			"Driver cannot handle more that %d CAN interfaces\n",
963 			GS_MAX_INTF);
964 		kfree(dconf);
965 		return -EINVAL;
966 	}
967 
968 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
969 	if (!dev) {
970 		kfree(dconf);
971 		return -ENOMEM;
972 	}
973 
974 	init_usb_anchor(&dev->rx_submitted);
975 
976 	atomic_set(&dev->active_channels, 0);
977 
978 	usb_set_intfdata(intf, dev);
979 	dev->udev = interface_to_usbdev(intf);
980 
981 	for (i = 0; i < icount; i++) {
982 		dev->canch[i] = gs_make_candev(i, intf, dconf);
983 		if (IS_ERR_OR_NULL(dev->canch[i])) {
984 			/* save error code to return later */
985 			rc = PTR_ERR(dev->canch[i]);
986 
987 			/* on failure destroy previously created candevs */
988 			icount = i;
989 			for (i = 0; i < icount; i++)
990 				gs_destroy_candev(dev->canch[i]);
991 
992 			usb_kill_anchored_urbs(&dev->rx_submitted);
993 			kfree(dconf);
994 			kfree(dev);
995 			return rc;
996 		}
997 		dev->canch[i]->parent = dev;
998 	}
999 
1000 	kfree(dconf);
1001 
1002 	return 0;
1003 }
1004 
1005 static void gs_usb_disconnect(struct usb_interface *intf)
1006 {
1007 	unsigned i;
1008 	struct gs_usb *dev = usb_get_intfdata(intf);
1009 	usb_set_intfdata(intf, NULL);
1010 
1011 	if (!dev) {
1012 		dev_err(&intf->dev, "Disconnect (nodata)\n");
1013 		return;
1014 	}
1015 
1016 	for (i = 0; i < GS_MAX_INTF; i++)
1017 		if (dev->canch[i])
1018 			gs_destroy_candev(dev->canch[i]);
1019 
1020 	usb_kill_anchored_urbs(&dev->rx_submitted);
1021 	kfree(dev);
1022 }
1023 
1024 static const struct usb_device_id gs_usb_table[] = {
1025 	{ USB_DEVICE_INTERFACE_NUMBER(USB_GSUSB_1_VENDOR_ID,
1026 				      USB_GSUSB_1_PRODUCT_ID, 0) },
1027 	{ USB_DEVICE_INTERFACE_NUMBER(USB_CANDLELIGHT_VENDOR_ID,
1028 				      USB_CANDLELIGHT_PRODUCT_ID, 0) },
1029 	{} /* Terminating entry */
1030 };
1031 
1032 MODULE_DEVICE_TABLE(usb, gs_usb_table);
1033 
1034 static struct usb_driver gs_usb_driver = {
1035 	.name       = "gs_usb",
1036 	.probe      = gs_usb_probe,
1037 	.disconnect = gs_usb_disconnect,
1038 	.id_table   = gs_usb_table,
1039 };
1040 
1041 module_usb_driver(gs_usb_driver);
1042 
1043 MODULE_AUTHOR("Maximilian Schneider <mws@schneidersoft.net>");
1044 MODULE_DESCRIPTION(
1045 "Socket CAN device driver for Geschwister Schneider Technologie-, "
1046 "Entwicklungs- und Vertriebs UG. USB2.0 to CAN interfaces\n"
1047 "and bytewerk.org candleLight USB CAN interfaces.");
1048 MODULE_LICENSE("GPL v2");
1049