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