blob: 022bfa13ebfa0c85491cfc2cb17a3437c01f0205 [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. Darwisha9dc9602015-03-14 09:02:49 -040017#include <linux/spinlock.h>
Ahmed S. Darwish2fec5102015-02-26 10:22:02 -050018#include <linux/kernel.h>
Olivier Sobrie080f40a2012-11-21 08:11:13 +010019#include <linux/completion.h>
20#include <linux/module.h>
21#include <linux/netdevice.h>
22#include <linux/usb.h>
23
24#include <linux/can.h>
25#include <linux/can/dev.h>
26#include <linux/can/error.h>
27
Olivier Sobrie080f40a2012-11-21 08:11:13 +010028#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
Ahmed S. Darwish082d70b2015-03-15 11:03:38 -0400445/* Context for an outstanding, not yet ACKed, transmission */
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100446struct kvaser_usb_tx_urb_context {
447 struct kvaser_usb_net_priv *priv;
448 u32 echo_index;
449 int dlc;
450};
451
452struct kvaser_usb {
453 struct usb_device *udev;
454 struct kvaser_usb_net_priv *nets[MAX_NET_DEVICES];
455
456 struct usb_endpoint_descriptor *bulk_in, *bulk_out;
457 struct usb_anchor rx_submitted;
458
Ahmed S. Darwish082d70b2015-03-15 11:03:38 -0400459 /* @max_tx_urbs: Firmware-reported maximum number of oustanding,
460 * not yet ACKed, transmissions on this device. This value is
461 * also used as a sentinel for marking free tx contexts.
462 */
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100463 u32 fw_version;
464 unsigned int nchannels;
Ahmed S. Darwish082d70b2015-03-15 11:03:38 -0400465 unsigned int max_tx_urbs;
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +0200466 enum kvaser_usb_family family;
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100467
468 bool rxinitdone;
469 void *rxbuf[MAX_RX_URBS];
470 dma_addr_t rxbuf_dma[MAX_RX_URBS];
471};
472
473struct kvaser_usb_net_priv {
474 struct can_priv can;
Ahmed S. Darwish082d70b2015-03-15 11:03:38 -0400475 struct can_berr_counter bec;
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100476
477 struct kvaser_usb *dev;
478 struct net_device *netdev;
479 int channel;
480
Ahmed S. Darwish082d70b2015-03-15 11:03:38 -0400481 struct completion start_comp, stop_comp;
482 struct usb_anchor tx_submitted;
483
484 spinlock_t tx_contexts_lock;
485 int active_tx_contexts;
486 struct kvaser_usb_tx_urb_context tx_contexts[];
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100487};
488
489static const struct usb_device_id kvaser_usb_table[] = {
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +0200490 /* Leaf family IDs */
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100491 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_DEVEL_PRODUCT_ID) },
492 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_PRODUCT_ID) },
493 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_PRODUCT_ID),
494 .driver_info = KVASER_HAS_TXRX_ERRORS |
495 KVASER_HAS_SILENT_MODE },
496 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_SPRO_PRODUCT_ID),
497 .driver_info = KVASER_HAS_TXRX_ERRORS |
498 KVASER_HAS_SILENT_MODE },
499 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_LS_PRODUCT_ID),
500 .driver_info = KVASER_HAS_TXRX_ERRORS |
501 KVASER_HAS_SILENT_MODE },
502 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_SWC_PRODUCT_ID),
503 .driver_info = KVASER_HAS_TXRX_ERRORS |
504 KVASER_HAS_SILENT_MODE },
505 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_LIN_PRODUCT_ID),
506 .driver_info = KVASER_HAS_TXRX_ERRORS |
507 KVASER_HAS_SILENT_MODE },
508 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_SPRO_LS_PRODUCT_ID),
509 .driver_info = KVASER_HAS_TXRX_ERRORS |
510 KVASER_HAS_SILENT_MODE },
511 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_SPRO_SWC_PRODUCT_ID),
512 .driver_info = KVASER_HAS_TXRX_ERRORS |
513 KVASER_HAS_SILENT_MODE },
514 { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO2_DEVEL_PRODUCT_ID),
515 .driver_info = KVASER_HAS_TXRX_ERRORS |
516 KVASER_HAS_SILENT_MODE },
517 { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO2_HSHS_PRODUCT_ID),
518 .driver_info = KVASER_HAS_TXRX_ERRORS |
519 KVASER_HAS_SILENT_MODE },
520 { USB_DEVICE(KVASER_VENDOR_ID, USB_UPRO_HSHS_PRODUCT_ID),
521 .driver_info = KVASER_HAS_TXRX_ERRORS },
522 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_GI_PRODUCT_ID) },
523 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_OBDII_PRODUCT_ID),
524 .driver_info = KVASER_HAS_TXRX_ERRORS |
525 KVASER_HAS_SILENT_MODE },
526 { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO2_HSLS_PRODUCT_ID),
527 .driver_info = KVASER_HAS_TXRX_ERRORS },
528 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_CH_PRODUCT_ID),
529 .driver_info = KVASER_HAS_TXRX_ERRORS },
530 { USB_DEVICE(KVASER_VENDOR_ID, USB_BLACKBIRD_SPRO_PRODUCT_ID),
531 .driver_info = KVASER_HAS_TXRX_ERRORS },
532 { USB_DEVICE(KVASER_VENDOR_ID, USB_OEM_MERCURY_PRODUCT_ID),
533 .driver_info = KVASER_HAS_TXRX_ERRORS },
534 { USB_DEVICE(KVASER_VENDOR_ID, USB_OEM_LEAF_PRODUCT_ID),
535 .driver_info = KVASER_HAS_TXRX_ERRORS },
536 { USB_DEVICE(KVASER_VENDOR_ID, USB_CAN_R_PRODUCT_ID),
537 .driver_info = KVASER_HAS_TXRX_ERRORS },
Olivier Sobrieb4f20132014-04-10 21:44:22 +0200538 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_V2_PRODUCT_ID) },
539 { USB_DEVICE(KVASER_VENDOR_ID, USB_MINI_PCIE_HS_PRODUCT_ID) },
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +0200540
541 /* USBCANII family IDs */
542 { USB_DEVICE(KVASER_VENDOR_ID, USB_USBCAN2_PRODUCT_ID),
543 .driver_info = KVASER_HAS_TXRX_ERRORS },
544 { USB_DEVICE(KVASER_VENDOR_ID, USB_USBCAN_REVB_PRODUCT_ID),
545 .driver_info = KVASER_HAS_TXRX_ERRORS },
546 { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMORATOR_PRODUCT_ID),
547 .driver_info = KVASER_HAS_TXRX_ERRORS },
548 { USB_DEVICE(KVASER_VENDOR_ID, USB_VCI2_PRODUCT_ID),
549 .driver_info = KVASER_HAS_TXRX_ERRORS },
550
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100551 { }
552};
553MODULE_DEVICE_TABLE(usb, kvaser_usb_table);
554
555static inline int kvaser_usb_send_msg(const struct kvaser_usb *dev,
556 struct kvaser_msg *msg)
557{
558 int actual_len;
559
560 return usb_bulk_msg(dev->udev,
561 usb_sndbulkpipe(dev->udev,
562 dev->bulk_out->bEndpointAddress),
563 msg, msg->len, &actual_len,
564 USB_SEND_TIMEOUT);
565}
566
567static int kvaser_usb_wait_msg(const struct kvaser_usb *dev, u8 id,
568 struct kvaser_msg *msg)
569{
570 struct kvaser_msg *tmp;
571 void *buf;
572 int actual_len;
573 int err;
Olivier Sobriee59e36e2014-04-10 21:44:21 +0200574 int pos;
575 unsigned long to = jiffies + msecs_to_jiffies(USB_RECV_TIMEOUT);
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100576
577 buf = kzalloc(RX_BUFFER_SIZE, GFP_KERNEL);
578 if (!buf)
579 return -ENOMEM;
580
Olivier Sobriee59e36e2014-04-10 21:44:21 +0200581 do {
582 err = usb_bulk_msg(dev->udev,
583 usb_rcvbulkpipe(dev->udev,
584 dev->bulk_in->bEndpointAddress),
585 buf, RX_BUFFER_SIZE, &actual_len,
586 USB_RECV_TIMEOUT);
587 if (err < 0)
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100588 goto end;
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100589
Olivier Sobriee59e36e2014-04-10 21:44:21 +0200590 pos = 0;
591 while (pos <= actual_len - MSG_HEADER_LEN) {
592 tmp = buf + pos;
593
Ahmed S. Darwish2fec5102015-02-26 10:22:02 -0500594 /* Handle messages crossing the USB endpoint max packet
595 * size boundary. Check kvaser_usb_read_bulk_callback()
596 * for further details.
597 */
598 if (tmp->len == 0) {
Ahmed S. Darwish7e184c22015-03-15 11:10:49 -0400599 pos = round_up(pos, le16_to_cpu(dev->bulk_in->
600 wMaxPacketSize));
Ahmed S. Darwish2fec5102015-02-26 10:22:02 -0500601 continue;
602 }
Olivier Sobriee59e36e2014-04-10 21:44:21 +0200603
604 if (pos + tmp->len > actual_len) {
605 dev_err(dev->udev->dev.parent,
606 "Format error\n");
607 break;
608 }
609
610 if (tmp->id == id) {
611 memcpy(msg, tmp, tmp->len);
612 goto end;
613 }
614
615 pos += tmp->len;
616 }
617 } while (time_before(jiffies, to));
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100618
619 err = -EINVAL;
620
621end:
622 kfree(buf);
623
624 return err;
625}
626
627static int kvaser_usb_send_simple_msg(const struct kvaser_usb *dev,
628 u8 msg_id, int channel)
629{
630 struct kvaser_msg *msg;
631 int rc;
632
633 msg = kmalloc(sizeof(*msg), GFP_KERNEL);
634 if (!msg)
635 return -ENOMEM;
636
637 msg->id = msg_id;
638 msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_simple);
639 msg->u.simple.channel = channel;
640 msg->u.simple.tid = 0xff;
641
642 rc = kvaser_usb_send_msg(dev, msg);
643
644 kfree(msg);
645 return rc;
646}
647
648static int kvaser_usb_get_software_info(struct kvaser_usb *dev)
649{
650 struct kvaser_msg msg;
651 int err;
652
653 err = kvaser_usb_send_simple_msg(dev, CMD_GET_SOFTWARE_INFO, 0);
654 if (err)
655 return err;
656
657 err = kvaser_usb_wait_msg(dev, CMD_GET_SOFTWARE_INFO_REPLY, &msg);
658 if (err)
659 return err;
660
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +0200661 switch (dev->family) {
662 case KVASER_LEAF:
663 dev->fw_version = le32_to_cpu(msg.u.leaf.softinfo.fw_version);
Ahmed S. Darwish082d70b2015-03-15 11:03:38 -0400664 dev->max_tx_urbs =
665 le16_to_cpu(msg.u.leaf.softinfo.max_outstanding_tx);
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +0200666 break;
667 case KVASER_USBCAN:
668 dev->fw_version = le32_to_cpu(msg.u.usbcan.softinfo.fw_version);
Ahmed S. Darwish082d70b2015-03-15 11:03:38 -0400669 dev->max_tx_urbs =
670 le16_to_cpu(msg.u.usbcan.softinfo.max_outstanding_tx);
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +0200671 break;
672 }
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100673
674 return 0;
675}
676
677static int kvaser_usb_get_card_info(struct kvaser_usb *dev)
678{
679 struct kvaser_msg msg;
680 int err;
681
682 err = kvaser_usb_send_simple_msg(dev, CMD_GET_CARD_INFO, 0);
683 if (err)
684 return err;
685
686 err = kvaser_usb_wait_msg(dev, CMD_GET_CARD_INFO_REPLY, &msg);
687 if (err)
688 return err;
689
690 dev->nchannels = msg.u.cardinfo.nchannels;
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +0200691 if ((dev->nchannels > MAX_NET_DEVICES) ||
692 (dev->family == KVASER_USBCAN &&
693 dev->nchannels > MAX_USBCAN_NET_DEVICES))
Olivier Sobrie862474f82014-02-11 11:01:23 +0100694 return -EINVAL;
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100695
696 return 0;
697}
698
699static void kvaser_usb_tx_acknowledge(const struct kvaser_usb *dev,
700 const struct kvaser_msg *msg)
701{
702 struct net_device_stats *stats;
703 struct kvaser_usb_tx_urb_context *context;
704 struct kvaser_usb_net_priv *priv;
705 struct sk_buff *skb;
706 struct can_frame *cf;
Ahmed S. Darwisha9dc9602015-03-14 09:02:49 -0400707 unsigned long flags;
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +0200708 u8 channel, tid;
709
710 channel = msg->u.tx_acknowledge_header.channel;
711 tid = msg->u.tx_acknowledge_header.tid;
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100712
713 if (channel >= dev->nchannels) {
714 dev_err(dev->udev->dev.parent,
715 "Invalid channel number (%d)\n", channel);
716 return;
717 }
718
719 priv = dev->nets[channel];
720
721 if (!netif_device_present(priv->netdev))
722 return;
723
724 stats = &priv->netdev->stats;
725
Ahmed S. Darwish082d70b2015-03-15 11:03:38 -0400726 context = &priv->tx_contexts[tid % dev->max_tx_urbs];
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100727
728 /* Sometimes the state change doesn't come after a bus-off event */
729 if (priv->can.restart_ms &&
730 (priv->can.state >= CAN_STATE_BUS_OFF)) {
731 skb = alloc_can_err_skb(priv->netdev, &cf);
732 if (skb) {
733 cf->can_id |= CAN_ERR_RESTARTED;
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100734
735 stats->rx_packets++;
736 stats->rx_bytes += cf->can_dlc;
Ahmed S. Darwisha58518c2015-01-11 15:49:52 -0500737 netif_rx(skb);
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100738 } else {
739 netdev_err(priv->netdev,
740 "No memory left for err_skb\n");
741 }
742
743 priv->can.can_stats.restarts++;
744 netif_carrier_on(priv->netdev);
745
746 priv->can.state = CAN_STATE_ERROR_ACTIVE;
747 }
748
749 stats->tx_packets++;
750 stats->tx_bytes += context->dlc;
Ahmed S. Darwisha9dc9602015-03-14 09:02:49 -0400751
752 spin_lock_irqsave(&priv->tx_contexts_lock, flags);
753
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100754 can_get_echo_skb(priv->netdev, context->echo_index);
Ahmed S. Darwish082d70b2015-03-15 11:03:38 -0400755 context->echo_index = dev->max_tx_urbs;
Ahmed S. Darwisha9dc9602015-03-14 09:02:49 -0400756 --priv->active_tx_contexts;
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100757 netif_wake_queue(priv->netdev);
Ahmed S. Darwisha9dc9602015-03-14 09:02:49 -0400758
759 spin_unlock_irqrestore(&priv->tx_contexts_lock, flags);
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100760}
761
762static void kvaser_usb_simple_msg_callback(struct urb *urb)
763{
764 struct net_device *netdev = urb->context;
765
766 kfree(urb->transfer_buffer);
767
768 if (urb->status)
769 netdev_warn(netdev, "urb status received: %d\n",
770 urb->status);
771}
772
773static int kvaser_usb_simple_msg_async(struct kvaser_usb_net_priv *priv,
774 u8 msg_id)
775{
776 struct kvaser_usb *dev = priv->dev;
777 struct net_device *netdev = priv->netdev;
778 struct kvaser_msg *msg;
779 struct urb *urb;
780 void *buf;
781 int err;
782
783 urb = usb_alloc_urb(0, GFP_ATOMIC);
784 if (!urb) {
785 netdev_err(netdev, "No memory left for URBs\n");
786 return -ENOMEM;
787 }
788
789 buf = kmalloc(sizeof(struct kvaser_msg), GFP_ATOMIC);
790 if (!buf) {
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100791 usb_free_urb(urb);
792 return -ENOMEM;
793 }
794
795 msg = (struct kvaser_msg *)buf;
796 msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_simple);
797 msg->id = msg_id;
798 msg->u.simple.channel = priv->channel;
799
800 usb_fill_bulk_urb(urb, dev->udev,
801 usb_sndbulkpipe(dev->udev,
802 dev->bulk_out->bEndpointAddress),
803 buf, msg->len,
Ahmed S. Darwish3803fa62015-01-26 07:22:54 +0200804 kvaser_usb_simple_msg_callback, netdev);
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100805 usb_anchor_urb(urb, &priv->tx_submitted);
806
807 err = usb_submit_urb(urb, GFP_ATOMIC);
808 if (err) {
809 netdev_err(netdev, "Error transmitting URB\n");
810 usb_unanchor_urb(urb);
811 usb_free_urb(urb);
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100812 return err;
813 }
814
815 usb_free_urb(urb);
816
817 return 0;
818}
819
Ahmed S. Darwish3b07a442015-01-26 07:27:19 +0200820static void kvaser_usb_rx_error_update_can_state(struct kvaser_usb_net_priv *priv,
Ahmed S. Darwish96d7f102015-01-26 07:29:15 +0200821 const struct kvaser_usb_error_summary *es,
822 struct can_frame *cf)
Ahmed S. Darwish3b07a442015-01-26 07:27:19 +0200823{
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +0200824 struct kvaser_usb *dev = priv->dev;
825 struct net_device_stats *stats = &priv->netdev->stats;
Ahmed S. Darwish96d7f102015-01-26 07:29:15 +0200826 enum can_state cur_state, new_state, tx_state, rx_state;
Ahmed S. Darwish3b07a442015-01-26 07:27:19 +0200827
828 netdev_dbg(priv->netdev, "Error status: 0x%02x\n", es->status);
829
Ahmed S. Darwish96d7f102015-01-26 07:29:15 +0200830 new_state = cur_state = priv->can.state;
831
832 if (es->status & (M16C_STATE_BUS_OFF | M16C_STATE_BUS_RESET))
Ahmed S. Darwish3b07a442015-01-26 07:27:19 +0200833 new_state = CAN_STATE_BUS_OFF;
Ahmed S. Darwish96d7f102015-01-26 07:29:15 +0200834 else if (es->status & M16C_STATE_BUS_PASSIVE)
Ahmed S. Darwish3b07a442015-01-26 07:27:19 +0200835 new_state = CAN_STATE_ERROR_PASSIVE;
Ahmed S. Darwish96d7f102015-01-26 07:29:15 +0200836 else if (es->status & M16C_STATE_BUS_ERROR) {
Ahmed S. Darwisha9ca6e12015-02-02 15:15:55 -0500837 /* Guard against spurious error events after a busoff */
838 if (cur_state < CAN_STATE_BUS_OFF) {
839 if ((es->txerr >= 128) || (es->rxerr >= 128))
840 new_state = CAN_STATE_ERROR_PASSIVE;
841 else if ((es->txerr >= 96) || (es->rxerr >= 96))
842 new_state = CAN_STATE_ERROR_WARNING;
843 else if (cur_state > CAN_STATE_ERROR_ACTIVE)
844 new_state = CAN_STATE_ERROR_ACTIVE;
845 }
Ahmed S. Darwish3b07a442015-01-26 07:27:19 +0200846 }
847
848 if (!es->status)
849 new_state = CAN_STATE_ERROR_ACTIVE;
850
Ahmed S. Darwish96d7f102015-01-26 07:29:15 +0200851 if (new_state != cur_state) {
852 tx_state = (es->txerr >= es->rxerr) ? new_state : 0;
853 rx_state = (es->txerr <= es->rxerr) ? new_state : 0;
854
855 can_change_state(priv->netdev, cf, tx_state, rx_state);
856 }
857
Ahmed S. Darwish3b07a442015-01-26 07:27:19 +0200858 if (priv->can.restart_ms &&
Ahmed S. Darwish96d7f102015-01-26 07:29:15 +0200859 (cur_state >= CAN_STATE_BUS_OFF) &&
Ahmed S. Darwish3b07a442015-01-26 07:27:19 +0200860 (new_state < CAN_STATE_BUS_OFF)) {
861 priv->can.can_stats.restarts++;
862 }
863
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +0200864 switch (dev->family) {
865 case KVASER_LEAF:
866 if (es->leaf.error_factor) {
867 priv->can.can_stats.bus_error++;
868 stats->rx_errors++;
869 }
870 break;
871 case KVASER_USBCAN:
872 if (es->usbcan.error_state & USBCAN_ERROR_STATE_TX_ERROR)
873 stats->tx_errors++;
874 if (es->usbcan.error_state & USBCAN_ERROR_STATE_RX_ERROR)
875 stats->rx_errors++;
876 if (es->usbcan.error_state & USBCAN_ERROR_STATE_BUSERROR) {
877 priv->can.can_stats.bus_error++;
878 }
879 break;
Ahmed S. Darwish3b07a442015-01-26 07:27:19 +0200880 }
881
882 priv->bec.txerr = es->txerr;
883 priv->bec.rxerr = es->rxerr;
Ahmed S. Darwish3b07a442015-01-26 07:27:19 +0200884}
885
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100886static void kvaser_usb_rx_error(const struct kvaser_usb *dev,
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +0200887 const struct kvaser_usb_error_summary *es)
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100888{
Ahmed S. Darwish96d7f102015-01-26 07:29:15 +0200889 struct can_frame *cf, tmp_cf = { .can_id = CAN_ERR_FLAG, .can_dlc = CAN_ERR_DLC };
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100890 struct sk_buff *skb;
891 struct net_device_stats *stats;
892 struct kvaser_usb_net_priv *priv;
Ahmed S. Darwish96d7f102015-01-26 07:29:15 +0200893 enum can_state old_state, new_state;
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100894
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +0200895 if (es->channel >= dev->nchannels) {
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100896 dev_err(dev->udev->dev.parent,
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +0200897 "Invalid channel number (%d)\n", es->channel);
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100898 return;
899 }
900
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +0200901 priv = dev->nets[es->channel];
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100902 stats = &priv->netdev->stats;
903
Ahmed S. Darwish3b07a442015-01-26 07:27:19 +0200904 /* Update all of the can interface's state and error counters before
Ahmed S. Darwish96d7f102015-01-26 07:29:15 +0200905 * trying any memory allocation that can actually fail with -ENOMEM.
906 *
907 * We send a temporary stack-allocated error can frame to
908 * can_change_state() for the very same reason.
909 *
910 * TODO: Split can_change_state() responsibility between updating the
911 * can interface's state and counters, and the setting up of can error
912 * frame ID and data to userspace. Remove stack allocation afterwards.
Ahmed S. Darwish3b07a442015-01-26 07:27:19 +0200913 */
914 old_state = priv->can.state;
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +0200915 kvaser_usb_rx_error_update_can_state(priv, es, &tmp_cf);
Ahmed S. Darwish96d7f102015-01-26 07:29:15 +0200916 new_state = priv->can.state;
Ahmed S. Darwish3b07a442015-01-26 07:27:19 +0200917
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100918 skb = alloc_can_err_skb(priv->netdev, &cf);
919 if (!skb) {
920 stats->rx_dropped++;
921 return;
922 }
Ahmed S. Darwish96d7f102015-01-26 07:29:15 +0200923 memcpy(cf, &tmp_cf, sizeof(*cf));
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100924
Ahmed S. Darwish96d7f102015-01-26 07:29:15 +0200925 if (new_state != old_state) {
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +0200926 if (es->status &
Ahmed S. Darwish96d7f102015-01-26 07:29:15 +0200927 (M16C_STATE_BUS_OFF | M16C_STATE_BUS_RESET)) {
928 if (!priv->can.restart_ms)
929 kvaser_usb_simple_msg_async(priv, CMD_STOP_CHIP);
930 netif_carrier_off(priv->netdev);
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100931 }
Ahmed S. Darwish96d7f102015-01-26 07:29:15 +0200932
933 if (priv->can.restart_ms &&
934 (old_state >= CAN_STATE_BUS_OFF) &&
935 (new_state < CAN_STATE_BUS_OFF)) {
936 cf->can_id |= CAN_ERR_RESTARTED;
937 netif_carrier_on(priv->netdev);
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100938 }
939 }
940
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +0200941 switch (dev->family) {
942 case KVASER_LEAF:
943 if (es->leaf.error_factor) {
944 cf->can_id |= CAN_ERR_BUSERROR | CAN_ERR_PROT;
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100945
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +0200946 if (es->leaf.error_factor & M16C_EF_ACKE)
Oliver Hartkoppffd461f2015-11-21 18:41:20 +0100947 cf->data[3] = CAN_ERR_PROT_LOC_ACK;
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +0200948 if (es->leaf.error_factor & M16C_EF_CRCE)
Oliver Hartkoppffd461f2015-11-21 18:41:20 +0100949 cf->data[3] = CAN_ERR_PROT_LOC_CRC_SEQ;
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +0200950 if (es->leaf.error_factor & M16C_EF_FORME)
951 cf->data[2] |= CAN_ERR_PROT_FORM;
952 if (es->leaf.error_factor & M16C_EF_STFE)
953 cf->data[2] |= CAN_ERR_PROT_STUFF;
954 if (es->leaf.error_factor & M16C_EF_BITE0)
955 cf->data[2] |= CAN_ERR_PROT_BIT0;
956 if (es->leaf.error_factor & M16C_EF_BITE1)
957 cf->data[2] |= CAN_ERR_PROT_BIT1;
958 if (es->leaf.error_factor & M16C_EF_TRE)
959 cf->data[2] |= CAN_ERR_PROT_TX;
960 }
961 break;
962 case KVASER_USBCAN:
963 if (es->usbcan.error_state & USBCAN_ERROR_STATE_BUSERROR) {
964 cf->can_id |= CAN_ERR_BUSERROR;
965 }
966 break;
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100967 }
968
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +0200969 cf->data[6] = es->txerr;
970 cf->data[7] = es->rxerr;
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100971
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100972 stats->rx_packets++;
973 stats->rx_bytes += cf->can_dlc;
Ahmed S. Darwisha58518c2015-01-11 15:49:52 -0500974 netif_rx(skb);
Olivier Sobrie080f40a2012-11-21 08:11:13 +0100975}
976
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +0200977/* For USBCAN, report error to userspace iff the channels's errors counter
978 * has changed, or we're the only channel seeing a bus error state.
979 */
980static void kvaser_usbcan_conditionally_rx_error(const struct kvaser_usb *dev,
981 struct kvaser_usb_error_summary *es)
982{
983 struct kvaser_usb_net_priv *priv;
984 int channel;
985 bool report_error;
986
987 channel = es->channel;
988 if (channel >= dev->nchannels) {
989 dev_err(dev->udev->dev.parent,
990 "Invalid channel number (%d)\n", channel);
991 return;
992 }
993
994 priv = dev->nets[channel];
995 report_error = false;
996
997 if (es->txerr != priv->bec.txerr) {
998 es->usbcan.error_state |= USBCAN_ERROR_STATE_TX_ERROR;
999 report_error = true;
1000 }
1001 if (es->rxerr != priv->bec.rxerr) {
1002 es->usbcan.error_state |= USBCAN_ERROR_STATE_RX_ERROR;
1003 report_error = true;
1004 }
1005 if ((es->status & M16C_STATE_BUS_ERROR) &&
1006 !(es->usbcan.other_ch_status & M16C_STATE_BUS_ERROR)) {
1007 es->usbcan.error_state |= USBCAN_ERROR_STATE_BUSERROR;
1008 report_error = true;
1009 }
1010
1011 if (report_error)
1012 kvaser_usb_rx_error(dev, es);
1013}
1014
1015static void kvaser_usbcan_rx_error(const struct kvaser_usb *dev,
1016 const struct kvaser_msg *msg)
1017{
1018 struct kvaser_usb_error_summary es = { };
1019
1020 switch (msg->id) {
1021 /* Sometimes errors are sent as unsolicited chip state events */
1022 case CMD_CHIP_STATE_EVENT:
1023 es.channel = msg->u.usbcan.chip_state_event.channel;
1024 es.status = msg->u.usbcan.chip_state_event.status;
1025 es.txerr = msg->u.usbcan.chip_state_event.tx_errors_count;
1026 es.rxerr = msg->u.usbcan.chip_state_event.rx_errors_count;
1027 kvaser_usbcan_conditionally_rx_error(dev, &es);
1028 break;
1029
1030 case CMD_CAN_ERROR_EVENT:
1031 es.channel = 0;
1032 es.status = msg->u.usbcan.error_event.status_ch0;
1033 es.txerr = msg->u.usbcan.error_event.tx_errors_count_ch0;
1034 es.rxerr = msg->u.usbcan.error_event.rx_errors_count_ch0;
1035 es.usbcan.other_ch_status =
1036 msg->u.usbcan.error_event.status_ch1;
1037 kvaser_usbcan_conditionally_rx_error(dev, &es);
1038
1039 /* The USBCAN firmware supports up to 2 channels.
1040 * Now that ch0 was checked, check if ch1 has any errors.
1041 */
1042 if (dev->nchannels == MAX_USBCAN_NET_DEVICES) {
1043 es.channel = 1;
1044 es.status = msg->u.usbcan.error_event.status_ch1;
1045 es.txerr = msg->u.usbcan.error_event.tx_errors_count_ch1;
1046 es.rxerr = msg->u.usbcan.error_event.rx_errors_count_ch1;
1047 es.usbcan.other_ch_status =
1048 msg->u.usbcan.error_event.status_ch0;
1049 kvaser_usbcan_conditionally_rx_error(dev, &es);
1050 }
1051 break;
1052
1053 default:
1054 dev_err(dev->udev->dev.parent, "Invalid msg id (%d)\n",
1055 msg->id);
1056 }
1057}
1058
1059static void kvaser_leaf_rx_error(const struct kvaser_usb *dev,
1060 const struct kvaser_msg *msg)
1061{
1062 struct kvaser_usb_error_summary es = { };
1063
1064 switch (msg->id) {
1065 case CMD_CAN_ERROR_EVENT:
1066 es.channel = msg->u.leaf.error_event.channel;
1067 es.status = msg->u.leaf.error_event.status;
1068 es.txerr = msg->u.leaf.error_event.tx_errors_count;
1069 es.rxerr = msg->u.leaf.error_event.rx_errors_count;
1070 es.leaf.error_factor = msg->u.leaf.error_event.error_factor;
1071 break;
1072 case CMD_LEAF_LOG_MESSAGE:
1073 es.channel = msg->u.leaf.log_message.channel;
1074 es.status = msg->u.leaf.log_message.data[0];
1075 es.txerr = msg->u.leaf.log_message.data[2];
1076 es.rxerr = msg->u.leaf.log_message.data[3];
1077 es.leaf.error_factor = msg->u.leaf.log_message.data[1];
1078 break;
1079 case CMD_CHIP_STATE_EVENT:
1080 es.channel = msg->u.leaf.chip_state_event.channel;
1081 es.status = msg->u.leaf.chip_state_event.status;
1082 es.txerr = msg->u.leaf.chip_state_event.tx_errors_count;
1083 es.rxerr = msg->u.leaf.chip_state_event.rx_errors_count;
1084 es.leaf.error_factor = 0;
1085 break;
1086 default:
1087 dev_err(dev->udev->dev.parent, "Invalid msg id (%d)\n",
1088 msg->id);
1089 return;
1090 }
1091
1092 kvaser_usb_rx_error(dev, &es);
1093}
1094
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001095static void kvaser_usb_rx_can_err(const struct kvaser_usb_net_priv *priv,
1096 const struct kvaser_msg *msg)
1097{
1098 struct can_frame *cf;
1099 struct sk_buff *skb;
1100 struct net_device_stats *stats = &priv->netdev->stats;
1101
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +02001102 if (msg->u.rx_can_header.flag & (MSG_FLAG_ERROR_FRAME |
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001103 MSG_FLAG_NERR)) {
Masanari Iida94435f72015-04-27 23:14:57 +09001104 netdev_err(priv->netdev, "Unknown error (flags: 0x%02x)\n",
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +02001105 msg->u.rx_can_header.flag);
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001106
1107 stats->rx_errors++;
1108 return;
1109 }
1110
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +02001111 if (msg->u.rx_can_header.flag & MSG_FLAG_OVERRUN) {
Ahmed S. Darwish3b07a442015-01-26 07:27:19 +02001112 stats->rx_over_errors++;
1113 stats->rx_errors++;
1114
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001115 skb = alloc_can_err_skb(priv->netdev, &cf);
1116 if (!skb) {
1117 stats->rx_dropped++;
1118 return;
1119 }
1120
1121 cf->can_id |= CAN_ERR_CRTL;
1122 cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
1123
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001124 stats->rx_packets++;
1125 stats->rx_bytes += cf->can_dlc;
Ahmed S. Darwisha58518c2015-01-11 15:49:52 -05001126 netif_rx(skb);
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001127 }
1128}
1129
1130static void kvaser_usb_rx_can_msg(const struct kvaser_usb *dev,
1131 const struct kvaser_msg *msg)
1132{
1133 struct kvaser_usb_net_priv *priv;
1134 struct can_frame *cf;
1135 struct sk_buff *skb;
1136 struct net_device_stats *stats;
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +02001137 u8 channel = msg->u.rx_can_header.channel;
1138 const u8 *rx_msg = NULL; /* GCC */
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001139
1140 if (channel >= dev->nchannels) {
1141 dev_err(dev->udev->dev.parent,
1142 "Invalid channel number (%d)\n", channel);
1143 return;
1144 }
1145
1146 priv = dev->nets[channel];
1147 stats = &priv->netdev->stats;
1148
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +02001149 if ((msg->u.rx_can_header.flag & MSG_FLAG_ERROR_FRAME) &&
1150 (dev->family == KVASER_LEAF && msg->id == CMD_LEAF_LOG_MESSAGE)) {
1151 kvaser_leaf_rx_error(dev, msg);
Jonas Petersona90f13b2013-05-07 22:05:23 +02001152 return;
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +02001153 } else if (msg->u.rx_can_header.flag & (MSG_FLAG_ERROR_FRAME |
1154 MSG_FLAG_NERR |
1155 MSG_FLAG_OVERRUN)) {
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001156 kvaser_usb_rx_can_err(priv, msg);
1157 return;
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +02001158 } else if (msg->u.rx_can_header.flag & ~MSG_FLAG_REMOTE_FRAME) {
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001159 netdev_warn(priv->netdev,
1160 "Unhandled frame (flags: 0x%02x)",
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +02001161 msg->u.rx_can_header.flag);
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001162 return;
1163 }
1164
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +02001165 switch (dev->family) {
1166 case KVASER_LEAF:
1167 rx_msg = msg->u.leaf.rx_can.msg;
1168 break;
1169 case KVASER_USBCAN:
1170 rx_msg = msg->u.usbcan.rx_can.msg;
1171 break;
1172 }
1173
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001174 skb = alloc_can_skb(priv->netdev, &cf);
1175 if (!skb) {
1176 stats->tx_dropped++;
1177 return;
1178 }
1179
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +02001180 if (dev->family == KVASER_LEAF && msg->id == CMD_LEAF_LOG_MESSAGE) {
1181 cf->can_id = le32_to_cpu(msg->u.leaf.log_message.id);
Jonas Petersona90f13b2013-05-07 22:05:23 +02001182 if (cf->can_id & KVASER_EXTENDED_FRAME)
1183 cf->can_id &= CAN_EFF_MASK | CAN_EFF_FLAG;
1184 else
1185 cf->can_id &= CAN_SFF_MASK;
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001186
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +02001187 cf->can_dlc = get_can_dlc(msg->u.leaf.log_message.dlc);
Jonas Petersona90f13b2013-05-07 22:05:23 +02001188
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +02001189 if (msg->u.leaf.log_message.flags & MSG_FLAG_REMOTE_FRAME)
Jonas Petersona90f13b2013-05-07 22:05:23 +02001190 cf->can_id |= CAN_RTR_FLAG;
1191 else
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +02001192 memcpy(cf->data, &msg->u.leaf.log_message.data,
Jonas Petersona90f13b2013-05-07 22:05:23 +02001193 cf->can_dlc);
1194 } else {
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +02001195 cf->can_id = ((rx_msg[0] & 0x1f) << 6) | (rx_msg[1] & 0x3f);
Jonas Petersona90f13b2013-05-07 22:05:23 +02001196
1197 if (msg->id == CMD_RX_EXT_MESSAGE) {
1198 cf->can_id <<= 18;
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +02001199 cf->can_id |= ((rx_msg[2] & 0x0f) << 14) |
1200 ((rx_msg[3] & 0xff) << 6) |
1201 (rx_msg[4] & 0x3f);
Jonas Petersona90f13b2013-05-07 22:05:23 +02001202 cf->can_id |= CAN_EFF_FLAG;
1203 }
1204
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +02001205 cf->can_dlc = get_can_dlc(rx_msg[5]);
Jonas Petersona90f13b2013-05-07 22:05:23 +02001206
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +02001207 if (msg->u.rx_can_header.flag & MSG_FLAG_REMOTE_FRAME)
Jonas Petersona90f13b2013-05-07 22:05:23 +02001208 cf->can_id |= CAN_RTR_FLAG;
1209 else
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +02001210 memcpy(cf->data, &rx_msg[6],
Jonas Petersona90f13b2013-05-07 22:05:23 +02001211 cf->can_dlc);
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001212 }
1213
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001214 stats->rx_packets++;
1215 stats->rx_bytes += cf->can_dlc;
Ahmed S. Darwisha58518c2015-01-11 15:49:52 -05001216 netif_rx(skb);
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001217}
1218
1219static void kvaser_usb_start_chip_reply(const struct kvaser_usb *dev,
1220 const struct kvaser_msg *msg)
1221{
1222 struct kvaser_usb_net_priv *priv;
1223 u8 channel = msg->u.simple.channel;
1224
1225 if (channel >= dev->nchannels) {
1226 dev_err(dev->udev->dev.parent,
1227 "Invalid channel number (%d)\n", channel);
1228 return;
1229 }
1230
1231 priv = dev->nets[channel];
1232
1233 if (completion_done(&priv->start_comp) &&
1234 netif_queue_stopped(priv->netdev)) {
1235 netif_wake_queue(priv->netdev);
1236 } else {
1237 netif_start_queue(priv->netdev);
1238 complete(&priv->start_comp);
1239 }
1240}
1241
1242static void kvaser_usb_stop_chip_reply(const struct kvaser_usb *dev,
1243 const struct kvaser_msg *msg)
1244{
1245 struct kvaser_usb_net_priv *priv;
1246 u8 channel = msg->u.simple.channel;
1247
1248 if (channel >= dev->nchannels) {
1249 dev_err(dev->udev->dev.parent,
1250 "Invalid channel number (%d)\n", channel);
1251 return;
1252 }
1253
1254 priv = dev->nets[channel];
1255
1256 complete(&priv->stop_comp);
1257}
1258
1259static void kvaser_usb_handle_message(const struct kvaser_usb *dev,
1260 const struct kvaser_msg *msg)
1261{
1262 switch (msg->id) {
1263 case CMD_START_CHIP_REPLY:
1264 kvaser_usb_start_chip_reply(dev, msg);
1265 break;
1266
1267 case CMD_STOP_CHIP_REPLY:
1268 kvaser_usb_stop_chip_reply(dev, msg);
1269 break;
1270
1271 case CMD_RX_STD_MESSAGE:
1272 case CMD_RX_EXT_MESSAGE:
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +02001273 kvaser_usb_rx_can_msg(dev, msg);
1274 break;
1275
1276 case CMD_LEAF_LOG_MESSAGE:
1277 if (dev->family != KVASER_LEAF)
1278 goto warn;
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001279 kvaser_usb_rx_can_msg(dev, msg);
1280 break;
1281
1282 case CMD_CHIP_STATE_EVENT:
1283 case CMD_CAN_ERROR_EVENT:
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +02001284 if (dev->family == KVASER_LEAF)
1285 kvaser_leaf_rx_error(dev, msg);
1286 else
1287 kvaser_usbcan_rx_error(dev, msg);
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001288 break;
1289
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001290 case CMD_TX_ACKNOWLEDGE:
1291 kvaser_usb_tx_acknowledge(dev, msg);
1292 break;
1293
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +02001294 /* Ignored messages */
1295 case CMD_USBCAN_CLOCK_OVERFLOW_EVENT:
1296 if (dev->family != KVASER_USBCAN)
1297 goto warn;
1298 break;
1299
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001300 default:
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +02001301warn: dev_warn(dev->udev->dev.parent,
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001302 "Unhandled message (%d)\n", msg->id);
1303 break;
1304 }
1305}
1306
1307static void kvaser_usb_read_bulk_callback(struct urb *urb)
1308{
1309 struct kvaser_usb *dev = urb->context;
1310 struct kvaser_msg *msg;
1311 int pos = 0;
1312 int err, i;
1313
1314 switch (urb->status) {
1315 case 0:
1316 break;
1317 case -ENOENT:
1318 case -ESHUTDOWN:
1319 return;
1320 default:
1321 dev_info(dev->udev->dev.parent, "Rx URB aborted (%d)\n",
1322 urb->status);
1323 goto resubmit_urb;
1324 }
1325
1326 while (pos <= urb->actual_length - MSG_HEADER_LEN) {
1327 msg = urb->transfer_buffer + pos;
1328
Ahmed S. Darwish2fec5102015-02-26 10:22:02 -05001329 /* The Kvaser firmware can only read and write messages that
1330 * does not cross the USB's endpoint wMaxPacketSize boundary.
1331 * If a follow-up command crosses such boundary, firmware puts
1332 * a placeholder zero-length command in its place then aligns
1333 * the real command to the next max packet size.
1334 *
1335 * Handle such cases or we're going to miss a significant
1336 * number of events in case of a heavy rx load on the bus.
1337 */
1338 if (msg->len == 0) {
Ahmed S. Darwish7e184c22015-03-15 11:10:49 -04001339 pos = round_up(pos, le16_to_cpu(dev->bulk_in->
1340 wMaxPacketSize));
Ahmed S. Darwish2fec5102015-02-26 10:22:02 -05001341 continue;
1342 }
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001343
1344 if (pos + msg->len > urb->actual_length) {
1345 dev_err(dev->udev->dev.parent, "Format error\n");
1346 break;
1347 }
1348
1349 kvaser_usb_handle_message(dev, msg);
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001350 pos += msg->len;
1351 }
1352
1353resubmit_urb:
1354 usb_fill_bulk_urb(urb, dev->udev,
1355 usb_rcvbulkpipe(dev->udev,
1356 dev->bulk_in->bEndpointAddress),
1357 urb->transfer_buffer, RX_BUFFER_SIZE,
1358 kvaser_usb_read_bulk_callback, dev);
1359
1360 err = usb_submit_urb(urb, GFP_ATOMIC);
1361 if (err == -ENODEV) {
1362 for (i = 0; i < dev->nchannels; i++) {
1363 if (!dev->nets[i])
1364 continue;
1365
1366 netif_device_detach(dev->nets[i]->netdev);
1367 }
1368 } else if (err) {
1369 dev_err(dev->udev->dev.parent,
1370 "Failed resubmitting read bulk urb: %d\n", err);
1371 }
1372
1373 return;
1374}
1375
1376static int kvaser_usb_setup_rx_urbs(struct kvaser_usb *dev)
1377{
1378 int i, err = 0;
1379
1380 if (dev->rxinitdone)
1381 return 0;
1382
1383 for (i = 0; i < MAX_RX_URBS; i++) {
1384 struct urb *urb = NULL;
1385 u8 *buf = NULL;
1386 dma_addr_t buf_dma;
1387
1388 urb = usb_alloc_urb(0, GFP_KERNEL);
1389 if (!urb) {
1390 dev_warn(dev->udev->dev.parent,
1391 "No memory left for URBs\n");
1392 err = -ENOMEM;
1393 break;
1394 }
1395
1396 buf = usb_alloc_coherent(dev->udev, RX_BUFFER_SIZE,
1397 GFP_KERNEL, &buf_dma);
1398 if (!buf) {
1399 dev_warn(dev->udev->dev.parent,
1400 "No memory left for USB buffer\n");
1401 usb_free_urb(urb);
1402 err = -ENOMEM;
1403 break;
1404 }
1405
1406 usb_fill_bulk_urb(urb, dev->udev,
1407 usb_rcvbulkpipe(dev->udev,
1408 dev->bulk_in->bEndpointAddress),
1409 buf, RX_BUFFER_SIZE,
1410 kvaser_usb_read_bulk_callback,
1411 dev);
1412 urb->transfer_dma = buf_dma;
1413 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1414 usb_anchor_urb(urb, &dev->rx_submitted);
1415
1416 err = usb_submit_urb(urb, GFP_KERNEL);
1417 if (err) {
1418 usb_unanchor_urb(urb);
1419 usb_free_coherent(dev->udev, RX_BUFFER_SIZE, buf,
1420 buf_dma);
1421 usb_free_urb(urb);
1422 break;
1423 }
1424
1425 dev->rxbuf[i] = buf;
1426 dev->rxbuf_dma[i] = buf_dma;
1427
1428 usb_free_urb(urb);
1429 }
1430
1431 if (i == 0) {
1432 dev_warn(dev->udev->dev.parent,
1433 "Cannot setup read URBs, error %d\n", err);
1434 return err;
1435 } else if (i < MAX_RX_URBS) {
1436 dev_warn(dev->udev->dev.parent,
1437 "RX performances may be slow\n");
1438 }
1439
1440 dev->rxinitdone = true;
1441
1442 return 0;
1443}
1444
1445static int kvaser_usb_set_opt_mode(const struct kvaser_usb_net_priv *priv)
1446{
1447 struct kvaser_msg *msg;
1448 int rc;
1449
1450 msg = kmalloc(sizeof(*msg), GFP_KERNEL);
1451 if (!msg)
1452 return -ENOMEM;
1453
1454 msg->id = CMD_SET_CTRL_MODE;
1455 msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_ctrl_mode);
1456 msg->u.ctrl_mode.tid = 0xff;
1457 msg->u.ctrl_mode.channel = priv->channel;
1458
1459 if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
1460 msg->u.ctrl_mode.ctrl_mode = KVASER_CTRL_MODE_SILENT;
1461 else
1462 msg->u.ctrl_mode.ctrl_mode = KVASER_CTRL_MODE_NORMAL;
1463
1464 rc = kvaser_usb_send_msg(priv->dev, msg);
1465
1466 kfree(msg);
1467 return rc;
1468}
1469
1470static int kvaser_usb_start_chip(struct kvaser_usb_net_priv *priv)
1471{
1472 int err;
1473
1474 init_completion(&priv->start_comp);
1475
1476 err = kvaser_usb_send_simple_msg(priv->dev, CMD_START_CHIP,
1477 priv->channel);
1478 if (err)
1479 return err;
1480
1481 if (!wait_for_completion_timeout(&priv->start_comp,
1482 msecs_to_jiffies(START_TIMEOUT)))
1483 return -ETIMEDOUT;
1484
1485 return 0;
1486}
1487
1488static int kvaser_usb_open(struct net_device *netdev)
1489{
1490 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1491 struct kvaser_usb *dev = priv->dev;
1492 int err;
1493
1494 err = open_candev(netdev);
1495 if (err)
1496 return err;
1497
1498 err = kvaser_usb_setup_rx_urbs(dev);
1499 if (err)
1500 goto error;
1501
1502 err = kvaser_usb_set_opt_mode(priv);
1503 if (err)
1504 goto error;
1505
1506 err = kvaser_usb_start_chip(priv);
1507 if (err) {
1508 netdev_warn(netdev, "Cannot start device, error %d\n", err);
1509 goto error;
1510 }
1511
1512 priv->can.state = CAN_STATE_ERROR_ACTIVE;
1513
1514 return 0;
1515
1516error:
1517 close_candev(netdev);
1518 return err;
1519}
1520
Ahmed S. Darwisha9dc9602015-03-14 09:02:49 -04001521static void kvaser_usb_reset_tx_urb_contexts(struct kvaser_usb_net_priv *priv)
1522{
Ahmed S. Darwish082d70b2015-03-15 11:03:38 -04001523 int i, max_tx_urbs;
1524
1525 max_tx_urbs = priv->dev->max_tx_urbs;
Ahmed S. Darwisha9dc9602015-03-14 09:02:49 -04001526
1527 priv->active_tx_contexts = 0;
Ahmed S. Darwish082d70b2015-03-15 11:03:38 -04001528 for (i = 0; i < max_tx_urbs; i++)
1529 priv->tx_contexts[i].echo_index = max_tx_urbs;
Ahmed S. Darwisha9dc9602015-03-14 09:02:49 -04001530}
1531
1532/* This method might sleep. Do not call it in the atomic context
1533 * of URB completions.
1534 */
1535static void kvaser_usb_unlink_tx_urbs(struct kvaser_usb_net_priv *priv)
1536{
1537 usb_kill_anchored_urbs(&priv->tx_submitted);
1538 kvaser_usb_reset_tx_urb_contexts(priv);
1539}
1540
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001541static void kvaser_usb_unlink_all_urbs(struct kvaser_usb *dev)
1542{
1543 int i;
1544
1545 usb_kill_anchored_urbs(&dev->rx_submitted);
1546
1547 for (i = 0; i < MAX_RX_URBS; i++)
1548 usb_free_coherent(dev->udev, RX_BUFFER_SIZE,
1549 dev->rxbuf[i],
1550 dev->rxbuf_dma[i]);
1551
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +02001552 for (i = 0; i < dev->nchannels; i++) {
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001553 struct kvaser_usb_net_priv *priv = dev->nets[i];
1554
1555 if (priv)
1556 kvaser_usb_unlink_tx_urbs(priv);
1557 }
1558}
1559
1560static int kvaser_usb_stop_chip(struct kvaser_usb_net_priv *priv)
1561{
1562 int err;
1563
1564 init_completion(&priv->stop_comp);
1565
1566 err = kvaser_usb_send_simple_msg(priv->dev, CMD_STOP_CHIP,
1567 priv->channel);
1568 if (err)
1569 return err;
1570
1571 if (!wait_for_completion_timeout(&priv->stop_comp,
1572 msecs_to_jiffies(STOP_TIMEOUT)))
1573 return -ETIMEDOUT;
1574
1575 return 0;
1576}
1577
1578static int kvaser_usb_flush_queue(struct kvaser_usb_net_priv *priv)
1579{
1580 struct kvaser_msg *msg;
1581 int rc;
1582
1583 msg = kmalloc(sizeof(*msg), GFP_KERNEL);
1584 if (!msg)
1585 return -ENOMEM;
1586
1587 msg->id = CMD_FLUSH_QUEUE;
1588 msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_flush_queue);
1589 msg->u.flush_queue.channel = priv->channel;
1590 msg->u.flush_queue.flags = 0x00;
1591
1592 rc = kvaser_usb_send_msg(priv->dev, msg);
1593
1594 kfree(msg);
1595 return rc;
1596}
1597
1598static int kvaser_usb_close(struct net_device *netdev)
1599{
1600 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1601 struct kvaser_usb *dev = priv->dev;
1602 int err;
1603
1604 netif_stop_queue(netdev);
1605
1606 err = kvaser_usb_flush_queue(priv);
1607 if (err)
1608 netdev_warn(netdev, "Cannot flush queue, error %d\n", err);
1609
1610 if (kvaser_usb_send_simple_msg(dev, CMD_RESET_CHIP, priv->channel))
1611 netdev_warn(netdev, "Cannot reset card, error %d\n", err);
1612
1613 err = kvaser_usb_stop_chip(priv);
1614 if (err)
1615 netdev_warn(netdev, "Cannot stop device, error %d\n", err);
1616
Ahmed S. Darwish889b77f2015-01-05 12:52:06 -05001617 /* reset tx contexts */
1618 kvaser_usb_unlink_tx_urbs(priv);
1619
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001620 priv->can.state = CAN_STATE_STOPPED;
1621 close_candev(priv->netdev);
1622
1623 return 0;
1624}
1625
1626static void kvaser_usb_write_bulk_callback(struct urb *urb)
1627{
1628 struct kvaser_usb_tx_urb_context *context = urb->context;
1629 struct kvaser_usb_net_priv *priv;
1630 struct net_device *netdev;
1631
1632 if (WARN_ON(!context))
1633 return;
1634
1635 priv = context->priv;
1636 netdev = priv->netdev;
1637
1638 kfree(urb->transfer_buffer);
1639
1640 if (!netif_device_present(netdev))
1641 return;
1642
1643 if (urb->status)
1644 netdev_info(netdev, "Tx URB aborted (%d)\n", urb->status);
1645}
1646
1647static netdev_tx_t kvaser_usb_start_xmit(struct sk_buff *skb,
1648 struct net_device *netdev)
1649{
1650 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1651 struct kvaser_usb *dev = priv->dev;
1652 struct net_device_stats *stats = &netdev->stats;
1653 struct can_frame *cf = (struct can_frame *)skb->data;
1654 struct kvaser_usb_tx_urb_context *context = NULL;
1655 struct urb *urb;
1656 void *buf;
1657 struct kvaser_msg *msg;
Ahmed S. Darwishdeb27012015-02-26 10:20:11 -05001658 int i, err, ret = NETDEV_TX_OK;
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +02001659 u8 *msg_tx_can_flags = NULL; /* GCC */
Ahmed S. Darwisha9dc9602015-03-14 09:02:49 -04001660 unsigned long flags;
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001661
1662 if (can_dropped_invalid_skb(netdev, skb))
1663 return NETDEV_TX_OK;
1664
1665 urb = usb_alloc_urb(0, GFP_ATOMIC);
1666 if (!urb) {
1667 netdev_err(netdev, "No memory left for URBs\n");
1668 stats->tx_dropped++;
Ahmed S. Darwishb4427232015-01-05 12:49:10 -05001669 dev_kfree_skb(skb);
1670 return NETDEV_TX_OK;
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001671 }
1672
1673 buf = kmalloc(sizeof(struct kvaser_msg), GFP_ATOMIC);
1674 if (!buf) {
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001675 stats->tx_dropped++;
Ahmed S. Darwishb4427232015-01-05 12:49:10 -05001676 dev_kfree_skb(skb);
Ahmed S. Darwishdeb27012015-02-26 10:20:11 -05001677 goto freeurb;
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001678 }
1679
1680 msg = buf;
1681 msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_tx_can);
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001682 msg->u.tx_can.channel = priv->channel;
1683
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +02001684 switch (dev->family) {
1685 case KVASER_LEAF:
1686 msg_tx_can_flags = &msg->u.tx_can.leaf.flags;
1687 break;
1688 case KVASER_USBCAN:
1689 msg_tx_can_flags = &msg->u.tx_can.usbcan.flags;
1690 break;
1691 }
1692
1693 *msg_tx_can_flags = 0;
1694
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001695 if (cf->can_id & CAN_EFF_FLAG) {
1696 msg->id = CMD_TX_EXT_MESSAGE;
1697 msg->u.tx_can.msg[0] = (cf->can_id >> 24) & 0x1f;
1698 msg->u.tx_can.msg[1] = (cf->can_id >> 18) & 0x3f;
1699 msg->u.tx_can.msg[2] = (cf->can_id >> 14) & 0x0f;
1700 msg->u.tx_can.msg[3] = (cf->can_id >> 6) & 0xff;
1701 msg->u.tx_can.msg[4] = cf->can_id & 0x3f;
1702 } else {
1703 msg->id = CMD_TX_STD_MESSAGE;
1704 msg->u.tx_can.msg[0] = (cf->can_id >> 6) & 0x1f;
1705 msg->u.tx_can.msg[1] = cf->can_id & 0x3f;
1706 }
1707
1708 msg->u.tx_can.msg[5] = cf->can_dlc;
1709 memcpy(&msg->u.tx_can.msg[6], cf->data, cf->can_dlc);
1710
1711 if (cf->can_id & CAN_RTR_FLAG)
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +02001712 *msg_tx_can_flags |= MSG_FLAG_REMOTE_FRAME;
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001713
Ahmed S. Darwisha9dc9602015-03-14 09:02:49 -04001714 spin_lock_irqsave(&priv->tx_contexts_lock, flags);
Ahmed S. Darwish082d70b2015-03-15 11:03:38 -04001715 for (i = 0; i < dev->max_tx_urbs; i++) {
1716 if (priv->tx_contexts[i].echo_index == dev->max_tx_urbs) {
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001717 context = &priv->tx_contexts[i];
Ahmed S. Darwisha9dc9602015-03-14 09:02:49 -04001718
1719 context->echo_index = i;
1720 can_put_echo_skb(skb, netdev, context->echo_index);
1721 ++priv->active_tx_contexts;
Ahmed S. Darwish082d70b2015-03-15 11:03:38 -04001722 if (priv->active_tx_contexts >= dev->max_tx_urbs)
Ahmed S. Darwisha9dc9602015-03-14 09:02:49 -04001723 netif_stop_queue(netdev);
1724
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001725 break;
1726 }
1727 }
Ahmed S. Darwisha9dc9602015-03-14 09:02:49 -04001728 spin_unlock_irqrestore(&priv->tx_contexts_lock, flags);
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001729
Ahmed S. Darwishb4427232015-01-05 12:49:10 -05001730 /* This should never happen; it implies a flow control bug */
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001731 if (!context) {
1732 netdev_warn(netdev, "cannot find free context\n");
Ahmed S. Darwishdeb27012015-02-26 10:20:11 -05001733
1734 kfree(buf);
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001735 ret = NETDEV_TX_BUSY;
Ahmed S. Darwishdeb27012015-02-26 10:20:11 -05001736 goto freeurb;
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001737 }
1738
1739 context->priv = priv;
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001740 context->dlc = cf->can_dlc;
1741
1742 msg->u.tx_can.tid = context->echo_index;
1743
1744 usb_fill_bulk_urb(urb, dev->udev,
1745 usb_sndbulkpipe(dev->udev,
1746 dev->bulk_out->bEndpointAddress),
1747 buf, msg->len,
1748 kvaser_usb_write_bulk_callback, context);
1749 usb_anchor_urb(urb, &priv->tx_submitted);
1750
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001751 err = usb_submit_urb(urb, GFP_ATOMIC);
1752 if (unlikely(err)) {
Ahmed S. Darwisha9dc9602015-03-14 09:02:49 -04001753 spin_lock_irqsave(&priv->tx_contexts_lock, flags);
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001754
Ahmed S. Darwisha9dc9602015-03-14 09:02:49 -04001755 can_free_echo_skb(netdev, context->echo_index);
Ahmed S. Darwish082d70b2015-03-15 11:03:38 -04001756 context->echo_index = dev->max_tx_urbs;
Ahmed S. Darwisha9dc9602015-03-14 09:02:49 -04001757 --priv->active_tx_contexts;
1758 netif_wake_queue(netdev);
1759
1760 spin_unlock_irqrestore(&priv->tx_contexts_lock, flags);
1761
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001762 usb_unanchor_urb(urb);
1763
1764 stats->tx_dropped++;
1765
1766 if (err == -ENODEV)
1767 netif_device_detach(netdev);
1768 else
1769 netdev_warn(netdev, "Failed tx_urb %d\n", err);
1770
Ahmed S. Darwishdeb27012015-02-26 10:20:11 -05001771 goto freeurb;
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001772 }
1773
Ahmed S. Darwishdeb27012015-02-26 10:20:11 -05001774 ret = NETDEV_TX_OK;
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001775
Ahmed S. Darwishdeb27012015-02-26 10:20:11 -05001776freeurb:
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001777 usb_free_urb(urb);
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001778 return ret;
1779}
1780
1781static const struct net_device_ops kvaser_usb_netdev_ops = {
1782 .ndo_open = kvaser_usb_open,
1783 .ndo_stop = kvaser_usb_close,
1784 .ndo_start_xmit = kvaser_usb_start_xmit,
Oliver Hartkoppc971fa22014-03-07 09:23:41 +01001785 .ndo_change_mtu = can_change_mtu,
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001786};
1787
1788static const struct can_bittiming_const kvaser_usb_bittiming_const = {
1789 .name = "kvaser_usb",
1790 .tseg1_min = KVASER_USB_TSEG1_MIN,
1791 .tseg1_max = KVASER_USB_TSEG1_MAX,
1792 .tseg2_min = KVASER_USB_TSEG2_MIN,
1793 .tseg2_max = KVASER_USB_TSEG2_MAX,
1794 .sjw_max = KVASER_USB_SJW_MAX,
1795 .brp_min = KVASER_USB_BRP_MIN,
1796 .brp_max = KVASER_USB_BRP_MAX,
1797 .brp_inc = KVASER_USB_BRP_INC,
1798};
1799
1800static int kvaser_usb_set_bittiming(struct net_device *netdev)
1801{
1802 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1803 struct can_bittiming *bt = &priv->can.bittiming;
1804 struct kvaser_usb *dev = priv->dev;
1805 struct kvaser_msg *msg;
1806 int rc;
1807
1808 msg = kmalloc(sizeof(*msg), GFP_KERNEL);
1809 if (!msg)
1810 return -ENOMEM;
1811
1812 msg->id = CMD_SET_BUS_PARAMS;
1813 msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_busparams);
1814 msg->u.busparams.channel = priv->channel;
1815 msg->u.busparams.tid = 0xff;
1816 msg->u.busparams.bitrate = cpu_to_le32(bt->bitrate);
1817 msg->u.busparams.sjw = bt->sjw;
1818 msg->u.busparams.tseg1 = bt->prop_seg + bt->phase_seg1;
1819 msg->u.busparams.tseg2 = bt->phase_seg2;
1820
1821 if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
1822 msg->u.busparams.no_samp = 3;
1823 else
1824 msg->u.busparams.no_samp = 1;
1825
1826 rc = kvaser_usb_send_msg(dev, msg);
1827
1828 kfree(msg);
1829 return rc;
1830}
1831
1832static int kvaser_usb_set_mode(struct net_device *netdev,
1833 enum can_mode mode)
1834{
1835 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1836 int err;
1837
1838 switch (mode) {
1839 case CAN_MODE_START:
1840 err = kvaser_usb_simple_msg_async(priv, CMD_START_CHIP);
1841 if (err)
1842 return err;
1843 break;
1844 default:
1845 return -EOPNOTSUPP;
1846 }
1847
1848 return 0;
1849}
1850
1851static int kvaser_usb_get_berr_counter(const struct net_device *netdev,
1852 struct can_berr_counter *bec)
1853{
1854 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1855
1856 *bec = priv->bec;
1857
1858 return 0;
1859}
1860
1861static void kvaser_usb_remove_interfaces(struct kvaser_usb *dev)
1862{
1863 int i;
1864
1865 for (i = 0; i < dev->nchannels; i++) {
1866 if (!dev->nets[i])
1867 continue;
1868
Ahmed S. Darwishc637aab2015-03-14 09:11:47 -04001869 unregister_candev(dev->nets[i]->netdev);
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001870 }
1871
1872 kvaser_usb_unlink_all_urbs(dev);
1873
1874 for (i = 0; i < dev->nchannels; i++) {
1875 if (!dev->nets[i])
1876 continue;
1877
1878 free_candev(dev->nets[i]->netdev);
1879 }
1880}
1881
1882static int kvaser_usb_init_one(struct usb_interface *intf,
1883 const struct usb_device_id *id, int channel)
1884{
1885 struct kvaser_usb *dev = usb_get_intfdata(intf);
1886 struct net_device *netdev;
1887 struct kvaser_usb_net_priv *priv;
Ahmed S. Darwisha9dc9602015-03-14 09:02:49 -04001888 int err;
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001889
Ahmed S. Darwish5e7e6e02015-01-05 12:57:13 -05001890 err = kvaser_usb_send_simple_msg(dev, CMD_RESET_CHIP, channel);
1891 if (err)
1892 return err;
1893
Ahmed S. Darwish082d70b2015-03-15 11:03:38 -04001894 netdev = alloc_candev(sizeof(*priv) +
1895 dev->max_tx_urbs * sizeof(*priv->tx_contexts),
1896 dev->max_tx_urbs);
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001897 if (!netdev) {
1898 dev_err(&intf->dev, "Cannot alloc candev\n");
1899 return -ENOMEM;
1900 }
1901
1902 priv = netdev_priv(netdev);
1903
Ahmed S. Darwisha9dc9602015-03-14 09:02:49 -04001904 init_usb_anchor(&priv->tx_submitted);
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001905 init_completion(&priv->start_comp);
1906 init_completion(&priv->stop_comp);
1907
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001908 priv->dev = dev;
1909 priv->netdev = netdev;
1910 priv->channel = channel;
1911
Ahmed S. Darwisha9dc9602015-03-14 09:02:49 -04001912 spin_lock_init(&priv->tx_contexts_lock);
1913 kvaser_usb_reset_tx_urb_contexts(priv);
1914
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001915 priv->can.state = CAN_STATE_STOPPED;
1916 priv->can.clock.freq = CAN_USB_CLOCK;
1917 priv->can.bittiming_const = &kvaser_usb_bittiming_const;
1918 priv->can.do_set_bittiming = kvaser_usb_set_bittiming;
1919 priv->can.do_set_mode = kvaser_usb_set_mode;
1920 if (id->driver_info & KVASER_HAS_TXRX_ERRORS)
1921 priv->can.do_get_berr_counter = kvaser_usb_get_berr_counter;
1922 priv->can.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES;
1923 if (id->driver_info & KVASER_HAS_SILENT_MODE)
1924 priv->can.ctrlmode_supported |= CAN_CTRLMODE_LISTENONLY;
1925
1926 netdev->flags |= IFF_ECHO;
1927
1928 netdev->netdev_ops = &kvaser_usb_netdev_ops;
1929
1930 SET_NETDEV_DEV(netdev, &intf->dev);
Christopher R. Baker3e66d012014-03-08 11:00:20 -05001931 netdev->dev_id = channel;
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001932
1933 dev->nets[channel] = priv;
1934
1935 err = register_candev(netdev);
1936 if (err) {
1937 dev_err(&intf->dev, "Failed to register can device\n");
1938 free_candev(netdev);
1939 dev->nets[channel] = NULL;
1940 return err;
1941 }
1942
1943 netdev_dbg(netdev, "device registered\n");
1944
1945 return 0;
1946}
1947
Olivier Sobrie896e23b2013-10-27 22:07:53 +01001948static int kvaser_usb_get_endpoints(const struct usb_interface *intf,
1949 struct usb_endpoint_descriptor **in,
1950 struct usb_endpoint_descriptor **out)
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001951{
1952 const struct usb_host_interface *iface_desc;
1953 struct usb_endpoint_descriptor *endpoint;
1954 int i;
1955
1956 iface_desc = &intf->altsetting[0];
1957
1958 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1959 endpoint = &iface_desc->endpoint[i].desc;
1960
Olivier Sobrie896e23b2013-10-27 22:07:53 +01001961 if (!*in && usb_endpoint_is_bulk_in(endpoint))
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001962 *in = endpoint;
1963
Olivier Sobrie896e23b2013-10-27 22:07:53 +01001964 if (!*out && usb_endpoint_is_bulk_out(endpoint))
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001965 *out = endpoint;
Olivier Sobrie896e23b2013-10-27 22:07:53 +01001966
1967 /* use first bulk endpoint for in and out */
1968 if (*in && *out)
1969 return 0;
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001970 }
Olivier Sobrie896e23b2013-10-27 22:07:53 +01001971
1972 return -ENODEV;
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001973}
1974
1975static int kvaser_usb_probe(struct usb_interface *intf,
1976 const struct usb_device_id *id)
1977{
1978 struct kvaser_usb *dev;
1979 int err = -ENOMEM;
Ahmed S. Darwish14c10c22015-01-26 07:24:06 +02001980 int i, retry = 3;
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001981
1982 dev = devm_kzalloc(&intf->dev, sizeof(*dev), GFP_KERNEL);
1983 if (!dev)
1984 return -ENOMEM;
1985
Ahmed S. Darwishf5d4abea2015-01-26 07:33:10 +02001986 if (kvaser_is_leaf(id)) {
1987 dev->family = KVASER_LEAF;
1988 } else if (kvaser_is_usbcan(id)) {
1989 dev->family = KVASER_USBCAN;
1990 } else {
1991 dev_err(&intf->dev,
1992 "Product ID (%d) does not belong to any known Kvaser USB family",
1993 id->idProduct);
1994 return -ENODEV;
1995 }
1996
Olivier Sobrie896e23b2013-10-27 22:07:53 +01001997 err = kvaser_usb_get_endpoints(intf, &dev->bulk_in, &dev->bulk_out);
1998 if (err) {
Olivier Sobrie080f40a2012-11-21 08:11:13 +01001999 dev_err(&intf->dev, "Cannot get usb endpoint(s)");
2000 return err;
2001 }
2002
2003 dev->udev = interface_to_usbdev(intf);
2004
2005 init_usb_anchor(&dev->rx_submitted);
2006
2007 usb_set_intfdata(intf, dev);
2008
Ahmed S. Darwish14c10c22015-01-26 07:24:06 +02002009 /* On some x86 laptops, plugging a Kvaser device again after
2010 * an unplug makes the firmware always ignore the very first
2011 * command. For such a case, provide some room for retries
2012 * instead of completely exiting the driver.
2013 */
2014 do {
2015 err = kvaser_usb_get_software_info(dev);
2016 } while (--retry && err == -ETIMEDOUT);
2017
Olivier Sobrie080f40a2012-11-21 08:11:13 +01002018 if (err) {
2019 dev_err(&intf->dev,
2020 "Cannot get software infos, error %d\n", err);
2021 return err;
2022 }
2023
Ahmed S. Darwish082d70b2015-03-15 11:03:38 -04002024 dev_dbg(&intf->dev, "Firmware version: %d.%d.%d\n",
2025 ((dev->fw_version >> 24) & 0xff),
2026 ((dev->fw_version >> 16) & 0xff),
2027 (dev->fw_version & 0xffff));
2028
2029 dev_dbg(&intf->dev, "Max oustanding tx = %d URBs\n", dev->max_tx_urbs);
2030
Olivier Sobrie080f40a2012-11-21 08:11:13 +01002031 err = kvaser_usb_get_card_info(dev);
2032 if (err) {
2033 dev_err(&intf->dev,
2034 "Cannot get card infos, error %d\n", err);
2035 return err;
2036 }
2037
Olivier Sobrie080f40a2012-11-21 08:11:13 +01002038 for (i = 0; i < dev->nchannels; i++) {
2039 err = kvaser_usb_init_one(intf, id, i);
2040 if (err) {
2041 kvaser_usb_remove_interfaces(dev);
2042 return err;
2043 }
2044 }
2045
2046 return 0;
2047}
2048
2049static void kvaser_usb_disconnect(struct usb_interface *intf)
2050{
2051 struct kvaser_usb *dev = usb_get_intfdata(intf);
2052
2053 usb_set_intfdata(intf, NULL);
2054
2055 if (!dev)
2056 return;
2057
2058 kvaser_usb_remove_interfaces(dev);
2059}
2060
2061static struct usb_driver kvaser_usb_driver = {
2062 .name = "kvaser_usb",
2063 .probe = kvaser_usb_probe,
2064 .disconnect = kvaser_usb_disconnect,
2065 .id_table = kvaser_usb_table,
2066};
2067
2068module_usb_driver(kvaser_usb_driver);
2069
2070MODULE_AUTHOR("Olivier Sobrie <olivier@sobrie.be>");
2071MODULE_DESCRIPTION("CAN driver for Kvaser CAN/USB devices");
2072MODULE_LICENSE("GPL v2");