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