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