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/init.h>
16 #include <linux/completion.h>
17 #include <linux/module.h>
18 #include <linux/netdevice.h>
19 #include <linux/usb.h>
21 #include <linux/can.h>
22 #include <linux/can/dev.h>
23 #include <linux/can/error.h>
25 #define MAX_TX_URBS 16
27 #define START_TIMEOUT 1000 /* msecs */
28 #define STOP_TIMEOUT 1000 /* msecs */
29 #define USB_SEND_TIMEOUT 1000 /* msecs */
30 #define USB_RECV_TIMEOUT 1000 /* msecs */
31 #define RX_BUFFER_SIZE 3072
32 #define CAN_USB_CLOCK 8000000
33 #define MAX_NET_DEVICES 3
35 /* Kvaser USB devices */
36 #define KVASER_VENDOR_ID 0x0bfd
37 #define USB_LEAF_DEVEL_PRODUCT_ID 10
38 #define USB_LEAF_LITE_PRODUCT_ID 11
39 #define USB_LEAF_PRO_PRODUCT_ID 12
40 #define USB_LEAF_SPRO_PRODUCT_ID 14
41 #define USB_LEAF_PRO_LS_PRODUCT_ID 15
42 #define USB_LEAF_PRO_SWC_PRODUCT_ID 16
43 #define USB_LEAF_PRO_LIN_PRODUCT_ID 17
44 #define USB_LEAF_SPRO_LS_PRODUCT_ID 18
45 #define USB_LEAF_SPRO_SWC_PRODUCT_ID 19
46 #define USB_MEMO2_DEVEL_PRODUCT_ID 22
47 #define USB_MEMO2_HSHS_PRODUCT_ID 23
48 #define USB_UPRO_HSHS_PRODUCT_ID 24
49 #define USB_LEAF_LITE_GI_PRODUCT_ID 25
50 #define USB_LEAF_PRO_OBDII_PRODUCT_ID 26
51 #define USB_MEMO2_HSLS_PRODUCT_ID 27
52 #define USB_LEAF_LITE_CH_PRODUCT_ID 28
53 #define USB_BLACKBIRD_SPRO_PRODUCT_ID 29
54 #define USB_OEM_MERCURY_PRODUCT_ID 34
55 #define USB_OEM_LEAF_PRODUCT_ID 35
56 #define USB_CAN_R_PRODUCT_ID 39
58 /* USB devices features */
59 #define KVASER_HAS_SILENT_MODE BIT(0)
60 #define KVASER_HAS_TXRX_ERRORS BIT(1)
62 /* Message header size */
63 #define MSG_HEADER_LEN 2
65 /* Can message flags */
66 #define MSG_FLAG_ERROR_FRAME BIT(0)
67 #define MSG_FLAG_OVERRUN BIT(1)
68 #define MSG_FLAG_NERR BIT(2)
69 #define MSG_FLAG_WAKEUP BIT(3)
70 #define MSG_FLAG_REMOTE_FRAME BIT(4)
71 #define MSG_FLAG_RESERVED BIT(5)
72 #define MSG_FLAG_TX_ACK BIT(6)
73 #define MSG_FLAG_TX_REQUEST BIT(7)
76 #define M16C_STATE_BUS_RESET BIT(0)
77 #define M16C_STATE_BUS_ERROR BIT(4)
78 #define M16C_STATE_BUS_PASSIVE BIT(5)
79 #define M16C_STATE_BUS_OFF BIT(6)
82 #define CMD_RX_STD_MESSAGE 12
83 #define CMD_TX_STD_MESSAGE 13
84 #define CMD_RX_EXT_MESSAGE 14
85 #define CMD_TX_EXT_MESSAGE 15
86 #define CMD_SET_BUS_PARAMS 16
87 #define CMD_GET_BUS_PARAMS 17
88 #define CMD_GET_BUS_PARAMS_REPLY 18
89 #define CMD_GET_CHIP_STATE 19
90 #define CMD_CHIP_STATE_EVENT 20
91 #define CMD_SET_CTRL_MODE 21
92 #define CMD_GET_CTRL_MODE 22
93 #define CMD_GET_CTRL_MODE_REPLY 23
94 #define CMD_RESET_CHIP 24
95 #define CMD_RESET_CARD 25
96 #define CMD_START_CHIP 26
97 #define CMD_START_CHIP_REPLY 27
98 #define CMD_STOP_CHIP 28
99 #define CMD_STOP_CHIP_REPLY 29
100 #define CMD_GET_CARD_INFO2 32
101 #define CMD_GET_CARD_INFO 34
102 #define CMD_GET_CARD_INFO_REPLY 35
103 #define CMD_GET_SOFTWARE_INFO 38
104 #define CMD_GET_SOFTWARE_INFO_REPLY 39
105 #define CMD_ERROR_EVENT 45
106 #define CMD_FLUSH_QUEUE 48
107 #define CMD_RESET_ERROR_COUNTER 49
108 #define CMD_TX_ACKNOWLEDGE 50
109 #define CMD_CAN_ERROR_EVENT 51
110 #define CMD_USB_THROTTLE 77
111 #define CMD_LOG_MESSAGE 106
114 #define M16C_EF_ACKE BIT(0)
115 #define M16C_EF_CRCE BIT(1)
116 #define M16C_EF_FORME BIT(2)
117 #define M16C_EF_STFE BIT(3)
118 #define M16C_EF_BITE0 BIT(4)
119 #define M16C_EF_BITE1 BIT(5)
120 #define M16C_EF_RCVE BIT(6)
121 #define M16C_EF_TRE BIT(7)
123 /* bittiming parameters */
124 #define KVASER_USB_TSEG1_MIN 1
125 #define KVASER_USB_TSEG1_MAX 16
126 #define KVASER_USB_TSEG2_MIN 1
127 #define KVASER_USB_TSEG2_MAX 8
128 #define KVASER_USB_SJW_MAX 4
129 #define KVASER_USB_BRP_MIN 1
130 #define KVASER_USB_BRP_MAX 64
131 #define KVASER_USB_BRP_INC 1
134 #define KVASER_CTRL_MODE_NORMAL 1
135 #define KVASER_CTRL_MODE_SILENT 2
136 #define KVASER_CTRL_MODE_SELFRECEPTION 3
137 #define KVASER_CTRL_MODE_OFF 4
140 #define KVASER_EXTENDED_FRAME BIT(31)
142 struct kvaser_msg_simple
{
147 struct kvaser_msg_cardinfo
{
150 __le32 serial_number
;
152 __le32 clock_resolution
;
160 struct kvaser_msg_cardinfo2
{
164 __le32 oem_unlock_code
;
167 struct kvaser_msg_softinfo
{
172 __le16 max_outstanding_tx
;
176 struct kvaser_msg_busparams
{
186 struct kvaser_msg_tx_can
{
194 struct kvaser_msg_rx_can
{
201 struct kvaser_msg_chip_state_event
{
211 struct kvaser_msg_tx_acknowledge
{
219 struct kvaser_msg_error_event
{
231 struct kvaser_msg_ctrl_mode
{
238 struct kvaser_msg_flush_queue
{
245 struct kvaser_msg_log_message
{
259 struct kvaser_msg_simple simple
;
260 struct kvaser_msg_cardinfo cardinfo
;
261 struct kvaser_msg_cardinfo2 cardinfo2
;
262 struct kvaser_msg_softinfo softinfo
;
263 struct kvaser_msg_busparams busparams
;
264 struct kvaser_msg_tx_can tx_can
;
265 struct kvaser_msg_rx_can rx_can
;
266 struct kvaser_msg_chip_state_event chip_state_event
;
267 struct kvaser_msg_tx_acknowledge tx_acknowledge
;
268 struct kvaser_msg_error_event error_event
;
269 struct kvaser_msg_ctrl_mode ctrl_mode
;
270 struct kvaser_msg_flush_queue flush_queue
;
271 struct kvaser_msg_log_message log_message
;
275 struct kvaser_usb_tx_urb_context
{
276 struct kvaser_usb_net_priv
*priv
;
282 struct usb_device
*udev
;
283 struct kvaser_usb_net_priv
*nets
[MAX_NET_DEVICES
];
285 struct usb_endpoint_descriptor
*bulk_in
, *bulk_out
;
286 struct usb_anchor rx_submitted
;
289 unsigned int nchannels
;
292 void *rxbuf
[MAX_RX_URBS
];
293 dma_addr_t rxbuf_dma
[MAX_RX_URBS
];
296 struct kvaser_usb_net_priv
{
299 atomic_t active_tx_urbs
;
300 struct usb_anchor tx_submitted
;
301 struct kvaser_usb_tx_urb_context tx_contexts
[MAX_TX_URBS
];
303 struct completion start_comp
, stop_comp
;
305 struct kvaser_usb
*dev
;
306 struct net_device
*netdev
;
309 struct can_berr_counter bec
;
312 static const struct usb_device_id kvaser_usb_table
[] = {
313 { USB_DEVICE(KVASER_VENDOR_ID
, USB_LEAF_DEVEL_PRODUCT_ID
) },
314 { USB_DEVICE(KVASER_VENDOR_ID
, USB_LEAF_LITE_PRODUCT_ID
) },
315 { USB_DEVICE(KVASER_VENDOR_ID
, USB_LEAF_PRO_PRODUCT_ID
),
316 .driver_info
= KVASER_HAS_TXRX_ERRORS
|
317 KVASER_HAS_SILENT_MODE
},
318 { USB_DEVICE(KVASER_VENDOR_ID
, USB_LEAF_SPRO_PRODUCT_ID
),
319 .driver_info
= KVASER_HAS_TXRX_ERRORS
|
320 KVASER_HAS_SILENT_MODE
},
321 { USB_DEVICE(KVASER_VENDOR_ID
, USB_LEAF_PRO_LS_PRODUCT_ID
),
322 .driver_info
= KVASER_HAS_TXRX_ERRORS
|
323 KVASER_HAS_SILENT_MODE
},
324 { USB_DEVICE(KVASER_VENDOR_ID
, USB_LEAF_PRO_SWC_PRODUCT_ID
),
325 .driver_info
= KVASER_HAS_TXRX_ERRORS
|
326 KVASER_HAS_SILENT_MODE
},
327 { USB_DEVICE(KVASER_VENDOR_ID
, USB_LEAF_PRO_LIN_PRODUCT_ID
),
328 .driver_info
= KVASER_HAS_TXRX_ERRORS
|
329 KVASER_HAS_SILENT_MODE
},
330 { USB_DEVICE(KVASER_VENDOR_ID
, USB_LEAF_SPRO_LS_PRODUCT_ID
),
331 .driver_info
= KVASER_HAS_TXRX_ERRORS
|
332 KVASER_HAS_SILENT_MODE
},
333 { USB_DEVICE(KVASER_VENDOR_ID
, USB_LEAF_SPRO_SWC_PRODUCT_ID
),
334 .driver_info
= KVASER_HAS_TXRX_ERRORS
|
335 KVASER_HAS_SILENT_MODE
},
336 { USB_DEVICE(KVASER_VENDOR_ID
, USB_MEMO2_DEVEL_PRODUCT_ID
),
337 .driver_info
= KVASER_HAS_TXRX_ERRORS
|
338 KVASER_HAS_SILENT_MODE
},
339 { USB_DEVICE(KVASER_VENDOR_ID
, USB_MEMO2_HSHS_PRODUCT_ID
),
340 .driver_info
= KVASER_HAS_TXRX_ERRORS
|
341 KVASER_HAS_SILENT_MODE
},
342 { USB_DEVICE(KVASER_VENDOR_ID
, USB_UPRO_HSHS_PRODUCT_ID
),
343 .driver_info
= KVASER_HAS_TXRX_ERRORS
},
344 { USB_DEVICE(KVASER_VENDOR_ID
, USB_LEAF_LITE_GI_PRODUCT_ID
) },
345 { USB_DEVICE(KVASER_VENDOR_ID
, USB_LEAF_PRO_OBDII_PRODUCT_ID
),
346 .driver_info
= KVASER_HAS_TXRX_ERRORS
|
347 KVASER_HAS_SILENT_MODE
},
348 { USB_DEVICE(KVASER_VENDOR_ID
, USB_MEMO2_HSLS_PRODUCT_ID
),
349 .driver_info
= KVASER_HAS_TXRX_ERRORS
},
350 { USB_DEVICE(KVASER_VENDOR_ID
, USB_LEAF_LITE_CH_PRODUCT_ID
),
351 .driver_info
= KVASER_HAS_TXRX_ERRORS
},
352 { USB_DEVICE(KVASER_VENDOR_ID
, USB_BLACKBIRD_SPRO_PRODUCT_ID
),
353 .driver_info
= KVASER_HAS_TXRX_ERRORS
},
354 { USB_DEVICE(KVASER_VENDOR_ID
, USB_OEM_MERCURY_PRODUCT_ID
),
355 .driver_info
= KVASER_HAS_TXRX_ERRORS
},
356 { USB_DEVICE(KVASER_VENDOR_ID
, USB_OEM_LEAF_PRODUCT_ID
),
357 .driver_info
= KVASER_HAS_TXRX_ERRORS
},
358 { USB_DEVICE(KVASER_VENDOR_ID
, USB_CAN_R_PRODUCT_ID
),
359 .driver_info
= KVASER_HAS_TXRX_ERRORS
},
362 MODULE_DEVICE_TABLE(usb
, kvaser_usb_table
);
364 static inline int kvaser_usb_send_msg(const struct kvaser_usb
*dev
,
365 struct kvaser_msg
*msg
)
369 return usb_bulk_msg(dev
->udev
,
370 usb_sndbulkpipe(dev
->udev
,
371 dev
->bulk_out
->bEndpointAddress
),
372 msg
, msg
->len
, &actual_len
,
376 static int kvaser_usb_wait_msg(const struct kvaser_usb
*dev
, u8 id
,
377 struct kvaser_msg
*msg
)
379 struct kvaser_msg
*tmp
;
385 buf
= kzalloc(RX_BUFFER_SIZE
, GFP_KERNEL
);
389 err
= usb_bulk_msg(dev
->udev
,
390 usb_rcvbulkpipe(dev
->udev
,
391 dev
->bulk_in
->bEndpointAddress
),
392 buf
, RX_BUFFER_SIZE
, &actual_len
,
397 while (pos
<= actual_len
- MSG_HEADER_LEN
) {
403 if (pos
+ tmp
->len
> actual_len
) {
404 dev_err(dev
->udev
->dev
.parent
, "Format error\n");
409 memcpy(msg
, tmp
, tmp
->len
);
424 static int kvaser_usb_send_simple_msg(const struct kvaser_usb
*dev
,
425 u8 msg_id
, int channel
)
427 struct kvaser_msg
*msg
;
430 msg
= kmalloc(sizeof(*msg
), GFP_KERNEL
);
435 msg
->len
= MSG_HEADER_LEN
+ sizeof(struct kvaser_msg_simple
);
436 msg
->u
.simple
.channel
= channel
;
437 msg
->u
.simple
.tid
= 0xff;
439 rc
= kvaser_usb_send_msg(dev
, msg
);
445 static int kvaser_usb_get_software_info(struct kvaser_usb
*dev
)
447 struct kvaser_msg msg
;
450 err
= kvaser_usb_send_simple_msg(dev
, CMD_GET_SOFTWARE_INFO
, 0);
454 err
= kvaser_usb_wait_msg(dev
, CMD_GET_SOFTWARE_INFO_REPLY
, &msg
);
458 dev
->fw_version
= le32_to_cpu(msg
.u
.softinfo
.fw_version
);
463 static int kvaser_usb_get_card_info(struct kvaser_usb
*dev
)
465 struct kvaser_msg msg
;
468 err
= kvaser_usb_send_simple_msg(dev
, CMD_GET_CARD_INFO
, 0);
472 err
= kvaser_usb_wait_msg(dev
, CMD_GET_CARD_INFO_REPLY
, &msg
);
476 dev
->nchannels
= msg
.u
.cardinfo
.nchannels
;
481 static void kvaser_usb_tx_acknowledge(const struct kvaser_usb
*dev
,
482 const struct kvaser_msg
*msg
)
484 struct net_device_stats
*stats
;
485 struct kvaser_usb_tx_urb_context
*context
;
486 struct kvaser_usb_net_priv
*priv
;
488 struct can_frame
*cf
;
489 u8 channel
= msg
->u
.tx_acknowledge
.channel
;
490 u8 tid
= msg
->u
.tx_acknowledge
.tid
;
492 if (channel
>= dev
->nchannels
) {
493 dev_err(dev
->udev
->dev
.parent
,
494 "Invalid channel number (%d)\n", channel
);
498 priv
= dev
->nets
[channel
];
500 if (!netif_device_present(priv
->netdev
))
503 stats
= &priv
->netdev
->stats
;
505 context
= &priv
->tx_contexts
[tid
% MAX_TX_URBS
];
507 /* Sometimes the state change doesn't come after a bus-off event */
508 if (priv
->can
.restart_ms
&&
509 (priv
->can
.state
>= CAN_STATE_BUS_OFF
)) {
510 skb
= alloc_can_err_skb(priv
->netdev
, &cf
);
512 cf
->can_id
|= CAN_ERR_RESTARTED
;
516 stats
->rx_bytes
+= cf
->can_dlc
;
518 netdev_err(priv
->netdev
,
519 "No memory left for err_skb\n");
522 priv
->can
.can_stats
.restarts
++;
523 netif_carrier_on(priv
->netdev
);
525 priv
->can
.state
= CAN_STATE_ERROR_ACTIVE
;
529 stats
->tx_bytes
+= context
->dlc
;
530 can_get_echo_skb(priv
->netdev
, context
->echo_index
);
532 context
->echo_index
= MAX_TX_URBS
;
533 atomic_dec(&priv
->active_tx_urbs
);
535 netif_wake_queue(priv
->netdev
);
538 static void kvaser_usb_simple_msg_callback(struct urb
*urb
)
540 struct net_device
*netdev
= urb
->context
;
542 kfree(urb
->transfer_buffer
);
545 netdev_warn(netdev
, "urb status received: %d\n",
549 static int kvaser_usb_simple_msg_async(struct kvaser_usb_net_priv
*priv
,
552 struct kvaser_usb
*dev
= priv
->dev
;
553 struct net_device
*netdev
= priv
->netdev
;
554 struct kvaser_msg
*msg
;
559 urb
= usb_alloc_urb(0, GFP_ATOMIC
);
561 netdev_err(netdev
, "No memory left for URBs\n");
565 buf
= kmalloc(sizeof(struct kvaser_msg
), GFP_ATOMIC
);
571 msg
= (struct kvaser_msg
*)buf
;
572 msg
->len
= MSG_HEADER_LEN
+ sizeof(struct kvaser_msg_simple
);
574 msg
->u
.simple
.channel
= priv
->channel
;
576 usb_fill_bulk_urb(urb
, dev
->udev
,
577 usb_sndbulkpipe(dev
->udev
,
578 dev
->bulk_out
->bEndpointAddress
),
580 kvaser_usb_simple_msg_callback
, priv
);
581 usb_anchor_urb(urb
, &priv
->tx_submitted
);
583 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
585 netdev_err(netdev
, "Error transmitting URB\n");
586 usb_unanchor_urb(urb
);
597 static void kvaser_usb_unlink_tx_urbs(struct kvaser_usb_net_priv
*priv
)
601 usb_kill_anchored_urbs(&priv
->tx_submitted
);
602 atomic_set(&priv
->active_tx_urbs
, 0);
604 for (i
= 0; i
< MAX_TX_URBS
; i
++)
605 priv
->tx_contexts
[i
].echo_index
= MAX_TX_URBS
;
608 static void kvaser_usb_rx_error(const struct kvaser_usb
*dev
,
609 const struct kvaser_msg
*msg
)
611 struct can_frame
*cf
;
613 struct net_device_stats
*stats
;
614 struct kvaser_usb_net_priv
*priv
;
615 unsigned int new_state
;
616 u8 channel
, status
, txerr
, rxerr
, error_factor
;
619 case CMD_CAN_ERROR_EVENT
:
620 channel
= msg
->u
.error_event
.channel
;
621 status
= msg
->u
.error_event
.status
;
622 txerr
= msg
->u
.error_event
.tx_errors_count
;
623 rxerr
= msg
->u
.error_event
.rx_errors_count
;
624 error_factor
= msg
->u
.error_event
.error_factor
;
626 case CMD_LOG_MESSAGE
:
627 channel
= msg
->u
.log_message
.channel
;
628 status
= msg
->u
.log_message
.data
[0];
629 txerr
= msg
->u
.log_message
.data
[2];
630 rxerr
= msg
->u
.log_message
.data
[3];
631 error_factor
= msg
->u
.log_message
.data
[1];
633 case CMD_CHIP_STATE_EVENT
:
634 channel
= msg
->u
.chip_state_event
.channel
;
635 status
= msg
->u
.chip_state_event
.status
;
636 txerr
= msg
->u
.chip_state_event
.tx_errors_count
;
637 rxerr
= msg
->u
.chip_state_event
.rx_errors_count
;
641 dev_err(dev
->udev
->dev
.parent
, "Invalid msg id (%d)\n",
646 if (channel
>= dev
->nchannels
) {
647 dev_err(dev
->udev
->dev
.parent
,
648 "Invalid channel number (%d)\n", channel
);
652 priv
= dev
->nets
[channel
];
653 stats
= &priv
->netdev
->stats
;
655 if (status
& M16C_STATE_BUS_RESET
) {
656 kvaser_usb_unlink_tx_urbs(priv
);
660 skb
= alloc_can_err_skb(priv
->netdev
, &cf
);
666 new_state
= priv
->can
.state
;
668 netdev_dbg(priv
->netdev
, "Error status: 0x%02x\n", status
);
670 if (status
& M16C_STATE_BUS_OFF
) {
671 cf
->can_id
|= CAN_ERR_BUSOFF
;
673 priv
->can
.can_stats
.bus_off
++;
674 if (!priv
->can
.restart_ms
)
675 kvaser_usb_simple_msg_async(priv
, CMD_STOP_CHIP
);
677 netif_carrier_off(priv
->netdev
);
679 new_state
= CAN_STATE_BUS_OFF
;
680 } else if (status
& M16C_STATE_BUS_PASSIVE
) {
681 if (priv
->can
.state
!= CAN_STATE_ERROR_PASSIVE
) {
682 cf
->can_id
|= CAN_ERR_CRTL
;
685 cf
->data
[1] = (txerr
> rxerr
)
686 ? CAN_ERR_CRTL_TX_PASSIVE
687 : CAN_ERR_CRTL_RX_PASSIVE
;
689 cf
->data
[1] = CAN_ERR_CRTL_TX_PASSIVE
|
690 CAN_ERR_CRTL_RX_PASSIVE
;
692 priv
->can
.can_stats
.error_passive
++;
695 new_state
= CAN_STATE_ERROR_PASSIVE
;
698 if (status
== M16C_STATE_BUS_ERROR
) {
699 if ((priv
->can
.state
< CAN_STATE_ERROR_WARNING
) &&
700 ((txerr
>= 96) || (rxerr
>= 96))) {
701 cf
->can_id
|= CAN_ERR_CRTL
;
702 cf
->data
[1] = (txerr
> rxerr
)
703 ? CAN_ERR_CRTL_TX_WARNING
704 : CAN_ERR_CRTL_RX_WARNING
;
706 priv
->can
.can_stats
.error_warning
++;
707 new_state
= CAN_STATE_ERROR_WARNING
;
708 } else if (priv
->can
.state
> CAN_STATE_ERROR_ACTIVE
) {
709 cf
->can_id
|= CAN_ERR_PROT
;
710 cf
->data
[2] = CAN_ERR_PROT_ACTIVE
;
712 new_state
= CAN_STATE_ERROR_ACTIVE
;
717 cf
->can_id
|= CAN_ERR_PROT
;
718 cf
->data
[2] = CAN_ERR_PROT_ACTIVE
;
720 new_state
= CAN_STATE_ERROR_ACTIVE
;
723 if (priv
->can
.restart_ms
&&
724 (priv
->can
.state
>= CAN_STATE_BUS_OFF
) &&
725 (new_state
< CAN_STATE_BUS_OFF
)) {
726 cf
->can_id
|= CAN_ERR_RESTARTED
;
727 netif_carrier_on(priv
->netdev
);
729 priv
->can
.can_stats
.restarts
++;
733 priv
->can
.can_stats
.bus_error
++;
736 cf
->can_id
|= CAN_ERR_BUSERROR
| CAN_ERR_PROT
;
738 if (error_factor
& M16C_EF_ACKE
)
739 cf
->data
[3] |= (CAN_ERR_PROT_LOC_ACK
);
740 if (error_factor
& M16C_EF_CRCE
)
741 cf
->data
[3] |= (CAN_ERR_PROT_LOC_CRC_SEQ
|
742 CAN_ERR_PROT_LOC_CRC_DEL
);
743 if (error_factor
& M16C_EF_FORME
)
744 cf
->data
[2] |= CAN_ERR_PROT_FORM
;
745 if (error_factor
& M16C_EF_STFE
)
746 cf
->data
[2] |= CAN_ERR_PROT_STUFF
;
747 if (error_factor
& M16C_EF_BITE0
)
748 cf
->data
[2] |= CAN_ERR_PROT_BIT0
;
749 if (error_factor
& M16C_EF_BITE1
)
750 cf
->data
[2] |= CAN_ERR_PROT_BIT1
;
751 if (error_factor
& M16C_EF_TRE
)
752 cf
->data
[2] |= CAN_ERR_PROT_TX
;
758 priv
->bec
.txerr
= txerr
;
759 priv
->bec
.rxerr
= rxerr
;
761 priv
->can
.state
= new_state
;
766 stats
->rx_bytes
+= cf
->can_dlc
;
769 static void kvaser_usb_rx_can_err(const struct kvaser_usb_net_priv
*priv
,
770 const struct kvaser_msg
*msg
)
772 struct can_frame
*cf
;
774 struct net_device_stats
*stats
= &priv
->netdev
->stats
;
776 if (msg
->u
.rx_can
.flag
& (MSG_FLAG_ERROR_FRAME
|
778 netdev_err(priv
->netdev
, "Unknow error (flags: 0x%02x)\n",
785 if (msg
->u
.rx_can
.flag
& MSG_FLAG_OVERRUN
) {
786 skb
= alloc_can_err_skb(priv
->netdev
, &cf
);
792 cf
->can_id
|= CAN_ERR_CRTL
;
793 cf
->data
[1] = CAN_ERR_CRTL_RX_OVERFLOW
;
795 stats
->rx_over_errors
++;
801 stats
->rx_bytes
+= cf
->can_dlc
;
805 static void kvaser_usb_rx_can_msg(const struct kvaser_usb
*dev
,
806 const struct kvaser_msg
*msg
)
808 struct kvaser_usb_net_priv
*priv
;
809 struct can_frame
*cf
;
811 struct net_device_stats
*stats
;
812 u8 channel
= msg
->u
.rx_can
.channel
;
814 if (channel
>= dev
->nchannels
) {
815 dev_err(dev
->udev
->dev
.parent
,
816 "Invalid channel number (%d)\n", channel
);
820 priv
= dev
->nets
[channel
];
821 stats
= &priv
->netdev
->stats
;
823 if ((msg
->u
.rx_can
.flag
& MSG_FLAG_ERROR_FRAME
) &&
824 (msg
->id
== CMD_LOG_MESSAGE
)) {
825 kvaser_usb_rx_error(dev
, msg
);
827 } else if (msg
->u
.rx_can
.flag
& (MSG_FLAG_ERROR_FRAME
|
830 kvaser_usb_rx_can_err(priv
, msg
);
832 } else if (msg
->u
.rx_can
.flag
& ~MSG_FLAG_REMOTE_FRAME
) {
833 netdev_warn(priv
->netdev
,
834 "Unhandled frame (flags: 0x%02x)",
839 skb
= alloc_can_skb(priv
->netdev
, &cf
);
845 if (msg
->id
== CMD_LOG_MESSAGE
) {
846 cf
->can_id
= le32_to_cpu(msg
->u
.log_message
.id
);
847 if (cf
->can_id
& KVASER_EXTENDED_FRAME
)
848 cf
->can_id
&= CAN_EFF_MASK
| CAN_EFF_FLAG
;
850 cf
->can_id
&= CAN_SFF_MASK
;
852 cf
->can_dlc
= get_can_dlc(msg
->u
.log_message
.dlc
);
854 if (msg
->u
.log_message
.flags
& MSG_FLAG_REMOTE_FRAME
)
855 cf
->can_id
|= CAN_RTR_FLAG
;
857 memcpy(cf
->data
, &msg
->u
.log_message
.data
,
860 cf
->can_id
= ((msg
->u
.rx_can
.msg
[0] & 0x1f) << 6) |
861 (msg
->u
.rx_can
.msg
[1] & 0x3f);
863 if (msg
->id
== CMD_RX_EXT_MESSAGE
) {
865 cf
->can_id
|= ((msg
->u
.rx_can
.msg
[2] & 0x0f) << 14) |
866 ((msg
->u
.rx_can
.msg
[3] & 0xff) << 6) |
867 (msg
->u
.rx_can
.msg
[4] & 0x3f);
868 cf
->can_id
|= CAN_EFF_FLAG
;
871 cf
->can_dlc
= get_can_dlc(msg
->u
.rx_can
.msg
[5]);
873 if (msg
->u
.rx_can
.flag
& MSG_FLAG_REMOTE_FRAME
)
874 cf
->can_id
|= CAN_RTR_FLAG
;
876 memcpy(cf
->data
, &msg
->u
.rx_can
.msg
[6],
883 stats
->rx_bytes
+= cf
->can_dlc
;
886 static void kvaser_usb_start_chip_reply(const struct kvaser_usb
*dev
,
887 const struct kvaser_msg
*msg
)
889 struct kvaser_usb_net_priv
*priv
;
890 u8 channel
= msg
->u
.simple
.channel
;
892 if (channel
>= dev
->nchannels
) {
893 dev_err(dev
->udev
->dev
.parent
,
894 "Invalid channel number (%d)\n", channel
);
898 priv
= dev
->nets
[channel
];
900 if (completion_done(&priv
->start_comp
) &&
901 netif_queue_stopped(priv
->netdev
)) {
902 netif_wake_queue(priv
->netdev
);
904 netif_start_queue(priv
->netdev
);
905 complete(&priv
->start_comp
);
909 static void kvaser_usb_stop_chip_reply(const struct kvaser_usb
*dev
,
910 const struct kvaser_msg
*msg
)
912 struct kvaser_usb_net_priv
*priv
;
913 u8 channel
= msg
->u
.simple
.channel
;
915 if (channel
>= dev
->nchannels
) {
916 dev_err(dev
->udev
->dev
.parent
,
917 "Invalid channel number (%d)\n", channel
);
921 priv
= dev
->nets
[channel
];
923 complete(&priv
->stop_comp
);
926 static void kvaser_usb_handle_message(const struct kvaser_usb
*dev
,
927 const struct kvaser_msg
*msg
)
930 case CMD_START_CHIP_REPLY
:
931 kvaser_usb_start_chip_reply(dev
, msg
);
934 case CMD_STOP_CHIP_REPLY
:
935 kvaser_usb_stop_chip_reply(dev
, msg
);
938 case CMD_RX_STD_MESSAGE
:
939 case CMD_RX_EXT_MESSAGE
:
940 case CMD_LOG_MESSAGE
:
941 kvaser_usb_rx_can_msg(dev
, msg
);
944 case CMD_CHIP_STATE_EVENT
:
945 case CMD_CAN_ERROR_EVENT
:
946 kvaser_usb_rx_error(dev
, msg
);
949 case CMD_TX_ACKNOWLEDGE
:
950 kvaser_usb_tx_acknowledge(dev
, msg
);
954 dev_warn(dev
->udev
->dev
.parent
,
955 "Unhandled message (%d)\n", msg
->id
);
960 static void kvaser_usb_read_bulk_callback(struct urb
*urb
)
962 struct kvaser_usb
*dev
= urb
->context
;
963 struct kvaser_msg
*msg
;
967 switch (urb
->status
) {
974 dev_info(dev
->udev
->dev
.parent
, "Rx URB aborted (%d)\n",
979 while (pos
<= urb
->actual_length
- MSG_HEADER_LEN
) {
980 msg
= urb
->transfer_buffer
+ pos
;
985 if (pos
+ msg
->len
> urb
->actual_length
) {
986 dev_err(dev
->udev
->dev
.parent
, "Format error\n");
990 kvaser_usb_handle_message(dev
, msg
);
996 usb_fill_bulk_urb(urb
, dev
->udev
,
997 usb_rcvbulkpipe(dev
->udev
,
998 dev
->bulk_in
->bEndpointAddress
),
999 urb
->transfer_buffer
, RX_BUFFER_SIZE
,
1000 kvaser_usb_read_bulk_callback
, dev
);
1002 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
1003 if (err
== -ENODEV
) {
1004 for (i
= 0; i
< dev
->nchannels
; i
++) {
1008 netif_device_detach(dev
->nets
[i
]->netdev
);
1011 dev_err(dev
->udev
->dev
.parent
,
1012 "Failed resubmitting read bulk urb: %d\n", err
);
1018 static int kvaser_usb_setup_rx_urbs(struct kvaser_usb
*dev
)
1022 if (dev
->rxinitdone
)
1025 for (i
= 0; i
< MAX_RX_URBS
; i
++) {
1026 struct urb
*urb
= NULL
;
1030 urb
= usb_alloc_urb(0, GFP_KERNEL
);
1032 dev_warn(dev
->udev
->dev
.parent
,
1033 "No memory left for URBs\n");
1038 buf
= usb_alloc_coherent(dev
->udev
, RX_BUFFER_SIZE
,
1039 GFP_KERNEL
, &buf_dma
);
1041 dev_warn(dev
->udev
->dev
.parent
,
1042 "No memory left for USB buffer\n");
1048 usb_fill_bulk_urb(urb
, dev
->udev
,
1049 usb_rcvbulkpipe(dev
->udev
,
1050 dev
->bulk_in
->bEndpointAddress
),
1051 buf
, RX_BUFFER_SIZE
,
1052 kvaser_usb_read_bulk_callback
,
1054 urb
->transfer_dma
= buf_dma
;
1055 urb
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
1056 usb_anchor_urb(urb
, &dev
->rx_submitted
);
1058 err
= usb_submit_urb(urb
, GFP_KERNEL
);
1060 usb_unanchor_urb(urb
);
1061 usb_free_coherent(dev
->udev
, RX_BUFFER_SIZE
, buf
,
1067 dev
->rxbuf
[i
] = buf
;
1068 dev
->rxbuf_dma
[i
] = buf_dma
;
1074 dev_warn(dev
->udev
->dev
.parent
,
1075 "Cannot setup read URBs, error %d\n", err
);
1077 } else if (i
< MAX_RX_URBS
) {
1078 dev_warn(dev
->udev
->dev
.parent
,
1079 "RX performances may be slow\n");
1082 dev
->rxinitdone
= true;
1087 static int kvaser_usb_set_opt_mode(const struct kvaser_usb_net_priv
*priv
)
1089 struct kvaser_msg
*msg
;
1092 msg
= kmalloc(sizeof(*msg
), GFP_KERNEL
);
1096 msg
->id
= CMD_SET_CTRL_MODE
;
1097 msg
->len
= MSG_HEADER_LEN
+ sizeof(struct kvaser_msg_ctrl_mode
);
1098 msg
->u
.ctrl_mode
.tid
= 0xff;
1099 msg
->u
.ctrl_mode
.channel
= priv
->channel
;
1101 if (priv
->can
.ctrlmode
& CAN_CTRLMODE_LISTENONLY
)
1102 msg
->u
.ctrl_mode
.ctrl_mode
= KVASER_CTRL_MODE_SILENT
;
1104 msg
->u
.ctrl_mode
.ctrl_mode
= KVASER_CTRL_MODE_NORMAL
;
1106 rc
= kvaser_usb_send_msg(priv
->dev
, msg
);
1112 static int kvaser_usb_start_chip(struct kvaser_usb_net_priv
*priv
)
1116 init_completion(&priv
->start_comp
);
1118 err
= kvaser_usb_send_simple_msg(priv
->dev
, CMD_START_CHIP
,
1123 if (!wait_for_completion_timeout(&priv
->start_comp
,
1124 msecs_to_jiffies(START_TIMEOUT
)))
1130 static int kvaser_usb_open(struct net_device
*netdev
)
1132 struct kvaser_usb_net_priv
*priv
= netdev_priv(netdev
);
1133 struct kvaser_usb
*dev
= priv
->dev
;
1136 err
= open_candev(netdev
);
1140 err
= kvaser_usb_setup_rx_urbs(dev
);
1144 err
= kvaser_usb_set_opt_mode(priv
);
1148 err
= kvaser_usb_start_chip(priv
);
1150 netdev_warn(netdev
, "Cannot start device, error %d\n", err
);
1154 priv
->can
.state
= CAN_STATE_ERROR_ACTIVE
;
1159 close_candev(netdev
);
1163 static void kvaser_usb_unlink_all_urbs(struct kvaser_usb
*dev
)
1167 usb_kill_anchored_urbs(&dev
->rx_submitted
);
1169 for (i
= 0; i
< MAX_RX_URBS
; i
++)
1170 usb_free_coherent(dev
->udev
, RX_BUFFER_SIZE
,
1174 for (i
= 0; i
< MAX_NET_DEVICES
; i
++) {
1175 struct kvaser_usb_net_priv
*priv
= dev
->nets
[i
];
1178 kvaser_usb_unlink_tx_urbs(priv
);
1182 static int kvaser_usb_stop_chip(struct kvaser_usb_net_priv
*priv
)
1186 init_completion(&priv
->stop_comp
);
1188 err
= kvaser_usb_send_simple_msg(priv
->dev
, CMD_STOP_CHIP
,
1193 if (!wait_for_completion_timeout(&priv
->stop_comp
,
1194 msecs_to_jiffies(STOP_TIMEOUT
)))
1200 static int kvaser_usb_flush_queue(struct kvaser_usb_net_priv
*priv
)
1202 struct kvaser_msg
*msg
;
1205 msg
= kmalloc(sizeof(*msg
), GFP_KERNEL
);
1209 msg
->id
= CMD_FLUSH_QUEUE
;
1210 msg
->len
= MSG_HEADER_LEN
+ sizeof(struct kvaser_msg_flush_queue
);
1211 msg
->u
.flush_queue
.channel
= priv
->channel
;
1212 msg
->u
.flush_queue
.flags
= 0x00;
1214 rc
= kvaser_usb_send_msg(priv
->dev
, msg
);
1220 static int kvaser_usb_close(struct net_device
*netdev
)
1222 struct kvaser_usb_net_priv
*priv
= netdev_priv(netdev
);
1223 struct kvaser_usb
*dev
= priv
->dev
;
1226 netif_stop_queue(netdev
);
1228 err
= kvaser_usb_flush_queue(priv
);
1230 netdev_warn(netdev
, "Cannot flush queue, error %d\n", err
);
1232 if (kvaser_usb_send_simple_msg(dev
, CMD_RESET_CHIP
, priv
->channel
))
1233 netdev_warn(netdev
, "Cannot reset card, error %d\n", err
);
1235 err
= kvaser_usb_stop_chip(priv
);
1237 netdev_warn(netdev
, "Cannot stop device, error %d\n", err
);
1239 priv
->can
.state
= CAN_STATE_STOPPED
;
1240 close_candev(priv
->netdev
);
1245 static void kvaser_usb_write_bulk_callback(struct urb
*urb
)
1247 struct kvaser_usb_tx_urb_context
*context
= urb
->context
;
1248 struct kvaser_usb_net_priv
*priv
;
1249 struct net_device
*netdev
;
1251 if (WARN_ON(!context
))
1254 priv
= context
->priv
;
1255 netdev
= priv
->netdev
;
1257 kfree(urb
->transfer_buffer
);
1259 if (!netif_device_present(netdev
))
1263 netdev_info(netdev
, "Tx URB aborted (%d)\n", urb
->status
);
1266 static netdev_tx_t
kvaser_usb_start_xmit(struct sk_buff
*skb
,
1267 struct net_device
*netdev
)
1269 struct kvaser_usb_net_priv
*priv
= netdev_priv(netdev
);
1270 struct kvaser_usb
*dev
= priv
->dev
;
1271 struct net_device_stats
*stats
= &netdev
->stats
;
1272 struct can_frame
*cf
= (struct can_frame
*)skb
->data
;
1273 struct kvaser_usb_tx_urb_context
*context
= NULL
;
1276 struct kvaser_msg
*msg
;
1278 int ret
= NETDEV_TX_OK
;
1280 if (can_dropped_invalid_skb(netdev
, skb
))
1281 return NETDEV_TX_OK
;
1283 urb
= usb_alloc_urb(0, GFP_ATOMIC
);
1285 netdev_err(netdev
, "No memory left for URBs\n");
1286 stats
->tx_dropped
++;
1290 buf
= kmalloc(sizeof(struct kvaser_msg
), GFP_ATOMIC
);
1292 stats
->tx_dropped
++;
1297 msg
->len
= MSG_HEADER_LEN
+ sizeof(struct kvaser_msg_tx_can
);
1298 msg
->u
.tx_can
.flags
= 0;
1299 msg
->u
.tx_can
.channel
= priv
->channel
;
1301 if (cf
->can_id
& CAN_EFF_FLAG
) {
1302 msg
->id
= CMD_TX_EXT_MESSAGE
;
1303 msg
->u
.tx_can
.msg
[0] = (cf
->can_id
>> 24) & 0x1f;
1304 msg
->u
.tx_can
.msg
[1] = (cf
->can_id
>> 18) & 0x3f;
1305 msg
->u
.tx_can
.msg
[2] = (cf
->can_id
>> 14) & 0x0f;
1306 msg
->u
.tx_can
.msg
[3] = (cf
->can_id
>> 6) & 0xff;
1307 msg
->u
.tx_can
.msg
[4] = cf
->can_id
& 0x3f;
1309 msg
->id
= CMD_TX_STD_MESSAGE
;
1310 msg
->u
.tx_can
.msg
[0] = (cf
->can_id
>> 6) & 0x1f;
1311 msg
->u
.tx_can
.msg
[1] = cf
->can_id
& 0x3f;
1314 msg
->u
.tx_can
.msg
[5] = cf
->can_dlc
;
1315 memcpy(&msg
->u
.tx_can
.msg
[6], cf
->data
, cf
->can_dlc
);
1317 if (cf
->can_id
& CAN_RTR_FLAG
)
1318 msg
->u
.tx_can
.flags
|= MSG_FLAG_REMOTE_FRAME
;
1320 for (i
= 0; i
< ARRAY_SIZE(priv
->tx_contexts
); i
++) {
1321 if (priv
->tx_contexts
[i
].echo_index
== MAX_TX_URBS
) {
1322 context
= &priv
->tx_contexts
[i
];
1328 netdev_warn(netdev
, "cannot find free context\n");
1329 ret
= NETDEV_TX_BUSY
;
1333 context
->priv
= priv
;
1334 context
->echo_index
= i
;
1335 context
->dlc
= cf
->can_dlc
;
1337 msg
->u
.tx_can
.tid
= context
->echo_index
;
1339 usb_fill_bulk_urb(urb
, dev
->udev
,
1340 usb_sndbulkpipe(dev
->udev
,
1341 dev
->bulk_out
->bEndpointAddress
),
1343 kvaser_usb_write_bulk_callback
, context
);
1344 usb_anchor_urb(urb
, &priv
->tx_submitted
);
1346 can_put_echo_skb(skb
, netdev
, context
->echo_index
);
1348 atomic_inc(&priv
->active_tx_urbs
);
1350 if (atomic_read(&priv
->active_tx_urbs
) >= MAX_TX_URBS
)
1351 netif_stop_queue(netdev
);
1353 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
1354 if (unlikely(err
)) {
1355 can_free_echo_skb(netdev
, context
->echo_index
);
1357 skb
= NULL
; /* set to NULL to avoid double free in
1358 * dev_kfree_skb(skb) */
1360 atomic_dec(&priv
->active_tx_urbs
);
1361 usb_unanchor_urb(urb
);
1363 stats
->tx_dropped
++;
1366 netif_device_detach(netdev
);
1368 netdev_warn(netdev
, "Failed tx_urb %d\n", err
);
1375 return NETDEV_TX_OK
;
1386 static const struct net_device_ops kvaser_usb_netdev_ops
= {
1387 .ndo_open
= kvaser_usb_open
,
1388 .ndo_stop
= kvaser_usb_close
,
1389 .ndo_start_xmit
= kvaser_usb_start_xmit
,
1392 static const struct can_bittiming_const kvaser_usb_bittiming_const
= {
1393 .name
= "kvaser_usb",
1394 .tseg1_min
= KVASER_USB_TSEG1_MIN
,
1395 .tseg1_max
= KVASER_USB_TSEG1_MAX
,
1396 .tseg2_min
= KVASER_USB_TSEG2_MIN
,
1397 .tseg2_max
= KVASER_USB_TSEG2_MAX
,
1398 .sjw_max
= KVASER_USB_SJW_MAX
,
1399 .brp_min
= KVASER_USB_BRP_MIN
,
1400 .brp_max
= KVASER_USB_BRP_MAX
,
1401 .brp_inc
= KVASER_USB_BRP_INC
,
1404 static int kvaser_usb_set_bittiming(struct net_device
*netdev
)
1406 struct kvaser_usb_net_priv
*priv
= netdev_priv(netdev
);
1407 struct can_bittiming
*bt
= &priv
->can
.bittiming
;
1408 struct kvaser_usb
*dev
= priv
->dev
;
1409 struct kvaser_msg
*msg
;
1412 msg
= kmalloc(sizeof(*msg
), GFP_KERNEL
);
1416 msg
->id
= CMD_SET_BUS_PARAMS
;
1417 msg
->len
= MSG_HEADER_LEN
+ sizeof(struct kvaser_msg_busparams
);
1418 msg
->u
.busparams
.channel
= priv
->channel
;
1419 msg
->u
.busparams
.tid
= 0xff;
1420 msg
->u
.busparams
.bitrate
= cpu_to_le32(bt
->bitrate
);
1421 msg
->u
.busparams
.sjw
= bt
->sjw
;
1422 msg
->u
.busparams
.tseg1
= bt
->prop_seg
+ bt
->phase_seg1
;
1423 msg
->u
.busparams
.tseg2
= bt
->phase_seg2
;
1425 if (priv
->can
.ctrlmode
& CAN_CTRLMODE_3_SAMPLES
)
1426 msg
->u
.busparams
.no_samp
= 3;
1428 msg
->u
.busparams
.no_samp
= 1;
1430 rc
= kvaser_usb_send_msg(dev
, msg
);
1436 static int kvaser_usb_set_mode(struct net_device
*netdev
,
1439 struct kvaser_usb_net_priv
*priv
= netdev_priv(netdev
);
1443 case CAN_MODE_START
:
1444 err
= kvaser_usb_simple_msg_async(priv
, CMD_START_CHIP
);
1455 static int kvaser_usb_get_berr_counter(const struct net_device
*netdev
,
1456 struct can_berr_counter
*bec
)
1458 struct kvaser_usb_net_priv
*priv
= netdev_priv(netdev
);
1465 static void kvaser_usb_remove_interfaces(struct kvaser_usb
*dev
)
1469 for (i
= 0; i
< dev
->nchannels
; i
++) {
1473 unregister_netdev(dev
->nets
[i
]->netdev
);
1476 kvaser_usb_unlink_all_urbs(dev
);
1478 for (i
= 0; i
< dev
->nchannels
; i
++) {
1482 free_candev(dev
->nets
[i
]->netdev
);
1486 static int kvaser_usb_init_one(struct usb_interface
*intf
,
1487 const struct usb_device_id
*id
, int channel
)
1489 struct kvaser_usb
*dev
= usb_get_intfdata(intf
);
1490 struct net_device
*netdev
;
1491 struct kvaser_usb_net_priv
*priv
;
1494 netdev
= alloc_candev(sizeof(*priv
), MAX_TX_URBS
);
1496 dev_err(&intf
->dev
, "Cannot alloc candev\n");
1500 priv
= netdev_priv(netdev
);
1502 init_completion(&priv
->start_comp
);
1503 init_completion(&priv
->stop_comp
);
1505 init_usb_anchor(&priv
->tx_submitted
);
1506 atomic_set(&priv
->active_tx_urbs
, 0);
1508 for (i
= 0; i
< ARRAY_SIZE(priv
->tx_contexts
); i
++)
1509 priv
->tx_contexts
[i
].echo_index
= MAX_TX_URBS
;
1512 priv
->netdev
= netdev
;
1513 priv
->channel
= channel
;
1515 priv
->can
.state
= CAN_STATE_STOPPED
;
1516 priv
->can
.clock
.freq
= CAN_USB_CLOCK
;
1517 priv
->can
.bittiming_const
= &kvaser_usb_bittiming_const
;
1518 priv
->can
.do_set_bittiming
= kvaser_usb_set_bittiming
;
1519 priv
->can
.do_set_mode
= kvaser_usb_set_mode
;
1520 if (id
->driver_info
& KVASER_HAS_TXRX_ERRORS
)
1521 priv
->can
.do_get_berr_counter
= kvaser_usb_get_berr_counter
;
1522 priv
->can
.ctrlmode_supported
= CAN_CTRLMODE_3_SAMPLES
;
1523 if (id
->driver_info
& KVASER_HAS_SILENT_MODE
)
1524 priv
->can
.ctrlmode_supported
|= CAN_CTRLMODE_LISTENONLY
;
1526 netdev
->flags
|= IFF_ECHO
;
1528 netdev
->netdev_ops
= &kvaser_usb_netdev_ops
;
1530 SET_NETDEV_DEV(netdev
, &intf
->dev
);
1532 dev
->nets
[channel
] = priv
;
1534 err
= register_candev(netdev
);
1536 dev_err(&intf
->dev
, "Failed to register can device\n");
1537 free_candev(netdev
);
1538 dev
->nets
[channel
] = NULL
;
1542 netdev_dbg(netdev
, "device registered\n");
1547 static void kvaser_usb_get_endpoints(const struct usb_interface
*intf
,
1548 struct usb_endpoint_descriptor
**in
,
1549 struct usb_endpoint_descriptor
**out
)
1551 const struct usb_host_interface
*iface_desc
;
1552 struct usb_endpoint_descriptor
*endpoint
;
1555 iface_desc
= &intf
->altsetting
[0];
1557 for (i
= 0; i
< iface_desc
->desc
.bNumEndpoints
; ++i
) {
1558 endpoint
= &iface_desc
->endpoint
[i
].desc
;
1560 if (usb_endpoint_is_bulk_in(endpoint
))
1563 if (usb_endpoint_is_bulk_out(endpoint
))
1568 static int kvaser_usb_probe(struct usb_interface
*intf
,
1569 const struct usb_device_id
*id
)
1571 struct kvaser_usb
*dev
;
1575 dev
= devm_kzalloc(&intf
->dev
, sizeof(*dev
), GFP_KERNEL
);
1579 kvaser_usb_get_endpoints(intf
, &dev
->bulk_in
, &dev
->bulk_out
);
1580 if (!dev
->bulk_in
|| !dev
->bulk_out
) {
1581 dev_err(&intf
->dev
, "Cannot get usb endpoint(s)");
1585 dev
->udev
= interface_to_usbdev(intf
);
1587 init_usb_anchor(&dev
->rx_submitted
);
1589 usb_set_intfdata(intf
, dev
);
1591 for (i
= 0; i
< MAX_NET_DEVICES
; i
++)
1592 kvaser_usb_send_simple_msg(dev
, CMD_RESET_CHIP
, i
);
1594 err
= kvaser_usb_get_software_info(dev
);
1597 "Cannot get software infos, error %d\n", err
);
1601 err
= kvaser_usb_get_card_info(dev
);
1604 "Cannot get card infos, error %d\n", err
);
1608 dev_dbg(&intf
->dev
, "Firmware version: %d.%d.%d\n",
1609 ((dev
->fw_version
>> 24) & 0xff),
1610 ((dev
->fw_version
>> 16) & 0xff),
1611 (dev
->fw_version
& 0xffff));
1613 for (i
= 0; i
< dev
->nchannels
; i
++) {
1614 err
= kvaser_usb_init_one(intf
, id
, i
);
1616 kvaser_usb_remove_interfaces(dev
);
1624 static void kvaser_usb_disconnect(struct usb_interface
*intf
)
1626 struct kvaser_usb
*dev
= usb_get_intfdata(intf
);
1628 usb_set_intfdata(intf
, NULL
);
1633 kvaser_usb_remove_interfaces(dev
);
1636 static struct usb_driver kvaser_usb_driver
= {
1637 .name
= "kvaser_usb",
1638 .probe
= kvaser_usb_probe
,
1639 .disconnect
= kvaser_usb_disconnect
,
1640 .id_table
= kvaser_usb_table
,
1643 module_usb_driver(kvaser_usb_driver
);
1645 MODULE_AUTHOR("Olivier Sobrie <olivier@sobrie.be>");
1646 MODULE_DESCRIPTION("CAN driver for Kvaser CAN/USB devices");
1647 MODULE_LICENSE("GPL v2");