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