xref: /openbmc/linux/drivers/net/can/usb/esd_usb.c (revision 6726d552)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * CAN driver for esd electronics gmbh CAN-USB/2 and CAN-USB/Micro
4  *
5  * Copyright (C) 2010-2012 esd electronic system design gmbh, Matthias Fuchs <socketcan@esd.eu>
6  * Copyright (C) 2022 esd electronics gmbh, Frank Jungclaus <frank.jungclaus@esd.eu>
7  */
8 #include <linux/ethtool.h>
9 #include <linux/signal.h>
10 #include <linux/slab.h>
11 #include <linux/module.h>
12 #include <linux/netdevice.h>
13 #include <linux/usb.h>
14 
15 #include <linux/can.h>
16 #include <linux/can/dev.h>
17 #include <linux/can/error.h>
18 
19 MODULE_AUTHOR("Matthias Fuchs <socketcan@esd.eu>");
20 MODULE_AUTHOR("Frank Jungclaus <frank.jungclaus@esd.eu>");
21 MODULE_DESCRIPTION("CAN driver for esd electronics gmbh CAN-USB/2 and CAN-USB/Micro interfaces");
22 MODULE_LICENSE("GPL v2");
23 
24 /* USB vendor and product ID */
25 #define USB_ESDGMBH_VENDOR_ID	0x0ab4
26 #define USB_CANUSB2_PRODUCT_ID	0x0010
27 #define USB_CANUSBM_PRODUCT_ID	0x0011
28 
29 /* CAN controller clock frequencies */
30 #define ESD_USB2_CAN_CLOCK	60000000
31 #define ESD_USBM_CAN_CLOCK	36000000
32 
33 /* Maximum number of CAN nets */
34 #define ESD_USB_MAX_NETS	2
35 
36 /* USB commands */
37 #define CMD_VERSION		1 /* also used for VERSION_REPLY */
38 #define CMD_CAN_RX		2 /* device to host only */
39 #define CMD_CAN_TX		3 /* also used for TX_DONE */
40 #define CMD_SETBAUD		4 /* also used for SETBAUD_REPLY */
41 #define CMD_TS			5 /* also used for TS_REPLY */
42 #define CMD_IDADD		6 /* also used for IDADD_REPLY */
43 
44 /* esd CAN message flags - dlc field */
45 #define ESD_RTR			0x10
46 
47 /* esd CAN message flags - id field */
48 #define ESD_EXTID		0x20000000
49 #define ESD_EVENT		0x40000000
50 #define ESD_IDMASK		0x1fffffff
51 
52 /* esd CAN event ids */
53 #define ESD_EV_CAN_ERROR_EXT	2 /* CAN controller specific diagnostic data */
54 
55 /* baudrate message flags */
56 #define ESD_USB_UBR		0x80000000
57 #define ESD_USB_LOM		0x40000000
58 #define ESD_USB_NO_BAUDRATE	0x7fffffff
59 
60 /* bit timing CAN-USB/2 */
61 #define ESD_USB2_TSEG1_MIN	1
62 #define ESD_USB2_TSEG1_MAX	16
63 #define ESD_USB2_TSEG1_SHIFT	16
64 #define ESD_USB2_TSEG2_MIN	1
65 #define ESD_USB2_TSEG2_MAX	8
66 #define ESD_USB2_TSEG2_SHIFT	20
67 #define ESD_USB2_SJW_MAX	4
68 #define ESD_USB2_SJW_SHIFT	14
69 #define ESD_USBM_SJW_SHIFT	24
70 #define ESD_USB2_BRP_MIN	1
71 #define ESD_USB2_BRP_MAX	1024
72 #define ESD_USB2_BRP_INC	1
73 #define ESD_USB2_3_SAMPLES	0x00800000
74 
75 /* esd IDADD message */
76 #define ESD_ID_ENABLE		0x80
77 #define ESD_MAX_ID_SEGMENT	64
78 
79 /* SJA1000 ECC register (emulated by usb firmware) */
80 #define SJA1000_ECC_SEG		0x1F
81 #define SJA1000_ECC_DIR		0x20
82 #define SJA1000_ECC_ERR		0x06
83 #define SJA1000_ECC_BIT		0x00
84 #define SJA1000_ECC_FORM	0x40
85 #define SJA1000_ECC_STUFF	0x80
86 #define SJA1000_ECC_MASK	0xc0
87 
88 /* esd bus state event codes */
89 #define ESD_BUSSTATE_MASK	0xc0
90 #define ESD_BUSSTATE_WARN	0x40
91 #define ESD_BUSSTATE_ERRPASSIVE	0x80
92 #define ESD_BUSSTATE_BUSOFF	0xc0
93 
94 #define RX_BUFFER_SIZE		1024
95 #define MAX_RX_URBS		4
96 #define MAX_TX_URBS		16 /* must be power of 2 */
97 
98 struct header_msg {
99 	u8 len; /* len is always the total message length in 32bit words */
100 	u8 cmd;
101 	u8 rsvd[2];
102 };
103 
104 struct version_msg {
105 	u8 len;
106 	u8 cmd;
107 	u8 rsvd;
108 	u8 flags;
109 	__le32 drv_version;
110 };
111 
112 struct version_reply_msg {
113 	u8 len;
114 	u8 cmd;
115 	u8 nets;
116 	u8 features;
117 	__le32 version;
118 	u8 name[16];
119 	__le32 rsvd;
120 	__le32 ts;
121 };
122 
123 struct rx_msg {
124 	u8 len;
125 	u8 cmd;
126 	u8 net;
127 	u8 dlc;
128 	__le32 ts;
129 	__le32 id; /* upper 3 bits contain flags */
130 	u8 data[8];
131 };
132 
133 struct tx_msg {
134 	u8 len;
135 	u8 cmd;
136 	u8 net;
137 	u8 dlc;
138 	u32 hnd;	/* opaque handle, not used by device */
139 	__le32 id; /* upper 3 bits contain flags */
140 	u8 data[8];
141 };
142 
143 struct tx_done_msg {
144 	u8 len;
145 	u8 cmd;
146 	u8 net;
147 	u8 status;
148 	u32 hnd;	/* opaque handle, not used by device */
149 	__le32 ts;
150 };
151 
152 struct id_filter_msg {
153 	u8 len;
154 	u8 cmd;
155 	u8 net;
156 	u8 option;
157 	__le32 mask[ESD_MAX_ID_SEGMENT + 1];
158 };
159 
160 struct set_baudrate_msg {
161 	u8 len;
162 	u8 cmd;
163 	u8 net;
164 	u8 rsvd;
165 	__le32 baud;
166 };
167 
168 /* Main message type used between library and application */
169 struct __packed esd_usb_msg {
170 	union {
171 		struct header_msg hdr;
172 		struct version_msg version;
173 		struct version_reply_msg version_reply;
174 		struct rx_msg rx;
175 		struct tx_msg tx;
176 		struct tx_done_msg txdone;
177 		struct set_baudrate_msg setbaud;
178 		struct id_filter_msg filter;
179 	} msg;
180 };
181 
182 static struct usb_device_id esd_usb_table[] = {
183 	{USB_DEVICE(USB_ESDGMBH_VENDOR_ID, USB_CANUSB2_PRODUCT_ID)},
184 	{USB_DEVICE(USB_ESDGMBH_VENDOR_ID, USB_CANUSBM_PRODUCT_ID)},
185 	{}
186 };
187 MODULE_DEVICE_TABLE(usb, esd_usb_table);
188 
189 struct esd_usb_net_priv;
190 
191 struct esd_tx_urb_context {
192 	struct esd_usb_net_priv *priv;
193 	u32 echo_index;
194 };
195 
196 struct esd_usb {
197 	struct usb_device *udev;
198 	struct esd_usb_net_priv *nets[ESD_USB_MAX_NETS];
199 
200 	struct usb_anchor rx_submitted;
201 
202 	int net_count;
203 	u32 version;
204 	int rxinitdone;
205 	void *rxbuf[MAX_RX_URBS];
206 	dma_addr_t rxbuf_dma[MAX_RX_URBS];
207 };
208 
209 struct esd_usb_net_priv {
210 	struct can_priv can; /* must be the first member */
211 
212 	atomic_t active_tx_jobs;
213 	struct usb_anchor tx_submitted;
214 	struct esd_tx_urb_context tx_contexts[MAX_TX_URBS];
215 
216 	struct esd_usb *usb;
217 	struct net_device *netdev;
218 	int index;
219 	u8 old_state;
220 	struct can_berr_counter bec;
221 };
222 
223 static void esd_usb_rx_event(struct esd_usb_net_priv *priv,
224 			     struct esd_usb_msg *msg)
225 {
226 	struct net_device_stats *stats = &priv->netdev->stats;
227 	struct can_frame *cf;
228 	struct sk_buff *skb;
229 	u32 id = le32_to_cpu(msg->msg.rx.id) & ESD_IDMASK;
230 
231 	if (id == ESD_EV_CAN_ERROR_EXT) {
232 		u8 state = msg->msg.rx.data[0];
233 		u8 ecc = msg->msg.rx.data[1];
234 		u8 rxerr = msg->msg.rx.data[2];
235 		u8 txerr = msg->msg.rx.data[3];
236 
237 		skb = alloc_can_err_skb(priv->netdev, &cf);
238 		if (skb == NULL) {
239 			stats->rx_dropped++;
240 			return;
241 		}
242 
243 		if (state != priv->old_state) {
244 			priv->old_state = state;
245 
246 			switch (state & ESD_BUSSTATE_MASK) {
247 			case ESD_BUSSTATE_BUSOFF:
248 				priv->can.state = CAN_STATE_BUS_OFF;
249 				cf->can_id |= CAN_ERR_BUSOFF;
250 				priv->can.can_stats.bus_off++;
251 				can_bus_off(priv->netdev);
252 				break;
253 			case ESD_BUSSTATE_WARN:
254 				priv->can.state = CAN_STATE_ERROR_WARNING;
255 				priv->can.can_stats.error_warning++;
256 				break;
257 			case ESD_BUSSTATE_ERRPASSIVE:
258 				priv->can.state = CAN_STATE_ERROR_PASSIVE;
259 				priv->can.can_stats.error_passive++;
260 				break;
261 			default:
262 				priv->can.state = CAN_STATE_ERROR_ACTIVE;
263 				break;
264 			}
265 		} else {
266 			priv->can.can_stats.bus_error++;
267 			stats->rx_errors++;
268 
269 			cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR |
270 				      CAN_ERR_CNT;
271 
272 			switch (ecc & SJA1000_ECC_MASK) {
273 			case SJA1000_ECC_BIT:
274 				cf->data[2] |= CAN_ERR_PROT_BIT;
275 				break;
276 			case SJA1000_ECC_FORM:
277 				cf->data[2] |= CAN_ERR_PROT_FORM;
278 				break;
279 			case SJA1000_ECC_STUFF:
280 				cf->data[2] |= CAN_ERR_PROT_STUFF;
281 				break;
282 			default:
283 				cf->data[3] = ecc & SJA1000_ECC_SEG;
284 				break;
285 			}
286 
287 			/* Error occurred during transmission? */
288 			if (!(ecc & SJA1000_ECC_DIR))
289 				cf->data[2] |= CAN_ERR_PROT_TX;
290 
291 			if (priv->can.state == CAN_STATE_ERROR_WARNING ||
292 			    priv->can.state == CAN_STATE_ERROR_PASSIVE) {
293 				cf->data[1] = (txerr > rxerr) ?
294 					CAN_ERR_CRTL_TX_PASSIVE :
295 					CAN_ERR_CRTL_RX_PASSIVE;
296 			}
297 			cf->data[6] = txerr;
298 			cf->data[7] = rxerr;
299 		}
300 
301 		priv->bec.txerr = txerr;
302 		priv->bec.rxerr = rxerr;
303 
304 		netif_rx(skb);
305 	}
306 }
307 
308 static void esd_usb_rx_can_msg(struct esd_usb_net_priv *priv,
309 			       struct esd_usb_msg *msg)
310 {
311 	struct net_device_stats *stats = &priv->netdev->stats;
312 	struct can_frame *cf;
313 	struct sk_buff *skb;
314 	int i;
315 	u32 id;
316 
317 	if (!netif_device_present(priv->netdev))
318 		return;
319 
320 	id = le32_to_cpu(msg->msg.rx.id);
321 
322 	if (id & ESD_EVENT) {
323 		esd_usb_rx_event(priv, msg);
324 	} else {
325 		skb = alloc_can_skb(priv->netdev, &cf);
326 		if (skb == NULL) {
327 			stats->rx_dropped++;
328 			return;
329 		}
330 
331 		cf->can_id = id & ESD_IDMASK;
332 		can_frame_set_cc_len(cf, msg->msg.rx.dlc & ~ESD_RTR,
333 				     priv->can.ctrlmode);
334 
335 		if (id & ESD_EXTID)
336 			cf->can_id |= CAN_EFF_FLAG;
337 
338 		if (msg->msg.rx.dlc & ESD_RTR) {
339 			cf->can_id |= CAN_RTR_FLAG;
340 		} else {
341 			for (i = 0; i < cf->len; i++)
342 				cf->data[i] = msg->msg.rx.data[i];
343 
344 			stats->rx_bytes += cf->len;
345 		}
346 		stats->rx_packets++;
347 
348 		netif_rx(skb);
349 	}
350 }
351 
352 static void esd_usb_tx_done_msg(struct esd_usb_net_priv *priv,
353 				struct esd_usb_msg *msg)
354 {
355 	struct net_device_stats *stats = &priv->netdev->stats;
356 	struct net_device *netdev = priv->netdev;
357 	struct esd_tx_urb_context *context;
358 
359 	if (!netif_device_present(netdev))
360 		return;
361 
362 	context = &priv->tx_contexts[msg->msg.txdone.hnd & (MAX_TX_URBS - 1)];
363 
364 	if (!msg->msg.txdone.status) {
365 		stats->tx_packets++;
366 		stats->tx_bytes += can_get_echo_skb(netdev, context->echo_index,
367 						    NULL);
368 	} else {
369 		stats->tx_errors++;
370 		can_free_echo_skb(netdev, context->echo_index, NULL);
371 	}
372 
373 	/* Release context */
374 	context->echo_index = MAX_TX_URBS;
375 	atomic_dec(&priv->active_tx_jobs);
376 
377 	netif_wake_queue(netdev);
378 }
379 
380 static void esd_usb_read_bulk_callback(struct urb *urb)
381 {
382 	struct esd_usb *dev = urb->context;
383 	int retval;
384 	int pos = 0;
385 	int i;
386 
387 	switch (urb->status) {
388 	case 0: /* success */
389 		break;
390 
391 	case -ENOENT:
392 	case -EPIPE:
393 	case -EPROTO:
394 	case -ESHUTDOWN:
395 		return;
396 
397 	default:
398 		dev_info(dev->udev->dev.parent,
399 			 "Rx URB aborted (%d)\n", urb->status);
400 		goto resubmit_urb;
401 	}
402 
403 	while (pos < urb->actual_length) {
404 		struct esd_usb_msg *msg;
405 
406 		msg = (struct esd_usb_msg *)(urb->transfer_buffer + pos);
407 
408 		switch (msg->msg.hdr.cmd) {
409 		case CMD_CAN_RX:
410 			if (msg->msg.rx.net >= dev->net_count) {
411 				dev_err(dev->udev->dev.parent, "format error\n");
412 				break;
413 			}
414 
415 			esd_usb_rx_can_msg(dev->nets[msg->msg.rx.net], msg);
416 			break;
417 
418 		case CMD_CAN_TX:
419 			if (msg->msg.txdone.net >= dev->net_count) {
420 				dev_err(dev->udev->dev.parent, "format error\n");
421 				break;
422 			}
423 
424 			esd_usb_tx_done_msg(dev->nets[msg->msg.txdone.net],
425 					    msg);
426 			break;
427 		}
428 
429 		pos += msg->msg.hdr.len << 2;
430 
431 		if (pos > urb->actual_length) {
432 			dev_err(dev->udev->dev.parent, "format error\n");
433 			break;
434 		}
435 	}
436 
437 resubmit_urb:
438 	usb_fill_bulk_urb(urb, dev->udev, usb_rcvbulkpipe(dev->udev, 1),
439 			  urb->transfer_buffer, RX_BUFFER_SIZE,
440 			  esd_usb_read_bulk_callback, dev);
441 
442 	retval = usb_submit_urb(urb, GFP_ATOMIC);
443 	if (retval == -ENODEV) {
444 		for (i = 0; i < dev->net_count; i++) {
445 			if (dev->nets[i])
446 				netif_device_detach(dev->nets[i]->netdev);
447 		}
448 	} else if (retval) {
449 		dev_err(dev->udev->dev.parent,
450 			"failed resubmitting read bulk urb: %d\n", retval);
451 	}
452 }
453 
454 /* callback for bulk IN urb */
455 static void esd_usb_write_bulk_callback(struct urb *urb)
456 {
457 	struct esd_tx_urb_context *context = urb->context;
458 	struct esd_usb_net_priv *priv;
459 	struct net_device *netdev;
460 	size_t size = sizeof(struct esd_usb_msg);
461 
462 	WARN_ON(!context);
463 
464 	priv = context->priv;
465 	netdev = priv->netdev;
466 
467 	/* free up our allocated buffer */
468 	usb_free_coherent(urb->dev, size,
469 			  urb->transfer_buffer, urb->transfer_dma);
470 
471 	if (!netif_device_present(netdev))
472 		return;
473 
474 	if (urb->status)
475 		netdev_info(netdev, "Tx URB aborted (%d)\n", urb->status);
476 
477 	netif_trans_update(netdev);
478 }
479 
480 static ssize_t firmware_show(struct device *d,
481 			     struct device_attribute *attr, char *buf)
482 {
483 	struct usb_interface *intf = to_usb_interface(d);
484 	struct esd_usb *dev = usb_get_intfdata(intf);
485 
486 	return sprintf(buf, "%d.%d.%d\n",
487 		       (dev->version >> 12) & 0xf,
488 		       (dev->version >> 8) & 0xf,
489 		       dev->version & 0xff);
490 }
491 static DEVICE_ATTR_RO(firmware);
492 
493 static ssize_t hardware_show(struct device *d,
494 			     struct device_attribute *attr, char *buf)
495 {
496 	struct usb_interface *intf = to_usb_interface(d);
497 	struct esd_usb *dev = usb_get_intfdata(intf);
498 
499 	return sprintf(buf, "%d.%d.%d\n",
500 		       (dev->version >> 28) & 0xf,
501 		       (dev->version >> 24) & 0xf,
502 		       (dev->version >> 16) & 0xff);
503 }
504 static DEVICE_ATTR_RO(hardware);
505 
506 static ssize_t nets_show(struct device *d,
507 			 struct device_attribute *attr, char *buf)
508 {
509 	struct usb_interface *intf = to_usb_interface(d);
510 	struct esd_usb *dev = usb_get_intfdata(intf);
511 
512 	return sprintf(buf, "%d", dev->net_count);
513 }
514 static DEVICE_ATTR_RO(nets);
515 
516 static int esd_usb_send_msg(struct esd_usb *dev, struct esd_usb_msg *msg)
517 {
518 	int actual_length;
519 
520 	return usb_bulk_msg(dev->udev,
521 			    usb_sndbulkpipe(dev->udev, 2),
522 			    msg,
523 			    msg->msg.hdr.len << 2,
524 			    &actual_length,
525 			    1000);
526 }
527 
528 static int esd_usb_wait_msg(struct esd_usb *dev,
529 			    struct esd_usb_msg *msg)
530 {
531 	int actual_length;
532 
533 	return usb_bulk_msg(dev->udev,
534 			    usb_rcvbulkpipe(dev->udev, 1),
535 			    msg,
536 			    sizeof(*msg),
537 			    &actual_length,
538 			    1000);
539 }
540 
541 static int esd_usb_setup_rx_urbs(struct esd_usb *dev)
542 {
543 	int i, err = 0;
544 
545 	if (dev->rxinitdone)
546 		return 0;
547 
548 	for (i = 0; i < MAX_RX_URBS; i++) {
549 		struct urb *urb = NULL;
550 		u8 *buf = NULL;
551 		dma_addr_t buf_dma;
552 
553 		/* create a URB, and a buffer for it */
554 		urb = usb_alloc_urb(0, GFP_KERNEL);
555 		if (!urb) {
556 			err = -ENOMEM;
557 			break;
558 		}
559 
560 		buf = usb_alloc_coherent(dev->udev, RX_BUFFER_SIZE, GFP_KERNEL,
561 					 &buf_dma);
562 		if (!buf) {
563 			dev_warn(dev->udev->dev.parent,
564 				 "No memory left for USB buffer\n");
565 			err = -ENOMEM;
566 			goto freeurb;
567 		}
568 
569 		urb->transfer_dma = buf_dma;
570 
571 		usb_fill_bulk_urb(urb, dev->udev,
572 				  usb_rcvbulkpipe(dev->udev, 1),
573 				  buf, RX_BUFFER_SIZE,
574 				  esd_usb_read_bulk_callback, dev);
575 		urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
576 		usb_anchor_urb(urb, &dev->rx_submitted);
577 
578 		err = usb_submit_urb(urb, GFP_KERNEL);
579 		if (err) {
580 			usb_unanchor_urb(urb);
581 			usb_free_coherent(dev->udev, RX_BUFFER_SIZE, buf,
582 					  urb->transfer_dma);
583 			goto freeurb;
584 		}
585 
586 		dev->rxbuf[i] = buf;
587 		dev->rxbuf_dma[i] = buf_dma;
588 
589 freeurb:
590 		/* Drop reference, USB core will take care of freeing it */
591 		usb_free_urb(urb);
592 		if (err)
593 			break;
594 	}
595 
596 	/* Did we submit any URBs */
597 	if (i == 0) {
598 		dev_err(dev->udev->dev.parent, "couldn't setup read URBs\n");
599 		return err;
600 	}
601 
602 	/* Warn if we've couldn't transmit all the URBs */
603 	if (i < MAX_RX_URBS) {
604 		dev_warn(dev->udev->dev.parent,
605 			 "rx performance may be slow\n");
606 	}
607 
608 	dev->rxinitdone = 1;
609 	return 0;
610 }
611 
612 /* Start interface */
613 static int esd_usb_start(struct esd_usb_net_priv *priv)
614 {
615 	struct esd_usb *dev = priv->usb;
616 	struct net_device *netdev = priv->netdev;
617 	struct esd_usb_msg *msg;
618 	int err, i;
619 
620 	msg = kmalloc(sizeof(*msg), GFP_KERNEL);
621 	if (!msg) {
622 		err = -ENOMEM;
623 		goto out;
624 	}
625 
626 	/* Enable all IDs
627 	 * The IDADD message takes up to 64 32 bit bitmasks (2048 bits).
628 	 * Each bit represents one 11 bit CAN identifier. A set bit
629 	 * enables reception of the corresponding CAN identifier. A cleared
630 	 * bit disabled this identifier. An additional bitmask value
631 	 * following the CAN 2.0A bits is used to enable reception of
632 	 * extended CAN frames. Only the LSB of this final mask is checked
633 	 * for the complete 29 bit ID range. The IDADD message also allows
634 	 * filter configuration for an ID subset. In this case you can add
635 	 * the number of the starting bitmask (0..64) to the filter.option
636 	 * field followed by only some bitmasks.
637 	 */
638 	msg->msg.hdr.cmd = CMD_IDADD;
639 	msg->msg.hdr.len = 2 + ESD_MAX_ID_SEGMENT;
640 	msg->msg.filter.net = priv->index;
641 	msg->msg.filter.option = ESD_ID_ENABLE; /* start with segment 0 */
642 	for (i = 0; i < ESD_MAX_ID_SEGMENT; i++)
643 		msg->msg.filter.mask[i] = cpu_to_le32(0xffffffff);
644 	/* enable 29bit extended IDs */
645 	msg->msg.filter.mask[ESD_MAX_ID_SEGMENT] = cpu_to_le32(0x00000001);
646 
647 	err = esd_usb_send_msg(dev, msg);
648 	if (err)
649 		goto out;
650 
651 	err = esd_usb_setup_rx_urbs(dev);
652 	if (err)
653 		goto out;
654 
655 	priv->can.state = CAN_STATE_ERROR_ACTIVE;
656 
657 out:
658 	if (err == -ENODEV)
659 		netif_device_detach(netdev);
660 	if (err)
661 		netdev_err(netdev, "couldn't start device: %d\n", err);
662 
663 	kfree(msg);
664 	return err;
665 }
666 
667 static void unlink_all_urbs(struct esd_usb *dev)
668 {
669 	struct esd_usb_net_priv *priv;
670 	int i, j;
671 
672 	usb_kill_anchored_urbs(&dev->rx_submitted);
673 
674 	for (i = 0; i < MAX_RX_URBS; ++i)
675 		usb_free_coherent(dev->udev, RX_BUFFER_SIZE,
676 				  dev->rxbuf[i], dev->rxbuf_dma[i]);
677 
678 	for (i = 0; i < dev->net_count; i++) {
679 		priv = dev->nets[i];
680 		if (priv) {
681 			usb_kill_anchored_urbs(&priv->tx_submitted);
682 			atomic_set(&priv->active_tx_jobs, 0);
683 
684 			for (j = 0; j < MAX_TX_URBS; j++)
685 				priv->tx_contexts[j].echo_index = MAX_TX_URBS;
686 		}
687 	}
688 }
689 
690 static int esd_usb_open(struct net_device *netdev)
691 {
692 	struct esd_usb_net_priv *priv = netdev_priv(netdev);
693 	int err;
694 
695 	/* common open */
696 	err = open_candev(netdev);
697 	if (err)
698 		return err;
699 
700 	/* finally start device */
701 	err = esd_usb_start(priv);
702 	if (err) {
703 		netdev_warn(netdev, "couldn't start device: %d\n", err);
704 		close_candev(netdev);
705 		return err;
706 	}
707 
708 	netif_start_queue(netdev);
709 
710 	return 0;
711 }
712 
713 static netdev_tx_t esd_usb_start_xmit(struct sk_buff *skb,
714 				      struct net_device *netdev)
715 {
716 	struct esd_usb_net_priv *priv = netdev_priv(netdev);
717 	struct esd_usb *dev = priv->usb;
718 	struct esd_tx_urb_context *context = NULL;
719 	struct net_device_stats *stats = &netdev->stats;
720 	struct can_frame *cf = (struct can_frame *)skb->data;
721 	struct esd_usb_msg *msg;
722 	struct urb *urb;
723 	u8 *buf;
724 	int i, err;
725 	int ret = NETDEV_TX_OK;
726 	size_t size = sizeof(struct esd_usb_msg);
727 
728 	if (can_dropped_invalid_skb(netdev, skb))
729 		return NETDEV_TX_OK;
730 
731 	/* create a URB, and a buffer for it, and copy the data to the URB */
732 	urb = usb_alloc_urb(0, GFP_ATOMIC);
733 	if (!urb) {
734 		stats->tx_dropped++;
735 		dev_kfree_skb(skb);
736 		goto nourbmem;
737 	}
738 
739 	buf = usb_alloc_coherent(dev->udev, size, GFP_ATOMIC,
740 				 &urb->transfer_dma);
741 	if (!buf) {
742 		netdev_err(netdev, "No memory left for USB buffer\n");
743 		stats->tx_dropped++;
744 		dev_kfree_skb(skb);
745 		goto nobufmem;
746 	}
747 
748 	msg = (struct esd_usb_msg *)buf;
749 
750 	msg->msg.hdr.len = 3; /* minimal length */
751 	msg->msg.hdr.cmd = CMD_CAN_TX;
752 	msg->msg.tx.net = priv->index;
753 	msg->msg.tx.dlc = can_get_cc_dlc(cf, priv->can.ctrlmode);
754 	msg->msg.tx.id = cpu_to_le32(cf->can_id & CAN_ERR_MASK);
755 
756 	if (cf->can_id & CAN_RTR_FLAG)
757 		msg->msg.tx.dlc |= ESD_RTR;
758 
759 	if (cf->can_id & CAN_EFF_FLAG)
760 		msg->msg.tx.id |= cpu_to_le32(ESD_EXTID);
761 
762 	for (i = 0; i < cf->len; i++)
763 		msg->msg.tx.data[i] = cf->data[i];
764 
765 	msg->msg.hdr.len += (cf->len + 3) >> 2;
766 
767 	for (i = 0; i < MAX_TX_URBS; i++) {
768 		if (priv->tx_contexts[i].echo_index == MAX_TX_URBS) {
769 			context = &priv->tx_contexts[i];
770 			break;
771 		}
772 	}
773 
774 	/* This may never happen */
775 	if (!context) {
776 		netdev_warn(netdev, "couldn't find free context\n");
777 		ret = NETDEV_TX_BUSY;
778 		goto releasebuf;
779 	}
780 
781 	context->priv = priv;
782 	context->echo_index = i;
783 
784 	/* hnd must not be 0 - MSB is stripped in txdone handling */
785 	msg->msg.tx.hnd = 0x80000000 | i; /* returned in TX done message */
786 
787 	usb_fill_bulk_urb(urb, dev->udev, usb_sndbulkpipe(dev->udev, 2), buf,
788 			  msg->msg.hdr.len << 2,
789 			  esd_usb_write_bulk_callback, context);
790 
791 	urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
792 
793 	usb_anchor_urb(urb, &priv->tx_submitted);
794 
795 	can_put_echo_skb(skb, netdev, context->echo_index, 0);
796 
797 	atomic_inc(&priv->active_tx_jobs);
798 
799 	/* Slow down tx path */
800 	if (atomic_read(&priv->active_tx_jobs) >= MAX_TX_URBS)
801 		netif_stop_queue(netdev);
802 
803 	err = usb_submit_urb(urb, GFP_ATOMIC);
804 	if (err) {
805 		can_free_echo_skb(netdev, context->echo_index, NULL);
806 
807 		atomic_dec(&priv->active_tx_jobs);
808 		usb_unanchor_urb(urb);
809 
810 		stats->tx_dropped++;
811 
812 		if (err == -ENODEV)
813 			netif_device_detach(netdev);
814 		else
815 			netdev_warn(netdev, "failed tx_urb %d\n", err);
816 
817 		goto releasebuf;
818 	}
819 
820 	netif_trans_update(netdev);
821 
822 	/* Release our reference to this URB, the USB core will eventually free
823 	 * it entirely.
824 	 */
825 	usb_free_urb(urb);
826 
827 	return NETDEV_TX_OK;
828 
829 releasebuf:
830 	usb_free_coherent(dev->udev, size, buf, urb->transfer_dma);
831 
832 nobufmem:
833 	usb_free_urb(urb);
834 
835 nourbmem:
836 	return ret;
837 }
838 
839 static int esd_usb_close(struct net_device *netdev)
840 {
841 	struct esd_usb_net_priv *priv = netdev_priv(netdev);
842 	struct esd_usb_msg *msg;
843 	int i;
844 
845 	msg = kmalloc(sizeof(*msg), GFP_KERNEL);
846 	if (!msg)
847 		return -ENOMEM;
848 
849 	/* Disable all IDs (see esd_usb_start()) */
850 	msg->msg.hdr.cmd = CMD_IDADD;
851 	msg->msg.hdr.len = 2 + ESD_MAX_ID_SEGMENT;
852 	msg->msg.filter.net = priv->index;
853 	msg->msg.filter.option = ESD_ID_ENABLE; /* start with segment 0 */
854 	for (i = 0; i <= ESD_MAX_ID_SEGMENT; i++)
855 		msg->msg.filter.mask[i] = 0;
856 	if (esd_usb_send_msg(priv->usb, msg) < 0)
857 		netdev_err(netdev, "sending idadd message failed\n");
858 
859 	/* set CAN controller to reset mode */
860 	msg->msg.hdr.len = 2;
861 	msg->msg.hdr.cmd = CMD_SETBAUD;
862 	msg->msg.setbaud.net = priv->index;
863 	msg->msg.setbaud.rsvd = 0;
864 	msg->msg.setbaud.baud = cpu_to_le32(ESD_USB_NO_BAUDRATE);
865 	if (esd_usb_send_msg(priv->usb, msg) < 0)
866 		netdev_err(netdev, "sending setbaud message failed\n");
867 
868 	priv->can.state = CAN_STATE_STOPPED;
869 
870 	netif_stop_queue(netdev);
871 
872 	close_candev(netdev);
873 
874 	kfree(msg);
875 
876 	return 0;
877 }
878 
879 static const struct net_device_ops esd_usb_netdev_ops = {
880 	.ndo_open = esd_usb_open,
881 	.ndo_stop = esd_usb_close,
882 	.ndo_start_xmit = esd_usb_start_xmit,
883 	.ndo_change_mtu = can_change_mtu,
884 };
885 
886 static const struct ethtool_ops esd_usb_ethtool_ops = {
887 	.get_ts_info = ethtool_op_get_ts_info,
888 };
889 
890 static const struct can_bittiming_const esd_usb2_bittiming_const = {
891 	.name = "esd_usb2",
892 	.tseg1_min = ESD_USB2_TSEG1_MIN,
893 	.tseg1_max = ESD_USB2_TSEG1_MAX,
894 	.tseg2_min = ESD_USB2_TSEG2_MIN,
895 	.tseg2_max = ESD_USB2_TSEG2_MAX,
896 	.sjw_max = ESD_USB2_SJW_MAX,
897 	.brp_min = ESD_USB2_BRP_MIN,
898 	.brp_max = ESD_USB2_BRP_MAX,
899 	.brp_inc = ESD_USB2_BRP_INC,
900 };
901 
902 static int esd_usb2_set_bittiming(struct net_device *netdev)
903 {
904 	struct esd_usb_net_priv *priv = netdev_priv(netdev);
905 	struct can_bittiming *bt = &priv->can.bittiming;
906 	struct esd_usb_msg *msg;
907 	int err;
908 	u32 canbtr;
909 	int sjw_shift;
910 
911 	canbtr = ESD_USB_UBR;
912 	if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
913 		canbtr |= ESD_USB_LOM;
914 
915 	canbtr |= (bt->brp - 1) & (ESD_USB2_BRP_MAX - 1);
916 
917 	if (le16_to_cpu(priv->usb->udev->descriptor.idProduct) ==
918 	    USB_CANUSBM_PRODUCT_ID)
919 		sjw_shift = ESD_USBM_SJW_SHIFT;
920 	else
921 		sjw_shift = ESD_USB2_SJW_SHIFT;
922 
923 	canbtr |= ((bt->sjw - 1) & (ESD_USB2_SJW_MAX - 1))
924 		<< sjw_shift;
925 	canbtr |= ((bt->prop_seg + bt->phase_seg1 - 1)
926 		   & (ESD_USB2_TSEG1_MAX - 1))
927 		<< ESD_USB2_TSEG1_SHIFT;
928 	canbtr |= ((bt->phase_seg2 - 1) & (ESD_USB2_TSEG2_MAX - 1))
929 		<< ESD_USB2_TSEG2_SHIFT;
930 	if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
931 		canbtr |= ESD_USB2_3_SAMPLES;
932 
933 	msg = kmalloc(sizeof(*msg), GFP_KERNEL);
934 	if (!msg)
935 		return -ENOMEM;
936 
937 	msg->msg.hdr.len = 2;
938 	msg->msg.hdr.cmd = CMD_SETBAUD;
939 	msg->msg.setbaud.net = priv->index;
940 	msg->msg.setbaud.rsvd = 0;
941 	msg->msg.setbaud.baud = cpu_to_le32(canbtr);
942 
943 	netdev_info(netdev, "setting BTR=%#x\n", canbtr);
944 
945 	err = esd_usb_send_msg(priv->usb, msg);
946 
947 	kfree(msg);
948 	return err;
949 }
950 
951 static int esd_usb_get_berr_counter(const struct net_device *netdev,
952 				    struct can_berr_counter *bec)
953 {
954 	struct esd_usb_net_priv *priv = netdev_priv(netdev);
955 
956 	bec->txerr = priv->bec.txerr;
957 	bec->rxerr = priv->bec.rxerr;
958 
959 	return 0;
960 }
961 
962 static int esd_usb_set_mode(struct net_device *netdev, enum can_mode mode)
963 {
964 	switch (mode) {
965 	case CAN_MODE_START:
966 		netif_wake_queue(netdev);
967 		break;
968 
969 	default:
970 		return -EOPNOTSUPP;
971 	}
972 
973 	return 0;
974 }
975 
976 static int esd_usb_probe_one_net(struct usb_interface *intf, int index)
977 {
978 	struct esd_usb *dev = usb_get_intfdata(intf);
979 	struct net_device *netdev;
980 	struct esd_usb_net_priv *priv;
981 	int err = 0;
982 	int i;
983 
984 	netdev = alloc_candev(sizeof(*priv), MAX_TX_URBS);
985 	if (!netdev) {
986 		dev_err(&intf->dev, "couldn't alloc candev\n");
987 		err = -ENOMEM;
988 		goto done;
989 	}
990 
991 	priv = netdev_priv(netdev);
992 
993 	init_usb_anchor(&priv->tx_submitted);
994 	atomic_set(&priv->active_tx_jobs, 0);
995 
996 	for (i = 0; i < MAX_TX_URBS; i++)
997 		priv->tx_contexts[i].echo_index = MAX_TX_URBS;
998 
999 	priv->usb = dev;
1000 	priv->netdev = netdev;
1001 	priv->index = index;
1002 
1003 	priv->can.state = CAN_STATE_STOPPED;
1004 	priv->can.ctrlmode_supported = CAN_CTRLMODE_LISTENONLY |
1005 		CAN_CTRLMODE_CC_LEN8_DLC;
1006 
1007 	if (le16_to_cpu(dev->udev->descriptor.idProduct) ==
1008 	    USB_CANUSBM_PRODUCT_ID)
1009 		priv->can.clock.freq = ESD_USBM_CAN_CLOCK;
1010 	else {
1011 		priv->can.clock.freq = ESD_USB2_CAN_CLOCK;
1012 		priv->can.ctrlmode_supported |= CAN_CTRLMODE_3_SAMPLES;
1013 	}
1014 
1015 	priv->can.bittiming_const = &esd_usb2_bittiming_const;
1016 	priv->can.do_set_bittiming = esd_usb2_set_bittiming;
1017 	priv->can.do_set_mode = esd_usb_set_mode;
1018 	priv->can.do_get_berr_counter = esd_usb_get_berr_counter;
1019 
1020 	netdev->flags |= IFF_ECHO; /* we support local echo */
1021 
1022 	netdev->netdev_ops = &esd_usb_netdev_ops;
1023 	netdev->ethtool_ops = &esd_usb_ethtool_ops;
1024 
1025 	SET_NETDEV_DEV(netdev, &intf->dev);
1026 	netdev->dev_id = index;
1027 
1028 	err = register_candev(netdev);
1029 	if (err) {
1030 		dev_err(&intf->dev, "couldn't register CAN device: %d\n", err);
1031 		free_candev(netdev);
1032 		err = -ENOMEM;
1033 		goto done;
1034 	}
1035 
1036 	dev->nets[index] = priv;
1037 	netdev_info(netdev, "device %s registered\n", netdev->name);
1038 
1039 done:
1040 	return err;
1041 }
1042 
1043 /* probe function for new USB devices
1044  *
1045  * check version information and number of available
1046  * CAN interfaces
1047  */
1048 static int esd_usb_probe(struct usb_interface *intf,
1049 			 const struct usb_device_id *id)
1050 {
1051 	struct esd_usb *dev;
1052 	struct esd_usb_msg *msg;
1053 	int i, err;
1054 
1055 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1056 	if (!dev) {
1057 		err = -ENOMEM;
1058 		goto done;
1059 	}
1060 
1061 	dev->udev = interface_to_usbdev(intf);
1062 
1063 	init_usb_anchor(&dev->rx_submitted);
1064 
1065 	usb_set_intfdata(intf, dev);
1066 
1067 	msg = kmalloc(sizeof(*msg), GFP_KERNEL);
1068 	if (!msg) {
1069 		err = -ENOMEM;
1070 		goto free_msg;
1071 	}
1072 
1073 	/* query number of CAN interfaces (nets) */
1074 	msg->msg.hdr.cmd = CMD_VERSION;
1075 	msg->msg.hdr.len = 2;
1076 	msg->msg.version.rsvd = 0;
1077 	msg->msg.version.flags = 0;
1078 	msg->msg.version.drv_version = 0;
1079 
1080 	err = esd_usb_send_msg(dev, msg);
1081 	if (err < 0) {
1082 		dev_err(&intf->dev, "sending version message failed\n");
1083 		goto free_msg;
1084 	}
1085 
1086 	err = esd_usb_wait_msg(dev, msg);
1087 	if (err < 0) {
1088 		dev_err(&intf->dev, "no version message answer\n");
1089 		goto free_msg;
1090 	}
1091 
1092 	dev->net_count = (int)msg->msg.version_reply.nets;
1093 	dev->version = le32_to_cpu(msg->msg.version_reply.version);
1094 
1095 	if (device_create_file(&intf->dev, &dev_attr_firmware))
1096 		dev_err(&intf->dev,
1097 			"Couldn't create device file for firmware\n");
1098 
1099 	if (device_create_file(&intf->dev, &dev_attr_hardware))
1100 		dev_err(&intf->dev,
1101 			"Couldn't create device file for hardware\n");
1102 
1103 	if (device_create_file(&intf->dev, &dev_attr_nets))
1104 		dev_err(&intf->dev,
1105 			"Couldn't create device file for nets\n");
1106 
1107 	/* do per device probing */
1108 	for (i = 0; i < dev->net_count; i++)
1109 		esd_usb_probe_one_net(intf, i);
1110 
1111 free_msg:
1112 	kfree(msg);
1113 	if (err)
1114 		kfree(dev);
1115 done:
1116 	return err;
1117 }
1118 
1119 /* called by the usb core when the device is removed from the system */
1120 static void esd_usb_disconnect(struct usb_interface *intf)
1121 {
1122 	struct esd_usb *dev = usb_get_intfdata(intf);
1123 	struct net_device *netdev;
1124 	int i;
1125 
1126 	device_remove_file(&intf->dev, &dev_attr_firmware);
1127 	device_remove_file(&intf->dev, &dev_attr_hardware);
1128 	device_remove_file(&intf->dev, &dev_attr_nets);
1129 
1130 	usb_set_intfdata(intf, NULL);
1131 
1132 	if (dev) {
1133 		for (i = 0; i < dev->net_count; i++) {
1134 			if (dev->nets[i]) {
1135 				netdev = dev->nets[i]->netdev;
1136 				unregister_netdev(netdev);
1137 				free_candev(netdev);
1138 			}
1139 		}
1140 		unlink_all_urbs(dev);
1141 		kfree(dev);
1142 	}
1143 }
1144 
1145 /* usb specific object needed to register this driver with the usb subsystem */
1146 static struct usb_driver esd_usb_driver = {
1147 	.name = KBUILD_MODNAME,
1148 	.probe = esd_usb_probe,
1149 	.disconnect = esd_usb_disconnect,
1150 	.id_table = esd_usb_table,
1151 };
1152 
1153 module_usb_driver(esd_usb_driver);
1154