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 /* MCBA endpoint numbers */
48 #define MCBA_USB_EP_IN 1
49 #define MCBA_USB_EP_OUT 1
51 /* Microchip command id */
52 #define MBCA_CMD_RECEIVE_MESSAGE 0xE3
53 #define MBCA_CMD_I_AM_ALIVE_FROM_CAN 0xF5
54 #define MBCA_CMD_I_AM_ALIVE_FROM_USB 0xF7
55 #define MBCA_CMD_CHANGE_BIT_RATE 0xA1
56 #define MBCA_CMD_TRANSMIT_MESSAGE_EV 0xA3
57 #define MBCA_CMD_SETUP_TERMINATION_RESISTANCE 0xA8
58 #define MBCA_CMD_READ_FW_VERSION 0xA9
59 #define MBCA_CMD_NOTHING_TO_SEND 0xFF
60 #define MBCA_CMD_TRANSMIT_MESSAGE_RSP 0xE2
62 #define MCBA_VER_REQ_USB 1
63 #define MCBA_VER_REQ_CAN 2
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
;
99 struct __packed mcba_usb_msg_can
{
110 struct __packed mcba_usb_msg
{
115 struct __packed mcba_usb_msg_ka_usb
{
117 u8 termination_state
;
123 struct __packed mcba_usb_msg_ka_can
{
140 struct __packed mcba_usb_msg_change_bitrate
{
146 struct __packed mcba_usb_msg_termination
{
152 struct __packed mcba_usb_msg_fw_ver
{
158 static const struct usb_device_id mcba_usb_table
[] = {
159 { USB_DEVICE(MCBA_VENDOR_ID
, MCBA_PRODUCT_ID
) },
160 {} /* Terminating entry */
163 MODULE_DEVICE_TABLE(usb
, mcba_usb_table
);
165 static const u16 mcba_termination
[] = { MCBA_TERMINATION_DISABLED
,
166 MCBA_TERMINATION_ENABLED
};
168 static const u32 mcba_bitrate
[] = { 20000, 33333, 50000, 80000, 83333,
169 100000, 125000, 150000, 175000, 200000,
170 225000, 250000, 275000, 300000, 500000,
171 625000, 800000, 1000000 };
173 static inline void mcba_init_ctx(struct mcba_priv
*priv
)
177 for (i
= 0; i
< MCBA_MAX_TX_URBS
; i
++) {
178 priv
->tx_context
[i
].ndx
= MCBA_CTX_FREE
;
179 priv
->tx_context
[i
].priv
= priv
;
182 atomic_set(&priv
->free_ctx_cnt
, ARRAY_SIZE(priv
->tx_context
));
185 static inline struct mcba_usb_ctx
*mcba_usb_get_free_ctx(struct mcba_priv
*priv
,
186 struct can_frame
*cf
)
189 struct mcba_usb_ctx
*ctx
= NULL
;
191 for (i
= 0; i
< MCBA_MAX_TX_URBS
; i
++) {
192 if (priv
->tx_context
[i
].ndx
== MCBA_CTX_FREE
) {
193 ctx
= &priv
->tx_context
[i
];
198 ctx
->dlc
= cf
->can_dlc
;
204 atomic_dec(&priv
->free_ctx_cnt
);
209 if (!atomic_read(&priv
->free_ctx_cnt
))
210 /* That was the last free ctx. Slow down tx path */
211 netif_stop_queue(priv
->netdev
);
216 /* mcba_usb_free_ctx and mcba_usb_get_free_ctx are executed by different
217 * threads. The order of execution in below function is important.
219 static inline void mcba_usb_free_ctx(struct mcba_usb_ctx
*ctx
)
221 /* Increase number of free ctxs before freeing ctx */
222 atomic_inc(&ctx
->priv
->free_ctx_cnt
);
224 ctx
->ndx
= MCBA_CTX_FREE
;
226 /* Wake up the queue once ctx is marked free */
227 netif_wake_queue(ctx
->priv
->netdev
);
230 static void mcba_usb_write_bulk_callback(struct urb
*urb
)
232 struct mcba_usb_ctx
*ctx
= urb
->context
;
233 struct net_device
*netdev
;
237 netdev
= ctx
->priv
->netdev
;
239 /* free up our allocated buffer */
240 usb_free_coherent(urb
->dev
, urb
->transfer_buffer_length
,
241 urb
->transfer_buffer
, urb
->transfer_dma
);
244 if (!netif_device_present(netdev
))
247 netdev
->stats
.tx_packets
++;
248 netdev
->stats
.tx_bytes
+= ctx
->dlc
;
250 can_led_event(netdev
, CAN_LED_EVENT_TX
);
251 can_get_echo_skb(netdev
, ctx
->ndx
);
255 netdev_info(netdev
, "Tx URB aborted (%d)\n", urb
->status
);
257 /* Release the context */
258 mcba_usb_free_ctx(ctx
);
261 /* Send data to device */
262 static netdev_tx_t
mcba_usb_xmit(struct mcba_priv
*priv
,
263 struct mcba_usb_msg
*usb_msg
,
264 struct mcba_usb_ctx
*ctx
)
270 /* create a URB, and a buffer for it, and copy the data to the URB */
271 urb
= usb_alloc_urb(0, GFP_ATOMIC
);
275 buf
= usb_alloc_coherent(priv
->udev
, MCBA_USB_TX_BUFF_SIZE
, GFP_ATOMIC
,
282 memcpy(buf
, usb_msg
, MCBA_USB_TX_BUFF_SIZE
);
284 usb_fill_bulk_urb(urb
, priv
->udev
,
285 usb_sndbulkpipe(priv
->udev
, MCBA_USB_EP_OUT
), buf
,
286 MCBA_USB_TX_BUFF_SIZE
, mcba_usb_write_bulk_callback
,
289 urb
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
290 usb_anchor_urb(urb
, &priv
->tx_submitted
);
292 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
296 /* Release our reference to this URB, the USB core will eventually free
304 usb_unanchor_urb(urb
);
305 usb_free_coherent(priv
->udev
, MCBA_USB_TX_BUFF_SIZE
, buf
,
309 netif_device_detach(priv
->netdev
);
311 netdev_warn(priv
->netdev
, "failed tx_urb %d\n", err
);
319 /* Send data to device */
320 static netdev_tx_t
mcba_usb_start_xmit(struct sk_buff
*skb
,
321 struct net_device
*netdev
)
323 struct mcba_priv
*priv
= netdev_priv(netdev
);
324 struct can_frame
*cf
= (struct can_frame
*)skb
->data
;
325 struct mcba_usb_ctx
*ctx
= NULL
;
326 struct net_device_stats
*stats
= &priv
->netdev
->stats
;
329 struct mcba_usb_msg_can usb_msg
= {
330 .cmd_id
= MBCA_CMD_TRANSMIT_MESSAGE_EV
333 if (can_dropped_invalid_skb(netdev
, skb
))
336 ctx
= mcba_usb_get_free_ctx(priv
, cf
);
338 return NETDEV_TX_BUSY
;
340 can_put_echo_skb(skb
, priv
->netdev
, ctx
->ndx
);
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 err
= mcba_usb_xmit(priv
, (struct mcba_usb_msg
*)&usb_msg
, ctx
);
378 can_free_echo_skb(priv
->netdev
, ctx
->ndx
);
379 mcba_usb_free_ctx(ctx
);
386 /* Send cmd to device */
387 static void mcba_usb_xmit_cmd(struct mcba_priv
*priv
,
388 struct mcba_usb_msg
*usb_msg
)
390 struct mcba_usb_ctx
*ctx
= NULL
;
393 ctx
= mcba_usb_get_free_ctx(priv
, NULL
);
395 netdev_err(priv
->netdev
,
396 "Lack of free ctx. Sending (%d) cmd aborted",
402 err
= mcba_usb_xmit(priv
, usb_msg
, ctx
);
404 netdev_err(priv
->netdev
, "Failed to send cmd (%d)",
408 static void mcba_usb_xmit_change_bitrate(struct mcba_priv
*priv
, u16 bitrate
)
410 struct mcba_usb_msg_change_bitrate usb_msg
= {
411 .cmd_id
= MBCA_CMD_CHANGE_BIT_RATE
414 put_unaligned_be16(bitrate
, &usb_msg
.bitrate
);
416 mcba_usb_xmit_cmd(priv
, (struct mcba_usb_msg
*)&usb_msg
);
419 static void mcba_usb_xmit_read_fw_ver(struct mcba_priv
*priv
, u8 pic
)
421 struct mcba_usb_msg_fw_ver usb_msg
= {
422 .cmd_id
= MBCA_CMD_READ_FW_VERSION
,
426 mcba_usb_xmit_cmd(priv
, (struct mcba_usb_msg
*)&usb_msg
);
429 static void mcba_usb_process_can(struct mcba_priv
*priv
,
430 struct mcba_usb_msg_can
*msg
)
432 struct can_frame
*cf
;
434 struct net_device_stats
*stats
= &priv
->netdev
->stats
;
437 skb
= alloc_can_skb(priv
->netdev
, &cf
);
441 sid
= get_unaligned_be16(&msg
->sid
);
443 if (sid
& MCBA_SIDL_EXID_MASK
) {
444 /* SIDH | SIDL | EIDH | EIDL
445 * 28 - 21 | 20 19 18 x x x 17 16 | 15 - 8 | 7 - 0
447 cf
->can_id
= CAN_EFF_FLAG
;
449 /* store 28-18 bits */
450 cf
->can_id
|= (sid
& 0xffe0) << 13;
451 /* store 17-16 bits */
452 cf
->can_id
|= (sid
& 3) << 16;
453 /* store 15-0 bits */
454 cf
->can_id
|= get_unaligned_be16(&msg
->eid
);
457 * 10 - 3 | 2 1 0 x x x x x
459 cf
->can_id
= (sid
& 0xffe0) >> 5;
462 if (msg
->dlc
& MCBA_DLC_RTR_MASK
)
463 cf
->can_id
|= CAN_RTR_FLAG
;
465 cf
->can_dlc
= get_can_dlc(msg
->dlc
& MCBA_DLC_MASK
);
467 memcpy(cf
->data
, msg
->data
, cf
->can_dlc
);
470 stats
->rx_bytes
+= cf
->can_dlc
;
472 can_led_event(priv
->netdev
, CAN_LED_EVENT_RX
);
476 static void mcba_usb_process_ka_usb(struct mcba_priv
*priv
,
477 struct mcba_usb_msg_ka_usb
*msg
)
479 if (unlikely(priv
->usb_ka_first_pass
)) {
480 netdev_info(priv
->netdev
, "PIC USB version %hhu.%hhu\n",
481 msg
->soft_ver_major
, msg
->soft_ver_minor
);
483 priv
->usb_ka_first_pass
= false;
486 if (msg
->termination_state
)
487 priv
->can
.termination
= MCBA_TERMINATION_ENABLED
;
489 priv
->can
.termination
= MCBA_TERMINATION_DISABLED
;
492 static u32
convert_can2host_bitrate(struct mcba_usb_msg_ka_can
*msg
)
494 const u32 bitrate
= get_unaligned_be16(&msg
->can_bitrate
);
496 if ((bitrate
== 33) || (bitrate
== 83))
497 return bitrate
* 1000 + 333;
499 return bitrate
* 1000;
502 static void mcba_usb_process_ka_can(struct mcba_priv
*priv
,
503 struct mcba_usb_msg_ka_can
*msg
)
505 if (unlikely(priv
->can_ka_first_pass
)) {
506 netdev_info(priv
->netdev
, "PIC CAN version %hhu.%hhu\n",
507 msg
->soft_ver_major
, msg
->soft_ver_minor
);
509 priv
->can_ka_first_pass
= false;
512 if (unlikely(priv
->can_speed_check
)) {
513 const u32 bitrate
= convert_can2host_bitrate(msg
);
515 priv
->can_speed_check
= false;
517 if (bitrate
!= priv
->can
.bittiming
.bitrate
)
520 "Wrong bitrate reported by the device (%u). Expected %u",
521 bitrate
, priv
->can
.bittiming
.bitrate
);
524 priv
->bec
.txerr
= msg
->tx_err_cnt
;
525 priv
->bec
.rxerr
= msg
->rx_err_cnt
;
528 priv
->can
.state
= CAN_STATE_BUS_OFF
;
530 else if ((priv
->bec
.txerr
> MCBA_CAN_STATE_ERR_PSV_TH
) ||
531 (priv
->bec
.rxerr
> MCBA_CAN_STATE_ERR_PSV_TH
))
532 priv
->can
.state
= CAN_STATE_ERROR_PASSIVE
;
534 else if ((priv
->bec
.txerr
> MCBA_CAN_STATE_WRN_TH
) ||
535 (priv
->bec
.rxerr
> MCBA_CAN_STATE_WRN_TH
))
536 priv
->can
.state
= CAN_STATE_ERROR_WARNING
;
539 static void mcba_usb_process_rx(struct mcba_priv
*priv
,
540 struct mcba_usb_msg
*msg
)
542 switch (msg
->cmd_id
) {
543 case MBCA_CMD_I_AM_ALIVE_FROM_CAN
:
544 mcba_usb_process_ka_can(priv
,
545 (struct mcba_usb_msg_ka_can
*)msg
);
548 case MBCA_CMD_I_AM_ALIVE_FROM_USB
:
549 mcba_usb_process_ka_usb(priv
,
550 (struct mcba_usb_msg_ka_usb
*)msg
);
553 case MBCA_CMD_RECEIVE_MESSAGE
:
554 mcba_usb_process_can(priv
, (struct mcba_usb_msg_can
*)msg
);
557 case MBCA_CMD_NOTHING_TO_SEND
:
558 /* Side effect of communication between PIC_USB and PIC_CAN.
559 * PIC_CAN is telling us that it has nothing to send
563 case MBCA_CMD_TRANSMIT_MESSAGE_RSP
:
564 /* Transmission response from the device containing timestamp */
568 netdev_warn(priv
->netdev
, "Unsupported msg (0x%hhX)",
574 /* Callback for reading data from device
576 * Check urb status, call read function and resubmit urb read operation.
578 static void mcba_usb_read_bulk_callback(struct urb
*urb
)
580 struct mcba_priv
*priv
= urb
->context
;
581 struct net_device
*netdev
;
585 netdev
= priv
->netdev
;
587 if (!netif_device_present(netdev
))
590 switch (urb
->status
) {
591 case 0: /* success */
601 netdev_info(netdev
, "Rx URB aborted (%d)\n", urb
->status
);
606 while (pos
< urb
->actual_length
) {
607 struct mcba_usb_msg
*msg
;
609 if (pos
+ sizeof(struct mcba_usb_msg
) > urb
->actual_length
) {
610 netdev_err(priv
->netdev
, "format error\n");
614 msg
= (struct mcba_usb_msg
*)(urb
->transfer_buffer
+ pos
);
615 mcba_usb_process_rx(priv
, msg
);
617 pos
+= sizeof(struct mcba_usb_msg
);
622 usb_fill_bulk_urb(urb
, priv
->udev
,
623 usb_rcvbulkpipe(priv
->udev
, MCBA_USB_EP_OUT
),
624 urb
->transfer_buffer
, MCBA_USB_RX_BUFF_SIZE
,
625 mcba_usb_read_bulk_callback
, priv
);
627 retval
= usb_submit_urb(urb
, GFP_ATOMIC
);
629 if (retval
== -ENODEV
)
630 netif_device_detach(netdev
);
632 netdev_err(netdev
, "failed resubmitting read bulk urb: %d\n",
636 /* Start USB device */
637 static int mcba_usb_start(struct mcba_priv
*priv
)
639 struct net_device
*netdev
= priv
->netdev
;
644 for (i
= 0; i
< MCBA_MAX_RX_URBS
; i
++) {
645 struct urb
*urb
= NULL
;
648 /* create a URB, and a buffer for it */
649 urb
= usb_alloc_urb(0, GFP_KERNEL
);
655 buf
= usb_alloc_coherent(priv
->udev
, MCBA_USB_RX_BUFF_SIZE
,
656 GFP_KERNEL
, &urb
->transfer_dma
);
658 netdev_err(netdev
, "No memory left for USB buffer\n");
664 usb_fill_bulk_urb(urb
, priv
->udev
,
665 usb_rcvbulkpipe(priv
->udev
, MCBA_USB_EP_IN
),
666 buf
, MCBA_USB_RX_BUFF_SIZE
,
667 mcba_usb_read_bulk_callback
, priv
);
668 urb
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
669 usb_anchor_urb(urb
, &priv
->rx_submitted
);
671 err
= usb_submit_urb(urb
, GFP_KERNEL
);
673 usb_unanchor_urb(urb
);
674 usb_free_coherent(priv
->udev
, MCBA_USB_RX_BUFF_SIZE
,
675 buf
, urb
->transfer_dma
);
680 /* Drop reference, USB core will take care of freeing it */
684 /* Did we submit any URBs */
686 netdev_warn(netdev
, "couldn't setup read URBs\n");
690 /* Warn if we've couldn't transmit all the URBs */
691 if (i
< MCBA_MAX_RX_URBS
)
692 netdev_warn(netdev
, "rx performance may be slow\n");
694 mcba_usb_xmit_read_fw_ver(priv
, MCBA_VER_REQ_USB
);
695 mcba_usb_xmit_read_fw_ver(priv
, MCBA_VER_REQ_CAN
);
700 /* Open USB device */
701 static int mcba_usb_open(struct net_device
*netdev
)
703 struct mcba_priv
*priv
= netdev_priv(netdev
);
707 err
= open_candev(netdev
);
711 priv
->can_speed_check
= true;
712 priv
->can
.state
= CAN_STATE_ERROR_ACTIVE
;
714 can_led_event(netdev
, CAN_LED_EVENT_OPEN
);
715 netif_start_queue(netdev
);
720 static void mcba_urb_unlink(struct mcba_priv
*priv
)
722 usb_kill_anchored_urbs(&priv
->rx_submitted
);
723 usb_kill_anchored_urbs(&priv
->tx_submitted
);
726 /* Close USB device */
727 static int mcba_usb_close(struct net_device
*netdev
)
729 struct mcba_priv
*priv
= netdev_priv(netdev
);
731 priv
->can
.state
= CAN_STATE_STOPPED
;
733 netif_stop_queue(netdev
);
736 mcba_urb_unlink(priv
);
738 close_candev(netdev
);
739 can_led_event(netdev
, CAN_LED_EVENT_STOP
);
744 /* Set network device mode
746 * Maybe we should leave this function empty, because the device
747 * set mode variable with open command.
749 static int mcba_net_set_mode(struct net_device
*netdev
, enum can_mode mode
)
754 static int mcba_net_get_berr_counter(const struct net_device
*netdev
,
755 struct can_berr_counter
*bec
)
757 struct mcba_priv
*priv
= netdev_priv(netdev
);
759 bec
->txerr
= priv
->bec
.txerr
;
760 bec
->rxerr
= priv
->bec
.rxerr
;
765 static const struct net_device_ops mcba_netdev_ops
= {
766 .ndo_open
= mcba_usb_open
,
767 .ndo_stop
= mcba_usb_close
,
768 .ndo_start_xmit
= mcba_usb_start_xmit
,
771 /* Microchip CANBUS has hardcoded bittiming values by default.
772 * This function sends request via USB to change the speed and align bittiming
773 * values for presentation purposes only
775 static int mcba_net_set_bittiming(struct net_device
*netdev
)
777 struct mcba_priv
*priv
= netdev_priv(netdev
);
778 const u16 bitrate_kbps
= priv
->can
.bittiming
.bitrate
/ 1000;
780 mcba_usb_xmit_change_bitrate(priv
, bitrate_kbps
);
785 static int mcba_set_termination(struct net_device
*netdev
, u16 term
)
787 struct mcba_priv
*priv
= netdev_priv(netdev
);
788 struct mcba_usb_msg_termination usb_msg
= {
789 .cmd_id
= MBCA_CMD_SETUP_TERMINATION_RESISTANCE
792 if (term
== MCBA_TERMINATION_ENABLED
)
793 usb_msg
.termination
= 1;
795 usb_msg
.termination
= 0;
797 mcba_usb_xmit_cmd(priv
, (struct mcba_usb_msg
*)&usb_msg
);
802 static int mcba_usb_probe(struct usb_interface
*intf
,
803 const struct usb_device_id
*id
)
805 struct net_device
*netdev
;
806 struct mcba_priv
*priv
;
808 struct usb_device
*usbdev
= interface_to_usbdev(intf
);
810 netdev
= alloc_candev(sizeof(struct mcba_priv
), MCBA_MAX_TX_URBS
);
812 dev_err(&intf
->dev
, "Couldn't alloc candev\n");
816 priv
= netdev_priv(netdev
);
819 priv
->netdev
= netdev
;
820 priv
->usb_ka_first_pass
= true;
821 priv
->can_ka_first_pass
= true;
822 priv
->can_speed_check
= false;
824 init_usb_anchor(&priv
->rx_submitted
);
825 init_usb_anchor(&priv
->tx_submitted
);
827 usb_set_intfdata(intf
, priv
);
829 /* Init CAN device */
830 priv
->can
.state
= CAN_STATE_STOPPED
;
831 priv
->can
.termination_const
= mcba_termination
;
832 priv
->can
.termination_const_cnt
= ARRAY_SIZE(mcba_termination
);
833 priv
->can
.bitrate_const
= mcba_bitrate
;
834 priv
->can
.bitrate_const_cnt
= ARRAY_SIZE(mcba_bitrate
);
836 priv
->can
.do_set_termination
= mcba_set_termination
;
837 priv
->can
.do_set_mode
= mcba_net_set_mode
;
838 priv
->can
.do_get_berr_counter
= mcba_net_get_berr_counter
;
839 priv
->can
.do_set_bittiming
= mcba_net_set_bittiming
;
841 netdev
->netdev_ops
= &mcba_netdev_ops
;
843 netdev
->flags
|= IFF_ECHO
; /* we support local echo */
845 SET_NETDEV_DEV(netdev
, &intf
->dev
);
847 err
= register_candev(netdev
);
849 netdev_err(netdev
, "couldn't register CAN device: %d\n", err
);
851 goto cleanup_free_candev
;
854 devm_can_led_init(netdev
);
856 /* Start USB dev only if we have successfully registered CAN device */
857 err
= mcba_usb_start(priv
);
860 netif_device_detach(priv
->netdev
);
862 netdev_warn(netdev
, "couldn't start device: %d\n", err
);
864 goto cleanup_unregister_candev
;
867 dev_info(&intf
->dev
, "Microchip CAN BUS Analyzer connected\n");
871 cleanup_unregister_candev
:
872 unregister_candev(priv
->netdev
);
880 /* Called by the usb core when driver is unloaded or device is removed */
881 static void mcba_usb_disconnect(struct usb_interface
*intf
)
883 struct mcba_priv
*priv
= usb_get_intfdata(intf
);
885 usb_set_intfdata(intf
, NULL
);
887 netdev_info(priv
->netdev
, "device disconnected\n");
889 unregister_candev(priv
->netdev
);
890 mcba_urb_unlink(priv
);
891 free_candev(priv
->netdev
);
894 static struct usb_driver mcba_usb_driver
= {
895 .name
= MCBA_MODULE_NAME
,
896 .probe
= mcba_usb_probe
,
897 .disconnect
= mcba_usb_disconnect
,
898 .id_table
= mcba_usb_table
,
901 module_usb_driver(mcba_usb_driver
);
903 MODULE_AUTHOR("Remigiusz Kołłątaj <remigiusz.kollataj@mobica.com>");
904 MODULE_DESCRIPTION("SocketCAN driver for Microchip CAN BUS Analyzer Tool");
905 MODULE_LICENSE("GPL v2");