xref: /openbmc/linux/drivers/net/can/usb/usb_8dev.c (revision 5f32c314)
1 /*
2  * CAN driver for "8 devices" USB2CAN converter
3  *
4  * Copyright (C) 2012 Bernd Krumboeck (krumboeck@universalnet.at)
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published
8  * by the Free Software Foundation; version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program.
17  *
18  * This driver is inspired by the 3.2.0 version of drivers/net/can/usb/ems_usb.c
19  * and drivers/net/can/usb/esd_usb2.c
20  *
21  * Many thanks to Gerhard Bertelsmann (info@gerhard-bertelsmann.de)
22  * for testing and fixing this driver. Also many thanks to "8 devices",
23  * who were very cooperative and answered my questions.
24  */
25 
26 #include <linux/signal.h>
27 #include <linux/slab.h>
28 #include <linux/module.h>
29 #include <linux/netdevice.h>
30 #include <linux/usb.h>
31 
32 #include <linux/can.h>
33 #include <linux/can/dev.h>
34 #include <linux/can/error.h>
35 #include <linux/can/led.h>
36 
37 /* driver constants */
38 #define MAX_RX_URBS			20
39 #define MAX_TX_URBS			20
40 #define RX_BUFFER_SIZE			64
41 
42 /* vendor and product id */
43 #define USB_8DEV_VENDOR_ID		0x0483
44 #define USB_8DEV_PRODUCT_ID		0x1234
45 
46 /* endpoints */
47 enum usb_8dev_endpoint {
48 	USB_8DEV_ENDP_DATA_RX = 1,
49 	USB_8DEV_ENDP_DATA_TX,
50 	USB_8DEV_ENDP_CMD_RX,
51 	USB_8DEV_ENDP_CMD_TX
52 };
53 
54 /* device CAN clock */
55 #define USB_8DEV_ABP_CLOCK		32000000
56 
57 /* setup flags */
58 #define USB_8DEV_SILENT			0x01
59 #define USB_8DEV_LOOPBACK		0x02
60 #define USB_8DEV_DISABLE_AUTO_RESTRANS	0x04
61 #define USB_8DEV_STATUS_FRAME		0x08
62 
63 /* commands */
64 enum usb_8dev_cmd {
65 	USB_8DEV_RESET = 1,
66 	USB_8DEV_OPEN,
67 	USB_8DEV_CLOSE,
68 	USB_8DEV_SET_SPEED,
69 	USB_8DEV_SET_MASK_FILTER,
70 	USB_8DEV_GET_STATUS,
71 	USB_8DEV_GET_STATISTICS,
72 	USB_8DEV_GET_SERIAL,
73 	USB_8DEV_GET_SOFTW_VER,
74 	USB_8DEV_GET_HARDW_VER,
75 	USB_8DEV_RESET_TIMESTAMP,
76 	USB_8DEV_GET_SOFTW_HARDW_VER
77 };
78 
79 /* command options */
80 #define USB_8DEV_BAUD_MANUAL		0x09
81 #define USB_8DEV_CMD_START		0x11
82 #define USB_8DEV_CMD_END		0x22
83 
84 #define USB_8DEV_CMD_SUCCESS		0
85 #define USB_8DEV_CMD_ERROR		255
86 
87 #define USB_8DEV_CMD_TIMEOUT		1000
88 
89 /* frames */
90 #define USB_8DEV_DATA_START		0x55
91 #define USB_8DEV_DATA_END		0xAA
92 
93 #define USB_8DEV_TYPE_CAN_FRAME		0
94 #define USB_8DEV_TYPE_ERROR_FRAME	3
95 
96 #define USB_8DEV_EXTID			0x01
97 #define USB_8DEV_RTR			0x02
98 #define USB_8DEV_ERR_FLAG		0x04
99 
100 /* status */
101 #define USB_8DEV_STATUSMSG_OK		0x00  /* Normal condition. */
102 #define USB_8DEV_STATUSMSG_OVERRUN	0x01  /* Overrun occured when sending */
103 #define USB_8DEV_STATUSMSG_BUSLIGHT	0x02  /* Error counter has reached 96 */
104 #define USB_8DEV_STATUSMSG_BUSHEAVY	0x03  /* Error count. has reached 128 */
105 #define USB_8DEV_STATUSMSG_BUSOFF	0x04  /* Device is in BUSOFF */
106 #define USB_8DEV_STATUSMSG_STUFF	0x20  /* Stuff Error */
107 #define USB_8DEV_STATUSMSG_FORM		0x21  /* Form Error */
108 #define USB_8DEV_STATUSMSG_ACK		0x23  /* Ack Error */
109 #define USB_8DEV_STATUSMSG_BIT0		0x24  /* Bit1 Error */
110 #define USB_8DEV_STATUSMSG_BIT1		0x25  /* Bit0 Error */
111 #define USB_8DEV_STATUSMSG_CRC		0x27  /* CRC Error */
112 
113 #define USB_8DEV_RP_MASK		0x7F  /* Mask for Receive Error Bit */
114 
115 
116 /* table of devices that work with this driver */
117 static const struct usb_device_id usb_8dev_table[] = {
118 	{ USB_DEVICE(USB_8DEV_VENDOR_ID, USB_8DEV_PRODUCT_ID) },
119 	{ }					/* Terminating entry */
120 };
121 
122 MODULE_DEVICE_TABLE(usb, usb_8dev_table);
123 
124 struct usb_8dev_tx_urb_context {
125 	struct usb_8dev_priv *priv;
126 
127 	u32 echo_index;
128 	u8 dlc;
129 };
130 
131 /* Structure to hold all of our device specific stuff */
132 struct usb_8dev_priv {
133 	struct can_priv can; /* must be the first member */
134 
135 	struct sk_buff *echo_skb[MAX_TX_URBS];
136 
137 	struct usb_device *udev;
138 	struct net_device *netdev;
139 
140 	atomic_t active_tx_urbs;
141 	struct usb_anchor tx_submitted;
142 	struct usb_8dev_tx_urb_context tx_contexts[MAX_TX_URBS];
143 
144 	struct usb_anchor rx_submitted;
145 
146 	struct can_berr_counter bec;
147 
148 	u8 *cmd_msg_buffer;
149 
150 	struct mutex usb_8dev_cmd_lock;
151 
152 };
153 
154 /* tx frame */
155 struct __packed usb_8dev_tx_msg {
156 	u8 begin;
157 	u8 flags;	/* RTR and EXT_ID flag */
158 	__be32 id;	/* upper 3 bits not used */
159 	u8 dlc;		/* data length code 0-8 bytes */
160 	u8 data[8];	/* 64-bit data */
161 	u8 end;
162 };
163 
164 /* rx frame */
165 struct __packed usb_8dev_rx_msg {
166 	u8 begin;
167 	u8 type;		/* frame type */
168 	u8 flags;		/* RTR and EXT_ID flag */
169 	__be32 id;		/* upper 3 bits not used */
170 	u8 dlc;			/* data length code 0-8 bytes */
171 	u8 data[8];		/* 64-bit data */
172 	__be32 timestamp;	/* 32-bit timestamp */
173 	u8 end;
174 };
175 
176 /* command frame */
177 struct __packed usb_8dev_cmd_msg {
178 	u8 begin;
179 	u8 channel;	/* unkown - always 0 */
180 	u8 command;	/* command to execute */
181 	u8 opt1;	/* optional parameter / return value */
182 	u8 opt2;	/* optional parameter 2 */
183 	u8 data[10];	/* optional parameter and data */
184 	u8 end;
185 };
186 
187 static int usb_8dev_send_cmd_msg(struct usb_8dev_priv *priv, u8 *msg, int size)
188 {
189 	int actual_length;
190 
191 	return usb_bulk_msg(priv->udev,
192 			    usb_sndbulkpipe(priv->udev, USB_8DEV_ENDP_CMD_TX),
193 			    msg, size, &actual_length, USB_8DEV_CMD_TIMEOUT);
194 }
195 
196 static int usb_8dev_wait_cmd_msg(struct usb_8dev_priv *priv, u8 *msg, int size,
197 				int *actual_length)
198 {
199 	return usb_bulk_msg(priv->udev,
200 			    usb_rcvbulkpipe(priv->udev, USB_8DEV_ENDP_CMD_RX),
201 			    msg, size, actual_length, USB_8DEV_CMD_TIMEOUT);
202 }
203 
204 /* Send command to device and receive result.
205  * Command was successful when opt1 = 0.
206  */
207 static int usb_8dev_send_cmd(struct usb_8dev_priv *priv,
208 			     struct usb_8dev_cmd_msg *out,
209 			     struct usb_8dev_cmd_msg *in)
210 {
211 	int err;
212 	int num_bytes_read;
213 	struct net_device *netdev;
214 
215 	netdev = priv->netdev;
216 
217 	out->begin = USB_8DEV_CMD_START;
218 	out->end = USB_8DEV_CMD_END;
219 
220 	mutex_lock(&priv->usb_8dev_cmd_lock);
221 
222 	memcpy(priv->cmd_msg_buffer, out,
223 		sizeof(struct usb_8dev_cmd_msg));
224 
225 	err = usb_8dev_send_cmd_msg(priv, priv->cmd_msg_buffer,
226 				    sizeof(struct usb_8dev_cmd_msg));
227 	if (err < 0) {
228 		netdev_err(netdev, "sending command message failed\n");
229 		goto failed;
230 	}
231 
232 	err = usb_8dev_wait_cmd_msg(priv, priv->cmd_msg_buffer,
233 				    sizeof(struct usb_8dev_cmd_msg),
234 				    &num_bytes_read);
235 	if (err < 0) {
236 		netdev_err(netdev, "no command message answer\n");
237 		goto failed;
238 	}
239 
240 	memcpy(in, priv->cmd_msg_buffer, sizeof(struct usb_8dev_cmd_msg));
241 
242 	if (in->begin != USB_8DEV_CMD_START || in->end != USB_8DEV_CMD_END ||
243 			num_bytes_read != 16 || in->opt1 != 0)
244 		err = -EPROTO;
245 
246 failed:
247 	mutex_unlock(&priv->usb_8dev_cmd_lock);
248 	return err;
249 }
250 
251 /* Send open command to device */
252 static int usb_8dev_cmd_open(struct usb_8dev_priv *priv)
253 {
254 	struct can_bittiming *bt = &priv->can.bittiming;
255 	struct usb_8dev_cmd_msg outmsg;
256 	struct usb_8dev_cmd_msg inmsg;
257 	u32 ctrlmode = priv->can.ctrlmode;
258 	u32 flags = USB_8DEV_STATUS_FRAME;
259 	__be32 beflags;
260 	__be16 bebrp;
261 
262 	memset(&outmsg, 0, sizeof(outmsg));
263 	outmsg.command = USB_8DEV_OPEN;
264 	outmsg.opt1 = USB_8DEV_BAUD_MANUAL;
265 	outmsg.data[0] = bt->prop_seg + bt->phase_seg1;
266 	outmsg.data[1] = bt->phase_seg2;
267 	outmsg.data[2] = bt->sjw;
268 
269 	/* BRP */
270 	bebrp = cpu_to_be16((u16)bt->brp);
271 	memcpy(&outmsg.data[3], &bebrp, sizeof(bebrp));
272 
273 	/* flags */
274 	if (ctrlmode & CAN_CTRLMODE_LOOPBACK)
275 		flags |= USB_8DEV_LOOPBACK;
276 	if (ctrlmode & CAN_CTRLMODE_LISTENONLY)
277 		flags |= USB_8DEV_SILENT;
278 	if (ctrlmode & CAN_CTRLMODE_ONE_SHOT)
279 		flags |= USB_8DEV_DISABLE_AUTO_RESTRANS;
280 
281 	beflags = cpu_to_be32(flags);
282 	memcpy(&outmsg.data[5], &beflags, sizeof(beflags));
283 
284 	return usb_8dev_send_cmd(priv, &outmsg, &inmsg);
285 }
286 
287 /* Send close command to device */
288 static int usb_8dev_cmd_close(struct usb_8dev_priv *priv)
289 {
290 	struct usb_8dev_cmd_msg inmsg;
291 	struct usb_8dev_cmd_msg outmsg = {
292 		.channel = 0,
293 		.command = USB_8DEV_CLOSE,
294 		.opt1 = 0,
295 		.opt2 = 0
296 	};
297 
298 	return usb_8dev_send_cmd(priv, &outmsg, &inmsg);
299 }
300 
301 /* Get firmware and hardware version */
302 static int usb_8dev_cmd_version(struct usb_8dev_priv *priv, u32 *res)
303 {
304 	struct usb_8dev_cmd_msg	inmsg;
305 	struct usb_8dev_cmd_msg	outmsg = {
306 		.channel = 0,
307 		.command = USB_8DEV_GET_SOFTW_HARDW_VER,
308 		.opt1 = 0,
309 		.opt2 = 0
310 	};
311 
312 	int err = usb_8dev_send_cmd(priv, &outmsg, &inmsg);
313 	if (err)
314 		return err;
315 
316 	*res = be32_to_cpup((__be32 *)inmsg.data);
317 
318 	return err;
319 }
320 
321 /* Set network device mode
322  *
323  * Maybe we should leave this function empty, because the device
324  * set mode variable with open command.
325  */
326 static int usb_8dev_set_mode(struct net_device *netdev, enum can_mode mode)
327 {
328 	struct usb_8dev_priv *priv = netdev_priv(netdev);
329 	int err = 0;
330 
331 	switch (mode) {
332 	case CAN_MODE_START:
333 		err = usb_8dev_cmd_open(priv);
334 		if (err)
335 			netdev_warn(netdev, "couldn't start device");
336 		break;
337 
338 	default:
339 		return -EOPNOTSUPP;
340 	}
341 
342 	return err;
343 }
344 
345 /* Read error/status frames */
346 static void usb_8dev_rx_err_msg(struct usb_8dev_priv *priv,
347 				struct usb_8dev_rx_msg *msg)
348 {
349 	struct can_frame *cf;
350 	struct sk_buff *skb;
351 	struct net_device_stats *stats = &priv->netdev->stats;
352 
353 	/* Error message:
354 	 * byte 0: Status
355 	 * byte 1: bit   7: Receive Passive
356 	 * byte 1: bit 0-6: Receive Error Counter
357 	 * byte 2: Transmit Error Counter
358 	 * byte 3: Always 0 (maybe reserved for future use)
359 	 */
360 
361 	u8 state = msg->data[0];
362 	u8 rxerr = msg->data[1] & USB_8DEV_RP_MASK;
363 	u8 txerr = msg->data[2];
364 	int rx_errors = 0;
365 	int tx_errors = 0;
366 
367 	skb = alloc_can_err_skb(priv->netdev, &cf);
368 	if (!skb)
369 		return;
370 
371 	switch (state) {
372 	case USB_8DEV_STATUSMSG_OK:
373 		priv->can.state = CAN_STATE_ERROR_ACTIVE;
374 		cf->can_id |= CAN_ERR_PROT;
375 		cf->data[2] = CAN_ERR_PROT_ACTIVE;
376 		break;
377 	case USB_8DEV_STATUSMSG_BUSOFF:
378 		priv->can.state = CAN_STATE_BUS_OFF;
379 		cf->can_id |= CAN_ERR_BUSOFF;
380 		can_bus_off(priv->netdev);
381 		break;
382 	case USB_8DEV_STATUSMSG_OVERRUN:
383 	case USB_8DEV_STATUSMSG_BUSLIGHT:
384 	case USB_8DEV_STATUSMSG_BUSHEAVY:
385 		cf->can_id |= CAN_ERR_CRTL;
386 		break;
387 	default:
388 		priv->can.state = CAN_STATE_ERROR_WARNING;
389 		cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
390 		priv->can.can_stats.bus_error++;
391 		break;
392 	}
393 
394 	switch (state) {
395 	case USB_8DEV_STATUSMSG_OK:
396 	case USB_8DEV_STATUSMSG_BUSOFF:
397 		break;
398 	case USB_8DEV_STATUSMSG_ACK:
399 		cf->can_id |= CAN_ERR_ACK;
400 		tx_errors = 1;
401 		break;
402 	case USB_8DEV_STATUSMSG_CRC:
403 		cf->data[2] |= CAN_ERR_PROT_UNSPEC;
404 		cf->data[3] |= CAN_ERR_PROT_LOC_CRC_SEQ |
405 			       CAN_ERR_PROT_LOC_CRC_DEL;
406 		rx_errors = 1;
407 		break;
408 	case USB_8DEV_STATUSMSG_BIT0:
409 		cf->data[2] |= CAN_ERR_PROT_BIT0;
410 		tx_errors = 1;
411 		break;
412 	case USB_8DEV_STATUSMSG_BIT1:
413 		cf->data[2] |= CAN_ERR_PROT_BIT1;
414 		tx_errors = 1;
415 		break;
416 	case USB_8DEV_STATUSMSG_FORM:
417 		cf->data[2] |= CAN_ERR_PROT_FORM;
418 		rx_errors = 1;
419 		break;
420 	case USB_8DEV_STATUSMSG_STUFF:
421 		cf->data[2] |= CAN_ERR_PROT_STUFF;
422 		rx_errors = 1;
423 		break;
424 	case USB_8DEV_STATUSMSG_OVERRUN:
425 		cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
426 		stats->rx_over_errors++;
427 		rx_errors = 1;
428 		break;
429 	case USB_8DEV_STATUSMSG_BUSLIGHT:
430 		priv->can.state = CAN_STATE_ERROR_WARNING;
431 		cf->data[1] = (txerr > rxerr) ?
432 			CAN_ERR_CRTL_TX_WARNING :
433 			CAN_ERR_CRTL_RX_WARNING;
434 		priv->can.can_stats.error_warning++;
435 		break;
436 	case USB_8DEV_STATUSMSG_BUSHEAVY:
437 		priv->can.state = CAN_STATE_ERROR_PASSIVE;
438 		cf->data[1] = (txerr > rxerr) ?
439 			CAN_ERR_CRTL_TX_PASSIVE :
440 			CAN_ERR_CRTL_RX_PASSIVE;
441 		priv->can.can_stats.error_passive++;
442 		break;
443 	default:
444 		netdev_warn(priv->netdev,
445 			    "Unknown status/error message (%d)\n", state);
446 		break;
447 	}
448 
449 	if (tx_errors) {
450 		cf->data[2] |= CAN_ERR_PROT_TX;
451 		stats->tx_errors++;
452 	}
453 
454 	if (rx_errors)
455 		stats->rx_errors++;
456 
457 	cf->data[6] = txerr;
458 	cf->data[7] = rxerr;
459 
460 	priv->bec.txerr = txerr;
461 	priv->bec.rxerr = rxerr;
462 
463 	netif_rx(skb);
464 
465 	stats->rx_packets++;
466 	stats->rx_bytes += cf->can_dlc;
467 }
468 
469 /* Read data and status frames */
470 static void usb_8dev_rx_can_msg(struct usb_8dev_priv *priv,
471 				struct usb_8dev_rx_msg *msg)
472 {
473 	struct can_frame *cf;
474 	struct sk_buff *skb;
475 	struct net_device_stats *stats = &priv->netdev->stats;
476 
477 	if (msg->type == USB_8DEV_TYPE_ERROR_FRAME &&
478 		   msg->flags == USB_8DEV_ERR_FLAG) {
479 		usb_8dev_rx_err_msg(priv, msg);
480 	} else if (msg->type == USB_8DEV_TYPE_CAN_FRAME) {
481 		skb = alloc_can_skb(priv->netdev, &cf);
482 		if (!skb)
483 			return;
484 
485 		cf->can_id = be32_to_cpu(msg->id);
486 		cf->can_dlc = get_can_dlc(msg->dlc & 0xF);
487 
488 		if (msg->flags & USB_8DEV_EXTID)
489 			cf->can_id |= CAN_EFF_FLAG;
490 
491 		if (msg->flags & USB_8DEV_RTR)
492 			cf->can_id |= CAN_RTR_FLAG;
493 		else
494 			memcpy(cf->data, msg->data, cf->can_dlc);
495 
496 		netif_rx(skb);
497 
498 		stats->rx_packets++;
499 		stats->rx_bytes += cf->can_dlc;
500 
501 		can_led_event(priv->netdev, CAN_LED_EVENT_RX);
502 	} else {
503 		netdev_warn(priv->netdev, "frame type %d unknown",
504 			 msg->type);
505 	}
506 
507 }
508 
509 /* Callback for reading data from device
510  *
511  * Check urb status, call read function and resubmit urb read operation.
512  */
513 static void usb_8dev_read_bulk_callback(struct urb *urb)
514 {
515 	struct usb_8dev_priv *priv = urb->context;
516 	struct net_device *netdev;
517 	int retval;
518 	int pos = 0;
519 
520 	netdev = priv->netdev;
521 
522 	if (!netif_device_present(netdev))
523 		return;
524 
525 	switch (urb->status) {
526 	case 0: /* success */
527 		break;
528 
529 	case -ENOENT:
530 	case -ESHUTDOWN:
531 		return;
532 
533 	default:
534 		netdev_info(netdev, "Rx URB aborted (%d)\n",
535 			 urb->status);
536 		goto resubmit_urb;
537 	}
538 
539 	while (pos < urb->actual_length) {
540 		struct usb_8dev_rx_msg *msg;
541 
542 		if (pos + sizeof(struct usb_8dev_rx_msg) > urb->actual_length) {
543 			netdev_err(priv->netdev, "format error\n");
544 			break;
545 		}
546 
547 		msg = (struct usb_8dev_rx_msg *)(urb->transfer_buffer + pos);
548 		usb_8dev_rx_can_msg(priv, msg);
549 
550 		pos += sizeof(struct usb_8dev_rx_msg);
551 	}
552 
553 resubmit_urb:
554 	usb_fill_bulk_urb(urb, priv->udev,
555 			  usb_rcvbulkpipe(priv->udev, USB_8DEV_ENDP_DATA_RX),
556 			  urb->transfer_buffer, RX_BUFFER_SIZE,
557 			  usb_8dev_read_bulk_callback, priv);
558 
559 	retval = usb_submit_urb(urb, GFP_ATOMIC);
560 
561 	if (retval == -ENODEV)
562 		netif_device_detach(netdev);
563 	else if (retval)
564 		netdev_err(netdev,
565 			"failed resubmitting read bulk urb: %d\n", retval);
566 }
567 
568 /* Callback handler for write operations
569  *
570  * Free allocated buffers, check transmit status and
571  * calculate statistic.
572  */
573 static void usb_8dev_write_bulk_callback(struct urb *urb)
574 {
575 	struct usb_8dev_tx_urb_context *context = urb->context;
576 	struct usb_8dev_priv *priv;
577 	struct net_device *netdev;
578 
579 	BUG_ON(!context);
580 
581 	priv = context->priv;
582 	netdev = priv->netdev;
583 
584 	/* free up our allocated buffer */
585 	usb_free_coherent(urb->dev, urb->transfer_buffer_length,
586 			  urb->transfer_buffer, urb->transfer_dma);
587 
588 	atomic_dec(&priv->active_tx_urbs);
589 
590 	if (!netif_device_present(netdev))
591 		return;
592 
593 	if (urb->status)
594 		netdev_info(netdev, "Tx URB aborted (%d)\n",
595 			 urb->status);
596 
597 	netdev->stats.tx_packets++;
598 	netdev->stats.tx_bytes += context->dlc;
599 
600 	can_get_echo_skb(netdev, context->echo_index);
601 
602 	can_led_event(netdev, CAN_LED_EVENT_TX);
603 
604 	/* Release context */
605 	context->echo_index = MAX_TX_URBS;
606 
607 	netif_wake_queue(netdev);
608 }
609 
610 /* Send data to device */
611 static netdev_tx_t usb_8dev_start_xmit(struct sk_buff *skb,
612 				      struct net_device *netdev)
613 {
614 	struct usb_8dev_priv *priv = netdev_priv(netdev);
615 	struct net_device_stats *stats = &netdev->stats;
616 	struct can_frame *cf = (struct can_frame *) skb->data;
617 	struct usb_8dev_tx_msg *msg;
618 	struct urb *urb;
619 	struct usb_8dev_tx_urb_context *context = NULL;
620 	u8 *buf;
621 	int i, err;
622 	size_t size = sizeof(struct usb_8dev_tx_msg);
623 
624 	if (can_dropped_invalid_skb(netdev, skb))
625 		return NETDEV_TX_OK;
626 
627 	/* create a URB, and a buffer for it, and copy the data to the URB */
628 	urb = usb_alloc_urb(0, GFP_ATOMIC);
629 	if (!urb) {
630 		netdev_err(netdev, "No memory left for URBs\n");
631 		goto nomem;
632 	}
633 
634 	buf = usb_alloc_coherent(priv->udev, size, GFP_ATOMIC,
635 				 &urb->transfer_dma);
636 	if (!buf) {
637 		netdev_err(netdev, "No memory left for USB buffer\n");
638 		goto nomembuf;
639 	}
640 
641 	memset(buf, 0, size);
642 
643 	msg = (struct usb_8dev_tx_msg *)buf;
644 	msg->begin = USB_8DEV_DATA_START;
645 	msg->flags = 0x00;
646 
647 	if (cf->can_id & CAN_RTR_FLAG)
648 		msg->flags |= USB_8DEV_RTR;
649 
650 	if (cf->can_id & CAN_EFF_FLAG)
651 		msg->flags |= USB_8DEV_EXTID;
652 
653 	msg->id = cpu_to_be32(cf->can_id & CAN_ERR_MASK);
654 	msg->dlc = cf->can_dlc;
655 	memcpy(msg->data, cf->data, cf->can_dlc);
656 	msg->end = USB_8DEV_DATA_END;
657 
658 	for (i = 0; i < MAX_TX_URBS; i++) {
659 		if (priv->tx_contexts[i].echo_index == MAX_TX_URBS) {
660 			context = &priv->tx_contexts[i];
661 			break;
662 		}
663 	}
664 
665 	/* May never happen! When this happens we'd more URBs in flight as
666 	 * allowed (MAX_TX_URBS).
667 	 */
668 	if (!context)
669 		goto nofreecontext;
670 
671 	context->priv = priv;
672 	context->echo_index = i;
673 	context->dlc = cf->can_dlc;
674 
675 	usb_fill_bulk_urb(urb, priv->udev,
676 			  usb_sndbulkpipe(priv->udev, USB_8DEV_ENDP_DATA_TX),
677 			  buf, size, usb_8dev_write_bulk_callback, context);
678 	urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
679 	usb_anchor_urb(urb, &priv->tx_submitted);
680 
681 	can_put_echo_skb(skb, netdev, context->echo_index);
682 
683 	atomic_inc(&priv->active_tx_urbs);
684 
685 	err = usb_submit_urb(urb, GFP_ATOMIC);
686 	if (unlikely(err))
687 		goto failed;
688 	else if (atomic_read(&priv->active_tx_urbs) >= MAX_TX_URBS)
689 		/* Slow down tx path */
690 		netif_stop_queue(netdev);
691 
692 	/* Release our reference to this URB, the USB core will eventually free
693 	 * it entirely.
694 	 */
695 	usb_free_urb(urb);
696 
697 	return NETDEV_TX_OK;
698 
699 nofreecontext:
700 	usb_unanchor_urb(urb);
701 	usb_free_coherent(priv->udev, size, buf, urb->transfer_dma);
702 
703 	netdev_warn(netdev, "couldn't find free context");
704 
705 	return NETDEV_TX_BUSY;
706 
707 failed:
708 	can_free_echo_skb(netdev, context->echo_index);
709 
710 	usb_unanchor_urb(urb);
711 	usb_free_coherent(priv->udev, size, buf, urb->transfer_dma);
712 
713 	atomic_dec(&priv->active_tx_urbs);
714 
715 	if (err == -ENODEV)
716 		netif_device_detach(netdev);
717 	else
718 		netdev_warn(netdev, "failed tx_urb %d\n", err);
719 
720 nomembuf:
721 	usb_free_urb(urb);
722 
723 nomem:
724 	dev_kfree_skb(skb);
725 	stats->tx_dropped++;
726 
727 	return NETDEV_TX_OK;
728 }
729 
730 static int usb_8dev_get_berr_counter(const struct net_device *netdev,
731 				     struct can_berr_counter *bec)
732 {
733 	struct usb_8dev_priv *priv = netdev_priv(netdev);
734 
735 	bec->txerr = priv->bec.txerr;
736 	bec->rxerr = priv->bec.rxerr;
737 
738 	return 0;
739 }
740 
741 /* Start USB device */
742 static int usb_8dev_start(struct usb_8dev_priv *priv)
743 {
744 	struct net_device *netdev = priv->netdev;
745 	int err, i;
746 
747 	for (i = 0; i < MAX_RX_URBS; i++) {
748 		struct urb *urb = NULL;
749 		u8 *buf;
750 
751 		/* create a URB, and a buffer for it */
752 		urb = usb_alloc_urb(0, GFP_KERNEL);
753 		if (!urb) {
754 			netdev_err(netdev, "No memory left for URBs\n");
755 			err = -ENOMEM;
756 			break;
757 		}
758 
759 		buf = usb_alloc_coherent(priv->udev, RX_BUFFER_SIZE, GFP_KERNEL,
760 					 &urb->transfer_dma);
761 		if (!buf) {
762 			netdev_err(netdev, "No memory left for USB buffer\n");
763 			usb_free_urb(urb);
764 			err = -ENOMEM;
765 			break;
766 		}
767 
768 		usb_fill_bulk_urb(urb, priv->udev,
769 				  usb_rcvbulkpipe(priv->udev,
770 						  USB_8DEV_ENDP_DATA_RX),
771 				  buf, RX_BUFFER_SIZE,
772 				  usb_8dev_read_bulk_callback, priv);
773 		urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
774 		usb_anchor_urb(urb, &priv->rx_submitted);
775 
776 		err = usb_submit_urb(urb, GFP_KERNEL);
777 		if (err) {
778 			usb_unanchor_urb(urb);
779 			usb_free_coherent(priv->udev, RX_BUFFER_SIZE, buf,
780 					  urb->transfer_dma);
781 			usb_free_urb(urb);
782 			break;
783 		}
784 
785 		/* Drop reference, USB core will take care of freeing it */
786 		usb_free_urb(urb);
787 	}
788 
789 	/* Did we submit any URBs */
790 	if (i == 0) {
791 		netdev_warn(netdev, "couldn't setup read URBs\n");
792 		return err;
793 	}
794 
795 	/* Warn if we've couldn't transmit all the URBs */
796 	if (i < MAX_RX_URBS)
797 		netdev_warn(netdev, "rx performance may be slow\n");
798 
799 	err = usb_8dev_cmd_open(priv);
800 	if (err)
801 		goto failed;
802 
803 	priv->can.state = CAN_STATE_ERROR_ACTIVE;
804 
805 	return 0;
806 
807 failed:
808 	if (err == -ENODEV)
809 		netif_device_detach(priv->netdev);
810 
811 	netdev_warn(netdev, "couldn't submit control: %d\n", err);
812 
813 	return err;
814 }
815 
816 /* Open USB device */
817 static int usb_8dev_open(struct net_device *netdev)
818 {
819 	struct usb_8dev_priv *priv = netdev_priv(netdev);
820 	int err;
821 
822 	/* common open */
823 	err = open_candev(netdev);
824 	if (err)
825 		return err;
826 
827 	can_led_event(netdev, CAN_LED_EVENT_OPEN);
828 
829 	/* finally start device */
830 	err = usb_8dev_start(priv);
831 	if (err) {
832 		if (err == -ENODEV)
833 			netif_device_detach(priv->netdev);
834 
835 		netdev_warn(netdev, "couldn't start device: %d\n",
836 			 err);
837 
838 		close_candev(netdev);
839 
840 		return err;
841 	}
842 
843 	netif_start_queue(netdev);
844 
845 	return 0;
846 }
847 
848 static void unlink_all_urbs(struct usb_8dev_priv *priv)
849 {
850 	int i;
851 
852 	usb_kill_anchored_urbs(&priv->rx_submitted);
853 
854 	usb_kill_anchored_urbs(&priv->tx_submitted);
855 	atomic_set(&priv->active_tx_urbs, 0);
856 
857 	for (i = 0; i < MAX_TX_URBS; i++)
858 		priv->tx_contexts[i].echo_index = MAX_TX_URBS;
859 }
860 
861 /* Close USB device */
862 static int usb_8dev_close(struct net_device *netdev)
863 {
864 	struct usb_8dev_priv *priv = netdev_priv(netdev);
865 	int err = 0;
866 
867 	/* Send CLOSE command to CAN controller */
868 	err = usb_8dev_cmd_close(priv);
869 	if (err)
870 		netdev_warn(netdev, "couldn't stop device");
871 
872 	priv->can.state = CAN_STATE_STOPPED;
873 
874 	netif_stop_queue(netdev);
875 
876 	/* Stop polling */
877 	unlink_all_urbs(priv);
878 
879 	close_candev(netdev);
880 
881 	can_led_event(netdev, CAN_LED_EVENT_STOP);
882 
883 	return err;
884 }
885 
886 static const struct net_device_ops usb_8dev_netdev_ops = {
887 	.ndo_open = usb_8dev_open,
888 	.ndo_stop = usb_8dev_close,
889 	.ndo_start_xmit = usb_8dev_start_xmit,
890 };
891 
892 static const struct can_bittiming_const usb_8dev_bittiming_const = {
893 	.name = "usb_8dev",
894 	.tseg1_min = 1,
895 	.tseg1_max = 16,
896 	.tseg2_min = 1,
897 	.tseg2_max = 8,
898 	.sjw_max = 4,
899 	.brp_min = 1,
900 	.brp_max = 1024,
901 	.brp_inc = 1,
902 };
903 
904 /* Probe USB device
905  *
906  * Check device and firmware.
907  * Set supported modes and bittiming constants.
908  * Allocate some memory.
909  */
910 static int usb_8dev_probe(struct usb_interface *intf,
911 			 const struct usb_device_id *id)
912 {
913 	struct net_device *netdev;
914 	struct usb_8dev_priv *priv;
915 	int i, err = -ENOMEM;
916 	u32 version;
917 	char buf[18];
918 	struct usb_device *usbdev = interface_to_usbdev(intf);
919 
920 	/* product id looks strange, better we also check iProduct string */
921 	if (usb_string(usbdev, usbdev->descriptor.iProduct, buf,
922 		       sizeof(buf)) > 0 && strcmp(buf, "USB2CAN converter")) {
923 		dev_info(&usbdev->dev, "ignoring: not an USB2CAN converter\n");
924 		return -ENODEV;
925 	}
926 
927 	netdev = alloc_candev(sizeof(struct usb_8dev_priv), MAX_TX_URBS);
928 	if (!netdev) {
929 		dev_err(&intf->dev, "Couldn't alloc candev\n");
930 		return -ENOMEM;
931 	}
932 
933 	priv = netdev_priv(netdev);
934 
935 	priv->udev = usbdev;
936 	priv->netdev = netdev;
937 
938 	priv->can.state = CAN_STATE_STOPPED;
939 	priv->can.clock.freq = USB_8DEV_ABP_CLOCK;
940 	priv->can.bittiming_const = &usb_8dev_bittiming_const;
941 	priv->can.do_set_mode = usb_8dev_set_mode;
942 	priv->can.do_get_berr_counter = usb_8dev_get_berr_counter;
943 	priv->can.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK |
944 				      CAN_CTRLMODE_LISTENONLY |
945 				      CAN_CTRLMODE_ONE_SHOT;
946 
947 	netdev->netdev_ops = &usb_8dev_netdev_ops;
948 
949 	netdev->flags |= IFF_ECHO; /* we support local echo */
950 
951 	init_usb_anchor(&priv->rx_submitted);
952 
953 	init_usb_anchor(&priv->tx_submitted);
954 	atomic_set(&priv->active_tx_urbs, 0);
955 
956 	for (i = 0; i < MAX_TX_URBS; i++)
957 		priv->tx_contexts[i].echo_index = MAX_TX_URBS;
958 
959 	priv->cmd_msg_buffer = kzalloc(sizeof(struct usb_8dev_cmd_msg),
960 				      GFP_KERNEL);
961 	if (!priv->cmd_msg_buffer)
962 		goto cleanup_candev;
963 
964 	usb_set_intfdata(intf, priv);
965 
966 	SET_NETDEV_DEV(netdev, &intf->dev);
967 
968 	mutex_init(&priv->usb_8dev_cmd_lock);
969 
970 	err = register_candev(netdev);
971 	if (err) {
972 		netdev_err(netdev,
973 			"couldn't register CAN device: %d\n", err);
974 		goto cleanup_cmd_msg_buffer;
975 	}
976 
977 	err = usb_8dev_cmd_version(priv, &version);
978 	if (err) {
979 		netdev_err(netdev, "can't get firmware version\n");
980 		goto cleanup_unregister_candev;
981 	} else {
982 		netdev_info(netdev,
983 			 "firmware: %d.%d, hardware: %d.%d\n",
984 			 (version>>24) & 0xff, (version>>16) & 0xff,
985 			 (version>>8) & 0xff, version & 0xff);
986 	}
987 
988 	devm_can_led_init(netdev);
989 
990 	return 0;
991 
992 cleanup_unregister_candev:
993 	unregister_netdev(priv->netdev);
994 
995 cleanup_cmd_msg_buffer:
996 	kfree(priv->cmd_msg_buffer);
997 
998 cleanup_candev:
999 	free_candev(netdev);
1000 
1001 	return err;
1002 
1003 }
1004 
1005 /* Called by the usb core when driver is unloaded or device is removed */
1006 static void usb_8dev_disconnect(struct usb_interface *intf)
1007 {
1008 	struct usb_8dev_priv *priv = usb_get_intfdata(intf);
1009 
1010 	usb_set_intfdata(intf, NULL);
1011 
1012 	if (priv) {
1013 		netdev_info(priv->netdev, "device disconnected\n");
1014 
1015 		unregister_netdev(priv->netdev);
1016 		free_candev(priv->netdev);
1017 
1018 		unlink_all_urbs(priv);
1019 	}
1020 
1021 }
1022 
1023 static struct usb_driver usb_8dev_driver = {
1024 	.name =		"usb_8dev",
1025 	.probe =	usb_8dev_probe,
1026 	.disconnect =	usb_8dev_disconnect,
1027 	.id_table =	usb_8dev_table,
1028 };
1029 
1030 module_usb_driver(usb_8dev_driver);
1031 
1032 MODULE_AUTHOR("Bernd Krumboeck <krumboeck@universalnet.at>");
1033 MODULE_DESCRIPTION("CAN driver for 8 devices USB2CAN interfaces");
1034 MODULE_LICENSE("GPL v2");
1035