Merge tag 'v3.10.107' into update
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / net / can / usb / usb_8dev.c
CommitLineData
0024d8ad
BK
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/init.h>
27#include <linux/signal.h>
28#include <linux/slab.h>
29#include <linux/module.h>
30#include <linux/netdevice.h>
31#include <linux/usb.h>
32
33#include <linux/can.h>
34#include <linux/can/dev.h>
35#include <linux/can/error.h>
e2d5f2c7 36#include <linux/can/led.h>
0024d8ad
BK
37
38/* driver constants */
39#define MAX_RX_URBS 20
40#define MAX_TX_URBS 20
41#define RX_BUFFER_SIZE 64
42
43/* vendor and product id */
44#define USB_8DEV_VENDOR_ID 0x0483
45#define USB_8DEV_PRODUCT_ID 0x1234
46
47/* endpoints */
48enum usb_8dev_endpoint {
49 USB_8DEV_ENDP_DATA_RX = 1,
50 USB_8DEV_ENDP_DATA_TX,
51 USB_8DEV_ENDP_CMD_RX,
52 USB_8DEV_ENDP_CMD_TX
53};
54
55/* device CAN clock */
56#define USB_8DEV_ABP_CLOCK 32000000
57
58/* setup flags */
59#define USB_8DEV_SILENT 0x01
60#define USB_8DEV_LOOPBACK 0x02
61#define USB_8DEV_DISABLE_AUTO_RESTRANS 0x04
62#define USB_8DEV_STATUS_FRAME 0x08
63
64/* commands */
65enum usb_8dev_cmd {
66 USB_8DEV_RESET = 1,
67 USB_8DEV_OPEN,
68 USB_8DEV_CLOSE,
69 USB_8DEV_SET_SPEED,
70 USB_8DEV_SET_MASK_FILTER,
71 USB_8DEV_GET_STATUS,
72 USB_8DEV_GET_STATISTICS,
73 USB_8DEV_GET_SERIAL,
74 USB_8DEV_GET_SOFTW_VER,
75 USB_8DEV_GET_HARDW_VER,
76 USB_8DEV_RESET_TIMESTAMP,
77 USB_8DEV_GET_SOFTW_HARDW_VER
78};
79
80/* command options */
81#define USB_8DEV_BAUD_MANUAL 0x09
82#define USB_8DEV_CMD_START 0x11
83#define USB_8DEV_CMD_END 0x22
84
85#define USB_8DEV_CMD_SUCCESS 0
86#define USB_8DEV_CMD_ERROR 255
87
88#define USB_8DEV_CMD_TIMEOUT 1000
89
90/* frames */
91#define USB_8DEV_DATA_START 0x55
92#define USB_8DEV_DATA_END 0xAA
93
94#define USB_8DEV_TYPE_CAN_FRAME 0
95#define USB_8DEV_TYPE_ERROR_FRAME 3
96
97#define USB_8DEV_EXTID 0x01
98#define USB_8DEV_RTR 0x02
99#define USB_8DEV_ERR_FLAG 0x04
100
101/* status */
102#define USB_8DEV_STATUSMSG_OK 0x00 /* Normal condition. */
103#define USB_8DEV_STATUSMSG_OVERRUN 0x01 /* Overrun occured when sending */
104#define USB_8DEV_STATUSMSG_BUSLIGHT 0x02 /* Error counter has reached 96 */
105#define USB_8DEV_STATUSMSG_BUSHEAVY 0x03 /* Error count. has reached 128 */
106#define USB_8DEV_STATUSMSG_BUSOFF 0x04 /* Device is in BUSOFF */
107#define USB_8DEV_STATUSMSG_STUFF 0x20 /* Stuff Error */
108#define USB_8DEV_STATUSMSG_FORM 0x21 /* Form Error */
109#define USB_8DEV_STATUSMSG_ACK 0x23 /* Ack Error */
110#define USB_8DEV_STATUSMSG_BIT0 0x24 /* Bit1 Error */
111#define USB_8DEV_STATUSMSG_BIT1 0x25 /* Bit0 Error */
112#define USB_8DEV_STATUSMSG_CRC 0x27 /* CRC Error */
113
114#define USB_8DEV_RP_MASK 0x7F /* Mask for Receive Error Bit */
115
116
117/* table of devices that work with this driver */
118static const struct usb_device_id usb_8dev_table[] = {
119 { USB_DEVICE(USB_8DEV_VENDOR_ID, USB_8DEV_PRODUCT_ID) },
120 { } /* Terminating entry */
121};
122
123MODULE_DEVICE_TABLE(usb, usb_8dev_table);
124
125struct usb_8dev_tx_urb_context {
126 struct usb_8dev_priv *priv;
127
128 u32 echo_index;
129 u8 dlc;
130};
131
132/* Structure to hold all of our device specific stuff */
133struct usb_8dev_priv {
134 struct can_priv can; /* must be the first member */
135
136 struct sk_buff *echo_skb[MAX_TX_URBS];
137
138 struct usb_device *udev;
139 struct net_device *netdev;
140
141 atomic_t active_tx_urbs;
142 struct usb_anchor tx_submitted;
143 struct usb_8dev_tx_urb_context tx_contexts[MAX_TX_URBS];
144
145 struct usb_anchor rx_submitted;
146
147 struct can_berr_counter bec;
148
149 u8 *cmd_msg_buffer;
150
151 struct mutex usb_8dev_cmd_lock;
152
153};
154
155/* tx frame */
156struct __packed usb_8dev_tx_msg {
157 u8 begin;
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 u8 end;
163};
164
165/* rx frame */
166struct __packed usb_8dev_rx_msg {
167 u8 begin;
168 u8 type; /* frame type */
169 u8 flags; /* RTR and EXT_ID flag */
170 __be32 id; /* upper 3 bits not used */
171 u8 dlc; /* data length code 0-8 bytes */
172 u8 data[8]; /* 64-bit data */
173 __be32 timestamp; /* 32-bit timestamp */
174 u8 end;
175};
176
177/* command frame */
178struct __packed usb_8dev_cmd_msg {
179 u8 begin;
180 u8 channel; /* unkown - always 0 */
181 u8 command; /* command to execute */
182 u8 opt1; /* optional parameter / return value */
183 u8 opt2; /* optional parameter 2 */
184 u8 data[10]; /* optional parameter and data */
185 u8 end;
186};
187
188static int usb_8dev_send_cmd_msg(struct usb_8dev_priv *priv, u8 *msg, int size)
189{
190 int actual_length;
191
192 return usb_bulk_msg(priv->udev,
193 usb_sndbulkpipe(priv->udev, USB_8DEV_ENDP_CMD_TX),
194 msg, size, &actual_length, USB_8DEV_CMD_TIMEOUT);
195}
196
197static int usb_8dev_wait_cmd_msg(struct usb_8dev_priv *priv, u8 *msg, int size,
198 int *actual_length)
199{
200 return usb_bulk_msg(priv->udev,
201 usb_rcvbulkpipe(priv->udev, USB_8DEV_ENDP_CMD_RX),
202 msg, size, actual_length, USB_8DEV_CMD_TIMEOUT);
203}
204
205/* Send command to device and receive result.
206 * Command was successful when opt1 = 0.
207 */
208static int usb_8dev_send_cmd(struct usb_8dev_priv *priv,
209 struct usb_8dev_cmd_msg *out,
210 struct usb_8dev_cmd_msg *in)
211{
212 int err;
213 int num_bytes_read;
214 struct net_device *netdev;
215
216 netdev = priv->netdev;
217
218 out->begin = USB_8DEV_CMD_START;
219 out->end = USB_8DEV_CMD_END;
220
221 mutex_lock(&priv->usb_8dev_cmd_lock);
222
223 memcpy(priv->cmd_msg_buffer, out,
224 sizeof(struct usb_8dev_cmd_msg));
225
226 err = usb_8dev_send_cmd_msg(priv, priv->cmd_msg_buffer,
227 sizeof(struct usb_8dev_cmd_msg));
228 if (err < 0) {
229 netdev_err(netdev, "sending command message failed\n");
230 goto failed;
231 }
232
233 err = usb_8dev_wait_cmd_msg(priv, priv->cmd_msg_buffer,
234 sizeof(struct usb_8dev_cmd_msg),
235 &num_bytes_read);
236 if (err < 0) {
237 netdev_err(netdev, "no command message answer\n");
238 goto failed;
239 }
240
241 memcpy(in, priv->cmd_msg_buffer, sizeof(struct usb_8dev_cmd_msg));
242
243 if (in->begin != USB_8DEV_CMD_START || in->end != USB_8DEV_CMD_END ||
244 num_bytes_read != 16 || in->opt1 != 0)
245 err = -EPROTO;
246
247failed:
248 mutex_unlock(&priv->usb_8dev_cmd_lock);
249 return err;
250}
251
252/* Send open command to device */
253static int usb_8dev_cmd_open(struct usb_8dev_priv *priv)
254{
255 struct can_bittiming *bt = &priv->can.bittiming;
256 struct usb_8dev_cmd_msg outmsg;
257 struct usb_8dev_cmd_msg inmsg;
258 u32 ctrlmode = priv->can.ctrlmode;
259 u32 flags = USB_8DEV_STATUS_FRAME;
260 __be32 beflags;
261 __be16 bebrp;
262
263 memset(&outmsg, 0, sizeof(outmsg));
264 outmsg.command = USB_8DEV_OPEN;
265 outmsg.opt1 = USB_8DEV_BAUD_MANUAL;
266 outmsg.data[0] = bt->prop_seg + bt->phase_seg1;
267 outmsg.data[1] = bt->phase_seg2;
268 outmsg.data[2] = bt->sjw;
269
270 /* BRP */
271 bebrp = cpu_to_be16((u16)bt->brp);
272 memcpy(&outmsg.data[3], &bebrp, sizeof(bebrp));
273
274 /* flags */
275 if (ctrlmode & CAN_CTRLMODE_LOOPBACK)
276 flags |= USB_8DEV_LOOPBACK;
277 if (ctrlmode & CAN_CTRLMODE_LISTENONLY)
278 flags |= USB_8DEV_SILENT;
279 if (ctrlmode & CAN_CTRLMODE_ONE_SHOT)
280 flags |= USB_8DEV_DISABLE_AUTO_RESTRANS;
281
282 beflags = cpu_to_be32(flags);
283 memcpy(&outmsg.data[5], &beflags, sizeof(beflags));
284
285 return usb_8dev_send_cmd(priv, &outmsg, &inmsg);
286}
287
288/* Send close command to device */
289static int usb_8dev_cmd_close(struct usb_8dev_priv *priv)
290{
291 struct usb_8dev_cmd_msg inmsg;
292 struct usb_8dev_cmd_msg outmsg = {
293 .channel = 0,
294 .command = USB_8DEV_CLOSE,
295 .opt1 = 0,
296 .opt2 = 0
297 };
298
299 return usb_8dev_send_cmd(priv, &outmsg, &inmsg);
300}
301
302/* Get firmware and hardware version */
303static int usb_8dev_cmd_version(struct usb_8dev_priv *priv, u32 *res)
304{
305 struct usb_8dev_cmd_msg inmsg;
306 struct usb_8dev_cmd_msg outmsg = {
307 .channel = 0,
308 .command = USB_8DEV_GET_SOFTW_HARDW_VER,
309 .opt1 = 0,
310 .opt2 = 0
311 };
312
313 int err = usb_8dev_send_cmd(priv, &outmsg, &inmsg);
314 if (err)
315 return err;
316
317 *res = be32_to_cpup((__be32 *)inmsg.data);
318
319 return err;
320}
321
322/* Set network device mode
323 *
324 * Maybe we should leave this function empty, because the device
325 * set mode variable with open command.
326 */
327static int usb_8dev_set_mode(struct net_device *netdev, enum can_mode mode)
328{
329 struct usb_8dev_priv *priv = netdev_priv(netdev);
330 int err = 0;
331
332 switch (mode) {
333 case CAN_MODE_START:
334 err = usb_8dev_cmd_open(priv);
335 if (err)
336 netdev_warn(netdev, "couldn't start device");
337 break;
338
339 default:
340 return -EOPNOTSUPP;
341 }
342
343 return err;
344}
345
346/* Read error/status frames */
347static void usb_8dev_rx_err_msg(struct usb_8dev_priv *priv,
348 struct usb_8dev_rx_msg *msg)
349{
350 struct can_frame *cf;
351 struct sk_buff *skb;
352 struct net_device_stats *stats = &priv->netdev->stats;
353
354 /* Error message:
355 * byte 0: Status
356 * byte 1: bit 7: Receive Passive
357 * byte 1: bit 0-6: Receive Error Counter
358 * byte 2: Transmit Error Counter
359 * byte 3: Always 0 (maybe reserved for future use)
360 */
361
362 u8 state = msg->data[0];
363 u8 rxerr = msg->data[1] & USB_8DEV_RP_MASK;
364 u8 txerr = msg->data[2];
365 int rx_errors = 0;
366 int tx_errors = 0;
367
368 skb = alloc_can_err_skb(priv->netdev, &cf);
369 if (!skb)
370 return;
371
372 switch (state) {
373 case USB_8DEV_STATUSMSG_OK:
374 priv->can.state = CAN_STATE_ERROR_ACTIVE;
375 cf->can_id |= CAN_ERR_PROT;
376 cf->data[2] = CAN_ERR_PROT_ACTIVE;
377 break;
378 case USB_8DEV_STATUSMSG_BUSOFF:
379 priv->can.state = CAN_STATE_BUS_OFF;
380 cf->can_id |= CAN_ERR_BUSOFF;
381 can_bus_off(priv->netdev);
382 break;
383 case USB_8DEV_STATUSMSG_OVERRUN:
384 case USB_8DEV_STATUSMSG_BUSLIGHT:
385 case USB_8DEV_STATUSMSG_BUSHEAVY:
386 cf->can_id |= CAN_ERR_CRTL;
387 break;
388 default:
389 priv->can.state = CAN_STATE_ERROR_WARNING;
390 cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
391 priv->can.can_stats.bus_error++;
392 break;
393 }
394
395 switch (state) {
396 case USB_8DEV_STATUSMSG_OK:
397 case USB_8DEV_STATUSMSG_BUSOFF:
398 break;
399 case USB_8DEV_STATUSMSG_ACK:
400 cf->can_id |= CAN_ERR_ACK;
401 tx_errors = 1;
402 break;
403 case USB_8DEV_STATUSMSG_CRC:
404 cf->data[2] |= CAN_ERR_PROT_UNSPEC;
405 cf->data[3] |= CAN_ERR_PROT_LOC_CRC_SEQ |
406 CAN_ERR_PROT_LOC_CRC_DEL;
407 rx_errors = 1;
408 break;
409 case USB_8DEV_STATUSMSG_BIT0:
410 cf->data[2] |= CAN_ERR_PROT_BIT0;
411 tx_errors = 1;
412 break;
413 case USB_8DEV_STATUSMSG_BIT1:
414 cf->data[2] |= CAN_ERR_PROT_BIT1;
415 tx_errors = 1;
416 break;
417 case USB_8DEV_STATUSMSG_FORM:
418 cf->data[2] |= CAN_ERR_PROT_FORM;
419 rx_errors = 1;
420 break;
421 case USB_8DEV_STATUSMSG_STUFF:
422 cf->data[2] |= CAN_ERR_PROT_STUFF;
423 rx_errors = 1;
424 break;
425 case USB_8DEV_STATUSMSG_OVERRUN:
426 cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
427 stats->rx_over_errors++;
428 rx_errors = 1;
429 break;
430 case USB_8DEV_STATUSMSG_BUSLIGHT:
431 priv->can.state = CAN_STATE_ERROR_WARNING;
432 cf->data[1] = (txerr > rxerr) ?
433 CAN_ERR_CRTL_TX_WARNING :
434 CAN_ERR_CRTL_RX_WARNING;
435 priv->can.can_stats.error_warning++;
436 break;
437 case USB_8DEV_STATUSMSG_BUSHEAVY:
438 priv->can.state = CAN_STATE_ERROR_PASSIVE;
439 cf->data[1] = (txerr > rxerr) ?
440 CAN_ERR_CRTL_TX_PASSIVE :
441 CAN_ERR_CRTL_RX_PASSIVE;
442 priv->can.can_stats.error_passive++;
443 break;
444 default:
445 netdev_warn(priv->netdev,
446 "Unknown status/error message (%d)\n", state);
447 break;
448 }
449
450 if (tx_errors) {
451 cf->data[2] |= CAN_ERR_PROT_TX;
452 stats->tx_errors++;
453 }
454
455 if (rx_errors)
456 stats->rx_errors++;
457
458 cf->data[6] = txerr;
459 cf->data[7] = rxerr;
460
461 priv->bec.txerr = txerr;
462 priv->bec.rxerr = rxerr;
463
464 netif_rx(skb);
465
466 stats->rx_packets++;
467 stats->rx_bytes += cf->can_dlc;
468}
469
470/* Read data and status frames */
471static void usb_8dev_rx_can_msg(struct usb_8dev_priv *priv,
472 struct usb_8dev_rx_msg *msg)
473{
474 struct can_frame *cf;
475 struct sk_buff *skb;
476 struct net_device_stats *stats = &priv->netdev->stats;
477
478 if (msg->type == USB_8DEV_TYPE_ERROR_FRAME &&
479 msg->flags == USB_8DEV_ERR_FLAG) {
480 usb_8dev_rx_err_msg(priv, msg);
481 } else if (msg->type == USB_8DEV_TYPE_CAN_FRAME) {
482 skb = alloc_can_skb(priv->netdev, &cf);
483 if (!skb)
484 return;
485
486 cf->can_id = be32_to_cpu(msg->id);
487 cf->can_dlc = get_can_dlc(msg->dlc & 0xF);
488
489 if (msg->flags & USB_8DEV_EXTID)
490 cf->can_id |= CAN_EFF_FLAG;
491
492 if (msg->flags & USB_8DEV_RTR)
493 cf->can_id |= CAN_RTR_FLAG;
494 else
495 memcpy(cf->data, msg->data, cf->can_dlc);
496
497 netif_rx(skb);
498
499 stats->rx_packets++;
500 stats->rx_bytes += cf->can_dlc;
e2d5f2c7
BK
501
502 can_led_event(priv->netdev, CAN_LED_EVENT_RX);
0024d8ad
BK
503 } else {
504 netdev_warn(priv->netdev, "frame type %d unknown",
505 msg->type);
506 }
507
508}
509
510/* Callback for reading data from device
511 *
512 * Check urb status, call read function and resubmit urb read operation.
513 */
514static void usb_8dev_read_bulk_callback(struct urb *urb)
515{
516 struct usb_8dev_priv *priv = urb->context;
517 struct net_device *netdev;
518 int retval;
519 int pos = 0;
520
521 netdev = priv->netdev;
522
523 if (!netif_device_present(netdev))
524 return;
525
526 switch (urb->status) {
527 case 0: /* success */
528 break;
529
530 case -ENOENT:
531 case -ESHUTDOWN:
532 return;
533
534 default:
535 netdev_info(netdev, "Rx URB aborted (%d)\n",
536 urb->status);
537 goto resubmit_urb;
538 }
539
540 while (pos < urb->actual_length) {
541 struct usb_8dev_rx_msg *msg;
542
543 if (pos + sizeof(struct usb_8dev_rx_msg) > urb->actual_length) {
544 netdev_err(priv->netdev, "format error\n");
545 break;
546 }
547
548 msg = (struct usb_8dev_rx_msg *)(urb->transfer_buffer + pos);
549 usb_8dev_rx_can_msg(priv, msg);
550
551 pos += sizeof(struct usb_8dev_rx_msg);
552 }
553
554resubmit_urb:
555 usb_fill_bulk_urb(urb, priv->udev,
556 usb_rcvbulkpipe(priv->udev, USB_8DEV_ENDP_DATA_RX),
557 urb->transfer_buffer, RX_BUFFER_SIZE,
558 usb_8dev_read_bulk_callback, priv);
559
560 retval = usb_submit_urb(urb, GFP_ATOMIC);
561
562 if (retval == -ENODEV)
563 netif_device_detach(netdev);
564 else if (retval)
565 netdev_err(netdev,
566 "failed resubmitting read bulk urb: %d\n", retval);
567}
568
569/* Callback handler for write operations
570 *
571 * Free allocated buffers, check transmit status and
572 * calculate statistic.
573 */
574static void usb_8dev_write_bulk_callback(struct urb *urb)
575{
576 struct usb_8dev_tx_urb_context *context = urb->context;
577 struct usb_8dev_priv *priv;
578 struct net_device *netdev;
579
580 BUG_ON(!context);
581
582 priv = context->priv;
583 netdev = priv->netdev;
584
585 /* free up our allocated buffer */
586 usb_free_coherent(urb->dev, urb->transfer_buffer_length,
587 urb->transfer_buffer, urb->transfer_dma);
588
589 atomic_dec(&priv->active_tx_urbs);
590
591 if (!netif_device_present(netdev))
592 return;
593
594 if (urb->status)
595 netdev_info(netdev, "Tx URB aborted (%d)\n",
596 urb->status);
597
598 netdev->stats.tx_packets++;
599 netdev->stats.tx_bytes += context->dlc;
600
601 can_get_echo_skb(netdev, context->echo_index);
602
e2d5f2c7
BK
603 can_led_event(netdev, CAN_LED_EVENT_TX);
604
0024d8ad
BK
605 /* Release context */
606 context->echo_index = MAX_TX_URBS;
607
608 netif_wake_queue(netdev);
609}
610
611/* Send data to device */
612static netdev_tx_t usb_8dev_start_xmit(struct sk_buff *skb,
613 struct net_device *netdev)
614{
615 struct usb_8dev_priv *priv = netdev_priv(netdev);
616 struct net_device_stats *stats = &netdev->stats;
617 struct can_frame *cf = (struct can_frame *) skb->data;
618 struct usb_8dev_tx_msg *msg;
619 struct urb *urb;
620 struct usb_8dev_tx_urb_context *context = NULL;
621 u8 *buf;
622 int i, err;
623 size_t size = sizeof(struct usb_8dev_tx_msg);
624
625 if (can_dropped_invalid_skb(netdev, skb))
626 return NETDEV_TX_OK;
627
628 /* create a URB, and a buffer for it, and copy the data to the URB */
629 urb = usb_alloc_urb(0, GFP_ATOMIC);
630 if (!urb) {
631 netdev_err(netdev, "No memory left for URBs\n");
632 goto nomem;
633 }
634
635 buf = usb_alloc_coherent(priv->udev, size, GFP_ATOMIC,
636 &urb->transfer_dma);
637 if (!buf) {
638 netdev_err(netdev, "No memory left for USB buffer\n");
639 goto nomembuf;
640 }
641
642 memset(buf, 0, size);
643
644 msg = (struct usb_8dev_tx_msg *)buf;
645 msg->begin = USB_8DEV_DATA_START;
646 msg->flags = 0x00;
647
648 if (cf->can_id & CAN_RTR_FLAG)
649 msg->flags |= USB_8DEV_RTR;
650
651 if (cf->can_id & CAN_EFF_FLAG)
652 msg->flags |= USB_8DEV_EXTID;
653
654 msg->id = cpu_to_be32(cf->can_id & CAN_ERR_MASK);
655 msg->dlc = cf->can_dlc;
656 memcpy(msg->data, cf->data, cf->can_dlc);
657 msg->end = USB_8DEV_DATA_END;
658
659 for (i = 0; i < MAX_TX_URBS; i++) {
660 if (priv->tx_contexts[i].echo_index == MAX_TX_URBS) {
661 context = &priv->tx_contexts[i];
662 break;
663 }
664 }
665
666 /* May never happen! When this happens we'd more URBs in flight as
667 * allowed (MAX_TX_URBS).
668 */
669 if (!context)
670 goto nofreecontext;
671
672 context->priv = priv;
673 context->echo_index = i;
674 context->dlc = cf->can_dlc;
675
676 usb_fill_bulk_urb(urb, priv->udev,
677 usb_sndbulkpipe(priv->udev, USB_8DEV_ENDP_DATA_TX),
678 buf, size, usb_8dev_write_bulk_callback, context);
679 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
680 usb_anchor_urb(urb, &priv->tx_submitted);
681
682 can_put_echo_skb(skb, netdev, context->echo_index);
683
684 atomic_inc(&priv->active_tx_urbs);
685
686 err = usb_submit_urb(urb, GFP_ATOMIC);
687 if (unlikely(err))
688 goto failed;
689 else if (atomic_read(&priv->active_tx_urbs) >= MAX_TX_URBS)
690 /* Slow down tx path */
691 netif_stop_queue(netdev);
692
693 /* Release our reference to this URB, the USB core will eventually free
694 * it entirely.
695 */
696 usb_free_urb(urb);
697
698 return NETDEV_TX_OK;
699
700nofreecontext:
701 usb_unanchor_urb(urb);
702 usb_free_coherent(priv->udev, size, buf, urb->transfer_dma);
703
704 netdev_warn(netdev, "couldn't find free context");
705
706 return NETDEV_TX_BUSY;
707
708failed:
709 can_free_echo_skb(netdev, context->echo_index);
710
711 usb_unanchor_urb(urb);
712 usb_free_coherent(priv->udev, size, buf, urb->transfer_dma);
713
714 atomic_dec(&priv->active_tx_urbs);
715
716 if (err == -ENODEV)
717 netif_device_detach(netdev);
718 else
719 netdev_warn(netdev, "failed tx_urb %d\n", err);
720
721nomembuf:
722 usb_free_urb(urb);
723
724nomem:
725 dev_kfree_skb(skb);
726 stats->tx_dropped++;
727
728 return NETDEV_TX_OK;
729}
730
731static int usb_8dev_get_berr_counter(const struct net_device *netdev,
732 struct can_berr_counter *bec)
733{
734 struct usb_8dev_priv *priv = netdev_priv(netdev);
735
736 bec->txerr = priv->bec.txerr;
737 bec->rxerr = priv->bec.rxerr;
738
739 return 0;
740}
741
742/* Start USB device */
743static int usb_8dev_start(struct usb_8dev_priv *priv)
744{
745 struct net_device *netdev = priv->netdev;
746 int err, i;
747
748 for (i = 0; i < MAX_RX_URBS; i++) {
749 struct urb *urb = NULL;
750 u8 *buf;
751
752 /* create a URB, and a buffer for it */
753 urb = usb_alloc_urb(0, GFP_KERNEL);
754 if (!urb) {
755 netdev_err(netdev, "No memory left for URBs\n");
756 err = -ENOMEM;
757 break;
758 }
759
760 buf = usb_alloc_coherent(priv->udev, RX_BUFFER_SIZE, GFP_KERNEL,
761 &urb->transfer_dma);
762 if (!buf) {
763 netdev_err(netdev, "No memory left for USB buffer\n");
764 usb_free_urb(urb);
765 err = -ENOMEM;
766 break;
767 }
768
769 usb_fill_bulk_urb(urb, priv->udev,
770 usb_rcvbulkpipe(priv->udev,
771 USB_8DEV_ENDP_DATA_RX),
772 buf, RX_BUFFER_SIZE,
773 usb_8dev_read_bulk_callback, priv);
774 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
775 usb_anchor_urb(urb, &priv->rx_submitted);
776
777 err = usb_submit_urb(urb, GFP_KERNEL);
778 if (err) {
779 usb_unanchor_urb(urb);
780 usb_free_coherent(priv->udev, RX_BUFFER_SIZE, buf,
781 urb->transfer_dma);
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
807failed:
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 */
817static 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
e2d5f2c7
BK
827 can_led_event(netdev, CAN_LED_EVENT_OPEN);
828
0024d8ad
BK
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
848static 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 */
862static 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
e2d5f2c7
BK
881 can_led_event(netdev, CAN_LED_EVENT_STOP);
882
0024d8ad
BK
883 return err;
884}
885
886static 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
892static 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 */
910static 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
d2ab6e52
MKB
959 priv->cmd_msg_buffer = devm_kzalloc(&intf->dev, sizeof(struct usb_8dev_cmd_msg),
960 GFP_KERNEL);
09da6c5f 961 if (!priv->cmd_msg_buffer)
0024d8ad 962 goto cleanup_candev;
0024d8ad
BK
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);
d2ab6e52 974 goto cleanup_candev;
0024d8ad
BK
975 }
976
977 err = usb_8dev_cmd_version(priv, &version);
978 if (err) {
979 netdev_err(netdev, "can't get firmware version\n");
4afe2156 980 goto cleanup_unregister_candev;
0024d8ad
BK
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
e2d5f2c7
BK
988 devm_can_led_init(netdev);
989
0024d8ad
BK
990 return 0;
991
4afe2156
MKB
992cleanup_unregister_candev:
993 unregister_netdev(priv->netdev);
994
0024d8ad
BK
995cleanup_candev:
996 free_candev(netdev);
997
998 return err;
999
1000}
1001
1002/* Called by the usb core when driver is unloaded or device is removed */
1003static void usb_8dev_disconnect(struct usb_interface *intf)
1004{
1005 struct usb_8dev_priv *priv = usb_get_intfdata(intf);
1006
1007 usb_set_intfdata(intf, NULL);
1008
1009 if (priv) {
1010 netdev_info(priv->netdev, "device disconnected\n");
1011
1012 unregister_netdev(priv->netdev);
1013 free_candev(priv->netdev);
1014
1015 unlink_all_urbs(priv);
1016 }
1017
1018}
1019
1020static struct usb_driver usb_8dev_driver = {
1021 .name = "usb_8dev",
1022 .probe = usb_8dev_probe,
1023 .disconnect = usb_8dev_disconnect,
1024 .id_table = usb_8dev_table,
1025};
1026
1027module_usb_driver(usb_8dev_driver);
1028
1029MODULE_AUTHOR("Bernd Krumboeck <krumboeck@universalnet.at>");
1030MODULE_DESCRIPTION("CAN driver for 8 devices USB2CAN interfaces");
1031MODULE_LICENSE("GPL v2");