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