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