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
56 #define USB_LEAF_LITE_V2_PRODUCT_ID 288
57 #define USB_MINI_PCIE_HS_PRODUCT_ID 289
59 /* USB devices features */
60 #define KVASER_HAS_SILENT_MODE BIT(0)
61 #define KVASER_HAS_TXRX_ERRORS BIT(1)
63 /* Message header size */
64 #define MSG_HEADER_LEN 2
66 /* Can message flags */
67 #define MSG_FLAG_ERROR_FRAME BIT(0)
68 #define MSG_FLAG_OVERRUN BIT(1)
69 #define MSG_FLAG_NERR BIT(2)
70 #define MSG_FLAG_WAKEUP BIT(3)
71 #define MSG_FLAG_REMOTE_FRAME BIT(4)
72 #define MSG_FLAG_RESERVED BIT(5)
73 #define MSG_FLAG_TX_ACK BIT(6)
74 #define MSG_FLAG_TX_REQUEST BIT(7)
77 #define M16C_STATE_BUS_RESET BIT(0)
78 #define M16C_STATE_BUS_ERROR BIT(4)
79 #define M16C_STATE_BUS_PASSIVE BIT(5)
80 #define M16C_STATE_BUS_OFF BIT(6)
83 #define CMD_RX_STD_MESSAGE 12
84 #define CMD_TX_STD_MESSAGE 13
85 #define CMD_RX_EXT_MESSAGE 14
86 #define CMD_TX_EXT_MESSAGE 15
87 #define CMD_SET_BUS_PARAMS 16
88 #define CMD_GET_BUS_PARAMS 17
89 #define CMD_GET_BUS_PARAMS_REPLY 18
90 #define CMD_GET_CHIP_STATE 19
91 #define CMD_CHIP_STATE_EVENT 20
92 #define CMD_SET_CTRL_MODE 21
93 #define CMD_GET_CTRL_MODE 22
94 #define CMD_GET_CTRL_MODE_REPLY 23
95 #define CMD_RESET_CHIP 24
96 #define CMD_RESET_CARD 25
97 #define CMD_START_CHIP 26
98 #define CMD_START_CHIP_REPLY 27
99 #define CMD_STOP_CHIP 28
100 #define CMD_STOP_CHIP_REPLY 29
101 #define CMD_GET_CARD_INFO2 32
102 #define CMD_GET_CARD_INFO 34
103 #define CMD_GET_CARD_INFO_REPLY 35
104 #define CMD_GET_SOFTWARE_INFO 38
105 #define CMD_GET_SOFTWARE_INFO_REPLY 39
106 #define CMD_ERROR_EVENT 45
107 #define CMD_FLUSH_QUEUE 48
108 #define CMD_RESET_ERROR_COUNTER 49
109 #define CMD_TX_ACKNOWLEDGE 50
110 #define CMD_CAN_ERROR_EVENT 51
111 #define CMD_USB_THROTTLE 77
112 #define CMD_LOG_MESSAGE 106
115 #define M16C_EF_ACKE BIT(0)
116 #define M16C_EF_CRCE BIT(1)
117 #define M16C_EF_FORME BIT(2)
118 #define M16C_EF_STFE BIT(3)
119 #define M16C_EF_BITE0 BIT(4)
120 #define M16C_EF_BITE1 BIT(5)
121 #define M16C_EF_RCVE BIT(6)
122 #define M16C_EF_TRE BIT(7)
124 /* bittiming parameters */
125 #define KVASER_USB_TSEG1_MIN 1
126 #define KVASER_USB_TSEG1_MAX 16
127 #define KVASER_USB_TSEG2_MIN 1
128 #define KVASER_USB_TSEG2_MAX 8
129 #define KVASER_USB_SJW_MAX 4
130 #define KVASER_USB_BRP_MIN 1
131 #define KVASER_USB_BRP_MAX 64
132 #define KVASER_USB_BRP_INC 1
135 #define KVASER_CTRL_MODE_NORMAL 1
136 #define KVASER_CTRL_MODE_SILENT 2
137 #define KVASER_CTRL_MODE_SELFRECEPTION 3
138 #define KVASER_CTRL_MODE_OFF 4
141 #define KVASER_EXTENDED_FRAME BIT(31)
143 struct kvaser_msg_simple
{
148 struct kvaser_msg_cardinfo
{
151 __le32 serial_number
;
153 __le32 clock_resolution
;
161 struct kvaser_msg_cardinfo2
{
165 __le32 oem_unlock_code
;
168 struct kvaser_msg_softinfo
{
173 __le16 max_outstanding_tx
;
177 struct kvaser_msg_busparams
{
187 struct kvaser_msg_tx_can
{
195 struct kvaser_msg_rx_can
{
202 struct kvaser_msg_chip_state_event
{
212 struct kvaser_msg_tx_acknowledge
{
220 struct kvaser_msg_error_event
{
232 struct kvaser_msg_ctrl_mode
{
239 struct kvaser_msg_flush_queue
{
246 struct kvaser_msg_log_message
{
260 struct kvaser_msg_simple simple
;
261 struct kvaser_msg_cardinfo cardinfo
;
262 struct kvaser_msg_cardinfo2 cardinfo2
;
263 struct kvaser_msg_softinfo softinfo
;
264 struct kvaser_msg_busparams busparams
;
265 struct kvaser_msg_tx_can tx_can
;
266 struct kvaser_msg_rx_can rx_can
;
267 struct kvaser_msg_chip_state_event chip_state_event
;
268 struct kvaser_msg_tx_acknowledge tx_acknowledge
;
269 struct kvaser_msg_error_event error_event
;
270 struct kvaser_msg_ctrl_mode ctrl_mode
;
271 struct kvaser_msg_flush_queue flush_queue
;
272 struct kvaser_msg_log_message log_message
;
276 struct kvaser_usb_tx_urb_context
{
277 struct kvaser_usb_net_priv
*priv
;
283 struct usb_device
*udev
;
284 struct kvaser_usb_net_priv
*nets
[MAX_NET_DEVICES
];
286 struct usb_endpoint_descriptor
*bulk_in
, *bulk_out
;
287 struct usb_anchor rx_submitted
;
290 unsigned int nchannels
;
293 void *rxbuf
[MAX_RX_URBS
];
294 dma_addr_t rxbuf_dma
[MAX_RX_URBS
];
297 struct kvaser_usb_net_priv
{
300 atomic_t active_tx_urbs
;
301 struct usb_anchor tx_submitted
;
302 struct kvaser_usb_tx_urb_context tx_contexts
[MAX_TX_URBS
];
304 struct completion start_comp
, stop_comp
;
306 struct kvaser_usb
*dev
;
307 struct net_device
*netdev
;
310 struct can_berr_counter bec
;
313 static const struct usb_device_id kvaser_usb_table
[] = {
314 { USB_DEVICE(KVASER_VENDOR_ID
, USB_LEAF_DEVEL_PRODUCT_ID
) },
315 { USB_DEVICE(KVASER_VENDOR_ID
, USB_LEAF_LITE_PRODUCT_ID
) },
316 { USB_DEVICE(KVASER_VENDOR_ID
, USB_LEAF_PRO_PRODUCT_ID
),
317 .driver_info
= KVASER_HAS_TXRX_ERRORS
|
318 KVASER_HAS_SILENT_MODE
},
319 { USB_DEVICE(KVASER_VENDOR_ID
, USB_LEAF_SPRO_PRODUCT_ID
),
320 .driver_info
= KVASER_HAS_TXRX_ERRORS
|
321 KVASER_HAS_SILENT_MODE
},
322 { USB_DEVICE(KVASER_VENDOR_ID
, USB_LEAF_PRO_LS_PRODUCT_ID
),
323 .driver_info
= KVASER_HAS_TXRX_ERRORS
|
324 KVASER_HAS_SILENT_MODE
},
325 { USB_DEVICE(KVASER_VENDOR_ID
, USB_LEAF_PRO_SWC_PRODUCT_ID
),
326 .driver_info
= KVASER_HAS_TXRX_ERRORS
|
327 KVASER_HAS_SILENT_MODE
},
328 { USB_DEVICE(KVASER_VENDOR_ID
, USB_LEAF_PRO_LIN_PRODUCT_ID
),
329 .driver_info
= KVASER_HAS_TXRX_ERRORS
|
330 KVASER_HAS_SILENT_MODE
},
331 { USB_DEVICE(KVASER_VENDOR_ID
, USB_LEAF_SPRO_LS_PRODUCT_ID
),
332 .driver_info
= KVASER_HAS_TXRX_ERRORS
|
333 KVASER_HAS_SILENT_MODE
},
334 { USB_DEVICE(KVASER_VENDOR_ID
, USB_LEAF_SPRO_SWC_PRODUCT_ID
),
335 .driver_info
= KVASER_HAS_TXRX_ERRORS
|
336 KVASER_HAS_SILENT_MODE
},
337 { USB_DEVICE(KVASER_VENDOR_ID
, USB_MEMO2_DEVEL_PRODUCT_ID
),
338 .driver_info
= KVASER_HAS_TXRX_ERRORS
|
339 KVASER_HAS_SILENT_MODE
},
340 { USB_DEVICE(KVASER_VENDOR_ID
, USB_MEMO2_HSHS_PRODUCT_ID
),
341 .driver_info
= KVASER_HAS_TXRX_ERRORS
|
342 KVASER_HAS_SILENT_MODE
},
343 { USB_DEVICE(KVASER_VENDOR_ID
, USB_UPRO_HSHS_PRODUCT_ID
),
344 .driver_info
= KVASER_HAS_TXRX_ERRORS
},
345 { USB_DEVICE(KVASER_VENDOR_ID
, USB_LEAF_LITE_GI_PRODUCT_ID
) },
346 { USB_DEVICE(KVASER_VENDOR_ID
, USB_LEAF_PRO_OBDII_PRODUCT_ID
),
347 .driver_info
= KVASER_HAS_TXRX_ERRORS
|
348 KVASER_HAS_SILENT_MODE
},
349 { USB_DEVICE(KVASER_VENDOR_ID
, USB_MEMO2_HSLS_PRODUCT_ID
),
350 .driver_info
= KVASER_HAS_TXRX_ERRORS
},
351 { USB_DEVICE(KVASER_VENDOR_ID
, USB_LEAF_LITE_CH_PRODUCT_ID
),
352 .driver_info
= KVASER_HAS_TXRX_ERRORS
},
353 { USB_DEVICE(KVASER_VENDOR_ID
, USB_BLACKBIRD_SPRO_PRODUCT_ID
),
354 .driver_info
= KVASER_HAS_TXRX_ERRORS
},
355 { USB_DEVICE(KVASER_VENDOR_ID
, USB_OEM_MERCURY_PRODUCT_ID
),
356 .driver_info
= KVASER_HAS_TXRX_ERRORS
},
357 { USB_DEVICE(KVASER_VENDOR_ID
, USB_OEM_LEAF_PRODUCT_ID
),
358 .driver_info
= KVASER_HAS_TXRX_ERRORS
},
359 { USB_DEVICE(KVASER_VENDOR_ID
, USB_CAN_R_PRODUCT_ID
),
360 .driver_info
= KVASER_HAS_TXRX_ERRORS
},
361 { USB_DEVICE(KVASER_VENDOR_ID
, USB_LEAF_LITE_V2_PRODUCT_ID
) },
362 { USB_DEVICE(KVASER_VENDOR_ID
, USB_MINI_PCIE_HS_PRODUCT_ID
) },
365 MODULE_DEVICE_TABLE(usb
, kvaser_usb_table
);
367 static inline int kvaser_usb_send_msg(const struct kvaser_usb
*dev
,
368 struct kvaser_msg
*msg
)
372 return usb_bulk_msg(dev
->udev
,
373 usb_sndbulkpipe(dev
->udev
,
374 dev
->bulk_out
->bEndpointAddress
),
375 msg
, msg
->len
, &actual_len
,
379 static int kvaser_usb_wait_msg(const struct kvaser_usb
*dev
, u8 id
,
380 struct kvaser_msg
*msg
)
382 struct kvaser_msg
*tmp
;
387 unsigned long to
= jiffies
+ msecs_to_jiffies(USB_RECV_TIMEOUT
);
389 buf
= kzalloc(RX_BUFFER_SIZE
, GFP_KERNEL
);
394 err
= usb_bulk_msg(dev
->udev
,
395 usb_rcvbulkpipe(dev
->udev
,
396 dev
->bulk_in
->bEndpointAddress
),
397 buf
, RX_BUFFER_SIZE
, &actual_len
,
403 while (pos
<= actual_len
- MSG_HEADER_LEN
) {
409 if (pos
+ tmp
->len
> actual_len
) {
410 dev_err(dev
->udev
->dev
.parent
,
416 memcpy(msg
, tmp
, tmp
->len
);
422 } while (time_before(jiffies
, to
));
432 static int kvaser_usb_send_simple_msg(const struct kvaser_usb
*dev
,
433 u8 msg_id
, int channel
)
435 struct kvaser_msg
*msg
;
438 msg
= kmalloc(sizeof(*msg
), GFP_KERNEL
);
443 msg
->len
= MSG_HEADER_LEN
+ sizeof(struct kvaser_msg_simple
);
444 msg
->u
.simple
.channel
= channel
;
445 msg
->u
.simple
.tid
= 0xff;
447 rc
= kvaser_usb_send_msg(dev
, msg
);
453 static int kvaser_usb_get_software_info(struct kvaser_usb
*dev
)
455 struct kvaser_msg msg
;
458 err
= kvaser_usb_send_simple_msg(dev
, CMD_GET_SOFTWARE_INFO
, 0);
462 err
= kvaser_usb_wait_msg(dev
, CMD_GET_SOFTWARE_INFO_REPLY
, &msg
);
466 dev
->fw_version
= le32_to_cpu(msg
.u
.softinfo
.fw_version
);
471 static int kvaser_usb_get_card_info(struct kvaser_usb
*dev
)
473 struct kvaser_msg msg
;
476 err
= kvaser_usb_send_simple_msg(dev
, CMD_GET_CARD_INFO
, 0);
480 err
= kvaser_usb_wait_msg(dev
, CMD_GET_CARD_INFO_REPLY
, &msg
);
484 dev
->nchannels
= msg
.u
.cardinfo
.nchannels
;
485 if (dev
->nchannels
> MAX_NET_DEVICES
)
491 static void kvaser_usb_tx_acknowledge(const struct kvaser_usb
*dev
,
492 const struct kvaser_msg
*msg
)
494 struct net_device_stats
*stats
;
495 struct kvaser_usb_tx_urb_context
*context
;
496 struct kvaser_usb_net_priv
*priv
;
498 struct can_frame
*cf
;
499 u8 channel
= msg
->u
.tx_acknowledge
.channel
;
500 u8 tid
= msg
->u
.tx_acknowledge
.tid
;
502 if (channel
>= dev
->nchannels
) {
503 dev_err(dev
->udev
->dev
.parent
,
504 "Invalid channel number (%d)\n", channel
);
508 priv
= dev
->nets
[channel
];
510 if (!netif_device_present(priv
->netdev
))
513 stats
= &priv
->netdev
->stats
;
515 context
= &priv
->tx_contexts
[tid
% MAX_TX_URBS
];
517 /* Sometimes the state change doesn't come after a bus-off event */
518 if (priv
->can
.restart_ms
&&
519 (priv
->can
.state
>= CAN_STATE_BUS_OFF
)) {
520 skb
= alloc_can_err_skb(priv
->netdev
, &cf
);
522 cf
->can_id
|= CAN_ERR_RESTARTED
;
526 stats
->rx_bytes
+= cf
->can_dlc
;
528 netdev_err(priv
->netdev
,
529 "No memory left for err_skb\n");
532 priv
->can
.can_stats
.restarts
++;
533 netif_carrier_on(priv
->netdev
);
535 priv
->can
.state
= CAN_STATE_ERROR_ACTIVE
;
539 stats
->tx_bytes
+= context
->dlc
;
540 can_get_echo_skb(priv
->netdev
, context
->echo_index
);
542 context
->echo_index
= MAX_TX_URBS
;
543 atomic_dec(&priv
->active_tx_urbs
);
545 netif_wake_queue(priv
->netdev
);
548 static void kvaser_usb_simple_msg_callback(struct urb
*urb
)
550 struct net_device
*netdev
= urb
->context
;
552 kfree(urb
->transfer_buffer
);
555 netdev_warn(netdev
, "urb status received: %d\n",
559 static int kvaser_usb_simple_msg_async(struct kvaser_usb_net_priv
*priv
,
562 struct kvaser_usb
*dev
= priv
->dev
;
563 struct net_device
*netdev
= priv
->netdev
;
564 struct kvaser_msg
*msg
;
569 urb
= usb_alloc_urb(0, GFP_ATOMIC
);
571 netdev_err(netdev
, "No memory left for URBs\n");
575 buf
= kmalloc(sizeof(struct kvaser_msg
), GFP_ATOMIC
);
581 msg
= (struct kvaser_msg
*)buf
;
582 msg
->len
= MSG_HEADER_LEN
+ sizeof(struct kvaser_msg_simple
);
584 msg
->u
.simple
.channel
= priv
->channel
;
586 usb_fill_bulk_urb(urb
, dev
->udev
,
587 usb_sndbulkpipe(dev
->udev
,
588 dev
->bulk_out
->bEndpointAddress
),
590 kvaser_usb_simple_msg_callback
, priv
);
591 usb_anchor_urb(urb
, &priv
->tx_submitted
);
593 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
595 netdev_err(netdev
, "Error transmitting URB\n");
596 usb_unanchor_urb(urb
);
607 static void kvaser_usb_unlink_tx_urbs(struct kvaser_usb_net_priv
*priv
)
611 usb_kill_anchored_urbs(&priv
->tx_submitted
);
612 atomic_set(&priv
->active_tx_urbs
, 0);
614 for (i
= 0; i
< MAX_TX_URBS
; i
++)
615 priv
->tx_contexts
[i
].echo_index
= MAX_TX_URBS
;
618 static void kvaser_usb_rx_error(const struct kvaser_usb
*dev
,
619 const struct kvaser_msg
*msg
)
621 struct can_frame
*cf
;
623 struct net_device_stats
*stats
;
624 struct kvaser_usb_net_priv
*priv
;
625 unsigned int new_state
;
626 u8 channel
, status
, txerr
, rxerr
, error_factor
;
629 case CMD_CAN_ERROR_EVENT
:
630 channel
= msg
->u
.error_event
.channel
;
631 status
= msg
->u
.error_event
.status
;
632 txerr
= msg
->u
.error_event
.tx_errors_count
;
633 rxerr
= msg
->u
.error_event
.rx_errors_count
;
634 error_factor
= msg
->u
.error_event
.error_factor
;
636 case CMD_LOG_MESSAGE
:
637 channel
= msg
->u
.log_message
.channel
;
638 status
= msg
->u
.log_message
.data
[0];
639 txerr
= msg
->u
.log_message
.data
[2];
640 rxerr
= msg
->u
.log_message
.data
[3];
641 error_factor
= msg
->u
.log_message
.data
[1];
643 case CMD_CHIP_STATE_EVENT
:
644 channel
= msg
->u
.chip_state_event
.channel
;
645 status
= msg
->u
.chip_state_event
.status
;
646 txerr
= msg
->u
.chip_state_event
.tx_errors_count
;
647 rxerr
= msg
->u
.chip_state_event
.rx_errors_count
;
651 dev_err(dev
->udev
->dev
.parent
, "Invalid msg id (%d)\n",
656 if (channel
>= dev
->nchannels
) {
657 dev_err(dev
->udev
->dev
.parent
,
658 "Invalid channel number (%d)\n", channel
);
662 priv
= dev
->nets
[channel
];
663 stats
= &priv
->netdev
->stats
;
665 if (status
& M16C_STATE_BUS_RESET
) {
666 kvaser_usb_unlink_tx_urbs(priv
);
670 skb
= alloc_can_err_skb(priv
->netdev
, &cf
);
676 new_state
= priv
->can
.state
;
678 netdev_dbg(priv
->netdev
, "Error status: 0x%02x\n", status
);
680 if (status
& M16C_STATE_BUS_OFF
) {
681 cf
->can_id
|= CAN_ERR_BUSOFF
;
683 priv
->can
.can_stats
.bus_off
++;
684 if (!priv
->can
.restart_ms
)
685 kvaser_usb_simple_msg_async(priv
, CMD_STOP_CHIP
);
687 netif_carrier_off(priv
->netdev
);
689 new_state
= CAN_STATE_BUS_OFF
;
690 } else if (status
& M16C_STATE_BUS_PASSIVE
) {
691 if (priv
->can
.state
!= CAN_STATE_ERROR_PASSIVE
) {
692 cf
->can_id
|= CAN_ERR_CRTL
;
695 cf
->data
[1] = (txerr
> rxerr
)
696 ? CAN_ERR_CRTL_TX_PASSIVE
697 : CAN_ERR_CRTL_RX_PASSIVE
;
699 cf
->data
[1] = CAN_ERR_CRTL_TX_PASSIVE
|
700 CAN_ERR_CRTL_RX_PASSIVE
;
702 priv
->can
.can_stats
.error_passive
++;
705 new_state
= CAN_STATE_ERROR_PASSIVE
;
708 if (status
== M16C_STATE_BUS_ERROR
) {
709 if ((priv
->can
.state
< CAN_STATE_ERROR_WARNING
) &&
710 ((txerr
>= 96) || (rxerr
>= 96))) {
711 cf
->can_id
|= CAN_ERR_CRTL
;
712 cf
->data
[1] = (txerr
> rxerr
)
713 ? CAN_ERR_CRTL_TX_WARNING
714 : CAN_ERR_CRTL_RX_WARNING
;
716 priv
->can
.can_stats
.error_warning
++;
717 new_state
= CAN_STATE_ERROR_WARNING
;
718 } else if (priv
->can
.state
> CAN_STATE_ERROR_ACTIVE
) {
719 cf
->can_id
|= CAN_ERR_PROT
;
720 cf
->data
[2] = CAN_ERR_PROT_ACTIVE
;
722 new_state
= CAN_STATE_ERROR_ACTIVE
;
727 cf
->can_id
|= CAN_ERR_PROT
;
728 cf
->data
[2] = CAN_ERR_PROT_ACTIVE
;
730 new_state
= CAN_STATE_ERROR_ACTIVE
;
733 if (priv
->can
.restart_ms
&&
734 (priv
->can
.state
>= CAN_STATE_BUS_OFF
) &&
735 (new_state
< CAN_STATE_BUS_OFF
)) {
736 cf
->can_id
|= CAN_ERR_RESTARTED
;
737 netif_carrier_on(priv
->netdev
);
739 priv
->can
.can_stats
.restarts
++;
743 priv
->can
.can_stats
.bus_error
++;
746 cf
->can_id
|= CAN_ERR_BUSERROR
| CAN_ERR_PROT
;
748 if (error_factor
& M16C_EF_ACKE
)
749 cf
->data
[3] |= (CAN_ERR_PROT_LOC_ACK
);
750 if (error_factor
& M16C_EF_CRCE
)
751 cf
->data
[3] |= (CAN_ERR_PROT_LOC_CRC_SEQ
|
752 CAN_ERR_PROT_LOC_CRC_DEL
);
753 if (error_factor
& M16C_EF_FORME
)
754 cf
->data
[2] |= CAN_ERR_PROT_FORM
;
755 if (error_factor
& M16C_EF_STFE
)
756 cf
->data
[2] |= CAN_ERR_PROT_STUFF
;
757 if (error_factor
& M16C_EF_BITE0
)
758 cf
->data
[2] |= CAN_ERR_PROT_BIT0
;
759 if (error_factor
& M16C_EF_BITE1
)
760 cf
->data
[2] |= CAN_ERR_PROT_BIT1
;
761 if (error_factor
& M16C_EF_TRE
)
762 cf
->data
[2] |= CAN_ERR_PROT_TX
;
768 priv
->bec
.txerr
= txerr
;
769 priv
->bec
.rxerr
= rxerr
;
771 priv
->can
.state
= new_state
;
776 stats
->rx_bytes
+= cf
->can_dlc
;
779 static void kvaser_usb_rx_can_err(const struct kvaser_usb_net_priv
*priv
,
780 const struct kvaser_msg
*msg
)
782 struct can_frame
*cf
;
784 struct net_device_stats
*stats
= &priv
->netdev
->stats
;
786 if (msg
->u
.rx_can
.flag
& (MSG_FLAG_ERROR_FRAME
|
788 netdev_err(priv
->netdev
, "Unknow error (flags: 0x%02x)\n",
795 if (msg
->u
.rx_can
.flag
& MSG_FLAG_OVERRUN
) {
796 skb
= alloc_can_err_skb(priv
->netdev
, &cf
);
802 cf
->can_id
|= CAN_ERR_CRTL
;
803 cf
->data
[1] = CAN_ERR_CRTL_RX_OVERFLOW
;
805 stats
->rx_over_errors
++;
811 stats
->rx_bytes
+= cf
->can_dlc
;
815 static void kvaser_usb_rx_can_msg(const struct kvaser_usb
*dev
,
816 const struct kvaser_msg
*msg
)
818 struct kvaser_usb_net_priv
*priv
;
819 struct can_frame
*cf
;
821 struct net_device_stats
*stats
;
822 u8 channel
= msg
->u
.rx_can
.channel
;
824 if (channel
>= dev
->nchannels
) {
825 dev_err(dev
->udev
->dev
.parent
,
826 "Invalid channel number (%d)\n", channel
);
830 priv
= dev
->nets
[channel
];
831 stats
= &priv
->netdev
->stats
;
833 if ((msg
->u
.rx_can
.flag
& MSG_FLAG_ERROR_FRAME
) &&
834 (msg
->id
== CMD_LOG_MESSAGE
)) {
835 kvaser_usb_rx_error(dev
, msg
);
837 } else if (msg
->u
.rx_can
.flag
& (MSG_FLAG_ERROR_FRAME
|
840 kvaser_usb_rx_can_err(priv
, msg
);
842 } else if (msg
->u
.rx_can
.flag
& ~MSG_FLAG_REMOTE_FRAME
) {
843 netdev_warn(priv
->netdev
,
844 "Unhandled frame (flags: 0x%02x)",
849 skb
= alloc_can_skb(priv
->netdev
, &cf
);
855 if (msg
->id
== CMD_LOG_MESSAGE
) {
856 cf
->can_id
= le32_to_cpu(msg
->u
.log_message
.id
);
857 if (cf
->can_id
& KVASER_EXTENDED_FRAME
)
858 cf
->can_id
&= CAN_EFF_MASK
| CAN_EFF_FLAG
;
860 cf
->can_id
&= CAN_SFF_MASK
;
862 cf
->can_dlc
= get_can_dlc(msg
->u
.log_message
.dlc
);
864 if (msg
->u
.log_message
.flags
& MSG_FLAG_REMOTE_FRAME
)
865 cf
->can_id
|= CAN_RTR_FLAG
;
867 memcpy(cf
->data
, &msg
->u
.log_message
.data
,
870 cf
->can_id
= ((msg
->u
.rx_can
.msg
[0] & 0x1f) << 6) |
871 (msg
->u
.rx_can
.msg
[1] & 0x3f);
873 if (msg
->id
== CMD_RX_EXT_MESSAGE
) {
875 cf
->can_id
|= ((msg
->u
.rx_can
.msg
[2] & 0x0f) << 14) |
876 ((msg
->u
.rx_can
.msg
[3] & 0xff) << 6) |
877 (msg
->u
.rx_can
.msg
[4] & 0x3f);
878 cf
->can_id
|= CAN_EFF_FLAG
;
881 cf
->can_dlc
= get_can_dlc(msg
->u
.rx_can
.msg
[5]);
883 if (msg
->u
.rx_can
.flag
& MSG_FLAG_REMOTE_FRAME
)
884 cf
->can_id
|= CAN_RTR_FLAG
;
886 memcpy(cf
->data
, &msg
->u
.rx_can
.msg
[6],
893 stats
->rx_bytes
+= cf
->can_dlc
;
896 static void kvaser_usb_start_chip_reply(const struct kvaser_usb
*dev
,
897 const struct kvaser_msg
*msg
)
899 struct kvaser_usb_net_priv
*priv
;
900 u8 channel
= msg
->u
.simple
.channel
;
902 if (channel
>= dev
->nchannels
) {
903 dev_err(dev
->udev
->dev
.parent
,
904 "Invalid channel number (%d)\n", channel
);
908 priv
= dev
->nets
[channel
];
910 if (completion_done(&priv
->start_comp
) &&
911 netif_queue_stopped(priv
->netdev
)) {
912 netif_wake_queue(priv
->netdev
);
914 netif_start_queue(priv
->netdev
);
915 complete(&priv
->start_comp
);
919 static void kvaser_usb_stop_chip_reply(const struct kvaser_usb
*dev
,
920 const struct kvaser_msg
*msg
)
922 struct kvaser_usb_net_priv
*priv
;
923 u8 channel
= msg
->u
.simple
.channel
;
925 if (channel
>= dev
->nchannels
) {
926 dev_err(dev
->udev
->dev
.parent
,
927 "Invalid channel number (%d)\n", channel
);
931 priv
= dev
->nets
[channel
];
933 complete(&priv
->stop_comp
);
936 static void kvaser_usb_handle_message(const struct kvaser_usb
*dev
,
937 const struct kvaser_msg
*msg
)
940 case CMD_START_CHIP_REPLY
:
941 kvaser_usb_start_chip_reply(dev
, msg
);
944 case CMD_STOP_CHIP_REPLY
:
945 kvaser_usb_stop_chip_reply(dev
, msg
);
948 case CMD_RX_STD_MESSAGE
:
949 case CMD_RX_EXT_MESSAGE
:
950 case CMD_LOG_MESSAGE
:
951 kvaser_usb_rx_can_msg(dev
, msg
);
954 case CMD_CHIP_STATE_EVENT
:
955 case CMD_CAN_ERROR_EVENT
:
956 kvaser_usb_rx_error(dev
, msg
);
959 case CMD_TX_ACKNOWLEDGE
:
960 kvaser_usb_tx_acknowledge(dev
, msg
);
964 dev_warn(dev
->udev
->dev
.parent
,
965 "Unhandled message (%d)\n", msg
->id
);
970 static void kvaser_usb_read_bulk_callback(struct urb
*urb
)
972 struct kvaser_usb
*dev
= urb
->context
;
973 struct kvaser_msg
*msg
;
977 switch (urb
->status
) {
984 dev_info(dev
->udev
->dev
.parent
, "Rx URB aborted (%d)\n",
989 while (pos
<= urb
->actual_length
- MSG_HEADER_LEN
) {
990 msg
= urb
->transfer_buffer
+ pos
;
995 if (pos
+ msg
->len
> urb
->actual_length
) {
996 dev_err(dev
->udev
->dev
.parent
, "Format error\n");
1000 kvaser_usb_handle_message(dev
, msg
);
1006 usb_fill_bulk_urb(urb
, dev
->udev
,
1007 usb_rcvbulkpipe(dev
->udev
,
1008 dev
->bulk_in
->bEndpointAddress
),
1009 urb
->transfer_buffer
, RX_BUFFER_SIZE
,
1010 kvaser_usb_read_bulk_callback
, dev
);
1012 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
1013 if (err
== -ENODEV
) {
1014 for (i
= 0; i
< dev
->nchannels
; i
++) {
1018 netif_device_detach(dev
->nets
[i
]->netdev
);
1021 dev_err(dev
->udev
->dev
.parent
,
1022 "Failed resubmitting read bulk urb: %d\n", err
);
1028 static int kvaser_usb_setup_rx_urbs(struct kvaser_usb
*dev
)
1032 if (dev
->rxinitdone
)
1035 for (i
= 0; i
< MAX_RX_URBS
; i
++) {
1036 struct urb
*urb
= NULL
;
1040 urb
= usb_alloc_urb(0, GFP_KERNEL
);
1042 dev_warn(dev
->udev
->dev
.parent
,
1043 "No memory left for URBs\n");
1048 buf
= usb_alloc_coherent(dev
->udev
, RX_BUFFER_SIZE
,
1049 GFP_KERNEL
, &buf_dma
);
1051 dev_warn(dev
->udev
->dev
.parent
,
1052 "No memory left for USB buffer\n");
1058 usb_fill_bulk_urb(urb
, dev
->udev
,
1059 usb_rcvbulkpipe(dev
->udev
,
1060 dev
->bulk_in
->bEndpointAddress
),
1061 buf
, RX_BUFFER_SIZE
,
1062 kvaser_usb_read_bulk_callback
,
1064 urb
->transfer_dma
= buf_dma
;
1065 urb
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
1066 usb_anchor_urb(urb
, &dev
->rx_submitted
);
1068 err
= usb_submit_urb(urb
, GFP_KERNEL
);
1070 usb_unanchor_urb(urb
);
1071 usb_free_coherent(dev
->udev
, RX_BUFFER_SIZE
, buf
,
1077 dev
->rxbuf
[i
] = buf
;
1078 dev
->rxbuf_dma
[i
] = buf_dma
;
1084 dev_warn(dev
->udev
->dev
.parent
,
1085 "Cannot setup read URBs, error %d\n", err
);
1087 } else if (i
< MAX_RX_URBS
) {
1088 dev_warn(dev
->udev
->dev
.parent
,
1089 "RX performances may be slow\n");
1092 dev
->rxinitdone
= true;
1097 static int kvaser_usb_set_opt_mode(const struct kvaser_usb_net_priv
*priv
)
1099 struct kvaser_msg
*msg
;
1102 msg
= kmalloc(sizeof(*msg
), GFP_KERNEL
);
1106 msg
->id
= CMD_SET_CTRL_MODE
;
1107 msg
->len
= MSG_HEADER_LEN
+ sizeof(struct kvaser_msg_ctrl_mode
);
1108 msg
->u
.ctrl_mode
.tid
= 0xff;
1109 msg
->u
.ctrl_mode
.channel
= priv
->channel
;
1111 if (priv
->can
.ctrlmode
& CAN_CTRLMODE_LISTENONLY
)
1112 msg
->u
.ctrl_mode
.ctrl_mode
= KVASER_CTRL_MODE_SILENT
;
1114 msg
->u
.ctrl_mode
.ctrl_mode
= KVASER_CTRL_MODE_NORMAL
;
1116 rc
= kvaser_usb_send_msg(priv
->dev
, msg
);
1122 static int kvaser_usb_start_chip(struct kvaser_usb_net_priv
*priv
)
1126 init_completion(&priv
->start_comp
);
1128 err
= kvaser_usb_send_simple_msg(priv
->dev
, CMD_START_CHIP
,
1133 if (!wait_for_completion_timeout(&priv
->start_comp
,
1134 msecs_to_jiffies(START_TIMEOUT
)))
1140 static int kvaser_usb_open(struct net_device
*netdev
)
1142 struct kvaser_usb_net_priv
*priv
= netdev_priv(netdev
);
1143 struct kvaser_usb
*dev
= priv
->dev
;
1146 err
= open_candev(netdev
);
1150 err
= kvaser_usb_setup_rx_urbs(dev
);
1154 err
= kvaser_usb_set_opt_mode(priv
);
1158 err
= kvaser_usb_start_chip(priv
);
1160 netdev_warn(netdev
, "Cannot start device, error %d\n", err
);
1164 priv
->can
.state
= CAN_STATE_ERROR_ACTIVE
;
1169 close_candev(netdev
);
1173 static void kvaser_usb_unlink_all_urbs(struct kvaser_usb
*dev
)
1177 usb_kill_anchored_urbs(&dev
->rx_submitted
);
1179 for (i
= 0; i
< MAX_RX_URBS
; i
++)
1180 usb_free_coherent(dev
->udev
, RX_BUFFER_SIZE
,
1184 for (i
= 0; i
< MAX_NET_DEVICES
; i
++) {
1185 struct kvaser_usb_net_priv
*priv
= dev
->nets
[i
];
1188 kvaser_usb_unlink_tx_urbs(priv
);
1192 static int kvaser_usb_stop_chip(struct kvaser_usb_net_priv
*priv
)
1196 init_completion(&priv
->stop_comp
);
1198 err
= kvaser_usb_send_simple_msg(priv
->dev
, CMD_STOP_CHIP
,
1203 if (!wait_for_completion_timeout(&priv
->stop_comp
,
1204 msecs_to_jiffies(STOP_TIMEOUT
)))
1210 static int kvaser_usb_flush_queue(struct kvaser_usb_net_priv
*priv
)
1212 struct kvaser_msg
*msg
;
1215 msg
= kmalloc(sizeof(*msg
), GFP_KERNEL
);
1219 msg
->id
= CMD_FLUSH_QUEUE
;
1220 msg
->len
= MSG_HEADER_LEN
+ sizeof(struct kvaser_msg_flush_queue
);
1221 msg
->u
.flush_queue
.channel
= priv
->channel
;
1222 msg
->u
.flush_queue
.flags
= 0x00;
1224 rc
= kvaser_usb_send_msg(priv
->dev
, msg
);
1230 static int kvaser_usb_close(struct net_device
*netdev
)
1232 struct kvaser_usb_net_priv
*priv
= netdev_priv(netdev
);
1233 struct kvaser_usb
*dev
= priv
->dev
;
1236 netif_stop_queue(netdev
);
1238 err
= kvaser_usb_flush_queue(priv
);
1240 netdev_warn(netdev
, "Cannot flush queue, error %d\n", err
);
1242 if (kvaser_usb_send_simple_msg(dev
, CMD_RESET_CHIP
, priv
->channel
))
1243 netdev_warn(netdev
, "Cannot reset card, error %d\n", err
);
1245 err
= kvaser_usb_stop_chip(priv
);
1247 netdev_warn(netdev
, "Cannot stop device, error %d\n", err
);
1249 priv
->can
.state
= CAN_STATE_STOPPED
;
1250 close_candev(priv
->netdev
);
1255 static void kvaser_usb_write_bulk_callback(struct urb
*urb
)
1257 struct kvaser_usb_tx_urb_context
*context
= urb
->context
;
1258 struct kvaser_usb_net_priv
*priv
;
1259 struct net_device
*netdev
;
1261 if (WARN_ON(!context
))
1264 priv
= context
->priv
;
1265 netdev
= priv
->netdev
;
1267 kfree(urb
->transfer_buffer
);
1269 if (!netif_device_present(netdev
))
1273 netdev_info(netdev
, "Tx URB aborted (%d)\n", urb
->status
);
1276 static netdev_tx_t
kvaser_usb_start_xmit(struct sk_buff
*skb
,
1277 struct net_device
*netdev
)
1279 struct kvaser_usb_net_priv
*priv
= netdev_priv(netdev
);
1280 struct kvaser_usb
*dev
= priv
->dev
;
1281 struct net_device_stats
*stats
= &netdev
->stats
;
1282 struct can_frame
*cf
= (struct can_frame
*)skb
->data
;
1283 struct kvaser_usb_tx_urb_context
*context
= NULL
;
1286 struct kvaser_msg
*msg
;
1288 int ret
= NETDEV_TX_OK
;
1290 if (can_dropped_invalid_skb(netdev
, skb
))
1291 return NETDEV_TX_OK
;
1293 urb
= usb_alloc_urb(0, GFP_ATOMIC
);
1295 netdev_err(netdev
, "No memory left for URBs\n");
1296 stats
->tx_dropped
++;
1300 buf
= kmalloc(sizeof(struct kvaser_msg
), GFP_ATOMIC
);
1302 stats
->tx_dropped
++;
1307 msg
->len
= MSG_HEADER_LEN
+ sizeof(struct kvaser_msg_tx_can
);
1308 msg
->u
.tx_can
.flags
= 0;
1309 msg
->u
.tx_can
.channel
= priv
->channel
;
1311 if (cf
->can_id
& CAN_EFF_FLAG
) {
1312 msg
->id
= CMD_TX_EXT_MESSAGE
;
1313 msg
->u
.tx_can
.msg
[0] = (cf
->can_id
>> 24) & 0x1f;
1314 msg
->u
.tx_can
.msg
[1] = (cf
->can_id
>> 18) & 0x3f;
1315 msg
->u
.tx_can
.msg
[2] = (cf
->can_id
>> 14) & 0x0f;
1316 msg
->u
.tx_can
.msg
[3] = (cf
->can_id
>> 6) & 0xff;
1317 msg
->u
.tx_can
.msg
[4] = cf
->can_id
& 0x3f;
1319 msg
->id
= CMD_TX_STD_MESSAGE
;
1320 msg
->u
.tx_can
.msg
[0] = (cf
->can_id
>> 6) & 0x1f;
1321 msg
->u
.tx_can
.msg
[1] = cf
->can_id
& 0x3f;
1324 msg
->u
.tx_can
.msg
[5] = cf
->can_dlc
;
1325 memcpy(&msg
->u
.tx_can
.msg
[6], cf
->data
, cf
->can_dlc
);
1327 if (cf
->can_id
& CAN_RTR_FLAG
)
1328 msg
->u
.tx_can
.flags
|= MSG_FLAG_REMOTE_FRAME
;
1330 for (i
= 0; i
< ARRAY_SIZE(priv
->tx_contexts
); i
++) {
1331 if (priv
->tx_contexts
[i
].echo_index
== MAX_TX_URBS
) {
1332 context
= &priv
->tx_contexts
[i
];
1338 netdev_warn(netdev
, "cannot find free context\n");
1339 ret
= NETDEV_TX_BUSY
;
1343 context
->priv
= priv
;
1344 context
->echo_index
= i
;
1345 context
->dlc
= cf
->can_dlc
;
1347 msg
->u
.tx_can
.tid
= context
->echo_index
;
1349 usb_fill_bulk_urb(urb
, dev
->udev
,
1350 usb_sndbulkpipe(dev
->udev
,
1351 dev
->bulk_out
->bEndpointAddress
),
1353 kvaser_usb_write_bulk_callback
, context
);
1354 usb_anchor_urb(urb
, &priv
->tx_submitted
);
1356 can_put_echo_skb(skb
, netdev
, context
->echo_index
);
1358 atomic_inc(&priv
->active_tx_urbs
);
1360 if (atomic_read(&priv
->active_tx_urbs
) >= MAX_TX_URBS
)
1361 netif_stop_queue(netdev
);
1363 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
1364 if (unlikely(err
)) {
1365 can_free_echo_skb(netdev
, context
->echo_index
);
1367 skb
= NULL
; /* set to NULL to avoid double free in
1368 * dev_kfree_skb(skb) */
1370 atomic_dec(&priv
->active_tx_urbs
);
1371 usb_unanchor_urb(urb
);
1373 stats
->tx_dropped
++;
1376 netif_device_detach(netdev
);
1378 netdev_warn(netdev
, "Failed tx_urb %d\n", err
);
1385 return NETDEV_TX_OK
;
1396 static const struct net_device_ops kvaser_usb_netdev_ops
= {
1397 .ndo_open
= kvaser_usb_open
,
1398 .ndo_stop
= kvaser_usb_close
,
1399 .ndo_start_xmit
= kvaser_usb_start_xmit
,
1400 .ndo_change_mtu
= can_change_mtu
,
1403 static const struct can_bittiming_const kvaser_usb_bittiming_const
= {
1404 .name
= "kvaser_usb",
1405 .tseg1_min
= KVASER_USB_TSEG1_MIN
,
1406 .tseg1_max
= KVASER_USB_TSEG1_MAX
,
1407 .tseg2_min
= KVASER_USB_TSEG2_MIN
,
1408 .tseg2_max
= KVASER_USB_TSEG2_MAX
,
1409 .sjw_max
= KVASER_USB_SJW_MAX
,
1410 .brp_min
= KVASER_USB_BRP_MIN
,
1411 .brp_max
= KVASER_USB_BRP_MAX
,
1412 .brp_inc
= KVASER_USB_BRP_INC
,
1415 static int kvaser_usb_set_bittiming(struct net_device
*netdev
)
1417 struct kvaser_usb_net_priv
*priv
= netdev_priv(netdev
);
1418 struct can_bittiming
*bt
= &priv
->can
.bittiming
;
1419 struct kvaser_usb
*dev
= priv
->dev
;
1420 struct kvaser_msg
*msg
;
1423 msg
= kmalloc(sizeof(*msg
), GFP_KERNEL
);
1427 msg
->id
= CMD_SET_BUS_PARAMS
;
1428 msg
->len
= MSG_HEADER_LEN
+ sizeof(struct kvaser_msg_busparams
);
1429 msg
->u
.busparams
.channel
= priv
->channel
;
1430 msg
->u
.busparams
.tid
= 0xff;
1431 msg
->u
.busparams
.bitrate
= cpu_to_le32(bt
->bitrate
);
1432 msg
->u
.busparams
.sjw
= bt
->sjw
;
1433 msg
->u
.busparams
.tseg1
= bt
->prop_seg
+ bt
->phase_seg1
;
1434 msg
->u
.busparams
.tseg2
= bt
->phase_seg2
;
1436 if (priv
->can
.ctrlmode
& CAN_CTRLMODE_3_SAMPLES
)
1437 msg
->u
.busparams
.no_samp
= 3;
1439 msg
->u
.busparams
.no_samp
= 1;
1441 rc
= kvaser_usb_send_msg(dev
, msg
);
1447 static int kvaser_usb_set_mode(struct net_device
*netdev
,
1450 struct kvaser_usb_net_priv
*priv
= netdev_priv(netdev
);
1454 case CAN_MODE_START
:
1455 err
= kvaser_usb_simple_msg_async(priv
, CMD_START_CHIP
);
1466 static int kvaser_usb_get_berr_counter(const struct net_device
*netdev
,
1467 struct can_berr_counter
*bec
)
1469 struct kvaser_usb_net_priv
*priv
= netdev_priv(netdev
);
1476 static void kvaser_usb_remove_interfaces(struct kvaser_usb
*dev
)
1480 for (i
= 0; i
< dev
->nchannels
; i
++) {
1484 unregister_netdev(dev
->nets
[i
]->netdev
);
1487 kvaser_usb_unlink_all_urbs(dev
);
1489 for (i
= 0; i
< dev
->nchannels
; i
++) {
1493 free_candev(dev
->nets
[i
]->netdev
);
1497 static int kvaser_usb_init_one(struct usb_interface
*intf
,
1498 const struct usb_device_id
*id
, int channel
)
1500 struct kvaser_usb
*dev
= usb_get_intfdata(intf
);
1501 struct net_device
*netdev
;
1502 struct kvaser_usb_net_priv
*priv
;
1505 netdev
= alloc_candev(sizeof(*priv
), MAX_TX_URBS
);
1507 dev_err(&intf
->dev
, "Cannot alloc candev\n");
1511 priv
= netdev_priv(netdev
);
1513 init_completion(&priv
->start_comp
);
1514 init_completion(&priv
->stop_comp
);
1516 init_usb_anchor(&priv
->tx_submitted
);
1517 atomic_set(&priv
->active_tx_urbs
, 0);
1519 for (i
= 0; i
< ARRAY_SIZE(priv
->tx_contexts
); i
++)
1520 priv
->tx_contexts
[i
].echo_index
= MAX_TX_URBS
;
1523 priv
->netdev
= netdev
;
1524 priv
->channel
= channel
;
1526 priv
->can
.state
= CAN_STATE_STOPPED
;
1527 priv
->can
.clock
.freq
= CAN_USB_CLOCK
;
1528 priv
->can
.bittiming_const
= &kvaser_usb_bittiming_const
;
1529 priv
->can
.do_set_bittiming
= kvaser_usb_set_bittiming
;
1530 priv
->can
.do_set_mode
= kvaser_usb_set_mode
;
1531 if (id
->driver_info
& KVASER_HAS_TXRX_ERRORS
)
1532 priv
->can
.do_get_berr_counter
= kvaser_usb_get_berr_counter
;
1533 priv
->can
.ctrlmode_supported
= CAN_CTRLMODE_3_SAMPLES
;
1534 if (id
->driver_info
& KVASER_HAS_SILENT_MODE
)
1535 priv
->can
.ctrlmode_supported
|= CAN_CTRLMODE_LISTENONLY
;
1537 netdev
->flags
|= IFF_ECHO
;
1539 netdev
->netdev_ops
= &kvaser_usb_netdev_ops
;
1541 SET_NETDEV_DEV(netdev
, &intf
->dev
);
1542 netdev
->dev_id
= channel
;
1544 dev
->nets
[channel
] = priv
;
1546 err
= register_candev(netdev
);
1548 dev_err(&intf
->dev
, "Failed to register can device\n");
1549 free_candev(netdev
);
1550 dev
->nets
[channel
] = NULL
;
1554 netdev_dbg(netdev
, "device registered\n");
1559 static int kvaser_usb_get_endpoints(const struct usb_interface
*intf
,
1560 struct usb_endpoint_descriptor
**in
,
1561 struct usb_endpoint_descriptor
**out
)
1563 const struct usb_host_interface
*iface_desc
;
1564 struct usb_endpoint_descriptor
*endpoint
;
1567 iface_desc
= &intf
->altsetting
[0];
1569 for (i
= 0; i
< iface_desc
->desc
.bNumEndpoints
; ++i
) {
1570 endpoint
= &iface_desc
->endpoint
[i
].desc
;
1572 if (!*in
&& usb_endpoint_is_bulk_in(endpoint
))
1575 if (!*out
&& usb_endpoint_is_bulk_out(endpoint
))
1578 /* use first bulk endpoint for in and out */
1586 static int kvaser_usb_probe(struct usb_interface
*intf
,
1587 const struct usb_device_id
*id
)
1589 struct kvaser_usb
*dev
;
1593 dev
= devm_kzalloc(&intf
->dev
, sizeof(*dev
), GFP_KERNEL
);
1597 err
= kvaser_usb_get_endpoints(intf
, &dev
->bulk_in
, &dev
->bulk_out
);
1599 dev_err(&intf
->dev
, "Cannot get usb endpoint(s)");
1603 dev
->udev
= interface_to_usbdev(intf
);
1605 init_usb_anchor(&dev
->rx_submitted
);
1607 usb_set_intfdata(intf
, dev
);
1609 for (i
= 0; i
< MAX_NET_DEVICES
; i
++)
1610 kvaser_usb_send_simple_msg(dev
, CMD_RESET_CHIP
, i
);
1612 err
= kvaser_usb_get_software_info(dev
);
1615 "Cannot get software infos, error %d\n", err
);
1619 err
= kvaser_usb_get_card_info(dev
);
1622 "Cannot get card infos, error %d\n", err
);
1626 dev_dbg(&intf
->dev
, "Firmware version: %d.%d.%d\n",
1627 ((dev
->fw_version
>> 24) & 0xff),
1628 ((dev
->fw_version
>> 16) & 0xff),
1629 (dev
->fw_version
& 0xffff));
1631 for (i
= 0; i
< dev
->nchannels
; i
++) {
1632 err
= kvaser_usb_init_one(intf
, id
, i
);
1634 kvaser_usb_remove_interfaces(dev
);
1642 static void kvaser_usb_disconnect(struct usb_interface
*intf
)
1644 struct kvaser_usb
*dev
= usb_get_intfdata(intf
);
1646 usb_set_intfdata(intf
, NULL
);
1651 kvaser_usb_remove_interfaces(dev
);
1654 static struct usb_driver kvaser_usb_driver
= {
1655 .name
= "kvaser_usb",
1656 .probe
= kvaser_usb_probe
,
1657 .disconnect
= kvaser_usb_disconnect
,
1658 .id_table
= kvaser_usb_table
,
1661 module_usb_driver(kvaser_usb_driver
);
1663 MODULE_AUTHOR("Olivier Sobrie <olivier@sobrie.be>");
1664 MODULE_DESCRIPTION("CAN driver for Kvaser CAN/USB devices");
1665 MODULE_LICENSE("GPL v2");