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