.. | .. |
---|
20 | 20 | #include <linux/string.h> |
---|
21 | 21 | #include <linux/types.h> |
---|
22 | 22 | #include <linux/usb.h> |
---|
| 23 | +#include <linux/workqueue.h> |
---|
23 | 24 | |
---|
24 | 25 | #include <linux/can.h> |
---|
25 | 26 | #include <linux/can/dev.h> |
---|
.. | .. |
---|
55 | 56 | #define CMD_RX_EXT_MESSAGE 14 |
---|
56 | 57 | #define CMD_TX_EXT_MESSAGE 15 |
---|
57 | 58 | #define CMD_SET_BUS_PARAMS 16 |
---|
| 59 | +#define CMD_GET_BUS_PARAMS 17 |
---|
| 60 | +#define CMD_GET_BUS_PARAMS_REPLY 18 |
---|
| 61 | +#define CMD_GET_CHIP_STATE 19 |
---|
58 | 62 | #define CMD_CHIP_STATE_EVENT 20 |
---|
59 | 63 | #define CMD_SET_CTRL_MODE 21 |
---|
60 | 64 | #define CMD_RESET_CHIP 24 |
---|
.. | .. |
---|
69 | 73 | #define CMD_GET_CARD_INFO_REPLY 35 |
---|
70 | 74 | #define CMD_GET_SOFTWARE_INFO 38 |
---|
71 | 75 | #define CMD_GET_SOFTWARE_INFO_REPLY 39 |
---|
| 76 | +#define CMD_ERROR_EVENT 45 |
---|
72 | 77 | #define CMD_FLUSH_QUEUE 48 |
---|
73 | 78 | #define CMD_TX_ACKNOWLEDGE 50 |
---|
74 | 79 | #define CMD_CAN_ERROR_EVENT 51 |
---|
75 | 80 | #define CMD_FLUSH_QUEUE_REPLY 68 |
---|
| 81 | +#define CMD_GET_CAPABILITIES_REQ 95 |
---|
| 82 | +#define CMD_GET_CAPABILITIES_RESP 96 |
---|
76 | 83 | |
---|
77 | 84 | #define CMD_LEAF_LOG_MESSAGE 106 |
---|
78 | 85 | |
---|
.. | .. |
---|
81 | 88 | #define KVASER_USB_LEAF_SWOPTION_FREQ_16_MHZ_CLK 0 |
---|
82 | 89 | #define KVASER_USB_LEAF_SWOPTION_FREQ_32_MHZ_CLK BIT(5) |
---|
83 | 90 | #define KVASER_USB_LEAF_SWOPTION_FREQ_24_MHZ_CLK BIT(6) |
---|
| 91 | + |
---|
| 92 | +#define KVASER_USB_LEAF_SWOPTION_EXT_CAP BIT(12) |
---|
84 | 93 | |
---|
85 | 94 | /* error factors */ |
---|
86 | 95 | #define M16C_EF_ACKE BIT(0) |
---|
.. | .. |
---|
99 | 108 | #define USBCAN_ERROR_STATE_TX_ERROR BIT(0) |
---|
100 | 109 | #define USBCAN_ERROR_STATE_RX_ERROR BIT(1) |
---|
101 | 110 | #define USBCAN_ERROR_STATE_BUSERROR BIT(2) |
---|
102 | | - |
---|
103 | | -/* bittiming parameters */ |
---|
104 | | -#define KVASER_USB_TSEG1_MIN 1 |
---|
105 | | -#define KVASER_USB_TSEG1_MAX 16 |
---|
106 | | -#define KVASER_USB_TSEG2_MIN 1 |
---|
107 | | -#define KVASER_USB_TSEG2_MAX 8 |
---|
108 | | -#define KVASER_USB_SJW_MAX 4 |
---|
109 | | -#define KVASER_USB_BRP_MIN 1 |
---|
110 | | -#define KVASER_USB_BRP_MAX 64 |
---|
111 | | -#define KVASER_USB_BRP_INC 1 |
---|
112 | 111 | |
---|
113 | 112 | /* ctrl modes */ |
---|
114 | 113 | #define KVASER_CTRL_MODE_NORMAL 1 |
---|
.. | .. |
---|
166 | 165 | struct kvaser_cmd_busparams { |
---|
167 | 166 | u8 tid; |
---|
168 | 167 | u8 channel; |
---|
169 | | - __le32 bitrate; |
---|
170 | | - u8 tseg1; |
---|
171 | | - u8 tseg2; |
---|
172 | | - u8 sjw; |
---|
173 | | - u8 no_samp; |
---|
| 168 | + struct kvaser_usb_busparams busparams; |
---|
174 | 169 | } __packed; |
---|
175 | 170 | |
---|
176 | 171 | struct kvaser_cmd_tx_can { |
---|
.. | .. |
---|
239 | 234 | u8 tid; |
---|
240 | 235 | } __packed; |
---|
241 | 236 | |
---|
242 | | -struct leaf_cmd_error_event { |
---|
| 237 | +struct leaf_cmd_can_error_event { |
---|
243 | 238 | u8 tid; |
---|
244 | 239 | u8 flags; |
---|
245 | 240 | __le16 time[3]; |
---|
.. | .. |
---|
251 | 246 | u8 error_factor; |
---|
252 | 247 | } __packed; |
---|
253 | 248 | |
---|
254 | | -struct usbcan_cmd_error_event { |
---|
| 249 | +struct usbcan_cmd_can_error_event { |
---|
255 | 250 | u8 tid; |
---|
256 | 251 | u8 padding; |
---|
257 | 252 | u8 tx_errors_count_ch0; |
---|
.. | .. |
---|
261 | 256 | u8 status_ch0; |
---|
262 | 257 | u8 status_ch1; |
---|
263 | 258 | __le16 time; |
---|
| 259 | +} __packed; |
---|
| 260 | + |
---|
| 261 | +/* CMD_ERROR_EVENT error codes */ |
---|
| 262 | +#define KVASER_USB_LEAF_ERROR_EVENT_TX_QUEUE_FULL 0x8 |
---|
| 263 | +#define KVASER_USB_LEAF_ERROR_EVENT_PARAM 0x9 |
---|
| 264 | + |
---|
| 265 | +struct leaf_cmd_error_event { |
---|
| 266 | + u8 tid; |
---|
| 267 | + u8 error_code; |
---|
| 268 | + __le16 timestamp[3]; |
---|
| 269 | + __le16 padding; |
---|
| 270 | + __le16 info1; |
---|
| 271 | + __le16 info2; |
---|
| 272 | +} __packed; |
---|
| 273 | + |
---|
| 274 | +struct usbcan_cmd_error_event { |
---|
| 275 | + u8 tid; |
---|
| 276 | + u8 error_code; |
---|
| 277 | + __le16 info1; |
---|
| 278 | + __le16 info2; |
---|
| 279 | + __le16 timestamp; |
---|
| 280 | + __le16 padding; |
---|
264 | 281 | } __packed; |
---|
265 | 282 | |
---|
266 | 283 | struct kvaser_cmd_ctrl_mode { |
---|
.. | .. |
---|
287 | 304 | u8 data[8]; |
---|
288 | 305 | } __packed; |
---|
289 | 306 | |
---|
| 307 | +/* Sub commands for cap_req and cap_res */ |
---|
| 308 | +#define KVASER_USB_LEAF_CAP_CMD_LISTEN_MODE 0x02 |
---|
| 309 | +#define KVASER_USB_LEAF_CAP_CMD_ERR_REPORT 0x05 |
---|
| 310 | +struct kvaser_cmd_cap_req { |
---|
| 311 | + __le16 padding0; |
---|
| 312 | + __le16 cap_cmd; |
---|
| 313 | + __le16 padding1; |
---|
| 314 | + __le16 channel; |
---|
| 315 | +} __packed; |
---|
| 316 | + |
---|
| 317 | +/* Status codes for cap_res */ |
---|
| 318 | +#define KVASER_USB_LEAF_CAP_STAT_OK 0x00 |
---|
| 319 | +#define KVASER_USB_LEAF_CAP_STAT_NOT_IMPL 0x01 |
---|
| 320 | +#define KVASER_USB_LEAF_CAP_STAT_UNAVAIL 0x02 |
---|
| 321 | +struct kvaser_cmd_cap_res { |
---|
| 322 | + __le16 padding; |
---|
| 323 | + __le16 cap_cmd; |
---|
| 324 | + __le16 status; |
---|
| 325 | + __le32 mask; |
---|
| 326 | + __le32 value; |
---|
| 327 | +} __packed; |
---|
| 328 | + |
---|
290 | 329 | struct kvaser_cmd { |
---|
291 | 330 | u8 len; |
---|
292 | 331 | u8 id; |
---|
.. | .. |
---|
302 | 341 | struct leaf_cmd_softinfo softinfo; |
---|
303 | 342 | struct leaf_cmd_rx_can rx_can; |
---|
304 | 343 | struct leaf_cmd_chip_state_event chip_state_event; |
---|
305 | | - struct leaf_cmd_error_event error_event; |
---|
| 344 | + struct leaf_cmd_can_error_event can_error_event; |
---|
306 | 345 | struct leaf_cmd_log_message log_message; |
---|
| 346 | + struct leaf_cmd_error_event error_event; |
---|
| 347 | + struct kvaser_cmd_cap_req cap_req; |
---|
| 348 | + struct kvaser_cmd_cap_res cap_res; |
---|
307 | 349 | } __packed leaf; |
---|
308 | 350 | |
---|
309 | 351 | union { |
---|
310 | 352 | struct usbcan_cmd_softinfo softinfo; |
---|
311 | 353 | struct usbcan_cmd_rx_can rx_can; |
---|
312 | 354 | struct usbcan_cmd_chip_state_event chip_state_event; |
---|
| 355 | + struct usbcan_cmd_can_error_event can_error_event; |
---|
313 | 356 | struct usbcan_cmd_error_event error_event; |
---|
314 | 357 | } __packed usbcan; |
---|
315 | 358 | |
---|
.. | .. |
---|
318 | 361 | struct kvaser_cmd_flush_queue flush_queue; |
---|
319 | 362 | } u; |
---|
320 | 363 | } __packed; |
---|
| 364 | + |
---|
| 365 | +#define CMD_SIZE_ANY 0xff |
---|
| 366 | +#define kvaser_fsize(field) sizeof_field(struct kvaser_cmd, field) |
---|
| 367 | + |
---|
| 368 | +static const u8 kvaser_usb_leaf_cmd_sizes_leaf[] = { |
---|
| 369 | + [CMD_START_CHIP_REPLY] = kvaser_fsize(u.simple), |
---|
| 370 | + [CMD_STOP_CHIP_REPLY] = kvaser_fsize(u.simple), |
---|
| 371 | + [CMD_GET_CARD_INFO_REPLY] = kvaser_fsize(u.cardinfo), |
---|
| 372 | + [CMD_TX_ACKNOWLEDGE] = kvaser_fsize(u.tx_acknowledge_header), |
---|
| 373 | + [CMD_GET_SOFTWARE_INFO_REPLY] = kvaser_fsize(u.leaf.softinfo), |
---|
| 374 | + [CMD_RX_STD_MESSAGE] = kvaser_fsize(u.leaf.rx_can), |
---|
| 375 | + [CMD_RX_EXT_MESSAGE] = kvaser_fsize(u.leaf.rx_can), |
---|
| 376 | + [CMD_LEAF_LOG_MESSAGE] = kvaser_fsize(u.leaf.log_message), |
---|
| 377 | + [CMD_CHIP_STATE_EVENT] = kvaser_fsize(u.leaf.chip_state_event), |
---|
| 378 | + [CMD_CAN_ERROR_EVENT] = kvaser_fsize(u.leaf.can_error_event), |
---|
| 379 | + [CMD_GET_CAPABILITIES_RESP] = kvaser_fsize(u.leaf.cap_res), |
---|
| 380 | + [CMD_GET_BUS_PARAMS_REPLY] = kvaser_fsize(u.busparams), |
---|
| 381 | + [CMD_ERROR_EVENT] = kvaser_fsize(u.leaf.error_event), |
---|
| 382 | + /* ignored events: */ |
---|
| 383 | + [CMD_FLUSH_QUEUE_REPLY] = CMD_SIZE_ANY, |
---|
| 384 | +}; |
---|
| 385 | + |
---|
| 386 | +static const u8 kvaser_usb_leaf_cmd_sizes_usbcan[] = { |
---|
| 387 | + [CMD_START_CHIP_REPLY] = kvaser_fsize(u.simple), |
---|
| 388 | + [CMD_STOP_CHIP_REPLY] = kvaser_fsize(u.simple), |
---|
| 389 | + [CMD_GET_CARD_INFO_REPLY] = kvaser_fsize(u.cardinfo), |
---|
| 390 | + [CMD_TX_ACKNOWLEDGE] = kvaser_fsize(u.tx_acknowledge_header), |
---|
| 391 | + [CMD_GET_SOFTWARE_INFO_REPLY] = kvaser_fsize(u.usbcan.softinfo), |
---|
| 392 | + [CMD_RX_STD_MESSAGE] = kvaser_fsize(u.usbcan.rx_can), |
---|
| 393 | + [CMD_RX_EXT_MESSAGE] = kvaser_fsize(u.usbcan.rx_can), |
---|
| 394 | + [CMD_CHIP_STATE_EVENT] = kvaser_fsize(u.usbcan.chip_state_event), |
---|
| 395 | + [CMD_CAN_ERROR_EVENT] = kvaser_fsize(u.usbcan.can_error_event), |
---|
| 396 | + [CMD_ERROR_EVENT] = kvaser_fsize(u.usbcan.error_event), |
---|
| 397 | + /* ignored events: */ |
---|
| 398 | + [CMD_USBCAN_CLOCK_OVERFLOW_EVENT] = CMD_SIZE_ANY, |
---|
| 399 | +}; |
---|
321 | 400 | |
---|
322 | 401 | /* Summary of a kvaser error event, for a unified Leaf/Usbcan error |
---|
323 | 402 | * handling. Some discrepancies between the two families exist: |
---|
.. | .. |
---|
342 | 421 | }; |
---|
343 | 422 | }; |
---|
344 | 423 | |
---|
345 | | -static const struct can_bittiming_const kvaser_usb_leaf_bittiming_const = { |
---|
346 | | - .name = "kvaser_usb", |
---|
347 | | - .tseg1_min = KVASER_USB_TSEG1_MIN, |
---|
348 | | - .tseg1_max = KVASER_USB_TSEG1_MAX, |
---|
349 | | - .tseg2_min = KVASER_USB_TSEG2_MIN, |
---|
350 | | - .tseg2_max = KVASER_USB_TSEG2_MAX, |
---|
351 | | - .sjw_max = KVASER_USB_SJW_MAX, |
---|
352 | | - .brp_min = KVASER_USB_BRP_MIN, |
---|
353 | | - .brp_max = KVASER_USB_BRP_MAX, |
---|
354 | | - .brp_inc = KVASER_USB_BRP_INC, |
---|
| 424 | +struct kvaser_usb_net_leaf_priv { |
---|
| 425 | + struct kvaser_usb_net_priv *net; |
---|
| 426 | + |
---|
| 427 | + struct delayed_work chip_state_req_work; |
---|
355 | 428 | }; |
---|
356 | 429 | |
---|
357 | | -static const struct kvaser_usb_dev_cfg kvaser_usb_leaf_dev_cfg_8mhz = { |
---|
| 430 | +static const struct can_bittiming_const kvaser_usb_leaf_m16c_bittiming_const = { |
---|
| 431 | + .name = "kvaser_usb_ucii", |
---|
| 432 | + .tseg1_min = 4, |
---|
| 433 | + .tseg1_max = 16, |
---|
| 434 | + .tseg2_min = 2, |
---|
| 435 | + .tseg2_max = 8, |
---|
| 436 | + .sjw_max = 4, |
---|
| 437 | + .brp_min = 1, |
---|
| 438 | + .brp_max = 16, |
---|
| 439 | + .brp_inc = 1, |
---|
| 440 | +}; |
---|
| 441 | + |
---|
| 442 | +static const struct can_bittiming_const kvaser_usb_leaf_m32c_bittiming_const = { |
---|
| 443 | + .name = "kvaser_usb_leaf", |
---|
| 444 | + .tseg1_min = 3, |
---|
| 445 | + .tseg1_max = 16, |
---|
| 446 | + .tseg2_min = 2, |
---|
| 447 | + .tseg2_max = 8, |
---|
| 448 | + .sjw_max = 4, |
---|
| 449 | + .brp_min = 2, |
---|
| 450 | + .brp_max = 128, |
---|
| 451 | + .brp_inc = 2, |
---|
| 452 | +}; |
---|
| 453 | + |
---|
| 454 | +static const struct kvaser_usb_dev_cfg kvaser_usb_leaf_usbcan_dev_cfg = { |
---|
358 | 455 | .clock = { |
---|
359 | 456 | .freq = 8000000, |
---|
360 | 457 | }, |
---|
361 | 458 | .timestamp_freq = 1, |
---|
362 | | - .bittiming_const = &kvaser_usb_leaf_bittiming_const, |
---|
| 459 | + .bittiming_const = &kvaser_usb_leaf_m16c_bittiming_const, |
---|
363 | 460 | }; |
---|
364 | 461 | |
---|
365 | | -static const struct kvaser_usb_dev_cfg kvaser_usb_leaf_dev_cfg_16mhz = { |
---|
| 462 | +static const struct kvaser_usb_dev_cfg kvaser_usb_leaf_m32c_dev_cfg = { |
---|
366 | 463 | .clock = { |
---|
367 | 464 | .freq = 16000000, |
---|
368 | 465 | }, |
---|
369 | 466 | .timestamp_freq = 1, |
---|
370 | | - .bittiming_const = &kvaser_usb_leaf_bittiming_const, |
---|
| 467 | + .bittiming_const = &kvaser_usb_leaf_m32c_bittiming_const, |
---|
371 | 468 | }; |
---|
372 | 469 | |
---|
373 | | -static const struct kvaser_usb_dev_cfg kvaser_usb_leaf_dev_cfg_24mhz = { |
---|
| 470 | +static const struct kvaser_usb_dev_cfg kvaser_usb_leaf_imx_dev_cfg_16mhz = { |
---|
| 471 | + .clock = { |
---|
| 472 | + .freq = 16000000, |
---|
| 473 | + }, |
---|
| 474 | + .timestamp_freq = 1, |
---|
| 475 | + .bittiming_const = &kvaser_usb_flexc_bittiming_const, |
---|
| 476 | +}; |
---|
| 477 | + |
---|
| 478 | +static const struct kvaser_usb_dev_cfg kvaser_usb_leaf_imx_dev_cfg_24mhz = { |
---|
374 | 479 | .clock = { |
---|
375 | 480 | .freq = 24000000, |
---|
376 | 481 | }, |
---|
377 | 482 | .timestamp_freq = 1, |
---|
378 | | - .bittiming_const = &kvaser_usb_leaf_bittiming_const, |
---|
| 483 | + .bittiming_const = &kvaser_usb_flexc_bittiming_const, |
---|
379 | 484 | }; |
---|
380 | 485 | |
---|
381 | | -static const struct kvaser_usb_dev_cfg kvaser_usb_leaf_dev_cfg_32mhz = { |
---|
| 486 | +static const struct kvaser_usb_dev_cfg kvaser_usb_leaf_imx_dev_cfg_32mhz = { |
---|
382 | 487 | .clock = { |
---|
383 | 488 | .freq = 32000000, |
---|
384 | 489 | }, |
---|
385 | 490 | .timestamp_freq = 1, |
---|
386 | | - .bittiming_const = &kvaser_usb_leaf_bittiming_const, |
---|
| 491 | + .bittiming_const = &kvaser_usb_flexc_bittiming_const, |
---|
387 | 492 | }; |
---|
| 493 | + |
---|
| 494 | +static int kvaser_usb_leaf_verify_size(const struct kvaser_usb *dev, |
---|
| 495 | + const struct kvaser_cmd *cmd) |
---|
| 496 | +{ |
---|
| 497 | + /* buffer size >= cmd->len ensured by caller */ |
---|
| 498 | + u8 min_size = 0; |
---|
| 499 | + |
---|
| 500 | + switch (dev->driver_info->family) { |
---|
| 501 | + case KVASER_LEAF: |
---|
| 502 | + if (cmd->id < ARRAY_SIZE(kvaser_usb_leaf_cmd_sizes_leaf)) |
---|
| 503 | + min_size = kvaser_usb_leaf_cmd_sizes_leaf[cmd->id]; |
---|
| 504 | + break; |
---|
| 505 | + case KVASER_USBCAN: |
---|
| 506 | + if (cmd->id < ARRAY_SIZE(kvaser_usb_leaf_cmd_sizes_usbcan)) |
---|
| 507 | + min_size = kvaser_usb_leaf_cmd_sizes_usbcan[cmd->id]; |
---|
| 508 | + break; |
---|
| 509 | + } |
---|
| 510 | + |
---|
| 511 | + if (min_size == CMD_SIZE_ANY) |
---|
| 512 | + return 0; |
---|
| 513 | + |
---|
| 514 | + if (min_size) { |
---|
| 515 | + min_size += CMD_HEADER_LEN; |
---|
| 516 | + if (cmd->len >= min_size) |
---|
| 517 | + return 0; |
---|
| 518 | + |
---|
| 519 | + dev_err_ratelimited(&dev->intf->dev, |
---|
| 520 | + "Received command %u too short (size %u, needed %u)", |
---|
| 521 | + cmd->id, cmd->len, min_size); |
---|
| 522 | + return -EIO; |
---|
| 523 | + } |
---|
| 524 | + |
---|
| 525 | + dev_warn_ratelimited(&dev->intf->dev, |
---|
| 526 | + "Unhandled command (%d, size %d)\n", |
---|
| 527 | + cmd->id, cmd->len); |
---|
| 528 | + return -EINVAL; |
---|
| 529 | +} |
---|
388 | 530 | |
---|
389 | 531 | static void * |
---|
390 | 532 | kvaser_usb_leaf_frame_to_cmd(const struct kvaser_usb_net_priv *priv, |
---|
.. | .. |
---|
405 | 547 | sizeof(struct kvaser_cmd_tx_can); |
---|
406 | 548 | cmd->u.tx_can.channel = priv->channel; |
---|
407 | 549 | |
---|
408 | | - switch (dev->card_data.leaf.family) { |
---|
| 550 | + switch (dev->driver_info->family) { |
---|
409 | 551 | case KVASER_LEAF: |
---|
410 | 552 | cmd_tx_can_flags = &cmd->u.tx_can.leaf.flags; |
---|
411 | 553 | break; |
---|
.. | .. |
---|
493 | 635 | end: |
---|
494 | 636 | kfree(buf); |
---|
495 | 637 | |
---|
| 638 | + if (err == 0) |
---|
| 639 | + err = kvaser_usb_leaf_verify_size(dev, cmd); |
---|
| 640 | + |
---|
496 | 641 | return err; |
---|
497 | 642 | } |
---|
498 | 643 | |
---|
.. | .. |
---|
525 | 670 | dev->fw_version = le32_to_cpu(softinfo->fw_version); |
---|
526 | 671 | dev->max_tx_urbs = le16_to_cpu(softinfo->max_outstanding_tx); |
---|
527 | 672 | |
---|
528 | | - switch (sw_options & KVASER_USB_LEAF_SWOPTION_FREQ_MASK) { |
---|
529 | | - case KVASER_USB_LEAF_SWOPTION_FREQ_16_MHZ_CLK: |
---|
530 | | - dev->cfg = &kvaser_usb_leaf_dev_cfg_16mhz; |
---|
531 | | - break; |
---|
532 | | - case KVASER_USB_LEAF_SWOPTION_FREQ_24_MHZ_CLK: |
---|
533 | | - dev->cfg = &kvaser_usb_leaf_dev_cfg_24mhz; |
---|
534 | | - break; |
---|
535 | | - case KVASER_USB_LEAF_SWOPTION_FREQ_32_MHZ_CLK: |
---|
536 | | - dev->cfg = &kvaser_usb_leaf_dev_cfg_32mhz; |
---|
537 | | - break; |
---|
| 673 | + if (sw_options & KVASER_USB_LEAF_SWOPTION_EXT_CAP) |
---|
| 674 | + dev->card_data.capabilities |= KVASER_USB_CAP_EXT_CAP; |
---|
| 675 | + |
---|
| 676 | + if (dev->driver_info->quirks & KVASER_USB_QUIRK_IGNORE_CLK_FREQ) { |
---|
| 677 | + /* Firmware expects bittiming parameters calculated for 16MHz |
---|
| 678 | + * clock, regardless of the actual clock |
---|
| 679 | + */ |
---|
| 680 | + dev->cfg = &kvaser_usb_leaf_m32c_dev_cfg; |
---|
| 681 | + } else { |
---|
| 682 | + switch (sw_options & KVASER_USB_LEAF_SWOPTION_FREQ_MASK) { |
---|
| 683 | + case KVASER_USB_LEAF_SWOPTION_FREQ_16_MHZ_CLK: |
---|
| 684 | + dev->cfg = &kvaser_usb_leaf_imx_dev_cfg_16mhz; |
---|
| 685 | + break; |
---|
| 686 | + case KVASER_USB_LEAF_SWOPTION_FREQ_24_MHZ_CLK: |
---|
| 687 | + dev->cfg = &kvaser_usb_leaf_imx_dev_cfg_24mhz; |
---|
| 688 | + break; |
---|
| 689 | + case KVASER_USB_LEAF_SWOPTION_FREQ_32_MHZ_CLK: |
---|
| 690 | + dev->cfg = &kvaser_usb_leaf_imx_dev_cfg_32mhz; |
---|
| 691 | + break; |
---|
| 692 | + } |
---|
538 | 693 | } |
---|
539 | 694 | } |
---|
540 | 695 | |
---|
.. | .. |
---|
551 | 706 | if (err) |
---|
552 | 707 | return err; |
---|
553 | 708 | |
---|
554 | | - switch (dev->card_data.leaf.family) { |
---|
| 709 | + switch (dev->driver_info->family) { |
---|
555 | 710 | case KVASER_LEAF: |
---|
556 | 711 | kvaser_usb_leaf_get_software_info_leaf(dev, &cmd.u.leaf.softinfo); |
---|
557 | 712 | break; |
---|
.. | .. |
---|
559 | 714 | dev->fw_version = le32_to_cpu(cmd.u.usbcan.softinfo.fw_version); |
---|
560 | 715 | dev->max_tx_urbs = |
---|
561 | 716 | le16_to_cpu(cmd.u.usbcan.softinfo.max_outstanding_tx); |
---|
562 | | - dev->cfg = &kvaser_usb_leaf_dev_cfg_8mhz; |
---|
| 717 | + dev->cfg = &kvaser_usb_leaf_usbcan_dev_cfg; |
---|
563 | 718 | break; |
---|
564 | 719 | } |
---|
565 | 720 | |
---|
.. | .. |
---|
598 | 753 | |
---|
599 | 754 | dev->nchannels = cmd.u.cardinfo.nchannels; |
---|
600 | 755 | if (dev->nchannels > KVASER_USB_MAX_NET_DEVICES || |
---|
601 | | - (dev->card_data.leaf.family == KVASER_USBCAN && |
---|
| 756 | + (dev->driver_info->family == KVASER_USBCAN && |
---|
602 | 757 | dev->nchannels > MAX_USBCAN_NET_DEVICES)) |
---|
603 | 758 | return -EINVAL; |
---|
604 | 759 | |
---|
605 | 760 | return 0; |
---|
| 761 | +} |
---|
| 762 | + |
---|
| 763 | +static int kvaser_usb_leaf_get_single_capability(struct kvaser_usb *dev, |
---|
| 764 | + u16 cap_cmd_req, u16 *status) |
---|
| 765 | +{ |
---|
| 766 | + struct kvaser_usb_dev_card_data *card_data = &dev->card_data; |
---|
| 767 | + struct kvaser_cmd *cmd; |
---|
| 768 | + u32 value = 0; |
---|
| 769 | + u32 mask = 0; |
---|
| 770 | + u16 cap_cmd_res; |
---|
| 771 | + int err; |
---|
| 772 | + int i; |
---|
| 773 | + |
---|
| 774 | + cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); |
---|
| 775 | + if (!cmd) |
---|
| 776 | + return -ENOMEM; |
---|
| 777 | + |
---|
| 778 | + cmd->id = CMD_GET_CAPABILITIES_REQ; |
---|
| 779 | + cmd->u.leaf.cap_req.cap_cmd = cpu_to_le16(cap_cmd_req); |
---|
| 780 | + cmd->len = CMD_HEADER_LEN + sizeof(struct kvaser_cmd_cap_req); |
---|
| 781 | + |
---|
| 782 | + err = kvaser_usb_send_cmd(dev, cmd, cmd->len); |
---|
| 783 | + if (err) |
---|
| 784 | + goto end; |
---|
| 785 | + |
---|
| 786 | + err = kvaser_usb_leaf_wait_cmd(dev, CMD_GET_CAPABILITIES_RESP, cmd); |
---|
| 787 | + if (err) |
---|
| 788 | + goto end; |
---|
| 789 | + |
---|
| 790 | + *status = le16_to_cpu(cmd->u.leaf.cap_res.status); |
---|
| 791 | + |
---|
| 792 | + if (*status != KVASER_USB_LEAF_CAP_STAT_OK) |
---|
| 793 | + goto end; |
---|
| 794 | + |
---|
| 795 | + cap_cmd_res = le16_to_cpu(cmd->u.leaf.cap_res.cap_cmd); |
---|
| 796 | + switch (cap_cmd_res) { |
---|
| 797 | + case KVASER_USB_LEAF_CAP_CMD_LISTEN_MODE: |
---|
| 798 | + case KVASER_USB_LEAF_CAP_CMD_ERR_REPORT: |
---|
| 799 | + value = le32_to_cpu(cmd->u.leaf.cap_res.value); |
---|
| 800 | + mask = le32_to_cpu(cmd->u.leaf.cap_res.mask); |
---|
| 801 | + break; |
---|
| 802 | + default: |
---|
| 803 | + dev_warn(&dev->intf->dev, "Unknown capability command %u\n", |
---|
| 804 | + cap_cmd_res); |
---|
| 805 | + break; |
---|
| 806 | + } |
---|
| 807 | + |
---|
| 808 | + for (i = 0; i < dev->nchannels; i++) { |
---|
| 809 | + if (BIT(i) & (value & mask)) { |
---|
| 810 | + switch (cap_cmd_res) { |
---|
| 811 | + case KVASER_USB_LEAF_CAP_CMD_LISTEN_MODE: |
---|
| 812 | + card_data->ctrlmode_supported |= |
---|
| 813 | + CAN_CTRLMODE_LISTENONLY; |
---|
| 814 | + break; |
---|
| 815 | + case KVASER_USB_LEAF_CAP_CMD_ERR_REPORT: |
---|
| 816 | + card_data->capabilities |= |
---|
| 817 | + KVASER_USB_CAP_BERR_CAP; |
---|
| 818 | + break; |
---|
| 819 | + } |
---|
| 820 | + } |
---|
| 821 | + } |
---|
| 822 | + |
---|
| 823 | +end: |
---|
| 824 | + kfree(cmd); |
---|
| 825 | + |
---|
| 826 | + return err; |
---|
| 827 | +} |
---|
| 828 | + |
---|
| 829 | +static int kvaser_usb_leaf_get_capabilities_leaf(struct kvaser_usb *dev) |
---|
| 830 | +{ |
---|
| 831 | + int err; |
---|
| 832 | + u16 status; |
---|
| 833 | + |
---|
| 834 | + if (!(dev->card_data.capabilities & KVASER_USB_CAP_EXT_CAP)) { |
---|
| 835 | + dev_info(&dev->intf->dev, |
---|
| 836 | + "No extended capability support. Upgrade device firmware.\n"); |
---|
| 837 | + return 0; |
---|
| 838 | + } |
---|
| 839 | + |
---|
| 840 | + err = kvaser_usb_leaf_get_single_capability(dev, |
---|
| 841 | + KVASER_USB_LEAF_CAP_CMD_LISTEN_MODE, |
---|
| 842 | + &status); |
---|
| 843 | + if (err) |
---|
| 844 | + return err; |
---|
| 845 | + if (status) |
---|
| 846 | + dev_info(&dev->intf->dev, |
---|
| 847 | + "KVASER_USB_LEAF_CAP_CMD_LISTEN_MODE failed %u\n", |
---|
| 848 | + status); |
---|
| 849 | + |
---|
| 850 | + err = kvaser_usb_leaf_get_single_capability(dev, |
---|
| 851 | + KVASER_USB_LEAF_CAP_CMD_ERR_REPORT, |
---|
| 852 | + &status); |
---|
| 853 | + if (err) |
---|
| 854 | + return err; |
---|
| 855 | + if (status) |
---|
| 856 | + dev_info(&dev->intf->dev, |
---|
| 857 | + "KVASER_USB_LEAF_CAP_CMD_ERR_REPORT failed %u\n", |
---|
| 858 | + status); |
---|
| 859 | + |
---|
| 860 | + return 0; |
---|
| 861 | +} |
---|
| 862 | + |
---|
| 863 | +static int kvaser_usb_leaf_get_capabilities(struct kvaser_usb *dev) |
---|
| 864 | +{ |
---|
| 865 | + int err = 0; |
---|
| 866 | + |
---|
| 867 | + if (dev->driver_info->family == KVASER_LEAF) |
---|
| 868 | + err = kvaser_usb_leaf_get_capabilities_leaf(dev); |
---|
| 869 | + |
---|
| 870 | + return err; |
---|
606 | 871 | } |
---|
607 | 872 | |
---|
608 | 873 | static void kvaser_usb_leaf_tx_acknowledge(const struct kvaser_usb *dev, |
---|
.. | .. |
---|
633 | 898 | context = &priv->tx_contexts[tid % dev->max_tx_urbs]; |
---|
634 | 899 | |
---|
635 | 900 | /* Sometimes the state change doesn't come after a bus-off event */ |
---|
636 | | - if (priv->can.restart_ms && priv->can.state >= CAN_STATE_BUS_OFF) { |
---|
| 901 | + if (priv->can.restart_ms && priv->can.state == CAN_STATE_BUS_OFF) { |
---|
637 | 902 | struct sk_buff *skb; |
---|
638 | 903 | struct can_frame *cf; |
---|
639 | 904 | |
---|
.. | .. |
---|
689 | 954 | return err; |
---|
690 | 955 | } |
---|
691 | 956 | |
---|
| 957 | +static void kvaser_usb_leaf_chip_state_req_work(struct work_struct *work) |
---|
| 958 | +{ |
---|
| 959 | + struct kvaser_usb_net_leaf_priv *leaf = |
---|
| 960 | + container_of(work, struct kvaser_usb_net_leaf_priv, |
---|
| 961 | + chip_state_req_work.work); |
---|
| 962 | + struct kvaser_usb_net_priv *priv = leaf->net; |
---|
| 963 | + |
---|
| 964 | + kvaser_usb_leaf_simple_cmd_async(priv, CMD_GET_CHIP_STATE); |
---|
| 965 | +} |
---|
| 966 | + |
---|
692 | 967 | static void |
---|
693 | 968 | kvaser_usb_leaf_rx_error_update_can_state(struct kvaser_usb_net_priv *priv, |
---|
694 | 969 | const struct kvaser_usb_err_summary *es, |
---|
.. | .. |
---|
707 | 982 | new_state = CAN_STATE_BUS_OFF; |
---|
708 | 983 | } else if (es->status & M16C_STATE_BUS_PASSIVE) { |
---|
709 | 984 | new_state = CAN_STATE_ERROR_PASSIVE; |
---|
710 | | - } else if (es->status & M16C_STATE_BUS_ERROR) { |
---|
| 985 | + } else if ((es->status & M16C_STATE_BUS_ERROR) && |
---|
| 986 | + cur_state >= CAN_STATE_BUS_OFF) { |
---|
711 | 987 | /* Guard against spurious error events after a busoff */ |
---|
712 | | - if (cur_state < CAN_STATE_BUS_OFF) { |
---|
713 | | - if (es->txerr >= 128 || es->rxerr >= 128) |
---|
714 | | - new_state = CAN_STATE_ERROR_PASSIVE; |
---|
715 | | - else if (es->txerr >= 96 || es->rxerr >= 96) |
---|
716 | | - new_state = CAN_STATE_ERROR_WARNING; |
---|
717 | | - else if (cur_state > CAN_STATE_ERROR_ACTIVE) |
---|
718 | | - new_state = CAN_STATE_ERROR_ACTIVE; |
---|
719 | | - } |
---|
720 | | - } |
---|
721 | | - |
---|
722 | | - if (!es->status) |
---|
| 988 | + } else if (es->txerr >= 128 || es->rxerr >= 128) { |
---|
| 989 | + new_state = CAN_STATE_ERROR_PASSIVE; |
---|
| 990 | + } else if (es->txerr >= 96 || es->rxerr >= 96) { |
---|
| 991 | + new_state = CAN_STATE_ERROR_WARNING; |
---|
| 992 | + } else { |
---|
723 | 993 | new_state = CAN_STATE_ERROR_ACTIVE; |
---|
| 994 | + } |
---|
724 | 995 | |
---|
725 | 996 | if (new_state != cur_state) { |
---|
726 | 997 | tx_state = (es->txerr >= es->rxerr) ? new_state : 0; |
---|
.. | .. |
---|
730 | 1001 | } |
---|
731 | 1002 | |
---|
732 | 1003 | if (priv->can.restart_ms && |
---|
733 | | - cur_state >= CAN_STATE_BUS_OFF && |
---|
| 1004 | + cur_state == CAN_STATE_BUS_OFF && |
---|
734 | 1005 | new_state < CAN_STATE_BUS_OFF) |
---|
735 | 1006 | priv->can.can_stats.restarts++; |
---|
736 | 1007 | |
---|
737 | | - switch (dev->card_data.leaf.family) { |
---|
| 1008 | + switch (dev->driver_info->family) { |
---|
738 | 1009 | case KVASER_LEAF: |
---|
739 | 1010 | if (es->leaf.error_factor) { |
---|
740 | 1011 | priv->can.can_stats.bus_error++; |
---|
.. | .. |
---|
764 | 1035 | struct sk_buff *skb; |
---|
765 | 1036 | struct net_device_stats *stats; |
---|
766 | 1037 | struct kvaser_usb_net_priv *priv; |
---|
| 1038 | + struct kvaser_usb_net_leaf_priv *leaf; |
---|
767 | 1039 | enum can_state old_state, new_state; |
---|
768 | 1040 | |
---|
769 | 1041 | if (es->channel >= dev->nchannels) { |
---|
.. | .. |
---|
773 | 1045 | } |
---|
774 | 1046 | |
---|
775 | 1047 | priv = dev->nets[es->channel]; |
---|
| 1048 | + leaf = priv->sub_priv; |
---|
776 | 1049 | stats = &priv->netdev->stats; |
---|
| 1050 | + |
---|
| 1051 | + /* Ignore e.g. state change to bus-off reported just after stopping */ |
---|
| 1052 | + if (!netif_running(priv->netdev)) |
---|
| 1053 | + return; |
---|
777 | 1054 | |
---|
778 | 1055 | /* Update all of the CAN interface's state and error counters before |
---|
779 | 1056 | * trying any memory allocation that can actually fail with -ENOMEM. |
---|
.. | .. |
---|
788 | 1065 | old_state = priv->can.state; |
---|
789 | 1066 | kvaser_usb_leaf_rx_error_update_can_state(priv, es, &tmp_cf); |
---|
790 | 1067 | new_state = priv->can.state; |
---|
| 1068 | + |
---|
| 1069 | + /* If there are errors, request status updates periodically as we do |
---|
| 1070 | + * not get automatic notifications of improved state. |
---|
| 1071 | + */ |
---|
| 1072 | + if (new_state < CAN_STATE_BUS_OFF && |
---|
| 1073 | + (es->rxerr || es->txerr || new_state == CAN_STATE_ERROR_PASSIVE)) |
---|
| 1074 | + schedule_delayed_work(&leaf->chip_state_req_work, |
---|
| 1075 | + msecs_to_jiffies(500)); |
---|
791 | 1076 | |
---|
792 | 1077 | skb = alloc_can_err_skb(priv->netdev, &cf); |
---|
793 | 1078 | if (!skb) { |
---|
.. | .. |
---|
806 | 1091 | } |
---|
807 | 1092 | |
---|
808 | 1093 | if (priv->can.restart_ms && |
---|
809 | | - old_state >= CAN_STATE_BUS_OFF && |
---|
| 1094 | + old_state == CAN_STATE_BUS_OFF && |
---|
810 | 1095 | new_state < CAN_STATE_BUS_OFF) { |
---|
811 | 1096 | cf->can_id |= CAN_ERR_RESTARTED; |
---|
812 | 1097 | netif_carrier_on(priv->netdev); |
---|
813 | 1098 | } |
---|
814 | 1099 | } |
---|
815 | 1100 | |
---|
816 | | - switch (dev->card_data.leaf.family) { |
---|
| 1101 | + switch (dev->driver_info->family) { |
---|
817 | 1102 | case KVASER_LEAF: |
---|
818 | 1103 | if (es->leaf.error_factor) { |
---|
819 | 1104 | cf->can_id |= CAN_ERR_BUSERROR | CAN_ERR_PROT; |
---|
.. | .. |
---|
840 | 1125 | break; |
---|
841 | 1126 | } |
---|
842 | 1127 | |
---|
843 | | - cf->data[6] = es->txerr; |
---|
844 | | - cf->data[7] = es->rxerr; |
---|
| 1128 | + if (new_state != CAN_STATE_BUS_OFF) { |
---|
| 1129 | + cf->data[6] = es->txerr; |
---|
| 1130 | + cf->data[7] = es->rxerr; |
---|
| 1131 | + } |
---|
845 | 1132 | |
---|
846 | 1133 | stats->rx_packets++; |
---|
847 | 1134 | stats->rx_bytes += cf->can_dlc; |
---|
.. | .. |
---|
904 | 1191 | |
---|
905 | 1192 | case CMD_CAN_ERROR_EVENT: |
---|
906 | 1193 | es.channel = 0; |
---|
907 | | - es.status = cmd->u.usbcan.error_event.status_ch0; |
---|
908 | | - es.txerr = cmd->u.usbcan.error_event.tx_errors_count_ch0; |
---|
909 | | - es.rxerr = cmd->u.usbcan.error_event.rx_errors_count_ch0; |
---|
| 1194 | + es.status = cmd->u.usbcan.can_error_event.status_ch0; |
---|
| 1195 | + es.txerr = cmd->u.usbcan.can_error_event.tx_errors_count_ch0; |
---|
| 1196 | + es.rxerr = cmd->u.usbcan.can_error_event.rx_errors_count_ch0; |
---|
910 | 1197 | es.usbcan.other_ch_status = |
---|
911 | | - cmd->u.usbcan.error_event.status_ch1; |
---|
| 1198 | + cmd->u.usbcan.can_error_event.status_ch1; |
---|
912 | 1199 | kvaser_usb_leaf_usbcan_conditionally_rx_error(dev, &es); |
---|
913 | 1200 | |
---|
914 | 1201 | /* The USBCAN firmware supports up to 2 channels. |
---|
.. | .. |
---|
916 | 1203 | */ |
---|
917 | 1204 | if (dev->nchannels == MAX_USBCAN_NET_DEVICES) { |
---|
918 | 1205 | es.channel = 1; |
---|
919 | | - es.status = cmd->u.usbcan.error_event.status_ch1; |
---|
| 1206 | + es.status = cmd->u.usbcan.can_error_event.status_ch1; |
---|
920 | 1207 | es.txerr = |
---|
921 | | - cmd->u.usbcan.error_event.tx_errors_count_ch1; |
---|
| 1208 | + cmd->u.usbcan.can_error_event.tx_errors_count_ch1; |
---|
922 | 1209 | es.rxerr = |
---|
923 | | - cmd->u.usbcan.error_event.rx_errors_count_ch1; |
---|
| 1210 | + cmd->u.usbcan.can_error_event.rx_errors_count_ch1; |
---|
924 | 1211 | es.usbcan.other_ch_status = |
---|
925 | | - cmd->u.usbcan.error_event.status_ch0; |
---|
| 1212 | + cmd->u.usbcan.can_error_event.status_ch0; |
---|
926 | 1213 | kvaser_usb_leaf_usbcan_conditionally_rx_error(dev, &es); |
---|
927 | 1214 | } |
---|
928 | 1215 | break; |
---|
.. | .. |
---|
939 | 1226 | |
---|
940 | 1227 | switch (cmd->id) { |
---|
941 | 1228 | case CMD_CAN_ERROR_EVENT: |
---|
942 | | - es.channel = cmd->u.leaf.error_event.channel; |
---|
943 | | - es.status = cmd->u.leaf.error_event.status; |
---|
944 | | - es.txerr = cmd->u.leaf.error_event.tx_errors_count; |
---|
945 | | - es.rxerr = cmd->u.leaf.error_event.rx_errors_count; |
---|
946 | | - es.leaf.error_factor = cmd->u.leaf.error_event.error_factor; |
---|
| 1229 | + es.channel = cmd->u.leaf.can_error_event.channel; |
---|
| 1230 | + es.status = cmd->u.leaf.can_error_event.status; |
---|
| 1231 | + es.txerr = cmd->u.leaf.can_error_event.tx_errors_count; |
---|
| 1232 | + es.rxerr = cmd->u.leaf.can_error_event.rx_errors_count; |
---|
| 1233 | + es.leaf.error_factor = cmd->u.leaf.can_error_event.error_factor; |
---|
947 | 1234 | break; |
---|
948 | 1235 | case CMD_LEAF_LOG_MESSAGE: |
---|
949 | 1236 | es.channel = cmd->u.leaf.log_message.channel; |
---|
.. | .. |
---|
1005 | 1292 | stats = &priv->netdev->stats; |
---|
1006 | 1293 | |
---|
1007 | 1294 | if ((cmd->u.rx_can_header.flag & MSG_FLAG_ERROR_FRAME) && |
---|
1008 | | - (dev->card_data.leaf.family == KVASER_LEAF && |
---|
| 1295 | + (dev->driver_info->family == KVASER_LEAF && |
---|
1009 | 1296 | cmd->id == CMD_LEAF_LOG_MESSAGE)) { |
---|
1010 | 1297 | kvaser_usb_leaf_leaf_rx_error(dev, cmd); |
---|
1011 | 1298 | return; |
---|
.. | .. |
---|
1021 | 1308 | return; |
---|
1022 | 1309 | } |
---|
1023 | 1310 | |
---|
1024 | | - switch (dev->card_data.leaf.family) { |
---|
| 1311 | + switch (dev->driver_info->family) { |
---|
1025 | 1312 | case KVASER_LEAF: |
---|
1026 | 1313 | rx_data = cmd->u.leaf.rx_can.data; |
---|
1027 | 1314 | break; |
---|
.. | .. |
---|
1036 | 1323 | return; |
---|
1037 | 1324 | } |
---|
1038 | 1325 | |
---|
1039 | | - if (dev->card_data.leaf.family == KVASER_LEAF && cmd->id == |
---|
| 1326 | + if (dev->driver_info->family == KVASER_LEAF && cmd->id == |
---|
1040 | 1327 | CMD_LEAF_LOG_MESSAGE) { |
---|
1041 | 1328 | cf->can_id = le32_to_cpu(cmd->u.leaf.log_message.id); |
---|
1042 | 1329 | if (cf->can_id & KVASER_EXTENDED_FRAME) |
---|
.. | .. |
---|
1073 | 1360 | stats->rx_packets++; |
---|
1074 | 1361 | stats->rx_bytes += cf->can_dlc; |
---|
1075 | 1362 | netif_rx(skb); |
---|
| 1363 | +} |
---|
| 1364 | + |
---|
| 1365 | +static void kvaser_usb_leaf_error_event_parameter(const struct kvaser_usb *dev, |
---|
| 1366 | + const struct kvaser_cmd *cmd) |
---|
| 1367 | +{ |
---|
| 1368 | + u16 info1 = 0; |
---|
| 1369 | + |
---|
| 1370 | + switch (dev->driver_info->family) { |
---|
| 1371 | + case KVASER_LEAF: |
---|
| 1372 | + info1 = le16_to_cpu(cmd->u.leaf.error_event.info1); |
---|
| 1373 | + break; |
---|
| 1374 | + case KVASER_USBCAN: |
---|
| 1375 | + info1 = le16_to_cpu(cmd->u.usbcan.error_event.info1); |
---|
| 1376 | + break; |
---|
| 1377 | + } |
---|
| 1378 | + |
---|
| 1379 | + /* info1 will contain the offending cmd_no */ |
---|
| 1380 | + switch (info1) { |
---|
| 1381 | + case CMD_SET_CTRL_MODE: |
---|
| 1382 | + dev_warn(&dev->intf->dev, |
---|
| 1383 | + "CMD_SET_CTRL_MODE error in parameter\n"); |
---|
| 1384 | + break; |
---|
| 1385 | + |
---|
| 1386 | + case CMD_SET_BUS_PARAMS: |
---|
| 1387 | + dev_warn(&dev->intf->dev, |
---|
| 1388 | + "CMD_SET_BUS_PARAMS error in parameter\n"); |
---|
| 1389 | + break; |
---|
| 1390 | + |
---|
| 1391 | + default: |
---|
| 1392 | + dev_warn(&dev->intf->dev, |
---|
| 1393 | + "Unhandled parameter error event cmd_no (%u)\n", |
---|
| 1394 | + info1); |
---|
| 1395 | + break; |
---|
| 1396 | + } |
---|
| 1397 | +} |
---|
| 1398 | + |
---|
| 1399 | +static void kvaser_usb_leaf_error_event(const struct kvaser_usb *dev, |
---|
| 1400 | + const struct kvaser_cmd *cmd) |
---|
| 1401 | +{ |
---|
| 1402 | + u8 error_code = 0; |
---|
| 1403 | + |
---|
| 1404 | + switch (dev->driver_info->family) { |
---|
| 1405 | + case KVASER_LEAF: |
---|
| 1406 | + error_code = cmd->u.leaf.error_event.error_code; |
---|
| 1407 | + break; |
---|
| 1408 | + case KVASER_USBCAN: |
---|
| 1409 | + error_code = cmd->u.usbcan.error_event.error_code; |
---|
| 1410 | + break; |
---|
| 1411 | + } |
---|
| 1412 | + |
---|
| 1413 | + switch (error_code) { |
---|
| 1414 | + case KVASER_USB_LEAF_ERROR_EVENT_TX_QUEUE_FULL: |
---|
| 1415 | + /* Received additional CAN message, when firmware TX queue is |
---|
| 1416 | + * already full. Something is wrong with the driver. |
---|
| 1417 | + * This should never happen! |
---|
| 1418 | + */ |
---|
| 1419 | + dev_err(&dev->intf->dev, |
---|
| 1420 | + "Received error event TX_QUEUE_FULL\n"); |
---|
| 1421 | + break; |
---|
| 1422 | + case KVASER_USB_LEAF_ERROR_EVENT_PARAM: |
---|
| 1423 | + kvaser_usb_leaf_error_event_parameter(dev, cmd); |
---|
| 1424 | + break; |
---|
| 1425 | + |
---|
| 1426 | + default: |
---|
| 1427 | + dev_warn(&dev->intf->dev, |
---|
| 1428 | + "Unhandled error event (%d)\n", error_code); |
---|
| 1429 | + break; |
---|
| 1430 | + } |
---|
1076 | 1431 | } |
---|
1077 | 1432 | |
---|
1078 | 1433 | static void kvaser_usb_leaf_start_chip_reply(const struct kvaser_usb *dev, |
---|
.. | .. |
---|
1115 | 1470 | complete(&priv->stop_comp); |
---|
1116 | 1471 | } |
---|
1117 | 1472 | |
---|
| 1473 | +static void kvaser_usb_leaf_get_busparams_reply(const struct kvaser_usb *dev, |
---|
| 1474 | + const struct kvaser_cmd *cmd) |
---|
| 1475 | +{ |
---|
| 1476 | + struct kvaser_usb_net_priv *priv; |
---|
| 1477 | + u8 channel = cmd->u.busparams.channel; |
---|
| 1478 | + |
---|
| 1479 | + if (channel >= dev->nchannels) { |
---|
| 1480 | + dev_err(&dev->intf->dev, |
---|
| 1481 | + "Invalid channel number (%d)\n", channel); |
---|
| 1482 | + return; |
---|
| 1483 | + } |
---|
| 1484 | + |
---|
| 1485 | + priv = dev->nets[channel]; |
---|
| 1486 | + memcpy(&priv->busparams_nominal, &cmd->u.busparams.busparams, |
---|
| 1487 | + sizeof(priv->busparams_nominal)); |
---|
| 1488 | + |
---|
| 1489 | + complete(&priv->get_busparams_comp); |
---|
| 1490 | +} |
---|
| 1491 | + |
---|
1118 | 1492 | static void kvaser_usb_leaf_handle_command(const struct kvaser_usb *dev, |
---|
1119 | 1493 | const struct kvaser_cmd *cmd) |
---|
1120 | 1494 | { |
---|
| 1495 | + if (kvaser_usb_leaf_verify_size(dev, cmd) < 0) |
---|
| 1496 | + return; |
---|
| 1497 | + |
---|
1121 | 1498 | switch (cmd->id) { |
---|
1122 | 1499 | case CMD_START_CHIP_REPLY: |
---|
1123 | 1500 | kvaser_usb_leaf_start_chip_reply(dev, cmd); |
---|
.. | .. |
---|
1133 | 1510 | break; |
---|
1134 | 1511 | |
---|
1135 | 1512 | case CMD_LEAF_LOG_MESSAGE: |
---|
1136 | | - if (dev->card_data.leaf.family != KVASER_LEAF) |
---|
| 1513 | + if (dev->driver_info->family != KVASER_LEAF) |
---|
1137 | 1514 | goto warn; |
---|
1138 | 1515 | kvaser_usb_leaf_rx_can_msg(dev, cmd); |
---|
1139 | 1516 | break; |
---|
1140 | 1517 | |
---|
1141 | 1518 | case CMD_CHIP_STATE_EVENT: |
---|
1142 | 1519 | case CMD_CAN_ERROR_EVENT: |
---|
1143 | | - if (dev->card_data.leaf.family == KVASER_LEAF) |
---|
| 1520 | + if (dev->driver_info->family == KVASER_LEAF) |
---|
1144 | 1521 | kvaser_usb_leaf_leaf_rx_error(dev, cmd); |
---|
1145 | 1522 | else |
---|
1146 | 1523 | kvaser_usb_leaf_usbcan_rx_error(dev, cmd); |
---|
.. | .. |
---|
1150 | 1527 | kvaser_usb_leaf_tx_acknowledge(dev, cmd); |
---|
1151 | 1528 | break; |
---|
1152 | 1529 | |
---|
| 1530 | + case CMD_ERROR_EVENT: |
---|
| 1531 | + kvaser_usb_leaf_error_event(dev, cmd); |
---|
| 1532 | + break; |
---|
| 1533 | + |
---|
| 1534 | + case CMD_GET_BUS_PARAMS_REPLY: |
---|
| 1535 | + kvaser_usb_leaf_get_busparams_reply(dev, cmd); |
---|
| 1536 | + break; |
---|
| 1537 | + |
---|
1153 | 1538 | /* Ignored commands */ |
---|
1154 | 1539 | case CMD_USBCAN_CLOCK_OVERFLOW_EVENT: |
---|
1155 | | - if (dev->card_data.leaf.family != KVASER_USBCAN) |
---|
| 1540 | + if (dev->driver_info->family != KVASER_USBCAN) |
---|
1156 | 1541 | goto warn; |
---|
1157 | 1542 | break; |
---|
1158 | 1543 | |
---|
1159 | 1544 | case CMD_FLUSH_QUEUE_REPLY: |
---|
1160 | | - if (dev->card_data.leaf.family != KVASER_LEAF) |
---|
| 1545 | + if (dev->driver_info->family != KVASER_LEAF) |
---|
1161 | 1546 | goto warn; |
---|
1162 | 1547 | break; |
---|
1163 | 1548 | |
---|
.. | .. |
---|
1230 | 1615 | { |
---|
1231 | 1616 | int err; |
---|
1232 | 1617 | |
---|
1233 | | - init_completion(&priv->start_comp); |
---|
| 1618 | + reinit_completion(&priv->start_comp); |
---|
1234 | 1619 | |
---|
1235 | 1620 | err = kvaser_usb_leaf_send_simple_cmd(priv->dev, CMD_START_CHIP, |
---|
1236 | 1621 | priv->channel); |
---|
.. | .. |
---|
1246 | 1631 | |
---|
1247 | 1632 | static int kvaser_usb_leaf_stop_chip(struct kvaser_usb_net_priv *priv) |
---|
1248 | 1633 | { |
---|
| 1634 | + struct kvaser_usb_net_leaf_priv *leaf = priv->sub_priv; |
---|
1249 | 1635 | int err; |
---|
1250 | 1636 | |
---|
1251 | | - init_completion(&priv->stop_comp); |
---|
| 1637 | + reinit_completion(&priv->stop_comp); |
---|
| 1638 | + |
---|
| 1639 | + cancel_delayed_work(&leaf->chip_state_req_work); |
---|
1252 | 1640 | |
---|
1253 | 1641 | err = kvaser_usb_leaf_send_simple_cmd(priv->dev, CMD_STOP_CHIP, |
---|
1254 | 1642 | priv->channel); |
---|
.. | .. |
---|
1296 | 1684 | return 0; |
---|
1297 | 1685 | } |
---|
1298 | 1686 | |
---|
1299 | | -static int kvaser_usb_leaf_set_bittiming(struct net_device *netdev) |
---|
| 1687 | +static int kvaser_usb_leaf_init_channel(struct kvaser_usb_net_priv *priv) |
---|
| 1688 | +{ |
---|
| 1689 | + struct kvaser_usb_net_leaf_priv *leaf; |
---|
| 1690 | + |
---|
| 1691 | + leaf = devm_kzalloc(&priv->dev->intf->dev, sizeof(*leaf), GFP_KERNEL); |
---|
| 1692 | + if (!leaf) |
---|
| 1693 | + return -ENOMEM; |
---|
| 1694 | + |
---|
| 1695 | + leaf->net = priv; |
---|
| 1696 | + INIT_DELAYED_WORK(&leaf->chip_state_req_work, |
---|
| 1697 | + kvaser_usb_leaf_chip_state_req_work); |
---|
| 1698 | + |
---|
| 1699 | + priv->sub_priv = leaf; |
---|
| 1700 | + |
---|
| 1701 | + return 0; |
---|
| 1702 | +} |
---|
| 1703 | + |
---|
| 1704 | +static void kvaser_usb_leaf_remove_channel(struct kvaser_usb_net_priv *priv) |
---|
| 1705 | +{ |
---|
| 1706 | + struct kvaser_usb_net_leaf_priv *leaf = priv->sub_priv; |
---|
| 1707 | + |
---|
| 1708 | + if (leaf) |
---|
| 1709 | + cancel_delayed_work_sync(&leaf->chip_state_req_work); |
---|
| 1710 | +} |
---|
| 1711 | + |
---|
| 1712 | +static int kvaser_usb_leaf_set_bittiming(const struct net_device *netdev, |
---|
| 1713 | + const struct kvaser_usb_busparams *busparams) |
---|
1300 | 1714 | { |
---|
1301 | 1715 | struct kvaser_usb_net_priv *priv = netdev_priv(netdev); |
---|
1302 | | - struct can_bittiming *bt = &priv->can.bittiming; |
---|
1303 | 1716 | struct kvaser_usb *dev = priv->dev; |
---|
1304 | 1717 | struct kvaser_cmd *cmd; |
---|
1305 | 1718 | int rc; |
---|
.. | .. |
---|
1312 | 1725 | cmd->len = CMD_HEADER_LEN + sizeof(struct kvaser_cmd_busparams); |
---|
1313 | 1726 | cmd->u.busparams.channel = priv->channel; |
---|
1314 | 1727 | cmd->u.busparams.tid = 0xff; |
---|
1315 | | - cmd->u.busparams.bitrate = cpu_to_le32(bt->bitrate); |
---|
1316 | | - cmd->u.busparams.sjw = bt->sjw; |
---|
1317 | | - cmd->u.busparams.tseg1 = bt->prop_seg + bt->phase_seg1; |
---|
1318 | | - cmd->u.busparams.tseg2 = bt->phase_seg2; |
---|
1319 | | - |
---|
1320 | | - if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES) |
---|
1321 | | - cmd->u.busparams.no_samp = 3; |
---|
1322 | | - else |
---|
1323 | | - cmd->u.busparams.no_samp = 1; |
---|
| 1728 | + memcpy(&cmd->u.busparams.busparams, busparams, |
---|
| 1729 | + sizeof(cmd->u.busparams.busparams)); |
---|
1324 | 1730 | |
---|
1325 | 1731 | rc = kvaser_usb_send_cmd(dev, cmd, cmd->len); |
---|
1326 | 1732 | |
---|
1327 | 1733 | kfree(cmd); |
---|
1328 | 1734 | return rc; |
---|
| 1735 | +} |
---|
| 1736 | + |
---|
| 1737 | +static int kvaser_usb_leaf_get_busparams(struct kvaser_usb_net_priv *priv) |
---|
| 1738 | +{ |
---|
| 1739 | + int err; |
---|
| 1740 | + |
---|
| 1741 | + if (priv->dev->driver_info->family == KVASER_USBCAN) |
---|
| 1742 | + return -EOPNOTSUPP; |
---|
| 1743 | + |
---|
| 1744 | + reinit_completion(&priv->get_busparams_comp); |
---|
| 1745 | + |
---|
| 1746 | + err = kvaser_usb_leaf_send_simple_cmd(priv->dev, CMD_GET_BUS_PARAMS, |
---|
| 1747 | + priv->channel); |
---|
| 1748 | + if (err) |
---|
| 1749 | + return err; |
---|
| 1750 | + |
---|
| 1751 | + if (!wait_for_completion_timeout(&priv->get_busparams_comp, |
---|
| 1752 | + msecs_to_jiffies(KVASER_USB_TIMEOUT))) |
---|
| 1753 | + return -ETIMEDOUT; |
---|
| 1754 | + |
---|
| 1755 | + return 0; |
---|
1329 | 1756 | } |
---|
1330 | 1757 | |
---|
1331 | 1758 | static int kvaser_usb_leaf_set_mode(struct net_device *netdev, |
---|
.. | .. |
---|
1336 | 1763 | |
---|
1337 | 1764 | switch (mode) { |
---|
1338 | 1765 | case CAN_MODE_START: |
---|
| 1766 | + kvaser_usb_unlink_tx_urbs(priv); |
---|
| 1767 | + |
---|
1339 | 1768 | err = kvaser_usb_leaf_simple_cmd_async(priv, CMD_START_CHIP); |
---|
1340 | 1769 | if (err) |
---|
1341 | 1770 | return err; |
---|
| 1771 | + |
---|
| 1772 | + priv->can.state = CAN_STATE_ERROR_ACTIVE; |
---|
1342 | 1773 | break; |
---|
1343 | 1774 | default: |
---|
1344 | 1775 | return -EOPNOTSUPP; |
---|
.. | .. |
---|
1385 | 1816 | const struct kvaser_usb_dev_ops kvaser_usb_leaf_dev_ops = { |
---|
1386 | 1817 | .dev_set_mode = kvaser_usb_leaf_set_mode, |
---|
1387 | 1818 | .dev_set_bittiming = kvaser_usb_leaf_set_bittiming, |
---|
| 1819 | + .dev_get_busparams = kvaser_usb_leaf_get_busparams, |
---|
1388 | 1820 | .dev_set_data_bittiming = NULL, |
---|
| 1821 | + .dev_get_data_busparams = NULL, |
---|
1389 | 1822 | .dev_get_berr_counter = kvaser_usb_leaf_get_berr_counter, |
---|
1390 | 1823 | .dev_setup_endpoints = kvaser_usb_leaf_setup_endpoints, |
---|
1391 | 1824 | .dev_init_card = kvaser_usb_leaf_init_card, |
---|
| 1825 | + .dev_init_channel = kvaser_usb_leaf_init_channel, |
---|
| 1826 | + .dev_remove_channel = kvaser_usb_leaf_remove_channel, |
---|
1392 | 1827 | .dev_get_software_info = kvaser_usb_leaf_get_software_info, |
---|
1393 | 1828 | .dev_get_software_details = NULL, |
---|
1394 | 1829 | .dev_get_card_info = kvaser_usb_leaf_get_card_info, |
---|
1395 | | - .dev_get_capabilities = NULL, |
---|
| 1830 | + .dev_get_capabilities = kvaser_usb_leaf_get_capabilities, |
---|
1396 | 1831 | .dev_set_opt_mode = kvaser_usb_leaf_set_opt_mode, |
---|
1397 | 1832 | .dev_start_chip = kvaser_usb_leaf_start_chip, |
---|
1398 | 1833 | .dev_stop_chip = kvaser_usb_leaf_stop_chip, |
---|