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