1 // SPDX-License-Identifier: GPL-2.0 2 /* Parts of this driver are based on the following: 3 * - Kvaser linux mhydra driver (version 5.24) 4 * - CAN driver for esd CAN-USB/2 5 * 6 * Copyright (C) 2018 KVASER AB, Sweden. All rights reserved. 7 * Copyright (C) 2010 Matthias Fuchs <matthias.fuchs@esd.eu>, esd gmbh 8 * 9 * Known issues: 10 * - Transition from CAN_STATE_ERROR_WARNING to CAN_STATE_ERROR_ACTIVE is only 11 * reported after a call to do_get_berr_counter(), since firmware does not 12 * distinguish between ERROR_WARNING and ERROR_ACTIVE. 13 * - Hardware timestamps are not set for CAN Tx frames. 14 */ 15 16 #include <linux/completion.h> 17 #include <linux/device.h> 18 #include <linux/gfp.h> 19 #include <linux/jiffies.h> 20 #include <linux/kernel.h> 21 #include <linux/netdevice.h> 22 #include <linux/spinlock.h> 23 #include <linux/string.h> 24 #include <linux/types.h> 25 #include <linux/units.h> 26 #include <linux/usb.h> 27 28 #include <linux/can.h> 29 #include <linux/can/dev.h> 30 #include <linux/can/error.h> 31 #include <linux/can/netlink.h> 32 33 #include "kvaser_usb.h" 34 35 /* Forward declarations */ 36 static const struct kvaser_usb_dev_cfg kvaser_usb_hydra_dev_cfg_kcan; 37 static const struct kvaser_usb_dev_cfg kvaser_usb_hydra_dev_cfg_flexc; 38 static const struct kvaser_usb_dev_cfg kvaser_usb_hydra_dev_cfg_rt; 39 40 #define KVASER_USB_HYDRA_BULK_EP_IN_ADDR 0x82 41 #define KVASER_USB_HYDRA_BULK_EP_OUT_ADDR 0x02 42 43 #define KVASER_USB_HYDRA_MAX_TRANSID 0xff 44 #define KVASER_USB_HYDRA_MIN_TRANSID 0x01 45 46 /* Minihydra command IDs */ 47 #define CMD_SET_BUSPARAMS_REQ 16 48 #define CMD_GET_BUSPARAMS_REQ 17 49 #define CMD_GET_BUSPARAMS_RESP 18 50 #define CMD_GET_CHIP_STATE_REQ 19 51 #define CMD_CHIP_STATE_EVENT 20 52 #define CMD_SET_DRIVERMODE_REQ 21 53 #define CMD_START_CHIP_REQ 26 54 #define CMD_START_CHIP_RESP 27 55 #define CMD_STOP_CHIP_REQ 28 56 #define CMD_STOP_CHIP_RESP 29 57 #define CMD_TX_CAN_MESSAGE 33 58 #define CMD_GET_CARD_INFO_REQ 34 59 #define CMD_GET_CARD_INFO_RESP 35 60 #define CMD_GET_SOFTWARE_INFO_REQ 38 61 #define CMD_GET_SOFTWARE_INFO_RESP 39 62 #define CMD_ERROR_EVENT 45 63 #define CMD_FLUSH_QUEUE 48 64 #define CMD_TX_ACKNOWLEDGE 50 65 #define CMD_FLUSH_QUEUE_RESP 66 66 #define CMD_SET_BUSPARAMS_FD_REQ 69 67 #define CMD_SET_BUSPARAMS_FD_RESP 70 68 #define CMD_SET_BUSPARAMS_RESP 85 69 #define CMD_GET_CAPABILITIES_REQ 95 70 #define CMD_GET_CAPABILITIES_RESP 96 71 #define CMD_RX_MESSAGE 106 72 #define CMD_MAP_CHANNEL_REQ 200 73 #define CMD_MAP_CHANNEL_RESP 201 74 #define CMD_GET_SOFTWARE_DETAILS_REQ 202 75 #define CMD_GET_SOFTWARE_DETAILS_RESP 203 76 #define CMD_EXTENDED 255 77 78 /* Minihydra extended command IDs */ 79 #define CMD_TX_CAN_MESSAGE_FD 224 80 #define CMD_TX_ACKNOWLEDGE_FD 225 81 #define CMD_RX_MESSAGE_FD 226 82 83 /* Hydra commands are handled by different threads in firmware. 84 * The threads are denoted hydra entity (HE). Each HE got a unique 6-bit 85 * address. The address is used in hydra commands to get/set source and 86 * destination HE. There are two predefined HE addresses, the remaining 87 * addresses are different between devices and firmware versions. Hence, we need 88 * to enumerate the addresses (see kvaser_usb_hydra_map_channel()). 89 */ 90 91 /* Well-known HE addresses */ 92 #define KVASER_USB_HYDRA_HE_ADDRESS_ROUTER 0x00 93 #define KVASER_USB_HYDRA_HE_ADDRESS_ILLEGAL 0x3e 94 95 #define KVASER_USB_HYDRA_TRANSID_CANHE 0x40 96 #define KVASER_USB_HYDRA_TRANSID_SYSDBG 0x61 97 98 struct kvaser_cmd_map_ch_req { 99 char name[16]; 100 u8 channel; 101 u8 reserved[11]; 102 } __packed; 103 104 struct kvaser_cmd_map_ch_res { 105 u8 he_addr; 106 u8 channel; 107 u8 reserved[26]; 108 } __packed; 109 110 struct kvaser_cmd_card_info { 111 __le32 serial_number; 112 __le32 clock_res; 113 __le32 mfg_date; 114 __le32 ean[2]; 115 u8 hw_version; 116 u8 usb_mode; 117 u8 hw_type; 118 u8 reserved0; 119 u8 nchannels; 120 u8 reserved1[3]; 121 } __packed; 122 123 struct kvaser_cmd_sw_info { 124 u8 reserved0[8]; 125 __le16 max_outstanding_tx; 126 u8 reserved1[18]; 127 } __packed; 128 129 struct kvaser_cmd_sw_detail_req { 130 u8 use_ext_cmd; 131 u8 reserved[27]; 132 } __packed; 133 134 /* Software detail flags */ 135 #define KVASER_USB_HYDRA_SW_FLAG_FW_BETA BIT(2) 136 #define KVASER_USB_HYDRA_SW_FLAG_FW_BAD BIT(4) 137 #define KVASER_USB_HYDRA_SW_FLAG_FREQ_80M BIT(5) 138 #define KVASER_USB_HYDRA_SW_FLAG_EXT_CMD BIT(9) 139 #define KVASER_USB_HYDRA_SW_FLAG_CANFD BIT(10) 140 #define KVASER_USB_HYDRA_SW_FLAG_NONISO BIT(11) 141 #define KVASER_USB_HYDRA_SW_FLAG_EXT_CAP BIT(12) 142 #define KVASER_USB_HYDRA_SW_FLAG_CAN_FREQ_80M BIT(13) 143 struct kvaser_cmd_sw_detail_res { 144 __le32 sw_flags; 145 __le32 sw_version; 146 __le32 sw_name; 147 __le32 ean[2]; 148 __le32 max_bitrate; 149 u8 reserved[4]; 150 } __packed; 151 152 /* Sub commands for cap_req and cap_res */ 153 #define KVASER_USB_HYDRA_CAP_CMD_LISTEN_MODE 0x02 154 #define KVASER_USB_HYDRA_CAP_CMD_ERR_REPORT 0x05 155 #define KVASER_USB_HYDRA_CAP_CMD_ONE_SHOT 0x06 156 struct kvaser_cmd_cap_req { 157 __le16 cap_cmd; 158 u8 reserved[26]; 159 } __packed; 160 161 /* Status codes for cap_res */ 162 #define KVASER_USB_HYDRA_CAP_STAT_OK 0x00 163 #define KVASER_USB_HYDRA_CAP_STAT_NOT_IMPL 0x01 164 #define KVASER_USB_HYDRA_CAP_STAT_UNAVAIL 0x02 165 struct kvaser_cmd_cap_res { 166 __le16 cap_cmd; 167 __le16 status; 168 __le32 mask; 169 __le32 value; 170 u8 reserved[16]; 171 } __packed; 172 173 /* CMD_ERROR_EVENT error codes */ 174 #define KVASER_USB_HYDRA_ERROR_EVENT_CAN 0x01 175 #define KVASER_USB_HYDRA_ERROR_EVENT_PARAM 0x09 176 struct kvaser_cmd_error_event { 177 __le16 timestamp[3]; 178 u8 reserved; 179 u8 error_code; 180 __le16 info1; 181 __le16 info2; 182 } __packed; 183 184 /* Chip state status flags. Used for chip_state_event and err_frame_data. */ 185 #define KVASER_USB_HYDRA_BUS_ERR_ACT 0x00 186 #define KVASER_USB_HYDRA_BUS_ERR_PASS BIT(5) 187 #define KVASER_USB_HYDRA_BUS_BUS_OFF BIT(6) 188 struct kvaser_cmd_chip_state_event { 189 __le16 timestamp[3]; 190 u8 tx_err_counter; 191 u8 rx_err_counter; 192 u8 bus_status; 193 u8 reserved[19]; 194 } __packed; 195 196 /* Busparam modes */ 197 #define KVASER_USB_HYDRA_BUS_MODE_CAN 0x00 198 #define KVASER_USB_HYDRA_BUS_MODE_CANFD_ISO 0x01 199 #define KVASER_USB_HYDRA_BUS_MODE_NONISO 0x02 200 struct kvaser_cmd_set_busparams { 201 struct kvaser_usb_busparams busparams_nominal; 202 u8 reserved0[4]; 203 struct kvaser_usb_busparams busparams_data; 204 u8 canfd_mode; 205 u8 reserved1[7]; 206 } __packed; 207 208 /* Busparam type */ 209 #define KVASER_USB_HYDRA_BUSPARAM_TYPE_CAN 0x00 210 #define KVASER_USB_HYDRA_BUSPARAM_TYPE_CANFD 0x01 211 struct kvaser_cmd_get_busparams_req { 212 u8 type; 213 u8 reserved[27]; 214 } __packed; 215 216 struct kvaser_cmd_get_busparams_res { 217 struct kvaser_usb_busparams busparams; 218 u8 reserved[20]; 219 } __packed; 220 221 /* Ctrl modes */ 222 #define KVASER_USB_HYDRA_CTRLMODE_NORMAL 0x01 223 #define KVASER_USB_HYDRA_CTRLMODE_LISTEN 0x02 224 struct kvaser_cmd_set_ctrlmode { 225 u8 mode; 226 u8 reserved[27]; 227 } __packed; 228 229 struct kvaser_err_frame_data { 230 u8 bus_status; 231 u8 reserved0; 232 u8 tx_err_counter; 233 u8 rx_err_counter; 234 u8 reserved1[4]; 235 } __packed; 236 237 struct kvaser_cmd_rx_can { 238 u8 cmd_len; 239 u8 cmd_no; 240 u8 channel; 241 u8 flags; 242 __le16 timestamp[3]; 243 u8 dlc; 244 u8 padding; 245 __le32 id; 246 union { 247 u8 data[8]; 248 struct kvaser_err_frame_data err_frame_data; 249 }; 250 } __packed; 251 252 /* Extended CAN ID flag. Used in rx_can and tx_can */ 253 #define KVASER_USB_HYDRA_EXTENDED_FRAME_ID BIT(31) 254 struct kvaser_cmd_tx_can { 255 __le32 id; 256 u8 data[8]; 257 u8 dlc; 258 u8 flags; 259 __le16 transid; 260 u8 channel; 261 u8 reserved[11]; 262 } __packed; 263 264 struct kvaser_cmd_header { 265 u8 cmd_no; 266 /* The destination HE address is stored in 0..5 of he_addr. 267 * The upper part of source HE address is stored in 6..7 of he_addr, and 268 * the lower part is stored in 12..15 of transid. 269 */ 270 u8 he_addr; 271 __le16 transid; 272 } __packed; 273 274 struct kvaser_cmd { 275 struct kvaser_cmd_header header; 276 union { 277 struct kvaser_cmd_map_ch_req map_ch_req; 278 struct kvaser_cmd_map_ch_res map_ch_res; 279 280 struct kvaser_cmd_card_info card_info; 281 struct kvaser_cmd_sw_info sw_info; 282 struct kvaser_cmd_sw_detail_req sw_detail_req; 283 struct kvaser_cmd_sw_detail_res sw_detail_res; 284 285 struct kvaser_cmd_cap_req cap_req; 286 struct kvaser_cmd_cap_res cap_res; 287 288 struct kvaser_cmd_error_event error_event; 289 290 struct kvaser_cmd_set_busparams set_busparams_req; 291 struct kvaser_cmd_get_busparams_req get_busparams_req; 292 struct kvaser_cmd_get_busparams_res get_busparams_res; 293 294 struct kvaser_cmd_chip_state_event chip_state_event; 295 296 struct kvaser_cmd_set_ctrlmode set_ctrlmode; 297 298 struct kvaser_cmd_rx_can rx_can; 299 struct kvaser_cmd_tx_can tx_can; 300 } __packed; 301 } __packed; 302 303 /* CAN frame flags. Used in rx_can, ext_rx_can, tx_can and ext_tx_can */ 304 #define KVASER_USB_HYDRA_CF_FLAG_ERROR_FRAME BIT(0) 305 #define KVASER_USB_HYDRA_CF_FLAG_OVERRUN BIT(1) 306 #define KVASER_USB_HYDRA_CF_FLAG_REMOTE_FRAME BIT(4) 307 #define KVASER_USB_HYDRA_CF_FLAG_EXTENDED_ID BIT(5) 308 #define KVASER_USB_HYDRA_CF_FLAG_TX_ACK BIT(6) 309 /* CAN frame flags. Used in ext_rx_can and ext_tx_can */ 310 #define KVASER_USB_HYDRA_CF_FLAG_OSM_NACK BIT(12) 311 #define KVASER_USB_HYDRA_CF_FLAG_ABL BIT(13) 312 #define KVASER_USB_HYDRA_CF_FLAG_FDF BIT(16) 313 #define KVASER_USB_HYDRA_CF_FLAG_BRS BIT(17) 314 #define KVASER_USB_HYDRA_CF_FLAG_ESI BIT(18) 315 316 /* KCAN packet header macros. Used in ext_rx_can and ext_tx_can */ 317 #define KVASER_USB_KCAN_DATA_DLC_BITS 4 318 #define KVASER_USB_KCAN_DATA_DLC_SHIFT 8 319 #define KVASER_USB_KCAN_DATA_DLC_MASK \ 320 GENMASK(KVASER_USB_KCAN_DATA_DLC_BITS - 1 + \ 321 KVASER_USB_KCAN_DATA_DLC_SHIFT, \ 322 KVASER_USB_KCAN_DATA_DLC_SHIFT) 323 324 #define KVASER_USB_KCAN_DATA_BRS BIT(14) 325 #define KVASER_USB_KCAN_DATA_FDF BIT(15) 326 #define KVASER_USB_KCAN_DATA_OSM BIT(16) 327 #define KVASER_USB_KCAN_DATA_AREQ BIT(31) 328 #define KVASER_USB_KCAN_DATA_SRR BIT(31) 329 #define KVASER_USB_KCAN_DATA_RTR BIT(29) 330 #define KVASER_USB_KCAN_DATA_IDE BIT(30) 331 struct kvaser_cmd_ext_rx_can { 332 __le32 flags; 333 __le32 id; 334 __le32 kcan_id; 335 __le32 kcan_header; 336 __le64 timestamp; 337 union { 338 u8 kcan_payload[64]; 339 struct kvaser_err_frame_data err_frame_data; 340 }; 341 } __packed; 342 343 struct kvaser_cmd_ext_tx_can { 344 __le32 flags; 345 __le32 id; 346 __le32 kcan_id; 347 __le32 kcan_header; 348 u8 databytes; 349 u8 dlc; 350 u8 reserved[6]; 351 u8 kcan_payload[64]; 352 } __packed; 353 354 struct kvaser_cmd_ext_tx_ack { 355 __le32 flags; 356 u8 reserved0[4]; 357 __le64 timestamp; 358 u8 reserved1[8]; 359 } __packed; 360 361 /* struct for extended commands (CMD_EXTENDED) */ 362 struct kvaser_cmd_ext { 363 struct kvaser_cmd_header header; 364 __le16 len; 365 u8 cmd_no_ext; 366 u8 reserved; 367 368 union { 369 struct kvaser_cmd_ext_rx_can rx_can; 370 struct kvaser_cmd_ext_tx_can tx_can; 371 struct kvaser_cmd_ext_tx_ack tx_ack; 372 } __packed; 373 } __packed; 374 375 struct kvaser_usb_net_hydra_priv { 376 int pending_get_busparams_type; 377 }; 378 379 static const struct can_bittiming_const kvaser_usb_hydra_kcan_bittiming_c = { 380 .name = "kvaser_usb_kcan", 381 .tseg1_min = 1, 382 .tseg1_max = 255, 383 .tseg2_min = 1, 384 .tseg2_max = 32, 385 .sjw_max = 16, 386 .brp_min = 1, 387 .brp_max = 8192, 388 .brp_inc = 1, 389 }; 390 391 const struct can_bittiming_const kvaser_usb_flexc_bittiming_const = { 392 .name = "kvaser_usb_flex", 393 .tseg1_min = 4, 394 .tseg1_max = 16, 395 .tseg2_min = 2, 396 .tseg2_max = 8, 397 .sjw_max = 4, 398 .brp_min = 1, 399 .brp_max = 256, 400 .brp_inc = 1, 401 }; 402 403 static const struct can_bittiming_const kvaser_usb_hydra_rt_bittiming_c = { 404 .name = "kvaser_usb_rt", 405 .tseg1_min = 2, 406 .tseg1_max = 96, 407 .tseg2_min = 2, 408 .tseg2_max = 32, 409 .sjw_max = 32, 410 .brp_min = 1, 411 .brp_max = 1024, 412 .brp_inc = 1, 413 }; 414 415 static const struct can_bittiming_const kvaser_usb_hydra_rtd_bittiming_c = { 416 .name = "kvaser_usb_rt", 417 .tseg1_min = 2, 418 .tseg1_max = 39, 419 .tseg2_min = 2, 420 .tseg2_max = 8, 421 .sjw_max = 8, 422 .brp_min = 1, 423 .brp_max = 1024, 424 .brp_inc = 1, 425 }; 426 427 #define KVASER_USB_HYDRA_TRANSID_BITS 12 428 #define KVASER_USB_HYDRA_TRANSID_MASK \ 429 GENMASK(KVASER_USB_HYDRA_TRANSID_BITS - 1, 0) 430 #define KVASER_USB_HYDRA_HE_ADDR_SRC_MASK GENMASK(7, 6) 431 #define KVASER_USB_HYDRA_HE_ADDR_DEST_MASK GENMASK(5, 0) 432 #define KVASER_USB_HYDRA_HE_ADDR_SRC_BITS 2 433 static inline u16 kvaser_usb_hydra_get_cmd_transid(const struct kvaser_cmd *cmd) 434 { 435 return le16_to_cpu(cmd->header.transid) & KVASER_USB_HYDRA_TRANSID_MASK; 436 } 437 438 static inline void kvaser_usb_hydra_set_cmd_transid(struct kvaser_cmd *cmd, 439 u16 transid) 440 { 441 cmd->header.transid = 442 cpu_to_le16(transid & KVASER_USB_HYDRA_TRANSID_MASK); 443 } 444 445 static inline u8 kvaser_usb_hydra_get_cmd_src_he(const struct kvaser_cmd *cmd) 446 { 447 return (cmd->header.he_addr & KVASER_USB_HYDRA_HE_ADDR_SRC_MASK) >> 448 KVASER_USB_HYDRA_HE_ADDR_SRC_BITS | 449 le16_to_cpu(cmd->header.transid) >> 450 KVASER_USB_HYDRA_TRANSID_BITS; 451 } 452 453 static inline void kvaser_usb_hydra_set_cmd_dest_he(struct kvaser_cmd *cmd, 454 u8 dest_he) 455 { 456 cmd->header.he_addr = 457 (cmd->header.he_addr & KVASER_USB_HYDRA_HE_ADDR_SRC_MASK) | 458 (dest_he & KVASER_USB_HYDRA_HE_ADDR_DEST_MASK); 459 } 460 461 static u8 kvaser_usb_hydra_channel_from_cmd(const struct kvaser_usb *dev, 462 const struct kvaser_cmd *cmd) 463 { 464 int i; 465 u8 channel = 0xff; 466 u8 src_he = kvaser_usb_hydra_get_cmd_src_he(cmd); 467 468 for (i = 0; i < KVASER_USB_MAX_NET_DEVICES; i++) { 469 if (dev->card_data.hydra.channel_to_he[i] == src_he) { 470 channel = i; 471 break; 472 } 473 } 474 475 return channel; 476 } 477 478 static u16 kvaser_usb_hydra_get_next_transid(struct kvaser_usb *dev) 479 { 480 unsigned long flags; 481 u16 transid; 482 struct kvaser_usb_dev_card_data_hydra *card_data = 483 &dev->card_data.hydra; 484 485 spin_lock_irqsave(&card_data->transid_lock, flags); 486 transid = card_data->transid; 487 if (transid >= KVASER_USB_HYDRA_MAX_TRANSID) 488 transid = KVASER_USB_HYDRA_MIN_TRANSID; 489 else 490 transid++; 491 card_data->transid = transid; 492 spin_unlock_irqrestore(&card_data->transid_lock, flags); 493 494 return transid; 495 } 496 497 static size_t kvaser_usb_hydra_cmd_size(struct kvaser_cmd *cmd) 498 { 499 size_t ret; 500 501 if (cmd->header.cmd_no == CMD_EXTENDED) 502 ret = le16_to_cpu(((struct kvaser_cmd_ext *)cmd)->len); 503 else 504 ret = sizeof(struct kvaser_cmd); 505 506 return ret; 507 } 508 509 static struct kvaser_usb_net_priv * 510 kvaser_usb_hydra_net_priv_from_cmd(const struct kvaser_usb *dev, 511 const struct kvaser_cmd *cmd) 512 { 513 struct kvaser_usb_net_priv *priv = NULL; 514 u8 channel = kvaser_usb_hydra_channel_from_cmd(dev, cmd); 515 516 if (channel >= dev->nchannels) 517 dev_err(&dev->intf->dev, 518 "Invalid channel number (%d)\n", channel); 519 else 520 priv = dev->nets[channel]; 521 522 return priv; 523 } 524 525 static ktime_t 526 kvaser_usb_hydra_ktime_from_rx_cmd(const struct kvaser_usb_dev_cfg *cfg, 527 const struct kvaser_cmd *cmd) 528 { 529 u64 ticks; 530 531 if (cmd->header.cmd_no == CMD_EXTENDED) { 532 struct kvaser_cmd_ext *cmd_ext = (struct kvaser_cmd_ext *)cmd; 533 534 ticks = le64_to_cpu(cmd_ext->rx_can.timestamp); 535 } else { 536 ticks = le16_to_cpu(cmd->rx_can.timestamp[0]); 537 ticks += (u64)(le16_to_cpu(cmd->rx_can.timestamp[1])) << 16; 538 ticks += (u64)(le16_to_cpu(cmd->rx_can.timestamp[2])) << 32; 539 } 540 541 return ns_to_ktime(div_u64(ticks * 1000, cfg->timestamp_freq)); 542 } 543 544 static int kvaser_usb_hydra_send_simple_cmd(struct kvaser_usb *dev, 545 u8 cmd_no, int channel) 546 { 547 struct kvaser_cmd *cmd; 548 size_t cmd_len; 549 int err; 550 551 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); 552 if (!cmd) 553 return -ENOMEM; 554 555 cmd->header.cmd_no = cmd_no; 556 cmd_len = kvaser_usb_hydra_cmd_size(cmd); 557 if (channel < 0) { 558 kvaser_usb_hydra_set_cmd_dest_he 559 (cmd, KVASER_USB_HYDRA_HE_ADDRESS_ILLEGAL); 560 } else { 561 if (channel >= KVASER_USB_MAX_NET_DEVICES) { 562 dev_err(&dev->intf->dev, "channel (%d) out of range.\n", 563 channel); 564 err = -EINVAL; 565 goto end; 566 } 567 kvaser_usb_hydra_set_cmd_dest_he 568 (cmd, dev->card_data.hydra.channel_to_he[channel]); 569 } 570 kvaser_usb_hydra_set_cmd_transid 571 (cmd, kvaser_usb_hydra_get_next_transid(dev)); 572 573 err = kvaser_usb_send_cmd(dev, cmd, cmd_len); 574 if (err) 575 goto end; 576 577 end: 578 kfree(cmd); 579 580 return err; 581 } 582 583 static int 584 kvaser_usb_hydra_send_simple_cmd_async(struct kvaser_usb_net_priv *priv, 585 u8 cmd_no) 586 { 587 struct kvaser_cmd *cmd; 588 struct kvaser_usb *dev = priv->dev; 589 size_t cmd_len; 590 int err; 591 592 cmd = kzalloc(sizeof(*cmd), GFP_ATOMIC); 593 if (!cmd) 594 return -ENOMEM; 595 596 cmd->header.cmd_no = cmd_no; 597 cmd_len = kvaser_usb_hydra_cmd_size(cmd); 598 599 kvaser_usb_hydra_set_cmd_dest_he 600 (cmd, dev->card_data.hydra.channel_to_he[priv->channel]); 601 kvaser_usb_hydra_set_cmd_transid 602 (cmd, kvaser_usb_hydra_get_next_transid(dev)); 603 604 err = kvaser_usb_send_cmd_async(priv, cmd, cmd_len); 605 if (err) 606 kfree(cmd); 607 608 return err; 609 } 610 611 /* This function is used for synchronously waiting on hydra control commands. 612 * Note: Compared to kvaser_usb_hydra_read_bulk_callback(), we never need to 613 * handle partial hydra commands. Since hydra control commands are always 614 * non-extended commands. 615 */ 616 static int kvaser_usb_hydra_wait_cmd(const struct kvaser_usb *dev, u8 cmd_no, 617 struct kvaser_cmd *cmd) 618 { 619 void *buf; 620 int err; 621 unsigned long timeout = jiffies + msecs_to_jiffies(KVASER_USB_TIMEOUT); 622 623 if (cmd->header.cmd_no == CMD_EXTENDED) { 624 dev_err(&dev->intf->dev, "Wait for CMD_EXTENDED not allowed\n"); 625 return -EINVAL; 626 } 627 628 buf = kzalloc(KVASER_USB_RX_BUFFER_SIZE, GFP_KERNEL); 629 if (!buf) 630 return -ENOMEM; 631 632 do { 633 int actual_len = 0; 634 int pos = 0; 635 636 err = kvaser_usb_recv_cmd(dev, buf, KVASER_USB_RX_BUFFER_SIZE, 637 &actual_len); 638 if (err < 0) 639 goto end; 640 641 while (pos < actual_len) { 642 struct kvaser_cmd *tmp_cmd; 643 size_t cmd_len; 644 645 tmp_cmd = buf + pos; 646 cmd_len = kvaser_usb_hydra_cmd_size(tmp_cmd); 647 if (pos + cmd_len > actual_len) { 648 dev_err_ratelimited(&dev->intf->dev, 649 "Format error\n"); 650 break; 651 } 652 653 if (tmp_cmd->header.cmd_no == cmd_no) { 654 memcpy(cmd, tmp_cmd, cmd_len); 655 goto end; 656 } 657 pos += cmd_len; 658 } 659 } while (time_before(jiffies, timeout)); 660 661 err = -EINVAL; 662 663 end: 664 kfree(buf); 665 666 return err; 667 } 668 669 static int kvaser_usb_hydra_map_channel_resp(struct kvaser_usb *dev, 670 const struct kvaser_cmd *cmd) 671 { 672 u8 he, channel; 673 u16 transid = kvaser_usb_hydra_get_cmd_transid(cmd); 674 struct kvaser_usb_dev_card_data_hydra *card_data = 675 &dev->card_data.hydra; 676 677 if (transid > 0x007f || transid < 0x0040) { 678 dev_err(&dev->intf->dev, 679 "CMD_MAP_CHANNEL_RESP, invalid transid: 0x%x\n", 680 transid); 681 return -EINVAL; 682 } 683 684 switch (transid) { 685 case KVASER_USB_HYDRA_TRANSID_CANHE: 686 case KVASER_USB_HYDRA_TRANSID_CANHE + 1: 687 case KVASER_USB_HYDRA_TRANSID_CANHE + 2: 688 case KVASER_USB_HYDRA_TRANSID_CANHE + 3: 689 case KVASER_USB_HYDRA_TRANSID_CANHE + 4: 690 channel = transid & 0x000f; 691 he = cmd->map_ch_res.he_addr; 692 card_data->channel_to_he[channel] = he; 693 break; 694 case KVASER_USB_HYDRA_TRANSID_SYSDBG: 695 card_data->sysdbg_he = cmd->map_ch_res.he_addr; 696 break; 697 default: 698 dev_warn(&dev->intf->dev, 699 "Unknown CMD_MAP_CHANNEL_RESP transid=0x%x\n", 700 transid); 701 break; 702 } 703 704 return 0; 705 } 706 707 static int kvaser_usb_hydra_map_channel(struct kvaser_usb *dev, u16 transid, 708 u8 channel, const char *name) 709 { 710 struct kvaser_cmd *cmd; 711 int err; 712 713 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); 714 if (!cmd) 715 return -ENOMEM; 716 717 strcpy(cmd->map_ch_req.name, name); 718 cmd->header.cmd_no = CMD_MAP_CHANNEL_REQ; 719 kvaser_usb_hydra_set_cmd_dest_he 720 (cmd, KVASER_USB_HYDRA_HE_ADDRESS_ROUTER); 721 cmd->map_ch_req.channel = channel; 722 723 kvaser_usb_hydra_set_cmd_transid(cmd, transid); 724 725 err = kvaser_usb_send_cmd(dev, cmd, kvaser_usb_hydra_cmd_size(cmd)); 726 if (err) 727 goto end; 728 729 err = kvaser_usb_hydra_wait_cmd(dev, CMD_MAP_CHANNEL_RESP, cmd); 730 if (err) 731 goto end; 732 733 err = kvaser_usb_hydra_map_channel_resp(dev, cmd); 734 if (err) 735 goto end; 736 737 end: 738 kfree(cmd); 739 740 return err; 741 } 742 743 static int kvaser_usb_hydra_get_single_capability(struct kvaser_usb *dev, 744 u16 cap_cmd_req, u16 *status) 745 { 746 struct kvaser_usb_dev_card_data *card_data = &dev->card_data; 747 struct kvaser_cmd *cmd; 748 size_t cmd_len; 749 u32 value = 0; 750 u32 mask = 0; 751 u16 cap_cmd_res; 752 int err; 753 int i; 754 755 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); 756 if (!cmd) 757 return -ENOMEM; 758 759 cmd->header.cmd_no = CMD_GET_CAPABILITIES_REQ; 760 cmd_len = kvaser_usb_hydra_cmd_size(cmd); 761 cmd->cap_req.cap_cmd = cpu_to_le16(cap_cmd_req); 762 763 kvaser_usb_hydra_set_cmd_dest_he(cmd, card_data->hydra.sysdbg_he); 764 kvaser_usb_hydra_set_cmd_transid 765 (cmd, kvaser_usb_hydra_get_next_transid(dev)); 766 767 err = kvaser_usb_send_cmd(dev, cmd, cmd_len); 768 if (err) 769 goto end; 770 771 err = kvaser_usb_hydra_wait_cmd(dev, CMD_GET_CAPABILITIES_RESP, cmd); 772 if (err) 773 goto end; 774 775 *status = le16_to_cpu(cmd->cap_res.status); 776 777 if (*status != KVASER_USB_HYDRA_CAP_STAT_OK) 778 goto end; 779 780 cap_cmd_res = le16_to_cpu(cmd->cap_res.cap_cmd); 781 switch (cap_cmd_res) { 782 case KVASER_USB_HYDRA_CAP_CMD_LISTEN_MODE: 783 case KVASER_USB_HYDRA_CAP_CMD_ERR_REPORT: 784 case KVASER_USB_HYDRA_CAP_CMD_ONE_SHOT: 785 value = le32_to_cpu(cmd->cap_res.value); 786 mask = le32_to_cpu(cmd->cap_res.mask); 787 break; 788 default: 789 dev_warn(&dev->intf->dev, "Unknown capability command %u\n", 790 cap_cmd_res); 791 break; 792 } 793 794 for (i = 0; i < dev->nchannels; i++) { 795 if (BIT(i) & (value & mask)) { 796 switch (cap_cmd_res) { 797 case KVASER_USB_HYDRA_CAP_CMD_LISTEN_MODE: 798 card_data->ctrlmode_supported |= 799 CAN_CTRLMODE_LISTENONLY; 800 break; 801 case KVASER_USB_HYDRA_CAP_CMD_ERR_REPORT: 802 card_data->capabilities |= 803 KVASER_USB_CAP_BERR_CAP; 804 break; 805 case KVASER_USB_HYDRA_CAP_CMD_ONE_SHOT: 806 card_data->ctrlmode_supported |= 807 CAN_CTRLMODE_ONE_SHOT; 808 break; 809 } 810 } 811 } 812 813 end: 814 kfree(cmd); 815 816 return err; 817 } 818 819 static void kvaser_usb_hydra_start_chip_reply(const struct kvaser_usb *dev, 820 const struct kvaser_cmd *cmd) 821 { 822 struct kvaser_usb_net_priv *priv; 823 824 priv = kvaser_usb_hydra_net_priv_from_cmd(dev, cmd); 825 if (!priv) 826 return; 827 828 if (completion_done(&priv->start_comp) && 829 netif_queue_stopped(priv->netdev)) { 830 netif_wake_queue(priv->netdev); 831 } else { 832 netif_start_queue(priv->netdev); 833 complete(&priv->start_comp); 834 } 835 } 836 837 static void kvaser_usb_hydra_stop_chip_reply(const struct kvaser_usb *dev, 838 const struct kvaser_cmd *cmd) 839 { 840 struct kvaser_usb_net_priv *priv; 841 842 priv = kvaser_usb_hydra_net_priv_from_cmd(dev, cmd); 843 if (!priv) 844 return; 845 846 complete(&priv->stop_comp); 847 } 848 849 static void kvaser_usb_hydra_flush_queue_reply(const struct kvaser_usb *dev, 850 const struct kvaser_cmd *cmd) 851 { 852 struct kvaser_usb_net_priv *priv; 853 854 priv = kvaser_usb_hydra_net_priv_from_cmd(dev, cmd); 855 if (!priv) 856 return; 857 858 complete(&priv->flush_comp); 859 } 860 861 static void kvaser_usb_hydra_get_busparams_reply(const struct kvaser_usb *dev, 862 const struct kvaser_cmd *cmd) 863 { 864 struct kvaser_usb_net_priv *priv; 865 struct kvaser_usb_net_hydra_priv *hydra; 866 867 priv = kvaser_usb_hydra_net_priv_from_cmd(dev, cmd); 868 if (!priv) 869 return; 870 871 hydra = priv->sub_priv; 872 if (!hydra) 873 return; 874 875 switch (hydra->pending_get_busparams_type) { 876 case KVASER_USB_HYDRA_BUSPARAM_TYPE_CAN: 877 memcpy(&priv->busparams_nominal, &cmd->get_busparams_res.busparams, 878 sizeof(priv->busparams_nominal)); 879 break; 880 case KVASER_USB_HYDRA_BUSPARAM_TYPE_CANFD: 881 memcpy(&priv->busparams_data, &cmd->get_busparams_res.busparams, 882 sizeof(priv->busparams_nominal)); 883 break; 884 default: 885 dev_warn(&dev->intf->dev, "Unknown get_busparams_type %d\n", 886 hydra->pending_get_busparams_type); 887 break; 888 } 889 hydra->pending_get_busparams_type = -1; 890 891 complete(&priv->get_busparams_comp); 892 } 893 894 static void 895 kvaser_usb_hydra_bus_status_to_can_state(const struct kvaser_usb_net_priv *priv, 896 u8 bus_status, 897 const struct can_berr_counter *bec, 898 enum can_state *new_state) 899 { 900 if (bus_status & KVASER_USB_HYDRA_BUS_BUS_OFF) { 901 *new_state = CAN_STATE_BUS_OFF; 902 } else if (bus_status & KVASER_USB_HYDRA_BUS_ERR_PASS) { 903 *new_state = CAN_STATE_ERROR_PASSIVE; 904 } else if (bus_status == KVASER_USB_HYDRA_BUS_ERR_ACT) { 905 if (bec->txerr >= 128 || bec->rxerr >= 128) { 906 netdev_warn(priv->netdev, 907 "ERR_ACTIVE but err tx=%u or rx=%u >=128\n", 908 bec->txerr, bec->rxerr); 909 *new_state = CAN_STATE_ERROR_PASSIVE; 910 } else if (bec->txerr >= 96 || bec->rxerr >= 96) { 911 *new_state = CAN_STATE_ERROR_WARNING; 912 } else { 913 *new_state = CAN_STATE_ERROR_ACTIVE; 914 } 915 } 916 } 917 918 static void kvaser_usb_hydra_update_state(struct kvaser_usb_net_priv *priv, 919 u8 bus_status, 920 const struct can_berr_counter *bec) 921 { 922 struct net_device *netdev = priv->netdev; 923 struct can_frame *cf; 924 struct sk_buff *skb; 925 enum can_state new_state, old_state; 926 927 old_state = priv->can.state; 928 929 kvaser_usb_hydra_bus_status_to_can_state(priv, bus_status, bec, 930 &new_state); 931 932 if (new_state == old_state) 933 return; 934 935 /* Ignore state change if previous state was STOPPED and the new state 936 * is BUS_OFF. Firmware always report this as BUS_OFF, since firmware 937 * does not distinguish between BUS_OFF and STOPPED. 938 */ 939 if (old_state == CAN_STATE_STOPPED && new_state == CAN_STATE_BUS_OFF) 940 return; 941 942 skb = alloc_can_err_skb(netdev, &cf); 943 if (skb) { 944 enum can_state tx_state, rx_state; 945 946 tx_state = (bec->txerr >= bec->rxerr) ? 947 new_state : CAN_STATE_ERROR_ACTIVE; 948 rx_state = (bec->txerr <= bec->rxerr) ? 949 new_state : CAN_STATE_ERROR_ACTIVE; 950 can_change_state(netdev, cf, tx_state, rx_state); 951 } 952 953 if (new_state == CAN_STATE_BUS_OFF && old_state < CAN_STATE_BUS_OFF) { 954 if (!priv->can.restart_ms) 955 kvaser_usb_hydra_send_simple_cmd_async 956 (priv, CMD_STOP_CHIP_REQ); 957 958 can_bus_off(netdev); 959 } 960 961 if (!skb) { 962 netdev_warn(netdev, "No memory left for err_skb\n"); 963 return; 964 } 965 966 if (priv->can.restart_ms && 967 old_state >= CAN_STATE_BUS_OFF && 968 new_state < CAN_STATE_BUS_OFF) 969 priv->can.can_stats.restarts++; 970 971 if (new_state != CAN_STATE_BUS_OFF) { 972 cf->can_id |= CAN_ERR_CNT; 973 cf->data[6] = bec->txerr; 974 cf->data[7] = bec->rxerr; 975 } 976 977 netif_rx(skb); 978 } 979 980 static void kvaser_usb_hydra_state_event(const struct kvaser_usb *dev, 981 const struct kvaser_cmd *cmd) 982 { 983 struct kvaser_usb_net_priv *priv; 984 struct can_berr_counter bec; 985 u8 bus_status; 986 987 priv = kvaser_usb_hydra_net_priv_from_cmd(dev, cmd); 988 if (!priv) 989 return; 990 991 bus_status = cmd->chip_state_event.bus_status; 992 bec.txerr = cmd->chip_state_event.tx_err_counter; 993 bec.rxerr = cmd->chip_state_event.rx_err_counter; 994 995 kvaser_usb_hydra_update_state(priv, bus_status, &bec); 996 priv->bec.txerr = bec.txerr; 997 priv->bec.rxerr = bec.rxerr; 998 } 999 1000 static void kvaser_usb_hydra_error_event_parameter(const struct kvaser_usb *dev, 1001 const struct kvaser_cmd *cmd) 1002 { 1003 /* info1 will contain the offending cmd_no */ 1004 switch (le16_to_cpu(cmd->error_event.info1)) { 1005 case CMD_START_CHIP_REQ: 1006 dev_warn(&dev->intf->dev, 1007 "CMD_START_CHIP_REQ error in parameter\n"); 1008 break; 1009 1010 case CMD_STOP_CHIP_REQ: 1011 dev_warn(&dev->intf->dev, 1012 "CMD_STOP_CHIP_REQ error in parameter\n"); 1013 break; 1014 1015 case CMD_FLUSH_QUEUE: 1016 dev_warn(&dev->intf->dev, 1017 "CMD_FLUSH_QUEUE error in parameter\n"); 1018 break; 1019 1020 case CMD_SET_BUSPARAMS_REQ: 1021 dev_warn(&dev->intf->dev, 1022 "Set bittiming failed. Error in parameter\n"); 1023 break; 1024 1025 case CMD_SET_BUSPARAMS_FD_REQ: 1026 dev_warn(&dev->intf->dev, 1027 "Set data bittiming failed. Error in parameter\n"); 1028 break; 1029 1030 default: 1031 dev_warn(&dev->intf->dev, 1032 "Unhandled parameter error event cmd_no (%u)\n", 1033 le16_to_cpu(cmd->error_event.info1)); 1034 break; 1035 } 1036 } 1037 1038 static void kvaser_usb_hydra_error_event(const struct kvaser_usb *dev, 1039 const struct kvaser_cmd *cmd) 1040 { 1041 switch (cmd->error_event.error_code) { 1042 case KVASER_USB_HYDRA_ERROR_EVENT_PARAM: 1043 kvaser_usb_hydra_error_event_parameter(dev, cmd); 1044 break; 1045 1046 case KVASER_USB_HYDRA_ERROR_EVENT_CAN: 1047 /* Wrong channel mapping?! This should never happen! 1048 * info1 will contain the offending cmd_no 1049 */ 1050 dev_err(&dev->intf->dev, 1051 "Received CAN error event for cmd_no (%u)\n", 1052 le16_to_cpu(cmd->error_event.info1)); 1053 break; 1054 1055 default: 1056 dev_warn(&dev->intf->dev, 1057 "Unhandled error event (%d)\n", 1058 cmd->error_event.error_code); 1059 break; 1060 } 1061 } 1062 1063 static void 1064 kvaser_usb_hydra_error_frame(struct kvaser_usb_net_priv *priv, 1065 const struct kvaser_err_frame_data *err_frame_data, 1066 ktime_t hwtstamp) 1067 { 1068 struct net_device *netdev = priv->netdev; 1069 struct net_device_stats *stats = &netdev->stats; 1070 struct can_frame *cf; 1071 struct sk_buff *skb; 1072 struct skb_shared_hwtstamps *shhwtstamps; 1073 struct can_berr_counter bec; 1074 enum can_state new_state, old_state; 1075 u8 bus_status; 1076 1077 priv->can.can_stats.bus_error++; 1078 stats->rx_errors++; 1079 1080 bus_status = err_frame_data->bus_status; 1081 bec.txerr = err_frame_data->tx_err_counter; 1082 bec.rxerr = err_frame_data->rx_err_counter; 1083 1084 old_state = priv->can.state; 1085 kvaser_usb_hydra_bus_status_to_can_state(priv, bus_status, &bec, 1086 &new_state); 1087 1088 skb = alloc_can_err_skb(netdev, &cf); 1089 1090 if (new_state != old_state) { 1091 if (skb) { 1092 enum can_state tx_state, rx_state; 1093 1094 tx_state = (bec.txerr >= bec.rxerr) ? 1095 new_state : CAN_STATE_ERROR_ACTIVE; 1096 rx_state = (bec.txerr <= bec.rxerr) ? 1097 new_state : CAN_STATE_ERROR_ACTIVE; 1098 1099 can_change_state(netdev, cf, tx_state, rx_state); 1100 1101 if (priv->can.restart_ms && 1102 old_state >= CAN_STATE_BUS_OFF && 1103 new_state < CAN_STATE_BUS_OFF) 1104 cf->can_id |= CAN_ERR_RESTARTED; 1105 } 1106 1107 if (new_state == CAN_STATE_BUS_OFF) { 1108 if (!priv->can.restart_ms) 1109 kvaser_usb_hydra_send_simple_cmd_async 1110 (priv, CMD_STOP_CHIP_REQ); 1111 1112 can_bus_off(netdev); 1113 } 1114 } 1115 1116 if (!skb) { 1117 stats->rx_dropped++; 1118 netdev_warn(netdev, "No memory left for err_skb\n"); 1119 return; 1120 } 1121 1122 shhwtstamps = skb_hwtstamps(skb); 1123 shhwtstamps->hwtstamp = hwtstamp; 1124 1125 cf->can_id |= CAN_ERR_BUSERROR; 1126 if (new_state != CAN_STATE_BUS_OFF) { 1127 cf->can_id |= CAN_ERR_CNT; 1128 cf->data[6] = bec.txerr; 1129 cf->data[7] = bec.rxerr; 1130 } 1131 1132 netif_rx(skb); 1133 1134 priv->bec.txerr = bec.txerr; 1135 priv->bec.rxerr = bec.rxerr; 1136 } 1137 1138 static void kvaser_usb_hydra_one_shot_fail(struct kvaser_usb_net_priv *priv, 1139 const struct kvaser_cmd_ext *cmd) 1140 { 1141 struct net_device *netdev = priv->netdev; 1142 struct net_device_stats *stats = &netdev->stats; 1143 struct can_frame *cf; 1144 struct sk_buff *skb; 1145 u32 flags; 1146 1147 skb = alloc_can_err_skb(netdev, &cf); 1148 if (!skb) { 1149 stats->rx_dropped++; 1150 netdev_warn(netdev, "No memory left for err_skb\n"); 1151 return; 1152 } 1153 1154 cf->can_id |= CAN_ERR_BUSERROR; 1155 flags = le32_to_cpu(cmd->tx_ack.flags); 1156 1157 if (flags & KVASER_USB_HYDRA_CF_FLAG_OSM_NACK) 1158 cf->can_id |= CAN_ERR_ACK; 1159 if (flags & KVASER_USB_HYDRA_CF_FLAG_ABL) { 1160 cf->can_id |= CAN_ERR_LOSTARB; 1161 priv->can.can_stats.arbitration_lost++; 1162 } 1163 1164 stats->tx_errors++; 1165 netif_rx(skb); 1166 } 1167 1168 static void kvaser_usb_hydra_tx_acknowledge(const struct kvaser_usb *dev, 1169 const struct kvaser_cmd *cmd) 1170 { 1171 struct kvaser_usb_tx_urb_context *context; 1172 struct kvaser_usb_net_priv *priv; 1173 unsigned long irq_flags; 1174 unsigned int len; 1175 bool one_shot_fail = false; 1176 bool is_err_frame = false; 1177 u16 transid = kvaser_usb_hydra_get_cmd_transid(cmd); 1178 1179 priv = kvaser_usb_hydra_net_priv_from_cmd(dev, cmd); 1180 if (!priv) 1181 return; 1182 1183 if (!netif_device_present(priv->netdev)) 1184 return; 1185 1186 if (cmd->header.cmd_no == CMD_EXTENDED) { 1187 struct kvaser_cmd_ext *cmd_ext = (struct kvaser_cmd_ext *)cmd; 1188 u32 flags = le32_to_cpu(cmd_ext->tx_ack.flags); 1189 1190 if (flags & (KVASER_USB_HYDRA_CF_FLAG_OSM_NACK | 1191 KVASER_USB_HYDRA_CF_FLAG_ABL)) { 1192 kvaser_usb_hydra_one_shot_fail(priv, cmd_ext); 1193 one_shot_fail = true; 1194 } 1195 1196 is_err_frame = flags & KVASER_USB_HYDRA_CF_FLAG_TX_ACK && 1197 flags & KVASER_USB_HYDRA_CF_FLAG_ERROR_FRAME; 1198 } 1199 1200 context = &priv->tx_contexts[transid % dev->max_tx_urbs]; 1201 1202 spin_lock_irqsave(&priv->tx_contexts_lock, irq_flags); 1203 1204 len = can_get_echo_skb(priv->netdev, context->echo_index, NULL); 1205 context->echo_index = dev->max_tx_urbs; 1206 --priv->active_tx_contexts; 1207 netif_wake_queue(priv->netdev); 1208 1209 spin_unlock_irqrestore(&priv->tx_contexts_lock, irq_flags); 1210 1211 if (!one_shot_fail && !is_err_frame) { 1212 struct net_device_stats *stats = &priv->netdev->stats; 1213 1214 stats->tx_packets++; 1215 stats->tx_bytes += len; 1216 } 1217 } 1218 1219 static void kvaser_usb_hydra_rx_msg_std(const struct kvaser_usb *dev, 1220 const struct kvaser_cmd *cmd) 1221 { 1222 struct kvaser_usb_net_priv *priv = NULL; 1223 struct can_frame *cf; 1224 struct sk_buff *skb; 1225 struct skb_shared_hwtstamps *shhwtstamps; 1226 struct net_device_stats *stats; 1227 u8 flags; 1228 ktime_t hwtstamp; 1229 1230 priv = kvaser_usb_hydra_net_priv_from_cmd(dev, cmd); 1231 if (!priv) 1232 return; 1233 1234 stats = &priv->netdev->stats; 1235 1236 flags = cmd->rx_can.flags; 1237 hwtstamp = kvaser_usb_hydra_ktime_from_rx_cmd(dev->cfg, cmd); 1238 1239 if (flags & KVASER_USB_HYDRA_CF_FLAG_ERROR_FRAME) { 1240 kvaser_usb_hydra_error_frame(priv, &cmd->rx_can.err_frame_data, 1241 hwtstamp); 1242 return; 1243 } 1244 1245 skb = alloc_can_skb(priv->netdev, &cf); 1246 if (!skb) { 1247 stats->rx_dropped++; 1248 return; 1249 } 1250 1251 shhwtstamps = skb_hwtstamps(skb); 1252 shhwtstamps->hwtstamp = hwtstamp; 1253 1254 cf->can_id = le32_to_cpu(cmd->rx_can.id); 1255 1256 if (cf->can_id & KVASER_USB_HYDRA_EXTENDED_FRAME_ID) { 1257 cf->can_id &= CAN_EFF_MASK; 1258 cf->can_id |= CAN_EFF_FLAG; 1259 } else { 1260 cf->can_id &= CAN_SFF_MASK; 1261 } 1262 1263 if (flags & KVASER_USB_HYDRA_CF_FLAG_OVERRUN) 1264 kvaser_usb_can_rx_over_error(priv->netdev); 1265 1266 cf->len = can_cc_dlc2len(cmd->rx_can.dlc); 1267 1268 if (flags & KVASER_USB_HYDRA_CF_FLAG_REMOTE_FRAME) { 1269 cf->can_id |= CAN_RTR_FLAG; 1270 } else { 1271 memcpy(cf->data, cmd->rx_can.data, cf->len); 1272 1273 stats->rx_bytes += cf->len; 1274 } 1275 stats->rx_packets++; 1276 1277 netif_rx(skb); 1278 } 1279 1280 static void kvaser_usb_hydra_rx_msg_ext(const struct kvaser_usb *dev, 1281 const struct kvaser_cmd_ext *cmd) 1282 { 1283 struct kvaser_cmd *std_cmd = (struct kvaser_cmd *)cmd; 1284 struct kvaser_usb_net_priv *priv; 1285 struct canfd_frame *cf; 1286 struct sk_buff *skb; 1287 struct skb_shared_hwtstamps *shhwtstamps; 1288 struct net_device_stats *stats; 1289 u32 flags; 1290 u8 dlc; 1291 u32 kcan_header; 1292 ktime_t hwtstamp; 1293 1294 priv = kvaser_usb_hydra_net_priv_from_cmd(dev, std_cmd); 1295 if (!priv) 1296 return; 1297 1298 stats = &priv->netdev->stats; 1299 1300 kcan_header = le32_to_cpu(cmd->rx_can.kcan_header); 1301 dlc = (kcan_header & KVASER_USB_KCAN_DATA_DLC_MASK) >> 1302 KVASER_USB_KCAN_DATA_DLC_SHIFT; 1303 1304 flags = le32_to_cpu(cmd->rx_can.flags); 1305 hwtstamp = kvaser_usb_hydra_ktime_from_rx_cmd(dev->cfg, std_cmd); 1306 1307 if (flags & KVASER_USB_HYDRA_CF_FLAG_ERROR_FRAME) { 1308 kvaser_usb_hydra_error_frame(priv, &cmd->rx_can.err_frame_data, 1309 hwtstamp); 1310 return; 1311 } 1312 1313 if (flags & KVASER_USB_HYDRA_CF_FLAG_FDF) 1314 skb = alloc_canfd_skb(priv->netdev, &cf); 1315 else 1316 skb = alloc_can_skb(priv->netdev, (struct can_frame **)&cf); 1317 1318 if (!skb) { 1319 stats->rx_dropped++; 1320 return; 1321 } 1322 1323 shhwtstamps = skb_hwtstamps(skb); 1324 shhwtstamps->hwtstamp = hwtstamp; 1325 1326 cf->can_id = le32_to_cpu(cmd->rx_can.id); 1327 1328 if (flags & KVASER_USB_HYDRA_CF_FLAG_EXTENDED_ID) { 1329 cf->can_id &= CAN_EFF_MASK; 1330 cf->can_id |= CAN_EFF_FLAG; 1331 } else { 1332 cf->can_id &= CAN_SFF_MASK; 1333 } 1334 1335 if (flags & KVASER_USB_HYDRA_CF_FLAG_OVERRUN) 1336 kvaser_usb_can_rx_over_error(priv->netdev); 1337 1338 if (flags & KVASER_USB_HYDRA_CF_FLAG_FDF) { 1339 cf->len = can_fd_dlc2len(dlc); 1340 if (flags & KVASER_USB_HYDRA_CF_FLAG_BRS) 1341 cf->flags |= CANFD_BRS; 1342 if (flags & KVASER_USB_HYDRA_CF_FLAG_ESI) 1343 cf->flags |= CANFD_ESI; 1344 } else { 1345 cf->len = can_cc_dlc2len(dlc); 1346 } 1347 1348 if (flags & KVASER_USB_HYDRA_CF_FLAG_REMOTE_FRAME) { 1349 cf->can_id |= CAN_RTR_FLAG; 1350 } else { 1351 memcpy(cf->data, cmd->rx_can.kcan_payload, cf->len); 1352 1353 stats->rx_bytes += cf->len; 1354 } 1355 stats->rx_packets++; 1356 1357 netif_rx(skb); 1358 } 1359 1360 static void kvaser_usb_hydra_handle_cmd_std(const struct kvaser_usb *dev, 1361 const struct kvaser_cmd *cmd) 1362 { 1363 switch (cmd->header.cmd_no) { 1364 case CMD_START_CHIP_RESP: 1365 kvaser_usb_hydra_start_chip_reply(dev, cmd); 1366 break; 1367 1368 case CMD_STOP_CHIP_RESP: 1369 kvaser_usb_hydra_stop_chip_reply(dev, cmd); 1370 break; 1371 1372 case CMD_FLUSH_QUEUE_RESP: 1373 kvaser_usb_hydra_flush_queue_reply(dev, cmd); 1374 break; 1375 1376 case CMD_CHIP_STATE_EVENT: 1377 kvaser_usb_hydra_state_event(dev, cmd); 1378 break; 1379 1380 case CMD_GET_BUSPARAMS_RESP: 1381 kvaser_usb_hydra_get_busparams_reply(dev, cmd); 1382 break; 1383 1384 case CMD_ERROR_EVENT: 1385 kvaser_usb_hydra_error_event(dev, cmd); 1386 break; 1387 1388 case CMD_TX_ACKNOWLEDGE: 1389 kvaser_usb_hydra_tx_acknowledge(dev, cmd); 1390 break; 1391 1392 case CMD_RX_MESSAGE: 1393 kvaser_usb_hydra_rx_msg_std(dev, cmd); 1394 break; 1395 1396 /* Ignored commands */ 1397 case CMD_SET_BUSPARAMS_RESP: 1398 case CMD_SET_BUSPARAMS_FD_RESP: 1399 break; 1400 1401 default: 1402 dev_warn(&dev->intf->dev, "Unhandled command (%d)\n", 1403 cmd->header.cmd_no); 1404 break; 1405 } 1406 } 1407 1408 static void kvaser_usb_hydra_handle_cmd_ext(const struct kvaser_usb *dev, 1409 const struct kvaser_cmd_ext *cmd) 1410 { 1411 switch (cmd->cmd_no_ext) { 1412 case CMD_TX_ACKNOWLEDGE_FD: 1413 kvaser_usb_hydra_tx_acknowledge(dev, (struct kvaser_cmd *)cmd); 1414 break; 1415 1416 case CMD_RX_MESSAGE_FD: 1417 kvaser_usb_hydra_rx_msg_ext(dev, cmd); 1418 break; 1419 1420 default: 1421 dev_warn(&dev->intf->dev, "Unhandled extended command (%d)\n", 1422 cmd->header.cmd_no); 1423 break; 1424 } 1425 } 1426 1427 static void kvaser_usb_hydra_handle_cmd(const struct kvaser_usb *dev, 1428 const struct kvaser_cmd *cmd) 1429 { 1430 if (cmd->header.cmd_no == CMD_EXTENDED) 1431 kvaser_usb_hydra_handle_cmd_ext 1432 (dev, (struct kvaser_cmd_ext *)cmd); 1433 else 1434 kvaser_usb_hydra_handle_cmd_std(dev, cmd); 1435 } 1436 1437 static void * 1438 kvaser_usb_hydra_frame_to_cmd_ext(const struct kvaser_usb_net_priv *priv, 1439 const struct sk_buff *skb, int *cmd_len, 1440 u16 transid) 1441 { 1442 struct kvaser_usb *dev = priv->dev; 1443 struct kvaser_cmd_ext *cmd; 1444 struct canfd_frame *cf = (struct canfd_frame *)skb->data; 1445 u8 dlc = can_fd_len2dlc(cf->len); 1446 u8 nbr_of_bytes = cf->len; 1447 u32 flags; 1448 u32 id; 1449 u32 kcan_id; 1450 u32 kcan_header; 1451 1452 cmd = kzalloc(sizeof(*cmd), GFP_ATOMIC); 1453 if (!cmd) 1454 return NULL; 1455 1456 kvaser_usb_hydra_set_cmd_dest_he 1457 ((struct kvaser_cmd *)cmd, 1458 dev->card_data.hydra.channel_to_he[priv->channel]); 1459 kvaser_usb_hydra_set_cmd_transid((struct kvaser_cmd *)cmd, transid); 1460 1461 cmd->header.cmd_no = CMD_EXTENDED; 1462 cmd->cmd_no_ext = CMD_TX_CAN_MESSAGE_FD; 1463 1464 *cmd_len = ALIGN(sizeof(struct kvaser_cmd_ext) - 1465 sizeof(cmd->tx_can.kcan_payload) + nbr_of_bytes, 1466 8); 1467 1468 cmd->len = cpu_to_le16(*cmd_len); 1469 1470 cmd->tx_can.databytes = nbr_of_bytes; 1471 cmd->tx_can.dlc = dlc; 1472 1473 if (cf->can_id & CAN_EFF_FLAG) { 1474 id = cf->can_id & CAN_EFF_MASK; 1475 flags = KVASER_USB_HYDRA_CF_FLAG_EXTENDED_ID; 1476 kcan_id = (cf->can_id & CAN_EFF_MASK) | 1477 KVASER_USB_KCAN_DATA_IDE | KVASER_USB_KCAN_DATA_SRR; 1478 } else { 1479 id = cf->can_id & CAN_SFF_MASK; 1480 flags = 0; 1481 kcan_id = cf->can_id & CAN_SFF_MASK; 1482 } 1483 1484 if (cf->can_id & CAN_ERR_FLAG) 1485 flags |= KVASER_USB_HYDRA_CF_FLAG_ERROR_FRAME; 1486 1487 kcan_header = ((dlc << KVASER_USB_KCAN_DATA_DLC_SHIFT) & 1488 KVASER_USB_KCAN_DATA_DLC_MASK) | 1489 KVASER_USB_KCAN_DATA_AREQ | 1490 (priv->can.ctrlmode & CAN_CTRLMODE_ONE_SHOT ? 1491 KVASER_USB_KCAN_DATA_OSM : 0); 1492 1493 if (can_is_canfd_skb(skb)) { 1494 kcan_header |= KVASER_USB_KCAN_DATA_FDF | 1495 (cf->flags & CANFD_BRS ? 1496 KVASER_USB_KCAN_DATA_BRS : 0); 1497 } else { 1498 if (cf->can_id & CAN_RTR_FLAG) { 1499 kcan_id |= KVASER_USB_KCAN_DATA_RTR; 1500 cmd->tx_can.databytes = 0; 1501 flags |= KVASER_USB_HYDRA_CF_FLAG_REMOTE_FRAME; 1502 } 1503 } 1504 1505 cmd->tx_can.kcan_id = cpu_to_le32(kcan_id); 1506 cmd->tx_can.id = cpu_to_le32(id); 1507 cmd->tx_can.flags = cpu_to_le32(flags); 1508 cmd->tx_can.kcan_header = cpu_to_le32(kcan_header); 1509 1510 memcpy(cmd->tx_can.kcan_payload, cf->data, nbr_of_bytes); 1511 1512 return cmd; 1513 } 1514 1515 static void * 1516 kvaser_usb_hydra_frame_to_cmd_std(const struct kvaser_usb_net_priv *priv, 1517 const struct sk_buff *skb, int *cmd_len, 1518 u16 transid) 1519 { 1520 struct kvaser_usb *dev = priv->dev; 1521 struct kvaser_cmd *cmd; 1522 struct can_frame *cf = (struct can_frame *)skb->data; 1523 u32 flags; 1524 u32 id; 1525 1526 cmd = kzalloc(sizeof(*cmd), GFP_ATOMIC); 1527 if (!cmd) 1528 return NULL; 1529 1530 kvaser_usb_hydra_set_cmd_dest_he 1531 (cmd, dev->card_data.hydra.channel_to_he[priv->channel]); 1532 kvaser_usb_hydra_set_cmd_transid(cmd, transid); 1533 1534 cmd->header.cmd_no = CMD_TX_CAN_MESSAGE; 1535 1536 *cmd_len = ALIGN(sizeof(struct kvaser_cmd), 8); 1537 1538 if (cf->can_id & CAN_EFF_FLAG) { 1539 id = (cf->can_id & CAN_EFF_MASK); 1540 id |= KVASER_USB_HYDRA_EXTENDED_FRAME_ID; 1541 } else { 1542 id = cf->can_id & CAN_SFF_MASK; 1543 } 1544 1545 cmd->tx_can.dlc = cf->len; 1546 1547 flags = (cf->can_id & CAN_EFF_FLAG ? 1548 KVASER_USB_HYDRA_CF_FLAG_EXTENDED_ID : 0); 1549 1550 if (cf->can_id & CAN_RTR_FLAG) 1551 flags |= KVASER_USB_HYDRA_CF_FLAG_REMOTE_FRAME; 1552 1553 flags |= (cf->can_id & CAN_ERR_FLAG ? 1554 KVASER_USB_HYDRA_CF_FLAG_ERROR_FRAME : 0); 1555 1556 cmd->tx_can.id = cpu_to_le32(id); 1557 cmd->tx_can.flags = flags; 1558 1559 memcpy(cmd->tx_can.data, cf->data, cf->len); 1560 1561 return cmd; 1562 } 1563 1564 static int kvaser_usb_hydra_set_mode(struct net_device *netdev, 1565 enum can_mode mode) 1566 { 1567 int err = 0; 1568 1569 switch (mode) { 1570 case CAN_MODE_START: 1571 /* CAN controller automatically recovers from BUS_OFF */ 1572 break; 1573 default: 1574 err = -EOPNOTSUPP; 1575 } 1576 1577 return err; 1578 } 1579 1580 static int kvaser_usb_hydra_get_busparams(struct kvaser_usb_net_priv *priv, 1581 int busparams_type) 1582 { 1583 struct kvaser_usb *dev = priv->dev; 1584 struct kvaser_usb_net_hydra_priv *hydra = priv->sub_priv; 1585 struct kvaser_cmd *cmd; 1586 size_t cmd_len; 1587 int err; 1588 1589 if (!hydra) 1590 return -EINVAL; 1591 1592 cmd = kcalloc(1, sizeof(struct kvaser_cmd), GFP_KERNEL); 1593 if (!cmd) 1594 return -ENOMEM; 1595 1596 cmd->header.cmd_no = CMD_GET_BUSPARAMS_REQ; 1597 cmd_len = kvaser_usb_hydra_cmd_size(cmd); 1598 kvaser_usb_hydra_set_cmd_dest_he 1599 (cmd, dev->card_data.hydra.channel_to_he[priv->channel]); 1600 kvaser_usb_hydra_set_cmd_transid 1601 (cmd, kvaser_usb_hydra_get_next_transid(dev)); 1602 cmd->get_busparams_req.type = busparams_type; 1603 hydra->pending_get_busparams_type = busparams_type; 1604 1605 reinit_completion(&priv->get_busparams_comp); 1606 1607 err = kvaser_usb_send_cmd(dev, cmd, cmd_len); 1608 if (err) 1609 return err; 1610 1611 if (!wait_for_completion_timeout(&priv->get_busparams_comp, 1612 msecs_to_jiffies(KVASER_USB_TIMEOUT))) 1613 return -ETIMEDOUT; 1614 1615 return err; 1616 } 1617 1618 static int kvaser_usb_hydra_get_nominal_busparams(struct kvaser_usb_net_priv *priv) 1619 { 1620 return kvaser_usb_hydra_get_busparams(priv, KVASER_USB_HYDRA_BUSPARAM_TYPE_CAN); 1621 } 1622 1623 static int kvaser_usb_hydra_get_data_busparams(struct kvaser_usb_net_priv *priv) 1624 { 1625 return kvaser_usb_hydra_get_busparams(priv, KVASER_USB_HYDRA_BUSPARAM_TYPE_CANFD); 1626 } 1627 1628 static int kvaser_usb_hydra_set_bittiming(const struct net_device *netdev, 1629 const struct kvaser_usb_busparams *busparams) 1630 { 1631 struct kvaser_cmd *cmd; 1632 struct kvaser_usb_net_priv *priv = netdev_priv(netdev); 1633 struct kvaser_usb *dev = priv->dev; 1634 size_t cmd_len; 1635 int err; 1636 1637 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); 1638 if (!cmd) 1639 return -ENOMEM; 1640 1641 cmd->header.cmd_no = CMD_SET_BUSPARAMS_REQ; 1642 cmd_len = kvaser_usb_hydra_cmd_size(cmd); 1643 memcpy(&cmd->set_busparams_req.busparams_nominal, busparams, 1644 sizeof(cmd->set_busparams_req.busparams_nominal)); 1645 1646 kvaser_usb_hydra_set_cmd_dest_he 1647 (cmd, dev->card_data.hydra.channel_to_he[priv->channel]); 1648 kvaser_usb_hydra_set_cmd_transid 1649 (cmd, kvaser_usb_hydra_get_next_transid(dev)); 1650 1651 err = kvaser_usb_send_cmd(dev, cmd, cmd_len); 1652 1653 kfree(cmd); 1654 1655 return err; 1656 } 1657 1658 static int kvaser_usb_hydra_set_data_bittiming(const struct net_device *netdev, 1659 const struct kvaser_usb_busparams *busparams) 1660 { 1661 struct kvaser_cmd *cmd; 1662 struct kvaser_usb_net_priv *priv = netdev_priv(netdev); 1663 struct kvaser_usb *dev = priv->dev; 1664 size_t cmd_len; 1665 int err; 1666 1667 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); 1668 if (!cmd) 1669 return -ENOMEM; 1670 1671 cmd->header.cmd_no = CMD_SET_BUSPARAMS_FD_REQ; 1672 cmd_len = kvaser_usb_hydra_cmd_size(cmd); 1673 memcpy(&cmd->set_busparams_req.busparams_data, busparams, 1674 sizeof(cmd->set_busparams_req.busparams_data)); 1675 1676 if (priv->can.ctrlmode & CAN_CTRLMODE_FD) { 1677 if (priv->can.ctrlmode & CAN_CTRLMODE_FD_NON_ISO) 1678 cmd->set_busparams_req.canfd_mode = 1679 KVASER_USB_HYDRA_BUS_MODE_NONISO; 1680 else 1681 cmd->set_busparams_req.canfd_mode = 1682 KVASER_USB_HYDRA_BUS_MODE_CANFD_ISO; 1683 } 1684 1685 kvaser_usb_hydra_set_cmd_dest_he 1686 (cmd, dev->card_data.hydra.channel_to_he[priv->channel]); 1687 kvaser_usb_hydra_set_cmd_transid 1688 (cmd, kvaser_usb_hydra_get_next_transid(dev)); 1689 1690 err = kvaser_usb_send_cmd(dev, cmd, cmd_len); 1691 1692 kfree(cmd); 1693 1694 return err; 1695 } 1696 1697 static int kvaser_usb_hydra_get_berr_counter(const struct net_device *netdev, 1698 struct can_berr_counter *bec) 1699 { 1700 struct kvaser_usb_net_priv *priv = netdev_priv(netdev); 1701 int err; 1702 1703 err = kvaser_usb_hydra_send_simple_cmd(priv->dev, 1704 CMD_GET_CHIP_STATE_REQ, 1705 priv->channel); 1706 if (err) 1707 return err; 1708 1709 *bec = priv->bec; 1710 1711 return 0; 1712 } 1713 1714 static int kvaser_usb_hydra_setup_endpoints(struct kvaser_usb *dev) 1715 { 1716 const struct usb_host_interface *iface_desc; 1717 struct usb_endpoint_descriptor *ep; 1718 int i; 1719 1720 iface_desc = dev->intf->cur_altsetting; 1721 1722 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) { 1723 ep = &iface_desc->endpoint[i].desc; 1724 1725 if (!dev->bulk_in && usb_endpoint_is_bulk_in(ep) && 1726 ep->bEndpointAddress == KVASER_USB_HYDRA_BULK_EP_IN_ADDR) 1727 dev->bulk_in = ep; 1728 1729 if (!dev->bulk_out && usb_endpoint_is_bulk_out(ep) && 1730 ep->bEndpointAddress == KVASER_USB_HYDRA_BULK_EP_OUT_ADDR) 1731 dev->bulk_out = ep; 1732 1733 if (dev->bulk_in && dev->bulk_out) 1734 return 0; 1735 } 1736 1737 return -ENODEV; 1738 } 1739 1740 static int kvaser_usb_hydra_init_card(struct kvaser_usb *dev) 1741 { 1742 int err; 1743 unsigned int i; 1744 struct kvaser_usb_dev_card_data_hydra *card_data = 1745 &dev->card_data.hydra; 1746 1747 card_data->transid = KVASER_USB_HYDRA_MIN_TRANSID; 1748 spin_lock_init(&card_data->transid_lock); 1749 1750 memset(card_data->usb_rx_leftover, 0, KVASER_USB_HYDRA_MAX_CMD_LEN); 1751 card_data->usb_rx_leftover_len = 0; 1752 spin_lock_init(&card_data->usb_rx_leftover_lock); 1753 1754 memset(card_data->channel_to_he, KVASER_USB_HYDRA_HE_ADDRESS_ILLEGAL, 1755 sizeof(card_data->channel_to_he)); 1756 card_data->sysdbg_he = 0; 1757 1758 for (i = 0; i < KVASER_USB_MAX_NET_DEVICES; i++) { 1759 err = kvaser_usb_hydra_map_channel 1760 (dev, 1761 (KVASER_USB_HYDRA_TRANSID_CANHE | i), 1762 i, "CAN"); 1763 if (err) { 1764 dev_err(&dev->intf->dev, 1765 "CMD_MAP_CHANNEL_REQ failed for CAN%u\n", i); 1766 return err; 1767 } 1768 } 1769 1770 err = kvaser_usb_hydra_map_channel(dev, KVASER_USB_HYDRA_TRANSID_SYSDBG, 1771 0, "SYSDBG"); 1772 if (err) { 1773 dev_err(&dev->intf->dev, 1774 "CMD_MAP_CHANNEL_REQ failed for SYSDBG\n"); 1775 return err; 1776 } 1777 1778 return 0; 1779 } 1780 1781 static int kvaser_usb_hydra_init_channel(struct kvaser_usb_net_priv *priv) 1782 { 1783 struct kvaser_usb_net_hydra_priv *hydra; 1784 1785 hydra = devm_kzalloc(&priv->dev->intf->dev, sizeof(*hydra), GFP_KERNEL); 1786 if (!hydra) 1787 return -ENOMEM; 1788 1789 priv->sub_priv = hydra; 1790 1791 return 0; 1792 } 1793 1794 static int kvaser_usb_hydra_get_software_info(struct kvaser_usb *dev) 1795 { 1796 struct kvaser_cmd cmd; 1797 int err; 1798 1799 err = kvaser_usb_hydra_send_simple_cmd(dev, CMD_GET_SOFTWARE_INFO_REQ, 1800 -1); 1801 if (err) 1802 return err; 1803 1804 memset(&cmd, 0, sizeof(struct kvaser_cmd)); 1805 err = kvaser_usb_hydra_wait_cmd(dev, CMD_GET_SOFTWARE_INFO_RESP, &cmd); 1806 if (err) 1807 return err; 1808 1809 dev->max_tx_urbs = min_t(unsigned int, KVASER_USB_MAX_TX_URBS, 1810 le16_to_cpu(cmd.sw_info.max_outstanding_tx)); 1811 1812 return 0; 1813 } 1814 1815 static int kvaser_usb_hydra_get_software_details(struct kvaser_usb *dev) 1816 { 1817 struct kvaser_cmd *cmd; 1818 size_t cmd_len; 1819 int err; 1820 u32 flags; 1821 struct kvaser_usb_dev_card_data *card_data = &dev->card_data; 1822 1823 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); 1824 if (!cmd) 1825 return -ENOMEM; 1826 1827 cmd->header.cmd_no = CMD_GET_SOFTWARE_DETAILS_REQ; 1828 cmd_len = kvaser_usb_hydra_cmd_size(cmd); 1829 cmd->sw_detail_req.use_ext_cmd = 1; 1830 kvaser_usb_hydra_set_cmd_dest_he 1831 (cmd, KVASER_USB_HYDRA_HE_ADDRESS_ILLEGAL); 1832 1833 kvaser_usb_hydra_set_cmd_transid 1834 (cmd, kvaser_usb_hydra_get_next_transid(dev)); 1835 1836 err = kvaser_usb_send_cmd(dev, cmd, cmd_len); 1837 if (err) 1838 goto end; 1839 1840 err = kvaser_usb_hydra_wait_cmd(dev, CMD_GET_SOFTWARE_DETAILS_RESP, 1841 cmd); 1842 if (err) 1843 goto end; 1844 1845 dev->fw_version = le32_to_cpu(cmd->sw_detail_res.sw_version); 1846 flags = le32_to_cpu(cmd->sw_detail_res.sw_flags); 1847 1848 if (flags & KVASER_USB_HYDRA_SW_FLAG_FW_BAD) { 1849 dev_err(&dev->intf->dev, 1850 "Bad firmware, device refuse to run!\n"); 1851 err = -EINVAL; 1852 goto end; 1853 } 1854 1855 if (flags & KVASER_USB_HYDRA_SW_FLAG_FW_BETA) 1856 dev_info(&dev->intf->dev, "Beta firmware in use\n"); 1857 1858 if (flags & KVASER_USB_HYDRA_SW_FLAG_EXT_CAP) 1859 card_data->capabilities |= KVASER_USB_CAP_EXT_CAP; 1860 1861 if (flags & KVASER_USB_HYDRA_SW_FLAG_EXT_CMD) 1862 card_data->capabilities |= KVASER_USB_HYDRA_CAP_EXT_CMD; 1863 1864 if (flags & KVASER_USB_HYDRA_SW_FLAG_CANFD) 1865 card_data->ctrlmode_supported |= CAN_CTRLMODE_FD; 1866 1867 if (flags & KVASER_USB_HYDRA_SW_FLAG_NONISO) 1868 card_data->ctrlmode_supported |= CAN_CTRLMODE_FD_NON_ISO; 1869 1870 if (flags & KVASER_USB_HYDRA_SW_FLAG_FREQ_80M) 1871 dev->cfg = &kvaser_usb_hydra_dev_cfg_kcan; 1872 else if (flags & KVASER_USB_HYDRA_SW_FLAG_CAN_FREQ_80M) 1873 dev->cfg = &kvaser_usb_hydra_dev_cfg_rt; 1874 else 1875 dev->cfg = &kvaser_usb_hydra_dev_cfg_flexc; 1876 1877 end: 1878 kfree(cmd); 1879 1880 return err; 1881 } 1882 1883 static int kvaser_usb_hydra_get_card_info(struct kvaser_usb *dev) 1884 { 1885 struct kvaser_cmd cmd; 1886 int err; 1887 1888 err = kvaser_usb_hydra_send_simple_cmd(dev, CMD_GET_CARD_INFO_REQ, -1); 1889 if (err) 1890 return err; 1891 1892 memset(&cmd, 0, sizeof(struct kvaser_cmd)); 1893 err = kvaser_usb_hydra_wait_cmd(dev, CMD_GET_CARD_INFO_RESP, &cmd); 1894 if (err) 1895 return err; 1896 1897 dev->nchannels = cmd.card_info.nchannels; 1898 if (dev->nchannels > KVASER_USB_MAX_NET_DEVICES) 1899 return -EINVAL; 1900 1901 return 0; 1902 } 1903 1904 static int kvaser_usb_hydra_get_capabilities(struct kvaser_usb *dev) 1905 { 1906 int err; 1907 u16 status; 1908 1909 if (!(dev->card_data.capabilities & KVASER_USB_CAP_EXT_CAP)) { 1910 dev_info(&dev->intf->dev, 1911 "No extended capability support. Upgrade your device.\n"); 1912 return 0; 1913 } 1914 1915 err = kvaser_usb_hydra_get_single_capability 1916 (dev, 1917 KVASER_USB_HYDRA_CAP_CMD_LISTEN_MODE, 1918 &status); 1919 if (err) 1920 return err; 1921 if (status) 1922 dev_info(&dev->intf->dev, 1923 "KVASER_USB_HYDRA_CAP_CMD_LISTEN_MODE failed %u\n", 1924 status); 1925 1926 err = kvaser_usb_hydra_get_single_capability 1927 (dev, 1928 KVASER_USB_HYDRA_CAP_CMD_ERR_REPORT, 1929 &status); 1930 if (err) 1931 return err; 1932 if (status) 1933 dev_info(&dev->intf->dev, 1934 "KVASER_USB_HYDRA_CAP_CMD_ERR_REPORT failed %u\n", 1935 status); 1936 1937 err = kvaser_usb_hydra_get_single_capability 1938 (dev, KVASER_USB_HYDRA_CAP_CMD_ONE_SHOT, 1939 &status); 1940 if (err) 1941 return err; 1942 if (status) 1943 dev_info(&dev->intf->dev, 1944 "KVASER_USB_HYDRA_CAP_CMD_ONE_SHOT failed %u\n", 1945 status); 1946 1947 return 0; 1948 } 1949 1950 static int kvaser_usb_hydra_set_opt_mode(const struct kvaser_usb_net_priv *priv) 1951 { 1952 struct kvaser_usb *dev = priv->dev; 1953 struct kvaser_cmd *cmd; 1954 size_t cmd_len; 1955 int err; 1956 1957 if ((priv->can.ctrlmode & 1958 (CAN_CTRLMODE_FD | CAN_CTRLMODE_FD_NON_ISO)) == 1959 CAN_CTRLMODE_FD_NON_ISO) { 1960 netdev_warn(priv->netdev, 1961 "CTRLMODE_FD shall be on if CTRLMODE_FD_NON_ISO is on\n"); 1962 return -EINVAL; 1963 } 1964 1965 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); 1966 if (!cmd) 1967 return -ENOMEM; 1968 1969 cmd->header.cmd_no = CMD_SET_DRIVERMODE_REQ; 1970 cmd_len = kvaser_usb_hydra_cmd_size(cmd); 1971 kvaser_usb_hydra_set_cmd_dest_he 1972 (cmd, dev->card_data.hydra.channel_to_he[priv->channel]); 1973 kvaser_usb_hydra_set_cmd_transid 1974 (cmd, kvaser_usb_hydra_get_next_transid(dev)); 1975 if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY) 1976 cmd->set_ctrlmode.mode = KVASER_USB_HYDRA_CTRLMODE_LISTEN; 1977 else 1978 cmd->set_ctrlmode.mode = KVASER_USB_HYDRA_CTRLMODE_NORMAL; 1979 1980 err = kvaser_usb_send_cmd(dev, cmd, cmd_len); 1981 kfree(cmd); 1982 1983 return err; 1984 } 1985 1986 static int kvaser_usb_hydra_start_chip(struct kvaser_usb_net_priv *priv) 1987 { 1988 int err; 1989 1990 reinit_completion(&priv->start_comp); 1991 1992 err = kvaser_usb_hydra_send_simple_cmd(priv->dev, CMD_START_CHIP_REQ, 1993 priv->channel); 1994 if (err) 1995 return err; 1996 1997 if (!wait_for_completion_timeout(&priv->start_comp, 1998 msecs_to_jiffies(KVASER_USB_TIMEOUT))) 1999 return -ETIMEDOUT; 2000 2001 return 0; 2002 } 2003 2004 static int kvaser_usb_hydra_stop_chip(struct kvaser_usb_net_priv *priv) 2005 { 2006 int err; 2007 2008 reinit_completion(&priv->stop_comp); 2009 2010 /* Make sure we do not report invalid BUS_OFF from CMD_CHIP_STATE_EVENT 2011 * see comment in kvaser_usb_hydra_update_state() 2012 */ 2013 priv->can.state = CAN_STATE_STOPPED; 2014 2015 err = kvaser_usb_hydra_send_simple_cmd(priv->dev, CMD_STOP_CHIP_REQ, 2016 priv->channel); 2017 if (err) 2018 return err; 2019 2020 if (!wait_for_completion_timeout(&priv->stop_comp, 2021 msecs_to_jiffies(KVASER_USB_TIMEOUT))) 2022 return -ETIMEDOUT; 2023 2024 return 0; 2025 } 2026 2027 static int kvaser_usb_hydra_flush_queue(struct kvaser_usb_net_priv *priv) 2028 { 2029 int err; 2030 2031 reinit_completion(&priv->flush_comp); 2032 2033 err = kvaser_usb_hydra_send_simple_cmd(priv->dev, CMD_FLUSH_QUEUE, 2034 priv->channel); 2035 if (err) 2036 return err; 2037 2038 if (!wait_for_completion_timeout(&priv->flush_comp, 2039 msecs_to_jiffies(KVASER_USB_TIMEOUT))) 2040 return -ETIMEDOUT; 2041 2042 return 0; 2043 } 2044 2045 /* A single extended hydra command can be transmitted in multiple transfers 2046 * We have to buffer partial hydra commands, and handle them on next callback. 2047 */ 2048 static void kvaser_usb_hydra_read_bulk_callback(struct kvaser_usb *dev, 2049 void *buf, int len) 2050 { 2051 unsigned long irq_flags; 2052 struct kvaser_cmd *cmd; 2053 int pos = 0; 2054 size_t cmd_len; 2055 struct kvaser_usb_dev_card_data_hydra *card_data = 2056 &dev->card_data.hydra; 2057 int usb_rx_leftover_len; 2058 spinlock_t *usb_rx_leftover_lock = &card_data->usb_rx_leftover_lock; 2059 2060 spin_lock_irqsave(usb_rx_leftover_lock, irq_flags); 2061 usb_rx_leftover_len = card_data->usb_rx_leftover_len; 2062 if (usb_rx_leftover_len) { 2063 int remaining_bytes; 2064 2065 cmd = (struct kvaser_cmd *)card_data->usb_rx_leftover; 2066 2067 cmd_len = kvaser_usb_hydra_cmd_size(cmd); 2068 2069 remaining_bytes = min_t(unsigned int, len, 2070 cmd_len - usb_rx_leftover_len); 2071 /* Make sure we do not overflow usb_rx_leftover */ 2072 if (remaining_bytes + usb_rx_leftover_len > 2073 KVASER_USB_HYDRA_MAX_CMD_LEN) { 2074 dev_err(&dev->intf->dev, "Format error\n"); 2075 spin_unlock_irqrestore(usb_rx_leftover_lock, irq_flags); 2076 return; 2077 } 2078 2079 memcpy(card_data->usb_rx_leftover + usb_rx_leftover_len, buf, 2080 remaining_bytes); 2081 pos += remaining_bytes; 2082 2083 if (remaining_bytes + usb_rx_leftover_len == cmd_len) { 2084 kvaser_usb_hydra_handle_cmd(dev, cmd); 2085 usb_rx_leftover_len = 0; 2086 } else { 2087 /* Command still not complete */ 2088 usb_rx_leftover_len += remaining_bytes; 2089 } 2090 card_data->usb_rx_leftover_len = usb_rx_leftover_len; 2091 } 2092 spin_unlock_irqrestore(usb_rx_leftover_lock, irq_flags); 2093 2094 while (pos < len) { 2095 cmd = buf + pos; 2096 2097 cmd_len = kvaser_usb_hydra_cmd_size(cmd); 2098 2099 if (pos + cmd_len > len) { 2100 /* We got first part of a command */ 2101 int leftover_bytes; 2102 2103 leftover_bytes = len - pos; 2104 /* Make sure we do not overflow usb_rx_leftover */ 2105 if (leftover_bytes > KVASER_USB_HYDRA_MAX_CMD_LEN) { 2106 dev_err(&dev->intf->dev, "Format error\n"); 2107 return; 2108 } 2109 spin_lock_irqsave(usb_rx_leftover_lock, irq_flags); 2110 memcpy(card_data->usb_rx_leftover, buf + pos, 2111 leftover_bytes); 2112 card_data->usb_rx_leftover_len = leftover_bytes; 2113 spin_unlock_irqrestore(usb_rx_leftover_lock, irq_flags); 2114 break; 2115 } 2116 2117 kvaser_usb_hydra_handle_cmd(dev, cmd); 2118 pos += cmd_len; 2119 } 2120 } 2121 2122 static void * 2123 kvaser_usb_hydra_frame_to_cmd(const struct kvaser_usb_net_priv *priv, 2124 const struct sk_buff *skb, int *cmd_len, 2125 u16 transid) 2126 { 2127 void *buf; 2128 2129 if (priv->dev->card_data.capabilities & KVASER_USB_HYDRA_CAP_EXT_CMD) 2130 buf = kvaser_usb_hydra_frame_to_cmd_ext(priv, skb, cmd_len, 2131 transid); 2132 else 2133 buf = kvaser_usb_hydra_frame_to_cmd_std(priv, skb, cmd_len, 2134 transid); 2135 2136 return buf; 2137 } 2138 2139 const struct kvaser_usb_dev_ops kvaser_usb_hydra_dev_ops = { 2140 .dev_set_mode = kvaser_usb_hydra_set_mode, 2141 .dev_set_bittiming = kvaser_usb_hydra_set_bittiming, 2142 .dev_get_busparams = kvaser_usb_hydra_get_nominal_busparams, 2143 .dev_set_data_bittiming = kvaser_usb_hydra_set_data_bittiming, 2144 .dev_get_data_busparams = kvaser_usb_hydra_get_data_busparams, 2145 .dev_get_berr_counter = kvaser_usb_hydra_get_berr_counter, 2146 .dev_setup_endpoints = kvaser_usb_hydra_setup_endpoints, 2147 .dev_init_card = kvaser_usb_hydra_init_card, 2148 .dev_init_channel = kvaser_usb_hydra_init_channel, 2149 .dev_get_software_info = kvaser_usb_hydra_get_software_info, 2150 .dev_get_software_details = kvaser_usb_hydra_get_software_details, 2151 .dev_get_card_info = kvaser_usb_hydra_get_card_info, 2152 .dev_get_capabilities = kvaser_usb_hydra_get_capabilities, 2153 .dev_set_opt_mode = kvaser_usb_hydra_set_opt_mode, 2154 .dev_start_chip = kvaser_usb_hydra_start_chip, 2155 .dev_stop_chip = kvaser_usb_hydra_stop_chip, 2156 .dev_reset_chip = NULL, 2157 .dev_flush_queue = kvaser_usb_hydra_flush_queue, 2158 .dev_read_bulk_callback = kvaser_usb_hydra_read_bulk_callback, 2159 .dev_frame_to_cmd = kvaser_usb_hydra_frame_to_cmd, 2160 }; 2161 2162 static const struct kvaser_usb_dev_cfg kvaser_usb_hydra_dev_cfg_kcan = { 2163 .clock = { 2164 .freq = 80 * MEGA /* Hz */, 2165 }, 2166 .timestamp_freq = 80, 2167 .bittiming_const = &kvaser_usb_hydra_kcan_bittiming_c, 2168 .data_bittiming_const = &kvaser_usb_hydra_kcan_bittiming_c, 2169 }; 2170 2171 static const struct kvaser_usb_dev_cfg kvaser_usb_hydra_dev_cfg_flexc = { 2172 .clock = { 2173 .freq = 24 * MEGA /* Hz */, 2174 }, 2175 .timestamp_freq = 1, 2176 .bittiming_const = &kvaser_usb_flexc_bittiming_const, 2177 }; 2178 2179 static const struct kvaser_usb_dev_cfg kvaser_usb_hydra_dev_cfg_rt = { 2180 .clock = { 2181 .freq = 80 * MEGA /* Hz */, 2182 }, 2183 .timestamp_freq = 24, 2184 .bittiming_const = &kvaser_usb_hydra_rt_bittiming_c, 2185 .data_bittiming_const = &kvaser_usb_hydra_rtd_bittiming_c, 2186 }; 2187