1 // SPDX-License-Identifier: GPL-2.0 2 /* Parts of this driver are based on the following: 3 * - Kvaser linux leaf driver (version 4.78) 4 * - CAN driver for esd CAN-USB/2 5 * - Kvaser linux usbcanII driver (version 5.3) 6 * 7 * Copyright (C) 2002-2018 KVASER AB, Sweden. All rights reserved. 8 * Copyright (C) 2010 Matthias Fuchs <matthias.fuchs@esd.eu>, esd gmbh 9 * Copyright (C) 2012 Olivier Sobrie <olivier@sobrie.be> 10 * Copyright (C) 2015 Valeo S.A. 11 */ 12 13 #include <linux/completion.h> 14 #include <linux/device.h> 15 #include <linux/gfp.h> 16 #include <linux/jiffies.h> 17 #include <linux/kernel.h> 18 #include <linux/netdevice.h> 19 #include <linux/spinlock.h> 20 #include <linux/string.h> 21 #include <linux/types.h> 22 #include <linux/units.h> 23 #include <linux/usb.h> 24 25 #include <linux/can.h> 26 #include <linux/can/dev.h> 27 #include <linux/can/error.h> 28 #include <linux/can/netlink.h> 29 30 #include "kvaser_usb.h" 31 32 #define MAX_USBCAN_NET_DEVICES 2 33 34 /* Command header size */ 35 #define CMD_HEADER_LEN 2 36 37 /* Kvaser CAN message flags */ 38 #define MSG_FLAG_ERROR_FRAME BIT(0) 39 #define MSG_FLAG_OVERRUN BIT(1) 40 #define MSG_FLAG_NERR BIT(2) 41 #define MSG_FLAG_WAKEUP BIT(3) 42 #define MSG_FLAG_REMOTE_FRAME BIT(4) 43 #define MSG_FLAG_RESERVED BIT(5) 44 #define MSG_FLAG_TX_ACK BIT(6) 45 #define MSG_FLAG_TX_REQUEST BIT(7) 46 47 /* CAN states (M16C CxSTRH register) */ 48 #define M16C_STATE_BUS_RESET BIT(0) 49 #define M16C_STATE_BUS_ERROR BIT(4) 50 #define M16C_STATE_BUS_PASSIVE BIT(5) 51 #define M16C_STATE_BUS_OFF BIT(6) 52 53 /* Leaf/usbcan command ids */ 54 #define CMD_RX_STD_MESSAGE 12 55 #define CMD_TX_STD_MESSAGE 13 56 #define CMD_RX_EXT_MESSAGE 14 57 #define CMD_TX_EXT_MESSAGE 15 58 #define CMD_SET_BUS_PARAMS 16 59 #define CMD_CHIP_STATE_EVENT 20 60 #define CMD_SET_CTRL_MODE 21 61 #define CMD_RESET_CHIP 24 62 #define CMD_START_CHIP 26 63 #define CMD_START_CHIP_REPLY 27 64 #define CMD_STOP_CHIP 28 65 #define CMD_STOP_CHIP_REPLY 29 66 67 #define CMD_USBCAN_CLOCK_OVERFLOW_EVENT 33 68 69 #define CMD_GET_CARD_INFO 34 70 #define CMD_GET_CARD_INFO_REPLY 35 71 #define CMD_GET_SOFTWARE_INFO 38 72 #define CMD_GET_SOFTWARE_INFO_REPLY 39 73 #define CMD_FLUSH_QUEUE 48 74 #define CMD_TX_ACKNOWLEDGE 50 75 #define CMD_CAN_ERROR_EVENT 51 76 #define CMD_FLUSH_QUEUE_REPLY 68 77 78 #define CMD_LEAF_LOG_MESSAGE 106 79 80 /* Leaf frequency options */ 81 #define KVASER_USB_LEAF_SWOPTION_FREQ_MASK 0x60 82 #define KVASER_USB_LEAF_SWOPTION_FREQ_16_MHZ_CLK 0 83 #define KVASER_USB_LEAF_SWOPTION_FREQ_32_MHZ_CLK BIT(5) 84 #define KVASER_USB_LEAF_SWOPTION_FREQ_24_MHZ_CLK BIT(6) 85 86 /* error factors */ 87 #define M16C_EF_ACKE BIT(0) 88 #define M16C_EF_CRCE BIT(1) 89 #define M16C_EF_FORME BIT(2) 90 #define M16C_EF_STFE BIT(3) 91 #define M16C_EF_BITE0 BIT(4) 92 #define M16C_EF_BITE1 BIT(5) 93 #define M16C_EF_RCVE BIT(6) 94 #define M16C_EF_TRE BIT(7) 95 96 /* Only Leaf-based devices can report M16C error factors, 97 * thus define our own error status flags for USBCANII 98 */ 99 #define USBCAN_ERROR_STATE_NONE 0 100 #define USBCAN_ERROR_STATE_TX_ERROR BIT(0) 101 #define USBCAN_ERROR_STATE_RX_ERROR BIT(1) 102 #define USBCAN_ERROR_STATE_BUSERROR BIT(2) 103 104 /* ctrl modes */ 105 #define KVASER_CTRL_MODE_NORMAL 1 106 #define KVASER_CTRL_MODE_SILENT 2 107 #define KVASER_CTRL_MODE_SELFRECEPTION 3 108 #define KVASER_CTRL_MODE_OFF 4 109 110 /* Extended CAN identifier flag */ 111 #define KVASER_EXTENDED_FRAME BIT(31) 112 113 struct kvaser_cmd_simple { 114 u8 tid; 115 u8 channel; 116 } __packed; 117 118 struct kvaser_cmd_cardinfo { 119 u8 tid; 120 u8 nchannels; 121 __le32 serial_number; 122 __le32 padding0; 123 __le32 clock_resolution; 124 __le32 mfgdate; 125 u8 ean[8]; 126 u8 hw_revision; 127 union { 128 struct { 129 u8 usb_hs_mode; 130 } __packed leaf1; 131 struct { 132 u8 padding; 133 } __packed usbcan1; 134 } __packed; 135 __le16 padding1; 136 } __packed; 137 138 struct leaf_cmd_softinfo { 139 u8 tid; 140 u8 padding0; 141 __le32 sw_options; 142 __le32 fw_version; 143 __le16 max_outstanding_tx; 144 __le16 padding1[9]; 145 } __packed; 146 147 struct usbcan_cmd_softinfo { 148 u8 tid; 149 u8 fw_name[5]; 150 __le16 max_outstanding_tx; 151 u8 padding[6]; 152 __le32 fw_version; 153 __le16 checksum; 154 __le16 sw_options; 155 } __packed; 156 157 struct kvaser_cmd_busparams { 158 u8 tid; 159 u8 channel; 160 __le32 bitrate; 161 u8 tseg1; 162 u8 tseg2; 163 u8 sjw; 164 u8 no_samp; 165 } __packed; 166 167 struct kvaser_cmd_tx_can { 168 u8 channel; 169 u8 tid; 170 u8 data[14]; 171 union { 172 struct { 173 u8 padding; 174 u8 flags; 175 } __packed leaf; 176 struct { 177 u8 flags; 178 u8 padding; 179 } __packed usbcan; 180 } __packed; 181 } __packed; 182 183 struct kvaser_cmd_rx_can_header { 184 u8 channel; 185 u8 flag; 186 } __packed; 187 188 struct leaf_cmd_rx_can { 189 u8 channel; 190 u8 flag; 191 192 __le16 time[3]; 193 u8 data[14]; 194 } __packed; 195 196 struct usbcan_cmd_rx_can { 197 u8 channel; 198 u8 flag; 199 200 u8 data[14]; 201 __le16 time; 202 } __packed; 203 204 struct leaf_cmd_chip_state_event { 205 u8 tid; 206 u8 channel; 207 208 __le16 time[3]; 209 u8 tx_errors_count; 210 u8 rx_errors_count; 211 212 u8 status; 213 u8 padding[3]; 214 } __packed; 215 216 struct usbcan_cmd_chip_state_event { 217 u8 tid; 218 u8 channel; 219 220 u8 tx_errors_count; 221 u8 rx_errors_count; 222 __le16 time; 223 224 u8 status; 225 u8 padding[3]; 226 } __packed; 227 228 struct kvaser_cmd_tx_acknowledge_header { 229 u8 channel; 230 u8 tid; 231 } __packed; 232 233 struct leaf_cmd_error_event { 234 u8 tid; 235 u8 flags; 236 __le16 time[3]; 237 u8 channel; 238 u8 padding; 239 u8 tx_errors_count; 240 u8 rx_errors_count; 241 u8 status; 242 u8 error_factor; 243 } __packed; 244 245 struct usbcan_cmd_error_event { 246 u8 tid; 247 u8 padding; 248 u8 tx_errors_count_ch0; 249 u8 rx_errors_count_ch0; 250 u8 tx_errors_count_ch1; 251 u8 rx_errors_count_ch1; 252 u8 status_ch0; 253 u8 status_ch1; 254 __le16 time; 255 } __packed; 256 257 struct kvaser_cmd_ctrl_mode { 258 u8 tid; 259 u8 channel; 260 u8 ctrl_mode; 261 u8 padding[3]; 262 } __packed; 263 264 struct kvaser_cmd_flush_queue { 265 u8 tid; 266 u8 channel; 267 u8 flags; 268 u8 padding[3]; 269 } __packed; 270 271 struct leaf_cmd_log_message { 272 u8 channel; 273 u8 flags; 274 __le16 time[3]; 275 u8 dlc; 276 u8 time_offset; 277 __le32 id; 278 u8 data[8]; 279 } __packed; 280 281 struct kvaser_cmd { 282 u8 len; 283 u8 id; 284 union { 285 struct kvaser_cmd_simple simple; 286 struct kvaser_cmd_cardinfo cardinfo; 287 struct kvaser_cmd_busparams busparams; 288 289 struct kvaser_cmd_rx_can_header rx_can_header; 290 struct kvaser_cmd_tx_acknowledge_header tx_acknowledge_header; 291 292 union { 293 struct leaf_cmd_softinfo softinfo; 294 struct leaf_cmd_rx_can rx_can; 295 struct leaf_cmd_chip_state_event chip_state_event; 296 struct leaf_cmd_error_event error_event; 297 struct leaf_cmd_log_message log_message; 298 } __packed leaf; 299 300 union { 301 struct usbcan_cmd_softinfo softinfo; 302 struct usbcan_cmd_rx_can rx_can; 303 struct usbcan_cmd_chip_state_event chip_state_event; 304 struct usbcan_cmd_error_event error_event; 305 } __packed usbcan; 306 307 struct kvaser_cmd_tx_can tx_can; 308 struct kvaser_cmd_ctrl_mode ctrl_mode; 309 struct kvaser_cmd_flush_queue flush_queue; 310 } u; 311 } __packed; 312 313 #define CMD_SIZE_ANY 0xff 314 #define kvaser_fsize(field) sizeof_field(struct kvaser_cmd, field) 315 316 static const u8 kvaser_usb_leaf_cmd_sizes_leaf[] = { 317 [CMD_START_CHIP_REPLY] = kvaser_fsize(u.simple), 318 [CMD_STOP_CHIP_REPLY] = kvaser_fsize(u.simple), 319 [CMD_GET_CARD_INFO_REPLY] = kvaser_fsize(u.cardinfo), 320 [CMD_TX_ACKNOWLEDGE] = kvaser_fsize(u.tx_acknowledge_header), 321 [CMD_GET_SOFTWARE_INFO_REPLY] = kvaser_fsize(u.leaf.softinfo), 322 [CMD_RX_STD_MESSAGE] = kvaser_fsize(u.leaf.rx_can), 323 [CMD_RX_EXT_MESSAGE] = kvaser_fsize(u.leaf.rx_can), 324 [CMD_LEAF_LOG_MESSAGE] = kvaser_fsize(u.leaf.log_message), 325 [CMD_CHIP_STATE_EVENT] = kvaser_fsize(u.leaf.chip_state_event), 326 [CMD_CAN_ERROR_EVENT] = kvaser_fsize(u.leaf.error_event), 327 /* ignored events: */ 328 [CMD_FLUSH_QUEUE_REPLY] = CMD_SIZE_ANY, 329 }; 330 331 static const u8 kvaser_usb_leaf_cmd_sizes_usbcan[] = { 332 [CMD_START_CHIP_REPLY] = kvaser_fsize(u.simple), 333 [CMD_STOP_CHIP_REPLY] = kvaser_fsize(u.simple), 334 [CMD_GET_CARD_INFO_REPLY] = kvaser_fsize(u.cardinfo), 335 [CMD_TX_ACKNOWLEDGE] = kvaser_fsize(u.tx_acknowledge_header), 336 [CMD_GET_SOFTWARE_INFO_REPLY] = kvaser_fsize(u.usbcan.softinfo), 337 [CMD_RX_STD_MESSAGE] = kvaser_fsize(u.usbcan.rx_can), 338 [CMD_RX_EXT_MESSAGE] = kvaser_fsize(u.usbcan.rx_can), 339 [CMD_CHIP_STATE_EVENT] = kvaser_fsize(u.usbcan.chip_state_event), 340 [CMD_CAN_ERROR_EVENT] = kvaser_fsize(u.usbcan.error_event), 341 /* ignored events: */ 342 [CMD_USBCAN_CLOCK_OVERFLOW_EVENT] = CMD_SIZE_ANY, 343 }; 344 345 /* Summary of a kvaser error event, for a unified Leaf/Usbcan error 346 * handling. Some discrepancies between the two families exist: 347 * 348 * - USBCAN firmware does not report M16C "error factors" 349 * - USBCAN controllers has difficulties reporting if the raised error 350 * event is for ch0 or ch1. They leave such arbitration to the OS 351 * driver by letting it compare error counters with previous values 352 * and decide the error event's channel. Thus for USBCAN, the channel 353 * field is only advisory. 354 */ 355 struct kvaser_usb_err_summary { 356 u8 channel, status, txerr, rxerr; 357 union { 358 struct { 359 u8 error_factor; 360 } leaf; 361 struct { 362 u8 other_ch_status; 363 u8 error_state; 364 } usbcan; 365 }; 366 }; 367 368 static const struct can_bittiming_const kvaser_usb_leaf_m16c_bittiming_const = { 369 .name = "kvaser_usb_ucii", 370 .tseg1_min = 4, 371 .tseg1_max = 16, 372 .tseg2_min = 2, 373 .tseg2_max = 8, 374 .sjw_max = 4, 375 .brp_min = 1, 376 .brp_max = 16, 377 .brp_inc = 1, 378 }; 379 380 static const struct can_bittiming_const kvaser_usb_leaf_m32c_bittiming_const = { 381 .name = "kvaser_usb_leaf", 382 .tseg1_min = 3, 383 .tseg1_max = 16, 384 .tseg2_min = 2, 385 .tseg2_max = 8, 386 .sjw_max = 4, 387 .brp_min = 2, 388 .brp_max = 128, 389 .brp_inc = 2, 390 }; 391 392 static const struct kvaser_usb_dev_cfg kvaser_usb_leaf_usbcan_dev_cfg = { 393 .clock = { 394 .freq = 8 * MEGA /* Hz */, 395 }, 396 .timestamp_freq = 1, 397 .bittiming_const = &kvaser_usb_leaf_m16c_bittiming_const, 398 }; 399 400 static const struct kvaser_usb_dev_cfg kvaser_usb_leaf_m32c_dev_cfg = { 401 .clock = { 402 .freq = 16 * MEGA /* Hz */, 403 }, 404 .timestamp_freq = 1, 405 .bittiming_const = &kvaser_usb_leaf_m32c_bittiming_const, 406 }; 407 408 static const struct kvaser_usb_dev_cfg kvaser_usb_leaf_imx_dev_cfg_16mhz = { 409 .clock = { 410 .freq = 16 * MEGA /* Hz */, 411 }, 412 .timestamp_freq = 1, 413 .bittiming_const = &kvaser_usb_flexc_bittiming_const, 414 }; 415 416 static const struct kvaser_usb_dev_cfg kvaser_usb_leaf_imx_dev_cfg_24mhz = { 417 .clock = { 418 .freq = 24 * MEGA /* Hz */, 419 }, 420 .timestamp_freq = 1, 421 .bittiming_const = &kvaser_usb_flexc_bittiming_const, 422 }; 423 424 static const struct kvaser_usb_dev_cfg kvaser_usb_leaf_imx_dev_cfg_32mhz = { 425 .clock = { 426 .freq = 32 * MEGA /* Hz */, 427 }, 428 .timestamp_freq = 1, 429 .bittiming_const = &kvaser_usb_flexc_bittiming_const, 430 }; 431 432 static int kvaser_usb_leaf_verify_size(const struct kvaser_usb *dev, 433 const struct kvaser_cmd *cmd) 434 { 435 /* buffer size >= cmd->len ensured by caller */ 436 u8 min_size = 0; 437 438 switch (dev->driver_info->family) { 439 case KVASER_LEAF: 440 if (cmd->id < ARRAY_SIZE(kvaser_usb_leaf_cmd_sizes_leaf)) 441 min_size = kvaser_usb_leaf_cmd_sizes_leaf[cmd->id]; 442 break; 443 case KVASER_USBCAN: 444 if (cmd->id < ARRAY_SIZE(kvaser_usb_leaf_cmd_sizes_usbcan)) 445 min_size = kvaser_usb_leaf_cmd_sizes_usbcan[cmd->id]; 446 break; 447 } 448 449 if (min_size == CMD_SIZE_ANY) 450 return 0; 451 452 if (min_size) { 453 min_size += CMD_HEADER_LEN; 454 if (cmd->len >= min_size) 455 return 0; 456 457 dev_err_ratelimited(&dev->intf->dev, 458 "Received command %u too short (size %u, needed %u)", 459 cmd->id, cmd->len, min_size); 460 return -EIO; 461 } 462 463 dev_warn_ratelimited(&dev->intf->dev, 464 "Unhandled command (%d, size %d)\n", 465 cmd->id, cmd->len); 466 return -EINVAL; 467 } 468 469 static void * 470 kvaser_usb_leaf_frame_to_cmd(const struct kvaser_usb_net_priv *priv, 471 const struct sk_buff *skb, int *cmd_len, 472 u16 transid) 473 { 474 struct kvaser_usb *dev = priv->dev; 475 struct kvaser_cmd *cmd; 476 u8 *cmd_tx_can_flags = NULL; /* GCC */ 477 struct can_frame *cf = (struct can_frame *)skb->data; 478 479 cmd = kmalloc(sizeof(*cmd), GFP_ATOMIC); 480 if (cmd) { 481 cmd->u.tx_can.tid = transid & 0xff; 482 cmd->len = *cmd_len = CMD_HEADER_LEN + 483 sizeof(struct kvaser_cmd_tx_can); 484 cmd->u.tx_can.channel = priv->channel; 485 486 switch (dev->driver_info->family) { 487 case KVASER_LEAF: 488 cmd_tx_can_flags = &cmd->u.tx_can.leaf.flags; 489 break; 490 case KVASER_USBCAN: 491 cmd_tx_can_flags = &cmd->u.tx_can.usbcan.flags; 492 break; 493 } 494 495 *cmd_tx_can_flags = 0; 496 497 if (cf->can_id & CAN_EFF_FLAG) { 498 cmd->id = CMD_TX_EXT_MESSAGE; 499 cmd->u.tx_can.data[0] = (cf->can_id >> 24) & 0x1f; 500 cmd->u.tx_can.data[1] = (cf->can_id >> 18) & 0x3f; 501 cmd->u.tx_can.data[2] = (cf->can_id >> 14) & 0x0f; 502 cmd->u.tx_can.data[3] = (cf->can_id >> 6) & 0xff; 503 cmd->u.tx_can.data[4] = cf->can_id & 0x3f; 504 } else { 505 cmd->id = CMD_TX_STD_MESSAGE; 506 cmd->u.tx_can.data[0] = (cf->can_id >> 6) & 0x1f; 507 cmd->u.tx_can.data[1] = cf->can_id & 0x3f; 508 } 509 510 cmd->u.tx_can.data[5] = cf->len; 511 memcpy(&cmd->u.tx_can.data[6], cf->data, cf->len); 512 513 if (cf->can_id & CAN_RTR_FLAG) 514 *cmd_tx_can_flags |= MSG_FLAG_REMOTE_FRAME; 515 } 516 return cmd; 517 } 518 519 static int kvaser_usb_leaf_wait_cmd(const struct kvaser_usb *dev, u8 id, 520 struct kvaser_cmd *cmd) 521 { 522 struct kvaser_cmd *tmp; 523 void *buf; 524 int actual_len; 525 int err; 526 int pos; 527 unsigned long to = jiffies + msecs_to_jiffies(KVASER_USB_TIMEOUT); 528 529 buf = kzalloc(KVASER_USB_RX_BUFFER_SIZE, GFP_KERNEL); 530 if (!buf) 531 return -ENOMEM; 532 533 do { 534 err = kvaser_usb_recv_cmd(dev, buf, KVASER_USB_RX_BUFFER_SIZE, 535 &actual_len); 536 if (err < 0) 537 goto end; 538 539 pos = 0; 540 while (pos <= actual_len - CMD_HEADER_LEN) { 541 tmp = buf + pos; 542 543 /* Handle commands crossing the USB endpoint max packet 544 * size boundary. Check kvaser_usb_read_bulk_callback() 545 * for further details. 546 */ 547 if (tmp->len == 0) { 548 pos = round_up(pos, 549 le16_to_cpu 550 (dev->bulk_in->wMaxPacketSize)); 551 continue; 552 } 553 554 if (pos + tmp->len > actual_len) { 555 dev_err_ratelimited(&dev->intf->dev, 556 "Format error\n"); 557 break; 558 } 559 560 if (tmp->id == id) { 561 memcpy(cmd, tmp, tmp->len); 562 goto end; 563 } 564 565 pos += tmp->len; 566 } 567 } while (time_before(jiffies, to)); 568 569 err = -EINVAL; 570 571 end: 572 kfree(buf); 573 574 if (err == 0) 575 err = kvaser_usb_leaf_verify_size(dev, cmd); 576 577 return err; 578 } 579 580 static int kvaser_usb_leaf_send_simple_cmd(const struct kvaser_usb *dev, 581 u8 cmd_id, int channel) 582 { 583 struct kvaser_cmd *cmd; 584 int rc; 585 586 cmd = kmalloc(sizeof(*cmd), GFP_KERNEL); 587 if (!cmd) 588 return -ENOMEM; 589 590 cmd->id = cmd_id; 591 cmd->len = CMD_HEADER_LEN + sizeof(struct kvaser_cmd_simple); 592 cmd->u.simple.channel = channel; 593 cmd->u.simple.tid = 0xff; 594 595 rc = kvaser_usb_send_cmd(dev, cmd, cmd->len); 596 597 kfree(cmd); 598 return rc; 599 } 600 601 static void kvaser_usb_leaf_get_software_info_leaf(struct kvaser_usb *dev, 602 const struct leaf_cmd_softinfo *softinfo) 603 { 604 u32 sw_options = le32_to_cpu(softinfo->sw_options); 605 606 dev->fw_version = le32_to_cpu(softinfo->fw_version); 607 dev->max_tx_urbs = le16_to_cpu(softinfo->max_outstanding_tx); 608 609 if (dev->driver_info->quirks & KVASER_USB_QUIRK_IGNORE_CLK_FREQ) { 610 /* Firmware expects bittiming parameters calculated for 16MHz 611 * clock, regardless of the actual clock 612 */ 613 dev->cfg = &kvaser_usb_leaf_m32c_dev_cfg; 614 } else { 615 switch (sw_options & KVASER_USB_LEAF_SWOPTION_FREQ_MASK) { 616 case KVASER_USB_LEAF_SWOPTION_FREQ_16_MHZ_CLK: 617 dev->cfg = &kvaser_usb_leaf_imx_dev_cfg_16mhz; 618 break; 619 case KVASER_USB_LEAF_SWOPTION_FREQ_24_MHZ_CLK: 620 dev->cfg = &kvaser_usb_leaf_imx_dev_cfg_24mhz; 621 break; 622 case KVASER_USB_LEAF_SWOPTION_FREQ_32_MHZ_CLK: 623 dev->cfg = &kvaser_usb_leaf_imx_dev_cfg_32mhz; 624 break; 625 } 626 } 627 } 628 629 static int kvaser_usb_leaf_get_software_info_inner(struct kvaser_usb *dev) 630 { 631 struct kvaser_cmd cmd; 632 int err; 633 634 err = kvaser_usb_leaf_send_simple_cmd(dev, CMD_GET_SOFTWARE_INFO, 0); 635 if (err) 636 return err; 637 638 err = kvaser_usb_leaf_wait_cmd(dev, CMD_GET_SOFTWARE_INFO_REPLY, &cmd); 639 if (err) 640 return err; 641 642 switch (dev->driver_info->family) { 643 case KVASER_LEAF: 644 kvaser_usb_leaf_get_software_info_leaf(dev, &cmd.u.leaf.softinfo); 645 break; 646 case KVASER_USBCAN: 647 dev->fw_version = le32_to_cpu(cmd.u.usbcan.softinfo.fw_version); 648 dev->max_tx_urbs = 649 le16_to_cpu(cmd.u.usbcan.softinfo.max_outstanding_tx); 650 dev->cfg = &kvaser_usb_leaf_usbcan_dev_cfg; 651 break; 652 } 653 654 return 0; 655 } 656 657 static int kvaser_usb_leaf_get_software_info(struct kvaser_usb *dev) 658 { 659 int err; 660 int retry = 3; 661 662 /* On some x86 laptops, plugging a Kvaser device again after 663 * an unplug makes the firmware always ignore the very first 664 * command. For such a case, provide some room for retries 665 * instead of completely exiting the driver. 666 */ 667 do { 668 err = kvaser_usb_leaf_get_software_info_inner(dev); 669 } while (--retry && err == -ETIMEDOUT); 670 671 return err; 672 } 673 674 static int kvaser_usb_leaf_get_card_info(struct kvaser_usb *dev) 675 { 676 struct kvaser_cmd cmd; 677 int err; 678 679 err = kvaser_usb_leaf_send_simple_cmd(dev, CMD_GET_CARD_INFO, 0); 680 if (err) 681 return err; 682 683 err = kvaser_usb_leaf_wait_cmd(dev, CMD_GET_CARD_INFO_REPLY, &cmd); 684 if (err) 685 return err; 686 687 dev->nchannels = cmd.u.cardinfo.nchannels; 688 if (dev->nchannels > KVASER_USB_MAX_NET_DEVICES || 689 (dev->driver_info->family == KVASER_USBCAN && 690 dev->nchannels > MAX_USBCAN_NET_DEVICES)) 691 return -EINVAL; 692 693 return 0; 694 } 695 696 static void kvaser_usb_leaf_tx_acknowledge(const struct kvaser_usb *dev, 697 const struct kvaser_cmd *cmd) 698 { 699 struct net_device_stats *stats; 700 struct kvaser_usb_tx_urb_context *context; 701 struct kvaser_usb_net_priv *priv; 702 unsigned long flags; 703 u8 channel, tid; 704 705 channel = cmd->u.tx_acknowledge_header.channel; 706 tid = cmd->u.tx_acknowledge_header.tid; 707 708 if (channel >= dev->nchannels) { 709 dev_err(&dev->intf->dev, 710 "Invalid channel number (%d)\n", channel); 711 return; 712 } 713 714 priv = dev->nets[channel]; 715 716 if (!netif_device_present(priv->netdev)) 717 return; 718 719 stats = &priv->netdev->stats; 720 721 context = &priv->tx_contexts[tid % dev->max_tx_urbs]; 722 723 /* Sometimes the state change doesn't come after a bus-off event */ 724 if (priv->can.restart_ms && priv->can.state >= CAN_STATE_BUS_OFF) { 725 struct sk_buff *skb; 726 struct can_frame *cf; 727 728 skb = alloc_can_err_skb(priv->netdev, &cf); 729 if (skb) { 730 cf->can_id |= CAN_ERR_RESTARTED; 731 732 netif_rx(skb); 733 } else { 734 netdev_err(priv->netdev, 735 "No memory left for err_skb\n"); 736 } 737 738 priv->can.can_stats.restarts++; 739 netif_carrier_on(priv->netdev); 740 741 priv->can.state = CAN_STATE_ERROR_ACTIVE; 742 } 743 744 spin_lock_irqsave(&priv->tx_contexts_lock, flags); 745 746 stats->tx_packets++; 747 stats->tx_bytes += can_get_echo_skb(priv->netdev, 748 context->echo_index, NULL); 749 context->echo_index = dev->max_tx_urbs; 750 --priv->active_tx_contexts; 751 netif_wake_queue(priv->netdev); 752 753 spin_unlock_irqrestore(&priv->tx_contexts_lock, flags); 754 } 755 756 static int kvaser_usb_leaf_simple_cmd_async(struct kvaser_usb_net_priv *priv, 757 u8 cmd_id) 758 { 759 struct kvaser_cmd *cmd; 760 int err; 761 762 cmd = kzalloc(sizeof(*cmd), GFP_ATOMIC); 763 if (!cmd) 764 return -ENOMEM; 765 766 cmd->len = CMD_HEADER_LEN + sizeof(struct kvaser_cmd_simple); 767 cmd->id = cmd_id; 768 cmd->u.simple.channel = priv->channel; 769 770 err = kvaser_usb_send_cmd_async(priv, cmd, cmd->len); 771 if (err) 772 kfree(cmd); 773 774 return err; 775 } 776 777 static void 778 kvaser_usb_leaf_rx_error_update_can_state(struct kvaser_usb_net_priv *priv, 779 const struct kvaser_usb_err_summary *es, 780 struct can_frame *cf) 781 { 782 struct kvaser_usb *dev = priv->dev; 783 struct net_device_stats *stats = &priv->netdev->stats; 784 enum can_state cur_state, new_state, tx_state, rx_state; 785 786 netdev_dbg(priv->netdev, "Error status: 0x%02x\n", es->status); 787 788 new_state = priv->can.state; 789 cur_state = priv->can.state; 790 791 if (es->status & (M16C_STATE_BUS_OFF | M16C_STATE_BUS_RESET)) { 792 new_state = CAN_STATE_BUS_OFF; 793 } else if (es->status & M16C_STATE_BUS_PASSIVE) { 794 new_state = CAN_STATE_ERROR_PASSIVE; 795 } else if (es->status & M16C_STATE_BUS_ERROR) { 796 /* Guard against spurious error events after a busoff */ 797 if (cur_state < CAN_STATE_BUS_OFF) { 798 if (es->txerr >= 128 || es->rxerr >= 128) 799 new_state = CAN_STATE_ERROR_PASSIVE; 800 else if (es->txerr >= 96 || es->rxerr >= 96) 801 new_state = CAN_STATE_ERROR_WARNING; 802 else if (cur_state > CAN_STATE_ERROR_ACTIVE) 803 new_state = CAN_STATE_ERROR_ACTIVE; 804 } 805 } 806 807 if (!es->status) 808 new_state = CAN_STATE_ERROR_ACTIVE; 809 810 if (new_state != cur_state) { 811 tx_state = (es->txerr >= es->rxerr) ? new_state : 0; 812 rx_state = (es->txerr <= es->rxerr) ? new_state : 0; 813 814 can_change_state(priv->netdev, cf, tx_state, rx_state); 815 } 816 817 if (priv->can.restart_ms && 818 cur_state >= CAN_STATE_BUS_OFF && 819 new_state < CAN_STATE_BUS_OFF) 820 priv->can.can_stats.restarts++; 821 822 switch (dev->driver_info->family) { 823 case KVASER_LEAF: 824 if (es->leaf.error_factor) { 825 priv->can.can_stats.bus_error++; 826 stats->rx_errors++; 827 } 828 break; 829 case KVASER_USBCAN: 830 if (es->usbcan.error_state & USBCAN_ERROR_STATE_TX_ERROR) 831 stats->tx_errors++; 832 if (es->usbcan.error_state & USBCAN_ERROR_STATE_RX_ERROR) 833 stats->rx_errors++; 834 if (es->usbcan.error_state & USBCAN_ERROR_STATE_BUSERROR) 835 priv->can.can_stats.bus_error++; 836 break; 837 } 838 839 priv->bec.txerr = es->txerr; 840 priv->bec.rxerr = es->rxerr; 841 } 842 843 static void kvaser_usb_leaf_rx_error(const struct kvaser_usb *dev, 844 const struct kvaser_usb_err_summary *es) 845 { 846 struct can_frame *cf; 847 struct can_frame tmp_cf = { .can_id = CAN_ERR_FLAG, 848 .len = CAN_ERR_DLC }; 849 struct sk_buff *skb; 850 struct net_device_stats *stats; 851 struct kvaser_usb_net_priv *priv; 852 enum can_state old_state, new_state; 853 854 if (es->channel >= dev->nchannels) { 855 dev_err(&dev->intf->dev, 856 "Invalid channel number (%d)\n", es->channel); 857 return; 858 } 859 860 priv = dev->nets[es->channel]; 861 stats = &priv->netdev->stats; 862 863 /* Update all of the CAN interface's state and error counters before 864 * trying any memory allocation that can actually fail with -ENOMEM. 865 * 866 * We send a temporary stack-allocated error CAN frame to 867 * can_change_state() for the very same reason. 868 * 869 * TODO: Split can_change_state() responsibility between updating the 870 * CAN interface's state and counters, and the setting up of CAN error 871 * frame ID and data to userspace. Remove stack allocation afterwards. 872 */ 873 old_state = priv->can.state; 874 kvaser_usb_leaf_rx_error_update_can_state(priv, es, &tmp_cf); 875 new_state = priv->can.state; 876 877 skb = alloc_can_err_skb(priv->netdev, &cf); 878 if (!skb) { 879 stats->rx_dropped++; 880 return; 881 } 882 memcpy(cf, &tmp_cf, sizeof(*cf)); 883 884 if (new_state != old_state) { 885 if (es->status & 886 (M16C_STATE_BUS_OFF | M16C_STATE_BUS_RESET)) { 887 if (!priv->can.restart_ms) 888 kvaser_usb_leaf_simple_cmd_async(priv, 889 CMD_STOP_CHIP); 890 netif_carrier_off(priv->netdev); 891 } 892 893 if (priv->can.restart_ms && 894 old_state >= CAN_STATE_BUS_OFF && 895 new_state < CAN_STATE_BUS_OFF) { 896 cf->can_id |= CAN_ERR_RESTARTED; 897 netif_carrier_on(priv->netdev); 898 } 899 } 900 901 switch (dev->driver_info->family) { 902 case KVASER_LEAF: 903 if (es->leaf.error_factor) { 904 cf->can_id |= CAN_ERR_BUSERROR | CAN_ERR_PROT; 905 906 if (es->leaf.error_factor & M16C_EF_ACKE) 907 cf->data[3] = CAN_ERR_PROT_LOC_ACK; 908 if (es->leaf.error_factor & M16C_EF_CRCE) 909 cf->data[3] = CAN_ERR_PROT_LOC_CRC_SEQ; 910 if (es->leaf.error_factor & M16C_EF_FORME) 911 cf->data[2] |= CAN_ERR_PROT_FORM; 912 if (es->leaf.error_factor & M16C_EF_STFE) 913 cf->data[2] |= CAN_ERR_PROT_STUFF; 914 if (es->leaf.error_factor & M16C_EF_BITE0) 915 cf->data[2] |= CAN_ERR_PROT_BIT0; 916 if (es->leaf.error_factor & M16C_EF_BITE1) 917 cf->data[2] |= CAN_ERR_PROT_BIT1; 918 if (es->leaf.error_factor & M16C_EF_TRE) 919 cf->data[2] |= CAN_ERR_PROT_TX; 920 } 921 break; 922 case KVASER_USBCAN: 923 if (es->usbcan.error_state & USBCAN_ERROR_STATE_BUSERROR) 924 cf->can_id |= CAN_ERR_BUSERROR; 925 break; 926 } 927 928 if (new_state != CAN_STATE_BUS_OFF) { 929 cf->can_id |= CAN_ERR_CNT; 930 cf->data[6] = es->txerr; 931 cf->data[7] = es->rxerr; 932 } 933 934 netif_rx(skb); 935 } 936 937 /* For USBCAN, report error to userspace if the channels's errors counter 938 * has changed, or we're the only channel seeing a bus error state. 939 */ 940 static void 941 kvaser_usb_leaf_usbcan_conditionally_rx_error(const struct kvaser_usb *dev, 942 struct kvaser_usb_err_summary *es) 943 { 944 struct kvaser_usb_net_priv *priv; 945 unsigned int channel; 946 bool report_error; 947 948 channel = es->channel; 949 if (channel >= dev->nchannels) { 950 dev_err(&dev->intf->dev, 951 "Invalid channel number (%d)\n", channel); 952 return; 953 } 954 955 priv = dev->nets[channel]; 956 report_error = false; 957 958 if (es->txerr != priv->bec.txerr) { 959 es->usbcan.error_state |= USBCAN_ERROR_STATE_TX_ERROR; 960 report_error = true; 961 } 962 if (es->rxerr != priv->bec.rxerr) { 963 es->usbcan.error_state |= USBCAN_ERROR_STATE_RX_ERROR; 964 report_error = true; 965 } 966 if ((es->status & M16C_STATE_BUS_ERROR) && 967 !(es->usbcan.other_ch_status & M16C_STATE_BUS_ERROR)) { 968 es->usbcan.error_state |= USBCAN_ERROR_STATE_BUSERROR; 969 report_error = true; 970 } 971 972 if (report_error) 973 kvaser_usb_leaf_rx_error(dev, es); 974 } 975 976 static void kvaser_usb_leaf_usbcan_rx_error(const struct kvaser_usb *dev, 977 const struct kvaser_cmd *cmd) 978 { 979 struct kvaser_usb_err_summary es = { }; 980 981 switch (cmd->id) { 982 /* Sometimes errors are sent as unsolicited chip state events */ 983 case CMD_CHIP_STATE_EVENT: 984 es.channel = cmd->u.usbcan.chip_state_event.channel; 985 es.status = cmd->u.usbcan.chip_state_event.status; 986 es.txerr = cmd->u.usbcan.chip_state_event.tx_errors_count; 987 es.rxerr = cmd->u.usbcan.chip_state_event.rx_errors_count; 988 kvaser_usb_leaf_usbcan_conditionally_rx_error(dev, &es); 989 break; 990 991 case CMD_CAN_ERROR_EVENT: 992 es.channel = 0; 993 es.status = cmd->u.usbcan.error_event.status_ch0; 994 es.txerr = cmd->u.usbcan.error_event.tx_errors_count_ch0; 995 es.rxerr = cmd->u.usbcan.error_event.rx_errors_count_ch0; 996 es.usbcan.other_ch_status = 997 cmd->u.usbcan.error_event.status_ch1; 998 kvaser_usb_leaf_usbcan_conditionally_rx_error(dev, &es); 999 1000 /* The USBCAN firmware supports up to 2 channels. 1001 * Now that ch0 was checked, check if ch1 has any errors. 1002 */ 1003 if (dev->nchannels == MAX_USBCAN_NET_DEVICES) { 1004 es.channel = 1; 1005 es.status = cmd->u.usbcan.error_event.status_ch1; 1006 es.txerr = 1007 cmd->u.usbcan.error_event.tx_errors_count_ch1; 1008 es.rxerr = 1009 cmd->u.usbcan.error_event.rx_errors_count_ch1; 1010 es.usbcan.other_ch_status = 1011 cmd->u.usbcan.error_event.status_ch0; 1012 kvaser_usb_leaf_usbcan_conditionally_rx_error(dev, &es); 1013 } 1014 break; 1015 1016 default: 1017 dev_err(&dev->intf->dev, "Invalid cmd id (%d)\n", cmd->id); 1018 } 1019 } 1020 1021 static void kvaser_usb_leaf_leaf_rx_error(const struct kvaser_usb *dev, 1022 const struct kvaser_cmd *cmd) 1023 { 1024 struct kvaser_usb_err_summary es = { }; 1025 1026 switch (cmd->id) { 1027 case CMD_CAN_ERROR_EVENT: 1028 es.channel = cmd->u.leaf.error_event.channel; 1029 es.status = cmd->u.leaf.error_event.status; 1030 es.txerr = cmd->u.leaf.error_event.tx_errors_count; 1031 es.rxerr = cmd->u.leaf.error_event.rx_errors_count; 1032 es.leaf.error_factor = cmd->u.leaf.error_event.error_factor; 1033 break; 1034 case CMD_LEAF_LOG_MESSAGE: 1035 es.channel = cmd->u.leaf.log_message.channel; 1036 es.status = cmd->u.leaf.log_message.data[0]; 1037 es.txerr = cmd->u.leaf.log_message.data[2]; 1038 es.rxerr = cmd->u.leaf.log_message.data[3]; 1039 es.leaf.error_factor = cmd->u.leaf.log_message.data[1]; 1040 break; 1041 case CMD_CHIP_STATE_EVENT: 1042 es.channel = cmd->u.leaf.chip_state_event.channel; 1043 es.status = cmd->u.leaf.chip_state_event.status; 1044 es.txerr = cmd->u.leaf.chip_state_event.tx_errors_count; 1045 es.rxerr = cmd->u.leaf.chip_state_event.rx_errors_count; 1046 es.leaf.error_factor = 0; 1047 break; 1048 default: 1049 dev_err(&dev->intf->dev, "Invalid cmd id (%d)\n", cmd->id); 1050 return; 1051 } 1052 1053 kvaser_usb_leaf_rx_error(dev, &es); 1054 } 1055 1056 static void kvaser_usb_leaf_rx_can_err(const struct kvaser_usb_net_priv *priv, 1057 const struct kvaser_cmd *cmd) 1058 { 1059 if (cmd->u.rx_can_header.flag & (MSG_FLAG_ERROR_FRAME | 1060 MSG_FLAG_NERR)) { 1061 struct net_device_stats *stats = &priv->netdev->stats; 1062 1063 netdev_err(priv->netdev, "Unknown error (flags: 0x%02x)\n", 1064 cmd->u.rx_can_header.flag); 1065 1066 stats->rx_errors++; 1067 return; 1068 } 1069 1070 if (cmd->u.rx_can_header.flag & MSG_FLAG_OVERRUN) 1071 kvaser_usb_can_rx_over_error(priv->netdev); 1072 } 1073 1074 static void kvaser_usb_leaf_rx_can_msg(const struct kvaser_usb *dev, 1075 const struct kvaser_cmd *cmd) 1076 { 1077 struct kvaser_usb_net_priv *priv; 1078 struct can_frame *cf; 1079 struct sk_buff *skb; 1080 struct net_device_stats *stats; 1081 u8 channel = cmd->u.rx_can_header.channel; 1082 const u8 *rx_data = NULL; /* GCC */ 1083 1084 if (channel >= dev->nchannels) { 1085 dev_err(&dev->intf->dev, 1086 "Invalid channel number (%d)\n", channel); 1087 return; 1088 } 1089 1090 priv = dev->nets[channel]; 1091 stats = &priv->netdev->stats; 1092 1093 if ((cmd->u.rx_can_header.flag & MSG_FLAG_ERROR_FRAME) && 1094 (dev->driver_info->family == KVASER_LEAF && 1095 cmd->id == CMD_LEAF_LOG_MESSAGE)) { 1096 kvaser_usb_leaf_leaf_rx_error(dev, cmd); 1097 return; 1098 } else if (cmd->u.rx_can_header.flag & (MSG_FLAG_ERROR_FRAME | 1099 MSG_FLAG_NERR | 1100 MSG_FLAG_OVERRUN)) { 1101 kvaser_usb_leaf_rx_can_err(priv, cmd); 1102 return; 1103 } else if (cmd->u.rx_can_header.flag & ~MSG_FLAG_REMOTE_FRAME) { 1104 netdev_warn(priv->netdev, 1105 "Unhandled frame (flags: 0x%02x)\n", 1106 cmd->u.rx_can_header.flag); 1107 return; 1108 } 1109 1110 switch (dev->driver_info->family) { 1111 case KVASER_LEAF: 1112 rx_data = cmd->u.leaf.rx_can.data; 1113 break; 1114 case KVASER_USBCAN: 1115 rx_data = cmd->u.usbcan.rx_can.data; 1116 break; 1117 } 1118 1119 skb = alloc_can_skb(priv->netdev, &cf); 1120 if (!skb) { 1121 stats->rx_dropped++; 1122 return; 1123 } 1124 1125 if (dev->driver_info->family == KVASER_LEAF && cmd->id == 1126 CMD_LEAF_LOG_MESSAGE) { 1127 cf->can_id = le32_to_cpu(cmd->u.leaf.log_message.id); 1128 if (cf->can_id & KVASER_EXTENDED_FRAME) 1129 cf->can_id &= CAN_EFF_MASK | CAN_EFF_FLAG; 1130 else 1131 cf->can_id &= CAN_SFF_MASK; 1132 1133 cf->len = can_cc_dlc2len(cmd->u.leaf.log_message.dlc); 1134 1135 if (cmd->u.leaf.log_message.flags & MSG_FLAG_REMOTE_FRAME) 1136 cf->can_id |= CAN_RTR_FLAG; 1137 else 1138 memcpy(cf->data, &cmd->u.leaf.log_message.data, 1139 cf->len); 1140 } else { 1141 cf->can_id = ((rx_data[0] & 0x1f) << 6) | (rx_data[1] & 0x3f); 1142 1143 if (cmd->id == CMD_RX_EXT_MESSAGE) { 1144 cf->can_id <<= 18; 1145 cf->can_id |= ((rx_data[2] & 0x0f) << 14) | 1146 ((rx_data[3] & 0xff) << 6) | 1147 (rx_data[4] & 0x3f); 1148 cf->can_id |= CAN_EFF_FLAG; 1149 } 1150 1151 cf->len = can_cc_dlc2len(rx_data[5]); 1152 1153 if (cmd->u.rx_can_header.flag & MSG_FLAG_REMOTE_FRAME) 1154 cf->can_id |= CAN_RTR_FLAG; 1155 else 1156 memcpy(cf->data, &rx_data[6], cf->len); 1157 } 1158 1159 stats->rx_packets++; 1160 if (!(cf->can_id & CAN_RTR_FLAG)) 1161 stats->rx_bytes += cf->len; 1162 netif_rx(skb); 1163 } 1164 1165 static void kvaser_usb_leaf_start_chip_reply(const struct kvaser_usb *dev, 1166 const struct kvaser_cmd *cmd) 1167 { 1168 struct kvaser_usb_net_priv *priv; 1169 u8 channel = cmd->u.simple.channel; 1170 1171 if (channel >= dev->nchannels) { 1172 dev_err(&dev->intf->dev, 1173 "Invalid channel number (%d)\n", channel); 1174 return; 1175 } 1176 1177 priv = dev->nets[channel]; 1178 1179 if (completion_done(&priv->start_comp) && 1180 netif_queue_stopped(priv->netdev)) { 1181 netif_wake_queue(priv->netdev); 1182 } else { 1183 netif_start_queue(priv->netdev); 1184 complete(&priv->start_comp); 1185 } 1186 } 1187 1188 static void kvaser_usb_leaf_stop_chip_reply(const struct kvaser_usb *dev, 1189 const struct kvaser_cmd *cmd) 1190 { 1191 struct kvaser_usb_net_priv *priv; 1192 u8 channel = cmd->u.simple.channel; 1193 1194 if (channel >= dev->nchannels) { 1195 dev_err(&dev->intf->dev, 1196 "Invalid channel number (%d)\n", channel); 1197 return; 1198 } 1199 1200 priv = dev->nets[channel]; 1201 1202 complete(&priv->stop_comp); 1203 } 1204 1205 static void kvaser_usb_leaf_handle_command(const struct kvaser_usb *dev, 1206 const struct kvaser_cmd *cmd) 1207 { 1208 if (kvaser_usb_leaf_verify_size(dev, cmd) < 0) 1209 return; 1210 1211 switch (cmd->id) { 1212 case CMD_START_CHIP_REPLY: 1213 kvaser_usb_leaf_start_chip_reply(dev, cmd); 1214 break; 1215 1216 case CMD_STOP_CHIP_REPLY: 1217 kvaser_usb_leaf_stop_chip_reply(dev, cmd); 1218 break; 1219 1220 case CMD_RX_STD_MESSAGE: 1221 case CMD_RX_EXT_MESSAGE: 1222 kvaser_usb_leaf_rx_can_msg(dev, cmd); 1223 break; 1224 1225 case CMD_LEAF_LOG_MESSAGE: 1226 if (dev->driver_info->family != KVASER_LEAF) 1227 goto warn; 1228 kvaser_usb_leaf_rx_can_msg(dev, cmd); 1229 break; 1230 1231 case CMD_CHIP_STATE_EVENT: 1232 case CMD_CAN_ERROR_EVENT: 1233 if (dev->driver_info->family == KVASER_LEAF) 1234 kvaser_usb_leaf_leaf_rx_error(dev, cmd); 1235 else 1236 kvaser_usb_leaf_usbcan_rx_error(dev, cmd); 1237 break; 1238 1239 case CMD_TX_ACKNOWLEDGE: 1240 kvaser_usb_leaf_tx_acknowledge(dev, cmd); 1241 break; 1242 1243 /* Ignored commands */ 1244 case CMD_USBCAN_CLOCK_OVERFLOW_EVENT: 1245 if (dev->driver_info->family != KVASER_USBCAN) 1246 goto warn; 1247 break; 1248 1249 case CMD_FLUSH_QUEUE_REPLY: 1250 if (dev->driver_info->family != KVASER_LEAF) 1251 goto warn; 1252 break; 1253 1254 default: 1255 warn: dev_warn(&dev->intf->dev, "Unhandled command (%d)\n", cmd->id); 1256 break; 1257 } 1258 } 1259 1260 static void kvaser_usb_leaf_read_bulk_callback(struct kvaser_usb *dev, 1261 void *buf, int len) 1262 { 1263 struct kvaser_cmd *cmd; 1264 int pos = 0; 1265 1266 while (pos <= len - CMD_HEADER_LEN) { 1267 cmd = buf + pos; 1268 1269 /* The Kvaser firmware can only read and write commands that 1270 * does not cross the USB's endpoint wMaxPacketSize boundary. 1271 * If a follow-up command crosses such boundary, firmware puts 1272 * a placeholder zero-length command in its place then aligns 1273 * the real command to the next max packet size. 1274 * 1275 * Handle such cases or we're going to miss a significant 1276 * number of events in case of a heavy rx load on the bus. 1277 */ 1278 if (cmd->len == 0) { 1279 pos = round_up(pos, le16_to_cpu 1280 (dev->bulk_in->wMaxPacketSize)); 1281 continue; 1282 } 1283 1284 if (pos + cmd->len > len) { 1285 dev_err_ratelimited(&dev->intf->dev, "Format error\n"); 1286 break; 1287 } 1288 1289 kvaser_usb_leaf_handle_command(dev, cmd); 1290 pos += cmd->len; 1291 } 1292 } 1293 1294 static int kvaser_usb_leaf_set_opt_mode(const struct kvaser_usb_net_priv *priv) 1295 { 1296 struct kvaser_cmd *cmd; 1297 int rc; 1298 1299 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); 1300 if (!cmd) 1301 return -ENOMEM; 1302 1303 cmd->id = CMD_SET_CTRL_MODE; 1304 cmd->len = CMD_HEADER_LEN + sizeof(struct kvaser_cmd_ctrl_mode); 1305 cmd->u.ctrl_mode.tid = 0xff; 1306 cmd->u.ctrl_mode.channel = priv->channel; 1307 1308 if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY) 1309 cmd->u.ctrl_mode.ctrl_mode = KVASER_CTRL_MODE_SILENT; 1310 else 1311 cmd->u.ctrl_mode.ctrl_mode = KVASER_CTRL_MODE_NORMAL; 1312 1313 rc = kvaser_usb_send_cmd(priv->dev, cmd, cmd->len); 1314 1315 kfree(cmd); 1316 return rc; 1317 } 1318 1319 static int kvaser_usb_leaf_start_chip(struct kvaser_usb_net_priv *priv) 1320 { 1321 int err; 1322 1323 init_completion(&priv->start_comp); 1324 1325 err = kvaser_usb_leaf_send_simple_cmd(priv->dev, CMD_START_CHIP, 1326 priv->channel); 1327 if (err) 1328 return err; 1329 1330 if (!wait_for_completion_timeout(&priv->start_comp, 1331 msecs_to_jiffies(KVASER_USB_TIMEOUT))) 1332 return -ETIMEDOUT; 1333 1334 return 0; 1335 } 1336 1337 static int kvaser_usb_leaf_stop_chip(struct kvaser_usb_net_priv *priv) 1338 { 1339 int err; 1340 1341 init_completion(&priv->stop_comp); 1342 1343 err = kvaser_usb_leaf_send_simple_cmd(priv->dev, CMD_STOP_CHIP, 1344 priv->channel); 1345 if (err) 1346 return err; 1347 1348 if (!wait_for_completion_timeout(&priv->stop_comp, 1349 msecs_to_jiffies(KVASER_USB_TIMEOUT))) 1350 return -ETIMEDOUT; 1351 1352 return 0; 1353 } 1354 1355 static int kvaser_usb_leaf_reset_chip(struct kvaser_usb *dev, int channel) 1356 { 1357 return kvaser_usb_leaf_send_simple_cmd(dev, CMD_RESET_CHIP, channel); 1358 } 1359 1360 static int kvaser_usb_leaf_flush_queue(struct kvaser_usb_net_priv *priv) 1361 { 1362 struct kvaser_cmd *cmd; 1363 int rc; 1364 1365 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); 1366 if (!cmd) 1367 return -ENOMEM; 1368 1369 cmd->id = CMD_FLUSH_QUEUE; 1370 cmd->len = CMD_HEADER_LEN + sizeof(struct kvaser_cmd_flush_queue); 1371 cmd->u.flush_queue.channel = priv->channel; 1372 cmd->u.flush_queue.flags = 0x00; 1373 1374 rc = kvaser_usb_send_cmd(priv->dev, cmd, cmd->len); 1375 1376 kfree(cmd); 1377 return rc; 1378 } 1379 1380 static int kvaser_usb_leaf_init_card(struct kvaser_usb *dev) 1381 { 1382 struct kvaser_usb_dev_card_data *card_data = &dev->card_data; 1383 1384 card_data->ctrlmode_supported |= CAN_CTRLMODE_3_SAMPLES; 1385 1386 return 0; 1387 } 1388 1389 static int kvaser_usb_leaf_set_bittiming(struct net_device *netdev) 1390 { 1391 struct kvaser_usb_net_priv *priv = netdev_priv(netdev); 1392 struct can_bittiming *bt = &priv->can.bittiming; 1393 struct kvaser_usb *dev = priv->dev; 1394 struct kvaser_cmd *cmd; 1395 int rc; 1396 1397 cmd = kmalloc(sizeof(*cmd), GFP_KERNEL); 1398 if (!cmd) 1399 return -ENOMEM; 1400 1401 cmd->id = CMD_SET_BUS_PARAMS; 1402 cmd->len = CMD_HEADER_LEN + sizeof(struct kvaser_cmd_busparams); 1403 cmd->u.busparams.channel = priv->channel; 1404 cmd->u.busparams.tid = 0xff; 1405 cmd->u.busparams.bitrate = cpu_to_le32(bt->bitrate); 1406 cmd->u.busparams.sjw = bt->sjw; 1407 cmd->u.busparams.tseg1 = bt->prop_seg + bt->phase_seg1; 1408 cmd->u.busparams.tseg2 = bt->phase_seg2; 1409 1410 if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES) 1411 cmd->u.busparams.no_samp = 3; 1412 else 1413 cmd->u.busparams.no_samp = 1; 1414 1415 rc = kvaser_usb_send_cmd(dev, cmd, cmd->len); 1416 1417 kfree(cmd); 1418 return rc; 1419 } 1420 1421 static int kvaser_usb_leaf_set_mode(struct net_device *netdev, 1422 enum can_mode mode) 1423 { 1424 struct kvaser_usb_net_priv *priv = netdev_priv(netdev); 1425 int err; 1426 1427 switch (mode) { 1428 case CAN_MODE_START: 1429 kvaser_usb_unlink_tx_urbs(priv); 1430 1431 err = kvaser_usb_leaf_simple_cmd_async(priv, CMD_START_CHIP); 1432 if (err) 1433 return err; 1434 1435 priv->can.state = CAN_STATE_ERROR_ACTIVE; 1436 break; 1437 default: 1438 return -EOPNOTSUPP; 1439 } 1440 1441 return 0; 1442 } 1443 1444 static int kvaser_usb_leaf_get_berr_counter(const struct net_device *netdev, 1445 struct can_berr_counter *bec) 1446 { 1447 struct kvaser_usb_net_priv *priv = netdev_priv(netdev); 1448 1449 *bec = priv->bec; 1450 1451 return 0; 1452 } 1453 1454 static int kvaser_usb_leaf_setup_endpoints(struct kvaser_usb *dev) 1455 { 1456 const struct usb_host_interface *iface_desc; 1457 struct usb_endpoint_descriptor *endpoint; 1458 int i; 1459 1460 iface_desc = dev->intf->cur_altsetting; 1461 1462 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) { 1463 endpoint = &iface_desc->endpoint[i].desc; 1464 1465 if (!dev->bulk_in && usb_endpoint_is_bulk_in(endpoint)) 1466 dev->bulk_in = endpoint; 1467 1468 if (!dev->bulk_out && usb_endpoint_is_bulk_out(endpoint)) 1469 dev->bulk_out = endpoint; 1470 1471 /* use first bulk endpoint for in and out */ 1472 if (dev->bulk_in && dev->bulk_out) 1473 return 0; 1474 } 1475 1476 return -ENODEV; 1477 } 1478 1479 const struct kvaser_usb_dev_ops kvaser_usb_leaf_dev_ops = { 1480 .dev_set_mode = kvaser_usb_leaf_set_mode, 1481 .dev_set_bittiming = kvaser_usb_leaf_set_bittiming, 1482 .dev_set_data_bittiming = NULL, 1483 .dev_get_berr_counter = kvaser_usb_leaf_get_berr_counter, 1484 .dev_setup_endpoints = kvaser_usb_leaf_setup_endpoints, 1485 .dev_init_card = kvaser_usb_leaf_init_card, 1486 .dev_get_software_info = kvaser_usb_leaf_get_software_info, 1487 .dev_get_software_details = NULL, 1488 .dev_get_card_info = kvaser_usb_leaf_get_card_info, 1489 .dev_get_capabilities = NULL, 1490 .dev_set_opt_mode = kvaser_usb_leaf_set_opt_mode, 1491 .dev_start_chip = kvaser_usb_leaf_start_chip, 1492 .dev_stop_chip = kvaser_usb_leaf_stop_chip, 1493 .dev_reset_chip = kvaser_usb_leaf_reset_chip, 1494 .dev_flush_queue = kvaser_usb_leaf_flush_queue, 1495 .dev_read_bulk_callback = kvaser_usb_leaf_read_bulk_callback, 1496 .dev_frame_to_cmd = kvaser_usb_leaf_frame_to_cmd, 1497 }; 1498