2 * CAN driver for "8 devices" USB2CAN converter
4 * Copyright (C) 2012 Bernd Krumboeck (krumboeck@universalnet.at)
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published
8 * by the Free Software Foundation; version 2 of the License.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
18 * This driver is inspired by the 3.2.0 version of drivers/net/can/usb/ems_usb.c
19 * and drivers/net/can/usb/esd_usb2.c
21 * Many thanks to Gerhard Bertelsmann (info@gerhard-bertelsmann.de)
22 * for testing and fixing this driver. Also many thanks to "8 devices",
23 * who were very cooperative and answered my questions.
26 #include <linux/init.h>
27 #include <linux/signal.h>
28 #include <linux/slab.h>
29 #include <linux/module.h>
30 #include <linux/netdevice.h>
31 #include <linux/usb.h>
33 #include <linux/can.h>
34 #include <linux/can/dev.h>
35 #include <linux/can/error.h>
36 #include <linux/can/led.h>
38 /* driver constants */
39 #define MAX_RX_URBS 20
40 #define MAX_TX_URBS 20
41 #define RX_BUFFER_SIZE 64
43 /* vendor and product id */
44 #define USB_8DEV_VENDOR_ID 0x0483
45 #define USB_8DEV_PRODUCT_ID 0x1234
48 enum usb_8dev_endpoint
{
49 USB_8DEV_ENDP_DATA_RX
= 1,
50 USB_8DEV_ENDP_DATA_TX
,
55 /* device CAN clock */
56 #define USB_8DEV_ABP_CLOCK 32000000
59 #define USB_8DEV_SILENT 0x01
60 #define USB_8DEV_LOOPBACK 0x02
61 #define USB_8DEV_DISABLE_AUTO_RESTRANS 0x04
62 #define USB_8DEV_STATUS_FRAME 0x08
70 USB_8DEV_SET_MASK_FILTER
,
72 USB_8DEV_GET_STATISTICS
,
74 USB_8DEV_GET_SOFTW_VER
,
75 USB_8DEV_GET_HARDW_VER
,
76 USB_8DEV_RESET_TIMESTAMP
,
77 USB_8DEV_GET_SOFTW_HARDW_VER
81 #define USB_8DEV_BAUD_MANUAL 0x09
82 #define USB_8DEV_CMD_START 0x11
83 #define USB_8DEV_CMD_END 0x22
85 #define USB_8DEV_CMD_SUCCESS 0
86 #define USB_8DEV_CMD_ERROR 255
88 #define USB_8DEV_CMD_TIMEOUT 1000
91 #define USB_8DEV_DATA_START 0x55
92 #define USB_8DEV_DATA_END 0xAA
94 #define USB_8DEV_TYPE_CAN_FRAME 0
95 #define USB_8DEV_TYPE_ERROR_FRAME 3
97 #define USB_8DEV_EXTID 0x01
98 #define USB_8DEV_RTR 0x02
99 #define USB_8DEV_ERR_FLAG 0x04
102 #define USB_8DEV_STATUSMSG_OK 0x00 /* Normal condition. */
103 #define USB_8DEV_STATUSMSG_OVERRUN 0x01 /* Overrun occured when sending */
104 #define USB_8DEV_STATUSMSG_BUSLIGHT 0x02 /* Error counter has reached 96 */
105 #define USB_8DEV_STATUSMSG_BUSHEAVY 0x03 /* Error count. has reached 128 */
106 #define USB_8DEV_STATUSMSG_BUSOFF 0x04 /* Device is in BUSOFF */
107 #define USB_8DEV_STATUSMSG_STUFF 0x20 /* Stuff Error */
108 #define USB_8DEV_STATUSMSG_FORM 0x21 /* Form Error */
109 #define USB_8DEV_STATUSMSG_ACK 0x23 /* Ack Error */
110 #define USB_8DEV_STATUSMSG_BIT0 0x24 /* Bit1 Error */
111 #define USB_8DEV_STATUSMSG_BIT1 0x25 /* Bit0 Error */
112 #define USB_8DEV_STATUSMSG_CRC 0x27 /* CRC Error */
114 #define USB_8DEV_RP_MASK 0x7F /* Mask for Receive Error Bit */
117 /* table of devices that work with this driver */
118 static const struct usb_device_id usb_8dev_table
[] = {
119 { USB_DEVICE(USB_8DEV_VENDOR_ID
, USB_8DEV_PRODUCT_ID
) },
120 { } /* Terminating entry */
123 MODULE_DEVICE_TABLE(usb
, usb_8dev_table
);
125 struct usb_8dev_tx_urb_context
{
126 struct usb_8dev_priv
*priv
;
132 /* Structure to hold all of our device specific stuff */
133 struct usb_8dev_priv
{
134 struct can_priv can
; /* must be the first member */
136 struct sk_buff
*echo_skb
[MAX_TX_URBS
];
138 struct usb_device
*udev
;
139 struct net_device
*netdev
;
141 atomic_t active_tx_urbs
;
142 struct usb_anchor tx_submitted
;
143 struct usb_8dev_tx_urb_context tx_contexts
[MAX_TX_URBS
];
145 struct usb_anchor rx_submitted
;
147 struct can_berr_counter bec
;
151 struct mutex usb_8dev_cmd_lock
;
156 struct __packed usb_8dev_tx_msg
{
158 u8 flags
; /* RTR and EXT_ID flag */
159 __be32 id
; /* upper 3 bits not used */
160 u8 dlc
; /* data length code 0-8 bytes */
161 u8 data
[8]; /* 64-bit data */
166 struct __packed usb_8dev_rx_msg
{
168 u8 type
; /* frame type */
169 u8 flags
; /* RTR and EXT_ID flag */
170 __be32 id
; /* upper 3 bits not used */
171 u8 dlc
; /* data length code 0-8 bytes */
172 u8 data
[8]; /* 64-bit data */
173 __be32 timestamp
; /* 32-bit timestamp */
178 struct __packed usb_8dev_cmd_msg
{
180 u8 channel
; /* unkown - always 0 */
181 u8 command
; /* command to execute */
182 u8 opt1
; /* optional parameter / return value */
183 u8 opt2
; /* optional parameter 2 */
184 u8 data
[10]; /* optional parameter and data */
188 static int usb_8dev_send_cmd_msg(struct usb_8dev_priv
*priv
, u8
*msg
, int size
)
192 return usb_bulk_msg(priv
->udev
,
193 usb_sndbulkpipe(priv
->udev
, USB_8DEV_ENDP_CMD_TX
),
194 msg
, size
, &actual_length
, USB_8DEV_CMD_TIMEOUT
);
197 static int usb_8dev_wait_cmd_msg(struct usb_8dev_priv
*priv
, u8
*msg
, int size
,
200 return usb_bulk_msg(priv
->udev
,
201 usb_rcvbulkpipe(priv
->udev
, USB_8DEV_ENDP_CMD_RX
),
202 msg
, size
, actual_length
, USB_8DEV_CMD_TIMEOUT
);
205 /* Send command to device and receive result.
206 * Command was successful when opt1 = 0.
208 static int usb_8dev_send_cmd(struct usb_8dev_priv
*priv
,
209 struct usb_8dev_cmd_msg
*out
,
210 struct usb_8dev_cmd_msg
*in
)
214 struct net_device
*netdev
;
216 netdev
= priv
->netdev
;
218 out
->begin
= USB_8DEV_CMD_START
;
219 out
->end
= USB_8DEV_CMD_END
;
221 mutex_lock(&priv
->usb_8dev_cmd_lock
);
223 memcpy(priv
->cmd_msg_buffer
, out
,
224 sizeof(struct usb_8dev_cmd_msg
));
226 err
= usb_8dev_send_cmd_msg(priv
, priv
->cmd_msg_buffer
,
227 sizeof(struct usb_8dev_cmd_msg
));
229 netdev_err(netdev
, "sending command message failed\n");
233 err
= usb_8dev_wait_cmd_msg(priv
, priv
->cmd_msg_buffer
,
234 sizeof(struct usb_8dev_cmd_msg
),
237 netdev_err(netdev
, "no command message answer\n");
241 memcpy(in
, priv
->cmd_msg_buffer
, sizeof(struct usb_8dev_cmd_msg
));
243 if (in
->begin
!= USB_8DEV_CMD_START
|| in
->end
!= USB_8DEV_CMD_END
||
244 num_bytes_read
!= 16 || in
->opt1
!= 0)
248 mutex_unlock(&priv
->usb_8dev_cmd_lock
);
252 /* Send open command to device */
253 static int usb_8dev_cmd_open(struct usb_8dev_priv
*priv
)
255 struct can_bittiming
*bt
= &priv
->can
.bittiming
;
256 struct usb_8dev_cmd_msg outmsg
;
257 struct usb_8dev_cmd_msg inmsg
;
258 u32 ctrlmode
= priv
->can
.ctrlmode
;
259 u32 flags
= USB_8DEV_STATUS_FRAME
;
263 memset(&outmsg
, 0, sizeof(outmsg
));
264 outmsg
.command
= USB_8DEV_OPEN
;
265 outmsg
.opt1
= USB_8DEV_BAUD_MANUAL
;
266 outmsg
.data
[0] = bt
->prop_seg
+ bt
->phase_seg1
;
267 outmsg
.data
[1] = bt
->phase_seg2
;
268 outmsg
.data
[2] = bt
->sjw
;
271 bebrp
= cpu_to_be16((u16
)bt
->brp
);
272 memcpy(&outmsg
.data
[3], &bebrp
, sizeof(bebrp
));
275 if (ctrlmode
& CAN_CTRLMODE_LOOPBACK
)
276 flags
|= USB_8DEV_LOOPBACK
;
277 if (ctrlmode
& CAN_CTRLMODE_LISTENONLY
)
278 flags
|= USB_8DEV_SILENT
;
279 if (ctrlmode
& CAN_CTRLMODE_ONE_SHOT
)
280 flags
|= USB_8DEV_DISABLE_AUTO_RESTRANS
;
282 beflags
= cpu_to_be32(flags
);
283 memcpy(&outmsg
.data
[5], &beflags
, sizeof(beflags
));
285 return usb_8dev_send_cmd(priv
, &outmsg
, &inmsg
);
288 /* Send close command to device */
289 static int usb_8dev_cmd_close(struct usb_8dev_priv
*priv
)
291 struct usb_8dev_cmd_msg inmsg
;
292 struct usb_8dev_cmd_msg outmsg
= {
294 .command
= USB_8DEV_CLOSE
,
299 return usb_8dev_send_cmd(priv
, &outmsg
, &inmsg
);
302 /* Get firmware and hardware version */
303 static int usb_8dev_cmd_version(struct usb_8dev_priv
*priv
, u32
*res
)
305 struct usb_8dev_cmd_msg inmsg
;
306 struct usb_8dev_cmd_msg outmsg
= {
308 .command
= USB_8DEV_GET_SOFTW_HARDW_VER
,
313 int err
= usb_8dev_send_cmd(priv
, &outmsg
, &inmsg
);
317 *res
= be32_to_cpup((__be32
*)inmsg
.data
);
322 /* Set network device mode
324 * Maybe we should leave this function empty, because the device
325 * set mode variable with open command.
327 static int usb_8dev_set_mode(struct net_device
*netdev
, enum can_mode mode
)
329 struct usb_8dev_priv
*priv
= netdev_priv(netdev
);
334 err
= usb_8dev_cmd_open(priv
);
336 netdev_warn(netdev
, "couldn't start device");
346 /* Read error/status frames */
347 static void usb_8dev_rx_err_msg(struct usb_8dev_priv
*priv
,
348 struct usb_8dev_rx_msg
*msg
)
350 struct can_frame
*cf
;
352 struct net_device_stats
*stats
= &priv
->netdev
->stats
;
356 * byte 1: bit 7: Receive Passive
357 * byte 1: bit 0-6: Receive Error Counter
358 * byte 2: Transmit Error Counter
359 * byte 3: Always 0 (maybe reserved for future use)
362 u8 state
= msg
->data
[0];
363 u8 rxerr
= msg
->data
[1] & USB_8DEV_RP_MASK
;
364 u8 txerr
= msg
->data
[2];
368 skb
= alloc_can_err_skb(priv
->netdev
, &cf
);
373 case USB_8DEV_STATUSMSG_OK
:
374 priv
->can
.state
= CAN_STATE_ERROR_ACTIVE
;
375 cf
->can_id
|= CAN_ERR_PROT
;
376 cf
->data
[2] = CAN_ERR_PROT_ACTIVE
;
378 case USB_8DEV_STATUSMSG_BUSOFF
:
379 priv
->can
.state
= CAN_STATE_BUS_OFF
;
380 cf
->can_id
|= CAN_ERR_BUSOFF
;
381 can_bus_off(priv
->netdev
);
383 case USB_8DEV_STATUSMSG_OVERRUN
:
384 case USB_8DEV_STATUSMSG_BUSLIGHT
:
385 case USB_8DEV_STATUSMSG_BUSHEAVY
:
386 cf
->can_id
|= CAN_ERR_CRTL
;
389 priv
->can
.state
= CAN_STATE_ERROR_WARNING
;
390 cf
->can_id
|= CAN_ERR_PROT
| CAN_ERR_BUSERROR
;
391 priv
->can
.can_stats
.bus_error
++;
396 case USB_8DEV_STATUSMSG_OK
:
397 case USB_8DEV_STATUSMSG_BUSOFF
:
399 case USB_8DEV_STATUSMSG_ACK
:
400 cf
->can_id
|= CAN_ERR_ACK
;
403 case USB_8DEV_STATUSMSG_CRC
:
404 cf
->data
[2] |= CAN_ERR_PROT_UNSPEC
;
405 cf
->data
[3] |= CAN_ERR_PROT_LOC_CRC_SEQ
|
406 CAN_ERR_PROT_LOC_CRC_DEL
;
409 case USB_8DEV_STATUSMSG_BIT0
:
410 cf
->data
[2] |= CAN_ERR_PROT_BIT0
;
413 case USB_8DEV_STATUSMSG_BIT1
:
414 cf
->data
[2] |= CAN_ERR_PROT_BIT1
;
417 case USB_8DEV_STATUSMSG_FORM
:
418 cf
->data
[2] |= CAN_ERR_PROT_FORM
;
421 case USB_8DEV_STATUSMSG_STUFF
:
422 cf
->data
[2] |= CAN_ERR_PROT_STUFF
;
425 case USB_8DEV_STATUSMSG_OVERRUN
:
426 cf
->data
[1] = CAN_ERR_CRTL_RX_OVERFLOW
;
427 stats
->rx_over_errors
++;
430 case USB_8DEV_STATUSMSG_BUSLIGHT
:
431 priv
->can
.state
= CAN_STATE_ERROR_WARNING
;
432 cf
->data
[1] = (txerr
> rxerr
) ?
433 CAN_ERR_CRTL_TX_WARNING
:
434 CAN_ERR_CRTL_RX_WARNING
;
435 priv
->can
.can_stats
.error_warning
++;
437 case USB_8DEV_STATUSMSG_BUSHEAVY
:
438 priv
->can
.state
= CAN_STATE_ERROR_PASSIVE
;
439 cf
->data
[1] = (txerr
> rxerr
) ?
440 CAN_ERR_CRTL_TX_PASSIVE
:
441 CAN_ERR_CRTL_RX_PASSIVE
;
442 priv
->can
.can_stats
.error_passive
++;
445 netdev_warn(priv
->netdev
,
446 "Unknown status/error message (%d)\n", state
);
451 cf
->data
[2] |= CAN_ERR_PROT_TX
;
461 priv
->bec
.txerr
= txerr
;
462 priv
->bec
.rxerr
= rxerr
;
467 stats
->rx_bytes
+= cf
->can_dlc
;
470 /* Read data and status frames */
471 static void usb_8dev_rx_can_msg(struct usb_8dev_priv
*priv
,
472 struct usb_8dev_rx_msg
*msg
)
474 struct can_frame
*cf
;
476 struct net_device_stats
*stats
= &priv
->netdev
->stats
;
478 if (msg
->type
== USB_8DEV_TYPE_ERROR_FRAME
&&
479 msg
->flags
== USB_8DEV_ERR_FLAG
) {
480 usb_8dev_rx_err_msg(priv
, msg
);
481 } else if (msg
->type
== USB_8DEV_TYPE_CAN_FRAME
) {
482 skb
= alloc_can_skb(priv
->netdev
, &cf
);
486 cf
->can_id
= be32_to_cpu(msg
->id
);
487 cf
->can_dlc
= get_can_dlc(msg
->dlc
& 0xF);
489 if (msg
->flags
& USB_8DEV_EXTID
)
490 cf
->can_id
|= CAN_EFF_FLAG
;
492 if (msg
->flags
& USB_8DEV_RTR
)
493 cf
->can_id
|= CAN_RTR_FLAG
;
495 memcpy(cf
->data
, msg
->data
, cf
->can_dlc
);
500 stats
->rx_bytes
+= cf
->can_dlc
;
502 can_led_event(priv
->netdev
, CAN_LED_EVENT_RX
);
504 netdev_warn(priv
->netdev
, "frame type %d unknown",
510 /* Callback for reading data from device
512 * Check urb status, call read function and resubmit urb read operation.
514 static void usb_8dev_read_bulk_callback(struct urb
*urb
)
516 struct usb_8dev_priv
*priv
= urb
->context
;
517 struct net_device
*netdev
;
521 netdev
= priv
->netdev
;
523 if (!netif_device_present(netdev
))
526 switch (urb
->status
) {
527 case 0: /* success */
535 netdev_info(netdev
, "Rx URB aborted (%d)\n",
540 while (pos
< urb
->actual_length
) {
541 struct usb_8dev_rx_msg
*msg
;
543 if (pos
+ sizeof(struct usb_8dev_rx_msg
) > urb
->actual_length
) {
544 netdev_err(priv
->netdev
, "format error\n");
548 msg
= (struct usb_8dev_rx_msg
*)(urb
->transfer_buffer
+ pos
);
549 usb_8dev_rx_can_msg(priv
, msg
);
551 pos
+= sizeof(struct usb_8dev_rx_msg
);
555 usb_fill_bulk_urb(urb
, priv
->udev
,
556 usb_rcvbulkpipe(priv
->udev
, USB_8DEV_ENDP_DATA_RX
),
557 urb
->transfer_buffer
, RX_BUFFER_SIZE
,
558 usb_8dev_read_bulk_callback
, priv
);
560 retval
= usb_submit_urb(urb
, GFP_ATOMIC
);
562 if (retval
== -ENODEV
)
563 netif_device_detach(netdev
);
566 "failed resubmitting read bulk urb: %d\n", retval
);
569 /* Callback handler for write operations
571 * Free allocated buffers, check transmit status and
572 * calculate statistic.
574 static void usb_8dev_write_bulk_callback(struct urb
*urb
)
576 struct usb_8dev_tx_urb_context
*context
= urb
->context
;
577 struct usb_8dev_priv
*priv
;
578 struct net_device
*netdev
;
582 priv
= context
->priv
;
583 netdev
= priv
->netdev
;
585 /* free up our allocated buffer */
586 usb_free_coherent(urb
->dev
, urb
->transfer_buffer_length
,
587 urb
->transfer_buffer
, urb
->transfer_dma
);
589 atomic_dec(&priv
->active_tx_urbs
);
591 if (!netif_device_present(netdev
))
595 netdev_info(netdev
, "Tx URB aborted (%d)\n",
598 netdev
->stats
.tx_packets
++;
599 netdev
->stats
.tx_bytes
+= context
->dlc
;
601 can_get_echo_skb(netdev
, context
->echo_index
);
603 can_led_event(netdev
, CAN_LED_EVENT_TX
);
605 /* Release context */
606 context
->echo_index
= MAX_TX_URBS
;
608 netif_wake_queue(netdev
);
611 /* Send data to device */
612 static netdev_tx_t
usb_8dev_start_xmit(struct sk_buff
*skb
,
613 struct net_device
*netdev
)
615 struct usb_8dev_priv
*priv
= netdev_priv(netdev
);
616 struct net_device_stats
*stats
= &netdev
->stats
;
617 struct can_frame
*cf
= (struct can_frame
*) skb
->data
;
618 struct usb_8dev_tx_msg
*msg
;
620 struct usb_8dev_tx_urb_context
*context
= NULL
;
623 size_t size
= sizeof(struct usb_8dev_tx_msg
);
625 if (can_dropped_invalid_skb(netdev
, skb
))
628 /* create a URB, and a buffer for it, and copy the data to the URB */
629 urb
= usb_alloc_urb(0, GFP_ATOMIC
);
631 netdev_err(netdev
, "No memory left for URBs\n");
635 buf
= usb_alloc_coherent(priv
->udev
, size
, GFP_ATOMIC
,
638 netdev_err(netdev
, "No memory left for USB buffer\n");
642 memset(buf
, 0, size
);
644 msg
= (struct usb_8dev_tx_msg
*)buf
;
645 msg
->begin
= USB_8DEV_DATA_START
;
648 if (cf
->can_id
& CAN_RTR_FLAG
)
649 msg
->flags
|= USB_8DEV_RTR
;
651 if (cf
->can_id
& CAN_EFF_FLAG
)
652 msg
->flags
|= USB_8DEV_EXTID
;
654 msg
->id
= cpu_to_be32(cf
->can_id
& CAN_ERR_MASK
);
655 msg
->dlc
= cf
->can_dlc
;
656 memcpy(msg
->data
, cf
->data
, cf
->can_dlc
);
657 msg
->end
= USB_8DEV_DATA_END
;
659 for (i
= 0; i
< MAX_TX_URBS
; i
++) {
660 if (priv
->tx_contexts
[i
].echo_index
== MAX_TX_URBS
) {
661 context
= &priv
->tx_contexts
[i
];
666 /* May never happen! When this happens we'd more URBs in flight as
667 * allowed (MAX_TX_URBS).
672 context
->priv
= priv
;
673 context
->echo_index
= i
;
674 context
->dlc
= cf
->can_dlc
;
676 usb_fill_bulk_urb(urb
, priv
->udev
,
677 usb_sndbulkpipe(priv
->udev
, USB_8DEV_ENDP_DATA_TX
),
678 buf
, size
, usb_8dev_write_bulk_callback
, context
);
679 urb
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
680 usb_anchor_urb(urb
, &priv
->tx_submitted
);
682 can_put_echo_skb(skb
, netdev
, context
->echo_index
);
684 atomic_inc(&priv
->active_tx_urbs
);
686 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
689 else if (atomic_read(&priv
->active_tx_urbs
) >= MAX_TX_URBS
)
690 /* Slow down tx path */
691 netif_stop_queue(netdev
);
693 /* Release our reference to this URB, the USB core will eventually free
701 usb_unanchor_urb(urb
);
702 usb_free_coherent(priv
->udev
, size
, buf
, urb
->transfer_dma
);
704 netdev_warn(netdev
, "couldn't find free context");
706 return NETDEV_TX_BUSY
;
709 can_free_echo_skb(netdev
, context
->echo_index
);
711 usb_unanchor_urb(urb
);
712 usb_free_coherent(priv
->udev
, size
, buf
, urb
->transfer_dma
);
714 atomic_dec(&priv
->active_tx_urbs
);
717 netif_device_detach(netdev
);
719 netdev_warn(netdev
, "failed tx_urb %d\n", err
);
731 static int usb_8dev_get_berr_counter(const struct net_device
*netdev
,
732 struct can_berr_counter
*bec
)
734 struct usb_8dev_priv
*priv
= netdev_priv(netdev
);
736 bec
->txerr
= priv
->bec
.txerr
;
737 bec
->rxerr
= priv
->bec
.rxerr
;
742 /* Start USB device */
743 static int usb_8dev_start(struct usb_8dev_priv
*priv
)
745 struct net_device
*netdev
= priv
->netdev
;
748 for (i
= 0; i
< MAX_RX_URBS
; i
++) {
749 struct urb
*urb
= NULL
;
752 /* create a URB, and a buffer for it */
753 urb
= usb_alloc_urb(0, GFP_KERNEL
);
755 netdev_err(netdev
, "No memory left for URBs\n");
760 buf
= usb_alloc_coherent(priv
->udev
, RX_BUFFER_SIZE
, GFP_KERNEL
,
763 netdev_err(netdev
, "No memory left for USB buffer\n");
769 usb_fill_bulk_urb(urb
, priv
->udev
,
770 usb_rcvbulkpipe(priv
->udev
,
771 USB_8DEV_ENDP_DATA_RX
),
773 usb_8dev_read_bulk_callback
, priv
);
774 urb
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
775 usb_anchor_urb(urb
, &priv
->rx_submitted
);
777 err
= usb_submit_urb(urb
, GFP_KERNEL
);
779 usb_unanchor_urb(urb
);
780 usb_free_coherent(priv
->udev
, RX_BUFFER_SIZE
, buf
,
786 /* Drop reference, USB core will take care of freeing it */
790 /* Did we submit any URBs */
792 netdev_warn(netdev
, "couldn't setup read URBs\n");
796 /* Warn if we've couldn't transmit all the URBs */
798 netdev_warn(netdev
, "rx performance may be slow\n");
800 err
= usb_8dev_cmd_open(priv
);
804 priv
->can
.state
= CAN_STATE_ERROR_ACTIVE
;
810 netif_device_detach(priv
->netdev
);
812 netdev_warn(netdev
, "couldn't submit control: %d\n", err
);
817 /* Open USB device */
818 static int usb_8dev_open(struct net_device
*netdev
)
820 struct usb_8dev_priv
*priv
= netdev_priv(netdev
);
824 err
= open_candev(netdev
);
828 can_led_event(netdev
, CAN_LED_EVENT_OPEN
);
830 /* finally start device */
831 err
= usb_8dev_start(priv
);
834 netif_device_detach(priv
->netdev
);
836 netdev_warn(netdev
, "couldn't start device: %d\n",
839 close_candev(netdev
);
844 netif_start_queue(netdev
);
849 static void unlink_all_urbs(struct usb_8dev_priv
*priv
)
853 usb_kill_anchored_urbs(&priv
->rx_submitted
);
855 usb_kill_anchored_urbs(&priv
->tx_submitted
);
856 atomic_set(&priv
->active_tx_urbs
, 0);
858 for (i
= 0; i
< MAX_TX_URBS
; i
++)
859 priv
->tx_contexts
[i
].echo_index
= MAX_TX_URBS
;
862 /* Close USB device */
863 static int usb_8dev_close(struct net_device
*netdev
)
865 struct usb_8dev_priv
*priv
= netdev_priv(netdev
);
868 /* Send CLOSE command to CAN controller */
869 err
= usb_8dev_cmd_close(priv
);
871 netdev_warn(netdev
, "couldn't stop device");
873 priv
->can
.state
= CAN_STATE_STOPPED
;
875 netif_stop_queue(netdev
);
878 unlink_all_urbs(priv
);
880 close_candev(netdev
);
882 can_led_event(netdev
, CAN_LED_EVENT_STOP
);
887 static const struct net_device_ops usb_8dev_netdev_ops
= {
888 .ndo_open
= usb_8dev_open
,
889 .ndo_stop
= usb_8dev_close
,
890 .ndo_start_xmit
= usb_8dev_start_xmit
,
893 static const struct can_bittiming_const usb_8dev_bittiming_const
= {
907 * Check device and firmware.
908 * Set supported modes and bittiming constants.
909 * Allocate some memory.
911 static int usb_8dev_probe(struct usb_interface
*intf
,
912 const struct usb_device_id
*id
)
914 struct net_device
*netdev
;
915 struct usb_8dev_priv
*priv
;
916 int i
, err
= -ENOMEM
;
919 struct usb_device
*usbdev
= interface_to_usbdev(intf
);
921 /* product id looks strange, better we also check iProduct string */
922 if (usb_string(usbdev
, usbdev
->descriptor
.iProduct
, buf
,
923 sizeof(buf
)) > 0 && strcmp(buf
, "USB2CAN converter")) {
924 dev_info(&usbdev
->dev
, "ignoring: not an USB2CAN converter\n");
928 netdev
= alloc_candev(sizeof(struct usb_8dev_priv
), MAX_TX_URBS
);
930 dev_err(&intf
->dev
, "Couldn't alloc candev\n");
934 priv
= netdev_priv(netdev
);
937 priv
->netdev
= netdev
;
939 priv
->can
.state
= CAN_STATE_STOPPED
;
940 priv
->can
.clock
.freq
= USB_8DEV_ABP_CLOCK
;
941 priv
->can
.bittiming_const
= &usb_8dev_bittiming_const
;
942 priv
->can
.do_set_mode
= usb_8dev_set_mode
;
943 priv
->can
.do_get_berr_counter
= usb_8dev_get_berr_counter
;
944 priv
->can
.ctrlmode_supported
= CAN_CTRLMODE_LOOPBACK
|
945 CAN_CTRLMODE_LISTENONLY
|
946 CAN_CTRLMODE_ONE_SHOT
;
948 netdev
->netdev_ops
= &usb_8dev_netdev_ops
;
950 netdev
->flags
|= IFF_ECHO
; /* we support local echo */
952 init_usb_anchor(&priv
->rx_submitted
);
954 init_usb_anchor(&priv
->tx_submitted
);
955 atomic_set(&priv
->active_tx_urbs
, 0);
957 for (i
= 0; i
< MAX_TX_URBS
; i
++)
958 priv
->tx_contexts
[i
].echo_index
= MAX_TX_URBS
;
960 priv
->cmd_msg_buffer
= kzalloc(sizeof(struct usb_8dev_cmd_msg
),
962 if (!priv
->cmd_msg_buffer
)
965 usb_set_intfdata(intf
, priv
);
967 SET_NETDEV_DEV(netdev
, &intf
->dev
);
969 mutex_init(&priv
->usb_8dev_cmd_lock
);
971 err
= register_candev(netdev
);
974 "couldn't register CAN device: %d\n", err
);
975 goto cleanup_cmd_msg_buffer
;
978 err
= usb_8dev_cmd_version(priv
, &version
);
980 netdev_err(netdev
, "can't get firmware version\n");
981 goto cleanup_unregister_candev
;
984 "firmware: %d.%d, hardware: %d.%d\n",
985 (version
>>24) & 0xff, (version
>>16) & 0xff,
986 (version
>>8) & 0xff, version
& 0xff);
989 devm_can_led_init(netdev
);
993 cleanup_unregister_candev
:
994 unregister_netdev(priv
->netdev
);
996 cleanup_cmd_msg_buffer
:
997 kfree(priv
->cmd_msg_buffer
);
1000 free_candev(netdev
);
1006 /* Called by the usb core when driver is unloaded or device is removed */
1007 static void usb_8dev_disconnect(struct usb_interface
*intf
)
1009 struct usb_8dev_priv
*priv
= usb_get_intfdata(intf
);
1011 usb_set_intfdata(intf
, NULL
);
1014 netdev_info(priv
->netdev
, "device disconnected\n");
1016 unregister_netdev(priv
->netdev
);
1017 free_candev(priv
->netdev
);
1019 unlink_all_urbs(priv
);
1024 static struct usb_driver usb_8dev_driver
= {
1026 .probe
= usb_8dev_probe
,
1027 .disconnect
= usb_8dev_disconnect
,
1028 .id_table
= usb_8dev_table
,
1031 module_usb_driver(usb_8dev_driver
);
1033 MODULE_AUTHOR("Bernd Krumboeck <krumboeck@universalnet.at>");
1034 MODULE_DESCRIPTION("CAN driver for 8 devices USB2CAN interfaces");
1035 MODULE_LICENSE("GPL v2");