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
10 * Copyright (C) 2002-2006 KVASER AB, Sweden. All rights reserved.
11 * Copyright (C) 2010 Matthias Fuchs <matthias.fuchs@esd.eu>, esd gmbh
12 * Copyright (C) 2012 Olivier Sobrie <olivier@sobrie.be>
15 #include <linux/spinlock.h>
16 #include <linux/kernel.h>
17 #include <linux/completion.h>
18 #include <linux/module.h>
19 #include <linux/netdevice.h>
20 #include <linux/usb.h>
22 #include <linux/can.h>
23 #include <linux/can/dev.h>
24 #include <linux/can/error.h>
26 #define MAX_TX_URBS 16
28 #define START_TIMEOUT 1000 /* msecs */
29 #define STOP_TIMEOUT 1000 /* msecs */
30 #define USB_SEND_TIMEOUT 1000 /* msecs */
31 #define USB_RECV_TIMEOUT 1000 /* msecs */
32 #define RX_BUFFER_SIZE 3072
33 #define CAN_USB_CLOCK 8000000
34 #define MAX_NET_DEVICES 3
36 /* Kvaser USB devices */
37 #define KVASER_VENDOR_ID 0x0bfd
38 #define USB_LEAF_DEVEL_PRODUCT_ID 10
39 #define USB_LEAF_LITE_PRODUCT_ID 11
40 #define USB_LEAF_PRO_PRODUCT_ID 12
41 #define USB_LEAF_SPRO_PRODUCT_ID 14
42 #define USB_LEAF_PRO_LS_PRODUCT_ID 15
43 #define USB_LEAF_PRO_SWC_PRODUCT_ID 16
44 #define USB_LEAF_PRO_LIN_PRODUCT_ID 17
45 #define USB_LEAF_SPRO_LS_PRODUCT_ID 18
46 #define USB_LEAF_SPRO_SWC_PRODUCT_ID 19
47 #define USB_MEMO2_DEVEL_PRODUCT_ID 22
48 #define USB_MEMO2_HSHS_PRODUCT_ID 23
49 #define USB_UPRO_HSHS_PRODUCT_ID 24
50 #define USB_LEAF_LITE_GI_PRODUCT_ID 25
51 #define USB_LEAF_PRO_OBDII_PRODUCT_ID 26
52 #define USB_MEMO2_HSLS_PRODUCT_ID 27
53 #define USB_LEAF_LITE_CH_PRODUCT_ID 28
54 #define USB_BLACKBIRD_SPRO_PRODUCT_ID 29
55 #define USB_OEM_MERCURY_PRODUCT_ID 34
56 #define USB_OEM_LEAF_PRODUCT_ID 35
57 #define USB_CAN_R_PRODUCT_ID 39
58 #define USB_LEAF_LITE_V2_PRODUCT_ID 288
59 #define USB_MINI_PCIE_HS_PRODUCT_ID 289
61 /* USB devices features */
62 #define KVASER_HAS_SILENT_MODE BIT(0)
63 #define KVASER_HAS_TXRX_ERRORS BIT(1)
65 /* Message header size */
66 #define MSG_HEADER_LEN 2
68 /* Can message flags */
69 #define MSG_FLAG_ERROR_FRAME BIT(0)
70 #define MSG_FLAG_OVERRUN BIT(1)
71 #define MSG_FLAG_NERR BIT(2)
72 #define MSG_FLAG_WAKEUP BIT(3)
73 #define MSG_FLAG_REMOTE_FRAME BIT(4)
74 #define MSG_FLAG_RESERVED BIT(5)
75 #define MSG_FLAG_TX_ACK BIT(6)
76 #define MSG_FLAG_TX_REQUEST BIT(7)
79 #define M16C_STATE_BUS_RESET BIT(0)
80 #define M16C_STATE_BUS_ERROR BIT(4)
81 #define M16C_STATE_BUS_PASSIVE BIT(5)
82 #define M16C_STATE_BUS_OFF BIT(6)
85 #define CMD_RX_STD_MESSAGE 12
86 #define CMD_TX_STD_MESSAGE 13
87 #define CMD_RX_EXT_MESSAGE 14
88 #define CMD_TX_EXT_MESSAGE 15
89 #define CMD_SET_BUS_PARAMS 16
90 #define CMD_GET_BUS_PARAMS 17
91 #define CMD_GET_BUS_PARAMS_REPLY 18
92 #define CMD_GET_CHIP_STATE 19
93 #define CMD_CHIP_STATE_EVENT 20
94 #define CMD_SET_CTRL_MODE 21
95 #define CMD_GET_CTRL_MODE 22
96 #define CMD_GET_CTRL_MODE_REPLY 23
97 #define CMD_RESET_CHIP 24
98 #define CMD_RESET_CARD 25
99 #define CMD_START_CHIP 26
100 #define CMD_START_CHIP_REPLY 27
101 #define CMD_STOP_CHIP 28
102 #define CMD_STOP_CHIP_REPLY 29
103 #define CMD_GET_CARD_INFO2 32
104 #define CMD_GET_CARD_INFO 34
105 #define CMD_GET_CARD_INFO_REPLY 35
106 #define CMD_GET_SOFTWARE_INFO 38
107 #define CMD_GET_SOFTWARE_INFO_REPLY 39
108 #define CMD_ERROR_EVENT 45
109 #define CMD_FLUSH_QUEUE 48
110 #define CMD_RESET_ERROR_COUNTER 49
111 #define CMD_TX_ACKNOWLEDGE 50
112 #define CMD_CAN_ERROR_EVENT 51
113 #define CMD_USB_THROTTLE 77
114 #define CMD_LOG_MESSAGE 106
117 #define M16C_EF_ACKE BIT(0)
118 #define M16C_EF_CRCE BIT(1)
119 #define M16C_EF_FORME BIT(2)
120 #define M16C_EF_STFE BIT(3)
121 #define M16C_EF_BITE0 BIT(4)
122 #define M16C_EF_BITE1 BIT(5)
123 #define M16C_EF_RCVE BIT(6)
124 #define M16C_EF_TRE BIT(7)
126 /* bittiming parameters */
127 #define KVASER_USB_TSEG1_MIN 1
128 #define KVASER_USB_TSEG1_MAX 16
129 #define KVASER_USB_TSEG2_MIN 1
130 #define KVASER_USB_TSEG2_MAX 8
131 #define KVASER_USB_SJW_MAX 4
132 #define KVASER_USB_BRP_MIN 1
133 #define KVASER_USB_BRP_MAX 64
134 #define KVASER_USB_BRP_INC 1
137 #define KVASER_CTRL_MODE_NORMAL 1
138 #define KVASER_CTRL_MODE_SILENT 2
139 #define KVASER_CTRL_MODE_SELFRECEPTION 3
140 #define KVASER_CTRL_MODE_OFF 4
143 #define KVASER_EXTENDED_FRAME BIT(31)
145 struct kvaser_msg_simple
{
150 struct kvaser_msg_cardinfo
{
153 __le32 serial_number
;
155 __le32 clock_resolution
;
163 struct kvaser_msg_cardinfo2
{
167 __le32 oem_unlock_code
;
170 struct kvaser_msg_softinfo
{
175 __le16 max_outstanding_tx
;
179 struct kvaser_msg_busparams
{
189 struct kvaser_msg_tx_can
{
197 struct kvaser_msg_rx_can
{
204 struct kvaser_msg_chip_state_event
{
214 struct kvaser_msg_tx_acknowledge
{
222 struct kvaser_msg_error_event
{
234 struct kvaser_msg_ctrl_mode
{
241 struct kvaser_msg_flush_queue
{
248 struct kvaser_msg_log_message
{
262 struct kvaser_msg_simple simple
;
263 struct kvaser_msg_cardinfo cardinfo
;
264 struct kvaser_msg_cardinfo2 cardinfo2
;
265 struct kvaser_msg_softinfo softinfo
;
266 struct kvaser_msg_busparams busparams
;
267 struct kvaser_msg_tx_can tx_can
;
268 struct kvaser_msg_rx_can rx_can
;
269 struct kvaser_msg_chip_state_event chip_state_event
;
270 struct kvaser_msg_tx_acknowledge tx_acknowledge
;
271 struct kvaser_msg_error_event error_event
;
272 struct kvaser_msg_ctrl_mode ctrl_mode
;
273 struct kvaser_msg_flush_queue flush_queue
;
274 struct kvaser_msg_log_message log_message
;
278 struct kvaser_usb_tx_urb_context
{
279 struct kvaser_usb_net_priv
*priv
;
285 struct usb_device
*udev
;
286 struct kvaser_usb_net_priv
*nets
[MAX_NET_DEVICES
];
288 struct usb_endpoint_descriptor
*bulk_in
, *bulk_out
;
289 struct usb_anchor rx_submitted
;
292 unsigned int nchannels
;
295 void *rxbuf
[MAX_RX_URBS
];
296 dma_addr_t rxbuf_dma
[MAX_RX_URBS
];
299 struct kvaser_usb_net_priv
{
302 spinlock_t tx_contexts_lock
;
303 int active_tx_contexts
;
304 struct kvaser_usb_tx_urb_context tx_contexts
[MAX_TX_URBS
];
306 struct usb_anchor tx_submitted
;
307 struct completion start_comp
, stop_comp
;
309 struct kvaser_usb
*dev
;
310 struct net_device
*netdev
;
313 struct can_berr_counter bec
;
316 static const struct usb_device_id kvaser_usb_table
[] = {
317 { USB_DEVICE(KVASER_VENDOR_ID
, USB_LEAF_DEVEL_PRODUCT_ID
) },
318 { USB_DEVICE(KVASER_VENDOR_ID
, USB_LEAF_LITE_PRODUCT_ID
) },
319 { USB_DEVICE(KVASER_VENDOR_ID
, USB_LEAF_PRO_PRODUCT_ID
),
320 .driver_info
= KVASER_HAS_TXRX_ERRORS
|
321 KVASER_HAS_SILENT_MODE
},
322 { USB_DEVICE(KVASER_VENDOR_ID
, USB_LEAF_SPRO_PRODUCT_ID
),
323 .driver_info
= KVASER_HAS_TXRX_ERRORS
|
324 KVASER_HAS_SILENT_MODE
},
325 { USB_DEVICE(KVASER_VENDOR_ID
, USB_LEAF_PRO_LS_PRODUCT_ID
),
326 .driver_info
= KVASER_HAS_TXRX_ERRORS
|
327 KVASER_HAS_SILENT_MODE
},
328 { USB_DEVICE(KVASER_VENDOR_ID
, USB_LEAF_PRO_SWC_PRODUCT_ID
),
329 .driver_info
= KVASER_HAS_TXRX_ERRORS
|
330 KVASER_HAS_SILENT_MODE
},
331 { USB_DEVICE(KVASER_VENDOR_ID
, USB_LEAF_PRO_LIN_PRODUCT_ID
),
332 .driver_info
= KVASER_HAS_TXRX_ERRORS
|
333 KVASER_HAS_SILENT_MODE
},
334 { USB_DEVICE(KVASER_VENDOR_ID
, USB_LEAF_SPRO_LS_PRODUCT_ID
),
335 .driver_info
= KVASER_HAS_TXRX_ERRORS
|
336 KVASER_HAS_SILENT_MODE
},
337 { USB_DEVICE(KVASER_VENDOR_ID
, USB_LEAF_SPRO_SWC_PRODUCT_ID
),
338 .driver_info
= KVASER_HAS_TXRX_ERRORS
|
339 KVASER_HAS_SILENT_MODE
},
340 { USB_DEVICE(KVASER_VENDOR_ID
, USB_MEMO2_DEVEL_PRODUCT_ID
),
341 .driver_info
= KVASER_HAS_TXRX_ERRORS
|
342 KVASER_HAS_SILENT_MODE
},
343 { USB_DEVICE(KVASER_VENDOR_ID
, USB_MEMO2_HSHS_PRODUCT_ID
),
344 .driver_info
= KVASER_HAS_TXRX_ERRORS
|
345 KVASER_HAS_SILENT_MODE
},
346 { USB_DEVICE(KVASER_VENDOR_ID
, USB_UPRO_HSHS_PRODUCT_ID
),
347 .driver_info
= KVASER_HAS_TXRX_ERRORS
},
348 { USB_DEVICE(KVASER_VENDOR_ID
, USB_LEAF_LITE_GI_PRODUCT_ID
) },
349 { USB_DEVICE(KVASER_VENDOR_ID
, USB_LEAF_PRO_OBDII_PRODUCT_ID
),
350 .driver_info
= KVASER_HAS_TXRX_ERRORS
|
351 KVASER_HAS_SILENT_MODE
},
352 { USB_DEVICE(KVASER_VENDOR_ID
, USB_MEMO2_HSLS_PRODUCT_ID
),
353 .driver_info
= KVASER_HAS_TXRX_ERRORS
},
354 { USB_DEVICE(KVASER_VENDOR_ID
, USB_LEAF_LITE_CH_PRODUCT_ID
),
355 .driver_info
= KVASER_HAS_TXRX_ERRORS
},
356 { USB_DEVICE(KVASER_VENDOR_ID
, USB_BLACKBIRD_SPRO_PRODUCT_ID
),
357 .driver_info
= KVASER_HAS_TXRX_ERRORS
},
358 { USB_DEVICE(KVASER_VENDOR_ID
, USB_OEM_MERCURY_PRODUCT_ID
),
359 .driver_info
= KVASER_HAS_TXRX_ERRORS
},
360 { USB_DEVICE(KVASER_VENDOR_ID
, USB_OEM_LEAF_PRODUCT_ID
),
361 .driver_info
= KVASER_HAS_TXRX_ERRORS
},
362 { USB_DEVICE(KVASER_VENDOR_ID
, USB_CAN_R_PRODUCT_ID
),
363 .driver_info
= KVASER_HAS_TXRX_ERRORS
},
364 { USB_DEVICE(KVASER_VENDOR_ID
, USB_LEAF_LITE_V2_PRODUCT_ID
) },
365 { USB_DEVICE(KVASER_VENDOR_ID
, USB_MINI_PCIE_HS_PRODUCT_ID
) },
368 MODULE_DEVICE_TABLE(usb
, kvaser_usb_table
);
370 static inline int kvaser_usb_send_msg(const struct kvaser_usb
*dev
,
371 struct kvaser_msg
*msg
)
375 return usb_bulk_msg(dev
->udev
,
376 usb_sndbulkpipe(dev
->udev
,
377 dev
->bulk_out
->bEndpointAddress
),
378 msg
, msg
->len
, &actual_len
,
382 static int kvaser_usb_wait_msg(const struct kvaser_usb
*dev
, u8 id
,
383 struct kvaser_msg
*msg
)
385 struct kvaser_msg
*tmp
;
390 unsigned long to
= jiffies
+ msecs_to_jiffies(USB_RECV_TIMEOUT
);
392 buf
= kzalloc(RX_BUFFER_SIZE
, GFP_KERNEL
);
397 err
= usb_bulk_msg(dev
->udev
,
398 usb_rcvbulkpipe(dev
->udev
,
399 dev
->bulk_in
->bEndpointAddress
),
400 buf
, RX_BUFFER_SIZE
, &actual_len
,
406 while (pos
<= actual_len
- MSG_HEADER_LEN
) {
409 /* Handle messages crossing the USB endpoint max packet
410 * size boundary. Check kvaser_usb_read_bulk_callback()
411 * for further details.
415 dev
->bulk_in
->wMaxPacketSize
);
419 if (pos
+ tmp
->len
> actual_len
) {
420 dev_err(dev
->udev
->dev
.parent
,
426 memcpy(msg
, tmp
, tmp
->len
);
432 } while (time_before(jiffies
, to
));
442 static int kvaser_usb_send_simple_msg(const struct kvaser_usb
*dev
,
443 u8 msg_id
, int channel
)
445 struct kvaser_msg
*msg
;
448 msg
= kmalloc(sizeof(*msg
), GFP_KERNEL
);
453 msg
->len
= MSG_HEADER_LEN
+ sizeof(struct kvaser_msg_simple
);
454 msg
->u
.simple
.channel
= channel
;
455 msg
->u
.simple
.tid
= 0xff;
457 rc
= kvaser_usb_send_msg(dev
, msg
);
463 static int kvaser_usb_get_software_info(struct kvaser_usb
*dev
)
465 struct kvaser_msg msg
;
468 err
= kvaser_usb_send_simple_msg(dev
, CMD_GET_SOFTWARE_INFO
, 0);
472 err
= kvaser_usb_wait_msg(dev
, CMD_GET_SOFTWARE_INFO_REPLY
, &msg
);
476 dev
->fw_version
= le32_to_cpu(msg
.u
.softinfo
.fw_version
);
481 static int kvaser_usb_get_card_info(struct kvaser_usb
*dev
)
483 struct kvaser_msg msg
;
486 err
= kvaser_usb_send_simple_msg(dev
, CMD_GET_CARD_INFO
, 0);
490 err
= kvaser_usb_wait_msg(dev
, CMD_GET_CARD_INFO_REPLY
, &msg
);
494 dev
->nchannels
= msg
.u
.cardinfo
.nchannels
;
495 if (dev
->nchannels
> MAX_NET_DEVICES
)
501 static void kvaser_usb_tx_acknowledge(const struct kvaser_usb
*dev
,
502 const struct kvaser_msg
*msg
)
504 struct net_device_stats
*stats
;
505 struct kvaser_usb_tx_urb_context
*context
;
506 struct kvaser_usb_net_priv
*priv
;
508 struct can_frame
*cf
;
509 u8 channel
= msg
->u
.tx_acknowledge
.channel
;
510 u8 tid
= msg
->u
.tx_acknowledge
.tid
;
513 if (channel
>= dev
->nchannels
) {
514 dev_err(dev
->udev
->dev
.parent
,
515 "Invalid channel number (%d)\n", channel
);
519 priv
= dev
->nets
[channel
];
521 if (!netif_device_present(priv
->netdev
))
524 stats
= &priv
->netdev
->stats
;
526 context
= &priv
->tx_contexts
[tid
% MAX_TX_URBS
];
528 /* Sometimes the state change doesn't come after a bus-off event */
529 if (priv
->can
.restart_ms
&&
530 (priv
->can
.state
>= CAN_STATE_BUS_OFF
)) {
531 skb
= alloc_can_err_skb(priv
->netdev
, &cf
);
533 cf
->can_id
|= CAN_ERR_RESTARTED
;
537 stats
->rx_bytes
+= cf
->can_dlc
;
539 netdev_err(priv
->netdev
,
540 "No memory left for err_skb\n");
543 priv
->can
.can_stats
.restarts
++;
544 netif_carrier_on(priv
->netdev
);
546 priv
->can
.state
= CAN_STATE_ERROR_ACTIVE
;
550 stats
->tx_bytes
+= context
->dlc
;
552 spin_lock_irqsave(&priv
->tx_contexts_lock
, flags
);
554 can_get_echo_skb(priv
->netdev
, context
->echo_index
);
555 context
->echo_index
= MAX_TX_URBS
;
556 --priv
->active_tx_contexts
;
557 netif_wake_queue(priv
->netdev
);
559 spin_unlock_irqrestore(&priv
->tx_contexts_lock
, flags
);
562 static void kvaser_usb_simple_msg_callback(struct urb
*urb
)
564 struct net_device
*netdev
= urb
->context
;
566 kfree(urb
->transfer_buffer
);
569 netdev_warn(netdev
, "urb status received: %d\n",
573 static int kvaser_usb_simple_msg_async(struct kvaser_usb_net_priv
*priv
,
576 struct kvaser_usb
*dev
= priv
->dev
;
577 struct net_device
*netdev
= priv
->netdev
;
578 struct kvaser_msg
*msg
;
583 urb
= usb_alloc_urb(0, GFP_ATOMIC
);
585 netdev_err(netdev
, "No memory left for URBs\n");
589 buf
= kmalloc(sizeof(struct kvaser_msg
), GFP_ATOMIC
);
595 msg
= (struct kvaser_msg
*)buf
;
596 msg
->len
= MSG_HEADER_LEN
+ sizeof(struct kvaser_msg_simple
);
598 msg
->u
.simple
.channel
= priv
->channel
;
600 usb_fill_bulk_urb(urb
, dev
->udev
,
601 usb_sndbulkpipe(dev
->udev
,
602 dev
->bulk_out
->bEndpointAddress
),
604 kvaser_usb_simple_msg_callback
, netdev
);
605 usb_anchor_urb(urb
, &priv
->tx_submitted
);
607 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
609 netdev_err(netdev
, "Error transmitting URB\n");
610 usb_unanchor_urb(urb
);
620 static void kvaser_usb_rx_error(const struct kvaser_usb
*dev
,
621 const struct kvaser_msg
*msg
)
623 struct can_frame
*cf
;
625 struct net_device_stats
*stats
;
626 struct kvaser_usb_net_priv
*priv
;
627 unsigned int new_state
;
628 u8 channel
, status
, txerr
, rxerr
, error_factor
;
631 case CMD_CAN_ERROR_EVENT
:
632 channel
= msg
->u
.error_event
.channel
;
633 status
= msg
->u
.error_event
.status
;
634 txerr
= msg
->u
.error_event
.tx_errors_count
;
635 rxerr
= msg
->u
.error_event
.rx_errors_count
;
636 error_factor
= msg
->u
.error_event
.error_factor
;
638 case CMD_LOG_MESSAGE
:
639 channel
= msg
->u
.log_message
.channel
;
640 status
= msg
->u
.log_message
.data
[0];
641 txerr
= msg
->u
.log_message
.data
[2];
642 rxerr
= msg
->u
.log_message
.data
[3];
643 error_factor
= msg
->u
.log_message
.data
[1];
645 case CMD_CHIP_STATE_EVENT
:
646 channel
= msg
->u
.chip_state_event
.channel
;
647 status
= msg
->u
.chip_state_event
.status
;
648 txerr
= msg
->u
.chip_state_event
.tx_errors_count
;
649 rxerr
= msg
->u
.chip_state_event
.rx_errors_count
;
653 dev_err(dev
->udev
->dev
.parent
, "Invalid msg id (%d)\n",
658 if (channel
>= dev
->nchannels
) {
659 dev_err(dev
->udev
->dev
.parent
,
660 "Invalid channel number (%d)\n", channel
);
664 priv
= dev
->nets
[channel
];
665 stats
= &priv
->netdev
->stats
;
667 skb
= alloc_can_err_skb(priv
->netdev
, &cf
);
673 new_state
= priv
->can
.state
;
675 netdev_dbg(priv
->netdev
, "Error status: 0x%02x\n", status
);
677 if (status
& (M16C_STATE_BUS_OFF
| M16C_STATE_BUS_RESET
)) {
678 cf
->can_id
|= CAN_ERR_BUSOFF
;
680 priv
->can
.can_stats
.bus_off
++;
681 if (!priv
->can
.restart_ms
)
682 kvaser_usb_simple_msg_async(priv
, CMD_STOP_CHIP
);
684 netif_carrier_off(priv
->netdev
);
686 new_state
= CAN_STATE_BUS_OFF
;
687 } else if (status
& M16C_STATE_BUS_PASSIVE
) {
688 if (priv
->can
.state
!= CAN_STATE_ERROR_PASSIVE
) {
689 cf
->can_id
|= CAN_ERR_CRTL
;
692 cf
->data
[1] = (txerr
> rxerr
)
693 ? CAN_ERR_CRTL_TX_PASSIVE
694 : CAN_ERR_CRTL_RX_PASSIVE
;
696 cf
->data
[1] = CAN_ERR_CRTL_TX_PASSIVE
|
697 CAN_ERR_CRTL_RX_PASSIVE
;
699 priv
->can
.can_stats
.error_passive
++;
702 new_state
= CAN_STATE_ERROR_PASSIVE
;
703 } else if (status
& M16C_STATE_BUS_ERROR
) {
704 if ((priv
->can
.state
< CAN_STATE_ERROR_WARNING
) &&
705 ((txerr
>= 96) || (rxerr
>= 96))) {
706 cf
->can_id
|= CAN_ERR_CRTL
;
707 cf
->data
[1] = (txerr
> rxerr
)
708 ? CAN_ERR_CRTL_TX_WARNING
709 : CAN_ERR_CRTL_RX_WARNING
;
711 priv
->can
.can_stats
.error_warning
++;
712 new_state
= CAN_STATE_ERROR_WARNING
;
713 } else if ((priv
->can
.state
> CAN_STATE_ERROR_ACTIVE
) &&
714 ((txerr
< 96) && (rxerr
< 96))) {
715 cf
->can_id
|= CAN_ERR_PROT
;
716 cf
->data
[2] = CAN_ERR_PROT_ACTIVE
;
718 new_state
= CAN_STATE_ERROR_ACTIVE
;
723 cf
->can_id
|= CAN_ERR_PROT
;
724 cf
->data
[2] = CAN_ERR_PROT_ACTIVE
;
726 new_state
= CAN_STATE_ERROR_ACTIVE
;
729 if (priv
->can
.restart_ms
&&
730 (priv
->can
.state
>= CAN_STATE_BUS_OFF
) &&
731 (new_state
< CAN_STATE_BUS_OFF
)) {
732 cf
->can_id
|= CAN_ERR_RESTARTED
;
733 netif_carrier_on(priv
->netdev
);
735 priv
->can
.can_stats
.restarts
++;
739 priv
->can
.can_stats
.bus_error
++;
742 cf
->can_id
|= CAN_ERR_BUSERROR
| CAN_ERR_PROT
;
744 if (error_factor
& M16C_EF_ACKE
)
745 cf
->data
[3] |= (CAN_ERR_PROT_LOC_ACK
);
746 if (error_factor
& M16C_EF_CRCE
)
747 cf
->data
[3] |= (CAN_ERR_PROT_LOC_CRC_SEQ
|
748 CAN_ERR_PROT_LOC_CRC_DEL
);
749 if (error_factor
& M16C_EF_FORME
)
750 cf
->data
[2] |= CAN_ERR_PROT_FORM
;
751 if (error_factor
& M16C_EF_STFE
)
752 cf
->data
[2] |= CAN_ERR_PROT_STUFF
;
753 if (error_factor
& M16C_EF_BITE0
)
754 cf
->data
[2] |= CAN_ERR_PROT_BIT0
;
755 if (error_factor
& M16C_EF_BITE1
)
756 cf
->data
[2] |= CAN_ERR_PROT_BIT1
;
757 if (error_factor
& M16C_EF_TRE
)
758 cf
->data
[2] |= CAN_ERR_PROT_TX
;
764 priv
->bec
.txerr
= txerr
;
765 priv
->bec
.rxerr
= rxerr
;
767 priv
->can
.state
= new_state
;
772 stats
->rx_bytes
+= cf
->can_dlc
;
775 static void kvaser_usb_rx_can_err(const struct kvaser_usb_net_priv
*priv
,
776 const struct kvaser_msg
*msg
)
778 struct can_frame
*cf
;
780 struct net_device_stats
*stats
= &priv
->netdev
->stats
;
782 if (msg
->u
.rx_can
.flag
& (MSG_FLAG_ERROR_FRAME
|
784 netdev_err(priv
->netdev
, "Unknow error (flags: 0x%02x)\n",
791 if (msg
->u
.rx_can
.flag
& MSG_FLAG_OVERRUN
) {
792 skb
= alloc_can_err_skb(priv
->netdev
, &cf
);
798 cf
->can_id
|= CAN_ERR_CRTL
;
799 cf
->data
[1] = CAN_ERR_CRTL_RX_OVERFLOW
;
801 stats
->rx_over_errors
++;
807 stats
->rx_bytes
+= cf
->can_dlc
;
811 static void kvaser_usb_rx_can_msg(const struct kvaser_usb
*dev
,
812 const struct kvaser_msg
*msg
)
814 struct kvaser_usb_net_priv
*priv
;
815 struct can_frame
*cf
;
817 struct net_device_stats
*stats
;
818 u8 channel
= msg
->u
.rx_can
.channel
;
820 if (channel
>= dev
->nchannels
) {
821 dev_err(dev
->udev
->dev
.parent
,
822 "Invalid channel number (%d)\n", channel
);
826 priv
= dev
->nets
[channel
];
827 stats
= &priv
->netdev
->stats
;
829 if ((msg
->u
.rx_can
.flag
& MSG_FLAG_ERROR_FRAME
) &&
830 (msg
->id
== CMD_LOG_MESSAGE
)) {
831 kvaser_usb_rx_error(dev
, msg
);
833 } else if (msg
->u
.rx_can
.flag
& (MSG_FLAG_ERROR_FRAME
|
836 kvaser_usb_rx_can_err(priv
, msg
);
838 } else if (msg
->u
.rx_can
.flag
& ~MSG_FLAG_REMOTE_FRAME
) {
839 netdev_warn(priv
->netdev
,
840 "Unhandled frame (flags: 0x%02x)",
845 skb
= alloc_can_skb(priv
->netdev
, &cf
);
851 if (msg
->id
== CMD_LOG_MESSAGE
) {
852 cf
->can_id
= le32_to_cpu(msg
->u
.log_message
.id
);
853 if (cf
->can_id
& KVASER_EXTENDED_FRAME
)
854 cf
->can_id
&= CAN_EFF_MASK
| CAN_EFF_FLAG
;
856 cf
->can_id
&= CAN_SFF_MASK
;
858 cf
->can_dlc
= get_can_dlc(msg
->u
.log_message
.dlc
);
860 if (msg
->u
.log_message
.flags
& MSG_FLAG_REMOTE_FRAME
)
861 cf
->can_id
|= CAN_RTR_FLAG
;
863 memcpy(cf
->data
, &msg
->u
.log_message
.data
,
866 cf
->can_id
= ((msg
->u
.rx_can
.msg
[0] & 0x1f) << 6) |
867 (msg
->u
.rx_can
.msg
[1] & 0x3f);
869 if (msg
->id
== CMD_RX_EXT_MESSAGE
) {
871 cf
->can_id
|= ((msg
->u
.rx_can
.msg
[2] & 0x0f) << 14) |
872 ((msg
->u
.rx_can
.msg
[3] & 0xff) << 6) |
873 (msg
->u
.rx_can
.msg
[4] & 0x3f);
874 cf
->can_id
|= CAN_EFF_FLAG
;
877 cf
->can_dlc
= get_can_dlc(msg
->u
.rx_can
.msg
[5]);
879 if (msg
->u
.rx_can
.flag
& MSG_FLAG_REMOTE_FRAME
)
880 cf
->can_id
|= CAN_RTR_FLAG
;
882 memcpy(cf
->data
, &msg
->u
.rx_can
.msg
[6],
889 stats
->rx_bytes
+= cf
->can_dlc
;
892 static void kvaser_usb_start_chip_reply(const struct kvaser_usb
*dev
,
893 const struct kvaser_msg
*msg
)
895 struct kvaser_usb_net_priv
*priv
;
896 u8 channel
= msg
->u
.simple
.channel
;
898 if (channel
>= dev
->nchannels
) {
899 dev_err(dev
->udev
->dev
.parent
,
900 "Invalid channel number (%d)\n", channel
);
904 priv
= dev
->nets
[channel
];
906 if (completion_done(&priv
->start_comp
) &&
907 netif_queue_stopped(priv
->netdev
)) {
908 netif_wake_queue(priv
->netdev
);
910 netif_start_queue(priv
->netdev
);
911 complete(&priv
->start_comp
);
915 static void kvaser_usb_stop_chip_reply(const struct kvaser_usb
*dev
,
916 const struct kvaser_msg
*msg
)
918 struct kvaser_usb_net_priv
*priv
;
919 u8 channel
= msg
->u
.simple
.channel
;
921 if (channel
>= dev
->nchannels
) {
922 dev_err(dev
->udev
->dev
.parent
,
923 "Invalid channel number (%d)\n", channel
);
927 priv
= dev
->nets
[channel
];
929 complete(&priv
->stop_comp
);
932 static void kvaser_usb_handle_message(const struct kvaser_usb
*dev
,
933 const struct kvaser_msg
*msg
)
936 case CMD_START_CHIP_REPLY
:
937 kvaser_usb_start_chip_reply(dev
, msg
);
940 case CMD_STOP_CHIP_REPLY
:
941 kvaser_usb_stop_chip_reply(dev
, msg
);
944 case CMD_RX_STD_MESSAGE
:
945 case CMD_RX_EXT_MESSAGE
:
946 case CMD_LOG_MESSAGE
:
947 kvaser_usb_rx_can_msg(dev
, msg
);
950 case CMD_CHIP_STATE_EVENT
:
951 case CMD_CAN_ERROR_EVENT
:
952 kvaser_usb_rx_error(dev
, msg
);
955 case CMD_TX_ACKNOWLEDGE
:
956 kvaser_usb_tx_acknowledge(dev
, msg
);
960 dev_warn(dev
->udev
->dev
.parent
,
961 "Unhandled message (%d)\n", msg
->id
);
966 static void kvaser_usb_read_bulk_callback(struct urb
*urb
)
968 struct kvaser_usb
*dev
= urb
->context
;
969 struct kvaser_msg
*msg
;
973 switch (urb
->status
) {
980 dev_info(dev
->udev
->dev
.parent
, "Rx URB aborted (%d)\n",
985 while (pos
<= urb
->actual_length
- MSG_HEADER_LEN
) {
986 msg
= urb
->transfer_buffer
+ pos
;
988 /* The Kvaser firmware can only read and write messages that
989 * does not cross the USB's endpoint wMaxPacketSize boundary.
990 * If a follow-up command crosses such boundary, firmware puts
991 * a placeholder zero-length command in its place then aligns
992 * the real command to the next max packet size.
994 * Handle such cases or we're going to miss a significant
995 * number of events in case of a heavy rx load on the bus.
998 pos
= round_up(pos
, dev
->bulk_in
->wMaxPacketSize
);
1002 if (pos
+ msg
->len
> urb
->actual_length
) {
1003 dev_err(dev
->udev
->dev
.parent
, "Format error\n");
1007 kvaser_usb_handle_message(dev
, msg
);
1012 usb_fill_bulk_urb(urb
, dev
->udev
,
1013 usb_rcvbulkpipe(dev
->udev
,
1014 dev
->bulk_in
->bEndpointAddress
),
1015 urb
->transfer_buffer
, RX_BUFFER_SIZE
,
1016 kvaser_usb_read_bulk_callback
, dev
);
1018 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
1019 if (err
== -ENODEV
) {
1020 for (i
= 0; i
< dev
->nchannels
; i
++) {
1024 netif_device_detach(dev
->nets
[i
]->netdev
);
1027 dev_err(dev
->udev
->dev
.parent
,
1028 "Failed resubmitting read bulk urb: %d\n", err
);
1034 static int kvaser_usb_setup_rx_urbs(struct kvaser_usb
*dev
)
1038 if (dev
->rxinitdone
)
1041 for (i
= 0; i
< MAX_RX_URBS
; i
++) {
1042 struct urb
*urb
= NULL
;
1046 urb
= usb_alloc_urb(0, GFP_KERNEL
);
1048 dev_warn(dev
->udev
->dev
.parent
,
1049 "No memory left for URBs\n");
1054 buf
= usb_alloc_coherent(dev
->udev
, RX_BUFFER_SIZE
,
1055 GFP_KERNEL
, &buf_dma
);
1057 dev_warn(dev
->udev
->dev
.parent
,
1058 "No memory left for USB buffer\n");
1064 usb_fill_bulk_urb(urb
, dev
->udev
,
1065 usb_rcvbulkpipe(dev
->udev
,
1066 dev
->bulk_in
->bEndpointAddress
),
1067 buf
, RX_BUFFER_SIZE
,
1068 kvaser_usb_read_bulk_callback
,
1070 urb
->transfer_dma
= buf_dma
;
1071 urb
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
1072 usb_anchor_urb(urb
, &dev
->rx_submitted
);
1074 err
= usb_submit_urb(urb
, GFP_KERNEL
);
1076 usb_unanchor_urb(urb
);
1077 usb_free_coherent(dev
->udev
, RX_BUFFER_SIZE
, buf
,
1083 dev
->rxbuf
[i
] = buf
;
1084 dev
->rxbuf_dma
[i
] = buf_dma
;
1090 dev_warn(dev
->udev
->dev
.parent
,
1091 "Cannot setup read URBs, error %d\n", err
);
1093 } else if (i
< MAX_RX_URBS
) {
1094 dev_warn(dev
->udev
->dev
.parent
,
1095 "RX performances may be slow\n");
1098 dev
->rxinitdone
= true;
1103 static int kvaser_usb_set_opt_mode(const struct kvaser_usb_net_priv
*priv
)
1105 struct kvaser_msg
*msg
;
1108 msg
= kmalloc(sizeof(*msg
), GFP_KERNEL
);
1112 msg
->id
= CMD_SET_CTRL_MODE
;
1113 msg
->len
= MSG_HEADER_LEN
+ sizeof(struct kvaser_msg_ctrl_mode
);
1114 msg
->u
.ctrl_mode
.tid
= 0xff;
1115 msg
->u
.ctrl_mode
.channel
= priv
->channel
;
1117 if (priv
->can
.ctrlmode
& CAN_CTRLMODE_LISTENONLY
)
1118 msg
->u
.ctrl_mode
.ctrl_mode
= KVASER_CTRL_MODE_SILENT
;
1120 msg
->u
.ctrl_mode
.ctrl_mode
= KVASER_CTRL_MODE_NORMAL
;
1122 rc
= kvaser_usb_send_msg(priv
->dev
, msg
);
1128 static int kvaser_usb_start_chip(struct kvaser_usb_net_priv
*priv
)
1132 init_completion(&priv
->start_comp
);
1134 err
= kvaser_usb_send_simple_msg(priv
->dev
, CMD_START_CHIP
,
1139 if (!wait_for_completion_timeout(&priv
->start_comp
,
1140 msecs_to_jiffies(START_TIMEOUT
)))
1146 static int kvaser_usb_open(struct net_device
*netdev
)
1148 struct kvaser_usb_net_priv
*priv
= netdev_priv(netdev
);
1149 struct kvaser_usb
*dev
= priv
->dev
;
1152 err
= open_candev(netdev
);
1156 err
= kvaser_usb_setup_rx_urbs(dev
);
1160 err
= kvaser_usb_set_opt_mode(priv
);
1164 err
= kvaser_usb_start_chip(priv
);
1166 netdev_warn(netdev
, "Cannot start device, error %d\n", err
);
1170 priv
->can
.state
= CAN_STATE_ERROR_ACTIVE
;
1175 close_candev(netdev
);
1179 static void kvaser_usb_reset_tx_urb_contexts(struct kvaser_usb_net_priv
*priv
)
1183 priv
->active_tx_contexts
= 0;
1184 for (i
= 0; i
< MAX_TX_URBS
; i
++)
1185 priv
->tx_contexts
[i
].echo_index
= MAX_TX_URBS
;
1188 /* This method might sleep. Do not call it in the atomic context
1189 * of URB completions.
1191 static void kvaser_usb_unlink_tx_urbs(struct kvaser_usb_net_priv
*priv
)
1193 usb_kill_anchored_urbs(&priv
->tx_submitted
);
1194 kvaser_usb_reset_tx_urb_contexts(priv
);
1197 static void kvaser_usb_unlink_all_urbs(struct kvaser_usb
*dev
)
1201 usb_kill_anchored_urbs(&dev
->rx_submitted
);
1203 for (i
= 0; i
< MAX_RX_URBS
; i
++)
1204 usb_free_coherent(dev
->udev
, RX_BUFFER_SIZE
,
1208 for (i
= 0; i
< MAX_NET_DEVICES
; i
++) {
1209 struct kvaser_usb_net_priv
*priv
= dev
->nets
[i
];
1212 kvaser_usb_unlink_tx_urbs(priv
);
1216 static int kvaser_usb_stop_chip(struct kvaser_usb_net_priv
*priv
)
1220 init_completion(&priv
->stop_comp
);
1222 err
= kvaser_usb_send_simple_msg(priv
->dev
, CMD_STOP_CHIP
,
1227 if (!wait_for_completion_timeout(&priv
->stop_comp
,
1228 msecs_to_jiffies(STOP_TIMEOUT
)))
1234 static int kvaser_usb_flush_queue(struct kvaser_usb_net_priv
*priv
)
1236 struct kvaser_msg
*msg
;
1239 msg
= kmalloc(sizeof(*msg
), GFP_KERNEL
);
1243 msg
->id
= CMD_FLUSH_QUEUE
;
1244 msg
->len
= MSG_HEADER_LEN
+ sizeof(struct kvaser_msg_flush_queue
);
1245 msg
->u
.flush_queue
.channel
= priv
->channel
;
1246 msg
->u
.flush_queue
.flags
= 0x00;
1248 rc
= kvaser_usb_send_msg(priv
->dev
, msg
);
1254 static int kvaser_usb_close(struct net_device
*netdev
)
1256 struct kvaser_usb_net_priv
*priv
= netdev_priv(netdev
);
1257 struct kvaser_usb
*dev
= priv
->dev
;
1260 netif_stop_queue(netdev
);
1262 err
= kvaser_usb_flush_queue(priv
);
1264 netdev_warn(netdev
, "Cannot flush queue, error %d\n", err
);
1266 if (kvaser_usb_send_simple_msg(dev
, CMD_RESET_CHIP
, priv
->channel
))
1267 netdev_warn(netdev
, "Cannot reset card, error %d\n", err
);
1269 err
= kvaser_usb_stop_chip(priv
);
1271 netdev_warn(netdev
, "Cannot stop device, error %d\n", err
);
1273 /* reset tx contexts */
1274 kvaser_usb_unlink_tx_urbs(priv
);
1276 priv
->can
.state
= CAN_STATE_STOPPED
;
1277 close_candev(priv
->netdev
);
1282 static void kvaser_usb_write_bulk_callback(struct urb
*urb
)
1284 struct kvaser_usb_tx_urb_context
*context
= urb
->context
;
1285 struct kvaser_usb_net_priv
*priv
;
1286 struct net_device
*netdev
;
1288 if (WARN_ON(!context
))
1291 priv
= context
->priv
;
1292 netdev
= priv
->netdev
;
1294 kfree(urb
->transfer_buffer
);
1296 if (!netif_device_present(netdev
))
1300 netdev_info(netdev
, "Tx URB aborted (%d)\n", urb
->status
);
1303 static netdev_tx_t
kvaser_usb_start_xmit(struct sk_buff
*skb
,
1304 struct net_device
*netdev
)
1306 struct kvaser_usb_net_priv
*priv
= netdev_priv(netdev
);
1307 struct kvaser_usb
*dev
= priv
->dev
;
1308 struct net_device_stats
*stats
= &netdev
->stats
;
1309 struct can_frame
*cf
= (struct can_frame
*)skb
->data
;
1310 struct kvaser_usb_tx_urb_context
*context
= NULL
;
1313 struct kvaser_msg
*msg
;
1314 int i
, err
, ret
= NETDEV_TX_OK
;
1315 unsigned long flags
;
1317 if (can_dropped_invalid_skb(netdev
, skb
))
1318 return NETDEV_TX_OK
;
1320 urb
= usb_alloc_urb(0, GFP_ATOMIC
);
1322 netdev_err(netdev
, "No memory left for URBs\n");
1323 stats
->tx_dropped
++;
1325 return NETDEV_TX_OK
;
1328 buf
= kmalloc(sizeof(struct kvaser_msg
), GFP_ATOMIC
);
1330 stats
->tx_dropped
++;
1336 msg
->len
= MSG_HEADER_LEN
+ sizeof(struct kvaser_msg_tx_can
);
1337 msg
->u
.tx_can
.flags
= 0;
1338 msg
->u
.tx_can
.channel
= priv
->channel
;
1340 if (cf
->can_id
& CAN_EFF_FLAG
) {
1341 msg
->id
= CMD_TX_EXT_MESSAGE
;
1342 msg
->u
.tx_can
.msg
[0] = (cf
->can_id
>> 24) & 0x1f;
1343 msg
->u
.tx_can
.msg
[1] = (cf
->can_id
>> 18) & 0x3f;
1344 msg
->u
.tx_can
.msg
[2] = (cf
->can_id
>> 14) & 0x0f;
1345 msg
->u
.tx_can
.msg
[3] = (cf
->can_id
>> 6) & 0xff;
1346 msg
->u
.tx_can
.msg
[4] = cf
->can_id
& 0x3f;
1348 msg
->id
= CMD_TX_STD_MESSAGE
;
1349 msg
->u
.tx_can
.msg
[0] = (cf
->can_id
>> 6) & 0x1f;
1350 msg
->u
.tx_can
.msg
[1] = cf
->can_id
& 0x3f;
1353 msg
->u
.tx_can
.msg
[5] = cf
->can_dlc
;
1354 memcpy(&msg
->u
.tx_can
.msg
[6], cf
->data
, cf
->can_dlc
);
1356 if (cf
->can_id
& CAN_RTR_FLAG
)
1357 msg
->u
.tx_can
.flags
|= MSG_FLAG_REMOTE_FRAME
;
1359 spin_lock_irqsave(&priv
->tx_contexts_lock
, flags
);
1360 for (i
= 0; i
< ARRAY_SIZE(priv
->tx_contexts
); i
++) {
1361 if (priv
->tx_contexts
[i
].echo_index
== MAX_TX_URBS
) {
1362 context
= &priv
->tx_contexts
[i
];
1364 context
->echo_index
= i
;
1365 can_put_echo_skb(skb
, netdev
, context
->echo_index
);
1366 ++priv
->active_tx_contexts
;
1367 if (priv
->active_tx_contexts
>= MAX_TX_URBS
)
1368 netif_stop_queue(netdev
);
1373 spin_unlock_irqrestore(&priv
->tx_contexts_lock
, flags
);
1375 /* This should never happen; it implies a flow control bug */
1377 netdev_warn(netdev
, "cannot find free context\n");
1380 ret
= NETDEV_TX_BUSY
;
1384 context
->priv
= priv
;
1385 context
->dlc
= cf
->can_dlc
;
1387 msg
->u
.tx_can
.tid
= context
->echo_index
;
1389 usb_fill_bulk_urb(urb
, dev
->udev
,
1390 usb_sndbulkpipe(dev
->udev
,
1391 dev
->bulk_out
->bEndpointAddress
),
1393 kvaser_usb_write_bulk_callback
, context
);
1394 usb_anchor_urb(urb
, &priv
->tx_submitted
);
1396 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
1397 if (unlikely(err
)) {
1398 spin_lock_irqsave(&priv
->tx_contexts_lock
, flags
);
1400 can_free_echo_skb(netdev
, context
->echo_index
);
1401 context
->echo_index
= MAX_TX_URBS
;
1402 --priv
->active_tx_contexts
;
1403 netif_wake_queue(netdev
);
1405 spin_unlock_irqrestore(&priv
->tx_contexts_lock
, flags
);
1407 usb_unanchor_urb(urb
);
1409 stats
->tx_dropped
++;
1412 netif_device_detach(netdev
);
1414 netdev_warn(netdev
, "Failed tx_urb %d\n", err
);
1426 static const struct net_device_ops kvaser_usb_netdev_ops
= {
1427 .ndo_open
= kvaser_usb_open
,
1428 .ndo_stop
= kvaser_usb_close
,
1429 .ndo_start_xmit
= kvaser_usb_start_xmit
,
1430 .ndo_change_mtu
= can_change_mtu
,
1433 static const struct can_bittiming_const kvaser_usb_bittiming_const
= {
1434 .name
= "kvaser_usb",
1435 .tseg1_min
= KVASER_USB_TSEG1_MIN
,
1436 .tseg1_max
= KVASER_USB_TSEG1_MAX
,
1437 .tseg2_min
= KVASER_USB_TSEG2_MIN
,
1438 .tseg2_max
= KVASER_USB_TSEG2_MAX
,
1439 .sjw_max
= KVASER_USB_SJW_MAX
,
1440 .brp_min
= KVASER_USB_BRP_MIN
,
1441 .brp_max
= KVASER_USB_BRP_MAX
,
1442 .brp_inc
= KVASER_USB_BRP_INC
,
1445 static int kvaser_usb_set_bittiming(struct net_device
*netdev
)
1447 struct kvaser_usb_net_priv
*priv
= netdev_priv(netdev
);
1448 struct can_bittiming
*bt
= &priv
->can
.bittiming
;
1449 struct kvaser_usb
*dev
= priv
->dev
;
1450 struct kvaser_msg
*msg
;
1453 msg
= kmalloc(sizeof(*msg
), GFP_KERNEL
);
1457 msg
->id
= CMD_SET_BUS_PARAMS
;
1458 msg
->len
= MSG_HEADER_LEN
+ sizeof(struct kvaser_msg_busparams
);
1459 msg
->u
.busparams
.channel
= priv
->channel
;
1460 msg
->u
.busparams
.tid
= 0xff;
1461 msg
->u
.busparams
.bitrate
= cpu_to_le32(bt
->bitrate
);
1462 msg
->u
.busparams
.sjw
= bt
->sjw
;
1463 msg
->u
.busparams
.tseg1
= bt
->prop_seg
+ bt
->phase_seg1
;
1464 msg
->u
.busparams
.tseg2
= bt
->phase_seg2
;
1466 if (priv
->can
.ctrlmode
& CAN_CTRLMODE_3_SAMPLES
)
1467 msg
->u
.busparams
.no_samp
= 3;
1469 msg
->u
.busparams
.no_samp
= 1;
1471 rc
= kvaser_usb_send_msg(dev
, msg
);
1477 static int kvaser_usb_set_mode(struct net_device
*netdev
,
1480 struct kvaser_usb_net_priv
*priv
= netdev_priv(netdev
);
1484 case CAN_MODE_START
:
1485 err
= kvaser_usb_simple_msg_async(priv
, CMD_START_CHIP
);
1496 static int kvaser_usb_get_berr_counter(const struct net_device
*netdev
,
1497 struct can_berr_counter
*bec
)
1499 struct kvaser_usb_net_priv
*priv
= netdev_priv(netdev
);
1506 static void kvaser_usb_remove_interfaces(struct kvaser_usb
*dev
)
1510 for (i
= 0; i
< dev
->nchannels
; i
++) {
1514 unregister_netdev(dev
->nets
[i
]->netdev
);
1517 kvaser_usb_unlink_all_urbs(dev
);
1519 for (i
= 0; i
< dev
->nchannels
; i
++) {
1523 free_candev(dev
->nets
[i
]->netdev
);
1527 static int kvaser_usb_init_one(struct usb_interface
*intf
,
1528 const struct usb_device_id
*id
, int channel
)
1530 struct kvaser_usb
*dev
= usb_get_intfdata(intf
);
1531 struct net_device
*netdev
;
1532 struct kvaser_usb_net_priv
*priv
;
1535 err
= kvaser_usb_send_simple_msg(dev
, CMD_RESET_CHIP
, channel
);
1539 netdev
= alloc_candev(sizeof(*priv
), MAX_TX_URBS
);
1541 dev_err(&intf
->dev
, "Cannot alloc candev\n");
1545 priv
= netdev_priv(netdev
);
1547 init_usb_anchor(&priv
->tx_submitted
);
1548 init_completion(&priv
->start_comp
);
1549 init_completion(&priv
->stop_comp
);
1552 priv
->netdev
= netdev
;
1553 priv
->channel
= channel
;
1555 spin_lock_init(&priv
->tx_contexts_lock
);
1556 kvaser_usb_reset_tx_urb_contexts(priv
);
1558 priv
->can
.state
= CAN_STATE_STOPPED
;
1559 priv
->can
.clock
.freq
= CAN_USB_CLOCK
;
1560 priv
->can
.bittiming_const
= &kvaser_usb_bittiming_const
;
1561 priv
->can
.do_set_bittiming
= kvaser_usb_set_bittiming
;
1562 priv
->can
.do_set_mode
= kvaser_usb_set_mode
;
1563 if (id
->driver_info
& KVASER_HAS_TXRX_ERRORS
)
1564 priv
->can
.do_get_berr_counter
= kvaser_usb_get_berr_counter
;
1565 priv
->can
.ctrlmode_supported
= CAN_CTRLMODE_3_SAMPLES
;
1566 if (id
->driver_info
& KVASER_HAS_SILENT_MODE
)
1567 priv
->can
.ctrlmode_supported
|= CAN_CTRLMODE_LISTENONLY
;
1569 netdev
->flags
|= IFF_ECHO
;
1571 netdev
->netdev_ops
= &kvaser_usb_netdev_ops
;
1573 SET_NETDEV_DEV(netdev
, &intf
->dev
);
1574 netdev
->dev_id
= channel
;
1576 dev
->nets
[channel
] = priv
;
1578 err
= register_candev(netdev
);
1580 dev_err(&intf
->dev
, "Failed to register can device\n");
1581 free_candev(netdev
);
1582 dev
->nets
[channel
] = NULL
;
1586 netdev_dbg(netdev
, "device registered\n");
1591 static int kvaser_usb_get_endpoints(const struct usb_interface
*intf
,
1592 struct usb_endpoint_descriptor
**in
,
1593 struct usb_endpoint_descriptor
**out
)
1595 const struct usb_host_interface
*iface_desc
;
1596 struct usb_endpoint_descriptor
*endpoint
;
1599 iface_desc
= &intf
->altsetting
[0];
1601 for (i
= 0; i
< iface_desc
->desc
.bNumEndpoints
; ++i
) {
1602 endpoint
= &iface_desc
->endpoint
[i
].desc
;
1604 if (!*in
&& usb_endpoint_is_bulk_in(endpoint
))
1607 if (!*out
&& usb_endpoint_is_bulk_out(endpoint
))
1610 /* use first bulk endpoint for in and out */
1618 static int kvaser_usb_probe(struct usb_interface
*intf
,
1619 const struct usb_device_id
*id
)
1621 struct kvaser_usb
*dev
;
1625 dev
= devm_kzalloc(&intf
->dev
, sizeof(*dev
), GFP_KERNEL
);
1629 err
= kvaser_usb_get_endpoints(intf
, &dev
->bulk_in
, &dev
->bulk_out
);
1631 dev_err(&intf
->dev
, "Cannot get usb endpoint(s)");
1635 dev
->udev
= interface_to_usbdev(intf
);
1637 init_usb_anchor(&dev
->rx_submitted
);
1639 usb_set_intfdata(intf
, dev
);
1641 /* On some x86 laptops, plugging a Kvaser device again after
1642 * an unplug makes the firmware always ignore the very first
1643 * command. For such a case, provide some room for retries
1644 * instead of completely exiting the driver.
1647 err
= kvaser_usb_get_software_info(dev
);
1648 } while (--retry
&& err
== -ETIMEDOUT
);
1652 "Cannot get software infos, error %d\n", err
);
1656 err
= kvaser_usb_get_card_info(dev
);
1659 "Cannot get card infos, error %d\n", err
);
1663 dev_dbg(&intf
->dev
, "Firmware version: %d.%d.%d\n",
1664 ((dev
->fw_version
>> 24) & 0xff),
1665 ((dev
->fw_version
>> 16) & 0xff),
1666 (dev
->fw_version
& 0xffff));
1668 for (i
= 0; i
< dev
->nchannels
; i
++) {
1669 err
= kvaser_usb_init_one(intf
, id
, i
);
1671 kvaser_usb_remove_interfaces(dev
);
1679 static void kvaser_usb_disconnect(struct usb_interface
*intf
)
1681 struct kvaser_usb
*dev
= usb_get_intfdata(intf
);
1683 usb_set_intfdata(intf
, NULL
);
1688 kvaser_usb_remove_interfaces(dev
);
1691 static struct usb_driver kvaser_usb_driver
= {
1692 .name
= "kvaser_usb",
1693 .probe
= kvaser_usb_probe
,
1694 .disconnect
= kvaser_usb_disconnect
,
1695 .id_table
= kvaser_usb_table
,
1698 module_usb_driver(kvaser_usb_driver
);
1700 MODULE_AUTHOR("Olivier Sobrie <olivier@sobrie.be>");
1701 MODULE_DESCRIPTION("CAN driver for Kvaser CAN/USB devices");
1702 MODULE_LICENSE("GPL v2");