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