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