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