1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * CAN driver for PEAK System PCAN-USB FD / PCAN-USB Pro FD adapter 4 * 5 * Copyright (C) 2013-2014 Stephane Grosjean <s.grosjean@peak-system.com> 6 */ 7 #include <linux/netdevice.h> 8 #include <linux/usb.h> 9 #include <linux/module.h> 10 #include <linux/ethtool.h> 11 12 #include <linux/can.h> 13 #include <linux/can/dev.h> 14 #include <linux/can/error.h> 15 #include <linux/can/dev/peak_canfd.h> 16 17 #include "pcan_usb_core.h" 18 #include "pcan_usb_pro.h" 19 20 #define PCAN_USBPROFD_CHANNEL_COUNT 2 21 #define PCAN_USBFD_CHANNEL_COUNT 1 22 23 /* PCAN-USB Pro FD adapter internal clock (Hz) */ 24 #define PCAN_UFD_CRYSTAL_HZ 80000000 25 26 #define PCAN_UFD_CMD_BUFFER_SIZE 512 27 #define PCAN_UFD_LOSPD_PKT_SIZE 64 28 29 /* PCAN-USB Pro FD command timeout (ms.) */ 30 #define PCAN_UFD_CMD_TIMEOUT_MS 1000 31 32 /* PCAN-USB Pro FD rx/tx buffers size */ 33 #define PCAN_UFD_RX_BUFFER_SIZE 2048 34 #define PCAN_UFD_TX_BUFFER_SIZE 512 35 36 /* read some versions info from the hw device */ 37 struct __packed pcan_ufd_fw_info { 38 __le16 size_of; /* sizeof this */ 39 __le16 type; /* type of this structure */ 40 u8 hw_type; /* Type of hardware (HW_TYPE_xxx) */ 41 u8 bl_version[3]; /* Bootloader version */ 42 u8 hw_version; /* Hardware version (PCB) */ 43 u8 fw_version[3]; /* Firmware version */ 44 __le32 dev_id[2]; /* "device id" per CAN */ 45 __le32 ser_no; /* S/N */ 46 __le32 flags; /* special functions */ 47 }; 48 49 /* handle device specific info used by the netdevices */ 50 struct pcan_usb_fd_if { 51 struct peak_usb_device *dev[PCAN_USB_MAX_CHANNEL]; 52 struct pcan_ufd_fw_info fw_info; 53 struct peak_time_ref time_ref; 54 int cm_ignore_count; 55 int dev_opened_count; 56 }; 57 58 /* device information */ 59 struct pcan_usb_fd_device { 60 struct peak_usb_device dev; 61 struct can_berr_counter bec; 62 struct pcan_usb_fd_if *usb_if; 63 u8 *cmd_buffer_addr; 64 }; 65 66 /* Extended USB commands (non uCAN commands) */ 67 68 /* Clock Modes command */ 69 #define PCAN_UFD_CMD_CLK_SET 0x80 70 71 #define PCAN_UFD_CLK_80MHZ 0x0 72 #define PCAN_UFD_CLK_60MHZ 0x1 73 #define PCAN_UFD_CLK_40MHZ 0x2 74 #define PCAN_UFD_CLK_30MHZ 0x3 75 #define PCAN_UFD_CLK_24MHZ 0x4 76 #define PCAN_UFD_CLK_20MHZ 0x5 77 #define PCAN_UFD_CLK_DEF PCAN_UFD_CLK_80MHZ 78 79 struct __packed pcan_ufd_clock { 80 __le16 opcode_channel; 81 82 u8 mode; 83 u8 unused[5]; 84 }; 85 86 /* LED control command */ 87 #define PCAN_UFD_CMD_LED_SET 0x86 88 89 #define PCAN_UFD_LED_DEV 0x00 90 #define PCAN_UFD_LED_FAST 0x01 91 #define PCAN_UFD_LED_SLOW 0x02 92 #define PCAN_UFD_LED_ON 0x03 93 #define PCAN_UFD_LED_OFF 0x04 94 #define PCAN_UFD_LED_DEF PCAN_UFD_LED_DEV 95 96 struct __packed pcan_ufd_led { 97 __le16 opcode_channel; 98 99 u8 mode; 100 u8 unused[5]; 101 }; 102 103 /* Extended usage of uCAN commands CMD_xxx_xx_OPTION for PCAN-USB Pro FD */ 104 #define PCAN_UFD_FLTEXT_CALIBRATION 0x8000 105 106 struct __packed pcan_ufd_options { 107 __le16 opcode_channel; 108 109 __le16 ucan_mask; 110 u16 unused; 111 __le16 usb_mask; 112 }; 113 114 /* Extended usage of uCAN messages for PCAN-USB Pro FD */ 115 #define PCAN_UFD_MSG_CALIBRATION 0x100 116 117 struct __packed pcan_ufd_ts_msg { 118 __le16 size; 119 __le16 type; 120 __le32 ts_low; 121 __le32 ts_high; 122 __le16 usb_frame_index; 123 u16 unused; 124 }; 125 126 #define PCAN_UFD_MSG_OVERRUN 0x101 127 128 #define PCAN_UFD_OVMSG_CHANNEL(o) ((o)->channel & 0xf) 129 130 struct __packed pcan_ufd_ovr_msg { 131 __le16 size; 132 __le16 type; 133 __le32 ts_low; 134 __le32 ts_high; 135 u8 channel; 136 u8 unused[3]; 137 }; 138 139 static inline int pufd_omsg_get_channel(struct pcan_ufd_ovr_msg *om) 140 { 141 return om->channel & 0xf; 142 } 143 144 /* Clock mode frequency values */ 145 static const u32 pcan_usb_fd_clk_freq[6] = { 146 [PCAN_UFD_CLK_80MHZ] = 80000000, 147 [PCAN_UFD_CLK_60MHZ] = 60000000, 148 [PCAN_UFD_CLK_40MHZ] = 40000000, 149 [PCAN_UFD_CLK_30MHZ] = 30000000, 150 [PCAN_UFD_CLK_24MHZ] = 24000000, 151 [PCAN_UFD_CLK_20MHZ] = 20000000 152 }; 153 154 /* return a device USB interface */ 155 static inline 156 struct pcan_usb_fd_if *pcan_usb_fd_dev_if(struct peak_usb_device *dev) 157 { 158 struct pcan_usb_fd_device *pdev = 159 container_of(dev, struct pcan_usb_fd_device, dev); 160 return pdev->usb_if; 161 } 162 163 /* return a device USB commands buffer */ 164 static inline void *pcan_usb_fd_cmd_buffer(struct peak_usb_device *dev) 165 { 166 struct pcan_usb_fd_device *pdev = 167 container_of(dev, struct pcan_usb_fd_device, dev); 168 return pdev->cmd_buffer_addr; 169 } 170 171 /* send PCAN-USB Pro FD commands synchronously */ 172 static int pcan_usb_fd_send_cmd(struct peak_usb_device *dev, void *cmd_tail) 173 { 174 void *cmd_head = pcan_usb_fd_cmd_buffer(dev); 175 int err = 0; 176 u8 *packet_ptr; 177 int packet_len; 178 ptrdiff_t cmd_len; 179 180 /* usb device unregistered? */ 181 if (!(dev->state & PCAN_USB_STATE_CONNECTED)) 182 return 0; 183 184 /* if a packet is not filled completely by commands, the command list 185 * is terminated with an "end of collection" record. 186 */ 187 cmd_len = cmd_tail - cmd_head; 188 if (cmd_len <= (PCAN_UFD_CMD_BUFFER_SIZE - sizeof(u64))) { 189 memset(cmd_tail, 0xff, sizeof(u64)); 190 cmd_len += sizeof(u64); 191 } 192 193 packet_ptr = cmd_head; 194 packet_len = cmd_len; 195 196 /* firmware is not able to re-assemble 512 bytes buffer in full-speed */ 197 if (unlikely(dev->udev->speed != USB_SPEED_HIGH)) 198 packet_len = min(packet_len, PCAN_UFD_LOSPD_PKT_SIZE); 199 200 do { 201 err = usb_bulk_msg(dev->udev, 202 usb_sndbulkpipe(dev->udev, 203 PCAN_USBPRO_EP_CMDOUT), 204 packet_ptr, packet_len, 205 NULL, PCAN_UFD_CMD_TIMEOUT_MS); 206 if (err) { 207 netdev_err(dev->netdev, 208 "sending command failure: %d\n", err); 209 break; 210 } 211 212 packet_ptr += packet_len; 213 cmd_len -= packet_len; 214 215 if (cmd_len < PCAN_UFD_LOSPD_PKT_SIZE) 216 packet_len = cmd_len; 217 218 } while (packet_len > 0); 219 220 return err; 221 } 222 223 /* build the commands list in the given buffer, to enter operational mode */ 224 static int pcan_usb_fd_build_restart_cmd(struct peak_usb_device *dev, u8 *buf) 225 { 226 struct pucan_wr_err_cnt *prc; 227 struct pucan_command *cmd; 228 u8 *pc = buf; 229 230 /* 1st, reset error counters: */ 231 prc = (struct pucan_wr_err_cnt *)pc; 232 prc->opcode_channel = pucan_cmd_opcode_channel(dev->ctrl_idx, 233 PUCAN_CMD_WR_ERR_CNT); 234 235 /* select both counters */ 236 prc->sel_mask = cpu_to_le16(PUCAN_WRERRCNT_TE|PUCAN_WRERRCNT_RE); 237 238 /* and reset their values */ 239 prc->tx_counter = 0; 240 prc->rx_counter = 0; 241 242 /* moves the pointer forward */ 243 pc += sizeof(struct pucan_wr_err_cnt); 244 245 /* add command to switch from ISO to non-ISO mode, if fw allows it */ 246 if (dev->can.ctrlmode_supported & CAN_CTRLMODE_FD_NON_ISO) { 247 struct pucan_options *puo = (struct pucan_options *)pc; 248 249 puo->opcode_channel = 250 (dev->can.ctrlmode & CAN_CTRLMODE_FD_NON_ISO) ? 251 pucan_cmd_opcode_channel(dev->ctrl_idx, 252 PUCAN_CMD_CLR_DIS_OPTION) : 253 pucan_cmd_opcode_channel(dev->ctrl_idx, 254 PUCAN_CMD_SET_EN_OPTION); 255 256 puo->options = cpu_to_le16(PUCAN_OPTION_CANDFDISO); 257 258 /* to be sure that no other extended bits will be taken into 259 * account 260 */ 261 puo->unused = 0; 262 263 /* moves the pointer forward */ 264 pc += sizeof(struct pucan_options); 265 } 266 267 /* next, go back to operational mode */ 268 cmd = (struct pucan_command *)pc; 269 cmd->opcode_channel = pucan_cmd_opcode_channel(dev->ctrl_idx, 270 (dev->can.ctrlmode & CAN_CTRLMODE_LISTENONLY) ? 271 PUCAN_CMD_LISTEN_ONLY_MODE : 272 PUCAN_CMD_NORMAL_MODE); 273 pc += sizeof(struct pucan_command); 274 275 return pc - buf; 276 } 277 278 /* set CAN bus on/off */ 279 static int pcan_usb_fd_set_bus(struct peak_usb_device *dev, u8 onoff) 280 { 281 u8 *pc = pcan_usb_fd_cmd_buffer(dev); 282 int l; 283 284 if (onoff) { 285 /* build the cmds list to enter operational mode */ 286 l = pcan_usb_fd_build_restart_cmd(dev, pc); 287 } else { 288 struct pucan_command *cmd = (struct pucan_command *)pc; 289 290 /* build cmd to go back to reset mode */ 291 cmd->opcode_channel = pucan_cmd_opcode_channel(dev->ctrl_idx, 292 PUCAN_CMD_RESET_MODE); 293 l = sizeof(struct pucan_command); 294 } 295 296 /* send the command */ 297 return pcan_usb_fd_send_cmd(dev, pc + l); 298 } 299 300 /* set filtering masks: 301 * 302 * idx in range [0..63] selects a row #idx, all rows otherwise 303 * mask in range [0..0xffffffff] defines up to 32 CANIDs in the row(s) 304 * 305 * Each bit of this 64 x 32 bits array defines a CANID value: 306 * 307 * bit[i,j] = 1 implies that CANID=(i x 32)+j will be received, while 308 * bit[i,j] = 0 implies that CANID=(i x 32)+j will be discarded. 309 */ 310 static int pcan_usb_fd_set_filter_std(struct peak_usb_device *dev, int idx, 311 u32 mask) 312 { 313 struct pucan_filter_std *cmd = pcan_usb_fd_cmd_buffer(dev); 314 int i, n; 315 316 /* select all rows when idx is out of range [0..63] */ 317 if ((idx < 0) || (idx >= (1 << PUCAN_FLTSTD_ROW_IDX_BITS))) { 318 n = 1 << PUCAN_FLTSTD_ROW_IDX_BITS; 319 idx = 0; 320 321 /* select the row (and only the row) otherwise */ 322 } else { 323 n = idx + 1; 324 } 325 326 for (i = idx; i < n; i++, cmd++) { 327 cmd->opcode_channel = pucan_cmd_opcode_channel(dev->ctrl_idx, 328 PUCAN_CMD_FILTER_STD); 329 cmd->idx = cpu_to_le16(i); 330 cmd->mask = cpu_to_le32(mask); 331 } 332 333 /* send the command */ 334 return pcan_usb_fd_send_cmd(dev, cmd); 335 } 336 337 /* set/unset options 338 * 339 * onoff set(1)/unset(0) options 340 * mask each bit defines a kind of options to set/unset 341 */ 342 static int pcan_usb_fd_set_options(struct peak_usb_device *dev, 343 bool onoff, u16 ucan_mask, u16 usb_mask) 344 { 345 struct pcan_ufd_options *cmd = pcan_usb_fd_cmd_buffer(dev); 346 347 cmd->opcode_channel = pucan_cmd_opcode_channel(dev->ctrl_idx, 348 (onoff) ? PUCAN_CMD_SET_EN_OPTION : 349 PUCAN_CMD_CLR_DIS_OPTION); 350 351 cmd->ucan_mask = cpu_to_le16(ucan_mask); 352 cmd->usb_mask = cpu_to_le16(usb_mask); 353 354 /* send the command */ 355 return pcan_usb_fd_send_cmd(dev, ++cmd); 356 } 357 358 /* setup LED control */ 359 static int pcan_usb_fd_set_can_led(struct peak_usb_device *dev, u8 led_mode) 360 { 361 struct pcan_ufd_led *cmd = pcan_usb_fd_cmd_buffer(dev); 362 363 cmd->opcode_channel = pucan_cmd_opcode_channel(dev->ctrl_idx, 364 PCAN_UFD_CMD_LED_SET); 365 cmd->mode = led_mode; 366 367 /* send the command */ 368 return pcan_usb_fd_send_cmd(dev, ++cmd); 369 } 370 371 /* set CAN clock domain */ 372 static int pcan_usb_fd_set_clock_domain(struct peak_usb_device *dev, 373 u8 clk_mode) 374 { 375 struct pcan_ufd_clock *cmd = pcan_usb_fd_cmd_buffer(dev); 376 377 cmd->opcode_channel = pucan_cmd_opcode_channel(dev->ctrl_idx, 378 PCAN_UFD_CMD_CLK_SET); 379 cmd->mode = clk_mode; 380 381 /* send the command */ 382 return pcan_usb_fd_send_cmd(dev, ++cmd); 383 } 384 385 /* set bittiming for CAN and CAN-FD header */ 386 static int pcan_usb_fd_set_bittiming_slow(struct peak_usb_device *dev, 387 struct can_bittiming *bt) 388 { 389 struct pucan_timing_slow *cmd = pcan_usb_fd_cmd_buffer(dev); 390 391 cmd->opcode_channel = pucan_cmd_opcode_channel(dev->ctrl_idx, 392 PUCAN_CMD_TIMING_SLOW); 393 cmd->sjw_t = PUCAN_TSLOW_SJW_T(bt->sjw - 1, 394 dev->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES); 395 396 cmd->tseg2 = PUCAN_TSLOW_TSEG2(bt->phase_seg2 - 1); 397 cmd->tseg1 = PUCAN_TSLOW_TSEG1(bt->prop_seg + bt->phase_seg1 - 1); 398 cmd->brp = cpu_to_le16(PUCAN_TSLOW_BRP(bt->brp - 1)); 399 400 cmd->ewl = 96; /* default */ 401 402 /* send the command */ 403 return pcan_usb_fd_send_cmd(dev, ++cmd); 404 } 405 406 /* set CAN-FD bittiming for data */ 407 static int pcan_usb_fd_set_bittiming_fast(struct peak_usb_device *dev, 408 struct can_bittiming *bt) 409 { 410 struct pucan_timing_fast *cmd = pcan_usb_fd_cmd_buffer(dev); 411 412 cmd->opcode_channel = pucan_cmd_opcode_channel(dev->ctrl_idx, 413 PUCAN_CMD_TIMING_FAST); 414 cmd->sjw = PUCAN_TFAST_SJW(bt->sjw - 1); 415 cmd->tseg2 = PUCAN_TFAST_TSEG2(bt->phase_seg2 - 1); 416 cmd->tseg1 = PUCAN_TFAST_TSEG1(bt->prop_seg + bt->phase_seg1 - 1); 417 cmd->brp = cpu_to_le16(PUCAN_TFAST_BRP(bt->brp - 1)); 418 419 /* send the command */ 420 return pcan_usb_fd_send_cmd(dev, ++cmd); 421 } 422 423 /* handle restart but in asynchronously way 424 * (uses PCAN-USB Pro code to complete asynchronous request) 425 */ 426 static int pcan_usb_fd_restart_async(struct peak_usb_device *dev, 427 struct urb *urb, u8 *buf) 428 { 429 u8 *pc = buf; 430 431 /* build the entire cmds list in the provided buffer, to go back into 432 * operational mode. 433 */ 434 pc += pcan_usb_fd_build_restart_cmd(dev, pc); 435 436 /* add EOC */ 437 memset(pc, 0xff, sizeof(struct pucan_command)); 438 pc += sizeof(struct pucan_command); 439 440 /* complete the URB */ 441 usb_fill_bulk_urb(urb, dev->udev, 442 usb_sndbulkpipe(dev->udev, PCAN_USBPRO_EP_CMDOUT), 443 buf, pc - buf, 444 pcan_usb_pro_restart_complete, dev); 445 446 /* and submit it. */ 447 return usb_submit_urb(urb, GFP_ATOMIC); 448 } 449 450 static int pcan_usb_fd_drv_loaded(struct peak_usb_device *dev, bool loaded) 451 { 452 struct pcan_usb_fd_device *pdev = 453 container_of(dev, struct pcan_usb_fd_device, dev); 454 455 pdev->cmd_buffer_addr[0] = 0; 456 pdev->cmd_buffer_addr[1] = !!loaded; 457 458 return pcan_usb_pro_send_req(dev, 459 PCAN_USBPRO_REQ_FCT, 460 PCAN_USBPRO_FCT_DRVLD, 461 pdev->cmd_buffer_addr, 462 PCAN_USBPRO_FCT_DRVLD_REQ_LEN); 463 } 464 465 static int pcan_usb_fd_decode_canmsg(struct pcan_usb_fd_if *usb_if, 466 struct pucan_msg *rx_msg) 467 { 468 struct pucan_rx_msg *rm = (struct pucan_rx_msg *)rx_msg; 469 struct peak_usb_device *dev; 470 struct net_device *netdev; 471 struct canfd_frame *cfd; 472 struct sk_buff *skb; 473 const u16 rx_msg_flags = le16_to_cpu(rm->flags); 474 475 if (pucan_msg_get_channel(rm) >= ARRAY_SIZE(usb_if->dev)) 476 return -ENOMEM; 477 478 dev = usb_if->dev[pucan_msg_get_channel(rm)]; 479 netdev = dev->netdev; 480 481 if (rx_msg_flags & PUCAN_MSG_EXT_DATA_LEN) { 482 /* CANFD frame case */ 483 skb = alloc_canfd_skb(netdev, &cfd); 484 if (!skb) 485 return -ENOMEM; 486 487 if (rx_msg_flags & PUCAN_MSG_BITRATE_SWITCH) 488 cfd->flags |= CANFD_BRS; 489 490 if (rx_msg_flags & PUCAN_MSG_ERROR_STATE_IND) 491 cfd->flags |= CANFD_ESI; 492 493 cfd->len = can_fd_dlc2len(pucan_msg_get_dlc(rm)); 494 } else { 495 /* CAN 2.0 frame case */ 496 skb = alloc_can_skb(netdev, (struct can_frame **)&cfd); 497 if (!skb) 498 return -ENOMEM; 499 500 can_frame_set_cc_len((struct can_frame *)cfd, 501 pucan_msg_get_dlc(rm), 502 dev->can.ctrlmode); 503 } 504 505 cfd->can_id = le32_to_cpu(rm->can_id); 506 507 if (rx_msg_flags & PUCAN_MSG_EXT_ID) 508 cfd->can_id |= CAN_EFF_FLAG; 509 510 if (rx_msg_flags & PUCAN_MSG_RTR) 511 cfd->can_id |= CAN_RTR_FLAG; 512 else 513 memcpy(cfd->data, rm->d, cfd->len); 514 515 netdev->stats.rx_packets++; 516 netdev->stats.rx_bytes += cfd->len; 517 518 peak_usb_netif_rx_64(skb, le32_to_cpu(rm->ts_low), 519 le32_to_cpu(rm->ts_high)); 520 521 return 0; 522 } 523 524 /* handle uCAN status message */ 525 static int pcan_usb_fd_decode_status(struct pcan_usb_fd_if *usb_if, 526 struct pucan_msg *rx_msg) 527 { 528 struct pucan_status_msg *sm = (struct pucan_status_msg *)rx_msg; 529 struct pcan_usb_fd_device *pdev; 530 enum can_state new_state = CAN_STATE_ERROR_ACTIVE; 531 enum can_state rx_state, tx_state; 532 struct peak_usb_device *dev; 533 struct net_device *netdev; 534 struct can_frame *cf; 535 struct sk_buff *skb; 536 537 if (pucan_stmsg_get_channel(sm) >= ARRAY_SIZE(usb_if->dev)) 538 return -ENOMEM; 539 540 dev = usb_if->dev[pucan_stmsg_get_channel(sm)]; 541 pdev = container_of(dev, struct pcan_usb_fd_device, dev); 542 netdev = dev->netdev; 543 544 /* nothing should be sent while in BUS_OFF state */ 545 if (dev->can.state == CAN_STATE_BUS_OFF) 546 return 0; 547 548 if (sm->channel_p_w_b & PUCAN_BUS_BUSOFF) { 549 new_state = CAN_STATE_BUS_OFF; 550 } else if (sm->channel_p_w_b & PUCAN_BUS_PASSIVE) { 551 new_state = CAN_STATE_ERROR_PASSIVE; 552 } else if (sm->channel_p_w_b & PUCAN_BUS_WARNING) { 553 new_state = CAN_STATE_ERROR_WARNING; 554 } else { 555 /* back to (or still in) ERROR_ACTIVE state */ 556 new_state = CAN_STATE_ERROR_ACTIVE; 557 pdev->bec.txerr = 0; 558 pdev->bec.rxerr = 0; 559 } 560 561 /* state hasn't changed */ 562 if (new_state == dev->can.state) 563 return 0; 564 565 /* handle bus state change */ 566 tx_state = (pdev->bec.txerr >= pdev->bec.rxerr) ? new_state : 0; 567 rx_state = (pdev->bec.txerr <= pdev->bec.rxerr) ? new_state : 0; 568 569 /* allocate an skb to store the error frame */ 570 skb = alloc_can_err_skb(netdev, &cf); 571 can_change_state(netdev, cf, tx_state, rx_state); 572 573 /* things must be done even in case of OOM */ 574 if (new_state == CAN_STATE_BUS_OFF) 575 can_bus_off(netdev); 576 577 if (!skb) 578 return -ENOMEM; 579 580 peak_usb_netif_rx_64(skb, le32_to_cpu(sm->ts_low), 581 le32_to_cpu(sm->ts_high)); 582 583 return 0; 584 } 585 586 /* handle uCAN error message */ 587 static int pcan_usb_fd_decode_error(struct pcan_usb_fd_if *usb_if, 588 struct pucan_msg *rx_msg) 589 { 590 struct pucan_error_msg *er = (struct pucan_error_msg *)rx_msg; 591 struct pcan_usb_fd_device *pdev; 592 struct peak_usb_device *dev; 593 594 if (pucan_ermsg_get_channel(er) >= ARRAY_SIZE(usb_if->dev)) 595 return -EINVAL; 596 597 dev = usb_if->dev[pucan_ermsg_get_channel(er)]; 598 pdev = container_of(dev, struct pcan_usb_fd_device, dev); 599 600 /* keep a trace of tx and rx error counters for later use */ 601 pdev->bec.txerr = er->tx_err_cnt; 602 pdev->bec.rxerr = er->rx_err_cnt; 603 604 return 0; 605 } 606 607 /* handle uCAN overrun message */ 608 static int pcan_usb_fd_decode_overrun(struct pcan_usb_fd_if *usb_if, 609 struct pucan_msg *rx_msg) 610 { 611 struct pcan_ufd_ovr_msg *ov = (struct pcan_ufd_ovr_msg *)rx_msg; 612 struct peak_usb_device *dev; 613 struct net_device *netdev; 614 struct can_frame *cf; 615 struct sk_buff *skb; 616 617 if (pufd_omsg_get_channel(ov) >= ARRAY_SIZE(usb_if->dev)) 618 return -EINVAL; 619 620 dev = usb_if->dev[pufd_omsg_get_channel(ov)]; 621 netdev = dev->netdev; 622 623 /* allocate an skb to store the error frame */ 624 skb = alloc_can_err_skb(netdev, &cf); 625 if (!skb) 626 return -ENOMEM; 627 628 cf->can_id |= CAN_ERR_CRTL; 629 cf->data[1] |= CAN_ERR_CRTL_RX_OVERFLOW; 630 631 peak_usb_netif_rx_64(skb, le32_to_cpu(ov->ts_low), 632 le32_to_cpu(ov->ts_high)); 633 634 netdev->stats.rx_over_errors++; 635 netdev->stats.rx_errors++; 636 637 return 0; 638 } 639 640 /* handle USB calibration message */ 641 static void pcan_usb_fd_decode_ts(struct pcan_usb_fd_if *usb_if, 642 struct pucan_msg *rx_msg) 643 { 644 struct pcan_ufd_ts_msg *ts = (struct pcan_ufd_ts_msg *)rx_msg; 645 646 /* should wait until clock is stabilized */ 647 if (usb_if->cm_ignore_count > 0) 648 usb_if->cm_ignore_count--; 649 else 650 peak_usb_set_ts_now(&usb_if->time_ref, le32_to_cpu(ts->ts_low)); 651 } 652 653 /* callback for bulk IN urb */ 654 static int pcan_usb_fd_decode_buf(struct peak_usb_device *dev, struct urb *urb) 655 { 656 struct pcan_usb_fd_if *usb_if = pcan_usb_fd_dev_if(dev); 657 struct net_device *netdev = dev->netdev; 658 struct pucan_msg *rx_msg; 659 u8 *msg_ptr, *msg_end; 660 int err = 0; 661 662 /* loop reading all the records from the incoming message */ 663 msg_ptr = urb->transfer_buffer; 664 msg_end = urb->transfer_buffer + urb->actual_length; 665 for (; msg_ptr < msg_end;) { 666 u16 rx_msg_type, rx_msg_size; 667 668 rx_msg = (struct pucan_msg *)msg_ptr; 669 if (!rx_msg->size) { 670 /* null packet found: end of list */ 671 break; 672 } 673 674 rx_msg_size = le16_to_cpu(rx_msg->size); 675 rx_msg_type = le16_to_cpu(rx_msg->type); 676 677 /* check if the record goes out of current packet */ 678 if (msg_ptr + rx_msg_size > msg_end) { 679 netdev_err(netdev, 680 "got frag rec: should inc usb rx buf sze\n"); 681 err = -EBADMSG; 682 break; 683 } 684 685 switch (rx_msg_type) { 686 case PUCAN_MSG_CAN_RX: 687 err = pcan_usb_fd_decode_canmsg(usb_if, rx_msg); 688 if (err < 0) 689 goto fail; 690 break; 691 692 case PCAN_UFD_MSG_CALIBRATION: 693 pcan_usb_fd_decode_ts(usb_if, rx_msg); 694 break; 695 696 case PUCAN_MSG_ERROR: 697 err = pcan_usb_fd_decode_error(usb_if, rx_msg); 698 if (err < 0) 699 goto fail; 700 break; 701 702 case PUCAN_MSG_STATUS: 703 err = pcan_usb_fd_decode_status(usb_if, rx_msg); 704 if (err < 0) 705 goto fail; 706 break; 707 708 case PCAN_UFD_MSG_OVERRUN: 709 err = pcan_usb_fd_decode_overrun(usb_if, rx_msg); 710 if (err < 0) 711 goto fail; 712 break; 713 714 default: 715 netdev_err(netdev, 716 "unhandled msg type 0x%02x (%d): ignored\n", 717 rx_msg_type, rx_msg_type); 718 break; 719 } 720 721 msg_ptr += rx_msg_size; 722 } 723 724 fail: 725 if (err) 726 pcan_dump_mem("received msg", 727 urb->transfer_buffer, urb->actual_length); 728 return err; 729 } 730 731 /* CAN/CANFD frames encoding callback */ 732 static int pcan_usb_fd_encode_msg(struct peak_usb_device *dev, 733 struct sk_buff *skb, u8 *obuf, size_t *size) 734 { 735 struct pucan_tx_msg *tx_msg = (struct pucan_tx_msg *)obuf; 736 struct canfd_frame *cfd = (struct canfd_frame *)skb->data; 737 u16 tx_msg_size, tx_msg_flags; 738 u8 dlc; 739 740 if (cfd->len > CANFD_MAX_DLEN) 741 return -EINVAL; 742 743 tx_msg_size = ALIGN(sizeof(struct pucan_tx_msg) + cfd->len, 4); 744 tx_msg->size = cpu_to_le16(tx_msg_size); 745 tx_msg->type = cpu_to_le16(PUCAN_MSG_CAN_TX); 746 747 tx_msg_flags = 0; 748 if (cfd->can_id & CAN_EFF_FLAG) { 749 tx_msg_flags |= PUCAN_MSG_EXT_ID; 750 tx_msg->can_id = cpu_to_le32(cfd->can_id & CAN_EFF_MASK); 751 } else { 752 tx_msg->can_id = cpu_to_le32(cfd->can_id & CAN_SFF_MASK); 753 } 754 755 if (can_is_canfd_skb(skb)) { 756 /* considering a CANFD frame */ 757 dlc = can_fd_len2dlc(cfd->len); 758 759 tx_msg_flags |= PUCAN_MSG_EXT_DATA_LEN; 760 761 if (cfd->flags & CANFD_BRS) 762 tx_msg_flags |= PUCAN_MSG_BITRATE_SWITCH; 763 764 if (cfd->flags & CANFD_ESI) 765 tx_msg_flags |= PUCAN_MSG_ERROR_STATE_IND; 766 } else { 767 /* CAND 2.0 frames */ 768 dlc = can_get_cc_dlc((struct can_frame *)cfd, 769 dev->can.ctrlmode); 770 771 if (cfd->can_id & CAN_RTR_FLAG) 772 tx_msg_flags |= PUCAN_MSG_RTR; 773 } 774 775 /* Single-Shot frame */ 776 if (dev->can.ctrlmode & CAN_CTRLMODE_ONE_SHOT) 777 tx_msg_flags |= PUCAN_MSG_SINGLE_SHOT; 778 779 tx_msg->flags = cpu_to_le16(tx_msg_flags); 780 tx_msg->channel_dlc = PUCAN_MSG_CHANNEL_DLC(dev->ctrl_idx, dlc); 781 memcpy(tx_msg->d, cfd->data, cfd->len); 782 783 /* add null size message to tag the end (messages are 32-bits aligned) 784 */ 785 tx_msg = (struct pucan_tx_msg *)(obuf + tx_msg_size); 786 787 tx_msg->size = 0; 788 789 /* set the whole size of the USB packet to send */ 790 *size = tx_msg_size + sizeof(u32); 791 792 return 0; 793 } 794 795 /* start the interface (last chance before set bus on) */ 796 static int pcan_usb_fd_start(struct peak_usb_device *dev) 797 { 798 struct pcan_usb_fd_device *pdev = 799 container_of(dev, struct pcan_usb_fd_device, dev); 800 int err; 801 802 /* set filter mode: all acceptance */ 803 err = pcan_usb_fd_set_filter_std(dev, -1, 0xffffffff); 804 if (err) 805 return err; 806 807 /* opening first device: */ 808 if (pdev->usb_if->dev_opened_count == 0) { 809 /* reset time_ref */ 810 peak_usb_init_time_ref(&pdev->usb_if->time_ref, 811 &pcan_usb_pro_fd); 812 813 /* enable USB calibration messages */ 814 err = pcan_usb_fd_set_options(dev, 1, 815 PUCAN_OPTION_ERROR, 816 PCAN_UFD_FLTEXT_CALIBRATION); 817 } 818 819 pdev->usb_if->dev_opened_count++; 820 821 /* reset cached error counters */ 822 pdev->bec.txerr = 0; 823 pdev->bec.rxerr = 0; 824 825 return err; 826 } 827 828 /* socket callback used to copy berr counters values received through USB */ 829 static int pcan_usb_fd_get_berr_counter(const struct net_device *netdev, 830 struct can_berr_counter *bec) 831 { 832 struct peak_usb_device *dev = netdev_priv(netdev); 833 struct pcan_usb_fd_device *pdev = 834 container_of(dev, struct pcan_usb_fd_device, dev); 835 836 *bec = pdev->bec; 837 838 /* must return 0 */ 839 return 0; 840 } 841 842 /* stop interface (last chance before set bus off) */ 843 static int pcan_usb_fd_stop(struct peak_usb_device *dev) 844 { 845 struct pcan_usb_fd_device *pdev = 846 container_of(dev, struct pcan_usb_fd_device, dev); 847 848 /* turn off special msgs for that interface if no other dev opened */ 849 if (pdev->usb_if->dev_opened_count == 1) 850 pcan_usb_fd_set_options(dev, 0, 851 PUCAN_OPTION_ERROR, 852 PCAN_UFD_FLTEXT_CALIBRATION); 853 pdev->usb_if->dev_opened_count--; 854 855 return 0; 856 } 857 858 /* called when probing, to initialize a device object */ 859 static int pcan_usb_fd_init(struct peak_usb_device *dev) 860 { 861 struct pcan_usb_fd_device *pdev = 862 container_of(dev, struct pcan_usb_fd_device, dev); 863 int i, err = -ENOMEM; 864 865 /* do this for 1st channel only */ 866 if (!dev->prev_siblings) { 867 /* allocate netdevices common structure attached to first one */ 868 pdev->usb_if = kzalloc(sizeof(*pdev->usb_if), GFP_KERNEL); 869 if (!pdev->usb_if) 870 goto err_out; 871 872 /* allocate command buffer once for all for the interface */ 873 pdev->cmd_buffer_addr = kzalloc(PCAN_UFD_CMD_BUFFER_SIZE, 874 GFP_KERNEL); 875 if (!pdev->cmd_buffer_addr) 876 goto err_out_1; 877 878 /* number of ts msgs to ignore before taking one into account */ 879 pdev->usb_if->cm_ignore_count = 5; 880 881 err = pcan_usb_pro_send_req(dev, PCAN_USBPRO_REQ_INFO, 882 PCAN_USBPRO_INFO_FW, 883 &pdev->usb_if->fw_info, 884 sizeof(pdev->usb_if->fw_info)); 885 if (err) { 886 dev_err(dev->netdev->dev.parent, 887 "unable to read %s firmware info (err %d)\n", 888 dev->adapter->name, err); 889 goto err_out_2; 890 } 891 892 /* explicit use of dev_xxx() instead of netdev_xxx() here: 893 * information displayed are related to the device itself, not 894 * to the canx (channel) device. 895 */ 896 dev_info(dev->netdev->dev.parent, 897 "PEAK-System %s v%u fw v%u.%u.%u (%u channels)\n", 898 dev->adapter->name, pdev->usb_if->fw_info.hw_version, 899 pdev->usb_if->fw_info.fw_version[0], 900 pdev->usb_if->fw_info.fw_version[1], 901 pdev->usb_if->fw_info.fw_version[2], 902 dev->adapter->ctrl_count); 903 904 /* check for ability to switch between ISO/non-ISO modes */ 905 if (pdev->usb_if->fw_info.fw_version[0] >= 2) { 906 /* firmware >= 2.x supports ISO/non-ISO switching */ 907 dev->can.ctrlmode_supported |= CAN_CTRLMODE_FD_NON_ISO; 908 } else { 909 /* firmware < 2.x only supports fixed(!) non-ISO */ 910 dev->can.ctrlmode |= CAN_CTRLMODE_FD_NON_ISO; 911 } 912 913 /* tell the hardware the can driver is running */ 914 err = pcan_usb_fd_drv_loaded(dev, 1); 915 if (err) { 916 dev_err(dev->netdev->dev.parent, 917 "unable to tell %s driver is loaded (err %d)\n", 918 dev->adapter->name, err); 919 goto err_out_2; 920 } 921 } else { 922 /* otherwise, simply copy previous sibling's values */ 923 struct pcan_usb_fd_device *ppdev = 924 container_of(dev->prev_siblings, 925 struct pcan_usb_fd_device, dev); 926 927 pdev->usb_if = ppdev->usb_if; 928 pdev->cmd_buffer_addr = ppdev->cmd_buffer_addr; 929 930 /* do a copy of the ctrlmode[_supported] too */ 931 dev->can.ctrlmode = ppdev->dev.can.ctrlmode; 932 dev->can.ctrlmode_supported = ppdev->dev.can.ctrlmode_supported; 933 } 934 935 pdev->usb_if->dev[dev->ctrl_idx] = dev; 936 dev->device_number = 937 le32_to_cpu(pdev->usb_if->fw_info.dev_id[dev->ctrl_idx]); 938 939 /* set clock domain */ 940 for (i = 0; i < ARRAY_SIZE(pcan_usb_fd_clk_freq); i++) 941 if (dev->adapter->clock.freq == pcan_usb_fd_clk_freq[i]) 942 break; 943 944 if (i >= ARRAY_SIZE(pcan_usb_fd_clk_freq)) { 945 dev_warn(dev->netdev->dev.parent, 946 "incompatible clock frequencies\n"); 947 err = -EINVAL; 948 goto err_out_2; 949 } 950 951 pcan_usb_fd_set_clock_domain(dev, i); 952 953 /* set LED in default state (end of init phase) */ 954 pcan_usb_fd_set_can_led(dev, PCAN_UFD_LED_DEF); 955 956 return 0; 957 958 err_out_2: 959 kfree(pdev->cmd_buffer_addr); 960 err_out_1: 961 kfree(pdev->usb_if); 962 err_out: 963 return err; 964 } 965 966 /* called when driver module is being unloaded */ 967 static void pcan_usb_fd_exit(struct peak_usb_device *dev) 968 { 969 struct pcan_usb_fd_device *pdev = 970 container_of(dev, struct pcan_usb_fd_device, dev); 971 972 /* when rmmod called before unplug and if down, should reset things 973 * before leaving 974 */ 975 if (dev->can.state != CAN_STATE_STOPPED) { 976 /* set bus off on the corresponding channel */ 977 pcan_usb_fd_set_bus(dev, 0); 978 } 979 980 /* switch off corresponding CAN LEDs */ 981 pcan_usb_fd_set_can_led(dev, PCAN_UFD_LED_OFF); 982 983 /* if channel #0 (only) */ 984 if (dev->ctrl_idx == 0) { 985 /* turn off calibration message if any device were opened */ 986 if (pdev->usb_if->dev_opened_count > 0) 987 pcan_usb_fd_set_options(dev, 0, 988 PUCAN_OPTION_ERROR, 989 PCAN_UFD_FLTEXT_CALIBRATION); 990 991 /* tell USB adapter that the driver is being unloaded */ 992 pcan_usb_fd_drv_loaded(dev, 0); 993 } 994 } 995 996 /* called when the USB adapter is unplugged */ 997 static void pcan_usb_fd_free(struct peak_usb_device *dev) 998 { 999 /* last device: can free shared objects now */ 1000 if (!dev->prev_siblings && !dev->next_siblings) { 1001 struct pcan_usb_fd_device *pdev = 1002 container_of(dev, struct pcan_usb_fd_device, dev); 1003 1004 /* free commands buffer */ 1005 kfree(pdev->cmd_buffer_addr); 1006 1007 /* free usb interface object */ 1008 kfree(pdev->usb_if); 1009 } 1010 } 1011 1012 /* blink LED's */ 1013 static int pcan_usb_fd_set_phys_id(struct net_device *netdev, 1014 enum ethtool_phys_id_state state) 1015 { 1016 struct peak_usb_device *dev = netdev_priv(netdev); 1017 int err = 0; 1018 1019 switch (state) { 1020 case ETHTOOL_ID_ACTIVE: 1021 err = pcan_usb_fd_set_can_led(dev, PCAN_UFD_LED_FAST); 1022 break; 1023 case ETHTOOL_ID_INACTIVE: 1024 err = pcan_usb_fd_set_can_led(dev, PCAN_UFD_LED_DEF); 1025 break; 1026 default: 1027 break; 1028 } 1029 1030 return err; 1031 } 1032 1033 static const struct ethtool_ops pcan_usb_fd_ethtool_ops = { 1034 .set_phys_id = pcan_usb_fd_set_phys_id, 1035 }; 1036 1037 /* describes the PCAN-USB FD adapter */ 1038 static const struct can_bittiming_const pcan_usb_fd_const = { 1039 .name = "pcan_usb_fd", 1040 .tseg1_min = 1, 1041 .tseg1_max = (1 << PUCAN_TSLOW_TSGEG1_BITS), 1042 .tseg2_min = 1, 1043 .tseg2_max = (1 << PUCAN_TSLOW_TSGEG2_BITS), 1044 .sjw_max = (1 << PUCAN_TSLOW_SJW_BITS), 1045 .brp_min = 1, 1046 .brp_max = (1 << PUCAN_TSLOW_BRP_BITS), 1047 .brp_inc = 1, 1048 }; 1049 1050 static const struct can_bittiming_const pcan_usb_fd_data_const = { 1051 .name = "pcan_usb_fd", 1052 .tseg1_min = 1, 1053 .tseg1_max = (1 << PUCAN_TFAST_TSGEG1_BITS), 1054 .tseg2_min = 1, 1055 .tseg2_max = (1 << PUCAN_TFAST_TSGEG2_BITS), 1056 .sjw_max = (1 << PUCAN_TFAST_SJW_BITS), 1057 .brp_min = 1, 1058 .brp_max = (1 << PUCAN_TFAST_BRP_BITS), 1059 .brp_inc = 1, 1060 }; 1061 1062 const struct peak_usb_adapter pcan_usb_fd = { 1063 .name = "PCAN-USB FD", 1064 .device_id = PCAN_USBFD_PRODUCT_ID, 1065 .ctrl_count = PCAN_USBFD_CHANNEL_COUNT, 1066 .ctrlmode_supported = CAN_CTRLMODE_FD | 1067 CAN_CTRLMODE_3_SAMPLES | CAN_CTRLMODE_LISTENONLY | 1068 CAN_CTRLMODE_ONE_SHOT | CAN_CTRLMODE_CC_LEN8_DLC, 1069 .clock = { 1070 .freq = PCAN_UFD_CRYSTAL_HZ, 1071 }, 1072 .bittiming_const = &pcan_usb_fd_const, 1073 .data_bittiming_const = &pcan_usb_fd_data_const, 1074 1075 /* size of device private data */ 1076 .sizeof_dev_private = sizeof(struct pcan_usb_fd_device), 1077 1078 .ethtool_ops = &pcan_usb_fd_ethtool_ops, 1079 1080 /* timestamps usage */ 1081 .ts_used_bits = 32, 1082 .us_per_ts_scale = 1, /* us = (ts * scale) >> shift */ 1083 .us_per_ts_shift = 0, 1084 1085 /* give here messages in/out endpoints */ 1086 .ep_msg_in = PCAN_USBPRO_EP_MSGIN, 1087 .ep_msg_out = {PCAN_USBPRO_EP_MSGOUT_0}, 1088 1089 /* size of rx/tx usb buffers */ 1090 .rx_buffer_size = PCAN_UFD_RX_BUFFER_SIZE, 1091 .tx_buffer_size = PCAN_UFD_TX_BUFFER_SIZE, 1092 1093 /* device callbacks */ 1094 .intf_probe = pcan_usb_pro_probe, /* same as PCAN-USB Pro */ 1095 .dev_init = pcan_usb_fd_init, 1096 1097 .dev_exit = pcan_usb_fd_exit, 1098 .dev_free = pcan_usb_fd_free, 1099 .dev_set_bus = pcan_usb_fd_set_bus, 1100 .dev_set_bittiming = pcan_usb_fd_set_bittiming_slow, 1101 .dev_set_data_bittiming = pcan_usb_fd_set_bittiming_fast, 1102 .dev_decode_buf = pcan_usb_fd_decode_buf, 1103 .dev_start = pcan_usb_fd_start, 1104 .dev_stop = pcan_usb_fd_stop, 1105 .dev_restart_async = pcan_usb_fd_restart_async, 1106 .dev_encode_msg = pcan_usb_fd_encode_msg, 1107 1108 .do_get_berr_counter = pcan_usb_fd_get_berr_counter, 1109 }; 1110 1111 /* describes the PCAN-CHIP USB */ 1112 static const struct can_bittiming_const pcan_usb_chip_const = { 1113 .name = "pcan_chip_usb", 1114 .tseg1_min = 1, 1115 .tseg1_max = (1 << PUCAN_TSLOW_TSGEG1_BITS), 1116 .tseg2_min = 1, 1117 .tseg2_max = (1 << PUCAN_TSLOW_TSGEG2_BITS), 1118 .sjw_max = (1 << PUCAN_TSLOW_SJW_BITS), 1119 .brp_min = 1, 1120 .brp_max = (1 << PUCAN_TSLOW_BRP_BITS), 1121 .brp_inc = 1, 1122 }; 1123 1124 static const struct can_bittiming_const pcan_usb_chip_data_const = { 1125 .name = "pcan_chip_usb", 1126 .tseg1_min = 1, 1127 .tseg1_max = (1 << PUCAN_TFAST_TSGEG1_BITS), 1128 .tseg2_min = 1, 1129 .tseg2_max = (1 << PUCAN_TFAST_TSGEG2_BITS), 1130 .sjw_max = (1 << PUCAN_TFAST_SJW_BITS), 1131 .brp_min = 1, 1132 .brp_max = (1 << PUCAN_TFAST_BRP_BITS), 1133 .brp_inc = 1, 1134 }; 1135 1136 const struct peak_usb_adapter pcan_usb_chip = { 1137 .name = "PCAN-Chip USB", 1138 .device_id = PCAN_USBCHIP_PRODUCT_ID, 1139 .ctrl_count = PCAN_USBFD_CHANNEL_COUNT, 1140 .ctrlmode_supported = CAN_CTRLMODE_FD | 1141 CAN_CTRLMODE_3_SAMPLES | CAN_CTRLMODE_LISTENONLY | 1142 CAN_CTRLMODE_ONE_SHOT | CAN_CTRLMODE_CC_LEN8_DLC, 1143 .clock = { 1144 .freq = PCAN_UFD_CRYSTAL_HZ, 1145 }, 1146 .bittiming_const = &pcan_usb_chip_const, 1147 .data_bittiming_const = &pcan_usb_chip_data_const, 1148 1149 /* size of device private data */ 1150 .sizeof_dev_private = sizeof(struct pcan_usb_fd_device), 1151 1152 .ethtool_ops = &pcan_usb_fd_ethtool_ops, 1153 1154 /* timestamps usage */ 1155 .ts_used_bits = 32, 1156 .us_per_ts_scale = 1, /* us = (ts * scale) >> shift */ 1157 .us_per_ts_shift = 0, 1158 1159 /* give here messages in/out endpoints */ 1160 .ep_msg_in = PCAN_USBPRO_EP_MSGIN, 1161 .ep_msg_out = {PCAN_USBPRO_EP_MSGOUT_0}, 1162 1163 /* size of rx/tx usb buffers */ 1164 .rx_buffer_size = PCAN_UFD_RX_BUFFER_SIZE, 1165 .tx_buffer_size = PCAN_UFD_TX_BUFFER_SIZE, 1166 1167 /* device callbacks */ 1168 .intf_probe = pcan_usb_pro_probe, /* same as PCAN-USB Pro */ 1169 .dev_init = pcan_usb_fd_init, 1170 1171 .dev_exit = pcan_usb_fd_exit, 1172 .dev_free = pcan_usb_fd_free, 1173 .dev_set_bus = pcan_usb_fd_set_bus, 1174 .dev_set_bittiming = pcan_usb_fd_set_bittiming_slow, 1175 .dev_set_data_bittiming = pcan_usb_fd_set_bittiming_fast, 1176 .dev_decode_buf = pcan_usb_fd_decode_buf, 1177 .dev_start = pcan_usb_fd_start, 1178 .dev_stop = pcan_usb_fd_stop, 1179 .dev_restart_async = pcan_usb_fd_restart_async, 1180 .dev_encode_msg = pcan_usb_fd_encode_msg, 1181 1182 .do_get_berr_counter = pcan_usb_fd_get_berr_counter, 1183 }; 1184 1185 /* describes the PCAN-USB Pro FD adapter */ 1186 static const struct can_bittiming_const pcan_usb_pro_fd_const = { 1187 .name = "pcan_usb_pro_fd", 1188 .tseg1_min = 1, 1189 .tseg1_max = (1 << PUCAN_TSLOW_TSGEG1_BITS), 1190 .tseg2_min = 1, 1191 .tseg2_max = (1 << PUCAN_TSLOW_TSGEG2_BITS), 1192 .sjw_max = (1 << PUCAN_TSLOW_SJW_BITS), 1193 .brp_min = 1, 1194 .brp_max = (1 << PUCAN_TSLOW_BRP_BITS), 1195 .brp_inc = 1, 1196 }; 1197 1198 static const struct can_bittiming_const pcan_usb_pro_fd_data_const = { 1199 .name = "pcan_usb_pro_fd", 1200 .tseg1_min = 1, 1201 .tseg1_max = (1 << PUCAN_TFAST_TSGEG1_BITS), 1202 .tseg2_min = 1, 1203 .tseg2_max = (1 << PUCAN_TFAST_TSGEG2_BITS), 1204 .sjw_max = (1 << PUCAN_TFAST_SJW_BITS), 1205 .brp_min = 1, 1206 .brp_max = (1 << PUCAN_TFAST_BRP_BITS), 1207 .brp_inc = 1, 1208 }; 1209 1210 const struct peak_usb_adapter pcan_usb_pro_fd = { 1211 .name = "PCAN-USB Pro FD", 1212 .device_id = PCAN_USBPROFD_PRODUCT_ID, 1213 .ctrl_count = PCAN_USBPROFD_CHANNEL_COUNT, 1214 .ctrlmode_supported = CAN_CTRLMODE_FD | 1215 CAN_CTRLMODE_3_SAMPLES | CAN_CTRLMODE_LISTENONLY | 1216 CAN_CTRLMODE_ONE_SHOT | CAN_CTRLMODE_CC_LEN8_DLC, 1217 .clock = { 1218 .freq = PCAN_UFD_CRYSTAL_HZ, 1219 }, 1220 .bittiming_const = &pcan_usb_pro_fd_const, 1221 .data_bittiming_const = &pcan_usb_pro_fd_data_const, 1222 1223 /* size of device private data */ 1224 .sizeof_dev_private = sizeof(struct pcan_usb_fd_device), 1225 1226 .ethtool_ops = &pcan_usb_fd_ethtool_ops, 1227 1228 /* timestamps usage */ 1229 .ts_used_bits = 32, 1230 .us_per_ts_scale = 1, /* us = (ts * scale) >> shift */ 1231 .us_per_ts_shift = 0, 1232 1233 /* give here messages in/out endpoints */ 1234 .ep_msg_in = PCAN_USBPRO_EP_MSGIN, 1235 .ep_msg_out = {PCAN_USBPRO_EP_MSGOUT_0, PCAN_USBPRO_EP_MSGOUT_1}, 1236 1237 /* size of rx/tx usb buffers */ 1238 .rx_buffer_size = PCAN_UFD_RX_BUFFER_SIZE, 1239 .tx_buffer_size = PCAN_UFD_TX_BUFFER_SIZE, 1240 1241 /* device callbacks */ 1242 .intf_probe = pcan_usb_pro_probe, /* same as PCAN-USB Pro */ 1243 .dev_init = pcan_usb_fd_init, 1244 1245 .dev_exit = pcan_usb_fd_exit, 1246 .dev_free = pcan_usb_fd_free, 1247 .dev_set_bus = pcan_usb_fd_set_bus, 1248 .dev_set_bittiming = pcan_usb_fd_set_bittiming_slow, 1249 .dev_set_data_bittiming = pcan_usb_fd_set_bittiming_fast, 1250 .dev_decode_buf = pcan_usb_fd_decode_buf, 1251 .dev_start = pcan_usb_fd_start, 1252 .dev_stop = pcan_usb_fd_stop, 1253 .dev_restart_async = pcan_usb_fd_restart_async, 1254 .dev_encode_msg = pcan_usb_fd_encode_msg, 1255 1256 .do_get_berr_counter = pcan_usb_fd_get_berr_counter, 1257 }; 1258 1259 /* describes the PCAN-USB X6 adapter */ 1260 static const struct can_bittiming_const pcan_usb_x6_const = { 1261 .name = "pcan_usb_x6", 1262 .tseg1_min = 1, 1263 .tseg1_max = (1 << PUCAN_TSLOW_TSGEG1_BITS), 1264 .tseg2_min = 1, 1265 .tseg2_max = (1 << PUCAN_TSLOW_TSGEG2_BITS), 1266 .sjw_max = (1 << PUCAN_TSLOW_SJW_BITS), 1267 .brp_min = 1, 1268 .brp_max = (1 << PUCAN_TSLOW_BRP_BITS), 1269 .brp_inc = 1, 1270 }; 1271 1272 static const struct can_bittiming_const pcan_usb_x6_data_const = { 1273 .name = "pcan_usb_x6", 1274 .tseg1_min = 1, 1275 .tseg1_max = (1 << PUCAN_TFAST_TSGEG1_BITS), 1276 .tseg2_min = 1, 1277 .tseg2_max = (1 << PUCAN_TFAST_TSGEG2_BITS), 1278 .sjw_max = (1 << PUCAN_TFAST_SJW_BITS), 1279 .brp_min = 1, 1280 .brp_max = (1 << PUCAN_TFAST_BRP_BITS), 1281 .brp_inc = 1, 1282 }; 1283 1284 const struct peak_usb_adapter pcan_usb_x6 = { 1285 .name = "PCAN-USB X6", 1286 .device_id = PCAN_USBX6_PRODUCT_ID, 1287 .ctrl_count = PCAN_USBPROFD_CHANNEL_COUNT, 1288 .ctrlmode_supported = CAN_CTRLMODE_FD | 1289 CAN_CTRLMODE_3_SAMPLES | CAN_CTRLMODE_LISTENONLY | 1290 CAN_CTRLMODE_ONE_SHOT | CAN_CTRLMODE_CC_LEN8_DLC, 1291 .clock = { 1292 .freq = PCAN_UFD_CRYSTAL_HZ, 1293 }, 1294 .bittiming_const = &pcan_usb_x6_const, 1295 .data_bittiming_const = &pcan_usb_x6_data_const, 1296 1297 /* size of device private data */ 1298 .sizeof_dev_private = sizeof(struct pcan_usb_fd_device), 1299 1300 .ethtool_ops = &pcan_usb_fd_ethtool_ops, 1301 1302 /* timestamps usage */ 1303 .ts_used_bits = 32, 1304 .us_per_ts_scale = 1, /* us = (ts * scale) >> shift */ 1305 .us_per_ts_shift = 0, 1306 1307 /* give here messages in/out endpoints */ 1308 .ep_msg_in = PCAN_USBPRO_EP_MSGIN, 1309 .ep_msg_out = {PCAN_USBPRO_EP_MSGOUT_0, PCAN_USBPRO_EP_MSGOUT_1}, 1310 1311 /* size of rx/tx usb buffers */ 1312 .rx_buffer_size = PCAN_UFD_RX_BUFFER_SIZE, 1313 .tx_buffer_size = PCAN_UFD_TX_BUFFER_SIZE, 1314 1315 /* device callbacks */ 1316 .intf_probe = pcan_usb_pro_probe, /* same as PCAN-USB Pro */ 1317 .dev_init = pcan_usb_fd_init, 1318 1319 .dev_exit = pcan_usb_fd_exit, 1320 .dev_free = pcan_usb_fd_free, 1321 .dev_set_bus = pcan_usb_fd_set_bus, 1322 .dev_set_bittiming = pcan_usb_fd_set_bittiming_slow, 1323 .dev_set_data_bittiming = pcan_usb_fd_set_bittiming_fast, 1324 .dev_decode_buf = pcan_usb_fd_decode_buf, 1325 .dev_start = pcan_usb_fd_start, 1326 .dev_stop = pcan_usb_fd_stop, 1327 .dev_restart_async = pcan_usb_fd_restart_async, 1328 .dev_encode_msg = pcan_usb_fd_encode_msg, 1329 1330 .do_get_berr_counter = pcan_usb_fd_get_berr_counter, 1331 }; 1332