2 * CAN driver for esd CAN-USB/2
4 * Copyright (C) 2010 Matthias Fuchs <matthias.fuchs@esd.eu>, esd gmbh
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
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include <linux/init.h>
20 #include <linux/signal.h>
21 #include <linux/slab.h>
22 #include <linux/module.h>
23 #include <linux/netdevice.h>
24 #include <linux/usb.h>
26 #include <linux/can.h>
27 #include <linux/can/dev.h>
28 #include <linux/can/error.h>
30 MODULE_AUTHOR("Matthias Fuchs <matthias.fuchs@esd.eu>");
31 MODULE_DESCRIPTION("CAN driver for esd CAN-USB/2 interfaces");
32 MODULE_LICENSE("GPL v2");
34 /* Define these values to match your devices */
35 #define USB_ESDGMBH_VENDOR_ID 0x0ab4
36 #define USB_CANUSB2_PRODUCT_ID 0x0010
38 #define ESD_USB2_CAN_CLOCK 60000000
39 #define ESD_USB2_MAX_NETS 2
42 #define CMD_VERSION 1 /* also used for VERSION_REPLY */
43 #define CMD_CAN_RX 2 /* device to host only */
44 #define CMD_CAN_TX 3 /* also used for TX_DONE */
45 #define CMD_SETBAUD 4 /* also used for SETBAUD_REPLY */
46 #define CMD_TS 5 /* also used for TS_REPLY */
47 #define CMD_IDADD 6 /* also used for IDADD_REPLY */
49 /* esd CAN message flags - dlc field */
52 /* esd CAN message flags - id field */
53 #define ESD_EXTID 0x20000000
54 #define ESD_EVENT 0x40000000
55 #define ESD_IDMASK 0x1fffffff
57 /* esd CAN event ids used by this driver */
58 #define ESD_EV_CAN_ERROR_EXT 2
60 /* baudrate message flags */
61 #define ESD_USB2_UBR 0x80000000
62 #define ESD_USB2_LOM 0x40000000
63 #define ESD_USB2_NO_BAUDRATE 0x7fffffff
64 #define ESD_USB2_TSEG1_MIN 1
65 #define ESD_USB2_TSEG1_MAX 16
66 #define ESD_USB2_TSEG1_SHIFT 16
67 #define ESD_USB2_TSEG2_MIN 1
68 #define ESD_USB2_TSEG2_MAX 8
69 #define ESD_USB2_TSEG2_SHIFT 20
70 #define ESD_USB2_SJW_MAX 4
71 #define ESD_USB2_SJW_SHIFT 14
72 #define ESD_USB2_BRP_MIN 1
73 #define ESD_USB2_BRP_MAX 1024
74 #define ESD_USB2_BRP_INC 1
75 #define ESD_USB2_3_SAMPLES 0x00800000
77 /* esd IDADD message */
78 #define ESD_ID_ENABLE 0x80
79 #define ESD_MAX_ID_SEGMENT 64
81 /* SJA1000 ECC register (emulated by usb2 firmware) */
82 #define SJA1000_ECC_SEG 0x1F
83 #define SJA1000_ECC_DIR 0x20
84 #define SJA1000_ECC_ERR 0x06
85 #define SJA1000_ECC_BIT 0x00
86 #define SJA1000_ECC_FORM 0x40
87 #define SJA1000_ECC_STUFF 0x80
88 #define SJA1000_ECC_MASK 0xc0
90 /* esd bus state event codes */
91 #define ESD_BUSSTATE_MASK 0xc0
92 #define ESD_BUSSTATE_WARN 0x40
93 #define ESD_BUSSTATE_ERRPASSIVE 0x80
94 #define ESD_BUSSTATE_BUSOFF 0xc0
96 #define RX_BUFFER_SIZE 1024
98 #define MAX_TX_URBS 16 /* must be power of 2 */
101 u8 len
; /* len is always the total message length in 32bit words */
114 struct version_reply_msg
{
131 __le32 id
; /* upper 3 bits contain flags */
141 __le32 id
; /* upper 3 bits contain flags */
154 struct id_filter_msg
{
159 __le32 mask
[ESD_MAX_ID_SEGMENT
+ 1];
162 struct set_baudrate_msg
{
170 /* Main message type used between library and application */
171 struct __attribute__ ((packed
)) esd_usb2_msg
{
173 struct header_msg hdr
;
174 struct version_msg version
;
175 struct version_reply_msg version_reply
;
178 struct tx_done_msg txdone
;
179 struct set_baudrate_msg setbaud
;
180 struct id_filter_msg filter
;
184 static struct usb_device_id esd_usb2_table
[] = {
185 {USB_DEVICE(USB_ESDGMBH_VENDOR_ID
, USB_CANUSB2_PRODUCT_ID
)},
188 MODULE_DEVICE_TABLE(usb
, esd_usb2_table
);
190 struct esd_usb2_net_priv
;
192 struct esd_tx_urb_context
{
193 struct esd_usb2_net_priv
*priv
;
199 struct usb_device
*udev
;
200 struct esd_usb2_net_priv
*nets
[ESD_USB2_MAX_NETS
];
202 struct usb_anchor rx_submitted
;
209 struct esd_usb2_net_priv
{
210 struct can_priv can
; /* must be the first member */
212 atomic_t active_tx_jobs
;
213 struct usb_anchor tx_submitted
;
214 struct esd_tx_urb_context tx_contexts
[MAX_TX_URBS
];
217 struct esd_usb2
*usb2
;
218 struct net_device
*netdev
;
221 struct can_berr_counter bec
;
224 static void esd_usb2_rx_event(struct esd_usb2_net_priv
*priv
,
225 struct esd_usb2_msg
*msg
)
227 struct net_device_stats
*stats
= &priv
->netdev
->stats
;
228 struct can_frame
*cf
;
230 u32 id
= le32_to_cpu(msg
->msg
.rx
.id
) & ESD_IDMASK
;
232 if (id
== ESD_EV_CAN_ERROR_EXT
) {
233 u8 state
= msg
->msg
.rx
.data
[0];
234 u8 ecc
= msg
->msg
.rx
.data
[1];
235 u8 txerr
= msg
->msg
.rx
.data
[2];
236 u8 rxerr
= msg
->msg
.rx
.data
[3];
238 skb
= alloc_can_err_skb(priv
->netdev
, &cf
);
244 if (state
!= priv
->old_state
) {
245 priv
->old_state
= state
;
247 switch (state
& ESD_BUSSTATE_MASK
) {
248 case ESD_BUSSTATE_BUSOFF
:
249 priv
->can
.state
= CAN_STATE_BUS_OFF
;
250 cf
->can_id
|= CAN_ERR_BUSOFF
;
251 can_bus_off(priv
->netdev
);
253 case ESD_BUSSTATE_WARN
:
254 priv
->can
.state
= CAN_STATE_ERROR_WARNING
;
255 priv
->can
.can_stats
.error_warning
++;
257 case ESD_BUSSTATE_ERRPASSIVE
:
258 priv
->can
.state
= CAN_STATE_ERROR_PASSIVE
;
259 priv
->can
.can_stats
.error_passive
++;
262 priv
->can
.state
= CAN_STATE_ERROR_ACTIVE
;
266 priv
->can
.can_stats
.bus_error
++;
269 cf
->can_id
|= CAN_ERR_PROT
| CAN_ERR_BUSERROR
;
271 switch (ecc
& SJA1000_ECC_MASK
) {
272 case SJA1000_ECC_BIT
:
273 cf
->data
[2] |= CAN_ERR_PROT_BIT
;
275 case SJA1000_ECC_FORM
:
276 cf
->data
[2] |= CAN_ERR_PROT_FORM
;
278 case SJA1000_ECC_STUFF
:
279 cf
->data
[2] |= CAN_ERR_PROT_STUFF
;
282 cf
->data
[2] |= CAN_ERR_PROT_UNSPEC
;
283 cf
->data
[3] = ecc
& SJA1000_ECC_SEG
;
287 /* Error occurred during transmission? */
288 if (!(ecc
& SJA1000_ECC_DIR
))
289 cf
->data
[2] |= CAN_ERR_PROT_TX
;
291 if (priv
->can
.state
== CAN_STATE_ERROR_WARNING
||
292 priv
->can
.state
== CAN_STATE_ERROR_PASSIVE
) {
293 cf
->data
[1] = (txerr
> rxerr
) ?
294 CAN_ERR_CRTL_TX_PASSIVE
:
295 CAN_ERR_CRTL_RX_PASSIVE
;
303 priv
->bec
.txerr
= txerr
;
304 priv
->bec
.rxerr
= rxerr
;
307 stats
->rx_bytes
+= cf
->can_dlc
;
311 static void esd_usb2_rx_can_msg(struct esd_usb2_net_priv
*priv
,
312 struct esd_usb2_msg
*msg
)
314 struct net_device_stats
*stats
= &priv
->netdev
->stats
;
315 struct can_frame
*cf
;
320 if (!netif_device_present(priv
->netdev
))
323 id
= le32_to_cpu(msg
->msg
.rx
.id
);
325 if (id
& ESD_EVENT
) {
326 esd_usb2_rx_event(priv
, msg
);
328 skb
= alloc_can_skb(priv
->netdev
, &cf
);
334 cf
->can_id
= id
& ESD_IDMASK
;
335 cf
->can_dlc
= get_can_dlc(msg
->msg
.rx
.dlc
);
338 cf
->can_id
|= CAN_EFF_FLAG
;
340 if (msg
->msg
.rx
.dlc
& ESD_RTR
) {
341 cf
->can_id
|= CAN_RTR_FLAG
;
343 for (i
= 0; i
< cf
->can_dlc
; i
++)
344 cf
->data
[i
] = msg
->msg
.rx
.data
[i
];
350 stats
->rx_bytes
+= cf
->can_dlc
;
356 static void esd_usb2_tx_done_msg(struct esd_usb2_net_priv
*priv
,
357 struct esd_usb2_msg
*msg
)
359 struct net_device_stats
*stats
= &priv
->netdev
->stats
;
360 struct net_device
*netdev
= priv
->netdev
;
361 struct esd_tx_urb_context
*context
;
363 if (!netif_device_present(netdev
))
366 context
= &priv
->tx_contexts
[msg
->msg
.txdone
.hnd
& (MAX_TX_URBS
- 1)];
368 if (!msg
->msg
.txdone
.status
) {
370 stats
->tx_bytes
+= context
->dlc
;
371 can_get_echo_skb(netdev
, context
->echo_index
);
374 can_free_echo_skb(netdev
, context
->echo_index
);
377 /* Release context */
378 context
->echo_index
= MAX_TX_URBS
;
379 atomic_dec(&priv
->active_tx_jobs
);
381 netif_wake_queue(netdev
);
384 static void esd_usb2_read_bulk_callback(struct urb
*urb
)
386 struct esd_usb2
*dev
= urb
->context
;
391 switch (urb
->status
) {
392 case 0: /* success */
400 dev_info(dev
->udev
->dev
.parent
,
401 "Rx URB aborted (%d)\n", urb
->status
);
405 while (pos
< urb
->actual_length
) {
406 struct esd_usb2_msg
*msg
;
408 msg
= (struct esd_usb2_msg
*)(urb
->transfer_buffer
+ pos
);
410 switch (msg
->msg
.hdr
.cmd
) {
412 esd_usb2_rx_can_msg(dev
->nets
[msg
->msg
.rx
.net
], msg
);
416 esd_usb2_tx_done_msg(dev
->nets
[msg
->msg
.txdone
.net
],
421 pos
+= msg
->msg
.hdr
.len
<< 2;
423 if (pos
> urb
->actual_length
) {
424 dev_err(dev
->udev
->dev
.parent
, "format error\n");
430 usb_fill_bulk_urb(urb
, dev
->udev
, usb_rcvbulkpipe(dev
->udev
, 1),
431 urb
->transfer_buffer
, RX_BUFFER_SIZE
,
432 esd_usb2_read_bulk_callback
, dev
);
434 retval
= usb_submit_urb(urb
, GFP_ATOMIC
);
435 if (retval
== -ENODEV
) {
436 for (i
= 0; i
< dev
->net_count
; i
++) {
438 netif_device_detach(dev
->nets
[i
]->netdev
);
441 dev_err(dev
->udev
->dev
.parent
,
442 "failed resubmitting read bulk urb: %d\n", retval
);
449 * callback for bulk IN urb
451 static void esd_usb2_write_bulk_callback(struct urb
*urb
)
453 struct esd_tx_urb_context
*context
= urb
->context
;
454 struct esd_usb2_net_priv
*priv
;
455 struct esd_usb2
*dev
;
456 struct net_device
*netdev
;
457 size_t size
= sizeof(struct esd_usb2_msg
);
461 priv
= context
->priv
;
462 netdev
= priv
->netdev
;
465 /* free up our allocated buffer */
466 usb_free_coherent(urb
->dev
, size
,
467 urb
->transfer_buffer
, urb
->transfer_dma
);
469 if (!netif_device_present(netdev
))
473 dev_info(netdev
->dev
.parent
, "Tx URB aborted (%d)\n",
476 netdev
->trans_start
= jiffies
;
479 static ssize_t
show_firmware(struct device
*d
,
480 struct device_attribute
*attr
, char *buf
)
482 struct usb_interface
*intf
= to_usb_interface(d
);
483 struct esd_usb2
*dev
= usb_get_intfdata(intf
);
485 return sprintf(buf
, "%d.%d.%d\n",
486 (dev
->version
>> 12) & 0xf,
487 (dev
->version
>> 8) & 0xf,
488 dev
->version
& 0xff);
490 static DEVICE_ATTR(firmware
, S_IRUGO
, show_firmware
, NULL
);
492 static ssize_t
show_hardware(struct device
*d
,
493 struct device_attribute
*attr
, char *buf
)
495 struct usb_interface
*intf
= to_usb_interface(d
);
496 struct esd_usb2
*dev
= usb_get_intfdata(intf
);
498 return sprintf(buf
, "%d.%d.%d\n",
499 (dev
->version
>> 28) & 0xf,
500 (dev
->version
>> 24) & 0xf,
501 (dev
->version
>> 16) & 0xff);
503 static DEVICE_ATTR(hardware
, S_IRUGO
, show_hardware
, NULL
);
505 static ssize_t
show_nets(struct device
*d
,
506 struct device_attribute
*attr
, char *buf
)
508 struct usb_interface
*intf
= to_usb_interface(d
);
509 struct esd_usb2
*dev
= usb_get_intfdata(intf
);
511 return sprintf(buf
, "%d", dev
->net_count
);
513 static DEVICE_ATTR(nets
, S_IRUGO
, show_nets
, NULL
);
515 static int esd_usb2_send_msg(struct esd_usb2
*dev
, struct esd_usb2_msg
*msg
)
519 return usb_bulk_msg(dev
->udev
,
520 usb_sndbulkpipe(dev
->udev
, 2),
522 msg
->msg
.hdr
.len
<< 2,
527 static int esd_usb2_wait_msg(struct esd_usb2
*dev
,
528 struct esd_usb2_msg
*msg
)
532 return usb_bulk_msg(dev
->udev
,
533 usb_rcvbulkpipe(dev
->udev
, 1),
540 static int esd_usb2_setup_rx_urbs(struct esd_usb2
*dev
)
547 for (i
= 0; i
< MAX_RX_URBS
; i
++) {
548 struct urb
*urb
= NULL
;
551 /* create a URB, and a buffer for it */
552 urb
= usb_alloc_urb(0, GFP_KERNEL
);
554 dev_warn(dev
->udev
->dev
.parent
,
555 "No memory left for URBs\n");
560 buf
= usb_alloc_coherent(dev
->udev
, RX_BUFFER_SIZE
, GFP_KERNEL
,
563 dev_warn(dev
->udev
->dev
.parent
,
564 "No memory left for USB buffer\n");
569 usb_fill_bulk_urb(urb
, dev
->udev
,
570 usb_rcvbulkpipe(dev
->udev
, 1),
572 esd_usb2_read_bulk_callback
, dev
);
573 urb
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
574 usb_anchor_urb(urb
, &dev
->rx_submitted
);
576 err
= usb_submit_urb(urb
, GFP_KERNEL
);
578 usb_unanchor_urb(urb
);
579 usb_free_coherent(dev
->udev
, RX_BUFFER_SIZE
, buf
,
584 /* Drop reference, USB core will take care of freeing it */
590 /* Did we submit any URBs */
592 dev_err(dev
->udev
->dev
.parent
, "couldn't setup read URBs\n");
596 /* Warn if we've couldn't transmit all the URBs */
597 if (i
< MAX_RX_URBS
) {
598 dev_warn(dev
->udev
->dev
.parent
,
599 "rx performance may be slow\n");
609 static int esd_usb2_start(struct esd_usb2_net_priv
*priv
)
611 struct esd_usb2
*dev
= priv
->usb2
;
612 struct net_device
*netdev
= priv
->netdev
;
613 struct esd_usb2_msg msg
;
618 * The IDADD message takes up to 64 32 bit bitmasks (2048 bits).
619 * Each bit represents one 11 bit CAN identifier. A set bit
620 * enables reception of the corresponding CAN identifier. A cleared
621 * bit disabled this identifier. An additional bitmask value
622 * following the CAN 2.0A bits is used to enable reception of
623 * extended CAN frames. Only the LSB of this final mask is checked
624 * for the complete 29 bit ID range. The IDADD message also allows
625 * filter configuration for an ID subset. In this case you can add
626 * the number of the starting bitmask (0..64) to the filter.option
627 * field followed by only some bitmasks.
629 msg
.msg
.hdr
.cmd
= CMD_IDADD
;
630 msg
.msg
.hdr
.len
= 2 + ESD_MAX_ID_SEGMENT
;
631 msg
.msg
.filter
.net
= priv
->index
;
632 msg
.msg
.filter
.option
= ESD_ID_ENABLE
; /* start with segment 0 */
633 for (i
= 0; i
< ESD_MAX_ID_SEGMENT
; i
++)
634 msg
.msg
.filter
.mask
[i
] = cpu_to_le32(0xffffffff);
635 /* enable 29bit extended IDs */
636 msg
.msg
.filter
.mask
[ESD_MAX_ID_SEGMENT
] = cpu_to_le32(0x00000001);
638 err
= esd_usb2_send_msg(dev
, &msg
);
642 err
= esd_usb2_setup_rx_urbs(dev
);
646 priv
->can
.state
= CAN_STATE_ERROR_ACTIVE
;
652 netif_device_detach(netdev
);
654 dev_err(netdev
->dev
.parent
, "couldn't start device: %d\n", err
);
659 static void unlink_all_urbs(struct esd_usb2
*dev
)
661 struct esd_usb2_net_priv
*priv
;
664 usb_kill_anchored_urbs(&dev
->rx_submitted
);
665 for (i
= 0; i
< dev
->net_count
; i
++) {
668 usb_kill_anchored_urbs(&priv
->tx_submitted
);
669 atomic_set(&priv
->active_tx_jobs
, 0);
671 for (j
= 0; j
< MAX_TX_URBS
; j
++)
672 priv
->tx_contexts
[j
].echo_index
= MAX_TX_URBS
;
677 static int esd_usb2_open(struct net_device
*netdev
)
679 struct esd_usb2_net_priv
*priv
= netdev_priv(netdev
);
683 err
= open_candev(netdev
);
687 /* finally start device */
688 err
= esd_usb2_start(priv
);
690 dev_warn(netdev
->dev
.parent
,
691 "couldn't start device: %d\n", err
);
692 close_candev(netdev
);
696 priv
->open_time
= jiffies
;
698 netif_start_queue(netdev
);
703 static netdev_tx_t
esd_usb2_start_xmit(struct sk_buff
*skb
,
704 struct net_device
*netdev
)
706 struct esd_usb2_net_priv
*priv
= netdev_priv(netdev
);
707 struct esd_usb2
*dev
= priv
->usb2
;
708 struct esd_tx_urb_context
*context
= NULL
;
709 struct net_device_stats
*stats
= &netdev
->stats
;
710 struct can_frame
*cf
= (struct can_frame
*)skb
->data
;
711 struct esd_usb2_msg
*msg
;
715 int ret
= NETDEV_TX_OK
;
716 size_t size
= sizeof(struct esd_usb2_msg
);
718 if (can_dropped_invalid_skb(netdev
, skb
))
721 /* create a URB, and a buffer for it, and copy the data to the URB */
722 urb
= usb_alloc_urb(0, GFP_ATOMIC
);
724 dev_err(netdev
->dev
.parent
, "No memory left for URBs\n");
730 buf
= usb_alloc_coherent(dev
->udev
, size
, GFP_ATOMIC
,
733 dev_err(netdev
->dev
.parent
, "No memory left for USB buffer\n");
739 msg
= (struct esd_usb2_msg
*)buf
;
741 msg
->msg
.hdr
.len
= 3; /* minimal length */
742 msg
->msg
.hdr
.cmd
= CMD_CAN_TX
;
743 msg
->msg
.tx
.net
= priv
->index
;
744 msg
->msg
.tx
.dlc
= cf
->can_dlc
;
745 msg
->msg
.tx
.id
= cpu_to_le32(cf
->can_id
& CAN_ERR_MASK
);
747 if (cf
->can_id
& CAN_RTR_FLAG
)
748 msg
->msg
.tx
.dlc
|= ESD_RTR
;
750 if (cf
->can_id
& CAN_EFF_FLAG
)
751 msg
->msg
.tx
.id
|= cpu_to_le32(ESD_EXTID
);
753 for (i
= 0; i
< cf
->can_dlc
; i
++)
754 msg
->msg
.tx
.data
[i
] = cf
->data
[i
];
756 msg
->msg
.hdr
.len
+= (cf
->can_dlc
+ 3) >> 2;
758 for (i
= 0; i
< MAX_TX_URBS
; i
++) {
759 if (priv
->tx_contexts
[i
].echo_index
== MAX_TX_URBS
) {
760 context
= &priv
->tx_contexts
[i
];
766 * This may never happen.
769 dev_warn(netdev
->dev
.parent
, "couldn't find free context\n");
770 ret
= NETDEV_TX_BUSY
;
774 context
->priv
= priv
;
775 context
->echo_index
= i
;
776 context
->dlc
= cf
->can_dlc
;
778 /* hnd must not be 0 - MSB is stripped in txdone handling */
779 msg
->msg
.tx
.hnd
= 0x80000000 | i
; /* returned in TX done message */
781 usb_fill_bulk_urb(urb
, dev
->udev
, usb_sndbulkpipe(dev
->udev
, 2), buf
,
782 msg
->msg
.hdr
.len
<< 2,
783 esd_usb2_write_bulk_callback
, context
);
785 urb
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
787 usb_anchor_urb(urb
, &priv
->tx_submitted
);
789 can_put_echo_skb(skb
, netdev
, context
->echo_index
);
791 atomic_inc(&priv
->active_tx_jobs
);
793 /* Slow down tx path */
794 if (atomic_read(&priv
->active_tx_jobs
) >= MAX_TX_URBS
)
795 netif_stop_queue(netdev
);
797 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
799 can_free_echo_skb(netdev
, context
->echo_index
);
801 atomic_dec(&priv
->active_tx_jobs
);
802 usb_unanchor_urb(urb
);
807 netif_device_detach(netdev
);
809 dev_warn(netdev
->dev
.parent
, "failed tx_urb %d\n", err
);
814 netdev
->trans_start
= jiffies
;
817 * Release our reference to this URB, the USB core will eventually free
825 usb_free_coherent(dev
->udev
, size
, buf
, urb
->transfer_dma
);
834 static int esd_usb2_close(struct net_device
*netdev
)
836 struct esd_usb2_net_priv
*priv
= netdev_priv(netdev
);
837 struct esd_usb2_msg msg
;
840 /* Disable all IDs (see esd_usb2_start()) */
841 msg
.msg
.hdr
.cmd
= CMD_IDADD
;
842 msg
.msg
.hdr
.len
= 2 + ESD_MAX_ID_SEGMENT
;
843 msg
.msg
.filter
.net
= priv
->index
;
844 msg
.msg
.filter
.option
= ESD_ID_ENABLE
; /* start with segment 0 */
845 for (i
= 0; i
<= ESD_MAX_ID_SEGMENT
; i
++)
846 msg
.msg
.filter
.mask
[i
] = 0;
847 if (esd_usb2_send_msg(priv
->usb2
, &msg
) < 0)
848 dev_err(netdev
->dev
.parent
, "sending idadd message failed\n");
850 /* set CAN controller to reset mode */
852 msg
.msg
.hdr
.cmd
= CMD_SETBAUD
;
853 msg
.msg
.setbaud
.net
= priv
->index
;
854 msg
.msg
.setbaud
.rsvd
= 0;
855 msg
.msg
.setbaud
.baud
= cpu_to_le32(ESD_USB2_NO_BAUDRATE
);
856 if (esd_usb2_send_msg(priv
->usb2
, &msg
) < 0)
857 dev_err(netdev
->dev
.parent
, "sending setbaud message failed\n");
859 priv
->can
.state
= CAN_STATE_STOPPED
;
861 netif_stop_queue(netdev
);
863 close_candev(netdev
);
870 static const struct net_device_ops esd_usb2_netdev_ops
= {
871 .ndo_open
= esd_usb2_open
,
872 .ndo_stop
= esd_usb2_close
,
873 .ndo_start_xmit
= esd_usb2_start_xmit
,
876 static struct can_bittiming_const esd_usb2_bittiming_const
= {
878 .tseg1_min
= ESD_USB2_TSEG1_MIN
,
879 .tseg1_max
= ESD_USB2_TSEG1_MAX
,
880 .tseg2_min
= ESD_USB2_TSEG2_MIN
,
881 .tseg2_max
= ESD_USB2_TSEG2_MAX
,
882 .sjw_max
= ESD_USB2_SJW_MAX
,
883 .brp_min
= ESD_USB2_BRP_MIN
,
884 .brp_max
= ESD_USB2_BRP_MAX
,
885 .brp_inc
= ESD_USB2_BRP_INC
,
888 static int esd_usb2_set_bittiming(struct net_device
*netdev
)
890 struct esd_usb2_net_priv
*priv
= netdev_priv(netdev
);
891 struct can_bittiming
*bt
= &priv
->can
.bittiming
;
892 struct esd_usb2_msg msg
;
895 canbtr
= ESD_USB2_UBR
;
896 canbtr
|= (bt
->brp
- 1) & (ESD_USB2_BRP_MAX
- 1);
897 canbtr
|= ((bt
->sjw
- 1) & (ESD_USB2_SJW_MAX
- 1))
898 << ESD_USB2_SJW_SHIFT
;
899 canbtr
|= ((bt
->prop_seg
+ bt
->phase_seg1
- 1)
900 & (ESD_USB2_TSEG1_MAX
- 1))
901 << ESD_USB2_TSEG1_SHIFT
;
902 canbtr
|= ((bt
->phase_seg2
- 1) & (ESD_USB2_TSEG2_MAX
- 1))
903 << ESD_USB2_TSEG2_SHIFT
;
904 if (priv
->can
.ctrlmode
& CAN_CTRLMODE_3_SAMPLES
)
905 canbtr
|= ESD_USB2_3_SAMPLES
;
908 msg
.msg
.hdr
.cmd
= CMD_SETBAUD
;
909 msg
.msg
.setbaud
.net
= priv
->index
;
910 msg
.msg
.setbaud
.rsvd
= 0;
911 msg
.msg
.setbaud
.baud
= cpu_to_le32(canbtr
);
913 dev_info(netdev
->dev
.parent
, "setting BTR=%#x\n", canbtr
);
915 return esd_usb2_send_msg(priv
->usb2
, &msg
);
918 static int esd_usb2_get_berr_counter(const struct net_device
*netdev
,
919 struct can_berr_counter
*bec
)
921 struct esd_usb2_net_priv
*priv
= netdev_priv(netdev
);
923 bec
->txerr
= priv
->bec
.txerr
;
924 bec
->rxerr
= priv
->bec
.rxerr
;
929 static int esd_usb2_set_mode(struct net_device
*netdev
, enum can_mode mode
)
931 struct esd_usb2_net_priv
*priv
= netdev_priv(netdev
);
933 if (!priv
->open_time
)
938 netif_wake_queue(netdev
);
948 static int esd_usb2_probe_one_net(struct usb_interface
*intf
, int index
)
950 struct esd_usb2
*dev
= usb_get_intfdata(intf
);
951 struct net_device
*netdev
;
952 struct esd_usb2_net_priv
*priv
;
956 netdev
= alloc_candev(sizeof(*priv
), MAX_TX_URBS
);
958 dev_err(&intf
->dev
, "couldn't alloc candev\n");
963 priv
= netdev_priv(netdev
);
965 init_usb_anchor(&priv
->tx_submitted
);
966 atomic_set(&priv
->active_tx_jobs
, 0);
968 for (i
= 0; i
< MAX_TX_URBS
; i
++)
969 priv
->tx_contexts
[i
].echo_index
= MAX_TX_URBS
;
972 priv
->netdev
= netdev
;
975 priv
->can
.state
= CAN_STATE_STOPPED
;
976 priv
->can
.clock
.freq
= ESD_USB2_CAN_CLOCK
;
977 priv
->can
.bittiming_const
= &esd_usb2_bittiming_const
;
978 priv
->can
.do_set_bittiming
= esd_usb2_set_bittiming
;
979 priv
->can
.do_set_mode
= esd_usb2_set_mode
;
980 priv
->can
.do_get_berr_counter
= esd_usb2_get_berr_counter
;
981 priv
->can
.ctrlmode_supported
= CAN_CTRLMODE_3_SAMPLES
;
983 netdev
->flags
|= IFF_ECHO
; /* we support local echo */
985 netdev
->netdev_ops
= &esd_usb2_netdev_ops
;
987 SET_NETDEV_DEV(netdev
, &intf
->dev
);
989 err
= register_candev(netdev
);
992 "couldn't register CAN device: %d\n", err
);
998 dev
->nets
[index
] = priv
;
999 dev_info(netdev
->dev
.parent
, "device %s registered\n", netdev
->name
);
1006 * probe function for new USB2 devices
1008 * check version information and number of available
1011 static int esd_usb2_probe(struct usb_interface
*intf
,
1012 const struct usb_device_id
*id
)
1014 struct esd_usb2
*dev
;
1015 struct esd_usb2_msg msg
;
1018 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
1024 dev
->udev
= interface_to_usbdev(intf
);
1026 init_usb_anchor(&dev
->rx_submitted
);
1028 usb_set_intfdata(intf
, dev
);
1030 /* query number of CAN interfaces (nets) */
1031 msg
.msg
.hdr
.cmd
= CMD_VERSION
;
1032 msg
.msg
.hdr
.len
= 2;
1033 msg
.msg
.version
.rsvd
= 0;
1034 msg
.msg
.version
.flags
= 0;
1035 msg
.msg
.version
.drv_version
= 0;
1037 err
= esd_usb2_send_msg(dev
, &msg
);
1039 dev_err(&intf
->dev
, "sending version message failed\n");
1043 err
= esd_usb2_wait_msg(dev
, &msg
);
1045 dev_err(&intf
->dev
, "no version message answer\n");
1049 dev
->net_count
= (int)msg
.msg
.version_reply
.nets
;
1050 dev
->version
= le32_to_cpu(msg
.msg
.version_reply
.version
);
1052 if (device_create_file(&intf
->dev
, &dev_attr_firmware
))
1054 "Couldn't create device file for firmware\n");
1056 if (device_create_file(&intf
->dev
, &dev_attr_hardware
))
1058 "Couldn't create device file for hardware\n");
1060 if (device_create_file(&intf
->dev
, &dev_attr_nets
))
1062 "Couldn't create device file for nets\n");
1064 /* do per device probing */
1065 for (i
= 0; i
< dev
->net_count
; i
++)
1066 esd_usb2_probe_one_net(intf
, i
);
1077 * called by the usb core when the device is removed from the system
1079 static void esd_usb2_disconnect(struct usb_interface
*intf
)
1081 struct esd_usb2
*dev
= usb_get_intfdata(intf
);
1082 struct net_device
*netdev
;
1085 device_remove_file(&intf
->dev
, &dev_attr_firmware
);
1086 device_remove_file(&intf
->dev
, &dev_attr_hardware
);
1087 device_remove_file(&intf
->dev
, &dev_attr_nets
);
1089 usb_set_intfdata(intf
, NULL
);
1092 for (i
= 0; i
< dev
->net_count
; i
++) {
1094 netdev
= dev
->nets
[i
]->netdev
;
1095 unregister_netdev(netdev
);
1096 free_candev(netdev
);
1099 unlink_all_urbs(dev
);
1103 /* usb specific object needed to register this driver with the usb subsystem */
1104 static struct usb_driver esd_usb2_driver
= {
1106 .probe
= esd_usb2_probe
,
1107 .disconnect
= esd_usb2_disconnect
,
1108 .id_table
= esd_usb2_table
,
1111 module_usb_driver(esd_usb2_driver
);