irqchip/s3c24xx: Mark init_eint as __maybe_unused
[linux/fpc-iii.git] / drivers / net / can / usb / kvaser_usb.c
blob022bfa13ebfa0c85491cfc2cb17a3437c01f0205
1 /*
2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License as
4 * published by the Free Software Foundation version 2.
6 * Parts of this driver are based on the following:
7 * - Kvaser linux leaf driver (version 4.78)
8 * - CAN driver for esd CAN-USB/2
9 * - Kvaser linux usbcanII driver (version 5.3)
11 * Copyright (C) 2002-2006 KVASER AB, Sweden. All rights reserved.
12 * Copyright (C) 2010 Matthias Fuchs <matthias.fuchs@esd.eu>, esd gmbh
13 * Copyright (C) 2012 Olivier Sobrie <olivier@sobrie.be>
14 * Copyright (C) 2015 Valeo S.A.
17 #include <linux/spinlock.h>
18 #include <linux/kernel.h>
19 #include <linux/completion.h>
20 #include <linux/module.h>
21 #include <linux/netdevice.h>
22 #include <linux/usb.h>
24 #include <linux/can.h>
25 #include <linux/can/dev.h>
26 #include <linux/can/error.h>
28 #define MAX_RX_URBS 4
29 #define START_TIMEOUT 1000 /* msecs */
30 #define STOP_TIMEOUT 1000 /* msecs */
31 #define USB_SEND_TIMEOUT 1000 /* msecs */
32 #define USB_RECV_TIMEOUT 1000 /* msecs */
33 #define RX_BUFFER_SIZE 3072
34 #define CAN_USB_CLOCK 8000000
35 #define MAX_NET_DEVICES 3
36 #define MAX_USBCAN_NET_DEVICES 2
38 /* Kvaser Leaf USB devices */
39 #define KVASER_VENDOR_ID 0x0bfd
40 #define USB_LEAF_DEVEL_PRODUCT_ID 10
41 #define USB_LEAF_LITE_PRODUCT_ID 11
42 #define USB_LEAF_PRO_PRODUCT_ID 12
43 #define USB_LEAF_SPRO_PRODUCT_ID 14
44 #define USB_LEAF_PRO_LS_PRODUCT_ID 15
45 #define USB_LEAF_PRO_SWC_PRODUCT_ID 16
46 #define USB_LEAF_PRO_LIN_PRODUCT_ID 17
47 #define USB_LEAF_SPRO_LS_PRODUCT_ID 18
48 #define USB_LEAF_SPRO_SWC_PRODUCT_ID 19
49 #define USB_MEMO2_DEVEL_PRODUCT_ID 22
50 #define USB_MEMO2_HSHS_PRODUCT_ID 23
51 #define USB_UPRO_HSHS_PRODUCT_ID 24
52 #define USB_LEAF_LITE_GI_PRODUCT_ID 25
53 #define USB_LEAF_PRO_OBDII_PRODUCT_ID 26
54 #define USB_MEMO2_HSLS_PRODUCT_ID 27
55 #define USB_LEAF_LITE_CH_PRODUCT_ID 28
56 #define USB_BLACKBIRD_SPRO_PRODUCT_ID 29
57 #define USB_OEM_MERCURY_PRODUCT_ID 34
58 #define USB_OEM_LEAF_PRODUCT_ID 35
59 #define USB_CAN_R_PRODUCT_ID 39
60 #define USB_LEAF_LITE_V2_PRODUCT_ID 288
61 #define USB_MINI_PCIE_HS_PRODUCT_ID 289
63 static inline bool kvaser_is_leaf(const struct usb_device_id *id)
65 return id->idProduct >= USB_LEAF_DEVEL_PRODUCT_ID &&
66 id->idProduct <= USB_MINI_PCIE_HS_PRODUCT_ID;
69 /* Kvaser USBCan-II devices */
70 #define USB_USBCAN_REVB_PRODUCT_ID 2
71 #define USB_VCI2_PRODUCT_ID 3
72 #define USB_USBCAN2_PRODUCT_ID 4
73 #define USB_MEMORATOR_PRODUCT_ID 5
75 static inline bool kvaser_is_usbcan(const struct usb_device_id *id)
77 return id->idProduct >= USB_USBCAN_REVB_PRODUCT_ID &&
78 id->idProduct <= USB_MEMORATOR_PRODUCT_ID;
81 /* USB devices features */
82 #define KVASER_HAS_SILENT_MODE BIT(0)
83 #define KVASER_HAS_TXRX_ERRORS BIT(1)
85 /* Message header size */
86 #define MSG_HEADER_LEN 2
88 /* Can message flags */
89 #define MSG_FLAG_ERROR_FRAME BIT(0)
90 #define MSG_FLAG_OVERRUN BIT(1)
91 #define MSG_FLAG_NERR BIT(2)
92 #define MSG_FLAG_WAKEUP BIT(3)
93 #define MSG_FLAG_REMOTE_FRAME BIT(4)
94 #define MSG_FLAG_RESERVED BIT(5)
95 #define MSG_FLAG_TX_ACK BIT(6)
96 #define MSG_FLAG_TX_REQUEST BIT(7)
98 /* Can states (M16C CxSTRH register) */
99 #define M16C_STATE_BUS_RESET BIT(0)
100 #define M16C_STATE_BUS_ERROR BIT(4)
101 #define M16C_STATE_BUS_PASSIVE BIT(5)
102 #define M16C_STATE_BUS_OFF BIT(6)
104 /* Can msg ids */
105 #define CMD_RX_STD_MESSAGE 12
106 #define CMD_TX_STD_MESSAGE 13
107 #define CMD_RX_EXT_MESSAGE 14
108 #define CMD_TX_EXT_MESSAGE 15
109 #define CMD_SET_BUS_PARAMS 16
110 #define CMD_GET_BUS_PARAMS 17
111 #define CMD_GET_BUS_PARAMS_REPLY 18
112 #define CMD_GET_CHIP_STATE 19
113 #define CMD_CHIP_STATE_EVENT 20
114 #define CMD_SET_CTRL_MODE 21
115 #define CMD_GET_CTRL_MODE 22
116 #define CMD_GET_CTRL_MODE_REPLY 23
117 #define CMD_RESET_CHIP 24
118 #define CMD_RESET_CARD 25
119 #define CMD_START_CHIP 26
120 #define CMD_START_CHIP_REPLY 27
121 #define CMD_STOP_CHIP 28
122 #define CMD_STOP_CHIP_REPLY 29
124 #define CMD_LEAF_GET_CARD_INFO2 32
125 #define CMD_USBCAN_RESET_CLOCK 32
126 #define CMD_USBCAN_CLOCK_OVERFLOW_EVENT 33
128 #define CMD_GET_CARD_INFO 34
129 #define CMD_GET_CARD_INFO_REPLY 35
130 #define CMD_GET_SOFTWARE_INFO 38
131 #define CMD_GET_SOFTWARE_INFO_REPLY 39
132 #define CMD_ERROR_EVENT 45
133 #define CMD_FLUSH_QUEUE 48
134 #define CMD_RESET_ERROR_COUNTER 49
135 #define CMD_TX_ACKNOWLEDGE 50
136 #define CMD_CAN_ERROR_EVENT 51
138 #define CMD_LEAF_USB_THROTTLE 77
139 #define CMD_LEAF_LOG_MESSAGE 106
141 /* error factors */
142 #define M16C_EF_ACKE BIT(0)
143 #define M16C_EF_CRCE BIT(1)
144 #define M16C_EF_FORME BIT(2)
145 #define M16C_EF_STFE BIT(3)
146 #define M16C_EF_BITE0 BIT(4)
147 #define M16C_EF_BITE1 BIT(5)
148 #define M16C_EF_RCVE BIT(6)
149 #define M16C_EF_TRE BIT(7)
151 /* Only Leaf-based devices can report M16C error factors,
152 * thus define our own error status flags for USBCANII
154 #define USBCAN_ERROR_STATE_NONE 0
155 #define USBCAN_ERROR_STATE_TX_ERROR BIT(0)
156 #define USBCAN_ERROR_STATE_RX_ERROR BIT(1)
157 #define USBCAN_ERROR_STATE_BUSERROR BIT(2)
159 /* bittiming parameters */
160 #define KVASER_USB_TSEG1_MIN 1
161 #define KVASER_USB_TSEG1_MAX 16
162 #define KVASER_USB_TSEG2_MIN 1
163 #define KVASER_USB_TSEG2_MAX 8
164 #define KVASER_USB_SJW_MAX 4
165 #define KVASER_USB_BRP_MIN 1
166 #define KVASER_USB_BRP_MAX 64
167 #define KVASER_USB_BRP_INC 1
169 /* ctrl modes */
170 #define KVASER_CTRL_MODE_NORMAL 1
171 #define KVASER_CTRL_MODE_SILENT 2
172 #define KVASER_CTRL_MODE_SELFRECEPTION 3
173 #define KVASER_CTRL_MODE_OFF 4
175 /* Extended CAN identifier flag */
176 #define KVASER_EXTENDED_FRAME BIT(31)
178 /* Kvaser USB CAN dongles are divided into two major families:
179 * - Leaf: Based on Renesas M32C, running firmware labeled as 'filo'
180 * - UsbcanII: Based on Renesas M16C, running firmware labeled as 'helios'
182 enum kvaser_usb_family {
183 KVASER_LEAF,
184 KVASER_USBCAN,
187 struct kvaser_msg_simple {
188 u8 tid;
189 u8 channel;
190 } __packed;
192 struct kvaser_msg_cardinfo {
193 u8 tid;
194 u8 nchannels;
195 union {
196 struct {
197 __le32 serial_number;
198 __le32 padding;
199 } __packed leaf0;
200 struct {
201 __le32 serial_number_low;
202 __le32 serial_number_high;
203 } __packed usbcan0;
204 } __packed;
205 __le32 clock_resolution;
206 __le32 mfgdate;
207 u8 ean[8];
208 u8 hw_revision;
209 union {
210 struct {
211 u8 usb_hs_mode;
212 } __packed leaf1;
213 struct {
214 u8 padding;
215 } __packed usbcan1;
216 } __packed;
217 __le16 padding;
218 } __packed;
220 struct kvaser_msg_cardinfo2 {
221 u8 tid;
222 u8 reserved;
223 u8 pcb_id[24];
224 __le32 oem_unlock_code;
225 } __packed;
227 struct leaf_msg_softinfo {
228 u8 tid;
229 u8 padding0;
230 __le32 sw_options;
231 __le32 fw_version;
232 __le16 max_outstanding_tx;
233 __le16 padding1[9];
234 } __packed;
236 struct usbcan_msg_softinfo {
237 u8 tid;
238 u8 fw_name[5];
239 __le16 max_outstanding_tx;
240 u8 padding[6];
241 __le32 fw_version;
242 __le16 checksum;
243 __le16 sw_options;
244 } __packed;
246 struct kvaser_msg_busparams {
247 u8 tid;
248 u8 channel;
249 __le32 bitrate;
250 u8 tseg1;
251 u8 tseg2;
252 u8 sjw;
253 u8 no_samp;
254 } __packed;
256 struct kvaser_msg_tx_can {
257 u8 channel;
258 u8 tid;
259 u8 msg[14];
260 union {
261 struct {
262 u8 padding;
263 u8 flags;
264 } __packed leaf;
265 struct {
266 u8 flags;
267 u8 padding;
268 } __packed usbcan;
269 } __packed;
270 } __packed;
272 struct kvaser_msg_rx_can_header {
273 u8 channel;
274 u8 flag;
275 } __packed;
277 struct leaf_msg_rx_can {
278 u8 channel;
279 u8 flag;
281 __le16 time[3];
282 u8 msg[14];
283 } __packed;
285 struct usbcan_msg_rx_can {
286 u8 channel;
287 u8 flag;
289 u8 msg[14];
290 __le16 time;
291 } __packed;
293 struct leaf_msg_chip_state_event {
294 u8 tid;
295 u8 channel;
297 __le16 time[3];
298 u8 tx_errors_count;
299 u8 rx_errors_count;
301 u8 status;
302 u8 padding[3];
303 } __packed;
305 struct usbcan_msg_chip_state_event {
306 u8 tid;
307 u8 channel;
309 u8 tx_errors_count;
310 u8 rx_errors_count;
311 __le16 time;
313 u8 status;
314 u8 padding[3];
315 } __packed;
317 struct kvaser_msg_tx_acknowledge_header {
318 u8 channel;
319 u8 tid;
320 } __packed;
322 struct leaf_msg_tx_acknowledge {
323 u8 channel;
324 u8 tid;
326 __le16 time[3];
327 u8 flags;
328 u8 time_offset;
329 } __packed;
331 struct usbcan_msg_tx_acknowledge {
332 u8 channel;
333 u8 tid;
335 __le16 time;
336 __le16 padding;
337 } __packed;
339 struct leaf_msg_error_event {
340 u8 tid;
341 u8 flags;
342 __le16 time[3];
343 u8 channel;
344 u8 padding;
345 u8 tx_errors_count;
346 u8 rx_errors_count;
347 u8 status;
348 u8 error_factor;
349 } __packed;
351 struct usbcan_msg_error_event {
352 u8 tid;
353 u8 padding;
354 u8 tx_errors_count_ch0;
355 u8 rx_errors_count_ch0;
356 u8 tx_errors_count_ch1;
357 u8 rx_errors_count_ch1;
358 u8 status_ch0;
359 u8 status_ch1;
360 __le16 time;
361 } __packed;
363 struct kvaser_msg_ctrl_mode {
364 u8 tid;
365 u8 channel;
366 u8 ctrl_mode;
367 u8 padding[3];
368 } __packed;
370 struct kvaser_msg_flush_queue {
371 u8 tid;
372 u8 channel;
373 u8 flags;
374 u8 padding[3];
375 } __packed;
377 struct leaf_msg_log_message {
378 u8 channel;
379 u8 flags;
380 __le16 time[3];
381 u8 dlc;
382 u8 time_offset;
383 __le32 id;
384 u8 data[8];
385 } __packed;
387 struct kvaser_msg {
388 u8 len;
389 u8 id;
390 union {
391 struct kvaser_msg_simple simple;
392 struct kvaser_msg_cardinfo cardinfo;
393 struct kvaser_msg_cardinfo2 cardinfo2;
394 struct kvaser_msg_busparams busparams;
396 struct kvaser_msg_rx_can_header rx_can_header;
397 struct kvaser_msg_tx_acknowledge_header tx_acknowledge_header;
399 union {
400 struct leaf_msg_softinfo softinfo;
401 struct leaf_msg_rx_can rx_can;
402 struct leaf_msg_chip_state_event chip_state_event;
403 struct leaf_msg_tx_acknowledge tx_acknowledge;
404 struct leaf_msg_error_event error_event;
405 struct leaf_msg_log_message log_message;
406 } __packed leaf;
408 union {
409 struct usbcan_msg_softinfo softinfo;
410 struct usbcan_msg_rx_can rx_can;
411 struct usbcan_msg_chip_state_event chip_state_event;
412 struct usbcan_msg_tx_acknowledge tx_acknowledge;
413 struct usbcan_msg_error_event error_event;
414 } __packed usbcan;
416 struct kvaser_msg_tx_can tx_can;
417 struct kvaser_msg_ctrl_mode ctrl_mode;
418 struct kvaser_msg_flush_queue flush_queue;
419 } u;
420 } __packed;
422 /* Summary of a kvaser error event, for a unified Leaf/Usbcan error
423 * handling. Some discrepancies between the two families exist:
425 * - USBCAN firmware does not report M16C "error factors"
426 * - USBCAN controllers has difficulties reporting if the raised error
427 * event is for ch0 or ch1. They leave such arbitration to the OS
428 * driver by letting it compare error counters with previous values
429 * and decide the error event's channel. Thus for USBCAN, the channel
430 * field is only advisory.
432 struct kvaser_usb_error_summary {
433 u8 channel, status, txerr, rxerr;
434 union {
435 struct {
436 u8 error_factor;
437 } leaf;
438 struct {
439 u8 other_ch_status;
440 u8 error_state;
441 } usbcan;
445 /* Context for an outstanding, not yet ACKed, transmission */
446 struct kvaser_usb_tx_urb_context {
447 struct kvaser_usb_net_priv *priv;
448 u32 echo_index;
449 int dlc;
452 struct kvaser_usb {
453 struct usb_device *udev;
454 struct kvaser_usb_net_priv *nets[MAX_NET_DEVICES];
456 struct usb_endpoint_descriptor *bulk_in, *bulk_out;
457 struct usb_anchor rx_submitted;
459 /* @max_tx_urbs: Firmware-reported maximum number of oustanding,
460 * not yet ACKed, transmissions on this device. This value is
461 * also used as a sentinel for marking free tx contexts.
463 u32 fw_version;
464 unsigned int nchannels;
465 unsigned int max_tx_urbs;
466 enum kvaser_usb_family family;
468 bool rxinitdone;
469 void *rxbuf[MAX_RX_URBS];
470 dma_addr_t rxbuf_dma[MAX_RX_URBS];
473 struct kvaser_usb_net_priv {
474 struct can_priv can;
475 struct can_berr_counter bec;
477 struct kvaser_usb *dev;
478 struct net_device *netdev;
479 int channel;
481 struct completion start_comp, stop_comp;
482 struct usb_anchor tx_submitted;
484 spinlock_t tx_contexts_lock;
485 int active_tx_contexts;
486 struct kvaser_usb_tx_urb_context tx_contexts[];
489 static const struct usb_device_id kvaser_usb_table[] = {
490 /* Leaf family IDs */
491 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_DEVEL_PRODUCT_ID) },
492 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_PRODUCT_ID) },
493 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_PRODUCT_ID),
494 .driver_info = KVASER_HAS_TXRX_ERRORS |
495 KVASER_HAS_SILENT_MODE },
496 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_SPRO_PRODUCT_ID),
497 .driver_info = KVASER_HAS_TXRX_ERRORS |
498 KVASER_HAS_SILENT_MODE },
499 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_LS_PRODUCT_ID),
500 .driver_info = KVASER_HAS_TXRX_ERRORS |
501 KVASER_HAS_SILENT_MODE },
502 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_SWC_PRODUCT_ID),
503 .driver_info = KVASER_HAS_TXRX_ERRORS |
504 KVASER_HAS_SILENT_MODE },
505 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_LIN_PRODUCT_ID),
506 .driver_info = KVASER_HAS_TXRX_ERRORS |
507 KVASER_HAS_SILENT_MODE },
508 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_SPRO_LS_PRODUCT_ID),
509 .driver_info = KVASER_HAS_TXRX_ERRORS |
510 KVASER_HAS_SILENT_MODE },
511 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_SPRO_SWC_PRODUCT_ID),
512 .driver_info = KVASER_HAS_TXRX_ERRORS |
513 KVASER_HAS_SILENT_MODE },
514 { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO2_DEVEL_PRODUCT_ID),
515 .driver_info = KVASER_HAS_TXRX_ERRORS |
516 KVASER_HAS_SILENT_MODE },
517 { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO2_HSHS_PRODUCT_ID),
518 .driver_info = KVASER_HAS_TXRX_ERRORS |
519 KVASER_HAS_SILENT_MODE },
520 { USB_DEVICE(KVASER_VENDOR_ID, USB_UPRO_HSHS_PRODUCT_ID),
521 .driver_info = KVASER_HAS_TXRX_ERRORS },
522 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_GI_PRODUCT_ID) },
523 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_OBDII_PRODUCT_ID),
524 .driver_info = KVASER_HAS_TXRX_ERRORS |
525 KVASER_HAS_SILENT_MODE },
526 { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO2_HSLS_PRODUCT_ID),
527 .driver_info = KVASER_HAS_TXRX_ERRORS },
528 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_CH_PRODUCT_ID),
529 .driver_info = KVASER_HAS_TXRX_ERRORS },
530 { USB_DEVICE(KVASER_VENDOR_ID, USB_BLACKBIRD_SPRO_PRODUCT_ID),
531 .driver_info = KVASER_HAS_TXRX_ERRORS },
532 { USB_DEVICE(KVASER_VENDOR_ID, USB_OEM_MERCURY_PRODUCT_ID),
533 .driver_info = KVASER_HAS_TXRX_ERRORS },
534 { USB_DEVICE(KVASER_VENDOR_ID, USB_OEM_LEAF_PRODUCT_ID),
535 .driver_info = KVASER_HAS_TXRX_ERRORS },
536 { USB_DEVICE(KVASER_VENDOR_ID, USB_CAN_R_PRODUCT_ID),
537 .driver_info = KVASER_HAS_TXRX_ERRORS },
538 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_V2_PRODUCT_ID) },
539 { USB_DEVICE(KVASER_VENDOR_ID, USB_MINI_PCIE_HS_PRODUCT_ID) },
541 /* USBCANII family IDs */
542 { USB_DEVICE(KVASER_VENDOR_ID, USB_USBCAN2_PRODUCT_ID),
543 .driver_info = KVASER_HAS_TXRX_ERRORS },
544 { USB_DEVICE(KVASER_VENDOR_ID, USB_USBCAN_REVB_PRODUCT_ID),
545 .driver_info = KVASER_HAS_TXRX_ERRORS },
546 { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMORATOR_PRODUCT_ID),
547 .driver_info = KVASER_HAS_TXRX_ERRORS },
548 { USB_DEVICE(KVASER_VENDOR_ID, USB_VCI2_PRODUCT_ID),
549 .driver_info = KVASER_HAS_TXRX_ERRORS },
553 MODULE_DEVICE_TABLE(usb, kvaser_usb_table);
555 static inline int kvaser_usb_send_msg(const struct kvaser_usb *dev,
556 struct kvaser_msg *msg)
558 int actual_len;
560 return usb_bulk_msg(dev->udev,
561 usb_sndbulkpipe(dev->udev,
562 dev->bulk_out->bEndpointAddress),
563 msg, msg->len, &actual_len,
564 USB_SEND_TIMEOUT);
567 static int kvaser_usb_wait_msg(const struct kvaser_usb *dev, u8 id,
568 struct kvaser_msg *msg)
570 struct kvaser_msg *tmp;
571 void *buf;
572 int actual_len;
573 int err;
574 int pos;
575 unsigned long to = jiffies + msecs_to_jiffies(USB_RECV_TIMEOUT);
577 buf = kzalloc(RX_BUFFER_SIZE, GFP_KERNEL);
578 if (!buf)
579 return -ENOMEM;
581 do {
582 err = usb_bulk_msg(dev->udev,
583 usb_rcvbulkpipe(dev->udev,
584 dev->bulk_in->bEndpointAddress),
585 buf, RX_BUFFER_SIZE, &actual_len,
586 USB_RECV_TIMEOUT);
587 if (err < 0)
588 goto end;
590 pos = 0;
591 while (pos <= actual_len - MSG_HEADER_LEN) {
592 tmp = buf + pos;
594 /* Handle messages crossing the USB endpoint max packet
595 * size boundary. Check kvaser_usb_read_bulk_callback()
596 * for further details.
598 if (tmp->len == 0) {
599 pos = round_up(pos, le16_to_cpu(dev->bulk_in->
600 wMaxPacketSize));
601 continue;
604 if (pos + tmp->len > actual_len) {
605 dev_err(dev->udev->dev.parent,
606 "Format error\n");
607 break;
610 if (tmp->id == id) {
611 memcpy(msg, tmp, tmp->len);
612 goto end;
615 pos += tmp->len;
617 } while (time_before(jiffies, to));
619 err = -EINVAL;
621 end:
622 kfree(buf);
624 return err;
627 static int kvaser_usb_send_simple_msg(const struct kvaser_usb *dev,
628 u8 msg_id, int channel)
630 struct kvaser_msg *msg;
631 int rc;
633 msg = kmalloc(sizeof(*msg), GFP_KERNEL);
634 if (!msg)
635 return -ENOMEM;
637 msg->id = msg_id;
638 msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_simple);
639 msg->u.simple.channel = channel;
640 msg->u.simple.tid = 0xff;
642 rc = kvaser_usb_send_msg(dev, msg);
644 kfree(msg);
645 return rc;
648 static int kvaser_usb_get_software_info(struct kvaser_usb *dev)
650 struct kvaser_msg msg;
651 int err;
653 err = kvaser_usb_send_simple_msg(dev, CMD_GET_SOFTWARE_INFO, 0);
654 if (err)
655 return err;
657 err = kvaser_usb_wait_msg(dev, CMD_GET_SOFTWARE_INFO_REPLY, &msg);
658 if (err)
659 return err;
661 switch (dev->family) {
662 case KVASER_LEAF:
663 dev->fw_version = le32_to_cpu(msg.u.leaf.softinfo.fw_version);
664 dev->max_tx_urbs =
665 le16_to_cpu(msg.u.leaf.softinfo.max_outstanding_tx);
666 break;
667 case KVASER_USBCAN:
668 dev->fw_version = le32_to_cpu(msg.u.usbcan.softinfo.fw_version);
669 dev->max_tx_urbs =
670 le16_to_cpu(msg.u.usbcan.softinfo.max_outstanding_tx);
671 break;
674 return 0;
677 static int kvaser_usb_get_card_info(struct kvaser_usb *dev)
679 struct kvaser_msg msg;
680 int err;
682 err = kvaser_usb_send_simple_msg(dev, CMD_GET_CARD_INFO, 0);
683 if (err)
684 return err;
686 err = kvaser_usb_wait_msg(dev, CMD_GET_CARD_INFO_REPLY, &msg);
687 if (err)
688 return err;
690 dev->nchannels = msg.u.cardinfo.nchannels;
691 if ((dev->nchannels > MAX_NET_DEVICES) ||
692 (dev->family == KVASER_USBCAN &&
693 dev->nchannels > MAX_USBCAN_NET_DEVICES))
694 return -EINVAL;
696 return 0;
699 static void kvaser_usb_tx_acknowledge(const struct kvaser_usb *dev,
700 const struct kvaser_msg *msg)
702 struct net_device_stats *stats;
703 struct kvaser_usb_tx_urb_context *context;
704 struct kvaser_usb_net_priv *priv;
705 struct sk_buff *skb;
706 struct can_frame *cf;
707 unsigned long flags;
708 u8 channel, tid;
710 channel = msg->u.tx_acknowledge_header.channel;
711 tid = msg->u.tx_acknowledge_header.tid;
713 if (channel >= dev->nchannels) {
714 dev_err(dev->udev->dev.parent,
715 "Invalid channel number (%d)\n", channel);
716 return;
719 priv = dev->nets[channel];
721 if (!netif_device_present(priv->netdev))
722 return;
724 stats = &priv->netdev->stats;
726 context = &priv->tx_contexts[tid % dev->max_tx_urbs];
728 /* Sometimes the state change doesn't come after a bus-off event */
729 if (priv->can.restart_ms &&
730 (priv->can.state >= CAN_STATE_BUS_OFF)) {
731 skb = alloc_can_err_skb(priv->netdev, &cf);
732 if (skb) {
733 cf->can_id |= CAN_ERR_RESTARTED;
735 stats->rx_packets++;
736 stats->rx_bytes += cf->can_dlc;
737 netif_rx(skb);
738 } else {
739 netdev_err(priv->netdev,
740 "No memory left for err_skb\n");
743 priv->can.can_stats.restarts++;
744 netif_carrier_on(priv->netdev);
746 priv->can.state = CAN_STATE_ERROR_ACTIVE;
749 stats->tx_packets++;
750 stats->tx_bytes += context->dlc;
752 spin_lock_irqsave(&priv->tx_contexts_lock, flags);
754 can_get_echo_skb(priv->netdev, context->echo_index);
755 context->echo_index = dev->max_tx_urbs;
756 --priv->active_tx_contexts;
757 netif_wake_queue(priv->netdev);
759 spin_unlock_irqrestore(&priv->tx_contexts_lock, flags);
762 static void kvaser_usb_simple_msg_callback(struct urb *urb)
764 struct net_device *netdev = urb->context;
766 kfree(urb->transfer_buffer);
768 if (urb->status)
769 netdev_warn(netdev, "urb status received: %d\n",
770 urb->status);
773 static int kvaser_usb_simple_msg_async(struct kvaser_usb_net_priv *priv,
774 u8 msg_id)
776 struct kvaser_usb *dev = priv->dev;
777 struct net_device *netdev = priv->netdev;
778 struct kvaser_msg *msg;
779 struct urb *urb;
780 void *buf;
781 int err;
783 urb = usb_alloc_urb(0, GFP_ATOMIC);
784 if (!urb) {
785 netdev_err(netdev, "No memory left for URBs\n");
786 return -ENOMEM;
789 buf = kmalloc(sizeof(struct kvaser_msg), GFP_ATOMIC);
790 if (!buf) {
791 usb_free_urb(urb);
792 return -ENOMEM;
795 msg = (struct kvaser_msg *)buf;
796 msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_simple);
797 msg->id = msg_id;
798 msg->u.simple.channel = priv->channel;
800 usb_fill_bulk_urb(urb, dev->udev,
801 usb_sndbulkpipe(dev->udev,
802 dev->bulk_out->bEndpointAddress),
803 buf, msg->len,
804 kvaser_usb_simple_msg_callback, netdev);
805 usb_anchor_urb(urb, &priv->tx_submitted);
807 err = usb_submit_urb(urb, GFP_ATOMIC);
808 if (err) {
809 netdev_err(netdev, "Error transmitting URB\n");
810 usb_unanchor_urb(urb);
811 usb_free_urb(urb);
812 return err;
815 usb_free_urb(urb);
817 return 0;
820 static void kvaser_usb_rx_error_update_can_state(struct kvaser_usb_net_priv *priv,
821 const struct kvaser_usb_error_summary *es,
822 struct can_frame *cf)
824 struct kvaser_usb *dev = priv->dev;
825 struct net_device_stats *stats = &priv->netdev->stats;
826 enum can_state cur_state, new_state, tx_state, rx_state;
828 netdev_dbg(priv->netdev, "Error status: 0x%02x\n", es->status);
830 new_state = cur_state = priv->can.state;
832 if (es->status & (M16C_STATE_BUS_OFF | M16C_STATE_BUS_RESET))
833 new_state = CAN_STATE_BUS_OFF;
834 else if (es->status & M16C_STATE_BUS_PASSIVE)
835 new_state = CAN_STATE_ERROR_PASSIVE;
836 else if (es->status & M16C_STATE_BUS_ERROR) {
837 /* Guard against spurious error events after a busoff */
838 if (cur_state < CAN_STATE_BUS_OFF) {
839 if ((es->txerr >= 128) || (es->rxerr >= 128))
840 new_state = CAN_STATE_ERROR_PASSIVE;
841 else if ((es->txerr >= 96) || (es->rxerr >= 96))
842 new_state = CAN_STATE_ERROR_WARNING;
843 else if (cur_state > CAN_STATE_ERROR_ACTIVE)
844 new_state = CAN_STATE_ERROR_ACTIVE;
848 if (!es->status)
849 new_state = CAN_STATE_ERROR_ACTIVE;
851 if (new_state != cur_state) {
852 tx_state = (es->txerr >= es->rxerr) ? new_state : 0;
853 rx_state = (es->txerr <= es->rxerr) ? new_state : 0;
855 can_change_state(priv->netdev, cf, tx_state, rx_state);
858 if (priv->can.restart_ms &&
859 (cur_state >= CAN_STATE_BUS_OFF) &&
860 (new_state < CAN_STATE_BUS_OFF)) {
861 priv->can.can_stats.restarts++;
864 switch (dev->family) {
865 case KVASER_LEAF:
866 if (es->leaf.error_factor) {
867 priv->can.can_stats.bus_error++;
868 stats->rx_errors++;
870 break;
871 case KVASER_USBCAN:
872 if (es->usbcan.error_state & USBCAN_ERROR_STATE_TX_ERROR)
873 stats->tx_errors++;
874 if (es->usbcan.error_state & USBCAN_ERROR_STATE_RX_ERROR)
875 stats->rx_errors++;
876 if (es->usbcan.error_state & USBCAN_ERROR_STATE_BUSERROR) {
877 priv->can.can_stats.bus_error++;
879 break;
882 priv->bec.txerr = es->txerr;
883 priv->bec.rxerr = es->rxerr;
886 static void kvaser_usb_rx_error(const struct kvaser_usb *dev,
887 const struct kvaser_usb_error_summary *es)
889 struct can_frame *cf, tmp_cf = { .can_id = CAN_ERR_FLAG, .can_dlc = CAN_ERR_DLC };
890 struct sk_buff *skb;
891 struct net_device_stats *stats;
892 struct kvaser_usb_net_priv *priv;
893 enum can_state old_state, new_state;
895 if (es->channel >= dev->nchannels) {
896 dev_err(dev->udev->dev.parent,
897 "Invalid channel number (%d)\n", es->channel);
898 return;
901 priv = dev->nets[es->channel];
902 stats = &priv->netdev->stats;
904 /* Update all of the can interface's state and error counters before
905 * trying any memory allocation that can actually fail with -ENOMEM.
907 * We send a temporary stack-allocated error can frame to
908 * can_change_state() for the very same reason.
910 * TODO: Split can_change_state() responsibility between updating the
911 * can interface's state and counters, and the setting up of can error
912 * frame ID and data to userspace. Remove stack allocation afterwards.
914 old_state = priv->can.state;
915 kvaser_usb_rx_error_update_can_state(priv, es, &tmp_cf);
916 new_state = priv->can.state;
918 skb = alloc_can_err_skb(priv->netdev, &cf);
919 if (!skb) {
920 stats->rx_dropped++;
921 return;
923 memcpy(cf, &tmp_cf, sizeof(*cf));
925 if (new_state != old_state) {
926 if (es->status &
927 (M16C_STATE_BUS_OFF | M16C_STATE_BUS_RESET)) {
928 if (!priv->can.restart_ms)
929 kvaser_usb_simple_msg_async(priv, CMD_STOP_CHIP);
930 netif_carrier_off(priv->netdev);
933 if (priv->can.restart_ms &&
934 (old_state >= CAN_STATE_BUS_OFF) &&
935 (new_state < CAN_STATE_BUS_OFF)) {
936 cf->can_id |= CAN_ERR_RESTARTED;
937 netif_carrier_on(priv->netdev);
941 switch (dev->family) {
942 case KVASER_LEAF:
943 if (es->leaf.error_factor) {
944 cf->can_id |= CAN_ERR_BUSERROR | CAN_ERR_PROT;
946 if (es->leaf.error_factor & M16C_EF_ACKE)
947 cf->data[3] = CAN_ERR_PROT_LOC_ACK;
948 if (es->leaf.error_factor & M16C_EF_CRCE)
949 cf->data[3] = CAN_ERR_PROT_LOC_CRC_SEQ;
950 if (es->leaf.error_factor & M16C_EF_FORME)
951 cf->data[2] |= CAN_ERR_PROT_FORM;
952 if (es->leaf.error_factor & M16C_EF_STFE)
953 cf->data[2] |= CAN_ERR_PROT_STUFF;
954 if (es->leaf.error_factor & M16C_EF_BITE0)
955 cf->data[2] |= CAN_ERR_PROT_BIT0;
956 if (es->leaf.error_factor & M16C_EF_BITE1)
957 cf->data[2] |= CAN_ERR_PROT_BIT1;
958 if (es->leaf.error_factor & M16C_EF_TRE)
959 cf->data[2] |= CAN_ERR_PROT_TX;
961 break;
962 case KVASER_USBCAN:
963 if (es->usbcan.error_state & USBCAN_ERROR_STATE_BUSERROR) {
964 cf->can_id |= CAN_ERR_BUSERROR;
966 break;
969 cf->data[6] = es->txerr;
970 cf->data[7] = es->rxerr;
972 stats->rx_packets++;
973 stats->rx_bytes += cf->can_dlc;
974 netif_rx(skb);
977 /* For USBCAN, report error to userspace iff the channels's errors counter
978 * has changed, or we're the only channel seeing a bus error state.
980 static void kvaser_usbcan_conditionally_rx_error(const struct kvaser_usb *dev,
981 struct kvaser_usb_error_summary *es)
983 struct kvaser_usb_net_priv *priv;
984 int channel;
985 bool report_error;
987 channel = es->channel;
988 if (channel >= dev->nchannels) {
989 dev_err(dev->udev->dev.parent,
990 "Invalid channel number (%d)\n", channel);
991 return;
994 priv = dev->nets[channel];
995 report_error = false;
997 if (es->txerr != priv->bec.txerr) {
998 es->usbcan.error_state |= USBCAN_ERROR_STATE_TX_ERROR;
999 report_error = true;
1001 if (es->rxerr != priv->bec.rxerr) {
1002 es->usbcan.error_state |= USBCAN_ERROR_STATE_RX_ERROR;
1003 report_error = true;
1005 if ((es->status & M16C_STATE_BUS_ERROR) &&
1006 !(es->usbcan.other_ch_status & M16C_STATE_BUS_ERROR)) {
1007 es->usbcan.error_state |= USBCAN_ERROR_STATE_BUSERROR;
1008 report_error = true;
1011 if (report_error)
1012 kvaser_usb_rx_error(dev, es);
1015 static void kvaser_usbcan_rx_error(const struct kvaser_usb *dev,
1016 const struct kvaser_msg *msg)
1018 struct kvaser_usb_error_summary es = { };
1020 switch (msg->id) {
1021 /* Sometimes errors are sent as unsolicited chip state events */
1022 case CMD_CHIP_STATE_EVENT:
1023 es.channel = msg->u.usbcan.chip_state_event.channel;
1024 es.status = msg->u.usbcan.chip_state_event.status;
1025 es.txerr = msg->u.usbcan.chip_state_event.tx_errors_count;
1026 es.rxerr = msg->u.usbcan.chip_state_event.rx_errors_count;
1027 kvaser_usbcan_conditionally_rx_error(dev, &es);
1028 break;
1030 case CMD_CAN_ERROR_EVENT:
1031 es.channel = 0;
1032 es.status = msg->u.usbcan.error_event.status_ch0;
1033 es.txerr = msg->u.usbcan.error_event.tx_errors_count_ch0;
1034 es.rxerr = msg->u.usbcan.error_event.rx_errors_count_ch0;
1035 es.usbcan.other_ch_status =
1036 msg->u.usbcan.error_event.status_ch1;
1037 kvaser_usbcan_conditionally_rx_error(dev, &es);
1039 /* The USBCAN firmware supports up to 2 channels.
1040 * Now that ch0 was checked, check if ch1 has any errors.
1042 if (dev->nchannels == MAX_USBCAN_NET_DEVICES) {
1043 es.channel = 1;
1044 es.status = msg->u.usbcan.error_event.status_ch1;
1045 es.txerr = msg->u.usbcan.error_event.tx_errors_count_ch1;
1046 es.rxerr = msg->u.usbcan.error_event.rx_errors_count_ch1;
1047 es.usbcan.other_ch_status =
1048 msg->u.usbcan.error_event.status_ch0;
1049 kvaser_usbcan_conditionally_rx_error(dev, &es);
1051 break;
1053 default:
1054 dev_err(dev->udev->dev.parent, "Invalid msg id (%d)\n",
1055 msg->id);
1059 static void kvaser_leaf_rx_error(const struct kvaser_usb *dev,
1060 const struct kvaser_msg *msg)
1062 struct kvaser_usb_error_summary es = { };
1064 switch (msg->id) {
1065 case CMD_CAN_ERROR_EVENT:
1066 es.channel = msg->u.leaf.error_event.channel;
1067 es.status = msg->u.leaf.error_event.status;
1068 es.txerr = msg->u.leaf.error_event.tx_errors_count;
1069 es.rxerr = msg->u.leaf.error_event.rx_errors_count;
1070 es.leaf.error_factor = msg->u.leaf.error_event.error_factor;
1071 break;
1072 case CMD_LEAF_LOG_MESSAGE:
1073 es.channel = msg->u.leaf.log_message.channel;
1074 es.status = msg->u.leaf.log_message.data[0];
1075 es.txerr = msg->u.leaf.log_message.data[2];
1076 es.rxerr = msg->u.leaf.log_message.data[3];
1077 es.leaf.error_factor = msg->u.leaf.log_message.data[1];
1078 break;
1079 case CMD_CHIP_STATE_EVENT:
1080 es.channel = msg->u.leaf.chip_state_event.channel;
1081 es.status = msg->u.leaf.chip_state_event.status;
1082 es.txerr = msg->u.leaf.chip_state_event.tx_errors_count;
1083 es.rxerr = msg->u.leaf.chip_state_event.rx_errors_count;
1084 es.leaf.error_factor = 0;
1085 break;
1086 default:
1087 dev_err(dev->udev->dev.parent, "Invalid msg id (%d)\n",
1088 msg->id);
1089 return;
1092 kvaser_usb_rx_error(dev, &es);
1095 static void kvaser_usb_rx_can_err(const struct kvaser_usb_net_priv *priv,
1096 const struct kvaser_msg *msg)
1098 struct can_frame *cf;
1099 struct sk_buff *skb;
1100 struct net_device_stats *stats = &priv->netdev->stats;
1102 if (msg->u.rx_can_header.flag & (MSG_FLAG_ERROR_FRAME |
1103 MSG_FLAG_NERR)) {
1104 netdev_err(priv->netdev, "Unknown error (flags: 0x%02x)\n",
1105 msg->u.rx_can_header.flag);
1107 stats->rx_errors++;
1108 return;
1111 if (msg->u.rx_can_header.flag & MSG_FLAG_OVERRUN) {
1112 stats->rx_over_errors++;
1113 stats->rx_errors++;
1115 skb = alloc_can_err_skb(priv->netdev, &cf);
1116 if (!skb) {
1117 stats->rx_dropped++;
1118 return;
1121 cf->can_id |= CAN_ERR_CRTL;
1122 cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
1124 stats->rx_packets++;
1125 stats->rx_bytes += cf->can_dlc;
1126 netif_rx(skb);
1130 static void kvaser_usb_rx_can_msg(const struct kvaser_usb *dev,
1131 const struct kvaser_msg *msg)
1133 struct kvaser_usb_net_priv *priv;
1134 struct can_frame *cf;
1135 struct sk_buff *skb;
1136 struct net_device_stats *stats;
1137 u8 channel = msg->u.rx_can_header.channel;
1138 const u8 *rx_msg = NULL; /* GCC */
1140 if (channel >= dev->nchannels) {
1141 dev_err(dev->udev->dev.parent,
1142 "Invalid channel number (%d)\n", channel);
1143 return;
1146 priv = dev->nets[channel];
1147 stats = &priv->netdev->stats;
1149 if ((msg->u.rx_can_header.flag & MSG_FLAG_ERROR_FRAME) &&
1150 (dev->family == KVASER_LEAF && msg->id == CMD_LEAF_LOG_MESSAGE)) {
1151 kvaser_leaf_rx_error(dev, msg);
1152 return;
1153 } else if (msg->u.rx_can_header.flag & (MSG_FLAG_ERROR_FRAME |
1154 MSG_FLAG_NERR |
1155 MSG_FLAG_OVERRUN)) {
1156 kvaser_usb_rx_can_err(priv, msg);
1157 return;
1158 } else if (msg->u.rx_can_header.flag & ~MSG_FLAG_REMOTE_FRAME) {
1159 netdev_warn(priv->netdev,
1160 "Unhandled frame (flags: 0x%02x)",
1161 msg->u.rx_can_header.flag);
1162 return;
1165 switch (dev->family) {
1166 case KVASER_LEAF:
1167 rx_msg = msg->u.leaf.rx_can.msg;
1168 break;
1169 case KVASER_USBCAN:
1170 rx_msg = msg->u.usbcan.rx_can.msg;
1171 break;
1174 skb = alloc_can_skb(priv->netdev, &cf);
1175 if (!skb) {
1176 stats->tx_dropped++;
1177 return;
1180 if (dev->family == KVASER_LEAF && msg->id == CMD_LEAF_LOG_MESSAGE) {
1181 cf->can_id = le32_to_cpu(msg->u.leaf.log_message.id);
1182 if (cf->can_id & KVASER_EXTENDED_FRAME)
1183 cf->can_id &= CAN_EFF_MASK | CAN_EFF_FLAG;
1184 else
1185 cf->can_id &= CAN_SFF_MASK;
1187 cf->can_dlc = get_can_dlc(msg->u.leaf.log_message.dlc);
1189 if (msg->u.leaf.log_message.flags & MSG_FLAG_REMOTE_FRAME)
1190 cf->can_id |= CAN_RTR_FLAG;
1191 else
1192 memcpy(cf->data, &msg->u.leaf.log_message.data,
1193 cf->can_dlc);
1194 } else {
1195 cf->can_id = ((rx_msg[0] & 0x1f) << 6) | (rx_msg[1] & 0x3f);
1197 if (msg->id == CMD_RX_EXT_MESSAGE) {
1198 cf->can_id <<= 18;
1199 cf->can_id |= ((rx_msg[2] & 0x0f) << 14) |
1200 ((rx_msg[3] & 0xff) << 6) |
1201 (rx_msg[4] & 0x3f);
1202 cf->can_id |= CAN_EFF_FLAG;
1205 cf->can_dlc = get_can_dlc(rx_msg[5]);
1207 if (msg->u.rx_can_header.flag & MSG_FLAG_REMOTE_FRAME)
1208 cf->can_id |= CAN_RTR_FLAG;
1209 else
1210 memcpy(cf->data, &rx_msg[6],
1211 cf->can_dlc);
1214 stats->rx_packets++;
1215 stats->rx_bytes += cf->can_dlc;
1216 netif_rx(skb);
1219 static void kvaser_usb_start_chip_reply(const struct kvaser_usb *dev,
1220 const struct kvaser_msg *msg)
1222 struct kvaser_usb_net_priv *priv;
1223 u8 channel = msg->u.simple.channel;
1225 if (channel >= dev->nchannels) {
1226 dev_err(dev->udev->dev.parent,
1227 "Invalid channel number (%d)\n", channel);
1228 return;
1231 priv = dev->nets[channel];
1233 if (completion_done(&priv->start_comp) &&
1234 netif_queue_stopped(priv->netdev)) {
1235 netif_wake_queue(priv->netdev);
1236 } else {
1237 netif_start_queue(priv->netdev);
1238 complete(&priv->start_comp);
1242 static void kvaser_usb_stop_chip_reply(const struct kvaser_usb *dev,
1243 const struct kvaser_msg *msg)
1245 struct kvaser_usb_net_priv *priv;
1246 u8 channel = msg->u.simple.channel;
1248 if (channel >= dev->nchannels) {
1249 dev_err(dev->udev->dev.parent,
1250 "Invalid channel number (%d)\n", channel);
1251 return;
1254 priv = dev->nets[channel];
1256 complete(&priv->stop_comp);
1259 static void kvaser_usb_handle_message(const struct kvaser_usb *dev,
1260 const struct kvaser_msg *msg)
1262 switch (msg->id) {
1263 case CMD_START_CHIP_REPLY:
1264 kvaser_usb_start_chip_reply(dev, msg);
1265 break;
1267 case CMD_STOP_CHIP_REPLY:
1268 kvaser_usb_stop_chip_reply(dev, msg);
1269 break;
1271 case CMD_RX_STD_MESSAGE:
1272 case CMD_RX_EXT_MESSAGE:
1273 kvaser_usb_rx_can_msg(dev, msg);
1274 break;
1276 case CMD_LEAF_LOG_MESSAGE:
1277 if (dev->family != KVASER_LEAF)
1278 goto warn;
1279 kvaser_usb_rx_can_msg(dev, msg);
1280 break;
1282 case CMD_CHIP_STATE_EVENT:
1283 case CMD_CAN_ERROR_EVENT:
1284 if (dev->family == KVASER_LEAF)
1285 kvaser_leaf_rx_error(dev, msg);
1286 else
1287 kvaser_usbcan_rx_error(dev, msg);
1288 break;
1290 case CMD_TX_ACKNOWLEDGE:
1291 kvaser_usb_tx_acknowledge(dev, msg);
1292 break;
1294 /* Ignored messages */
1295 case CMD_USBCAN_CLOCK_OVERFLOW_EVENT:
1296 if (dev->family != KVASER_USBCAN)
1297 goto warn;
1298 break;
1300 default:
1301 warn: dev_warn(dev->udev->dev.parent,
1302 "Unhandled message (%d)\n", msg->id);
1303 break;
1307 static void kvaser_usb_read_bulk_callback(struct urb *urb)
1309 struct kvaser_usb *dev = urb->context;
1310 struct kvaser_msg *msg;
1311 int pos = 0;
1312 int err, i;
1314 switch (urb->status) {
1315 case 0:
1316 break;
1317 case -ENOENT:
1318 case -ESHUTDOWN:
1319 return;
1320 default:
1321 dev_info(dev->udev->dev.parent, "Rx URB aborted (%d)\n",
1322 urb->status);
1323 goto resubmit_urb;
1326 while (pos <= urb->actual_length - MSG_HEADER_LEN) {
1327 msg = urb->transfer_buffer + pos;
1329 /* The Kvaser firmware can only read and write messages that
1330 * does not cross the USB's endpoint wMaxPacketSize boundary.
1331 * If a follow-up command crosses such boundary, firmware puts
1332 * a placeholder zero-length command in its place then aligns
1333 * the real command to the next max packet size.
1335 * Handle such cases or we're going to miss a significant
1336 * number of events in case of a heavy rx load on the bus.
1338 if (msg->len == 0) {
1339 pos = round_up(pos, le16_to_cpu(dev->bulk_in->
1340 wMaxPacketSize));
1341 continue;
1344 if (pos + msg->len > urb->actual_length) {
1345 dev_err(dev->udev->dev.parent, "Format error\n");
1346 break;
1349 kvaser_usb_handle_message(dev, msg);
1350 pos += msg->len;
1353 resubmit_urb:
1354 usb_fill_bulk_urb(urb, dev->udev,
1355 usb_rcvbulkpipe(dev->udev,
1356 dev->bulk_in->bEndpointAddress),
1357 urb->transfer_buffer, RX_BUFFER_SIZE,
1358 kvaser_usb_read_bulk_callback, dev);
1360 err = usb_submit_urb(urb, GFP_ATOMIC);
1361 if (err == -ENODEV) {
1362 for (i = 0; i < dev->nchannels; i++) {
1363 if (!dev->nets[i])
1364 continue;
1366 netif_device_detach(dev->nets[i]->netdev);
1368 } else if (err) {
1369 dev_err(dev->udev->dev.parent,
1370 "Failed resubmitting read bulk urb: %d\n", err);
1373 return;
1376 static int kvaser_usb_setup_rx_urbs(struct kvaser_usb *dev)
1378 int i, err = 0;
1380 if (dev->rxinitdone)
1381 return 0;
1383 for (i = 0; i < MAX_RX_URBS; i++) {
1384 struct urb *urb = NULL;
1385 u8 *buf = NULL;
1386 dma_addr_t buf_dma;
1388 urb = usb_alloc_urb(0, GFP_KERNEL);
1389 if (!urb) {
1390 dev_warn(dev->udev->dev.parent,
1391 "No memory left for URBs\n");
1392 err = -ENOMEM;
1393 break;
1396 buf = usb_alloc_coherent(dev->udev, RX_BUFFER_SIZE,
1397 GFP_KERNEL, &buf_dma);
1398 if (!buf) {
1399 dev_warn(dev->udev->dev.parent,
1400 "No memory left for USB buffer\n");
1401 usb_free_urb(urb);
1402 err = -ENOMEM;
1403 break;
1406 usb_fill_bulk_urb(urb, dev->udev,
1407 usb_rcvbulkpipe(dev->udev,
1408 dev->bulk_in->bEndpointAddress),
1409 buf, RX_BUFFER_SIZE,
1410 kvaser_usb_read_bulk_callback,
1411 dev);
1412 urb->transfer_dma = buf_dma;
1413 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1414 usb_anchor_urb(urb, &dev->rx_submitted);
1416 err = usb_submit_urb(urb, GFP_KERNEL);
1417 if (err) {
1418 usb_unanchor_urb(urb);
1419 usb_free_coherent(dev->udev, RX_BUFFER_SIZE, buf,
1420 buf_dma);
1421 usb_free_urb(urb);
1422 break;
1425 dev->rxbuf[i] = buf;
1426 dev->rxbuf_dma[i] = buf_dma;
1428 usb_free_urb(urb);
1431 if (i == 0) {
1432 dev_warn(dev->udev->dev.parent,
1433 "Cannot setup read URBs, error %d\n", err);
1434 return err;
1435 } else if (i < MAX_RX_URBS) {
1436 dev_warn(dev->udev->dev.parent,
1437 "RX performances may be slow\n");
1440 dev->rxinitdone = true;
1442 return 0;
1445 static int kvaser_usb_set_opt_mode(const struct kvaser_usb_net_priv *priv)
1447 struct kvaser_msg *msg;
1448 int rc;
1450 msg = kmalloc(sizeof(*msg), GFP_KERNEL);
1451 if (!msg)
1452 return -ENOMEM;
1454 msg->id = CMD_SET_CTRL_MODE;
1455 msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_ctrl_mode);
1456 msg->u.ctrl_mode.tid = 0xff;
1457 msg->u.ctrl_mode.channel = priv->channel;
1459 if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
1460 msg->u.ctrl_mode.ctrl_mode = KVASER_CTRL_MODE_SILENT;
1461 else
1462 msg->u.ctrl_mode.ctrl_mode = KVASER_CTRL_MODE_NORMAL;
1464 rc = kvaser_usb_send_msg(priv->dev, msg);
1466 kfree(msg);
1467 return rc;
1470 static int kvaser_usb_start_chip(struct kvaser_usb_net_priv *priv)
1472 int err;
1474 init_completion(&priv->start_comp);
1476 err = kvaser_usb_send_simple_msg(priv->dev, CMD_START_CHIP,
1477 priv->channel);
1478 if (err)
1479 return err;
1481 if (!wait_for_completion_timeout(&priv->start_comp,
1482 msecs_to_jiffies(START_TIMEOUT)))
1483 return -ETIMEDOUT;
1485 return 0;
1488 static int kvaser_usb_open(struct net_device *netdev)
1490 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1491 struct kvaser_usb *dev = priv->dev;
1492 int err;
1494 err = open_candev(netdev);
1495 if (err)
1496 return err;
1498 err = kvaser_usb_setup_rx_urbs(dev);
1499 if (err)
1500 goto error;
1502 err = kvaser_usb_set_opt_mode(priv);
1503 if (err)
1504 goto error;
1506 err = kvaser_usb_start_chip(priv);
1507 if (err) {
1508 netdev_warn(netdev, "Cannot start device, error %d\n", err);
1509 goto error;
1512 priv->can.state = CAN_STATE_ERROR_ACTIVE;
1514 return 0;
1516 error:
1517 close_candev(netdev);
1518 return err;
1521 static void kvaser_usb_reset_tx_urb_contexts(struct kvaser_usb_net_priv *priv)
1523 int i, max_tx_urbs;
1525 max_tx_urbs = priv->dev->max_tx_urbs;
1527 priv->active_tx_contexts = 0;
1528 for (i = 0; i < max_tx_urbs; i++)
1529 priv->tx_contexts[i].echo_index = max_tx_urbs;
1532 /* This method might sleep. Do not call it in the atomic context
1533 * of URB completions.
1535 static void kvaser_usb_unlink_tx_urbs(struct kvaser_usb_net_priv *priv)
1537 usb_kill_anchored_urbs(&priv->tx_submitted);
1538 kvaser_usb_reset_tx_urb_contexts(priv);
1541 static void kvaser_usb_unlink_all_urbs(struct kvaser_usb *dev)
1543 int i;
1545 usb_kill_anchored_urbs(&dev->rx_submitted);
1547 for (i = 0; i < MAX_RX_URBS; i++)
1548 usb_free_coherent(dev->udev, RX_BUFFER_SIZE,
1549 dev->rxbuf[i],
1550 dev->rxbuf_dma[i]);
1552 for (i = 0; i < dev->nchannels; i++) {
1553 struct kvaser_usb_net_priv *priv = dev->nets[i];
1555 if (priv)
1556 kvaser_usb_unlink_tx_urbs(priv);
1560 static int kvaser_usb_stop_chip(struct kvaser_usb_net_priv *priv)
1562 int err;
1564 init_completion(&priv->stop_comp);
1566 err = kvaser_usb_send_simple_msg(priv->dev, CMD_STOP_CHIP,
1567 priv->channel);
1568 if (err)
1569 return err;
1571 if (!wait_for_completion_timeout(&priv->stop_comp,
1572 msecs_to_jiffies(STOP_TIMEOUT)))
1573 return -ETIMEDOUT;
1575 return 0;
1578 static int kvaser_usb_flush_queue(struct kvaser_usb_net_priv *priv)
1580 struct kvaser_msg *msg;
1581 int rc;
1583 msg = kmalloc(sizeof(*msg), GFP_KERNEL);
1584 if (!msg)
1585 return -ENOMEM;
1587 msg->id = CMD_FLUSH_QUEUE;
1588 msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_flush_queue);
1589 msg->u.flush_queue.channel = priv->channel;
1590 msg->u.flush_queue.flags = 0x00;
1592 rc = kvaser_usb_send_msg(priv->dev, msg);
1594 kfree(msg);
1595 return rc;
1598 static int kvaser_usb_close(struct net_device *netdev)
1600 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1601 struct kvaser_usb *dev = priv->dev;
1602 int err;
1604 netif_stop_queue(netdev);
1606 err = kvaser_usb_flush_queue(priv);
1607 if (err)
1608 netdev_warn(netdev, "Cannot flush queue, error %d\n", err);
1610 if (kvaser_usb_send_simple_msg(dev, CMD_RESET_CHIP, priv->channel))
1611 netdev_warn(netdev, "Cannot reset card, error %d\n", err);
1613 err = kvaser_usb_stop_chip(priv);
1614 if (err)
1615 netdev_warn(netdev, "Cannot stop device, error %d\n", err);
1617 /* reset tx contexts */
1618 kvaser_usb_unlink_tx_urbs(priv);
1620 priv->can.state = CAN_STATE_STOPPED;
1621 close_candev(priv->netdev);
1623 return 0;
1626 static void kvaser_usb_write_bulk_callback(struct urb *urb)
1628 struct kvaser_usb_tx_urb_context *context = urb->context;
1629 struct kvaser_usb_net_priv *priv;
1630 struct net_device *netdev;
1632 if (WARN_ON(!context))
1633 return;
1635 priv = context->priv;
1636 netdev = priv->netdev;
1638 kfree(urb->transfer_buffer);
1640 if (!netif_device_present(netdev))
1641 return;
1643 if (urb->status)
1644 netdev_info(netdev, "Tx URB aborted (%d)\n", urb->status);
1647 static netdev_tx_t kvaser_usb_start_xmit(struct sk_buff *skb,
1648 struct net_device *netdev)
1650 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1651 struct kvaser_usb *dev = priv->dev;
1652 struct net_device_stats *stats = &netdev->stats;
1653 struct can_frame *cf = (struct can_frame *)skb->data;
1654 struct kvaser_usb_tx_urb_context *context = NULL;
1655 struct urb *urb;
1656 void *buf;
1657 struct kvaser_msg *msg;
1658 int i, err, ret = NETDEV_TX_OK;
1659 u8 *msg_tx_can_flags = NULL; /* GCC */
1660 unsigned long flags;
1662 if (can_dropped_invalid_skb(netdev, skb))
1663 return NETDEV_TX_OK;
1665 urb = usb_alloc_urb(0, GFP_ATOMIC);
1666 if (!urb) {
1667 netdev_err(netdev, "No memory left for URBs\n");
1668 stats->tx_dropped++;
1669 dev_kfree_skb(skb);
1670 return NETDEV_TX_OK;
1673 buf = kmalloc(sizeof(struct kvaser_msg), GFP_ATOMIC);
1674 if (!buf) {
1675 stats->tx_dropped++;
1676 dev_kfree_skb(skb);
1677 goto freeurb;
1680 msg = buf;
1681 msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_tx_can);
1682 msg->u.tx_can.channel = priv->channel;
1684 switch (dev->family) {
1685 case KVASER_LEAF:
1686 msg_tx_can_flags = &msg->u.tx_can.leaf.flags;
1687 break;
1688 case KVASER_USBCAN:
1689 msg_tx_can_flags = &msg->u.tx_can.usbcan.flags;
1690 break;
1693 *msg_tx_can_flags = 0;
1695 if (cf->can_id & CAN_EFF_FLAG) {
1696 msg->id = CMD_TX_EXT_MESSAGE;
1697 msg->u.tx_can.msg[0] = (cf->can_id >> 24) & 0x1f;
1698 msg->u.tx_can.msg[1] = (cf->can_id >> 18) & 0x3f;
1699 msg->u.tx_can.msg[2] = (cf->can_id >> 14) & 0x0f;
1700 msg->u.tx_can.msg[3] = (cf->can_id >> 6) & 0xff;
1701 msg->u.tx_can.msg[4] = cf->can_id & 0x3f;
1702 } else {
1703 msg->id = CMD_TX_STD_MESSAGE;
1704 msg->u.tx_can.msg[0] = (cf->can_id >> 6) & 0x1f;
1705 msg->u.tx_can.msg[1] = cf->can_id & 0x3f;
1708 msg->u.tx_can.msg[5] = cf->can_dlc;
1709 memcpy(&msg->u.tx_can.msg[6], cf->data, cf->can_dlc);
1711 if (cf->can_id & CAN_RTR_FLAG)
1712 *msg_tx_can_flags |= MSG_FLAG_REMOTE_FRAME;
1714 spin_lock_irqsave(&priv->tx_contexts_lock, flags);
1715 for (i = 0; i < dev->max_tx_urbs; i++) {
1716 if (priv->tx_contexts[i].echo_index == dev->max_tx_urbs) {
1717 context = &priv->tx_contexts[i];
1719 context->echo_index = i;
1720 can_put_echo_skb(skb, netdev, context->echo_index);
1721 ++priv->active_tx_contexts;
1722 if (priv->active_tx_contexts >= dev->max_tx_urbs)
1723 netif_stop_queue(netdev);
1725 break;
1728 spin_unlock_irqrestore(&priv->tx_contexts_lock, flags);
1730 /* This should never happen; it implies a flow control bug */
1731 if (!context) {
1732 netdev_warn(netdev, "cannot find free context\n");
1734 kfree(buf);
1735 ret = NETDEV_TX_BUSY;
1736 goto freeurb;
1739 context->priv = priv;
1740 context->dlc = cf->can_dlc;
1742 msg->u.tx_can.tid = context->echo_index;
1744 usb_fill_bulk_urb(urb, dev->udev,
1745 usb_sndbulkpipe(dev->udev,
1746 dev->bulk_out->bEndpointAddress),
1747 buf, msg->len,
1748 kvaser_usb_write_bulk_callback, context);
1749 usb_anchor_urb(urb, &priv->tx_submitted);
1751 err = usb_submit_urb(urb, GFP_ATOMIC);
1752 if (unlikely(err)) {
1753 spin_lock_irqsave(&priv->tx_contexts_lock, flags);
1755 can_free_echo_skb(netdev, context->echo_index);
1756 context->echo_index = dev->max_tx_urbs;
1757 --priv->active_tx_contexts;
1758 netif_wake_queue(netdev);
1760 spin_unlock_irqrestore(&priv->tx_contexts_lock, flags);
1762 usb_unanchor_urb(urb);
1764 stats->tx_dropped++;
1766 if (err == -ENODEV)
1767 netif_device_detach(netdev);
1768 else
1769 netdev_warn(netdev, "Failed tx_urb %d\n", err);
1771 goto freeurb;
1774 ret = NETDEV_TX_OK;
1776 freeurb:
1777 usb_free_urb(urb);
1778 return ret;
1781 static const struct net_device_ops kvaser_usb_netdev_ops = {
1782 .ndo_open = kvaser_usb_open,
1783 .ndo_stop = kvaser_usb_close,
1784 .ndo_start_xmit = kvaser_usb_start_xmit,
1785 .ndo_change_mtu = can_change_mtu,
1788 static const struct can_bittiming_const kvaser_usb_bittiming_const = {
1789 .name = "kvaser_usb",
1790 .tseg1_min = KVASER_USB_TSEG1_MIN,
1791 .tseg1_max = KVASER_USB_TSEG1_MAX,
1792 .tseg2_min = KVASER_USB_TSEG2_MIN,
1793 .tseg2_max = KVASER_USB_TSEG2_MAX,
1794 .sjw_max = KVASER_USB_SJW_MAX,
1795 .brp_min = KVASER_USB_BRP_MIN,
1796 .brp_max = KVASER_USB_BRP_MAX,
1797 .brp_inc = KVASER_USB_BRP_INC,
1800 static int kvaser_usb_set_bittiming(struct net_device *netdev)
1802 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1803 struct can_bittiming *bt = &priv->can.bittiming;
1804 struct kvaser_usb *dev = priv->dev;
1805 struct kvaser_msg *msg;
1806 int rc;
1808 msg = kmalloc(sizeof(*msg), GFP_KERNEL);
1809 if (!msg)
1810 return -ENOMEM;
1812 msg->id = CMD_SET_BUS_PARAMS;
1813 msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_busparams);
1814 msg->u.busparams.channel = priv->channel;
1815 msg->u.busparams.tid = 0xff;
1816 msg->u.busparams.bitrate = cpu_to_le32(bt->bitrate);
1817 msg->u.busparams.sjw = bt->sjw;
1818 msg->u.busparams.tseg1 = bt->prop_seg + bt->phase_seg1;
1819 msg->u.busparams.tseg2 = bt->phase_seg2;
1821 if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
1822 msg->u.busparams.no_samp = 3;
1823 else
1824 msg->u.busparams.no_samp = 1;
1826 rc = kvaser_usb_send_msg(dev, msg);
1828 kfree(msg);
1829 return rc;
1832 static int kvaser_usb_set_mode(struct net_device *netdev,
1833 enum can_mode mode)
1835 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1836 int err;
1838 switch (mode) {
1839 case CAN_MODE_START:
1840 err = kvaser_usb_simple_msg_async(priv, CMD_START_CHIP);
1841 if (err)
1842 return err;
1843 break;
1844 default:
1845 return -EOPNOTSUPP;
1848 return 0;
1851 static int kvaser_usb_get_berr_counter(const struct net_device *netdev,
1852 struct can_berr_counter *bec)
1854 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1856 *bec = priv->bec;
1858 return 0;
1861 static void kvaser_usb_remove_interfaces(struct kvaser_usb *dev)
1863 int i;
1865 for (i = 0; i < dev->nchannels; i++) {
1866 if (!dev->nets[i])
1867 continue;
1869 unregister_candev(dev->nets[i]->netdev);
1872 kvaser_usb_unlink_all_urbs(dev);
1874 for (i = 0; i < dev->nchannels; i++) {
1875 if (!dev->nets[i])
1876 continue;
1878 free_candev(dev->nets[i]->netdev);
1882 static int kvaser_usb_init_one(struct usb_interface *intf,
1883 const struct usb_device_id *id, int channel)
1885 struct kvaser_usb *dev = usb_get_intfdata(intf);
1886 struct net_device *netdev;
1887 struct kvaser_usb_net_priv *priv;
1888 int err;
1890 err = kvaser_usb_send_simple_msg(dev, CMD_RESET_CHIP, channel);
1891 if (err)
1892 return err;
1894 netdev = alloc_candev(sizeof(*priv) +
1895 dev->max_tx_urbs * sizeof(*priv->tx_contexts),
1896 dev->max_tx_urbs);
1897 if (!netdev) {
1898 dev_err(&intf->dev, "Cannot alloc candev\n");
1899 return -ENOMEM;
1902 priv = netdev_priv(netdev);
1904 init_usb_anchor(&priv->tx_submitted);
1905 init_completion(&priv->start_comp);
1906 init_completion(&priv->stop_comp);
1908 priv->dev = dev;
1909 priv->netdev = netdev;
1910 priv->channel = channel;
1912 spin_lock_init(&priv->tx_contexts_lock);
1913 kvaser_usb_reset_tx_urb_contexts(priv);
1915 priv->can.state = CAN_STATE_STOPPED;
1916 priv->can.clock.freq = CAN_USB_CLOCK;
1917 priv->can.bittiming_const = &kvaser_usb_bittiming_const;
1918 priv->can.do_set_bittiming = kvaser_usb_set_bittiming;
1919 priv->can.do_set_mode = kvaser_usb_set_mode;
1920 if (id->driver_info & KVASER_HAS_TXRX_ERRORS)
1921 priv->can.do_get_berr_counter = kvaser_usb_get_berr_counter;
1922 priv->can.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES;
1923 if (id->driver_info & KVASER_HAS_SILENT_MODE)
1924 priv->can.ctrlmode_supported |= CAN_CTRLMODE_LISTENONLY;
1926 netdev->flags |= IFF_ECHO;
1928 netdev->netdev_ops = &kvaser_usb_netdev_ops;
1930 SET_NETDEV_DEV(netdev, &intf->dev);
1931 netdev->dev_id = channel;
1933 dev->nets[channel] = priv;
1935 err = register_candev(netdev);
1936 if (err) {
1937 dev_err(&intf->dev, "Failed to register can device\n");
1938 free_candev(netdev);
1939 dev->nets[channel] = NULL;
1940 return err;
1943 netdev_dbg(netdev, "device registered\n");
1945 return 0;
1948 static int kvaser_usb_get_endpoints(const struct usb_interface *intf,
1949 struct usb_endpoint_descriptor **in,
1950 struct usb_endpoint_descriptor **out)
1952 const struct usb_host_interface *iface_desc;
1953 struct usb_endpoint_descriptor *endpoint;
1954 int i;
1956 iface_desc = &intf->altsetting[0];
1958 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1959 endpoint = &iface_desc->endpoint[i].desc;
1961 if (!*in && usb_endpoint_is_bulk_in(endpoint))
1962 *in = endpoint;
1964 if (!*out && usb_endpoint_is_bulk_out(endpoint))
1965 *out = endpoint;
1967 /* use first bulk endpoint for in and out */
1968 if (*in && *out)
1969 return 0;
1972 return -ENODEV;
1975 static int kvaser_usb_probe(struct usb_interface *intf,
1976 const struct usb_device_id *id)
1978 struct kvaser_usb *dev;
1979 int err = -ENOMEM;
1980 int i, retry = 3;
1982 dev = devm_kzalloc(&intf->dev, sizeof(*dev), GFP_KERNEL);
1983 if (!dev)
1984 return -ENOMEM;
1986 if (kvaser_is_leaf(id)) {
1987 dev->family = KVASER_LEAF;
1988 } else if (kvaser_is_usbcan(id)) {
1989 dev->family = KVASER_USBCAN;
1990 } else {
1991 dev_err(&intf->dev,
1992 "Product ID (%d) does not belong to any known Kvaser USB family",
1993 id->idProduct);
1994 return -ENODEV;
1997 err = kvaser_usb_get_endpoints(intf, &dev->bulk_in, &dev->bulk_out);
1998 if (err) {
1999 dev_err(&intf->dev, "Cannot get usb endpoint(s)");
2000 return err;
2003 dev->udev = interface_to_usbdev(intf);
2005 init_usb_anchor(&dev->rx_submitted);
2007 usb_set_intfdata(intf, dev);
2009 /* On some x86 laptops, plugging a Kvaser device again after
2010 * an unplug makes the firmware always ignore the very first
2011 * command. For such a case, provide some room for retries
2012 * instead of completely exiting the driver.
2014 do {
2015 err = kvaser_usb_get_software_info(dev);
2016 } while (--retry && err == -ETIMEDOUT);
2018 if (err) {
2019 dev_err(&intf->dev,
2020 "Cannot get software infos, error %d\n", err);
2021 return err;
2024 dev_dbg(&intf->dev, "Firmware version: %d.%d.%d\n",
2025 ((dev->fw_version >> 24) & 0xff),
2026 ((dev->fw_version >> 16) & 0xff),
2027 (dev->fw_version & 0xffff));
2029 dev_dbg(&intf->dev, "Max oustanding tx = %d URBs\n", dev->max_tx_urbs);
2031 err = kvaser_usb_get_card_info(dev);
2032 if (err) {
2033 dev_err(&intf->dev,
2034 "Cannot get card infos, error %d\n", err);
2035 return err;
2038 for (i = 0; i < dev->nchannels; i++) {
2039 err = kvaser_usb_init_one(intf, id, i);
2040 if (err) {
2041 kvaser_usb_remove_interfaces(dev);
2042 return err;
2046 return 0;
2049 static void kvaser_usb_disconnect(struct usb_interface *intf)
2051 struct kvaser_usb *dev = usb_get_intfdata(intf);
2053 usb_set_intfdata(intf, NULL);
2055 if (!dev)
2056 return;
2058 kvaser_usb_remove_interfaces(dev);
2061 static struct usb_driver kvaser_usb_driver = {
2062 .name = "kvaser_usb",
2063 .probe = kvaser_usb_probe,
2064 .disconnect = kvaser_usb_disconnect,
2065 .id_table = kvaser_usb_table,
2068 module_usb_driver(kvaser_usb_driver);
2070 MODULE_AUTHOR("Olivier Sobrie <olivier@sobrie.be>");
2071 MODULE_DESCRIPTION("CAN driver for Kvaser CAN/USB devices");
2072 MODULE_LICENSE("GPL v2");