1 /* SocketCAN driver for Microchip CAN BUS Analyzer Tool
3 * Copyright (C) 2017 Mobica Limited
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published
7 * by the Free Software Foundation; version 2 of the License.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
17 * This driver is inspired by the 4.6.2 version of net/can/usb/usb_8dev.c
20 #include <asm/unaligned.h>
21 #include <linux/can.h>
22 #include <linux/can/dev.h>
23 #include <linux/can/error.h>
24 #include <linux/can/led.h>
25 #include <linux/module.h>
26 #include <linux/netdevice.h>
27 #include <linux/signal.h>
28 #include <linux/slab.h>
29 #include <linux/usb.h>
31 /* vendor and product id */
32 #define MCBA_MODULE_NAME "mcba_usb"
33 #define MCBA_VENDOR_ID 0x04d8
34 #define MCBA_PRODUCT_ID 0x0a30
36 /* driver constants */
37 #define MCBA_MAX_RX_URBS 20
38 #define MCBA_MAX_TX_URBS 20
39 #define MCBA_CTX_FREE MCBA_MAX_TX_URBS
41 /* RX buffer must be bigger than msg size since at the
42 * beggining USB messages are stacked.
44 #define MCBA_USB_RX_BUFF_SIZE 64
45 #define MCBA_USB_TX_BUFF_SIZE (sizeof(struct mcba_usb_msg))
47 /* Microchip command id */
48 #define MBCA_CMD_RECEIVE_MESSAGE 0xE3
49 #define MBCA_CMD_I_AM_ALIVE_FROM_CAN 0xF5
50 #define MBCA_CMD_I_AM_ALIVE_FROM_USB 0xF7
51 #define MBCA_CMD_CHANGE_BIT_RATE 0xA1
52 #define MBCA_CMD_TRANSMIT_MESSAGE_EV 0xA3
53 #define MBCA_CMD_SETUP_TERMINATION_RESISTANCE 0xA8
54 #define MBCA_CMD_READ_FW_VERSION 0xA9
55 #define MBCA_CMD_NOTHING_TO_SEND 0xFF
56 #define MBCA_CMD_TRANSMIT_MESSAGE_RSP 0xE2
58 #define MCBA_VER_REQ_USB 1
59 #define MCBA_VER_REQ_CAN 2
61 /* Drive the CAN_RES signal LOW "0" to activate R24 and R25 */
62 #define MCBA_VER_TERMINATION_ON 0
63 #define MCBA_VER_TERMINATION_OFF 1
65 #define MCBA_SIDL_EXID_MASK 0x8
66 #define MCBA_DLC_MASK 0xf
67 #define MCBA_DLC_RTR_MASK 0x40
69 #define MCBA_CAN_STATE_WRN_TH 95
70 #define MCBA_CAN_STATE_ERR_PSV_TH 127
72 #define MCBA_TERMINATION_DISABLED CAN_TERMINATION_DISABLED
73 #define MCBA_TERMINATION_ENABLED 120
76 struct mcba_priv *priv;
82 /* Structure to hold all of our device specific stuff */
84 struct can_priv can; /* must be the first member */
85 struct sk_buff *echo_skb[MCBA_MAX_TX_URBS];
86 struct mcba_usb_ctx tx_context[MCBA_MAX_TX_URBS];
87 struct usb_device *udev;
88 struct net_device *netdev;
89 struct usb_anchor tx_submitted;
90 struct usb_anchor rx_submitted;
91 struct can_berr_counter bec;
92 bool usb_ka_first_pass;
93 bool can_ka_first_pass;
95 atomic_t free_ctx_cnt;
96 void *rxbuf[MCBA_MAX_RX_URBS];
97 dma_addr_t rxbuf_dma[MCBA_MAX_RX_URBS];
103 struct __packed mcba_usb_msg_can {
114 struct __packed mcba_usb_msg {
119 struct __packed mcba_usb_msg_ka_usb {
121 u8 termination_state;
127 struct __packed mcba_usb_msg_ka_can {
144 struct __packed mcba_usb_msg_change_bitrate {
150 struct __packed mcba_usb_msg_termination {
156 struct __packed mcba_usb_msg_fw_ver {
162 static const struct usb_device_id mcba_usb_table[] = {
163 { USB_DEVICE(MCBA_VENDOR_ID, MCBA_PRODUCT_ID) },
164 {} /* Terminating entry */
167 MODULE_DEVICE_TABLE(usb, mcba_usb_table);
169 static const u16 mcba_termination[] = { MCBA_TERMINATION_DISABLED,
170 MCBA_TERMINATION_ENABLED };
172 static const u32 mcba_bitrate[] = { 20000, 33333, 50000, 80000, 83333,
173 100000, 125000, 150000, 175000, 200000,
174 225000, 250000, 275000, 300000, 500000,
175 625000, 800000, 1000000 };
177 static inline void mcba_init_ctx(struct mcba_priv *priv)
181 for (i = 0; i < MCBA_MAX_TX_URBS; i++) {
182 priv->tx_context[i].ndx = MCBA_CTX_FREE;
183 priv->tx_context[i].priv = priv;
186 atomic_set(&priv->free_ctx_cnt, ARRAY_SIZE(priv->tx_context));
189 static inline struct mcba_usb_ctx *mcba_usb_get_free_ctx(struct mcba_priv *priv,
190 struct can_frame *cf)
193 struct mcba_usb_ctx *ctx = NULL;
195 for (i = 0; i < MCBA_MAX_TX_URBS; i++) {
196 if (priv->tx_context[i].ndx == MCBA_CTX_FREE) {
197 ctx = &priv->tx_context[i];
202 ctx->dlc = cf->can_dlc;
208 atomic_dec(&priv->free_ctx_cnt);
213 if (!atomic_read(&priv->free_ctx_cnt))
214 /* That was the last free ctx. Slow down tx path */
215 netif_stop_queue(priv->netdev);
220 /* mcba_usb_free_ctx and mcba_usb_get_free_ctx are executed by different
221 * threads. The order of execution in below function is important.
223 static inline void mcba_usb_free_ctx(struct mcba_usb_ctx *ctx)
225 /* Increase number of free ctxs before freeing ctx */
226 atomic_inc(&ctx->priv->free_ctx_cnt);
228 ctx->ndx = MCBA_CTX_FREE;
230 /* Wake up the queue once ctx is marked free */
231 netif_wake_queue(ctx->priv->netdev);
234 static void mcba_usb_write_bulk_callback(struct urb *urb)
236 struct mcba_usb_ctx *ctx = urb->context;
237 struct net_device *netdev;
241 netdev = ctx->priv->netdev;
243 /* free up our allocated buffer */
244 usb_free_coherent(urb->dev, urb->transfer_buffer_length,
245 urb->transfer_buffer, urb->transfer_dma);
248 if (!netif_device_present(netdev))
251 netdev->stats.tx_packets++;
252 netdev->stats.tx_bytes += ctx->dlc;
254 can_led_event(netdev, CAN_LED_EVENT_TX);
255 can_get_echo_skb(netdev, ctx->ndx);
259 netdev_info(netdev, "Tx URB aborted (%d)\n", urb->status);
261 /* Release the context */
262 mcba_usb_free_ctx(ctx);
265 /* Send data to device */
266 static netdev_tx_t mcba_usb_xmit(struct mcba_priv *priv,
267 struct mcba_usb_msg *usb_msg,
268 struct mcba_usb_ctx *ctx)
274 /* create a URB, and a buffer for it, and copy the data to the URB */
275 urb = usb_alloc_urb(0, GFP_ATOMIC);
279 buf = usb_alloc_coherent(priv->udev, MCBA_USB_TX_BUFF_SIZE, GFP_ATOMIC,
286 memcpy(buf, usb_msg, MCBA_USB_TX_BUFF_SIZE);
288 usb_fill_bulk_urb(urb, priv->udev, priv->tx_pipe, buf, MCBA_USB_TX_BUFF_SIZE,
289 mcba_usb_write_bulk_callback, ctx);
291 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
292 usb_anchor_urb(urb, &priv->tx_submitted);
294 err = usb_submit_urb(urb, GFP_ATOMIC);
298 /* Release our reference to this URB, the USB core will eventually free
306 usb_unanchor_urb(urb);
307 usb_free_coherent(priv->udev, MCBA_USB_TX_BUFF_SIZE, buf,
311 netif_device_detach(priv->netdev);
313 netdev_warn(priv->netdev, "failed tx_urb %d\n", err);
321 /* Send data to device */
322 static netdev_tx_t mcba_usb_start_xmit(struct sk_buff *skb,
323 struct net_device *netdev)
325 struct mcba_priv *priv = netdev_priv(netdev);
326 struct can_frame *cf = (struct can_frame *)skb->data;
327 struct mcba_usb_ctx *ctx = NULL;
328 struct net_device_stats *stats = &priv->netdev->stats;
331 struct mcba_usb_msg_can usb_msg = {
332 .cmd_id = MBCA_CMD_TRANSMIT_MESSAGE_EV
335 if (can_dropped_invalid_skb(netdev, skb))
338 ctx = mcba_usb_get_free_ctx(priv, cf);
340 return NETDEV_TX_BUSY;
342 if (cf->can_id & CAN_EFF_FLAG) {
343 /* SIDH | SIDL | EIDH | EIDL
344 * 28 - 21 | 20 19 18 x x x 17 16 | 15 - 8 | 7 - 0
346 sid = MCBA_SIDL_EXID_MASK;
347 /* store 28-18 bits */
348 sid |= (cf->can_id & 0x1ffc0000) >> 13;
349 /* store 17-16 bits */
350 sid |= (cf->can_id & 0x30000) >> 16;
351 put_unaligned_be16(sid, &usb_msg.sid);
353 /* store 15-0 bits */
354 put_unaligned_be16(cf->can_id & 0xffff, &usb_msg.eid);
357 * 10 - 3 | 2 1 0 x x x x x
359 put_unaligned_be16((cf->can_id & CAN_SFF_MASK) << 5,
364 usb_msg.dlc = cf->can_dlc;
366 memcpy(usb_msg.data, cf->data, usb_msg.dlc);
368 if (cf->can_id & CAN_RTR_FLAG)
369 usb_msg.dlc |= MCBA_DLC_RTR_MASK;
371 can_put_echo_skb(skb, priv->netdev, ctx->ndx);
373 err = mcba_usb_xmit(priv, (struct mcba_usb_msg *)&usb_msg, ctx);
380 can_free_echo_skb(priv->netdev, ctx->ndx);
381 mcba_usb_free_ctx(ctx);
387 /* Send cmd to device */
388 static void mcba_usb_xmit_cmd(struct mcba_priv *priv,
389 struct mcba_usb_msg *usb_msg)
391 struct mcba_usb_ctx *ctx = NULL;
394 ctx = mcba_usb_get_free_ctx(priv, NULL);
396 netdev_err(priv->netdev,
397 "Lack of free ctx. Sending (%d) cmd aborted",
403 err = mcba_usb_xmit(priv, usb_msg, ctx);
405 netdev_err(priv->netdev, "Failed to send cmd (%d)",
409 static void mcba_usb_xmit_change_bitrate(struct mcba_priv *priv, u16 bitrate)
411 struct mcba_usb_msg_change_bitrate usb_msg = {
412 .cmd_id = MBCA_CMD_CHANGE_BIT_RATE
415 put_unaligned_be16(bitrate, &usb_msg.bitrate);
417 mcba_usb_xmit_cmd(priv, (struct mcba_usb_msg *)&usb_msg);
420 static void mcba_usb_xmit_read_fw_ver(struct mcba_priv *priv, u8 pic)
422 struct mcba_usb_msg_fw_ver usb_msg = {
423 .cmd_id = MBCA_CMD_READ_FW_VERSION,
427 mcba_usb_xmit_cmd(priv, (struct mcba_usb_msg *)&usb_msg);
430 static void mcba_usb_process_can(struct mcba_priv *priv,
431 struct mcba_usb_msg_can *msg)
433 struct can_frame *cf;
435 struct net_device_stats *stats = &priv->netdev->stats;
438 skb = alloc_can_skb(priv->netdev, &cf);
442 sid = get_unaligned_be16(&msg->sid);
444 if (sid & MCBA_SIDL_EXID_MASK) {
445 /* SIDH | SIDL | EIDH | EIDL
446 * 28 - 21 | 20 19 18 x x x 17 16 | 15 - 8 | 7 - 0
448 cf->can_id = CAN_EFF_FLAG;
450 /* store 28-18 bits */
451 cf->can_id |= (sid & 0xffe0) << 13;
452 /* store 17-16 bits */
453 cf->can_id |= (sid & 3) << 16;
454 /* store 15-0 bits */
455 cf->can_id |= get_unaligned_be16(&msg->eid);
458 * 10 - 3 | 2 1 0 x x x x x
460 cf->can_id = (sid & 0xffe0) >> 5;
463 if (msg->dlc & MCBA_DLC_RTR_MASK)
464 cf->can_id |= CAN_RTR_FLAG;
466 cf->can_dlc = get_can_dlc(msg->dlc & MCBA_DLC_MASK);
468 memcpy(cf->data, msg->data, cf->can_dlc);
471 stats->rx_bytes += cf->can_dlc;
473 can_led_event(priv->netdev, CAN_LED_EVENT_RX);
477 static void mcba_usb_process_ka_usb(struct mcba_priv *priv,
478 struct mcba_usb_msg_ka_usb *msg)
480 if (unlikely(priv->usb_ka_first_pass)) {
481 netdev_info(priv->netdev, "PIC USB version %hhu.%hhu\n",
482 msg->soft_ver_major, msg->soft_ver_minor);
484 priv->usb_ka_first_pass = false;
487 if (msg->termination_state == MCBA_VER_TERMINATION_ON)
488 priv->can.termination = MCBA_TERMINATION_ENABLED;
490 priv->can.termination = MCBA_TERMINATION_DISABLED;
493 static u32 convert_can2host_bitrate(struct mcba_usb_msg_ka_can *msg)
495 const u32 bitrate = get_unaligned_be16(&msg->can_bitrate);
497 if ((bitrate == 33) || (bitrate == 83))
498 return bitrate * 1000 + 333;
500 return bitrate * 1000;
503 static void mcba_usb_process_ka_can(struct mcba_priv *priv,
504 struct mcba_usb_msg_ka_can *msg)
506 if (unlikely(priv->can_ka_first_pass)) {
507 netdev_info(priv->netdev, "PIC CAN version %hhu.%hhu\n",
508 msg->soft_ver_major, msg->soft_ver_minor);
510 priv->can_ka_first_pass = false;
513 if (unlikely(priv->can_speed_check)) {
514 const u32 bitrate = convert_can2host_bitrate(msg);
516 priv->can_speed_check = false;
518 if (bitrate != priv->can.bittiming.bitrate)
521 "Wrong bitrate reported by the device (%u). Expected %u",
522 bitrate, priv->can.bittiming.bitrate);
525 priv->bec.txerr = msg->tx_err_cnt;
526 priv->bec.rxerr = msg->rx_err_cnt;
529 priv->can.state = CAN_STATE_BUS_OFF;
531 else if ((priv->bec.txerr > MCBA_CAN_STATE_ERR_PSV_TH) ||
532 (priv->bec.rxerr > MCBA_CAN_STATE_ERR_PSV_TH))
533 priv->can.state = CAN_STATE_ERROR_PASSIVE;
535 else if ((priv->bec.txerr > MCBA_CAN_STATE_WRN_TH) ||
536 (priv->bec.rxerr > MCBA_CAN_STATE_WRN_TH))
537 priv->can.state = CAN_STATE_ERROR_WARNING;
540 static void mcba_usb_process_rx(struct mcba_priv *priv,
541 struct mcba_usb_msg *msg)
543 switch (msg->cmd_id) {
544 case MBCA_CMD_I_AM_ALIVE_FROM_CAN:
545 mcba_usb_process_ka_can(priv,
546 (struct mcba_usb_msg_ka_can *)msg);
549 case MBCA_CMD_I_AM_ALIVE_FROM_USB:
550 mcba_usb_process_ka_usb(priv,
551 (struct mcba_usb_msg_ka_usb *)msg);
554 case MBCA_CMD_RECEIVE_MESSAGE:
555 mcba_usb_process_can(priv, (struct mcba_usb_msg_can *)msg);
558 case MBCA_CMD_NOTHING_TO_SEND:
559 /* Side effect of communication between PIC_USB and PIC_CAN.
560 * PIC_CAN is telling us that it has nothing to send
564 case MBCA_CMD_TRANSMIT_MESSAGE_RSP:
565 /* Transmission response from the device containing timestamp */
569 netdev_warn(priv->netdev, "Unsupported msg (0x%hhX)",
575 /* Callback for reading data from device
577 * Check urb status, call read function and resubmit urb read operation.
579 static void mcba_usb_read_bulk_callback(struct urb *urb)
581 struct mcba_priv *priv = urb->context;
582 struct net_device *netdev;
586 netdev = priv->netdev;
588 if (!netif_device_present(netdev))
591 switch (urb->status) {
592 case 0: /* success */
602 netdev_info(netdev, "Rx URB aborted (%d)\n", urb->status);
607 while (pos < urb->actual_length) {
608 struct mcba_usb_msg *msg;
610 if (pos + sizeof(struct mcba_usb_msg) > urb->actual_length) {
611 netdev_err(priv->netdev, "format error\n");
615 msg = (struct mcba_usb_msg *)(urb->transfer_buffer + pos);
616 mcba_usb_process_rx(priv, msg);
618 pos += sizeof(struct mcba_usb_msg);
623 usb_fill_bulk_urb(urb, priv->udev,
625 urb->transfer_buffer, MCBA_USB_RX_BUFF_SIZE,
626 mcba_usb_read_bulk_callback, priv);
628 retval = usb_submit_urb(urb, GFP_ATOMIC);
630 if (retval == -ENODEV)
631 netif_device_detach(netdev);
633 netdev_err(netdev, "failed resubmitting read bulk urb: %d\n",
637 /* Start USB device */
638 static int mcba_usb_start(struct mcba_priv *priv)
640 struct net_device *netdev = priv->netdev;
645 for (i = 0; i < MCBA_MAX_RX_URBS; i++) {
646 struct urb *urb = NULL;
650 /* create a URB, and a buffer for it */
651 urb = usb_alloc_urb(0, GFP_KERNEL);
657 buf = usb_alloc_coherent(priv->udev, MCBA_USB_RX_BUFF_SIZE,
658 GFP_KERNEL, &buf_dma);
660 netdev_err(netdev, "No memory left for USB buffer\n");
666 urb->transfer_dma = buf_dma;
668 usb_fill_bulk_urb(urb, priv->udev,
670 buf, MCBA_USB_RX_BUFF_SIZE,
671 mcba_usb_read_bulk_callback, priv);
672 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
673 usb_anchor_urb(urb, &priv->rx_submitted);
675 err = usb_submit_urb(urb, GFP_KERNEL);
677 usb_unanchor_urb(urb);
678 usb_free_coherent(priv->udev, MCBA_USB_RX_BUFF_SIZE,
684 priv->rxbuf[i] = buf;
685 priv->rxbuf_dma[i] = buf_dma;
687 /* Drop reference, USB core will take care of freeing it */
691 /* Did we submit any URBs */
693 netdev_warn(netdev, "couldn't setup read URBs\n");
697 /* Warn if we've couldn't transmit all the URBs */
698 if (i < MCBA_MAX_RX_URBS)
699 netdev_warn(netdev, "rx performance may be slow\n");
701 mcba_usb_xmit_read_fw_ver(priv, MCBA_VER_REQ_USB);
702 mcba_usb_xmit_read_fw_ver(priv, MCBA_VER_REQ_CAN);
707 /* Open USB device */
708 static int mcba_usb_open(struct net_device *netdev)
710 struct mcba_priv *priv = netdev_priv(netdev);
714 err = open_candev(netdev);
718 priv->can_speed_check = true;
719 priv->can.state = CAN_STATE_ERROR_ACTIVE;
721 can_led_event(netdev, CAN_LED_EVENT_OPEN);
722 netif_start_queue(netdev);
727 static void mcba_urb_unlink(struct mcba_priv *priv)
731 usb_kill_anchored_urbs(&priv->rx_submitted);
733 for (i = 0; i < MCBA_MAX_RX_URBS; ++i)
734 usb_free_coherent(priv->udev, MCBA_USB_RX_BUFF_SIZE,
735 priv->rxbuf[i], priv->rxbuf_dma[i]);
737 usb_kill_anchored_urbs(&priv->tx_submitted);
740 /* Close USB device */
741 static int mcba_usb_close(struct net_device *netdev)
743 struct mcba_priv *priv = netdev_priv(netdev);
745 priv->can.state = CAN_STATE_STOPPED;
747 netif_stop_queue(netdev);
750 mcba_urb_unlink(priv);
752 close_candev(netdev);
753 can_led_event(netdev, CAN_LED_EVENT_STOP);
758 /* Set network device mode
760 * Maybe we should leave this function empty, because the device
761 * set mode variable with open command.
763 static int mcba_net_set_mode(struct net_device *netdev, enum can_mode mode)
768 static int mcba_net_get_berr_counter(const struct net_device *netdev,
769 struct can_berr_counter *bec)
771 struct mcba_priv *priv = netdev_priv(netdev);
773 bec->txerr = priv->bec.txerr;
774 bec->rxerr = priv->bec.rxerr;
779 static const struct net_device_ops mcba_netdev_ops = {
780 .ndo_open = mcba_usb_open,
781 .ndo_stop = mcba_usb_close,
782 .ndo_start_xmit = mcba_usb_start_xmit,
785 /* Microchip CANBUS has hardcoded bittiming values by default.
786 * This function sends request via USB to change the speed and align bittiming
787 * values for presentation purposes only
789 static int mcba_net_set_bittiming(struct net_device *netdev)
791 struct mcba_priv *priv = netdev_priv(netdev);
792 const u16 bitrate_kbps = priv->can.bittiming.bitrate / 1000;
794 mcba_usb_xmit_change_bitrate(priv, bitrate_kbps);
799 static int mcba_set_termination(struct net_device *netdev, u16 term)
801 struct mcba_priv *priv = netdev_priv(netdev);
802 struct mcba_usb_msg_termination usb_msg = {
803 .cmd_id = MBCA_CMD_SETUP_TERMINATION_RESISTANCE
806 if (term == MCBA_TERMINATION_ENABLED)
807 usb_msg.termination = MCBA_VER_TERMINATION_ON;
809 usb_msg.termination = MCBA_VER_TERMINATION_OFF;
811 mcba_usb_xmit_cmd(priv, (struct mcba_usb_msg *)&usb_msg);
816 static int mcba_usb_probe(struct usb_interface *intf,
817 const struct usb_device_id *id)
819 struct net_device *netdev;
820 struct mcba_priv *priv;
822 struct usb_device *usbdev = interface_to_usbdev(intf);
823 struct usb_endpoint_descriptor *in, *out;
825 err = usb_find_common_endpoints(intf->cur_altsetting, &in, &out, NULL, NULL);
827 dev_err(&intf->dev, "Can't find endpoints\n");
831 netdev = alloc_candev(sizeof(struct mcba_priv), MCBA_MAX_TX_URBS);
833 dev_err(&intf->dev, "Couldn't alloc candev\n");
837 priv = netdev_priv(netdev);
840 priv->netdev = netdev;
841 priv->usb_ka_first_pass = true;
842 priv->can_ka_first_pass = true;
843 priv->can_speed_check = false;
845 init_usb_anchor(&priv->rx_submitted);
846 init_usb_anchor(&priv->tx_submitted);
848 usb_set_intfdata(intf, priv);
850 /* Init CAN device */
851 priv->can.state = CAN_STATE_STOPPED;
852 priv->can.termination_const = mcba_termination;
853 priv->can.termination_const_cnt = ARRAY_SIZE(mcba_termination);
854 priv->can.bitrate_const = mcba_bitrate;
855 priv->can.bitrate_const_cnt = ARRAY_SIZE(mcba_bitrate);
857 priv->can.do_set_termination = mcba_set_termination;
858 priv->can.do_set_mode = mcba_net_set_mode;
859 priv->can.do_get_berr_counter = mcba_net_get_berr_counter;
860 priv->can.do_set_bittiming = mcba_net_set_bittiming;
862 netdev->netdev_ops = &mcba_netdev_ops;
864 netdev->flags |= IFF_ECHO; /* we support local echo */
866 SET_NETDEV_DEV(netdev, &intf->dev);
868 err = register_candev(netdev);
870 netdev_err(netdev, "couldn't register CAN device: %d\n", err);
872 goto cleanup_free_candev;
875 priv->rx_pipe = usb_rcvbulkpipe(priv->udev, in->bEndpointAddress);
876 priv->tx_pipe = usb_sndbulkpipe(priv->udev, out->bEndpointAddress);
878 devm_can_led_init(netdev);
880 /* Start USB dev only if we have successfully registered CAN device */
881 err = mcba_usb_start(priv);
884 netif_device_detach(priv->netdev);
886 netdev_warn(netdev, "couldn't start device: %d\n", err);
888 goto cleanup_unregister_candev;
891 dev_info(&intf->dev, "Microchip CAN BUS Analyzer connected\n");
895 cleanup_unregister_candev:
896 unregister_candev(priv->netdev);
904 /* Called by the usb core when driver is unloaded or device is removed */
905 static void mcba_usb_disconnect(struct usb_interface *intf)
907 struct mcba_priv *priv = usb_get_intfdata(intf);
909 usb_set_intfdata(intf, NULL);
911 netdev_info(priv->netdev, "device disconnected\n");
913 unregister_candev(priv->netdev);
914 mcba_urb_unlink(priv);
915 free_candev(priv->netdev);
918 static struct usb_driver mcba_usb_driver = {
919 .name = MCBA_MODULE_NAME,
920 .probe = mcba_usb_probe,
921 .disconnect = mcba_usb_disconnect,
922 .id_table = mcba_usb_table,
925 module_usb_driver(mcba_usb_driver);
927 MODULE_AUTHOR("Remigiusz Kołłątaj <remigiusz.kollataj@mobica.com>");
928 MODULE_DESCRIPTION("SocketCAN driver for Microchip CAN BUS Analyzer Tool");
929 MODULE_LICENSE("GPL v2");