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.
5 *
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
9 *
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>
13 */
14
15 #include <linux/kernel.h>
16 #include <linux/completion.h>
17 #include <linux/module.h>
18 #include <linux/netdevice.h>
19 #include <linux/usb.h>
20
21 #include <linux/can.h>
22 #include <linux/can/dev.h>
23 #include <linux/can/error.h>
24
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
34
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
57 #define USB_LEAF_LITE_V2_PRODUCT_ID 288
58 #define USB_MINI_PCIE_HS_PRODUCT_ID 289
59
60 /* USB devices features */
61 #define KVASER_HAS_SILENT_MODE BIT(0)
62 #define KVASER_HAS_TXRX_ERRORS BIT(1)
63
64 /* Message header size */
65 #define MSG_HEADER_LEN 2
66
67 /* Can message flags */
68 #define MSG_FLAG_ERROR_FRAME BIT(0)
69 #define MSG_FLAG_OVERRUN BIT(1)
70 #define MSG_FLAG_NERR BIT(2)
71 #define MSG_FLAG_WAKEUP BIT(3)
72 #define MSG_FLAG_REMOTE_FRAME BIT(4)
73 #define MSG_FLAG_RESERVED BIT(5)
74 #define MSG_FLAG_TX_ACK BIT(6)
75 #define MSG_FLAG_TX_REQUEST BIT(7)
76
77 /* Can states */
78 #define M16C_STATE_BUS_RESET BIT(0)
79 #define M16C_STATE_BUS_ERROR BIT(4)
80 #define M16C_STATE_BUS_PASSIVE BIT(5)
81 #define M16C_STATE_BUS_OFF BIT(6)
82
83 /* Can msg ids */
84 #define CMD_RX_STD_MESSAGE 12
85 #define CMD_TX_STD_MESSAGE 13
86 #define CMD_RX_EXT_MESSAGE 14
87 #define CMD_TX_EXT_MESSAGE 15
88 #define CMD_SET_BUS_PARAMS 16
89 #define CMD_GET_BUS_PARAMS 17
90 #define CMD_GET_BUS_PARAMS_REPLY 18
91 #define CMD_GET_CHIP_STATE 19
92 #define CMD_CHIP_STATE_EVENT 20
93 #define CMD_SET_CTRL_MODE 21
94 #define CMD_GET_CTRL_MODE 22
95 #define CMD_GET_CTRL_MODE_REPLY 23
96 #define CMD_RESET_CHIP 24
97 #define CMD_RESET_CARD 25
98 #define CMD_START_CHIP 26
99 #define CMD_START_CHIP_REPLY 27
100 #define CMD_STOP_CHIP 28
101 #define CMD_STOP_CHIP_REPLY 29
102 #define CMD_GET_CARD_INFO2 32
103 #define CMD_GET_CARD_INFO 34
104 #define CMD_GET_CARD_INFO_REPLY 35
105 #define CMD_GET_SOFTWARE_INFO 38
106 #define CMD_GET_SOFTWARE_INFO_REPLY 39
107 #define CMD_ERROR_EVENT 45
108 #define CMD_FLUSH_QUEUE 48
109 #define CMD_RESET_ERROR_COUNTER 49
110 #define CMD_TX_ACKNOWLEDGE 50
111 #define CMD_CAN_ERROR_EVENT 51
112 #define CMD_USB_THROTTLE 77
113 #define CMD_LOG_MESSAGE 106
114
115 /* error factors */
116 #define M16C_EF_ACKE BIT(0)
117 #define M16C_EF_CRCE BIT(1)
118 #define M16C_EF_FORME BIT(2)
119 #define M16C_EF_STFE BIT(3)
120 #define M16C_EF_BITE0 BIT(4)
121 #define M16C_EF_BITE1 BIT(5)
122 #define M16C_EF_RCVE BIT(6)
123 #define M16C_EF_TRE BIT(7)
124
125 /* bittiming parameters */
126 #define KVASER_USB_TSEG1_MIN 1
127 #define KVASER_USB_TSEG1_MAX 16
128 #define KVASER_USB_TSEG2_MIN 1
129 #define KVASER_USB_TSEG2_MAX 8
130 #define KVASER_USB_SJW_MAX 4
131 #define KVASER_USB_BRP_MIN 1
132 #define KVASER_USB_BRP_MAX 64
133 #define KVASER_USB_BRP_INC 1
134
135 /* ctrl modes */
136 #define KVASER_CTRL_MODE_NORMAL 1
137 #define KVASER_CTRL_MODE_SILENT 2
138 #define KVASER_CTRL_MODE_SELFRECEPTION 3
139 #define KVASER_CTRL_MODE_OFF 4
140
141 /* log message */
142 #define KVASER_EXTENDED_FRAME BIT(31)
143
144 struct kvaser_msg_simple {
145 u8 tid;
146 u8 channel;
147 } __packed;
148
149 struct kvaser_msg_cardinfo {
150 u8 tid;
151 u8 nchannels;
152 __le32 serial_number;
153 __le32 padding;
154 __le32 clock_resolution;
155 __le32 mfgdate;
156 u8 ean[8];
157 u8 hw_revision;
158 u8 usb_hs_mode;
159 __le16 padding2;
160 } __packed;
161
162 struct kvaser_msg_cardinfo2 {
163 u8 tid;
164 u8 channel;
165 u8 pcb_id[24];
166 __le32 oem_unlock_code;
167 } __packed;
168
169 struct kvaser_msg_softinfo {
170 u8 tid;
171 u8 channel;
172 __le32 sw_options;
173 __le32 fw_version;
174 __le16 max_outstanding_tx;
175 __le16 padding[9];
176 } __packed;
177
178 struct kvaser_msg_busparams {
179 u8 tid;
180 u8 channel;
181 __le32 bitrate;
182 u8 tseg1;
183 u8 tseg2;
184 u8 sjw;
185 u8 no_samp;
186 } __packed;
187
188 struct kvaser_msg_tx_can {
189 u8 channel;
190 u8 tid;
191 u8 msg[14];
192 u8 padding;
193 u8 flags;
194 } __packed;
195
196 struct kvaser_msg_rx_can {
197 u8 channel;
198 u8 flag;
199 __le16 time[3];
200 u8 msg[14];
201 } __packed;
202
203 struct kvaser_msg_chip_state_event {
204 u8 tid;
205 u8 channel;
206 __le16 time[3];
207 u8 tx_errors_count;
208 u8 rx_errors_count;
209 u8 status;
210 u8 padding[3];
211 } __packed;
212
213 struct kvaser_msg_tx_acknowledge {
214 u8 channel;
215 u8 tid;
216 __le16 time[3];
217 u8 flags;
218 u8 time_offset;
219 } __packed;
220
221 struct kvaser_msg_error_event {
222 u8 tid;
223 u8 flags;
224 __le16 time[3];
225 u8 channel;
226 u8 padding;
227 u8 tx_errors_count;
228 u8 rx_errors_count;
229 u8 status;
230 u8 error_factor;
231 } __packed;
232
233 struct kvaser_msg_ctrl_mode {
234 u8 tid;
235 u8 channel;
236 u8 ctrl_mode;
237 u8 padding[3];
238 } __packed;
239
240 struct kvaser_msg_flush_queue {
241 u8 tid;
242 u8 channel;
243 u8 flags;
244 u8 padding[3];
245 } __packed;
246
247 struct kvaser_msg_log_message {
248 u8 channel;
249 u8 flags;
250 __le16 time[3];
251 u8 dlc;
252 u8 time_offset;
253 __le32 id;
254 u8 data[8];
255 } __packed;
256
257 struct kvaser_msg {
258 u8 len;
259 u8 id;
260 union {
261 struct kvaser_msg_simple simple;
262 struct kvaser_msg_cardinfo cardinfo;
263 struct kvaser_msg_cardinfo2 cardinfo2;
264 struct kvaser_msg_softinfo softinfo;
265 struct kvaser_msg_busparams busparams;
266 struct kvaser_msg_tx_can tx_can;
267 struct kvaser_msg_rx_can rx_can;
268 struct kvaser_msg_chip_state_event chip_state_event;
269 struct kvaser_msg_tx_acknowledge tx_acknowledge;
270 struct kvaser_msg_error_event error_event;
271 struct kvaser_msg_ctrl_mode ctrl_mode;
272 struct kvaser_msg_flush_queue flush_queue;
273 struct kvaser_msg_log_message log_message;
274 } u;
275 } __packed;
276
277 struct kvaser_usb_tx_urb_context {
278 struct kvaser_usb_net_priv *priv;
279 u32 echo_index;
280 int dlc;
281 };
282
283 struct kvaser_usb {
284 struct usb_device *udev;
285 struct kvaser_usb_net_priv *nets[MAX_NET_DEVICES];
286
287 struct usb_endpoint_descriptor *bulk_in, *bulk_out;
288 struct usb_anchor rx_submitted;
289
290 u32 fw_version;
291 unsigned int nchannels;
292
293 bool rxinitdone;
294 void *rxbuf[MAX_RX_URBS];
295 dma_addr_t rxbuf_dma[MAX_RX_URBS];
296 };
297
298 struct kvaser_usb_net_priv {
299 struct can_priv can;
300
301 atomic_t active_tx_urbs;
302 struct usb_anchor tx_submitted;
303 struct kvaser_usb_tx_urb_context tx_contexts[MAX_TX_URBS];
304
305 struct completion start_comp, stop_comp;
306
307 struct kvaser_usb *dev;
308 struct net_device *netdev;
309 int channel;
310
311 struct can_berr_counter bec;
312 };
313
314 static const struct usb_device_id kvaser_usb_table[] = {
315 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_DEVEL_PRODUCT_ID) },
316 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_PRODUCT_ID) },
317 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_PRODUCT_ID),
318 .driver_info = KVASER_HAS_TXRX_ERRORS |
319 KVASER_HAS_SILENT_MODE },
320 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_SPRO_PRODUCT_ID),
321 .driver_info = KVASER_HAS_TXRX_ERRORS |
322 KVASER_HAS_SILENT_MODE },
323 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_LS_PRODUCT_ID),
324 .driver_info = KVASER_HAS_TXRX_ERRORS |
325 KVASER_HAS_SILENT_MODE },
326 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_SWC_PRODUCT_ID),
327 .driver_info = KVASER_HAS_TXRX_ERRORS |
328 KVASER_HAS_SILENT_MODE },
329 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_LIN_PRODUCT_ID),
330 .driver_info = KVASER_HAS_TXRX_ERRORS |
331 KVASER_HAS_SILENT_MODE },
332 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_SPRO_LS_PRODUCT_ID),
333 .driver_info = KVASER_HAS_TXRX_ERRORS |
334 KVASER_HAS_SILENT_MODE },
335 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_SPRO_SWC_PRODUCT_ID),
336 .driver_info = KVASER_HAS_TXRX_ERRORS |
337 KVASER_HAS_SILENT_MODE },
338 { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO2_DEVEL_PRODUCT_ID),
339 .driver_info = KVASER_HAS_TXRX_ERRORS |
340 KVASER_HAS_SILENT_MODE },
341 { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO2_HSHS_PRODUCT_ID),
342 .driver_info = KVASER_HAS_TXRX_ERRORS |
343 KVASER_HAS_SILENT_MODE },
344 { USB_DEVICE(KVASER_VENDOR_ID, USB_UPRO_HSHS_PRODUCT_ID),
345 .driver_info = KVASER_HAS_TXRX_ERRORS },
346 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_GI_PRODUCT_ID) },
347 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_OBDII_PRODUCT_ID),
348 .driver_info = KVASER_HAS_TXRX_ERRORS |
349 KVASER_HAS_SILENT_MODE },
350 { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO2_HSLS_PRODUCT_ID),
351 .driver_info = KVASER_HAS_TXRX_ERRORS },
352 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_CH_PRODUCT_ID),
353 .driver_info = KVASER_HAS_TXRX_ERRORS },
354 { USB_DEVICE(KVASER_VENDOR_ID, USB_BLACKBIRD_SPRO_PRODUCT_ID),
355 .driver_info = KVASER_HAS_TXRX_ERRORS },
356 { USB_DEVICE(KVASER_VENDOR_ID, USB_OEM_MERCURY_PRODUCT_ID),
357 .driver_info = KVASER_HAS_TXRX_ERRORS },
358 { USB_DEVICE(KVASER_VENDOR_ID, USB_OEM_LEAF_PRODUCT_ID),
359 .driver_info = KVASER_HAS_TXRX_ERRORS },
360 { USB_DEVICE(KVASER_VENDOR_ID, USB_CAN_R_PRODUCT_ID),
361 .driver_info = KVASER_HAS_TXRX_ERRORS },
362 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_V2_PRODUCT_ID) },
363 { USB_DEVICE(KVASER_VENDOR_ID, USB_MINI_PCIE_HS_PRODUCT_ID) },
364 { }
365 };
366 MODULE_DEVICE_TABLE(usb, kvaser_usb_table);
367
kvaser_usb_send_msg(const struct kvaser_usb * dev,struct kvaser_msg * msg)368 static inline int kvaser_usb_send_msg(const struct kvaser_usb *dev,
369 struct kvaser_msg *msg)
370 {
371 int actual_len;
372
373 return usb_bulk_msg(dev->udev,
374 usb_sndbulkpipe(dev->udev,
375 dev->bulk_out->bEndpointAddress),
376 msg, msg->len, &actual_len,
377 USB_SEND_TIMEOUT);
378 }
379
kvaser_usb_wait_msg(const struct kvaser_usb * dev,u8 id,struct kvaser_msg * msg)380 static int kvaser_usb_wait_msg(const struct kvaser_usb *dev, u8 id,
381 struct kvaser_msg *msg)
382 {
383 struct kvaser_msg *tmp;
384 void *buf;
385 int actual_len;
386 int err;
387 int pos;
388 unsigned long to = jiffies + msecs_to_jiffies(USB_RECV_TIMEOUT);
389
390 buf = kzalloc(RX_BUFFER_SIZE, GFP_KERNEL);
391 if (!buf)
392 return -ENOMEM;
393
394 do {
395 err = usb_bulk_msg(dev->udev,
396 usb_rcvbulkpipe(dev->udev,
397 dev->bulk_in->bEndpointAddress),
398 buf, RX_BUFFER_SIZE, &actual_len,
399 USB_RECV_TIMEOUT);
400 if (err < 0)
401 goto end;
402
403 pos = 0;
404 while (pos <= actual_len - MSG_HEADER_LEN) {
405 tmp = buf + pos;
406
407 /* Handle messages crossing the USB endpoint max packet
408 * size boundary. Check kvaser_usb_read_bulk_callback()
409 * for further details.
410 */
411 if (tmp->len == 0) {
412 pos = round_up(pos,
413 dev->bulk_in->wMaxPacketSize);
414 continue;
415 }
416
417 if (pos + tmp->len > actual_len) {
418 dev_err_ratelimited(dev->udev->dev.parent,
419 "Format error\n");
420 break;
421 }
422
423 if (tmp->id == id) {
424 memcpy(msg, tmp, tmp->len);
425 goto end;
426 }
427
428 pos += tmp->len;
429 }
430 } while (time_before(jiffies, to));
431
432 err = -EINVAL;
433
434 end:
435 kfree(buf);
436
437 return err;
438 }
439
kvaser_usb_send_simple_msg(const struct kvaser_usb * dev,u8 msg_id,int channel)440 static int kvaser_usb_send_simple_msg(const struct kvaser_usb *dev,
441 u8 msg_id, int channel)
442 {
443 struct kvaser_msg *msg;
444 int rc;
445
446 msg = kmalloc(sizeof(*msg), GFP_KERNEL);
447 if (!msg)
448 return -ENOMEM;
449
450 msg->id = msg_id;
451 msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_simple);
452 msg->u.simple.channel = channel;
453 msg->u.simple.tid = 0xff;
454
455 rc = kvaser_usb_send_msg(dev, msg);
456
457 kfree(msg);
458 return rc;
459 }
460
kvaser_usb_get_software_info(struct kvaser_usb * dev)461 static int kvaser_usb_get_software_info(struct kvaser_usb *dev)
462 {
463 struct kvaser_msg msg;
464 int err;
465
466 err = kvaser_usb_send_simple_msg(dev, CMD_GET_SOFTWARE_INFO, 0);
467 if (err)
468 return err;
469
470 err = kvaser_usb_wait_msg(dev, CMD_GET_SOFTWARE_INFO_REPLY, &msg);
471 if (err)
472 return err;
473
474 dev->fw_version = le32_to_cpu(msg.u.softinfo.fw_version);
475
476 return 0;
477 }
478
kvaser_usb_get_card_info(struct kvaser_usb * dev)479 static int kvaser_usb_get_card_info(struct kvaser_usb *dev)
480 {
481 struct kvaser_msg msg;
482 int err;
483
484 err = kvaser_usb_send_simple_msg(dev, CMD_GET_CARD_INFO, 0);
485 if (err)
486 return err;
487
488 err = kvaser_usb_wait_msg(dev, CMD_GET_CARD_INFO_REPLY, &msg);
489 if (err)
490 return err;
491
492 dev->nchannels = msg.u.cardinfo.nchannels;
493 if (dev->nchannels > MAX_NET_DEVICES)
494 return -EINVAL;
495
496 return 0;
497 }
498
kvaser_usb_tx_acknowledge(const struct kvaser_usb * dev,const struct kvaser_msg * msg)499 static void kvaser_usb_tx_acknowledge(const struct kvaser_usb *dev,
500 const struct kvaser_msg *msg)
501 {
502 struct net_device_stats *stats;
503 struct kvaser_usb_tx_urb_context *context;
504 struct kvaser_usb_net_priv *priv;
505 struct sk_buff *skb;
506 struct can_frame *cf;
507 u8 channel = msg->u.tx_acknowledge.channel;
508 u8 tid = msg->u.tx_acknowledge.tid;
509
510 if (channel >= dev->nchannels) {
511 dev_err(dev->udev->dev.parent,
512 "Invalid channel number (%d)\n", channel);
513 return;
514 }
515
516 priv = dev->nets[channel];
517
518 if (!netif_device_present(priv->netdev))
519 return;
520
521 stats = &priv->netdev->stats;
522
523 context = &priv->tx_contexts[tid % MAX_TX_URBS];
524
525 /* Sometimes the state change doesn't come after a bus-off event */
526 if (priv->can.restart_ms &&
527 (priv->can.state >= CAN_STATE_BUS_OFF)) {
528 skb = alloc_can_err_skb(priv->netdev, &cf);
529 if (skb) {
530 cf->can_id |= CAN_ERR_RESTARTED;
531 netif_rx(skb);
532
533 stats->rx_packets++;
534 stats->rx_bytes += cf->can_dlc;
535 } else {
536 netdev_err(priv->netdev,
537 "No memory left for err_skb\n");
538 }
539
540 priv->can.can_stats.restarts++;
541 netif_carrier_on(priv->netdev);
542
543 priv->can.state = CAN_STATE_ERROR_ACTIVE;
544 }
545
546 stats->tx_packets++;
547 stats->tx_bytes += context->dlc;
548 can_get_echo_skb(priv->netdev, context->echo_index);
549
550 context->echo_index = MAX_TX_URBS;
551 atomic_dec(&priv->active_tx_urbs);
552
553 netif_wake_queue(priv->netdev);
554 }
555
kvaser_usb_simple_msg_callback(struct urb * urb)556 static void kvaser_usb_simple_msg_callback(struct urb *urb)
557 {
558 struct net_device *netdev = urb->context;
559
560 kfree(urb->transfer_buffer);
561
562 if (urb->status)
563 netdev_warn(netdev, "urb status received: %d\n",
564 urb->status);
565 }
566
kvaser_usb_simple_msg_async(struct kvaser_usb_net_priv * priv,u8 msg_id)567 static int kvaser_usb_simple_msg_async(struct kvaser_usb_net_priv *priv,
568 u8 msg_id)
569 {
570 struct kvaser_usb *dev = priv->dev;
571 struct net_device *netdev = priv->netdev;
572 struct kvaser_msg *msg;
573 struct urb *urb;
574 void *buf;
575 int err;
576
577 urb = usb_alloc_urb(0, GFP_ATOMIC);
578 if (!urb) {
579 netdev_err(netdev, "No memory left for URBs\n");
580 return -ENOMEM;
581 }
582
583 buf = kmalloc(sizeof(struct kvaser_msg), GFP_ATOMIC);
584 if (!buf) {
585 usb_free_urb(urb);
586 return -ENOMEM;
587 }
588
589 msg = (struct kvaser_msg *)buf;
590 msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_simple);
591 msg->id = msg_id;
592 msg->u.simple.channel = priv->channel;
593
594 usb_fill_bulk_urb(urb, dev->udev,
595 usb_sndbulkpipe(dev->udev,
596 dev->bulk_out->bEndpointAddress),
597 buf, msg->len,
598 kvaser_usb_simple_msg_callback, netdev);
599 usb_anchor_urb(urb, &priv->tx_submitted);
600
601 err = usb_submit_urb(urb, GFP_ATOMIC);
602 if (err) {
603 netdev_err(netdev, "Error transmitting URB\n");
604 usb_unanchor_urb(urb);
605 usb_free_urb(urb);
606 kfree(buf);
607 return err;
608 }
609
610 usb_free_urb(urb);
611
612 return 0;
613 }
614
kvaser_usb_unlink_tx_urbs(struct kvaser_usb_net_priv * priv)615 static void kvaser_usb_unlink_tx_urbs(struct kvaser_usb_net_priv *priv)
616 {
617 int i;
618
619 usb_kill_anchored_urbs(&priv->tx_submitted);
620 atomic_set(&priv->active_tx_urbs, 0);
621
622 for (i = 0; i < MAX_TX_URBS; i++)
623 priv->tx_contexts[i].echo_index = MAX_TX_URBS;
624 }
625
kvaser_usb_rx_error(const struct kvaser_usb * dev,const struct kvaser_msg * msg)626 static void kvaser_usb_rx_error(const struct kvaser_usb *dev,
627 const struct kvaser_msg *msg)
628 {
629 struct can_frame *cf;
630 struct sk_buff *skb;
631 struct net_device_stats *stats;
632 struct kvaser_usb_net_priv *priv;
633 unsigned int new_state;
634 u8 channel, status, txerr, rxerr, error_factor;
635
636 switch (msg->id) {
637 case CMD_CAN_ERROR_EVENT:
638 channel = msg->u.error_event.channel;
639 status = msg->u.error_event.status;
640 txerr = msg->u.error_event.tx_errors_count;
641 rxerr = msg->u.error_event.rx_errors_count;
642 error_factor = msg->u.error_event.error_factor;
643 break;
644 case CMD_LOG_MESSAGE:
645 channel = msg->u.log_message.channel;
646 status = msg->u.log_message.data[0];
647 txerr = msg->u.log_message.data[2];
648 rxerr = msg->u.log_message.data[3];
649 error_factor = msg->u.log_message.data[1];
650 break;
651 case CMD_CHIP_STATE_EVENT:
652 channel = msg->u.chip_state_event.channel;
653 status = msg->u.chip_state_event.status;
654 txerr = msg->u.chip_state_event.tx_errors_count;
655 rxerr = msg->u.chip_state_event.rx_errors_count;
656 error_factor = 0;
657 break;
658 default:
659 dev_err(dev->udev->dev.parent, "Invalid msg id (%d)\n",
660 msg->id);
661 return;
662 }
663
664 if (channel >= dev->nchannels) {
665 dev_err(dev->udev->dev.parent,
666 "Invalid channel number (%d)\n", channel);
667 return;
668 }
669
670 priv = dev->nets[channel];
671 stats = &priv->netdev->stats;
672
673 skb = alloc_can_err_skb(priv->netdev, &cf);
674 if (!skb) {
675 stats->rx_dropped++;
676 return;
677 }
678
679 new_state = priv->can.state;
680
681 netdev_dbg(priv->netdev, "Error status: 0x%02x\n", status);
682
683 if (status & (M16C_STATE_BUS_OFF | M16C_STATE_BUS_RESET)) {
684 cf->can_id |= CAN_ERR_BUSOFF;
685
686 priv->can.can_stats.bus_off++;
687 if (!priv->can.restart_ms)
688 kvaser_usb_simple_msg_async(priv, CMD_STOP_CHIP);
689
690 netif_carrier_off(priv->netdev);
691
692 new_state = CAN_STATE_BUS_OFF;
693 } else if (status & M16C_STATE_BUS_PASSIVE) {
694 if (priv->can.state != CAN_STATE_ERROR_PASSIVE) {
695 cf->can_id |= CAN_ERR_CRTL;
696
697 if (txerr || rxerr)
698 cf->data[1] = (txerr > rxerr)
699 ? CAN_ERR_CRTL_TX_PASSIVE
700 : CAN_ERR_CRTL_RX_PASSIVE;
701 else
702 cf->data[1] = CAN_ERR_CRTL_TX_PASSIVE |
703 CAN_ERR_CRTL_RX_PASSIVE;
704
705 priv->can.can_stats.error_passive++;
706 }
707
708 new_state = CAN_STATE_ERROR_PASSIVE;
709 } else if (status & M16C_STATE_BUS_ERROR) {
710 if ((priv->can.state < CAN_STATE_ERROR_WARNING) &&
711 ((txerr >= 96) || (rxerr >= 96))) {
712 cf->can_id |= CAN_ERR_CRTL;
713 cf->data[1] = (txerr > rxerr)
714 ? CAN_ERR_CRTL_TX_WARNING
715 : CAN_ERR_CRTL_RX_WARNING;
716
717 priv->can.can_stats.error_warning++;
718 new_state = CAN_STATE_ERROR_WARNING;
719 } else if ((priv->can.state > CAN_STATE_ERROR_ACTIVE) &&
720 ((txerr < 96) && (rxerr < 96))) {
721 cf->can_id |= CAN_ERR_PROT;
722 cf->data[2] = CAN_ERR_PROT_ACTIVE;
723
724 new_state = CAN_STATE_ERROR_ACTIVE;
725 }
726 }
727
728 if (!status) {
729 cf->can_id |= CAN_ERR_PROT;
730 cf->data[2] = CAN_ERR_PROT_ACTIVE;
731
732 new_state = CAN_STATE_ERROR_ACTIVE;
733 }
734
735 if (priv->can.restart_ms &&
736 (priv->can.state >= CAN_STATE_BUS_OFF) &&
737 (new_state < CAN_STATE_BUS_OFF)) {
738 cf->can_id |= CAN_ERR_RESTARTED;
739 netif_carrier_on(priv->netdev);
740
741 priv->can.can_stats.restarts++;
742 }
743
744 if (error_factor) {
745 priv->can.can_stats.bus_error++;
746 stats->rx_errors++;
747
748 cf->can_id |= CAN_ERR_BUSERROR | CAN_ERR_PROT;
749
750 if (error_factor & M16C_EF_ACKE)
751 cf->data[3] |= (CAN_ERR_PROT_LOC_ACK);
752 if (error_factor & M16C_EF_CRCE)
753 cf->data[3] |= (CAN_ERR_PROT_LOC_CRC_SEQ |
754 CAN_ERR_PROT_LOC_CRC_DEL);
755 if (error_factor & M16C_EF_FORME)
756 cf->data[2] |= CAN_ERR_PROT_FORM;
757 if (error_factor & M16C_EF_STFE)
758 cf->data[2] |= CAN_ERR_PROT_STUFF;
759 if (error_factor & M16C_EF_BITE0)
760 cf->data[2] |= CAN_ERR_PROT_BIT0;
761 if (error_factor & M16C_EF_BITE1)
762 cf->data[2] |= CAN_ERR_PROT_BIT1;
763 if (error_factor & M16C_EF_TRE)
764 cf->data[2] |= CAN_ERR_PROT_TX;
765 }
766
767 cf->data[6] = txerr;
768 cf->data[7] = rxerr;
769
770 priv->bec.txerr = txerr;
771 priv->bec.rxerr = rxerr;
772
773 priv->can.state = new_state;
774
775 netif_rx(skb);
776
777 stats->rx_packets++;
778 stats->rx_bytes += cf->can_dlc;
779 }
780
kvaser_usb_rx_can_err(const struct kvaser_usb_net_priv * priv,const struct kvaser_msg * msg)781 static void kvaser_usb_rx_can_err(const struct kvaser_usb_net_priv *priv,
782 const struct kvaser_msg *msg)
783 {
784 struct can_frame *cf;
785 struct sk_buff *skb;
786 struct net_device_stats *stats = &priv->netdev->stats;
787
788 if (msg->u.rx_can.flag & (MSG_FLAG_ERROR_FRAME |
789 MSG_FLAG_NERR)) {
790 netdev_err(priv->netdev, "Unknow error (flags: 0x%02x)\n",
791 msg->u.rx_can.flag);
792
793 stats->rx_errors++;
794 return;
795 }
796
797 if (msg->u.rx_can.flag & MSG_FLAG_OVERRUN) {
798 skb = alloc_can_err_skb(priv->netdev, &cf);
799 if (!skb) {
800 stats->rx_dropped++;
801 return;
802 }
803
804 cf->can_id |= CAN_ERR_CRTL;
805 cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
806
807 stats->rx_over_errors++;
808 stats->rx_errors++;
809
810 netif_rx(skb);
811
812 stats->rx_packets++;
813 stats->rx_bytes += cf->can_dlc;
814 }
815 }
816
kvaser_usb_rx_can_msg(const struct kvaser_usb * dev,const struct kvaser_msg * msg)817 static void kvaser_usb_rx_can_msg(const struct kvaser_usb *dev,
818 const struct kvaser_msg *msg)
819 {
820 struct kvaser_usb_net_priv *priv;
821 struct can_frame *cf;
822 struct sk_buff *skb;
823 struct net_device_stats *stats;
824 u8 channel = msg->u.rx_can.channel;
825
826 if (channel >= dev->nchannels) {
827 dev_err(dev->udev->dev.parent,
828 "Invalid channel number (%d)\n", channel);
829 return;
830 }
831
832 priv = dev->nets[channel];
833 stats = &priv->netdev->stats;
834
835 if ((msg->u.rx_can.flag & MSG_FLAG_ERROR_FRAME) &&
836 (msg->id == CMD_LOG_MESSAGE)) {
837 kvaser_usb_rx_error(dev, msg);
838 return;
839 } else if (msg->u.rx_can.flag & (MSG_FLAG_ERROR_FRAME |
840 MSG_FLAG_NERR |
841 MSG_FLAG_OVERRUN)) {
842 kvaser_usb_rx_can_err(priv, msg);
843 return;
844 } else if (msg->u.rx_can.flag & ~MSG_FLAG_REMOTE_FRAME) {
845 netdev_warn(priv->netdev,
846 "Unhandled frame (flags: 0x%02x)",
847 msg->u.rx_can.flag);
848 return;
849 }
850
851 skb = alloc_can_skb(priv->netdev, &cf);
852 if (!skb) {
853 stats->tx_dropped++;
854 return;
855 }
856
857 if (msg->id == CMD_LOG_MESSAGE) {
858 cf->can_id = le32_to_cpu(msg->u.log_message.id);
859 if (cf->can_id & KVASER_EXTENDED_FRAME)
860 cf->can_id &= CAN_EFF_MASK | CAN_EFF_FLAG;
861 else
862 cf->can_id &= CAN_SFF_MASK;
863
864 cf->can_dlc = get_can_dlc(msg->u.log_message.dlc);
865
866 if (msg->u.log_message.flags & MSG_FLAG_REMOTE_FRAME)
867 cf->can_id |= CAN_RTR_FLAG;
868 else
869 memcpy(cf->data, &msg->u.log_message.data,
870 cf->can_dlc);
871 } else {
872 cf->can_id = ((msg->u.rx_can.msg[0] & 0x1f) << 6) |
873 (msg->u.rx_can.msg[1] & 0x3f);
874
875 if (msg->id == CMD_RX_EXT_MESSAGE) {
876 cf->can_id <<= 18;
877 cf->can_id |= ((msg->u.rx_can.msg[2] & 0x0f) << 14) |
878 ((msg->u.rx_can.msg[3] & 0xff) << 6) |
879 (msg->u.rx_can.msg[4] & 0x3f);
880 cf->can_id |= CAN_EFF_FLAG;
881 }
882
883 cf->can_dlc = get_can_dlc(msg->u.rx_can.msg[5]);
884
885 if (msg->u.rx_can.flag & MSG_FLAG_REMOTE_FRAME)
886 cf->can_id |= CAN_RTR_FLAG;
887 else
888 memcpy(cf->data, &msg->u.rx_can.msg[6],
889 cf->can_dlc);
890 }
891
892 netif_rx(skb);
893
894 stats->rx_packets++;
895 stats->rx_bytes += cf->can_dlc;
896 }
897
kvaser_usb_start_chip_reply(const struct kvaser_usb * dev,const struct kvaser_msg * msg)898 static void kvaser_usb_start_chip_reply(const struct kvaser_usb *dev,
899 const struct kvaser_msg *msg)
900 {
901 struct kvaser_usb_net_priv *priv;
902 u8 channel = msg->u.simple.channel;
903
904 if (channel >= dev->nchannels) {
905 dev_err(dev->udev->dev.parent,
906 "Invalid channel number (%d)\n", channel);
907 return;
908 }
909
910 priv = dev->nets[channel];
911
912 if (completion_done(&priv->start_comp) &&
913 netif_queue_stopped(priv->netdev)) {
914 netif_wake_queue(priv->netdev);
915 } else {
916 netif_start_queue(priv->netdev);
917 complete(&priv->start_comp);
918 }
919 }
920
kvaser_usb_stop_chip_reply(const struct kvaser_usb * dev,const struct kvaser_msg * msg)921 static void kvaser_usb_stop_chip_reply(const struct kvaser_usb *dev,
922 const struct kvaser_msg *msg)
923 {
924 struct kvaser_usb_net_priv *priv;
925 u8 channel = msg->u.simple.channel;
926
927 if (channel >= dev->nchannels) {
928 dev_err(dev->udev->dev.parent,
929 "Invalid channel number (%d)\n", channel);
930 return;
931 }
932
933 priv = dev->nets[channel];
934
935 complete(&priv->stop_comp);
936 }
937
kvaser_usb_handle_message(const struct kvaser_usb * dev,const struct kvaser_msg * msg)938 static void kvaser_usb_handle_message(const struct kvaser_usb *dev,
939 const struct kvaser_msg *msg)
940 {
941 switch (msg->id) {
942 case CMD_START_CHIP_REPLY:
943 kvaser_usb_start_chip_reply(dev, msg);
944 break;
945
946 case CMD_STOP_CHIP_REPLY:
947 kvaser_usb_stop_chip_reply(dev, msg);
948 break;
949
950 case CMD_RX_STD_MESSAGE:
951 case CMD_RX_EXT_MESSAGE:
952 case CMD_LOG_MESSAGE:
953 kvaser_usb_rx_can_msg(dev, msg);
954 break;
955
956 case CMD_CHIP_STATE_EVENT:
957 case CMD_CAN_ERROR_EVENT:
958 kvaser_usb_rx_error(dev, msg);
959 break;
960
961 case CMD_TX_ACKNOWLEDGE:
962 kvaser_usb_tx_acknowledge(dev, msg);
963 break;
964
965 default:
966 dev_warn(dev->udev->dev.parent,
967 "Unhandled message (%d)\n", msg->id);
968 break;
969 }
970 }
971
kvaser_usb_read_bulk_callback(struct urb * urb)972 static void kvaser_usb_read_bulk_callback(struct urb *urb)
973 {
974 struct kvaser_usb *dev = urb->context;
975 struct kvaser_msg *msg;
976 int pos = 0;
977 int err, i;
978
979 switch (urb->status) {
980 case 0:
981 break;
982 case -ENOENT:
983 case -EPIPE:
984 case -EPROTO:
985 case -ESHUTDOWN:
986 return;
987 default:
988 dev_info(dev->udev->dev.parent, "Rx URB aborted (%d)\n",
989 urb->status);
990 goto resubmit_urb;
991 }
992
993 while (pos <= (int)(urb->actual_length - MSG_HEADER_LEN)) {
994 msg = urb->transfer_buffer + pos;
995
996 /* The Kvaser firmware can only read and write messages that
997 * does not cross the USB's endpoint wMaxPacketSize boundary.
998 * If a follow-up command crosses such boundary, firmware puts
999 * a placeholder zero-length command in its place then aligns
1000 * the real command to the next max packet size.
1001 *
1002 * Handle such cases or we're going to miss a significant
1003 * number of events in case of a heavy rx load on the bus.
1004 */
1005 if (msg->len == 0) {
1006 pos = round_up(pos, dev->bulk_in->wMaxPacketSize);
1007 continue;
1008 }
1009
1010 if (pos + msg->len > urb->actual_length) {
1011 dev_err_ratelimited(dev->udev->dev.parent,
1012 "Format error\n");
1013 break;
1014 }
1015
1016 kvaser_usb_handle_message(dev, msg);
1017 pos += msg->len;
1018 }
1019
1020 resubmit_urb:
1021 usb_fill_bulk_urb(urb, dev->udev,
1022 usb_rcvbulkpipe(dev->udev,
1023 dev->bulk_in->bEndpointAddress),
1024 urb->transfer_buffer, RX_BUFFER_SIZE,
1025 kvaser_usb_read_bulk_callback, dev);
1026
1027 err = usb_submit_urb(urb, GFP_ATOMIC);
1028 if (err == -ENODEV) {
1029 for (i = 0; i < dev->nchannels; i++) {
1030 if (!dev->nets[i])
1031 continue;
1032
1033 netif_device_detach(dev->nets[i]->netdev);
1034 }
1035 } else if (err) {
1036 dev_err(dev->udev->dev.parent,
1037 "Failed resubmitting read bulk urb: %d\n", err);
1038 }
1039
1040 return;
1041 }
1042
kvaser_usb_setup_rx_urbs(struct kvaser_usb * dev)1043 static int kvaser_usb_setup_rx_urbs(struct kvaser_usb *dev)
1044 {
1045 int i, err = 0;
1046
1047 if (dev->rxinitdone)
1048 return 0;
1049
1050 for (i = 0; i < MAX_RX_URBS; i++) {
1051 struct urb *urb = NULL;
1052 u8 *buf = NULL;
1053 dma_addr_t buf_dma;
1054
1055 urb = usb_alloc_urb(0, GFP_KERNEL);
1056 if (!urb) {
1057 dev_warn(dev->udev->dev.parent,
1058 "No memory left for URBs\n");
1059 err = -ENOMEM;
1060 break;
1061 }
1062
1063 buf = usb_alloc_coherent(dev->udev, RX_BUFFER_SIZE,
1064 GFP_KERNEL, &buf_dma);
1065 if (!buf) {
1066 dev_warn(dev->udev->dev.parent,
1067 "No memory left for USB buffer\n");
1068 usb_free_urb(urb);
1069 err = -ENOMEM;
1070 break;
1071 }
1072
1073 usb_fill_bulk_urb(urb, dev->udev,
1074 usb_rcvbulkpipe(dev->udev,
1075 dev->bulk_in->bEndpointAddress),
1076 buf, RX_BUFFER_SIZE,
1077 kvaser_usb_read_bulk_callback,
1078 dev);
1079 urb->transfer_dma = buf_dma;
1080 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1081 usb_anchor_urb(urb, &dev->rx_submitted);
1082
1083 err = usb_submit_urb(urb, GFP_KERNEL);
1084 if (err) {
1085 usb_unanchor_urb(urb);
1086 usb_free_coherent(dev->udev, RX_BUFFER_SIZE, buf,
1087 buf_dma);
1088 usb_free_urb(urb);
1089 break;
1090 }
1091
1092 dev->rxbuf[i] = buf;
1093 dev->rxbuf_dma[i] = buf_dma;
1094
1095 usb_free_urb(urb);
1096 }
1097
1098 if (i == 0) {
1099 dev_warn(dev->udev->dev.parent,
1100 "Cannot setup read URBs, error %d\n", err);
1101 return err;
1102 } else if (i < MAX_RX_URBS) {
1103 dev_warn(dev->udev->dev.parent,
1104 "RX performances may be slow\n");
1105 }
1106
1107 dev->rxinitdone = true;
1108
1109 return 0;
1110 }
1111
kvaser_usb_set_opt_mode(const struct kvaser_usb_net_priv * priv)1112 static int kvaser_usb_set_opt_mode(const struct kvaser_usb_net_priv *priv)
1113 {
1114 struct kvaser_msg *msg;
1115 int rc;
1116
1117 msg = kmalloc(sizeof(*msg), GFP_KERNEL);
1118 if (!msg)
1119 return -ENOMEM;
1120
1121 msg->id = CMD_SET_CTRL_MODE;
1122 msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_ctrl_mode);
1123 msg->u.ctrl_mode.tid = 0xff;
1124 msg->u.ctrl_mode.channel = priv->channel;
1125
1126 if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
1127 msg->u.ctrl_mode.ctrl_mode = KVASER_CTRL_MODE_SILENT;
1128 else
1129 msg->u.ctrl_mode.ctrl_mode = KVASER_CTRL_MODE_NORMAL;
1130
1131 rc = kvaser_usb_send_msg(priv->dev, msg);
1132
1133 kfree(msg);
1134 return rc;
1135 }
1136
kvaser_usb_start_chip(struct kvaser_usb_net_priv * priv)1137 static int kvaser_usb_start_chip(struct kvaser_usb_net_priv *priv)
1138 {
1139 int err;
1140
1141 init_completion(&priv->start_comp);
1142
1143 err = kvaser_usb_send_simple_msg(priv->dev, CMD_START_CHIP,
1144 priv->channel);
1145 if (err)
1146 return err;
1147
1148 if (!wait_for_completion_timeout(&priv->start_comp,
1149 msecs_to_jiffies(START_TIMEOUT)))
1150 return -ETIMEDOUT;
1151
1152 return 0;
1153 }
1154
kvaser_usb_open(struct net_device * netdev)1155 static int kvaser_usb_open(struct net_device *netdev)
1156 {
1157 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1158 struct kvaser_usb *dev = priv->dev;
1159 int err;
1160
1161 err = open_candev(netdev);
1162 if (err)
1163 return err;
1164
1165 err = kvaser_usb_setup_rx_urbs(dev);
1166 if (err)
1167 goto error;
1168
1169 err = kvaser_usb_set_opt_mode(priv);
1170 if (err)
1171 goto error;
1172
1173 err = kvaser_usb_start_chip(priv);
1174 if (err) {
1175 netdev_warn(netdev, "Cannot start device, error %d\n", err);
1176 goto error;
1177 }
1178
1179 priv->can.state = CAN_STATE_ERROR_ACTIVE;
1180
1181 return 0;
1182
1183 error:
1184 close_candev(netdev);
1185 return err;
1186 }
1187
kvaser_usb_unlink_all_urbs(struct kvaser_usb * dev)1188 static void kvaser_usb_unlink_all_urbs(struct kvaser_usb *dev)
1189 {
1190 int i;
1191
1192 usb_kill_anchored_urbs(&dev->rx_submitted);
1193
1194 for (i = 0; i < MAX_RX_URBS; i++)
1195 usb_free_coherent(dev->udev, RX_BUFFER_SIZE,
1196 dev->rxbuf[i],
1197 dev->rxbuf_dma[i]);
1198
1199 for (i = 0; i < MAX_NET_DEVICES; i++) {
1200 struct kvaser_usb_net_priv *priv = dev->nets[i];
1201
1202 if (priv)
1203 kvaser_usb_unlink_tx_urbs(priv);
1204 }
1205 }
1206
kvaser_usb_stop_chip(struct kvaser_usb_net_priv * priv)1207 static int kvaser_usb_stop_chip(struct kvaser_usb_net_priv *priv)
1208 {
1209 int err;
1210
1211 init_completion(&priv->stop_comp);
1212
1213 err = kvaser_usb_send_simple_msg(priv->dev, CMD_STOP_CHIP,
1214 priv->channel);
1215 if (err)
1216 return err;
1217
1218 if (!wait_for_completion_timeout(&priv->stop_comp,
1219 msecs_to_jiffies(STOP_TIMEOUT)))
1220 return -ETIMEDOUT;
1221
1222 return 0;
1223 }
1224
kvaser_usb_flush_queue(struct kvaser_usb_net_priv * priv)1225 static int kvaser_usb_flush_queue(struct kvaser_usb_net_priv *priv)
1226 {
1227 struct kvaser_msg *msg;
1228 int rc;
1229
1230 msg = kmalloc(sizeof(*msg), GFP_KERNEL);
1231 if (!msg)
1232 return -ENOMEM;
1233
1234 msg->id = CMD_FLUSH_QUEUE;
1235 msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_flush_queue);
1236 msg->u.flush_queue.channel = priv->channel;
1237 msg->u.flush_queue.flags = 0x00;
1238
1239 rc = kvaser_usb_send_msg(priv->dev, msg);
1240
1241 kfree(msg);
1242 return rc;
1243 }
1244
kvaser_usb_close(struct net_device * netdev)1245 static int kvaser_usb_close(struct net_device *netdev)
1246 {
1247 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1248 struct kvaser_usb *dev = priv->dev;
1249 int err;
1250
1251 netif_stop_queue(netdev);
1252
1253 err = kvaser_usb_flush_queue(priv);
1254 if (err)
1255 netdev_warn(netdev, "Cannot flush queue, error %d\n", err);
1256
1257 err = kvaser_usb_send_simple_msg(dev, CMD_RESET_CHIP, priv->channel);
1258 if (err)
1259 netdev_warn(netdev, "Cannot reset card, error %d\n", err);
1260
1261 err = kvaser_usb_stop_chip(priv);
1262 if (err)
1263 netdev_warn(netdev, "Cannot stop device, error %d\n", err);
1264
1265 /* reset tx contexts */
1266 kvaser_usb_unlink_tx_urbs(priv);
1267
1268 priv->can.state = CAN_STATE_STOPPED;
1269 close_candev(priv->netdev);
1270
1271 return 0;
1272 }
1273
kvaser_usb_write_bulk_callback(struct urb * urb)1274 static void kvaser_usb_write_bulk_callback(struct urb *urb)
1275 {
1276 struct kvaser_usb_tx_urb_context *context = urb->context;
1277 struct kvaser_usb_net_priv *priv;
1278 struct net_device *netdev;
1279
1280 if (WARN_ON(!context))
1281 return;
1282
1283 priv = context->priv;
1284 netdev = priv->netdev;
1285
1286 kfree(urb->transfer_buffer);
1287
1288 if (!netif_device_present(netdev))
1289 return;
1290
1291 if (urb->status)
1292 netdev_info(netdev, "Tx URB aborted (%d)\n", urb->status);
1293 }
1294
kvaser_usb_start_xmit(struct sk_buff * skb,struct net_device * netdev)1295 static netdev_tx_t kvaser_usb_start_xmit(struct sk_buff *skb,
1296 struct net_device *netdev)
1297 {
1298 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1299 struct kvaser_usb *dev = priv->dev;
1300 struct net_device_stats *stats = &netdev->stats;
1301 struct can_frame *cf = (struct can_frame *)skb->data;
1302 struct kvaser_usb_tx_urb_context *context = NULL;
1303 struct urb *urb;
1304 void *buf;
1305 struct kvaser_msg *msg;
1306 int i, err;
1307 int ret = NETDEV_TX_OK;
1308
1309 if (can_dropped_invalid_skb(netdev, skb))
1310 return NETDEV_TX_OK;
1311
1312 urb = usb_alloc_urb(0, GFP_ATOMIC);
1313 if (!urb) {
1314 netdev_err(netdev, "No memory left for URBs\n");
1315 stats->tx_dropped++;
1316 dev_kfree_skb(skb);
1317 return NETDEV_TX_OK;
1318 }
1319
1320 buf = kmalloc(sizeof(struct kvaser_msg), GFP_ATOMIC);
1321 if (!buf) {
1322 stats->tx_dropped++;
1323 dev_kfree_skb(skb);
1324 goto nobufmem;
1325 }
1326
1327 msg = buf;
1328 msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_tx_can);
1329 msg->u.tx_can.flags = 0;
1330 msg->u.tx_can.channel = priv->channel;
1331
1332 if (cf->can_id & CAN_EFF_FLAG) {
1333 msg->id = CMD_TX_EXT_MESSAGE;
1334 msg->u.tx_can.msg[0] = (cf->can_id >> 24) & 0x1f;
1335 msg->u.tx_can.msg[1] = (cf->can_id >> 18) & 0x3f;
1336 msg->u.tx_can.msg[2] = (cf->can_id >> 14) & 0x0f;
1337 msg->u.tx_can.msg[3] = (cf->can_id >> 6) & 0xff;
1338 msg->u.tx_can.msg[4] = cf->can_id & 0x3f;
1339 } else {
1340 msg->id = CMD_TX_STD_MESSAGE;
1341 msg->u.tx_can.msg[0] = (cf->can_id >> 6) & 0x1f;
1342 msg->u.tx_can.msg[1] = cf->can_id & 0x3f;
1343 }
1344
1345 msg->u.tx_can.msg[5] = cf->can_dlc;
1346 memcpy(&msg->u.tx_can.msg[6], cf->data, cf->can_dlc);
1347
1348 if (cf->can_id & CAN_RTR_FLAG)
1349 msg->u.tx_can.flags |= MSG_FLAG_REMOTE_FRAME;
1350
1351 for (i = 0; i < ARRAY_SIZE(priv->tx_contexts); i++) {
1352 if (priv->tx_contexts[i].echo_index == MAX_TX_URBS) {
1353 context = &priv->tx_contexts[i];
1354 break;
1355 }
1356 }
1357
1358 /* This should never happen; it implies a flow control bug */
1359 if (!context) {
1360 netdev_warn(netdev, "cannot find free context\n");
1361 ret = NETDEV_TX_BUSY;
1362 goto releasebuf;
1363 }
1364
1365 context->priv = priv;
1366 context->echo_index = i;
1367 context->dlc = cf->can_dlc;
1368
1369 msg->u.tx_can.tid = context->echo_index;
1370
1371 usb_fill_bulk_urb(urb, dev->udev,
1372 usb_sndbulkpipe(dev->udev,
1373 dev->bulk_out->bEndpointAddress),
1374 buf, msg->len,
1375 kvaser_usb_write_bulk_callback, context);
1376 usb_anchor_urb(urb, &priv->tx_submitted);
1377
1378 can_put_echo_skb(skb, netdev, context->echo_index);
1379
1380 atomic_inc(&priv->active_tx_urbs);
1381
1382 if (atomic_read(&priv->active_tx_urbs) >= MAX_TX_URBS)
1383 netif_stop_queue(netdev);
1384
1385 err = usb_submit_urb(urb, GFP_ATOMIC);
1386 if (unlikely(err)) {
1387 can_free_echo_skb(netdev, context->echo_index);
1388
1389 atomic_dec(&priv->active_tx_urbs);
1390 usb_unanchor_urb(urb);
1391
1392 stats->tx_dropped++;
1393
1394 if (err == -ENODEV)
1395 netif_device_detach(netdev);
1396 else
1397 netdev_warn(netdev, "Failed tx_urb %d\n", err);
1398
1399 goto releasebuf;
1400 }
1401
1402 usb_free_urb(urb);
1403
1404 return NETDEV_TX_OK;
1405
1406 releasebuf:
1407 kfree(buf);
1408 nobufmem:
1409 usb_free_urb(urb);
1410 return ret;
1411 }
1412
1413 static const struct net_device_ops kvaser_usb_netdev_ops = {
1414 .ndo_open = kvaser_usb_open,
1415 .ndo_stop = kvaser_usb_close,
1416 .ndo_start_xmit = kvaser_usb_start_xmit,
1417 .ndo_change_mtu = can_change_mtu,
1418 };
1419
1420 static const struct can_bittiming_const kvaser_usb_bittiming_const = {
1421 .name = "kvaser_usb",
1422 .tseg1_min = KVASER_USB_TSEG1_MIN,
1423 .tseg1_max = KVASER_USB_TSEG1_MAX,
1424 .tseg2_min = KVASER_USB_TSEG2_MIN,
1425 .tseg2_max = KVASER_USB_TSEG2_MAX,
1426 .sjw_max = KVASER_USB_SJW_MAX,
1427 .brp_min = KVASER_USB_BRP_MIN,
1428 .brp_max = KVASER_USB_BRP_MAX,
1429 .brp_inc = KVASER_USB_BRP_INC,
1430 };
1431
kvaser_usb_set_bittiming(struct net_device * netdev)1432 static int kvaser_usb_set_bittiming(struct net_device *netdev)
1433 {
1434 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1435 struct can_bittiming *bt = &priv->can.bittiming;
1436 struct kvaser_usb *dev = priv->dev;
1437 struct kvaser_msg *msg;
1438 int rc;
1439
1440 msg = kmalloc(sizeof(*msg), GFP_KERNEL);
1441 if (!msg)
1442 return -ENOMEM;
1443
1444 msg->id = CMD_SET_BUS_PARAMS;
1445 msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_busparams);
1446 msg->u.busparams.channel = priv->channel;
1447 msg->u.busparams.tid = 0xff;
1448 msg->u.busparams.bitrate = cpu_to_le32(bt->bitrate);
1449 msg->u.busparams.sjw = bt->sjw;
1450 msg->u.busparams.tseg1 = bt->prop_seg + bt->phase_seg1;
1451 msg->u.busparams.tseg2 = bt->phase_seg2;
1452
1453 if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
1454 msg->u.busparams.no_samp = 3;
1455 else
1456 msg->u.busparams.no_samp = 1;
1457
1458 rc = kvaser_usb_send_msg(dev, msg);
1459
1460 kfree(msg);
1461 return rc;
1462 }
1463
kvaser_usb_set_mode(struct net_device * netdev,enum can_mode mode)1464 static int kvaser_usb_set_mode(struct net_device *netdev,
1465 enum can_mode mode)
1466 {
1467 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1468 int err;
1469
1470 switch (mode) {
1471 case CAN_MODE_START:
1472 err = kvaser_usb_simple_msg_async(priv, CMD_START_CHIP);
1473 if (err)
1474 return err;
1475 break;
1476 default:
1477 return -EOPNOTSUPP;
1478 }
1479
1480 return 0;
1481 }
1482
kvaser_usb_get_berr_counter(const struct net_device * netdev,struct can_berr_counter * bec)1483 static int kvaser_usb_get_berr_counter(const struct net_device *netdev,
1484 struct can_berr_counter *bec)
1485 {
1486 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1487
1488 *bec = priv->bec;
1489
1490 return 0;
1491 }
1492
kvaser_usb_remove_interfaces(struct kvaser_usb * dev)1493 static void kvaser_usb_remove_interfaces(struct kvaser_usb *dev)
1494 {
1495 int i;
1496
1497 for (i = 0; i < dev->nchannels; i++) {
1498 if (!dev->nets[i])
1499 continue;
1500
1501 unregister_netdev(dev->nets[i]->netdev);
1502 }
1503
1504 kvaser_usb_unlink_all_urbs(dev);
1505
1506 for (i = 0; i < dev->nchannels; i++) {
1507 if (!dev->nets[i])
1508 continue;
1509
1510 free_candev(dev->nets[i]->netdev);
1511 }
1512 }
1513
kvaser_usb_init_one(struct usb_interface * intf,const struct usb_device_id * id,int channel)1514 static int kvaser_usb_init_one(struct usb_interface *intf,
1515 const struct usb_device_id *id, int channel)
1516 {
1517 struct kvaser_usb *dev = usb_get_intfdata(intf);
1518 struct net_device *netdev;
1519 struct kvaser_usb_net_priv *priv;
1520 int i, err;
1521
1522 err = kvaser_usb_send_simple_msg(dev, CMD_RESET_CHIP, channel);
1523 if (err)
1524 return err;
1525
1526 netdev = alloc_candev(sizeof(*priv), MAX_TX_URBS);
1527 if (!netdev) {
1528 dev_err(&intf->dev, "Cannot alloc candev\n");
1529 return -ENOMEM;
1530 }
1531
1532 priv = netdev_priv(netdev);
1533
1534 init_completion(&priv->start_comp);
1535 init_completion(&priv->stop_comp);
1536
1537 init_usb_anchor(&priv->tx_submitted);
1538 atomic_set(&priv->active_tx_urbs, 0);
1539
1540 for (i = 0; i < ARRAY_SIZE(priv->tx_contexts); i++)
1541 priv->tx_contexts[i].echo_index = MAX_TX_URBS;
1542
1543 priv->dev = dev;
1544 priv->netdev = netdev;
1545 priv->channel = channel;
1546
1547 priv->can.state = CAN_STATE_STOPPED;
1548 priv->can.clock.freq = CAN_USB_CLOCK;
1549 priv->can.bittiming_const = &kvaser_usb_bittiming_const;
1550 priv->can.do_set_bittiming = kvaser_usb_set_bittiming;
1551 priv->can.do_set_mode = kvaser_usb_set_mode;
1552 if (id->driver_info & KVASER_HAS_TXRX_ERRORS)
1553 priv->can.do_get_berr_counter = kvaser_usb_get_berr_counter;
1554 priv->can.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES;
1555 if (id->driver_info & KVASER_HAS_SILENT_MODE)
1556 priv->can.ctrlmode_supported |= CAN_CTRLMODE_LISTENONLY;
1557
1558 netdev->flags |= IFF_ECHO;
1559
1560 netdev->netdev_ops = &kvaser_usb_netdev_ops;
1561
1562 SET_NETDEV_DEV(netdev, &intf->dev);
1563 netdev->dev_id = channel;
1564
1565 dev->nets[channel] = priv;
1566
1567 err = register_candev(netdev);
1568 if (err) {
1569 dev_err(&intf->dev, "Failed to register can device\n");
1570 free_candev(netdev);
1571 dev->nets[channel] = NULL;
1572 return err;
1573 }
1574
1575 netdev_dbg(netdev, "device registered\n");
1576
1577 return 0;
1578 }
1579
kvaser_usb_get_endpoints(const struct usb_interface * intf,struct usb_endpoint_descriptor ** in,struct usb_endpoint_descriptor ** out)1580 static int kvaser_usb_get_endpoints(const struct usb_interface *intf,
1581 struct usb_endpoint_descriptor **in,
1582 struct usb_endpoint_descriptor **out)
1583 {
1584 const struct usb_host_interface *iface_desc;
1585 struct usb_endpoint_descriptor *endpoint;
1586 int i;
1587
1588 iface_desc = &intf->altsetting[0];
1589
1590 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1591 endpoint = &iface_desc->endpoint[i].desc;
1592
1593 if (!*in && usb_endpoint_is_bulk_in(endpoint))
1594 *in = endpoint;
1595
1596 if (!*out && usb_endpoint_is_bulk_out(endpoint))
1597 *out = endpoint;
1598
1599 /* use first bulk endpoint for in and out */
1600 if (*in && *out)
1601 return 0;
1602 }
1603
1604 return -ENODEV;
1605 }
1606
kvaser_usb_probe(struct usb_interface * intf,const struct usb_device_id * id)1607 static int kvaser_usb_probe(struct usb_interface *intf,
1608 const struct usb_device_id *id)
1609 {
1610 struct kvaser_usb *dev;
1611 int err = -ENOMEM;
1612 int i, retry = 3;
1613
1614 dev = devm_kzalloc(&intf->dev, sizeof(*dev), GFP_KERNEL);
1615 if (!dev)
1616 return -ENOMEM;
1617
1618 err = kvaser_usb_get_endpoints(intf, &dev->bulk_in, &dev->bulk_out);
1619 if (err) {
1620 dev_err(&intf->dev, "Cannot get usb endpoint(s)");
1621 return err;
1622 }
1623
1624 dev->udev = interface_to_usbdev(intf);
1625
1626 init_usb_anchor(&dev->rx_submitted);
1627
1628 usb_set_intfdata(intf, dev);
1629
1630 /* On some x86 laptops, plugging a Kvaser device again after
1631 * an unplug makes the firmware always ignore the very first
1632 * command. For such a case, provide some room for retries
1633 * instead of completely exiting the driver.
1634 */
1635 do {
1636 err = kvaser_usb_get_software_info(dev);
1637 } while (--retry && err == -ETIMEDOUT);
1638
1639 if (err) {
1640 dev_err(&intf->dev,
1641 "Cannot get software infos, error %d\n", err);
1642 return err;
1643 }
1644
1645 err = kvaser_usb_get_card_info(dev);
1646 if (err) {
1647 dev_err(&intf->dev,
1648 "Cannot get card infos, error %d\n", err);
1649 return err;
1650 }
1651
1652 dev_dbg(&intf->dev, "Firmware version: %d.%d.%d\n",
1653 ((dev->fw_version >> 24) & 0xff),
1654 ((dev->fw_version >> 16) & 0xff),
1655 (dev->fw_version & 0xffff));
1656
1657 for (i = 0; i < dev->nchannels; i++) {
1658 err = kvaser_usb_init_one(intf, id, i);
1659 if (err) {
1660 kvaser_usb_remove_interfaces(dev);
1661 return err;
1662 }
1663 }
1664
1665 return 0;
1666 }
1667
kvaser_usb_disconnect(struct usb_interface * intf)1668 static void kvaser_usb_disconnect(struct usb_interface *intf)
1669 {
1670 struct kvaser_usb *dev = usb_get_intfdata(intf);
1671
1672 usb_set_intfdata(intf, NULL);
1673
1674 if (!dev)
1675 return;
1676
1677 kvaser_usb_remove_interfaces(dev);
1678 }
1679
1680 static struct usb_driver kvaser_usb_driver = {
1681 .name = "kvaser_usb",
1682 .probe = kvaser_usb_probe,
1683 .disconnect = kvaser_usb_disconnect,
1684 .id_table = kvaser_usb_table,
1685 };
1686
1687 module_usb_driver(kvaser_usb_driver);
1688
1689 MODULE_AUTHOR("Olivier Sobrie <olivier@sobrie.be>");
1690 MODULE_DESCRIPTION("CAN driver for Kvaser CAN/USB devices");
1691 MODULE_LICENSE("GPL v2");
1692