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