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 CAN_ERR_PROT_LOC_CRC_DEL
);
951 if (es
->leaf
.error_factor
& M16C_EF_FORME
)
952 cf
->data
[2] |= CAN_ERR_PROT_FORM
;
953 if (es
->leaf
.error_factor
& M16C_EF_STFE
)
954 cf
->data
[2] |= CAN_ERR_PROT_STUFF
;
955 if (es
->leaf
.error_factor
& M16C_EF_BITE0
)
956 cf
->data
[2] |= CAN_ERR_PROT_BIT0
;
957 if (es
->leaf
.error_factor
& M16C_EF_BITE1
)
958 cf
->data
[2] |= CAN_ERR_PROT_BIT1
;
959 if (es
->leaf
.error_factor
& M16C_EF_TRE
)
960 cf
->data
[2] |= CAN_ERR_PROT_TX
;
964 if (es
->usbcan
.error_state
& USBCAN_ERROR_STATE_BUSERROR
) {
965 cf
->can_id
|= CAN_ERR_BUSERROR
;
970 cf
->data
[6] = es
->txerr
;
971 cf
->data
[7] = es
->rxerr
;
974 stats
->rx_bytes
+= cf
->can_dlc
;
978 /* For USBCAN, report error to userspace iff the channels's errors counter
979 * has changed, or we're the only channel seeing a bus error state.
981 static void kvaser_usbcan_conditionally_rx_error(const struct kvaser_usb
*dev
,
982 struct kvaser_usb_error_summary
*es
)
984 struct kvaser_usb_net_priv
*priv
;
988 channel
= es
->channel
;
989 if (channel
>= dev
->nchannels
) {
990 dev_err(dev
->udev
->dev
.parent
,
991 "Invalid channel number (%d)\n", channel
);
995 priv
= dev
->nets
[channel
];
996 report_error
= false;
998 if (es
->txerr
!= priv
->bec
.txerr
) {
999 es
->usbcan
.error_state
|= USBCAN_ERROR_STATE_TX_ERROR
;
1000 report_error
= true;
1002 if (es
->rxerr
!= priv
->bec
.rxerr
) {
1003 es
->usbcan
.error_state
|= USBCAN_ERROR_STATE_RX_ERROR
;
1004 report_error
= true;
1006 if ((es
->status
& M16C_STATE_BUS_ERROR
) &&
1007 !(es
->usbcan
.other_ch_status
& M16C_STATE_BUS_ERROR
)) {
1008 es
->usbcan
.error_state
|= USBCAN_ERROR_STATE_BUSERROR
;
1009 report_error
= true;
1013 kvaser_usb_rx_error(dev
, es
);
1016 static void kvaser_usbcan_rx_error(const struct kvaser_usb
*dev
,
1017 const struct kvaser_msg
*msg
)
1019 struct kvaser_usb_error_summary es
= { };
1022 /* Sometimes errors are sent as unsolicited chip state events */
1023 case CMD_CHIP_STATE_EVENT
:
1024 es
.channel
= msg
->u
.usbcan
.chip_state_event
.channel
;
1025 es
.status
= msg
->u
.usbcan
.chip_state_event
.status
;
1026 es
.txerr
= msg
->u
.usbcan
.chip_state_event
.tx_errors_count
;
1027 es
.rxerr
= msg
->u
.usbcan
.chip_state_event
.rx_errors_count
;
1028 kvaser_usbcan_conditionally_rx_error(dev
, &es
);
1031 case CMD_CAN_ERROR_EVENT
:
1033 es
.status
= msg
->u
.usbcan
.error_event
.status_ch0
;
1034 es
.txerr
= msg
->u
.usbcan
.error_event
.tx_errors_count_ch0
;
1035 es
.rxerr
= msg
->u
.usbcan
.error_event
.rx_errors_count_ch0
;
1036 es
.usbcan
.other_ch_status
=
1037 msg
->u
.usbcan
.error_event
.status_ch1
;
1038 kvaser_usbcan_conditionally_rx_error(dev
, &es
);
1040 /* The USBCAN firmware supports up to 2 channels.
1041 * Now that ch0 was checked, check if ch1 has any errors.
1043 if (dev
->nchannels
== MAX_USBCAN_NET_DEVICES
) {
1045 es
.status
= msg
->u
.usbcan
.error_event
.status_ch1
;
1046 es
.txerr
= msg
->u
.usbcan
.error_event
.tx_errors_count_ch1
;
1047 es
.rxerr
= msg
->u
.usbcan
.error_event
.rx_errors_count_ch1
;
1048 es
.usbcan
.other_ch_status
=
1049 msg
->u
.usbcan
.error_event
.status_ch0
;
1050 kvaser_usbcan_conditionally_rx_error(dev
, &es
);
1055 dev_err(dev
->udev
->dev
.parent
, "Invalid msg id (%d)\n",
1060 static void kvaser_leaf_rx_error(const struct kvaser_usb
*dev
,
1061 const struct kvaser_msg
*msg
)
1063 struct kvaser_usb_error_summary es
= { };
1066 case CMD_CAN_ERROR_EVENT
:
1067 es
.channel
= msg
->u
.leaf
.error_event
.channel
;
1068 es
.status
= msg
->u
.leaf
.error_event
.status
;
1069 es
.txerr
= msg
->u
.leaf
.error_event
.tx_errors_count
;
1070 es
.rxerr
= msg
->u
.leaf
.error_event
.rx_errors_count
;
1071 es
.leaf
.error_factor
= msg
->u
.leaf
.error_event
.error_factor
;
1073 case CMD_LEAF_LOG_MESSAGE
:
1074 es
.channel
= msg
->u
.leaf
.log_message
.channel
;
1075 es
.status
= msg
->u
.leaf
.log_message
.data
[0];
1076 es
.txerr
= msg
->u
.leaf
.log_message
.data
[2];
1077 es
.rxerr
= msg
->u
.leaf
.log_message
.data
[3];
1078 es
.leaf
.error_factor
= msg
->u
.leaf
.log_message
.data
[1];
1080 case CMD_CHIP_STATE_EVENT
:
1081 es
.channel
= msg
->u
.leaf
.chip_state_event
.channel
;
1082 es
.status
= msg
->u
.leaf
.chip_state_event
.status
;
1083 es
.txerr
= msg
->u
.leaf
.chip_state_event
.tx_errors_count
;
1084 es
.rxerr
= msg
->u
.leaf
.chip_state_event
.rx_errors_count
;
1085 es
.leaf
.error_factor
= 0;
1088 dev_err(dev
->udev
->dev
.parent
, "Invalid msg id (%d)\n",
1093 kvaser_usb_rx_error(dev
, &es
);
1096 static void kvaser_usb_rx_can_err(const struct kvaser_usb_net_priv
*priv
,
1097 const struct kvaser_msg
*msg
)
1099 struct can_frame
*cf
;
1100 struct sk_buff
*skb
;
1101 struct net_device_stats
*stats
= &priv
->netdev
->stats
;
1103 if (msg
->u
.rx_can_header
.flag
& (MSG_FLAG_ERROR_FRAME
|
1105 netdev_err(priv
->netdev
, "Unknown error (flags: 0x%02x)\n",
1106 msg
->u
.rx_can_header
.flag
);
1112 if (msg
->u
.rx_can_header
.flag
& MSG_FLAG_OVERRUN
) {
1113 stats
->rx_over_errors
++;
1116 skb
= alloc_can_err_skb(priv
->netdev
, &cf
);
1118 stats
->rx_dropped
++;
1122 cf
->can_id
|= CAN_ERR_CRTL
;
1123 cf
->data
[1] = CAN_ERR_CRTL_RX_OVERFLOW
;
1125 stats
->rx_packets
++;
1126 stats
->rx_bytes
+= cf
->can_dlc
;
1131 static void kvaser_usb_rx_can_msg(const struct kvaser_usb
*dev
,
1132 const struct kvaser_msg
*msg
)
1134 struct kvaser_usb_net_priv
*priv
;
1135 struct can_frame
*cf
;
1136 struct sk_buff
*skb
;
1137 struct net_device_stats
*stats
;
1138 u8 channel
= msg
->u
.rx_can_header
.channel
;
1139 const u8
*rx_msg
= NULL
; /* GCC */
1141 if (channel
>= dev
->nchannels
) {
1142 dev_err(dev
->udev
->dev
.parent
,
1143 "Invalid channel number (%d)\n", channel
);
1147 priv
= dev
->nets
[channel
];
1148 stats
= &priv
->netdev
->stats
;
1150 if ((msg
->u
.rx_can_header
.flag
& MSG_FLAG_ERROR_FRAME
) &&
1151 (dev
->family
== KVASER_LEAF
&& msg
->id
== CMD_LEAF_LOG_MESSAGE
)) {
1152 kvaser_leaf_rx_error(dev
, msg
);
1154 } else if (msg
->u
.rx_can_header
.flag
& (MSG_FLAG_ERROR_FRAME
|
1156 MSG_FLAG_OVERRUN
)) {
1157 kvaser_usb_rx_can_err(priv
, msg
);
1159 } else if (msg
->u
.rx_can_header
.flag
& ~MSG_FLAG_REMOTE_FRAME
) {
1160 netdev_warn(priv
->netdev
,
1161 "Unhandled frame (flags: 0x%02x)",
1162 msg
->u
.rx_can_header
.flag
);
1166 switch (dev
->family
) {
1168 rx_msg
= msg
->u
.leaf
.rx_can
.msg
;
1171 rx_msg
= msg
->u
.usbcan
.rx_can
.msg
;
1175 skb
= alloc_can_skb(priv
->netdev
, &cf
);
1177 stats
->tx_dropped
++;
1181 if (dev
->family
== KVASER_LEAF
&& msg
->id
== CMD_LEAF_LOG_MESSAGE
) {
1182 cf
->can_id
= le32_to_cpu(msg
->u
.leaf
.log_message
.id
);
1183 if (cf
->can_id
& KVASER_EXTENDED_FRAME
)
1184 cf
->can_id
&= CAN_EFF_MASK
| CAN_EFF_FLAG
;
1186 cf
->can_id
&= CAN_SFF_MASK
;
1188 cf
->can_dlc
= get_can_dlc(msg
->u
.leaf
.log_message
.dlc
);
1190 if (msg
->u
.leaf
.log_message
.flags
& MSG_FLAG_REMOTE_FRAME
)
1191 cf
->can_id
|= CAN_RTR_FLAG
;
1193 memcpy(cf
->data
, &msg
->u
.leaf
.log_message
.data
,
1196 cf
->can_id
= ((rx_msg
[0] & 0x1f) << 6) | (rx_msg
[1] & 0x3f);
1198 if (msg
->id
== CMD_RX_EXT_MESSAGE
) {
1200 cf
->can_id
|= ((rx_msg
[2] & 0x0f) << 14) |
1201 ((rx_msg
[3] & 0xff) << 6) |
1203 cf
->can_id
|= CAN_EFF_FLAG
;
1206 cf
->can_dlc
= get_can_dlc(rx_msg
[5]);
1208 if (msg
->u
.rx_can_header
.flag
& MSG_FLAG_REMOTE_FRAME
)
1209 cf
->can_id
|= CAN_RTR_FLAG
;
1211 memcpy(cf
->data
, &rx_msg
[6],
1215 stats
->rx_packets
++;
1216 stats
->rx_bytes
+= cf
->can_dlc
;
1220 static void kvaser_usb_start_chip_reply(const struct kvaser_usb
*dev
,
1221 const struct kvaser_msg
*msg
)
1223 struct kvaser_usb_net_priv
*priv
;
1224 u8 channel
= msg
->u
.simple
.channel
;
1226 if (channel
>= dev
->nchannels
) {
1227 dev_err(dev
->udev
->dev
.parent
,
1228 "Invalid channel number (%d)\n", channel
);
1232 priv
= dev
->nets
[channel
];
1234 if (completion_done(&priv
->start_comp
) &&
1235 netif_queue_stopped(priv
->netdev
)) {
1236 netif_wake_queue(priv
->netdev
);
1238 netif_start_queue(priv
->netdev
);
1239 complete(&priv
->start_comp
);
1243 static void kvaser_usb_stop_chip_reply(const struct kvaser_usb
*dev
,
1244 const struct kvaser_msg
*msg
)
1246 struct kvaser_usb_net_priv
*priv
;
1247 u8 channel
= msg
->u
.simple
.channel
;
1249 if (channel
>= dev
->nchannels
) {
1250 dev_err(dev
->udev
->dev
.parent
,
1251 "Invalid channel number (%d)\n", channel
);
1255 priv
= dev
->nets
[channel
];
1257 complete(&priv
->stop_comp
);
1260 static void kvaser_usb_handle_message(const struct kvaser_usb
*dev
,
1261 const struct kvaser_msg
*msg
)
1264 case CMD_START_CHIP_REPLY
:
1265 kvaser_usb_start_chip_reply(dev
, msg
);
1268 case CMD_STOP_CHIP_REPLY
:
1269 kvaser_usb_stop_chip_reply(dev
, msg
);
1272 case CMD_RX_STD_MESSAGE
:
1273 case CMD_RX_EXT_MESSAGE
:
1274 kvaser_usb_rx_can_msg(dev
, msg
);
1277 case CMD_LEAF_LOG_MESSAGE
:
1278 if (dev
->family
!= KVASER_LEAF
)
1280 kvaser_usb_rx_can_msg(dev
, msg
);
1283 case CMD_CHIP_STATE_EVENT
:
1284 case CMD_CAN_ERROR_EVENT
:
1285 if (dev
->family
== KVASER_LEAF
)
1286 kvaser_leaf_rx_error(dev
, msg
);
1288 kvaser_usbcan_rx_error(dev
, msg
);
1291 case CMD_TX_ACKNOWLEDGE
:
1292 kvaser_usb_tx_acknowledge(dev
, msg
);
1295 /* Ignored messages */
1296 case CMD_USBCAN_CLOCK_OVERFLOW_EVENT
:
1297 if (dev
->family
!= KVASER_USBCAN
)
1302 warn
: dev_warn(dev
->udev
->dev
.parent
,
1303 "Unhandled message (%d)\n", msg
->id
);
1308 static void kvaser_usb_read_bulk_callback(struct urb
*urb
)
1310 struct kvaser_usb
*dev
= urb
->context
;
1311 struct kvaser_msg
*msg
;
1315 switch (urb
->status
) {
1322 dev_info(dev
->udev
->dev
.parent
, "Rx URB aborted (%d)\n",
1327 while (pos
<= urb
->actual_length
- MSG_HEADER_LEN
) {
1328 msg
= urb
->transfer_buffer
+ pos
;
1330 /* The Kvaser firmware can only read and write messages that
1331 * does not cross the USB's endpoint wMaxPacketSize boundary.
1332 * If a follow-up command crosses such boundary, firmware puts
1333 * a placeholder zero-length command in its place then aligns
1334 * the real command to the next max packet size.
1336 * Handle such cases or we're going to miss a significant
1337 * number of events in case of a heavy rx load on the bus.
1339 if (msg
->len
== 0) {
1340 pos
= round_up(pos
, le16_to_cpu(dev
->bulk_in
->
1345 if (pos
+ msg
->len
> urb
->actual_length
) {
1346 dev_err(dev
->udev
->dev
.parent
, "Format error\n");
1350 kvaser_usb_handle_message(dev
, msg
);
1355 usb_fill_bulk_urb(urb
, dev
->udev
,
1356 usb_rcvbulkpipe(dev
->udev
,
1357 dev
->bulk_in
->bEndpointAddress
),
1358 urb
->transfer_buffer
, RX_BUFFER_SIZE
,
1359 kvaser_usb_read_bulk_callback
, dev
);
1361 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
1362 if (err
== -ENODEV
) {
1363 for (i
= 0; i
< dev
->nchannels
; i
++) {
1367 netif_device_detach(dev
->nets
[i
]->netdev
);
1370 dev_err(dev
->udev
->dev
.parent
,
1371 "Failed resubmitting read bulk urb: %d\n", err
);
1377 static int kvaser_usb_setup_rx_urbs(struct kvaser_usb
*dev
)
1381 if (dev
->rxinitdone
)
1384 for (i
= 0; i
< MAX_RX_URBS
; i
++) {
1385 struct urb
*urb
= NULL
;
1389 urb
= usb_alloc_urb(0, GFP_KERNEL
);
1391 dev_warn(dev
->udev
->dev
.parent
,
1392 "No memory left for URBs\n");
1397 buf
= usb_alloc_coherent(dev
->udev
, RX_BUFFER_SIZE
,
1398 GFP_KERNEL
, &buf_dma
);
1400 dev_warn(dev
->udev
->dev
.parent
,
1401 "No memory left for USB buffer\n");
1407 usb_fill_bulk_urb(urb
, dev
->udev
,
1408 usb_rcvbulkpipe(dev
->udev
,
1409 dev
->bulk_in
->bEndpointAddress
),
1410 buf
, RX_BUFFER_SIZE
,
1411 kvaser_usb_read_bulk_callback
,
1413 urb
->transfer_dma
= buf_dma
;
1414 urb
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
1415 usb_anchor_urb(urb
, &dev
->rx_submitted
);
1417 err
= usb_submit_urb(urb
, GFP_KERNEL
);
1419 usb_unanchor_urb(urb
);
1420 usb_free_coherent(dev
->udev
, RX_BUFFER_SIZE
, buf
,
1426 dev
->rxbuf
[i
] = buf
;
1427 dev
->rxbuf_dma
[i
] = buf_dma
;
1433 dev_warn(dev
->udev
->dev
.parent
,
1434 "Cannot setup read URBs, error %d\n", err
);
1436 } else if (i
< MAX_RX_URBS
) {
1437 dev_warn(dev
->udev
->dev
.parent
,
1438 "RX performances may be slow\n");
1441 dev
->rxinitdone
= true;
1446 static int kvaser_usb_set_opt_mode(const struct kvaser_usb_net_priv
*priv
)
1448 struct kvaser_msg
*msg
;
1451 msg
= kmalloc(sizeof(*msg
), GFP_KERNEL
);
1455 msg
->id
= CMD_SET_CTRL_MODE
;
1456 msg
->len
= MSG_HEADER_LEN
+ sizeof(struct kvaser_msg_ctrl_mode
);
1457 msg
->u
.ctrl_mode
.tid
= 0xff;
1458 msg
->u
.ctrl_mode
.channel
= priv
->channel
;
1460 if (priv
->can
.ctrlmode
& CAN_CTRLMODE_LISTENONLY
)
1461 msg
->u
.ctrl_mode
.ctrl_mode
= KVASER_CTRL_MODE_SILENT
;
1463 msg
->u
.ctrl_mode
.ctrl_mode
= KVASER_CTRL_MODE_NORMAL
;
1465 rc
= kvaser_usb_send_msg(priv
->dev
, msg
);
1471 static int kvaser_usb_start_chip(struct kvaser_usb_net_priv
*priv
)
1475 init_completion(&priv
->start_comp
);
1477 err
= kvaser_usb_send_simple_msg(priv
->dev
, CMD_START_CHIP
,
1482 if (!wait_for_completion_timeout(&priv
->start_comp
,
1483 msecs_to_jiffies(START_TIMEOUT
)))
1489 static int kvaser_usb_open(struct net_device
*netdev
)
1491 struct kvaser_usb_net_priv
*priv
= netdev_priv(netdev
);
1492 struct kvaser_usb
*dev
= priv
->dev
;
1495 err
= open_candev(netdev
);
1499 err
= kvaser_usb_setup_rx_urbs(dev
);
1503 err
= kvaser_usb_set_opt_mode(priv
);
1507 err
= kvaser_usb_start_chip(priv
);
1509 netdev_warn(netdev
, "Cannot start device, error %d\n", err
);
1513 priv
->can
.state
= CAN_STATE_ERROR_ACTIVE
;
1518 close_candev(netdev
);
1522 static void kvaser_usb_reset_tx_urb_contexts(struct kvaser_usb_net_priv
*priv
)
1526 max_tx_urbs
= priv
->dev
->max_tx_urbs
;
1528 priv
->active_tx_contexts
= 0;
1529 for (i
= 0; i
< max_tx_urbs
; i
++)
1530 priv
->tx_contexts
[i
].echo_index
= max_tx_urbs
;
1533 /* This method might sleep. Do not call it in the atomic context
1534 * of URB completions.
1536 static void kvaser_usb_unlink_tx_urbs(struct kvaser_usb_net_priv
*priv
)
1538 usb_kill_anchored_urbs(&priv
->tx_submitted
);
1539 kvaser_usb_reset_tx_urb_contexts(priv
);
1542 static void kvaser_usb_unlink_all_urbs(struct kvaser_usb
*dev
)
1546 usb_kill_anchored_urbs(&dev
->rx_submitted
);
1548 for (i
= 0; i
< MAX_RX_URBS
; i
++)
1549 usb_free_coherent(dev
->udev
, RX_BUFFER_SIZE
,
1553 for (i
= 0; i
< dev
->nchannels
; i
++) {
1554 struct kvaser_usb_net_priv
*priv
= dev
->nets
[i
];
1557 kvaser_usb_unlink_tx_urbs(priv
);
1561 static int kvaser_usb_stop_chip(struct kvaser_usb_net_priv
*priv
)
1565 init_completion(&priv
->stop_comp
);
1567 err
= kvaser_usb_send_simple_msg(priv
->dev
, CMD_STOP_CHIP
,
1572 if (!wait_for_completion_timeout(&priv
->stop_comp
,
1573 msecs_to_jiffies(STOP_TIMEOUT
)))
1579 static int kvaser_usb_flush_queue(struct kvaser_usb_net_priv
*priv
)
1581 struct kvaser_msg
*msg
;
1584 msg
= kmalloc(sizeof(*msg
), GFP_KERNEL
);
1588 msg
->id
= CMD_FLUSH_QUEUE
;
1589 msg
->len
= MSG_HEADER_LEN
+ sizeof(struct kvaser_msg_flush_queue
);
1590 msg
->u
.flush_queue
.channel
= priv
->channel
;
1591 msg
->u
.flush_queue
.flags
= 0x00;
1593 rc
= kvaser_usb_send_msg(priv
->dev
, msg
);
1599 static int kvaser_usb_close(struct net_device
*netdev
)
1601 struct kvaser_usb_net_priv
*priv
= netdev_priv(netdev
);
1602 struct kvaser_usb
*dev
= priv
->dev
;
1605 netif_stop_queue(netdev
);
1607 err
= kvaser_usb_flush_queue(priv
);
1609 netdev_warn(netdev
, "Cannot flush queue, error %d\n", err
);
1611 if (kvaser_usb_send_simple_msg(dev
, CMD_RESET_CHIP
, priv
->channel
))
1612 netdev_warn(netdev
, "Cannot reset card, error %d\n", err
);
1614 err
= kvaser_usb_stop_chip(priv
);
1616 netdev_warn(netdev
, "Cannot stop device, error %d\n", err
);
1618 /* reset tx contexts */
1619 kvaser_usb_unlink_tx_urbs(priv
);
1621 priv
->can
.state
= CAN_STATE_STOPPED
;
1622 close_candev(priv
->netdev
);
1627 static void kvaser_usb_write_bulk_callback(struct urb
*urb
)
1629 struct kvaser_usb_tx_urb_context
*context
= urb
->context
;
1630 struct kvaser_usb_net_priv
*priv
;
1631 struct net_device
*netdev
;
1633 if (WARN_ON(!context
))
1636 priv
= context
->priv
;
1637 netdev
= priv
->netdev
;
1639 kfree(urb
->transfer_buffer
);
1641 if (!netif_device_present(netdev
))
1645 netdev_info(netdev
, "Tx URB aborted (%d)\n", urb
->status
);
1648 static netdev_tx_t
kvaser_usb_start_xmit(struct sk_buff
*skb
,
1649 struct net_device
*netdev
)
1651 struct kvaser_usb_net_priv
*priv
= netdev_priv(netdev
);
1652 struct kvaser_usb
*dev
= priv
->dev
;
1653 struct net_device_stats
*stats
= &netdev
->stats
;
1654 struct can_frame
*cf
= (struct can_frame
*)skb
->data
;
1655 struct kvaser_usb_tx_urb_context
*context
= NULL
;
1658 struct kvaser_msg
*msg
;
1659 int i
, err
, ret
= NETDEV_TX_OK
;
1660 u8
*msg_tx_can_flags
= NULL
; /* GCC */
1661 unsigned long flags
;
1663 if (can_dropped_invalid_skb(netdev
, skb
))
1664 return NETDEV_TX_OK
;
1666 urb
= usb_alloc_urb(0, GFP_ATOMIC
);
1668 netdev_err(netdev
, "No memory left for URBs\n");
1669 stats
->tx_dropped
++;
1671 return NETDEV_TX_OK
;
1674 buf
= kmalloc(sizeof(struct kvaser_msg
), GFP_ATOMIC
);
1676 stats
->tx_dropped
++;
1682 msg
->len
= MSG_HEADER_LEN
+ sizeof(struct kvaser_msg_tx_can
);
1683 msg
->u
.tx_can
.channel
= priv
->channel
;
1685 switch (dev
->family
) {
1687 msg_tx_can_flags
= &msg
->u
.tx_can
.leaf
.flags
;
1690 msg_tx_can_flags
= &msg
->u
.tx_can
.usbcan
.flags
;
1694 *msg_tx_can_flags
= 0;
1696 if (cf
->can_id
& CAN_EFF_FLAG
) {
1697 msg
->id
= CMD_TX_EXT_MESSAGE
;
1698 msg
->u
.tx_can
.msg
[0] = (cf
->can_id
>> 24) & 0x1f;
1699 msg
->u
.tx_can
.msg
[1] = (cf
->can_id
>> 18) & 0x3f;
1700 msg
->u
.tx_can
.msg
[2] = (cf
->can_id
>> 14) & 0x0f;
1701 msg
->u
.tx_can
.msg
[3] = (cf
->can_id
>> 6) & 0xff;
1702 msg
->u
.tx_can
.msg
[4] = cf
->can_id
& 0x3f;
1704 msg
->id
= CMD_TX_STD_MESSAGE
;
1705 msg
->u
.tx_can
.msg
[0] = (cf
->can_id
>> 6) & 0x1f;
1706 msg
->u
.tx_can
.msg
[1] = cf
->can_id
& 0x3f;
1709 msg
->u
.tx_can
.msg
[5] = cf
->can_dlc
;
1710 memcpy(&msg
->u
.tx_can
.msg
[6], cf
->data
, cf
->can_dlc
);
1712 if (cf
->can_id
& CAN_RTR_FLAG
)
1713 *msg_tx_can_flags
|= MSG_FLAG_REMOTE_FRAME
;
1715 spin_lock_irqsave(&priv
->tx_contexts_lock
, flags
);
1716 for (i
= 0; i
< dev
->max_tx_urbs
; i
++) {
1717 if (priv
->tx_contexts
[i
].echo_index
== dev
->max_tx_urbs
) {
1718 context
= &priv
->tx_contexts
[i
];
1720 context
->echo_index
= i
;
1721 can_put_echo_skb(skb
, netdev
, context
->echo_index
);
1722 ++priv
->active_tx_contexts
;
1723 if (priv
->active_tx_contexts
>= dev
->max_tx_urbs
)
1724 netif_stop_queue(netdev
);
1729 spin_unlock_irqrestore(&priv
->tx_contexts_lock
, flags
);
1731 /* This should never happen; it implies a flow control bug */
1733 netdev_warn(netdev
, "cannot find free context\n");
1736 ret
= NETDEV_TX_BUSY
;
1740 context
->priv
= priv
;
1741 context
->dlc
= cf
->can_dlc
;
1743 msg
->u
.tx_can
.tid
= context
->echo_index
;
1745 usb_fill_bulk_urb(urb
, dev
->udev
,
1746 usb_sndbulkpipe(dev
->udev
,
1747 dev
->bulk_out
->bEndpointAddress
),
1749 kvaser_usb_write_bulk_callback
, context
);
1750 usb_anchor_urb(urb
, &priv
->tx_submitted
);
1752 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
1753 if (unlikely(err
)) {
1754 spin_lock_irqsave(&priv
->tx_contexts_lock
, flags
);
1756 can_free_echo_skb(netdev
, context
->echo_index
);
1757 context
->echo_index
= dev
->max_tx_urbs
;
1758 --priv
->active_tx_contexts
;
1759 netif_wake_queue(netdev
);
1761 spin_unlock_irqrestore(&priv
->tx_contexts_lock
, flags
);
1763 usb_unanchor_urb(urb
);
1765 stats
->tx_dropped
++;
1768 netif_device_detach(netdev
);
1770 netdev_warn(netdev
, "Failed tx_urb %d\n", err
);
1782 static const struct net_device_ops kvaser_usb_netdev_ops
= {
1783 .ndo_open
= kvaser_usb_open
,
1784 .ndo_stop
= kvaser_usb_close
,
1785 .ndo_start_xmit
= kvaser_usb_start_xmit
,
1786 .ndo_change_mtu
= can_change_mtu
,
1789 static const struct can_bittiming_const kvaser_usb_bittiming_const
= {
1790 .name
= "kvaser_usb",
1791 .tseg1_min
= KVASER_USB_TSEG1_MIN
,
1792 .tseg1_max
= KVASER_USB_TSEG1_MAX
,
1793 .tseg2_min
= KVASER_USB_TSEG2_MIN
,
1794 .tseg2_max
= KVASER_USB_TSEG2_MAX
,
1795 .sjw_max
= KVASER_USB_SJW_MAX
,
1796 .brp_min
= KVASER_USB_BRP_MIN
,
1797 .brp_max
= KVASER_USB_BRP_MAX
,
1798 .brp_inc
= KVASER_USB_BRP_INC
,
1801 static int kvaser_usb_set_bittiming(struct net_device
*netdev
)
1803 struct kvaser_usb_net_priv
*priv
= netdev_priv(netdev
);
1804 struct can_bittiming
*bt
= &priv
->can
.bittiming
;
1805 struct kvaser_usb
*dev
= priv
->dev
;
1806 struct kvaser_msg
*msg
;
1809 msg
= kmalloc(sizeof(*msg
), GFP_KERNEL
);
1813 msg
->id
= CMD_SET_BUS_PARAMS
;
1814 msg
->len
= MSG_HEADER_LEN
+ sizeof(struct kvaser_msg_busparams
);
1815 msg
->u
.busparams
.channel
= priv
->channel
;
1816 msg
->u
.busparams
.tid
= 0xff;
1817 msg
->u
.busparams
.bitrate
= cpu_to_le32(bt
->bitrate
);
1818 msg
->u
.busparams
.sjw
= bt
->sjw
;
1819 msg
->u
.busparams
.tseg1
= bt
->prop_seg
+ bt
->phase_seg1
;
1820 msg
->u
.busparams
.tseg2
= bt
->phase_seg2
;
1822 if (priv
->can
.ctrlmode
& CAN_CTRLMODE_3_SAMPLES
)
1823 msg
->u
.busparams
.no_samp
= 3;
1825 msg
->u
.busparams
.no_samp
= 1;
1827 rc
= kvaser_usb_send_msg(dev
, msg
);
1833 static int kvaser_usb_set_mode(struct net_device
*netdev
,
1836 struct kvaser_usb_net_priv
*priv
= netdev_priv(netdev
);
1840 case CAN_MODE_START
:
1841 err
= kvaser_usb_simple_msg_async(priv
, CMD_START_CHIP
);
1852 static int kvaser_usb_get_berr_counter(const struct net_device
*netdev
,
1853 struct can_berr_counter
*bec
)
1855 struct kvaser_usb_net_priv
*priv
= netdev_priv(netdev
);
1862 static void kvaser_usb_remove_interfaces(struct kvaser_usb
*dev
)
1866 for (i
= 0; i
< dev
->nchannels
; i
++) {
1870 unregister_candev(dev
->nets
[i
]->netdev
);
1873 kvaser_usb_unlink_all_urbs(dev
);
1875 for (i
= 0; i
< dev
->nchannels
; i
++) {
1879 free_candev(dev
->nets
[i
]->netdev
);
1883 static int kvaser_usb_init_one(struct usb_interface
*intf
,
1884 const struct usb_device_id
*id
, int channel
)
1886 struct kvaser_usb
*dev
= usb_get_intfdata(intf
);
1887 struct net_device
*netdev
;
1888 struct kvaser_usb_net_priv
*priv
;
1891 err
= kvaser_usb_send_simple_msg(dev
, CMD_RESET_CHIP
, channel
);
1895 netdev
= alloc_candev(sizeof(*priv
) +
1896 dev
->max_tx_urbs
* sizeof(*priv
->tx_contexts
),
1899 dev_err(&intf
->dev
, "Cannot alloc candev\n");
1903 priv
= netdev_priv(netdev
);
1905 init_usb_anchor(&priv
->tx_submitted
);
1906 init_completion(&priv
->start_comp
);
1907 init_completion(&priv
->stop_comp
);
1910 priv
->netdev
= netdev
;
1911 priv
->channel
= channel
;
1913 spin_lock_init(&priv
->tx_contexts_lock
);
1914 kvaser_usb_reset_tx_urb_contexts(priv
);
1916 priv
->can
.state
= CAN_STATE_STOPPED
;
1917 priv
->can
.clock
.freq
= CAN_USB_CLOCK
;
1918 priv
->can
.bittiming_const
= &kvaser_usb_bittiming_const
;
1919 priv
->can
.do_set_bittiming
= kvaser_usb_set_bittiming
;
1920 priv
->can
.do_set_mode
= kvaser_usb_set_mode
;
1921 if (id
->driver_info
& KVASER_HAS_TXRX_ERRORS
)
1922 priv
->can
.do_get_berr_counter
= kvaser_usb_get_berr_counter
;
1923 priv
->can
.ctrlmode_supported
= CAN_CTRLMODE_3_SAMPLES
;
1924 if (id
->driver_info
& KVASER_HAS_SILENT_MODE
)
1925 priv
->can
.ctrlmode_supported
|= CAN_CTRLMODE_LISTENONLY
;
1927 netdev
->flags
|= IFF_ECHO
;
1929 netdev
->netdev_ops
= &kvaser_usb_netdev_ops
;
1931 SET_NETDEV_DEV(netdev
, &intf
->dev
);
1932 netdev
->dev_id
= channel
;
1934 dev
->nets
[channel
] = priv
;
1936 err
= register_candev(netdev
);
1938 dev_err(&intf
->dev
, "Failed to register can device\n");
1939 free_candev(netdev
);
1940 dev
->nets
[channel
] = NULL
;
1944 netdev_dbg(netdev
, "device registered\n");
1949 static int kvaser_usb_get_endpoints(const struct usb_interface
*intf
,
1950 struct usb_endpoint_descriptor
**in
,
1951 struct usb_endpoint_descriptor
**out
)
1953 const struct usb_host_interface
*iface_desc
;
1954 struct usb_endpoint_descriptor
*endpoint
;
1957 iface_desc
= &intf
->altsetting
[0];
1959 for (i
= 0; i
< iface_desc
->desc
.bNumEndpoints
; ++i
) {
1960 endpoint
= &iface_desc
->endpoint
[i
].desc
;
1962 if (!*in
&& usb_endpoint_is_bulk_in(endpoint
))
1965 if (!*out
&& usb_endpoint_is_bulk_out(endpoint
))
1968 /* use first bulk endpoint for in and out */
1976 static int kvaser_usb_probe(struct usb_interface
*intf
,
1977 const struct usb_device_id
*id
)
1979 struct kvaser_usb
*dev
;
1983 dev
= devm_kzalloc(&intf
->dev
, sizeof(*dev
), GFP_KERNEL
);
1987 if (kvaser_is_leaf(id
)) {
1988 dev
->family
= KVASER_LEAF
;
1989 } else if (kvaser_is_usbcan(id
)) {
1990 dev
->family
= KVASER_USBCAN
;
1993 "Product ID (%d) does not belong to any known Kvaser USB family",
1998 err
= kvaser_usb_get_endpoints(intf
, &dev
->bulk_in
, &dev
->bulk_out
);
2000 dev_err(&intf
->dev
, "Cannot get usb endpoint(s)");
2004 dev
->udev
= interface_to_usbdev(intf
);
2006 init_usb_anchor(&dev
->rx_submitted
);
2008 usb_set_intfdata(intf
, dev
);
2010 /* On some x86 laptops, plugging a Kvaser device again after
2011 * an unplug makes the firmware always ignore the very first
2012 * command. For such a case, provide some room for retries
2013 * instead of completely exiting the driver.
2016 err
= kvaser_usb_get_software_info(dev
);
2017 } while (--retry
&& err
== -ETIMEDOUT
);
2021 "Cannot get software infos, error %d\n", err
);
2025 dev_dbg(&intf
->dev
, "Firmware version: %d.%d.%d\n",
2026 ((dev
->fw_version
>> 24) & 0xff),
2027 ((dev
->fw_version
>> 16) & 0xff),
2028 (dev
->fw_version
& 0xffff));
2030 dev_dbg(&intf
->dev
, "Max oustanding tx = %d URBs\n", dev
->max_tx_urbs
);
2032 err
= kvaser_usb_get_card_info(dev
);
2035 "Cannot get card infos, error %d\n", err
);
2039 for (i
= 0; i
< dev
->nchannels
; i
++) {
2040 err
= kvaser_usb_init_one(intf
, id
, i
);
2042 kvaser_usb_remove_interfaces(dev
);
2050 static void kvaser_usb_disconnect(struct usb_interface
*intf
)
2052 struct kvaser_usb
*dev
= usb_get_intfdata(intf
);
2054 usb_set_intfdata(intf
, NULL
);
2059 kvaser_usb_remove_interfaces(dev
);
2062 static struct usb_driver kvaser_usb_driver
= {
2063 .name
= "kvaser_usb",
2064 .probe
= kvaser_usb_probe
,
2065 .disconnect
= kvaser_usb_disconnect
,
2066 .id_table
= kvaser_usb_table
,
2069 module_usb_driver(kvaser_usb_driver
);
2071 MODULE_AUTHOR("Olivier Sobrie <olivier@sobrie.be>");
2072 MODULE_DESCRIPTION("CAN driver for Kvaser CAN/USB devices");
2073 MODULE_LICENSE("GPL v2");