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