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