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