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