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