mfd: wm8350-i2c: Make sure the i2c regmap functions are compiled
[linux/fpc-iii.git] / drivers / net / can / usb / kvaser_usb.c
bloba3fb8b51038a17ae5d4ec3989c942afee4644ca9
1 /*
2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License as
4 * published by the Free Software Foundation version 2.
6 * Parts of this driver are based on the following:
7 * - Kvaser linux leaf driver (version 4.78)
8 * - CAN driver for esd CAN-USB/2
10 * Copyright (C) 2002-2006 KVASER AB, Sweden. All rights reserved.
11 * Copyright (C) 2010 Matthias Fuchs <matthias.fuchs@esd.eu>, esd gmbh
12 * Copyright (C) 2012 Olivier Sobrie <olivier@sobrie.be>
15 #include <linux/init.h>
16 #include <linux/completion.h>
17 #include <linux/module.h>
18 #include <linux/netdevice.h>
19 #include <linux/usb.h>
21 #include <linux/can.h>
22 #include <linux/can/dev.h>
23 #include <linux/can/error.h>
25 #define MAX_TX_URBS 16
26 #define MAX_RX_URBS 4
27 #define START_TIMEOUT 1000 /* msecs */
28 #define STOP_TIMEOUT 1000 /* msecs */
29 #define USB_SEND_TIMEOUT 1000 /* msecs */
30 #define USB_RECV_TIMEOUT 1000 /* msecs */
31 #define RX_BUFFER_SIZE 3072
32 #define CAN_USB_CLOCK 8000000
33 #define MAX_NET_DEVICES 3
35 /* Kvaser USB devices */
36 #define KVASER_VENDOR_ID 0x0bfd
37 #define USB_LEAF_DEVEL_PRODUCT_ID 10
38 #define USB_LEAF_LITE_PRODUCT_ID 11
39 #define USB_LEAF_PRO_PRODUCT_ID 12
40 #define USB_LEAF_SPRO_PRODUCT_ID 14
41 #define USB_LEAF_PRO_LS_PRODUCT_ID 15
42 #define USB_LEAF_PRO_SWC_PRODUCT_ID 16
43 #define USB_LEAF_PRO_LIN_PRODUCT_ID 17
44 #define USB_LEAF_SPRO_LS_PRODUCT_ID 18
45 #define USB_LEAF_SPRO_SWC_PRODUCT_ID 19
46 #define USB_MEMO2_DEVEL_PRODUCT_ID 22
47 #define USB_MEMO2_HSHS_PRODUCT_ID 23
48 #define USB_UPRO_HSHS_PRODUCT_ID 24
49 #define USB_LEAF_LITE_GI_PRODUCT_ID 25
50 #define USB_LEAF_PRO_OBDII_PRODUCT_ID 26
51 #define USB_MEMO2_HSLS_PRODUCT_ID 27
52 #define USB_LEAF_LITE_CH_PRODUCT_ID 28
53 #define USB_BLACKBIRD_SPRO_PRODUCT_ID 29
54 #define USB_OEM_MERCURY_PRODUCT_ID 34
55 #define USB_OEM_LEAF_PRODUCT_ID 35
56 #define USB_CAN_R_PRODUCT_ID 39
58 /* USB devices features */
59 #define KVASER_HAS_SILENT_MODE BIT(0)
60 #define KVASER_HAS_TXRX_ERRORS BIT(1)
62 /* Message header size */
63 #define MSG_HEADER_LEN 2
65 /* Can message flags */
66 #define MSG_FLAG_ERROR_FRAME BIT(0)
67 #define MSG_FLAG_OVERRUN BIT(1)
68 #define MSG_FLAG_NERR BIT(2)
69 #define MSG_FLAG_WAKEUP BIT(3)
70 #define MSG_FLAG_REMOTE_FRAME BIT(4)
71 #define MSG_FLAG_RESERVED BIT(5)
72 #define MSG_FLAG_TX_ACK BIT(6)
73 #define MSG_FLAG_TX_REQUEST BIT(7)
75 /* Can states */
76 #define M16C_STATE_BUS_RESET BIT(0)
77 #define M16C_STATE_BUS_ERROR BIT(4)
78 #define M16C_STATE_BUS_PASSIVE BIT(5)
79 #define M16C_STATE_BUS_OFF BIT(6)
81 /* Can msg ids */
82 #define CMD_RX_STD_MESSAGE 12
83 #define CMD_TX_STD_MESSAGE 13
84 #define CMD_RX_EXT_MESSAGE 14
85 #define CMD_TX_EXT_MESSAGE 15
86 #define CMD_SET_BUS_PARAMS 16
87 #define CMD_GET_BUS_PARAMS 17
88 #define CMD_GET_BUS_PARAMS_REPLY 18
89 #define CMD_GET_CHIP_STATE 19
90 #define CMD_CHIP_STATE_EVENT 20
91 #define CMD_SET_CTRL_MODE 21
92 #define CMD_GET_CTRL_MODE 22
93 #define CMD_GET_CTRL_MODE_REPLY 23
94 #define CMD_RESET_CHIP 24
95 #define CMD_RESET_CARD 25
96 #define CMD_START_CHIP 26
97 #define CMD_START_CHIP_REPLY 27
98 #define CMD_STOP_CHIP 28
99 #define CMD_STOP_CHIP_REPLY 29
100 #define CMD_GET_CARD_INFO2 32
101 #define CMD_GET_CARD_INFO 34
102 #define CMD_GET_CARD_INFO_REPLY 35
103 #define CMD_GET_SOFTWARE_INFO 38
104 #define CMD_GET_SOFTWARE_INFO_REPLY 39
105 #define CMD_ERROR_EVENT 45
106 #define CMD_FLUSH_QUEUE 48
107 #define CMD_RESET_ERROR_COUNTER 49
108 #define CMD_TX_ACKNOWLEDGE 50
109 #define CMD_CAN_ERROR_EVENT 51
110 #define CMD_USB_THROTTLE 77
111 #define CMD_LOG_MESSAGE 106
113 /* error factors */
114 #define M16C_EF_ACKE BIT(0)
115 #define M16C_EF_CRCE BIT(1)
116 #define M16C_EF_FORME BIT(2)
117 #define M16C_EF_STFE BIT(3)
118 #define M16C_EF_BITE0 BIT(4)
119 #define M16C_EF_BITE1 BIT(5)
120 #define M16C_EF_RCVE BIT(6)
121 #define M16C_EF_TRE BIT(7)
123 /* bittiming parameters */
124 #define KVASER_USB_TSEG1_MIN 1
125 #define KVASER_USB_TSEG1_MAX 16
126 #define KVASER_USB_TSEG2_MIN 1
127 #define KVASER_USB_TSEG2_MAX 8
128 #define KVASER_USB_SJW_MAX 4
129 #define KVASER_USB_BRP_MIN 1
130 #define KVASER_USB_BRP_MAX 64
131 #define KVASER_USB_BRP_INC 1
133 /* ctrl modes */
134 #define KVASER_CTRL_MODE_NORMAL 1
135 #define KVASER_CTRL_MODE_SILENT 2
136 #define KVASER_CTRL_MODE_SELFRECEPTION 3
137 #define KVASER_CTRL_MODE_OFF 4
139 /* log message */
140 #define KVASER_EXTENDED_FRAME BIT(31)
142 struct kvaser_msg_simple {
143 u8 tid;
144 u8 channel;
145 } __packed;
147 struct kvaser_msg_cardinfo {
148 u8 tid;
149 u8 nchannels;
150 __le32 serial_number;
151 __le32 padding;
152 __le32 clock_resolution;
153 __le32 mfgdate;
154 u8 ean[8];
155 u8 hw_revision;
156 u8 usb_hs_mode;
157 __le16 padding2;
158 } __packed;
160 struct kvaser_msg_cardinfo2 {
161 u8 tid;
162 u8 channel;
163 u8 pcb_id[24];
164 __le32 oem_unlock_code;
165 } __packed;
167 struct kvaser_msg_softinfo {
168 u8 tid;
169 u8 channel;
170 __le32 sw_options;
171 __le32 fw_version;
172 __le16 max_outstanding_tx;
173 __le16 padding[9];
174 } __packed;
176 struct kvaser_msg_busparams {
177 u8 tid;
178 u8 channel;
179 __le32 bitrate;
180 u8 tseg1;
181 u8 tseg2;
182 u8 sjw;
183 u8 no_samp;
184 } __packed;
186 struct kvaser_msg_tx_can {
187 u8 channel;
188 u8 tid;
189 u8 msg[14];
190 u8 padding;
191 u8 flags;
192 } __packed;
194 struct kvaser_msg_rx_can {
195 u8 channel;
196 u8 flag;
197 __le16 time[3];
198 u8 msg[14];
199 } __packed;
201 struct kvaser_msg_chip_state_event {
202 u8 tid;
203 u8 channel;
204 __le16 time[3];
205 u8 tx_errors_count;
206 u8 rx_errors_count;
207 u8 status;
208 u8 padding[3];
209 } __packed;
211 struct kvaser_msg_tx_acknowledge {
212 u8 channel;
213 u8 tid;
214 __le16 time[3];
215 u8 flags;
216 u8 time_offset;
217 } __packed;
219 struct kvaser_msg_error_event {
220 u8 tid;
221 u8 flags;
222 __le16 time[3];
223 u8 channel;
224 u8 padding;
225 u8 tx_errors_count;
226 u8 rx_errors_count;
227 u8 status;
228 u8 error_factor;
229 } __packed;
231 struct kvaser_msg_ctrl_mode {
232 u8 tid;
233 u8 channel;
234 u8 ctrl_mode;
235 u8 padding[3];
236 } __packed;
238 struct kvaser_msg_flush_queue {
239 u8 tid;
240 u8 channel;
241 u8 flags;
242 u8 padding[3];
243 } __packed;
245 struct kvaser_msg_log_message {
246 u8 channel;
247 u8 flags;
248 __le16 time[3];
249 u8 dlc;
250 u8 time_offset;
251 __le32 id;
252 u8 data[8];
253 } __packed;
255 struct kvaser_msg {
256 u8 len;
257 u8 id;
258 union {
259 struct kvaser_msg_simple simple;
260 struct kvaser_msg_cardinfo cardinfo;
261 struct kvaser_msg_cardinfo2 cardinfo2;
262 struct kvaser_msg_softinfo softinfo;
263 struct kvaser_msg_busparams busparams;
264 struct kvaser_msg_tx_can tx_can;
265 struct kvaser_msg_rx_can rx_can;
266 struct kvaser_msg_chip_state_event chip_state_event;
267 struct kvaser_msg_tx_acknowledge tx_acknowledge;
268 struct kvaser_msg_error_event error_event;
269 struct kvaser_msg_ctrl_mode ctrl_mode;
270 struct kvaser_msg_flush_queue flush_queue;
271 struct kvaser_msg_log_message log_message;
272 } u;
273 } __packed;
275 struct kvaser_usb_tx_urb_context {
276 struct kvaser_usb_net_priv *priv;
277 u32 echo_index;
278 int dlc;
281 struct kvaser_usb {
282 struct usb_device *udev;
283 struct kvaser_usb_net_priv *nets[MAX_NET_DEVICES];
285 struct usb_endpoint_descriptor *bulk_in, *bulk_out;
286 struct usb_anchor rx_submitted;
288 u32 fw_version;
289 unsigned int nchannels;
291 bool rxinitdone;
292 void *rxbuf[MAX_RX_URBS];
293 dma_addr_t rxbuf_dma[MAX_RX_URBS];
296 struct kvaser_usb_net_priv {
297 struct can_priv can;
299 atomic_t active_tx_urbs;
300 struct usb_anchor tx_submitted;
301 struct kvaser_usb_tx_urb_context tx_contexts[MAX_TX_URBS];
303 struct completion start_comp, stop_comp;
305 struct kvaser_usb *dev;
306 struct net_device *netdev;
307 int channel;
309 struct can_berr_counter bec;
312 static const struct usb_device_id kvaser_usb_table[] = {
313 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_DEVEL_PRODUCT_ID) },
314 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_PRODUCT_ID) },
315 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_PRODUCT_ID),
316 .driver_info = KVASER_HAS_TXRX_ERRORS |
317 KVASER_HAS_SILENT_MODE },
318 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_SPRO_PRODUCT_ID),
319 .driver_info = KVASER_HAS_TXRX_ERRORS |
320 KVASER_HAS_SILENT_MODE },
321 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_LS_PRODUCT_ID),
322 .driver_info = KVASER_HAS_TXRX_ERRORS |
323 KVASER_HAS_SILENT_MODE },
324 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_SWC_PRODUCT_ID),
325 .driver_info = KVASER_HAS_TXRX_ERRORS |
326 KVASER_HAS_SILENT_MODE },
327 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_LIN_PRODUCT_ID),
328 .driver_info = KVASER_HAS_TXRX_ERRORS |
329 KVASER_HAS_SILENT_MODE },
330 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_SPRO_LS_PRODUCT_ID),
331 .driver_info = KVASER_HAS_TXRX_ERRORS |
332 KVASER_HAS_SILENT_MODE },
333 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_SPRO_SWC_PRODUCT_ID),
334 .driver_info = KVASER_HAS_TXRX_ERRORS |
335 KVASER_HAS_SILENT_MODE },
336 { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO2_DEVEL_PRODUCT_ID),
337 .driver_info = KVASER_HAS_TXRX_ERRORS |
338 KVASER_HAS_SILENT_MODE },
339 { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO2_HSHS_PRODUCT_ID),
340 .driver_info = KVASER_HAS_TXRX_ERRORS |
341 KVASER_HAS_SILENT_MODE },
342 { USB_DEVICE(KVASER_VENDOR_ID, USB_UPRO_HSHS_PRODUCT_ID),
343 .driver_info = KVASER_HAS_TXRX_ERRORS },
344 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_GI_PRODUCT_ID) },
345 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_OBDII_PRODUCT_ID),
346 .driver_info = KVASER_HAS_TXRX_ERRORS |
347 KVASER_HAS_SILENT_MODE },
348 { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO2_HSLS_PRODUCT_ID),
349 .driver_info = KVASER_HAS_TXRX_ERRORS },
350 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_CH_PRODUCT_ID),
351 .driver_info = KVASER_HAS_TXRX_ERRORS },
352 { USB_DEVICE(KVASER_VENDOR_ID, USB_BLACKBIRD_SPRO_PRODUCT_ID),
353 .driver_info = KVASER_HAS_TXRX_ERRORS },
354 { USB_DEVICE(KVASER_VENDOR_ID, USB_OEM_MERCURY_PRODUCT_ID),
355 .driver_info = KVASER_HAS_TXRX_ERRORS },
356 { USB_DEVICE(KVASER_VENDOR_ID, USB_OEM_LEAF_PRODUCT_ID),
357 .driver_info = KVASER_HAS_TXRX_ERRORS },
358 { USB_DEVICE(KVASER_VENDOR_ID, USB_CAN_R_PRODUCT_ID),
359 .driver_info = KVASER_HAS_TXRX_ERRORS },
362 MODULE_DEVICE_TABLE(usb, kvaser_usb_table);
364 static inline int kvaser_usb_send_msg(const struct kvaser_usb *dev,
365 struct kvaser_msg *msg)
367 int actual_len;
369 return usb_bulk_msg(dev->udev,
370 usb_sndbulkpipe(dev->udev,
371 dev->bulk_out->bEndpointAddress),
372 msg, msg->len, &actual_len,
373 USB_SEND_TIMEOUT);
376 static int kvaser_usb_wait_msg(const struct kvaser_usb *dev, u8 id,
377 struct kvaser_msg *msg)
379 struct kvaser_msg *tmp;
380 void *buf;
381 int actual_len;
382 int err;
383 int pos = 0;
385 buf = kzalloc(RX_BUFFER_SIZE, GFP_KERNEL);
386 if (!buf)
387 return -ENOMEM;
389 err = usb_bulk_msg(dev->udev,
390 usb_rcvbulkpipe(dev->udev,
391 dev->bulk_in->bEndpointAddress),
392 buf, RX_BUFFER_SIZE, &actual_len,
393 USB_RECV_TIMEOUT);
394 if (err < 0)
395 goto end;
397 while (pos <= actual_len - MSG_HEADER_LEN) {
398 tmp = buf + pos;
400 if (!tmp->len)
401 break;
403 if (pos + tmp->len > actual_len) {
404 dev_err(dev->udev->dev.parent, "Format error\n");
405 break;
408 if (tmp->id == id) {
409 memcpy(msg, tmp, tmp->len);
410 goto end;
413 pos += tmp->len;
416 err = -EINVAL;
418 end:
419 kfree(buf);
421 return err;
424 static int kvaser_usb_send_simple_msg(const struct kvaser_usb *dev,
425 u8 msg_id, int channel)
427 struct kvaser_msg *msg;
428 int rc;
430 msg = kmalloc(sizeof(*msg), GFP_KERNEL);
431 if (!msg)
432 return -ENOMEM;
434 msg->id = msg_id;
435 msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_simple);
436 msg->u.simple.channel = channel;
437 msg->u.simple.tid = 0xff;
439 rc = kvaser_usb_send_msg(dev, msg);
441 kfree(msg);
442 return rc;
445 static int kvaser_usb_get_software_info(struct kvaser_usb *dev)
447 struct kvaser_msg msg;
448 int err;
450 err = kvaser_usb_send_simple_msg(dev, CMD_GET_SOFTWARE_INFO, 0);
451 if (err)
452 return err;
454 err = kvaser_usb_wait_msg(dev, CMD_GET_SOFTWARE_INFO_REPLY, &msg);
455 if (err)
456 return err;
458 dev->fw_version = le32_to_cpu(msg.u.softinfo.fw_version);
460 return 0;
463 static int kvaser_usb_get_card_info(struct kvaser_usb *dev)
465 struct kvaser_msg msg;
466 int err;
468 err = kvaser_usb_send_simple_msg(dev, CMD_GET_CARD_INFO, 0);
469 if (err)
470 return err;
472 err = kvaser_usb_wait_msg(dev, CMD_GET_CARD_INFO_REPLY, &msg);
473 if (err)
474 return err;
476 dev->nchannels = msg.u.cardinfo.nchannels;
477 if (dev->nchannels > MAX_NET_DEVICES)
478 return -EINVAL;
480 return 0;
483 static void kvaser_usb_tx_acknowledge(const struct kvaser_usb *dev,
484 const struct kvaser_msg *msg)
486 struct net_device_stats *stats;
487 struct kvaser_usb_tx_urb_context *context;
488 struct kvaser_usb_net_priv *priv;
489 struct sk_buff *skb;
490 struct can_frame *cf;
491 u8 channel = msg->u.tx_acknowledge.channel;
492 u8 tid = msg->u.tx_acknowledge.tid;
494 if (channel >= dev->nchannels) {
495 dev_err(dev->udev->dev.parent,
496 "Invalid channel number (%d)\n", channel);
497 return;
500 priv = dev->nets[channel];
502 if (!netif_device_present(priv->netdev))
503 return;
505 stats = &priv->netdev->stats;
507 context = &priv->tx_contexts[tid % MAX_TX_URBS];
509 /* Sometimes the state change doesn't come after a bus-off event */
510 if (priv->can.restart_ms &&
511 (priv->can.state >= CAN_STATE_BUS_OFF)) {
512 skb = alloc_can_err_skb(priv->netdev, &cf);
513 if (skb) {
514 cf->can_id |= CAN_ERR_RESTARTED;
515 netif_rx(skb);
517 stats->rx_packets++;
518 stats->rx_bytes += cf->can_dlc;
519 } else {
520 netdev_err(priv->netdev,
521 "No memory left for err_skb\n");
524 priv->can.can_stats.restarts++;
525 netif_carrier_on(priv->netdev);
527 priv->can.state = CAN_STATE_ERROR_ACTIVE;
530 stats->tx_packets++;
531 stats->tx_bytes += context->dlc;
532 can_get_echo_skb(priv->netdev, context->echo_index);
534 context->echo_index = MAX_TX_URBS;
535 atomic_dec(&priv->active_tx_urbs);
537 netif_wake_queue(priv->netdev);
540 static void kvaser_usb_simple_msg_callback(struct urb *urb)
542 struct net_device *netdev = urb->context;
544 kfree(urb->transfer_buffer);
546 if (urb->status)
547 netdev_warn(netdev, "urb status received: %d\n",
548 urb->status);
551 static int kvaser_usb_simple_msg_async(struct kvaser_usb_net_priv *priv,
552 u8 msg_id)
554 struct kvaser_usb *dev = priv->dev;
555 struct net_device *netdev = priv->netdev;
556 struct kvaser_msg *msg;
557 struct urb *urb;
558 void *buf;
559 int err;
561 urb = usb_alloc_urb(0, GFP_ATOMIC);
562 if (!urb) {
563 netdev_err(netdev, "No memory left for URBs\n");
564 return -ENOMEM;
567 buf = kmalloc(sizeof(struct kvaser_msg), GFP_ATOMIC);
568 if (!buf) {
569 usb_free_urb(urb);
570 return -ENOMEM;
573 msg = (struct kvaser_msg *)buf;
574 msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_simple);
575 msg->id = msg_id;
576 msg->u.simple.channel = priv->channel;
578 usb_fill_bulk_urb(urb, dev->udev,
579 usb_sndbulkpipe(dev->udev,
580 dev->bulk_out->bEndpointAddress),
581 buf, msg->len,
582 kvaser_usb_simple_msg_callback, netdev);
583 usb_anchor_urb(urb, &priv->tx_submitted);
585 err = usb_submit_urb(urb, GFP_ATOMIC);
586 if (err) {
587 netdev_err(netdev, "Error transmitting URB\n");
588 usb_unanchor_urb(urb);
589 usb_free_urb(urb);
590 kfree(buf);
591 return err;
594 usb_free_urb(urb);
596 return 0;
599 static void kvaser_usb_unlink_tx_urbs(struct kvaser_usb_net_priv *priv)
601 int i;
603 usb_kill_anchored_urbs(&priv->tx_submitted);
604 atomic_set(&priv->active_tx_urbs, 0);
606 for (i = 0; i < MAX_TX_URBS; i++)
607 priv->tx_contexts[i].echo_index = MAX_TX_URBS;
610 static void kvaser_usb_rx_error(const struct kvaser_usb *dev,
611 const struct kvaser_msg *msg)
613 struct can_frame *cf;
614 struct sk_buff *skb;
615 struct net_device_stats *stats;
616 struct kvaser_usb_net_priv *priv;
617 unsigned int new_state;
618 u8 channel, status, txerr, rxerr, error_factor;
620 switch (msg->id) {
621 case CMD_CAN_ERROR_EVENT:
622 channel = msg->u.error_event.channel;
623 status = msg->u.error_event.status;
624 txerr = msg->u.error_event.tx_errors_count;
625 rxerr = msg->u.error_event.rx_errors_count;
626 error_factor = msg->u.error_event.error_factor;
627 break;
628 case CMD_LOG_MESSAGE:
629 channel = msg->u.log_message.channel;
630 status = msg->u.log_message.data[0];
631 txerr = msg->u.log_message.data[2];
632 rxerr = msg->u.log_message.data[3];
633 error_factor = msg->u.log_message.data[1];
634 break;
635 case CMD_CHIP_STATE_EVENT:
636 channel = msg->u.chip_state_event.channel;
637 status = msg->u.chip_state_event.status;
638 txerr = msg->u.chip_state_event.tx_errors_count;
639 rxerr = msg->u.chip_state_event.rx_errors_count;
640 error_factor = 0;
641 break;
642 default:
643 dev_err(dev->udev->dev.parent, "Invalid msg id (%d)\n",
644 msg->id);
645 return;
648 if (channel >= dev->nchannels) {
649 dev_err(dev->udev->dev.parent,
650 "Invalid channel number (%d)\n", channel);
651 return;
654 priv = dev->nets[channel];
655 stats = &priv->netdev->stats;
657 skb = alloc_can_err_skb(priv->netdev, &cf);
658 if (!skb) {
659 stats->rx_dropped++;
660 return;
663 new_state = priv->can.state;
665 netdev_dbg(priv->netdev, "Error status: 0x%02x\n", status);
667 if (status & (M16C_STATE_BUS_OFF | M16C_STATE_BUS_RESET)) {
668 cf->can_id |= CAN_ERR_BUSOFF;
670 priv->can.can_stats.bus_off++;
671 if (!priv->can.restart_ms)
672 kvaser_usb_simple_msg_async(priv, CMD_STOP_CHIP);
674 netif_carrier_off(priv->netdev);
676 new_state = CAN_STATE_BUS_OFF;
677 } else if (status & M16C_STATE_BUS_PASSIVE) {
678 if (priv->can.state != CAN_STATE_ERROR_PASSIVE) {
679 cf->can_id |= CAN_ERR_CRTL;
681 if (txerr || rxerr)
682 cf->data[1] = (txerr > rxerr)
683 ? CAN_ERR_CRTL_TX_PASSIVE
684 : CAN_ERR_CRTL_RX_PASSIVE;
685 else
686 cf->data[1] = CAN_ERR_CRTL_TX_PASSIVE |
687 CAN_ERR_CRTL_RX_PASSIVE;
689 priv->can.can_stats.error_passive++;
692 new_state = CAN_STATE_ERROR_PASSIVE;
693 } else if (status & M16C_STATE_BUS_ERROR) {
694 if ((priv->can.state < CAN_STATE_ERROR_WARNING) &&
695 ((txerr >= 96) || (rxerr >= 96))) {
696 cf->can_id |= CAN_ERR_CRTL;
697 cf->data[1] = (txerr > rxerr)
698 ? CAN_ERR_CRTL_TX_WARNING
699 : CAN_ERR_CRTL_RX_WARNING;
701 priv->can.can_stats.error_warning++;
702 new_state = CAN_STATE_ERROR_WARNING;
703 } else if ((priv->can.state > CAN_STATE_ERROR_ACTIVE) &&
704 ((txerr < 96) && (rxerr < 96))) {
705 cf->can_id |= CAN_ERR_PROT;
706 cf->data[2] = CAN_ERR_PROT_ACTIVE;
708 new_state = CAN_STATE_ERROR_ACTIVE;
712 if (!status) {
713 cf->can_id |= CAN_ERR_PROT;
714 cf->data[2] = CAN_ERR_PROT_ACTIVE;
716 new_state = CAN_STATE_ERROR_ACTIVE;
719 if (priv->can.restart_ms &&
720 (priv->can.state >= CAN_STATE_BUS_OFF) &&
721 (new_state < CAN_STATE_BUS_OFF)) {
722 cf->can_id |= CAN_ERR_RESTARTED;
723 netif_carrier_on(priv->netdev);
725 priv->can.can_stats.restarts++;
728 if (error_factor) {
729 priv->can.can_stats.bus_error++;
730 stats->rx_errors++;
732 cf->can_id |= CAN_ERR_BUSERROR | CAN_ERR_PROT;
734 if (error_factor & M16C_EF_ACKE)
735 cf->data[3] |= (CAN_ERR_PROT_LOC_ACK);
736 if (error_factor & M16C_EF_CRCE)
737 cf->data[3] |= (CAN_ERR_PROT_LOC_CRC_SEQ |
738 CAN_ERR_PROT_LOC_CRC_DEL);
739 if (error_factor & M16C_EF_FORME)
740 cf->data[2] |= CAN_ERR_PROT_FORM;
741 if (error_factor & M16C_EF_STFE)
742 cf->data[2] |= CAN_ERR_PROT_STUFF;
743 if (error_factor & M16C_EF_BITE0)
744 cf->data[2] |= CAN_ERR_PROT_BIT0;
745 if (error_factor & M16C_EF_BITE1)
746 cf->data[2] |= CAN_ERR_PROT_BIT1;
747 if (error_factor & M16C_EF_TRE)
748 cf->data[2] |= CAN_ERR_PROT_TX;
751 cf->data[6] = txerr;
752 cf->data[7] = rxerr;
754 priv->bec.txerr = txerr;
755 priv->bec.rxerr = rxerr;
757 priv->can.state = new_state;
759 netif_rx(skb);
761 stats->rx_packets++;
762 stats->rx_bytes += cf->can_dlc;
765 static void kvaser_usb_rx_can_err(const struct kvaser_usb_net_priv *priv,
766 const struct kvaser_msg *msg)
768 struct can_frame *cf;
769 struct sk_buff *skb;
770 struct net_device_stats *stats = &priv->netdev->stats;
772 if (msg->u.rx_can.flag & (MSG_FLAG_ERROR_FRAME |
773 MSG_FLAG_NERR)) {
774 netdev_err(priv->netdev, "Unknow error (flags: 0x%02x)\n",
775 msg->u.rx_can.flag);
777 stats->rx_errors++;
778 return;
781 if (msg->u.rx_can.flag & MSG_FLAG_OVERRUN) {
782 skb = alloc_can_err_skb(priv->netdev, &cf);
783 if (!skb) {
784 stats->rx_dropped++;
785 return;
788 cf->can_id |= CAN_ERR_CRTL;
789 cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
791 stats->rx_over_errors++;
792 stats->rx_errors++;
794 netif_rx(skb);
796 stats->rx_packets++;
797 stats->rx_bytes += cf->can_dlc;
801 static void kvaser_usb_rx_can_msg(const struct kvaser_usb *dev,
802 const struct kvaser_msg *msg)
804 struct kvaser_usb_net_priv *priv;
805 struct can_frame *cf;
806 struct sk_buff *skb;
807 struct net_device_stats *stats;
808 u8 channel = msg->u.rx_can.channel;
810 if (channel >= dev->nchannels) {
811 dev_err(dev->udev->dev.parent,
812 "Invalid channel number (%d)\n", channel);
813 return;
816 priv = dev->nets[channel];
817 stats = &priv->netdev->stats;
819 if ((msg->u.rx_can.flag & MSG_FLAG_ERROR_FRAME) &&
820 (msg->id == CMD_LOG_MESSAGE)) {
821 kvaser_usb_rx_error(dev, msg);
822 return;
823 } else if (msg->u.rx_can.flag & (MSG_FLAG_ERROR_FRAME |
824 MSG_FLAG_NERR |
825 MSG_FLAG_OVERRUN)) {
826 kvaser_usb_rx_can_err(priv, msg);
827 return;
828 } else if (msg->u.rx_can.flag & ~MSG_FLAG_REMOTE_FRAME) {
829 netdev_warn(priv->netdev,
830 "Unhandled frame (flags: 0x%02x)",
831 msg->u.rx_can.flag);
832 return;
835 skb = alloc_can_skb(priv->netdev, &cf);
836 if (!skb) {
837 stats->tx_dropped++;
838 return;
841 if (msg->id == CMD_LOG_MESSAGE) {
842 cf->can_id = le32_to_cpu(msg->u.log_message.id);
843 if (cf->can_id & KVASER_EXTENDED_FRAME)
844 cf->can_id &= CAN_EFF_MASK | CAN_EFF_FLAG;
845 else
846 cf->can_id &= CAN_SFF_MASK;
848 cf->can_dlc = get_can_dlc(msg->u.log_message.dlc);
850 if (msg->u.log_message.flags & MSG_FLAG_REMOTE_FRAME)
851 cf->can_id |= CAN_RTR_FLAG;
852 else
853 memcpy(cf->data, &msg->u.log_message.data,
854 cf->can_dlc);
855 } else {
856 cf->can_id = ((msg->u.rx_can.msg[0] & 0x1f) << 6) |
857 (msg->u.rx_can.msg[1] & 0x3f);
859 if (msg->id == CMD_RX_EXT_MESSAGE) {
860 cf->can_id <<= 18;
861 cf->can_id |= ((msg->u.rx_can.msg[2] & 0x0f) << 14) |
862 ((msg->u.rx_can.msg[3] & 0xff) << 6) |
863 (msg->u.rx_can.msg[4] & 0x3f);
864 cf->can_id |= CAN_EFF_FLAG;
867 cf->can_dlc = get_can_dlc(msg->u.rx_can.msg[5]);
869 if (msg->u.rx_can.flag & MSG_FLAG_REMOTE_FRAME)
870 cf->can_id |= CAN_RTR_FLAG;
871 else
872 memcpy(cf->data, &msg->u.rx_can.msg[6],
873 cf->can_dlc);
876 netif_rx(skb);
878 stats->rx_packets++;
879 stats->rx_bytes += cf->can_dlc;
882 static void kvaser_usb_start_chip_reply(const struct kvaser_usb *dev,
883 const struct kvaser_msg *msg)
885 struct kvaser_usb_net_priv *priv;
886 u8 channel = msg->u.simple.channel;
888 if (channel >= dev->nchannels) {
889 dev_err(dev->udev->dev.parent,
890 "Invalid channel number (%d)\n", channel);
891 return;
894 priv = dev->nets[channel];
896 if (completion_done(&priv->start_comp) &&
897 netif_queue_stopped(priv->netdev)) {
898 netif_wake_queue(priv->netdev);
899 } else {
900 netif_start_queue(priv->netdev);
901 complete(&priv->start_comp);
905 static void kvaser_usb_stop_chip_reply(const struct kvaser_usb *dev,
906 const struct kvaser_msg *msg)
908 struct kvaser_usb_net_priv *priv;
909 u8 channel = msg->u.simple.channel;
911 if (channel >= dev->nchannels) {
912 dev_err(dev->udev->dev.parent,
913 "Invalid channel number (%d)\n", channel);
914 return;
917 priv = dev->nets[channel];
919 complete(&priv->stop_comp);
922 static void kvaser_usb_handle_message(const struct kvaser_usb *dev,
923 const struct kvaser_msg *msg)
925 switch (msg->id) {
926 case CMD_START_CHIP_REPLY:
927 kvaser_usb_start_chip_reply(dev, msg);
928 break;
930 case CMD_STOP_CHIP_REPLY:
931 kvaser_usb_stop_chip_reply(dev, msg);
932 break;
934 case CMD_RX_STD_MESSAGE:
935 case CMD_RX_EXT_MESSAGE:
936 case CMD_LOG_MESSAGE:
937 kvaser_usb_rx_can_msg(dev, msg);
938 break;
940 case CMD_CHIP_STATE_EVENT:
941 case CMD_CAN_ERROR_EVENT:
942 kvaser_usb_rx_error(dev, msg);
943 break;
945 case CMD_TX_ACKNOWLEDGE:
946 kvaser_usb_tx_acknowledge(dev, msg);
947 break;
949 default:
950 dev_warn(dev->udev->dev.parent,
951 "Unhandled message (%d)\n", msg->id);
952 break;
956 static void kvaser_usb_read_bulk_callback(struct urb *urb)
958 struct kvaser_usb *dev = urb->context;
959 struct kvaser_msg *msg;
960 int pos = 0;
961 int err, i;
963 switch (urb->status) {
964 case 0:
965 break;
966 case -ENOENT:
967 case -ESHUTDOWN:
968 return;
969 default:
970 dev_info(dev->udev->dev.parent, "Rx URB aborted (%d)\n",
971 urb->status);
972 goto resubmit_urb;
975 while (pos <= urb->actual_length - MSG_HEADER_LEN) {
976 msg = urb->transfer_buffer + pos;
978 if (!msg->len)
979 break;
981 if (pos + msg->len > urb->actual_length) {
982 dev_err(dev->udev->dev.parent, "Format error\n");
983 break;
986 kvaser_usb_handle_message(dev, msg);
988 pos += msg->len;
991 resubmit_urb:
992 usb_fill_bulk_urb(urb, dev->udev,
993 usb_rcvbulkpipe(dev->udev,
994 dev->bulk_in->bEndpointAddress),
995 urb->transfer_buffer, RX_BUFFER_SIZE,
996 kvaser_usb_read_bulk_callback, dev);
998 err = usb_submit_urb(urb, GFP_ATOMIC);
999 if (err == -ENODEV) {
1000 for (i = 0; i < dev->nchannels; i++) {
1001 if (!dev->nets[i])
1002 continue;
1004 netif_device_detach(dev->nets[i]->netdev);
1006 } else if (err) {
1007 dev_err(dev->udev->dev.parent,
1008 "Failed resubmitting read bulk urb: %d\n", err);
1011 return;
1014 static int kvaser_usb_setup_rx_urbs(struct kvaser_usb *dev)
1016 int i, err = 0;
1018 if (dev->rxinitdone)
1019 return 0;
1021 for (i = 0; i < MAX_RX_URBS; i++) {
1022 struct urb *urb = NULL;
1023 u8 *buf = NULL;
1024 dma_addr_t buf_dma;
1026 urb = usb_alloc_urb(0, GFP_KERNEL);
1027 if (!urb) {
1028 dev_warn(dev->udev->dev.parent,
1029 "No memory left for URBs\n");
1030 err = -ENOMEM;
1031 break;
1034 buf = usb_alloc_coherent(dev->udev, RX_BUFFER_SIZE,
1035 GFP_KERNEL, &buf_dma);
1036 if (!buf) {
1037 dev_warn(dev->udev->dev.parent,
1038 "No memory left for USB buffer\n");
1039 usb_free_urb(urb);
1040 err = -ENOMEM;
1041 break;
1044 usb_fill_bulk_urb(urb, dev->udev,
1045 usb_rcvbulkpipe(dev->udev,
1046 dev->bulk_in->bEndpointAddress),
1047 buf, RX_BUFFER_SIZE,
1048 kvaser_usb_read_bulk_callback,
1049 dev);
1050 urb->transfer_dma = buf_dma;
1051 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1052 usb_anchor_urb(urb, &dev->rx_submitted);
1054 err = usb_submit_urb(urb, GFP_KERNEL);
1055 if (err) {
1056 usb_unanchor_urb(urb);
1057 usb_free_coherent(dev->udev, RX_BUFFER_SIZE, buf,
1058 buf_dma);
1059 usb_free_urb(urb);
1060 break;
1063 dev->rxbuf[i] = buf;
1064 dev->rxbuf_dma[i] = buf_dma;
1066 usb_free_urb(urb);
1069 if (i == 0) {
1070 dev_warn(dev->udev->dev.parent,
1071 "Cannot setup read URBs, error %d\n", err);
1072 return err;
1073 } else if (i < MAX_RX_URBS) {
1074 dev_warn(dev->udev->dev.parent,
1075 "RX performances may be slow\n");
1078 dev->rxinitdone = true;
1080 return 0;
1083 static int kvaser_usb_set_opt_mode(const struct kvaser_usb_net_priv *priv)
1085 struct kvaser_msg *msg;
1086 int rc;
1088 msg = kmalloc(sizeof(*msg), GFP_KERNEL);
1089 if (!msg)
1090 return -ENOMEM;
1092 msg->id = CMD_SET_CTRL_MODE;
1093 msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_ctrl_mode);
1094 msg->u.ctrl_mode.tid = 0xff;
1095 msg->u.ctrl_mode.channel = priv->channel;
1097 if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
1098 msg->u.ctrl_mode.ctrl_mode = KVASER_CTRL_MODE_SILENT;
1099 else
1100 msg->u.ctrl_mode.ctrl_mode = KVASER_CTRL_MODE_NORMAL;
1102 rc = kvaser_usb_send_msg(priv->dev, msg);
1104 kfree(msg);
1105 return rc;
1108 static int kvaser_usb_start_chip(struct kvaser_usb_net_priv *priv)
1110 int err;
1112 init_completion(&priv->start_comp);
1114 err = kvaser_usb_send_simple_msg(priv->dev, CMD_START_CHIP,
1115 priv->channel);
1116 if (err)
1117 return err;
1119 if (!wait_for_completion_timeout(&priv->start_comp,
1120 msecs_to_jiffies(START_TIMEOUT)))
1121 return -ETIMEDOUT;
1123 return 0;
1126 static int kvaser_usb_open(struct net_device *netdev)
1128 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1129 struct kvaser_usb *dev = priv->dev;
1130 int err;
1132 err = open_candev(netdev);
1133 if (err)
1134 return err;
1136 err = kvaser_usb_setup_rx_urbs(dev);
1137 if (err)
1138 goto error;
1140 err = kvaser_usb_set_opt_mode(priv);
1141 if (err)
1142 goto error;
1144 err = kvaser_usb_start_chip(priv);
1145 if (err) {
1146 netdev_warn(netdev, "Cannot start device, error %d\n", err);
1147 goto error;
1150 priv->can.state = CAN_STATE_ERROR_ACTIVE;
1152 return 0;
1154 error:
1155 close_candev(netdev);
1156 return err;
1159 static void kvaser_usb_unlink_all_urbs(struct kvaser_usb *dev)
1161 int i;
1163 usb_kill_anchored_urbs(&dev->rx_submitted);
1165 for (i = 0; i < MAX_RX_URBS; i++)
1166 usb_free_coherent(dev->udev, RX_BUFFER_SIZE,
1167 dev->rxbuf[i],
1168 dev->rxbuf_dma[i]);
1170 for (i = 0; i < MAX_NET_DEVICES; i++) {
1171 struct kvaser_usb_net_priv *priv = dev->nets[i];
1173 if (priv)
1174 kvaser_usb_unlink_tx_urbs(priv);
1178 static int kvaser_usb_stop_chip(struct kvaser_usb_net_priv *priv)
1180 int err;
1182 init_completion(&priv->stop_comp);
1184 err = kvaser_usb_send_simple_msg(priv->dev, CMD_STOP_CHIP,
1185 priv->channel);
1186 if (err)
1187 return err;
1189 if (!wait_for_completion_timeout(&priv->stop_comp,
1190 msecs_to_jiffies(STOP_TIMEOUT)))
1191 return -ETIMEDOUT;
1193 return 0;
1196 static int kvaser_usb_flush_queue(struct kvaser_usb_net_priv *priv)
1198 struct kvaser_msg *msg;
1199 int rc;
1201 msg = kmalloc(sizeof(*msg), GFP_KERNEL);
1202 if (!msg)
1203 return -ENOMEM;
1205 msg->id = CMD_FLUSH_QUEUE;
1206 msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_flush_queue);
1207 msg->u.flush_queue.channel = priv->channel;
1208 msg->u.flush_queue.flags = 0x00;
1210 rc = kvaser_usb_send_msg(priv->dev, msg);
1212 kfree(msg);
1213 return rc;
1216 static int kvaser_usb_close(struct net_device *netdev)
1218 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1219 struct kvaser_usb *dev = priv->dev;
1220 int err;
1222 netif_stop_queue(netdev);
1224 err = kvaser_usb_flush_queue(priv);
1225 if (err)
1226 netdev_warn(netdev, "Cannot flush queue, error %d\n", err);
1228 if (kvaser_usb_send_simple_msg(dev, CMD_RESET_CHIP, priv->channel))
1229 netdev_warn(netdev, "Cannot reset card, error %d\n", err);
1231 err = kvaser_usb_stop_chip(priv);
1232 if (err)
1233 netdev_warn(netdev, "Cannot stop device, error %d\n", err);
1235 /* reset tx contexts */
1236 kvaser_usb_unlink_tx_urbs(priv);
1238 priv->can.state = CAN_STATE_STOPPED;
1239 close_candev(priv->netdev);
1241 return 0;
1244 static void kvaser_usb_write_bulk_callback(struct urb *urb)
1246 struct kvaser_usb_tx_urb_context *context = urb->context;
1247 struct kvaser_usb_net_priv *priv;
1248 struct net_device *netdev;
1250 if (WARN_ON(!context))
1251 return;
1253 priv = context->priv;
1254 netdev = priv->netdev;
1256 kfree(urb->transfer_buffer);
1258 if (!netif_device_present(netdev))
1259 return;
1261 if (urb->status)
1262 netdev_info(netdev, "Tx URB aborted (%d)\n", urb->status);
1265 static netdev_tx_t kvaser_usb_start_xmit(struct sk_buff *skb,
1266 struct net_device *netdev)
1268 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1269 struct kvaser_usb *dev = priv->dev;
1270 struct net_device_stats *stats = &netdev->stats;
1271 struct can_frame *cf = (struct can_frame *)skb->data;
1272 struct kvaser_usb_tx_urb_context *context = NULL;
1273 struct urb *urb;
1274 void *buf;
1275 struct kvaser_msg *msg;
1276 int i, err;
1277 int ret = NETDEV_TX_OK;
1279 if (can_dropped_invalid_skb(netdev, skb))
1280 return NETDEV_TX_OK;
1282 urb = usb_alloc_urb(0, GFP_ATOMIC);
1283 if (!urb) {
1284 netdev_err(netdev, "No memory left for URBs\n");
1285 stats->tx_dropped++;
1286 dev_kfree_skb(skb);
1287 return NETDEV_TX_OK;
1290 buf = kmalloc(sizeof(struct kvaser_msg), GFP_ATOMIC);
1291 if (!buf) {
1292 stats->tx_dropped++;
1293 dev_kfree_skb(skb);
1294 goto nobufmem;
1297 msg = buf;
1298 msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_tx_can);
1299 msg->u.tx_can.flags = 0;
1300 msg->u.tx_can.channel = priv->channel;
1302 if (cf->can_id & CAN_EFF_FLAG) {
1303 msg->id = CMD_TX_EXT_MESSAGE;
1304 msg->u.tx_can.msg[0] = (cf->can_id >> 24) & 0x1f;
1305 msg->u.tx_can.msg[1] = (cf->can_id >> 18) & 0x3f;
1306 msg->u.tx_can.msg[2] = (cf->can_id >> 14) & 0x0f;
1307 msg->u.tx_can.msg[3] = (cf->can_id >> 6) & 0xff;
1308 msg->u.tx_can.msg[4] = cf->can_id & 0x3f;
1309 } else {
1310 msg->id = CMD_TX_STD_MESSAGE;
1311 msg->u.tx_can.msg[0] = (cf->can_id >> 6) & 0x1f;
1312 msg->u.tx_can.msg[1] = cf->can_id & 0x3f;
1315 msg->u.tx_can.msg[5] = cf->can_dlc;
1316 memcpy(&msg->u.tx_can.msg[6], cf->data, cf->can_dlc);
1318 if (cf->can_id & CAN_RTR_FLAG)
1319 msg->u.tx_can.flags |= MSG_FLAG_REMOTE_FRAME;
1321 for (i = 0; i < ARRAY_SIZE(priv->tx_contexts); i++) {
1322 if (priv->tx_contexts[i].echo_index == MAX_TX_URBS) {
1323 context = &priv->tx_contexts[i];
1324 break;
1328 /* This should never happen; it implies a flow control bug */
1329 if (!context) {
1330 netdev_warn(netdev, "cannot find free context\n");
1331 ret = NETDEV_TX_BUSY;
1332 goto releasebuf;
1335 context->priv = priv;
1336 context->echo_index = i;
1337 context->dlc = cf->can_dlc;
1339 msg->u.tx_can.tid = context->echo_index;
1341 usb_fill_bulk_urb(urb, dev->udev,
1342 usb_sndbulkpipe(dev->udev,
1343 dev->bulk_out->bEndpointAddress),
1344 buf, msg->len,
1345 kvaser_usb_write_bulk_callback, context);
1346 usb_anchor_urb(urb, &priv->tx_submitted);
1348 can_put_echo_skb(skb, netdev, context->echo_index);
1350 atomic_inc(&priv->active_tx_urbs);
1352 if (atomic_read(&priv->active_tx_urbs) >= MAX_TX_URBS)
1353 netif_stop_queue(netdev);
1355 err = usb_submit_urb(urb, GFP_ATOMIC);
1356 if (unlikely(err)) {
1357 can_free_echo_skb(netdev, context->echo_index);
1359 atomic_dec(&priv->active_tx_urbs);
1360 usb_unanchor_urb(urb);
1362 stats->tx_dropped++;
1364 if (err == -ENODEV)
1365 netif_device_detach(netdev);
1366 else
1367 netdev_warn(netdev, "Failed tx_urb %d\n", err);
1369 goto releasebuf;
1372 usb_free_urb(urb);
1374 return NETDEV_TX_OK;
1376 releasebuf:
1377 kfree(buf);
1378 nobufmem:
1379 usb_free_urb(urb);
1380 return ret;
1383 static const struct net_device_ops kvaser_usb_netdev_ops = {
1384 .ndo_open = kvaser_usb_open,
1385 .ndo_stop = kvaser_usb_close,
1386 .ndo_start_xmit = kvaser_usb_start_xmit,
1389 static const struct can_bittiming_const kvaser_usb_bittiming_const = {
1390 .name = "kvaser_usb",
1391 .tseg1_min = KVASER_USB_TSEG1_MIN,
1392 .tseg1_max = KVASER_USB_TSEG1_MAX,
1393 .tseg2_min = KVASER_USB_TSEG2_MIN,
1394 .tseg2_max = KVASER_USB_TSEG2_MAX,
1395 .sjw_max = KVASER_USB_SJW_MAX,
1396 .brp_min = KVASER_USB_BRP_MIN,
1397 .brp_max = KVASER_USB_BRP_MAX,
1398 .brp_inc = KVASER_USB_BRP_INC,
1401 static int kvaser_usb_set_bittiming(struct net_device *netdev)
1403 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1404 struct can_bittiming *bt = &priv->can.bittiming;
1405 struct kvaser_usb *dev = priv->dev;
1406 struct kvaser_msg *msg;
1407 int rc;
1409 msg = kmalloc(sizeof(*msg), GFP_KERNEL);
1410 if (!msg)
1411 return -ENOMEM;
1413 msg->id = CMD_SET_BUS_PARAMS;
1414 msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_busparams);
1415 msg->u.busparams.channel = priv->channel;
1416 msg->u.busparams.tid = 0xff;
1417 msg->u.busparams.bitrate = cpu_to_le32(bt->bitrate);
1418 msg->u.busparams.sjw = bt->sjw;
1419 msg->u.busparams.tseg1 = bt->prop_seg + bt->phase_seg1;
1420 msg->u.busparams.tseg2 = bt->phase_seg2;
1422 if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
1423 msg->u.busparams.no_samp = 3;
1424 else
1425 msg->u.busparams.no_samp = 1;
1427 rc = kvaser_usb_send_msg(dev, msg);
1429 kfree(msg);
1430 return rc;
1433 static int kvaser_usb_set_mode(struct net_device *netdev,
1434 enum can_mode mode)
1436 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1437 int err;
1439 switch (mode) {
1440 case CAN_MODE_START:
1441 err = kvaser_usb_simple_msg_async(priv, CMD_START_CHIP);
1442 if (err)
1443 return err;
1444 break;
1445 default:
1446 return -EOPNOTSUPP;
1449 return 0;
1452 static int kvaser_usb_get_berr_counter(const struct net_device *netdev,
1453 struct can_berr_counter *bec)
1455 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1457 *bec = priv->bec;
1459 return 0;
1462 static void kvaser_usb_remove_interfaces(struct kvaser_usb *dev)
1464 int i;
1466 for (i = 0; i < dev->nchannels; i++) {
1467 if (!dev->nets[i])
1468 continue;
1470 unregister_netdev(dev->nets[i]->netdev);
1473 kvaser_usb_unlink_all_urbs(dev);
1475 for (i = 0; i < dev->nchannels; i++) {
1476 if (!dev->nets[i])
1477 continue;
1479 free_candev(dev->nets[i]->netdev);
1483 static int kvaser_usb_init_one(struct usb_interface *intf,
1484 const struct usb_device_id *id, int channel)
1486 struct kvaser_usb *dev = usb_get_intfdata(intf);
1487 struct net_device *netdev;
1488 struct kvaser_usb_net_priv *priv;
1489 int i, err;
1491 err = kvaser_usb_send_simple_msg(dev, CMD_RESET_CHIP, channel);
1492 if (err)
1493 return err;
1495 netdev = alloc_candev(sizeof(*priv), MAX_TX_URBS);
1496 if (!netdev) {
1497 dev_err(&intf->dev, "Cannot alloc candev\n");
1498 return -ENOMEM;
1501 priv = netdev_priv(netdev);
1503 init_completion(&priv->start_comp);
1504 init_completion(&priv->stop_comp);
1506 init_usb_anchor(&priv->tx_submitted);
1507 atomic_set(&priv->active_tx_urbs, 0);
1509 for (i = 0; i < ARRAY_SIZE(priv->tx_contexts); i++)
1510 priv->tx_contexts[i].echo_index = MAX_TX_URBS;
1512 priv->dev = dev;
1513 priv->netdev = netdev;
1514 priv->channel = channel;
1516 priv->can.state = CAN_STATE_STOPPED;
1517 priv->can.clock.freq = CAN_USB_CLOCK;
1518 priv->can.bittiming_const = &kvaser_usb_bittiming_const;
1519 priv->can.do_set_bittiming = kvaser_usb_set_bittiming;
1520 priv->can.do_set_mode = kvaser_usb_set_mode;
1521 if (id->driver_info & KVASER_HAS_TXRX_ERRORS)
1522 priv->can.do_get_berr_counter = kvaser_usb_get_berr_counter;
1523 priv->can.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES;
1524 if (id->driver_info & KVASER_HAS_SILENT_MODE)
1525 priv->can.ctrlmode_supported |= CAN_CTRLMODE_LISTENONLY;
1527 netdev->flags |= IFF_ECHO;
1529 netdev->netdev_ops = &kvaser_usb_netdev_ops;
1531 SET_NETDEV_DEV(netdev, &intf->dev);
1533 dev->nets[channel] = priv;
1535 err = register_candev(netdev);
1536 if (err) {
1537 dev_err(&intf->dev, "Failed to register can device\n");
1538 free_candev(netdev);
1539 dev->nets[channel] = NULL;
1540 return err;
1543 netdev_dbg(netdev, "device registered\n");
1545 return 0;
1548 static int kvaser_usb_get_endpoints(const struct usb_interface *intf,
1549 struct usb_endpoint_descriptor **in,
1550 struct usb_endpoint_descriptor **out)
1552 const struct usb_host_interface *iface_desc;
1553 struct usb_endpoint_descriptor *endpoint;
1554 int i;
1556 iface_desc = &intf->altsetting[0];
1558 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1559 endpoint = &iface_desc->endpoint[i].desc;
1561 if (!*in && usb_endpoint_is_bulk_in(endpoint))
1562 *in = endpoint;
1564 if (!*out && usb_endpoint_is_bulk_out(endpoint))
1565 *out = endpoint;
1567 /* use first bulk endpoint for in and out */
1568 if (*in && *out)
1569 return 0;
1572 return -ENODEV;
1575 static int kvaser_usb_probe(struct usb_interface *intf,
1576 const struct usb_device_id *id)
1578 struct kvaser_usb *dev;
1579 int err = -ENOMEM;
1580 int i, retry = 3;
1582 dev = devm_kzalloc(&intf->dev, sizeof(*dev), GFP_KERNEL);
1583 if (!dev)
1584 return -ENOMEM;
1586 err = kvaser_usb_get_endpoints(intf, &dev->bulk_in, &dev->bulk_out);
1587 if (err) {
1588 dev_err(&intf->dev, "Cannot get usb endpoint(s)");
1589 return err;
1592 dev->udev = interface_to_usbdev(intf);
1594 init_usb_anchor(&dev->rx_submitted);
1596 usb_set_intfdata(intf, dev);
1598 /* On some x86 laptops, plugging a Kvaser device again after
1599 * an unplug makes the firmware always ignore the very first
1600 * command. For such a case, provide some room for retries
1601 * instead of completely exiting the driver.
1603 do {
1604 err = kvaser_usb_get_software_info(dev);
1605 } while (--retry && err == -ETIMEDOUT);
1607 if (err) {
1608 dev_err(&intf->dev,
1609 "Cannot get software infos, error %d\n", err);
1610 return err;
1613 err = kvaser_usb_get_card_info(dev);
1614 if (err) {
1615 dev_err(&intf->dev,
1616 "Cannot get card infos, error %d\n", err);
1617 return err;
1620 dev_dbg(&intf->dev, "Firmware version: %d.%d.%d\n",
1621 ((dev->fw_version >> 24) & 0xff),
1622 ((dev->fw_version >> 16) & 0xff),
1623 (dev->fw_version & 0xffff));
1625 for (i = 0; i < dev->nchannels; i++) {
1626 err = kvaser_usb_init_one(intf, id, i);
1627 if (err) {
1628 kvaser_usb_remove_interfaces(dev);
1629 return err;
1633 return 0;
1636 static void kvaser_usb_disconnect(struct usb_interface *intf)
1638 struct kvaser_usb *dev = usb_get_intfdata(intf);
1640 usb_set_intfdata(intf, NULL);
1642 if (!dev)
1643 return;
1645 kvaser_usb_remove_interfaces(dev);
1648 static struct usb_driver kvaser_usb_driver = {
1649 .name = "kvaser_usb",
1650 .probe = kvaser_usb_probe,
1651 .disconnect = kvaser_usb_disconnect,
1652 .id_table = kvaser_usb_table,
1655 module_usb_driver(kvaser_usb_driver);
1657 MODULE_AUTHOR("Olivier Sobrie <olivier@sobrie.be>");
1658 MODULE_DESCRIPTION("CAN driver for Kvaser CAN/USB devices");
1659 MODULE_LICENSE("GPL v2");