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