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