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