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>
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)
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
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
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
{
187 struct kvaser_msg_simple
{
192 struct kvaser_msg_cardinfo
{
197 __le32 serial_number
;
201 __le32 serial_number_low
;
202 __le32 serial_number_high
;
205 __le32 clock_resolution
;
220 struct kvaser_msg_cardinfo2
{
224 __le32 oem_unlock_code
;
227 struct leaf_msg_softinfo
{
232 __le16 max_outstanding_tx
;
236 struct usbcan_msg_softinfo
{
239 __le16 max_outstanding_tx
;
246 struct kvaser_msg_busparams
{
256 struct kvaser_msg_tx_can
{
272 struct kvaser_msg_rx_can_header
{
277 struct leaf_msg_rx_can
{
285 struct usbcan_msg_rx_can
{
293 struct leaf_msg_chip_state_event
{
305 struct usbcan_msg_chip_state_event
{
317 struct kvaser_msg_tx_acknowledge_header
{
322 struct leaf_msg_tx_acknowledge
{
331 struct usbcan_msg_tx_acknowledge
{
339 struct leaf_msg_error_event
{
351 struct usbcan_msg_error_event
{
354 u8 tx_errors_count_ch0
;
355 u8 rx_errors_count_ch0
;
356 u8 tx_errors_count_ch1
;
357 u8 rx_errors_count_ch1
;
363 struct kvaser_msg_ctrl_mode
{
370 struct kvaser_msg_flush_queue
{
377 struct leaf_msg_log_message
{
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
;
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
;
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
;
416 struct kvaser_msg_tx_can tx_can
;
417 struct kvaser_msg_ctrl_mode ctrl_mode
;
418 struct kvaser_msg_flush_queue flush_queue
;
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
;
445 /* Context for an outstanding, not yet ACKed, transmission */
446 struct kvaser_usb_tx_urb_context
{
447 struct kvaser_usb_net_priv
*priv
;
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.
464 unsigned int nchannels
;
465 unsigned int max_tx_urbs
;
466 enum kvaser_usb_family family
;
469 void *rxbuf
[MAX_RX_URBS
];
470 dma_addr_t rxbuf_dma
[MAX_RX_URBS
];
473 struct kvaser_usb_net_priv
{
475 struct can_berr_counter bec
;
477 struct kvaser_usb
*dev
;
478 struct net_device
*netdev
;
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
)
560 return usb_bulk_msg(dev
->udev
,
561 usb_sndbulkpipe(dev
->udev
,
562 dev
->bulk_out
->bEndpointAddress
),
563 msg
, msg
->len
, &actual_len
,
567 static int kvaser_usb_wait_msg(const struct kvaser_usb
*dev
, u8 id
,
568 struct kvaser_msg
*msg
)
570 struct kvaser_msg
*tmp
;
575 unsigned long to
= jiffies
+ msecs_to_jiffies(USB_RECV_TIMEOUT
);
577 buf
= kzalloc(RX_BUFFER_SIZE
, GFP_KERNEL
);
582 err
= usb_bulk_msg(dev
->udev
,
583 usb_rcvbulkpipe(dev
->udev
,
584 dev
->bulk_in
->bEndpointAddress
),
585 buf
, RX_BUFFER_SIZE
, &actual_len
,
591 while (pos
<= actual_len
- MSG_HEADER_LEN
) {
594 /* Handle messages crossing the USB endpoint max packet
595 * size boundary. Check kvaser_usb_read_bulk_callback()
596 * for further details.
599 pos
= round_up(pos
, le16_to_cpu(dev
->bulk_in
->
604 if (pos
+ tmp
->len
> actual_len
) {
605 dev_err(dev
->udev
->dev
.parent
,
611 memcpy(msg
, tmp
, tmp
->len
);
617 } while (time_before(jiffies
, to
));
627 static int kvaser_usb_send_simple_msg(const struct kvaser_usb
*dev
,
628 u8 msg_id
, int channel
)
630 struct kvaser_msg
*msg
;
633 msg
= kmalloc(sizeof(*msg
), GFP_KERNEL
);
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
);
648 static int kvaser_usb_get_software_info(struct kvaser_usb
*dev
)
650 struct kvaser_msg msg
;
653 err
= kvaser_usb_send_simple_msg(dev
, CMD_GET_SOFTWARE_INFO
, 0);
657 err
= kvaser_usb_wait_msg(dev
, CMD_GET_SOFTWARE_INFO_REPLY
, &msg
);
661 switch (dev
->family
) {
663 dev
->fw_version
= le32_to_cpu(msg
.u
.leaf
.softinfo
.fw_version
);
665 le16_to_cpu(msg
.u
.leaf
.softinfo
.max_outstanding_tx
);
668 dev
->fw_version
= le32_to_cpu(msg
.u
.usbcan
.softinfo
.fw_version
);
670 le16_to_cpu(msg
.u
.usbcan
.softinfo
.max_outstanding_tx
);
677 static int kvaser_usb_get_card_info(struct kvaser_usb
*dev
)
679 struct kvaser_msg msg
;
682 err
= kvaser_usb_send_simple_msg(dev
, CMD_GET_CARD_INFO
, 0);
686 err
= kvaser_usb_wait_msg(dev
, CMD_GET_CARD_INFO_REPLY
, &msg
);
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
))
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
;
706 struct can_frame
*cf
;
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
);
719 priv
= dev
->nets
[channel
];
721 if (!netif_device_present(priv
->netdev
))
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
);
733 cf
->can_id
|= CAN_ERR_RESTARTED
;
736 stats
->rx_bytes
+= cf
->can_dlc
;
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
;
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
);
769 netdev_warn(netdev
, "urb status received: %d\n",
773 static int kvaser_usb_simple_msg_async(struct kvaser_usb_net_priv
*priv
,
776 struct kvaser_usb
*dev
= priv
->dev
;
777 struct net_device
*netdev
= priv
->netdev
;
778 struct kvaser_msg
*msg
;
783 urb
= usb_alloc_urb(0, GFP_ATOMIC
);
785 netdev_err(netdev
, "No memory left for URBs\n");
789 buf
= kmalloc(sizeof(struct kvaser_msg
), GFP_ATOMIC
);
795 msg
= (struct kvaser_msg
*)buf
;
796 msg
->len
= MSG_HEADER_LEN
+ sizeof(struct kvaser_msg_simple
);
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
),
804 kvaser_usb_simple_msg_callback
, netdev
);
805 usb_anchor_urb(urb
, &priv
->tx_submitted
);
807 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
809 netdev_err(netdev
, "Error transmitting URB\n");
810 usb_unanchor_urb(urb
);
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
;
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
) {
866 if (es
->leaf
.error_factor
) {
867 priv
->can
.can_stats
.bus_error
++;
872 if (es
->usbcan
.error_state
& USBCAN_ERROR_STATE_TX_ERROR
)
874 if (es
->usbcan
.error_state
& USBCAN_ERROR_STATE_RX_ERROR
)
876 if (es
->usbcan
.error_state
& USBCAN_ERROR_STATE_BUSERROR
) {
877 priv
->can
.can_stats
.bus_error
++;
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
};
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
);
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
);
923 memcpy(cf
, &tmp_cf
, sizeof(*cf
));
925 if (new_state
!= old_state
) {
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
) {
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
;
963 if (es
->usbcan
.error_state
& USBCAN_ERROR_STATE_BUSERROR
) {
964 cf
->can_id
|= CAN_ERR_BUSERROR
;
969 cf
->data
[6] = es
->txerr
;
970 cf
->data
[7] = es
->rxerr
;
973 stats
->rx_bytes
+= cf
->can_dlc
;
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
;
987 channel
= es
->channel
;
988 if (channel
>= dev
->nchannels
) {
989 dev_err(dev
->udev
->dev
.parent
,
990 "Invalid channel number (%d)\n", channel
);
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
;
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;
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
= { };
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
);
1030 case CMD_CAN_ERROR_EVENT
:
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
) {
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
);
1054 dev_err(dev
->udev
->dev
.parent
, "Invalid msg id (%d)\n",
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
= { };
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
;
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];
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;
1087 dev_err(dev
->udev
->dev
.parent
, "Invalid msg id (%d)\n",
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
|
1104 netdev_err(priv
->netdev
, "Unknown error (flags: 0x%02x)\n",
1105 msg
->u
.rx_can_header
.flag
);
1111 if (msg
->u
.rx_can_header
.flag
& MSG_FLAG_OVERRUN
) {
1112 stats
->rx_over_errors
++;
1115 skb
= alloc_can_err_skb(priv
->netdev
, &cf
);
1117 stats
->rx_dropped
++;
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
;
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
);
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
);
1153 } else if (msg
->u
.rx_can_header
.flag
& (MSG_FLAG_ERROR_FRAME
|
1155 MSG_FLAG_OVERRUN
)) {
1156 kvaser_usb_rx_can_err(priv
, msg
);
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
);
1165 switch (dev
->family
) {
1167 rx_msg
= msg
->u
.leaf
.rx_can
.msg
;
1170 rx_msg
= msg
->u
.usbcan
.rx_can
.msg
;
1174 skb
= alloc_can_skb(priv
->netdev
, &cf
);
1176 stats
->tx_dropped
++;
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
;
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
;
1192 memcpy(cf
->data
, &msg
->u
.leaf
.log_message
.data
,
1195 cf
->can_id
= ((rx_msg
[0] & 0x1f) << 6) | (rx_msg
[1] & 0x3f);
1197 if (msg
->id
== CMD_RX_EXT_MESSAGE
) {
1199 cf
->can_id
|= ((rx_msg
[2] & 0x0f) << 14) |
1200 ((rx_msg
[3] & 0xff) << 6) |
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
;
1210 memcpy(cf
->data
, &rx_msg
[6],
1214 stats
->rx_packets
++;
1215 stats
->rx_bytes
+= cf
->can_dlc
;
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
);
1231 priv
= dev
->nets
[channel
];
1233 if (completion_done(&priv
->start_comp
) &&
1234 netif_queue_stopped(priv
->netdev
)) {
1235 netif_wake_queue(priv
->netdev
);
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
);
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
)
1263 case CMD_START_CHIP_REPLY
:
1264 kvaser_usb_start_chip_reply(dev
, msg
);
1267 case CMD_STOP_CHIP_REPLY
:
1268 kvaser_usb_stop_chip_reply(dev
, msg
);
1271 case CMD_RX_STD_MESSAGE
:
1272 case CMD_RX_EXT_MESSAGE
:
1273 kvaser_usb_rx_can_msg(dev
, msg
);
1276 case CMD_LEAF_LOG_MESSAGE
:
1277 if (dev
->family
!= KVASER_LEAF
)
1279 kvaser_usb_rx_can_msg(dev
, msg
);
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
);
1287 kvaser_usbcan_rx_error(dev
, msg
);
1290 case CMD_TX_ACKNOWLEDGE
:
1291 kvaser_usb_tx_acknowledge(dev
, msg
);
1294 /* Ignored messages */
1295 case CMD_USBCAN_CLOCK_OVERFLOW_EVENT
:
1296 if (dev
->family
!= KVASER_USBCAN
)
1301 warn
: dev_warn(dev
->udev
->dev
.parent
,
1302 "Unhandled message (%d)\n", msg
->id
);
1307 static void kvaser_usb_read_bulk_callback(struct urb
*urb
)
1309 struct kvaser_usb
*dev
= urb
->context
;
1310 struct kvaser_msg
*msg
;
1314 switch (urb
->status
) {
1321 dev_info(dev
->udev
->dev
.parent
, "Rx URB aborted (%d)\n",
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
->
1344 if (pos
+ msg
->len
> urb
->actual_length
) {
1345 dev_err(dev
->udev
->dev
.parent
, "Format error\n");
1349 kvaser_usb_handle_message(dev
, msg
);
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
++) {
1366 netif_device_detach(dev
->nets
[i
]->netdev
);
1369 dev_err(dev
->udev
->dev
.parent
,
1370 "Failed resubmitting read bulk urb: %d\n", err
);
1376 static int kvaser_usb_setup_rx_urbs(struct kvaser_usb
*dev
)
1380 if (dev
->rxinitdone
)
1383 for (i
= 0; i
< MAX_RX_URBS
; i
++) {
1384 struct urb
*urb
= NULL
;
1388 urb
= usb_alloc_urb(0, GFP_KERNEL
);
1390 dev_warn(dev
->udev
->dev
.parent
,
1391 "No memory left for URBs\n");
1396 buf
= usb_alloc_coherent(dev
->udev
, RX_BUFFER_SIZE
,
1397 GFP_KERNEL
, &buf_dma
);
1399 dev_warn(dev
->udev
->dev
.parent
,
1400 "No memory left for USB buffer\n");
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
,
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
);
1418 usb_unanchor_urb(urb
);
1419 usb_free_coherent(dev
->udev
, RX_BUFFER_SIZE
, buf
,
1425 dev
->rxbuf
[i
] = buf
;
1426 dev
->rxbuf_dma
[i
] = buf_dma
;
1432 dev_warn(dev
->udev
->dev
.parent
,
1433 "Cannot setup read URBs, error %d\n", 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;
1445 static int kvaser_usb_set_opt_mode(const struct kvaser_usb_net_priv
*priv
)
1447 struct kvaser_msg
*msg
;
1450 msg
= kmalloc(sizeof(*msg
), GFP_KERNEL
);
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
;
1462 msg
->u
.ctrl_mode
.ctrl_mode
= KVASER_CTRL_MODE_NORMAL
;
1464 rc
= kvaser_usb_send_msg(priv
->dev
, msg
);
1470 static int kvaser_usb_start_chip(struct kvaser_usb_net_priv
*priv
)
1474 init_completion(&priv
->start_comp
);
1476 err
= kvaser_usb_send_simple_msg(priv
->dev
, CMD_START_CHIP
,
1481 if (!wait_for_completion_timeout(&priv
->start_comp
,
1482 msecs_to_jiffies(START_TIMEOUT
)))
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
;
1494 err
= open_candev(netdev
);
1498 err
= kvaser_usb_setup_rx_urbs(dev
);
1502 err
= kvaser_usb_set_opt_mode(priv
);
1506 err
= kvaser_usb_start_chip(priv
);
1508 netdev_warn(netdev
, "Cannot start device, error %d\n", err
);
1512 priv
->can
.state
= CAN_STATE_ERROR_ACTIVE
;
1517 close_candev(netdev
);
1521 static void kvaser_usb_reset_tx_urb_contexts(struct kvaser_usb_net_priv
*priv
)
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
)
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
,
1552 for (i
= 0; i
< dev
->nchannels
; i
++) {
1553 struct kvaser_usb_net_priv
*priv
= dev
->nets
[i
];
1556 kvaser_usb_unlink_tx_urbs(priv
);
1560 static int kvaser_usb_stop_chip(struct kvaser_usb_net_priv
*priv
)
1564 init_completion(&priv
->stop_comp
);
1566 err
= kvaser_usb_send_simple_msg(priv
->dev
, CMD_STOP_CHIP
,
1571 if (!wait_for_completion_timeout(&priv
->stop_comp
,
1572 msecs_to_jiffies(STOP_TIMEOUT
)))
1578 static int kvaser_usb_flush_queue(struct kvaser_usb_net_priv
*priv
)
1580 struct kvaser_msg
*msg
;
1583 msg
= kmalloc(sizeof(*msg
), GFP_KERNEL
);
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
);
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
;
1604 netif_stop_queue(netdev
);
1606 err
= kvaser_usb_flush_queue(priv
);
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
);
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
);
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
))
1635 priv
= context
->priv
;
1636 netdev
= priv
->netdev
;
1638 kfree(urb
->transfer_buffer
);
1640 if (!netif_device_present(netdev
))
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
;
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
);
1667 netdev_err(netdev
, "No memory left for URBs\n");
1668 stats
->tx_dropped
++;
1670 return NETDEV_TX_OK
;
1673 buf
= kmalloc(sizeof(struct kvaser_msg
), GFP_ATOMIC
);
1675 stats
->tx_dropped
++;
1681 msg
->len
= MSG_HEADER_LEN
+ sizeof(struct kvaser_msg_tx_can
);
1682 msg
->u
.tx_can
.channel
= priv
->channel
;
1684 switch (dev
->family
) {
1686 msg_tx_can_flags
= &msg
->u
.tx_can
.leaf
.flags
;
1689 msg_tx_can_flags
= &msg
->u
.tx_can
.usbcan
.flags
;
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;
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
);
1728 spin_unlock_irqrestore(&priv
->tx_contexts_lock
, flags
);
1730 /* This should never happen; it implies a flow control bug */
1732 netdev_warn(netdev
, "cannot find free context\n");
1735 ret
= NETDEV_TX_BUSY
;
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
),
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
++;
1767 netif_device_detach(netdev
);
1769 netdev_warn(netdev
, "Failed tx_urb %d\n", err
);
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
;
1808 msg
= kmalloc(sizeof(*msg
), GFP_KERNEL
);
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;
1824 msg
->u
.busparams
.no_samp
= 1;
1826 rc
= kvaser_usb_send_msg(dev
, msg
);
1832 static int kvaser_usb_set_mode(struct net_device
*netdev
,
1835 struct kvaser_usb_net_priv
*priv
= netdev_priv(netdev
);
1839 case CAN_MODE_START
:
1840 err
= kvaser_usb_simple_msg_async(priv
, CMD_START_CHIP
);
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
);
1861 static void kvaser_usb_remove_interfaces(struct kvaser_usb
*dev
)
1865 for (i
= 0; i
< dev
->nchannels
; i
++) {
1869 unregister_candev(dev
->nets
[i
]->netdev
);
1872 kvaser_usb_unlink_all_urbs(dev
);
1874 for (i
= 0; i
< dev
->nchannels
; i
++) {
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
;
1890 err
= kvaser_usb_send_simple_msg(dev
, CMD_RESET_CHIP
, channel
);
1894 netdev
= alloc_candev(sizeof(*priv
) +
1895 dev
->max_tx_urbs
* sizeof(*priv
->tx_contexts
),
1898 dev_err(&intf
->dev
, "Cannot alloc candev\n");
1902 priv
= netdev_priv(netdev
);
1904 init_usb_anchor(&priv
->tx_submitted
);
1905 init_completion(&priv
->start_comp
);
1906 init_completion(&priv
->stop_comp
);
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
);
1937 dev_err(&intf
->dev
, "Failed to register can device\n");
1938 free_candev(netdev
);
1939 dev
->nets
[channel
] = NULL
;
1943 netdev_dbg(netdev
, "device registered\n");
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
;
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
))
1964 if (!*out
&& usb_endpoint_is_bulk_out(endpoint
))
1967 /* use first bulk endpoint for in and out */
1975 static int kvaser_usb_probe(struct usb_interface
*intf
,
1976 const struct usb_device_id
*id
)
1978 struct kvaser_usb
*dev
;
1982 dev
= devm_kzalloc(&intf
->dev
, sizeof(*dev
), GFP_KERNEL
);
1986 if (kvaser_is_leaf(id
)) {
1987 dev
->family
= KVASER_LEAF
;
1988 } else if (kvaser_is_usbcan(id
)) {
1989 dev
->family
= KVASER_USBCAN
;
1992 "Product ID (%d) does not belong to any known Kvaser USB family",
1997 err
= kvaser_usb_get_endpoints(intf
, &dev
->bulk_in
, &dev
->bulk_out
);
1999 dev_err(&intf
->dev
, "Cannot get usb endpoint(s)");
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.
2015 err
= kvaser_usb_get_software_info(dev
);
2016 } while (--retry
&& err
== -ETIMEDOUT
);
2020 "Cannot get software infos, error %d\n", 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
);
2034 "Cannot get card infos, error %d\n", err
);
2038 for (i
= 0; i
< dev
->nchannels
; i
++) {
2039 err
= kvaser_usb_init_one(intf
, id
, i
);
2041 kvaser_usb_remove_interfaces(dev
);
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
);
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");