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