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