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