1 // SPDX-License-Identifier: GPL-2.0-only
3 * CAN driver for "8 devices" USB2CAN converter
5 * Copyright (C) 2012 Bernd Krumboeck (krumboeck@universalnet.at)
7 * This driver is inspired by the 3.2.0 version of drivers/net/can/usb/ems_usb.c
8 * and drivers/net/can/usb/esd_usb2.c
10 * Many thanks to Gerhard Bertelsmann (info@gerhard-bertelsmann.de)
11 * for testing and fixing this driver. Also many thanks to "8 devices",
12 * who were very cooperative and answered my questions.
15 #include <linux/signal.h>
16 #include <linux/slab.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>
24 #include <linux/can/led.h>
26 /* driver constants */
27 #define MAX_RX_URBS 20
28 #define MAX_TX_URBS 20
29 #define RX_BUFFER_SIZE 64
31 /* vendor and product id */
32 #define USB_8DEV_VENDOR_ID 0x0483
33 #define USB_8DEV_PRODUCT_ID 0x1234
36 enum usb_8dev_endpoint
{
37 USB_8DEV_ENDP_DATA_RX
= 1,
38 USB_8DEV_ENDP_DATA_TX
,
43 /* device CAN clock */
44 #define USB_8DEV_ABP_CLOCK 32000000
47 #define USB_8DEV_SILENT 0x01
48 #define USB_8DEV_LOOPBACK 0x02
49 #define USB_8DEV_DISABLE_AUTO_RESTRANS 0x04
50 #define USB_8DEV_STATUS_FRAME 0x08
58 USB_8DEV_SET_MASK_FILTER
,
60 USB_8DEV_GET_STATISTICS
,
62 USB_8DEV_GET_SOFTW_VER
,
63 USB_8DEV_GET_HARDW_VER
,
64 USB_8DEV_RESET_TIMESTAMP
,
65 USB_8DEV_GET_SOFTW_HARDW_VER
69 #define USB_8DEV_BAUD_MANUAL 0x09
70 #define USB_8DEV_CMD_START 0x11
71 #define USB_8DEV_CMD_END 0x22
73 #define USB_8DEV_CMD_SUCCESS 0
74 #define USB_8DEV_CMD_ERROR 255
76 #define USB_8DEV_CMD_TIMEOUT 1000
79 #define USB_8DEV_DATA_START 0x55
80 #define USB_8DEV_DATA_END 0xAA
82 #define USB_8DEV_TYPE_CAN_FRAME 0
83 #define USB_8DEV_TYPE_ERROR_FRAME 3
85 #define USB_8DEV_EXTID 0x01
86 #define USB_8DEV_RTR 0x02
87 #define USB_8DEV_ERR_FLAG 0x04
90 #define USB_8DEV_STATUSMSG_OK 0x00 /* Normal condition. */
91 #define USB_8DEV_STATUSMSG_OVERRUN 0x01 /* Overrun occured when sending */
92 #define USB_8DEV_STATUSMSG_BUSLIGHT 0x02 /* Error counter has reached 96 */
93 #define USB_8DEV_STATUSMSG_BUSHEAVY 0x03 /* Error count. has reached 128 */
94 #define USB_8DEV_STATUSMSG_BUSOFF 0x04 /* Device is in BUSOFF */
95 #define USB_8DEV_STATUSMSG_STUFF 0x20 /* Stuff Error */
96 #define USB_8DEV_STATUSMSG_FORM 0x21 /* Form Error */
97 #define USB_8DEV_STATUSMSG_ACK 0x23 /* Ack Error */
98 #define USB_8DEV_STATUSMSG_BIT0 0x24 /* Bit1 Error */
99 #define USB_8DEV_STATUSMSG_BIT1 0x25 /* Bit0 Error */
100 #define USB_8DEV_STATUSMSG_CRC 0x27 /* CRC Error */
102 #define USB_8DEV_RP_MASK 0x7F /* Mask for Receive Error Bit */
105 /* table of devices that work with this driver */
106 static const struct usb_device_id usb_8dev_table
[] = {
107 { USB_DEVICE(USB_8DEV_VENDOR_ID
, USB_8DEV_PRODUCT_ID
) },
108 { } /* Terminating entry */
111 MODULE_DEVICE_TABLE(usb
, usb_8dev_table
);
113 struct usb_8dev_tx_urb_context
{
114 struct usb_8dev_priv
*priv
;
120 /* Structure to hold all of our device specific stuff */
121 struct usb_8dev_priv
{
122 struct can_priv can
; /* must be the first member */
124 struct sk_buff
*echo_skb
[MAX_TX_URBS
];
126 struct usb_device
*udev
;
127 struct net_device
*netdev
;
129 atomic_t active_tx_urbs
;
130 struct usb_anchor tx_submitted
;
131 struct usb_8dev_tx_urb_context tx_contexts
[MAX_TX_URBS
];
133 struct usb_anchor rx_submitted
;
135 struct can_berr_counter bec
;
139 struct mutex usb_8dev_cmd_lock
;
144 struct __packed usb_8dev_tx_msg
{
146 u8 flags
; /* RTR and EXT_ID flag */
147 __be32 id
; /* upper 3 bits not used */
148 u8 dlc
; /* data length code 0-8 bytes */
149 u8 data
[8]; /* 64-bit data */
154 struct __packed usb_8dev_rx_msg
{
156 u8 type
; /* frame type */
157 u8 flags
; /* RTR and EXT_ID flag */
158 __be32 id
; /* upper 3 bits not used */
159 u8 dlc
; /* data length code 0-8 bytes */
160 u8 data
[8]; /* 64-bit data */
161 __be32 timestamp
; /* 32-bit timestamp */
166 struct __packed usb_8dev_cmd_msg
{
168 u8 channel
; /* unkown - always 0 */
169 u8 command
; /* command to execute */
170 u8 opt1
; /* optional parameter / return value */
171 u8 opt2
; /* optional parameter 2 */
172 u8 data
[10]; /* optional parameter and data */
176 static int usb_8dev_send_cmd_msg(struct usb_8dev_priv
*priv
, u8
*msg
, int size
)
180 return usb_bulk_msg(priv
->udev
,
181 usb_sndbulkpipe(priv
->udev
, USB_8DEV_ENDP_CMD_TX
),
182 msg
, size
, &actual_length
, USB_8DEV_CMD_TIMEOUT
);
185 static int usb_8dev_wait_cmd_msg(struct usb_8dev_priv
*priv
, u8
*msg
, int size
,
188 return usb_bulk_msg(priv
->udev
,
189 usb_rcvbulkpipe(priv
->udev
, USB_8DEV_ENDP_CMD_RX
),
190 msg
, size
, actual_length
, USB_8DEV_CMD_TIMEOUT
);
193 /* Send command to device and receive result.
194 * Command was successful when opt1 = 0.
196 static int usb_8dev_send_cmd(struct usb_8dev_priv
*priv
,
197 struct usb_8dev_cmd_msg
*out
,
198 struct usb_8dev_cmd_msg
*in
)
202 struct net_device
*netdev
;
204 netdev
= priv
->netdev
;
206 out
->begin
= USB_8DEV_CMD_START
;
207 out
->end
= USB_8DEV_CMD_END
;
209 mutex_lock(&priv
->usb_8dev_cmd_lock
);
211 memcpy(priv
->cmd_msg_buffer
, out
,
212 sizeof(struct usb_8dev_cmd_msg
));
214 err
= usb_8dev_send_cmd_msg(priv
, priv
->cmd_msg_buffer
,
215 sizeof(struct usb_8dev_cmd_msg
));
217 netdev_err(netdev
, "sending command message failed\n");
221 err
= usb_8dev_wait_cmd_msg(priv
, priv
->cmd_msg_buffer
,
222 sizeof(struct usb_8dev_cmd_msg
),
225 netdev_err(netdev
, "no command message answer\n");
229 memcpy(in
, priv
->cmd_msg_buffer
, sizeof(struct usb_8dev_cmd_msg
));
231 if (in
->begin
!= USB_8DEV_CMD_START
|| in
->end
!= USB_8DEV_CMD_END
||
232 num_bytes_read
!= 16 || in
->opt1
!= 0)
236 mutex_unlock(&priv
->usb_8dev_cmd_lock
);
240 /* Send open command to device */
241 static int usb_8dev_cmd_open(struct usb_8dev_priv
*priv
)
243 struct can_bittiming
*bt
= &priv
->can
.bittiming
;
244 struct usb_8dev_cmd_msg outmsg
;
245 struct usb_8dev_cmd_msg inmsg
;
246 u32 ctrlmode
= priv
->can
.ctrlmode
;
247 u32 flags
= USB_8DEV_STATUS_FRAME
;
251 memset(&outmsg
, 0, sizeof(outmsg
));
252 outmsg
.command
= USB_8DEV_OPEN
;
253 outmsg
.opt1
= USB_8DEV_BAUD_MANUAL
;
254 outmsg
.data
[0] = bt
->prop_seg
+ bt
->phase_seg1
;
255 outmsg
.data
[1] = bt
->phase_seg2
;
256 outmsg
.data
[2] = bt
->sjw
;
259 bebrp
= cpu_to_be16((u16
)bt
->brp
);
260 memcpy(&outmsg
.data
[3], &bebrp
, sizeof(bebrp
));
263 if (ctrlmode
& CAN_CTRLMODE_LOOPBACK
)
264 flags
|= USB_8DEV_LOOPBACK
;
265 if (ctrlmode
& CAN_CTRLMODE_LISTENONLY
)
266 flags
|= USB_8DEV_SILENT
;
267 if (ctrlmode
& CAN_CTRLMODE_ONE_SHOT
)
268 flags
|= USB_8DEV_DISABLE_AUTO_RESTRANS
;
270 beflags
= cpu_to_be32(flags
);
271 memcpy(&outmsg
.data
[5], &beflags
, sizeof(beflags
));
273 return usb_8dev_send_cmd(priv
, &outmsg
, &inmsg
);
276 /* Send close command to device */
277 static int usb_8dev_cmd_close(struct usb_8dev_priv
*priv
)
279 struct usb_8dev_cmd_msg inmsg
;
280 struct usb_8dev_cmd_msg outmsg
= {
282 .command
= USB_8DEV_CLOSE
,
287 return usb_8dev_send_cmd(priv
, &outmsg
, &inmsg
);
290 /* Get firmware and hardware version */
291 static int usb_8dev_cmd_version(struct usb_8dev_priv
*priv
, u32
*res
)
293 struct usb_8dev_cmd_msg inmsg
;
294 struct usb_8dev_cmd_msg outmsg
= {
296 .command
= USB_8DEV_GET_SOFTW_HARDW_VER
,
301 int err
= usb_8dev_send_cmd(priv
, &outmsg
, &inmsg
);
305 *res
= be32_to_cpup((__be32
*)inmsg
.data
);
310 /* Set network device mode
312 * Maybe we should leave this function empty, because the device
313 * set mode variable with open command.
315 static int usb_8dev_set_mode(struct net_device
*netdev
, enum can_mode mode
)
317 struct usb_8dev_priv
*priv
= netdev_priv(netdev
);
322 err
= usb_8dev_cmd_open(priv
);
324 netdev_warn(netdev
, "couldn't start device");
334 /* Read error/status frames */
335 static void usb_8dev_rx_err_msg(struct usb_8dev_priv
*priv
,
336 struct usb_8dev_rx_msg
*msg
)
338 struct can_frame
*cf
;
340 struct net_device_stats
*stats
= &priv
->netdev
->stats
;
344 * byte 1: bit 7: Receive Passive
345 * byte 1: bit 0-6: Receive Error Counter
346 * byte 2: Transmit Error Counter
347 * byte 3: Always 0 (maybe reserved for future use)
350 u8 state
= msg
->data
[0];
351 u8 rxerr
= msg
->data
[1] & USB_8DEV_RP_MASK
;
352 u8 txerr
= msg
->data
[2];
356 skb
= alloc_can_err_skb(priv
->netdev
, &cf
);
361 case USB_8DEV_STATUSMSG_OK
:
362 priv
->can
.state
= CAN_STATE_ERROR_ACTIVE
;
363 cf
->can_id
|= CAN_ERR_PROT
;
364 cf
->data
[2] = CAN_ERR_PROT_ACTIVE
;
366 case USB_8DEV_STATUSMSG_BUSOFF
:
367 priv
->can
.state
= CAN_STATE_BUS_OFF
;
368 cf
->can_id
|= CAN_ERR_BUSOFF
;
369 priv
->can
.can_stats
.bus_off
++;
370 can_bus_off(priv
->netdev
);
372 case USB_8DEV_STATUSMSG_OVERRUN
:
373 case USB_8DEV_STATUSMSG_BUSLIGHT
:
374 case USB_8DEV_STATUSMSG_BUSHEAVY
:
375 cf
->can_id
|= CAN_ERR_CRTL
;
378 priv
->can
.state
= CAN_STATE_ERROR_WARNING
;
379 cf
->can_id
|= CAN_ERR_PROT
| CAN_ERR_BUSERROR
;
380 priv
->can
.can_stats
.bus_error
++;
385 case USB_8DEV_STATUSMSG_OK
:
386 case USB_8DEV_STATUSMSG_BUSOFF
:
388 case USB_8DEV_STATUSMSG_ACK
:
389 cf
->can_id
|= CAN_ERR_ACK
;
392 case USB_8DEV_STATUSMSG_CRC
:
393 cf
->data
[3] = CAN_ERR_PROT_LOC_CRC_SEQ
;
396 case USB_8DEV_STATUSMSG_BIT0
:
397 cf
->data
[2] |= CAN_ERR_PROT_BIT0
;
400 case USB_8DEV_STATUSMSG_BIT1
:
401 cf
->data
[2] |= CAN_ERR_PROT_BIT1
;
404 case USB_8DEV_STATUSMSG_FORM
:
405 cf
->data
[2] |= CAN_ERR_PROT_FORM
;
408 case USB_8DEV_STATUSMSG_STUFF
:
409 cf
->data
[2] |= CAN_ERR_PROT_STUFF
;
412 case USB_8DEV_STATUSMSG_OVERRUN
:
413 cf
->data
[1] = CAN_ERR_CRTL_RX_OVERFLOW
;
414 stats
->rx_over_errors
++;
417 case USB_8DEV_STATUSMSG_BUSLIGHT
:
418 priv
->can
.state
= CAN_STATE_ERROR_WARNING
;
419 cf
->data
[1] = (txerr
> rxerr
) ?
420 CAN_ERR_CRTL_TX_WARNING
:
421 CAN_ERR_CRTL_RX_WARNING
;
422 priv
->can
.can_stats
.error_warning
++;
424 case USB_8DEV_STATUSMSG_BUSHEAVY
:
425 priv
->can
.state
= CAN_STATE_ERROR_PASSIVE
;
426 cf
->data
[1] = (txerr
> rxerr
) ?
427 CAN_ERR_CRTL_TX_PASSIVE
:
428 CAN_ERR_CRTL_RX_PASSIVE
;
429 priv
->can
.can_stats
.error_passive
++;
432 netdev_warn(priv
->netdev
,
433 "Unknown status/error message (%d)\n", state
);
438 cf
->data
[2] |= CAN_ERR_PROT_TX
;
448 priv
->bec
.txerr
= txerr
;
449 priv
->bec
.rxerr
= rxerr
;
452 stats
->rx_bytes
+= cf
->can_dlc
;
456 /* Read data and status frames */
457 static void usb_8dev_rx_can_msg(struct usb_8dev_priv
*priv
,
458 struct usb_8dev_rx_msg
*msg
)
460 struct can_frame
*cf
;
462 struct net_device_stats
*stats
= &priv
->netdev
->stats
;
464 if (msg
->type
== USB_8DEV_TYPE_ERROR_FRAME
&&
465 msg
->flags
== USB_8DEV_ERR_FLAG
) {
466 usb_8dev_rx_err_msg(priv
, msg
);
467 } else if (msg
->type
== USB_8DEV_TYPE_CAN_FRAME
) {
468 skb
= alloc_can_skb(priv
->netdev
, &cf
);
472 cf
->can_id
= be32_to_cpu(msg
->id
);
473 cf
->can_dlc
= get_can_dlc(msg
->dlc
& 0xF);
475 if (msg
->flags
& USB_8DEV_EXTID
)
476 cf
->can_id
|= CAN_EFF_FLAG
;
478 if (msg
->flags
& USB_8DEV_RTR
)
479 cf
->can_id
|= CAN_RTR_FLAG
;
481 memcpy(cf
->data
, msg
->data
, cf
->can_dlc
);
484 stats
->rx_bytes
+= cf
->can_dlc
;
487 can_led_event(priv
->netdev
, CAN_LED_EVENT_RX
);
489 netdev_warn(priv
->netdev
, "frame type %d unknown",
495 /* Callback for reading data from device
497 * Check urb status, call read function and resubmit urb read operation.
499 static void usb_8dev_read_bulk_callback(struct urb
*urb
)
501 struct usb_8dev_priv
*priv
= urb
->context
;
502 struct net_device
*netdev
;
506 netdev
= priv
->netdev
;
508 if (!netif_device_present(netdev
))
511 switch (urb
->status
) {
512 case 0: /* success */
522 netdev_info(netdev
, "Rx URB aborted (%d)\n",
527 while (pos
< urb
->actual_length
) {
528 struct usb_8dev_rx_msg
*msg
;
530 if (pos
+ sizeof(struct usb_8dev_rx_msg
) > urb
->actual_length
) {
531 netdev_err(priv
->netdev
, "format error\n");
535 msg
= (struct usb_8dev_rx_msg
*)(urb
->transfer_buffer
+ pos
);
536 usb_8dev_rx_can_msg(priv
, msg
);
538 pos
+= sizeof(struct usb_8dev_rx_msg
);
542 usb_fill_bulk_urb(urb
, priv
->udev
,
543 usb_rcvbulkpipe(priv
->udev
, USB_8DEV_ENDP_DATA_RX
),
544 urb
->transfer_buffer
, RX_BUFFER_SIZE
,
545 usb_8dev_read_bulk_callback
, priv
);
547 retval
= usb_submit_urb(urb
, GFP_ATOMIC
);
549 if (retval
== -ENODEV
)
550 netif_device_detach(netdev
);
553 "failed resubmitting read bulk urb: %d\n", retval
);
556 /* Callback handler for write operations
558 * Free allocated buffers, check transmit status and
559 * calculate statistic.
561 static void usb_8dev_write_bulk_callback(struct urb
*urb
)
563 struct usb_8dev_tx_urb_context
*context
= urb
->context
;
564 struct usb_8dev_priv
*priv
;
565 struct net_device
*netdev
;
569 priv
= context
->priv
;
570 netdev
= priv
->netdev
;
572 /* free up our allocated buffer */
573 usb_free_coherent(urb
->dev
, urb
->transfer_buffer_length
,
574 urb
->transfer_buffer
, urb
->transfer_dma
);
576 atomic_dec(&priv
->active_tx_urbs
);
578 if (!netif_device_present(netdev
))
582 netdev_info(netdev
, "Tx URB aborted (%d)\n",
585 netdev
->stats
.tx_packets
++;
586 netdev
->stats
.tx_bytes
+= context
->dlc
;
588 can_get_echo_skb(netdev
, context
->echo_index
);
590 can_led_event(netdev
, CAN_LED_EVENT_TX
);
592 /* Release context */
593 context
->echo_index
= MAX_TX_URBS
;
595 netif_wake_queue(netdev
);
598 /* Send data to device */
599 static netdev_tx_t
usb_8dev_start_xmit(struct sk_buff
*skb
,
600 struct net_device
*netdev
)
602 struct usb_8dev_priv
*priv
= netdev_priv(netdev
);
603 struct net_device_stats
*stats
= &netdev
->stats
;
604 struct can_frame
*cf
= (struct can_frame
*) skb
->data
;
605 struct usb_8dev_tx_msg
*msg
;
607 struct usb_8dev_tx_urb_context
*context
= NULL
;
610 size_t size
= sizeof(struct usb_8dev_tx_msg
);
612 if (can_dropped_invalid_skb(netdev
, skb
))
615 /* create a URB, and a buffer for it, and copy the data to the URB */
616 urb
= usb_alloc_urb(0, GFP_ATOMIC
);
620 buf
= usb_alloc_coherent(priv
->udev
, size
, GFP_ATOMIC
,
623 netdev_err(netdev
, "No memory left for USB buffer\n");
627 memset(buf
, 0, size
);
629 msg
= (struct usb_8dev_tx_msg
*)buf
;
630 msg
->begin
= USB_8DEV_DATA_START
;
633 if (cf
->can_id
& CAN_RTR_FLAG
)
634 msg
->flags
|= USB_8DEV_RTR
;
636 if (cf
->can_id
& CAN_EFF_FLAG
)
637 msg
->flags
|= USB_8DEV_EXTID
;
639 msg
->id
= cpu_to_be32(cf
->can_id
& CAN_ERR_MASK
);
640 msg
->dlc
= cf
->can_dlc
;
641 memcpy(msg
->data
, cf
->data
, cf
->can_dlc
);
642 msg
->end
= USB_8DEV_DATA_END
;
644 for (i
= 0; i
< MAX_TX_URBS
; i
++) {
645 if (priv
->tx_contexts
[i
].echo_index
== MAX_TX_URBS
) {
646 context
= &priv
->tx_contexts
[i
];
651 /* May never happen! When this happens we'd more URBs in flight as
652 * allowed (MAX_TX_URBS).
657 context
->priv
= priv
;
658 context
->echo_index
= i
;
659 context
->dlc
= cf
->can_dlc
;
661 usb_fill_bulk_urb(urb
, priv
->udev
,
662 usb_sndbulkpipe(priv
->udev
, USB_8DEV_ENDP_DATA_TX
),
663 buf
, size
, usb_8dev_write_bulk_callback
, context
);
664 urb
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
665 usb_anchor_urb(urb
, &priv
->tx_submitted
);
667 can_put_echo_skb(skb
, netdev
, context
->echo_index
);
669 atomic_inc(&priv
->active_tx_urbs
);
671 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
674 else if (atomic_read(&priv
->active_tx_urbs
) >= MAX_TX_URBS
)
675 /* Slow down tx path */
676 netif_stop_queue(netdev
);
678 /* Release our reference to this URB, the USB core will eventually free
686 usb_free_coherent(priv
->udev
, size
, buf
, urb
->transfer_dma
);
689 netdev_warn(netdev
, "couldn't find free context");
691 return NETDEV_TX_BUSY
;
694 can_free_echo_skb(netdev
, context
->echo_index
);
696 usb_unanchor_urb(urb
);
697 usb_free_coherent(priv
->udev
, size
, buf
, urb
->transfer_dma
);
699 atomic_dec(&priv
->active_tx_urbs
);
702 netif_device_detach(netdev
);
704 netdev_warn(netdev
, "failed tx_urb %d\n", err
);
716 static int usb_8dev_get_berr_counter(const struct net_device
*netdev
,
717 struct can_berr_counter
*bec
)
719 struct usb_8dev_priv
*priv
= netdev_priv(netdev
);
721 bec
->txerr
= priv
->bec
.txerr
;
722 bec
->rxerr
= priv
->bec
.rxerr
;
727 /* Start USB device */
728 static int usb_8dev_start(struct usb_8dev_priv
*priv
)
730 struct net_device
*netdev
= priv
->netdev
;
733 for (i
= 0; i
< MAX_RX_URBS
; i
++) {
734 struct urb
*urb
= NULL
;
737 /* create a URB, and a buffer for it */
738 urb
= usb_alloc_urb(0, GFP_KERNEL
);
744 buf
= usb_alloc_coherent(priv
->udev
, RX_BUFFER_SIZE
, GFP_KERNEL
,
747 netdev_err(netdev
, "No memory left for USB buffer\n");
753 usb_fill_bulk_urb(urb
, priv
->udev
,
754 usb_rcvbulkpipe(priv
->udev
,
755 USB_8DEV_ENDP_DATA_RX
),
757 usb_8dev_read_bulk_callback
, priv
);
758 urb
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
759 usb_anchor_urb(urb
, &priv
->rx_submitted
);
761 err
= usb_submit_urb(urb
, GFP_KERNEL
);
763 usb_unanchor_urb(urb
);
764 usb_free_coherent(priv
->udev
, RX_BUFFER_SIZE
, buf
,
770 /* Drop reference, USB core will take care of freeing it */
774 /* Did we submit any URBs */
776 netdev_warn(netdev
, "couldn't setup read URBs\n");
780 /* Warn if we've couldn't transmit all the URBs */
782 netdev_warn(netdev
, "rx performance may be slow\n");
784 err
= usb_8dev_cmd_open(priv
);
788 priv
->can
.state
= CAN_STATE_ERROR_ACTIVE
;
794 netif_device_detach(priv
->netdev
);
796 netdev_warn(netdev
, "couldn't submit control: %d\n", err
);
801 /* Open USB device */
802 static int usb_8dev_open(struct net_device
*netdev
)
804 struct usb_8dev_priv
*priv
= netdev_priv(netdev
);
808 err
= open_candev(netdev
);
812 can_led_event(netdev
, CAN_LED_EVENT_OPEN
);
814 /* finally start device */
815 err
= usb_8dev_start(priv
);
818 netif_device_detach(priv
->netdev
);
820 netdev_warn(netdev
, "couldn't start device: %d\n",
823 close_candev(netdev
);
828 netif_start_queue(netdev
);
833 static void unlink_all_urbs(struct usb_8dev_priv
*priv
)
837 usb_kill_anchored_urbs(&priv
->rx_submitted
);
839 usb_kill_anchored_urbs(&priv
->tx_submitted
);
840 atomic_set(&priv
->active_tx_urbs
, 0);
842 for (i
= 0; i
< MAX_TX_URBS
; i
++)
843 priv
->tx_contexts
[i
].echo_index
= MAX_TX_URBS
;
846 /* Close USB device */
847 static int usb_8dev_close(struct net_device
*netdev
)
849 struct usb_8dev_priv
*priv
= netdev_priv(netdev
);
852 /* Send CLOSE command to CAN controller */
853 err
= usb_8dev_cmd_close(priv
);
855 netdev_warn(netdev
, "couldn't stop device");
857 priv
->can
.state
= CAN_STATE_STOPPED
;
859 netif_stop_queue(netdev
);
862 unlink_all_urbs(priv
);
864 close_candev(netdev
);
866 can_led_event(netdev
, CAN_LED_EVENT_STOP
);
871 static const struct net_device_ops usb_8dev_netdev_ops
= {
872 .ndo_open
= usb_8dev_open
,
873 .ndo_stop
= usb_8dev_close
,
874 .ndo_start_xmit
= usb_8dev_start_xmit
,
875 .ndo_change_mtu
= can_change_mtu
,
878 static const struct can_bittiming_const usb_8dev_bittiming_const
= {
892 * Check device and firmware.
893 * Set supported modes and bittiming constants.
894 * Allocate some memory.
896 static int usb_8dev_probe(struct usb_interface
*intf
,
897 const struct usb_device_id
*id
)
899 struct net_device
*netdev
;
900 struct usb_8dev_priv
*priv
;
901 int i
, err
= -ENOMEM
;
904 struct usb_device
*usbdev
= interface_to_usbdev(intf
);
906 /* product id looks strange, better we also check iProduct string */
907 if (usb_string(usbdev
, usbdev
->descriptor
.iProduct
, buf
,
908 sizeof(buf
)) > 0 && strcmp(buf
, "USB2CAN converter")) {
909 dev_info(&usbdev
->dev
, "ignoring: not an USB2CAN converter\n");
913 netdev
= alloc_candev(sizeof(struct usb_8dev_priv
), MAX_TX_URBS
);
915 dev_err(&intf
->dev
, "Couldn't alloc candev\n");
919 priv
= netdev_priv(netdev
);
922 priv
->netdev
= netdev
;
924 priv
->can
.state
= CAN_STATE_STOPPED
;
925 priv
->can
.clock
.freq
= USB_8DEV_ABP_CLOCK
;
926 priv
->can
.bittiming_const
= &usb_8dev_bittiming_const
;
927 priv
->can
.do_set_mode
= usb_8dev_set_mode
;
928 priv
->can
.do_get_berr_counter
= usb_8dev_get_berr_counter
;
929 priv
->can
.ctrlmode_supported
= CAN_CTRLMODE_LOOPBACK
|
930 CAN_CTRLMODE_LISTENONLY
|
931 CAN_CTRLMODE_ONE_SHOT
;
933 netdev
->netdev_ops
= &usb_8dev_netdev_ops
;
935 netdev
->flags
|= IFF_ECHO
; /* we support local echo */
937 init_usb_anchor(&priv
->rx_submitted
);
939 init_usb_anchor(&priv
->tx_submitted
);
940 atomic_set(&priv
->active_tx_urbs
, 0);
942 for (i
= 0; i
< MAX_TX_URBS
; i
++)
943 priv
->tx_contexts
[i
].echo_index
= MAX_TX_URBS
;
945 priv
->cmd_msg_buffer
= devm_kzalloc(&intf
->dev
, sizeof(struct usb_8dev_cmd_msg
),
947 if (!priv
->cmd_msg_buffer
)
950 usb_set_intfdata(intf
, priv
);
952 SET_NETDEV_DEV(netdev
, &intf
->dev
);
954 mutex_init(&priv
->usb_8dev_cmd_lock
);
956 err
= register_candev(netdev
);
959 "couldn't register CAN device: %d\n", err
);
963 err
= usb_8dev_cmd_version(priv
, &version
);
965 netdev_err(netdev
, "can't get firmware version\n");
966 goto cleanup_unregister_candev
;
969 "firmware: %d.%d, hardware: %d.%d\n",
970 (version
>>24) & 0xff, (version
>>16) & 0xff,
971 (version
>>8) & 0xff, version
& 0xff);
974 devm_can_led_init(netdev
);
978 cleanup_unregister_candev
:
979 unregister_netdev(priv
->netdev
);
988 /* Called by the usb core when driver is unloaded or device is removed */
989 static void usb_8dev_disconnect(struct usb_interface
*intf
)
991 struct usb_8dev_priv
*priv
= usb_get_intfdata(intf
);
993 usb_set_intfdata(intf
, NULL
);
996 netdev_info(priv
->netdev
, "device disconnected\n");
998 unregister_netdev(priv
->netdev
);
999 unlink_all_urbs(priv
);
1000 free_candev(priv
->netdev
);
1005 static struct usb_driver usb_8dev_driver
= {
1007 .probe
= usb_8dev_probe
,
1008 .disconnect
= usb_8dev_disconnect
,
1009 .id_table
= usb_8dev_table
,
1012 module_usb_driver(usb_8dev_driver
);
1014 MODULE_AUTHOR("Bernd Krumboeck <krumboeck@universalnet.at>");
1015 MODULE_DESCRIPTION("CAN driver for 8 devices USB2CAN interfaces");
1016 MODULE_LICENSE("GPL v2");