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/completion.h>
16 #include <linux/module.h>
17 #include <linux/netdevice.h>
18 #include <linux/usb.h>
20 #include <linux/can.h>
21 #include <linux/can/dev.h>
22 #include <linux/can/error.h>
24 #define MAX_TX_URBS 16
26 #define START_TIMEOUT 1000 /* msecs */
27 #define STOP_TIMEOUT 1000 /* msecs */
28 #define USB_SEND_TIMEOUT 1000 /* msecs */
29 #define USB_RECV_TIMEOUT 1000 /* msecs */
30 #define RX_BUFFER_SIZE 3072
31 #define CAN_USB_CLOCK 8000000
32 #define MAX_NET_DEVICES 3
34 /* Kvaser USB devices */
35 #define KVASER_VENDOR_ID 0x0bfd
36 #define USB_LEAF_DEVEL_PRODUCT_ID 10
37 #define USB_LEAF_LITE_PRODUCT_ID 11
38 #define USB_LEAF_PRO_PRODUCT_ID 12
39 #define USB_LEAF_SPRO_PRODUCT_ID 14
40 #define USB_LEAF_PRO_LS_PRODUCT_ID 15
41 #define USB_LEAF_PRO_SWC_PRODUCT_ID 16
42 #define USB_LEAF_PRO_LIN_PRODUCT_ID 17
43 #define USB_LEAF_SPRO_LS_PRODUCT_ID 18
44 #define USB_LEAF_SPRO_SWC_PRODUCT_ID 19
45 #define USB_MEMO2_DEVEL_PRODUCT_ID 22
46 #define USB_MEMO2_HSHS_PRODUCT_ID 23
47 #define USB_UPRO_HSHS_PRODUCT_ID 24
48 #define USB_LEAF_LITE_GI_PRODUCT_ID 25
49 #define USB_LEAF_PRO_OBDII_PRODUCT_ID 26
50 #define USB_MEMO2_HSLS_PRODUCT_ID 27
51 #define USB_LEAF_LITE_CH_PRODUCT_ID 28
52 #define USB_BLACKBIRD_SPRO_PRODUCT_ID 29
53 #define USB_OEM_MERCURY_PRODUCT_ID 34
54 #define USB_OEM_LEAF_PRODUCT_ID 35
55 #define USB_CAN_R_PRODUCT_ID 39
57 /* USB devices features */
58 #define KVASER_HAS_SILENT_MODE BIT(0)
59 #define KVASER_HAS_TXRX_ERRORS BIT(1)
61 /* Message header size */
62 #define MSG_HEADER_LEN 2
64 /* Can message flags */
65 #define MSG_FLAG_ERROR_FRAME BIT(0)
66 #define MSG_FLAG_OVERRUN BIT(1)
67 #define MSG_FLAG_NERR BIT(2)
68 #define MSG_FLAG_WAKEUP BIT(3)
69 #define MSG_FLAG_REMOTE_FRAME BIT(4)
70 #define MSG_FLAG_RESERVED BIT(5)
71 #define MSG_FLAG_TX_ACK BIT(6)
72 #define MSG_FLAG_TX_REQUEST BIT(7)
75 #define M16C_STATE_BUS_RESET BIT(0)
76 #define M16C_STATE_BUS_ERROR BIT(4)
77 #define M16C_STATE_BUS_PASSIVE BIT(5)
78 #define M16C_STATE_BUS_OFF BIT(6)
81 #define CMD_RX_STD_MESSAGE 12
82 #define CMD_TX_STD_MESSAGE 13
83 #define CMD_RX_EXT_MESSAGE 14
84 #define CMD_TX_EXT_MESSAGE 15
85 #define CMD_SET_BUS_PARAMS 16
86 #define CMD_GET_BUS_PARAMS 17
87 #define CMD_GET_BUS_PARAMS_REPLY 18
88 #define CMD_GET_CHIP_STATE 19
89 #define CMD_CHIP_STATE_EVENT 20
90 #define CMD_SET_CTRL_MODE 21
91 #define CMD_GET_CTRL_MODE 22
92 #define CMD_GET_CTRL_MODE_REPLY 23
93 #define CMD_RESET_CHIP 24
94 #define CMD_RESET_CARD 25
95 #define CMD_START_CHIP 26
96 #define CMD_START_CHIP_REPLY 27
97 #define CMD_STOP_CHIP 28
98 #define CMD_STOP_CHIP_REPLY 29
99 #define CMD_GET_CARD_INFO2 32
100 #define CMD_GET_CARD_INFO 34
101 #define CMD_GET_CARD_INFO_REPLY 35
102 #define CMD_GET_SOFTWARE_INFO 38
103 #define CMD_GET_SOFTWARE_INFO_REPLY 39
104 #define CMD_ERROR_EVENT 45
105 #define CMD_FLUSH_QUEUE 48
106 #define CMD_RESET_ERROR_COUNTER 49
107 #define CMD_TX_ACKNOWLEDGE 50
108 #define CMD_CAN_ERROR_EVENT 51
109 #define CMD_USB_THROTTLE 77
110 #define CMD_LOG_MESSAGE 106
113 #define M16C_EF_ACKE BIT(0)
114 #define M16C_EF_CRCE BIT(1)
115 #define M16C_EF_FORME BIT(2)
116 #define M16C_EF_STFE BIT(3)
117 #define M16C_EF_BITE0 BIT(4)
118 #define M16C_EF_BITE1 BIT(5)
119 #define M16C_EF_RCVE BIT(6)
120 #define M16C_EF_TRE BIT(7)
122 /* bittiming parameters */
123 #define KVASER_USB_TSEG1_MIN 1
124 #define KVASER_USB_TSEG1_MAX 16
125 #define KVASER_USB_TSEG2_MIN 1
126 #define KVASER_USB_TSEG2_MAX 8
127 #define KVASER_USB_SJW_MAX 4
128 #define KVASER_USB_BRP_MIN 1
129 #define KVASER_USB_BRP_MAX 64
130 #define KVASER_USB_BRP_INC 1
133 #define KVASER_CTRL_MODE_NORMAL 1
134 #define KVASER_CTRL_MODE_SILENT 2
135 #define KVASER_CTRL_MODE_SELFRECEPTION 3
136 #define KVASER_CTRL_MODE_OFF 4
139 #define KVASER_EXTENDED_FRAME BIT(31)
141 struct kvaser_msg_simple
{
146 struct kvaser_msg_cardinfo
{
149 __le32 serial_number
;
151 __le32 clock_resolution
;
159 struct kvaser_msg_cardinfo2
{
163 __le32 oem_unlock_code
;
166 struct kvaser_msg_softinfo
{
171 __le16 max_outstanding_tx
;
175 struct kvaser_msg_busparams
{
185 struct kvaser_msg_tx_can
{
193 struct kvaser_msg_rx_can
{
200 struct kvaser_msg_chip_state_event
{
210 struct kvaser_msg_tx_acknowledge
{
218 struct kvaser_msg_error_event
{
230 struct kvaser_msg_ctrl_mode
{
237 struct kvaser_msg_flush_queue
{
244 struct kvaser_msg_log_message
{
258 struct kvaser_msg_simple simple
;
259 struct kvaser_msg_cardinfo cardinfo
;
260 struct kvaser_msg_cardinfo2 cardinfo2
;
261 struct kvaser_msg_softinfo softinfo
;
262 struct kvaser_msg_busparams busparams
;
263 struct kvaser_msg_tx_can tx_can
;
264 struct kvaser_msg_rx_can rx_can
;
265 struct kvaser_msg_chip_state_event chip_state_event
;
266 struct kvaser_msg_tx_acknowledge tx_acknowledge
;
267 struct kvaser_msg_error_event error_event
;
268 struct kvaser_msg_ctrl_mode ctrl_mode
;
269 struct kvaser_msg_flush_queue flush_queue
;
270 struct kvaser_msg_log_message log_message
;
274 struct kvaser_usb_tx_urb_context
{
275 struct kvaser_usb_net_priv
*priv
;
281 struct usb_device
*udev
;
282 struct kvaser_usb_net_priv
*nets
[MAX_NET_DEVICES
];
284 struct usb_endpoint_descriptor
*bulk_in
, *bulk_out
;
285 struct usb_anchor rx_submitted
;
288 unsigned int nchannels
;
291 void *rxbuf
[MAX_RX_URBS
];
292 dma_addr_t rxbuf_dma
[MAX_RX_URBS
];
295 struct kvaser_usb_net_priv
{
298 atomic_t active_tx_urbs
;
299 struct usb_anchor tx_submitted
;
300 struct kvaser_usb_tx_urb_context tx_contexts
[MAX_TX_URBS
];
302 struct completion start_comp
, stop_comp
;
304 struct kvaser_usb
*dev
;
305 struct net_device
*netdev
;
308 struct can_berr_counter bec
;
311 static const struct usb_device_id kvaser_usb_table
[] = {
312 { USB_DEVICE(KVASER_VENDOR_ID
, USB_LEAF_DEVEL_PRODUCT_ID
) },
313 { USB_DEVICE(KVASER_VENDOR_ID
, USB_LEAF_LITE_PRODUCT_ID
) },
314 { USB_DEVICE(KVASER_VENDOR_ID
, USB_LEAF_PRO_PRODUCT_ID
),
315 .driver_info
= KVASER_HAS_TXRX_ERRORS
|
316 KVASER_HAS_SILENT_MODE
},
317 { USB_DEVICE(KVASER_VENDOR_ID
, USB_LEAF_SPRO_PRODUCT_ID
),
318 .driver_info
= KVASER_HAS_TXRX_ERRORS
|
319 KVASER_HAS_SILENT_MODE
},
320 { USB_DEVICE(KVASER_VENDOR_ID
, USB_LEAF_PRO_LS_PRODUCT_ID
),
321 .driver_info
= KVASER_HAS_TXRX_ERRORS
|
322 KVASER_HAS_SILENT_MODE
},
323 { USB_DEVICE(KVASER_VENDOR_ID
, USB_LEAF_PRO_SWC_PRODUCT_ID
),
324 .driver_info
= KVASER_HAS_TXRX_ERRORS
|
325 KVASER_HAS_SILENT_MODE
},
326 { USB_DEVICE(KVASER_VENDOR_ID
, USB_LEAF_PRO_LIN_PRODUCT_ID
),
327 .driver_info
= KVASER_HAS_TXRX_ERRORS
|
328 KVASER_HAS_SILENT_MODE
},
329 { USB_DEVICE(KVASER_VENDOR_ID
, USB_LEAF_SPRO_LS_PRODUCT_ID
),
330 .driver_info
= KVASER_HAS_TXRX_ERRORS
|
331 KVASER_HAS_SILENT_MODE
},
332 { USB_DEVICE(KVASER_VENDOR_ID
, USB_LEAF_SPRO_SWC_PRODUCT_ID
),
333 .driver_info
= KVASER_HAS_TXRX_ERRORS
|
334 KVASER_HAS_SILENT_MODE
},
335 { USB_DEVICE(KVASER_VENDOR_ID
, USB_MEMO2_DEVEL_PRODUCT_ID
),
336 .driver_info
= KVASER_HAS_TXRX_ERRORS
|
337 KVASER_HAS_SILENT_MODE
},
338 { USB_DEVICE(KVASER_VENDOR_ID
, USB_MEMO2_HSHS_PRODUCT_ID
),
339 .driver_info
= KVASER_HAS_TXRX_ERRORS
|
340 KVASER_HAS_SILENT_MODE
},
341 { USB_DEVICE(KVASER_VENDOR_ID
, USB_UPRO_HSHS_PRODUCT_ID
),
342 .driver_info
= KVASER_HAS_TXRX_ERRORS
},
343 { USB_DEVICE(KVASER_VENDOR_ID
, USB_LEAF_LITE_GI_PRODUCT_ID
) },
344 { USB_DEVICE(KVASER_VENDOR_ID
, USB_LEAF_PRO_OBDII_PRODUCT_ID
),
345 .driver_info
= KVASER_HAS_TXRX_ERRORS
|
346 KVASER_HAS_SILENT_MODE
},
347 { USB_DEVICE(KVASER_VENDOR_ID
, USB_MEMO2_HSLS_PRODUCT_ID
),
348 .driver_info
= KVASER_HAS_TXRX_ERRORS
},
349 { USB_DEVICE(KVASER_VENDOR_ID
, USB_LEAF_LITE_CH_PRODUCT_ID
),
350 .driver_info
= KVASER_HAS_TXRX_ERRORS
},
351 { USB_DEVICE(KVASER_VENDOR_ID
, USB_BLACKBIRD_SPRO_PRODUCT_ID
),
352 .driver_info
= KVASER_HAS_TXRX_ERRORS
},
353 { USB_DEVICE(KVASER_VENDOR_ID
, USB_OEM_MERCURY_PRODUCT_ID
),
354 .driver_info
= KVASER_HAS_TXRX_ERRORS
},
355 { USB_DEVICE(KVASER_VENDOR_ID
, USB_OEM_LEAF_PRODUCT_ID
),
356 .driver_info
= KVASER_HAS_TXRX_ERRORS
},
357 { USB_DEVICE(KVASER_VENDOR_ID
, USB_CAN_R_PRODUCT_ID
),
358 .driver_info
= KVASER_HAS_TXRX_ERRORS
},
361 MODULE_DEVICE_TABLE(usb
, kvaser_usb_table
);
363 static inline int kvaser_usb_send_msg(const struct kvaser_usb
*dev
,
364 struct kvaser_msg
*msg
)
368 return usb_bulk_msg(dev
->udev
,
369 usb_sndbulkpipe(dev
->udev
,
370 dev
->bulk_out
->bEndpointAddress
),
371 msg
, msg
->len
, &actual_len
,
375 static int kvaser_usb_wait_msg(const struct kvaser_usb
*dev
, u8 id
,
376 struct kvaser_msg
*msg
)
378 struct kvaser_msg
*tmp
;
384 buf
= kzalloc(RX_BUFFER_SIZE
, GFP_KERNEL
);
388 err
= usb_bulk_msg(dev
->udev
,
389 usb_rcvbulkpipe(dev
->udev
,
390 dev
->bulk_in
->bEndpointAddress
),
391 buf
, RX_BUFFER_SIZE
, &actual_len
,
396 while (pos
<= actual_len
- MSG_HEADER_LEN
) {
402 if (pos
+ tmp
->len
> actual_len
) {
403 dev_err(dev
->udev
->dev
.parent
, "Format error\n");
408 memcpy(msg
, tmp
, tmp
->len
);
423 static int kvaser_usb_send_simple_msg(const struct kvaser_usb
*dev
,
424 u8 msg_id
, int channel
)
426 struct kvaser_msg
*msg
;
429 msg
= kmalloc(sizeof(*msg
), GFP_KERNEL
);
434 msg
->len
= MSG_HEADER_LEN
+ sizeof(struct kvaser_msg_simple
);
435 msg
->u
.simple
.channel
= channel
;
436 msg
->u
.simple
.tid
= 0xff;
438 rc
= kvaser_usb_send_msg(dev
, msg
);
444 static int kvaser_usb_get_software_info(struct kvaser_usb
*dev
)
446 struct kvaser_msg msg
;
449 err
= kvaser_usb_send_simple_msg(dev
, CMD_GET_SOFTWARE_INFO
, 0);
453 err
= kvaser_usb_wait_msg(dev
, CMD_GET_SOFTWARE_INFO_REPLY
, &msg
);
457 dev
->fw_version
= le32_to_cpu(msg
.u
.softinfo
.fw_version
);
462 static int kvaser_usb_get_card_info(struct kvaser_usb
*dev
)
464 struct kvaser_msg msg
;
467 err
= kvaser_usb_send_simple_msg(dev
, CMD_GET_CARD_INFO
, 0);
471 err
= kvaser_usb_wait_msg(dev
, CMD_GET_CARD_INFO_REPLY
, &msg
);
475 dev
->nchannels
= msg
.u
.cardinfo
.nchannels
;
480 static void kvaser_usb_tx_acknowledge(const struct kvaser_usb
*dev
,
481 const struct kvaser_msg
*msg
)
483 struct net_device_stats
*stats
;
484 struct kvaser_usb_tx_urb_context
*context
;
485 struct kvaser_usb_net_priv
*priv
;
487 struct can_frame
*cf
;
488 u8 channel
= msg
->u
.tx_acknowledge
.channel
;
489 u8 tid
= msg
->u
.tx_acknowledge
.tid
;
491 if (channel
>= dev
->nchannels
) {
492 dev_err(dev
->udev
->dev
.parent
,
493 "Invalid channel number (%d)\n", channel
);
497 priv
= dev
->nets
[channel
];
499 if (!netif_device_present(priv
->netdev
))
502 stats
= &priv
->netdev
->stats
;
504 context
= &priv
->tx_contexts
[tid
% MAX_TX_URBS
];
506 /* Sometimes the state change doesn't come after a bus-off event */
507 if (priv
->can
.restart_ms
&&
508 (priv
->can
.state
>= CAN_STATE_BUS_OFF
)) {
509 skb
= alloc_can_err_skb(priv
->netdev
, &cf
);
511 cf
->can_id
|= CAN_ERR_RESTARTED
;
515 stats
->rx_bytes
+= cf
->can_dlc
;
517 netdev_err(priv
->netdev
,
518 "No memory left for err_skb\n");
521 priv
->can
.can_stats
.restarts
++;
522 netif_carrier_on(priv
->netdev
);
524 priv
->can
.state
= CAN_STATE_ERROR_ACTIVE
;
528 stats
->tx_bytes
+= context
->dlc
;
529 can_get_echo_skb(priv
->netdev
, context
->echo_index
);
531 context
->echo_index
= MAX_TX_URBS
;
532 atomic_dec(&priv
->active_tx_urbs
);
534 netif_wake_queue(priv
->netdev
);
537 static void kvaser_usb_simple_msg_callback(struct urb
*urb
)
539 struct net_device
*netdev
= urb
->context
;
541 kfree(urb
->transfer_buffer
);
544 netdev_warn(netdev
, "urb status received: %d\n",
548 static int kvaser_usb_simple_msg_async(struct kvaser_usb_net_priv
*priv
,
551 struct kvaser_usb
*dev
= priv
->dev
;
552 struct net_device
*netdev
= priv
->netdev
;
553 struct kvaser_msg
*msg
;
558 urb
= usb_alloc_urb(0, GFP_ATOMIC
);
560 netdev_err(netdev
, "No memory left for URBs\n");
564 buf
= kmalloc(sizeof(struct kvaser_msg
), GFP_ATOMIC
);
570 msg
= (struct kvaser_msg
*)buf
;
571 msg
->len
= MSG_HEADER_LEN
+ sizeof(struct kvaser_msg_simple
);
573 msg
->u
.simple
.channel
= priv
->channel
;
575 usb_fill_bulk_urb(urb
, dev
->udev
,
576 usb_sndbulkpipe(dev
->udev
,
577 dev
->bulk_out
->bEndpointAddress
),
579 kvaser_usb_simple_msg_callback
, priv
);
580 usb_anchor_urb(urb
, &priv
->tx_submitted
);
582 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
584 netdev_err(netdev
, "Error transmitting URB\n");
585 usb_unanchor_urb(urb
);
596 static void kvaser_usb_unlink_tx_urbs(struct kvaser_usb_net_priv
*priv
)
600 usb_kill_anchored_urbs(&priv
->tx_submitted
);
601 atomic_set(&priv
->active_tx_urbs
, 0);
603 for (i
= 0; i
< MAX_TX_URBS
; i
++)
604 priv
->tx_contexts
[i
].echo_index
= MAX_TX_URBS
;
607 static void kvaser_usb_rx_error(const struct kvaser_usb
*dev
,
608 const struct kvaser_msg
*msg
)
610 struct can_frame
*cf
;
612 struct net_device_stats
*stats
;
613 struct kvaser_usb_net_priv
*priv
;
614 unsigned int new_state
;
615 u8 channel
, status
, txerr
, rxerr
, error_factor
;
618 case CMD_CAN_ERROR_EVENT
:
619 channel
= msg
->u
.error_event
.channel
;
620 status
= msg
->u
.error_event
.status
;
621 txerr
= msg
->u
.error_event
.tx_errors_count
;
622 rxerr
= msg
->u
.error_event
.rx_errors_count
;
623 error_factor
= msg
->u
.error_event
.error_factor
;
625 case CMD_LOG_MESSAGE
:
626 channel
= msg
->u
.log_message
.channel
;
627 status
= msg
->u
.log_message
.data
[0];
628 txerr
= msg
->u
.log_message
.data
[2];
629 rxerr
= msg
->u
.log_message
.data
[3];
630 error_factor
= msg
->u
.log_message
.data
[1];
632 case CMD_CHIP_STATE_EVENT
:
633 channel
= msg
->u
.chip_state_event
.channel
;
634 status
= msg
->u
.chip_state_event
.status
;
635 txerr
= msg
->u
.chip_state_event
.tx_errors_count
;
636 rxerr
= msg
->u
.chip_state_event
.rx_errors_count
;
640 dev_err(dev
->udev
->dev
.parent
, "Invalid msg id (%d)\n",
645 if (channel
>= dev
->nchannels
) {
646 dev_err(dev
->udev
->dev
.parent
,
647 "Invalid channel number (%d)\n", channel
);
651 priv
= dev
->nets
[channel
];
652 stats
= &priv
->netdev
->stats
;
654 if (status
& M16C_STATE_BUS_RESET
) {
655 kvaser_usb_unlink_tx_urbs(priv
);
659 skb
= alloc_can_err_skb(priv
->netdev
, &cf
);
665 new_state
= priv
->can
.state
;
667 netdev_dbg(priv
->netdev
, "Error status: 0x%02x\n", status
);
669 if (status
& M16C_STATE_BUS_OFF
) {
670 cf
->can_id
|= CAN_ERR_BUSOFF
;
672 priv
->can
.can_stats
.bus_off
++;
673 if (!priv
->can
.restart_ms
)
674 kvaser_usb_simple_msg_async(priv
, CMD_STOP_CHIP
);
676 netif_carrier_off(priv
->netdev
);
678 new_state
= CAN_STATE_BUS_OFF
;
679 } else if (status
& M16C_STATE_BUS_PASSIVE
) {
680 if (priv
->can
.state
!= CAN_STATE_ERROR_PASSIVE
) {
681 cf
->can_id
|= CAN_ERR_CRTL
;
684 cf
->data
[1] = (txerr
> rxerr
)
685 ? CAN_ERR_CRTL_TX_PASSIVE
686 : CAN_ERR_CRTL_RX_PASSIVE
;
688 cf
->data
[1] = CAN_ERR_CRTL_TX_PASSIVE
|
689 CAN_ERR_CRTL_RX_PASSIVE
;
691 priv
->can
.can_stats
.error_passive
++;
694 new_state
= CAN_STATE_ERROR_PASSIVE
;
697 if (status
== M16C_STATE_BUS_ERROR
) {
698 if ((priv
->can
.state
< CAN_STATE_ERROR_WARNING
) &&
699 ((txerr
>= 96) || (rxerr
>= 96))) {
700 cf
->can_id
|= CAN_ERR_CRTL
;
701 cf
->data
[1] = (txerr
> rxerr
)
702 ? CAN_ERR_CRTL_TX_WARNING
703 : CAN_ERR_CRTL_RX_WARNING
;
705 priv
->can
.can_stats
.error_warning
++;
706 new_state
= CAN_STATE_ERROR_WARNING
;
707 } else if (priv
->can
.state
> CAN_STATE_ERROR_ACTIVE
) {
708 cf
->can_id
|= CAN_ERR_PROT
;
709 cf
->data
[2] = CAN_ERR_PROT_ACTIVE
;
711 new_state
= CAN_STATE_ERROR_ACTIVE
;
716 cf
->can_id
|= CAN_ERR_PROT
;
717 cf
->data
[2] = CAN_ERR_PROT_ACTIVE
;
719 new_state
= CAN_STATE_ERROR_ACTIVE
;
722 if (priv
->can
.restart_ms
&&
723 (priv
->can
.state
>= CAN_STATE_BUS_OFF
) &&
724 (new_state
< CAN_STATE_BUS_OFF
)) {
725 cf
->can_id
|= CAN_ERR_RESTARTED
;
726 netif_carrier_on(priv
->netdev
);
728 priv
->can
.can_stats
.restarts
++;
732 priv
->can
.can_stats
.bus_error
++;
735 cf
->can_id
|= CAN_ERR_BUSERROR
| CAN_ERR_PROT
;
737 if (error_factor
& M16C_EF_ACKE
)
738 cf
->data
[3] |= (CAN_ERR_PROT_LOC_ACK
);
739 if (error_factor
& M16C_EF_CRCE
)
740 cf
->data
[3] |= (CAN_ERR_PROT_LOC_CRC_SEQ
|
741 CAN_ERR_PROT_LOC_CRC_DEL
);
742 if (error_factor
& M16C_EF_FORME
)
743 cf
->data
[2] |= CAN_ERR_PROT_FORM
;
744 if (error_factor
& M16C_EF_STFE
)
745 cf
->data
[2] |= CAN_ERR_PROT_STUFF
;
746 if (error_factor
& M16C_EF_BITE0
)
747 cf
->data
[2] |= CAN_ERR_PROT_BIT0
;
748 if (error_factor
& M16C_EF_BITE1
)
749 cf
->data
[2] |= CAN_ERR_PROT_BIT1
;
750 if (error_factor
& M16C_EF_TRE
)
751 cf
->data
[2] |= CAN_ERR_PROT_TX
;
757 priv
->bec
.txerr
= txerr
;
758 priv
->bec
.rxerr
= rxerr
;
760 priv
->can
.state
= new_state
;
765 stats
->rx_bytes
+= cf
->can_dlc
;
768 static void kvaser_usb_rx_can_err(const struct kvaser_usb_net_priv
*priv
,
769 const struct kvaser_msg
*msg
)
771 struct can_frame
*cf
;
773 struct net_device_stats
*stats
= &priv
->netdev
->stats
;
775 if (msg
->u
.rx_can
.flag
& (MSG_FLAG_ERROR_FRAME
|
777 netdev_err(priv
->netdev
, "Unknow error (flags: 0x%02x)\n",
784 if (msg
->u
.rx_can
.flag
& MSG_FLAG_OVERRUN
) {
785 skb
= alloc_can_err_skb(priv
->netdev
, &cf
);
791 cf
->can_id
|= CAN_ERR_CRTL
;
792 cf
->data
[1] = CAN_ERR_CRTL_RX_OVERFLOW
;
794 stats
->rx_over_errors
++;
800 stats
->rx_bytes
+= cf
->can_dlc
;
804 static void kvaser_usb_rx_can_msg(const struct kvaser_usb
*dev
,
805 const struct kvaser_msg
*msg
)
807 struct kvaser_usb_net_priv
*priv
;
808 struct can_frame
*cf
;
810 struct net_device_stats
*stats
;
811 u8 channel
= msg
->u
.rx_can
.channel
;
813 if (channel
>= dev
->nchannels
) {
814 dev_err(dev
->udev
->dev
.parent
,
815 "Invalid channel number (%d)\n", channel
);
819 priv
= dev
->nets
[channel
];
820 stats
= &priv
->netdev
->stats
;
822 if ((msg
->u
.rx_can
.flag
& MSG_FLAG_ERROR_FRAME
) &&
823 (msg
->id
== CMD_LOG_MESSAGE
)) {
824 kvaser_usb_rx_error(dev
, msg
);
826 } else if (msg
->u
.rx_can
.flag
& (MSG_FLAG_ERROR_FRAME
|
829 kvaser_usb_rx_can_err(priv
, msg
);
831 } else if (msg
->u
.rx_can
.flag
& ~MSG_FLAG_REMOTE_FRAME
) {
832 netdev_warn(priv
->netdev
,
833 "Unhandled frame (flags: 0x%02x)",
838 skb
= alloc_can_skb(priv
->netdev
, &cf
);
844 if (msg
->id
== CMD_LOG_MESSAGE
) {
845 cf
->can_id
= le32_to_cpu(msg
->u
.log_message
.id
);
846 if (cf
->can_id
& KVASER_EXTENDED_FRAME
)
847 cf
->can_id
&= CAN_EFF_MASK
| CAN_EFF_FLAG
;
849 cf
->can_id
&= CAN_SFF_MASK
;
851 cf
->can_dlc
= get_can_dlc(msg
->u
.log_message
.dlc
);
853 if (msg
->u
.log_message
.flags
& MSG_FLAG_REMOTE_FRAME
)
854 cf
->can_id
|= CAN_RTR_FLAG
;
856 memcpy(cf
->data
, &msg
->u
.log_message
.data
,
859 cf
->can_id
= ((msg
->u
.rx_can
.msg
[0] & 0x1f) << 6) |
860 (msg
->u
.rx_can
.msg
[1] & 0x3f);
862 if (msg
->id
== CMD_RX_EXT_MESSAGE
) {
864 cf
->can_id
|= ((msg
->u
.rx_can
.msg
[2] & 0x0f) << 14) |
865 ((msg
->u
.rx_can
.msg
[3] & 0xff) << 6) |
866 (msg
->u
.rx_can
.msg
[4] & 0x3f);
867 cf
->can_id
|= CAN_EFF_FLAG
;
870 cf
->can_dlc
= get_can_dlc(msg
->u
.rx_can
.msg
[5]);
872 if (msg
->u
.rx_can
.flag
& MSG_FLAG_REMOTE_FRAME
)
873 cf
->can_id
|= CAN_RTR_FLAG
;
875 memcpy(cf
->data
, &msg
->u
.rx_can
.msg
[6],
882 stats
->rx_bytes
+= cf
->can_dlc
;
885 static void kvaser_usb_start_chip_reply(const struct kvaser_usb
*dev
,
886 const struct kvaser_msg
*msg
)
888 struct kvaser_usb_net_priv
*priv
;
889 u8 channel
= msg
->u
.simple
.channel
;
891 if (channel
>= dev
->nchannels
) {
892 dev_err(dev
->udev
->dev
.parent
,
893 "Invalid channel number (%d)\n", channel
);
897 priv
= dev
->nets
[channel
];
899 if (completion_done(&priv
->start_comp
) &&
900 netif_queue_stopped(priv
->netdev
)) {
901 netif_wake_queue(priv
->netdev
);
903 netif_start_queue(priv
->netdev
);
904 complete(&priv
->start_comp
);
908 static void kvaser_usb_stop_chip_reply(const struct kvaser_usb
*dev
,
909 const struct kvaser_msg
*msg
)
911 struct kvaser_usb_net_priv
*priv
;
912 u8 channel
= msg
->u
.simple
.channel
;
914 if (channel
>= dev
->nchannels
) {
915 dev_err(dev
->udev
->dev
.parent
,
916 "Invalid channel number (%d)\n", channel
);
920 priv
= dev
->nets
[channel
];
922 complete(&priv
->stop_comp
);
925 static void kvaser_usb_handle_message(const struct kvaser_usb
*dev
,
926 const struct kvaser_msg
*msg
)
929 case CMD_START_CHIP_REPLY
:
930 kvaser_usb_start_chip_reply(dev
, msg
);
933 case CMD_STOP_CHIP_REPLY
:
934 kvaser_usb_stop_chip_reply(dev
, msg
);
937 case CMD_RX_STD_MESSAGE
:
938 case CMD_RX_EXT_MESSAGE
:
939 case CMD_LOG_MESSAGE
:
940 kvaser_usb_rx_can_msg(dev
, msg
);
943 case CMD_CHIP_STATE_EVENT
:
944 case CMD_CAN_ERROR_EVENT
:
945 kvaser_usb_rx_error(dev
, msg
);
948 case CMD_TX_ACKNOWLEDGE
:
949 kvaser_usb_tx_acknowledge(dev
, msg
);
953 dev_warn(dev
->udev
->dev
.parent
,
954 "Unhandled message (%d)\n", msg
->id
);
959 static void kvaser_usb_read_bulk_callback(struct urb
*urb
)
961 struct kvaser_usb
*dev
= urb
->context
;
962 struct kvaser_msg
*msg
;
966 switch (urb
->status
) {
973 dev_info(dev
->udev
->dev
.parent
, "Rx URB aborted (%d)\n",
978 while (pos
<= urb
->actual_length
- MSG_HEADER_LEN
) {
979 msg
= urb
->transfer_buffer
+ pos
;
984 if (pos
+ msg
->len
> urb
->actual_length
) {
985 dev_err(dev
->udev
->dev
.parent
, "Format error\n");
989 kvaser_usb_handle_message(dev
, msg
);
995 usb_fill_bulk_urb(urb
, dev
->udev
,
996 usb_rcvbulkpipe(dev
->udev
,
997 dev
->bulk_in
->bEndpointAddress
),
998 urb
->transfer_buffer
, RX_BUFFER_SIZE
,
999 kvaser_usb_read_bulk_callback
, dev
);
1001 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
1002 if (err
== -ENODEV
) {
1003 for (i
= 0; i
< dev
->nchannels
; i
++) {
1007 netif_device_detach(dev
->nets
[i
]->netdev
);
1010 dev_err(dev
->udev
->dev
.parent
,
1011 "Failed resubmitting read bulk urb: %d\n", err
);
1017 static int kvaser_usb_setup_rx_urbs(struct kvaser_usb
*dev
)
1021 if (dev
->rxinitdone
)
1024 for (i
= 0; i
< MAX_RX_URBS
; i
++) {
1025 struct urb
*urb
= NULL
;
1029 urb
= usb_alloc_urb(0, GFP_KERNEL
);
1031 dev_warn(dev
->udev
->dev
.parent
,
1032 "No memory left for URBs\n");
1037 buf
= usb_alloc_coherent(dev
->udev
, RX_BUFFER_SIZE
,
1038 GFP_KERNEL
, &buf_dma
);
1040 dev_warn(dev
->udev
->dev
.parent
,
1041 "No memory left for USB buffer\n");
1047 usb_fill_bulk_urb(urb
, dev
->udev
,
1048 usb_rcvbulkpipe(dev
->udev
,
1049 dev
->bulk_in
->bEndpointAddress
),
1050 buf
, RX_BUFFER_SIZE
,
1051 kvaser_usb_read_bulk_callback
,
1053 urb
->transfer_dma
= buf_dma
;
1054 urb
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
1055 usb_anchor_urb(urb
, &dev
->rx_submitted
);
1057 err
= usb_submit_urb(urb
, GFP_KERNEL
);
1059 usb_unanchor_urb(urb
);
1060 usb_free_coherent(dev
->udev
, RX_BUFFER_SIZE
, buf
,
1066 dev
->rxbuf
[i
] = buf
;
1067 dev
->rxbuf_dma
[i
] = buf_dma
;
1073 dev_warn(dev
->udev
->dev
.parent
,
1074 "Cannot setup read URBs, error %d\n", err
);
1076 } else if (i
< MAX_RX_URBS
) {
1077 dev_warn(dev
->udev
->dev
.parent
,
1078 "RX performances may be slow\n");
1081 dev
->rxinitdone
= true;
1086 static int kvaser_usb_set_opt_mode(const struct kvaser_usb_net_priv
*priv
)
1088 struct kvaser_msg
*msg
;
1091 msg
= kmalloc(sizeof(*msg
), GFP_KERNEL
);
1095 msg
->id
= CMD_SET_CTRL_MODE
;
1096 msg
->len
= MSG_HEADER_LEN
+ sizeof(struct kvaser_msg_ctrl_mode
);
1097 msg
->u
.ctrl_mode
.tid
= 0xff;
1098 msg
->u
.ctrl_mode
.channel
= priv
->channel
;
1100 if (priv
->can
.ctrlmode
& CAN_CTRLMODE_LISTENONLY
)
1101 msg
->u
.ctrl_mode
.ctrl_mode
= KVASER_CTRL_MODE_SILENT
;
1103 msg
->u
.ctrl_mode
.ctrl_mode
= KVASER_CTRL_MODE_NORMAL
;
1105 rc
= kvaser_usb_send_msg(priv
->dev
, msg
);
1111 static int kvaser_usb_start_chip(struct kvaser_usb_net_priv
*priv
)
1115 init_completion(&priv
->start_comp
);
1117 err
= kvaser_usb_send_simple_msg(priv
->dev
, CMD_START_CHIP
,
1122 if (!wait_for_completion_timeout(&priv
->start_comp
,
1123 msecs_to_jiffies(START_TIMEOUT
)))
1129 static int kvaser_usb_open(struct net_device
*netdev
)
1131 struct kvaser_usb_net_priv
*priv
= netdev_priv(netdev
);
1132 struct kvaser_usb
*dev
= priv
->dev
;
1135 err
= open_candev(netdev
);
1139 err
= kvaser_usb_setup_rx_urbs(dev
);
1143 err
= kvaser_usb_set_opt_mode(priv
);
1147 err
= kvaser_usb_start_chip(priv
);
1149 netdev_warn(netdev
, "Cannot start device, error %d\n", err
);
1153 priv
->can
.state
= CAN_STATE_ERROR_ACTIVE
;
1158 close_candev(netdev
);
1162 static void kvaser_usb_unlink_all_urbs(struct kvaser_usb
*dev
)
1166 usb_kill_anchored_urbs(&dev
->rx_submitted
);
1168 for (i
= 0; i
< MAX_RX_URBS
; i
++)
1169 usb_free_coherent(dev
->udev
, RX_BUFFER_SIZE
,
1173 for (i
= 0; i
< MAX_NET_DEVICES
; i
++) {
1174 struct kvaser_usb_net_priv
*priv
= dev
->nets
[i
];
1177 kvaser_usb_unlink_tx_urbs(priv
);
1181 static int kvaser_usb_stop_chip(struct kvaser_usb_net_priv
*priv
)
1185 init_completion(&priv
->stop_comp
);
1187 err
= kvaser_usb_send_simple_msg(priv
->dev
, CMD_STOP_CHIP
,
1192 if (!wait_for_completion_timeout(&priv
->stop_comp
,
1193 msecs_to_jiffies(STOP_TIMEOUT
)))
1199 static int kvaser_usb_flush_queue(struct kvaser_usb_net_priv
*priv
)
1201 struct kvaser_msg
*msg
;
1204 msg
= kmalloc(sizeof(*msg
), GFP_KERNEL
);
1208 msg
->id
= CMD_FLUSH_QUEUE
;
1209 msg
->len
= MSG_HEADER_LEN
+ sizeof(struct kvaser_msg_flush_queue
);
1210 msg
->u
.flush_queue
.channel
= priv
->channel
;
1211 msg
->u
.flush_queue
.flags
= 0x00;
1213 rc
= kvaser_usb_send_msg(priv
->dev
, msg
);
1219 static int kvaser_usb_close(struct net_device
*netdev
)
1221 struct kvaser_usb_net_priv
*priv
= netdev_priv(netdev
);
1222 struct kvaser_usb
*dev
= priv
->dev
;
1225 netif_stop_queue(netdev
);
1227 err
= kvaser_usb_flush_queue(priv
);
1229 netdev_warn(netdev
, "Cannot flush queue, error %d\n", err
);
1231 if (kvaser_usb_send_simple_msg(dev
, CMD_RESET_CHIP
, priv
->channel
))
1232 netdev_warn(netdev
, "Cannot reset card, error %d\n", err
);
1234 err
= kvaser_usb_stop_chip(priv
);
1236 netdev_warn(netdev
, "Cannot stop device, error %d\n", err
);
1238 priv
->can
.state
= CAN_STATE_STOPPED
;
1239 close_candev(priv
->netdev
);
1244 static void kvaser_usb_write_bulk_callback(struct urb
*urb
)
1246 struct kvaser_usb_tx_urb_context
*context
= urb
->context
;
1247 struct kvaser_usb_net_priv
*priv
;
1248 struct net_device
*netdev
;
1250 if (WARN_ON(!context
))
1253 priv
= context
->priv
;
1254 netdev
= priv
->netdev
;
1256 kfree(urb
->transfer_buffer
);
1258 if (!netif_device_present(netdev
))
1262 netdev_info(netdev
, "Tx URB aborted (%d)\n", urb
->status
);
1265 static netdev_tx_t
kvaser_usb_start_xmit(struct sk_buff
*skb
,
1266 struct net_device
*netdev
)
1268 struct kvaser_usb_net_priv
*priv
= netdev_priv(netdev
);
1269 struct kvaser_usb
*dev
= priv
->dev
;
1270 struct net_device_stats
*stats
= &netdev
->stats
;
1271 struct can_frame
*cf
= (struct can_frame
*)skb
->data
;
1272 struct kvaser_usb_tx_urb_context
*context
= NULL
;
1275 struct kvaser_msg
*msg
;
1277 int ret
= NETDEV_TX_OK
;
1279 if (can_dropped_invalid_skb(netdev
, skb
))
1280 return NETDEV_TX_OK
;
1282 urb
= usb_alloc_urb(0, GFP_ATOMIC
);
1284 netdev_err(netdev
, "No memory left for URBs\n");
1285 stats
->tx_dropped
++;
1289 buf
= kmalloc(sizeof(struct kvaser_msg
), GFP_ATOMIC
);
1291 stats
->tx_dropped
++;
1296 msg
->len
= MSG_HEADER_LEN
+ sizeof(struct kvaser_msg_tx_can
);
1297 msg
->u
.tx_can
.flags
= 0;
1298 msg
->u
.tx_can
.channel
= priv
->channel
;
1300 if (cf
->can_id
& CAN_EFF_FLAG
) {
1301 msg
->id
= CMD_TX_EXT_MESSAGE
;
1302 msg
->u
.tx_can
.msg
[0] = (cf
->can_id
>> 24) & 0x1f;
1303 msg
->u
.tx_can
.msg
[1] = (cf
->can_id
>> 18) & 0x3f;
1304 msg
->u
.tx_can
.msg
[2] = (cf
->can_id
>> 14) & 0x0f;
1305 msg
->u
.tx_can
.msg
[3] = (cf
->can_id
>> 6) & 0xff;
1306 msg
->u
.tx_can
.msg
[4] = cf
->can_id
& 0x3f;
1308 msg
->id
= CMD_TX_STD_MESSAGE
;
1309 msg
->u
.tx_can
.msg
[0] = (cf
->can_id
>> 6) & 0x1f;
1310 msg
->u
.tx_can
.msg
[1] = cf
->can_id
& 0x3f;
1313 msg
->u
.tx_can
.msg
[5] = cf
->can_dlc
;
1314 memcpy(&msg
->u
.tx_can
.msg
[6], cf
->data
, cf
->can_dlc
);
1316 if (cf
->can_id
& CAN_RTR_FLAG
)
1317 msg
->u
.tx_can
.flags
|= MSG_FLAG_REMOTE_FRAME
;
1319 for (i
= 0; i
< ARRAY_SIZE(priv
->tx_contexts
); i
++) {
1320 if (priv
->tx_contexts
[i
].echo_index
== MAX_TX_URBS
) {
1321 context
= &priv
->tx_contexts
[i
];
1327 netdev_warn(netdev
, "cannot find free context\n");
1328 ret
= NETDEV_TX_BUSY
;
1332 context
->priv
= priv
;
1333 context
->echo_index
= i
;
1334 context
->dlc
= cf
->can_dlc
;
1336 msg
->u
.tx_can
.tid
= context
->echo_index
;
1338 usb_fill_bulk_urb(urb
, dev
->udev
,
1339 usb_sndbulkpipe(dev
->udev
,
1340 dev
->bulk_out
->bEndpointAddress
),
1342 kvaser_usb_write_bulk_callback
, context
);
1343 usb_anchor_urb(urb
, &priv
->tx_submitted
);
1345 can_put_echo_skb(skb
, netdev
, context
->echo_index
);
1347 atomic_inc(&priv
->active_tx_urbs
);
1349 if (atomic_read(&priv
->active_tx_urbs
) >= MAX_TX_URBS
)
1350 netif_stop_queue(netdev
);
1352 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
1353 if (unlikely(err
)) {
1354 can_free_echo_skb(netdev
, context
->echo_index
);
1356 skb
= NULL
; /* set to NULL to avoid double free in
1357 * dev_kfree_skb(skb) */
1359 atomic_dec(&priv
->active_tx_urbs
);
1360 usb_unanchor_urb(urb
);
1362 stats
->tx_dropped
++;
1365 netif_device_detach(netdev
);
1367 netdev_warn(netdev
, "Failed tx_urb %d\n", err
);
1374 return NETDEV_TX_OK
;
1385 static const struct net_device_ops kvaser_usb_netdev_ops
= {
1386 .ndo_open
= kvaser_usb_open
,
1387 .ndo_stop
= kvaser_usb_close
,
1388 .ndo_start_xmit
= kvaser_usb_start_xmit
,
1391 static const struct can_bittiming_const kvaser_usb_bittiming_const
= {
1392 .name
= "kvaser_usb",
1393 .tseg1_min
= KVASER_USB_TSEG1_MIN
,
1394 .tseg1_max
= KVASER_USB_TSEG1_MAX
,
1395 .tseg2_min
= KVASER_USB_TSEG2_MIN
,
1396 .tseg2_max
= KVASER_USB_TSEG2_MAX
,
1397 .sjw_max
= KVASER_USB_SJW_MAX
,
1398 .brp_min
= KVASER_USB_BRP_MIN
,
1399 .brp_max
= KVASER_USB_BRP_MAX
,
1400 .brp_inc
= KVASER_USB_BRP_INC
,
1403 static int kvaser_usb_set_bittiming(struct net_device
*netdev
)
1405 struct kvaser_usb_net_priv
*priv
= netdev_priv(netdev
);
1406 struct can_bittiming
*bt
= &priv
->can
.bittiming
;
1407 struct kvaser_usb
*dev
= priv
->dev
;
1408 struct kvaser_msg
*msg
;
1411 msg
= kmalloc(sizeof(*msg
), GFP_KERNEL
);
1415 msg
->id
= CMD_SET_BUS_PARAMS
;
1416 msg
->len
= MSG_HEADER_LEN
+ sizeof(struct kvaser_msg_busparams
);
1417 msg
->u
.busparams
.channel
= priv
->channel
;
1418 msg
->u
.busparams
.tid
= 0xff;
1419 msg
->u
.busparams
.bitrate
= cpu_to_le32(bt
->bitrate
);
1420 msg
->u
.busparams
.sjw
= bt
->sjw
;
1421 msg
->u
.busparams
.tseg1
= bt
->prop_seg
+ bt
->phase_seg1
;
1422 msg
->u
.busparams
.tseg2
= bt
->phase_seg2
;
1424 if (priv
->can
.ctrlmode
& CAN_CTRLMODE_3_SAMPLES
)
1425 msg
->u
.busparams
.no_samp
= 3;
1427 msg
->u
.busparams
.no_samp
= 1;
1429 rc
= kvaser_usb_send_msg(dev
, msg
);
1435 static int kvaser_usb_set_mode(struct net_device
*netdev
,
1438 struct kvaser_usb_net_priv
*priv
= netdev_priv(netdev
);
1442 case CAN_MODE_START
:
1443 err
= kvaser_usb_simple_msg_async(priv
, CMD_START_CHIP
);
1454 static int kvaser_usb_get_berr_counter(const struct net_device
*netdev
,
1455 struct can_berr_counter
*bec
)
1457 struct kvaser_usb_net_priv
*priv
= netdev_priv(netdev
);
1464 static void kvaser_usb_remove_interfaces(struct kvaser_usb
*dev
)
1468 for (i
= 0; i
< dev
->nchannels
; i
++) {
1472 unregister_netdev(dev
->nets
[i
]->netdev
);
1475 kvaser_usb_unlink_all_urbs(dev
);
1477 for (i
= 0; i
< dev
->nchannels
; i
++) {
1481 free_candev(dev
->nets
[i
]->netdev
);
1485 static int kvaser_usb_init_one(struct usb_interface
*intf
,
1486 const struct usb_device_id
*id
, int channel
)
1488 struct kvaser_usb
*dev
= usb_get_intfdata(intf
);
1489 struct net_device
*netdev
;
1490 struct kvaser_usb_net_priv
*priv
;
1493 netdev
= alloc_candev(sizeof(*priv
), MAX_TX_URBS
);
1495 dev_err(&intf
->dev
, "Cannot alloc candev\n");
1499 priv
= netdev_priv(netdev
);
1501 init_completion(&priv
->start_comp
);
1502 init_completion(&priv
->stop_comp
);
1504 init_usb_anchor(&priv
->tx_submitted
);
1505 atomic_set(&priv
->active_tx_urbs
, 0);
1507 for (i
= 0; i
< ARRAY_SIZE(priv
->tx_contexts
); i
++)
1508 priv
->tx_contexts
[i
].echo_index
= MAX_TX_URBS
;
1511 priv
->netdev
= netdev
;
1512 priv
->channel
= channel
;
1514 priv
->can
.state
= CAN_STATE_STOPPED
;
1515 priv
->can
.clock
.freq
= CAN_USB_CLOCK
;
1516 priv
->can
.bittiming_const
= &kvaser_usb_bittiming_const
;
1517 priv
->can
.do_set_bittiming
= kvaser_usb_set_bittiming
;
1518 priv
->can
.do_set_mode
= kvaser_usb_set_mode
;
1519 if (id
->driver_info
& KVASER_HAS_TXRX_ERRORS
)
1520 priv
->can
.do_get_berr_counter
= kvaser_usb_get_berr_counter
;
1521 priv
->can
.ctrlmode_supported
= CAN_CTRLMODE_3_SAMPLES
;
1522 if (id
->driver_info
& KVASER_HAS_SILENT_MODE
)
1523 priv
->can
.ctrlmode_supported
|= CAN_CTRLMODE_LISTENONLY
;
1525 netdev
->flags
|= IFF_ECHO
;
1527 netdev
->netdev_ops
= &kvaser_usb_netdev_ops
;
1529 SET_NETDEV_DEV(netdev
, &intf
->dev
);
1531 dev
->nets
[channel
] = priv
;
1533 err
= register_candev(netdev
);
1535 dev_err(&intf
->dev
, "Failed to register can device\n");
1536 free_candev(netdev
);
1537 dev
->nets
[channel
] = NULL
;
1541 netdev_dbg(netdev
, "device registered\n");
1546 static int kvaser_usb_get_endpoints(const struct usb_interface
*intf
,
1547 struct usb_endpoint_descriptor
**in
,
1548 struct usb_endpoint_descriptor
**out
)
1550 const struct usb_host_interface
*iface_desc
;
1551 struct usb_endpoint_descriptor
*endpoint
;
1554 iface_desc
= &intf
->altsetting
[0];
1556 for (i
= 0; i
< iface_desc
->desc
.bNumEndpoints
; ++i
) {
1557 endpoint
= &iface_desc
->endpoint
[i
].desc
;
1559 if (!*in
&& usb_endpoint_is_bulk_in(endpoint
))
1562 if (!*out
&& usb_endpoint_is_bulk_out(endpoint
))
1565 /* use first bulk endpoint for in and out */
1573 static int kvaser_usb_probe(struct usb_interface
*intf
,
1574 const struct usb_device_id
*id
)
1576 struct kvaser_usb
*dev
;
1580 dev
= devm_kzalloc(&intf
->dev
, sizeof(*dev
), GFP_KERNEL
);
1584 err
= kvaser_usb_get_endpoints(intf
, &dev
->bulk_in
, &dev
->bulk_out
);
1586 dev_err(&intf
->dev
, "Cannot get usb endpoint(s)");
1590 dev
->udev
= interface_to_usbdev(intf
);
1592 init_usb_anchor(&dev
->rx_submitted
);
1594 usb_set_intfdata(intf
, dev
);
1596 for (i
= 0; i
< MAX_NET_DEVICES
; i
++)
1597 kvaser_usb_send_simple_msg(dev
, CMD_RESET_CHIP
, i
);
1599 err
= kvaser_usb_get_software_info(dev
);
1602 "Cannot get software infos, error %d\n", err
);
1606 err
= kvaser_usb_get_card_info(dev
);
1609 "Cannot get card infos, error %d\n", err
);
1613 dev_dbg(&intf
->dev
, "Firmware version: %d.%d.%d\n",
1614 ((dev
->fw_version
>> 24) & 0xff),
1615 ((dev
->fw_version
>> 16) & 0xff),
1616 (dev
->fw_version
& 0xffff));
1618 for (i
= 0; i
< dev
->nchannels
; i
++) {
1619 err
= kvaser_usb_init_one(intf
, id
, i
);
1621 kvaser_usb_remove_interfaces(dev
);
1629 static void kvaser_usb_disconnect(struct usb_interface
*intf
)
1631 struct kvaser_usb
*dev
= usb_get_intfdata(intf
);
1633 usb_set_intfdata(intf
, NULL
);
1638 kvaser_usb_remove_interfaces(dev
);
1641 static struct usb_driver kvaser_usb_driver
= {
1642 .name
= "kvaser_usb",
1643 .probe
= kvaser_usb_probe
,
1644 .disconnect
= kvaser_usb_disconnect
,
1645 .id_table
= kvaser_usb_table
,
1648 module_usb_driver(kvaser_usb_driver
);
1650 MODULE_AUTHOR("Olivier Sobrie <olivier@sobrie.be>");
1651 MODULE_DESCRIPTION("CAN driver for Kvaser CAN/USB devices");
1652 MODULE_LICENSE("GPL v2");