usb: renesas_usbhs: disable TX IRQ before starting TX DMAC transfer
[linux/fpc-iii.git] / drivers / net / can / usb / kvaser_usb.c
blob4d47f224d7056b7eb982becda069ec21e96b9593
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/spinlock.h>
16 #include <linux/kernel.h>
17 #include <linux/completion.h>
18 #include <linux/module.h>
19 #include <linux/netdevice.h>
20 #include <linux/usb.h>
22 #include <linux/can.h>
23 #include <linux/can/dev.h>
24 #include <linux/can/error.h>
26 #define MAX_TX_URBS 16
27 #define MAX_RX_URBS 4
28 #define START_TIMEOUT 1000 /* msecs */
29 #define STOP_TIMEOUT 1000 /* msecs */
30 #define USB_SEND_TIMEOUT 1000 /* msecs */
31 #define USB_RECV_TIMEOUT 1000 /* msecs */
32 #define RX_BUFFER_SIZE 3072
33 #define CAN_USB_CLOCK 8000000
34 #define MAX_NET_DEVICES 3
36 /* Kvaser USB devices */
37 #define KVASER_VENDOR_ID 0x0bfd
38 #define USB_LEAF_DEVEL_PRODUCT_ID 10
39 #define USB_LEAF_LITE_PRODUCT_ID 11
40 #define USB_LEAF_PRO_PRODUCT_ID 12
41 #define USB_LEAF_SPRO_PRODUCT_ID 14
42 #define USB_LEAF_PRO_LS_PRODUCT_ID 15
43 #define USB_LEAF_PRO_SWC_PRODUCT_ID 16
44 #define USB_LEAF_PRO_LIN_PRODUCT_ID 17
45 #define USB_LEAF_SPRO_LS_PRODUCT_ID 18
46 #define USB_LEAF_SPRO_SWC_PRODUCT_ID 19
47 #define USB_MEMO2_DEVEL_PRODUCT_ID 22
48 #define USB_MEMO2_HSHS_PRODUCT_ID 23
49 #define USB_UPRO_HSHS_PRODUCT_ID 24
50 #define USB_LEAF_LITE_GI_PRODUCT_ID 25
51 #define USB_LEAF_PRO_OBDII_PRODUCT_ID 26
52 #define USB_MEMO2_HSLS_PRODUCT_ID 27
53 #define USB_LEAF_LITE_CH_PRODUCT_ID 28
54 #define USB_BLACKBIRD_SPRO_PRODUCT_ID 29
55 #define USB_OEM_MERCURY_PRODUCT_ID 34
56 #define USB_OEM_LEAF_PRODUCT_ID 35
57 #define USB_CAN_R_PRODUCT_ID 39
58 #define USB_LEAF_LITE_V2_PRODUCT_ID 288
59 #define USB_MINI_PCIE_HS_PRODUCT_ID 289
61 /* USB devices features */
62 #define KVASER_HAS_SILENT_MODE BIT(0)
63 #define KVASER_HAS_TXRX_ERRORS BIT(1)
65 /* Message header size */
66 #define MSG_HEADER_LEN 2
68 /* Can message flags */
69 #define MSG_FLAG_ERROR_FRAME BIT(0)
70 #define MSG_FLAG_OVERRUN BIT(1)
71 #define MSG_FLAG_NERR BIT(2)
72 #define MSG_FLAG_WAKEUP BIT(3)
73 #define MSG_FLAG_REMOTE_FRAME BIT(4)
74 #define MSG_FLAG_RESERVED BIT(5)
75 #define MSG_FLAG_TX_ACK BIT(6)
76 #define MSG_FLAG_TX_REQUEST BIT(7)
78 /* Can states */
79 #define M16C_STATE_BUS_RESET BIT(0)
80 #define M16C_STATE_BUS_ERROR BIT(4)
81 #define M16C_STATE_BUS_PASSIVE BIT(5)
82 #define M16C_STATE_BUS_OFF BIT(6)
84 /* Can msg ids */
85 #define CMD_RX_STD_MESSAGE 12
86 #define CMD_TX_STD_MESSAGE 13
87 #define CMD_RX_EXT_MESSAGE 14
88 #define CMD_TX_EXT_MESSAGE 15
89 #define CMD_SET_BUS_PARAMS 16
90 #define CMD_GET_BUS_PARAMS 17
91 #define CMD_GET_BUS_PARAMS_REPLY 18
92 #define CMD_GET_CHIP_STATE 19
93 #define CMD_CHIP_STATE_EVENT 20
94 #define CMD_SET_CTRL_MODE 21
95 #define CMD_GET_CTRL_MODE 22
96 #define CMD_GET_CTRL_MODE_REPLY 23
97 #define CMD_RESET_CHIP 24
98 #define CMD_RESET_CARD 25
99 #define CMD_START_CHIP 26
100 #define CMD_START_CHIP_REPLY 27
101 #define CMD_STOP_CHIP 28
102 #define CMD_STOP_CHIP_REPLY 29
103 #define CMD_GET_CARD_INFO2 32
104 #define CMD_GET_CARD_INFO 34
105 #define CMD_GET_CARD_INFO_REPLY 35
106 #define CMD_GET_SOFTWARE_INFO 38
107 #define CMD_GET_SOFTWARE_INFO_REPLY 39
108 #define CMD_ERROR_EVENT 45
109 #define CMD_FLUSH_QUEUE 48
110 #define CMD_RESET_ERROR_COUNTER 49
111 #define CMD_TX_ACKNOWLEDGE 50
112 #define CMD_CAN_ERROR_EVENT 51
113 #define CMD_USB_THROTTLE 77
114 #define CMD_LOG_MESSAGE 106
116 /* error factors */
117 #define M16C_EF_ACKE BIT(0)
118 #define M16C_EF_CRCE BIT(1)
119 #define M16C_EF_FORME BIT(2)
120 #define M16C_EF_STFE BIT(3)
121 #define M16C_EF_BITE0 BIT(4)
122 #define M16C_EF_BITE1 BIT(5)
123 #define M16C_EF_RCVE BIT(6)
124 #define M16C_EF_TRE BIT(7)
126 /* bittiming parameters */
127 #define KVASER_USB_TSEG1_MIN 1
128 #define KVASER_USB_TSEG1_MAX 16
129 #define KVASER_USB_TSEG2_MIN 1
130 #define KVASER_USB_TSEG2_MAX 8
131 #define KVASER_USB_SJW_MAX 4
132 #define KVASER_USB_BRP_MIN 1
133 #define KVASER_USB_BRP_MAX 64
134 #define KVASER_USB_BRP_INC 1
136 /* ctrl modes */
137 #define KVASER_CTRL_MODE_NORMAL 1
138 #define KVASER_CTRL_MODE_SILENT 2
139 #define KVASER_CTRL_MODE_SELFRECEPTION 3
140 #define KVASER_CTRL_MODE_OFF 4
142 /* log message */
143 #define KVASER_EXTENDED_FRAME BIT(31)
145 struct kvaser_msg_simple {
146 u8 tid;
147 u8 channel;
148 } __packed;
150 struct kvaser_msg_cardinfo {
151 u8 tid;
152 u8 nchannels;
153 __le32 serial_number;
154 __le32 padding;
155 __le32 clock_resolution;
156 __le32 mfgdate;
157 u8 ean[8];
158 u8 hw_revision;
159 u8 usb_hs_mode;
160 __le16 padding2;
161 } __packed;
163 struct kvaser_msg_cardinfo2 {
164 u8 tid;
165 u8 channel;
166 u8 pcb_id[24];
167 __le32 oem_unlock_code;
168 } __packed;
170 struct kvaser_msg_softinfo {
171 u8 tid;
172 u8 channel;
173 __le32 sw_options;
174 __le32 fw_version;
175 __le16 max_outstanding_tx;
176 __le16 padding[9];
177 } __packed;
179 struct kvaser_msg_busparams {
180 u8 tid;
181 u8 channel;
182 __le32 bitrate;
183 u8 tseg1;
184 u8 tseg2;
185 u8 sjw;
186 u8 no_samp;
187 } __packed;
189 struct kvaser_msg_tx_can {
190 u8 channel;
191 u8 tid;
192 u8 msg[14];
193 u8 padding;
194 u8 flags;
195 } __packed;
197 struct kvaser_msg_rx_can {
198 u8 channel;
199 u8 flag;
200 __le16 time[3];
201 u8 msg[14];
202 } __packed;
204 struct kvaser_msg_chip_state_event {
205 u8 tid;
206 u8 channel;
207 __le16 time[3];
208 u8 tx_errors_count;
209 u8 rx_errors_count;
210 u8 status;
211 u8 padding[3];
212 } __packed;
214 struct kvaser_msg_tx_acknowledge {
215 u8 channel;
216 u8 tid;
217 __le16 time[3];
218 u8 flags;
219 u8 time_offset;
220 } __packed;
222 struct kvaser_msg_error_event {
223 u8 tid;
224 u8 flags;
225 __le16 time[3];
226 u8 channel;
227 u8 padding;
228 u8 tx_errors_count;
229 u8 rx_errors_count;
230 u8 status;
231 u8 error_factor;
232 } __packed;
234 struct kvaser_msg_ctrl_mode {
235 u8 tid;
236 u8 channel;
237 u8 ctrl_mode;
238 u8 padding[3];
239 } __packed;
241 struct kvaser_msg_flush_queue {
242 u8 tid;
243 u8 channel;
244 u8 flags;
245 u8 padding[3];
246 } __packed;
248 struct kvaser_msg_log_message {
249 u8 channel;
250 u8 flags;
251 __le16 time[3];
252 u8 dlc;
253 u8 time_offset;
254 __le32 id;
255 u8 data[8];
256 } __packed;
258 struct kvaser_msg {
259 u8 len;
260 u8 id;
261 union {
262 struct kvaser_msg_simple simple;
263 struct kvaser_msg_cardinfo cardinfo;
264 struct kvaser_msg_cardinfo2 cardinfo2;
265 struct kvaser_msg_softinfo softinfo;
266 struct kvaser_msg_busparams busparams;
267 struct kvaser_msg_tx_can tx_can;
268 struct kvaser_msg_rx_can rx_can;
269 struct kvaser_msg_chip_state_event chip_state_event;
270 struct kvaser_msg_tx_acknowledge tx_acknowledge;
271 struct kvaser_msg_error_event error_event;
272 struct kvaser_msg_ctrl_mode ctrl_mode;
273 struct kvaser_msg_flush_queue flush_queue;
274 struct kvaser_msg_log_message log_message;
275 } u;
276 } __packed;
278 struct kvaser_usb_tx_urb_context {
279 struct kvaser_usb_net_priv *priv;
280 u32 echo_index;
281 int dlc;
284 struct kvaser_usb {
285 struct usb_device *udev;
286 struct kvaser_usb_net_priv *nets[MAX_NET_DEVICES];
288 struct usb_endpoint_descriptor *bulk_in, *bulk_out;
289 struct usb_anchor rx_submitted;
291 u32 fw_version;
292 unsigned int nchannels;
294 bool rxinitdone;
295 void *rxbuf[MAX_RX_URBS];
296 dma_addr_t rxbuf_dma[MAX_RX_URBS];
299 struct kvaser_usb_net_priv {
300 struct can_priv can;
302 spinlock_t tx_contexts_lock;
303 int active_tx_contexts;
304 struct kvaser_usb_tx_urb_context tx_contexts[MAX_TX_URBS];
306 struct usb_anchor tx_submitted;
307 struct completion start_comp, stop_comp;
309 struct kvaser_usb *dev;
310 struct net_device *netdev;
311 int channel;
313 struct can_berr_counter bec;
316 static const struct usb_device_id kvaser_usb_table[] = {
317 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_DEVEL_PRODUCT_ID) },
318 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_PRODUCT_ID) },
319 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_PRODUCT_ID),
320 .driver_info = KVASER_HAS_TXRX_ERRORS |
321 KVASER_HAS_SILENT_MODE },
322 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_SPRO_PRODUCT_ID),
323 .driver_info = KVASER_HAS_TXRX_ERRORS |
324 KVASER_HAS_SILENT_MODE },
325 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_LS_PRODUCT_ID),
326 .driver_info = KVASER_HAS_TXRX_ERRORS |
327 KVASER_HAS_SILENT_MODE },
328 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_SWC_PRODUCT_ID),
329 .driver_info = KVASER_HAS_TXRX_ERRORS |
330 KVASER_HAS_SILENT_MODE },
331 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_LIN_PRODUCT_ID),
332 .driver_info = KVASER_HAS_TXRX_ERRORS |
333 KVASER_HAS_SILENT_MODE },
334 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_SPRO_LS_PRODUCT_ID),
335 .driver_info = KVASER_HAS_TXRX_ERRORS |
336 KVASER_HAS_SILENT_MODE },
337 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_SPRO_SWC_PRODUCT_ID),
338 .driver_info = KVASER_HAS_TXRX_ERRORS |
339 KVASER_HAS_SILENT_MODE },
340 { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO2_DEVEL_PRODUCT_ID),
341 .driver_info = KVASER_HAS_TXRX_ERRORS |
342 KVASER_HAS_SILENT_MODE },
343 { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO2_HSHS_PRODUCT_ID),
344 .driver_info = KVASER_HAS_TXRX_ERRORS |
345 KVASER_HAS_SILENT_MODE },
346 { USB_DEVICE(KVASER_VENDOR_ID, USB_UPRO_HSHS_PRODUCT_ID),
347 .driver_info = KVASER_HAS_TXRX_ERRORS },
348 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_GI_PRODUCT_ID) },
349 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_OBDII_PRODUCT_ID),
350 .driver_info = KVASER_HAS_TXRX_ERRORS |
351 KVASER_HAS_SILENT_MODE },
352 { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO2_HSLS_PRODUCT_ID),
353 .driver_info = KVASER_HAS_TXRX_ERRORS },
354 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_CH_PRODUCT_ID),
355 .driver_info = KVASER_HAS_TXRX_ERRORS },
356 { USB_DEVICE(KVASER_VENDOR_ID, USB_BLACKBIRD_SPRO_PRODUCT_ID),
357 .driver_info = KVASER_HAS_TXRX_ERRORS },
358 { USB_DEVICE(KVASER_VENDOR_ID, USB_OEM_MERCURY_PRODUCT_ID),
359 .driver_info = KVASER_HAS_TXRX_ERRORS },
360 { USB_DEVICE(KVASER_VENDOR_ID, USB_OEM_LEAF_PRODUCT_ID),
361 .driver_info = KVASER_HAS_TXRX_ERRORS },
362 { USB_DEVICE(KVASER_VENDOR_ID, USB_CAN_R_PRODUCT_ID),
363 .driver_info = KVASER_HAS_TXRX_ERRORS },
364 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_V2_PRODUCT_ID) },
365 { USB_DEVICE(KVASER_VENDOR_ID, USB_MINI_PCIE_HS_PRODUCT_ID) },
368 MODULE_DEVICE_TABLE(usb, kvaser_usb_table);
370 static inline int kvaser_usb_send_msg(const struct kvaser_usb *dev,
371 struct kvaser_msg *msg)
373 int actual_len;
375 return usb_bulk_msg(dev->udev,
376 usb_sndbulkpipe(dev->udev,
377 dev->bulk_out->bEndpointAddress),
378 msg, msg->len, &actual_len,
379 USB_SEND_TIMEOUT);
382 static int kvaser_usb_wait_msg(const struct kvaser_usb *dev, u8 id,
383 struct kvaser_msg *msg)
385 struct kvaser_msg *tmp;
386 void *buf;
387 int actual_len;
388 int err;
389 int pos;
390 unsigned long to = jiffies + msecs_to_jiffies(USB_RECV_TIMEOUT);
392 buf = kzalloc(RX_BUFFER_SIZE, GFP_KERNEL);
393 if (!buf)
394 return -ENOMEM;
396 do {
397 err = usb_bulk_msg(dev->udev,
398 usb_rcvbulkpipe(dev->udev,
399 dev->bulk_in->bEndpointAddress),
400 buf, RX_BUFFER_SIZE, &actual_len,
401 USB_RECV_TIMEOUT);
402 if (err < 0)
403 goto end;
405 pos = 0;
406 while (pos <= actual_len - MSG_HEADER_LEN) {
407 tmp = buf + pos;
409 /* Handle messages crossing the USB endpoint max packet
410 * size boundary. Check kvaser_usb_read_bulk_callback()
411 * for further details.
413 if (tmp->len == 0) {
414 pos = round_up(pos,
415 dev->bulk_in->wMaxPacketSize);
416 continue;
419 if (pos + tmp->len > actual_len) {
420 dev_err(dev->udev->dev.parent,
421 "Format error\n");
422 break;
425 if (tmp->id == id) {
426 memcpy(msg, tmp, tmp->len);
427 goto end;
430 pos += tmp->len;
432 } while (time_before(jiffies, to));
434 err = -EINVAL;
436 end:
437 kfree(buf);
439 return err;
442 static int kvaser_usb_send_simple_msg(const struct kvaser_usb *dev,
443 u8 msg_id, int channel)
445 struct kvaser_msg *msg;
446 int rc;
448 msg = kmalloc(sizeof(*msg), GFP_KERNEL);
449 if (!msg)
450 return -ENOMEM;
452 msg->id = msg_id;
453 msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_simple);
454 msg->u.simple.channel = channel;
455 msg->u.simple.tid = 0xff;
457 rc = kvaser_usb_send_msg(dev, msg);
459 kfree(msg);
460 return rc;
463 static int kvaser_usb_get_software_info(struct kvaser_usb *dev)
465 struct kvaser_msg msg;
466 int err;
468 err = kvaser_usb_send_simple_msg(dev, CMD_GET_SOFTWARE_INFO, 0);
469 if (err)
470 return err;
472 err = kvaser_usb_wait_msg(dev, CMD_GET_SOFTWARE_INFO_REPLY, &msg);
473 if (err)
474 return err;
476 dev->fw_version = le32_to_cpu(msg.u.softinfo.fw_version);
478 return 0;
481 static int kvaser_usb_get_card_info(struct kvaser_usb *dev)
483 struct kvaser_msg msg;
484 int err;
486 err = kvaser_usb_send_simple_msg(dev, CMD_GET_CARD_INFO, 0);
487 if (err)
488 return err;
490 err = kvaser_usb_wait_msg(dev, CMD_GET_CARD_INFO_REPLY, &msg);
491 if (err)
492 return err;
494 dev->nchannels = msg.u.cardinfo.nchannels;
495 if (dev->nchannels > MAX_NET_DEVICES)
496 return -EINVAL;
498 return 0;
501 static void kvaser_usb_tx_acknowledge(const struct kvaser_usb *dev,
502 const struct kvaser_msg *msg)
504 struct net_device_stats *stats;
505 struct kvaser_usb_tx_urb_context *context;
506 struct kvaser_usb_net_priv *priv;
507 struct sk_buff *skb;
508 struct can_frame *cf;
509 u8 channel = msg->u.tx_acknowledge.channel;
510 u8 tid = msg->u.tx_acknowledge.tid;
511 unsigned long flags;
513 if (channel >= dev->nchannels) {
514 dev_err(dev->udev->dev.parent,
515 "Invalid channel number (%d)\n", channel);
516 return;
519 priv = dev->nets[channel];
521 if (!netif_device_present(priv->netdev))
522 return;
524 stats = &priv->netdev->stats;
526 context = &priv->tx_contexts[tid % MAX_TX_URBS];
528 /* Sometimes the state change doesn't come after a bus-off event */
529 if (priv->can.restart_ms &&
530 (priv->can.state >= CAN_STATE_BUS_OFF)) {
531 skb = alloc_can_err_skb(priv->netdev, &cf);
532 if (skb) {
533 cf->can_id |= CAN_ERR_RESTARTED;
534 netif_rx(skb);
536 stats->rx_packets++;
537 stats->rx_bytes += cf->can_dlc;
538 } else {
539 netdev_err(priv->netdev,
540 "No memory left for err_skb\n");
543 priv->can.can_stats.restarts++;
544 netif_carrier_on(priv->netdev);
546 priv->can.state = CAN_STATE_ERROR_ACTIVE;
549 stats->tx_packets++;
550 stats->tx_bytes += context->dlc;
552 spin_lock_irqsave(&priv->tx_contexts_lock, flags);
554 can_get_echo_skb(priv->netdev, context->echo_index);
555 context->echo_index = MAX_TX_URBS;
556 --priv->active_tx_contexts;
557 netif_wake_queue(priv->netdev);
559 spin_unlock_irqrestore(&priv->tx_contexts_lock, flags);
562 static void kvaser_usb_simple_msg_callback(struct urb *urb)
564 struct net_device *netdev = urb->context;
566 kfree(urb->transfer_buffer);
568 if (urb->status)
569 netdev_warn(netdev, "urb status received: %d\n",
570 urb->status);
573 static int kvaser_usb_simple_msg_async(struct kvaser_usb_net_priv *priv,
574 u8 msg_id)
576 struct kvaser_usb *dev = priv->dev;
577 struct net_device *netdev = priv->netdev;
578 struct kvaser_msg *msg;
579 struct urb *urb;
580 void *buf;
581 int err;
583 urb = usb_alloc_urb(0, GFP_ATOMIC);
584 if (!urb) {
585 netdev_err(netdev, "No memory left for URBs\n");
586 return -ENOMEM;
589 buf = kmalloc(sizeof(struct kvaser_msg), GFP_ATOMIC);
590 if (!buf) {
591 usb_free_urb(urb);
592 return -ENOMEM;
595 msg = (struct kvaser_msg *)buf;
596 msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_simple);
597 msg->id = msg_id;
598 msg->u.simple.channel = priv->channel;
600 usb_fill_bulk_urb(urb, dev->udev,
601 usb_sndbulkpipe(dev->udev,
602 dev->bulk_out->bEndpointAddress),
603 buf, msg->len,
604 kvaser_usb_simple_msg_callback, netdev);
605 usb_anchor_urb(urb, &priv->tx_submitted);
607 err = usb_submit_urb(urb, GFP_ATOMIC);
608 if (err) {
609 netdev_err(netdev, "Error transmitting URB\n");
610 usb_unanchor_urb(urb);
611 usb_free_urb(urb);
612 return err;
615 usb_free_urb(urb);
617 return 0;
620 static void kvaser_usb_rx_error(const struct kvaser_usb *dev,
621 const struct kvaser_msg *msg)
623 struct can_frame *cf;
624 struct sk_buff *skb;
625 struct net_device_stats *stats;
626 struct kvaser_usb_net_priv *priv;
627 unsigned int new_state;
628 u8 channel, status, txerr, rxerr, error_factor;
630 switch (msg->id) {
631 case CMD_CAN_ERROR_EVENT:
632 channel = msg->u.error_event.channel;
633 status = msg->u.error_event.status;
634 txerr = msg->u.error_event.tx_errors_count;
635 rxerr = msg->u.error_event.rx_errors_count;
636 error_factor = msg->u.error_event.error_factor;
637 break;
638 case CMD_LOG_MESSAGE:
639 channel = msg->u.log_message.channel;
640 status = msg->u.log_message.data[0];
641 txerr = msg->u.log_message.data[2];
642 rxerr = msg->u.log_message.data[3];
643 error_factor = msg->u.log_message.data[1];
644 break;
645 case CMD_CHIP_STATE_EVENT:
646 channel = msg->u.chip_state_event.channel;
647 status = msg->u.chip_state_event.status;
648 txerr = msg->u.chip_state_event.tx_errors_count;
649 rxerr = msg->u.chip_state_event.rx_errors_count;
650 error_factor = 0;
651 break;
652 default:
653 dev_err(dev->udev->dev.parent, "Invalid msg id (%d)\n",
654 msg->id);
655 return;
658 if (channel >= dev->nchannels) {
659 dev_err(dev->udev->dev.parent,
660 "Invalid channel number (%d)\n", channel);
661 return;
664 priv = dev->nets[channel];
665 stats = &priv->netdev->stats;
667 skb = alloc_can_err_skb(priv->netdev, &cf);
668 if (!skb) {
669 stats->rx_dropped++;
670 return;
673 new_state = priv->can.state;
675 netdev_dbg(priv->netdev, "Error status: 0x%02x\n", status);
677 if (status & (M16C_STATE_BUS_OFF | M16C_STATE_BUS_RESET)) {
678 cf->can_id |= CAN_ERR_BUSOFF;
680 priv->can.can_stats.bus_off++;
681 if (!priv->can.restart_ms)
682 kvaser_usb_simple_msg_async(priv, CMD_STOP_CHIP);
684 netif_carrier_off(priv->netdev);
686 new_state = CAN_STATE_BUS_OFF;
687 } else if (status & M16C_STATE_BUS_PASSIVE) {
688 if (priv->can.state != CAN_STATE_ERROR_PASSIVE) {
689 cf->can_id |= CAN_ERR_CRTL;
691 if (txerr || rxerr)
692 cf->data[1] = (txerr > rxerr)
693 ? CAN_ERR_CRTL_TX_PASSIVE
694 : CAN_ERR_CRTL_RX_PASSIVE;
695 else
696 cf->data[1] = CAN_ERR_CRTL_TX_PASSIVE |
697 CAN_ERR_CRTL_RX_PASSIVE;
699 priv->can.can_stats.error_passive++;
702 new_state = CAN_STATE_ERROR_PASSIVE;
703 } else if (status & M16C_STATE_BUS_ERROR) {
704 if ((priv->can.state < CAN_STATE_ERROR_WARNING) &&
705 ((txerr >= 96) || (rxerr >= 96))) {
706 cf->can_id |= CAN_ERR_CRTL;
707 cf->data[1] = (txerr > rxerr)
708 ? CAN_ERR_CRTL_TX_WARNING
709 : CAN_ERR_CRTL_RX_WARNING;
711 priv->can.can_stats.error_warning++;
712 new_state = CAN_STATE_ERROR_WARNING;
713 } else if ((priv->can.state > CAN_STATE_ERROR_ACTIVE) &&
714 ((txerr < 96) && (rxerr < 96))) {
715 cf->can_id |= CAN_ERR_PROT;
716 cf->data[2] = CAN_ERR_PROT_ACTIVE;
718 new_state = CAN_STATE_ERROR_ACTIVE;
722 if (!status) {
723 cf->can_id |= CAN_ERR_PROT;
724 cf->data[2] = CAN_ERR_PROT_ACTIVE;
726 new_state = CAN_STATE_ERROR_ACTIVE;
729 if (priv->can.restart_ms &&
730 (priv->can.state >= CAN_STATE_BUS_OFF) &&
731 (new_state < CAN_STATE_BUS_OFF)) {
732 cf->can_id |= CAN_ERR_RESTARTED;
733 netif_carrier_on(priv->netdev);
735 priv->can.can_stats.restarts++;
738 if (error_factor) {
739 priv->can.can_stats.bus_error++;
740 stats->rx_errors++;
742 cf->can_id |= CAN_ERR_BUSERROR | CAN_ERR_PROT;
744 if (error_factor & M16C_EF_ACKE)
745 cf->data[3] |= (CAN_ERR_PROT_LOC_ACK);
746 if (error_factor & M16C_EF_CRCE)
747 cf->data[3] |= (CAN_ERR_PROT_LOC_CRC_SEQ |
748 CAN_ERR_PROT_LOC_CRC_DEL);
749 if (error_factor & M16C_EF_FORME)
750 cf->data[2] |= CAN_ERR_PROT_FORM;
751 if (error_factor & M16C_EF_STFE)
752 cf->data[2] |= CAN_ERR_PROT_STUFF;
753 if (error_factor & M16C_EF_BITE0)
754 cf->data[2] |= CAN_ERR_PROT_BIT0;
755 if (error_factor & M16C_EF_BITE1)
756 cf->data[2] |= CAN_ERR_PROT_BIT1;
757 if (error_factor & M16C_EF_TRE)
758 cf->data[2] |= CAN_ERR_PROT_TX;
761 cf->data[6] = txerr;
762 cf->data[7] = rxerr;
764 priv->bec.txerr = txerr;
765 priv->bec.rxerr = rxerr;
767 priv->can.state = new_state;
769 netif_rx(skb);
771 stats->rx_packets++;
772 stats->rx_bytes += cf->can_dlc;
775 static void kvaser_usb_rx_can_err(const struct kvaser_usb_net_priv *priv,
776 const struct kvaser_msg *msg)
778 struct can_frame *cf;
779 struct sk_buff *skb;
780 struct net_device_stats *stats = &priv->netdev->stats;
782 if (msg->u.rx_can.flag & (MSG_FLAG_ERROR_FRAME |
783 MSG_FLAG_NERR)) {
784 netdev_err(priv->netdev, "Unknow error (flags: 0x%02x)\n",
785 msg->u.rx_can.flag);
787 stats->rx_errors++;
788 return;
791 if (msg->u.rx_can.flag & MSG_FLAG_OVERRUN) {
792 skb = alloc_can_err_skb(priv->netdev, &cf);
793 if (!skb) {
794 stats->rx_dropped++;
795 return;
798 cf->can_id |= CAN_ERR_CRTL;
799 cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
801 stats->rx_over_errors++;
802 stats->rx_errors++;
804 netif_rx(skb);
806 stats->rx_packets++;
807 stats->rx_bytes += cf->can_dlc;
811 static void kvaser_usb_rx_can_msg(const struct kvaser_usb *dev,
812 const struct kvaser_msg *msg)
814 struct kvaser_usb_net_priv *priv;
815 struct can_frame *cf;
816 struct sk_buff *skb;
817 struct net_device_stats *stats;
818 u8 channel = msg->u.rx_can.channel;
820 if (channel >= dev->nchannels) {
821 dev_err(dev->udev->dev.parent,
822 "Invalid channel number (%d)\n", channel);
823 return;
826 priv = dev->nets[channel];
827 stats = &priv->netdev->stats;
829 if ((msg->u.rx_can.flag & MSG_FLAG_ERROR_FRAME) &&
830 (msg->id == CMD_LOG_MESSAGE)) {
831 kvaser_usb_rx_error(dev, msg);
832 return;
833 } else if (msg->u.rx_can.flag & (MSG_FLAG_ERROR_FRAME |
834 MSG_FLAG_NERR |
835 MSG_FLAG_OVERRUN)) {
836 kvaser_usb_rx_can_err(priv, msg);
837 return;
838 } else if (msg->u.rx_can.flag & ~MSG_FLAG_REMOTE_FRAME) {
839 netdev_warn(priv->netdev,
840 "Unhandled frame (flags: 0x%02x)",
841 msg->u.rx_can.flag);
842 return;
845 skb = alloc_can_skb(priv->netdev, &cf);
846 if (!skb) {
847 stats->tx_dropped++;
848 return;
851 if (msg->id == CMD_LOG_MESSAGE) {
852 cf->can_id = le32_to_cpu(msg->u.log_message.id);
853 if (cf->can_id & KVASER_EXTENDED_FRAME)
854 cf->can_id &= CAN_EFF_MASK | CAN_EFF_FLAG;
855 else
856 cf->can_id &= CAN_SFF_MASK;
858 cf->can_dlc = get_can_dlc(msg->u.log_message.dlc);
860 if (msg->u.log_message.flags & MSG_FLAG_REMOTE_FRAME)
861 cf->can_id |= CAN_RTR_FLAG;
862 else
863 memcpy(cf->data, &msg->u.log_message.data,
864 cf->can_dlc);
865 } else {
866 cf->can_id = ((msg->u.rx_can.msg[0] & 0x1f) << 6) |
867 (msg->u.rx_can.msg[1] & 0x3f);
869 if (msg->id == CMD_RX_EXT_MESSAGE) {
870 cf->can_id <<= 18;
871 cf->can_id |= ((msg->u.rx_can.msg[2] & 0x0f) << 14) |
872 ((msg->u.rx_can.msg[3] & 0xff) << 6) |
873 (msg->u.rx_can.msg[4] & 0x3f);
874 cf->can_id |= CAN_EFF_FLAG;
877 cf->can_dlc = get_can_dlc(msg->u.rx_can.msg[5]);
879 if (msg->u.rx_can.flag & MSG_FLAG_REMOTE_FRAME)
880 cf->can_id |= CAN_RTR_FLAG;
881 else
882 memcpy(cf->data, &msg->u.rx_can.msg[6],
883 cf->can_dlc);
886 netif_rx(skb);
888 stats->rx_packets++;
889 stats->rx_bytes += cf->can_dlc;
892 static void kvaser_usb_start_chip_reply(const struct kvaser_usb *dev,
893 const struct kvaser_msg *msg)
895 struct kvaser_usb_net_priv *priv;
896 u8 channel = msg->u.simple.channel;
898 if (channel >= dev->nchannels) {
899 dev_err(dev->udev->dev.parent,
900 "Invalid channel number (%d)\n", channel);
901 return;
904 priv = dev->nets[channel];
906 if (completion_done(&priv->start_comp) &&
907 netif_queue_stopped(priv->netdev)) {
908 netif_wake_queue(priv->netdev);
909 } else {
910 netif_start_queue(priv->netdev);
911 complete(&priv->start_comp);
915 static void kvaser_usb_stop_chip_reply(const struct kvaser_usb *dev,
916 const struct kvaser_msg *msg)
918 struct kvaser_usb_net_priv *priv;
919 u8 channel = msg->u.simple.channel;
921 if (channel >= dev->nchannels) {
922 dev_err(dev->udev->dev.parent,
923 "Invalid channel number (%d)\n", channel);
924 return;
927 priv = dev->nets[channel];
929 complete(&priv->stop_comp);
932 static void kvaser_usb_handle_message(const struct kvaser_usb *dev,
933 const struct kvaser_msg *msg)
935 switch (msg->id) {
936 case CMD_START_CHIP_REPLY:
937 kvaser_usb_start_chip_reply(dev, msg);
938 break;
940 case CMD_STOP_CHIP_REPLY:
941 kvaser_usb_stop_chip_reply(dev, msg);
942 break;
944 case CMD_RX_STD_MESSAGE:
945 case CMD_RX_EXT_MESSAGE:
946 case CMD_LOG_MESSAGE:
947 kvaser_usb_rx_can_msg(dev, msg);
948 break;
950 case CMD_CHIP_STATE_EVENT:
951 case CMD_CAN_ERROR_EVENT:
952 kvaser_usb_rx_error(dev, msg);
953 break;
955 case CMD_TX_ACKNOWLEDGE:
956 kvaser_usb_tx_acknowledge(dev, msg);
957 break;
959 default:
960 dev_warn(dev->udev->dev.parent,
961 "Unhandled message (%d)\n", msg->id);
962 break;
966 static void kvaser_usb_read_bulk_callback(struct urb *urb)
968 struct kvaser_usb *dev = urb->context;
969 struct kvaser_msg *msg;
970 int pos = 0;
971 int err, i;
973 switch (urb->status) {
974 case 0:
975 break;
976 case -ENOENT:
977 case -ESHUTDOWN:
978 return;
979 default:
980 dev_info(dev->udev->dev.parent, "Rx URB aborted (%d)\n",
981 urb->status);
982 goto resubmit_urb;
985 while (pos <= urb->actual_length - MSG_HEADER_LEN) {
986 msg = urb->transfer_buffer + pos;
988 /* The Kvaser firmware can only read and write messages that
989 * does not cross the USB's endpoint wMaxPacketSize boundary.
990 * If a follow-up command crosses such boundary, firmware puts
991 * a placeholder zero-length command in its place then aligns
992 * the real command to the next max packet size.
994 * Handle such cases or we're going to miss a significant
995 * number of events in case of a heavy rx load on the bus.
997 if (msg->len == 0) {
998 pos = round_up(pos, dev->bulk_in->wMaxPacketSize);
999 continue;
1002 if (pos + msg->len > urb->actual_length) {
1003 dev_err(dev->udev->dev.parent, "Format error\n");
1004 break;
1007 kvaser_usb_handle_message(dev, msg);
1008 pos += msg->len;
1011 resubmit_urb:
1012 usb_fill_bulk_urb(urb, dev->udev,
1013 usb_rcvbulkpipe(dev->udev,
1014 dev->bulk_in->bEndpointAddress),
1015 urb->transfer_buffer, RX_BUFFER_SIZE,
1016 kvaser_usb_read_bulk_callback, dev);
1018 err = usb_submit_urb(urb, GFP_ATOMIC);
1019 if (err == -ENODEV) {
1020 for (i = 0; i < dev->nchannels; i++) {
1021 if (!dev->nets[i])
1022 continue;
1024 netif_device_detach(dev->nets[i]->netdev);
1026 } else if (err) {
1027 dev_err(dev->udev->dev.parent,
1028 "Failed resubmitting read bulk urb: %d\n", err);
1031 return;
1034 static int kvaser_usb_setup_rx_urbs(struct kvaser_usb *dev)
1036 int i, err = 0;
1038 if (dev->rxinitdone)
1039 return 0;
1041 for (i = 0; i < MAX_RX_URBS; i++) {
1042 struct urb *urb = NULL;
1043 u8 *buf = NULL;
1044 dma_addr_t buf_dma;
1046 urb = usb_alloc_urb(0, GFP_KERNEL);
1047 if (!urb) {
1048 dev_warn(dev->udev->dev.parent,
1049 "No memory left for URBs\n");
1050 err = -ENOMEM;
1051 break;
1054 buf = usb_alloc_coherent(dev->udev, RX_BUFFER_SIZE,
1055 GFP_KERNEL, &buf_dma);
1056 if (!buf) {
1057 dev_warn(dev->udev->dev.parent,
1058 "No memory left for USB buffer\n");
1059 usb_free_urb(urb);
1060 err = -ENOMEM;
1061 break;
1064 usb_fill_bulk_urb(urb, dev->udev,
1065 usb_rcvbulkpipe(dev->udev,
1066 dev->bulk_in->bEndpointAddress),
1067 buf, RX_BUFFER_SIZE,
1068 kvaser_usb_read_bulk_callback,
1069 dev);
1070 urb->transfer_dma = buf_dma;
1071 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1072 usb_anchor_urb(urb, &dev->rx_submitted);
1074 err = usb_submit_urb(urb, GFP_KERNEL);
1075 if (err) {
1076 usb_unanchor_urb(urb);
1077 usb_free_coherent(dev->udev, RX_BUFFER_SIZE, buf,
1078 buf_dma);
1079 usb_free_urb(urb);
1080 break;
1083 dev->rxbuf[i] = buf;
1084 dev->rxbuf_dma[i] = buf_dma;
1086 usb_free_urb(urb);
1089 if (i == 0) {
1090 dev_warn(dev->udev->dev.parent,
1091 "Cannot setup read URBs, error %d\n", err);
1092 return err;
1093 } else if (i < MAX_RX_URBS) {
1094 dev_warn(dev->udev->dev.parent,
1095 "RX performances may be slow\n");
1098 dev->rxinitdone = true;
1100 return 0;
1103 static int kvaser_usb_set_opt_mode(const struct kvaser_usb_net_priv *priv)
1105 struct kvaser_msg *msg;
1106 int rc;
1108 msg = kmalloc(sizeof(*msg), GFP_KERNEL);
1109 if (!msg)
1110 return -ENOMEM;
1112 msg->id = CMD_SET_CTRL_MODE;
1113 msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_ctrl_mode);
1114 msg->u.ctrl_mode.tid = 0xff;
1115 msg->u.ctrl_mode.channel = priv->channel;
1117 if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
1118 msg->u.ctrl_mode.ctrl_mode = KVASER_CTRL_MODE_SILENT;
1119 else
1120 msg->u.ctrl_mode.ctrl_mode = KVASER_CTRL_MODE_NORMAL;
1122 rc = kvaser_usb_send_msg(priv->dev, msg);
1124 kfree(msg);
1125 return rc;
1128 static int kvaser_usb_start_chip(struct kvaser_usb_net_priv *priv)
1130 int err;
1132 init_completion(&priv->start_comp);
1134 err = kvaser_usb_send_simple_msg(priv->dev, CMD_START_CHIP,
1135 priv->channel);
1136 if (err)
1137 return err;
1139 if (!wait_for_completion_timeout(&priv->start_comp,
1140 msecs_to_jiffies(START_TIMEOUT)))
1141 return -ETIMEDOUT;
1143 return 0;
1146 static int kvaser_usb_open(struct net_device *netdev)
1148 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1149 struct kvaser_usb *dev = priv->dev;
1150 int err;
1152 err = open_candev(netdev);
1153 if (err)
1154 return err;
1156 err = kvaser_usb_setup_rx_urbs(dev);
1157 if (err)
1158 goto error;
1160 err = kvaser_usb_set_opt_mode(priv);
1161 if (err)
1162 goto error;
1164 err = kvaser_usb_start_chip(priv);
1165 if (err) {
1166 netdev_warn(netdev, "Cannot start device, error %d\n", err);
1167 goto error;
1170 priv->can.state = CAN_STATE_ERROR_ACTIVE;
1172 return 0;
1174 error:
1175 close_candev(netdev);
1176 return err;
1179 static void kvaser_usb_reset_tx_urb_contexts(struct kvaser_usb_net_priv *priv)
1181 int i;
1183 priv->active_tx_contexts = 0;
1184 for (i = 0; i < MAX_TX_URBS; i++)
1185 priv->tx_contexts[i].echo_index = MAX_TX_URBS;
1188 /* This method might sleep. Do not call it in the atomic context
1189 * of URB completions.
1191 static void kvaser_usb_unlink_tx_urbs(struct kvaser_usb_net_priv *priv)
1193 usb_kill_anchored_urbs(&priv->tx_submitted);
1194 kvaser_usb_reset_tx_urb_contexts(priv);
1197 static void kvaser_usb_unlink_all_urbs(struct kvaser_usb *dev)
1199 int i;
1201 usb_kill_anchored_urbs(&dev->rx_submitted);
1203 for (i = 0; i < MAX_RX_URBS; i++)
1204 usb_free_coherent(dev->udev, RX_BUFFER_SIZE,
1205 dev->rxbuf[i],
1206 dev->rxbuf_dma[i]);
1208 for (i = 0; i < MAX_NET_DEVICES; i++) {
1209 struct kvaser_usb_net_priv *priv = dev->nets[i];
1211 if (priv)
1212 kvaser_usb_unlink_tx_urbs(priv);
1216 static int kvaser_usb_stop_chip(struct kvaser_usb_net_priv *priv)
1218 int err;
1220 init_completion(&priv->stop_comp);
1222 err = kvaser_usb_send_simple_msg(priv->dev, CMD_STOP_CHIP,
1223 priv->channel);
1224 if (err)
1225 return err;
1227 if (!wait_for_completion_timeout(&priv->stop_comp,
1228 msecs_to_jiffies(STOP_TIMEOUT)))
1229 return -ETIMEDOUT;
1231 return 0;
1234 static int kvaser_usb_flush_queue(struct kvaser_usb_net_priv *priv)
1236 struct kvaser_msg *msg;
1237 int rc;
1239 msg = kmalloc(sizeof(*msg), GFP_KERNEL);
1240 if (!msg)
1241 return -ENOMEM;
1243 msg->id = CMD_FLUSH_QUEUE;
1244 msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_flush_queue);
1245 msg->u.flush_queue.channel = priv->channel;
1246 msg->u.flush_queue.flags = 0x00;
1248 rc = kvaser_usb_send_msg(priv->dev, msg);
1250 kfree(msg);
1251 return rc;
1254 static int kvaser_usb_close(struct net_device *netdev)
1256 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1257 struct kvaser_usb *dev = priv->dev;
1258 int err;
1260 netif_stop_queue(netdev);
1262 err = kvaser_usb_flush_queue(priv);
1263 if (err)
1264 netdev_warn(netdev, "Cannot flush queue, error %d\n", err);
1266 if (kvaser_usb_send_simple_msg(dev, CMD_RESET_CHIP, priv->channel))
1267 netdev_warn(netdev, "Cannot reset card, error %d\n", err);
1269 err = kvaser_usb_stop_chip(priv);
1270 if (err)
1271 netdev_warn(netdev, "Cannot stop device, error %d\n", err);
1273 /* reset tx contexts */
1274 kvaser_usb_unlink_tx_urbs(priv);
1276 priv->can.state = CAN_STATE_STOPPED;
1277 close_candev(priv->netdev);
1279 return 0;
1282 static void kvaser_usb_write_bulk_callback(struct urb *urb)
1284 struct kvaser_usb_tx_urb_context *context = urb->context;
1285 struct kvaser_usb_net_priv *priv;
1286 struct net_device *netdev;
1288 if (WARN_ON(!context))
1289 return;
1291 priv = context->priv;
1292 netdev = priv->netdev;
1294 kfree(urb->transfer_buffer);
1296 if (!netif_device_present(netdev))
1297 return;
1299 if (urb->status)
1300 netdev_info(netdev, "Tx URB aborted (%d)\n", urb->status);
1303 static netdev_tx_t kvaser_usb_start_xmit(struct sk_buff *skb,
1304 struct net_device *netdev)
1306 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1307 struct kvaser_usb *dev = priv->dev;
1308 struct net_device_stats *stats = &netdev->stats;
1309 struct can_frame *cf = (struct can_frame *)skb->data;
1310 struct kvaser_usb_tx_urb_context *context = NULL;
1311 struct urb *urb;
1312 void *buf;
1313 struct kvaser_msg *msg;
1314 int i, err, ret = NETDEV_TX_OK;
1315 unsigned long flags;
1317 if (can_dropped_invalid_skb(netdev, skb))
1318 return NETDEV_TX_OK;
1320 urb = usb_alloc_urb(0, GFP_ATOMIC);
1321 if (!urb) {
1322 netdev_err(netdev, "No memory left for URBs\n");
1323 stats->tx_dropped++;
1324 dev_kfree_skb(skb);
1325 return NETDEV_TX_OK;
1328 buf = kmalloc(sizeof(struct kvaser_msg), GFP_ATOMIC);
1329 if (!buf) {
1330 stats->tx_dropped++;
1331 dev_kfree_skb(skb);
1332 goto freeurb;
1335 msg = buf;
1336 msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_tx_can);
1337 msg->u.tx_can.flags = 0;
1338 msg->u.tx_can.channel = priv->channel;
1340 if (cf->can_id & CAN_EFF_FLAG) {
1341 msg->id = CMD_TX_EXT_MESSAGE;
1342 msg->u.tx_can.msg[0] = (cf->can_id >> 24) & 0x1f;
1343 msg->u.tx_can.msg[1] = (cf->can_id >> 18) & 0x3f;
1344 msg->u.tx_can.msg[2] = (cf->can_id >> 14) & 0x0f;
1345 msg->u.tx_can.msg[3] = (cf->can_id >> 6) & 0xff;
1346 msg->u.tx_can.msg[4] = cf->can_id & 0x3f;
1347 } else {
1348 msg->id = CMD_TX_STD_MESSAGE;
1349 msg->u.tx_can.msg[0] = (cf->can_id >> 6) & 0x1f;
1350 msg->u.tx_can.msg[1] = cf->can_id & 0x3f;
1353 msg->u.tx_can.msg[5] = cf->can_dlc;
1354 memcpy(&msg->u.tx_can.msg[6], cf->data, cf->can_dlc);
1356 if (cf->can_id & CAN_RTR_FLAG)
1357 msg->u.tx_can.flags |= MSG_FLAG_REMOTE_FRAME;
1359 spin_lock_irqsave(&priv->tx_contexts_lock, flags);
1360 for (i = 0; i < ARRAY_SIZE(priv->tx_contexts); i++) {
1361 if (priv->tx_contexts[i].echo_index == MAX_TX_URBS) {
1362 context = &priv->tx_contexts[i];
1364 context->echo_index = i;
1365 can_put_echo_skb(skb, netdev, context->echo_index);
1366 ++priv->active_tx_contexts;
1367 if (priv->active_tx_contexts >= MAX_TX_URBS)
1368 netif_stop_queue(netdev);
1370 break;
1373 spin_unlock_irqrestore(&priv->tx_contexts_lock, flags);
1375 /* This should never happen; it implies a flow control bug */
1376 if (!context) {
1377 netdev_warn(netdev, "cannot find free context\n");
1379 kfree(buf);
1380 ret = NETDEV_TX_BUSY;
1381 goto freeurb;
1384 context->priv = priv;
1385 context->dlc = cf->can_dlc;
1387 msg->u.tx_can.tid = context->echo_index;
1389 usb_fill_bulk_urb(urb, dev->udev,
1390 usb_sndbulkpipe(dev->udev,
1391 dev->bulk_out->bEndpointAddress),
1392 buf, msg->len,
1393 kvaser_usb_write_bulk_callback, context);
1394 usb_anchor_urb(urb, &priv->tx_submitted);
1396 err = usb_submit_urb(urb, GFP_ATOMIC);
1397 if (unlikely(err)) {
1398 spin_lock_irqsave(&priv->tx_contexts_lock, flags);
1400 can_free_echo_skb(netdev, context->echo_index);
1401 context->echo_index = MAX_TX_URBS;
1402 --priv->active_tx_contexts;
1403 netif_wake_queue(netdev);
1405 spin_unlock_irqrestore(&priv->tx_contexts_lock, flags);
1407 usb_unanchor_urb(urb);
1409 stats->tx_dropped++;
1411 if (err == -ENODEV)
1412 netif_device_detach(netdev);
1413 else
1414 netdev_warn(netdev, "Failed tx_urb %d\n", err);
1416 goto freeurb;
1419 ret = NETDEV_TX_OK;
1421 freeurb:
1422 usb_free_urb(urb);
1423 return ret;
1426 static const struct net_device_ops kvaser_usb_netdev_ops = {
1427 .ndo_open = kvaser_usb_open,
1428 .ndo_stop = kvaser_usb_close,
1429 .ndo_start_xmit = kvaser_usb_start_xmit,
1430 .ndo_change_mtu = can_change_mtu,
1433 static const struct can_bittiming_const kvaser_usb_bittiming_const = {
1434 .name = "kvaser_usb",
1435 .tseg1_min = KVASER_USB_TSEG1_MIN,
1436 .tseg1_max = KVASER_USB_TSEG1_MAX,
1437 .tseg2_min = KVASER_USB_TSEG2_MIN,
1438 .tseg2_max = KVASER_USB_TSEG2_MAX,
1439 .sjw_max = KVASER_USB_SJW_MAX,
1440 .brp_min = KVASER_USB_BRP_MIN,
1441 .brp_max = KVASER_USB_BRP_MAX,
1442 .brp_inc = KVASER_USB_BRP_INC,
1445 static int kvaser_usb_set_bittiming(struct net_device *netdev)
1447 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1448 struct can_bittiming *bt = &priv->can.bittiming;
1449 struct kvaser_usb *dev = priv->dev;
1450 struct kvaser_msg *msg;
1451 int rc;
1453 msg = kmalloc(sizeof(*msg), GFP_KERNEL);
1454 if (!msg)
1455 return -ENOMEM;
1457 msg->id = CMD_SET_BUS_PARAMS;
1458 msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_busparams);
1459 msg->u.busparams.channel = priv->channel;
1460 msg->u.busparams.tid = 0xff;
1461 msg->u.busparams.bitrate = cpu_to_le32(bt->bitrate);
1462 msg->u.busparams.sjw = bt->sjw;
1463 msg->u.busparams.tseg1 = bt->prop_seg + bt->phase_seg1;
1464 msg->u.busparams.tseg2 = bt->phase_seg2;
1466 if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
1467 msg->u.busparams.no_samp = 3;
1468 else
1469 msg->u.busparams.no_samp = 1;
1471 rc = kvaser_usb_send_msg(dev, msg);
1473 kfree(msg);
1474 return rc;
1477 static int kvaser_usb_set_mode(struct net_device *netdev,
1478 enum can_mode mode)
1480 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1481 int err;
1483 switch (mode) {
1484 case CAN_MODE_START:
1485 err = kvaser_usb_simple_msg_async(priv, CMD_START_CHIP);
1486 if (err)
1487 return err;
1488 break;
1489 default:
1490 return -EOPNOTSUPP;
1493 return 0;
1496 static int kvaser_usb_get_berr_counter(const struct net_device *netdev,
1497 struct can_berr_counter *bec)
1499 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1501 *bec = priv->bec;
1503 return 0;
1506 static void kvaser_usb_remove_interfaces(struct kvaser_usb *dev)
1508 int i;
1510 for (i = 0; i < dev->nchannels; i++) {
1511 if (!dev->nets[i])
1512 continue;
1514 unregister_netdev(dev->nets[i]->netdev);
1517 kvaser_usb_unlink_all_urbs(dev);
1519 for (i = 0; i < dev->nchannels; i++) {
1520 if (!dev->nets[i])
1521 continue;
1523 free_candev(dev->nets[i]->netdev);
1527 static int kvaser_usb_init_one(struct usb_interface *intf,
1528 const struct usb_device_id *id, int channel)
1530 struct kvaser_usb *dev = usb_get_intfdata(intf);
1531 struct net_device *netdev;
1532 struct kvaser_usb_net_priv *priv;
1533 int err;
1535 err = kvaser_usb_send_simple_msg(dev, CMD_RESET_CHIP, channel);
1536 if (err)
1537 return err;
1539 netdev = alloc_candev(sizeof(*priv), MAX_TX_URBS);
1540 if (!netdev) {
1541 dev_err(&intf->dev, "Cannot alloc candev\n");
1542 return -ENOMEM;
1545 priv = netdev_priv(netdev);
1547 init_usb_anchor(&priv->tx_submitted);
1548 init_completion(&priv->start_comp);
1549 init_completion(&priv->stop_comp);
1551 priv->dev = dev;
1552 priv->netdev = netdev;
1553 priv->channel = channel;
1555 spin_lock_init(&priv->tx_contexts_lock);
1556 kvaser_usb_reset_tx_urb_contexts(priv);
1558 priv->can.state = CAN_STATE_STOPPED;
1559 priv->can.clock.freq = CAN_USB_CLOCK;
1560 priv->can.bittiming_const = &kvaser_usb_bittiming_const;
1561 priv->can.do_set_bittiming = kvaser_usb_set_bittiming;
1562 priv->can.do_set_mode = kvaser_usb_set_mode;
1563 if (id->driver_info & KVASER_HAS_TXRX_ERRORS)
1564 priv->can.do_get_berr_counter = kvaser_usb_get_berr_counter;
1565 priv->can.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES;
1566 if (id->driver_info & KVASER_HAS_SILENT_MODE)
1567 priv->can.ctrlmode_supported |= CAN_CTRLMODE_LISTENONLY;
1569 netdev->flags |= IFF_ECHO;
1571 netdev->netdev_ops = &kvaser_usb_netdev_ops;
1573 SET_NETDEV_DEV(netdev, &intf->dev);
1574 netdev->dev_id = channel;
1576 dev->nets[channel] = priv;
1578 err = register_candev(netdev);
1579 if (err) {
1580 dev_err(&intf->dev, "Failed to register can device\n");
1581 free_candev(netdev);
1582 dev->nets[channel] = NULL;
1583 return err;
1586 netdev_dbg(netdev, "device registered\n");
1588 return 0;
1591 static int kvaser_usb_get_endpoints(const struct usb_interface *intf,
1592 struct usb_endpoint_descriptor **in,
1593 struct usb_endpoint_descriptor **out)
1595 const struct usb_host_interface *iface_desc;
1596 struct usb_endpoint_descriptor *endpoint;
1597 int i;
1599 iface_desc = &intf->altsetting[0];
1601 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1602 endpoint = &iface_desc->endpoint[i].desc;
1604 if (!*in && usb_endpoint_is_bulk_in(endpoint))
1605 *in = endpoint;
1607 if (!*out && usb_endpoint_is_bulk_out(endpoint))
1608 *out = endpoint;
1610 /* use first bulk endpoint for in and out */
1611 if (*in && *out)
1612 return 0;
1615 return -ENODEV;
1618 static int kvaser_usb_probe(struct usb_interface *intf,
1619 const struct usb_device_id *id)
1621 struct kvaser_usb *dev;
1622 int err = -ENOMEM;
1623 int i, retry = 3;
1625 dev = devm_kzalloc(&intf->dev, sizeof(*dev), GFP_KERNEL);
1626 if (!dev)
1627 return -ENOMEM;
1629 err = kvaser_usb_get_endpoints(intf, &dev->bulk_in, &dev->bulk_out);
1630 if (err) {
1631 dev_err(&intf->dev, "Cannot get usb endpoint(s)");
1632 return err;
1635 dev->udev = interface_to_usbdev(intf);
1637 init_usb_anchor(&dev->rx_submitted);
1639 usb_set_intfdata(intf, dev);
1641 /* On some x86 laptops, plugging a Kvaser device again after
1642 * an unplug makes the firmware always ignore the very first
1643 * command. For such a case, provide some room for retries
1644 * instead of completely exiting the driver.
1646 do {
1647 err = kvaser_usb_get_software_info(dev);
1648 } while (--retry && err == -ETIMEDOUT);
1650 if (err) {
1651 dev_err(&intf->dev,
1652 "Cannot get software infos, error %d\n", err);
1653 return err;
1656 err = kvaser_usb_get_card_info(dev);
1657 if (err) {
1658 dev_err(&intf->dev,
1659 "Cannot get card infos, error %d\n", err);
1660 return err;
1663 dev_dbg(&intf->dev, "Firmware version: %d.%d.%d\n",
1664 ((dev->fw_version >> 24) & 0xff),
1665 ((dev->fw_version >> 16) & 0xff),
1666 (dev->fw_version & 0xffff));
1668 for (i = 0; i < dev->nchannels; i++) {
1669 err = kvaser_usb_init_one(intf, id, i);
1670 if (err) {
1671 kvaser_usb_remove_interfaces(dev);
1672 return err;
1676 return 0;
1679 static void kvaser_usb_disconnect(struct usb_interface *intf)
1681 struct kvaser_usb *dev = usb_get_intfdata(intf);
1683 usb_set_intfdata(intf, NULL);
1685 if (!dev)
1686 return;
1688 kvaser_usb_remove_interfaces(dev);
1691 static struct usb_driver kvaser_usb_driver = {
1692 .name = "kvaser_usb",
1693 .probe = kvaser_usb_probe,
1694 .disconnect = kvaser_usb_disconnect,
1695 .id_table = kvaser_usb_table,
1698 module_usb_driver(kvaser_usb_driver);
1700 MODULE_AUTHOR("Olivier Sobrie <olivier@sobrie.be>");
1701 MODULE_DESCRIPTION("CAN driver for Kvaser CAN/USB devices");
1702 MODULE_LICENSE("GPL v2");