ARM: dts: add 'dr_mode' property to hsotg devices for exynos boards
[linux/fpc-iii.git] / drivers / net / can / usb / kvaser_usb.c
blob541fb7a05625aaf889089704ec155956629e77c8
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/completion.h>
16 #include <linux/module.h>
17 #include <linux/netdevice.h>
18 #include <linux/usb.h>
20 #include <linux/can.h>
21 #include <linux/can/dev.h>
22 #include <linux/can/error.h>
24 #define MAX_TX_URBS 16
25 #define MAX_RX_URBS 4
26 #define START_TIMEOUT 1000 /* msecs */
27 #define STOP_TIMEOUT 1000 /* msecs */
28 #define USB_SEND_TIMEOUT 1000 /* msecs */
29 #define USB_RECV_TIMEOUT 1000 /* msecs */
30 #define RX_BUFFER_SIZE 3072
31 #define CAN_USB_CLOCK 8000000
32 #define MAX_NET_DEVICES 3
34 /* Kvaser USB devices */
35 #define KVASER_VENDOR_ID 0x0bfd
36 #define USB_LEAF_DEVEL_PRODUCT_ID 10
37 #define USB_LEAF_LITE_PRODUCT_ID 11
38 #define USB_LEAF_PRO_PRODUCT_ID 12
39 #define USB_LEAF_SPRO_PRODUCT_ID 14
40 #define USB_LEAF_PRO_LS_PRODUCT_ID 15
41 #define USB_LEAF_PRO_SWC_PRODUCT_ID 16
42 #define USB_LEAF_PRO_LIN_PRODUCT_ID 17
43 #define USB_LEAF_SPRO_LS_PRODUCT_ID 18
44 #define USB_LEAF_SPRO_SWC_PRODUCT_ID 19
45 #define USB_MEMO2_DEVEL_PRODUCT_ID 22
46 #define USB_MEMO2_HSHS_PRODUCT_ID 23
47 #define USB_UPRO_HSHS_PRODUCT_ID 24
48 #define USB_LEAF_LITE_GI_PRODUCT_ID 25
49 #define USB_LEAF_PRO_OBDII_PRODUCT_ID 26
50 #define USB_MEMO2_HSLS_PRODUCT_ID 27
51 #define USB_LEAF_LITE_CH_PRODUCT_ID 28
52 #define USB_BLACKBIRD_SPRO_PRODUCT_ID 29
53 #define USB_OEM_MERCURY_PRODUCT_ID 34
54 #define USB_OEM_LEAF_PRODUCT_ID 35
55 #define USB_CAN_R_PRODUCT_ID 39
56 #define USB_LEAF_LITE_V2_PRODUCT_ID 288
57 #define USB_MINI_PCIE_HS_PRODUCT_ID 289
59 /* USB devices features */
60 #define KVASER_HAS_SILENT_MODE BIT(0)
61 #define KVASER_HAS_TXRX_ERRORS BIT(1)
63 /* Message header size */
64 #define MSG_HEADER_LEN 2
66 /* Can message flags */
67 #define MSG_FLAG_ERROR_FRAME BIT(0)
68 #define MSG_FLAG_OVERRUN BIT(1)
69 #define MSG_FLAG_NERR BIT(2)
70 #define MSG_FLAG_WAKEUP BIT(3)
71 #define MSG_FLAG_REMOTE_FRAME BIT(4)
72 #define MSG_FLAG_RESERVED BIT(5)
73 #define MSG_FLAG_TX_ACK BIT(6)
74 #define MSG_FLAG_TX_REQUEST BIT(7)
76 /* Can states */
77 #define M16C_STATE_BUS_RESET BIT(0)
78 #define M16C_STATE_BUS_ERROR BIT(4)
79 #define M16C_STATE_BUS_PASSIVE BIT(5)
80 #define M16C_STATE_BUS_OFF BIT(6)
82 /* Can msg ids */
83 #define CMD_RX_STD_MESSAGE 12
84 #define CMD_TX_STD_MESSAGE 13
85 #define CMD_RX_EXT_MESSAGE 14
86 #define CMD_TX_EXT_MESSAGE 15
87 #define CMD_SET_BUS_PARAMS 16
88 #define CMD_GET_BUS_PARAMS 17
89 #define CMD_GET_BUS_PARAMS_REPLY 18
90 #define CMD_GET_CHIP_STATE 19
91 #define CMD_CHIP_STATE_EVENT 20
92 #define CMD_SET_CTRL_MODE 21
93 #define CMD_GET_CTRL_MODE 22
94 #define CMD_GET_CTRL_MODE_REPLY 23
95 #define CMD_RESET_CHIP 24
96 #define CMD_RESET_CARD 25
97 #define CMD_START_CHIP 26
98 #define CMD_START_CHIP_REPLY 27
99 #define CMD_STOP_CHIP 28
100 #define CMD_STOP_CHIP_REPLY 29
101 #define CMD_GET_CARD_INFO2 32
102 #define CMD_GET_CARD_INFO 34
103 #define CMD_GET_CARD_INFO_REPLY 35
104 #define CMD_GET_SOFTWARE_INFO 38
105 #define CMD_GET_SOFTWARE_INFO_REPLY 39
106 #define CMD_ERROR_EVENT 45
107 #define CMD_FLUSH_QUEUE 48
108 #define CMD_RESET_ERROR_COUNTER 49
109 #define CMD_TX_ACKNOWLEDGE 50
110 #define CMD_CAN_ERROR_EVENT 51
111 #define CMD_USB_THROTTLE 77
112 #define CMD_LOG_MESSAGE 106
114 /* error factors */
115 #define M16C_EF_ACKE BIT(0)
116 #define M16C_EF_CRCE BIT(1)
117 #define M16C_EF_FORME BIT(2)
118 #define M16C_EF_STFE BIT(3)
119 #define M16C_EF_BITE0 BIT(4)
120 #define M16C_EF_BITE1 BIT(5)
121 #define M16C_EF_RCVE BIT(6)
122 #define M16C_EF_TRE BIT(7)
124 /* bittiming parameters */
125 #define KVASER_USB_TSEG1_MIN 1
126 #define KVASER_USB_TSEG1_MAX 16
127 #define KVASER_USB_TSEG2_MIN 1
128 #define KVASER_USB_TSEG2_MAX 8
129 #define KVASER_USB_SJW_MAX 4
130 #define KVASER_USB_BRP_MIN 1
131 #define KVASER_USB_BRP_MAX 64
132 #define KVASER_USB_BRP_INC 1
134 /* ctrl modes */
135 #define KVASER_CTRL_MODE_NORMAL 1
136 #define KVASER_CTRL_MODE_SILENT 2
137 #define KVASER_CTRL_MODE_SELFRECEPTION 3
138 #define KVASER_CTRL_MODE_OFF 4
140 /* log message */
141 #define KVASER_EXTENDED_FRAME BIT(31)
143 struct kvaser_msg_simple {
144 u8 tid;
145 u8 channel;
146 } __packed;
148 struct kvaser_msg_cardinfo {
149 u8 tid;
150 u8 nchannels;
151 __le32 serial_number;
152 __le32 padding;
153 __le32 clock_resolution;
154 __le32 mfgdate;
155 u8 ean[8];
156 u8 hw_revision;
157 u8 usb_hs_mode;
158 __le16 padding2;
159 } __packed;
161 struct kvaser_msg_cardinfo2 {
162 u8 tid;
163 u8 channel;
164 u8 pcb_id[24];
165 __le32 oem_unlock_code;
166 } __packed;
168 struct kvaser_msg_softinfo {
169 u8 tid;
170 u8 channel;
171 __le32 sw_options;
172 __le32 fw_version;
173 __le16 max_outstanding_tx;
174 __le16 padding[9];
175 } __packed;
177 struct kvaser_msg_busparams {
178 u8 tid;
179 u8 channel;
180 __le32 bitrate;
181 u8 tseg1;
182 u8 tseg2;
183 u8 sjw;
184 u8 no_samp;
185 } __packed;
187 struct kvaser_msg_tx_can {
188 u8 channel;
189 u8 tid;
190 u8 msg[14];
191 u8 padding;
192 u8 flags;
193 } __packed;
195 struct kvaser_msg_rx_can {
196 u8 channel;
197 u8 flag;
198 __le16 time[3];
199 u8 msg[14];
200 } __packed;
202 struct kvaser_msg_chip_state_event {
203 u8 tid;
204 u8 channel;
205 __le16 time[3];
206 u8 tx_errors_count;
207 u8 rx_errors_count;
208 u8 status;
209 u8 padding[3];
210 } __packed;
212 struct kvaser_msg_tx_acknowledge {
213 u8 channel;
214 u8 tid;
215 __le16 time[3];
216 u8 flags;
217 u8 time_offset;
218 } __packed;
220 struct kvaser_msg_error_event {
221 u8 tid;
222 u8 flags;
223 __le16 time[3];
224 u8 channel;
225 u8 padding;
226 u8 tx_errors_count;
227 u8 rx_errors_count;
228 u8 status;
229 u8 error_factor;
230 } __packed;
232 struct kvaser_msg_ctrl_mode {
233 u8 tid;
234 u8 channel;
235 u8 ctrl_mode;
236 u8 padding[3];
237 } __packed;
239 struct kvaser_msg_flush_queue {
240 u8 tid;
241 u8 channel;
242 u8 flags;
243 u8 padding[3];
244 } __packed;
246 struct kvaser_msg_log_message {
247 u8 channel;
248 u8 flags;
249 __le16 time[3];
250 u8 dlc;
251 u8 time_offset;
252 __le32 id;
253 u8 data[8];
254 } __packed;
256 struct kvaser_msg {
257 u8 len;
258 u8 id;
259 union {
260 struct kvaser_msg_simple simple;
261 struct kvaser_msg_cardinfo cardinfo;
262 struct kvaser_msg_cardinfo2 cardinfo2;
263 struct kvaser_msg_softinfo softinfo;
264 struct kvaser_msg_busparams busparams;
265 struct kvaser_msg_tx_can tx_can;
266 struct kvaser_msg_rx_can rx_can;
267 struct kvaser_msg_chip_state_event chip_state_event;
268 struct kvaser_msg_tx_acknowledge tx_acknowledge;
269 struct kvaser_msg_error_event error_event;
270 struct kvaser_msg_ctrl_mode ctrl_mode;
271 struct kvaser_msg_flush_queue flush_queue;
272 struct kvaser_msg_log_message log_message;
273 } u;
274 } __packed;
276 struct kvaser_usb_tx_urb_context {
277 struct kvaser_usb_net_priv *priv;
278 u32 echo_index;
279 int dlc;
282 struct kvaser_usb {
283 struct usb_device *udev;
284 struct kvaser_usb_net_priv *nets[MAX_NET_DEVICES];
286 struct usb_endpoint_descriptor *bulk_in, *bulk_out;
287 struct usb_anchor rx_submitted;
289 u32 fw_version;
290 unsigned int nchannels;
292 bool rxinitdone;
293 void *rxbuf[MAX_RX_URBS];
294 dma_addr_t rxbuf_dma[MAX_RX_URBS];
297 struct kvaser_usb_net_priv {
298 struct can_priv can;
300 atomic_t active_tx_urbs;
301 struct usb_anchor tx_submitted;
302 struct kvaser_usb_tx_urb_context tx_contexts[MAX_TX_URBS];
304 struct completion start_comp, stop_comp;
306 struct kvaser_usb *dev;
307 struct net_device *netdev;
308 int channel;
310 struct can_berr_counter bec;
313 static const struct usb_device_id kvaser_usb_table[] = {
314 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_DEVEL_PRODUCT_ID) },
315 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_PRODUCT_ID) },
316 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_PRODUCT_ID),
317 .driver_info = KVASER_HAS_TXRX_ERRORS |
318 KVASER_HAS_SILENT_MODE },
319 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_SPRO_PRODUCT_ID),
320 .driver_info = KVASER_HAS_TXRX_ERRORS |
321 KVASER_HAS_SILENT_MODE },
322 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_LS_PRODUCT_ID),
323 .driver_info = KVASER_HAS_TXRX_ERRORS |
324 KVASER_HAS_SILENT_MODE },
325 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_SWC_PRODUCT_ID),
326 .driver_info = KVASER_HAS_TXRX_ERRORS |
327 KVASER_HAS_SILENT_MODE },
328 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_LIN_PRODUCT_ID),
329 .driver_info = KVASER_HAS_TXRX_ERRORS |
330 KVASER_HAS_SILENT_MODE },
331 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_SPRO_LS_PRODUCT_ID),
332 .driver_info = KVASER_HAS_TXRX_ERRORS |
333 KVASER_HAS_SILENT_MODE },
334 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_SPRO_SWC_PRODUCT_ID),
335 .driver_info = KVASER_HAS_TXRX_ERRORS |
336 KVASER_HAS_SILENT_MODE },
337 { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO2_DEVEL_PRODUCT_ID),
338 .driver_info = KVASER_HAS_TXRX_ERRORS |
339 KVASER_HAS_SILENT_MODE },
340 { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO2_HSHS_PRODUCT_ID),
341 .driver_info = KVASER_HAS_TXRX_ERRORS |
342 KVASER_HAS_SILENT_MODE },
343 { USB_DEVICE(KVASER_VENDOR_ID, USB_UPRO_HSHS_PRODUCT_ID),
344 .driver_info = KVASER_HAS_TXRX_ERRORS },
345 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_GI_PRODUCT_ID) },
346 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_OBDII_PRODUCT_ID),
347 .driver_info = KVASER_HAS_TXRX_ERRORS |
348 KVASER_HAS_SILENT_MODE },
349 { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO2_HSLS_PRODUCT_ID),
350 .driver_info = KVASER_HAS_TXRX_ERRORS },
351 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_CH_PRODUCT_ID),
352 .driver_info = KVASER_HAS_TXRX_ERRORS },
353 { USB_DEVICE(KVASER_VENDOR_ID, USB_BLACKBIRD_SPRO_PRODUCT_ID),
354 .driver_info = KVASER_HAS_TXRX_ERRORS },
355 { USB_DEVICE(KVASER_VENDOR_ID, USB_OEM_MERCURY_PRODUCT_ID),
356 .driver_info = KVASER_HAS_TXRX_ERRORS },
357 { USB_DEVICE(KVASER_VENDOR_ID, USB_OEM_LEAF_PRODUCT_ID),
358 .driver_info = KVASER_HAS_TXRX_ERRORS },
359 { USB_DEVICE(KVASER_VENDOR_ID, USB_CAN_R_PRODUCT_ID),
360 .driver_info = KVASER_HAS_TXRX_ERRORS },
361 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_V2_PRODUCT_ID) },
362 { USB_DEVICE(KVASER_VENDOR_ID, USB_MINI_PCIE_HS_PRODUCT_ID) },
365 MODULE_DEVICE_TABLE(usb, kvaser_usb_table);
367 static inline int kvaser_usb_send_msg(const struct kvaser_usb *dev,
368 struct kvaser_msg *msg)
370 int actual_len;
372 return usb_bulk_msg(dev->udev,
373 usb_sndbulkpipe(dev->udev,
374 dev->bulk_out->bEndpointAddress),
375 msg, msg->len, &actual_len,
376 USB_SEND_TIMEOUT);
379 static int kvaser_usb_wait_msg(const struct kvaser_usb *dev, u8 id,
380 struct kvaser_msg *msg)
382 struct kvaser_msg *tmp;
383 void *buf;
384 int actual_len;
385 int err;
386 int pos;
387 unsigned long to = jiffies + msecs_to_jiffies(USB_RECV_TIMEOUT);
389 buf = kzalloc(RX_BUFFER_SIZE, GFP_KERNEL);
390 if (!buf)
391 return -ENOMEM;
393 do {
394 err = usb_bulk_msg(dev->udev,
395 usb_rcvbulkpipe(dev->udev,
396 dev->bulk_in->bEndpointAddress),
397 buf, RX_BUFFER_SIZE, &actual_len,
398 USB_RECV_TIMEOUT);
399 if (err < 0)
400 goto end;
402 pos = 0;
403 while (pos <= actual_len - MSG_HEADER_LEN) {
404 tmp = buf + pos;
406 if (!tmp->len)
407 break;
409 if (pos + tmp->len > actual_len) {
410 dev_err(dev->udev->dev.parent,
411 "Format error\n");
412 break;
415 if (tmp->id == id) {
416 memcpy(msg, tmp, tmp->len);
417 goto end;
420 pos += tmp->len;
422 } while (time_before(jiffies, to));
424 err = -EINVAL;
426 end:
427 kfree(buf);
429 return err;
432 static int kvaser_usb_send_simple_msg(const struct kvaser_usb *dev,
433 u8 msg_id, int channel)
435 struct kvaser_msg *msg;
436 int rc;
438 msg = kmalloc(sizeof(*msg), GFP_KERNEL);
439 if (!msg)
440 return -ENOMEM;
442 msg->id = msg_id;
443 msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_simple);
444 msg->u.simple.channel = channel;
445 msg->u.simple.tid = 0xff;
447 rc = kvaser_usb_send_msg(dev, msg);
449 kfree(msg);
450 return rc;
453 static int kvaser_usb_get_software_info(struct kvaser_usb *dev)
455 struct kvaser_msg msg;
456 int err;
458 err = kvaser_usb_send_simple_msg(dev, CMD_GET_SOFTWARE_INFO, 0);
459 if (err)
460 return err;
462 err = kvaser_usb_wait_msg(dev, CMD_GET_SOFTWARE_INFO_REPLY, &msg);
463 if (err)
464 return err;
466 dev->fw_version = le32_to_cpu(msg.u.softinfo.fw_version);
468 return 0;
471 static int kvaser_usb_get_card_info(struct kvaser_usb *dev)
473 struct kvaser_msg msg;
474 int err;
476 err = kvaser_usb_send_simple_msg(dev, CMD_GET_CARD_INFO, 0);
477 if (err)
478 return err;
480 err = kvaser_usb_wait_msg(dev, CMD_GET_CARD_INFO_REPLY, &msg);
481 if (err)
482 return err;
484 dev->nchannels = msg.u.cardinfo.nchannels;
485 if (dev->nchannels > MAX_NET_DEVICES)
486 return -EINVAL;
488 return 0;
491 static void kvaser_usb_tx_acknowledge(const struct kvaser_usb *dev,
492 const struct kvaser_msg *msg)
494 struct net_device_stats *stats;
495 struct kvaser_usb_tx_urb_context *context;
496 struct kvaser_usb_net_priv *priv;
497 struct sk_buff *skb;
498 struct can_frame *cf;
499 u8 channel = msg->u.tx_acknowledge.channel;
500 u8 tid = msg->u.tx_acknowledge.tid;
502 if (channel >= dev->nchannels) {
503 dev_err(dev->udev->dev.parent,
504 "Invalid channel number (%d)\n", channel);
505 return;
508 priv = dev->nets[channel];
510 if (!netif_device_present(priv->netdev))
511 return;
513 stats = &priv->netdev->stats;
515 context = &priv->tx_contexts[tid % MAX_TX_URBS];
517 /* Sometimes the state change doesn't come after a bus-off event */
518 if (priv->can.restart_ms &&
519 (priv->can.state >= CAN_STATE_BUS_OFF)) {
520 skb = alloc_can_err_skb(priv->netdev, &cf);
521 if (skb) {
522 cf->can_id |= CAN_ERR_RESTARTED;
523 netif_rx(skb);
525 stats->rx_packets++;
526 stats->rx_bytes += cf->can_dlc;
527 } else {
528 netdev_err(priv->netdev,
529 "No memory left for err_skb\n");
532 priv->can.can_stats.restarts++;
533 netif_carrier_on(priv->netdev);
535 priv->can.state = CAN_STATE_ERROR_ACTIVE;
538 stats->tx_packets++;
539 stats->tx_bytes += context->dlc;
540 can_get_echo_skb(priv->netdev, context->echo_index);
542 context->echo_index = MAX_TX_URBS;
543 atomic_dec(&priv->active_tx_urbs);
545 netif_wake_queue(priv->netdev);
548 static void kvaser_usb_simple_msg_callback(struct urb *urb)
550 struct net_device *netdev = urb->context;
552 kfree(urb->transfer_buffer);
554 if (urb->status)
555 netdev_warn(netdev, "urb status received: %d\n",
556 urb->status);
559 static int kvaser_usb_simple_msg_async(struct kvaser_usb_net_priv *priv,
560 u8 msg_id)
562 struct kvaser_usb *dev = priv->dev;
563 struct net_device *netdev = priv->netdev;
564 struct kvaser_msg *msg;
565 struct urb *urb;
566 void *buf;
567 int err;
569 urb = usb_alloc_urb(0, GFP_ATOMIC);
570 if (!urb) {
571 netdev_err(netdev, "No memory left for URBs\n");
572 return -ENOMEM;
575 buf = kmalloc(sizeof(struct kvaser_msg), GFP_ATOMIC);
576 if (!buf) {
577 usb_free_urb(urb);
578 return -ENOMEM;
581 msg = (struct kvaser_msg *)buf;
582 msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_simple);
583 msg->id = msg_id;
584 msg->u.simple.channel = priv->channel;
586 usb_fill_bulk_urb(urb, dev->udev,
587 usb_sndbulkpipe(dev->udev,
588 dev->bulk_out->bEndpointAddress),
589 buf, msg->len,
590 kvaser_usb_simple_msg_callback, priv);
591 usb_anchor_urb(urb, &priv->tx_submitted);
593 err = usb_submit_urb(urb, GFP_ATOMIC);
594 if (err) {
595 netdev_err(netdev, "Error transmitting URB\n");
596 usb_unanchor_urb(urb);
597 usb_free_urb(urb);
598 kfree(buf);
599 return err;
602 usb_free_urb(urb);
604 return 0;
607 static void kvaser_usb_unlink_tx_urbs(struct kvaser_usb_net_priv *priv)
609 int i;
611 usb_kill_anchored_urbs(&priv->tx_submitted);
612 atomic_set(&priv->active_tx_urbs, 0);
614 for (i = 0; i < MAX_TX_URBS; i++)
615 priv->tx_contexts[i].echo_index = MAX_TX_URBS;
618 static void kvaser_usb_rx_error(const struct kvaser_usb *dev,
619 const struct kvaser_msg *msg)
621 struct can_frame *cf;
622 struct sk_buff *skb;
623 struct net_device_stats *stats;
624 struct kvaser_usb_net_priv *priv;
625 unsigned int new_state;
626 u8 channel, status, txerr, rxerr, error_factor;
628 switch (msg->id) {
629 case CMD_CAN_ERROR_EVENT:
630 channel = msg->u.error_event.channel;
631 status = msg->u.error_event.status;
632 txerr = msg->u.error_event.tx_errors_count;
633 rxerr = msg->u.error_event.rx_errors_count;
634 error_factor = msg->u.error_event.error_factor;
635 break;
636 case CMD_LOG_MESSAGE:
637 channel = msg->u.log_message.channel;
638 status = msg->u.log_message.data[0];
639 txerr = msg->u.log_message.data[2];
640 rxerr = msg->u.log_message.data[3];
641 error_factor = msg->u.log_message.data[1];
642 break;
643 case CMD_CHIP_STATE_EVENT:
644 channel = msg->u.chip_state_event.channel;
645 status = msg->u.chip_state_event.status;
646 txerr = msg->u.chip_state_event.tx_errors_count;
647 rxerr = msg->u.chip_state_event.rx_errors_count;
648 error_factor = 0;
649 break;
650 default:
651 dev_err(dev->udev->dev.parent, "Invalid msg id (%d)\n",
652 msg->id);
653 return;
656 if (channel >= dev->nchannels) {
657 dev_err(dev->udev->dev.parent,
658 "Invalid channel number (%d)\n", channel);
659 return;
662 priv = dev->nets[channel];
663 stats = &priv->netdev->stats;
665 if (status & M16C_STATE_BUS_RESET) {
666 kvaser_usb_unlink_tx_urbs(priv);
667 return;
670 skb = alloc_can_err_skb(priv->netdev, &cf);
671 if (!skb) {
672 stats->rx_dropped++;
673 return;
676 new_state = priv->can.state;
678 netdev_dbg(priv->netdev, "Error status: 0x%02x\n", status);
680 if (status & M16C_STATE_BUS_OFF) {
681 cf->can_id |= CAN_ERR_BUSOFF;
683 priv->can.can_stats.bus_off++;
684 if (!priv->can.restart_ms)
685 kvaser_usb_simple_msg_async(priv, CMD_STOP_CHIP);
687 netif_carrier_off(priv->netdev);
689 new_state = CAN_STATE_BUS_OFF;
690 } else if (status & M16C_STATE_BUS_PASSIVE) {
691 if (priv->can.state != CAN_STATE_ERROR_PASSIVE) {
692 cf->can_id |= CAN_ERR_CRTL;
694 if (txerr || rxerr)
695 cf->data[1] = (txerr > rxerr)
696 ? CAN_ERR_CRTL_TX_PASSIVE
697 : CAN_ERR_CRTL_RX_PASSIVE;
698 else
699 cf->data[1] = CAN_ERR_CRTL_TX_PASSIVE |
700 CAN_ERR_CRTL_RX_PASSIVE;
702 priv->can.can_stats.error_passive++;
705 new_state = CAN_STATE_ERROR_PASSIVE;
708 if (status == M16C_STATE_BUS_ERROR) {
709 if ((priv->can.state < CAN_STATE_ERROR_WARNING) &&
710 ((txerr >= 96) || (rxerr >= 96))) {
711 cf->can_id |= CAN_ERR_CRTL;
712 cf->data[1] = (txerr > rxerr)
713 ? CAN_ERR_CRTL_TX_WARNING
714 : CAN_ERR_CRTL_RX_WARNING;
716 priv->can.can_stats.error_warning++;
717 new_state = CAN_STATE_ERROR_WARNING;
718 } else if (priv->can.state > CAN_STATE_ERROR_ACTIVE) {
719 cf->can_id |= CAN_ERR_PROT;
720 cf->data[2] = CAN_ERR_PROT_ACTIVE;
722 new_state = CAN_STATE_ERROR_ACTIVE;
726 if (!status) {
727 cf->can_id |= CAN_ERR_PROT;
728 cf->data[2] = CAN_ERR_PROT_ACTIVE;
730 new_state = CAN_STATE_ERROR_ACTIVE;
733 if (priv->can.restart_ms &&
734 (priv->can.state >= CAN_STATE_BUS_OFF) &&
735 (new_state < CAN_STATE_BUS_OFF)) {
736 cf->can_id |= CAN_ERR_RESTARTED;
737 netif_carrier_on(priv->netdev);
739 priv->can.can_stats.restarts++;
742 if (error_factor) {
743 priv->can.can_stats.bus_error++;
744 stats->rx_errors++;
746 cf->can_id |= CAN_ERR_BUSERROR | CAN_ERR_PROT;
748 if (error_factor & M16C_EF_ACKE)
749 cf->data[3] |= (CAN_ERR_PROT_LOC_ACK);
750 if (error_factor & M16C_EF_CRCE)
751 cf->data[3] |= (CAN_ERR_PROT_LOC_CRC_SEQ |
752 CAN_ERR_PROT_LOC_CRC_DEL);
753 if (error_factor & M16C_EF_FORME)
754 cf->data[2] |= CAN_ERR_PROT_FORM;
755 if (error_factor & M16C_EF_STFE)
756 cf->data[2] |= CAN_ERR_PROT_STUFF;
757 if (error_factor & M16C_EF_BITE0)
758 cf->data[2] |= CAN_ERR_PROT_BIT0;
759 if (error_factor & M16C_EF_BITE1)
760 cf->data[2] |= CAN_ERR_PROT_BIT1;
761 if (error_factor & M16C_EF_TRE)
762 cf->data[2] |= CAN_ERR_PROT_TX;
765 cf->data[6] = txerr;
766 cf->data[7] = rxerr;
768 priv->bec.txerr = txerr;
769 priv->bec.rxerr = rxerr;
771 priv->can.state = new_state;
773 netif_rx(skb);
775 stats->rx_packets++;
776 stats->rx_bytes += cf->can_dlc;
779 static void kvaser_usb_rx_can_err(const struct kvaser_usb_net_priv *priv,
780 const struct kvaser_msg *msg)
782 struct can_frame *cf;
783 struct sk_buff *skb;
784 struct net_device_stats *stats = &priv->netdev->stats;
786 if (msg->u.rx_can.flag & (MSG_FLAG_ERROR_FRAME |
787 MSG_FLAG_NERR)) {
788 netdev_err(priv->netdev, "Unknow error (flags: 0x%02x)\n",
789 msg->u.rx_can.flag);
791 stats->rx_errors++;
792 return;
795 if (msg->u.rx_can.flag & MSG_FLAG_OVERRUN) {
796 skb = alloc_can_err_skb(priv->netdev, &cf);
797 if (!skb) {
798 stats->rx_dropped++;
799 return;
802 cf->can_id |= CAN_ERR_CRTL;
803 cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
805 stats->rx_over_errors++;
806 stats->rx_errors++;
808 netif_rx(skb);
810 stats->rx_packets++;
811 stats->rx_bytes += cf->can_dlc;
815 static void kvaser_usb_rx_can_msg(const struct kvaser_usb *dev,
816 const struct kvaser_msg *msg)
818 struct kvaser_usb_net_priv *priv;
819 struct can_frame *cf;
820 struct sk_buff *skb;
821 struct net_device_stats *stats;
822 u8 channel = msg->u.rx_can.channel;
824 if (channel >= dev->nchannels) {
825 dev_err(dev->udev->dev.parent,
826 "Invalid channel number (%d)\n", channel);
827 return;
830 priv = dev->nets[channel];
831 stats = &priv->netdev->stats;
833 if ((msg->u.rx_can.flag & MSG_FLAG_ERROR_FRAME) &&
834 (msg->id == CMD_LOG_MESSAGE)) {
835 kvaser_usb_rx_error(dev, msg);
836 return;
837 } else if (msg->u.rx_can.flag & (MSG_FLAG_ERROR_FRAME |
838 MSG_FLAG_NERR |
839 MSG_FLAG_OVERRUN)) {
840 kvaser_usb_rx_can_err(priv, msg);
841 return;
842 } else if (msg->u.rx_can.flag & ~MSG_FLAG_REMOTE_FRAME) {
843 netdev_warn(priv->netdev,
844 "Unhandled frame (flags: 0x%02x)",
845 msg->u.rx_can.flag);
846 return;
849 skb = alloc_can_skb(priv->netdev, &cf);
850 if (!skb) {
851 stats->tx_dropped++;
852 return;
855 if (msg->id == CMD_LOG_MESSAGE) {
856 cf->can_id = le32_to_cpu(msg->u.log_message.id);
857 if (cf->can_id & KVASER_EXTENDED_FRAME)
858 cf->can_id &= CAN_EFF_MASK | CAN_EFF_FLAG;
859 else
860 cf->can_id &= CAN_SFF_MASK;
862 cf->can_dlc = get_can_dlc(msg->u.log_message.dlc);
864 if (msg->u.log_message.flags & MSG_FLAG_REMOTE_FRAME)
865 cf->can_id |= CAN_RTR_FLAG;
866 else
867 memcpy(cf->data, &msg->u.log_message.data,
868 cf->can_dlc);
869 } else {
870 cf->can_id = ((msg->u.rx_can.msg[0] & 0x1f) << 6) |
871 (msg->u.rx_can.msg[1] & 0x3f);
873 if (msg->id == CMD_RX_EXT_MESSAGE) {
874 cf->can_id <<= 18;
875 cf->can_id |= ((msg->u.rx_can.msg[2] & 0x0f) << 14) |
876 ((msg->u.rx_can.msg[3] & 0xff) << 6) |
877 (msg->u.rx_can.msg[4] & 0x3f);
878 cf->can_id |= CAN_EFF_FLAG;
881 cf->can_dlc = get_can_dlc(msg->u.rx_can.msg[5]);
883 if (msg->u.rx_can.flag & MSG_FLAG_REMOTE_FRAME)
884 cf->can_id |= CAN_RTR_FLAG;
885 else
886 memcpy(cf->data, &msg->u.rx_can.msg[6],
887 cf->can_dlc);
890 netif_rx(skb);
892 stats->rx_packets++;
893 stats->rx_bytes += cf->can_dlc;
896 static void kvaser_usb_start_chip_reply(const struct kvaser_usb *dev,
897 const struct kvaser_msg *msg)
899 struct kvaser_usb_net_priv *priv;
900 u8 channel = msg->u.simple.channel;
902 if (channel >= dev->nchannels) {
903 dev_err(dev->udev->dev.parent,
904 "Invalid channel number (%d)\n", channel);
905 return;
908 priv = dev->nets[channel];
910 if (completion_done(&priv->start_comp) &&
911 netif_queue_stopped(priv->netdev)) {
912 netif_wake_queue(priv->netdev);
913 } else {
914 netif_start_queue(priv->netdev);
915 complete(&priv->start_comp);
919 static void kvaser_usb_stop_chip_reply(const struct kvaser_usb *dev,
920 const struct kvaser_msg *msg)
922 struct kvaser_usb_net_priv *priv;
923 u8 channel = msg->u.simple.channel;
925 if (channel >= dev->nchannels) {
926 dev_err(dev->udev->dev.parent,
927 "Invalid channel number (%d)\n", channel);
928 return;
931 priv = dev->nets[channel];
933 complete(&priv->stop_comp);
936 static void kvaser_usb_handle_message(const struct kvaser_usb *dev,
937 const struct kvaser_msg *msg)
939 switch (msg->id) {
940 case CMD_START_CHIP_REPLY:
941 kvaser_usb_start_chip_reply(dev, msg);
942 break;
944 case CMD_STOP_CHIP_REPLY:
945 kvaser_usb_stop_chip_reply(dev, msg);
946 break;
948 case CMD_RX_STD_MESSAGE:
949 case CMD_RX_EXT_MESSAGE:
950 case CMD_LOG_MESSAGE:
951 kvaser_usb_rx_can_msg(dev, msg);
952 break;
954 case CMD_CHIP_STATE_EVENT:
955 case CMD_CAN_ERROR_EVENT:
956 kvaser_usb_rx_error(dev, msg);
957 break;
959 case CMD_TX_ACKNOWLEDGE:
960 kvaser_usb_tx_acknowledge(dev, msg);
961 break;
963 default:
964 dev_warn(dev->udev->dev.parent,
965 "Unhandled message (%d)\n", msg->id);
966 break;
970 static void kvaser_usb_read_bulk_callback(struct urb *urb)
972 struct kvaser_usb *dev = urb->context;
973 struct kvaser_msg *msg;
974 int pos = 0;
975 int err, i;
977 switch (urb->status) {
978 case 0:
979 break;
980 case -ENOENT:
981 case -ESHUTDOWN:
982 return;
983 default:
984 dev_info(dev->udev->dev.parent, "Rx URB aborted (%d)\n",
985 urb->status);
986 goto resubmit_urb;
989 while (pos <= urb->actual_length - MSG_HEADER_LEN) {
990 msg = urb->transfer_buffer + pos;
992 if (!msg->len)
993 break;
995 if (pos + msg->len > urb->actual_length) {
996 dev_err(dev->udev->dev.parent, "Format error\n");
997 break;
1000 kvaser_usb_handle_message(dev, msg);
1002 pos += msg->len;
1005 resubmit_urb:
1006 usb_fill_bulk_urb(urb, dev->udev,
1007 usb_rcvbulkpipe(dev->udev,
1008 dev->bulk_in->bEndpointAddress),
1009 urb->transfer_buffer, RX_BUFFER_SIZE,
1010 kvaser_usb_read_bulk_callback, dev);
1012 err = usb_submit_urb(urb, GFP_ATOMIC);
1013 if (err == -ENODEV) {
1014 for (i = 0; i < dev->nchannels; i++) {
1015 if (!dev->nets[i])
1016 continue;
1018 netif_device_detach(dev->nets[i]->netdev);
1020 } else if (err) {
1021 dev_err(dev->udev->dev.parent,
1022 "Failed resubmitting read bulk urb: %d\n", err);
1025 return;
1028 static int kvaser_usb_setup_rx_urbs(struct kvaser_usb *dev)
1030 int i, err = 0;
1032 if (dev->rxinitdone)
1033 return 0;
1035 for (i = 0; i < MAX_RX_URBS; i++) {
1036 struct urb *urb = NULL;
1037 u8 *buf = NULL;
1038 dma_addr_t buf_dma;
1040 urb = usb_alloc_urb(0, GFP_KERNEL);
1041 if (!urb) {
1042 dev_warn(dev->udev->dev.parent,
1043 "No memory left for URBs\n");
1044 err = -ENOMEM;
1045 break;
1048 buf = usb_alloc_coherent(dev->udev, RX_BUFFER_SIZE,
1049 GFP_KERNEL, &buf_dma);
1050 if (!buf) {
1051 dev_warn(dev->udev->dev.parent,
1052 "No memory left for USB buffer\n");
1053 usb_free_urb(urb);
1054 err = -ENOMEM;
1055 break;
1058 usb_fill_bulk_urb(urb, dev->udev,
1059 usb_rcvbulkpipe(dev->udev,
1060 dev->bulk_in->bEndpointAddress),
1061 buf, RX_BUFFER_SIZE,
1062 kvaser_usb_read_bulk_callback,
1063 dev);
1064 urb->transfer_dma = buf_dma;
1065 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1066 usb_anchor_urb(urb, &dev->rx_submitted);
1068 err = usb_submit_urb(urb, GFP_KERNEL);
1069 if (err) {
1070 usb_unanchor_urb(urb);
1071 usb_free_coherent(dev->udev, RX_BUFFER_SIZE, buf,
1072 buf_dma);
1073 usb_free_urb(urb);
1074 break;
1077 dev->rxbuf[i] = buf;
1078 dev->rxbuf_dma[i] = buf_dma;
1080 usb_free_urb(urb);
1083 if (i == 0) {
1084 dev_warn(dev->udev->dev.parent,
1085 "Cannot setup read URBs, error %d\n", err);
1086 return err;
1087 } else if (i < MAX_RX_URBS) {
1088 dev_warn(dev->udev->dev.parent,
1089 "RX performances may be slow\n");
1092 dev->rxinitdone = true;
1094 return 0;
1097 static int kvaser_usb_set_opt_mode(const struct kvaser_usb_net_priv *priv)
1099 struct kvaser_msg *msg;
1100 int rc;
1102 msg = kmalloc(sizeof(*msg), GFP_KERNEL);
1103 if (!msg)
1104 return -ENOMEM;
1106 msg->id = CMD_SET_CTRL_MODE;
1107 msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_ctrl_mode);
1108 msg->u.ctrl_mode.tid = 0xff;
1109 msg->u.ctrl_mode.channel = priv->channel;
1111 if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
1112 msg->u.ctrl_mode.ctrl_mode = KVASER_CTRL_MODE_SILENT;
1113 else
1114 msg->u.ctrl_mode.ctrl_mode = KVASER_CTRL_MODE_NORMAL;
1116 rc = kvaser_usb_send_msg(priv->dev, msg);
1118 kfree(msg);
1119 return rc;
1122 static int kvaser_usb_start_chip(struct kvaser_usb_net_priv *priv)
1124 int err;
1126 init_completion(&priv->start_comp);
1128 err = kvaser_usb_send_simple_msg(priv->dev, CMD_START_CHIP,
1129 priv->channel);
1130 if (err)
1131 return err;
1133 if (!wait_for_completion_timeout(&priv->start_comp,
1134 msecs_to_jiffies(START_TIMEOUT)))
1135 return -ETIMEDOUT;
1137 return 0;
1140 static int kvaser_usb_open(struct net_device *netdev)
1142 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1143 struct kvaser_usb *dev = priv->dev;
1144 int err;
1146 err = open_candev(netdev);
1147 if (err)
1148 return err;
1150 err = kvaser_usb_setup_rx_urbs(dev);
1151 if (err)
1152 goto error;
1154 err = kvaser_usb_set_opt_mode(priv);
1155 if (err)
1156 goto error;
1158 err = kvaser_usb_start_chip(priv);
1159 if (err) {
1160 netdev_warn(netdev, "Cannot start device, error %d\n", err);
1161 goto error;
1164 priv->can.state = CAN_STATE_ERROR_ACTIVE;
1166 return 0;
1168 error:
1169 close_candev(netdev);
1170 return err;
1173 static void kvaser_usb_unlink_all_urbs(struct kvaser_usb *dev)
1175 int i;
1177 usb_kill_anchored_urbs(&dev->rx_submitted);
1179 for (i = 0; i < MAX_RX_URBS; i++)
1180 usb_free_coherent(dev->udev, RX_BUFFER_SIZE,
1181 dev->rxbuf[i],
1182 dev->rxbuf_dma[i]);
1184 for (i = 0; i < MAX_NET_DEVICES; i++) {
1185 struct kvaser_usb_net_priv *priv = dev->nets[i];
1187 if (priv)
1188 kvaser_usb_unlink_tx_urbs(priv);
1192 static int kvaser_usb_stop_chip(struct kvaser_usb_net_priv *priv)
1194 int err;
1196 init_completion(&priv->stop_comp);
1198 err = kvaser_usb_send_simple_msg(priv->dev, CMD_STOP_CHIP,
1199 priv->channel);
1200 if (err)
1201 return err;
1203 if (!wait_for_completion_timeout(&priv->stop_comp,
1204 msecs_to_jiffies(STOP_TIMEOUT)))
1205 return -ETIMEDOUT;
1207 return 0;
1210 static int kvaser_usb_flush_queue(struct kvaser_usb_net_priv *priv)
1212 struct kvaser_msg *msg;
1213 int rc;
1215 msg = kmalloc(sizeof(*msg), GFP_KERNEL);
1216 if (!msg)
1217 return -ENOMEM;
1219 msg->id = CMD_FLUSH_QUEUE;
1220 msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_flush_queue);
1221 msg->u.flush_queue.channel = priv->channel;
1222 msg->u.flush_queue.flags = 0x00;
1224 rc = kvaser_usb_send_msg(priv->dev, msg);
1226 kfree(msg);
1227 return rc;
1230 static int kvaser_usb_close(struct net_device *netdev)
1232 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1233 struct kvaser_usb *dev = priv->dev;
1234 int err;
1236 netif_stop_queue(netdev);
1238 err = kvaser_usb_flush_queue(priv);
1239 if (err)
1240 netdev_warn(netdev, "Cannot flush queue, error %d\n", err);
1242 if (kvaser_usb_send_simple_msg(dev, CMD_RESET_CHIP, priv->channel))
1243 netdev_warn(netdev, "Cannot reset card, error %d\n", err);
1245 err = kvaser_usb_stop_chip(priv);
1246 if (err)
1247 netdev_warn(netdev, "Cannot stop device, error %d\n", err);
1249 priv->can.state = CAN_STATE_STOPPED;
1250 close_candev(priv->netdev);
1252 return 0;
1255 static void kvaser_usb_write_bulk_callback(struct urb *urb)
1257 struct kvaser_usb_tx_urb_context *context = urb->context;
1258 struct kvaser_usb_net_priv *priv;
1259 struct net_device *netdev;
1261 if (WARN_ON(!context))
1262 return;
1264 priv = context->priv;
1265 netdev = priv->netdev;
1267 kfree(urb->transfer_buffer);
1269 if (!netif_device_present(netdev))
1270 return;
1272 if (urb->status)
1273 netdev_info(netdev, "Tx URB aborted (%d)\n", urb->status);
1276 static netdev_tx_t kvaser_usb_start_xmit(struct sk_buff *skb,
1277 struct net_device *netdev)
1279 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1280 struct kvaser_usb *dev = priv->dev;
1281 struct net_device_stats *stats = &netdev->stats;
1282 struct can_frame *cf = (struct can_frame *)skb->data;
1283 struct kvaser_usb_tx_urb_context *context = NULL;
1284 struct urb *urb;
1285 void *buf;
1286 struct kvaser_msg *msg;
1287 int i, err;
1288 int ret = NETDEV_TX_OK;
1290 if (can_dropped_invalid_skb(netdev, skb))
1291 return NETDEV_TX_OK;
1293 urb = usb_alloc_urb(0, GFP_ATOMIC);
1294 if (!urb) {
1295 netdev_err(netdev, "No memory left for URBs\n");
1296 stats->tx_dropped++;
1297 goto nourbmem;
1300 buf = kmalloc(sizeof(struct kvaser_msg), GFP_ATOMIC);
1301 if (!buf) {
1302 stats->tx_dropped++;
1303 goto nobufmem;
1306 msg = buf;
1307 msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_tx_can);
1308 msg->u.tx_can.flags = 0;
1309 msg->u.tx_can.channel = priv->channel;
1311 if (cf->can_id & CAN_EFF_FLAG) {
1312 msg->id = CMD_TX_EXT_MESSAGE;
1313 msg->u.tx_can.msg[0] = (cf->can_id >> 24) & 0x1f;
1314 msg->u.tx_can.msg[1] = (cf->can_id >> 18) & 0x3f;
1315 msg->u.tx_can.msg[2] = (cf->can_id >> 14) & 0x0f;
1316 msg->u.tx_can.msg[3] = (cf->can_id >> 6) & 0xff;
1317 msg->u.tx_can.msg[4] = cf->can_id & 0x3f;
1318 } else {
1319 msg->id = CMD_TX_STD_MESSAGE;
1320 msg->u.tx_can.msg[0] = (cf->can_id >> 6) & 0x1f;
1321 msg->u.tx_can.msg[1] = cf->can_id & 0x3f;
1324 msg->u.tx_can.msg[5] = cf->can_dlc;
1325 memcpy(&msg->u.tx_can.msg[6], cf->data, cf->can_dlc);
1327 if (cf->can_id & CAN_RTR_FLAG)
1328 msg->u.tx_can.flags |= MSG_FLAG_REMOTE_FRAME;
1330 for (i = 0; i < ARRAY_SIZE(priv->tx_contexts); i++) {
1331 if (priv->tx_contexts[i].echo_index == MAX_TX_URBS) {
1332 context = &priv->tx_contexts[i];
1333 break;
1337 if (!context) {
1338 netdev_warn(netdev, "cannot find free context\n");
1339 ret = NETDEV_TX_BUSY;
1340 goto releasebuf;
1343 context->priv = priv;
1344 context->echo_index = i;
1345 context->dlc = cf->can_dlc;
1347 msg->u.tx_can.tid = context->echo_index;
1349 usb_fill_bulk_urb(urb, dev->udev,
1350 usb_sndbulkpipe(dev->udev,
1351 dev->bulk_out->bEndpointAddress),
1352 buf, msg->len,
1353 kvaser_usb_write_bulk_callback, context);
1354 usb_anchor_urb(urb, &priv->tx_submitted);
1356 can_put_echo_skb(skb, netdev, context->echo_index);
1358 atomic_inc(&priv->active_tx_urbs);
1360 if (atomic_read(&priv->active_tx_urbs) >= MAX_TX_URBS)
1361 netif_stop_queue(netdev);
1363 err = usb_submit_urb(urb, GFP_ATOMIC);
1364 if (unlikely(err)) {
1365 can_free_echo_skb(netdev, context->echo_index);
1367 skb = NULL; /* set to NULL to avoid double free in
1368 * dev_kfree_skb(skb) */
1370 atomic_dec(&priv->active_tx_urbs);
1371 usb_unanchor_urb(urb);
1373 stats->tx_dropped++;
1375 if (err == -ENODEV)
1376 netif_device_detach(netdev);
1377 else
1378 netdev_warn(netdev, "Failed tx_urb %d\n", err);
1380 goto releasebuf;
1383 usb_free_urb(urb);
1385 return NETDEV_TX_OK;
1387 releasebuf:
1388 kfree(buf);
1389 nobufmem:
1390 usb_free_urb(urb);
1391 nourbmem:
1392 dev_kfree_skb(skb);
1393 return ret;
1396 static const struct net_device_ops kvaser_usb_netdev_ops = {
1397 .ndo_open = kvaser_usb_open,
1398 .ndo_stop = kvaser_usb_close,
1399 .ndo_start_xmit = kvaser_usb_start_xmit,
1400 .ndo_change_mtu = can_change_mtu,
1403 static const struct can_bittiming_const kvaser_usb_bittiming_const = {
1404 .name = "kvaser_usb",
1405 .tseg1_min = KVASER_USB_TSEG1_MIN,
1406 .tseg1_max = KVASER_USB_TSEG1_MAX,
1407 .tseg2_min = KVASER_USB_TSEG2_MIN,
1408 .tseg2_max = KVASER_USB_TSEG2_MAX,
1409 .sjw_max = KVASER_USB_SJW_MAX,
1410 .brp_min = KVASER_USB_BRP_MIN,
1411 .brp_max = KVASER_USB_BRP_MAX,
1412 .brp_inc = KVASER_USB_BRP_INC,
1415 static int kvaser_usb_set_bittiming(struct net_device *netdev)
1417 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1418 struct can_bittiming *bt = &priv->can.bittiming;
1419 struct kvaser_usb *dev = priv->dev;
1420 struct kvaser_msg *msg;
1421 int rc;
1423 msg = kmalloc(sizeof(*msg), GFP_KERNEL);
1424 if (!msg)
1425 return -ENOMEM;
1427 msg->id = CMD_SET_BUS_PARAMS;
1428 msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_busparams);
1429 msg->u.busparams.channel = priv->channel;
1430 msg->u.busparams.tid = 0xff;
1431 msg->u.busparams.bitrate = cpu_to_le32(bt->bitrate);
1432 msg->u.busparams.sjw = bt->sjw;
1433 msg->u.busparams.tseg1 = bt->prop_seg + bt->phase_seg1;
1434 msg->u.busparams.tseg2 = bt->phase_seg2;
1436 if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
1437 msg->u.busparams.no_samp = 3;
1438 else
1439 msg->u.busparams.no_samp = 1;
1441 rc = kvaser_usb_send_msg(dev, msg);
1443 kfree(msg);
1444 return rc;
1447 static int kvaser_usb_set_mode(struct net_device *netdev,
1448 enum can_mode mode)
1450 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1451 int err;
1453 switch (mode) {
1454 case CAN_MODE_START:
1455 err = kvaser_usb_simple_msg_async(priv, CMD_START_CHIP);
1456 if (err)
1457 return err;
1458 break;
1459 default:
1460 return -EOPNOTSUPP;
1463 return 0;
1466 static int kvaser_usb_get_berr_counter(const struct net_device *netdev,
1467 struct can_berr_counter *bec)
1469 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1471 *bec = priv->bec;
1473 return 0;
1476 static void kvaser_usb_remove_interfaces(struct kvaser_usb *dev)
1478 int i;
1480 for (i = 0; i < dev->nchannels; i++) {
1481 if (!dev->nets[i])
1482 continue;
1484 unregister_netdev(dev->nets[i]->netdev);
1487 kvaser_usb_unlink_all_urbs(dev);
1489 for (i = 0; i < dev->nchannels; i++) {
1490 if (!dev->nets[i])
1491 continue;
1493 free_candev(dev->nets[i]->netdev);
1497 static int kvaser_usb_init_one(struct usb_interface *intf,
1498 const struct usb_device_id *id, int channel)
1500 struct kvaser_usb *dev = usb_get_intfdata(intf);
1501 struct net_device *netdev;
1502 struct kvaser_usb_net_priv *priv;
1503 int i, err;
1505 netdev = alloc_candev(sizeof(*priv), MAX_TX_URBS);
1506 if (!netdev) {
1507 dev_err(&intf->dev, "Cannot alloc candev\n");
1508 return -ENOMEM;
1511 priv = netdev_priv(netdev);
1513 init_completion(&priv->start_comp);
1514 init_completion(&priv->stop_comp);
1516 init_usb_anchor(&priv->tx_submitted);
1517 atomic_set(&priv->active_tx_urbs, 0);
1519 for (i = 0; i < ARRAY_SIZE(priv->tx_contexts); i++)
1520 priv->tx_contexts[i].echo_index = MAX_TX_URBS;
1522 priv->dev = dev;
1523 priv->netdev = netdev;
1524 priv->channel = channel;
1526 priv->can.state = CAN_STATE_STOPPED;
1527 priv->can.clock.freq = CAN_USB_CLOCK;
1528 priv->can.bittiming_const = &kvaser_usb_bittiming_const;
1529 priv->can.do_set_bittiming = kvaser_usb_set_bittiming;
1530 priv->can.do_set_mode = kvaser_usb_set_mode;
1531 if (id->driver_info & KVASER_HAS_TXRX_ERRORS)
1532 priv->can.do_get_berr_counter = kvaser_usb_get_berr_counter;
1533 priv->can.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES;
1534 if (id->driver_info & KVASER_HAS_SILENT_MODE)
1535 priv->can.ctrlmode_supported |= CAN_CTRLMODE_LISTENONLY;
1537 netdev->flags |= IFF_ECHO;
1539 netdev->netdev_ops = &kvaser_usb_netdev_ops;
1541 SET_NETDEV_DEV(netdev, &intf->dev);
1542 netdev->dev_id = channel;
1544 dev->nets[channel] = priv;
1546 err = register_candev(netdev);
1547 if (err) {
1548 dev_err(&intf->dev, "Failed to register can device\n");
1549 free_candev(netdev);
1550 dev->nets[channel] = NULL;
1551 return err;
1554 netdev_dbg(netdev, "device registered\n");
1556 return 0;
1559 static int kvaser_usb_get_endpoints(const struct usb_interface *intf,
1560 struct usb_endpoint_descriptor **in,
1561 struct usb_endpoint_descriptor **out)
1563 const struct usb_host_interface *iface_desc;
1564 struct usb_endpoint_descriptor *endpoint;
1565 int i;
1567 iface_desc = &intf->altsetting[0];
1569 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1570 endpoint = &iface_desc->endpoint[i].desc;
1572 if (!*in && usb_endpoint_is_bulk_in(endpoint))
1573 *in = endpoint;
1575 if (!*out && usb_endpoint_is_bulk_out(endpoint))
1576 *out = endpoint;
1578 /* use first bulk endpoint for in and out */
1579 if (*in && *out)
1580 return 0;
1583 return -ENODEV;
1586 static int kvaser_usb_probe(struct usb_interface *intf,
1587 const struct usb_device_id *id)
1589 struct kvaser_usb *dev;
1590 int err = -ENOMEM;
1591 int i;
1593 dev = devm_kzalloc(&intf->dev, sizeof(*dev), GFP_KERNEL);
1594 if (!dev)
1595 return -ENOMEM;
1597 err = kvaser_usb_get_endpoints(intf, &dev->bulk_in, &dev->bulk_out);
1598 if (err) {
1599 dev_err(&intf->dev, "Cannot get usb endpoint(s)");
1600 return err;
1603 dev->udev = interface_to_usbdev(intf);
1605 init_usb_anchor(&dev->rx_submitted);
1607 usb_set_intfdata(intf, dev);
1609 for (i = 0; i < MAX_NET_DEVICES; i++)
1610 kvaser_usb_send_simple_msg(dev, CMD_RESET_CHIP, i);
1612 err = kvaser_usb_get_software_info(dev);
1613 if (err) {
1614 dev_err(&intf->dev,
1615 "Cannot get software infos, error %d\n", err);
1616 return err;
1619 err = kvaser_usb_get_card_info(dev);
1620 if (err) {
1621 dev_err(&intf->dev,
1622 "Cannot get card infos, error %d\n", err);
1623 return err;
1626 dev_dbg(&intf->dev, "Firmware version: %d.%d.%d\n",
1627 ((dev->fw_version >> 24) & 0xff),
1628 ((dev->fw_version >> 16) & 0xff),
1629 (dev->fw_version & 0xffff));
1631 for (i = 0; i < dev->nchannels; i++) {
1632 err = kvaser_usb_init_one(intf, id, i);
1633 if (err) {
1634 kvaser_usb_remove_interfaces(dev);
1635 return err;
1639 return 0;
1642 static void kvaser_usb_disconnect(struct usb_interface *intf)
1644 struct kvaser_usb *dev = usb_get_intfdata(intf);
1646 usb_set_intfdata(intf, NULL);
1648 if (!dev)
1649 return;
1651 kvaser_usb_remove_interfaces(dev);
1654 static struct usb_driver kvaser_usb_driver = {
1655 .name = "kvaser_usb",
1656 .probe = kvaser_usb_probe,
1657 .disconnect = kvaser_usb_disconnect,
1658 .id_table = kvaser_usb_table,
1661 module_usb_driver(kvaser_usb_driver);
1663 MODULE_AUTHOR("Olivier Sobrie <olivier@sobrie.be>");
1664 MODULE_DESCRIPTION("CAN driver for Kvaser CAN/USB devices");
1665 MODULE_LICENSE("GPL v2");