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