2 * CAN driver for esd CAN-USB/2 and CAN-USB/Micro
4 * Copyright (C) 2010-2012 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/signal.h>
20 #include <linux/slab.h>
21 #include <linux/module.h>
22 #include <linux/netdevice.h>
23 #include <linux/usb.h>
25 #include <linux/can.h>
26 #include <linux/can/dev.h>
27 #include <linux/can/error.h>
29 MODULE_AUTHOR("Matthias Fuchs <matthias.fuchs@esd.eu>");
30 MODULE_DESCRIPTION("CAN driver for esd CAN-USB/2 and CAN-USB/Micro interfaces");
31 MODULE_LICENSE("GPL v2");
33 /* Define these values to match your devices */
34 #define USB_ESDGMBH_VENDOR_ID 0x0ab4
35 #define USB_CANUSB2_PRODUCT_ID 0x0010
36 #define USB_CANUSBM_PRODUCT_ID 0x0011
38 #define ESD_USB2_CAN_CLOCK 60000000
39 #define ESD_USBM_CAN_CLOCK 36000000
40 #define ESD_USB2_MAX_NETS 2
43 #define CMD_VERSION 1 /* also used for VERSION_REPLY */
44 #define CMD_CAN_RX 2 /* device to host only */
45 #define CMD_CAN_TX 3 /* also used for TX_DONE */
46 #define CMD_SETBAUD 4 /* also used for SETBAUD_REPLY */
47 #define CMD_TS 5 /* also used for TS_REPLY */
48 #define CMD_IDADD 6 /* also used for IDADD_REPLY */
50 /* esd CAN message flags - dlc field */
53 /* esd CAN message flags - id field */
54 #define ESD_EXTID 0x20000000
55 #define ESD_EVENT 0x40000000
56 #define ESD_IDMASK 0x1fffffff
58 /* esd CAN event ids used by this driver */
59 #define ESD_EV_CAN_ERROR_EXT 2
61 /* baudrate message flags */
62 #define ESD_USB2_UBR 0x80000000
63 #define ESD_USB2_LOM 0x40000000
64 #define ESD_USB2_NO_BAUDRATE 0x7fffffff
65 #define ESD_USB2_TSEG1_MIN 1
66 #define ESD_USB2_TSEG1_MAX 16
67 #define ESD_USB2_TSEG1_SHIFT 16
68 #define ESD_USB2_TSEG2_MIN 1
69 #define ESD_USB2_TSEG2_MAX 8
70 #define ESD_USB2_TSEG2_SHIFT 20
71 #define ESD_USB2_SJW_MAX 4
72 #define ESD_USB2_SJW_SHIFT 14
73 #define ESD_USBM_SJW_SHIFT 24
74 #define ESD_USB2_BRP_MIN 1
75 #define ESD_USB2_BRP_MAX 1024
76 #define ESD_USB2_BRP_INC 1
77 #define ESD_USB2_3_SAMPLES 0x00800000
79 /* esd IDADD message */
80 #define ESD_ID_ENABLE 0x80
81 #define ESD_MAX_ID_SEGMENT 64
83 /* SJA1000 ECC register (emulated by usb2 firmware) */
84 #define SJA1000_ECC_SEG 0x1F
85 #define SJA1000_ECC_DIR 0x20
86 #define SJA1000_ECC_ERR 0x06
87 #define SJA1000_ECC_BIT 0x00
88 #define SJA1000_ECC_FORM 0x40
89 #define SJA1000_ECC_STUFF 0x80
90 #define SJA1000_ECC_MASK 0xc0
92 /* esd bus state event codes */
93 #define ESD_BUSSTATE_MASK 0xc0
94 #define ESD_BUSSTATE_WARN 0x40
95 #define ESD_BUSSTATE_ERRPASSIVE 0x80
96 #define ESD_BUSSTATE_BUSOFF 0xc0
98 #define RX_BUFFER_SIZE 1024
100 #define MAX_TX_URBS 16 /* must be power of 2 */
103 u8 len; /* len is always the total message length in 32bit words */
116 struct version_reply_msg {
133 __le32 id; /* upper 3 bits contain flags */
142 u32 hnd; /* opaque handle, not used by device */
143 __le32 id; /* upper 3 bits contain flags */
152 u32 hnd; /* opaque handle, not used by device */
156 struct id_filter_msg {
161 __le32 mask[ESD_MAX_ID_SEGMENT + 1];
164 struct set_baudrate_msg {
172 /* Main message type used between library and application */
173 struct __attribute__ ((packed)) esd_usb2_msg {
175 struct header_msg hdr;
176 struct version_msg version;
177 struct version_reply_msg version_reply;
180 struct tx_done_msg txdone;
181 struct set_baudrate_msg setbaud;
182 struct id_filter_msg filter;
186 static struct usb_device_id esd_usb2_table[] = {
187 {USB_DEVICE(USB_ESDGMBH_VENDOR_ID, USB_CANUSB2_PRODUCT_ID)},
188 {USB_DEVICE(USB_ESDGMBH_VENDOR_ID, USB_CANUSBM_PRODUCT_ID)},
191 MODULE_DEVICE_TABLE(usb, esd_usb2_table);
193 struct esd_usb2_net_priv;
195 struct esd_tx_urb_context {
196 struct esd_usb2_net_priv *priv;
202 struct usb_device *udev;
203 struct esd_usb2_net_priv *nets[ESD_USB2_MAX_NETS];
205 struct usb_anchor rx_submitted;
212 struct esd_usb2_net_priv {
213 struct can_priv can; /* must be the first member */
215 atomic_t active_tx_jobs;
216 struct usb_anchor tx_submitted;
217 struct esd_tx_urb_context tx_contexts[MAX_TX_URBS];
219 struct esd_usb2 *usb2;
220 struct net_device *netdev;
223 struct can_berr_counter bec;
226 static void esd_usb2_rx_event(struct esd_usb2_net_priv *priv,
227 struct esd_usb2_msg *msg)
229 struct net_device_stats *stats = &priv->netdev->stats;
230 struct can_frame *cf;
232 u32 id = le32_to_cpu(msg->msg.rx.id) & ESD_IDMASK;
234 if (id == ESD_EV_CAN_ERROR_EXT) {
235 u8 state = msg->msg.rx.data[0];
236 u8 ecc = msg->msg.rx.data[1];
237 u8 txerr = msg->msg.rx.data[2];
238 u8 rxerr = msg->msg.rx.data[3];
240 skb = alloc_can_err_skb(priv->netdev, &cf);
246 if (state != priv->old_state) {
247 priv->old_state = state;
249 switch (state & ESD_BUSSTATE_MASK) {
250 case ESD_BUSSTATE_BUSOFF:
251 priv->can.state = CAN_STATE_BUS_OFF;
252 cf->can_id |= CAN_ERR_BUSOFF;
253 priv->can.can_stats.bus_off++;
254 can_bus_off(priv->netdev);
256 case ESD_BUSSTATE_WARN:
257 priv->can.state = CAN_STATE_ERROR_WARNING;
258 priv->can.can_stats.error_warning++;
260 case ESD_BUSSTATE_ERRPASSIVE:
261 priv->can.state = CAN_STATE_ERROR_PASSIVE;
262 priv->can.can_stats.error_passive++;
265 priv->can.state = CAN_STATE_ERROR_ACTIVE;
269 priv->can.can_stats.bus_error++;
272 cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
274 switch (ecc & SJA1000_ECC_MASK) {
275 case SJA1000_ECC_BIT:
276 cf->data[2] |= CAN_ERR_PROT_BIT;
278 case SJA1000_ECC_FORM:
279 cf->data[2] |= CAN_ERR_PROT_FORM;
281 case SJA1000_ECC_STUFF:
282 cf->data[2] |= CAN_ERR_PROT_STUFF;
285 cf->data[3] = ecc & SJA1000_ECC_SEG;
289 /* Error occurred during transmission? */
290 if (!(ecc & SJA1000_ECC_DIR))
291 cf->data[2] |= CAN_ERR_PROT_TX;
293 if (priv->can.state == CAN_STATE_ERROR_WARNING ||
294 priv->can.state == CAN_STATE_ERROR_PASSIVE) {
295 cf->data[1] = (txerr > rxerr) ?
296 CAN_ERR_CRTL_TX_PASSIVE :
297 CAN_ERR_CRTL_RX_PASSIVE;
303 priv->bec.txerr = txerr;
304 priv->bec.rxerr = rxerr;
307 stats->rx_bytes += cf->can_dlc;
312 static void esd_usb2_rx_can_msg(struct esd_usb2_net_priv *priv,
313 struct esd_usb2_msg *msg)
315 struct net_device_stats *stats = &priv->netdev->stats;
316 struct can_frame *cf;
321 if (!netif_device_present(priv->netdev))
324 id = le32_to_cpu(msg->msg.rx.id);
326 if (id & ESD_EVENT) {
327 esd_usb2_rx_event(priv, msg);
329 skb = alloc_can_skb(priv->netdev, &cf);
335 cf->can_id = id & ESD_IDMASK;
336 cf->can_dlc = get_can_dlc(msg->msg.rx.dlc & ~ESD_RTR);
339 cf->can_id |= CAN_EFF_FLAG;
341 if (msg->msg.rx.dlc & ESD_RTR) {
342 cf->can_id |= CAN_RTR_FLAG;
344 for (i = 0; i < cf->can_dlc; i++)
345 cf->data[i] = msg->msg.rx.data[i];
349 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 */
402 dev_info(dev->udev->dev.parent,
403 "Rx URB aborted (%d)\n", urb->status);
407 while (pos < urb->actual_length) {
408 struct esd_usb2_msg *msg;
410 msg = (struct esd_usb2_msg *)(urb->transfer_buffer + pos);
412 switch (msg->msg.hdr.cmd) {
414 if (msg->msg.rx.net >= dev->net_count) {
415 dev_err(dev->udev->dev.parent, "format error\n");
419 esd_usb2_rx_can_msg(dev->nets[msg->msg.rx.net], msg);
423 if (msg->msg.txdone.net >= dev->net_count) {
424 dev_err(dev->udev->dev.parent, "format error\n");
428 esd_usb2_tx_done_msg(dev->nets[msg->msg.txdone.net],
433 pos += msg->msg.hdr.len << 2;
435 if (pos > urb->actual_length) {
436 dev_err(dev->udev->dev.parent, "format error\n");
442 usb_fill_bulk_urb(urb, dev->udev, usb_rcvbulkpipe(dev->udev, 1),
443 urb->transfer_buffer, RX_BUFFER_SIZE,
444 esd_usb2_read_bulk_callback, dev);
446 retval = usb_submit_urb(urb, GFP_ATOMIC);
447 if (retval == -ENODEV) {
448 for (i = 0; i < dev->net_count; i++) {
450 netif_device_detach(dev->nets[i]->netdev);
453 dev_err(dev->udev->dev.parent,
454 "failed resubmitting read bulk urb: %d\n", retval);
461 * callback for bulk IN urb
463 static void esd_usb2_write_bulk_callback(struct urb *urb)
465 struct esd_tx_urb_context *context = urb->context;
466 struct esd_usb2_net_priv *priv;
467 struct net_device *netdev;
468 size_t size = sizeof(struct esd_usb2_msg);
472 priv = context->priv;
473 netdev = priv->netdev;
475 /* free up our allocated buffer */
476 usb_free_coherent(urb->dev, size,
477 urb->transfer_buffer, urb->transfer_dma);
479 if (!netif_device_present(netdev))
483 netdev_info(netdev, "Tx URB aborted (%d)\n", urb->status);
485 netif_trans_update(netdev);
488 static ssize_t show_firmware(struct device *d,
489 struct device_attribute *attr, char *buf)
491 struct usb_interface *intf = to_usb_interface(d);
492 struct esd_usb2 *dev = usb_get_intfdata(intf);
494 return sprintf(buf, "%d.%d.%d\n",
495 (dev->version >> 12) & 0xf,
496 (dev->version >> 8) & 0xf,
497 dev->version & 0xff);
499 static DEVICE_ATTR(firmware, S_IRUGO, show_firmware, NULL);
501 static ssize_t show_hardware(struct device *d,
502 struct device_attribute *attr, char *buf)
504 struct usb_interface *intf = to_usb_interface(d);
505 struct esd_usb2 *dev = usb_get_intfdata(intf);
507 return sprintf(buf, "%d.%d.%d\n",
508 (dev->version >> 28) & 0xf,
509 (dev->version >> 24) & 0xf,
510 (dev->version >> 16) & 0xff);
512 static DEVICE_ATTR(hardware, S_IRUGO, show_hardware, NULL);
514 static ssize_t show_nets(struct device *d,
515 struct device_attribute *attr, char *buf)
517 struct usb_interface *intf = to_usb_interface(d);
518 struct esd_usb2 *dev = usb_get_intfdata(intf);
520 return sprintf(buf, "%d", dev->net_count);
522 static DEVICE_ATTR(nets, S_IRUGO, show_nets, NULL);
524 static int esd_usb2_send_msg(struct esd_usb2 *dev, struct esd_usb2_msg *msg)
528 return usb_bulk_msg(dev->udev,
529 usb_sndbulkpipe(dev->udev, 2),
531 msg->msg.hdr.len << 2,
536 static int esd_usb2_wait_msg(struct esd_usb2 *dev,
537 struct esd_usb2_msg *msg)
541 return usb_bulk_msg(dev->udev,
542 usb_rcvbulkpipe(dev->udev, 1),
549 static int esd_usb2_setup_rx_urbs(struct esd_usb2 *dev)
556 for (i = 0; i < MAX_RX_URBS; i++) {
557 struct urb *urb = NULL;
560 /* create a URB, and a buffer for it */
561 urb = usb_alloc_urb(0, GFP_KERNEL);
567 buf = usb_alloc_coherent(dev->udev, RX_BUFFER_SIZE, GFP_KERNEL,
570 dev_warn(dev->udev->dev.parent,
571 "No memory left for USB buffer\n");
576 usb_fill_bulk_urb(urb, dev->udev,
577 usb_rcvbulkpipe(dev->udev, 1),
579 esd_usb2_read_bulk_callback, dev);
580 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
581 usb_anchor_urb(urb, &dev->rx_submitted);
583 err = usb_submit_urb(urb, GFP_KERNEL);
585 usb_unanchor_urb(urb);
586 usb_free_coherent(dev->udev, RX_BUFFER_SIZE, buf,
591 /* Drop reference, USB core will take care of freeing it */
597 /* Did we submit any URBs */
599 dev_err(dev->udev->dev.parent, "couldn't setup read URBs\n");
603 /* Warn if we've couldn't transmit all the URBs */
604 if (i < MAX_RX_URBS) {
605 dev_warn(dev->udev->dev.parent,
606 "rx performance may be slow\n");
616 static int esd_usb2_start(struct esd_usb2_net_priv *priv)
618 struct esd_usb2 *dev = priv->usb2;
619 struct net_device *netdev = priv->netdev;
620 struct esd_usb2_msg *msg;
623 msg = kmalloc(sizeof(*msg), GFP_KERNEL);
631 * The IDADD message takes up to 64 32 bit bitmasks (2048 bits).
632 * Each bit represents one 11 bit CAN identifier. A set bit
633 * enables reception of the corresponding CAN identifier. A cleared
634 * bit disabled this identifier. An additional bitmask value
635 * following the CAN 2.0A bits is used to enable reception of
636 * extended CAN frames. Only the LSB of this final mask is checked
637 * for the complete 29 bit ID range. The IDADD message also allows
638 * filter configuration for an ID subset. In this case you can add
639 * the number of the starting bitmask (0..64) to the filter.option
640 * field followed by only some bitmasks.
642 msg->msg.hdr.cmd = CMD_IDADD;
643 msg->msg.hdr.len = 2 + ESD_MAX_ID_SEGMENT;
644 msg->msg.filter.net = priv->index;
645 msg->msg.filter.option = ESD_ID_ENABLE; /* start with segment 0 */
646 for (i = 0; i < ESD_MAX_ID_SEGMENT; i++)
647 msg->msg.filter.mask[i] = cpu_to_le32(0xffffffff);
648 /* enable 29bit extended IDs */
649 msg->msg.filter.mask[ESD_MAX_ID_SEGMENT] = cpu_to_le32(0x00000001);
651 err = esd_usb2_send_msg(dev, msg);
655 err = esd_usb2_setup_rx_urbs(dev);
659 priv->can.state = CAN_STATE_ERROR_ACTIVE;
663 netif_device_detach(netdev);
665 netdev_err(netdev, "couldn't start device: %d\n", err);
671 static void unlink_all_urbs(struct esd_usb2 *dev)
673 struct esd_usb2_net_priv *priv;
676 usb_kill_anchored_urbs(&dev->rx_submitted);
677 for (i = 0; i < dev->net_count; i++) {
680 usb_kill_anchored_urbs(&priv->tx_submitted);
681 atomic_set(&priv->active_tx_jobs, 0);
683 for (j = 0; j < MAX_TX_URBS; j++)
684 priv->tx_contexts[j].echo_index = MAX_TX_URBS;
689 static int esd_usb2_open(struct net_device *netdev)
691 struct esd_usb2_net_priv *priv = netdev_priv(netdev);
695 err = open_candev(netdev);
699 /* finally start device */
700 err = esd_usb2_start(priv);
702 netdev_warn(netdev, "couldn't start device: %d\n", err);
703 close_candev(netdev);
707 netif_start_queue(netdev);
712 static netdev_tx_t esd_usb2_start_xmit(struct sk_buff *skb,
713 struct net_device *netdev)
715 struct esd_usb2_net_priv *priv = netdev_priv(netdev);
716 struct esd_usb2 *dev = priv->usb2;
717 struct esd_tx_urb_context *context = NULL;
718 struct net_device_stats *stats = &netdev->stats;
719 struct can_frame *cf = (struct can_frame *)skb->data;
720 struct esd_usb2_msg *msg;
724 int ret = NETDEV_TX_OK;
725 size_t size = sizeof(struct esd_usb2_msg);
727 if (can_dropped_invalid_skb(netdev, skb))
730 /* create a URB, and a buffer for it, and copy the data to the URB */
731 urb = usb_alloc_urb(0, GFP_ATOMIC);
738 buf = usb_alloc_coherent(dev->udev, size, GFP_ATOMIC,
741 netdev_err(netdev, "No memory left for USB buffer\n");
747 msg = (struct esd_usb2_msg *)buf;
749 msg->msg.hdr.len = 3; /* minimal length */
750 msg->msg.hdr.cmd = CMD_CAN_TX;
751 msg->msg.tx.net = priv->index;
752 msg->msg.tx.dlc = cf->can_dlc;
753 msg->msg.tx.id = cpu_to_le32(cf->can_id & CAN_ERR_MASK);
755 if (cf->can_id & CAN_RTR_FLAG)
756 msg->msg.tx.dlc |= ESD_RTR;
758 if (cf->can_id & CAN_EFF_FLAG)
759 msg->msg.tx.id |= cpu_to_le32(ESD_EXTID);
761 for (i = 0; i < cf->can_dlc; i++)
762 msg->msg.tx.data[i] = cf->data[i];
764 msg->msg.hdr.len += (cf->can_dlc + 3) >> 2;
766 for (i = 0; i < MAX_TX_URBS; i++) {
767 if (priv->tx_contexts[i].echo_index == MAX_TX_URBS) {
768 context = &priv->tx_contexts[i];
774 * This may never happen.
777 netdev_warn(netdev, "couldn't find free context\n");
778 ret = NETDEV_TX_BUSY;
782 context->priv = priv;
783 context->echo_index = i;
784 context->dlc = cf->can_dlc;
786 /* hnd must not be 0 - MSB is stripped in txdone handling */
787 msg->msg.tx.hnd = 0x80000000 | i; /* returned in TX done message */
789 usb_fill_bulk_urb(urb, dev->udev, usb_sndbulkpipe(dev->udev, 2), buf,
790 msg->msg.hdr.len << 2,
791 esd_usb2_write_bulk_callback, context);
793 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
795 usb_anchor_urb(urb, &priv->tx_submitted);
797 can_put_echo_skb(skb, netdev, context->echo_index);
799 atomic_inc(&priv->active_tx_jobs);
801 /* Slow down tx path */
802 if (atomic_read(&priv->active_tx_jobs) >= MAX_TX_URBS)
803 netif_stop_queue(netdev);
805 err = usb_submit_urb(urb, GFP_ATOMIC);
807 can_free_echo_skb(netdev, context->echo_index);
809 atomic_dec(&priv->active_tx_jobs);
810 usb_unanchor_urb(urb);
815 netif_device_detach(netdev);
817 netdev_warn(netdev, "failed tx_urb %d\n", err);
822 netif_trans_update(netdev);
825 * Release our reference to this URB, the USB core will eventually free
833 usb_free_coherent(dev->udev, size, buf, urb->transfer_dma);
842 static int esd_usb2_close(struct net_device *netdev)
844 struct esd_usb2_net_priv *priv = netdev_priv(netdev);
845 struct esd_usb2_msg *msg;
848 msg = kmalloc(sizeof(*msg), GFP_KERNEL);
852 /* Disable all IDs (see esd_usb2_start()) */
853 msg->msg.hdr.cmd = CMD_IDADD;
854 msg->msg.hdr.len = 2 + ESD_MAX_ID_SEGMENT;
855 msg->msg.filter.net = priv->index;
856 msg->msg.filter.option = ESD_ID_ENABLE; /* start with segment 0 */
857 for (i = 0; i <= ESD_MAX_ID_SEGMENT; i++)
858 msg->msg.filter.mask[i] = 0;
859 if (esd_usb2_send_msg(priv->usb2, msg) < 0)
860 netdev_err(netdev, "sending idadd message failed\n");
862 /* set CAN controller to reset mode */
863 msg->msg.hdr.len = 2;
864 msg->msg.hdr.cmd = CMD_SETBAUD;
865 msg->msg.setbaud.net = priv->index;
866 msg->msg.setbaud.rsvd = 0;
867 msg->msg.setbaud.baud = cpu_to_le32(ESD_USB2_NO_BAUDRATE);
868 if (esd_usb2_send_msg(priv->usb2, msg) < 0)
869 netdev_err(netdev, "sending setbaud message failed\n");
871 priv->can.state = CAN_STATE_STOPPED;
873 netif_stop_queue(netdev);
875 close_candev(netdev);
882 static const struct net_device_ops esd_usb2_netdev_ops = {
883 .ndo_open = esd_usb2_open,
884 .ndo_stop = esd_usb2_close,
885 .ndo_start_xmit = esd_usb2_start_xmit,
886 .ndo_change_mtu = can_change_mtu,
889 static const struct can_bittiming_const esd_usb2_bittiming_const = {
891 .tseg1_min = ESD_USB2_TSEG1_MIN,
892 .tseg1_max = ESD_USB2_TSEG1_MAX,
893 .tseg2_min = ESD_USB2_TSEG2_MIN,
894 .tseg2_max = ESD_USB2_TSEG2_MAX,
895 .sjw_max = ESD_USB2_SJW_MAX,
896 .brp_min = ESD_USB2_BRP_MIN,
897 .brp_max = ESD_USB2_BRP_MAX,
898 .brp_inc = ESD_USB2_BRP_INC,
901 static int esd_usb2_set_bittiming(struct net_device *netdev)
903 struct esd_usb2_net_priv *priv = netdev_priv(netdev);
904 struct can_bittiming *bt = &priv->can.bittiming;
905 struct esd_usb2_msg *msg;
910 canbtr = ESD_USB2_UBR;
911 if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
912 canbtr |= ESD_USB2_LOM;
914 canbtr |= (bt->brp - 1) & (ESD_USB2_BRP_MAX - 1);
916 if (le16_to_cpu(priv->usb2->udev->descriptor.idProduct) ==
917 USB_CANUSBM_PRODUCT_ID)
918 sjw_shift = ESD_USBM_SJW_SHIFT;
920 sjw_shift = ESD_USB2_SJW_SHIFT;
922 canbtr |= ((bt->sjw - 1) & (ESD_USB2_SJW_MAX - 1))
924 canbtr |= ((bt->prop_seg + bt->phase_seg1 - 1)
925 & (ESD_USB2_TSEG1_MAX - 1))
926 << ESD_USB2_TSEG1_SHIFT;
927 canbtr |= ((bt->phase_seg2 - 1) & (ESD_USB2_TSEG2_MAX - 1))
928 << ESD_USB2_TSEG2_SHIFT;
929 if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
930 canbtr |= ESD_USB2_3_SAMPLES;
932 msg = kmalloc(sizeof(*msg), GFP_KERNEL);
936 msg->msg.hdr.len = 2;
937 msg->msg.hdr.cmd = CMD_SETBAUD;
938 msg->msg.setbaud.net = priv->index;
939 msg->msg.setbaud.rsvd = 0;
940 msg->msg.setbaud.baud = cpu_to_le32(canbtr);
942 netdev_info(netdev, "setting BTR=%#x\n", canbtr);
944 err = esd_usb2_send_msg(priv->usb2, msg);
950 static int esd_usb2_get_berr_counter(const struct net_device *netdev,
951 struct can_berr_counter *bec)
953 struct esd_usb2_net_priv *priv = netdev_priv(netdev);
955 bec->txerr = priv->bec.txerr;
956 bec->rxerr = priv->bec.rxerr;
961 static int esd_usb2_set_mode(struct net_device *netdev, enum can_mode mode)
965 netif_wake_queue(netdev);
975 static int esd_usb2_probe_one_net(struct usb_interface *intf, int index)
977 struct esd_usb2 *dev = usb_get_intfdata(intf);
978 struct net_device *netdev;
979 struct esd_usb2_net_priv *priv;
983 netdev = alloc_candev(sizeof(*priv), MAX_TX_URBS);
985 dev_err(&intf->dev, "couldn't alloc candev\n");
990 priv = netdev_priv(netdev);
992 init_usb_anchor(&priv->tx_submitted);
993 atomic_set(&priv->active_tx_jobs, 0);
995 for (i = 0; i < MAX_TX_URBS; i++)
996 priv->tx_contexts[i].echo_index = MAX_TX_URBS;
999 priv->netdev = netdev;
1000 priv->index = index;
1002 priv->can.state = CAN_STATE_STOPPED;
1003 priv->can.ctrlmode_supported = CAN_CTRLMODE_LISTENONLY;
1005 if (le16_to_cpu(dev->udev->descriptor.idProduct) ==
1006 USB_CANUSBM_PRODUCT_ID)
1007 priv->can.clock.freq = ESD_USBM_CAN_CLOCK;
1009 priv->can.clock.freq = ESD_USB2_CAN_CLOCK;
1010 priv->can.ctrlmode_supported |= CAN_CTRLMODE_3_SAMPLES;
1013 priv->can.bittiming_const = &esd_usb2_bittiming_const;
1014 priv->can.do_set_bittiming = esd_usb2_set_bittiming;
1015 priv->can.do_set_mode = esd_usb2_set_mode;
1016 priv->can.do_get_berr_counter = esd_usb2_get_berr_counter;
1018 netdev->flags |= IFF_ECHO; /* we support local echo */
1020 netdev->netdev_ops = &esd_usb2_netdev_ops;
1022 SET_NETDEV_DEV(netdev, &intf->dev);
1023 netdev->dev_id = index;
1025 err = register_candev(netdev);
1027 dev_err(&intf->dev, "couldn't register CAN device: %d\n", err);
1028 free_candev(netdev);
1033 dev->nets[index] = priv;
1034 netdev_info(netdev, "device %s registered\n", netdev->name);
1041 * probe function for new USB2 devices
1043 * check version information and number of available
1046 static int esd_usb2_probe(struct usb_interface *intf,
1047 const struct usb_device_id *id)
1049 struct esd_usb2 *dev;
1050 struct esd_usb2_msg *msg;
1053 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1059 dev->udev = interface_to_usbdev(intf);
1061 init_usb_anchor(&dev->rx_submitted);
1063 usb_set_intfdata(intf, dev);
1065 msg = kmalloc(sizeof(*msg), GFP_KERNEL);
1071 /* query number of CAN interfaces (nets) */
1072 msg->msg.hdr.cmd = CMD_VERSION;
1073 msg->msg.hdr.len = 2;
1074 msg->msg.version.rsvd = 0;
1075 msg->msg.version.flags = 0;
1076 msg->msg.version.drv_version = 0;
1078 err = esd_usb2_send_msg(dev, msg);
1080 dev_err(&intf->dev, "sending version message failed\n");
1084 err = esd_usb2_wait_msg(dev, msg);
1086 dev_err(&intf->dev, "no version message answer\n");
1090 dev->net_count = (int)msg->msg.version_reply.nets;
1091 dev->version = le32_to_cpu(msg->msg.version_reply.version);
1093 if (device_create_file(&intf->dev, &dev_attr_firmware))
1095 "Couldn't create device file for firmware\n");
1097 if (device_create_file(&intf->dev, &dev_attr_hardware))
1099 "Couldn't create device file for hardware\n");
1101 if (device_create_file(&intf->dev, &dev_attr_nets))
1103 "Couldn't create device file for nets\n");
1105 /* do per device probing */
1106 for (i = 0; i < dev->net_count; i++)
1107 esd_usb2_probe_one_net(intf, i);
1118 * called by the usb core when the device is removed from the system
1120 static void esd_usb2_disconnect(struct usb_interface *intf)
1122 struct esd_usb2 *dev = usb_get_intfdata(intf);
1123 struct net_device *netdev;
1126 device_remove_file(&intf->dev, &dev_attr_firmware);
1127 device_remove_file(&intf->dev, &dev_attr_hardware);
1128 device_remove_file(&intf->dev, &dev_attr_nets);
1130 usb_set_intfdata(intf, NULL);
1133 for (i = 0; i < dev->net_count; i++) {
1135 netdev = dev->nets[i]->netdev;
1136 unregister_netdev(netdev);
1137 free_candev(netdev);
1140 unlink_all_urbs(dev);
1145 /* usb specific object needed to register this driver with the usb subsystem */
1146 static struct usb_driver esd_usb2_driver = {
1148 .probe = esd_usb2_probe,
1149 .disconnect = esd_usb2_disconnect,
1150 .id_table = esd_usb2_table,
1153 module_usb_driver(esd_usb2_driver);