1
2
3
4
5
6
7
8
9#include <asm/unaligned.h>
10#include <linux/can.h>
11#include <linux/can/dev.h>
12#include <linux/can/error.h>
13#include <linux/module.h>
14#include <linux/netdevice.h>
15#include <linux/signal.h>
16#include <linux/slab.h>
17#include <linux/usb.h>
18
19
20#define MCBA_MODULE_NAME "mcba_usb"
21#define MCBA_VENDOR_ID 0x04d8
22#define MCBA_PRODUCT_ID 0x0a30
23
24
25#define MCBA_MAX_RX_URBS 20
26#define MCBA_MAX_TX_URBS 20
27#define MCBA_CTX_FREE MCBA_MAX_TX_URBS
28
29
30
31
32#define MCBA_USB_RX_BUFF_SIZE 64
33#define MCBA_USB_TX_BUFF_SIZE (sizeof(struct mcba_usb_msg))
34
35
36#define MBCA_CMD_RECEIVE_MESSAGE 0xE3
37#define MBCA_CMD_I_AM_ALIVE_FROM_CAN 0xF5
38#define MBCA_CMD_I_AM_ALIVE_FROM_USB 0xF7
39#define MBCA_CMD_CHANGE_BIT_RATE 0xA1
40#define MBCA_CMD_TRANSMIT_MESSAGE_EV 0xA3
41#define MBCA_CMD_SETUP_TERMINATION_RESISTANCE 0xA8
42#define MBCA_CMD_READ_FW_VERSION 0xA9
43#define MBCA_CMD_NOTHING_TO_SEND 0xFF
44#define MBCA_CMD_TRANSMIT_MESSAGE_RSP 0xE2
45
46#define MCBA_VER_REQ_USB 1
47#define MCBA_VER_REQ_CAN 2
48
49#define MCBA_SIDL_EXID_MASK 0x8
50#define MCBA_DLC_MASK 0xf
51#define MCBA_DLC_RTR_MASK 0x40
52
53#define MCBA_CAN_STATE_WRN_TH 95
54#define MCBA_CAN_STATE_ERR_PSV_TH 127
55
56#define MCBA_TERMINATION_DISABLED CAN_TERMINATION_DISABLED
57#define MCBA_TERMINATION_ENABLED 120
58
59struct mcba_usb_ctx {
60 struct mcba_priv *priv;
61 u32 ndx;
62 bool can;
63};
64
65
66struct mcba_priv {
67 struct can_priv can;
68 struct sk_buff *echo_skb[MCBA_MAX_TX_URBS];
69 struct mcba_usb_ctx tx_context[MCBA_MAX_TX_URBS];
70 struct usb_device *udev;
71 struct net_device *netdev;
72 struct usb_anchor tx_submitted;
73 struct usb_anchor rx_submitted;
74 struct can_berr_counter bec;
75 bool usb_ka_first_pass;
76 bool can_ka_first_pass;
77 bool can_speed_check;
78 atomic_t free_ctx_cnt;
79 void *rxbuf[MCBA_MAX_RX_URBS];
80 dma_addr_t rxbuf_dma[MCBA_MAX_RX_URBS];
81 int rx_pipe;
82 int tx_pipe;
83};
84
85
86struct __packed mcba_usb_msg_can {
87 u8 cmd_id;
88 __be16 eid;
89 __be16 sid;
90 u8 dlc;
91 u8 data[8];
92 u8 timestamp[4];
93 u8 checksum;
94};
95
96
97struct __packed mcba_usb_msg {
98 u8 cmd_id;
99 u8 unused[18];
100};
101
102struct __packed mcba_usb_msg_ka_usb {
103 u8 cmd_id;
104 u8 termination_state;
105 u8 soft_ver_major;
106 u8 soft_ver_minor;
107 u8 unused[15];
108};
109
110struct __packed mcba_usb_msg_ka_can {
111 u8 cmd_id;
112 u8 tx_err_cnt;
113 u8 rx_err_cnt;
114 u8 rx_buff_ovfl;
115 u8 tx_bus_off;
116 __be16 can_bitrate;
117 __le16 rx_lost;
118 u8 can_stat;
119 u8 soft_ver_major;
120 u8 soft_ver_minor;
121 u8 debug_mode;
122 u8 test_complete;
123 u8 test_result;
124 u8 unused[4];
125};
126
127struct __packed mcba_usb_msg_change_bitrate {
128 u8 cmd_id;
129 __be16 bitrate;
130 u8 unused[16];
131};
132
133struct __packed mcba_usb_msg_termination {
134 u8 cmd_id;
135 u8 termination;
136 u8 unused[17];
137};
138
139struct __packed mcba_usb_msg_fw_ver {
140 u8 cmd_id;
141 u8 pic;
142 u8 unused[17];
143};
144
145static const struct usb_device_id mcba_usb_table[] = {
146 { USB_DEVICE(MCBA_VENDOR_ID, MCBA_PRODUCT_ID) },
147 {}
148};
149
150MODULE_DEVICE_TABLE(usb, mcba_usb_table);
151
152static const u16 mcba_termination[] = { MCBA_TERMINATION_DISABLED,
153 MCBA_TERMINATION_ENABLED };
154
155static const u32 mcba_bitrate[] = { 20000, 33333, 50000, 80000, 83333,
156 100000, 125000, 150000, 175000, 200000,
157 225000, 250000, 275000, 300000, 500000,
158 625000, 800000, 1000000 };
159
160static inline void mcba_init_ctx(struct mcba_priv *priv)
161{
162 int i = 0;
163
164 for (i = 0; i < MCBA_MAX_TX_URBS; i++) {
165 priv->tx_context[i].ndx = MCBA_CTX_FREE;
166 priv->tx_context[i].priv = priv;
167 }
168
169 atomic_set(&priv->free_ctx_cnt, ARRAY_SIZE(priv->tx_context));
170}
171
172static inline struct mcba_usb_ctx *mcba_usb_get_free_ctx(struct mcba_priv *priv,
173 struct can_frame *cf)
174{
175 int i = 0;
176 struct mcba_usb_ctx *ctx = NULL;
177
178 for (i = 0; i < MCBA_MAX_TX_URBS; i++) {
179 if (priv->tx_context[i].ndx == MCBA_CTX_FREE) {
180 ctx = &priv->tx_context[i];
181 ctx->ndx = i;
182
183 if (cf)
184 ctx->can = true;
185 else
186 ctx->can = false;
187
188 atomic_dec(&priv->free_ctx_cnt);
189 break;
190 }
191 }
192
193 if (!atomic_read(&priv->free_ctx_cnt))
194
195 netif_stop_queue(priv->netdev);
196
197 return ctx;
198}
199
200
201
202
203static inline void mcba_usb_free_ctx(struct mcba_usb_ctx *ctx)
204{
205
206 atomic_inc(&ctx->priv->free_ctx_cnt);
207
208 ctx->ndx = MCBA_CTX_FREE;
209
210
211 netif_wake_queue(ctx->priv->netdev);
212}
213
214static void mcba_usb_write_bulk_callback(struct urb *urb)
215{
216 struct mcba_usb_ctx *ctx = urb->context;
217 struct net_device *netdev;
218
219 WARN_ON(!ctx);
220
221 netdev = ctx->priv->netdev;
222
223
224 usb_free_coherent(urb->dev, urb->transfer_buffer_length,
225 urb->transfer_buffer, urb->transfer_dma);
226
227 if (ctx->can) {
228 if (!netif_device_present(netdev))
229 return;
230
231 netdev->stats.tx_packets++;
232 netdev->stats.tx_bytes += can_get_echo_skb(netdev, ctx->ndx,
233 NULL);
234 }
235
236 if (urb->status)
237 netdev_info(netdev, "Tx URB aborted (%d)\n", urb->status);
238
239
240 mcba_usb_free_ctx(ctx);
241}
242
243
244static netdev_tx_t mcba_usb_xmit(struct mcba_priv *priv,
245 struct mcba_usb_msg *usb_msg,
246 struct mcba_usb_ctx *ctx)
247{
248 struct urb *urb;
249 u8 *buf;
250 int err;
251
252
253 urb = usb_alloc_urb(0, GFP_ATOMIC);
254 if (!urb)
255 return -ENOMEM;
256
257 buf = usb_alloc_coherent(priv->udev, MCBA_USB_TX_BUFF_SIZE, GFP_ATOMIC,
258 &urb->transfer_dma);
259 if (!buf) {
260 err = -ENOMEM;
261 goto nomembuf;
262 }
263
264 memcpy(buf, usb_msg, MCBA_USB_TX_BUFF_SIZE);
265
266 usb_fill_bulk_urb(urb, priv->udev, priv->tx_pipe, buf, MCBA_USB_TX_BUFF_SIZE,
267 mcba_usb_write_bulk_callback, ctx);
268
269 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
270 usb_anchor_urb(urb, &priv->tx_submitted);
271
272 err = usb_submit_urb(urb, GFP_ATOMIC);
273 if (unlikely(err))
274 goto failed;
275
276
277
278
279 usb_free_urb(urb);
280
281 return 0;
282
283failed:
284 usb_unanchor_urb(urb);
285 usb_free_coherent(priv->udev, MCBA_USB_TX_BUFF_SIZE, buf,
286 urb->transfer_dma);
287
288 if (err == -ENODEV)
289 netif_device_detach(priv->netdev);
290 else
291 netdev_warn(priv->netdev, "failed tx_urb %d\n", err);
292
293nomembuf:
294 usb_free_urb(urb);
295
296 return err;
297}
298
299
300static netdev_tx_t mcba_usb_start_xmit(struct sk_buff *skb,
301 struct net_device *netdev)
302{
303 struct mcba_priv *priv = netdev_priv(netdev);
304 struct can_frame *cf = (struct can_frame *)skb->data;
305 struct mcba_usb_ctx *ctx = NULL;
306 struct net_device_stats *stats = &priv->netdev->stats;
307 u16 sid;
308 int err;
309 struct mcba_usb_msg_can usb_msg = {
310 .cmd_id = MBCA_CMD_TRANSMIT_MESSAGE_EV
311 };
312
313 if (can_dropped_invalid_skb(netdev, skb))
314 return NETDEV_TX_OK;
315
316 ctx = mcba_usb_get_free_ctx(priv, cf);
317 if (!ctx)
318 return NETDEV_TX_BUSY;
319
320 if (cf->can_id & CAN_EFF_FLAG) {
321
322
323
324 sid = MCBA_SIDL_EXID_MASK;
325
326 sid |= (cf->can_id & 0x1ffc0000) >> 13;
327
328 sid |= (cf->can_id & 0x30000) >> 16;
329 put_unaligned_be16(sid, &usb_msg.sid);
330
331
332 put_unaligned_be16(cf->can_id & 0xffff, &usb_msg.eid);
333 } else {
334
335
336
337 put_unaligned_be16((cf->can_id & CAN_SFF_MASK) << 5,
338 &usb_msg.sid);
339 usb_msg.eid = 0;
340 }
341
342 usb_msg.dlc = cf->len;
343
344 memcpy(usb_msg.data, cf->data, usb_msg.dlc);
345
346 if (cf->can_id & CAN_RTR_FLAG)
347 usb_msg.dlc |= MCBA_DLC_RTR_MASK;
348
349 can_put_echo_skb(skb, priv->netdev, ctx->ndx, 0);
350
351 err = mcba_usb_xmit(priv, (struct mcba_usb_msg *)&usb_msg, ctx);
352 if (err)
353 goto xmit_failed;
354
355 return NETDEV_TX_OK;
356
357xmit_failed:
358 can_free_echo_skb(priv->netdev, ctx->ndx, NULL);
359 mcba_usb_free_ctx(ctx);
360 stats->tx_dropped++;
361
362 return NETDEV_TX_OK;
363}
364
365
366static void mcba_usb_xmit_cmd(struct mcba_priv *priv,
367 struct mcba_usb_msg *usb_msg)
368{
369 struct mcba_usb_ctx *ctx = NULL;
370 int err;
371
372 ctx = mcba_usb_get_free_ctx(priv, NULL);
373 if (!ctx) {
374 netdev_err(priv->netdev,
375 "Lack of free ctx. Sending (%d) cmd aborted",
376 usb_msg->cmd_id);
377
378 return;
379 }
380
381 err = mcba_usb_xmit(priv, usb_msg, ctx);
382 if (err)
383 netdev_err(priv->netdev, "Failed to send cmd (%d)",
384 usb_msg->cmd_id);
385}
386
387static void mcba_usb_xmit_change_bitrate(struct mcba_priv *priv, u16 bitrate)
388{
389 struct mcba_usb_msg_change_bitrate usb_msg = {
390 .cmd_id = MBCA_CMD_CHANGE_BIT_RATE
391 };
392
393 put_unaligned_be16(bitrate, &usb_msg.bitrate);
394
395 mcba_usb_xmit_cmd(priv, (struct mcba_usb_msg *)&usb_msg);
396}
397
398static void mcba_usb_xmit_read_fw_ver(struct mcba_priv *priv, u8 pic)
399{
400 struct mcba_usb_msg_fw_ver usb_msg = {
401 .cmd_id = MBCA_CMD_READ_FW_VERSION,
402 .pic = pic
403 };
404
405 mcba_usb_xmit_cmd(priv, (struct mcba_usb_msg *)&usb_msg);
406}
407
408static void mcba_usb_process_can(struct mcba_priv *priv,
409 struct mcba_usb_msg_can *msg)
410{
411 struct can_frame *cf;
412 struct sk_buff *skb;
413 struct net_device_stats *stats = &priv->netdev->stats;
414 u16 sid;
415
416 skb = alloc_can_skb(priv->netdev, &cf);
417 if (!skb)
418 return;
419
420 sid = get_unaligned_be16(&msg->sid);
421
422 if (sid & MCBA_SIDL_EXID_MASK) {
423
424
425
426 cf->can_id = CAN_EFF_FLAG;
427
428
429 cf->can_id |= (sid & 0xffe0) << 13;
430
431 cf->can_id |= (sid & 3) << 16;
432
433 cf->can_id |= get_unaligned_be16(&msg->eid);
434 } else {
435
436
437
438 cf->can_id = (sid & 0xffe0) >> 5;
439 }
440
441 cf->len = can_cc_dlc2len(msg->dlc & MCBA_DLC_MASK);
442
443 if (msg->dlc & MCBA_DLC_RTR_MASK) {
444 cf->can_id |= CAN_RTR_FLAG;
445 } else {
446 memcpy(cf->data, msg->data, cf->len);
447
448 stats->rx_bytes += cf->len;
449 }
450 stats->rx_packets++;
451
452 netif_rx(skb);
453}
454
455static void mcba_usb_process_ka_usb(struct mcba_priv *priv,
456 struct mcba_usb_msg_ka_usb *msg)
457{
458 if (unlikely(priv->usb_ka_first_pass)) {
459 netdev_info(priv->netdev, "PIC USB version %u.%u\n",
460 msg->soft_ver_major, msg->soft_ver_minor);
461
462 priv->usb_ka_first_pass = false;
463 }
464
465 if (msg->termination_state)
466 priv->can.termination = MCBA_TERMINATION_ENABLED;
467 else
468 priv->can.termination = MCBA_TERMINATION_DISABLED;
469}
470
471static u32 convert_can2host_bitrate(struct mcba_usb_msg_ka_can *msg)
472{
473 const u32 bitrate = get_unaligned_be16(&msg->can_bitrate);
474
475 if ((bitrate == 33) || (bitrate == 83))
476 return bitrate * 1000 + 333;
477 else
478 return bitrate * 1000;
479}
480
481static void mcba_usb_process_ka_can(struct mcba_priv *priv,
482 struct mcba_usb_msg_ka_can *msg)
483{
484 if (unlikely(priv->can_ka_first_pass)) {
485 netdev_info(priv->netdev, "PIC CAN version %u.%u\n",
486 msg->soft_ver_major, msg->soft_ver_minor);
487
488 priv->can_ka_first_pass = false;
489 }
490
491 if (unlikely(priv->can_speed_check)) {
492 const u32 bitrate = convert_can2host_bitrate(msg);
493
494 priv->can_speed_check = false;
495
496 if (bitrate != priv->can.bittiming.bitrate)
497 netdev_err(
498 priv->netdev,
499 "Wrong bitrate reported by the device (%u). Expected %u",
500 bitrate, priv->can.bittiming.bitrate);
501 }
502
503 priv->bec.txerr = msg->tx_err_cnt;
504 priv->bec.rxerr = msg->rx_err_cnt;
505
506 if (msg->tx_bus_off)
507 priv->can.state = CAN_STATE_BUS_OFF;
508
509 else if ((priv->bec.txerr > MCBA_CAN_STATE_ERR_PSV_TH) ||
510 (priv->bec.rxerr > MCBA_CAN_STATE_ERR_PSV_TH))
511 priv->can.state = CAN_STATE_ERROR_PASSIVE;
512
513 else if ((priv->bec.txerr > MCBA_CAN_STATE_WRN_TH) ||
514 (priv->bec.rxerr > MCBA_CAN_STATE_WRN_TH))
515 priv->can.state = CAN_STATE_ERROR_WARNING;
516}
517
518static void mcba_usb_process_rx(struct mcba_priv *priv,
519 struct mcba_usb_msg *msg)
520{
521 switch (msg->cmd_id) {
522 case MBCA_CMD_I_AM_ALIVE_FROM_CAN:
523 mcba_usb_process_ka_can(priv,
524 (struct mcba_usb_msg_ka_can *)msg);
525 break;
526
527 case MBCA_CMD_I_AM_ALIVE_FROM_USB:
528 mcba_usb_process_ka_usb(priv,
529 (struct mcba_usb_msg_ka_usb *)msg);
530 break;
531
532 case MBCA_CMD_RECEIVE_MESSAGE:
533 mcba_usb_process_can(priv, (struct mcba_usb_msg_can *)msg);
534 break;
535
536 case MBCA_CMD_NOTHING_TO_SEND:
537
538
539
540 break;
541
542 case MBCA_CMD_TRANSMIT_MESSAGE_RSP:
543
544 break;
545
546 default:
547 netdev_warn(priv->netdev, "Unsupported msg (0x%X)",
548 msg->cmd_id);
549 break;
550 }
551}
552
553
554
555
556
557static void mcba_usb_read_bulk_callback(struct urb *urb)
558{
559 struct mcba_priv *priv = urb->context;
560 struct net_device *netdev;
561 int retval;
562 int pos = 0;
563
564 netdev = priv->netdev;
565
566 if (!netif_device_present(netdev))
567 return;
568
569 switch (urb->status) {
570 case 0:
571 break;
572
573 case -ENOENT:
574 case -EPIPE:
575 case -EPROTO:
576 case -ESHUTDOWN:
577 return;
578
579 default:
580 netdev_info(netdev, "Rx URB aborted (%d)\n", urb->status);
581
582 goto resubmit_urb;
583 }
584
585 while (pos < urb->actual_length) {
586 struct mcba_usb_msg *msg;
587
588 if (pos + sizeof(struct mcba_usb_msg) > urb->actual_length) {
589 netdev_err(priv->netdev, "format error\n");
590 break;
591 }
592
593 msg = (struct mcba_usb_msg *)(urb->transfer_buffer + pos);
594 mcba_usb_process_rx(priv, msg);
595
596 pos += sizeof(struct mcba_usb_msg);
597 }
598
599resubmit_urb:
600
601 usb_fill_bulk_urb(urb, priv->udev,
602 priv->rx_pipe,
603 urb->transfer_buffer, MCBA_USB_RX_BUFF_SIZE,
604 mcba_usb_read_bulk_callback, priv);
605
606 retval = usb_submit_urb(urb, GFP_ATOMIC);
607
608 if (retval == -ENODEV)
609 netif_device_detach(netdev);
610 else if (retval)
611 netdev_err(netdev, "failed resubmitting read bulk urb: %d\n",
612 retval);
613}
614
615
616static int mcba_usb_start(struct mcba_priv *priv)
617{
618 struct net_device *netdev = priv->netdev;
619 int err, i;
620
621 mcba_init_ctx(priv);
622
623 for (i = 0; i < MCBA_MAX_RX_URBS; i++) {
624 struct urb *urb = NULL;
625 u8 *buf;
626 dma_addr_t buf_dma;
627
628
629 urb = usb_alloc_urb(0, GFP_KERNEL);
630 if (!urb) {
631 err = -ENOMEM;
632 break;
633 }
634
635 buf = usb_alloc_coherent(priv->udev, MCBA_USB_RX_BUFF_SIZE,
636 GFP_KERNEL, &buf_dma);
637 if (!buf) {
638 netdev_err(netdev, "No memory left for USB buffer\n");
639 usb_free_urb(urb);
640 err = -ENOMEM;
641 break;
642 }
643
644 urb->transfer_dma = buf_dma;
645
646 usb_fill_bulk_urb(urb, priv->udev,
647 priv->rx_pipe,
648 buf, MCBA_USB_RX_BUFF_SIZE,
649 mcba_usb_read_bulk_callback, priv);
650 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
651 usb_anchor_urb(urb, &priv->rx_submitted);
652
653 err = usb_submit_urb(urb, GFP_KERNEL);
654 if (err) {
655 usb_unanchor_urb(urb);
656 usb_free_coherent(priv->udev, MCBA_USB_RX_BUFF_SIZE,
657 buf, buf_dma);
658 usb_free_urb(urb);
659 break;
660 }
661
662 priv->rxbuf[i] = buf;
663 priv->rxbuf_dma[i] = buf_dma;
664
665
666 usb_free_urb(urb);
667 }
668
669
670 if (i == 0) {
671 netdev_warn(netdev, "couldn't setup read URBs\n");
672 return err;
673 }
674
675
676 if (i < MCBA_MAX_RX_URBS)
677 netdev_warn(netdev, "rx performance may be slow\n");
678
679 mcba_usb_xmit_read_fw_ver(priv, MCBA_VER_REQ_USB);
680 mcba_usb_xmit_read_fw_ver(priv, MCBA_VER_REQ_CAN);
681
682 return err;
683}
684
685
686static int mcba_usb_open(struct net_device *netdev)
687{
688 struct mcba_priv *priv = netdev_priv(netdev);
689 int err;
690
691
692 err = open_candev(netdev);
693 if (err)
694 return err;
695
696 priv->can_speed_check = true;
697 priv->can.state = CAN_STATE_ERROR_ACTIVE;
698
699 netif_start_queue(netdev);
700
701 return 0;
702}
703
704static void mcba_urb_unlink(struct mcba_priv *priv)
705{
706 int i;
707
708 usb_kill_anchored_urbs(&priv->rx_submitted);
709
710 for (i = 0; i < MCBA_MAX_RX_URBS; ++i)
711 usb_free_coherent(priv->udev, MCBA_USB_RX_BUFF_SIZE,
712 priv->rxbuf[i], priv->rxbuf_dma[i]);
713
714 usb_kill_anchored_urbs(&priv->tx_submitted);
715}
716
717
718static int mcba_usb_close(struct net_device *netdev)
719{
720 struct mcba_priv *priv = netdev_priv(netdev);
721
722 priv->can.state = CAN_STATE_STOPPED;
723
724 netif_stop_queue(netdev);
725
726
727 mcba_urb_unlink(priv);
728
729 close_candev(netdev);
730
731 return 0;
732}
733
734
735
736
737
738
739static int mcba_net_set_mode(struct net_device *netdev, enum can_mode mode)
740{
741 return 0;
742}
743
744static int mcba_net_get_berr_counter(const struct net_device *netdev,
745 struct can_berr_counter *bec)
746{
747 struct mcba_priv *priv = netdev_priv(netdev);
748
749 bec->txerr = priv->bec.txerr;
750 bec->rxerr = priv->bec.rxerr;
751
752 return 0;
753}
754
755static const struct net_device_ops mcba_netdev_ops = {
756 .ndo_open = mcba_usb_open,
757 .ndo_stop = mcba_usb_close,
758 .ndo_start_xmit = mcba_usb_start_xmit,
759};
760
761
762
763
764
765static int mcba_net_set_bittiming(struct net_device *netdev)
766{
767 struct mcba_priv *priv = netdev_priv(netdev);
768 const u16 bitrate_kbps = priv->can.bittiming.bitrate / 1000;
769
770 mcba_usb_xmit_change_bitrate(priv, bitrate_kbps);
771
772 return 0;
773}
774
775static int mcba_set_termination(struct net_device *netdev, u16 term)
776{
777 struct mcba_priv *priv = netdev_priv(netdev);
778 struct mcba_usb_msg_termination usb_msg = {
779 .cmd_id = MBCA_CMD_SETUP_TERMINATION_RESISTANCE
780 };
781
782 if (term == MCBA_TERMINATION_ENABLED)
783 usb_msg.termination = 1;
784 else
785 usb_msg.termination = 0;
786
787 mcba_usb_xmit_cmd(priv, (struct mcba_usb_msg *)&usb_msg);
788
789 return 0;
790}
791
792static int mcba_usb_probe(struct usb_interface *intf,
793 const struct usb_device_id *id)
794{
795 struct net_device *netdev;
796 struct mcba_priv *priv;
797 int err;
798 struct usb_device *usbdev = interface_to_usbdev(intf);
799 struct usb_endpoint_descriptor *in, *out;
800
801 err = usb_find_common_endpoints(intf->cur_altsetting, &in, &out, NULL, NULL);
802 if (err) {
803 dev_err(&intf->dev, "Can't find endpoints\n");
804 return err;
805 }
806
807 netdev = alloc_candev(sizeof(struct mcba_priv), MCBA_MAX_TX_URBS);
808 if (!netdev) {
809 dev_err(&intf->dev, "Couldn't alloc candev\n");
810 return -ENOMEM;
811 }
812
813 priv = netdev_priv(netdev);
814
815 priv->udev = usbdev;
816 priv->netdev = netdev;
817 priv->usb_ka_first_pass = true;
818 priv->can_ka_first_pass = true;
819 priv->can_speed_check = false;
820
821 init_usb_anchor(&priv->rx_submitted);
822 init_usb_anchor(&priv->tx_submitted);
823
824 usb_set_intfdata(intf, priv);
825
826
827 priv->can.state = CAN_STATE_STOPPED;
828 priv->can.termination_const = mcba_termination;
829 priv->can.termination_const_cnt = ARRAY_SIZE(mcba_termination);
830 priv->can.bitrate_const = mcba_bitrate;
831 priv->can.bitrate_const_cnt = ARRAY_SIZE(mcba_bitrate);
832
833 priv->can.do_set_termination = mcba_set_termination;
834 priv->can.do_set_mode = mcba_net_set_mode;
835 priv->can.do_get_berr_counter = mcba_net_get_berr_counter;
836 priv->can.do_set_bittiming = mcba_net_set_bittiming;
837
838 netdev->netdev_ops = &mcba_netdev_ops;
839
840 netdev->flags |= IFF_ECHO;
841
842 SET_NETDEV_DEV(netdev, &intf->dev);
843
844 err = register_candev(netdev);
845 if (err) {
846 netdev_err(netdev, "couldn't register CAN device: %d\n", err);
847
848 goto cleanup_free_candev;
849 }
850
851 priv->rx_pipe = usb_rcvbulkpipe(priv->udev, in->bEndpointAddress);
852 priv->tx_pipe = usb_sndbulkpipe(priv->udev, out->bEndpointAddress);
853
854
855 err = mcba_usb_start(priv);
856 if (err) {
857 if (err == -ENODEV)
858 netif_device_detach(priv->netdev);
859
860 netdev_warn(netdev, "couldn't start device: %d\n", err);
861
862 goto cleanup_unregister_candev;
863 }
864
865 dev_info(&intf->dev, "Microchip CAN BUS Analyzer connected\n");
866
867 return 0;
868
869cleanup_unregister_candev:
870 unregister_candev(priv->netdev);
871
872cleanup_free_candev:
873 free_candev(netdev);
874
875 return err;
876}
877
878
879static void mcba_usb_disconnect(struct usb_interface *intf)
880{
881 struct mcba_priv *priv = usb_get_intfdata(intf);
882
883 usb_set_intfdata(intf, NULL);
884
885 netdev_info(priv->netdev, "device disconnected\n");
886
887 unregister_candev(priv->netdev);
888 mcba_urb_unlink(priv);
889 free_candev(priv->netdev);
890}
891
892static struct usb_driver mcba_usb_driver = {
893 .name = MCBA_MODULE_NAME,
894 .probe = mcba_usb_probe,
895 .disconnect = mcba_usb_disconnect,
896 .id_table = mcba_usb_table,
897};
898
899module_usb_driver(mcba_usb_driver);
900
901MODULE_AUTHOR("Remigiusz Kołłątaj <remigiusz.kollataj@mobica.com>");
902MODULE_DESCRIPTION("SocketCAN driver for Microchip CAN BUS Analyzer Tool");
903MODULE_LICENSE("GPL v2");
904