]> rtime.felk.cvut.cz Git - socketcan-devel.git/blob - kernel/2.6/drivers/net/can/usb/esd_usb2.c
Fix common misspellings
[socketcan-devel.git] / kernel / 2.6 / 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 <socketcan/can.h>
27 #include <socketcan/can/dev.h>
28 #include <socketcan/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[65];
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 };
222
223 static void esd_usb2_rx_event(struct esd_usb2_net_priv *priv,
224                               struct esd_usb2_msg *msg)
225 {
226         struct net_device_stats *stats = &priv->netdev->stats;
227         struct can_frame *cf;
228         struct sk_buff *skb;
229         u32 id = le32_to_cpu(msg->msg.rx.id) & ESD_IDMASK;
230
231         if (id == ESD_EV_CAN_ERROR_EXT) {
232                 u8 state = msg->msg.rx.data[0];
233                 u8 ecc = msg->msg.rx.data[1];
234                 u8 txerr = msg->msg.rx.data[2];
235                 u8 rxerr = msg->msg.rx.data[3];
236
237                 skb = alloc_can_err_skb(priv->netdev, &cf);
238                 if (skb == NULL) {
239                         stats->rx_dropped++;
240                         return;
241                 }
242
243                 if (state != priv->old_state) {
244                         priv->old_state = state;
245
246                         switch (state & ESD_BUSSTATE_MASK) {
247                         case ESD_BUSSTATE_BUSOFF:
248                                 priv->can.state = CAN_STATE_BUS_OFF;
249                                 cf->can_id |= CAN_ERR_BUSOFF;
250                                 can_bus_off(priv->netdev);
251                                 break;
252                         case ESD_BUSSTATE_WARN:
253                                 priv->can.state = CAN_STATE_ERROR_WARNING;
254                                 priv->can.can_stats.error_warning++;
255                                 break;
256                         case ESD_BUSSTATE_ERRPASSIVE:
257                                 priv->can.state = CAN_STATE_ERROR_PASSIVE;
258                                 priv->can.can_stats.error_passive++;
259                                 break;
260                         default:
261                                 priv->can.state = CAN_STATE_ERROR_ACTIVE;
262                                 break;
263                         }
264                 } else {
265                         priv->can.can_stats.bus_error++;
266                         stats->rx_errors++;
267
268                         cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
269
270                         switch (ecc & SJA1000_ECC_MASK) {
271                         case SJA1000_ECC_BIT:
272                                 cf->data[2] |= CAN_ERR_PROT_BIT;
273                                 break;
274                         case SJA1000_ECC_FORM:
275                                 cf->data[2] |= CAN_ERR_PROT_FORM;
276                                 break;
277                         case SJA1000_ECC_STUFF:
278                                 cf->data[2] |= CAN_ERR_PROT_STUFF;
279                                 break;
280                         default:
281                                 cf->data[2] |= CAN_ERR_PROT_UNSPEC;
282                                 cf->data[3] = ecc & SJA1000_ECC_SEG;
283                                 break;
284                         }
285
286                         /* Error occurred during transmission? */
287                         if (!(ecc & SJA1000_ECC_DIR))
288                                 cf->data[2] |= CAN_ERR_PROT_TX;
289
290                         if (priv->can.state == CAN_STATE_ERROR_WARNING ||
291                             priv->can.state == CAN_STATE_ERROR_PASSIVE) {
292                                 cf->data[1] = (txerr > rxerr) ?
293                                         CAN_ERR_CRTL_TX_PASSIVE :
294                                         CAN_ERR_CRTL_RX_PASSIVE;
295                         }
296                 }
297
298                 netif_rx(skb);
299
300 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,32)
301                 priv->netdev->last_rx = jiffies;
302 #endif
303                 stats->rx_packets++;
304                 stats->rx_bytes += cf->can_dlc;
305         }
306 }
307
308 static void esd_usb2_rx_can_msg(struct esd_usb2_net_priv *priv,
309                                 struct esd_usb2_msg *msg)
310 {
311         struct net_device_stats *stats = &priv->netdev->stats;
312         struct can_frame *cf;
313         struct sk_buff *skb;
314         int i;
315         u32 id;
316
317         if (!netif_device_present(priv->netdev))
318                 return;
319
320         id = le32_to_cpu(msg->msg.rx.id);
321
322         if (id & ESD_EVENT) {
323                 esd_usb2_rx_event(priv, msg);
324         } else {
325                 skb = alloc_can_skb(priv->netdev, &cf);
326                 if (skb == NULL) {
327                         stats->rx_dropped++;
328                         return;
329                 }
330
331                 cf->can_id = id & ESD_IDMASK;
332                 cf->can_dlc = get_can_dlc(msg->msg.rx.dlc);
333
334                 if (id & ESD_EXTID)
335                         cf->can_id |= CAN_EFF_FLAG;
336
337                 if (msg->msg.rx.dlc & ESD_RTR) {
338                         cf->can_id |= CAN_RTR_FLAG;
339                 } else {
340                         for (i = 0; i < cf->can_dlc; i++)
341                                 cf->data[i] = msg->msg.rx.data[i];
342                 }
343
344                 netif_rx(skb);
345
346 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,32)
347                 priv->netdev->last_rx = jiffies;
348 #endif
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 -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                         esd_usb2_rx_can_msg(dev->nets[msg->msg.rx.net], msg);
413                         break;
414
415                 case CMD_CAN_TX:
416                         esd_usb2_tx_done_msg(dev->nets[msg->msg.txdone.net],
417                                              msg);
418                         break;
419                 }
420
421                 pos += msg->msg.hdr.len << 2;
422
423                 if (pos > urb->actual_length) {
424                         dev_err(dev->udev->dev.parent, "format error\n");
425                         break;
426                 }
427         }
428
429 resubmit_urb:
430         usb_fill_bulk_urb(urb, dev->udev, usb_rcvbulkpipe(dev->udev, 1),
431                           urb->transfer_buffer, RX_BUFFER_SIZE,
432                           esd_usb2_read_bulk_callback, dev);
433
434         retval = usb_submit_urb(urb, GFP_ATOMIC);
435         if (retval == -ENODEV) {
436                 for (i = 0; i < dev->net_count; i++) {
437                         if (dev->nets[i])
438                                 netif_device_detach(dev->nets[i]->netdev);
439                 }
440         } else if (retval) {
441                 dev_err(dev->udev->dev.parent,
442                         "failed resubmitting read bulk urb: %d\n", retval);
443         }
444
445         return;
446 }
447
448 /*
449  * callback for bulk IN urb
450  */
451 static void esd_usb2_write_bulk_callback(struct urb *urb)
452 {
453         struct esd_tx_urb_context *context = urb->context;
454         struct esd_usb2_net_priv *priv;
455         struct esd_usb2 *dev;
456         struct net_device *netdev;
457         size_t size = sizeof(struct esd_usb2_msg);
458
459         BUG_ON(!context);
460
461         priv = context->priv;
462         netdev = priv->netdev;
463         dev = priv->usb2;
464
465         /* free up our allocated buffer */
466         usb_buffer_free(urb->dev, size,
467                         urb->transfer_buffer, urb->transfer_dma);
468
469         if (!netif_device_present(netdev))
470                 return;
471
472         if (urb->status)
473                 dev_info(ND2D(netdev), "Tx URB aborted (%d)\n",
474                          urb->status);
475
476         netdev->trans_start = jiffies;
477 }
478
479 #ifdef CONFIG_SYSFS
480 static ssize_t show_firmware(struct device *d,
481                              struct device_attribute *attr, char *buf)
482 {
483         struct usb_interface *intf = to_usb_interface(d);
484         struct esd_usb2 *dev = usb_get_intfdata(intf);
485
486         return sprintf(buf, "%d.%d.%d\n",
487                        (dev->version >> 12) & 0xf,
488                        (dev->version >> 8) & 0xf,
489                        dev->version & 0xff);
490 }
491 static DEVICE_ATTR(firmware, S_IRUGO, show_firmware, NULL);
492
493 static ssize_t show_hardware(struct device *d,
494                              struct device_attribute *attr, char *buf)
495 {
496         struct usb_interface *intf = to_usb_interface(d);
497         struct esd_usb2 *dev = usb_get_intfdata(intf);
498
499         return sprintf(buf, "%d.%d.%d\n",
500                        (dev->version >> 28) & 0xf,
501                        (dev->version >> 24) & 0xf,
502                        (dev->version >> 16) & 0xff);
503 }
504 static DEVICE_ATTR(hardware, S_IRUGO, show_hardware, NULL);
505
506 static ssize_t show_nets(struct device *d,
507                          struct device_attribute *attr, char *buf)
508 {
509         struct usb_interface *intf = to_usb_interface(d);
510         struct esd_usb2 *dev = usb_get_intfdata(intf);
511
512         return sprintf(buf, "%d", dev->net_count);
513 }
514 static DEVICE_ATTR(nets, S_IRUGO, show_nets, NULL);
515 #endif
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_buffer_alloc(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_buffer_free(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(ND2D(netdev), "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;
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 (i = 0; i < MAX_TX_URBS; i++)
674                                 priv->tx_contexts[i].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(ND2D(netdev), "couldn't start device: %d\n", err);
693
694                 close_candev(netdev);
695
696                 return err;
697         }
698
699         priv->open_time = jiffies;
700
701         netif_start_queue(netdev);
702
703         return 0;
704 }
705
706 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,32)
707 static int esd_usb2_start_xmit(struct sk_buff *skb, struct net_device *netdev)
708 #else
709 static netdev_tx_t esd_usb2_start_xmit(struct sk_buff *skb,
710                                       struct net_device *netdev)
711 #endif
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         /* create a URB, and a buffer for it, and copy the data to the URB */
726         urb = usb_alloc_urb(0, GFP_ATOMIC);
727         if (!urb) {
728                 dev_err(ND2D(netdev), "No memory left for URBs\n");
729                 stats->tx_dropped++;
730                 kfree_skb(skb);
731                 goto nourbmem;
732         }
733
734         buf = usb_buffer_alloc(dev->udev, size, GFP_ATOMIC, &urb->transfer_dma);
735         if (!buf) {
736                 dev_err(ND2D(netdev), "No memory left for USB buffer\n");
737                 stats->tx_dropped++;
738                 kfree_skb(skb);
739                 goto nobufmem;
740         }
741
742         msg = (struct esd_usb2_msg *)buf;
743
744         msg->msg.hdr.len = 3; /* minimal length */
745         msg->msg.hdr.cmd = CMD_CAN_TX;
746         msg->msg.tx.net = priv->index;
747         msg->msg.tx.dlc = cf->can_dlc;
748         msg->msg.tx.id = cpu_to_le32(cf->can_id & CAN_ERR_MASK);
749
750         if (cf->can_id & CAN_RTR_FLAG)
751                 msg->msg.tx.dlc |= ESD_RTR;
752
753         if (cf->can_id & CAN_EFF_FLAG)
754                 msg->msg.tx.id |= cpu_to_le32(ESD_EXTID);
755
756         for (i = 0; i < cf->can_dlc; i++)
757                 msg->msg.tx.data[i] = cf->data[i];
758
759         msg->msg.hdr.len += (cf->can_dlc + 3) >> 2;
760
761         for (i = 0; i < MAX_TX_URBS; i++) {
762                 if (priv->tx_contexts[i].echo_index == MAX_TX_URBS) {
763                         context = &priv->tx_contexts[i];
764                         break;
765                 }
766         }
767
768         /*
769          * This may never happen.
770          */
771         if (!context) {
772                 dev_warn(ND2D(netdev), "couldn't find free context\n");
773                 ret = NETDEV_TX_BUSY;
774                 goto releasebuf;
775         }
776
777         context->priv = priv;
778         context->echo_index = i;
779         context->dlc = cf->can_dlc;
780
781         /* hnd must not be 0 */
782         msg->msg.tx.hnd = 0x80000000 | i; /* returned in TX done message */
783
784         usb_fill_bulk_urb(urb, dev->udev, usb_sndbulkpipe(dev->udev, 2), buf,
785                           msg->msg.hdr.len << 2,
786                           esd_usb2_write_bulk_callback, context);
787
788         urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
789
790         usb_anchor_urb(urb, &priv->tx_submitted);
791
792         can_put_echo_skb(skb, netdev, context->echo_index);
793
794         atomic_inc(&priv->active_tx_jobs);
795
796         err = usb_submit_urb(urb, GFP_ATOMIC);
797         if (err) {
798                 can_free_echo_skb(netdev, context->echo_index);
799
800                 atomic_dec(&priv->active_tx_jobs);
801                 usb_unanchor_urb(urb);
802
803                 stats->tx_dropped++;
804
805                 if (err == -ENODEV)
806                         netif_device_detach(netdev);
807                 else
808                         dev_warn(ND2D(netdev), "failed tx_urb %d\n", err);
809
810                 goto releasebuf;
811         }
812
813         netdev->trans_start = jiffies;
814
815         /* Slow down tx path */
816         if (atomic_read(&priv->active_tx_jobs) >= MAX_TX_URBS)
817                 netif_stop_queue(netdev);
818
819         /*
820          * Release our reference to this URB, the USB core will eventually free
821          * it entirely.
822          */
823         usb_free_urb(urb);
824
825         return NETDEV_TX_OK;
826
827 releasebuf:
828         usb_buffer_free(dev->udev, size, buf, urb->transfer_dma);
829
830 nobufmem:
831         usb_free_urb(urb);
832
833 nourbmem:
834         return ret;
835 }
836
837 static int esd_usb2_close(struct net_device *netdev)
838 {
839         struct esd_usb2_net_priv *priv = netdev_priv(netdev);
840         struct esd_usb2_msg msg;
841         int i;
842
843         /* Disable all IDs (see esd_usb2_start()) */
844         msg.msg.hdr.cmd = CMD_IDADD;
845         msg.msg.hdr.len = 2 + ESD_MAX_ID_SEGMENT;
846         msg.msg.filter.net = priv->index;
847         msg.msg.filter.option = ESD_ID_ENABLE; /* start with segment 0 */
848         for (i = 0; i <= ESD_MAX_ID_SEGMENT; i++)
849                 msg.msg.filter.mask[i] = 0;
850         esd_usb2_send_msg(priv->usb2, &msg);
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         esd_usb2_send_msg(priv->usb2, &msg);
859
860         priv->can.state = CAN_STATE_STOPPED;
861
862         netif_stop_queue(netdev);
863
864         close_candev(netdev);
865
866         priv->open_time = 0;
867
868         return 0;
869 }
870
871 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,28)
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 #endif
878
879 static struct can_bittiming_const esd_usb2_bittiming_const = {
880         .name = "esd_usb2",
881         .tseg1_min = ESD_USB2_TSEG1_MIN,
882         .tseg1_max = ESD_USB2_TSEG1_MAX,
883         .tseg2_min = ESD_USB2_TSEG2_MIN,
884         .tseg2_max = ESD_USB2_TSEG2_MAX,
885         .sjw_max = ESD_USB2_SJW_MAX,
886         .brp_min = ESD_USB2_BRP_MIN,
887         .brp_max = ESD_USB2_BRP_MAX,
888         .brp_inc = ESD_USB2_BRP_INC,
889 };
890
891 static int esd_usb2_set_bittiming(struct net_device *netdev)
892 {
893         struct esd_usb2_net_priv *priv = netdev_priv(netdev);
894         struct can_bittiming *bt = &priv->can.bittiming;
895         struct esd_usb2_msg msg;
896         u32 canbtr;
897
898         canbtr = ESD_USB2_UBR;
899         canbtr |= (bt->brp - 1) & (ESD_USB2_BRP_MAX - 1);
900         canbtr |= ((bt->sjw - 1) & (ESD_USB2_SJW_MAX - 1))
901                 << ESD_USB2_SJW_SHIFT;
902         canbtr |= ((bt->prop_seg + bt->phase_seg1 - 1)
903                    & (ESD_USB2_TSEG1_MAX - 1))
904                 << ESD_USB2_TSEG1_SHIFT;
905         canbtr |= ((bt->phase_seg2 - 1) & (ESD_USB2_TSEG2_MAX - 1))
906                 << ESD_USB2_TSEG2_SHIFT;
907         if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
908                 canbtr |= ESD_USB2_3_SAMPLES;
909
910         msg.msg.hdr.len = 2;
911         msg.msg.hdr.cmd = CMD_SETBAUD;
912         msg.msg.setbaud.net = priv->index;
913         msg.msg.setbaud.rsvd = 0;
914         msg.msg.setbaud.baud = cpu_to_le32(canbtr);
915
916         dev_info(ND2D(netdev), "setting BTR=%#x\n", canbtr);
917
918         return esd_usb2_send_msg(priv->usb2, &msg);
919 }
920
921 static int esd_usb2_set_mode(struct net_device *netdev, enum can_mode mode)
922 {
923         struct esd_usb2_net_priv *priv = netdev_priv(netdev);
924
925         if (!priv->open_time)
926                 return -EINVAL;
927
928         switch (mode) {
929         case CAN_MODE_START:
930                 netif_wake_queue(netdev);
931                 break;
932
933         default:
934                 return -EOPNOTSUPP;
935         }
936
937         return 0;
938 }
939
940 static int esd_usb2_probe_one_net(struct usb_interface *intf, int index)
941 {
942         struct esd_usb2 *dev = usb_get_intfdata(intf);
943         struct net_device *netdev;
944         struct esd_usb2_net_priv *priv;
945         int err;
946         int i;
947
948         netdev = alloc_candev(sizeof(*priv), MAX_TX_URBS);
949         if (!netdev) {
950                 dev_err(&intf->dev, "couldn't alloc candev\n");
951                 return -ENOMEM;
952         }
953
954         priv = netdev_priv(netdev);
955
956         init_usb_anchor(&priv->tx_submitted);
957         atomic_set(&priv->active_tx_jobs, 0);
958
959         for (i = 0; i < MAX_TX_URBS; i++)
960                 priv->tx_contexts[i].echo_index = MAX_TX_URBS;
961
962         priv->usb2 = dev;
963         priv->netdev = netdev;
964         priv->index = index;
965
966         priv->can.state = CAN_STATE_STOPPED;
967         priv->can.clock.freq = ESD_USB2_CAN_CLOCK;
968         priv->can.bittiming_const = &esd_usb2_bittiming_const;
969         priv->can.do_set_bittiming = esd_usb2_set_bittiming;
970         priv->can.do_set_mode = esd_usb2_set_mode;
971
972         netdev->flags |= IFF_ECHO; /* we support local echo */
973
974 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,28)
975         netdev->netdev_ops = &esd_usb2_netdev_ops;
976 #else
977         netdev->open = esd_usb2_open;
978         netdev->stop = esd_usb2_close;
979         netdev->hard_start_xmit = esd_usb2_start_xmit;
980 #endif
981
982         SET_NETDEV_DEV(netdev, &intf->dev);
983
984         err = register_candev(netdev);
985         if (err) {
986                 dev_err(&intf->dev,
987                         "couldn't register CAN device: %d\n", err);
988                 free_candev(netdev);
989                 return -ENOMEM;
990         }
991
992         dev->nets[index] = priv;
993         dev_info(ND2D(netdev), "device %s registered\n", netdev->name);
994         return 0;
995 }
996
997 /*
998  * probe function for new USB2 devices
999  *
1000  * check version information and number of available
1001  * CAN interfaces
1002  */
1003 static int esd_usb2_probe(struct usb_interface *intf,
1004                          const struct usb_device_id *id)
1005 {
1006         struct esd_usb2 *dev;
1007         struct esd_usb2_msg msg;
1008         int i, err = -ENOMEM;
1009
1010         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1011         if (!dev)
1012                 return -ENOMEM;
1013
1014         dev->udev = interface_to_usbdev(intf);
1015
1016         init_usb_anchor(&dev->rx_submitted);
1017
1018         usb_set_intfdata(intf, dev);
1019
1020         /* query number of CAN interfaces (nets) */
1021         msg.msg.hdr.cmd = CMD_VERSION;
1022         msg.msg.hdr.len = 2;
1023         msg.msg.version.rsvd = 0;
1024         msg.msg.version.flags = 0;
1025         msg.msg.version.drv_version = 0;
1026
1027         if (esd_usb2_send_msg(dev, &msg) < 0) {
1028                 dev_err(&intf->dev, "sending version message failed\n");
1029                 goto free_dev;
1030         }
1031
1032         if (esd_usb2_wait_msg(dev, &msg) < 0) {
1033                 dev_err(&intf->dev, "no version message answer\n");
1034                 goto free_dev;
1035         }
1036
1037         dev->net_count = (int)msg.msg.version_reply.nets;
1038         dev->version = le32_to_cpu(msg.msg.version_reply.version);
1039
1040 #ifdef CONFIG_SYSFS
1041         if (device_create_file(&intf->dev, &dev_attr_firmware))
1042                 dev_err(&intf->dev,
1043                         "Couldn't create device file for firmware\n");
1044
1045         if (device_create_file(&intf->dev, &dev_attr_hardware))
1046                 dev_err(&intf->dev,
1047                         "Couldn't create device file for hardware\n");
1048
1049         if (device_create_file(&intf->dev, &dev_attr_nets))
1050                 dev_err(&intf->dev,
1051                         "Couldn't create device file for nets\n");
1052 #endif
1053
1054         /* do per device probing */
1055         for (i = 0; i < dev->net_count; i++)
1056                 esd_usb2_probe_one_net(intf, i);
1057
1058         return 0;
1059
1060 free_dev:
1061         kfree(dev);
1062         return err;
1063 }
1064
1065 /*
1066  * called by the usb core when the device is removed from the system
1067  */
1068 static void esd_usb2_disconnect(struct usb_interface *intf)
1069 {
1070         struct esd_usb2 *dev = usb_get_intfdata(intf);
1071         struct net_device *netdev;
1072         int i;
1073
1074 #ifdef CONFIG_SYSFS
1075         device_remove_file(&intf->dev, &dev_attr_firmware);
1076         device_remove_file(&intf->dev, &dev_attr_hardware);
1077         device_remove_file(&intf->dev, &dev_attr_nets);
1078 #endif
1079         usb_set_intfdata(intf, NULL);
1080
1081         if (dev) {
1082                 for (i = 0; i < dev->net_count; i++) {
1083                         if (dev->nets[i]) {
1084                                 netdev = dev->nets[i]->netdev;
1085                                 unregister_netdev(netdev);
1086                                 free_candev(netdev);
1087                         }
1088                 }
1089                 unlink_all_urbs(dev);
1090         }
1091 }
1092
1093 /* usb specific object needed to register this driver with the usb subsystem */
1094 static struct usb_driver esd_usb2_driver = {
1095         .name = "esd_usb2",
1096         .probe = esd_usb2_probe,
1097         .disconnect = esd_usb2_disconnect,
1098         .id_table = esd_usb2_table,
1099 };
1100
1101 static int __init esd_usb2_init(void)
1102 {
1103         int err;
1104
1105         /* register this driver with the USB subsystem */
1106         err = usb_register(&esd_usb2_driver);
1107
1108         if (err) {
1109                 err("usb_register failed. Error number %d\n", err);
1110                 return err;
1111         }
1112
1113         return 0;
1114 }
1115 module_init(esd_usb2_init);
1116
1117 static void __exit esd_usb2_exit(void)
1118 {
1119         /* deregister this driver with the USB subsystem */
1120         usb_deregister(&esd_usb2_driver);
1121 }
1122 module_exit(esd_usb2_exit);