Merge tag 'stable/for-linus-3.8-rc6-tag' of git://git.kernel.org/pub/scm/linux/kernel...
[GitHub/mt8127/android_kernel_alcatel_ttab.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/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 and CAN-USB/Micro 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 #define USB_CANUSBM_PRODUCT_ID 0x0011
38
39 #define ESD_USB2_CAN_CLOCK 60000000
40 #define ESD_USBM_CAN_CLOCK 36000000
41 #define ESD_USB2_MAX_NETS 2
42
43 /* USB2 commands */
44 #define CMD_VERSION 1 /* also used for VERSION_REPLY */
45 #define CMD_CAN_RX 2 /* device to host only */
46 #define CMD_CAN_TX 3 /* also used for TX_DONE */
47 #define CMD_SETBAUD 4 /* also used for SETBAUD_REPLY */
48 #define CMD_TS 5 /* also used for TS_REPLY */
49 #define CMD_IDADD 6 /* also used for IDADD_REPLY */
50
51 /* esd CAN message flags - dlc field */
52 #define ESD_RTR 0x10
53
54 /* esd CAN message flags - id field */
55 #define ESD_EXTID 0x20000000
56 #define ESD_EVENT 0x40000000
57 #define ESD_IDMASK 0x1fffffff
58
59 /* esd CAN event ids used by this driver */
60 #define ESD_EV_CAN_ERROR_EXT 2
61
62 /* baudrate message flags */
63 #define ESD_USB2_UBR 0x80000000
64 #define ESD_USB2_LOM 0x40000000
65 #define ESD_USB2_NO_BAUDRATE 0x7fffffff
66 #define ESD_USB2_TSEG1_MIN 1
67 #define ESD_USB2_TSEG1_MAX 16
68 #define ESD_USB2_TSEG1_SHIFT 16
69 #define ESD_USB2_TSEG2_MIN 1
70 #define ESD_USB2_TSEG2_MAX 8
71 #define ESD_USB2_TSEG2_SHIFT 20
72 #define ESD_USB2_SJW_MAX 4
73 #define ESD_USB2_SJW_SHIFT 14
74 #define ESD_USBM_SJW_SHIFT 24
75 #define ESD_USB2_BRP_MIN 1
76 #define ESD_USB2_BRP_MAX 1024
77 #define ESD_USB2_BRP_INC 1
78 #define ESD_USB2_3_SAMPLES 0x00800000
79
80 /* esd IDADD message */
81 #define ESD_ID_ENABLE 0x80
82 #define ESD_MAX_ID_SEGMENT 64
83
84 /* SJA1000 ECC register (emulated by usb2 firmware) */
85 #define SJA1000_ECC_SEG 0x1F
86 #define SJA1000_ECC_DIR 0x20
87 #define SJA1000_ECC_ERR 0x06
88 #define SJA1000_ECC_BIT 0x00
89 #define SJA1000_ECC_FORM 0x40
90 #define SJA1000_ECC_STUFF 0x80
91 #define SJA1000_ECC_MASK 0xc0
92
93 /* esd bus state event codes */
94 #define ESD_BUSSTATE_MASK 0xc0
95 #define ESD_BUSSTATE_WARN 0x40
96 #define ESD_BUSSTATE_ERRPASSIVE 0x80
97 #define ESD_BUSSTATE_BUSOFF 0xc0
98
99 #define RX_BUFFER_SIZE 1024
100 #define MAX_RX_URBS 4
101 #define MAX_TX_URBS 16 /* must be power of 2 */
102
103 struct header_msg {
104 u8 len; /* len is always the total message length in 32bit words */
105 u8 cmd;
106 u8 rsvd[2];
107 };
108
109 struct version_msg {
110 u8 len;
111 u8 cmd;
112 u8 rsvd;
113 u8 flags;
114 __le32 drv_version;
115 };
116
117 struct version_reply_msg {
118 u8 len;
119 u8 cmd;
120 u8 nets;
121 u8 features;
122 __le32 version;
123 u8 name[16];
124 __le32 rsvd;
125 __le32 ts;
126 };
127
128 struct rx_msg {
129 u8 len;
130 u8 cmd;
131 u8 net;
132 u8 dlc;
133 __le32 ts;
134 __le32 id; /* upper 3 bits contain flags */
135 u8 data[8];
136 };
137
138 struct tx_msg {
139 u8 len;
140 u8 cmd;
141 u8 net;
142 u8 dlc;
143 __le32 hnd;
144 __le32 id; /* upper 3 bits contain flags */
145 u8 data[8];
146 };
147
148 struct tx_done_msg {
149 u8 len;
150 u8 cmd;
151 u8 net;
152 u8 status;
153 __le32 hnd;
154 __le32 ts;
155 };
156
157 struct id_filter_msg {
158 u8 len;
159 u8 cmd;
160 u8 net;
161 u8 option;
162 __le32 mask[ESD_MAX_ID_SEGMENT + 1];
163 };
164
165 struct set_baudrate_msg {
166 u8 len;
167 u8 cmd;
168 u8 net;
169 u8 rsvd;
170 __le32 baud;
171 };
172
173 /* Main message type used between library and application */
174 struct __attribute__ ((packed)) esd_usb2_msg {
175 union {
176 struct header_msg hdr;
177 struct version_msg version;
178 struct version_reply_msg version_reply;
179 struct rx_msg rx;
180 struct tx_msg tx;
181 struct tx_done_msg txdone;
182 struct set_baudrate_msg setbaud;
183 struct id_filter_msg filter;
184 } msg;
185 };
186
187 static struct usb_device_id esd_usb2_table[] = {
188 {USB_DEVICE(USB_ESDGMBH_VENDOR_ID, USB_CANUSB2_PRODUCT_ID)},
189 {USB_DEVICE(USB_ESDGMBH_VENDOR_ID, USB_CANUSBM_PRODUCT_ID)},
190 {}
191 };
192 MODULE_DEVICE_TABLE(usb, esd_usb2_table);
193
194 struct esd_usb2_net_priv;
195
196 struct esd_tx_urb_context {
197 struct esd_usb2_net_priv *priv;
198 u32 echo_index;
199 int dlc;
200 };
201
202 struct esd_usb2 {
203 struct usb_device *udev;
204 struct esd_usb2_net_priv *nets[ESD_USB2_MAX_NETS];
205
206 struct usb_anchor rx_submitted;
207
208 int net_count;
209 u32 version;
210 int rxinitdone;
211 };
212
213 struct esd_usb2_net_priv {
214 struct can_priv can; /* must be the first member */
215
216 atomic_t active_tx_jobs;
217 struct usb_anchor tx_submitted;
218 struct esd_tx_urb_context tx_contexts[MAX_TX_URBS];
219
220 struct esd_usb2 *usb2;
221 struct net_device *netdev;
222 int index;
223 u8 old_state;
224 struct can_berr_counter bec;
225 };
226
227 static void esd_usb2_rx_event(struct esd_usb2_net_priv *priv,
228 struct esd_usb2_msg *msg)
229 {
230 struct net_device_stats *stats = &priv->netdev->stats;
231 struct can_frame *cf;
232 struct sk_buff *skb;
233 u32 id = le32_to_cpu(msg->msg.rx.id) & ESD_IDMASK;
234
235 if (id == ESD_EV_CAN_ERROR_EXT) {
236 u8 state = msg->msg.rx.data[0];
237 u8 ecc = msg->msg.rx.data[1];
238 u8 txerr = msg->msg.rx.data[2];
239 u8 rxerr = msg->msg.rx.data[3];
240
241 skb = alloc_can_err_skb(priv->netdev, &cf);
242 if (skb == NULL) {
243 stats->rx_dropped++;
244 return;
245 }
246
247 if (state != priv->old_state) {
248 priv->old_state = state;
249
250 switch (state & ESD_BUSSTATE_MASK) {
251 case ESD_BUSSTATE_BUSOFF:
252 priv->can.state = CAN_STATE_BUS_OFF;
253 cf->can_id |= CAN_ERR_BUSOFF;
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[2] |= CAN_ERR_PROT_UNSPEC;
286 cf->data[3] = ecc & SJA1000_ECC_SEG;
287 break;
288 }
289
290 /* Error occurred during transmission? */
291 if (!(ecc & SJA1000_ECC_DIR))
292 cf->data[2] |= CAN_ERR_PROT_TX;
293
294 if (priv->can.state == CAN_STATE_ERROR_WARNING ||
295 priv->can.state == CAN_STATE_ERROR_PASSIVE) {
296 cf->data[1] = (txerr > rxerr) ?
297 CAN_ERR_CRTL_TX_PASSIVE :
298 CAN_ERR_CRTL_RX_PASSIVE;
299 }
300 cf->data[6] = txerr;
301 cf->data[7] = rxerr;
302 }
303
304 netif_rx(skb);
305
306 priv->bec.txerr = txerr;
307 priv->bec.rxerr = rxerr;
308
309 stats->rx_packets++;
310 stats->rx_bytes += cf->can_dlc;
311 }
312 }
313
314 static void esd_usb2_rx_can_msg(struct esd_usb2_net_priv *priv,
315 struct esd_usb2_msg *msg)
316 {
317 struct net_device_stats *stats = &priv->netdev->stats;
318 struct can_frame *cf;
319 struct sk_buff *skb;
320 int i;
321 u32 id;
322
323 if (!netif_device_present(priv->netdev))
324 return;
325
326 id = le32_to_cpu(msg->msg.rx.id);
327
328 if (id & ESD_EVENT) {
329 esd_usb2_rx_event(priv, msg);
330 } else {
331 skb = alloc_can_skb(priv->netdev, &cf);
332 if (skb == NULL) {
333 stats->rx_dropped++;
334 return;
335 }
336
337 cf->can_id = id & ESD_IDMASK;
338 cf->can_dlc = get_can_dlc(msg->msg.rx.dlc);
339
340 if (id & ESD_EXTID)
341 cf->can_id |= CAN_EFF_FLAG;
342
343 if (msg->msg.rx.dlc & ESD_RTR) {
344 cf->can_id |= CAN_RTR_FLAG;
345 } else {
346 for (i = 0; i < cf->can_dlc; i++)
347 cf->data[i] = msg->msg.rx.data[i];
348 }
349
350 netif_rx(skb);
351
352 stats->rx_packets++;
353 stats->rx_bytes += cf->can_dlc;
354 }
355
356 return;
357 }
358
359 static void esd_usb2_tx_done_msg(struct esd_usb2_net_priv *priv,
360 struct esd_usb2_msg *msg)
361 {
362 struct net_device_stats *stats = &priv->netdev->stats;
363 struct net_device *netdev = priv->netdev;
364 struct esd_tx_urb_context *context;
365
366 if (!netif_device_present(netdev))
367 return;
368
369 context = &priv->tx_contexts[msg->msg.txdone.hnd & (MAX_TX_URBS - 1)];
370
371 if (!msg->msg.txdone.status) {
372 stats->tx_packets++;
373 stats->tx_bytes += context->dlc;
374 can_get_echo_skb(netdev, context->echo_index);
375 } else {
376 stats->tx_errors++;
377 can_free_echo_skb(netdev, context->echo_index);
378 }
379
380 /* Release context */
381 context->echo_index = MAX_TX_URBS;
382 atomic_dec(&priv->active_tx_jobs);
383
384 netif_wake_queue(netdev);
385 }
386
387 static void esd_usb2_read_bulk_callback(struct urb *urb)
388 {
389 struct esd_usb2 *dev = urb->context;
390 int retval;
391 int pos = 0;
392 int i;
393
394 switch (urb->status) {
395 case 0: /* success */
396 break;
397
398 case -ENOENT:
399 case -ESHUTDOWN:
400 return;
401
402 default:
403 dev_info(dev->udev->dev.parent,
404 "Rx URB aborted (%d)\n", urb->status);
405 goto resubmit_urb;
406 }
407
408 while (pos < urb->actual_length) {
409 struct esd_usb2_msg *msg;
410
411 msg = (struct esd_usb2_msg *)(urb->transfer_buffer + pos);
412
413 switch (msg->msg.hdr.cmd) {
414 case CMD_CAN_RX:
415 esd_usb2_rx_can_msg(dev->nets[msg->msg.rx.net], msg);
416 break;
417
418 case CMD_CAN_TX:
419 esd_usb2_tx_done_msg(dev->nets[msg->msg.txdone.net],
420 msg);
421 break;
422 }
423
424 pos += msg->msg.hdr.len << 2;
425
426 if (pos > urb->actual_length) {
427 dev_err(dev->udev->dev.parent, "format error\n");
428 break;
429 }
430 }
431
432 resubmit_urb:
433 usb_fill_bulk_urb(urb, dev->udev, usb_rcvbulkpipe(dev->udev, 1),
434 urb->transfer_buffer, RX_BUFFER_SIZE,
435 esd_usb2_read_bulk_callback, dev);
436
437 retval = usb_submit_urb(urb, GFP_ATOMIC);
438 if (retval == -ENODEV) {
439 for (i = 0; i < dev->net_count; i++) {
440 if (dev->nets[i])
441 netif_device_detach(dev->nets[i]->netdev);
442 }
443 } else if (retval) {
444 dev_err(dev->udev->dev.parent,
445 "failed resubmitting read bulk urb: %d\n", retval);
446 }
447
448 return;
449 }
450
451 /*
452 * callback for bulk IN urb
453 */
454 static void esd_usb2_write_bulk_callback(struct urb *urb)
455 {
456 struct esd_tx_urb_context *context = urb->context;
457 struct esd_usb2_net_priv *priv;
458 struct esd_usb2 *dev;
459 struct net_device *netdev;
460 size_t size = sizeof(struct esd_usb2_msg);
461
462 WARN_ON(!context);
463
464 priv = context->priv;
465 netdev = priv->netdev;
466 dev = priv->usb2;
467
468 /* free up our allocated buffer */
469 usb_free_coherent(urb->dev, size,
470 urb->transfer_buffer, urb->transfer_dma);
471
472 if (!netif_device_present(netdev))
473 return;
474
475 if (urb->status)
476 netdev_info(netdev, "Tx URB aborted (%d)\n", 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 netdev_err(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, 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 netdev_warn(netdev, "couldn't start device: %d\n", err);
693 close_candev(netdev);
694 return err;
695 }
696
697 netif_start_queue(netdev);
698
699 return 0;
700 }
701
702 static netdev_tx_t esd_usb2_start_xmit(struct sk_buff *skb,
703 struct net_device *netdev)
704 {
705 struct esd_usb2_net_priv *priv = netdev_priv(netdev);
706 struct esd_usb2 *dev = priv->usb2;
707 struct esd_tx_urb_context *context = NULL;
708 struct net_device_stats *stats = &netdev->stats;
709 struct can_frame *cf = (struct can_frame *)skb->data;
710 struct esd_usb2_msg *msg;
711 struct urb *urb;
712 u8 *buf;
713 int i, err;
714 int ret = NETDEV_TX_OK;
715 size_t size = sizeof(struct esd_usb2_msg);
716
717 if (can_dropped_invalid_skb(netdev, skb))
718 return NETDEV_TX_OK;
719
720 /* create a URB, and a buffer for it, and copy the data to the URB */
721 urb = usb_alloc_urb(0, GFP_ATOMIC);
722 if (!urb) {
723 netdev_err(netdev, "No memory left for URBs\n");
724 stats->tx_dropped++;
725 dev_kfree_skb(skb);
726 goto nourbmem;
727 }
728
729 buf = usb_alloc_coherent(dev->udev, size, GFP_ATOMIC,
730 &urb->transfer_dma);
731 if (!buf) {
732 netdev_err(netdev, "No memory left for USB buffer\n");
733 stats->tx_dropped++;
734 dev_kfree_skb(skb);
735 goto nobufmem;
736 }
737
738 msg = (struct esd_usb2_msg *)buf;
739
740 msg->msg.hdr.len = 3; /* minimal length */
741 msg->msg.hdr.cmd = CMD_CAN_TX;
742 msg->msg.tx.net = priv->index;
743 msg->msg.tx.dlc = cf->can_dlc;
744 msg->msg.tx.id = cpu_to_le32(cf->can_id & CAN_ERR_MASK);
745
746 if (cf->can_id & CAN_RTR_FLAG)
747 msg->msg.tx.dlc |= ESD_RTR;
748
749 if (cf->can_id & CAN_EFF_FLAG)
750 msg->msg.tx.id |= cpu_to_le32(ESD_EXTID);
751
752 for (i = 0; i < cf->can_dlc; i++)
753 msg->msg.tx.data[i] = cf->data[i];
754
755 msg->msg.hdr.len += (cf->can_dlc + 3) >> 2;
756
757 for (i = 0; i < MAX_TX_URBS; i++) {
758 if (priv->tx_contexts[i].echo_index == MAX_TX_URBS) {
759 context = &priv->tx_contexts[i];
760 break;
761 }
762 }
763
764 /*
765 * This may never happen.
766 */
767 if (!context) {
768 netdev_warn(netdev, "couldn't find free context\n");
769 ret = NETDEV_TX_BUSY;
770 goto releasebuf;
771 }
772
773 context->priv = priv;
774 context->echo_index = i;
775 context->dlc = cf->can_dlc;
776
777 /* hnd must not be 0 - MSB is stripped in txdone handling */
778 msg->msg.tx.hnd = 0x80000000 | i; /* returned in TX done message */
779
780 usb_fill_bulk_urb(urb, dev->udev, usb_sndbulkpipe(dev->udev, 2), buf,
781 msg->msg.hdr.len << 2,
782 esd_usb2_write_bulk_callback, context);
783
784 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
785
786 usb_anchor_urb(urb, &priv->tx_submitted);
787
788 can_put_echo_skb(skb, netdev, context->echo_index);
789
790 atomic_inc(&priv->active_tx_jobs);
791
792 /* Slow down tx path */
793 if (atomic_read(&priv->active_tx_jobs) >= MAX_TX_URBS)
794 netif_stop_queue(netdev);
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 netdev_warn(netdev, "failed tx_urb %d\n", err);
809
810 goto releasebuf;
811 }
812
813 netdev->trans_start = jiffies;
814
815 /*
816 * Release our reference to this URB, the USB core will eventually free
817 * it entirely.
818 */
819 usb_free_urb(urb);
820
821 return NETDEV_TX_OK;
822
823 releasebuf:
824 usb_free_coherent(dev->udev, size, buf, urb->transfer_dma);
825
826 nobufmem:
827 usb_free_urb(urb);
828
829 nourbmem:
830 return ret;
831 }
832
833 static int esd_usb2_close(struct net_device *netdev)
834 {
835 struct esd_usb2_net_priv *priv = netdev_priv(netdev);
836 struct esd_usb2_msg msg;
837 int i;
838
839 /* Disable all IDs (see esd_usb2_start()) */
840 msg.msg.hdr.cmd = CMD_IDADD;
841 msg.msg.hdr.len = 2 + ESD_MAX_ID_SEGMENT;
842 msg.msg.filter.net = priv->index;
843 msg.msg.filter.option = ESD_ID_ENABLE; /* start with segment 0 */
844 for (i = 0; i <= ESD_MAX_ID_SEGMENT; i++)
845 msg.msg.filter.mask[i] = 0;
846 if (esd_usb2_send_msg(priv->usb2, &msg) < 0)
847 netdev_err(netdev, "sending idadd message failed\n");
848
849 /* set CAN controller to reset mode */
850 msg.msg.hdr.len = 2;
851 msg.msg.hdr.cmd = CMD_SETBAUD;
852 msg.msg.setbaud.net = priv->index;
853 msg.msg.setbaud.rsvd = 0;
854 msg.msg.setbaud.baud = cpu_to_le32(ESD_USB2_NO_BAUDRATE);
855 if (esd_usb2_send_msg(priv->usb2, &msg) < 0)
856 netdev_err(netdev, "sending setbaud message failed\n");
857
858 priv->can.state = CAN_STATE_STOPPED;
859
860 netif_stop_queue(netdev);
861
862 close_candev(netdev);
863
864 return 0;
865 }
866
867 static const struct net_device_ops esd_usb2_netdev_ops = {
868 .ndo_open = esd_usb2_open,
869 .ndo_stop = esd_usb2_close,
870 .ndo_start_xmit = esd_usb2_start_xmit,
871 };
872
873 static const struct can_bittiming_const esd_usb2_bittiming_const = {
874 .name = "esd_usb2",
875 .tseg1_min = ESD_USB2_TSEG1_MIN,
876 .tseg1_max = ESD_USB2_TSEG1_MAX,
877 .tseg2_min = ESD_USB2_TSEG2_MIN,
878 .tseg2_max = ESD_USB2_TSEG2_MAX,
879 .sjw_max = ESD_USB2_SJW_MAX,
880 .brp_min = ESD_USB2_BRP_MIN,
881 .brp_max = ESD_USB2_BRP_MAX,
882 .brp_inc = ESD_USB2_BRP_INC,
883 };
884
885 static int esd_usb2_set_bittiming(struct net_device *netdev)
886 {
887 struct esd_usb2_net_priv *priv = netdev_priv(netdev);
888 struct can_bittiming *bt = &priv->can.bittiming;
889 struct esd_usb2_msg msg;
890 u32 canbtr;
891 int sjw_shift;
892
893 canbtr = ESD_USB2_UBR;
894 if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
895 canbtr |= ESD_USB2_LOM;
896
897 canbtr |= (bt->brp - 1) & (ESD_USB2_BRP_MAX - 1);
898
899 if (le16_to_cpu(priv->usb2->udev->descriptor.idProduct) ==
900 USB_CANUSBM_PRODUCT_ID)
901 sjw_shift = ESD_USBM_SJW_SHIFT;
902 else
903 sjw_shift = ESD_USB2_SJW_SHIFT;
904
905 canbtr |= ((bt->sjw - 1) & (ESD_USB2_SJW_MAX - 1))
906 << sjw_shift;
907 canbtr |= ((bt->prop_seg + bt->phase_seg1 - 1)
908 & (ESD_USB2_TSEG1_MAX - 1))
909 << ESD_USB2_TSEG1_SHIFT;
910 canbtr |= ((bt->phase_seg2 - 1) & (ESD_USB2_TSEG2_MAX - 1))
911 << ESD_USB2_TSEG2_SHIFT;
912 if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
913 canbtr |= ESD_USB2_3_SAMPLES;
914
915 msg.msg.hdr.len = 2;
916 msg.msg.hdr.cmd = CMD_SETBAUD;
917 msg.msg.setbaud.net = priv->index;
918 msg.msg.setbaud.rsvd = 0;
919 msg.msg.setbaud.baud = cpu_to_le32(canbtr);
920
921 netdev_info(netdev, "setting BTR=%#x\n", canbtr);
922
923 return esd_usb2_send_msg(priv->usb2, &msg);
924 }
925
926 static int esd_usb2_get_berr_counter(const struct net_device *netdev,
927 struct can_berr_counter *bec)
928 {
929 struct esd_usb2_net_priv *priv = netdev_priv(netdev);
930
931 bec->txerr = priv->bec.txerr;
932 bec->rxerr = priv->bec.rxerr;
933
934 return 0;
935 }
936
937 static int esd_usb2_set_mode(struct net_device *netdev, enum can_mode mode)
938 {
939 switch (mode) {
940 case CAN_MODE_START:
941 netif_wake_queue(netdev);
942 break;
943
944 default:
945 return -EOPNOTSUPP;
946 }
947
948 return 0;
949 }
950
951 static int esd_usb2_probe_one_net(struct usb_interface *intf, int index)
952 {
953 struct esd_usb2 *dev = usb_get_intfdata(intf);
954 struct net_device *netdev;
955 struct esd_usb2_net_priv *priv;
956 int err = 0;
957 int i;
958
959 netdev = alloc_candev(sizeof(*priv), MAX_TX_URBS);
960 if (!netdev) {
961 dev_err(&intf->dev, "couldn't alloc candev\n");
962 err = -ENOMEM;
963 goto done;
964 }
965
966 priv = netdev_priv(netdev);
967
968 init_usb_anchor(&priv->tx_submitted);
969 atomic_set(&priv->active_tx_jobs, 0);
970
971 for (i = 0; i < MAX_TX_URBS; i++)
972 priv->tx_contexts[i].echo_index = MAX_TX_URBS;
973
974 priv->usb2 = dev;
975 priv->netdev = netdev;
976 priv->index = index;
977
978 priv->can.state = CAN_STATE_STOPPED;
979 priv->can.ctrlmode_supported = CAN_CTRLMODE_LISTENONLY;
980
981 if (le16_to_cpu(dev->udev->descriptor.idProduct) ==
982 USB_CANUSBM_PRODUCT_ID)
983 priv->can.clock.freq = ESD_USBM_CAN_CLOCK;
984 else {
985 priv->can.clock.freq = ESD_USB2_CAN_CLOCK;
986 priv->can.ctrlmode_supported |= CAN_CTRLMODE_3_SAMPLES;
987 }
988
989 priv->can.bittiming_const = &esd_usb2_bittiming_const;
990 priv->can.do_set_bittiming = esd_usb2_set_bittiming;
991 priv->can.do_set_mode = esd_usb2_set_mode;
992 priv->can.do_get_berr_counter = esd_usb2_get_berr_counter;
993
994 netdev->flags |= IFF_ECHO; /* we support local echo */
995
996 netdev->netdev_ops = &esd_usb2_netdev_ops;
997
998 SET_NETDEV_DEV(netdev, &intf->dev);
999
1000 err = register_candev(netdev);
1001 if (err) {
1002 dev_err(&intf->dev, "couldn't register CAN device: %d\n", err);
1003 free_candev(netdev);
1004 err = -ENOMEM;
1005 goto done;
1006 }
1007
1008 dev->nets[index] = priv;
1009 netdev_info(netdev, "device %s registered\n", netdev->name);
1010
1011 done:
1012 return err;
1013 }
1014
1015 /*
1016 * probe function for new USB2 devices
1017 *
1018 * check version information and number of available
1019 * CAN interfaces
1020 */
1021 static int esd_usb2_probe(struct usb_interface *intf,
1022 const struct usb_device_id *id)
1023 {
1024 struct esd_usb2 *dev;
1025 struct esd_usb2_msg msg;
1026 int i, err;
1027
1028 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1029 if (!dev) {
1030 err = -ENOMEM;
1031 goto done;
1032 }
1033
1034 dev->udev = interface_to_usbdev(intf);
1035
1036 init_usb_anchor(&dev->rx_submitted);
1037
1038 usb_set_intfdata(intf, dev);
1039
1040 /* query number of CAN interfaces (nets) */
1041 msg.msg.hdr.cmd = CMD_VERSION;
1042 msg.msg.hdr.len = 2;
1043 msg.msg.version.rsvd = 0;
1044 msg.msg.version.flags = 0;
1045 msg.msg.version.drv_version = 0;
1046
1047 err = esd_usb2_send_msg(dev, &msg);
1048 if (err < 0) {
1049 dev_err(&intf->dev, "sending version message failed\n");
1050 goto free_dev;
1051 }
1052
1053 err = esd_usb2_wait_msg(dev, &msg);
1054 if (err < 0) {
1055 dev_err(&intf->dev, "no version message answer\n");
1056 goto free_dev;
1057 }
1058
1059 dev->net_count = (int)msg.msg.version_reply.nets;
1060 dev->version = le32_to_cpu(msg.msg.version_reply.version);
1061
1062 if (device_create_file(&intf->dev, &dev_attr_firmware))
1063 dev_err(&intf->dev,
1064 "Couldn't create device file for firmware\n");
1065
1066 if (device_create_file(&intf->dev, &dev_attr_hardware))
1067 dev_err(&intf->dev,
1068 "Couldn't create device file for hardware\n");
1069
1070 if (device_create_file(&intf->dev, &dev_attr_nets))
1071 dev_err(&intf->dev,
1072 "Couldn't create device file for nets\n");
1073
1074 /* do per device probing */
1075 for (i = 0; i < dev->net_count; i++)
1076 esd_usb2_probe_one_net(intf, i);
1077
1078 return 0;
1079
1080 free_dev:
1081 kfree(dev);
1082 done:
1083 return err;
1084 }
1085
1086 /*
1087 * called by the usb core when the device is removed from the system
1088 */
1089 static void esd_usb2_disconnect(struct usb_interface *intf)
1090 {
1091 struct esd_usb2 *dev = usb_get_intfdata(intf);
1092 struct net_device *netdev;
1093 int i;
1094
1095 device_remove_file(&intf->dev, &dev_attr_firmware);
1096 device_remove_file(&intf->dev, &dev_attr_hardware);
1097 device_remove_file(&intf->dev, &dev_attr_nets);
1098
1099 usb_set_intfdata(intf, NULL);
1100
1101 if (dev) {
1102 for (i = 0; i < dev->net_count; i++) {
1103 if (dev->nets[i]) {
1104 netdev = dev->nets[i]->netdev;
1105 unregister_netdev(netdev);
1106 free_candev(netdev);
1107 }
1108 }
1109 unlink_all_urbs(dev);
1110 }
1111 }
1112
1113 /* usb specific object needed to register this driver with the usb subsystem */
1114 static struct usb_driver esd_usb2_driver = {
1115 .name = "esd_usb2",
1116 .probe = esd_usb2_probe,
1117 .disconnect = esd_usb2_disconnect,
1118 .id_table = esd_usb2_table,
1119 };
1120
1121 module_usb_driver(esd_usb2_driver);