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