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