blob: a316fa4b91ab20aa0205b5fde1740784495ce6ca [file] [log] [blame]
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001/*
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
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +02009 * - Kvaser linux usbcanII driver (version 5.3)
Olivier Sobrie080f40a2012-11-21 08:11:13 +010010 *
11 * Copyright (C) 2002-2006 KVASER AB, Sweden. All rights reserved.
12 * Copyright (C) 2010 Matthias Fuchs <matthias.fuchs@esd.eu>, esd gmbh
13 * Copyright (C) 2012 Olivier Sobrie <olivier@sobrie.be>
Ahmed S. Darwisha9ca6e12015-02-02 15:15:55 -050014 * Copyright (C) 2015 Valeo S.A.
Olivier Sobrie080f40a2012-11-21 08:11:13 +010015 */
16
Ahmed S. Darwish2fec5102015-02-26 10:22:02 -050017#include <linux/kernel.h>
Olivier Sobrie080f40a2012-11-21 08:11:13 +010018#include <linux/completion.h>
19#include <linux/module.h>
20#include <linux/netdevice.h>
21#include <linux/usb.h>
22
23#include <linux/can.h>
24#include <linux/can/dev.h>
25#include <linux/can/error.h>
26
27#define MAX_TX_URBS 16
28#define MAX_RX_URBS 4
29#define START_TIMEOUT 1000 /* msecs */
30#define STOP_TIMEOUT 1000 /* msecs */
31#define USB_SEND_TIMEOUT 1000 /* msecs */
32#define USB_RECV_TIMEOUT 1000 /* msecs */
33#define RX_BUFFER_SIZE 3072
34#define CAN_USB_CLOCK 8000000
35#define MAX_NET_DEVICES 3
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +020036#define MAX_USBCAN_NET_DEVICES 2
Olivier Sobrie080f40a2012-11-21 08:11:13 +010037
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +020038/* Kvaser Leaf USB devices */
Olivier Sobrie080f40a2012-11-21 08:11:13 +010039#define KVASER_VENDOR_ID 0x0bfd
40#define USB_LEAF_DEVEL_PRODUCT_ID 10
41#define USB_LEAF_LITE_PRODUCT_ID 11
42#define USB_LEAF_PRO_PRODUCT_ID 12
43#define USB_LEAF_SPRO_PRODUCT_ID 14
44#define USB_LEAF_PRO_LS_PRODUCT_ID 15
45#define USB_LEAF_PRO_SWC_PRODUCT_ID 16
46#define USB_LEAF_PRO_LIN_PRODUCT_ID 17
47#define USB_LEAF_SPRO_LS_PRODUCT_ID 18
48#define USB_LEAF_SPRO_SWC_PRODUCT_ID 19
49#define USB_MEMO2_DEVEL_PRODUCT_ID 22
50#define USB_MEMO2_HSHS_PRODUCT_ID 23
51#define USB_UPRO_HSHS_PRODUCT_ID 24
52#define USB_LEAF_LITE_GI_PRODUCT_ID 25
53#define USB_LEAF_PRO_OBDII_PRODUCT_ID 26
54#define USB_MEMO2_HSLS_PRODUCT_ID 27
55#define USB_LEAF_LITE_CH_PRODUCT_ID 28
56#define USB_BLACKBIRD_SPRO_PRODUCT_ID 29
57#define USB_OEM_MERCURY_PRODUCT_ID 34
58#define USB_OEM_LEAF_PRODUCT_ID 35
59#define USB_CAN_R_PRODUCT_ID 39
Olivier Sobrieb4f20132014-04-10 21:44:22 +020060#define USB_LEAF_LITE_V2_PRODUCT_ID 288
61#define USB_MINI_PCIE_HS_PRODUCT_ID 289
Olivier Sobrie080f40a2012-11-21 08:11:13 +010062
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +020063static inline bool kvaser_is_leaf(const struct usb_device_id *id)
64{
65 return id->idProduct >= USB_LEAF_DEVEL_PRODUCT_ID &&
66 id->idProduct <= USB_MINI_PCIE_HS_PRODUCT_ID;
67}
68
69/* Kvaser USBCan-II devices */
70#define USB_USBCAN_REVB_PRODUCT_ID 2
71#define USB_VCI2_PRODUCT_ID 3
72#define USB_USBCAN2_PRODUCT_ID 4
73#define USB_MEMORATOR_PRODUCT_ID 5
74
75static inline bool kvaser_is_usbcan(const struct usb_device_id *id)
76{
77 return id->idProduct >= USB_USBCAN_REVB_PRODUCT_ID &&
78 id->idProduct <= USB_MEMORATOR_PRODUCT_ID;
79}
80
Olivier Sobrie080f40a2012-11-21 08:11:13 +010081/* USB devices features */
82#define KVASER_HAS_SILENT_MODE BIT(0)
83#define KVASER_HAS_TXRX_ERRORS BIT(1)
84
85/* Message header size */
86#define MSG_HEADER_LEN 2
87
88/* Can message flags */
89#define MSG_FLAG_ERROR_FRAME BIT(0)
90#define MSG_FLAG_OVERRUN BIT(1)
91#define MSG_FLAG_NERR BIT(2)
92#define MSG_FLAG_WAKEUP BIT(3)
93#define MSG_FLAG_REMOTE_FRAME BIT(4)
94#define MSG_FLAG_RESERVED BIT(5)
95#define MSG_FLAG_TX_ACK BIT(6)
96#define MSG_FLAG_TX_REQUEST BIT(7)
97
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +020098/* Can states (M16C CxSTRH register) */
Olivier Sobrie080f40a2012-11-21 08:11:13 +010099#define M16C_STATE_BUS_RESET BIT(0)
100#define M16C_STATE_BUS_ERROR BIT(4)
101#define M16C_STATE_BUS_PASSIVE BIT(5)
102#define M16C_STATE_BUS_OFF BIT(6)
103
104/* Can msg ids */
105#define CMD_RX_STD_MESSAGE 12
106#define CMD_TX_STD_MESSAGE 13
107#define CMD_RX_EXT_MESSAGE 14
108#define CMD_TX_EXT_MESSAGE 15
109#define CMD_SET_BUS_PARAMS 16
110#define CMD_GET_BUS_PARAMS 17
111#define CMD_GET_BUS_PARAMS_REPLY 18
112#define CMD_GET_CHIP_STATE 19
113#define CMD_CHIP_STATE_EVENT 20
114#define CMD_SET_CTRL_MODE 21
115#define CMD_GET_CTRL_MODE 22
116#define CMD_GET_CTRL_MODE_REPLY 23
117#define CMD_RESET_CHIP 24
118#define CMD_RESET_CARD 25
119#define CMD_START_CHIP 26
120#define CMD_START_CHIP_REPLY 27
121#define CMD_STOP_CHIP 28
122#define CMD_STOP_CHIP_REPLY 29
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +0200123
124#define CMD_LEAF_GET_CARD_INFO2 32
125#define CMD_USBCAN_RESET_CLOCK 32
126#define CMD_USBCAN_CLOCK_OVERFLOW_EVENT 33
127
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100128#define CMD_GET_CARD_INFO 34
129#define CMD_GET_CARD_INFO_REPLY 35
130#define CMD_GET_SOFTWARE_INFO 38
131#define CMD_GET_SOFTWARE_INFO_REPLY 39
132#define CMD_ERROR_EVENT 45
133#define CMD_FLUSH_QUEUE 48
134#define CMD_RESET_ERROR_COUNTER 49
135#define CMD_TX_ACKNOWLEDGE 50
136#define CMD_CAN_ERROR_EVENT 51
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +0200137
138#define CMD_LEAF_USB_THROTTLE 77
139#define CMD_LEAF_LOG_MESSAGE 106
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100140
141/* error factors */
142#define M16C_EF_ACKE BIT(0)
143#define M16C_EF_CRCE BIT(1)
144#define M16C_EF_FORME BIT(2)
145#define M16C_EF_STFE BIT(3)
146#define M16C_EF_BITE0 BIT(4)
147#define M16C_EF_BITE1 BIT(5)
148#define M16C_EF_RCVE BIT(6)
149#define M16C_EF_TRE BIT(7)
150
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +0200151/* Only Leaf-based devices can report M16C error factors,
152 * thus define our own error status flags for USBCANII
153 */
154#define USBCAN_ERROR_STATE_NONE 0
155#define USBCAN_ERROR_STATE_TX_ERROR BIT(0)
156#define USBCAN_ERROR_STATE_RX_ERROR BIT(1)
157#define USBCAN_ERROR_STATE_BUSERROR BIT(2)
158
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100159/* bittiming parameters */
160#define KVASER_USB_TSEG1_MIN 1
161#define KVASER_USB_TSEG1_MAX 16
162#define KVASER_USB_TSEG2_MIN 1
163#define KVASER_USB_TSEG2_MAX 8
164#define KVASER_USB_SJW_MAX 4
165#define KVASER_USB_BRP_MIN 1
166#define KVASER_USB_BRP_MAX 64
167#define KVASER_USB_BRP_INC 1
168
169/* ctrl modes */
170#define KVASER_CTRL_MODE_NORMAL 1
171#define KVASER_CTRL_MODE_SILENT 2
172#define KVASER_CTRL_MODE_SELFRECEPTION 3
173#define KVASER_CTRL_MODE_OFF 4
174
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +0200175/* Extended CAN identifier flag */
Jonas Petersona90f13b2013-05-07 22:05:23 +0200176#define KVASER_EXTENDED_FRAME BIT(31)
177
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +0200178/* Kvaser USB CAN dongles are divided into two major families:
179 * - Leaf: Based on Renesas M32C, running firmware labeled as 'filo'
180 * - UsbcanII: Based on Renesas M16C, running firmware labeled as 'helios'
181 */
182enum kvaser_usb_family {
183 KVASER_LEAF,
184 KVASER_USBCAN,
185};
186
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100187struct kvaser_msg_simple {
188 u8 tid;
189 u8 channel;
190} __packed;
191
192struct kvaser_msg_cardinfo {
193 u8 tid;
194 u8 nchannels;
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +0200195 union {
196 struct {
197 __le32 serial_number;
198 __le32 padding;
199 } __packed leaf0;
200 struct {
201 __le32 serial_number_low;
202 __le32 serial_number_high;
203 } __packed usbcan0;
204 } __packed;
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100205 __le32 clock_resolution;
206 __le32 mfgdate;
207 u8 ean[8];
208 u8 hw_revision;
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +0200209 union {
210 struct {
211 u8 usb_hs_mode;
212 } __packed leaf1;
213 struct {
214 u8 padding;
215 } __packed usbcan1;
216 } __packed;
217 __le16 padding;
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100218} __packed;
219
220struct kvaser_msg_cardinfo2 {
221 u8 tid;
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +0200222 u8 reserved;
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100223 u8 pcb_id[24];
224 __le32 oem_unlock_code;
225} __packed;
226
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +0200227struct leaf_msg_softinfo {
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100228 u8 tid;
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +0200229 u8 padding0;
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100230 __le32 sw_options;
231 __le32 fw_version;
232 __le16 max_outstanding_tx;
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +0200233 __le16 padding1[9];
234} __packed;
235
236struct usbcan_msg_softinfo {
237 u8 tid;
238 u8 fw_name[5];
239 __le16 max_outstanding_tx;
240 u8 padding[6];
241 __le32 fw_version;
242 __le16 checksum;
243 __le16 sw_options;
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100244} __packed;
245
246struct kvaser_msg_busparams {
247 u8 tid;
248 u8 channel;
249 __le32 bitrate;
250 u8 tseg1;
251 u8 tseg2;
252 u8 sjw;
253 u8 no_samp;
254} __packed;
255
256struct kvaser_msg_tx_can {
257 u8 channel;
258 u8 tid;
259 u8 msg[14];
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +0200260 union {
261 struct {
262 u8 padding;
263 u8 flags;
264 } __packed leaf;
265 struct {
266 u8 flags;
267 u8 padding;
268 } __packed usbcan;
269 } __packed;
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100270} __packed;
271
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +0200272struct kvaser_msg_rx_can_header {
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100273 u8 channel;
274 u8 flag;
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +0200275} __packed;
276
277struct leaf_msg_rx_can {
278 u8 channel;
279 u8 flag;
280
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100281 __le16 time[3];
282 u8 msg[14];
283} __packed;
284
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +0200285struct usbcan_msg_rx_can {
286 u8 channel;
287 u8 flag;
288
289 u8 msg[14];
290 __le16 time;
291} __packed;
292
293struct leaf_msg_chip_state_event {
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100294 u8 tid;
295 u8 channel;
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +0200296
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100297 __le16 time[3];
298 u8 tx_errors_count;
299 u8 rx_errors_count;
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +0200300
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100301 u8 status;
302 u8 padding[3];
303} __packed;
304
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +0200305struct usbcan_msg_chip_state_event {
306 u8 tid;
307 u8 channel;
308
309 u8 tx_errors_count;
310 u8 rx_errors_count;
311 __le16 time;
312
313 u8 status;
314 u8 padding[3];
315} __packed;
316
317struct kvaser_msg_tx_acknowledge_header {
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100318 u8 channel;
319 u8 tid;
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +0200320} __packed;
321
322struct leaf_msg_tx_acknowledge {
323 u8 channel;
324 u8 tid;
325
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100326 __le16 time[3];
327 u8 flags;
328 u8 time_offset;
329} __packed;
330
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +0200331struct usbcan_msg_tx_acknowledge {
332 u8 channel;
333 u8 tid;
334
335 __le16 time;
336 __le16 padding;
337} __packed;
338
339struct leaf_msg_error_event {
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100340 u8 tid;
341 u8 flags;
342 __le16 time[3];
343 u8 channel;
344 u8 padding;
345 u8 tx_errors_count;
346 u8 rx_errors_count;
347 u8 status;
348 u8 error_factor;
349} __packed;
350
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +0200351struct usbcan_msg_error_event {
352 u8 tid;
353 u8 padding;
354 u8 tx_errors_count_ch0;
355 u8 rx_errors_count_ch0;
356 u8 tx_errors_count_ch1;
357 u8 rx_errors_count_ch1;
358 u8 status_ch0;
359 u8 status_ch1;
360 __le16 time;
361} __packed;
362
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100363struct kvaser_msg_ctrl_mode {
364 u8 tid;
365 u8 channel;
366 u8 ctrl_mode;
367 u8 padding[3];
368} __packed;
369
370struct kvaser_msg_flush_queue {
371 u8 tid;
372 u8 channel;
373 u8 flags;
374 u8 padding[3];
375} __packed;
376
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +0200377struct leaf_msg_log_message {
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100378 u8 channel;
379 u8 flags;
380 __le16 time[3];
381 u8 dlc;
382 u8 time_offset;
383 __le32 id;
384 u8 data[8];
385} __packed;
386
387struct kvaser_msg {
388 u8 len;
389 u8 id;
390 union {
391 struct kvaser_msg_simple simple;
392 struct kvaser_msg_cardinfo cardinfo;
393 struct kvaser_msg_cardinfo2 cardinfo2;
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100394 struct kvaser_msg_busparams busparams;
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +0200395
396 struct kvaser_msg_rx_can_header rx_can_header;
397 struct kvaser_msg_tx_acknowledge_header tx_acknowledge_header;
398
399 union {
400 struct leaf_msg_softinfo softinfo;
401 struct leaf_msg_rx_can rx_can;
402 struct leaf_msg_chip_state_event chip_state_event;
403 struct leaf_msg_tx_acknowledge tx_acknowledge;
404 struct leaf_msg_error_event error_event;
405 struct leaf_msg_log_message log_message;
406 } __packed leaf;
407
408 union {
409 struct usbcan_msg_softinfo softinfo;
410 struct usbcan_msg_rx_can rx_can;
411 struct usbcan_msg_chip_state_event chip_state_event;
412 struct usbcan_msg_tx_acknowledge tx_acknowledge;
413 struct usbcan_msg_error_event error_event;
414 } __packed usbcan;
415
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100416 struct kvaser_msg_tx_can tx_can;
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100417 struct kvaser_msg_ctrl_mode ctrl_mode;
418 struct kvaser_msg_flush_queue flush_queue;
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100419 } u;
420} __packed;
421
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +0200422/* Summary of a kvaser error event, for a unified Leaf/Usbcan error
423 * handling. Some discrepancies between the two families exist:
424 *
425 * - USBCAN firmware does not report M16C "error factors"
426 * - USBCAN controllers has difficulties reporting if the raised error
427 * event is for ch0 or ch1. They leave such arbitration to the OS
428 * driver by letting it compare error counters with previous values
429 * and decide the error event's channel. Thus for USBCAN, the channel
430 * field is only advisory.
431 */
Ahmed S. Darwish3b07a442015-01-26 07:27:19 +0200432struct kvaser_usb_error_summary {
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +0200433 u8 channel, status, txerr, rxerr;
434 union {
435 struct {
436 u8 error_factor;
437 } leaf;
438 struct {
439 u8 other_ch_status;
440 u8 error_state;
441 } usbcan;
442 };
Ahmed S. Darwish3b07a442015-01-26 07:27:19 +0200443};
444
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100445struct kvaser_usb_tx_urb_context {
446 struct kvaser_usb_net_priv *priv;
447 u32 echo_index;
448 int dlc;
449};
450
451struct kvaser_usb {
452 struct usb_device *udev;
453 struct kvaser_usb_net_priv *nets[MAX_NET_DEVICES];
454
455 struct usb_endpoint_descriptor *bulk_in, *bulk_out;
456 struct usb_anchor rx_submitted;
457
458 u32 fw_version;
459 unsigned int nchannels;
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +0200460 enum kvaser_usb_family family;
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100461
462 bool rxinitdone;
463 void *rxbuf[MAX_RX_URBS];
464 dma_addr_t rxbuf_dma[MAX_RX_URBS];
465};
466
467struct kvaser_usb_net_priv {
468 struct can_priv can;
469
470 atomic_t active_tx_urbs;
471 struct usb_anchor tx_submitted;
472 struct kvaser_usb_tx_urb_context tx_contexts[MAX_TX_URBS];
473
474 struct completion start_comp, stop_comp;
475
476 struct kvaser_usb *dev;
477 struct net_device *netdev;
478 int channel;
479
480 struct can_berr_counter bec;
481};
482
483static const struct usb_device_id kvaser_usb_table[] = {
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +0200484 /* Leaf family IDs */
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100485 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_DEVEL_PRODUCT_ID) },
486 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_PRODUCT_ID) },
487 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_PRODUCT_ID),
488 .driver_info = KVASER_HAS_TXRX_ERRORS |
489 KVASER_HAS_SILENT_MODE },
490 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_SPRO_PRODUCT_ID),
491 .driver_info = KVASER_HAS_TXRX_ERRORS |
492 KVASER_HAS_SILENT_MODE },
493 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_LS_PRODUCT_ID),
494 .driver_info = KVASER_HAS_TXRX_ERRORS |
495 KVASER_HAS_SILENT_MODE },
496 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_SWC_PRODUCT_ID),
497 .driver_info = KVASER_HAS_TXRX_ERRORS |
498 KVASER_HAS_SILENT_MODE },
499 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_LIN_PRODUCT_ID),
500 .driver_info = KVASER_HAS_TXRX_ERRORS |
501 KVASER_HAS_SILENT_MODE },
502 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_SPRO_LS_PRODUCT_ID),
503 .driver_info = KVASER_HAS_TXRX_ERRORS |
504 KVASER_HAS_SILENT_MODE },
505 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_SPRO_SWC_PRODUCT_ID),
506 .driver_info = KVASER_HAS_TXRX_ERRORS |
507 KVASER_HAS_SILENT_MODE },
508 { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO2_DEVEL_PRODUCT_ID),
509 .driver_info = KVASER_HAS_TXRX_ERRORS |
510 KVASER_HAS_SILENT_MODE },
511 { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO2_HSHS_PRODUCT_ID),
512 .driver_info = KVASER_HAS_TXRX_ERRORS |
513 KVASER_HAS_SILENT_MODE },
514 { USB_DEVICE(KVASER_VENDOR_ID, USB_UPRO_HSHS_PRODUCT_ID),
515 .driver_info = KVASER_HAS_TXRX_ERRORS },
516 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_GI_PRODUCT_ID) },
517 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_OBDII_PRODUCT_ID),
518 .driver_info = KVASER_HAS_TXRX_ERRORS |
519 KVASER_HAS_SILENT_MODE },
520 { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO2_HSLS_PRODUCT_ID),
521 .driver_info = KVASER_HAS_TXRX_ERRORS },
522 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_CH_PRODUCT_ID),
523 .driver_info = KVASER_HAS_TXRX_ERRORS },
524 { USB_DEVICE(KVASER_VENDOR_ID, USB_BLACKBIRD_SPRO_PRODUCT_ID),
525 .driver_info = KVASER_HAS_TXRX_ERRORS },
526 { USB_DEVICE(KVASER_VENDOR_ID, USB_OEM_MERCURY_PRODUCT_ID),
527 .driver_info = KVASER_HAS_TXRX_ERRORS },
528 { USB_DEVICE(KVASER_VENDOR_ID, USB_OEM_LEAF_PRODUCT_ID),
529 .driver_info = KVASER_HAS_TXRX_ERRORS },
530 { USB_DEVICE(KVASER_VENDOR_ID, USB_CAN_R_PRODUCT_ID),
531 .driver_info = KVASER_HAS_TXRX_ERRORS },
Olivier Sobrieb4f20132014-04-10 21:44:22 +0200532 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_V2_PRODUCT_ID) },
533 { USB_DEVICE(KVASER_VENDOR_ID, USB_MINI_PCIE_HS_PRODUCT_ID) },
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +0200534
535 /* USBCANII family IDs */
536 { USB_DEVICE(KVASER_VENDOR_ID, USB_USBCAN2_PRODUCT_ID),
537 .driver_info = KVASER_HAS_TXRX_ERRORS },
538 { USB_DEVICE(KVASER_VENDOR_ID, USB_USBCAN_REVB_PRODUCT_ID),
539 .driver_info = KVASER_HAS_TXRX_ERRORS },
540 { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMORATOR_PRODUCT_ID),
541 .driver_info = KVASER_HAS_TXRX_ERRORS },
542 { USB_DEVICE(KVASER_VENDOR_ID, USB_VCI2_PRODUCT_ID),
543 .driver_info = KVASER_HAS_TXRX_ERRORS },
544
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100545 { }
546};
547MODULE_DEVICE_TABLE(usb, kvaser_usb_table);
548
549static inline int kvaser_usb_send_msg(const struct kvaser_usb *dev,
550 struct kvaser_msg *msg)
551{
552 int actual_len;
553
554 return usb_bulk_msg(dev->udev,
555 usb_sndbulkpipe(dev->udev,
556 dev->bulk_out->bEndpointAddress),
557 msg, msg->len, &actual_len,
558 USB_SEND_TIMEOUT);
559}
560
561static int kvaser_usb_wait_msg(const struct kvaser_usb *dev, u8 id,
562 struct kvaser_msg *msg)
563{
564 struct kvaser_msg *tmp;
565 void *buf;
566 int actual_len;
567 int err;
Olivier Sobriee59e36e2014-04-10 21:44:21 +0200568 int pos;
569 unsigned long to = jiffies + msecs_to_jiffies(USB_RECV_TIMEOUT);
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100570
571 buf = kzalloc(RX_BUFFER_SIZE, GFP_KERNEL);
572 if (!buf)
573 return -ENOMEM;
574
Olivier Sobriee59e36e2014-04-10 21:44:21 +0200575 do {
576 err = usb_bulk_msg(dev->udev,
577 usb_rcvbulkpipe(dev->udev,
578 dev->bulk_in->bEndpointAddress),
579 buf, RX_BUFFER_SIZE, &actual_len,
580 USB_RECV_TIMEOUT);
581 if (err < 0)
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100582 goto end;
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100583
Olivier Sobriee59e36e2014-04-10 21:44:21 +0200584 pos = 0;
585 while (pos <= actual_len - MSG_HEADER_LEN) {
586 tmp = buf + pos;
587
Ahmed S. Darwish2fec5102015-02-26 10:22:02 -0500588 /* Handle messages crossing the USB endpoint max packet
589 * size boundary. Check kvaser_usb_read_bulk_callback()
590 * for further details.
591 */
592 if (tmp->len == 0) {
593 pos = round_up(pos,
594 dev->bulk_in->wMaxPacketSize);
595 continue;
596 }
Olivier Sobriee59e36e2014-04-10 21:44:21 +0200597
598 if (pos + tmp->len > actual_len) {
599 dev_err(dev->udev->dev.parent,
600 "Format error\n");
601 break;
602 }
603
604 if (tmp->id == id) {
605 memcpy(msg, tmp, tmp->len);
606 goto end;
607 }
608
609 pos += tmp->len;
610 }
611 } while (time_before(jiffies, to));
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100612
613 err = -EINVAL;
614
615end:
616 kfree(buf);
617
618 return err;
619}
620
621static int kvaser_usb_send_simple_msg(const struct kvaser_usb *dev,
622 u8 msg_id, int channel)
623{
624 struct kvaser_msg *msg;
625 int rc;
626
627 msg = kmalloc(sizeof(*msg), GFP_KERNEL);
628 if (!msg)
629 return -ENOMEM;
630
631 msg->id = msg_id;
632 msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_simple);
633 msg->u.simple.channel = channel;
634 msg->u.simple.tid = 0xff;
635
636 rc = kvaser_usb_send_msg(dev, msg);
637
638 kfree(msg);
639 return rc;
640}
641
642static int kvaser_usb_get_software_info(struct kvaser_usb *dev)
643{
644 struct kvaser_msg msg;
645 int err;
646
647 err = kvaser_usb_send_simple_msg(dev, CMD_GET_SOFTWARE_INFO, 0);
648 if (err)
649 return err;
650
651 err = kvaser_usb_wait_msg(dev, CMD_GET_SOFTWARE_INFO_REPLY, &msg);
652 if (err)
653 return err;
654
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +0200655 switch (dev->family) {
656 case KVASER_LEAF:
657 dev->fw_version = le32_to_cpu(msg.u.leaf.softinfo.fw_version);
658 break;
659 case KVASER_USBCAN:
660 dev->fw_version = le32_to_cpu(msg.u.usbcan.softinfo.fw_version);
661 break;
662 }
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100663
664 return 0;
665}
666
667static int kvaser_usb_get_card_info(struct kvaser_usb *dev)
668{
669 struct kvaser_msg msg;
670 int err;
671
672 err = kvaser_usb_send_simple_msg(dev, CMD_GET_CARD_INFO, 0);
673 if (err)
674 return err;
675
676 err = kvaser_usb_wait_msg(dev, CMD_GET_CARD_INFO_REPLY, &msg);
677 if (err)
678 return err;
679
680 dev->nchannels = msg.u.cardinfo.nchannels;
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +0200681 if ((dev->nchannels > MAX_NET_DEVICES) ||
682 (dev->family == KVASER_USBCAN &&
683 dev->nchannels > MAX_USBCAN_NET_DEVICES))
Olivier Sobrie862474f82014-02-11 11:01:23 +0100684 return -EINVAL;
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100685
686 return 0;
687}
688
689static void kvaser_usb_tx_acknowledge(const struct kvaser_usb *dev,
690 const struct kvaser_msg *msg)
691{
692 struct net_device_stats *stats;
693 struct kvaser_usb_tx_urb_context *context;
694 struct kvaser_usb_net_priv *priv;
695 struct sk_buff *skb;
696 struct can_frame *cf;
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +0200697 u8 channel, tid;
698
699 channel = msg->u.tx_acknowledge_header.channel;
700 tid = msg->u.tx_acknowledge_header.tid;
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100701
702 if (channel >= dev->nchannels) {
703 dev_err(dev->udev->dev.parent,
704 "Invalid channel number (%d)\n", channel);
705 return;
706 }
707
708 priv = dev->nets[channel];
709
710 if (!netif_device_present(priv->netdev))
711 return;
712
713 stats = &priv->netdev->stats;
714
715 context = &priv->tx_contexts[tid % MAX_TX_URBS];
716
717 /* Sometimes the state change doesn't come after a bus-off event */
718 if (priv->can.restart_ms &&
719 (priv->can.state >= CAN_STATE_BUS_OFF)) {
720 skb = alloc_can_err_skb(priv->netdev, &cf);
721 if (skb) {
722 cf->can_id |= CAN_ERR_RESTARTED;
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100723
724 stats->rx_packets++;
725 stats->rx_bytes += cf->can_dlc;
Ahmed S. Darwisha58518c2015-01-11 15:49:52 -0500726 netif_rx(skb);
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100727 } else {
728 netdev_err(priv->netdev,
729 "No memory left for err_skb\n");
730 }
731
732 priv->can.can_stats.restarts++;
733 netif_carrier_on(priv->netdev);
734
735 priv->can.state = CAN_STATE_ERROR_ACTIVE;
736 }
737
738 stats->tx_packets++;
739 stats->tx_bytes += context->dlc;
740 can_get_echo_skb(priv->netdev, context->echo_index);
741
742 context->echo_index = MAX_TX_URBS;
743 atomic_dec(&priv->active_tx_urbs);
744
745 netif_wake_queue(priv->netdev);
746}
747
748static void kvaser_usb_simple_msg_callback(struct urb *urb)
749{
750 struct net_device *netdev = urb->context;
751
752 kfree(urb->transfer_buffer);
753
754 if (urb->status)
755 netdev_warn(netdev, "urb status received: %d\n",
756 urb->status);
757}
758
759static int kvaser_usb_simple_msg_async(struct kvaser_usb_net_priv *priv,
760 u8 msg_id)
761{
762 struct kvaser_usb *dev = priv->dev;
763 struct net_device *netdev = priv->netdev;
764 struct kvaser_msg *msg;
765 struct urb *urb;
766 void *buf;
767 int err;
768
769 urb = usb_alloc_urb(0, GFP_ATOMIC);
770 if (!urb) {
771 netdev_err(netdev, "No memory left for URBs\n");
772 return -ENOMEM;
773 }
774
775 buf = kmalloc(sizeof(struct kvaser_msg), GFP_ATOMIC);
776 if (!buf) {
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100777 usb_free_urb(urb);
778 return -ENOMEM;
779 }
780
781 msg = (struct kvaser_msg *)buf;
782 msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_simple);
783 msg->id = msg_id;
784 msg->u.simple.channel = priv->channel;
785
786 usb_fill_bulk_urb(urb, dev->udev,
787 usb_sndbulkpipe(dev->udev,
788 dev->bulk_out->bEndpointAddress),
789 buf, msg->len,
Ahmed S. Darwish3803fa62015-01-26 07:22:54 +0200790 kvaser_usb_simple_msg_callback, netdev);
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100791 usb_anchor_urb(urb, &priv->tx_submitted);
792
793 err = usb_submit_urb(urb, GFP_ATOMIC);
794 if (err) {
795 netdev_err(netdev, "Error transmitting URB\n");
796 usb_unanchor_urb(urb);
797 usb_free_urb(urb);
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100798 return err;
799 }
800
801 usb_free_urb(urb);
802
803 return 0;
804}
805
806static void kvaser_usb_unlink_tx_urbs(struct kvaser_usb_net_priv *priv)
807{
808 int i;
809
810 usb_kill_anchored_urbs(&priv->tx_submitted);
811 atomic_set(&priv->active_tx_urbs, 0);
812
813 for (i = 0; i < MAX_TX_URBS; i++)
814 priv->tx_contexts[i].echo_index = MAX_TX_URBS;
815}
816
Ahmed S. Darwish3b07a442015-01-26 07:27:19 +0200817static void kvaser_usb_rx_error_update_can_state(struct kvaser_usb_net_priv *priv,
Ahmed S. Darwish96d7f102015-01-26 07:29:15 +0200818 const struct kvaser_usb_error_summary *es,
819 struct can_frame *cf)
Ahmed S. Darwish3b07a442015-01-26 07:27:19 +0200820{
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +0200821 struct kvaser_usb *dev = priv->dev;
822 struct net_device_stats *stats = &priv->netdev->stats;
Ahmed S. Darwish96d7f102015-01-26 07:29:15 +0200823 enum can_state cur_state, new_state, tx_state, rx_state;
Ahmed S. Darwish3b07a442015-01-26 07:27:19 +0200824
825 netdev_dbg(priv->netdev, "Error status: 0x%02x\n", es->status);
826
Ahmed S. Darwish96d7f102015-01-26 07:29:15 +0200827 new_state = cur_state = priv->can.state;
828
829 if (es->status & (M16C_STATE_BUS_OFF | M16C_STATE_BUS_RESET))
Ahmed S. Darwish3b07a442015-01-26 07:27:19 +0200830 new_state = CAN_STATE_BUS_OFF;
Ahmed S. Darwish96d7f102015-01-26 07:29:15 +0200831 else if (es->status & M16C_STATE_BUS_PASSIVE)
Ahmed S. Darwish3b07a442015-01-26 07:27:19 +0200832 new_state = CAN_STATE_ERROR_PASSIVE;
Ahmed S. Darwish96d7f102015-01-26 07:29:15 +0200833 else if (es->status & M16C_STATE_BUS_ERROR) {
Ahmed S. Darwisha9ca6e12015-02-02 15:15:55 -0500834 /* Guard against spurious error events after a busoff */
835 if (cur_state < CAN_STATE_BUS_OFF) {
836 if ((es->txerr >= 128) || (es->rxerr >= 128))
837 new_state = CAN_STATE_ERROR_PASSIVE;
838 else if ((es->txerr >= 96) || (es->rxerr >= 96))
839 new_state = CAN_STATE_ERROR_WARNING;
840 else if (cur_state > CAN_STATE_ERROR_ACTIVE)
841 new_state = CAN_STATE_ERROR_ACTIVE;
842 }
Ahmed S. Darwish3b07a442015-01-26 07:27:19 +0200843 }
844
845 if (!es->status)
846 new_state = CAN_STATE_ERROR_ACTIVE;
847
Ahmed S. Darwish96d7f102015-01-26 07:29:15 +0200848 if (new_state != cur_state) {
849 tx_state = (es->txerr >= es->rxerr) ? new_state : 0;
850 rx_state = (es->txerr <= es->rxerr) ? new_state : 0;
851
852 can_change_state(priv->netdev, cf, tx_state, rx_state);
853 }
854
Ahmed S. Darwish3b07a442015-01-26 07:27:19 +0200855 if (priv->can.restart_ms &&
Ahmed S. Darwish96d7f102015-01-26 07:29:15 +0200856 (cur_state >= CAN_STATE_BUS_OFF) &&
Ahmed S. Darwish3b07a442015-01-26 07:27:19 +0200857 (new_state < CAN_STATE_BUS_OFF)) {
858 priv->can.can_stats.restarts++;
859 }
860
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +0200861 switch (dev->family) {
862 case KVASER_LEAF:
863 if (es->leaf.error_factor) {
864 priv->can.can_stats.bus_error++;
865 stats->rx_errors++;
866 }
867 break;
868 case KVASER_USBCAN:
869 if (es->usbcan.error_state & USBCAN_ERROR_STATE_TX_ERROR)
870 stats->tx_errors++;
871 if (es->usbcan.error_state & USBCAN_ERROR_STATE_RX_ERROR)
872 stats->rx_errors++;
873 if (es->usbcan.error_state & USBCAN_ERROR_STATE_BUSERROR) {
874 priv->can.can_stats.bus_error++;
875 }
876 break;
Ahmed S. Darwish3b07a442015-01-26 07:27:19 +0200877 }
878
879 priv->bec.txerr = es->txerr;
880 priv->bec.rxerr = es->rxerr;
Ahmed S. Darwish3b07a442015-01-26 07:27:19 +0200881}
882
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100883static void kvaser_usb_rx_error(const struct kvaser_usb *dev,
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +0200884 const struct kvaser_usb_error_summary *es)
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100885{
Ahmed S. Darwish96d7f102015-01-26 07:29:15 +0200886 struct can_frame *cf, tmp_cf = { .can_id = CAN_ERR_FLAG, .can_dlc = CAN_ERR_DLC };
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100887 struct sk_buff *skb;
888 struct net_device_stats *stats;
889 struct kvaser_usb_net_priv *priv;
Ahmed S. Darwish96d7f102015-01-26 07:29:15 +0200890 enum can_state old_state, new_state;
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100891
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +0200892 if (es->channel >= dev->nchannels) {
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100893 dev_err(dev->udev->dev.parent,
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +0200894 "Invalid channel number (%d)\n", es->channel);
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100895 return;
896 }
897
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +0200898 priv = dev->nets[es->channel];
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100899 stats = &priv->netdev->stats;
900
Ahmed S. Darwish3b07a442015-01-26 07:27:19 +0200901 /* Update all of the can interface's state and error counters before
Ahmed S. Darwish96d7f102015-01-26 07:29:15 +0200902 * trying any memory allocation that can actually fail with -ENOMEM.
903 *
904 * We send a temporary stack-allocated error can frame to
905 * can_change_state() for the very same reason.
906 *
907 * TODO: Split can_change_state() responsibility between updating the
908 * can interface's state and counters, and the setting up of can error
909 * frame ID and data to userspace. Remove stack allocation afterwards.
Ahmed S. Darwish3b07a442015-01-26 07:27:19 +0200910 */
911 old_state = priv->can.state;
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +0200912 kvaser_usb_rx_error_update_can_state(priv, es, &tmp_cf);
Ahmed S. Darwish96d7f102015-01-26 07:29:15 +0200913 new_state = priv->can.state;
Ahmed S. Darwish3b07a442015-01-26 07:27:19 +0200914
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100915 skb = alloc_can_err_skb(priv->netdev, &cf);
916 if (!skb) {
917 stats->rx_dropped++;
918 return;
919 }
Ahmed S. Darwish96d7f102015-01-26 07:29:15 +0200920 memcpy(cf, &tmp_cf, sizeof(*cf));
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100921
Ahmed S. Darwish96d7f102015-01-26 07:29:15 +0200922 if (new_state != old_state) {
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +0200923 if (es->status &
Ahmed S. Darwish96d7f102015-01-26 07:29:15 +0200924 (M16C_STATE_BUS_OFF | M16C_STATE_BUS_RESET)) {
925 if (!priv->can.restart_ms)
926 kvaser_usb_simple_msg_async(priv, CMD_STOP_CHIP);
927 netif_carrier_off(priv->netdev);
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100928 }
Ahmed S. Darwish96d7f102015-01-26 07:29:15 +0200929
930 if (priv->can.restart_ms &&
931 (old_state >= CAN_STATE_BUS_OFF) &&
932 (new_state < CAN_STATE_BUS_OFF)) {
933 cf->can_id |= CAN_ERR_RESTARTED;
934 netif_carrier_on(priv->netdev);
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100935 }
936 }
937
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +0200938 switch (dev->family) {
939 case KVASER_LEAF:
940 if (es->leaf.error_factor) {
941 cf->can_id |= CAN_ERR_BUSERROR | CAN_ERR_PROT;
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100942
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +0200943 if (es->leaf.error_factor & M16C_EF_ACKE)
944 cf->data[3] |= (CAN_ERR_PROT_LOC_ACK);
945 if (es->leaf.error_factor & M16C_EF_CRCE)
946 cf->data[3] |= (CAN_ERR_PROT_LOC_CRC_SEQ |
947 CAN_ERR_PROT_LOC_CRC_DEL);
948 if (es->leaf.error_factor & M16C_EF_FORME)
949 cf->data[2] |= CAN_ERR_PROT_FORM;
950 if (es->leaf.error_factor & M16C_EF_STFE)
951 cf->data[2] |= CAN_ERR_PROT_STUFF;
952 if (es->leaf.error_factor & M16C_EF_BITE0)
953 cf->data[2] |= CAN_ERR_PROT_BIT0;
954 if (es->leaf.error_factor & M16C_EF_BITE1)
955 cf->data[2] |= CAN_ERR_PROT_BIT1;
956 if (es->leaf.error_factor & M16C_EF_TRE)
957 cf->data[2] |= CAN_ERR_PROT_TX;
958 }
959 break;
960 case KVASER_USBCAN:
961 if (es->usbcan.error_state & USBCAN_ERROR_STATE_BUSERROR) {
962 cf->can_id |= CAN_ERR_BUSERROR;
963 }
964 break;
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100965 }
966
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +0200967 cf->data[6] = es->txerr;
968 cf->data[7] = es->rxerr;
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100969
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100970 stats->rx_packets++;
971 stats->rx_bytes += cf->can_dlc;
Ahmed S. Darwisha58518c2015-01-11 15:49:52 -0500972 netif_rx(skb);
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100973}
974
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +0200975/* For USBCAN, report error to userspace iff the channels's errors counter
976 * has changed, or we're the only channel seeing a bus error state.
977 */
978static void kvaser_usbcan_conditionally_rx_error(const struct kvaser_usb *dev,
979 struct kvaser_usb_error_summary *es)
980{
981 struct kvaser_usb_net_priv *priv;
982 int channel;
983 bool report_error;
984
985 channel = es->channel;
986 if (channel >= dev->nchannels) {
987 dev_err(dev->udev->dev.parent,
988 "Invalid channel number (%d)\n", channel);
989 return;
990 }
991
992 priv = dev->nets[channel];
993 report_error = false;
994
995 if (es->txerr != priv->bec.txerr) {
996 es->usbcan.error_state |= USBCAN_ERROR_STATE_TX_ERROR;
997 report_error = true;
998 }
999 if (es->rxerr != priv->bec.rxerr) {
1000 es->usbcan.error_state |= USBCAN_ERROR_STATE_RX_ERROR;
1001 report_error = true;
1002 }
1003 if ((es->status & M16C_STATE_BUS_ERROR) &&
1004 !(es->usbcan.other_ch_status & M16C_STATE_BUS_ERROR)) {
1005 es->usbcan.error_state |= USBCAN_ERROR_STATE_BUSERROR;
1006 report_error = true;
1007 }
1008
1009 if (report_error)
1010 kvaser_usb_rx_error(dev, es);
1011}
1012
1013static void kvaser_usbcan_rx_error(const struct kvaser_usb *dev,
1014 const struct kvaser_msg *msg)
1015{
1016 struct kvaser_usb_error_summary es = { };
1017
1018 switch (msg->id) {
1019 /* Sometimes errors are sent as unsolicited chip state events */
1020 case CMD_CHIP_STATE_EVENT:
1021 es.channel = msg->u.usbcan.chip_state_event.channel;
1022 es.status = msg->u.usbcan.chip_state_event.status;
1023 es.txerr = msg->u.usbcan.chip_state_event.tx_errors_count;
1024 es.rxerr = msg->u.usbcan.chip_state_event.rx_errors_count;
1025 kvaser_usbcan_conditionally_rx_error(dev, &es);
1026 break;
1027
1028 case CMD_CAN_ERROR_EVENT:
1029 es.channel = 0;
1030 es.status = msg->u.usbcan.error_event.status_ch0;
1031 es.txerr = msg->u.usbcan.error_event.tx_errors_count_ch0;
1032 es.rxerr = msg->u.usbcan.error_event.rx_errors_count_ch0;
1033 es.usbcan.other_ch_status =
1034 msg->u.usbcan.error_event.status_ch1;
1035 kvaser_usbcan_conditionally_rx_error(dev, &es);
1036
1037 /* The USBCAN firmware supports up to 2 channels.
1038 * Now that ch0 was checked, check if ch1 has any errors.
1039 */
1040 if (dev->nchannels == MAX_USBCAN_NET_DEVICES) {
1041 es.channel = 1;
1042 es.status = msg->u.usbcan.error_event.status_ch1;
1043 es.txerr = msg->u.usbcan.error_event.tx_errors_count_ch1;
1044 es.rxerr = msg->u.usbcan.error_event.rx_errors_count_ch1;
1045 es.usbcan.other_ch_status =
1046 msg->u.usbcan.error_event.status_ch0;
1047 kvaser_usbcan_conditionally_rx_error(dev, &es);
1048 }
1049 break;
1050
1051 default:
1052 dev_err(dev->udev->dev.parent, "Invalid msg id (%d)\n",
1053 msg->id);
1054 }
1055}
1056
1057static void kvaser_leaf_rx_error(const struct kvaser_usb *dev,
1058 const struct kvaser_msg *msg)
1059{
1060 struct kvaser_usb_error_summary es = { };
1061
1062 switch (msg->id) {
1063 case CMD_CAN_ERROR_EVENT:
1064 es.channel = msg->u.leaf.error_event.channel;
1065 es.status = msg->u.leaf.error_event.status;
1066 es.txerr = msg->u.leaf.error_event.tx_errors_count;
1067 es.rxerr = msg->u.leaf.error_event.rx_errors_count;
1068 es.leaf.error_factor = msg->u.leaf.error_event.error_factor;
1069 break;
1070 case CMD_LEAF_LOG_MESSAGE:
1071 es.channel = msg->u.leaf.log_message.channel;
1072 es.status = msg->u.leaf.log_message.data[0];
1073 es.txerr = msg->u.leaf.log_message.data[2];
1074 es.rxerr = msg->u.leaf.log_message.data[3];
1075 es.leaf.error_factor = msg->u.leaf.log_message.data[1];
1076 break;
1077 case CMD_CHIP_STATE_EVENT:
1078 es.channel = msg->u.leaf.chip_state_event.channel;
1079 es.status = msg->u.leaf.chip_state_event.status;
1080 es.txerr = msg->u.leaf.chip_state_event.tx_errors_count;
1081 es.rxerr = msg->u.leaf.chip_state_event.rx_errors_count;
1082 es.leaf.error_factor = 0;
1083 break;
1084 default:
1085 dev_err(dev->udev->dev.parent, "Invalid msg id (%d)\n",
1086 msg->id);
1087 return;
1088 }
1089
1090 kvaser_usb_rx_error(dev, &es);
1091}
1092
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001093static void kvaser_usb_rx_can_err(const struct kvaser_usb_net_priv *priv,
1094 const struct kvaser_msg *msg)
1095{
1096 struct can_frame *cf;
1097 struct sk_buff *skb;
1098 struct net_device_stats *stats = &priv->netdev->stats;
1099
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +02001100 if (msg->u.rx_can_header.flag & (MSG_FLAG_ERROR_FRAME |
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001101 MSG_FLAG_NERR)) {
1102 netdev_err(priv->netdev, "Unknow error (flags: 0x%02x)\n",
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +02001103 msg->u.rx_can_header.flag);
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001104
1105 stats->rx_errors++;
1106 return;
1107 }
1108
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +02001109 if (msg->u.rx_can_header.flag & MSG_FLAG_OVERRUN) {
Ahmed S. Darwish3b07a442015-01-26 07:27:19 +02001110 stats->rx_over_errors++;
1111 stats->rx_errors++;
1112
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001113 skb = alloc_can_err_skb(priv->netdev, &cf);
1114 if (!skb) {
1115 stats->rx_dropped++;
1116 return;
1117 }
1118
1119 cf->can_id |= CAN_ERR_CRTL;
1120 cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
1121
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001122 stats->rx_packets++;
1123 stats->rx_bytes += cf->can_dlc;
Ahmed S. Darwisha58518c2015-01-11 15:49:52 -05001124 netif_rx(skb);
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001125 }
1126}
1127
1128static void kvaser_usb_rx_can_msg(const struct kvaser_usb *dev,
1129 const struct kvaser_msg *msg)
1130{
1131 struct kvaser_usb_net_priv *priv;
1132 struct can_frame *cf;
1133 struct sk_buff *skb;
1134 struct net_device_stats *stats;
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +02001135 u8 channel = msg->u.rx_can_header.channel;
1136 const u8 *rx_msg = NULL; /* GCC */
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001137
1138 if (channel >= dev->nchannels) {
1139 dev_err(dev->udev->dev.parent,
1140 "Invalid channel number (%d)\n", channel);
1141 return;
1142 }
1143
1144 priv = dev->nets[channel];
1145 stats = &priv->netdev->stats;
1146
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +02001147 if ((msg->u.rx_can_header.flag & MSG_FLAG_ERROR_FRAME) &&
1148 (dev->family == KVASER_LEAF && msg->id == CMD_LEAF_LOG_MESSAGE)) {
1149 kvaser_leaf_rx_error(dev, msg);
Jonas Petersona90f13b2013-05-07 22:05:23 +02001150 return;
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +02001151 } else if (msg->u.rx_can_header.flag & (MSG_FLAG_ERROR_FRAME |
1152 MSG_FLAG_NERR |
1153 MSG_FLAG_OVERRUN)) {
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001154 kvaser_usb_rx_can_err(priv, msg);
1155 return;
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +02001156 } else if (msg->u.rx_can_header.flag & ~MSG_FLAG_REMOTE_FRAME) {
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001157 netdev_warn(priv->netdev,
1158 "Unhandled frame (flags: 0x%02x)",
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +02001159 msg->u.rx_can_header.flag);
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001160 return;
1161 }
1162
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +02001163 switch (dev->family) {
1164 case KVASER_LEAF:
1165 rx_msg = msg->u.leaf.rx_can.msg;
1166 break;
1167 case KVASER_USBCAN:
1168 rx_msg = msg->u.usbcan.rx_can.msg;
1169 break;
1170 }
1171
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001172 skb = alloc_can_skb(priv->netdev, &cf);
1173 if (!skb) {
1174 stats->tx_dropped++;
1175 return;
1176 }
1177
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +02001178 if (dev->family == KVASER_LEAF && msg->id == CMD_LEAF_LOG_MESSAGE) {
1179 cf->can_id = le32_to_cpu(msg->u.leaf.log_message.id);
Jonas Petersona90f13b2013-05-07 22:05:23 +02001180 if (cf->can_id & KVASER_EXTENDED_FRAME)
1181 cf->can_id &= CAN_EFF_MASK | CAN_EFF_FLAG;
1182 else
1183 cf->can_id &= CAN_SFF_MASK;
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001184
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +02001185 cf->can_dlc = get_can_dlc(msg->u.leaf.log_message.dlc);
Jonas Petersona90f13b2013-05-07 22:05:23 +02001186
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +02001187 if (msg->u.leaf.log_message.flags & MSG_FLAG_REMOTE_FRAME)
Jonas Petersona90f13b2013-05-07 22:05:23 +02001188 cf->can_id |= CAN_RTR_FLAG;
1189 else
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +02001190 memcpy(cf->data, &msg->u.leaf.log_message.data,
Jonas Petersona90f13b2013-05-07 22:05:23 +02001191 cf->can_dlc);
1192 } else {
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +02001193 cf->can_id = ((rx_msg[0] & 0x1f) << 6) | (rx_msg[1] & 0x3f);
Jonas Petersona90f13b2013-05-07 22:05:23 +02001194
1195 if (msg->id == CMD_RX_EXT_MESSAGE) {
1196 cf->can_id <<= 18;
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +02001197 cf->can_id |= ((rx_msg[2] & 0x0f) << 14) |
1198 ((rx_msg[3] & 0xff) << 6) |
1199 (rx_msg[4] & 0x3f);
Jonas Petersona90f13b2013-05-07 22:05:23 +02001200 cf->can_id |= CAN_EFF_FLAG;
1201 }
1202
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +02001203 cf->can_dlc = get_can_dlc(rx_msg[5]);
Jonas Petersona90f13b2013-05-07 22:05:23 +02001204
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +02001205 if (msg->u.rx_can_header.flag & MSG_FLAG_REMOTE_FRAME)
Jonas Petersona90f13b2013-05-07 22:05:23 +02001206 cf->can_id |= CAN_RTR_FLAG;
1207 else
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +02001208 memcpy(cf->data, &rx_msg[6],
Jonas Petersona90f13b2013-05-07 22:05:23 +02001209 cf->can_dlc);
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001210 }
1211
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001212 stats->rx_packets++;
1213 stats->rx_bytes += cf->can_dlc;
Ahmed S. Darwisha58518c2015-01-11 15:49:52 -05001214 netif_rx(skb);
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001215}
1216
1217static void kvaser_usb_start_chip_reply(const struct kvaser_usb *dev,
1218 const struct kvaser_msg *msg)
1219{
1220 struct kvaser_usb_net_priv *priv;
1221 u8 channel = msg->u.simple.channel;
1222
1223 if (channel >= dev->nchannels) {
1224 dev_err(dev->udev->dev.parent,
1225 "Invalid channel number (%d)\n", channel);
1226 return;
1227 }
1228
1229 priv = dev->nets[channel];
1230
1231 if (completion_done(&priv->start_comp) &&
1232 netif_queue_stopped(priv->netdev)) {
1233 netif_wake_queue(priv->netdev);
1234 } else {
1235 netif_start_queue(priv->netdev);
1236 complete(&priv->start_comp);
1237 }
1238}
1239
1240static void kvaser_usb_stop_chip_reply(const struct kvaser_usb *dev,
1241 const struct kvaser_msg *msg)
1242{
1243 struct kvaser_usb_net_priv *priv;
1244 u8 channel = msg->u.simple.channel;
1245
1246 if (channel >= dev->nchannels) {
1247 dev_err(dev->udev->dev.parent,
1248 "Invalid channel number (%d)\n", channel);
1249 return;
1250 }
1251
1252 priv = dev->nets[channel];
1253
1254 complete(&priv->stop_comp);
1255}
1256
1257static void kvaser_usb_handle_message(const struct kvaser_usb *dev,
1258 const struct kvaser_msg *msg)
1259{
1260 switch (msg->id) {
1261 case CMD_START_CHIP_REPLY:
1262 kvaser_usb_start_chip_reply(dev, msg);
1263 break;
1264
1265 case CMD_STOP_CHIP_REPLY:
1266 kvaser_usb_stop_chip_reply(dev, msg);
1267 break;
1268
1269 case CMD_RX_STD_MESSAGE:
1270 case CMD_RX_EXT_MESSAGE:
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +02001271 kvaser_usb_rx_can_msg(dev, msg);
1272 break;
1273
1274 case CMD_LEAF_LOG_MESSAGE:
1275 if (dev->family != KVASER_LEAF)
1276 goto warn;
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001277 kvaser_usb_rx_can_msg(dev, msg);
1278 break;
1279
1280 case CMD_CHIP_STATE_EVENT:
1281 case CMD_CAN_ERROR_EVENT:
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +02001282 if (dev->family == KVASER_LEAF)
1283 kvaser_leaf_rx_error(dev, msg);
1284 else
1285 kvaser_usbcan_rx_error(dev, msg);
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001286 break;
1287
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001288 case CMD_TX_ACKNOWLEDGE:
1289 kvaser_usb_tx_acknowledge(dev, msg);
1290 break;
1291
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +02001292 /* Ignored messages */
1293 case CMD_USBCAN_CLOCK_OVERFLOW_EVENT:
1294 if (dev->family != KVASER_USBCAN)
1295 goto warn;
1296 break;
1297
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001298 default:
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +02001299warn: dev_warn(dev->udev->dev.parent,
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001300 "Unhandled message (%d)\n", msg->id);
1301 break;
1302 }
1303}
1304
1305static void kvaser_usb_read_bulk_callback(struct urb *urb)
1306{
1307 struct kvaser_usb *dev = urb->context;
1308 struct kvaser_msg *msg;
1309 int pos = 0;
1310 int err, i;
1311
1312 switch (urb->status) {
1313 case 0:
1314 break;
1315 case -ENOENT:
1316 case -ESHUTDOWN:
1317 return;
1318 default:
1319 dev_info(dev->udev->dev.parent, "Rx URB aborted (%d)\n",
1320 urb->status);
1321 goto resubmit_urb;
1322 }
1323
1324 while (pos <= urb->actual_length - MSG_HEADER_LEN) {
1325 msg = urb->transfer_buffer + pos;
1326
Ahmed S. Darwish2fec5102015-02-26 10:22:02 -05001327 /* The Kvaser firmware can only read and write messages that
1328 * does not cross the USB's endpoint wMaxPacketSize boundary.
1329 * If a follow-up command crosses such boundary, firmware puts
1330 * a placeholder zero-length command in its place then aligns
1331 * the real command to the next max packet size.
1332 *
1333 * Handle such cases or we're going to miss a significant
1334 * number of events in case of a heavy rx load on the bus.
1335 */
1336 if (msg->len == 0) {
1337 pos = round_up(pos, dev->bulk_in->wMaxPacketSize);
1338 continue;
1339 }
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001340
1341 if (pos + msg->len > urb->actual_length) {
1342 dev_err(dev->udev->dev.parent, "Format error\n");
1343 break;
1344 }
1345
1346 kvaser_usb_handle_message(dev, msg);
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001347 pos += msg->len;
1348 }
1349
1350resubmit_urb:
1351 usb_fill_bulk_urb(urb, dev->udev,
1352 usb_rcvbulkpipe(dev->udev,
1353 dev->bulk_in->bEndpointAddress),
1354 urb->transfer_buffer, RX_BUFFER_SIZE,
1355 kvaser_usb_read_bulk_callback, dev);
1356
1357 err = usb_submit_urb(urb, GFP_ATOMIC);
1358 if (err == -ENODEV) {
1359 for (i = 0; i < dev->nchannels; i++) {
1360 if (!dev->nets[i])
1361 continue;
1362
1363 netif_device_detach(dev->nets[i]->netdev);
1364 }
1365 } else if (err) {
1366 dev_err(dev->udev->dev.parent,
1367 "Failed resubmitting read bulk urb: %d\n", err);
1368 }
1369
1370 return;
1371}
1372
1373static int kvaser_usb_setup_rx_urbs(struct kvaser_usb *dev)
1374{
1375 int i, err = 0;
1376
1377 if (dev->rxinitdone)
1378 return 0;
1379
1380 for (i = 0; i < MAX_RX_URBS; i++) {
1381 struct urb *urb = NULL;
1382 u8 *buf = NULL;
1383 dma_addr_t buf_dma;
1384
1385 urb = usb_alloc_urb(0, GFP_KERNEL);
1386 if (!urb) {
1387 dev_warn(dev->udev->dev.parent,
1388 "No memory left for URBs\n");
1389 err = -ENOMEM;
1390 break;
1391 }
1392
1393 buf = usb_alloc_coherent(dev->udev, RX_BUFFER_SIZE,
1394 GFP_KERNEL, &buf_dma);
1395 if (!buf) {
1396 dev_warn(dev->udev->dev.parent,
1397 "No memory left for USB buffer\n");
1398 usb_free_urb(urb);
1399 err = -ENOMEM;
1400 break;
1401 }
1402
1403 usb_fill_bulk_urb(urb, dev->udev,
1404 usb_rcvbulkpipe(dev->udev,
1405 dev->bulk_in->bEndpointAddress),
1406 buf, RX_BUFFER_SIZE,
1407 kvaser_usb_read_bulk_callback,
1408 dev);
1409 urb->transfer_dma = buf_dma;
1410 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1411 usb_anchor_urb(urb, &dev->rx_submitted);
1412
1413 err = usb_submit_urb(urb, GFP_KERNEL);
1414 if (err) {
1415 usb_unanchor_urb(urb);
1416 usb_free_coherent(dev->udev, RX_BUFFER_SIZE, buf,
1417 buf_dma);
1418 usb_free_urb(urb);
1419 break;
1420 }
1421
1422 dev->rxbuf[i] = buf;
1423 dev->rxbuf_dma[i] = buf_dma;
1424
1425 usb_free_urb(urb);
1426 }
1427
1428 if (i == 0) {
1429 dev_warn(dev->udev->dev.parent,
1430 "Cannot setup read URBs, error %d\n", err);
1431 return err;
1432 } else if (i < MAX_RX_URBS) {
1433 dev_warn(dev->udev->dev.parent,
1434 "RX performances may be slow\n");
1435 }
1436
1437 dev->rxinitdone = true;
1438
1439 return 0;
1440}
1441
1442static int kvaser_usb_set_opt_mode(const struct kvaser_usb_net_priv *priv)
1443{
1444 struct kvaser_msg *msg;
1445 int rc;
1446
1447 msg = kmalloc(sizeof(*msg), GFP_KERNEL);
1448 if (!msg)
1449 return -ENOMEM;
1450
1451 msg->id = CMD_SET_CTRL_MODE;
1452 msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_ctrl_mode);
1453 msg->u.ctrl_mode.tid = 0xff;
1454 msg->u.ctrl_mode.channel = priv->channel;
1455
1456 if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
1457 msg->u.ctrl_mode.ctrl_mode = KVASER_CTRL_MODE_SILENT;
1458 else
1459 msg->u.ctrl_mode.ctrl_mode = KVASER_CTRL_MODE_NORMAL;
1460
1461 rc = kvaser_usb_send_msg(priv->dev, msg);
1462
1463 kfree(msg);
1464 return rc;
1465}
1466
1467static int kvaser_usb_start_chip(struct kvaser_usb_net_priv *priv)
1468{
1469 int err;
1470
1471 init_completion(&priv->start_comp);
1472
1473 err = kvaser_usb_send_simple_msg(priv->dev, CMD_START_CHIP,
1474 priv->channel);
1475 if (err)
1476 return err;
1477
1478 if (!wait_for_completion_timeout(&priv->start_comp,
1479 msecs_to_jiffies(START_TIMEOUT)))
1480 return -ETIMEDOUT;
1481
1482 return 0;
1483}
1484
1485static int kvaser_usb_open(struct net_device *netdev)
1486{
1487 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1488 struct kvaser_usb *dev = priv->dev;
1489 int err;
1490
1491 err = open_candev(netdev);
1492 if (err)
1493 return err;
1494
1495 err = kvaser_usb_setup_rx_urbs(dev);
1496 if (err)
1497 goto error;
1498
1499 err = kvaser_usb_set_opt_mode(priv);
1500 if (err)
1501 goto error;
1502
1503 err = kvaser_usb_start_chip(priv);
1504 if (err) {
1505 netdev_warn(netdev, "Cannot start device, error %d\n", err);
1506 goto error;
1507 }
1508
1509 priv->can.state = CAN_STATE_ERROR_ACTIVE;
1510
1511 return 0;
1512
1513error:
1514 close_candev(netdev);
1515 return err;
1516}
1517
1518static void kvaser_usb_unlink_all_urbs(struct kvaser_usb *dev)
1519{
1520 int i;
1521
1522 usb_kill_anchored_urbs(&dev->rx_submitted);
1523
1524 for (i = 0; i < MAX_RX_URBS; i++)
1525 usb_free_coherent(dev->udev, RX_BUFFER_SIZE,
1526 dev->rxbuf[i],
1527 dev->rxbuf_dma[i]);
1528
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +02001529 for (i = 0; i < dev->nchannels; i++) {
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001530 struct kvaser_usb_net_priv *priv = dev->nets[i];
1531
1532 if (priv)
1533 kvaser_usb_unlink_tx_urbs(priv);
1534 }
1535}
1536
1537static int kvaser_usb_stop_chip(struct kvaser_usb_net_priv *priv)
1538{
1539 int err;
1540
1541 init_completion(&priv->stop_comp);
1542
1543 err = kvaser_usb_send_simple_msg(priv->dev, CMD_STOP_CHIP,
1544 priv->channel);
1545 if (err)
1546 return err;
1547
1548 if (!wait_for_completion_timeout(&priv->stop_comp,
1549 msecs_to_jiffies(STOP_TIMEOUT)))
1550 return -ETIMEDOUT;
1551
1552 return 0;
1553}
1554
1555static int kvaser_usb_flush_queue(struct kvaser_usb_net_priv *priv)
1556{
1557 struct kvaser_msg *msg;
1558 int rc;
1559
1560 msg = kmalloc(sizeof(*msg), GFP_KERNEL);
1561 if (!msg)
1562 return -ENOMEM;
1563
1564 msg->id = CMD_FLUSH_QUEUE;
1565 msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_flush_queue);
1566 msg->u.flush_queue.channel = priv->channel;
1567 msg->u.flush_queue.flags = 0x00;
1568
1569 rc = kvaser_usb_send_msg(priv->dev, msg);
1570
1571 kfree(msg);
1572 return rc;
1573}
1574
1575static int kvaser_usb_close(struct net_device *netdev)
1576{
1577 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1578 struct kvaser_usb *dev = priv->dev;
1579 int err;
1580
1581 netif_stop_queue(netdev);
1582
1583 err = kvaser_usb_flush_queue(priv);
1584 if (err)
1585 netdev_warn(netdev, "Cannot flush queue, error %d\n", err);
1586
1587 if (kvaser_usb_send_simple_msg(dev, CMD_RESET_CHIP, priv->channel))
1588 netdev_warn(netdev, "Cannot reset card, error %d\n", err);
1589
1590 err = kvaser_usb_stop_chip(priv);
1591 if (err)
1592 netdev_warn(netdev, "Cannot stop device, error %d\n", err);
1593
Ahmed S. Darwish889b77f2015-01-05 12:52:06 -05001594 /* reset tx contexts */
1595 kvaser_usb_unlink_tx_urbs(priv);
1596
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001597 priv->can.state = CAN_STATE_STOPPED;
1598 close_candev(priv->netdev);
1599
1600 return 0;
1601}
1602
1603static void kvaser_usb_write_bulk_callback(struct urb *urb)
1604{
1605 struct kvaser_usb_tx_urb_context *context = urb->context;
1606 struct kvaser_usb_net_priv *priv;
1607 struct net_device *netdev;
1608
1609 if (WARN_ON(!context))
1610 return;
1611
1612 priv = context->priv;
1613 netdev = priv->netdev;
1614
1615 kfree(urb->transfer_buffer);
1616
1617 if (!netif_device_present(netdev))
1618 return;
1619
1620 if (urb->status)
1621 netdev_info(netdev, "Tx URB aborted (%d)\n", urb->status);
1622}
1623
1624static netdev_tx_t kvaser_usb_start_xmit(struct sk_buff *skb,
1625 struct net_device *netdev)
1626{
1627 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1628 struct kvaser_usb *dev = priv->dev;
1629 struct net_device_stats *stats = &netdev->stats;
1630 struct can_frame *cf = (struct can_frame *)skb->data;
1631 struct kvaser_usb_tx_urb_context *context = NULL;
1632 struct urb *urb;
1633 void *buf;
1634 struct kvaser_msg *msg;
Ahmed S. Darwishdeb27012015-02-26 10:20:11 -05001635 int i, err, ret = NETDEV_TX_OK;
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +02001636 u8 *msg_tx_can_flags = NULL; /* GCC */
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001637
1638 if (can_dropped_invalid_skb(netdev, skb))
1639 return NETDEV_TX_OK;
1640
1641 urb = usb_alloc_urb(0, GFP_ATOMIC);
1642 if (!urb) {
1643 netdev_err(netdev, "No memory left for URBs\n");
1644 stats->tx_dropped++;
Ahmed S. Darwishb4427232015-01-05 12:49:10 -05001645 dev_kfree_skb(skb);
1646 return NETDEV_TX_OK;
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001647 }
1648
1649 buf = kmalloc(sizeof(struct kvaser_msg), GFP_ATOMIC);
1650 if (!buf) {
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001651 stats->tx_dropped++;
Ahmed S. Darwishb4427232015-01-05 12:49:10 -05001652 dev_kfree_skb(skb);
Ahmed S. Darwishdeb27012015-02-26 10:20:11 -05001653 goto freeurb;
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001654 }
1655
1656 msg = buf;
1657 msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_tx_can);
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001658 msg->u.tx_can.channel = priv->channel;
1659
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +02001660 switch (dev->family) {
1661 case KVASER_LEAF:
1662 msg_tx_can_flags = &msg->u.tx_can.leaf.flags;
1663 break;
1664 case KVASER_USBCAN:
1665 msg_tx_can_flags = &msg->u.tx_can.usbcan.flags;
1666 break;
1667 }
1668
1669 *msg_tx_can_flags = 0;
1670
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001671 if (cf->can_id & CAN_EFF_FLAG) {
1672 msg->id = CMD_TX_EXT_MESSAGE;
1673 msg->u.tx_can.msg[0] = (cf->can_id >> 24) & 0x1f;
1674 msg->u.tx_can.msg[1] = (cf->can_id >> 18) & 0x3f;
1675 msg->u.tx_can.msg[2] = (cf->can_id >> 14) & 0x0f;
1676 msg->u.tx_can.msg[3] = (cf->can_id >> 6) & 0xff;
1677 msg->u.tx_can.msg[4] = cf->can_id & 0x3f;
1678 } else {
1679 msg->id = CMD_TX_STD_MESSAGE;
1680 msg->u.tx_can.msg[0] = (cf->can_id >> 6) & 0x1f;
1681 msg->u.tx_can.msg[1] = cf->can_id & 0x3f;
1682 }
1683
1684 msg->u.tx_can.msg[5] = cf->can_dlc;
1685 memcpy(&msg->u.tx_can.msg[6], cf->data, cf->can_dlc);
1686
1687 if (cf->can_id & CAN_RTR_FLAG)
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +02001688 *msg_tx_can_flags |= MSG_FLAG_REMOTE_FRAME;
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001689
1690 for (i = 0; i < ARRAY_SIZE(priv->tx_contexts); i++) {
1691 if (priv->tx_contexts[i].echo_index == MAX_TX_URBS) {
1692 context = &priv->tx_contexts[i];
1693 break;
1694 }
1695 }
1696
Ahmed S. Darwishb4427232015-01-05 12:49:10 -05001697 /* This should never happen; it implies a flow control bug */
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001698 if (!context) {
1699 netdev_warn(netdev, "cannot find free context\n");
Ahmed S. Darwishdeb27012015-02-26 10:20:11 -05001700
1701 kfree(buf);
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001702 ret = NETDEV_TX_BUSY;
Ahmed S. Darwishdeb27012015-02-26 10:20:11 -05001703 goto freeurb;
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001704 }
1705
1706 context->priv = priv;
1707 context->echo_index = i;
1708 context->dlc = cf->can_dlc;
1709
1710 msg->u.tx_can.tid = context->echo_index;
1711
1712 usb_fill_bulk_urb(urb, dev->udev,
1713 usb_sndbulkpipe(dev->udev,
1714 dev->bulk_out->bEndpointAddress),
1715 buf, msg->len,
1716 kvaser_usb_write_bulk_callback, context);
1717 usb_anchor_urb(urb, &priv->tx_submitted);
1718
1719 can_put_echo_skb(skb, netdev, context->echo_index);
1720
1721 atomic_inc(&priv->active_tx_urbs);
1722
1723 if (atomic_read(&priv->active_tx_urbs) >= MAX_TX_URBS)
1724 netif_stop_queue(netdev);
1725
1726 err = usb_submit_urb(urb, GFP_ATOMIC);
1727 if (unlikely(err)) {
1728 can_free_echo_skb(netdev, context->echo_index);
1729
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001730 atomic_dec(&priv->active_tx_urbs);
1731 usb_unanchor_urb(urb);
1732
1733 stats->tx_dropped++;
1734
1735 if (err == -ENODEV)
1736 netif_device_detach(netdev);
1737 else
1738 netdev_warn(netdev, "Failed tx_urb %d\n", err);
1739
Ahmed S. Darwishdeb27012015-02-26 10:20:11 -05001740 goto freeurb;
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001741 }
1742
Ahmed S. Darwishdeb27012015-02-26 10:20:11 -05001743 ret = NETDEV_TX_OK;
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001744
Ahmed S. Darwishdeb27012015-02-26 10:20:11 -05001745freeurb:
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001746 usb_free_urb(urb);
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001747 return ret;
1748}
1749
1750static const struct net_device_ops kvaser_usb_netdev_ops = {
1751 .ndo_open = kvaser_usb_open,
1752 .ndo_stop = kvaser_usb_close,
1753 .ndo_start_xmit = kvaser_usb_start_xmit,
Oliver Hartkoppc971fa22014-03-07 09:23:41 +01001754 .ndo_change_mtu = can_change_mtu,
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001755};
1756
1757static const struct can_bittiming_const kvaser_usb_bittiming_const = {
1758 .name = "kvaser_usb",
1759 .tseg1_min = KVASER_USB_TSEG1_MIN,
1760 .tseg1_max = KVASER_USB_TSEG1_MAX,
1761 .tseg2_min = KVASER_USB_TSEG2_MIN,
1762 .tseg2_max = KVASER_USB_TSEG2_MAX,
1763 .sjw_max = KVASER_USB_SJW_MAX,
1764 .brp_min = KVASER_USB_BRP_MIN,
1765 .brp_max = KVASER_USB_BRP_MAX,
1766 .brp_inc = KVASER_USB_BRP_INC,
1767};
1768
1769static int kvaser_usb_set_bittiming(struct net_device *netdev)
1770{
1771 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1772 struct can_bittiming *bt = &priv->can.bittiming;
1773 struct kvaser_usb *dev = priv->dev;
1774 struct kvaser_msg *msg;
1775 int rc;
1776
1777 msg = kmalloc(sizeof(*msg), GFP_KERNEL);
1778 if (!msg)
1779 return -ENOMEM;
1780
1781 msg->id = CMD_SET_BUS_PARAMS;
1782 msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_busparams);
1783 msg->u.busparams.channel = priv->channel;
1784 msg->u.busparams.tid = 0xff;
1785 msg->u.busparams.bitrate = cpu_to_le32(bt->bitrate);
1786 msg->u.busparams.sjw = bt->sjw;
1787 msg->u.busparams.tseg1 = bt->prop_seg + bt->phase_seg1;
1788 msg->u.busparams.tseg2 = bt->phase_seg2;
1789
1790 if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
1791 msg->u.busparams.no_samp = 3;
1792 else
1793 msg->u.busparams.no_samp = 1;
1794
1795 rc = kvaser_usb_send_msg(dev, msg);
1796
1797 kfree(msg);
1798 return rc;
1799}
1800
1801static int kvaser_usb_set_mode(struct net_device *netdev,
1802 enum can_mode mode)
1803{
1804 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1805 int err;
1806
1807 switch (mode) {
1808 case CAN_MODE_START:
1809 err = kvaser_usb_simple_msg_async(priv, CMD_START_CHIP);
1810 if (err)
1811 return err;
1812 break;
1813 default:
1814 return -EOPNOTSUPP;
1815 }
1816
1817 return 0;
1818}
1819
1820static int kvaser_usb_get_berr_counter(const struct net_device *netdev,
1821 struct can_berr_counter *bec)
1822{
1823 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1824
1825 *bec = priv->bec;
1826
1827 return 0;
1828}
1829
1830static void kvaser_usb_remove_interfaces(struct kvaser_usb *dev)
1831{
1832 int i;
1833
1834 for (i = 0; i < dev->nchannels; i++) {
1835 if (!dev->nets[i])
1836 continue;
1837
1838 unregister_netdev(dev->nets[i]->netdev);
1839 }
1840
1841 kvaser_usb_unlink_all_urbs(dev);
1842
1843 for (i = 0; i < dev->nchannels; i++) {
1844 if (!dev->nets[i])
1845 continue;
1846
1847 free_candev(dev->nets[i]->netdev);
1848 }
1849}
1850
1851static int kvaser_usb_init_one(struct usb_interface *intf,
1852 const struct usb_device_id *id, int channel)
1853{
1854 struct kvaser_usb *dev = usb_get_intfdata(intf);
1855 struct net_device *netdev;
1856 struct kvaser_usb_net_priv *priv;
1857 int i, err;
1858
Ahmed S. Darwish5e7e6e02015-01-05 12:57:13 -05001859 err = kvaser_usb_send_simple_msg(dev, CMD_RESET_CHIP, channel);
1860 if (err)
1861 return err;
1862
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001863 netdev = alloc_candev(sizeof(*priv), MAX_TX_URBS);
1864 if (!netdev) {
1865 dev_err(&intf->dev, "Cannot alloc candev\n");
1866 return -ENOMEM;
1867 }
1868
1869 priv = netdev_priv(netdev);
1870
1871 init_completion(&priv->start_comp);
1872 init_completion(&priv->stop_comp);
1873
1874 init_usb_anchor(&priv->tx_submitted);
1875 atomic_set(&priv->active_tx_urbs, 0);
1876
1877 for (i = 0; i < ARRAY_SIZE(priv->tx_contexts); i++)
1878 priv->tx_contexts[i].echo_index = MAX_TX_URBS;
1879
1880 priv->dev = dev;
1881 priv->netdev = netdev;
1882 priv->channel = channel;
1883
1884 priv->can.state = CAN_STATE_STOPPED;
1885 priv->can.clock.freq = CAN_USB_CLOCK;
1886 priv->can.bittiming_const = &kvaser_usb_bittiming_const;
1887 priv->can.do_set_bittiming = kvaser_usb_set_bittiming;
1888 priv->can.do_set_mode = kvaser_usb_set_mode;
1889 if (id->driver_info & KVASER_HAS_TXRX_ERRORS)
1890 priv->can.do_get_berr_counter = kvaser_usb_get_berr_counter;
1891 priv->can.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES;
1892 if (id->driver_info & KVASER_HAS_SILENT_MODE)
1893 priv->can.ctrlmode_supported |= CAN_CTRLMODE_LISTENONLY;
1894
1895 netdev->flags |= IFF_ECHO;
1896
1897 netdev->netdev_ops = &kvaser_usb_netdev_ops;
1898
1899 SET_NETDEV_DEV(netdev, &intf->dev);
Christopher R. Baker3e66d012014-03-08 11:00:20 -05001900 netdev->dev_id = channel;
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001901
1902 dev->nets[channel] = priv;
1903
1904 err = register_candev(netdev);
1905 if (err) {
1906 dev_err(&intf->dev, "Failed to register can device\n");
1907 free_candev(netdev);
1908 dev->nets[channel] = NULL;
1909 return err;
1910 }
1911
1912 netdev_dbg(netdev, "device registered\n");
1913
1914 return 0;
1915}
1916
Olivier Sobrie896e23b2013-10-27 22:07:53 +01001917static int kvaser_usb_get_endpoints(const struct usb_interface *intf,
1918 struct usb_endpoint_descriptor **in,
1919 struct usb_endpoint_descriptor **out)
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001920{
1921 const struct usb_host_interface *iface_desc;
1922 struct usb_endpoint_descriptor *endpoint;
1923 int i;
1924
1925 iface_desc = &intf->altsetting[0];
1926
1927 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1928 endpoint = &iface_desc->endpoint[i].desc;
1929
Olivier Sobrie896e23b2013-10-27 22:07:53 +01001930 if (!*in && usb_endpoint_is_bulk_in(endpoint))
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001931 *in = endpoint;
1932
Olivier Sobrie896e23b2013-10-27 22:07:53 +01001933 if (!*out && usb_endpoint_is_bulk_out(endpoint))
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001934 *out = endpoint;
Olivier Sobrie896e23b2013-10-27 22:07:53 +01001935
1936 /* use first bulk endpoint for in and out */
1937 if (*in && *out)
1938 return 0;
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001939 }
Olivier Sobrie896e23b2013-10-27 22:07:53 +01001940
1941 return -ENODEV;
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001942}
1943
1944static int kvaser_usb_probe(struct usb_interface *intf,
1945 const struct usb_device_id *id)
1946{
1947 struct kvaser_usb *dev;
1948 int err = -ENOMEM;
Ahmed S. Darwish14c10c22015-01-26 07:24:06 +02001949 int i, retry = 3;
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001950
1951 dev = devm_kzalloc(&intf->dev, sizeof(*dev), GFP_KERNEL);
1952 if (!dev)
1953 return -ENOMEM;
1954
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +02001955 if (kvaser_is_leaf(id)) {
1956 dev->family = KVASER_LEAF;
1957 } else if (kvaser_is_usbcan(id)) {
1958 dev->family = KVASER_USBCAN;
1959 } else {
1960 dev_err(&intf->dev,
1961 "Product ID (%d) does not belong to any known Kvaser USB family",
1962 id->idProduct);
1963 return -ENODEV;
1964 }
1965
Olivier Sobrie896e23b2013-10-27 22:07:53 +01001966 err = kvaser_usb_get_endpoints(intf, &dev->bulk_in, &dev->bulk_out);
1967 if (err) {
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001968 dev_err(&intf->dev, "Cannot get usb endpoint(s)");
1969 return err;
1970 }
1971
1972 dev->udev = interface_to_usbdev(intf);
1973
1974 init_usb_anchor(&dev->rx_submitted);
1975
1976 usb_set_intfdata(intf, dev);
1977
Ahmed S. Darwish14c10c22015-01-26 07:24:06 +02001978 /* On some x86 laptops, plugging a Kvaser device again after
1979 * an unplug makes the firmware always ignore the very first
1980 * command. For such a case, provide some room for retries
1981 * instead of completely exiting the driver.
1982 */
1983 do {
1984 err = kvaser_usb_get_software_info(dev);
1985 } while (--retry && err == -ETIMEDOUT);
1986
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001987 if (err) {
1988 dev_err(&intf->dev,
1989 "Cannot get software infos, error %d\n", err);
1990 return err;
1991 }
1992
1993 err = kvaser_usb_get_card_info(dev);
1994 if (err) {
1995 dev_err(&intf->dev,
1996 "Cannot get card infos, error %d\n", err);
1997 return err;
1998 }
1999
2000 dev_dbg(&intf->dev, "Firmware version: %d.%d.%d\n",
2001 ((dev->fw_version >> 24) & 0xff),
2002 ((dev->fw_version >> 16) & 0xff),
2003 (dev->fw_version & 0xffff));
2004
2005 for (i = 0; i < dev->nchannels; i++) {
2006 err = kvaser_usb_init_one(intf, id, i);
2007 if (err) {
2008 kvaser_usb_remove_interfaces(dev);
2009 return err;
2010 }
2011 }
2012
2013 return 0;
2014}
2015
2016static void kvaser_usb_disconnect(struct usb_interface *intf)
2017{
2018 struct kvaser_usb *dev = usb_get_intfdata(intf);
2019
2020 usb_set_intfdata(intf, NULL);
2021
2022 if (!dev)
2023 return;
2024
2025 kvaser_usb_remove_interfaces(dev);
2026}
2027
2028static struct usb_driver kvaser_usb_driver = {
2029 .name = "kvaser_usb",
2030 .probe = kvaser_usb_probe,
2031 .disconnect = kvaser_usb_disconnect,
2032 .id_table = kvaser_usb_table,
2033};
2034
2035module_usb_driver(kvaser_usb_driver);
2036
2037MODULE_AUTHOR("Olivier Sobrie <olivier@sobrie.be>");
2038MODULE_DESCRIPTION("CAN driver for Kvaser CAN/USB devices");
2039MODULE_LICENSE("GPL v2");