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