ARM: dts: add 'dr_mode' property to hsotg devices for exynos boards
[linux/fpc-iii.git] / drivers / net / can / usb / esd_usb2.c
blobc063a54ab8dd8a598f36e5a7e712722bb5931df1
1 /*
2 * CAN driver for esd CAN-USB/2 and CAN-USB/Micro
4 * Copyright (C) 2010-2012 Matthias Fuchs <matthias.fuchs@esd.eu>, esd gmbh
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.
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.
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.
19 #include <linux/signal.h>
20 #include <linux/slab.h>
21 #include <linux/module.h>
22 #include <linux/netdevice.h>
23 #include <linux/usb.h>
25 #include <linux/can.h>
26 #include <linux/can/dev.h>
27 #include <linux/can/error.h>
29 MODULE_AUTHOR("Matthias Fuchs <matthias.fuchs@esd.eu>");
30 MODULE_DESCRIPTION("CAN driver for esd CAN-USB/2 and CAN-USB/Micro interfaces");
31 MODULE_LICENSE("GPL v2");
33 /* Define these values to match your devices */
34 #define USB_ESDGMBH_VENDOR_ID 0x0ab4
35 #define USB_CANUSB2_PRODUCT_ID 0x0010
36 #define USB_CANUSBM_PRODUCT_ID 0x0011
38 #define ESD_USB2_CAN_CLOCK 60000000
39 #define ESD_USBM_CAN_CLOCK 36000000
40 #define ESD_USB2_MAX_NETS 2
42 /* USB2 commands */
43 #define CMD_VERSION 1 /* also used for VERSION_REPLY */
44 #define CMD_CAN_RX 2 /* device to host only */
45 #define CMD_CAN_TX 3 /* also used for TX_DONE */
46 #define CMD_SETBAUD 4 /* also used for SETBAUD_REPLY */
47 #define CMD_TS 5 /* also used for TS_REPLY */
48 #define CMD_IDADD 6 /* also used for IDADD_REPLY */
50 /* esd CAN message flags - dlc field */
51 #define ESD_RTR 0x10
53 /* esd CAN message flags - id field */
54 #define ESD_EXTID 0x20000000
55 #define ESD_EVENT 0x40000000
56 #define ESD_IDMASK 0x1fffffff
58 /* esd CAN event ids used by this driver */
59 #define ESD_EV_CAN_ERROR_EXT 2
61 /* baudrate message flags */
62 #define ESD_USB2_UBR 0x80000000
63 #define ESD_USB2_LOM 0x40000000
64 #define ESD_USB2_NO_BAUDRATE 0x7fffffff
65 #define ESD_USB2_TSEG1_MIN 1
66 #define ESD_USB2_TSEG1_MAX 16
67 #define ESD_USB2_TSEG1_SHIFT 16
68 #define ESD_USB2_TSEG2_MIN 1
69 #define ESD_USB2_TSEG2_MAX 8
70 #define ESD_USB2_TSEG2_SHIFT 20
71 #define ESD_USB2_SJW_MAX 4
72 #define ESD_USB2_SJW_SHIFT 14
73 #define ESD_USBM_SJW_SHIFT 24
74 #define ESD_USB2_BRP_MIN 1
75 #define ESD_USB2_BRP_MAX 1024
76 #define ESD_USB2_BRP_INC 1
77 #define ESD_USB2_3_SAMPLES 0x00800000
79 /* esd IDADD message */
80 #define ESD_ID_ENABLE 0x80
81 #define ESD_MAX_ID_SEGMENT 64
83 /* SJA1000 ECC register (emulated by usb2 firmware) */
84 #define SJA1000_ECC_SEG 0x1F
85 #define SJA1000_ECC_DIR 0x20
86 #define SJA1000_ECC_ERR 0x06
87 #define SJA1000_ECC_BIT 0x00
88 #define SJA1000_ECC_FORM 0x40
89 #define SJA1000_ECC_STUFF 0x80
90 #define SJA1000_ECC_MASK 0xc0
92 /* esd bus state event codes */
93 #define ESD_BUSSTATE_MASK 0xc0
94 #define ESD_BUSSTATE_WARN 0x40
95 #define ESD_BUSSTATE_ERRPASSIVE 0x80
96 #define ESD_BUSSTATE_BUSOFF 0xc0
98 #define RX_BUFFER_SIZE 1024
99 #define MAX_RX_URBS 4
100 #define MAX_TX_URBS 16 /* must be power of 2 */
102 struct header_msg {
103 u8 len; /* len is always the total message length in 32bit words */
104 u8 cmd;
105 u8 rsvd[2];
108 struct version_msg {
109 u8 len;
110 u8 cmd;
111 u8 rsvd;
112 u8 flags;
113 __le32 drv_version;
116 struct version_reply_msg {
117 u8 len;
118 u8 cmd;
119 u8 nets;
120 u8 features;
121 __le32 version;
122 u8 name[16];
123 __le32 rsvd;
124 __le32 ts;
127 struct rx_msg {
128 u8 len;
129 u8 cmd;
130 u8 net;
131 u8 dlc;
132 __le32 ts;
133 __le32 id; /* upper 3 bits contain flags */
134 u8 data[8];
137 struct tx_msg {
138 u8 len;
139 u8 cmd;
140 u8 net;
141 u8 dlc;
142 __le32 hnd;
143 __le32 id; /* upper 3 bits contain flags */
144 u8 data[8];
147 struct tx_done_msg {
148 u8 len;
149 u8 cmd;
150 u8 net;
151 u8 status;
152 __le32 hnd;
153 __le32 ts;
156 struct id_filter_msg {
157 u8 len;
158 u8 cmd;
159 u8 net;
160 u8 option;
161 __le32 mask[ESD_MAX_ID_SEGMENT + 1];
164 struct set_baudrate_msg {
165 u8 len;
166 u8 cmd;
167 u8 net;
168 u8 rsvd;
169 __le32 baud;
172 /* Main message type used between library and application */
173 struct __attribute__ ((packed)) esd_usb2_msg {
174 union {
175 struct header_msg hdr;
176 struct version_msg version;
177 struct version_reply_msg version_reply;
178 struct rx_msg rx;
179 struct tx_msg tx;
180 struct tx_done_msg txdone;
181 struct set_baudrate_msg setbaud;
182 struct id_filter_msg filter;
183 } msg;
186 static struct usb_device_id esd_usb2_table[] = {
187 {USB_DEVICE(USB_ESDGMBH_VENDOR_ID, USB_CANUSB2_PRODUCT_ID)},
188 {USB_DEVICE(USB_ESDGMBH_VENDOR_ID, USB_CANUSBM_PRODUCT_ID)},
191 MODULE_DEVICE_TABLE(usb, esd_usb2_table);
193 struct esd_usb2_net_priv;
195 struct esd_tx_urb_context {
196 struct esd_usb2_net_priv *priv;
197 u32 echo_index;
198 int dlc;
201 struct esd_usb2 {
202 struct usb_device *udev;
203 struct esd_usb2_net_priv *nets[ESD_USB2_MAX_NETS];
205 struct usb_anchor rx_submitted;
207 int net_count;
208 u32 version;
209 int rxinitdone;
212 struct esd_usb2_net_priv {
213 struct can_priv can; /* must be the first member */
215 atomic_t active_tx_jobs;
216 struct usb_anchor tx_submitted;
217 struct esd_tx_urb_context tx_contexts[MAX_TX_URBS];
219 struct esd_usb2 *usb2;
220 struct net_device *netdev;
221 int index;
222 u8 old_state;
223 struct can_berr_counter bec;
226 static void esd_usb2_rx_event(struct esd_usb2_net_priv *priv,
227 struct esd_usb2_msg *msg)
229 struct net_device_stats *stats = &priv->netdev->stats;
230 struct can_frame *cf;
231 struct sk_buff *skb;
232 u32 id = le32_to_cpu(msg->msg.rx.id) & ESD_IDMASK;
234 if (id == ESD_EV_CAN_ERROR_EXT) {
235 u8 state = msg->msg.rx.data[0];
236 u8 ecc = msg->msg.rx.data[1];
237 u8 txerr = msg->msg.rx.data[2];
238 u8 rxerr = msg->msg.rx.data[3];
240 skb = alloc_can_err_skb(priv->netdev, &cf);
241 if (skb == NULL) {
242 stats->rx_dropped++;
243 return;
246 if (state != priv->old_state) {
247 priv->old_state = state;
249 switch (state & ESD_BUSSTATE_MASK) {
250 case ESD_BUSSTATE_BUSOFF:
251 priv->can.state = CAN_STATE_BUS_OFF;
252 cf->can_id |= CAN_ERR_BUSOFF;
253 can_bus_off(priv->netdev);
254 break;
255 case ESD_BUSSTATE_WARN:
256 priv->can.state = CAN_STATE_ERROR_WARNING;
257 priv->can.can_stats.error_warning++;
258 break;
259 case ESD_BUSSTATE_ERRPASSIVE:
260 priv->can.state = CAN_STATE_ERROR_PASSIVE;
261 priv->can.can_stats.error_passive++;
262 break;
263 default:
264 priv->can.state = CAN_STATE_ERROR_ACTIVE;
265 break;
267 } else {
268 priv->can.can_stats.bus_error++;
269 stats->rx_errors++;
271 cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
273 switch (ecc & SJA1000_ECC_MASK) {
274 case SJA1000_ECC_BIT:
275 cf->data[2] |= CAN_ERR_PROT_BIT;
276 break;
277 case SJA1000_ECC_FORM:
278 cf->data[2] |= CAN_ERR_PROT_FORM;
279 break;
280 case SJA1000_ECC_STUFF:
281 cf->data[2] |= CAN_ERR_PROT_STUFF;
282 break;
283 default:
284 cf->data[2] |= CAN_ERR_PROT_UNSPEC;
285 cf->data[3] = ecc & SJA1000_ECC_SEG;
286 break;
289 /* Error occurred during transmission? */
290 if (!(ecc & SJA1000_ECC_DIR))
291 cf->data[2] |= CAN_ERR_PROT_TX;
293 if (priv->can.state == CAN_STATE_ERROR_WARNING ||
294 priv->can.state == CAN_STATE_ERROR_PASSIVE) {
295 cf->data[1] = (txerr > rxerr) ?
296 CAN_ERR_CRTL_TX_PASSIVE :
297 CAN_ERR_CRTL_RX_PASSIVE;
299 cf->data[6] = txerr;
300 cf->data[7] = rxerr;
303 netif_rx(skb);
305 priv->bec.txerr = txerr;
306 priv->bec.rxerr = rxerr;
308 stats->rx_packets++;
309 stats->rx_bytes += cf->can_dlc;
313 static void esd_usb2_rx_can_msg(struct esd_usb2_net_priv *priv,
314 struct esd_usb2_msg *msg)
316 struct net_device_stats *stats = &priv->netdev->stats;
317 struct can_frame *cf;
318 struct sk_buff *skb;
319 int i;
320 u32 id;
322 if (!netif_device_present(priv->netdev))
323 return;
325 id = le32_to_cpu(msg->msg.rx.id);
327 if (id & ESD_EVENT) {
328 esd_usb2_rx_event(priv, msg);
329 } else {
330 skb = alloc_can_skb(priv->netdev, &cf);
331 if (skb == NULL) {
332 stats->rx_dropped++;
333 return;
336 cf->can_id = id & ESD_IDMASK;
337 cf->can_dlc = get_can_dlc(msg->msg.rx.dlc);
339 if (id & ESD_EXTID)
340 cf->can_id |= CAN_EFF_FLAG;
342 if (msg->msg.rx.dlc & ESD_RTR) {
343 cf->can_id |= CAN_RTR_FLAG;
344 } else {
345 for (i = 0; i < cf->can_dlc; i++)
346 cf->data[i] = msg->msg.rx.data[i];
349 netif_rx(skb);
351 stats->rx_packets++;
352 stats->rx_bytes += cf->can_dlc;
355 return;
358 static void esd_usb2_tx_done_msg(struct esd_usb2_net_priv *priv,
359 struct esd_usb2_msg *msg)
361 struct net_device_stats *stats = &priv->netdev->stats;
362 struct net_device *netdev = priv->netdev;
363 struct esd_tx_urb_context *context;
365 if (!netif_device_present(netdev))
366 return;
368 context = &priv->tx_contexts[msg->msg.txdone.hnd & (MAX_TX_URBS - 1)];
370 if (!msg->msg.txdone.status) {
371 stats->tx_packets++;
372 stats->tx_bytes += context->dlc;
373 can_get_echo_skb(netdev, context->echo_index);
374 } else {
375 stats->tx_errors++;
376 can_free_echo_skb(netdev, context->echo_index);
379 /* Release context */
380 context->echo_index = MAX_TX_URBS;
381 atomic_dec(&priv->active_tx_jobs);
383 netif_wake_queue(netdev);
386 static void esd_usb2_read_bulk_callback(struct urb *urb)
388 struct esd_usb2 *dev = urb->context;
389 int retval;
390 int pos = 0;
391 int i;
393 switch (urb->status) {
394 case 0: /* success */
395 break;
397 case -ENOENT:
398 case -ESHUTDOWN:
399 return;
401 default:
402 dev_info(dev->udev->dev.parent,
403 "Rx URB aborted (%d)\n", urb->status);
404 goto resubmit_urb;
407 while (pos < urb->actual_length) {
408 struct esd_usb2_msg *msg;
410 msg = (struct esd_usb2_msg *)(urb->transfer_buffer + pos);
412 switch (msg->msg.hdr.cmd) {
413 case CMD_CAN_RX:
414 if (msg->msg.rx.net >= dev->net_count) {
415 dev_err(dev->udev->dev.parent, "format error\n");
416 break;
419 esd_usb2_rx_can_msg(dev->nets[msg->msg.rx.net], msg);
420 break;
422 case CMD_CAN_TX:
423 if (msg->msg.txdone.net >= dev->net_count) {
424 dev_err(dev->udev->dev.parent, "format error\n");
425 break;
428 esd_usb2_tx_done_msg(dev->nets[msg->msg.txdone.net],
429 msg);
430 break;
433 pos += msg->msg.hdr.len << 2;
435 if (pos > urb->actual_length) {
436 dev_err(dev->udev->dev.parent, "format error\n");
437 break;
441 resubmit_urb:
442 usb_fill_bulk_urb(urb, dev->udev, usb_rcvbulkpipe(dev->udev, 1),
443 urb->transfer_buffer, RX_BUFFER_SIZE,
444 esd_usb2_read_bulk_callback, dev);
446 retval = usb_submit_urb(urb, GFP_ATOMIC);
447 if (retval == -ENODEV) {
448 for (i = 0; i < dev->net_count; i++) {
449 if (dev->nets[i])
450 netif_device_detach(dev->nets[i]->netdev);
452 } else if (retval) {
453 dev_err(dev->udev->dev.parent,
454 "failed resubmitting read bulk urb: %d\n", retval);
457 return;
461 * callback for bulk IN urb
463 static void esd_usb2_write_bulk_callback(struct urb *urb)
465 struct esd_tx_urb_context *context = urb->context;
466 struct esd_usb2_net_priv *priv;
467 struct net_device *netdev;
468 size_t size = sizeof(struct esd_usb2_msg);
470 WARN_ON(!context);
472 priv = context->priv;
473 netdev = priv->netdev;
475 /* free up our allocated buffer */
476 usb_free_coherent(urb->dev, size,
477 urb->transfer_buffer, urb->transfer_dma);
479 if (!netif_device_present(netdev))
480 return;
482 if (urb->status)
483 netdev_info(netdev, "Tx URB aborted (%d)\n", urb->status);
485 netdev->trans_start = jiffies;
488 static ssize_t show_firmware(struct device *d,
489 struct device_attribute *attr, char *buf)
491 struct usb_interface *intf = to_usb_interface(d);
492 struct esd_usb2 *dev = usb_get_intfdata(intf);
494 return sprintf(buf, "%d.%d.%d\n",
495 (dev->version >> 12) & 0xf,
496 (dev->version >> 8) & 0xf,
497 dev->version & 0xff);
499 static DEVICE_ATTR(firmware, S_IRUGO, show_firmware, NULL);
501 static ssize_t show_hardware(struct device *d,
502 struct device_attribute *attr, char *buf)
504 struct usb_interface *intf = to_usb_interface(d);
505 struct esd_usb2 *dev = usb_get_intfdata(intf);
507 return sprintf(buf, "%d.%d.%d\n",
508 (dev->version >> 28) & 0xf,
509 (dev->version >> 24) & 0xf,
510 (dev->version >> 16) & 0xff);
512 static DEVICE_ATTR(hardware, S_IRUGO, show_hardware, NULL);
514 static ssize_t show_nets(struct device *d,
515 struct device_attribute *attr, char *buf)
517 struct usb_interface *intf = to_usb_interface(d);
518 struct esd_usb2 *dev = usb_get_intfdata(intf);
520 return sprintf(buf, "%d", dev->net_count);
522 static DEVICE_ATTR(nets, S_IRUGO, show_nets, NULL);
524 static int esd_usb2_send_msg(struct esd_usb2 *dev, struct esd_usb2_msg *msg)
526 int actual_length;
528 return usb_bulk_msg(dev->udev,
529 usb_sndbulkpipe(dev->udev, 2),
530 msg,
531 msg->msg.hdr.len << 2,
532 &actual_length,
533 1000);
536 static int esd_usb2_wait_msg(struct esd_usb2 *dev,
537 struct esd_usb2_msg *msg)
539 int actual_length;
541 return usb_bulk_msg(dev->udev,
542 usb_rcvbulkpipe(dev->udev, 1),
543 msg,
544 sizeof(*msg),
545 &actual_length,
546 1000);
549 static int esd_usb2_setup_rx_urbs(struct esd_usb2 *dev)
551 int i, err = 0;
553 if (dev->rxinitdone)
554 return 0;
556 for (i = 0; i < MAX_RX_URBS; i++) {
557 struct urb *urb = NULL;
558 u8 *buf = NULL;
560 /* create a URB, and a buffer for it */
561 urb = usb_alloc_urb(0, GFP_KERNEL);
562 if (!urb) {
563 dev_warn(dev->udev->dev.parent,
564 "No memory left for URBs\n");
565 err = -ENOMEM;
566 break;
569 buf = usb_alloc_coherent(dev->udev, RX_BUFFER_SIZE, GFP_KERNEL,
570 &urb->transfer_dma);
571 if (!buf) {
572 dev_warn(dev->udev->dev.parent,
573 "No memory left for USB buffer\n");
574 err = -ENOMEM;
575 goto freeurb;
578 usb_fill_bulk_urb(urb, dev->udev,
579 usb_rcvbulkpipe(dev->udev, 1),
580 buf, RX_BUFFER_SIZE,
581 esd_usb2_read_bulk_callback, dev);
582 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
583 usb_anchor_urb(urb, &dev->rx_submitted);
585 err = usb_submit_urb(urb, GFP_KERNEL);
586 if (err) {
587 usb_unanchor_urb(urb);
588 usb_free_coherent(dev->udev, RX_BUFFER_SIZE, buf,
589 urb->transfer_dma);
592 freeurb:
593 /* Drop reference, USB core will take care of freeing it */
594 usb_free_urb(urb);
595 if (err)
596 break;
599 /* Did we submit any URBs */
600 if (i == 0) {
601 dev_err(dev->udev->dev.parent, "couldn't setup read URBs\n");
602 return err;
605 /* Warn if we've couldn't transmit all the URBs */
606 if (i < MAX_RX_URBS) {
607 dev_warn(dev->udev->dev.parent,
608 "rx performance may be slow\n");
611 dev->rxinitdone = 1;
612 return 0;
616 * Start interface
618 static int esd_usb2_start(struct esd_usb2_net_priv *priv)
620 struct esd_usb2 *dev = priv->usb2;
621 struct net_device *netdev = priv->netdev;
622 struct esd_usb2_msg *msg;
623 int err, i;
625 msg = kmalloc(sizeof(*msg), GFP_KERNEL);
626 if (!msg) {
627 err = -ENOMEM;
628 goto out;
632 * Enable all IDs
633 * The IDADD message takes up to 64 32 bit bitmasks (2048 bits).
634 * Each bit represents one 11 bit CAN identifier. A set bit
635 * enables reception of the corresponding CAN identifier. A cleared
636 * bit disabled this identifier. An additional bitmask value
637 * following the CAN 2.0A bits is used to enable reception of
638 * extended CAN frames. Only the LSB of this final mask is checked
639 * for the complete 29 bit ID range. The IDADD message also allows
640 * filter configuration for an ID subset. In this case you can add
641 * the number of the starting bitmask (0..64) to the filter.option
642 * field followed by only some bitmasks.
644 msg->msg.hdr.cmd = CMD_IDADD;
645 msg->msg.hdr.len = 2 + ESD_MAX_ID_SEGMENT;
646 msg->msg.filter.net = priv->index;
647 msg->msg.filter.option = ESD_ID_ENABLE; /* start with segment 0 */
648 for (i = 0; i < ESD_MAX_ID_SEGMENT; i++)
649 msg->msg.filter.mask[i] = cpu_to_le32(0xffffffff);
650 /* enable 29bit extended IDs */
651 msg->msg.filter.mask[ESD_MAX_ID_SEGMENT] = cpu_to_le32(0x00000001);
653 err = esd_usb2_send_msg(dev, msg);
654 if (err)
655 goto out;
657 err = esd_usb2_setup_rx_urbs(dev);
658 if (err)
659 goto out;
661 priv->can.state = CAN_STATE_ERROR_ACTIVE;
663 out:
664 if (err == -ENODEV)
665 netif_device_detach(netdev);
666 if (err)
667 netdev_err(netdev, "couldn't start device: %d\n", err);
669 kfree(msg);
670 return err;
673 static void unlink_all_urbs(struct esd_usb2 *dev)
675 struct esd_usb2_net_priv *priv;
676 int i, j;
678 usb_kill_anchored_urbs(&dev->rx_submitted);
679 for (i = 0; i < dev->net_count; i++) {
680 priv = dev->nets[i];
681 if (priv) {
682 usb_kill_anchored_urbs(&priv->tx_submitted);
683 atomic_set(&priv->active_tx_jobs, 0);
685 for (j = 0; j < MAX_TX_URBS; j++)
686 priv->tx_contexts[j].echo_index = MAX_TX_URBS;
691 static int esd_usb2_open(struct net_device *netdev)
693 struct esd_usb2_net_priv *priv = netdev_priv(netdev);
694 int err;
696 /* common open */
697 err = open_candev(netdev);
698 if (err)
699 return err;
701 /* finally start device */
702 err = esd_usb2_start(priv);
703 if (err) {
704 netdev_warn(netdev, "couldn't start device: %d\n", err);
705 close_candev(netdev);
706 return err;
709 netif_start_queue(netdev);
711 return 0;
714 static netdev_tx_t esd_usb2_start_xmit(struct sk_buff *skb,
715 struct net_device *netdev)
717 struct esd_usb2_net_priv *priv = netdev_priv(netdev);
718 struct esd_usb2 *dev = priv->usb2;
719 struct esd_tx_urb_context *context = NULL;
720 struct net_device_stats *stats = &netdev->stats;
721 struct can_frame *cf = (struct can_frame *)skb->data;
722 struct esd_usb2_msg *msg;
723 struct urb *urb;
724 u8 *buf;
725 int i, err;
726 int ret = NETDEV_TX_OK;
727 size_t size = sizeof(struct esd_usb2_msg);
729 if (can_dropped_invalid_skb(netdev, skb))
730 return NETDEV_TX_OK;
732 /* create a URB, and a buffer for it, and copy the data to the URB */
733 urb = usb_alloc_urb(0, GFP_ATOMIC);
734 if (!urb) {
735 netdev_err(netdev, "No memory left for URBs\n");
736 stats->tx_dropped++;
737 dev_kfree_skb(skb);
738 goto nourbmem;
741 buf = usb_alloc_coherent(dev->udev, size, GFP_ATOMIC,
742 &urb->transfer_dma);
743 if (!buf) {
744 netdev_err(netdev, "No memory left for USB buffer\n");
745 stats->tx_dropped++;
746 dev_kfree_skb(skb);
747 goto nobufmem;
750 msg = (struct esd_usb2_msg *)buf;
752 msg->msg.hdr.len = 3; /* minimal length */
753 msg->msg.hdr.cmd = CMD_CAN_TX;
754 msg->msg.tx.net = priv->index;
755 msg->msg.tx.dlc = cf->can_dlc;
756 msg->msg.tx.id = cpu_to_le32(cf->can_id & CAN_ERR_MASK);
758 if (cf->can_id & CAN_RTR_FLAG)
759 msg->msg.tx.dlc |= ESD_RTR;
761 if (cf->can_id & CAN_EFF_FLAG)
762 msg->msg.tx.id |= cpu_to_le32(ESD_EXTID);
764 for (i = 0; i < cf->can_dlc; i++)
765 msg->msg.tx.data[i] = cf->data[i];
767 msg->msg.hdr.len += (cf->can_dlc + 3) >> 2;
769 for (i = 0; i < MAX_TX_URBS; i++) {
770 if (priv->tx_contexts[i].echo_index == MAX_TX_URBS) {
771 context = &priv->tx_contexts[i];
772 break;
777 * This may never happen.
779 if (!context) {
780 netdev_warn(netdev, "couldn't find free context\n");
781 ret = NETDEV_TX_BUSY;
782 goto releasebuf;
785 context->priv = priv;
786 context->echo_index = i;
787 context->dlc = cf->can_dlc;
789 /* hnd must not be 0 - MSB is stripped in txdone handling */
790 msg->msg.tx.hnd = 0x80000000 | i; /* returned in TX done message */
792 usb_fill_bulk_urb(urb, dev->udev, usb_sndbulkpipe(dev->udev, 2), buf,
793 msg->msg.hdr.len << 2,
794 esd_usb2_write_bulk_callback, context);
796 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
798 usb_anchor_urb(urb, &priv->tx_submitted);
800 can_put_echo_skb(skb, netdev, context->echo_index);
802 atomic_inc(&priv->active_tx_jobs);
804 /* Slow down tx path */
805 if (atomic_read(&priv->active_tx_jobs) >= MAX_TX_URBS)
806 netif_stop_queue(netdev);
808 err = usb_submit_urb(urb, GFP_ATOMIC);
809 if (err) {
810 can_free_echo_skb(netdev, context->echo_index);
812 atomic_dec(&priv->active_tx_jobs);
813 usb_unanchor_urb(urb);
815 stats->tx_dropped++;
817 if (err == -ENODEV)
818 netif_device_detach(netdev);
819 else
820 netdev_warn(netdev, "failed tx_urb %d\n", err);
822 goto releasebuf;
825 netdev->trans_start = jiffies;
828 * Release our reference to this URB, the USB core will eventually free
829 * it entirely.
831 usb_free_urb(urb);
833 return NETDEV_TX_OK;
835 releasebuf:
836 usb_free_coherent(dev->udev, size, buf, urb->transfer_dma);
838 nobufmem:
839 usb_free_urb(urb);
841 nourbmem:
842 return ret;
845 static int esd_usb2_close(struct net_device *netdev)
847 struct esd_usb2_net_priv *priv = netdev_priv(netdev);
848 struct esd_usb2_msg *msg;
849 int i;
851 msg = kmalloc(sizeof(*msg), GFP_KERNEL);
852 if (!msg)
853 return -ENOMEM;
855 /* Disable all IDs (see esd_usb2_start()) */
856 msg->msg.hdr.cmd = CMD_IDADD;
857 msg->msg.hdr.len = 2 + ESD_MAX_ID_SEGMENT;
858 msg->msg.filter.net = priv->index;
859 msg->msg.filter.option = ESD_ID_ENABLE; /* start with segment 0 */
860 for (i = 0; i <= ESD_MAX_ID_SEGMENT; i++)
861 msg->msg.filter.mask[i] = 0;
862 if (esd_usb2_send_msg(priv->usb2, msg) < 0)
863 netdev_err(netdev, "sending idadd message failed\n");
865 /* set CAN controller to reset mode */
866 msg->msg.hdr.len = 2;
867 msg->msg.hdr.cmd = CMD_SETBAUD;
868 msg->msg.setbaud.net = priv->index;
869 msg->msg.setbaud.rsvd = 0;
870 msg->msg.setbaud.baud = cpu_to_le32(ESD_USB2_NO_BAUDRATE);
871 if (esd_usb2_send_msg(priv->usb2, msg) < 0)
872 netdev_err(netdev, "sending setbaud message failed\n");
874 priv->can.state = CAN_STATE_STOPPED;
876 netif_stop_queue(netdev);
878 close_candev(netdev);
880 kfree(msg);
882 return 0;
885 static const struct net_device_ops esd_usb2_netdev_ops = {
886 .ndo_open = esd_usb2_open,
887 .ndo_stop = esd_usb2_close,
888 .ndo_start_xmit = esd_usb2_start_xmit,
889 .ndo_change_mtu = can_change_mtu,
892 static const struct can_bittiming_const esd_usb2_bittiming_const = {
893 .name = "esd_usb2",
894 .tseg1_min = ESD_USB2_TSEG1_MIN,
895 .tseg1_max = ESD_USB2_TSEG1_MAX,
896 .tseg2_min = ESD_USB2_TSEG2_MIN,
897 .tseg2_max = ESD_USB2_TSEG2_MAX,
898 .sjw_max = ESD_USB2_SJW_MAX,
899 .brp_min = ESD_USB2_BRP_MIN,
900 .brp_max = ESD_USB2_BRP_MAX,
901 .brp_inc = ESD_USB2_BRP_INC,
904 static int esd_usb2_set_bittiming(struct net_device *netdev)
906 struct esd_usb2_net_priv *priv = netdev_priv(netdev);
907 struct can_bittiming *bt = &priv->can.bittiming;
908 struct esd_usb2_msg *msg;
909 int err;
910 u32 canbtr;
911 int sjw_shift;
913 canbtr = ESD_USB2_UBR;
914 if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
915 canbtr |= ESD_USB2_LOM;
917 canbtr |= (bt->brp - 1) & (ESD_USB2_BRP_MAX - 1);
919 if (le16_to_cpu(priv->usb2->udev->descriptor.idProduct) ==
920 USB_CANUSBM_PRODUCT_ID)
921 sjw_shift = ESD_USBM_SJW_SHIFT;
922 else
923 sjw_shift = ESD_USB2_SJW_SHIFT;
925 canbtr |= ((bt->sjw - 1) & (ESD_USB2_SJW_MAX - 1))
926 << sjw_shift;
927 canbtr |= ((bt->prop_seg + bt->phase_seg1 - 1)
928 & (ESD_USB2_TSEG1_MAX - 1))
929 << ESD_USB2_TSEG1_SHIFT;
930 canbtr |= ((bt->phase_seg2 - 1) & (ESD_USB2_TSEG2_MAX - 1))
931 << ESD_USB2_TSEG2_SHIFT;
932 if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
933 canbtr |= ESD_USB2_3_SAMPLES;
935 msg = kmalloc(sizeof(*msg), GFP_KERNEL);
936 if (!msg)
937 return -ENOMEM;
939 msg->msg.hdr.len = 2;
940 msg->msg.hdr.cmd = CMD_SETBAUD;
941 msg->msg.setbaud.net = priv->index;
942 msg->msg.setbaud.rsvd = 0;
943 msg->msg.setbaud.baud = cpu_to_le32(canbtr);
945 netdev_info(netdev, "setting BTR=%#x\n", canbtr);
947 err = esd_usb2_send_msg(priv->usb2, msg);
949 kfree(msg);
950 return err;
953 static int esd_usb2_get_berr_counter(const struct net_device *netdev,
954 struct can_berr_counter *bec)
956 struct esd_usb2_net_priv *priv = netdev_priv(netdev);
958 bec->txerr = priv->bec.txerr;
959 bec->rxerr = priv->bec.rxerr;
961 return 0;
964 static int esd_usb2_set_mode(struct net_device *netdev, enum can_mode mode)
966 switch (mode) {
967 case CAN_MODE_START:
968 netif_wake_queue(netdev);
969 break;
971 default:
972 return -EOPNOTSUPP;
975 return 0;
978 static int esd_usb2_probe_one_net(struct usb_interface *intf, int index)
980 struct esd_usb2 *dev = usb_get_intfdata(intf);
981 struct net_device *netdev;
982 struct esd_usb2_net_priv *priv;
983 int err = 0;
984 int i;
986 netdev = alloc_candev(sizeof(*priv), MAX_TX_URBS);
987 if (!netdev) {
988 dev_err(&intf->dev, "couldn't alloc candev\n");
989 err = -ENOMEM;
990 goto done;
993 priv = netdev_priv(netdev);
995 init_usb_anchor(&priv->tx_submitted);
996 atomic_set(&priv->active_tx_jobs, 0);
998 for (i = 0; i < MAX_TX_URBS; i++)
999 priv->tx_contexts[i].echo_index = MAX_TX_URBS;
1001 priv->usb2 = dev;
1002 priv->netdev = netdev;
1003 priv->index = index;
1005 priv->can.state = CAN_STATE_STOPPED;
1006 priv->can.ctrlmode_supported = CAN_CTRLMODE_LISTENONLY;
1008 if (le16_to_cpu(dev->udev->descriptor.idProduct) ==
1009 USB_CANUSBM_PRODUCT_ID)
1010 priv->can.clock.freq = ESD_USBM_CAN_CLOCK;
1011 else {
1012 priv->can.clock.freq = ESD_USB2_CAN_CLOCK;
1013 priv->can.ctrlmode_supported |= CAN_CTRLMODE_3_SAMPLES;
1016 priv->can.bittiming_const = &esd_usb2_bittiming_const;
1017 priv->can.do_set_bittiming = esd_usb2_set_bittiming;
1018 priv->can.do_set_mode = esd_usb2_set_mode;
1019 priv->can.do_get_berr_counter = esd_usb2_get_berr_counter;
1021 netdev->flags |= IFF_ECHO; /* we support local echo */
1023 netdev->netdev_ops = &esd_usb2_netdev_ops;
1025 SET_NETDEV_DEV(netdev, &intf->dev);
1026 netdev->dev_id = index;
1028 err = register_candev(netdev);
1029 if (err) {
1030 dev_err(&intf->dev, "couldn't register CAN device: %d\n", err);
1031 free_candev(netdev);
1032 err = -ENOMEM;
1033 goto done;
1036 dev->nets[index] = priv;
1037 netdev_info(netdev, "device %s registered\n", netdev->name);
1039 done:
1040 return err;
1044 * probe function for new USB2 devices
1046 * check version information and number of available
1047 * CAN interfaces
1049 static int esd_usb2_probe(struct usb_interface *intf,
1050 const struct usb_device_id *id)
1052 struct esd_usb2 *dev;
1053 struct esd_usb2_msg *msg;
1054 int i, err;
1056 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1057 if (!dev) {
1058 err = -ENOMEM;
1059 goto done;
1062 dev->udev = interface_to_usbdev(intf);
1064 init_usb_anchor(&dev->rx_submitted);
1066 usb_set_intfdata(intf, dev);
1068 msg = kmalloc(sizeof(*msg), GFP_KERNEL);
1069 if (!msg) {
1070 err = -ENOMEM;
1071 goto free_msg;
1074 /* query number of CAN interfaces (nets) */
1075 msg->msg.hdr.cmd = CMD_VERSION;
1076 msg->msg.hdr.len = 2;
1077 msg->msg.version.rsvd = 0;
1078 msg->msg.version.flags = 0;
1079 msg->msg.version.drv_version = 0;
1081 err = esd_usb2_send_msg(dev, msg);
1082 if (err < 0) {
1083 dev_err(&intf->dev, "sending version message failed\n");
1084 goto free_msg;
1087 err = esd_usb2_wait_msg(dev, msg);
1088 if (err < 0) {
1089 dev_err(&intf->dev, "no version message answer\n");
1090 goto free_msg;
1093 dev->net_count = (int)msg->msg.version_reply.nets;
1094 dev->version = le32_to_cpu(msg->msg.version_reply.version);
1096 if (device_create_file(&intf->dev, &dev_attr_firmware))
1097 dev_err(&intf->dev,
1098 "Couldn't create device file for firmware\n");
1100 if (device_create_file(&intf->dev, &dev_attr_hardware))
1101 dev_err(&intf->dev,
1102 "Couldn't create device file for hardware\n");
1104 if (device_create_file(&intf->dev, &dev_attr_nets))
1105 dev_err(&intf->dev,
1106 "Couldn't create device file for nets\n");
1108 /* do per device probing */
1109 for (i = 0; i < dev->net_count; i++)
1110 esd_usb2_probe_one_net(intf, i);
1112 free_msg:
1113 kfree(msg);
1114 if (err)
1115 kfree(dev);
1116 done:
1117 return err;
1121 * called by the usb core when the device is removed from the system
1123 static void esd_usb2_disconnect(struct usb_interface *intf)
1125 struct esd_usb2 *dev = usb_get_intfdata(intf);
1126 struct net_device *netdev;
1127 int i;
1129 device_remove_file(&intf->dev, &dev_attr_firmware);
1130 device_remove_file(&intf->dev, &dev_attr_hardware);
1131 device_remove_file(&intf->dev, &dev_attr_nets);
1133 usb_set_intfdata(intf, NULL);
1135 if (dev) {
1136 for (i = 0; i < dev->net_count; i++) {
1137 if (dev->nets[i]) {
1138 netdev = dev->nets[i]->netdev;
1139 unregister_netdev(netdev);
1140 free_candev(netdev);
1143 unlink_all_urbs(dev);
1144 kfree(dev);
1148 /* usb specific object needed to register this driver with the usb subsystem */
1149 static 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,
1156 module_usb_driver(esd_usb2_driver);