1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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>
23
24#include <linux/can.h>
25#include <linux/can/dev.h>
26#include <linux/can/error.h>
27
28#define MAX_RX_URBS 4
29#define START_TIMEOUT 1000
30#define STOP_TIMEOUT 1000
31#define USB_SEND_TIMEOUT 1000
32#define USB_RECV_TIMEOUT 1000
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
37
38
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
62#define USB_LEAF_LIGHT_HS_V2_OEM_PRODUCT_ID 290
63#define USB_USBCAN_LIGHT_2HS_PRODUCT_ID 291
64#define USB_MINI_PCIE_2HS_PRODUCT_ID 292
65
66static inline bool kvaser_is_leaf(const struct usb_device_id *id)
67{
68 return id->idProduct >= USB_LEAF_DEVEL_PRODUCT_ID &&
69 id->idProduct <= USB_MINI_PCIE_2HS_PRODUCT_ID;
70}
71
72
73#define USB_USBCAN_REVB_PRODUCT_ID 2
74#define USB_VCI2_PRODUCT_ID 3
75#define USB_USBCAN2_PRODUCT_ID 4
76#define USB_MEMORATOR_PRODUCT_ID 5
77
78static inline bool kvaser_is_usbcan(const struct usb_device_id *id)
79{
80 return id->idProduct >= USB_USBCAN_REVB_PRODUCT_ID &&
81 id->idProduct <= USB_MEMORATOR_PRODUCT_ID;
82}
83
84
85#define KVASER_HAS_SILENT_MODE BIT(0)
86#define KVASER_HAS_TXRX_ERRORS BIT(1)
87
88
89#define MSG_HEADER_LEN 2
90
91
92#define MSG_FLAG_ERROR_FRAME BIT(0)
93#define MSG_FLAG_OVERRUN BIT(1)
94#define MSG_FLAG_NERR BIT(2)
95#define MSG_FLAG_WAKEUP BIT(3)
96#define MSG_FLAG_REMOTE_FRAME BIT(4)
97#define MSG_FLAG_RESERVED BIT(5)
98#define MSG_FLAG_TX_ACK BIT(6)
99#define MSG_FLAG_TX_REQUEST BIT(7)
100
101
102#define M16C_STATE_BUS_RESET BIT(0)
103#define M16C_STATE_BUS_ERROR BIT(4)
104#define M16C_STATE_BUS_PASSIVE BIT(5)
105#define M16C_STATE_BUS_OFF BIT(6)
106
107
108#define CMD_RX_STD_MESSAGE 12
109#define CMD_TX_STD_MESSAGE 13
110#define CMD_RX_EXT_MESSAGE 14
111#define CMD_TX_EXT_MESSAGE 15
112#define CMD_SET_BUS_PARAMS 16
113#define CMD_GET_BUS_PARAMS 17
114#define CMD_GET_BUS_PARAMS_REPLY 18
115#define CMD_GET_CHIP_STATE 19
116#define CMD_CHIP_STATE_EVENT 20
117#define CMD_SET_CTRL_MODE 21
118#define CMD_GET_CTRL_MODE 22
119#define CMD_GET_CTRL_MODE_REPLY 23
120#define CMD_RESET_CHIP 24
121#define CMD_RESET_CARD 25
122#define CMD_START_CHIP 26
123#define CMD_START_CHIP_REPLY 27
124#define CMD_STOP_CHIP 28
125#define CMD_STOP_CHIP_REPLY 29
126
127#define CMD_LEAF_GET_CARD_INFO2 32
128#define CMD_USBCAN_RESET_CLOCK 32
129#define CMD_USBCAN_CLOCK_OVERFLOW_EVENT 33
130
131#define CMD_GET_CARD_INFO 34
132#define CMD_GET_CARD_INFO_REPLY 35
133#define CMD_GET_SOFTWARE_INFO 38
134#define CMD_GET_SOFTWARE_INFO_REPLY 39
135#define CMD_ERROR_EVENT 45
136#define CMD_FLUSH_QUEUE 48
137#define CMD_RESET_ERROR_COUNTER 49
138#define CMD_TX_ACKNOWLEDGE 50
139#define CMD_CAN_ERROR_EVENT 51
140#define CMD_FLUSH_QUEUE_REPLY 68
141
142#define CMD_LEAF_USB_THROTTLE 77
143#define CMD_LEAF_LOG_MESSAGE 106
144
145
146#define M16C_EF_ACKE BIT(0)
147#define M16C_EF_CRCE BIT(1)
148#define M16C_EF_FORME BIT(2)
149#define M16C_EF_STFE BIT(3)
150#define M16C_EF_BITE0 BIT(4)
151#define M16C_EF_BITE1 BIT(5)
152#define M16C_EF_RCVE BIT(6)
153#define M16C_EF_TRE BIT(7)
154
155
156
157
158#define USBCAN_ERROR_STATE_NONE 0
159#define USBCAN_ERROR_STATE_TX_ERROR BIT(0)
160#define USBCAN_ERROR_STATE_RX_ERROR BIT(1)
161#define USBCAN_ERROR_STATE_BUSERROR BIT(2)
162
163
164#define KVASER_USB_TSEG1_MIN 1
165#define KVASER_USB_TSEG1_MAX 16
166#define KVASER_USB_TSEG2_MIN 1
167#define KVASER_USB_TSEG2_MAX 8
168#define KVASER_USB_SJW_MAX 4
169#define KVASER_USB_BRP_MIN 1
170#define KVASER_USB_BRP_MAX 64
171#define KVASER_USB_BRP_INC 1
172
173
174#define KVASER_CTRL_MODE_NORMAL 1
175#define KVASER_CTRL_MODE_SILENT 2
176#define KVASER_CTRL_MODE_SELFRECEPTION 3
177#define KVASER_CTRL_MODE_OFF 4
178
179
180#define KVASER_EXTENDED_FRAME BIT(31)
181
182
183
184
185
186enum kvaser_usb_family {
187 KVASER_LEAF,
188 KVASER_USBCAN,
189};
190
191struct kvaser_msg_simple {
192 u8 tid;
193 u8 channel;
194} __packed;
195
196struct kvaser_msg_cardinfo {
197 u8 tid;
198 u8 nchannels;
199 union {
200 struct {
201 __le32 serial_number;
202 __le32 padding;
203 } __packed leaf0;
204 struct {
205 __le32 serial_number_low;
206 __le32 serial_number_high;
207 } __packed usbcan0;
208 } __packed;
209 __le32 clock_resolution;
210 __le32 mfgdate;
211 u8 ean[8];
212 u8 hw_revision;
213 union {
214 struct {
215 u8 usb_hs_mode;
216 } __packed leaf1;
217 struct {
218 u8 padding;
219 } __packed usbcan1;
220 } __packed;
221 __le16 padding;
222} __packed;
223
224struct kvaser_msg_cardinfo2 {
225 u8 tid;
226 u8 reserved;
227 u8 pcb_id[24];
228 __le32 oem_unlock_code;
229} __packed;
230
231struct leaf_msg_softinfo {
232 u8 tid;
233 u8 padding0;
234 __le32 sw_options;
235 __le32 fw_version;
236 __le16 max_outstanding_tx;
237 __le16 padding1[9];
238} __packed;
239
240struct usbcan_msg_softinfo {
241 u8 tid;
242 u8 fw_name[5];
243 __le16 max_outstanding_tx;
244 u8 padding[6];
245 __le32 fw_version;
246 __le16 checksum;
247 __le16 sw_options;
248} __packed;
249
250struct kvaser_msg_busparams {
251 u8 tid;
252 u8 channel;
253 __le32 bitrate;
254 u8 tseg1;
255 u8 tseg2;
256 u8 sjw;
257 u8 no_samp;
258} __packed;
259
260struct kvaser_msg_tx_can {
261 u8 channel;
262 u8 tid;
263 u8 msg[14];
264 union {
265 struct {
266 u8 padding;
267 u8 flags;
268 } __packed leaf;
269 struct {
270 u8 flags;
271 u8 padding;
272 } __packed usbcan;
273 } __packed;
274} __packed;
275
276struct kvaser_msg_rx_can_header {
277 u8 channel;
278 u8 flag;
279} __packed;
280
281struct leaf_msg_rx_can {
282 u8 channel;
283 u8 flag;
284
285 __le16 time[3];
286 u8 msg[14];
287} __packed;
288
289struct usbcan_msg_rx_can {
290 u8 channel;
291 u8 flag;
292
293 u8 msg[14];
294 __le16 time;
295} __packed;
296
297struct leaf_msg_chip_state_event {
298 u8 tid;
299 u8 channel;
300
301 __le16 time[3];
302 u8 tx_errors_count;
303 u8 rx_errors_count;
304
305 u8 status;
306 u8 padding[3];
307} __packed;
308
309struct usbcan_msg_chip_state_event {
310 u8 tid;
311 u8 channel;
312
313 u8 tx_errors_count;
314 u8 rx_errors_count;
315 __le16 time;
316
317 u8 status;
318 u8 padding[3];
319} __packed;
320
321struct kvaser_msg_tx_acknowledge_header {
322 u8 channel;
323 u8 tid;
324} __packed;
325
326struct leaf_msg_tx_acknowledge {
327 u8 channel;
328 u8 tid;
329
330 __le16 time[3];
331 u8 flags;
332 u8 time_offset;
333} __packed;
334
335struct usbcan_msg_tx_acknowledge {
336 u8 channel;
337 u8 tid;
338
339 __le16 time;
340 __le16 padding;
341} __packed;
342
343struct leaf_msg_error_event {
344 u8 tid;
345 u8 flags;
346 __le16 time[3];
347 u8 channel;
348 u8 padding;
349 u8 tx_errors_count;
350 u8 rx_errors_count;
351 u8 status;
352 u8 error_factor;
353} __packed;
354
355struct usbcan_msg_error_event {
356 u8 tid;
357 u8 padding;
358 u8 tx_errors_count_ch0;
359 u8 rx_errors_count_ch0;
360 u8 tx_errors_count_ch1;
361 u8 rx_errors_count_ch1;
362 u8 status_ch0;
363 u8 status_ch1;
364 __le16 time;
365} __packed;
366
367struct kvaser_msg_ctrl_mode {
368 u8 tid;
369 u8 channel;
370 u8 ctrl_mode;
371 u8 padding[3];
372} __packed;
373
374struct kvaser_msg_flush_queue {
375 u8 tid;
376 u8 channel;
377 u8 flags;
378 u8 padding[3];
379} __packed;
380
381struct leaf_msg_log_message {
382 u8 channel;
383 u8 flags;
384 __le16 time[3];
385 u8 dlc;
386 u8 time_offset;
387 __le32 id;
388 u8 data[8];
389} __packed;
390
391struct kvaser_msg {
392 u8 len;
393 u8 id;
394 union {
395 struct kvaser_msg_simple simple;
396 struct kvaser_msg_cardinfo cardinfo;
397 struct kvaser_msg_cardinfo2 cardinfo2;
398 struct kvaser_msg_busparams busparams;
399
400 struct kvaser_msg_rx_can_header rx_can_header;
401 struct kvaser_msg_tx_acknowledge_header tx_acknowledge_header;
402
403 union {
404 struct leaf_msg_softinfo softinfo;
405 struct leaf_msg_rx_can rx_can;
406 struct leaf_msg_chip_state_event chip_state_event;
407 struct leaf_msg_tx_acknowledge tx_acknowledge;
408 struct leaf_msg_error_event error_event;
409 struct leaf_msg_log_message log_message;
410 } __packed leaf;
411
412 union {
413 struct usbcan_msg_softinfo softinfo;
414 struct usbcan_msg_rx_can rx_can;
415 struct usbcan_msg_chip_state_event chip_state_event;
416 struct usbcan_msg_tx_acknowledge tx_acknowledge;
417 struct usbcan_msg_error_event error_event;
418 } __packed usbcan;
419
420 struct kvaser_msg_tx_can tx_can;
421 struct kvaser_msg_ctrl_mode ctrl_mode;
422 struct kvaser_msg_flush_queue flush_queue;
423 } u;
424} __packed;
425
426
427
428
429
430
431
432
433
434
435
436struct kvaser_usb_error_summary {
437 u8 channel, status, txerr, rxerr;
438 union {
439 struct {
440 u8 error_factor;
441 } leaf;
442 struct {
443 u8 other_ch_status;
444 u8 error_state;
445 } usbcan;
446 };
447};
448
449
450struct kvaser_usb_tx_urb_context {
451 struct kvaser_usb_net_priv *priv;
452 u32 echo_index;
453 int dlc;
454};
455
456struct kvaser_usb {
457 struct usb_device *udev;
458 struct kvaser_usb_net_priv *nets[MAX_NET_DEVICES];
459
460 struct usb_endpoint_descriptor *bulk_in, *bulk_out;
461 struct usb_anchor rx_submitted;
462
463
464
465
466
467 u32 fw_version;
468 unsigned int nchannels;
469 unsigned int max_tx_urbs;
470 enum kvaser_usb_family family;
471
472 bool rxinitdone;
473 void *rxbuf[MAX_RX_URBS];
474 dma_addr_t rxbuf_dma[MAX_RX_URBS];
475};
476
477struct kvaser_usb_net_priv {
478 struct can_priv can;
479 struct can_berr_counter bec;
480
481 struct kvaser_usb *dev;
482 struct net_device *netdev;
483 int channel;
484
485 struct completion start_comp, stop_comp;
486 struct usb_anchor tx_submitted;
487
488 spinlock_t tx_contexts_lock;
489 int active_tx_contexts;
490 struct kvaser_usb_tx_urb_context tx_contexts[];
491};
492
493static const struct usb_device_id kvaser_usb_table[] = {
494
495 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_DEVEL_PRODUCT_ID) },
496 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_PRODUCT_ID) },
497 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_PRODUCT_ID),
498 .driver_info = KVASER_HAS_TXRX_ERRORS |
499 KVASER_HAS_SILENT_MODE },
500 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_SPRO_PRODUCT_ID),
501 .driver_info = KVASER_HAS_TXRX_ERRORS |
502 KVASER_HAS_SILENT_MODE },
503 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_LS_PRODUCT_ID),
504 .driver_info = KVASER_HAS_TXRX_ERRORS |
505 KVASER_HAS_SILENT_MODE },
506 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_SWC_PRODUCT_ID),
507 .driver_info = KVASER_HAS_TXRX_ERRORS |
508 KVASER_HAS_SILENT_MODE },
509 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_LIN_PRODUCT_ID),
510 .driver_info = KVASER_HAS_TXRX_ERRORS |
511 KVASER_HAS_SILENT_MODE },
512 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_SPRO_LS_PRODUCT_ID),
513 .driver_info = KVASER_HAS_TXRX_ERRORS |
514 KVASER_HAS_SILENT_MODE },
515 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_SPRO_SWC_PRODUCT_ID),
516 .driver_info = KVASER_HAS_TXRX_ERRORS |
517 KVASER_HAS_SILENT_MODE },
518 { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO2_DEVEL_PRODUCT_ID),
519 .driver_info = KVASER_HAS_TXRX_ERRORS |
520 KVASER_HAS_SILENT_MODE },
521 { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO2_HSHS_PRODUCT_ID),
522 .driver_info = KVASER_HAS_TXRX_ERRORS |
523 KVASER_HAS_SILENT_MODE },
524 { USB_DEVICE(KVASER_VENDOR_ID, USB_UPRO_HSHS_PRODUCT_ID),
525 .driver_info = KVASER_HAS_TXRX_ERRORS },
526 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_GI_PRODUCT_ID) },
527 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_OBDII_PRODUCT_ID),
528 .driver_info = KVASER_HAS_TXRX_ERRORS |
529 KVASER_HAS_SILENT_MODE },
530 { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO2_HSLS_PRODUCT_ID),
531 .driver_info = KVASER_HAS_TXRX_ERRORS },
532 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_CH_PRODUCT_ID),
533 .driver_info = KVASER_HAS_TXRX_ERRORS },
534 { USB_DEVICE(KVASER_VENDOR_ID, USB_BLACKBIRD_SPRO_PRODUCT_ID),
535 .driver_info = KVASER_HAS_TXRX_ERRORS },
536 { USB_DEVICE(KVASER_VENDOR_ID, USB_OEM_MERCURY_PRODUCT_ID),
537 .driver_info = KVASER_HAS_TXRX_ERRORS },
538 { USB_DEVICE(KVASER_VENDOR_ID, USB_OEM_LEAF_PRODUCT_ID),
539 .driver_info = KVASER_HAS_TXRX_ERRORS },
540 { USB_DEVICE(KVASER_VENDOR_ID, USB_CAN_R_PRODUCT_ID),
541 .driver_info = KVASER_HAS_TXRX_ERRORS },
542 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_V2_PRODUCT_ID) },
543 { USB_DEVICE(KVASER_VENDOR_ID, USB_MINI_PCIE_HS_PRODUCT_ID) },
544 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LIGHT_HS_V2_OEM_PRODUCT_ID) },
545 { USB_DEVICE(KVASER_VENDOR_ID, USB_USBCAN_LIGHT_2HS_PRODUCT_ID) },
546 { USB_DEVICE(KVASER_VENDOR_ID, USB_MINI_PCIE_2HS_PRODUCT_ID) },
547
548
549 { USB_DEVICE(KVASER_VENDOR_ID, USB_USBCAN2_PRODUCT_ID),
550 .driver_info = KVASER_HAS_TXRX_ERRORS },
551 { USB_DEVICE(KVASER_VENDOR_ID, USB_USBCAN_REVB_PRODUCT_ID),
552 .driver_info = KVASER_HAS_TXRX_ERRORS },
553 { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMORATOR_PRODUCT_ID),
554 .driver_info = KVASER_HAS_TXRX_ERRORS },
555 { USB_DEVICE(KVASER_VENDOR_ID, USB_VCI2_PRODUCT_ID),
556 .driver_info = KVASER_HAS_TXRX_ERRORS },
557
558 { }
559};
560MODULE_DEVICE_TABLE(usb, kvaser_usb_table);
561
562static inline int kvaser_usb_send_msg(const struct kvaser_usb *dev,
563 struct kvaser_msg *msg)
564{
565 int actual_len;
566
567 return usb_bulk_msg(dev->udev,
568 usb_sndbulkpipe(dev->udev,
569 dev->bulk_out->bEndpointAddress),
570 msg, msg->len, &actual_len,
571 USB_SEND_TIMEOUT);
572}
573
574static int kvaser_usb_wait_msg(const struct kvaser_usb *dev, u8 id,
575 struct kvaser_msg *msg)
576{
577 struct kvaser_msg *tmp;
578 void *buf;
579 int actual_len;
580 int err;
581 int pos;
582 unsigned long to = jiffies + msecs_to_jiffies(USB_RECV_TIMEOUT);
583
584 buf = kzalloc(RX_BUFFER_SIZE, GFP_KERNEL);
585 if (!buf)
586 return -ENOMEM;
587
588 do {
589 err = usb_bulk_msg(dev->udev,
590 usb_rcvbulkpipe(dev->udev,
591 dev->bulk_in->bEndpointAddress),
592 buf, RX_BUFFER_SIZE, &actual_len,
593 USB_RECV_TIMEOUT);
594 if (err < 0)
595 goto end;
596
597 pos = 0;
598 while (pos <= actual_len - MSG_HEADER_LEN) {
599 tmp = buf + pos;
600
601
602
603
604
605 if (tmp->len == 0) {
606 pos = round_up(pos, le16_to_cpu(dev->bulk_in->
607 wMaxPacketSize));
608 continue;
609 }
610
611 if (pos + tmp->len > actual_len) {
612 dev_err(dev->udev->dev.parent,
613 "Format error\n");
614 break;
615 }
616
617 if (tmp->id == id) {
618 memcpy(msg, tmp, tmp->len);
619 goto end;
620 }
621
622 pos += tmp->len;
623 }
624 } while (time_before(jiffies, to));
625
626 err = -EINVAL;
627
628end:
629 kfree(buf);
630
631 return err;
632}
633
634static int kvaser_usb_send_simple_msg(const struct kvaser_usb *dev,
635 u8 msg_id, int channel)
636{
637 struct kvaser_msg *msg;
638 int rc;
639
640 msg = kmalloc(sizeof(*msg), GFP_KERNEL);
641 if (!msg)
642 return -ENOMEM;
643
644 msg->id = msg_id;
645 msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_simple);
646 msg->u.simple.channel = channel;
647 msg->u.simple.tid = 0xff;
648
649 rc = kvaser_usb_send_msg(dev, msg);
650
651 kfree(msg);
652 return rc;
653}
654
655static int kvaser_usb_get_software_info(struct kvaser_usb *dev)
656{
657 struct kvaser_msg msg;
658 int err;
659
660 err = kvaser_usb_send_simple_msg(dev, CMD_GET_SOFTWARE_INFO, 0);
661 if (err)
662 return err;
663
664 err = kvaser_usb_wait_msg(dev, CMD_GET_SOFTWARE_INFO_REPLY, &msg);
665 if (err)
666 return err;
667
668 switch (dev->family) {
669 case KVASER_LEAF:
670 dev->fw_version = le32_to_cpu(msg.u.leaf.softinfo.fw_version);
671 dev->max_tx_urbs =
672 le16_to_cpu(msg.u.leaf.softinfo.max_outstanding_tx);
673 break;
674 case KVASER_USBCAN:
675 dev->fw_version = le32_to_cpu(msg.u.usbcan.softinfo.fw_version);
676 dev->max_tx_urbs =
677 le16_to_cpu(msg.u.usbcan.softinfo.max_outstanding_tx);
678 break;
679 }
680
681 return 0;
682}
683
684static int kvaser_usb_get_card_info(struct kvaser_usb *dev)
685{
686 struct kvaser_msg msg;
687 int err;
688
689 err = kvaser_usb_send_simple_msg(dev, CMD_GET_CARD_INFO, 0);
690 if (err)
691 return err;
692
693 err = kvaser_usb_wait_msg(dev, CMD_GET_CARD_INFO_REPLY, &msg);
694 if (err)
695 return err;
696
697 dev->nchannels = msg.u.cardinfo.nchannels;
698 if ((dev->nchannels > MAX_NET_DEVICES) ||
699 (dev->family == KVASER_USBCAN &&
700 dev->nchannels > MAX_USBCAN_NET_DEVICES))
701 return -EINVAL;
702
703 return 0;
704}
705
706static void kvaser_usb_tx_acknowledge(const struct kvaser_usb *dev,
707 const struct kvaser_msg *msg)
708{
709 struct net_device_stats *stats;
710 struct kvaser_usb_tx_urb_context *context;
711 struct kvaser_usb_net_priv *priv;
712 struct sk_buff *skb;
713 struct can_frame *cf;
714 unsigned long flags;
715 u8 channel, tid;
716
717 channel = msg->u.tx_acknowledge_header.channel;
718 tid = msg->u.tx_acknowledge_header.tid;
719
720 if (channel >= dev->nchannels) {
721 dev_err(dev->udev->dev.parent,
722 "Invalid channel number (%d)\n", channel);
723 return;
724 }
725
726 priv = dev->nets[channel];
727
728 if (!netif_device_present(priv->netdev))
729 return;
730
731 stats = &priv->netdev->stats;
732
733 context = &priv->tx_contexts[tid % dev->max_tx_urbs];
734
735
736 if (priv->can.restart_ms &&
737 (priv->can.state >= CAN_STATE_BUS_OFF)) {
738 skb = alloc_can_err_skb(priv->netdev, &cf);
739 if (skb) {
740 cf->can_id |= CAN_ERR_RESTARTED;
741
742 stats->rx_packets++;
743 stats->rx_bytes += cf->can_dlc;
744 netif_rx(skb);
745 } else {
746 netdev_err(priv->netdev,
747 "No memory left for err_skb\n");
748 }
749
750 priv->can.can_stats.restarts++;
751 netif_carrier_on(priv->netdev);
752
753 priv->can.state = CAN_STATE_ERROR_ACTIVE;
754 }
755
756 stats->tx_packets++;
757 stats->tx_bytes += context->dlc;
758
759 spin_lock_irqsave(&priv->tx_contexts_lock, flags);
760
761 can_get_echo_skb(priv->netdev, context->echo_index);
762 context->echo_index = dev->max_tx_urbs;
763 --priv->active_tx_contexts;
764 netif_wake_queue(priv->netdev);
765
766 spin_unlock_irqrestore(&priv->tx_contexts_lock, flags);
767}
768
769static void kvaser_usb_simple_msg_callback(struct urb *urb)
770{
771 struct net_device *netdev = urb->context;
772
773 kfree(urb->transfer_buffer);
774
775 if (urb->status)
776 netdev_warn(netdev, "urb status received: %d\n",
777 urb->status);
778}
779
780static int kvaser_usb_simple_msg_async(struct kvaser_usb_net_priv *priv,
781 u8 msg_id)
782{
783 struct kvaser_usb *dev = priv->dev;
784 struct net_device *netdev = priv->netdev;
785 struct kvaser_msg *msg;
786 struct urb *urb;
787 void *buf;
788 int err;
789
790 urb = usb_alloc_urb(0, GFP_ATOMIC);
791 if (!urb)
792 return -ENOMEM;
793
794 buf = kmalloc(sizeof(struct kvaser_msg), GFP_ATOMIC);
795 if (!buf) {
796 usb_free_urb(urb);
797 return -ENOMEM;
798 }
799
800 msg = (struct kvaser_msg *)buf;
801 msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_simple);
802 msg->id = msg_id;
803 msg->u.simple.channel = priv->channel;
804
805 usb_fill_bulk_urb(urb, dev->udev,
806 usb_sndbulkpipe(dev->udev,
807 dev->bulk_out->bEndpointAddress),
808 buf, msg->len,
809 kvaser_usb_simple_msg_callback, netdev);
810 usb_anchor_urb(urb, &priv->tx_submitted);
811
812 err = usb_submit_urb(urb, GFP_ATOMIC);
813 if (err) {
814 netdev_err(netdev, "Error transmitting URB\n");
815 usb_unanchor_urb(urb);
816 usb_free_urb(urb);
817 return err;
818 }
819
820 usb_free_urb(urb);
821
822 return 0;
823}
824
825static void kvaser_usb_rx_error_update_can_state(struct kvaser_usb_net_priv *priv,
826 const struct kvaser_usb_error_summary *es,
827 struct can_frame *cf)
828{
829 struct kvaser_usb *dev = priv->dev;
830 struct net_device_stats *stats = &priv->netdev->stats;
831 enum can_state cur_state, new_state, tx_state, rx_state;
832
833 netdev_dbg(priv->netdev, "Error status: 0x%02x\n", es->status);
834
835 new_state = cur_state = priv->can.state;
836
837 if (es->status & (M16C_STATE_BUS_OFF | M16C_STATE_BUS_RESET))
838 new_state = CAN_STATE_BUS_OFF;
839 else if (es->status & M16C_STATE_BUS_PASSIVE)
840 new_state = CAN_STATE_ERROR_PASSIVE;
841 else if (es->status & M16C_STATE_BUS_ERROR) {
842
843 if (cur_state < CAN_STATE_BUS_OFF) {
844 if ((es->txerr >= 128) || (es->rxerr >= 128))
845 new_state = CAN_STATE_ERROR_PASSIVE;
846 else if ((es->txerr >= 96) || (es->rxerr >= 96))
847 new_state = CAN_STATE_ERROR_WARNING;
848 else if (cur_state > CAN_STATE_ERROR_ACTIVE)
849 new_state = CAN_STATE_ERROR_ACTIVE;
850 }
851 }
852
853 if (!es->status)
854 new_state = CAN_STATE_ERROR_ACTIVE;
855
856 if (new_state != cur_state) {
857 tx_state = (es->txerr >= es->rxerr) ? new_state : 0;
858 rx_state = (es->txerr <= es->rxerr) ? new_state : 0;
859
860 can_change_state(priv->netdev, cf, tx_state, rx_state);
861 }
862
863 if (priv->can.restart_ms &&
864 (cur_state >= CAN_STATE_BUS_OFF) &&
865 (new_state < CAN_STATE_BUS_OFF)) {
866 priv->can.can_stats.restarts++;
867 }
868
869 switch (dev->family) {
870 case KVASER_LEAF:
871 if (es->leaf.error_factor) {
872 priv->can.can_stats.bus_error++;
873 stats->rx_errors++;
874 }
875 break;
876 case KVASER_USBCAN:
877 if (es->usbcan.error_state & USBCAN_ERROR_STATE_TX_ERROR)
878 stats->tx_errors++;
879 if (es->usbcan.error_state & USBCAN_ERROR_STATE_RX_ERROR)
880 stats->rx_errors++;
881 if (es->usbcan.error_state & USBCAN_ERROR_STATE_BUSERROR) {
882 priv->can.can_stats.bus_error++;
883 }
884 break;
885 }
886
887 priv->bec.txerr = es->txerr;
888 priv->bec.rxerr = es->rxerr;
889}
890
891static void kvaser_usb_rx_error(const struct kvaser_usb *dev,
892 const struct kvaser_usb_error_summary *es)
893{
894 struct can_frame *cf, tmp_cf = { .can_id = CAN_ERR_FLAG, .can_dlc = CAN_ERR_DLC };
895 struct sk_buff *skb;
896 struct net_device_stats *stats;
897 struct kvaser_usb_net_priv *priv;
898 enum can_state old_state, new_state;
899
900 if (es->channel >= dev->nchannels) {
901 dev_err(dev->udev->dev.parent,
902 "Invalid channel number (%d)\n", es->channel);
903 return;
904 }
905
906 priv = dev->nets[es->channel];
907 stats = &priv->netdev->stats;
908
909
910
911
912
913
914
915
916
917
918
919 old_state = priv->can.state;
920 kvaser_usb_rx_error_update_can_state(priv, es, &tmp_cf);
921 new_state = priv->can.state;
922
923 skb = alloc_can_err_skb(priv->netdev, &cf);
924 if (!skb) {
925 stats->rx_dropped++;
926 return;
927 }
928 memcpy(cf, &tmp_cf, sizeof(*cf));
929
930 if (new_state != old_state) {
931 if (es->status &
932 (M16C_STATE_BUS_OFF | M16C_STATE_BUS_RESET)) {
933 if (!priv->can.restart_ms)
934 kvaser_usb_simple_msg_async(priv, CMD_STOP_CHIP);
935 netif_carrier_off(priv->netdev);
936 }
937
938 if (priv->can.restart_ms &&
939 (old_state >= CAN_STATE_BUS_OFF) &&
940 (new_state < CAN_STATE_BUS_OFF)) {
941 cf->can_id |= CAN_ERR_RESTARTED;
942 netif_carrier_on(priv->netdev);
943 }
944 }
945
946 switch (dev->family) {
947 case KVASER_LEAF:
948 if (es->leaf.error_factor) {
949 cf->can_id |= CAN_ERR_BUSERROR | CAN_ERR_PROT;
950
951 if (es->leaf.error_factor & M16C_EF_ACKE)
952 cf->data[3] = CAN_ERR_PROT_LOC_ACK;
953 if (es->leaf.error_factor & M16C_EF_CRCE)
954 cf->data[3] = CAN_ERR_PROT_LOC_CRC_SEQ;
955 if (es->leaf.error_factor & M16C_EF_FORME)
956 cf->data[2] |= CAN_ERR_PROT_FORM;
957 if (es->leaf.error_factor & M16C_EF_STFE)
958 cf->data[2] |= CAN_ERR_PROT_STUFF;
959 if (es->leaf.error_factor & M16C_EF_BITE0)
960 cf->data[2] |= CAN_ERR_PROT_BIT0;
961 if (es->leaf.error_factor & M16C_EF_BITE1)
962 cf->data[2] |= CAN_ERR_PROT_BIT1;
963 if (es->leaf.error_factor & M16C_EF_TRE)
964 cf->data[2] |= CAN_ERR_PROT_TX;
965 }
966 break;
967 case KVASER_USBCAN:
968 if (es->usbcan.error_state & USBCAN_ERROR_STATE_BUSERROR) {
969 cf->can_id |= CAN_ERR_BUSERROR;
970 }
971 break;
972 }
973
974 cf->data[6] = es->txerr;
975 cf->data[7] = es->rxerr;
976
977 stats->rx_packets++;
978 stats->rx_bytes += cf->can_dlc;
979 netif_rx(skb);
980}
981
982
983
984
985static void kvaser_usbcan_conditionally_rx_error(const struct kvaser_usb *dev,
986 struct kvaser_usb_error_summary *es)
987{
988 struct kvaser_usb_net_priv *priv;
989 int channel;
990 bool report_error;
991
992 channel = es->channel;
993 if (channel >= dev->nchannels) {
994 dev_err(dev->udev->dev.parent,
995 "Invalid channel number (%d)\n", channel);
996 return;
997 }
998
999 priv = dev->nets[channel];
1000 report_error = false;
1001
1002 if (es->txerr != priv->bec.txerr) {
1003 es->usbcan.error_state |= USBCAN_ERROR_STATE_TX_ERROR;
1004 report_error = true;
1005 }
1006 if (es->rxerr != priv->bec.rxerr) {
1007 es->usbcan.error_state |= USBCAN_ERROR_STATE_RX_ERROR;
1008 report_error = true;
1009 }
1010 if ((es->status & M16C_STATE_BUS_ERROR) &&
1011 !(es->usbcan.other_ch_status & M16C_STATE_BUS_ERROR)) {
1012 es->usbcan.error_state |= USBCAN_ERROR_STATE_BUSERROR;
1013 report_error = true;
1014 }
1015
1016 if (report_error)
1017 kvaser_usb_rx_error(dev, es);
1018}
1019
1020static void kvaser_usbcan_rx_error(const struct kvaser_usb *dev,
1021 const struct kvaser_msg *msg)
1022{
1023 struct kvaser_usb_error_summary es = { };
1024
1025 switch (msg->id) {
1026
1027 case CMD_CHIP_STATE_EVENT:
1028 es.channel = msg->u.usbcan.chip_state_event.channel;
1029 es.status = msg->u.usbcan.chip_state_event.status;
1030 es.txerr = msg->u.usbcan.chip_state_event.tx_errors_count;
1031 es.rxerr = msg->u.usbcan.chip_state_event.rx_errors_count;
1032 kvaser_usbcan_conditionally_rx_error(dev, &es);
1033 break;
1034
1035 case CMD_CAN_ERROR_EVENT:
1036 es.channel = 0;
1037 es.status = msg->u.usbcan.error_event.status_ch0;
1038 es.txerr = msg->u.usbcan.error_event.tx_errors_count_ch0;
1039 es.rxerr = msg->u.usbcan.error_event.rx_errors_count_ch0;
1040 es.usbcan.other_ch_status =
1041 msg->u.usbcan.error_event.status_ch1;
1042 kvaser_usbcan_conditionally_rx_error(dev, &es);
1043
1044
1045
1046
1047 if (dev->nchannels == MAX_USBCAN_NET_DEVICES) {
1048 es.channel = 1;
1049 es.status = msg->u.usbcan.error_event.status_ch1;
1050 es.txerr = msg->u.usbcan.error_event.tx_errors_count_ch1;
1051 es.rxerr = msg->u.usbcan.error_event.rx_errors_count_ch1;
1052 es.usbcan.other_ch_status =
1053 msg->u.usbcan.error_event.status_ch0;
1054 kvaser_usbcan_conditionally_rx_error(dev, &es);
1055 }
1056 break;
1057
1058 default:
1059 dev_err(dev->udev->dev.parent, "Invalid msg id (%d)\n",
1060 msg->id);
1061 }
1062}
1063
1064static void kvaser_leaf_rx_error(const struct kvaser_usb *dev,
1065 const struct kvaser_msg *msg)
1066{
1067 struct kvaser_usb_error_summary es = { };
1068
1069 switch (msg->id) {
1070 case CMD_CAN_ERROR_EVENT:
1071 es.channel = msg->u.leaf.error_event.channel;
1072 es.status = msg->u.leaf.error_event.status;
1073 es.txerr = msg->u.leaf.error_event.tx_errors_count;
1074 es.rxerr = msg->u.leaf.error_event.rx_errors_count;
1075 es.leaf.error_factor = msg->u.leaf.error_event.error_factor;
1076 break;
1077 case CMD_LEAF_LOG_MESSAGE:
1078 es.channel = msg->u.leaf.log_message.channel;
1079 es.status = msg->u.leaf.log_message.data[0];
1080 es.txerr = msg->u.leaf.log_message.data[2];
1081 es.rxerr = msg->u.leaf.log_message.data[3];
1082 es.leaf.error_factor = msg->u.leaf.log_message.data[1];
1083 break;
1084 case CMD_CHIP_STATE_EVENT:
1085 es.channel = msg->u.leaf.chip_state_event.channel;
1086 es.status = msg->u.leaf.chip_state_event.status;
1087 es.txerr = msg->u.leaf.chip_state_event.tx_errors_count;
1088 es.rxerr = msg->u.leaf.chip_state_event.rx_errors_count;
1089 es.leaf.error_factor = 0;
1090 break;
1091 default:
1092 dev_err(dev->udev->dev.parent, "Invalid msg id (%d)\n",
1093 msg->id);
1094 return;
1095 }
1096
1097 kvaser_usb_rx_error(dev, &es);
1098}
1099
1100static void kvaser_usb_rx_can_err(const struct kvaser_usb_net_priv *priv,
1101 const struct kvaser_msg *msg)
1102{
1103 struct can_frame *cf;
1104 struct sk_buff *skb;
1105 struct net_device_stats *stats = &priv->netdev->stats;
1106
1107 if (msg->u.rx_can_header.flag & (MSG_FLAG_ERROR_FRAME |
1108 MSG_FLAG_NERR)) {
1109 netdev_err(priv->netdev, "Unknown error (flags: 0x%02x)\n",
1110 msg->u.rx_can_header.flag);
1111
1112 stats->rx_errors++;
1113 return;
1114 }
1115
1116 if (msg->u.rx_can_header.flag & MSG_FLAG_OVERRUN) {
1117 stats->rx_over_errors++;
1118 stats->rx_errors++;
1119
1120 skb = alloc_can_err_skb(priv->netdev, &cf);
1121 if (!skb) {
1122 stats->rx_dropped++;
1123 return;
1124 }
1125
1126 cf->can_id |= CAN_ERR_CRTL;
1127 cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
1128
1129 stats->rx_packets++;
1130 stats->rx_bytes += cf->can_dlc;
1131 netif_rx(skb);
1132 }
1133}
1134
1135static void kvaser_usb_rx_can_msg(const struct kvaser_usb *dev,
1136 const struct kvaser_msg *msg)
1137{
1138 struct kvaser_usb_net_priv *priv;
1139 struct can_frame *cf;
1140 struct sk_buff *skb;
1141 struct net_device_stats *stats;
1142 u8 channel = msg->u.rx_can_header.channel;
1143 const u8 *rx_msg = NULL;
1144
1145 if (channel >= dev->nchannels) {
1146 dev_err(dev->udev->dev.parent,
1147 "Invalid channel number (%d)\n", channel);
1148 return;
1149 }
1150
1151 priv = dev->nets[channel];
1152 stats = &priv->netdev->stats;
1153
1154 if ((msg->u.rx_can_header.flag & MSG_FLAG_ERROR_FRAME) &&
1155 (dev->family == KVASER_LEAF && msg->id == CMD_LEAF_LOG_MESSAGE)) {
1156 kvaser_leaf_rx_error(dev, msg);
1157 return;
1158 } else if (msg->u.rx_can_header.flag & (MSG_FLAG_ERROR_FRAME |
1159 MSG_FLAG_NERR |
1160 MSG_FLAG_OVERRUN)) {
1161 kvaser_usb_rx_can_err(priv, msg);
1162 return;
1163 } else if (msg->u.rx_can_header.flag & ~MSG_FLAG_REMOTE_FRAME) {
1164 netdev_warn(priv->netdev,
1165 "Unhandled frame (flags: 0x%02x)",
1166 msg->u.rx_can_header.flag);
1167 return;
1168 }
1169
1170 switch (dev->family) {
1171 case KVASER_LEAF:
1172 rx_msg = msg->u.leaf.rx_can.msg;
1173 break;
1174 case KVASER_USBCAN:
1175 rx_msg = msg->u.usbcan.rx_can.msg;
1176 break;
1177 }
1178
1179 skb = alloc_can_skb(priv->netdev, &cf);
1180 if (!skb) {
1181 stats->tx_dropped++;
1182 return;
1183 }
1184
1185 if (dev->family == KVASER_LEAF && msg->id == CMD_LEAF_LOG_MESSAGE) {
1186 cf->can_id = le32_to_cpu(msg->u.leaf.log_message.id);
1187 if (cf->can_id & KVASER_EXTENDED_FRAME)
1188 cf->can_id &= CAN_EFF_MASK | CAN_EFF_FLAG;
1189 else
1190 cf->can_id &= CAN_SFF_MASK;
1191
1192 cf->can_dlc = get_can_dlc(msg->u.leaf.log_message.dlc);
1193
1194 if (msg->u.leaf.log_message.flags & MSG_FLAG_REMOTE_FRAME)
1195 cf->can_id |= CAN_RTR_FLAG;
1196 else
1197 memcpy(cf->data, &msg->u.leaf.log_message.data,
1198 cf->can_dlc);
1199 } else {
1200 cf->can_id = ((rx_msg[0] & 0x1f) << 6) | (rx_msg[1] & 0x3f);
1201
1202 if (msg->id == CMD_RX_EXT_MESSAGE) {
1203 cf->can_id <<= 18;
1204 cf->can_id |= ((rx_msg[2] & 0x0f) << 14) |
1205 ((rx_msg[3] & 0xff) << 6) |
1206 (rx_msg[4] & 0x3f);
1207 cf->can_id |= CAN_EFF_FLAG;
1208 }
1209
1210 cf->can_dlc = get_can_dlc(rx_msg[5]);
1211
1212 if (msg->u.rx_can_header.flag & MSG_FLAG_REMOTE_FRAME)
1213 cf->can_id |= CAN_RTR_FLAG;
1214 else
1215 memcpy(cf->data, &rx_msg[6],
1216 cf->can_dlc);
1217 }
1218
1219 stats->rx_packets++;
1220 stats->rx_bytes += cf->can_dlc;
1221 netif_rx(skb);
1222}
1223
1224static void kvaser_usb_start_chip_reply(const struct kvaser_usb *dev,
1225 const struct kvaser_msg *msg)
1226{
1227 struct kvaser_usb_net_priv *priv;
1228 u8 channel = msg->u.simple.channel;
1229
1230 if (channel >= dev->nchannels) {
1231 dev_err(dev->udev->dev.parent,
1232 "Invalid channel number (%d)\n", channel);
1233 return;
1234 }
1235
1236 priv = dev->nets[channel];
1237
1238 if (completion_done(&priv->start_comp) &&
1239 netif_queue_stopped(priv->netdev)) {
1240 netif_wake_queue(priv->netdev);
1241 } else {
1242 netif_start_queue(priv->netdev);
1243 complete(&priv->start_comp);
1244 }
1245}
1246
1247static void kvaser_usb_stop_chip_reply(const struct kvaser_usb *dev,
1248 const struct kvaser_msg *msg)
1249{
1250 struct kvaser_usb_net_priv *priv;
1251 u8 channel = msg->u.simple.channel;
1252
1253 if (channel >= dev->nchannels) {
1254 dev_err(dev->udev->dev.parent,
1255 "Invalid channel number (%d)\n", channel);
1256 return;
1257 }
1258
1259 priv = dev->nets[channel];
1260
1261 complete(&priv->stop_comp);
1262}
1263
1264static void kvaser_usb_handle_message(const struct kvaser_usb *dev,
1265 const struct kvaser_msg *msg)
1266{
1267 switch (msg->id) {
1268 case CMD_START_CHIP_REPLY:
1269 kvaser_usb_start_chip_reply(dev, msg);
1270 break;
1271
1272 case CMD_STOP_CHIP_REPLY:
1273 kvaser_usb_stop_chip_reply(dev, msg);
1274 break;
1275
1276 case CMD_RX_STD_MESSAGE:
1277 case CMD_RX_EXT_MESSAGE:
1278 kvaser_usb_rx_can_msg(dev, msg);
1279 break;
1280
1281 case CMD_LEAF_LOG_MESSAGE:
1282 if (dev->family != KVASER_LEAF)
1283 goto warn;
1284 kvaser_usb_rx_can_msg(dev, msg);
1285 break;
1286
1287 case CMD_CHIP_STATE_EVENT:
1288 case CMD_CAN_ERROR_EVENT:
1289 if (dev->family == KVASER_LEAF)
1290 kvaser_leaf_rx_error(dev, msg);
1291 else
1292 kvaser_usbcan_rx_error(dev, msg);
1293 break;
1294
1295 case CMD_TX_ACKNOWLEDGE:
1296 kvaser_usb_tx_acknowledge(dev, msg);
1297 break;
1298
1299
1300 case CMD_USBCAN_CLOCK_OVERFLOW_EVENT:
1301 if (dev->family != KVASER_USBCAN)
1302 goto warn;
1303 break;
1304
1305 case CMD_FLUSH_QUEUE_REPLY:
1306 if (dev->family != KVASER_LEAF)
1307 goto warn;
1308 break;
1309
1310 default:
1311warn: dev_warn(dev->udev->dev.parent,
1312 "Unhandled message (%d)\n", msg->id);
1313 break;
1314 }
1315}
1316
1317static void kvaser_usb_read_bulk_callback(struct urb *urb)
1318{
1319 struct kvaser_usb *dev = urb->context;
1320 struct kvaser_msg *msg;
1321 int pos = 0;
1322 int err, i;
1323
1324 switch (urb->status) {
1325 case 0:
1326 break;
1327 case -ENOENT:
1328 case -ESHUTDOWN:
1329 return;
1330 default:
1331 dev_info(dev->udev->dev.parent, "Rx URB aborted (%d)\n",
1332 urb->status);
1333 goto resubmit_urb;
1334 }
1335
1336 while (pos <= urb->actual_length - MSG_HEADER_LEN) {
1337 msg = urb->transfer_buffer + pos;
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348 if (msg->len == 0) {
1349 pos = round_up(pos, le16_to_cpu(dev->bulk_in->
1350 wMaxPacketSize));
1351 continue;
1352 }
1353
1354 if (pos + msg->len > urb->actual_length) {
1355 dev_err(dev->udev->dev.parent, "Format error\n");
1356 break;
1357 }
1358
1359 kvaser_usb_handle_message(dev, msg);
1360 pos += msg->len;
1361 }
1362
1363resubmit_urb:
1364 usb_fill_bulk_urb(urb, dev->udev,
1365 usb_rcvbulkpipe(dev->udev,
1366 dev->bulk_in->bEndpointAddress),
1367 urb->transfer_buffer, RX_BUFFER_SIZE,
1368 kvaser_usb_read_bulk_callback, dev);
1369
1370 err = usb_submit_urb(urb, GFP_ATOMIC);
1371 if (err == -ENODEV) {
1372 for (i = 0; i < dev->nchannels; i++) {
1373 if (!dev->nets[i])
1374 continue;
1375
1376 netif_device_detach(dev->nets[i]->netdev);
1377 }
1378 } else if (err) {
1379 dev_err(dev->udev->dev.parent,
1380 "Failed resubmitting read bulk urb: %d\n", err);
1381 }
1382
1383 return;
1384}
1385
1386static int kvaser_usb_setup_rx_urbs(struct kvaser_usb *dev)
1387{
1388 int i, err = 0;
1389
1390 if (dev->rxinitdone)
1391 return 0;
1392
1393 for (i = 0; i < MAX_RX_URBS; i++) {
1394 struct urb *urb = NULL;
1395 u8 *buf = NULL;
1396 dma_addr_t buf_dma;
1397
1398 urb = usb_alloc_urb(0, GFP_KERNEL);
1399 if (!urb) {
1400 err = -ENOMEM;
1401 break;
1402 }
1403
1404 buf = usb_alloc_coherent(dev->udev, RX_BUFFER_SIZE,
1405 GFP_KERNEL, &buf_dma);
1406 if (!buf) {
1407 dev_warn(dev->udev->dev.parent,
1408 "No memory left for USB buffer\n");
1409 usb_free_urb(urb);
1410 err = -ENOMEM;
1411 break;
1412 }
1413
1414 usb_fill_bulk_urb(urb, dev->udev,
1415 usb_rcvbulkpipe(dev->udev,
1416 dev->bulk_in->bEndpointAddress),
1417 buf, RX_BUFFER_SIZE,
1418 kvaser_usb_read_bulk_callback,
1419 dev);
1420 urb->transfer_dma = buf_dma;
1421 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1422 usb_anchor_urb(urb, &dev->rx_submitted);
1423
1424 err = usb_submit_urb(urb, GFP_KERNEL);
1425 if (err) {
1426 usb_unanchor_urb(urb);
1427 usb_free_coherent(dev->udev, RX_BUFFER_SIZE, buf,
1428 buf_dma);
1429 usb_free_urb(urb);
1430 break;
1431 }
1432
1433 dev->rxbuf[i] = buf;
1434 dev->rxbuf_dma[i] = buf_dma;
1435
1436 usb_free_urb(urb);
1437 }
1438
1439 if (i == 0) {
1440 dev_warn(dev->udev->dev.parent,
1441 "Cannot setup read URBs, error %d\n", err);
1442 return err;
1443 } else if (i < MAX_RX_URBS) {
1444 dev_warn(dev->udev->dev.parent,
1445 "RX performances may be slow\n");
1446 }
1447
1448 dev->rxinitdone = true;
1449
1450 return 0;
1451}
1452
1453static int kvaser_usb_set_opt_mode(const struct kvaser_usb_net_priv *priv)
1454{
1455 struct kvaser_msg *msg;
1456 int rc;
1457
1458 msg = kmalloc(sizeof(*msg), GFP_KERNEL);
1459 if (!msg)
1460 return -ENOMEM;
1461
1462 msg->id = CMD_SET_CTRL_MODE;
1463 msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_ctrl_mode);
1464 msg->u.ctrl_mode.tid = 0xff;
1465 msg->u.ctrl_mode.channel = priv->channel;
1466
1467 if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
1468 msg->u.ctrl_mode.ctrl_mode = KVASER_CTRL_MODE_SILENT;
1469 else
1470 msg->u.ctrl_mode.ctrl_mode = KVASER_CTRL_MODE_NORMAL;
1471
1472 rc = kvaser_usb_send_msg(priv->dev, msg);
1473
1474 kfree(msg);
1475 return rc;
1476}
1477
1478static int kvaser_usb_start_chip(struct kvaser_usb_net_priv *priv)
1479{
1480 int err;
1481
1482 init_completion(&priv->start_comp);
1483
1484 err = kvaser_usb_send_simple_msg(priv->dev, CMD_START_CHIP,
1485 priv->channel);
1486 if (err)
1487 return err;
1488
1489 if (!wait_for_completion_timeout(&priv->start_comp,
1490 msecs_to_jiffies(START_TIMEOUT)))
1491 return -ETIMEDOUT;
1492
1493 return 0;
1494}
1495
1496static int kvaser_usb_open(struct net_device *netdev)
1497{
1498 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1499 struct kvaser_usb *dev = priv->dev;
1500 int err;
1501
1502 err = open_candev(netdev);
1503 if (err)
1504 return err;
1505
1506 err = kvaser_usb_setup_rx_urbs(dev);
1507 if (err)
1508 goto error;
1509
1510 err = kvaser_usb_set_opt_mode(priv);
1511 if (err)
1512 goto error;
1513
1514 err = kvaser_usb_start_chip(priv);
1515 if (err) {
1516 netdev_warn(netdev, "Cannot start device, error %d\n", err);
1517 goto error;
1518 }
1519
1520 priv->can.state = CAN_STATE_ERROR_ACTIVE;
1521
1522 return 0;
1523
1524error:
1525 close_candev(netdev);
1526 return err;
1527}
1528
1529static void kvaser_usb_reset_tx_urb_contexts(struct kvaser_usb_net_priv *priv)
1530{
1531 int i, max_tx_urbs;
1532
1533 max_tx_urbs = priv->dev->max_tx_urbs;
1534
1535 priv->active_tx_contexts = 0;
1536 for (i = 0; i < max_tx_urbs; i++)
1537 priv->tx_contexts[i].echo_index = max_tx_urbs;
1538}
1539
1540
1541
1542
1543static void kvaser_usb_unlink_tx_urbs(struct kvaser_usb_net_priv *priv)
1544{
1545 usb_kill_anchored_urbs(&priv->tx_submitted);
1546 kvaser_usb_reset_tx_urb_contexts(priv);
1547}
1548
1549static void kvaser_usb_unlink_all_urbs(struct kvaser_usb *dev)
1550{
1551 int i;
1552
1553 usb_kill_anchored_urbs(&dev->rx_submitted);
1554
1555 for (i = 0; i < MAX_RX_URBS; i++)
1556 usb_free_coherent(dev->udev, RX_BUFFER_SIZE,
1557 dev->rxbuf[i],
1558 dev->rxbuf_dma[i]);
1559
1560 for (i = 0; i < dev->nchannels; i++) {
1561 struct kvaser_usb_net_priv *priv = dev->nets[i];
1562
1563 if (priv)
1564 kvaser_usb_unlink_tx_urbs(priv);
1565 }
1566}
1567
1568static int kvaser_usb_stop_chip(struct kvaser_usb_net_priv *priv)
1569{
1570 int err;
1571
1572 init_completion(&priv->stop_comp);
1573
1574 err = kvaser_usb_send_simple_msg(priv->dev, CMD_STOP_CHIP,
1575 priv->channel);
1576 if (err)
1577 return err;
1578
1579 if (!wait_for_completion_timeout(&priv->stop_comp,
1580 msecs_to_jiffies(STOP_TIMEOUT)))
1581 return -ETIMEDOUT;
1582
1583 return 0;
1584}
1585
1586static int kvaser_usb_flush_queue(struct kvaser_usb_net_priv *priv)
1587{
1588 struct kvaser_msg *msg;
1589 int rc;
1590
1591 msg = kmalloc(sizeof(*msg), GFP_KERNEL);
1592 if (!msg)
1593 return -ENOMEM;
1594
1595 msg->id = CMD_FLUSH_QUEUE;
1596 msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_flush_queue);
1597 msg->u.flush_queue.channel = priv->channel;
1598 msg->u.flush_queue.flags = 0x00;
1599
1600 rc = kvaser_usb_send_msg(priv->dev, msg);
1601
1602 kfree(msg);
1603 return rc;
1604}
1605
1606static int kvaser_usb_close(struct net_device *netdev)
1607{
1608 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1609 struct kvaser_usb *dev = priv->dev;
1610 int err;
1611
1612 netif_stop_queue(netdev);
1613
1614 err = kvaser_usb_flush_queue(priv);
1615 if (err)
1616 netdev_warn(netdev, "Cannot flush queue, error %d\n", err);
1617
1618 err = kvaser_usb_send_simple_msg(dev, CMD_RESET_CHIP, priv->channel);
1619 if (err)
1620 netdev_warn(netdev, "Cannot reset card, error %d\n", err);
1621
1622 err = kvaser_usb_stop_chip(priv);
1623 if (err)
1624 netdev_warn(netdev, "Cannot stop device, error %d\n", err);
1625
1626
1627 kvaser_usb_unlink_tx_urbs(priv);
1628
1629 priv->can.state = CAN_STATE_STOPPED;
1630 close_candev(priv->netdev);
1631
1632 return 0;
1633}
1634
1635static void kvaser_usb_write_bulk_callback(struct urb *urb)
1636{
1637 struct kvaser_usb_tx_urb_context *context = urb->context;
1638 struct kvaser_usb_net_priv *priv;
1639 struct net_device *netdev;
1640
1641 if (WARN_ON(!context))
1642 return;
1643
1644 priv = context->priv;
1645 netdev = priv->netdev;
1646
1647 kfree(urb->transfer_buffer);
1648
1649 if (!netif_device_present(netdev))
1650 return;
1651
1652 if (urb->status)
1653 netdev_info(netdev, "Tx URB aborted (%d)\n", urb->status);
1654}
1655
1656static netdev_tx_t kvaser_usb_start_xmit(struct sk_buff *skb,
1657 struct net_device *netdev)
1658{
1659 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1660 struct kvaser_usb *dev = priv->dev;
1661 struct net_device_stats *stats = &netdev->stats;
1662 struct can_frame *cf = (struct can_frame *)skb->data;
1663 struct kvaser_usb_tx_urb_context *context = NULL;
1664 struct urb *urb;
1665 void *buf;
1666 struct kvaser_msg *msg;
1667 int i, err, ret = NETDEV_TX_OK;
1668 u8 *msg_tx_can_flags = NULL;
1669 unsigned long flags;
1670
1671 if (can_dropped_invalid_skb(netdev, skb))
1672 return NETDEV_TX_OK;
1673
1674 urb = usb_alloc_urb(0, GFP_ATOMIC);
1675 if (!urb) {
1676 stats->tx_dropped++;
1677 dev_kfree_skb(skb);
1678 return NETDEV_TX_OK;
1679 }
1680
1681 buf = kmalloc(sizeof(struct kvaser_msg), GFP_ATOMIC);
1682 if (!buf) {
1683 stats->tx_dropped++;
1684 dev_kfree_skb(skb);
1685 goto freeurb;
1686 }
1687
1688 msg = buf;
1689 msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_tx_can);
1690 msg->u.tx_can.channel = priv->channel;
1691
1692 switch (dev->family) {
1693 case KVASER_LEAF:
1694 msg_tx_can_flags = &msg->u.tx_can.leaf.flags;
1695 break;
1696 case KVASER_USBCAN:
1697 msg_tx_can_flags = &msg->u.tx_can.usbcan.flags;
1698 break;
1699 }
1700
1701 *msg_tx_can_flags = 0;
1702
1703 if (cf->can_id & CAN_EFF_FLAG) {
1704 msg->id = CMD_TX_EXT_MESSAGE;
1705 msg->u.tx_can.msg[0] = (cf->can_id >> 24) & 0x1f;
1706 msg->u.tx_can.msg[1] = (cf->can_id >> 18) & 0x3f;
1707 msg->u.tx_can.msg[2] = (cf->can_id >> 14) & 0x0f;
1708 msg->u.tx_can.msg[3] = (cf->can_id >> 6) & 0xff;
1709 msg->u.tx_can.msg[4] = cf->can_id & 0x3f;
1710 } else {
1711 msg->id = CMD_TX_STD_MESSAGE;
1712 msg->u.tx_can.msg[0] = (cf->can_id >> 6) & 0x1f;
1713 msg->u.tx_can.msg[1] = cf->can_id & 0x3f;
1714 }
1715
1716 msg->u.tx_can.msg[5] = cf->can_dlc;
1717 memcpy(&msg->u.tx_can.msg[6], cf->data, cf->can_dlc);
1718
1719 if (cf->can_id & CAN_RTR_FLAG)
1720 *msg_tx_can_flags |= MSG_FLAG_REMOTE_FRAME;
1721
1722 spin_lock_irqsave(&priv->tx_contexts_lock, flags);
1723 for (i = 0; i < dev->max_tx_urbs; i++) {
1724 if (priv->tx_contexts[i].echo_index == dev->max_tx_urbs) {
1725 context = &priv->tx_contexts[i];
1726
1727 context->echo_index = i;
1728 can_put_echo_skb(skb, netdev, context->echo_index);
1729 ++priv->active_tx_contexts;
1730 if (priv->active_tx_contexts >= dev->max_tx_urbs)
1731 netif_stop_queue(netdev);
1732
1733 break;
1734 }
1735 }
1736 spin_unlock_irqrestore(&priv->tx_contexts_lock, flags);
1737
1738
1739 if (!context) {
1740 netdev_warn(netdev, "cannot find free context\n");
1741
1742 kfree(buf);
1743 ret = NETDEV_TX_BUSY;
1744 goto freeurb;
1745 }
1746
1747 context->priv = priv;
1748 context->dlc = cf->can_dlc;
1749
1750 msg->u.tx_can.tid = context->echo_index;
1751
1752 usb_fill_bulk_urb(urb, dev->udev,
1753 usb_sndbulkpipe(dev->udev,
1754 dev->bulk_out->bEndpointAddress),
1755 buf, msg->len,
1756 kvaser_usb_write_bulk_callback, context);
1757 usb_anchor_urb(urb, &priv->tx_submitted);
1758
1759 err = usb_submit_urb(urb, GFP_ATOMIC);
1760 if (unlikely(err)) {
1761 spin_lock_irqsave(&priv->tx_contexts_lock, flags);
1762
1763 can_free_echo_skb(netdev, context->echo_index);
1764 context->echo_index = dev->max_tx_urbs;
1765 --priv->active_tx_contexts;
1766 netif_wake_queue(netdev);
1767
1768 spin_unlock_irqrestore(&priv->tx_contexts_lock, flags);
1769
1770 usb_unanchor_urb(urb);
1771
1772 stats->tx_dropped++;
1773
1774 if (err == -ENODEV)
1775 netif_device_detach(netdev);
1776 else
1777 netdev_warn(netdev, "Failed tx_urb %d\n", err);
1778
1779 goto freeurb;
1780 }
1781
1782 ret = NETDEV_TX_OK;
1783
1784freeurb:
1785 usb_free_urb(urb);
1786 return ret;
1787}
1788
1789static const struct net_device_ops kvaser_usb_netdev_ops = {
1790 .ndo_open = kvaser_usb_open,
1791 .ndo_stop = kvaser_usb_close,
1792 .ndo_start_xmit = kvaser_usb_start_xmit,
1793 .ndo_change_mtu = can_change_mtu,
1794};
1795
1796static const struct can_bittiming_const kvaser_usb_bittiming_const = {
1797 .name = "kvaser_usb",
1798 .tseg1_min = KVASER_USB_TSEG1_MIN,
1799 .tseg1_max = KVASER_USB_TSEG1_MAX,
1800 .tseg2_min = KVASER_USB_TSEG2_MIN,
1801 .tseg2_max = KVASER_USB_TSEG2_MAX,
1802 .sjw_max = KVASER_USB_SJW_MAX,
1803 .brp_min = KVASER_USB_BRP_MIN,
1804 .brp_max = KVASER_USB_BRP_MAX,
1805 .brp_inc = KVASER_USB_BRP_INC,
1806};
1807
1808static int kvaser_usb_set_bittiming(struct net_device *netdev)
1809{
1810 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1811 struct can_bittiming *bt = &priv->can.bittiming;
1812 struct kvaser_usb *dev = priv->dev;
1813 struct kvaser_msg *msg;
1814 int rc;
1815
1816 msg = kmalloc(sizeof(*msg), GFP_KERNEL);
1817 if (!msg)
1818 return -ENOMEM;
1819
1820 msg->id = CMD_SET_BUS_PARAMS;
1821 msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_busparams);
1822 msg->u.busparams.channel = priv->channel;
1823 msg->u.busparams.tid = 0xff;
1824 msg->u.busparams.bitrate = cpu_to_le32(bt->bitrate);
1825 msg->u.busparams.sjw = bt->sjw;
1826 msg->u.busparams.tseg1 = bt->prop_seg + bt->phase_seg1;
1827 msg->u.busparams.tseg2 = bt->phase_seg2;
1828
1829 if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
1830 msg->u.busparams.no_samp = 3;
1831 else
1832 msg->u.busparams.no_samp = 1;
1833
1834 rc = kvaser_usb_send_msg(dev, msg);
1835
1836 kfree(msg);
1837 return rc;
1838}
1839
1840static int kvaser_usb_set_mode(struct net_device *netdev,
1841 enum can_mode mode)
1842{
1843 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1844 int err;
1845
1846 switch (mode) {
1847 case CAN_MODE_START:
1848 err = kvaser_usb_simple_msg_async(priv, CMD_START_CHIP);
1849 if (err)
1850 return err;
1851 break;
1852 default:
1853 return -EOPNOTSUPP;
1854 }
1855
1856 return 0;
1857}
1858
1859static int kvaser_usb_get_berr_counter(const struct net_device *netdev,
1860 struct can_berr_counter *bec)
1861{
1862 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1863
1864 *bec = priv->bec;
1865
1866 return 0;
1867}
1868
1869static void kvaser_usb_remove_interfaces(struct kvaser_usb *dev)
1870{
1871 int i;
1872
1873 for (i = 0; i < dev->nchannels; i++) {
1874 if (!dev->nets[i])
1875 continue;
1876
1877 unregister_candev(dev->nets[i]->netdev);
1878 }
1879
1880 kvaser_usb_unlink_all_urbs(dev);
1881
1882 for (i = 0; i < dev->nchannels; i++) {
1883 if (!dev->nets[i])
1884 continue;
1885
1886 free_candev(dev->nets[i]->netdev);
1887 }
1888}
1889
1890static int kvaser_usb_init_one(struct usb_interface *intf,
1891 const struct usb_device_id *id, int channel)
1892{
1893 struct kvaser_usb *dev = usb_get_intfdata(intf);
1894 struct net_device *netdev;
1895 struct kvaser_usb_net_priv *priv;
1896 int err;
1897
1898 err = kvaser_usb_send_simple_msg(dev, CMD_RESET_CHIP, channel);
1899 if (err)
1900 return err;
1901
1902 netdev = alloc_candev(sizeof(*priv) +
1903 dev->max_tx_urbs * sizeof(*priv->tx_contexts),
1904 dev->max_tx_urbs);
1905 if (!netdev) {
1906 dev_err(&intf->dev, "Cannot alloc candev\n");
1907 return -ENOMEM;
1908 }
1909
1910 priv = netdev_priv(netdev);
1911
1912 init_usb_anchor(&priv->tx_submitted);
1913 init_completion(&priv->start_comp);
1914 init_completion(&priv->stop_comp);
1915
1916 priv->dev = dev;
1917 priv->netdev = netdev;
1918 priv->channel = channel;
1919
1920 spin_lock_init(&priv->tx_contexts_lock);
1921 kvaser_usb_reset_tx_urb_contexts(priv);
1922
1923 priv->can.state = CAN_STATE_STOPPED;
1924 priv->can.clock.freq = CAN_USB_CLOCK;
1925 priv->can.bittiming_const = &kvaser_usb_bittiming_const;
1926 priv->can.do_set_bittiming = kvaser_usb_set_bittiming;
1927 priv->can.do_set_mode = kvaser_usb_set_mode;
1928 if (id->driver_info & KVASER_HAS_TXRX_ERRORS)
1929 priv->can.do_get_berr_counter = kvaser_usb_get_berr_counter;
1930 priv->can.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES;
1931 if (id->driver_info & KVASER_HAS_SILENT_MODE)
1932 priv->can.ctrlmode_supported |= CAN_CTRLMODE_LISTENONLY;
1933
1934 netdev->flags |= IFF_ECHO;
1935
1936 netdev->netdev_ops = &kvaser_usb_netdev_ops;
1937
1938 SET_NETDEV_DEV(netdev, &intf->dev);
1939 netdev->dev_id = channel;
1940
1941 dev->nets[channel] = priv;
1942
1943 err = register_candev(netdev);
1944 if (err) {
1945 dev_err(&intf->dev, "Failed to register can device\n");
1946 free_candev(netdev);
1947 dev->nets[channel] = NULL;
1948 return err;
1949 }
1950
1951 netdev_dbg(netdev, "device registered\n");
1952
1953 return 0;
1954}
1955
1956static int kvaser_usb_get_endpoints(const struct usb_interface *intf,
1957 struct usb_endpoint_descriptor **in,
1958 struct usb_endpoint_descriptor **out)
1959{
1960 const struct usb_host_interface *iface_desc;
1961 struct usb_endpoint_descriptor *endpoint;
1962 int i;
1963
1964 iface_desc = &intf->altsetting[0];
1965
1966 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1967 endpoint = &iface_desc->endpoint[i].desc;
1968
1969 if (!*in && usb_endpoint_is_bulk_in(endpoint))
1970 *in = endpoint;
1971
1972 if (!*out && usb_endpoint_is_bulk_out(endpoint))
1973 *out = endpoint;
1974
1975
1976 if (*in && *out)
1977 return 0;
1978 }
1979
1980 return -ENODEV;
1981}
1982
1983static int kvaser_usb_probe(struct usb_interface *intf,
1984 const struct usb_device_id *id)
1985{
1986 struct kvaser_usb *dev;
1987 int err = -ENOMEM;
1988 int i, retry = 3;
1989
1990 dev = devm_kzalloc(&intf->dev, sizeof(*dev), GFP_KERNEL);
1991 if (!dev)
1992 return -ENOMEM;
1993
1994 if (kvaser_is_leaf(id)) {
1995 dev->family = KVASER_LEAF;
1996 } else if (kvaser_is_usbcan(id)) {
1997 dev->family = KVASER_USBCAN;
1998 } else {
1999 dev_err(&intf->dev,
2000 "Product ID (%d) does not belong to any known Kvaser USB family",
2001 id->idProduct);
2002 return -ENODEV;
2003 }
2004
2005 err = kvaser_usb_get_endpoints(intf, &dev->bulk_in, &dev->bulk_out);
2006 if (err) {
2007 dev_err(&intf->dev, "Cannot get usb endpoint(s)");
2008 return err;
2009 }
2010
2011 dev->udev = interface_to_usbdev(intf);
2012
2013 init_usb_anchor(&dev->rx_submitted);
2014
2015 usb_set_intfdata(intf, dev);
2016
2017
2018
2019
2020
2021
2022 do {
2023 err = kvaser_usb_get_software_info(dev);
2024 } while (--retry && err == -ETIMEDOUT);
2025
2026 if (err) {
2027 dev_err(&intf->dev,
2028 "Cannot get software infos, error %d\n", err);
2029 return err;
2030 }
2031
2032 dev_dbg(&intf->dev, "Firmware version: %d.%d.%d\n",
2033 ((dev->fw_version >> 24) & 0xff),
2034 ((dev->fw_version >> 16) & 0xff),
2035 (dev->fw_version & 0xffff));
2036
2037 dev_dbg(&intf->dev, "Max outstanding tx = %d URBs\n", dev->max_tx_urbs);
2038
2039 err = kvaser_usb_get_card_info(dev);
2040 if (err) {
2041 dev_err(&intf->dev,
2042 "Cannot get card infos, error %d\n", err);
2043 return err;
2044 }
2045
2046 for (i = 0; i < dev->nchannels; i++) {
2047 err = kvaser_usb_init_one(intf, id, i);
2048 if (err) {
2049 kvaser_usb_remove_interfaces(dev);
2050 return err;
2051 }
2052 }
2053
2054 return 0;
2055}
2056
2057static void kvaser_usb_disconnect(struct usb_interface *intf)
2058{
2059 struct kvaser_usb *dev = usb_get_intfdata(intf);
2060
2061 usb_set_intfdata(intf, NULL);
2062
2063 if (!dev)
2064 return;
2065
2066 kvaser_usb_remove_interfaces(dev);
2067}
2068
2069static struct usb_driver kvaser_usb_driver = {
2070 .name = "kvaser_usb",
2071 .probe = kvaser_usb_probe,
2072 .disconnect = kvaser_usb_disconnect,
2073 .id_table = kvaser_usb_table,
2074};
2075
2076module_usb_driver(kvaser_usb_driver);
2077
2078MODULE_AUTHOR("Olivier Sobrie <olivier@sobrie.be>");
2079MODULE_DESCRIPTION("CAN driver for Kvaser CAN/USB devices");
2080MODULE_LICENSE("GPL v2");
2081