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