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