]>
Commit | Line | Data |
---|---|---|
080f40a6 OS |
1 | /* |
2 | * This program is free software; you can redistribute it and/or | |
3 | * modify it under the terms of the GNU General Public License as | |
4 | * published by the Free Software Foundation version 2. | |
5 | * | |
6 | * Parts of this driver are based on the following: | |
7 | * - Kvaser linux leaf driver (version 4.78) | |
8 | * - CAN driver for esd CAN-USB/2 | |
f5d4abea | 9 | * - Kvaser linux usbcanII driver (version 5.3) |
080f40a6 OS |
10 | * |
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> | |
a9ca6e13 | 14 | * Copyright (C) 2015 Valeo S.A. |
080f40a6 OS |
15 | */ |
16 | ||
a9dc960c | 17 | #include <linux/spinlock.h> |
2fec5104 | 18 | #include <linux/kernel.h> |
080f40a6 OS |
19 | #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 | ||
080f40a6 OS |
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 | |
f5d4abea | 36 | #define MAX_USBCAN_NET_DEVICES 2 |
080f40a6 | 37 | |
f5d4abea | 38 | /* Kvaser Leaf USB devices */ |
080f40a6 OS |
39 | #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 | |
b4f20130 OS |
60 | #define USB_LEAF_LITE_V2_PRODUCT_ID 288 |
61 | #define USB_MINI_PCIE_HS_PRODUCT_ID 289 | |
71873a9b JA |
62 | #define USB_LEAF_LIGHT_HS_V2_OEM_PRODUCT_ID 290 |
63 | #define USB_USBCAN_LIGHT_2HS_PRODUCT_ID 291 | |
64 | #define USB_MINI_PCIE_2HS_PRODUCT_ID 292 | |
080f40a6 | 65 | |
f5d4abea AD |
66 | static inline bool kvaser_is_leaf(const struct usb_device_id *id) |
67 | { | |
68 | return id->idProduct >= USB_LEAF_DEVEL_PRODUCT_ID && | |
71873a9b | 69 | id->idProduct <= USB_MINI_PCIE_2HS_PRODUCT_ID; |
f5d4abea AD |
70 | } |
71 | ||
72 | /* Kvaser USBCan-II devices */ | |
73 | #define USB_USBCAN_REVB_PRODUCT_ID 2 | |
74 | #define USB_VCI2_PRODUCT_ID 3 | |
75 | #define USB_USBCAN2_PRODUCT_ID 4 | |
76 | #define USB_MEMORATOR_PRODUCT_ID 5 | |
77 | ||
78 | static inline bool kvaser_is_usbcan(const struct usb_device_id *id) | |
79 | { | |
80 | return id->idProduct >= USB_USBCAN_REVB_PRODUCT_ID && | |
81 | id->idProduct <= USB_MEMORATOR_PRODUCT_ID; | |
82 | } | |
83 | ||
080f40a6 OS |
84 | /* USB devices features */ |
85 | #define KVASER_HAS_SILENT_MODE BIT(0) | |
86 | #define KVASER_HAS_TXRX_ERRORS BIT(1) | |
87 | ||
88 | /* Message header size */ | |
89 | #define MSG_HEADER_LEN 2 | |
90 | ||
91 | /* Can message flags */ | |
92 | #define MSG_FLAG_ERROR_FRAME BIT(0) | |
93 | #define MSG_FLAG_OVERRUN BIT(1) | |
94 | #define MSG_FLAG_NERR BIT(2) | |
95 | #define MSG_FLAG_WAKEUP BIT(3) | |
96 | #define MSG_FLAG_REMOTE_FRAME BIT(4) | |
97 | #define MSG_FLAG_RESERVED BIT(5) | |
98 | #define MSG_FLAG_TX_ACK BIT(6) | |
99 | #define MSG_FLAG_TX_REQUEST BIT(7) | |
100 | ||
f5d4abea | 101 | /* Can states (M16C CxSTRH register) */ |
080f40a6 OS |
102 | #define M16C_STATE_BUS_RESET BIT(0) |
103 | #define M16C_STATE_BUS_ERROR BIT(4) | |
104 | #define M16C_STATE_BUS_PASSIVE BIT(5) | |
105 | #define M16C_STATE_BUS_OFF BIT(6) | |
106 | ||
107 | /* Can msg ids */ | |
108 | #define CMD_RX_STD_MESSAGE 12 | |
109 | #define CMD_TX_STD_MESSAGE 13 | |
110 | #define CMD_RX_EXT_MESSAGE 14 | |
111 | #define CMD_TX_EXT_MESSAGE 15 | |
112 | #define CMD_SET_BUS_PARAMS 16 | |
113 | #define CMD_GET_BUS_PARAMS 17 | |
114 | #define CMD_GET_BUS_PARAMS_REPLY 18 | |
115 | #define CMD_GET_CHIP_STATE 19 | |
116 | #define CMD_CHIP_STATE_EVENT 20 | |
117 | #define CMD_SET_CTRL_MODE 21 | |
118 | #define CMD_GET_CTRL_MODE 22 | |
119 | #define CMD_GET_CTRL_MODE_REPLY 23 | |
120 | #define CMD_RESET_CHIP 24 | |
121 | #define CMD_RESET_CARD 25 | |
122 | #define CMD_START_CHIP 26 | |
123 | #define CMD_START_CHIP_REPLY 27 | |
124 | #define CMD_STOP_CHIP 28 | |
125 | #define CMD_STOP_CHIP_REPLY 29 | |
f5d4abea AD |
126 | |
127 | #define CMD_LEAF_GET_CARD_INFO2 32 | |
128 | #define CMD_USBCAN_RESET_CLOCK 32 | |
129 | #define CMD_USBCAN_CLOCK_OVERFLOW_EVENT 33 | |
130 | ||
080f40a6 OS |
131 | #define CMD_GET_CARD_INFO 34 |
132 | #define CMD_GET_CARD_INFO_REPLY 35 | |
133 | #define CMD_GET_SOFTWARE_INFO 38 | |
134 | #define CMD_GET_SOFTWARE_INFO_REPLY 39 | |
135 | #define CMD_ERROR_EVENT 45 | |
136 | #define CMD_FLUSH_QUEUE 48 | |
137 | #define CMD_RESET_ERROR_COUNTER 49 | |
138 | #define CMD_TX_ACKNOWLEDGE 50 | |
139 | #define CMD_CAN_ERROR_EVENT 51 | |
e1d2d132 | 140 | #define CMD_FLUSH_QUEUE_REPLY 68 |
f5d4abea AD |
141 | |
142 | #define CMD_LEAF_USB_THROTTLE 77 | |
143 | #define CMD_LEAF_LOG_MESSAGE 106 | |
080f40a6 OS |
144 | |
145 | /* error factors */ | |
146 | #define M16C_EF_ACKE BIT(0) | |
147 | #define M16C_EF_CRCE BIT(1) | |
148 | #define M16C_EF_FORME BIT(2) | |
149 | #define M16C_EF_STFE BIT(3) | |
150 | #define M16C_EF_BITE0 BIT(4) | |
151 | #define M16C_EF_BITE1 BIT(5) | |
152 | #define M16C_EF_RCVE BIT(6) | |
153 | #define M16C_EF_TRE BIT(7) | |
154 | ||
f5d4abea AD |
155 | /* Only Leaf-based devices can report M16C error factors, |
156 | * thus define our own error status flags for USBCANII | |
157 | */ | |
158 | #define USBCAN_ERROR_STATE_NONE 0 | |
159 | #define USBCAN_ERROR_STATE_TX_ERROR BIT(0) | |
160 | #define USBCAN_ERROR_STATE_RX_ERROR BIT(1) | |
161 | #define USBCAN_ERROR_STATE_BUSERROR BIT(2) | |
162 | ||
080f40a6 OS |
163 | /* bittiming parameters */ |
164 | #define KVASER_USB_TSEG1_MIN 1 | |
165 | #define KVASER_USB_TSEG1_MAX 16 | |
166 | #define KVASER_USB_TSEG2_MIN 1 | |
167 | #define KVASER_USB_TSEG2_MAX 8 | |
168 | #define KVASER_USB_SJW_MAX 4 | |
169 | #define KVASER_USB_BRP_MIN 1 | |
170 | #define KVASER_USB_BRP_MAX 64 | |
171 | #define KVASER_USB_BRP_INC 1 | |
172 | ||
173 | /* ctrl modes */ | |
174 | #define KVASER_CTRL_MODE_NORMAL 1 | |
175 | #define KVASER_CTRL_MODE_SILENT 2 | |
176 | #define KVASER_CTRL_MODE_SELFRECEPTION 3 | |
177 | #define KVASER_CTRL_MODE_OFF 4 | |
178 | ||
f5d4abea | 179 | /* Extended CAN identifier flag */ |
a90f13b2 JP |
180 | #define KVASER_EXTENDED_FRAME BIT(31) |
181 | ||
f5d4abea AD |
182 | /* Kvaser USB CAN dongles are divided into two major families: |
183 | * - Leaf: Based on Renesas M32C, running firmware labeled as 'filo' | |
184 | * - UsbcanII: Based on Renesas M16C, running firmware labeled as 'helios' | |
185 | */ | |
186 | enum kvaser_usb_family { | |
187 | KVASER_LEAF, | |
188 | KVASER_USBCAN, | |
189 | }; | |
190 | ||
080f40a6 OS |
191 | struct kvaser_msg_simple { |
192 | u8 tid; | |
193 | u8 channel; | |
194 | } __packed; | |
195 | ||
196 | struct kvaser_msg_cardinfo { | |
197 | u8 tid; | |
198 | u8 nchannels; | |
f5d4abea AD |
199 | union { |
200 | struct { | |
201 | __le32 serial_number; | |
202 | __le32 padding; | |
203 | } __packed leaf0; | |
204 | struct { | |
205 | __le32 serial_number_low; | |
206 | __le32 serial_number_high; | |
207 | } __packed usbcan0; | |
208 | } __packed; | |
080f40a6 OS |
209 | __le32 clock_resolution; |
210 | __le32 mfgdate; | |
211 | u8 ean[8]; | |
212 | u8 hw_revision; | |
f5d4abea AD |
213 | union { |
214 | struct { | |
215 | u8 usb_hs_mode; | |
216 | } __packed leaf1; | |
217 | struct { | |
218 | u8 padding; | |
219 | } __packed usbcan1; | |
220 | } __packed; | |
221 | __le16 padding; | |
080f40a6 OS |
222 | } __packed; |
223 | ||
224 | struct kvaser_msg_cardinfo2 { | |
225 | u8 tid; | |
f5d4abea | 226 | u8 reserved; |
080f40a6 OS |
227 | u8 pcb_id[24]; |
228 | __le32 oem_unlock_code; | |
229 | } __packed; | |
230 | ||
f5d4abea | 231 | struct leaf_msg_softinfo { |
080f40a6 | 232 | u8 tid; |
f5d4abea | 233 | u8 padding0; |
080f40a6 OS |
234 | __le32 sw_options; |
235 | __le32 fw_version; | |
236 | __le16 max_outstanding_tx; | |
f5d4abea AD |
237 | __le16 padding1[9]; |
238 | } __packed; | |
239 | ||
240 | struct usbcan_msg_softinfo { | |
241 | u8 tid; | |
242 | u8 fw_name[5]; | |
243 | __le16 max_outstanding_tx; | |
244 | u8 padding[6]; | |
245 | __le32 fw_version; | |
246 | __le16 checksum; | |
247 | __le16 sw_options; | |
080f40a6 OS |
248 | } __packed; |
249 | ||
250 | struct kvaser_msg_busparams { | |
251 | u8 tid; | |
252 | u8 channel; | |
253 | __le32 bitrate; | |
254 | u8 tseg1; | |
255 | u8 tseg2; | |
256 | u8 sjw; | |
257 | u8 no_samp; | |
258 | } __packed; | |
259 | ||
260 | struct kvaser_msg_tx_can { | |
261 | u8 channel; | |
262 | u8 tid; | |
263 | u8 msg[14]; | |
f5d4abea AD |
264 | union { |
265 | struct { | |
266 | u8 padding; | |
267 | u8 flags; | |
268 | } __packed leaf; | |
269 | struct { | |
270 | u8 flags; | |
271 | u8 padding; | |
272 | } __packed usbcan; | |
273 | } __packed; | |
274 | } __packed; | |
275 | ||
276 | struct kvaser_msg_rx_can_header { | |
277 | u8 channel; | |
278 | u8 flag; | |
080f40a6 OS |
279 | } __packed; |
280 | ||
f5d4abea | 281 | struct leaf_msg_rx_can { |
080f40a6 OS |
282 | u8 channel; |
283 | u8 flag; | |
f5d4abea | 284 | |
080f40a6 OS |
285 | __le16 time[3]; |
286 | u8 msg[14]; | |
287 | } __packed; | |
288 | ||
f5d4abea AD |
289 | struct usbcan_msg_rx_can { |
290 | u8 channel; | |
291 | u8 flag; | |
292 | ||
293 | u8 msg[14]; | |
294 | __le16 time; | |
295 | } __packed; | |
296 | ||
297 | struct leaf_msg_chip_state_event { | |
080f40a6 OS |
298 | u8 tid; |
299 | u8 channel; | |
f5d4abea | 300 | |
080f40a6 OS |
301 | __le16 time[3]; |
302 | u8 tx_errors_count; | |
303 | u8 rx_errors_count; | |
f5d4abea AD |
304 | |
305 | u8 status; | |
306 | u8 padding[3]; | |
307 | } __packed; | |
308 | ||
309 | struct usbcan_msg_chip_state_event { | |
310 | u8 tid; | |
311 | u8 channel; | |
312 | ||
313 | u8 tx_errors_count; | |
314 | u8 rx_errors_count; | |
315 | __le16 time; | |
316 | ||
080f40a6 OS |
317 | u8 status; |
318 | u8 padding[3]; | |
319 | } __packed; | |
320 | ||
f5d4abea | 321 | struct kvaser_msg_tx_acknowledge_header { |
080f40a6 OS |
322 | u8 channel; |
323 | u8 tid; | |
f5d4abea AD |
324 | } __packed; |
325 | ||
326 | struct leaf_msg_tx_acknowledge { | |
327 | u8 channel; | |
328 | u8 tid; | |
329 | ||
080f40a6 OS |
330 | __le16 time[3]; |
331 | u8 flags; | |
332 | u8 time_offset; | |
333 | } __packed; | |
334 | ||
f5d4abea AD |
335 | struct usbcan_msg_tx_acknowledge { |
336 | u8 channel; | |
337 | u8 tid; | |
338 | ||
339 | __le16 time; | |
340 | __le16 padding; | |
341 | } __packed; | |
342 | ||
343 | struct leaf_msg_error_event { | |
080f40a6 OS |
344 | u8 tid; |
345 | u8 flags; | |
346 | __le16 time[3]; | |
347 | u8 channel; | |
348 | u8 padding; | |
349 | u8 tx_errors_count; | |
350 | u8 rx_errors_count; | |
351 | u8 status; | |
352 | u8 error_factor; | |
353 | } __packed; | |
354 | ||
f5d4abea AD |
355 | struct usbcan_msg_error_event { |
356 | u8 tid; | |
357 | u8 padding; | |
358 | u8 tx_errors_count_ch0; | |
359 | u8 rx_errors_count_ch0; | |
360 | u8 tx_errors_count_ch1; | |
361 | u8 rx_errors_count_ch1; | |
362 | u8 status_ch0; | |
363 | u8 status_ch1; | |
364 | __le16 time; | |
365 | } __packed; | |
366 | ||
080f40a6 OS |
367 | struct kvaser_msg_ctrl_mode { |
368 | u8 tid; | |
369 | u8 channel; | |
370 | u8 ctrl_mode; | |
371 | u8 padding[3]; | |
372 | } __packed; | |
373 | ||
374 | struct kvaser_msg_flush_queue { | |
375 | u8 tid; | |
376 | u8 channel; | |
377 | u8 flags; | |
378 | u8 padding[3]; | |
379 | } __packed; | |
380 | ||
f5d4abea | 381 | struct leaf_msg_log_message { |
080f40a6 OS |
382 | u8 channel; |
383 | u8 flags; | |
384 | __le16 time[3]; | |
385 | u8 dlc; | |
386 | u8 time_offset; | |
387 | __le32 id; | |
388 | u8 data[8]; | |
389 | } __packed; | |
390 | ||
391 | struct kvaser_msg { | |
392 | u8 len; | |
393 | u8 id; | |
394 | union { | |
395 | struct kvaser_msg_simple simple; | |
396 | struct kvaser_msg_cardinfo cardinfo; | |
397 | struct kvaser_msg_cardinfo2 cardinfo2; | |
080f40a6 | 398 | struct kvaser_msg_busparams busparams; |
f5d4abea AD |
399 | |
400 | struct kvaser_msg_rx_can_header rx_can_header; | |
401 | struct kvaser_msg_tx_acknowledge_header tx_acknowledge_header; | |
402 | ||
403 | union { | |
404 | struct leaf_msg_softinfo softinfo; | |
405 | struct leaf_msg_rx_can rx_can; | |
406 | struct leaf_msg_chip_state_event chip_state_event; | |
407 | struct leaf_msg_tx_acknowledge tx_acknowledge; | |
408 | struct leaf_msg_error_event error_event; | |
409 | struct leaf_msg_log_message log_message; | |
410 | } __packed leaf; | |
411 | ||
412 | union { | |
413 | struct usbcan_msg_softinfo softinfo; | |
414 | struct usbcan_msg_rx_can rx_can; | |
415 | struct usbcan_msg_chip_state_event chip_state_event; | |
416 | struct usbcan_msg_tx_acknowledge tx_acknowledge; | |
417 | struct usbcan_msg_error_event error_event; | |
418 | } __packed usbcan; | |
419 | ||
080f40a6 | 420 | struct kvaser_msg_tx_can tx_can; |
080f40a6 OS |
421 | struct kvaser_msg_ctrl_mode ctrl_mode; |
422 | struct kvaser_msg_flush_queue flush_queue; | |
080f40a6 OS |
423 | } u; |
424 | } __packed; | |
425 | ||
f5d4abea AD |
426 | /* Summary of a kvaser error event, for a unified Leaf/Usbcan error |
427 | * handling. Some discrepancies between the two families exist: | |
428 | * | |
429 | * - USBCAN firmware does not report M16C "error factors" | |
430 | * - USBCAN controllers has difficulties reporting if the raised error | |
431 | * event is for ch0 or ch1. They leave such arbitration to the OS | |
432 | * driver by letting it compare error counters with previous values | |
433 | * and decide the error event's channel. Thus for USBCAN, the channel | |
434 | * field is only advisory. | |
435 | */ | |
3b07a444 | 436 | struct kvaser_usb_error_summary { |
f5d4abea AD |
437 | u8 channel, status, txerr, rxerr; |
438 | union { | |
439 | struct { | |
440 | u8 error_factor; | |
441 | } leaf; | |
442 | struct { | |
443 | u8 other_ch_status; | |
444 | u8 error_state; | |
445 | } usbcan; | |
446 | }; | |
3b07a444 AD |
447 | }; |
448 | ||
082d70b6 | 449 | /* Context for an outstanding, not yet ACKed, transmission */ |
080f40a6 OS |
450 | struct kvaser_usb_tx_urb_context { |
451 | struct kvaser_usb_net_priv *priv; | |
452 | u32 echo_index; | |
453 | int dlc; | |
454 | }; | |
455 | ||
456 | struct kvaser_usb { | |
457 | struct usb_device *udev; | |
458 | struct kvaser_usb_net_priv *nets[MAX_NET_DEVICES]; | |
459 | ||
460 | struct usb_endpoint_descriptor *bulk_in, *bulk_out; | |
461 | struct usb_anchor rx_submitted; | |
462 | ||
0d8f8efd | 463 | /* @max_tx_urbs: Firmware-reported maximum number of outstanding, |
082d70b6 AD |
464 | * not yet ACKed, transmissions on this device. This value is |
465 | * also used as a sentinel for marking free tx contexts. | |
466 | */ | |
080f40a6 OS |
467 | u32 fw_version; |
468 | unsigned int nchannels; | |
082d70b6 | 469 | unsigned int max_tx_urbs; |
f5d4abea | 470 | enum kvaser_usb_family family; |
080f40a6 OS |
471 | |
472 | bool rxinitdone; | |
473 | void *rxbuf[MAX_RX_URBS]; | |
474 | dma_addr_t rxbuf_dma[MAX_RX_URBS]; | |
475 | }; | |
476 | ||
477 | struct kvaser_usb_net_priv { | |
478 | struct can_priv can; | |
082d70b6 | 479 | struct can_berr_counter bec; |
080f40a6 OS |
480 | |
481 | struct kvaser_usb *dev; | |
482 | struct net_device *netdev; | |
483 | int channel; | |
484 | ||
082d70b6 AD |
485 | struct completion start_comp, stop_comp; |
486 | struct usb_anchor tx_submitted; | |
487 | ||
488 | spinlock_t tx_contexts_lock; | |
489 | int active_tx_contexts; | |
490 | struct kvaser_usb_tx_urb_context tx_contexts[]; | |
080f40a6 OS |
491 | }; |
492 | ||
493 | static const struct usb_device_id kvaser_usb_table[] = { | |
f5d4abea | 494 | /* Leaf family IDs */ |
080f40a6 OS |
495 | { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_DEVEL_PRODUCT_ID) }, |
496 | { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_PRODUCT_ID) }, | |
497 | { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_PRODUCT_ID), | |
498 | .driver_info = KVASER_HAS_TXRX_ERRORS | | |
499 | KVASER_HAS_SILENT_MODE }, | |
500 | { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_SPRO_PRODUCT_ID), | |
501 | .driver_info = KVASER_HAS_TXRX_ERRORS | | |
502 | KVASER_HAS_SILENT_MODE }, | |
503 | { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_LS_PRODUCT_ID), | |
504 | .driver_info = KVASER_HAS_TXRX_ERRORS | | |
505 | KVASER_HAS_SILENT_MODE }, | |
506 | { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_SWC_PRODUCT_ID), | |
507 | .driver_info = KVASER_HAS_TXRX_ERRORS | | |
508 | KVASER_HAS_SILENT_MODE }, | |
509 | { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_LIN_PRODUCT_ID), | |
510 | .driver_info = KVASER_HAS_TXRX_ERRORS | | |
511 | KVASER_HAS_SILENT_MODE }, | |
512 | { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_SPRO_LS_PRODUCT_ID), | |
513 | .driver_info = KVASER_HAS_TXRX_ERRORS | | |
514 | KVASER_HAS_SILENT_MODE }, | |
515 | { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_SPRO_SWC_PRODUCT_ID), | |
516 | .driver_info = KVASER_HAS_TXRX_ERRORS | | |
517 | KVASER_HAS_SILENT_MODE }, | |
518 | { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO2_DEVEL_PRODUCT_ID), | |
519 | .driver_info = KVASER_HAS_TXRX_ERRORS | | |
520 | KVASER_HAS_SILENT_MODE }, | |
521 | { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO2_HSHS_PRODUCT_ID), | |
522 | .driver_info = KVASER_HAS_TXRX_ERRORS | | |
523 | KVASER_HAS_SILENT_MODE }, | |
524 | { USB_DEVICE(KVASER_VENDOR_ID, USB_UPRO_HSHS_PRODUCT_ID), | |
525 | .driver_info = KVASER_HAS_TXRX_ERRORS }, | |
526 | { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_GI_PRODUCT_ID) }, | |
527 | { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_OBDII_PRODUCT_ID), | |
528 | .driver_info = KVASER_HAS_TXRX_ERRORS | | |
529 | KVASER_HAS_SILENT_MODE }, | |
530 | { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO2_HSLS_PRODUCT_ID), | |
531 | .driver_info = KVASER_HAS_TXRX_ERRORS }, | |
532 | { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_CH_PRODUCT_ID), | |
533 | .driver_info = KVASER_HAS_TXRX_ERRORS }, | |
534 | { USB_DEVICE(KVASER_VENDOR_ID, USB_BLACKBIRD_SPRO_PRODUCT_ID), | |
535 | .driver_info = KVASER_HAS_TXRX_ERRORS }, | |
536 | { USB_DEVICE(KVASER_VENDOR_ID, USB_OEM_MERCURY_PRODUCT_ID), | |
537 | .driver_info = KVASER_HAS_TXRX_ERRORS }, | |
538 | { USB_DEVICE(KVASER_VENDOR_ID, USB_OEM_LEAF_PRODUCT_ID), | |
539 | .driver_info = KVASER_HAS_TXRX_ERRORS }, | |
540 | { USB_DEVICE(KVASER_VENDOR_ID, USB_CAN_R_PRODUCT_ID), | |
541 | .driver_info = KVASER_HAS_TXRX_ERRORS }, | |
b4f20130 OS |
542 | { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_V2_PRODUCT_ID) }, |
543 | { USB_DEVICE(KVASER_VENDOR_ID, USB_MINI_PCIE_HS_PRODUCT_ID) }, | |
71873a9b JA |
544 | { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LIGHT_HS_V2_OEM_PRODUCT_ID) }, |
545 | { USB_DEVICE(KVASER_VENDOR_ID, USB_USBCAN_LIGHT_2HS_PRODUCT_ID) }, | |
546 | { USB_DEVICE(KVASER_VENDOR_ID, USB_MINI_PCIE_2HS_PRODUCT_ID) }, | |
f5d4abea AD |
547 | |
548 | /* USBCANII family IDs */ | |
549 | { USB_DEVICE(KVASER_VENDOR_ID, USB_USBCAN2_PRODUCT_ID), | |
550 | .driver_info = KVASER_HAS_TXRX_ERRORS }, | |
551 | { USB_DEVICE(KVASER_VENDOR_ID, USB_USBCAN_REVB_PRODUCT_ID), | |
552 | .driver_info = KVASER_HAS_TXRX_ERRORS }, | |
553 | { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMORATOR_PRODUCT_ID), | |
554 | .driver_info = KVASER_HAS_TXRX_ERRORS }, | |
555 | { USB_DEVICE(KVASER_VENDOR_ID, USB_VCI2_PRODUCT_ID), | |
556 | .driver_info = KVASER_HAS_TXRX_ERRORS }, | |
557 | ||
080f40a6 OS |
558 | { } |
559 | }; | |
560 | MODULE_DEVICE_TABLE(usb, kvaser_usb_table); | |
561 | ||
562 | static inline int kvaser_usb_send_msg(const struct kvaser_usb *dev, | |
563 | struct kvaser_msg *msg) | |
564 | { | |
565 | int actual_len; | |
566 | ||
567 | return usb_bulk_msg(dev->udev, | |
568 | usb_sndbulkpipe(dev->udev, | |
569 | dev->bulk_out->bEndpointAddress), | |
570 | msg, msg->len, &actual_len, | |
571 | USB_SEND_TIMEOUT); | |
572 | } | |
573 | ||
574 | static int kvaser_usb_wait_msg(const struct kvaser_usb *dev, u8 id, | |
575 | struct kvaser_msg *msg) | |
576 | { | |
577 | struct kvaser_msg *tmp; | |
578 | void *buf; | |
579 | int actual_len; | |
580 | int err; | |
e59e36e7 OS |
581 | int pos; |
582 | unsigned long to = jiffies + msecs_to_jiffies(USB_RECV_TIMEOUT); | |
080f40a6 OS |
583 | |
584 | buf = kzalloc(RX_BUFFER_SIZE, GFP_KERNEL); | |
585 | if (!buf) | |
586 | return -ENOMEM; | |
587 | ||
e59e36e7 OS |
588 | do { |
589 | err = usb_bulk_msg(dev->udev, | |
590 | usb_rcvbulkpipe(dev->udev, | |
591 | dev->bulk_in->bEndpointAddress), | |
592 | buf, RX_BUFFER_SIZE, &actual_len, | |
593 | USB_RECV_TIMEOUT); | |
594 | if (err < 0) | |
595 | goto end; | |
080f40a6 | 596 | |
e59e36e7 OS |
597 | pos = 0; |
598 | while (pos <= actual_len - MSG_HEADER_LEN) { | |
599 | tmp = buf + pos; | |
080f40a6 | 600 | |
2fec5104 AD |
601 | /* Handle messages crossing the USB endpoint max packet |
602 | * size boundary. Check kvaser_usb_read_bulk_callback() | |
603 | * for further details. | |
604 | */ | |
605 | if (tmp->len == 0) { | |
7e184c28 AD |
606 | pos = round_up(pos, le16_to_cpu(dev->bulk_in-> |
607 | wMaxPacketSize)); | |
2fec5104 AD |
608 | continue; |
609 | } | |
080f40a6 | 610 | |
e59e36e7 | 611 | if (pos + tmp->len > actual_len) { |
8bd13bd5 JA |
612 | dev_err_ratelimited(dev->udev->dev.parent, |
613 | "Format error\n"); | |
e59e36e7 OS |
614 | break; |
615 | } | |
080f40a6 | 616 | |
e59e36e7 OS |
617 | if (tmp->id == id) { |
618 | memcpy(msg, tmp, tmp->len); | |
619 | goto end; | |
620 | } | |
080f40a6 | 621 | |
e59e36e7 OS |
622 | pos += tmp->len; |
623 | } | |
624 | } while (time_before(jiffies, to)); | |
080f40a6 OS |
625 | |
626 | err = -EINVAL; | |
627 | ||
628 | end: | |
629 | kfree(buf); | |
630 | ||
631 | return err; | |
632 | } | |
633 | ||
634 | static int kvaser_usb_send_simple_msg(const struct kvaser_usb *dev, | |
635 | u8 msg_id, int channel) | |
636 | { | |
637 | struct kvaser_msg *msg; | |
638 | int rc; | |
639 | ||
640 | msg = kmalloc(sizeof(*msg), GFP_KERNEL); | |
641 | if (!msg) | |
642 | return -ENOMEM; | |
643 | ||
644 | msg->id = msg_id; | |
645 | msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_simple); | |
646 | msg->u.simple.channel = channel; | |
647 | msg->u.simple.tid = 0xff; | |
648 | ||
649 | rc = kvaser_usb_send_msg(dev, msg); | |
650 | ||
651 | kfree(msg); | |
652 | return rc; | |
653 | } | |
654 | ||
655 | static int kvaser_usb_get_software_info(struct kvaser_usb *dev) | |
656 | { | |
657 | struct kvaser_msg msg; | |
658 | int err; | |
659 | ||
660 | err = kvaser_usb_send_simple_msg(dev, CMD_GET_SOFTWARE_INFO, 0); | |
661 | if (err) | |
662 | return err; | |
663 | ||
664 | err = kvaser_usb_wait_msg(dev, CMD_GET_SOFTWARE_INFO_REPLY, &msg); | |
665 | if (err) | |
666 | return err; | |
667 | ||
f5d4abea AD |
668 | switch (dev->family) { |
669 | case KVASER_LEAF: | |
670 | dev->fw_version = le32_to_cpu(msg.u.leaf.softinfo.fw_version); | |
082d70b6 AD |
671 | dev->max_tx_urbs = |
672 | le16_to_cpu(msg.u.leaf.softinfo.max_outstanding_tx); | |
f5d4abea AD |
673 | break; |
674 | case KVASER_USBCAN: | |
675 | dev->fw_version = le32_to_cpu(msg.u.usbcan.softinfo.fw_version); | |
082d70b6 AD |
676 | dev->max_tx_urbs = |
677 | le16_to_cpu(msg.u.usbcan.softinfo.max_outstanding_tx); | |
f5d4abea AD |
678 | break; |
679 | } | |
080f40a6 OS |
680 | |
681 | return 0; | |
682 | } | |
683 | ||
684 | static int kvaser_usb_get_card_info(struct kvaser_usb *dev) | |
685 | { | |
686 | struct kvaser_msg msg; | |
687 | int err; | |
688 | ||
689 | err = kvaser_usb_send_simple_msg(dev, CMD_GET_CARD_INFO, 0); | |
690 | if (err) | |
691 | return err; | |
692 | ||
693 | err = kvaser_usb_wait_msg(dev, CMD_GET_CARD_INFO_REPLY, &msg); | |
694 | if (err) | |
695 | return err; | |
696 | ||
697 | dev->nchannels = msg.u.cardinfo.nchannels; | |
f5d4abea AD |
698 | if ((dev->nchannels > MAX_NET_DEVICES) || |
699 | (dev->family == KVASER_USBCAN && | |
700 | dev->nchannels > MAX_USBCAN_NET_DEVICES)) | |
862474f8 | 701 | return -EINVAL; |
080f40a6 OS |
702 | |
703 | return 0; | |
704 | } | |
705 | ||
706 | static void kvaser_usb_tx_acknowledge(const struct kvaser_usb *dev, | |
707 | const struct kvaser_msg *msg) | |
708 | { | |
709 | struct net_device_stats *stats; | |
710 | struct kvaser_usb_tx_urb_context *context; | |
711 | struct kvaser_usb_net_priv *priv; | |
712 | struct sk_buff *skb; | |
713 | struct can_frame *cf; | |
a9dc960c | 714 | unsigned long flags; |
f5d4abea AD |
715 | u8 channel, tid; |
716 | ||
717 | channel = msg->u.tx_acknowledge_header.channel; | |
718 | tid = msg->u.tx_acknowledge_header.tid; | |
080f40a6 OS |
719 | |
720 | if (channel >= dev->nchannels) { | |
721 | dev_err(dev->udev->dev.parent, | |
722 | "Invalid channel number (%d)\n", channel); | |
723 | return; | |
724 | } | |
725 | ||
726 | priv = dev->nets[channel]; | |
727 | ||
728 | if (!netif_device_present(priv->netdev)) | |
729 | return; | |
730 | ||
731 | stats = &priv->netdev->stats; | |
732 | ||
082d70b6 | 733 | context = &priv->tx_contexts[tid % dev->max_tx_urbs]; |
080f40a6 OS |
734 | |
735 | /* Sometimes the state change doesn't come after a bus-off event */ | |
736 | if (priv->can.restart_ms && | |
737 | (priv->can.state >= CAN_STATE_BUS_OFF)) { | |
738 | skb = alloc_can_err_skb(priv->netdev, &cf); | |
739 | if (skb) { | |
740 | cf->can_id |= CAN_ERR_RESTARTED; | |
080f40a6 OS |
741 | |
742 | stats->rx_packets++; | |
743 | stats->rx_bytes += cf->can_dlc; | |
a58518cc | 744 | netif_rx(skb); |
080f40a6 OS |
745 | } else { |
746 | netdev_err(priv->netdev, | |
747 | "No memory left for err_skb\n"); | |
748 | } | |
749 | ||
750 | priv->can.can_stats.restarts++; | |
751 | netif_carrier_on(priv->netdev); | |
752 | ||
753 | priv->can.state = CAN_STATE_ERROR_ACTIVE; | |
754 | } | |
755 | ||
756 | stats->tx_packets++; | |
757 | stats->tx_bytes += context->dlc; | |
080f40a6 | 758 | |
a9dc960c | 759 | spin_lock_irqsave(&priv->tx_contexts_lock, flags); |
080f40a6 | 760 | |
a9dc960c | 761 | can_get_echo_skb(priv->netdev, context->echo_index); |
082d70b6 | 762 | context->echo_index = dev->max_tx_urbs; |
a9dc960c | 763 | --priv->active_tx_contexts; |
080f40a6 | 764 | netif_wake_queue(priv->netdev); |
a9dc960c AD |
765 | |
766 | spin_unlock_irqrestore(&priv->tx_contexts_lock, flags); | |
080f40a6 OS |
767 | } |
768 | ||
769 | static void kvaser_usb_simple_msg_callback(struct urb *urb) | |
770 | { | |
771 | struct net_device *netdev = urb->context; | |
772 | ||
773 | kfree(urb->transfer_buffer); | |
774 | ||
775 | if (urb->status) | |
776 | netdev_warn(netdev, "urb status received: %d\n", | |
777 | urb->status); | |
778 | } | |
779 | ||
780 | static int kvaser_usb_simple_msg_async(struct kvaser_usb_net_priv *priv, | |
781 | u8 msg_id) | |
782 | { | |
783 | struct kvaser_usb *dev = priv->dev; | |
784 | struct net_device *netdev = priv->netdev; | |
785 | struct kvaser_msg *msg; | |
786 | struct urb *urb; | |
787 | void *buf; | |
788 | int err; | |
789 | ||
790 | urb = usb_alloc_urb(0, GFP_ATOMIC); | |
bc1af47d | 791 | if (!urb) |
080f40a6 | 792 | return -ENOMEM; |
080f40a6 OS |
793 | |
794 | buf = kmalloc(sizeof(struct kvaser_msg), GFP_ATOMIC); | |
795 | if (!buf) { | |
080f40a6 OS |
796 | usb_free_urb(urb); |
797 | return -ENOMEM; | |
798 | } | |
799 | ||
800 | msg = (struct kvaser_msg *)buf; | |
801 | msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_simple); | |
802 | msg->id = msg_id; | |
803 | msg->u.simple.channel = priv->channel; | |
804 | ||
805 | usb_fill_bulk_urb(urb, dev->udev, | |
806 | usb_sndbulkpipe(dev->udev, | |
807 | dev->bulk_out->bEndpointAddress), | |
808 | buf, msg->len, | |
3803fa69 | 809 | kvaser_usb_simple_msg_callback, netdev); |
080f40a6 OS |
810 | usb_anchor_urb(urb, &priv->tx_submitted); |
811 | ||
812 | err = usb_submit_urb(urb, GFP_ATOMIC); | |
813 | if (err) { | |
814 | netdev_err(netdev, "Error transmitting URB\n"); | |
815 | usb_unanchor_urb(urb); | |
435019b4 | 816 | kfree(buf); |
080f40a6 | 817 | usb_free_urb(urb); |
080f40a6 OS |
818 | return err; |
819 | } | |
820 | ||
821 | usb_free_urb(urb); | |
822 | ||
823 | return 0; | |
824 | } | |
825 | ||
3b07a444 | 826 | static void kvaser_usb_rx_error_update_can_state(struct kvaser_usb_net_priv *priv, |
96d7f106 AD |
827 | const struct kvaser_usb_error_summary *es, |
828 | struct can_frame *cf) | |
3b07a444 | 829 | { |
f5d4abea AD |
830 | struct kvaser_usb *dev = priv->dev; |
831 | struct net_device_stats *stats = &priv->netdev->stats; | |
96d7f106 | 832 | enum can_state cur_state, new_state, tx_state, rx_state; |
3b07a444 AD |
833 | |
834 | netdev_dbg(priv->netdev, "Error status: 0x%02x\n", es->status); | |
835 | ||
96d7f106 AD |
836 | new_state = cur_state = priv->can.state; |
837 | ||
838 | if (es->status & (M16C_STATE_BUS_OFF | M16C_STATE_BUS_RESET)) | |
3b07a444 | 839 | new_state = CAN_STATE_BUS_OFF; |
96d7f106 | 840 | else if (es->status & M16C_STATE_BUS_PASSIVE) |
3b07a444 | 841 | new_state = CAN_STATE_ERROR_PASSIVE; |
96d7f106 | 842 | else if (es->status & M16C_STATE_BUS_ERROR) { |
a9ca6e13 AD |
843 | /* Guard against spurious error events after a busoff */ |
844 | if (cur_state < CAN_STATE_BUS_OFF) { | |
845 | if ((es->txerr >= 128) || (es->rxerr >= 128)) | |
846 | new_state = CAN_STATE_ERROR_PASSIVE; | |
847 | else if ((es->txerr >= 96) || (es->rxerr >= 96)) | |
848 | new_state = CAN_STATE_ERROR_WARNING; | |
849 | else if (cur_state > CAN_STATE_ERROR_ACTIVE) | |
850 | new_state = CAN_STATE_ERROR_ACTIVE; | |
851 | } | |
3b07a444 AD |
852 | } |
853 | ||
854 | if (!es->status) | |
855 | new_state = CAN_STATE_ERROR_ACTIVE; | |
856 | ||
96d7f106 AD |
857 | if (new_state != cur_state) { |
858 | tx_state = (es->txerr >= es->rxerr) ? new_state : 0; | |
859 | rx_state = (es->txerr <= es->rxerr) ? new_state : 0; | |
860 | ||
861 | can_change_state(priv->netdev, cf, tx_state, rx_state); | |
862 | } | |
863 | ||
3b07a444 | 864 | if (priv->can.restart_ms && |
96d7f106 | 865 | (cur_state >= CAN_STATE_BUS_OFF) && |
3b07a444 AD |
866 | (new_state < CAN_STATE_BUS_OFF)) { |
867 | priv->can.can_stats.restarts++; | |
868 | } | |
869 | ||
f5d4abea AD |
870 | switch (dev->family) { |
871 | case KVASER_LEAF: | |
872 | if (es->leaf.error_factor) { | |
873 | priv->can.can_stats.bus_error++; | |
874 | stats->rx_errors++; | |
875 | } | |
876 | break; | |
877 | case KVASER_USBCAN: | |
878 | if (es->usbcan.error_state & USBCAN_ERROR_STATE_TX_ERROR) | |
879 | stats->tx_errors++; | |
880 | if (es->usbcan.error_state & USBCAN_ERROR_STATE_RX_ERROR) | |
881 | stats->rx_errors++; | |
882 | if (es->usbcan.error_state & USBCAN_ERROR_STATE_BUSERROR) { | |
883 | priv->can.can_stats.bus_error++; | |
884 | } | |
885 | break; | |
3b07a444 AD |
886 | } |
887 | ||
888 | priv->bec.txerr = es->txerr; | |
889 | priv->bec.rxerr = es->rxerr; | |
3b07a444 AD |
890 | } |
891 | ||
080f40a6 | 892 | static void kvaser_usb_rx_error(const struct kvaser_usb *dev, |
f5d4abea | 893 | const struct kvaser_usb_error_summary *es) |
080f40a6 | 894 | { |
96d7f106 | 895 | struct can_frame *cf, tmp_cf = { .can_id = CAN_ERR_FLAG, .can_dlc = CAN_ERR_DLC }; |
080f40a6 OS |
896 | struct sk_buff *skb; |
897 | struct net_device_stats *stats; | |
898 | struct kvaser_usb_net_priv *priv; | |
96d7f106 | 899 | enum can_state old_state, new_state; |
080f40a6 | 900 | |
f5d4abea | 901 | if (es->channel >= dev->nchannels) { |
080f40a6 | 902 | dev_err(dev->udev->dev.parent, |
f5d4abea | 903 | "Invalid channel number (%d)\n", es->channel); |
080f40a6 OS |
904 | return; |
905 | } | |
906 | ||
f5d4abea | 907 | priv = dev->nets[es->channel]; |
080f40a6 OS |
908 | stats = &priv->netdev->stats; |
909 | ||
3b07a444 | 910 | /* Update all of the can interface's state and error counters before |
96d7f106 AD |
911 | * trying any memory allocation that can actually fail with -ENOMEM. |
912 | * | |
913 | * We send a temporary stack-allocated error can frame to | |
914 | * can_change_state() for the very same reason. | |
915 | * | |
916 | * TODO: Split can_change_state() responsibility between updating the | |
917 | * can interface's state and counters, and the setting up of can error | |
918 | * frame ID and data to userspace. Remove stack allocation afterwards. | |
3b07a444 AD |
919 | */ |
920 | old_state = priv->can.state; | |
f5d4abea | 921 | kvaser_usb_rx_error_update_can_state(priv, es, &tmp_cf); |
96d7f106 | 922 | new_state = priv->can.state; |
3b07a444 | 923 | |
080f40a6 OS |
924 | skb = alloc_can_err_skb(priv->netdev, &cf); |
925 | if (!skb) { | |
926 | stats->rx_dropped++; | |
927 | return; | |
928 | } | |
96d7f106 AD |
929 | memcpy(cf, &tmp_cf, sizeof(*cf)); |
930 | ||
931 | if (new_state != old_state) { | |
f5d4abea | 932 | if (es->status & |
96d7f106 AD |
933 | (M16C_STATE_BUS_OFF | M16C_STATE_BUS_RESET)) { |
934 | if (!priv->can.restart_ms) | |
935 | kvaser_usb_simple_msg_async(priv, CMD_STOP_CHIP); | |
936 | netif_carrier_off(priv->netdev); | |
080f40a6 | 937 | } |
080f40a6 | 938 | |
96d7f106 AD |
939 | if (priv->can.restart_ms && |
940 | (old_state >= CAN_STATE_BUS_OFF) && | |
941 | (new_state < CAN_STATE_BUS_OFF)) { | |
942 | cf->can_id |= CAN_ERR_RESTARTED; | |
943 | netif_carrier_on(priv->netdev); | |
944 | } | |
080f40a6 OS |
945 | } |
946 | ||
f5d4abea AD |
947 | switch (dev->family) { |
948 | case KVASER_LEAF: | |
949 | if (es->leaf.error_factor) { | |
950 | cf->can_id |= CAN_ERR_BUSERROR | CAN_ERR_PROT; | |
951 | ||
952 | if (es->leaf.error_factor & M16C_EF_ACKE) | |
ffd461f8 | 953 | cf->data[3] = CAN_ERR_PROT_LOC_ACK; |
f5d4abea | 954 | if (es->leaf.error_factor & M16C_EF_CRCE) |
ffd461f8 | 955 | cf->data[3] = CAN_ERR_PROT_LOC_CRC_SEQ; |
f5d4abea AD |
956 | if (es->leaf.error_factor & M16C_EF_FORME) |
957 | cf->data[2] |= CAN_ERR_PROT_FORM; | |
958 | if (es->leaf.error_factor & M16C_EF_STFE) | |
959 | cf->data[2] |= CAN_ERR_PROT_STUFF; | |
960 | if (es->leaf.error_factor & M16C_EF_BITE0) | |
961 | cf->data[2] |= CAN_ERR_PROT_BIT0; | |
962 | if (es->leaf.error_factor & M16C_EF_BITE1) | |
963 | cf->data[2] |= CAN_ERR_PROT_BIT1; | |
964 | if (es->leaf.error_factor & M16C_EF_TRE) | |
965 | cf->data[2] |= CAN_ERR_PROT_TX; | |
966 | } | |
967 | break; | |
968 | case KVASER_USBCAN: | |
969 | if (es->usbcan.error_state & USBCAN_ERROR_STATE_BUSERROR) { | |
970 | cf->can_id |= CAN_ERR_BUSERROR; | |
971 | } | |
972 | break; | |
080f40a6 OS |
973 | } |
974 | ||
f5d4abea AD |
975 | cf->data[6] = es->txerr; |
976 | cf->data[7] = es->rxerr; | |
080f40a6 | 977 | |
080f40a6 OS |
978 | stats->rx_packets++; |
979 | stats->rx_bytes += cf->can_dlc; | |
a58518cc | 980 | netif_rx(skb); |
080f40a6 OS |
981 | } |
982 | ||
f5d4abea AD |
983 | /* For USBCAN, report error to userspace iff the channels's errors counter |
984 | * has changed, or we're the only channel seeing a bus error state. | |
985 | */ | |
986 | static void kvaser_usbcan_conditionally_rx_error(const struct kvaser_usb *dev, | |
987 | struct kvaser_usb_error_summary *es) | |
988 | { | |
989 | struct kvaser_usb_net_priv *priv; | |
990 | int channel; | |
991 | bool report_error; | |
992 | ||
993 | channel = es->channel; | |
994 | if (channel >= dev->nchannels) { | |
995 | dev_err(dev->udev->dev.parent, | |
996 | "Invalid channel number (%d)\n", channel); | |
997 | return; | |
998 | } | |
999 | ||
1000 | priv = dev->nets[channel]; | |
1001 | report_error = false; | |
1002 | ||
1003 | if (es->txerr != priv->bec.txerr) { | |
1004 | es->usbcan.error_state |= USBCAN_ERROR_STATE_TX_ERROR; | |
1005 | report_error = true; | |
1006 | } | |
1007 | if (es->rxerr != priv->bec.rxerr) { | |
1008 | es->usbcan.error_state |= USBCAN_ERROR_STATE_RX_ERROR; | |
1009 | report_error = true; | |
1010 | } | |
1011 | if ((es->status & M16C_STATE_BUS_ERROR) && | |
1012 | !(es->usbcan.other_ch_status & M16C_STATE_BUS_ERROR)) { | |
1013 | es->usbcan.error_state |= USBCAN_ERROR_STATE_BUSERROR; | |
1014 | report_error = true; | |
1015 | } | |
1016 | ||
1017 | if (report_error) | |
1018 | kvaser_usb_rx_error(dev, es); | |
1019 | } | |
1020 | ||
1021 | static void kvaser_usbcan_rx_error(const struct kvaser_usb *dev, | |
1022 | const struct kvaser_msg *msg) | |
1023 | { | |
1024 | struct kvaser_usb_error_summary es = { }; | |
1025 | ||
1026 | switch (msg->id) { | |
1027 | /* Sometimes errors are sent as unsolicited chip state events */ | |
1028 | case CMD_CHIP_STATE_EVENT: | |
1029 | es.channel = msg->u.usbcan.chip_state_event.channel; | |
1030 | es.status = msg->u.usbcan.chip_state_event.status; | |
1031 | es.txerr = msg->u.usbcan.chip_state_event.tx_errors_count; | |
1032 | es.rxerr = msg->u.usbcan.chip_state_event.rx_errors_count; | |
1033 | kvaser_usbcan_conditionally_rx_error(dev, &es); | |
1034 | break; | |
1035 | ||
1036 | case CMD_CAN_ERROR_EVENT: | |
1037 | es.channel = 0; | |
1038 | es.status = msg->u.usbcan.error_event.status_ch0; | |
1039 | es.txerr = msg->u.usbcan.error_event.tx_errors_count_ch0; | |
1040 | es.rxerr = msg->u.usbcan.error_event.rx_errors_count_ch0; | |
1041 | es.usbcan.other_ch_status = | |
1042 | msg->u.usbcan.error_event.status_ch1; | |
1043 | kvaser_usbcan_conditionally_rx_error(dev, &es); | |
1044 | ||
1045 | /* The USBCAN firmware supports up to 2 channels. | |
1046 | * Now that ch0 was checked, check if ch1 has any errors. | |
1047 | */ | |
1048 | if (dev->nchannels == MAX_USBCAN_NET_DEVICES) { | |
1049 | es.channel = 1; | |
1050 | es.status = msg->u.usbcan.error_event.status_ch1; | |
1051 | es.txerr = msg->u.usbcan.error_event.tx_errors_count_ch1; | |
1052 | es.rxerr = msg->u.usbcan.error_event.rx_errors_count_ch1; | |
1053 | es.usbcan.other_ch_status = | |
1054 | msg->u.usbcan.error_event.status_ch0; | |
1055 | kvaser_usbcan_conditionally_rx_error(dev, &es); | |
1056 | } | |
1057 | break; | |
1058 | ||
1059 | default: | |
1060 | dev_err(dev->udev->dev.parent, "Invalid msg id (%d)\n", | |
1061 | msg->id); | |
1062 | } | |
1063 | } | |
1064 | ||
1065 | static void kvaser_leaf_rx_error(const struct kvaser_usb *dev, | |
1066 | const struct kvaser_msg *msg) | |
1067 | { | |
1068 | struct kvaser_usb_error_summary es = { }; | |
1069 | ||
1070 | switch (msg->id) { | |
1071 | case CMD_CAN_ERROR_EVENT: | |
1072 | es.channel = msg->u.leaf.error_event.channel; | |
1073 | es.status = msg->u.leaf.error_event.status; | |
1074 | es.txerr = msg->u.leaf.error_event.tx_errors_count; | |
1075 | es.rxerr = msg->u.leaf.error_event.rx_errors_count; | |
1076 | es.leaf.error_factor = msg->u.leaf.error_event.error_factor; | |
1077 | break; | |
1078 | case CMD_LEAF_LOG_MESSAGE: | |
1079 | es.channel = msg->u.leaf.log_message.channel; | |
1080 | es.status = msg->u.leaf.log_message.data[0]; | |
1081 | es.txerr = msg->u.leaf.log_message.data[2]; | |
1082 | es.rxerr = msg->u.leaf.log_message.data[3]; | |
1083 | es.leaf.error_factor = msg->u.leaf.log_message.data[1]; | |
1084 | break; | |
1085 | case CMD_CHIP_STATE_EVENT: | |
1086 | es.channel = msg->u.leaf.chip_state_event.channel; | |
1087 | es.status = msg->u.leaf.chip_state_event.status; | |
1088 | es.txerr = msg->u.leaf.chip_state_event.tx_errors_count; | |
1089 | es.rxerr = msg->u.leaf.chip_state_event.rx_errors_count; | |
1090 | es.leaf.error_factor = 0; | |
1091 | break; | |
1092 | default: | |
1093 | dev_err(dev->udev->dev.parent, "Invalid msg id (%d)\n", | |
1094 | msg->id); | |
1095 | return; | |
1096 | } | |
1097 | ||
1098 | kvaser_usb_rx_error(dev, &es); | |
1099 | } | |
1100 | ||
080f40a6 OS |
1101 | static void kvaser_usb_rx_can_err(const struct kvaser_usb_net_priv *priv, |
1102 | const struct kvaser_msg *msg) | |
1103 | { | |
1104 | struct can_frame *cf; | |
1105 | struct sk_buff *skb; | |
1106 | struct net_device_stats *stats = &priv->netdev->stats; | |
1107 | ||
f5d4abea | 1108 | if (msg->u.rx_can_header.flag & (MSG_FLAG_ERROR_FRAME | |
080f40a6 | 1109 | MSG_FLAG_NERR)) { |
94435f76 | 1110 | netdev_err(priv->netdev, "Unknown error (flags: 0x%02x)\n", |
f5d4abea | 1111 | msg->u.rx_can_header.flag); |
080f40a6 OS |
1112 | |
1113 | stats->rx_errors++; | |
1114 | return; | |
1115 | } | |
1116 | ||
f5d4abea | 1117 | if (msg->u.rx_can_header.flag & MSG_FLAG_OVERRUN) { |
3b07a444 AD |
1118 | stats->rx_over_errors++; |
1119 | stats->rx_errors++; | |
1120 | ||
080f40a6 OS |
1121 | skb = alloc_can_err_skb(priv->netdev, &cf); |
1122 | if (!skb) { | |
1123 | stats->rx_dropped++; | |
1124 | return; | |
1125 | } | |
1126 | ||
1127 | cf->can_id |= CAN_ERR_CRTL; | |
1128 | cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW; | |
1129 | ||
080f40a6 OS |
1130 | stats->rx_packets++; |
1131 | stats->rx_bytes += cf->can_dlc; | |
a58518cc | 1132 | netif_rx(skb); |
080f40a6 OS |
1133 | } |
1134 | } | |
1135 | ||
1136 | static void kvaser_usb_rx_can_msg(const struct kvaser_usb *dev, | |
1137 | const struct kvaser_msg *msg) | |
1138 | { | |
1139 | struct kvaser_usb_net_priv *priv; | |
1140 | struct can_frame *cf; | |
1141 | struct sk_buff *skb; | |
1142 | struct net_device_stats *stats; | |
f5d4abea AD |
1143 | u8 channel = msg->u.rx_can_header.channel; |
1144 | const u8 *rx_msg = NULL; /* GCC */ | |
080f40a6 OS |
1145 | |
1146 | if (channel >= dev->nchannels) { | |
1147 | dev_err(dev->udev->dev.parent, | |
1148 | "Invalid channel number (%d)\n", channel); | |
1149 | return; | |
1150 | } | |
1151 | ||
1152 | priv = dev->nets[channel]; | |
1153 | stats = &priv->netdev->stats; | |
1154 | ||
f5d4abea AD |
1155 | if ((msg->u.rx_can_header.flag & MSG_FLAG_ERROR_FRAME) && |
1156 | (dev->family == KVASER_LEAF && msg->id == CMD_LEAF_LOG_MESSAGE)) { | |
1157 | kvaser_leaf_rx_error(dev, msg); | |
a90f13b2 | 1158 | return; |
f5d4abea AD |
1159 | } else if (msg->u.rx_can_header.flag & (MSG_FLAG_ERROR_FRAME | |
1160 | MSG_FLAG_NERR | | |
1161 | MSG_FLAG_OVERRUN)) { | |
080f40a6 OS |
1162 | kvaser_usb_rx_can_err(priv, msg); |
1163 | return; | |
f5d4abea | 1164 | } else if (msg->u.rx_can_header.flag & ~MSG_FLAG_REMOTE_FRAME) { |
080f40a6 OS |
1165 | netdev_warn(priv->netdev, |
1166 | "Unhandled frame (flags: 0x%02x)", | |
f5d4abea | 1167 | msg->u.rx_can_header.flag); |
080f40a6 OS |
1168 | return; |
1169 | } | |
1170 | ||
f5d4abea AD |
1171 | switch (dev->family) { |
1172 | case KVASER_LEAF: | |
1173 | rx_msg = msg->u.leaf.rx_can.msg; | |
1174 | break; | |
1175 | case KVASER_USBCAN: | |
1176 | rx_msg = msg->u.usbcan.rx_can.msg; | |
1177 | break; | |
1178 | } | |
1179 | ||
080f40a6 OS |
1180 | skb = alloc_can_skb(priv->netdev, &cf); |
1181 | if (!skb) { | |
1182 | stats->tx_dropped++; | |
1183 | return; | |
1184 | } | |
1185 | ||
f5d4abea AD |
1186 | if (dev->family == KVASER_LEAF && msg->id == CMD_LEAF_LOG_MESSAGE) { |
1187 | cf->can_id = le32_to_cpu(msg->u.leaf.log_message.id); | |
a90f13b2 JP |
1188 | if (cf->can_id & KVASER_EXTENDED_FRAME) |
1189 | cf->can_id &= CAN_EFF_MASK | CAN_EFF_FLAG; | |
1190 | else | |
1191 | cf->can_id &= CAN_SFF_MASK; | |
080f40a6 | 1192 | |
f5d4abea | 1193 | cf->can_dlc = get_can_dlc(msg->u.leaf.log_message.dlc); |
a90f13b2 | 1194 | |
f5d4abea | 1195 | if (msg->u.leaf.log_message.flags & MSG_FLAG_REMOTE_FRAME) |
a90f13b2 JP |
1196 | cf->can_id |= CAN_RTR_FLAG; |
1197 | else | |
f5d4abea | 1198 | memcpy(cf->data, &msg->u.leaf.log_message.data, |
a90f13b2 JP |
1199 | cf->can_dlc); |
1200 | } else { | |
f5d4abea | 1201 | cf->can_id = ((rx_msg[0] & 0x1f) << 6) | (rx_msg[1] & 0x3f); |
a90f13b2 JP |
1202 | |
1203 | if (msg->id == CMD_RX_EXT_MESSAGE) { | |
1204 | cf->can_id <<= 18; | |
f5d4abea AD |
1205 | cf->can_id |= ((rx_msg[2] & 0x0f) << 14) | |
1206 | ((rx_msg[3] & 0xff) << 6) | | |
1207 | (rx_msg[4] & 0x3f); | |
a90f13b2 JP |
1208 | cf->can_id |= CAN_EFF_FLAG; |
1209 | } | |
080f40a6 | 1210 | |
f5d4abea | 1211 | cf->can_dlc = get_can_dlc(rx_msg[5]); |
a90f13b2 | 1212 | |
f5d4abea | 1213 | if (msg->u.rx_can_header.flag & MSG_FLAG_REMOTE_FRAME) |
a90f13b2 JP |
1214 | cf->can_id |= CAN_RTR_FLAG; |
1215 | else | |
f5d4abea | 1216 | memcpy(cf->data, &rx_msg[6], |
a90f13b2 JP |
1217 | cf->can_dlc); |
1218 | } | |
080f40a6 | 1219 | |
080f40a6 OS |
1220 | stats->rx_packets++; |
1221 | stats->rx_bytes += cf->can_dlc; | |
a58518cc | 1222 | netif_rx(skb); |
080f40a6 OS |
1223 | } |
1224 | ||
1225 | static void kvaser_usb_start_chip_reply(const struct kvaser_usb *dev, | |
1226 | const struct kvaser_msg *msg) | |
1227 | { | |
1228 | struct kvaser_usb_net_priv *priv; | |
1229 | u8 channel = msg->u.simple.channel; | |
1230 | ||
1231 | if (channel >= dev->nchannels) { | |
1232 | dev_err(dev->udev->dev.parent, | |
1233 | "Invalid channel number (%d)\n", channel); | |
1234 | return; | |
1235 | } | |
1236 | ||
1237 | priv = dev->nets[channel]; | |
1238 | ||
1239 | if (completion_done(&priv->start_comp) && | |
1240 | netif_queue_stopped(priv->netdev)) { | |
1241 | netif_wake_queue(priv->netdev); | |
1242 | } else { | |
1243 | netif_start_queue(priv->netdev); | |
1244 | complete(&priv->start_comp); | |
1245 | } | |
1246 | } | |
1247 | ||
1248 | static void kvaser_usb_stop_chip_reply(const struct kvaser_usb *dev, | |
1249 | const struct kvaser_msg *msg) | |
1250 | { | |
1251 | struct kvaser_usb_net_priv *priv; | |
1252 | u8 channel = msg->u.simple.channel; | |
1253 | ||
1254 | if (channel >= dev->nchannels) { | |
1255 | dev_err(dev->udev->dev.parent, | |
1256 | "Invalid channel number (%d)\n", channel); | |
1257 | return; | |
1258 | } | |
1259 | ||
1260 | priv = dev->nets[channel]; | |
1261 | ||
1262 | complete(&priv->stop_comp); | |
1263 | } | |
1264 | ||
1265 | static void kvaser_usb_handle_message(const struct kvaser_usb *dev, | |
1266 | const struct kvaser_msg *msg) | |
1267 | { | |
1268 | switch (msg->id) { | |
1269 | case CMD_START_CHIP_REPLY: | |
1270 | kvaser_usb_start_chip_reply(dev, msg); | |
1271 | break; | |
1272 | ||
1273 | case CMD_STOP_CHIP_REPLY: | |
1274 | kvaser_usb_stop_chip_reply(dev, msg); | |
1275 | break; | |
1276 | ||
1277 | case CMD_RX_STD_MESSAGE: | |
1278 | case CMD_RX_EXT_MESSAGE: | |
f5d4abea AD |
1279 | kvaser_usb_rx_can_msg(dev, msg); |
1280 | break; | |
1281 | ||
1282 | case CMD_LEAF_LOG_MESSAGE: | |
1283 | if (dev->family != KVASER_LEAF) | |
1284 | goto warn; | |
080f40a6 OS |
1285 | kvaser_usb_rx_can_msg(dev, msg); |
1286 | break; | |
1287 | ||
1288 | case CMD_CHIP_STATE_EVENT: | |
1289 | case CMD_CAN_ERROR_EVENT: | |
f5d4abea AD |
1290 | if (dev->family == KVASER_LEAF) |
1291 | kvaser_leaf_rx_error(dev, msg); | |
1292 | else | |
1293 | kvaser_usbcan_rx_error(dev, msg); | |
080f40a6 OS |
1294 | break; |
1295 | ||
080f40a6 OS |
1296 | case CMD_TX_ACKNOWLEDGE: |
1297 | kvaser_usb_tx_acknowledge(dev, msg); | |
1298 | break; | |
1299 | ||
f5d4abea AD |
1300 | /* Ignored messages */ |
1301 | case CMD_USBCAN_CLOCK_OVERFLOW_EVENT: | |
1302 | if (dev->family != KVASER_USBCAN) | |
1303 | goto warn; | |
1304 | break; | |
1305 | ||
e1d2d132 JA |
1306 | case CMD_FLUSH_QUEUE_REPLY: |
1307 | if (dev->family != KVASER_LEAF) | |
1308 | goto warn; | |
1309 | break; | |
1310 | ||
080f40a6 | 1311 | default: |
f5d4abea | 1312 | warn: dev_warn(dev->udev->dev.parent, |
080f40a6 OS |
1313 | "Unhandled message (%d)\n", msg->id); |
1314 | break; | |
1315 | } | |
1316 | } | |
1317 | ||
1318 | static void kvaser_usb_read_bulk_callback(struct urb *urb) | |
1319 | { | |
1320 | struct kvaser_usb *dev = urb->context; | |
1321 | struct kvaser_msg *msg; | |
1322 | int pos = 0; | |
1323 | int err, i; | |
1324 | ||
1325 | switch (urb->status) { | |
1326 | case 0: | |
1327 | break; | |
1328 | case -ENOENT: | |
6aa8d594 MK |
1329 | case -EPIPE: |
1330 | case -EPROTO: | |
080f40a6 OS |
1331 | case -ESHUTDOWN: |
1332 | return; | |
1333 | default: | |
1334 | dev_info(dev->udev->dev.parent, "Rx URB aborted (%d)\n", | |
1335 | urb->status); | |
1336 | goto resubmit_urb; | |
1337 | } | |
1338 | ||
e84f44eb | 1339 | while (pos <= (int)(urb->actual_length - MSG_HEADER_LEN)) { |
080f40a6 OS |
1340 | msg = urb->transfer_buffer + pos; |
1341 | ||
2fec5104 AD |
1342 | /* The Kvaser firmware can only read and write messages that |
1343 | * does not cross the USB's endpoint wMaxPacketSize boundary. | |
1344 | * If a follow-up command crosses such boundary, firmware puts | |
1345 | * a placeholder zero-length command in its place then aligns | |
1346 | * the real command to the next max packet size. | |
1347 | * | |
1348 | * Handle such cases or we're going to miss a significant | |
1349 | * number of events in case of a heavy rx load on the bus. | |
1350 | */ | |
1351 | if (msg->len == 0) { | |
7e184c28 AD |
1352 | pos = round_up(pos, le16_to_cpu(dev->bulk_in-> |
1353 | wMaxPacketSize)); | |
2fec5104 AD |
1354 | continue; |
1355 | } | |
080f40a6 OS |
1356 | |
1357 | if (pos + msg->len > urb->actual_length) { | |
8bd13bd5 JA |
1358 | dev_err_ratelimited(dev->udev->dev.parent, |
1359 | "Format error\n"); | |
080f40a6 OS |
1360 | break; |
1361 | } | |
1362 | ||
1363 | kvaser_usb_handle_message(dev, msg); | |
080f40a6 OS |
1364 | pos += msg->len; |
1365 | } | |
1366 | ||
1367 | resubmit_urb: | |
1368 | usb_fill_bulk_urb(urb, dev->udev, | |
1369 | usb_rcvbulkpipe(dev->udev, | |
1370 | dev->bulk_in->bEndpointAddress), | |
1371 | urb->transfer_buffer, RX_BUFFER_SIZE, | |
1372 | kvaser_usb_read_bulk_callback, dev); | |
1373 | ||
1374 | err = usb_submit_urb(urb, GFP_ATOMIC); | |
1375 | if (err == -ENODEV) { | |
1376 | for (i = 0; i < dev->nchannels; i++) { | |
1377 | if (!dev->nets[i]) | |
1378 | continue; | |
1379 | ||
1380 | netif_device_detach(dev->nets[i]->netdev); | |
1381 | } | |
1382 | } else if (err) { | |
1383 | dev_err(dev->udev->dev.parent, | |
1384 | "Failed resubmitting read bulk urb: %d\n", err); | |
1385 | } | |
1386 | ||
1387 | return; | |
1388 | } | |
1389 | ||
1390 | static int kvaser_usb_setup_rx_urbs(struct kvaser_usb *dev) | |
1391 | { | |
1392 | int i, err = 0; | |
1393 | ||
1394 | if (dev->rxinitdone) | |
1395 | return 0; | |
1396 | ||
1397 | for (i = 0; i < MAX_RX_URBS; i++) { | |
1398 | struct urb *urb = NULL; | |
1399 | u8 *buf = NULL; | |
1400 | dma_addr_t buf_dma; | |
1401 | ||
1402 | urb = usb_alloc_urb(0, GFP_KERNEL); | |
1403 | if (!urb) { | |
080f40a6 OS |
1404 | err = -ENOMEM; |
1405 | break; | |
1406 | } | |
1407 | ||
1408 | buf = usb_alloc_coherent(dev->udev, RX_BUFFER_SIZE, | |
1409 | GFP_KERNEL, &buf_dma); | |
1410 | if (!buf) { | |
1411 | dev_warn(dev->udev->dev.parent, | |
1412 | "No memory left for USB buffer\n"); | |
1413 | usb_free_urb(urb); | |
1414 | err = -ENOMEM; | |
1415 | break; | |
1416 | } | |
1417 | ||
1418 | usb_fill_bulk_urb(urb, dev->udev, | |
1419 | usb_rcvbulkpipe(dev->udev, | |
1420 | dev->bulk_in->bEndpointAddress), | |
1421 | buf, RX_BUFFER_SIZE, | |
1422 | kvaser_usb_read_bulk_callback, | |
1423 | dev); | |
1424 | urb->transfer_dma = buf_dma; | |
1425 | urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; | |
1426 | usb_anchor_urb(urb, &dev->rx_submitted); | |
1427 | ||
1428 | err = usb_submit_urb(urb, GFP_KERNEL); | |
1429 | if (err) { | |
1430 | usb_unanchor_urb(urb); | |
1431 | usb_free_coherent(dev->udev, RX_BUFFER_SIZE, buf, | |
1432 | buf_dma); | |
1433 | usb_free_urb(urb); | |
1434 | break; | |
1435 | } | |
1436 | ||
1437 | dev->rxbuf[i] = buf; | |
1438 | dev->rxbuf_dma[i] = buf_dma; | |
1439 | ||
1440 | usb_free_urb(urb); | |
1441 | } | |
1442 | ||
1443 | if (i == 0) { | |
1444 | dev_warn(dev->udev->dev.parent, | |
1445 | "Cannot setup read URBs, error %d\n", err); | |
1446 | return err; | |
1447 | } else if (i < MAX_RX_URBS) { | |
1448 | dev_warn(dev->udev->dev.parent, | |
1449 | "RX performances may be slow\n"); | |
1450 | } | |
1451 | ||
1452 | dev->rxinitdone = true; | |
1453 | ||
1454 | return 0; | |
1455 | } | |
1456 | ||
1457 | static int kvaser_usb_set_opt_mode(const struct kvaser_usb_net_priv *priv) | |
1458 | { | |
1459 | struct kvaser_msg *msg; | |
1460 | int rc; | |
1461 | ||
1462 | msg = kmalloc(sizeof(*msg), GFP_KERNEL); | |
1463 | if (!msg) | |
1464 | return -ENOMEM; | |
1465 | ||
1466 | msg->id = CMD_SET_CTRL_MODE; | |
1467 | msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_ctrl_mode); | |
1468 | msg->u.ctrl_mode.tid = 0xff; | |
1469 | msg->u.ctrl_mode.channel = priv->channel; | |
1470 | ||
1471 | if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY) | |
1472 | msg->u.ctrl_mode.ctrl_mode = KVASER_CTRL_MODE_SILENT; | |
1473 | else | |
1474 | msg->u.ctrl_mode.ctrl_mode = KVASER_CTRL_MODE_NORMAL; | |
1475 | ||
1476 | rc = kvaser_usb_send_msg(priv->dev, msg); | |
1477 | ||
1478 | kfree(msg); | |
1479 | return rc; | |
1480 | } | |
1481 | ||
1482 | static int kvaser_usb_start_chip(struct kvaser_usb_net_priv *priv) | |
1483 | { | |
1484 | int err; | |
1485 | ||
1486 | init_completion(&priv->start_comp); | |
1487 | ||
1488 | err = kvaser_usb_send_simple_msg(priv->dev, CMD_START_CHIP, | |
1489 | priv->channel); | |
1490 | if (err) | |
1491 | return err; | |
1492 | ||
1493 | if (!wait_for_completion_timeout(&priv->start_comp, | |
1494 | msecs_to_jiffies(START_TIMEOUT))) | |
1495 | return -ETIMEDOUT; | |
1496 | ||
1497 | return 0; | |
1498 | } | |
1499 | ||
1500 | static int kvaser_usb_open(struct net_device *netdev) | |
1501 | { | |
1502 | struct kvaser_usb_net_priv *priv = netdev_priv(netdev); | |
1503 | struct kvaser_usb *dev = priv->dev; | |
1504 | int err; | |
1505 | ||
1506 | err = open_candev(netdev); | |
1507 | if (err) | |
1508 | return err; | |
1509 | ||
1510 | err = kvaser_usb_setup_rx_urbs(dev); | |
1511 | if (err) | |
1512 | goto error; | |
1513 | ||
1514 | err = kvaser_usb_set_opt_mode(priv); | |
1515 | if (err) | |
1516 | goto error; | |
1517 | ||
1518 | err = kvaser_usb_start_chip(priv); | |
1519 | if (err) { | |
1520 | netdev_warn(netdev, "Cannot start device, error %d\n", err); | |
1521 | goto error; | |
1522 | } | |
1523 | ||
1524 | priv->can.state = CAN_STATE_ERROR_ACTIVE; | |
1525 | ||
1526 | return 0; | |
1527 | ||
1528 | error: | |
1529 | close_candev(netdev); | |
1530 | return err; | |
1531 | } | |
1532 | ||
a9dc960c AD |
1533 | static void kvaser_usb_reset_tx_urb_contexts(struct kvaser_usb_net_priv *priv) |
1534 | { | |
082d70b6 AD |
1535 | int i, max_tx_urbs; |
1536 | ||
1537 | max_tx_urbs = priv->dev->max_tx_urbs; | |
a9dc960c AD |
1538 | |
1539 | priv->active_tx_contexts = 0; | |
082d70b6 AD |
1540 | for (i = 0; i < max_tx_urbs; i++) |
1541 | priv->tx_contexts[i].echo_index = max_tx_urbs; | |
a9dc960c AD |
1542 | } |
1543 | ||
1544 | /* This method might sleep. Do not call it in the atomic context | |
1545 | * of URB completions. | |
1546 | */ | |
1547 | static void kvaser_usb_unlink_tx_urbs(struct kvaser_usb_net_priv *priv) | |
1548 | { | |
1549 | usb_kill_anchored_urbs(&priv->tx_submitted); | |
1550 | kvaser_usb_reset_tx_urb_contexts(priv); | |
1551 | } | |
1552 | ||
080f40a6 OS |
1553 | static void kvaser_usb_unlink_all_urbs(struct kvaser_usb *dev) |
1554 | { | |
1555 | int i; | |
1556 | ||
1557 | usb_kill_anchored_urbs(&dev->rx_submitted); | |
1558 | ||
1559 | for (i = 0; i < MAX_RX_URBS; i++) | |
1560 | usb_free_coherent(dev->udev, RX_BUFFER_SIZE, | |
1561 | dev->rxbuf[i], | |
1562 | dev->rxbuf_dma[i]); | |
1563 | ||
f5d4abea | 1564 | for (i = 0; i < dev->nchannels; i++) { |
080f40a6 OS |
1565 | struct kvaser_usb_net_priv *priv = dev->nets[i]; |
1566 | ||
1567 | if (priv) | |
1568 | kvaser_usb_unlink_tx_urbs(priv); | |
1569 | } | |
1570 | } | |
1571 | ||
1572 | static int kvaser_usb_stop_chip(struct kvaser_usb_net_priv *priv) | |
1573 | { | |
1574 | int err; | |
1575 | ||
1576 | init_completion(&priv->stop_comp); | |
1577 | ||
1578 | err = kvaser_usb_send_simple_msg(priv->dev, CMD_STOP_CHIP, | |
1579 | priv->channel); | |
1580 | if (err) | |
1581 | return err; | |
1582 | ||
1583 | if (!wait_for_completion_timeout(&priv->stop_comp, | |
1584 | msecs_to_jiffies(STOP_TIMEOUT))) | |
1585 | return -ETIMEDOUT; | |
1586 | ||
1587 | return 0; | |
1588 | } | |
1589 | ||
1590 | static int kvaser_usb_flush_queue(struct kvaser_usb_net_priv *priv) | |
1591 | { | |
1592 | struct kvaser_msg *msg; | |
1593 | int rc; | |
1594 | ||
1595 | msg = kmalloc(sizeof(*msg), GFP_KERNEL); | |
1596 | if (!msg) | |
1597 | return -ENOMEM; | |
1598 | ||
1599 | msg->id = CMD_FLUSH_QUEUE; | |
1600 | msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_flush_queue); | |
1601 | msg->u.flush_queue.channel = priv->channel; | |
1602 | msg->u.flush_queue.flags = 0x00; | |
1603 | ||
1604 | rc = kvaser_usb_send_msg(priv->dev, msg); | |
1605 | ||
1606 | kfree(msg); | |
1607 | return rc; | |
1608 | } | |
1609 | ||
1610 | static int kvaser_usb_close(struct net_device *netdev) | |
1611 | { | |
1612 | struct kvaser_usb_net_priv *priv = netdev_priv(netdev); | |
1613 | struct kvaser_usb *dev = priv->dev; | |
1614 | int err; | |
1615 | ||
1616 | netif_stop_queue(netdev); | |
1617 | ||
1618 | err = kvaser_usb_flush_queue(priv); | |
1619 | if (err) | |
1620 | netdev_warn(netdev, "Cannot flush queue, error %d\n", err); | |
1621 | ||
8f65a923 JA |
1622 | err = kvaser_usb_send_simple_msg(dev, CMD_RESET_CHIP, priv->channel); |
1623 | if (err) | |
080f40a6 OS |
1624 | netdev_warn(netdev, "Cannot reset card, error %d\n", err); |
1625 | ||
1626 | err = kvaser_usb_stop_chip(priv); | |
1627 | if (err) | |
1628 | netdev_warn(netdev, "Cannot stop device, error %d\n", err); | |
1629 | ||
889b77f7 AD |
1630 | /* reset tx contexts */ |
1631 | kvaser_usb_unlink_tx_urbs(priv); | |
1632 | ||
080f40a6 OS |
1633 | priv->can.state = CAN_STATE_STOPPED; |
1634 | close_candev(priv->netdev); | |
1635 | ||
1636 | return 0; | |
1637 | } | |
1638 | ||
1639 | static void kvaser_usb_write_bulk_callback(struct urb *urb) | |
1640 | { | |
1641 | struct kvaser_usb_tx_urb_context *context = urb->context; | |
1642 | struct kvaser_usb_net_priv *priv; | |
1643 | struct net_device *netdev; | |
1644 | ||
1645 | if (WARN_ON(!context)) | |
1646 | return; | |
1647 | ||
1648 | priv = context->priv; | |
1649 | netdev = priv->netdev; | |
1650 | ||
1651 | kfree(urb->transfer_buffer); | |
1652 | ||
1653 | if (!netif_device_present(netdev)) | |
1654 | return; | |
1655 | ||
1656 | if (urb->status) | |
1657 | netdev_info(netdev, "Tx URB aborted (%d)\n", urb->status); | |
1658 | } | |
1659 | ||
1660 | static netdev_tx_t kvaser_usb_start_xmit(struct sk_buff *skb, | |
1661 | struct net_device *netdev) | |
1662 | { | |
1663 | struct kvaser_usb_net_priv *priv = netdev_priv(netdev); | |
1664 | struct kvaser_usb *dev = priv->dev; | |
1665 | struct net_device_stats *stats = &netdev->stats; | |
1666 | struct can_frame *cf = (struct can_frame *)skb->data; | |
1667 | struct kvaser_usb_tx_urb_context *context = NULL; | |
1668 | struct urb *urb; | |
1669 | void *buf; | |
1670 | struct kvaser_msg *msg; | |
deb2701c | 1671 | int i, err, ret = NETDEV_TX_OK; |
f5d4abea | 1672 | u8 *msg_tx_can_flags = NULL; /* GCC */ |
a9dc960c | 1673 | unsigned long flags; |
080f40a6 OS |
1674 | |
1675 | if (can_dropped_invalid_skb(netdev, skb)) | |
1676 | return NETDEV_TX_OK; | |
1677 | ||
1678 | urb = usb_alloc_urb(0, GFP_ATOMIC); | |
1679 | if (!urb) { | |
080f40a6 | 1680 | stats->tx_dropped++; |
b442723f AD |
1681 | dev_kfree_skb(skb); |
1682 | return NETDEV_TX_OK; | |
080f40a6 OS |
1683 | } |
1684 | ||
1685 | buf = kmalloc(sizeof(struct kvaser_msg), GFP_ATOMIC); | |
1686 | if (!buf) { | |
080f40a6 | 1687 | stats->tx_dropped++; |
b442723f | 1688 | dev_kfree_skb(skb); |
deb2701c | 1689 | goto freeurb; |
080f40a6 OS |
1690 | } |
1691 | ||
1692 | msg = buf; | |
1693 | msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_tx_can); | |
080f40a6 OS |
1694 | msg->u.tx_can.channel = priv->channel; |
1695 | ||
f5d4abea AD |
1696 | switch (dev->family) { |
1697 | case KVASER_LEAF: | |
1698 | msg_tx_can_flags = &msg->u.tx_can.leaf.flags; | |
1699 | break; | |
1700 | case KVASER_USBCAN: | |
1701 | msg_tx_can_flags = &msg->u.tx_can.usbcan.flags; | |
1702 | break; | |
1703 | } | |
1704 | ||
1705 | *msg_tx_can_flags = 0; | |
1706 | ||
080f40a6 OS |
1707 | if (cf->can_id & CAN_EFF_FLAG) { |
1708 | msg->id = CMD_TX_EXT_MESSAGE; | |
1709 | msg->u.tx_can.msg[0] = (cf->can_id >> 24) & 0x1f; | |
1710 | msg->u.tx_can.msg[1] = (cf->can_id >> 18) & 0x3f; | |
1711 | msg->u.tx_can.msg[2] = (cf->can_id >> 14) & 0x0f; | |
1712 | msg->u.tx_can.msg[3] = (cf->can_id >> 6) & 0xff; | |
1713 | msg->u.tx_can.msg[4] = cf->can_id & 0x3f; | |
1714 | } else { | |
1715 | msg->id = CMD_TX_STD_MESSAGE; | |
1716 | msg->u.tx_can.msg[0] = (cf->can_id >> 6) & 0x1f; | |
1717 | msg->u.tx_can.msg[1] = cf->can_id & 0x3f; | |
1718 | } | |
1719 | ||
1720 | msg->u.tx_can.msg[5] = cf->can_dlc; | |
1721 | memcpy(&msg->u.tx_can.msg[6], cf->data, cf->can_dlc); | |
1722 | ||
1723 | if (cf->can_id & CAN_RTR_FLAG) | |
f5d4abea | 1724 | *msg_tx_can_flags |= MSG_FLAG_REMOTE_FRAME; |
080f40a6 | 1725 | |
a9dc960c | 1726 | spin_lock_irqsave(&priv->tx_contexts_lock, flags); |
082d70b6 AD |
1727 | for (i = 0; i < dev->max_tx_urbs; i++) { |
1728 | if (priv->tx_contexts[i].echo_index == dev->max_tx_urbs) { | |
080f40a6 | 1729 | context = &priv->tx_contexts[i]; |
a9dc960c AD |
1730 | |
1731 | context->echo_index = i; | |
1732 | can_put_echo_skb(skb, netdev, context->echo_index); | |
1733 | ++priv->active_tx_contexts; | |
082d70b6 | 1734 | if (priv->active_tx_contexts >= dev->max_tx_urbs) |
a9dc960c AD |
1735 | netif_stop_queue(netdev); |
1736 | ||
080f40a6 OS |
1737 | break; |
1738 | } | |
1739 | } | |
a9dc960c | 1740 | spin_unlock_irqrestore(&priv->tx_contexts_lock, flags); |
080f40a6 | 1741 | |
b442723f | 1742 | /* This should never happen; it implies a flow control bug */ |
080f40a6 OS |
1743 | if (!context) { |
1744 | netdev_warn(netdev, "cannot find free context\n"); | |
deb2701c AD |
1745 | |
1746 | kfree(buf); | |
080f40a6 | 1747 | ret = NETDEV_TX_BUSY; |
deb2701c | 1748 | goto freeurb; |
080f40a6 OS |
1749 | } |
1750 | ||
1751 | context->priv = priv; | |
080f40a6 OS |
1752 | context->dlc = cf->can_dlc; |
1753 | ||
1754 | msg->u.tx_can.tid = context->echo_index; | |
1755 | ||
1756 | usb_fill_bulk_urb(urb, dev->udev, | |
1757 | usb_sndbulkpipe(dev->udev, | |
1758 | dev->bulk_out->bEndpointAddress), | |
1759 | buf, msg->len, | |
1760 | kvaser_usb_write_bulk_callback, context); | |
1761 | usb_anchor_urb(urb, &priv->tx_submitted); | |
1762 | ||
080f40a6 OS |
1763 | err = usb_submit_urb(urb, GFP_ATOMIC); |
1764 | if (unlikely(err)) { | |
a9dc960c AD |
1765 | spin_lock_irqsave(&priv->tx_contexts_lock, flags); |
1766 | ||
080f40a6 | 1767 | can_free_echo_skb(netdev, context->echo_index); |
082d70b6 | 1768 | context->echo_index = dev->max_tx_urbs; |
a9dc960c AD |
1769 | --priv->active_tx_contexts; |
1770 | netif_wake_queue(netdev); | |
1771 | ||
1772 | spin_unlock_irqrestore(&priv->tx_contexts_lock, flags); | |
080f40a6 | 1773 | |
080f40a6 | 1774 | usb_unanchor_urb(urb); |
435019b4 | 1775 | kfree(buf); |
080f40a6 OS |
1776 | |
1777 | stats->tx_dropped++; | |
1778 | ||
1779 | if (err == -ENODEV) | |
1780 | netif_device_detach(netdev); | |
1781 | else | |
1782 | netdev_warn(netdev, "Failed tx_urb %d\n", err); | |
1783 | ||
deb2701c | 1784 | goto freeurb; |
080f40a6 OS |
1785 | } |
1786 | ||
deb2701c | 1787 | ret = NETDEV_TX_OK; |
080f40a6 | 1788 | |
deb2701c | 1789 | freeurb: |
080f40a6 | 1790 | usb_free_urb(urb); |
080f40a6 OS |
1791 | return ret; |
1792 | } | |
1793 | ||
1794 | static const struct net_device_ops kvaser_usb_netdev_ops = { | |
1795 | .ndo_open = kvaser_usb_open, | |
1796 | .ndo_stop = kvaser_usb_close, | |
1797 | .ndo_start_xmit = kvaser_usb_start_xmit, | |
c971fa2a | 1798 | .ndo_change_mtu = can_change_mtu, |
080f40a6 OS |
1799 | }; |
1800 | ||
1801 | static const struct can_bittiming_const kvaser_usb_bittiming_const = { | |
1802 | .name = "kvaser_usb", | |
1803 | .tseg1_min = KVASER_USB_TSEG1_MIN, | |
1804 | .tseg1_max = KVASER_USB_TSEG1_MAX, | |
1805 | .tseg2_min = KVASER_USB_TSEG2_MIN, | |
1806 | .tseg2_max = KVASER_USB_TSEG2_MAX, | |
1807 | .sjw_max = KVASER_USB_SJW_MAX, | |
1808 | .brp_min = KVASER_USB_BRP_MIN, | |
1809 | .brp_max = KVASER_USB_BRP_MAX, | |
1810 | .brp_inc = KVASER_USB_BRP_INC, | |
1811 | }; | |
1812 | ||
1813 | static int kvaser_usb_set_bittiming(struct net_device *netdev) | |
1814 | { | |
1815 | struct kvaser_usb_net_priv *priv = netdev_priv(netdev); | |
1816 | struct can_bittiming *bt = &priv->can.bittiming; | |
1817 | struct kvaser_usb *dev = priv->dev; | |
1818 | struct kvaser_msg *msg; | |
1819 | int rc; | |
1820 | ||
1821 | msg = kmalloc(sizeof(*msg), GFP_KERNEL); | |
1822 | if (!msg) | |
1823 | return -ENOMEM; | |
1824 | ||
1825 | msg->id = CMD_SET_BUS_PARAMS; | |
1826 | msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_busparams); | |
1827 | msg->u.busparams.channel = priv->channel; | |
1828 | msg->u.busparams.tid = 0xff; | |
1829 | msg->u.busparams.bitrate = cpu_to_le32(bt->bitrate); | |
1830 | msg->u.busparams.sjw = bt->sjw; | |
1831 | msg->u.busparams.tseg1 = bt->prop_seg + bt->phase_seg1; | |
1832 | msg->u.busparams.tseg2 = bt->phase_seg2; | |
1833 | ||
1834 | if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES) | |
1835 | msg->u.busparams.no_samp = 3; | |
1836 | else | |
1837 | msg->u.busparams.no_samp = 1; | |
1838 | ||
1839 | rc = kvaser_usb_send_msg(dev, msg); | |
1840 | ||
1841 | kfree(msg); | |
1842 | return rc; | |
1843 | } | |
1844 | ||
1845 | static int kvaser_usb_set_mode(struct net_device *netdev, | |
1846 | enum can_mode mode) | |
1847 | { | |
1848 | struct kvaser_usb_net_priv *priv = netdev_priv(netdev); | |
1849 | int err; | |
1850 | ||
1851 | switch (mode) { | |
1852 | case CAN_MODE_START: | |
1853 | err = kvaser_usb_simple_msg_async(priv, CMD_START_CHIP); | |
1854 | if (err) | |
1855 | return err; | |
1856 | break; | |
1857 | default: | |
1858 | return -EOPNOTSUPP; | |
1859 | } | |
1860 | ||
1861 | return 0; | |
1862 | } | |
1863 | ||
1864 | static int kvaser_usb_get_berr_counter(const struct net_device *netdev, | |
1865 | struct can_berr_counter *bec) | |
1866 | { | |
1867 | struct kvaser_usb_net_priv *priv = netdev_priv(netdev); | |
1868 | ||
1869 | *bec = priv->bec; | |
1870 | ||
1871 | return 0; | |
1872 | } | |
1873 | ||
1874 | static void kvaser_usb_remove_interfaces(struct kvaser_usb *dev) | |
1875 | { | |
1876 | int i; | |
1877 | ||
1878 | for (i = 0; i < dev->nchannels; i++) { | |
1879 | if (!dev->nets[i]) | |
1880 | continue; | |
1881 | ||
c637aab0 | 1882 | unregister_candev(dev->nets[i]->netdev); |
080f40a6 OS |
1883 | } |
1884 | ||
1885 | kvaser_usb_unlink_all_urbs(dev); | |
1886 | ||
1887 | for (i = 0; i < dev->nchannels; i++) { | |
1888 | if (!dev->nets[i]) | |
1889 | continue; | |
1890 | ||
1891 | free_candev(dev->nets[i]->netdev); | |
1892 | } | |
1893 | } | |
1894 | ||
1895 | static int kvaser_usb_init_one(struct usb_interface *intf, | |
1896 | const struct usb_device_id *id, int channel) | |
1897 | { | |
1898 | struct kvaser_usb *dev = usb_get_intfdata(intf); | |
1899 | struct net_device *netdev; | |
1900 | struct kvaser_usb_net_priv *priv; | |
a9dc960c | 1901 | int err; |
080f40a6 | 1902 | |
5e7e6e0c AD |
1903 | err = kvaser_usb_send_simple_msg(dev, CMD_RESET_CHIP, channel); |
1904 | if (err) | |
1905 | return err; | |
1906 | ||
082d70b6 AD |
1907 | netdev = alloc_candev(sizeof(*priv) + |
1908 | dev->max_tx_urbs * sizeof(*priv->tx_contexts), | |
1909 | dev->max_tx_urbs); | |
080f40a6 OS |
1910 | if (!netdev) { |
1911 | dev_err(&intf->dev, "Cannot alloc candev\n"); | |
1912 | return -ENOMEM; | |
1913 | } | |
1914 | ||
1915 | priv = netdev_priv(netdev); | |
1916 | ||
a9dc960c | 1917 | init_usb_anchor(&priv->tx_submitted); |
080f40a6 OS |
1918 | init_completion(&priv->start_comp); |
1919 | init_completion(&priv->stop_comp); | |
1920 | ||
080f40a6 OS |
1921 | priv->dev = dev; |
1922 | priv->netdev = netdev; | |
1923 | priv->channel = channel; | |
1924 | ||
a9dc960c AD |
1925 | spin_lock_init(&priv->tx_contexts_lock); |
1926 | kvaser_usb_reset_tx_urb_contexts(priv); | |
1927 | ||
080f40a6 OS |
1928 | priv->can.state = CAN_STATE_STOPPED; |
1929 | priv->can.clock.freq = CAN_USB_CLOCK; | |
1930 | priv->can.bittiming_const = &kvaser_usb_bittiming_const; | |
1931 | priv->can.do_set_bittiming = kvaser_usb_set_bittiming; | |
1932 | priv->can.do_set_mode = kvaser_usb_set_mode; | |
1933 | if (id->driver_info & KVASER_HAS_TXRX_ERRORS) | |
1934 | priv->can.do_get_berr_counter = kvaser_usb_get_berr_counter; | |
1935 | priv->can.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES; | |
1936 | if (id->driver_info & KVASER_HAS_SILENT_MODE) | |
1937 | priv->can.ctrlmode_supported |= CAN_CTRLMODE_LISTENONLY; | |
1938 | ||
1939 | netdev->flags |= IFF_ECHO; | |
1940 | ||
1941 | netdev->netdev_ops = &kvaser_usb_netdev_ops; | |
1942 | ||
1943 | SET_NETDEV_DEV(netdev, &intf->dev); | |
3e66d013 | 1944 | netdev->dev_id = channel; |
080f40a6 OS |
1945 | |
1946 | dev->nets[channel] = priv; | |
1947 | ||
1948 | err = register_candev(netdev); | |
1949 | if (err) { | |
1950 | dev_err(&intf->dev, "Failed to register can device\n"); | |
1951 | free_candev(netdev); | |
1952 | dev->nets[channel] = NULL; | |
1953 | return err; | |
1954 | } | |
1955 | ||
1956 | netdev_dbg(netdev, "device registered\n"); | |
1957 | ||
1958 | return 0; | |
1959 | } | |
1960 | ||
896e23bd OS |
1961 | static int kvaser_usb_get_endpoints(const struct usb_interface *intf, |
1962 | struct usb_endpoint_descriptor **in, | |
1963 | struct usb_endpoint_descriptor **out) | |
080f40a6 OS |
1964 | { |
1965 | const struct usb_host_interface *iface_desc; | |
1966 | struct usb_endpoint_descriptor *endpoint; | |
1967 | int i; | |
1968 | ||
1969 | iface_desc = &intf->altsetting[0]; | |
1970 | ||
1971 | for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) { | |
1972 | endpoint = &iface_desc->endpoint[i].desc; | |
1973 | ||
896e23bd | 1974 | if (!*in && usb_endpoint_is_bulk_in(endpoint)) |
080f40a6 OS |
1975 | *in = endpoint; |
1976 | ||
896e23bd | 1977 | if (!*out && usb_endpoint_is_bulk_out(endpoint)) |
080f40a6 | 1978 | *out = endpoint; |
896e23bd OS |
1979 | |
1980 | /* use first bulk endpoint for in and out */ | |
1981 | if (*in && *out) | |
1982 | return 0; | |
080f40a6 | 1983 | } |
896e23bd OS |
1984 | |
1985 | return -ENODEV; | |
080f40a6 OS |
1986 | } |
1987 | ||
1988 | static int kvaser_usb_probe(struct usb_interface *intf, | |
1989 | const struct usb_device_id *id) | |
1990 | { | |
1991 | struct kvaser_usb *dev; | |
1992 | int err = -ENOMEM; | |
14c10c2a | 1993 | int i, retry = 3; |
080f40a6 OS |
1994 | |
1995 | dev = devm_kzalloc(&intf->dev, sizeof(*dev), GFP_KERNEL); | |
1996 | if (!dev) | |
1997 | return -ENOMEM; | |
1998 | ||
f5d4abea AD |
1999 | if (kvaser_is_leaf(id)) { |
2000 | dev->family = KVASER_LEAF; | |
2001 | } else if (kvaser_is_usbcan(id)) { | |
2002 | dev->family = KVASER_USBCAN; | |
2003 | } else { | |
2004 | dev_err(&intf->dev, | |
2005 | "Product ID (%d) does not belong to any known Kvaser USB family", | |
2006 | id->idProduct); | |
2007 | return -ENODEV; | |
2008 | } | |
2009 | ||
896e23bd OS |
2010 | err = kvaser_usb_get_endpoints(intf, &dev->bulk_in, &dev->bulk_out); |
2011 | if (err) { | |
080f40a6 OS |
2012 | dev_err(&intf->dev, "Cannot get usb endpoint(s)"); |
2013 | return err; | |
2014 | } | |
2015 | ||
2016 | dev->udev = interface_to_usbdev(intf); | |
2017 | ||
2018 | init_usb_anchor(&dev->rx_submitted); | |
2019 | ||
2020 | usb_set_intfdata(intf, dev); | |
2021 | ||
14c10c2a AD |
2022 | /* On some x86 laptops, plugging a Kvaser device again after |
2023 | * an unplug makes the firmware always ignore the very first | |
2024 | * command. For such a case, provide some room for retries | |
2025 | * instead of completely exiting the driver. | |
2026 | */ | |
2027 | do { | |
2028 | err = kvaser_usb_get_software_info(dev); | |
2029 | } while (--retry && err == -ETIMEDOUT); | |
2030 | ||
080f40a6 OS |
2031 | if (err) { |
2032 | dev_err(&intf->dev, | |
2033 | "Cannot get software infos, error %d\n", err); | |
2034 | return err; | |
2035 | } | |
2036 | ||
082d70b6 AD |
2037 | dev_dbg(&intf->dev, "Firmware version: %d.%d.%d\n", |
2038 | ((dev->fw_version >> 24) & 0xff), | |
2039 | ((dev->fw_version >> 16) & 0xff), | |
2040 | (dev->fw_version & 0xffff)); | |
2041 | ||
0d8f8efd | 2042 | dev_dbg(&intf->dev, "Max outstanding tx = %d URBs\n", dev->max_tx_urbs); |
082d70b6 | 2043 | |
080f40a6 OS |
2044 | err = kvaser_usb_get_card_info(dev); |
2045 | if (err) { | |
2046 | dev_err(&intf->dev, | |
2047 | "Cannot get card infos, error %d\n", err); | |
2048 | return err; | |
2049 | } | |
2050 | ||
080f40a6 OS |
2051 | for (i = 0; i < dev->nchannels; i++) { |
2052 | err = kvaser_usb_init_one(intf, id, i); | |
2053 | if (err) { | |
2054 | kvaser_usb_remove_interfaces(dev); | |
2055 | return err; | |
2056 | } | |
2057 | } | |
2058 | ||
2059 | return 0; | |
2060 | } | |
2061 | ||
2062 | static void kvaser_usb_disconnect(struct usb_interface *intf) | |
2063 | { | |
2064 | struct kvaser_usb *dev = usb_get_intfdata(intf); | |
2065 | ||
2066 | usb_set_intfdata(intf, NULL); | |
2067 | ||
2068 | if (!dev) | |
2069 | return; | |
2070 | ||
2071 | kvaser_usb_remove_interfaces(dev); | |
2072 | } | |
2073 | ||
2074 | static struct usb_driver kvaser_usb_driver = { | |
2075 | .name = "kvaser_usb", | |
2076 | .probe = kvaser_usb_probe, | |
2077 | .disconnect = kvaser_usb_disconnect, | |
2078 | .id_table = kvaser_usb_table, | |
2079 | }; | |
2080 | ||
2081 | module_usb_driver(kvaser_usb_driver); | |
2082 | ||
2083 | MODULE_AUTHOR("Olivier Sobrie <olivier@sobrie.be>"); | |
2084 | MODULE_DESCRIPTION("CAN driver for Kvaser CAN/USB devices"); | |
2085 | MODULE_LICENSE("GPL v2"); |