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