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