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