Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

can: kvaser_usb: Split driver into kvaser_usb_core.c and kvaser_usb_leaf.c

First part of adding support for Kvaser USB device family "hydra".

Split kvaser_usb.c into kvaser_usb/kvaser_usb{.h,_core.c,_leaf.c}.

kvaser_usb_core.c contains common functionality, such as USB
writing/reading and allocation of netdev.
kvaser_usb_leaf.c contains device specific code, used in
kvaser_usb_core.c.

struct kvaser_usb_dev_ops contains device specific functions that are
common for all devices in the family. While, struct kvaser_usb_dev_cfg
describes the device configurations in terms of CAN clock frequency,
timestamp frequency and CAN controller bittiming constants.

Signed-off-by: Jimmy Assarsson <extja@kvaser.com>
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>

authored by

Jimmy Assarsson and committed by
Marc Kleine-Budde
7259124e e0543f24

+2293 -2031
+1 -1
drivers/net/can/usb/Makefile
··· 7 7 obj-$(CONFIG_CAN_EMS_USB) += ems_usb.o 8 8 obj-$(CONFIG_CAN_ESD_USB2) += esd_usb2.o 9 9 obj-$(CONFIG_CAN_GS_USB) += gs_usb.o 10 - obj-$(CONFIG_CAN_KVASER_USB) += kvaser_usb.o 10 + obj-$(CONFIG_CAN_KVASER_USB) += kvaser_usb/ 11 11 obj-$(CONFIG_CAN_MCBA_USB) += mcba_usb.o 12 12 obj-$(CONFIG_CAN_PEAK_USB) += peak_usb/ 13 13 obj-$(CONFIG_CAN_UCAN) += ucan.o
-2030
drivers/net/can/usb/kvaser_usb.c
··· 1 - // SPDX-License-Identifier: GPL-2.0 2 - /* Parts of this driver are based on the following: 3 - * - Kvaser linux leaf driver (version 4.78) 4 - * - CAN driver for esd CAN-USB/2 5 - * - Kvaser linux usbcanII driver (version 5.3) 6 - * 7 - * Copyright (C) 2002-2006 KVASER AB, Sweden. All rights reserved. 8 - * Copyright (C) 2010 Matthias Fuchs <matthias.fuchs@esd.eu>, esd gmbh 9 - * Copyright (C) 2012 Olivier Sobrie <olivier@sobrie.be> 10 - * Copyright (C) 2015 Valeo S.A. 11 - */ 12 - 13 - #include <linux/spinlock.h> 14 - #include <linux/kernel.h> 15 - #include <linux/completion.h> 16 - #include <linux/module.h> 17 - #include <linux/netdevice.h> 18 - #include <linux/usb.h> 19 - 20 - #include <linux/can.h> 21 - #include <linux/can/dev.h> 22 - #include <linux/can/error.h> 23 - 24 - #define MAX_RX_URBS 4 25 - #define KVASER_USB_TIMEOUT 1000 /* msecs */ 26 - #define RX_BUFFER_SIZE 3072 27 - #define CAN_USB_CLOCK 8000000 28 - #define MAX_NET_DEVICES 3 29 - #define MAX_USBCAN_NET_DEVICES 2 30 - 31 - /* Kvaser Leaf USB devices */ 32 - #define KVASER_VENDOR_ID 0x0bfd 33 - #define USB_LEAF_DEVEL_PRODUCT_ID 10 34 - #define USB_LEAF_LITE_PRODUCT_ID 11 35 - #define USB_LEAF_PRO_PRODUCT_ID 12 36 - #define USB_LEAF_SPRO_PRODUCT_ID 14 37 - #define USB_LEAF_PRO_LS_PRODUCT_ID 15 38 - #define USB_LEAF_PRO_SWC_PRODUCT_ID 16 39 - #define USB_LEAF_PRO_LIN_PRODUCT_ID 17 40 - #define USB_LEAF_SPRO_LS_PRODUCT_ID 18 41 - #define USB_LEAF_SPRO_SWC_PRODUCT_ID 19 42 - #define USB_MEMO2_DEVEL_PRODUCT_ID 22 43 - #define USB_MEMO2_HSHS_PRODUCT_ID 23 44 - #define USB_UPRO_HSHS_PRODUCT_ID 24 45 - #define USB_LEAF_LITE_GI_PRODUCT_ID 25 46 - #define USB_LEAF_PRO_OBDII_PRODUCT_ID 26 47 - #define USB_MEMO2_HSLS_PRODUCT_ID 27 48 - #define USB_LEAF_LITE_CH_PRODUCT_ID 28 49 - #define USB_BLACKBIRD_SPRO_PRODUCT_ID 29 50 - #define USB_OEM_MERCURY_PRODUCT_ID 34 51 - #define USB_OEM_LEAF_PRODUCT_ID 35 52 - #define USB_CAN_R_PRODUCT_ID 39 53 - #define USB_LEAF_LITE_V2_PRODUCT_ID 288 54 - #define USB_MINI_PCIE_HS_PRODUCT_ID 289 55 - #define USB_LEAF_LIGHT_HS_V2_OEM_PRODUCT_ID 290 56 - #define USB_USBCAN_LIGHT_2HS_PRODUCT_ID 291 57 - #define USB_MINI_PCIE_2HS_PRODUCT_ID 292 58 - 59 - static inline bool kvaser_is_leaf(const struct usb_device_id *id) 60 - { 61 - return id->idProduct >= USB_LEAF_DEVEL_PRODUCT_ID && 62 - id->idProduct <= USB_MINI_PCIE_2HS_PRODUCT_ID; 63 - } 64 - 65 - /* Kvaser USBCan-II devices */ 66 - #define USB_USBCAN_REVB_PRODUCT_ID 2 67 - #define USB_VCI2_PRODUCT_ID 3 68 - #define USB_USBCAN2_PRODUCT_ID 4 69 - #define USB_MEMORATOR_PRODUCT_ID 5 70 - 71 - static inline bool kvaser_is_usbcan(const struct usb_device_id *id) 72 - { 73 - return id->idProduct >= USB_USBCAN_REVB_PRODUCT_ID && 74 - id->idProduct <= USB_MEMORATOR_PRODUCT_ID; 75 - } 76 - 77 - /* USB devices features */ 78 - #define KVASER_HAS_SILENT_MODE BIT(0) 79 - #define KVASER_HAS_TXRX_ERRORS BIT(1) 80 - 81 - /* Command header size */ 82 - #define CMD_HEADER_LEN 2 83 - 84 - /* CAN message flags */ 85 - #define MSG_FLAG_ERROR_FRAME BIT(0) 86 - #define MSG_FLAG_OVERRUN BIT(1) 87 - #define MSG_FLAG_NERR BIT(2) 88 - #define MSG_FLAG_WAKEUP BIT(3) 89 - #define MSG_FLAG_REMOTE_FRAME BIT(4) 90 - #define MSG_FLAG_RESERVED BIT(5) 91 - #define MSG_FLAG_TX_ACK BIT(6) 92 - #define MSG_FLAG_TX_REQUEST BIT(7) 93 - 94 - /* CAN states (M16C CxSTRH register) */ 95 - #define M16C_STATE_BUS_RESET BIT(0) 96 - #define M16C_STATE_BUS_ERROR BIT(4) 97 - #define M16C_STATE_BUS_PASSIVE BIT(5) 98 - #define M16C_STATE_BUS_OFF BIT(6) 99 - 100 - /* Leaf/usbcan command ids */ 101 - #define CMD_RX_STD_MESSAGE 12 102 - #define CMD_TX_STD_MESSAGE 13 103 - #define CMD_RX_EXT_MESSAGE 14 104 - #define CMD_TX_EXT_MESSAGE 15 105 - #define CMD_SET_BUS_PARAMS 16 106 - #define CMD_CHIP_STATE_EVENT 20 107 - #define CMD_SET_CTRL_MODE 21 108 - #define CMD_RESET_CHIP 24 109 - #define CMD_START_CHIP 26 110 - #define CMD_START_CHIP_REPLY 27 111 - #define CMD_STOP_CHIP 28 112 - #define CMD_STOP_CHIP_REPLY 29 113 - 114 - #define CMD_USBCAN_CLOCK_OVERFLOW_EVENT 33 115 - 116 - #define CMD_GET_CARD_INFO 34 117 - #define CMD_GET_CARD_INFO_REPLY 35 118 - #define CMD_GET_SOFTWARE_INFO 38 119 - #define CMD_GET_SOFTWARE_INFO_REPLY 39 120 - #define CMD_FLUSH_QUEUE 48 121 - #define CMD_TX_ACKNOWLEDGE 50 122 - #define CMD_CAN_ERROR_EVENT 51 123 - #define CMD_FLUSH_QUEUE_REPLY 68 124 - 125 - #define CMD_LEAF_LOG_MESSAGE 106 126 - 127 - /* error factors */ 128 - #define M16C_EF_ACKE BIT(0) 129 - #define M16C_EF_CRCE BIT(1) 130 - #define M16C_EF_FORME BIT(2) 131 - #define M16C_EF_STFE BIT(3) 132 - #define M16C_EF_BITE0 BIT(4) 133 - #define M16C_EF_BITE1 BIT(5) 134 - #define M16C_EF_RCVE BIT(6) 135 - #define M16C_EF_TRE BIT(7) 136 - 137 - /* Only Leaf-based devices can report M16C error factors, 138 - * thus define our own error status flags for USBCANII 139 - */ 140 - #define USBCAN_ERROR_STATE_NONE 0 141 - #define USBCAN_ERROR_STATE_TX_ERROR BIT(0) 142 - #define USBCAN_ERROR_STATE_RX_ERROR BIT(1) 143 - #define USBCAN_ERROR_STATE_BUSERROR BIT(2) 144 - 145 - /* bittiming parameters */ 146 - #define KVASER_USB_TSEG1_MIN 1 147 - #define KVASER_USB_TSEG1_MAX 16 148 - #define KVASER_USB_TSEG2_MIN 1 149 - #define KVASER_USB_TSEG2_MAX 8 150 - #define KVASER_USB_SJW_MAX 4 151 - #define KVASER_USB_BRP_MIN 1 152 - #define KVASER_USB_BRP_MAX 64 153 - #define KVASER_USB_BRP_INC 1 154 - 155 - /* ctrl modes */ 156 - #define KVASER_CTRL_MODE_NORMAL 1 157 - #define KVASER_CTRL_MODE_SILENT 2 158 - #define KVASER_CTRL_MODE_SELFRECEPTION 3 159 - #define KVASER_CTRL_MODE_OFF 4 160 - 161 - /* Extended CAN identifier flag */ 162 - #define KVASER_EXTENDED_FRAME BIT(31) 163 - 164 - /* Kvaser USB CAN dongles are divided into two major families: 165 - * - Leaf: Based on Renesas M32C, running firmware labeled as 'filo' 166 - * - UsbcanII: Based on Renesas M16C, running firmware labeled as 'helios' 167 - */ 168 - enum kvaser_usb_family { 169 - KVASER_LEAF, 170 - KVASER_USBCAN, 171 - }; 172 - 173 - struct kvaser_cmd_simple { 174 - u8 tid; 175 - u8 channel; 176 - } __packed; 177 - 178 - struct kvaser_cmd_cardinfo { 179 - u8 tid; 180 - u8 nchannels; 181 - union { 182 - struct { 183 - __le32 serial_number; 184 - __le32 padding; 185 - } __packed leaf0; 186 - struct { 187 - __le32 serial_number_low; 188 - __le32 serial_number_high; 189 - } __packed usbcan0; 190 - } __packed; 191 - __le32 clock_resolution; 192 - __le32 mfgdate; 193 - u8 ean[8]; 194 - u8 hw_revision; 195 - union { 196 - struct { 197 - u8 usb_hs_mode; 198 - } __packed leaf1; 199 - struct { 200 - u8 padding; 201 - } __packed usbcan1; 202 - } __packed; 203 - __le16 padding; 204 - } __packed; 205 - 206 - struct leaf_cmd_softinfo { 207 - u8 tid; 208 - u8 padding0; 209 - __le32 sw_options; 210 - __le32 fw_version; 211 - __le16 max_outstanding_tx; 212 - __le16 padding1[9]; 213 - } __packed; 214 - 215 - struct usbcan_cmd_softinfo { 216 - u8 tid; 217 - u8 fw_name[5]; 218 - __le16 max_outstanding_tx; 219 - u8 padding[6]; 220 - __le32 fw_version; 221 - __le16 checksum; 222 - __le16 sw_options; 223 - } __packed; 224 - 225 - struct kvaser_cmd_busparams { 226 - u8 tid; 227 - u8 channel; 228 - __le32 bitrate; 229 - u8 tseg1; 230 - u8 tseg2; 231 - u8 sjw; 232 - u8 no_samp; 233 - } __packed; 234 - 235 - struct kvaser_cmd_tx_can { 236 - u8 channel; 237 - u8 tid; 238 - u8 data[14]; 239 - union { 240 - struct { 241 - u8 padding; 242 - u8 flags; 243 - } __packed leaf; 244 - struct { 245 - u8 flags; 246 - u8 padding; 247 - } __packed usbcan; 248 - } __packed; 249 - } __packed; 250 - 251 - struct kvaser_cmd_rx_can_header { 252 - u8 channel; 253 - u8 flag; 254 - } __packed; 255 - 256 - struct leaf_cmd_rx_can { 257 - u8 channel; 258 - u8 flag; 259 - 260 - __le16 time[3]; 261 - u8 data[14]; 262 - } __packed; 263 - 264 - struct usbcan_cmd_rx_can { 265 - u8 channel; 266 - u8 flag; 267 - 268 - u8 data[14]; 269 - __le16 time; 270 - } __packed; 271 - 272 - struct leaf_cmd_chip_state_event { 273 - u8 tid; 274 - u8 channel; 275 - 276 - __le16 time[3]; 277 - u8 tx_errors_count; 278 - u8 rx_errors_count; 279 - 280 - u8 status; 281 - u8 padding[3]; 282 - } __packed; 283 - 284 - struct usbcan_cmd_chip_state_event { 285 - u8 tid; 286 - u8 channel; 287 - 288 - u8 tx_errors_count; 289 - u8 rx_errors_count; 290 - __le16 time; 291 - 292 - u8 status; 293 - u8 padding[3]; 294 - } __packed; 295 - 296 - struct kvaser_cmd_tx_acknowledge_header { 297 - u8 channel; 298 - u8 tid; 299 - } __packed; 300 - 301 - struct leaf_cmd_error_event { 302 - u8 tid; 303 - u8 flags; 304 - __le16 time[3]; 305 - u8 channel; 306 - u8 padding; 307 - u8 tx_errors_count; 308 - u8 rx_errors_count; 309 - u8 status; 310 - u8 error_factor; 311 - } __packed; 312 - 313 - struct usbcan_cmd_error_event { 314 - u8 tid; 315 - u8 padding; 316 - u8 tx_errors_count_ch0; 317 - u8 rx_errors_count_ch0; 318 - u8 tx_errors_count_ch1; 319 - u8 rx_errors_count_ch1; 320 - u8 status_ch0; 321 - u8 status_ch1; 322 - __le16 time; 323 - } __packed; 324 - 325 - struct kvaser_cmd_ctrl_mode { 326 - u8 tid; 327 - u8 channel; 328 - u8 ctrl_mode; 329 - u8 padding[3]; 330 - } __packed; 331 - 332 - struct kvaser_cmd_flush_queue { 333 - u8 tid; 334 - u8 channel; 335 - u8 flags; 336 - u8 padding[3]; 337 - } __packed; 338 - 339 - struct leaf_cmd_log_message { 340 - u8 channel; 341 - u8 flags; 342 - __le16 time[3]; 343 - u8 dlc; 344 - u8 time_offset; 345 - __le32 id; 346 - u8 data[8]; 347 - } __packed; 348 - 349 - struct kvaser_cmd { 350 - u8 len; 351 - u8 id; 352 - union { 353 - struct kvaser_cmd_simple simple; 354 - struct kvaser_cmd_cardinfo cardinfo; 355 - struct kvaser_cmd_busparams busparams; 356 - 357 - struct kvaser_cmd_rx_can_header rx_can_header; 358 - struct kvaser_cmd_tx_acknowledge_header tx_acknowledge_header; 359 - 360 - union { 361 - struct leaf_cmd_softinfo softinfo; 362 - struct leaf_cmd_rx_can rx_can; 363 - struct leaf_cmd_chip_state_event chip_state_event; 364 - struct leaf_cmd_error_event error_event; 365 - struct leaf_cmd_log_message log_message; 366 - } __packed leaf; 367 - 368 - union { 369 - struct usbcan_cmd_softinfo softinfo; 370 - struct usbcan_cmd_rx_can rx_can; 371 - struct usbcan_cmd_chip_state_event chip_state_event; 372 - struct usbcan_cmd_error_event error_event; 373 - } __packed usbcan; 374 - 375 - struct kvaser_cmd_tx_can tx_can; 376 - struct kvaser_cmd_ctrl_mode ctrl_mode; 377 - struct kvaser_cmd_flush_queue flush_queue; 378 - } u; 379 - } __packed; 380 - 381 - /* Summary of a kvaser error event, for a unified Leaf/Usbcan error 382 - * handling. Some discrepancies between the two families exist: 383 - * 384 - * - USBCAN firmware does not report M16C "error factors" 385 - * - USBCAN controllers has difficulties reporting if the raised error 386 - * event is for ch0 or ch1. They leave such arbitration to the OS 387 - * driver by letting it compare error counters with previous values 388 - * and decide the error event's channel. Thus for USBCAN, the channel 389 - * field is only advisory. 390 - */ 391 - struct kvaser_usb_error_summary { 392 - u8 channel, status, txerr, rxerr; 393 - union { 394 - struct { 395 - u8 error_factor; 396 - } leaf; 397 - struct { 398 - u8 other_ch_status; 399 - u8 error_state; 400 - } usbcan; 401 - }; 402 - }; 403 - 404 - /* Context for an outstanding, not yet ACKed, transmission */ 405 - struct kvaser_usb_tx_urb_context { 406 - struct kvaser_usb_net_priv *priv; 407 - u32 echo_index; 408 - int dlc; 409 - }; 410 - 411 - struct kvaser_usb { 412 - struct usb_device *udev; 413 - struct usb_interface *intf; 414 - struct kvaser_usb_net_priv *nets[MAX_NET_DEVICES]; 415 - 416 - struct usb_endpoint_descriptor *bulk_in, *bulk_out; 417 - struct usb_anchor rx_submitted; 418 - 419 - /* @max_tx_urbs: Firmware-reported maximum number of outstanding, 420 - * not yet ACKed, transmissions on this device. This value is 421 - * also used as a sentinel for marking free tx contexts. 422 - */ 423 - u32 fw_version; 424 - unsigned int nchannels; 425 - unsigned int max_tx_urbs; 426 - enum kvaser_usb_family family; 427 - 428 - bool rxinitdone; 429 - void *rxbuf[MAX_RX_URBS]; 430 - dma_addr_t rxbuf_dma[MAX_RX_URBS]; 431 - }; 432 - 433 - struct kvaser_usb_net_priv { 434 - struct can_priv can; 435 - struct can_berr_counter bec; 436 - 437 - struct kvaser_usb *dev; 438 - struct net_device *netdev; 439 - int channel; 440 - 441 - struct completion start_comp, stop_comp; 442 - struct usb_anchor tx_submitted; 443 - 444 - spinlock_t tx_contexts_lock; 445 - int active_tx_contexts; 446 - struct kvaser_usb_tx_urb_context tx_contexts[]; 447 - }; 448 - 449 - static const struct usb_device_id kvaser_usb_table[] = { 450 - /* Leaf family IDs */ 451 - { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_DEVEL_PRODUCT_ID) }, 452 - { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_PRODUCT_ID) }, 453 - { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_PRODUCT_ID), 454 - .driver_info = KVASER_HAS_TXRX_ERRORS | 455 - KVASER_HAS_SILENT_MODE }, 456 - { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_SPRO_PRODUCT_ID), 457 - .driver_info = KVASER_HAS_TXRX_ERRORS | 458 - KVASER_HAS_SILENT_MODE }, 459 - { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_LS_PRODUCT_ID), 460 - .driver_info = KVASER_HAS_TXRX_ERRORS | 461 - KVASER_HAS_SILENT_MODE }, 462 - { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_SWC_PRODUCT_ID), 463 - .driver_info = KVASER_HAS_TXRX_ERRORS | 464 - KVASER_HAS_SILENT_MODE }, 465 - { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_LIN_PRODUCT_ID), 466 - .driver_info = KVASER_HAS_TXRX_ERRORS | 467 - KVASER_HAS_SILENT_MODE }, 468 - { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_SPRO_LS_PRODUCT_ID), 469 - .driver_info = KVASER_HAS_TXRX_ERRORS | 470 - KVASER_HAS_SILENT_MODE }, 471 - { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_SPRO_SWC_PRODUCT_ID), 472 - .driver_info = KVASER_HAS_TXRX_ERRORS | 473 - KVASER_HAS_SILENT_MODE }, 474 - { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO2_DEVEL_PRODUCT_ID), 475 - .driver_info = KVASER_HAS_TXRX_ERRORS | 476 - KVASER_HAS_SILENT_MODE }, 477 - { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO2_HSHS_PRODUCT_ID), 478 - .driver_info = KVASER_HAS_TXRX_ERRORS | 479 - KVASER_HAS_SILENT_MODE }, 480 - { USB_DEVICE(KVASER_VENDOR_ID, USB_UPRO_HSHS_PRODUCT_ID), 481 - .driver_info = KVASER_HAS_TXRX_ERRORS }, 482 - { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_GI_PRODUCT_ID) }, 483 - { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_OBDII_PRODUCT_ID), 484 - .driver_info = KVASER_HAS_TXRX_ERRORS | 485 - KVASER_HAS_SILENT_MODE }, 486 - { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO2_HSLS_PRODUCT_ID), 487 - .driver_info = KVASER_HAS_TXRX_ERRORS }, 488 - { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_CH_PRODUCT_ID), 489 - .driver_info = KVASER_HAS_TXRX_ERRORS }, 490 - { USB_DEVICE(KVASER_VENDOR_ID, USB_BLACKBIRD_SPRO_PRODUCT_ID), 491 - .driver_info = KVASER_HAS_TXRX_ERRORS }, 492 - { USB_DEVICE(KVASER_VENDOR_ID, USB_OEM_MERCURY_PRODUCT_ID), 493 - .driver_info = KVASER_HAS_TXRX_ERRORS }, 494 - { USB_DEVICE(KVASER_VENDOR_ID, USB_OEM_LEAF_PRODUCT_ID), 495 - .driver_info = KVASER_HAS_TXRX_ERRORS }, 496 - { USB_DEVICE(KVASER_VENDOR_ID, USB_CAN_R_PRODUCT_ID), 497 - .driver_info = KVASER_HAS_TXRX_ERRORS }, 498 - { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_V2_PRODUCT_ID) }, 499 - { USB_DEVICE(KVASER_VENDOR_ID, USB_MINI_PCIE_HS_PRODUCT_ID) }, 500 - { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LIGHT_HS_V2_OEM_PRODUCT_ID) }, 501 - { USB_DEVICE(KVASER_VENDOR_ID, USB_USBCAN_LIGHT_2HS_PRODUCT_ID) }, 502 - { USB_DEVICE(KVASER_VENDOR_ID, USB_MINI_PCIE_2HS_PRODUCT_ID) }, 503 - 504 - /* USBCANII family IDs */ 505 - { USB_DEVICE(KVASER_VENDOR_ID, USB_USBCAN2_PRODUCT_ID), 506 - .driver_info = KVASER_HAS_TXRX_ERRORS }, 507 - { USB_DEVICE(KVASER_VENDOR_ID, USB_USBCAN_REVB_PRODUCT_ID), 508 - .driver_info = KVASER_HAS_TXRX_ERRORS }, 509 - { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMORATOR_PRODUCT_ID), 510 - .driver_info = KVASER_HAS_TXRX_ERRORS }, 511 - { USB_DEVICE(KVASER_VENDOR_ID, USB_VCI2_PRODUCT_ID), 512 - .driver_info = KVASER_HAS_TXRX_ERRORS }, 513 - 514 - { } 515 - }; 516 - MODULE_DEVICE_TABLE(usb, kvaser_usb_table); 517 - 518 - static inline int kvaser_usb_send_cmd(const struct kvaser_usb *dev, 519 - struct kvaser_cmd *cmd) 520 - { 521 - int actual_len; 522 - 523 - return usb_bulk_msg(dev->udev, 524 - usb_sndbulkpipe(dev->udev, 525 - dev->bulk_out->bEndpointAddress), 526 - cmd, cmd->len, &actual_len, KVASER_USB_TIMEOUT); 527 - } 528 - 529 - static int kvaser_usb_wait_cmd(const struct kvaser_usb *dev, u8 id, 530 - struct kvaser_cmd *cmd) 531 - { 532 - struct kvaser_cmd *tmp; 533 - void *buf; 534 - int actual_len; 535 - int err; 536 - int pos; 537 - unsigned long to = jiffies + msecs_to_jiffies(KVASER_USB_TIMEOUT); 538 - 539 - buf = kzalloc(RX_BUFFER_SIZE, GFP_KERNEL); 540 - if (!buf) 541 - return -ENOMEM; 542 - 543 - do { 544 - err = usb_bulk_msg(dev->udev, 545 - usb_rcvbulkpipe(dev->udev, 546 - dev->bulk_in->bEndpointAddress), 547 - buf, RX_BUFFER_SIZE, &actual_len, 548 - KVASER_USB_TIMEOUT); 549 - if (err < 0) 550 - goto end; 551 - 552 - pos = 0; 553 - while (pos <= actual_len - CMD_HEADER_LEN) { 554 - tmp = buf + pos; 555 - 556 - /* Handle commands crossing the USB endpoint max packet 557 - * size boundary. Check kvaser_usb_read_bulk_callback() 558 - * for further details. 559 - */ 560 - if (tmp->len == 0) { 561 - pos = round_up(pos, le16_to_cpu(dev->bulk_in-> 562 - wMaxPacketSize)); 563 - continue; 564 - } 565 - 566 - if (pos + tmp->len > actual_len) { 567 - dev_err_ratelimited(&dev->intf->dev, 568 - "Format error\n"); 569 - break; 570 - } 571 - 572 - if (tmp->id == id) { 573 - memcpy(cmd, tmp, tmp->len); 574 - goto end; 575 - } 576 - 577 - pos += tmp->len; 578 - } 579 - } while (time_before(jiffies, to)); 580 - 581 - err = -EINVAL; 582 - 583 - end: 584 - kfree(buf); 585 - 586 - return err; 587 - } 588 - 589 - static int kvaser_usb_send_simple_cmd(const struct kvaser_usb *dev, 590 - u8 cmd_id, int channel) 591 - { 592 - struct kvaser_cmd *cmd; 593 - int rc; 594 - 595 - cmd = kmalloc(sizeof(*cmd), GFP_KERNEL); 596 - if (!cmd) 597 - return -ENOMEM; 598 - 599 - cmd->id = cmd_id; 600 - cmd->len = CMD_HEADER_LEN + sizeof(struct kvaser_cmd_simple); 601 - cmd->u.simple.channel = channel; 602 - cmd->u.simple.tid = 0xff; 603 - 604 - rc = kvaser_usb_send_cmd(dev, cmd); 605 - 606 - kfree(cmd); 607 - return rc; 608 - } 609 - 610 - static int kvaser_usb_get_software_info(struct kvaser_usb *dev) 611 - { 612 - struct kvaser_cmd cmd; 613 - int err; 614 - 615 - err = kvaser_usb_send_simple_cmd(dev, CMD_GET_SOFTWARE_INFO, 0); 616 - if (err) 617 - return err; 618 - 619 - err = kvaser_usb_wait_cmd(dev, CMD_GET_SOFTWARE_INFO_REPLY, &cmd); 620 - if (err) 621 - return err; 622 - 623 - switch (dev->family) { 624 - case KVASER_LEAF: 625 - dev->fw_version = le32_to_cpu(cmd.u.leaf.softinfo.fw_version); 626 - dev->max_tx_urbs = 627 - le16_to_cpu(cmd.u.leaf.softinfo.max_outstanding_tx); 628 - break; 629 - case KVASER_USBCAN: 630 - dev->fw_version = le32_to_cpu(cmd.u.usbcan.softinfo.fw_version); 631 - dev->max_tx_urbs = 632 - le16_to_cpu(cmd.u.usbcan.softinfo.max_outstanding_tx); 633 - break; 634 - } 635 - 636 - return 0; 637 - } 638 - 639 - static int kvaser_usb_get_card_info(struct kvaser_usb *dev) 640 - { 641 - struct kvaser_cmd cmd; 642 - int err; 643 - 644 - err = kvaser_usb_send_simple_cmd(dev, CMD_GET_CARD_INFO, 0); 645 - if (err) 646 - return err; 647 - 648 - err = kvaser_usb_wait_cmd(dev, CMD_GET_CARD_INFO_REPLY, &cmd); 649 - if (err) 650 - return err; 651 - 652 - dev->nchannels = cmd.u.cardinfo.nchannels; 653 - if ((dev->nchannels > MAX_NET_DEVICES) || 654 - (dev->family == KVASER_USBCAN && 655 - dev->nchannels > MAX_USBCAN_NET_DEVICES)) 656 - return -EINVAL; 657 - 658 - return 0; 659 - } 660 - 661 - static void kvaser_usb_tx_acknowledge(const struct kvaser_usb *dev, 662 - const struct kvaser_cmd *cmd) 663 - { 664 - struct net_device_stats *stats; 665 - struct kvaser_usb_tx_urb_context *context; 666 - struct kvaser_usb_net_priv *priv; 667 - struct sk_buff *skb; 668 - struct can_frame *cf; 669 - unsigned long flags; 670 - u8 channel, tid; 671 - 672 - channel = cmd->u.tx_acknowledge_header.channel; 673 - tid = cmd->u.tx_acknowledge_header.tid; 674 - 675 - if (channel >= dev->nchannels) { 676 - dev_err(&dev->intf->dev, 677 - "Invalid channel number (%d)\n", channel); 678 - return; 679 - } 680 - 681 - priv = dev->nets[channel]; 682 - 683 - if (!netif_device_present(priv->netdev)) 684 - return; 685 - 686 - stats = &priv->netdev->stats; 687 - 688 - context = &priv->tx_contexts[tid % dev->max_tx_urbs]; 689 - 690 - /* Sometimes the state change doesn't come after a bus-off event */ 691 - if (priv->can.restart_ms && 692 - (priv->can.state >= CAN_STATE_BUS_OFF)) { 693 - skb = alloc_can_err_skb(priv->netdev, &cf); 694 - if (skb) { 695 - cf->can_id |= CAN_ERR_RESTARTED; 696 - 697 - stats->rx_packets++; 698 - stats->rx_bytes += cf->can_dlc; 699 - netif_rx(skb); 700 - } else { 701 - netdev_err(priv->netdev, 702 - "No memory left for err_skb\n"); 703 - } 704 - 705 - priv->can.can_stats.restarts++; 706 - netif_carrier_on(priv->netdev); 707 - 708 - priv->can.state = CAN_STATE_ERROR_ACTIVE; 709 - } 710 - 711 - stats->tx_packets++; 712 - stats->tx_bytes += context->dlc; 713 - 714 - spin_lock_irqsave(&priv->tx_contexts_lock, flags); 715 - 716 - can_get_echo_skb(priv->netdev, context->echo_index); 717 - context->echo_index = dev->max_tx_urbs; 718 - --priv->active_tx_contexts; 719 - netif_wake_queue(priv->netdev); 720 - 721 - spin_unlock_irqrestore(&priv->tx_contexts_lock, flags); 722 - } 723 - 724 - static void kvaser_usb_simple_cmd_callback(struct urb *urb) 725 - { 726 - struct net_device *netdev = urb->context; 727 - 728 - kfree(urb->transfer_buffer); 729 - 730 - if (urb->status) 731 - netdev_warn(netdev, "urb status received: %d\n", 732 - urb->status); 733 - } 734 - 735 - static int kvaser_usb_simple_cmd_async(struct kvaser_usb_net_priv *priv, 736 - u8 cmd_id) 737 - { 738 - struct kvaser_usb *dev = priv->dev; 739 - struct net_device *netdev = priv->netdev; 740 - struct kvaser_cmd *cmd; 741 - struct urb *urb; 742 - void *buf; 743 - int err; 744 - 745 - urb = usb_alloc_urb(0, GFP_ATOMIC); 746 - if (!urb) 747 - return -ENOMEM; 748 - 749 - buf = kmalloc(sizeof(struct kvaser_cmd), GFP_ATOMIC); 750 - if (!buf) { 751 - usb_free_urb(urb); 752 - return -ENOMEM; 753 - } 754 - 755 - cmd = (struct kvaser_cmd *)buf; 756 - cmd->len = CMD_HEADER_LEN + sizeof(struct kvaser_cmd_simple); 757 - cmd->id = cmd_id; 758 - cmd->u.simple.channel = priv->channel; 759 - 760 - usb_fill_bulk_urb(urb, dev->udev, 761 - usb_sndbulkpipe(dev->udev, 762 - dev->bulk_out->bEndpointAddress), 763 - buf, cmd->len, 764 - kvaser_usb_simple_cmd_callback, netdev); 765 - usb_anchor_urb(urb, &priv->tx_submitted); 766 - 767 - err = usb_submit_urb(urb, GFP_ATOMIC); 768 - if (err) { 769 - netdev_err(netdev, "Error transmitting URB\n"); 770 - usb_unanchor_urb(urb); 771 - kfree(buf); 772 - usb_free_urb(urb); 773 - return err; 774 - } 775 - 776 - usb_free_urb(urb); 777 - 778 - return 0; 779 - } 780 - 781 - static void kvaser_usb_rx_error_update_can_state(struct kvaser_usb_net_priv *priv, 782 - const struct kvaser_usb_error_summary *es, 783 - struct can_frame *cf) 784 - { 785 - struct kvaser_usb *dev = priv->dev; 786 - struct net_device_stats *stats = &priv->netdev->stats; 787 - enum can_state cur_state, new_state, tx_state, rx_state; 788 - 789 - netdev_dbg(priv->netdev, "Error status: 0x%02x\n", es->status); 790 - 791 - new_state = cur_state = priv->can.state; 792 - 793 - if (es->status & (M16C_STATE_BUS_OFF | M16C_STATE_BUS_RESET)) 794 - new_state = CAN_STATE_BUS_OFF; 795 - else if (es->status & M16C_STATE_BUS_PASSIVE) 796 - new_state = CAN_STATE_ERROR_PASSIVE; 797 - else if (es->status & M16C_STATE_BUS_ERROR) { 798 - /* Guard against spurious error events after a busoff */ 799 - if (cur_state < CAN_STATE_BUS_OFF) { 800 - if ((es->txerr >= 128) || (es->rxerr >= 128)) 801 - new_state = CAN_STATE_ERROR_PASSIVE; 802 - else if ((es->txerr >= 96) || (es->rxerr >= 96)) 803 - new_state = CAN_STATE_ERROR_WARNING; 804 - else if (cur_state > CAN_STATE_ERROR_ACTIVE) 805 - new_state = CAN_STATE_ERROR_ACTIVE; 806 - } 807 - } 808 - 809 - if (!es->status) 810 - new_state = CAN_STATE_ERROR_ACTIVE; 811 - 812 - if (new_state != cur_state) { 813 - tx_state = (es->txerr >= es->rxerr) ? new_state : 0; 814 - rx_state = (es->txerr <= es->rxerr) ? new_state : 0; 815 - 816 - can_change_state(priv->netdev, cf, tx_state, rx_state); 817 - } 818 - 819 - if (priv->can.restart_ms && 820 - (cur_state >= CAN_STATE_BUS_OFF) && 821 - (new_state < CAN_STATE_BUS_OFF)) { 822 - priv->can.can_stats.restarts++; 823 - } 824 - 825 - switch (dev->family) { 826 - case KVASER_LEAF: 827 - if (es->leaf.error_factor) { 828 - priv->can.can_stats.bus_error++; 829 - stats->rx_errors++; 830 - } 831 - break; 832 - case KVASER_USBCAN: 833 - if (es->usbcan.error_state & USBCAN_ERROR_STATE_TX_ERROR) 834 - stats->tx_errors++; 835 - if (es->usbcan.error_state & USBCAN_ERROR_STATE_RX_ERROR) 836 - stats->rx_errors++; 837 - if (es->usbcan.error_state & USBCAN_ERROR_STATE_BUSERROR) { 838 - priv->can.can_stats.bus_error++; 839 - } 840 - break; 841 - } 842 - 843 - priv->bec.txerr = es->txerr; 844 - priv->bec.rxerr = es->rxerr; 845 - } 846 - 847 - static void kvaser_usb_rx_error(const struct kvaser_usb *dev, 848 - const struct kvaser_usb_error_summary *es) 849 - { 850 - struct can_frame *cf, tmp_cf = { .can_id = CAN_ERR_FLAG, .can_dlc = CAN_ERR_DLC }; 851 - struct sk_buff *skb; 852 - struct net_device_stats *stats; 853 - struct kvaser_usb_net_priv *priv; 854 - enum can_state old_state, new_state; 855 - 856 - if (es->channel >= dev->nchannels) { 857 - dev_err(&dev->intf->dev, 858 - "Invalid channel number (%d)\n", es->channel); 859 - return; 860 - } 861 - 862 - priv = dev->nets[es->channel]; 863 - stats = &priv->netdev->stats; 864 - 865 - /* Update all of the CAN interface's state and error counters before 866 - * trying any memory allocation that can actually fail with -ENOMEM. 867 - * 868 - * We send a temporary stack-allocated error CAN frame to 869 - * can_change_state() for the very same reason. 870 - * 871 - * TODO: Split can_change_state() responsibility between updating the 872 - * CAN interface's state and counters, and the setting up of CAN error 873 - * frame ID and data to userspace. Remove stack allocation afterwards. 874 - */ 875 - old_state = priv->can.state; 876 - kvaser_usb_rx_error_update_can_state(priv, es, &tmp_cf); 877 - new_state = priv->can.state; 878 - 879 - skb = alloc_can_err_skb(priv->netdev, &cf); 880 - if (!skb) { 881 - stats->rx_dropped++; 882 - return; 883 - } 884 - memcpy(cf, &tmp_cf, sizeof(*cf)); 885 - 886 - if (new_state != old_state) { 887 - if (es->status & 888 - (M16C_STATE_BUS_OFF | M16C_STATE_BUS_RESET)) { 889 - if (!priv->can.restart_ms) 890 - kvaser_usb_simple_cmd_async(priv, CMD_STOP_CHIP); 891 - netif_carrier_off(priv->netdev); 892 - } 893 - 894 - if (priv->can.restart_ms && 895 - (old_state >= CAN_STATE_BUS_OFF) && 896 - (new_state < CAN_STATE_BUS_OFF)) { 897 - cf->can_id |= CAN_ERR_RESTARTED; 898 - netif_carrier_on(priv->netdev); 899 - } 900 - } 901 - 902 - switch (dev->family) { 903 - case KVASER_LEAF: 904 - if (es->leaf.error_factor) { 905 - cf->can_id |= CAN_ERR_BUSERROR | CAN_ERR_PROT; 906 - 907 - if (es->leaf.error_factor & M16C_EF_ACKE) 908 - cf->data[3] = CAN_ERR_PROT_LOC_ACK; 909 - if (es->leaf.error_factor & M16C_EF_CRCE) 910 - cf->data[3] = CAN_ERR_PROT_LOC_CRC_SEQ; 911 - if (es->leaf.error_factor & M16C_EF_FORME) 912 - cf->data[2] |= CAN_ERR_PROT_FORM; 913 - if (es->leaf.error_factor & M16C_EF_STFE) 914 - cf->data[2] |= CAN_ERR_PROT_STUFF; 915 - if (es->leaf.error_factor & M16C_EF_BITE0) 916 - cf->data[2] |= CAN_ERR_PROT_BIT0; 917 - if (es->leaf.error_factor & M16C_EF_BITE1) 918 - cf->data[2] |= CAN_ERR_PROT_BIT1; 919 - if (es->leaf.error_factor & M16C_EF_TRE) 920 - cf->data[2] |= CAN_ERR_PROT_TX; 921 - } 922 - break; 923 - case KVASER_USBCAN: 924 - if (es->usbcan.error_state & USBCAN_ERROR_STATE_BUSERROR) { 925 - cf->can_id |= CAN_ERR_BUSERROR; 926 - } 927 - break; 928 - } 929 - 930 - cf->data[6] = es->txerr; 931 - cf->data[7] = es->rxerr; 932 - 933 - stats->rx_packets++; 934 - stats->rx_bytes += cf->can_dlc; 935 - netif_rx(skb); 936 - } 937 - 938 - /* For USBCAN, report error to userspace if the channels's errors counter 939 - * has changed, or we're the only channel seeing a bus error state. 940 - */ 941 - static void kvaser_usbcan_conditionally_rx_error(const struct kvaser_usb *dev, 942 - struct kvaser_usb_error_summary *es) 943 - { 944 - struct kvaser_usb_net_priv *priv; 945 - int channel; 946 - bool report_error; 947 - 948 - channel = es->channel; 949 - if (channel >= dev->nchannels) { 950 - dev_err(&dev->intf->dev, 951 - "Invalid channel number (%d)\n", channel); 952 - return; 953 - } 954 - 955 - priv = dev->nets[channel]; 956 - report_error = false; 957 - 958 - if (es->txerr != priv->bec.txerr) { 959 - es->usbcan.error_state |= USBCAN_ERROR_STATE_TX_ERROR; 960 - report_error = true; 961 - } 962 - if (es->rxerr != priv->bec.rxerr) { 963 - es->usbcan.error_state |= USBCAN_ERROR_STATE_RX_ERROR; 964 - report_error = true; 965 - } 966 - if ((es->status & M16C_STATE_BUS_ERROR) && 967 - !(es->usbcan.other_ch_status & M16C_STATE_BUS_ERROR)) { 968 - es->usbcan.error_state |= USBCAN_ERROR_STATE_BUSERROR; 969 - report_error = true; 970 - } 971 - 972 - if (report_error) 973 - kvaser_usb_rx_error(dev, es); 974 - } 975 - 976 - static void kvaser_usbcan_rx_error(const struct kvaser_usb *dev, 977 - const struct kvaser_cmd *cmd) 978 - { 979 - struct kvaser_usb_error_summary es = { }; 980 - 981 - switch (cmd->id) { 982 - /* Sometimes errors are sent as unsolicited chip state events */ 983 - case CMD_CHIP_STATE_EVENT: 984 - es.channel = cmd->u.usbcan.chip_state_event.channel; 985 - es.status = cmd->u.usbcan.chip_state_event.status; 986 - es.txerr = cmd->u.usbcan.chip_state_event.tx_errors_count; 987 - es.rxerr = cmd->u.usbcan.chip_state_event.rx_errors_count; 988 - kvaser_usbcan_conditionally_rx_error(dev, &es); 989 - break; 990 - 991 - case CMD_CAN_ERROR_EVENT: 992 - es.channel = 0; 993 - es.status = cmd->u.usbcan.error_event.status_ch0; 994 - es.txerr = cmd->u.usbcan.error_event.tx_errors_count_ch0; 995 - es.rxerr = cmd->u.usbcan.error_event.rx_errors_count_ch0; 996 - es.usbcan.other_ch_status = 997 - cmd->u.usbcan.error_event.status_ch1; 998 - kvaser_usbcan_conditionally_rx_error(dev, &es); 999 - 1000 - /* The USBCAN firmware supports up to 2 channels. 1001 - * Now that ch0 was checked, check if ch1 has any errors. 1002 - */ 1003 - if (dev->nchannels == MAX_USBCAN_NET_DEVICES) { 1004 - es.channel = 1; 1005 - es.status = cmd->u.usbcan.error_event.status_ch1; 1006 - es.txerr = cmd->u.usbcan.error_event.tx_errors_count_ch1; 1007 - es.rxerr = cmd->u.usbcan.error_event.rx_errors_count_ch1; 1008 - es.usbcan.other_ch_status = 1009 - cmd->u.usbcan.error_event.status_ch0; 1010 - kvaser_usbcan_conditionally_rx_error(dev, &es); 1011 - } 1012 - break; 1013 - 1014 - default: 1015 - dev_err(&dev->intf->dev, "Invalid cmd id (%d)\n", cmd->id); 1016 - } 1017 - } 1018 - 1019 - static void kvaser_leaf_rx_error(const struct kvaser_usb *dev, 1020 - const struct kvaser_cmd *cmd) 1021 - { 1022 - struct kvaser_usb_error_summary es = { }; 1023 - 1024 - switch (cmd->id) { 1025 - case CMD_CAN_ERROR_EVENT: 1026 - es.channel = cmd->u.leaf.error_event.channel; 1027 - es.status = cmd->u.leaf.error_event.status; 1028 - es.txerr = cmd->u.leaf.error_event.tx_errors_count; 1029 - es.rxerr = cmd->u.leaf.error_event.rx_errors_count; 1030 - es.leaf.error_factor = cmd->u.leaf.error_event.error_factor; 1031 - break; 1032 - case CMD_LEAF_LOG_MESSAGE: 1033 - es.channel = cmd->u.leaf.log_message.channel; 1034 - es.status = cmd->u.leaf.log_message.data[0]; 1035 - es.txerr = cmd->u.leaf.log_message.data[2]; 1036 - es.rxerr = cmd->u.leaf.log_message.data[3]; 1037 - es.leaf.error_factor = cmd->u.leaf.log_message.data[1]; 1038 - break; 1039 - case CMD_CHIP_STATE_EVENT: 1040 - es.channel = cmd->u.leaf.chip_state_event.channel; 1041 - es.status = cmd->u.leaf.chip_state_event.status; 1042 - es.txerr = cmd->u.leaf.chip_state_event.tx_errors_count; 1043 - es.rxerr = cmd->u.leaf.chip_state_event.rx_errors_count; 1044 - es.leaf.error_factor = 0; 1045 - break; 1046 - default: 1047 - dev_err(&dev->intf->dev, "Invalid cmd id (%d)\n", cmd->id); 1048 - return; 1049 - } 1050 - 1051 - kvaser_usb_rx_error(dev, &es); 1052 - } 1053 - 1054 - static void kvaser_usb_rx_can_err(const struct kvaser_usb_net_priv *priv, 1055 - const struct kvaser_cmd *cmd) 1056 - { 1057 - struct can_frame *cf; 1058 - struct sk_buff *skb; 1059 - struct net_device_stats *stats = &priv->netdev->stats; 1060 - 1061 - if (cmd->u.rx_can_header.flag & (MSG_FLAG_ERROR_FRAME | 1062 - MSG_FLAG_NERR)) { 1063 - netdev_err(priv->netdev, "Unknown error (flags: 0x%02x)\n", 1064 - cmd->u.rx_can_header.flag); 1065 - 1066 - stats->rx_errors++; 1067 - return; 1068 - } 1069 - 1070 - if (cmd->u.rx_can_header.flag & MSG_FLAG_OVERRUN) { 1071 - stats->rx_over_errors++; 1072 - stats->rx_errors++; 1073 - 1074 - skb = alloc_can_err_skb(priv->netdev, &cf); 1075 - if (!skb) { 1076 - stats->rx_dropped++; 1077 - return; 1078 - } 1079 - 1080 - cf->can_id |= CAN_ERR_CRTL; 1081 - cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW; 1082 - 1083 - stats->rx_packets++; 1084 - stats->rx_bytes += cf->can_dlc; 1085 - netif_rx(skb); 1086 - } 1087 - } 1088 - 1089 - static void kvaser_usb_rx_can_msg(const struct kvaser_usb *dev, 1090 - const struct kvaser_cmd *cmd) 1091 - { 1092 - struct kvaser_usb_net_priv *priv; 1093 - struct can_frame *cf; 1094 - struct sk_buff *skb; 1095 - struct net_device_stats *stats; 1096 - u8 channel = cmd->u.rx_can_header.channel; 1097 - const u8 *rx_data = NULL; /* GCC */ 1098 - 1099 - if (channel >= dev->nchannels) { 1100 - dev_err(&dev->intf->dev, 1101 - "Invalid channel number (%d)\n", channel); 1102 - return; 1103 - } 1104 - 1105 - priv = dev->nets[channel]; 1106 - stats = &priv->netdev->stats; 1107 - 1108 - if ((cmd->u.rx_can_header.flag & MSG_FLAG_ERROR_FRAME) && 1109 - (dev->family == KVASER_LEAF && cmd->id == CMD_LEAF_LOG_MESSAGE)) { 1110 - kvaser_leaf_rx_error(dev, cmd); 1111 - return; 1112 - } else if (cmd->u.rx_can_header.flag & (MSG_FLAG_ERROR_FRAME | 1113 - MSG_FLAG_NERR | 1114 - MSG_FLAG_OVERRUN)) { 1115 - kvaser_usb_rx_can_err(priv, cmd); 1116 - return; 1117 - } else if (cmd->u.rx_can_header.flag & ~MSG_FLAG_REMOTE_FRAME) { 1118 - netdev_warn(priv->netdev, 1119 - "Unhandled frame (flags: 0x%02x)", 1120 - cmd->u.rx_can_header.flag); 1121 - return; 1122 - } 1123 - 1124 - switch (dev->family) { 1125 - case KVASER_LEAF: 1126 - rx_data = cmd->u.leaf.rx_can.data; 1127 - break; 1128 - case KVASER_USBCAN: 1129 - rx_data = cmd->u.usbcan.rx_can.data; 1130 - break; 1131 - } 1132 - 1133 - skb = alloc_can_skb(priv->netdev, &cf); 1134 - if (!skb) { 1135 - stats->rx_dropped++; 1136 - return; 1137 - } 1138 - 1139 - if (dev->family == KVASER_LEAF && cmd->id == CMD_LEAF_LOG_MESSAGE) { 1140 - cf->can_id = le32_to_cpu(cmd->u.leaf.log_message.id); 1141 - if (cf->can_id & KVASER_EXTENDED_FRAME) 1142 - cf->can_id &= CAN_EFF_MASK | CAN_EFF_FLAG; 1143 - else 1144 - cf->can_id &= CAN_SFF_MASK; 1145 - 1146 - cf->can_dlc = get_can_dlc(cmd->u.leaf.log_message.dlc); 1147 - 1148 - if (cmd->u.leaf.log_message.flags & MSG_FLAG_REMOTE_FRAME) 1149 - cf->can_id |= CAN_RTR_FLAG; 1150 - else 1151 - memcpy(cf->data, &cmd->u.leaf.log_message.data, 1152 - cf->can_dlc); 1153 - } else { 1154 - cf->can_id = ((rx_data[0] & 0x1f) << 6) | (rx_data[1] & 0x3f); 1155 - 1156 - if (cmd->id == CMD_RX_EXT_MESSAGE) { 1157 - cf->can_id <<= 18; 1158 - cf->can_id |= ((rx_data[2] & 0x0f) << 14) | 1159 - ((rx_data[3] & 0xff) << 6) | 1160 - (rx_data[4] & 0x3f); 1161 - cf->can_id |= CAN_EFF_FLAG; 1162 - } 1163 - 1164 - cf->can_dlc = get_can_dlc(rx_data[5]); 1165 - 1166 - if (cmd->u.rx_can_header.flag & MSG_FLAG_REMOTE_FRAME) 1167 - cf->can_id |= CAN_RTR_FLAG; 1168 - else 1169 - memcpy(cf->data, &rx_data[6], cf->can_dlc); 1170 - } 1171 - 1172 - stats->rx_packets++; 1173 - stats->rx_bytes += cf->can_dlc; 1174 - netif_rx(skb); 1175 - } 1176 - 1177 - static void kvaser_usb_start_chip_reply(const struct kvaser_usb *dev, 1178 - const struct kvaser_cmd *cmd) 1179 - { 1180 - struct kvaser_usb_net_priv *priv; 1181 - u8 channel = cmd->u.simple.channel; 1182 - 1183 - if (channel >= dev->nchannels) { 1184 - dev_err(&dev->intf->dev, 1185 - "Invalid channel number (%d)\n", channel); 1186 - return; 1187 - } 1188 - 1189 - priv = dev->nets[channel]; 1190 - 1191 - if (completion_done(&priv->start_comp) && 1192 - netif_queue_stopped(priv->netdev)) { 1193 - netif_wake_queue(priv->netdev); 1194 - } else { 1195 - netif_start_queue(priv->netdev); 1196 - complete(&priv->start_comp); 1197 - } 1198 - } 1199 - 1200 - static void kvaser_usb_stop_chip_reply(const struct kvaser_usb *dev, 1201 - const struct kvaser_cmd *cmd) 1202 - { 1203 - struct kvaser_usb_net_priv *priv; 1204 - u8 channel = cmd->u.simple.channel; 1205 - 1206 - if (channel >= dev->nchannels) { 1207 - dev_err(&dev->intf->dev, 1208 - "Invalid channel number (%d)\n", channel); 1209 - return; 1210 - } 1211 - 1212 - priv = dev->nets[channel]; 1213 - 1214 - complete(&priv->stop_comp); 1215 - } 1216 - 1217 - static void kvaser_usb_handle_cmd(const struct kvaser_usb *dev, 1218 - const struct kvaser_cmd *cmd) 1219 - { 1220 - switch (cmd->id) { 1221 - case CMD_START_CHIP_REPLY: 1222 - kvaser_usb_start_chip_reply(dev, cmd); 1223 - break; 1224 - 1225 - case CMD_STOP_CHIP_REPLY: 1226 - kvaser_usb_stop_chip_reply(dev, cmd); 1227 - break; 1228 - 1229 - case CMD_RX_STD_MESSAGE: 1230 - case CMD_RX_EXT_MESSAGE: 1231 - kvaser_usb_rx_can_msg(dev, cmd); 1232 - break; 1233 - 1234 - case CMD_LEAF_LOG_MESSAGE: 1235 - if (dev->family != KVASER_LEAF) 1236 - goto warn; 1237 - kvaser_usb_rx_can_msg(dev, cmd); 1238 - break; 1239 - 1240 - case CMD_CHIP_STATE_EVENT: 1241 - case CMD_CAN_ERROR_EVENT: 1242 - if (dev->family == KVASER_LEAF) 1243 - kvaser_leaf_rx_error(dev, cmd); 1244 - else 1245 - kvaser_usbcan_rx_error(dev, cmd); 1246 - break; 1247 - 1248 - case CMD_TX_ACKNOWLEDGE: 1249 - kvaser_usb_tx_acknowledge(dev, cmd); 1250 - break; 1251 - 1252 - /* Ignored commands */ 1253 - case CMD_USBCAN_CLOCK_OVERFLOW_EVENT: 1254 - if (dev->family != KVASER_USBCAN) 1255 - goto warn; 1256 - break; 1257 - 1258 - case CMD_FLUSH_QUEUE_REPLY: 1259 - if (dev->family != KVASER_LEAF) 1260 - goto warn; 1261 - break; 1262 - 1263 - default: 1264 - warn: dev_warn(&dev->intf->dev, "Unhandled command (%d)\n", cmd->id); 1265 - break; 1266 - } 1267 - } 1268 - 1269 - static void kvaser_usb_read_bulk_callback(struct urb *urb) 1270 - { 1271 - struct kvaser_usb *dev = urb->context; 1272 - struct kvaser_cmd *cmd; 1273 - int pos = 0; 1274 - int err, i; 1275 - 1276 - switch (urb->status) { 1277 - case 0: 1278 - break; 1279 - case -ENOENT: 1280 - case -EPIPE: 1281 - case -EPROTO: 1282 - case -ESHUTDOWN: 1283 - return; 1284 - default: 1285 - dev_info(&dev->intf->dev, "Rx URB aborted (%d)\n", urb->status); 1286 - goto resubmit_urb; 1287 - } 1288 - 1289 - while (pos <= (int)(urb->actual_length - CMD_HEADER_LEN)) { 1290 - cmd = urb->transfer_buffer + pos; 1291 - 1292 - /* The Kvaser firmware can only read and write commands that 1293 - * does not cross the USB's endpoint wMaxPacketSize boundary. 1294 - * If a follow-up command crosses such boundary, firmware puts 1295 - * a placeholder zero-length command in its place then aligns 1296 - * the real command to the next max packet size. 1297 - * 1298 - * Handle such cases or we're going to miss a significant 1299 - * number of events in case of a heavy rx load on the bus. 1300 - */ 1301 - if (cmd->len == 0) { 1302 - pos = round_up(pos, le16_to_cpu(dev->bulk_in-> 1303 - wMaxPacketSize)); 1304 - continue; 1305 - } 1306 - 1307 - if (pos + cmd->len > urb->actual_length) { 1308 - dev_err_ratelimited(&dev->intf->dev, "Format error\n"); 1309 - break; 1310 - } 1311 - 1312 - kvaser_usb_handle_cmd(dev, cmd); 1313 - pos += cmd->len; 1314 - } 1315 - 1316 - resubmit_urb: 1317 - usb_fill_bulk_urb(urb, dev->udev, 1318 - usb_rcvbulkpipe(dev->udev, 1319 - dev->bulk_in->bEndpointAddress), 1320 - urb->transfer_buffer, RX_BUFFER_SIZE, 1321 - kvaser_usb_read_bulk_callback, dev); 1322 - 1323 - err = usb_submit_urb(urb, GFP_ATOMIC); 1324 - if (err == -ENODEV) { 1325 - for (i = 0; i < dev->nchannels; i++) { 1326 - if (!dev->nets[i]) 1327 - continue; 1328 - 1329 - netif_device_detach(dev->nets[i]->netdev); 1330 - } 1331 - } else if (err) { 1332 - dev_err(&dev->intf->dev, 1333 - "Failed resubmitting read bulk urb: %d\n", err); 1334 - } 1335 - } 1336 - 1337 - static int kvaser_usb_setup_rx_urbs(struct kvaser_usb *dev) 1338 - { 1339 - int i, err = 0; 1340 - 1341 - if (dev->rxinitdone) 1342 - return 0; 1343 - 1344 - for (i = 0; i < MAX_RX_URBS; i++) { 1345 - struct urb *urb = NULL; 1346 - u8 *buf = NULL; 1347 - dma_addr_t buf_dma; 1348 - 1349 - urb = usb_alloc_urb(0, GFP_KERNEL); 1350 - if (!urb) { 1351 - err = -ENOMEM; 1352 - break; 1353 - } 1354 - 1355 - buf = usb_alloc_coherent(dev->udev, RX_BUFFER_SIZE, 1356 - GFP_KERNEL, &buf_dma); 1357 - if (!buf) { 1358 - dev_warn(&dev->intf->dev, 1359 - "No memory left for USB buffer\n"); 1360 - usb_free_urb(urb); 1361 - err = -ENOMEM; 1362 - break; 1363 - } 1364 - 1365 - usb_fill_bulk_urb(urb, dev->udev, 1366 - usb_rcvbulkpipe(dev->udev, 1367 - dev->bulk_in->bEndpointAddress), 1368 - buf, RX_BUFFER_SIZE, 1369 - kvaser_usb_read_bulk_callback, 1370 - dev); 1371 - urb->transfer_dma = buf_dma; 1372 - urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; 1373 - usb_anchor_urb(urb, &dev->rx_submitted); 1374 - 1375 - err = usb_submit_urb(urb, GFP_KERNEL); 1376 - if (err) { 1377 - usb_unanchor_urb(urb); 1378 - usb_free_coherent(dev->udev, RX_BUFFER_SIZE, buf, 1379 - buf_dma); 1380 - usb_free_urb(urb); 1381 - break; 1382 - } 1383 - 1384 - dev->rxbuf[i] = buf; 1385 - dev->rxbuf_dma[i] = buf_dma; 1386 - 1387 - usb_free_urb(urb); 1388 - } 1389 - 1390 - if (i == 0) { 1391 - dev_warn(&dev->intf->dev, "Cannot setup read URBs, error %d\n", 1392 - err); 1393 - return err; 1394 - } else if (i < MAX_RX_URBS) { 1395 - dev_warn(&dev->intf->dev, "RX performances may be slow\n"); 1396 - } 1397 - 1398 - dev->rxinitdone = true; 1399 - 1400 - return 0; 1401 - } 1402 - 1403 - static int kvaser_usb_set_opt_mode(const struct kvaser_usb_net_priv *priv) 1404 - { 1405 - struct kvaser_cmd *cmd; 1406 - int rc; 1407 - 1408 - cmd = kmalloc(sizeof(*cmd), GFP_KERNEL); 1409 - if (!cmd) 1410 - return -ENOMEM; 1411 - 1412 - cmd->id = CMD_SET_CTRL_MODE; 1413 - cmd->len = CMD_HEADER_LEN + sizeof(struct kvaser_cmd_ctrl_mode); 1414 - cmd->u.ctrl_mode.tid = 0xff; 1415 - cmd->u.ctrl_mode.channel = priv->channel; 1416 - 1417 - if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY) 1418 - cmd->u.ctrl_mode.ctrl_mode = KVASER_CTRL_MODE_SILENT; 1419 - else 1420 - cmd->u.ctrl_mode.ctrl_mode = KVASER_CTRL_MODE_NORMAL; 1421 - 1422 - rc = kvaser_usb_send_cmd(priv->dev, cmd); 1423 - 1424 - kfree(cmd); 1425 - return rc; 1426 - } 1427 - 1428 - static int kvaser_usb_start_chip(struct kvaser_usb_net_priv *priv) 1429 - { 1430 - int err; 1431 - 1432 - init_completion(&priv->start_comp); 1433 - 1434 - err = kvaser_usb_send_simple_cmd(priv->dev, CMD_START_CHIP, 1435 - priv->channel); 1436 - if (err) 1437 - return err; 1438 - 1439 - if (!wait_for_completion_timeout(&priv->start_comp, 1440 - msecs_to_jiffies(KVASER_USB_TIMEOUT))) 1441 - return -ETIMEDOUT; 1442 - 1443 - return 0; 1444 - } 1445 - 1446 - static int kvaser_usb_open(struct net_device *netdev) 1447 - { 1448 - struct kvaser_usb_net_priv *priv = netdev_priv(netdev); 1449 - struct kvaser_usb *dev = priv->dev; 1450 - int err; 1451 - 1452 - err = open_candev(netdev); 1453 - if (err) 1454 - return err; 1455 - 1456 - err = kvaser_usb_setup_rx_urbs(dev); 1457 - if (err) 1458 - goto error; 1459 - 1460 - err = kvaser_usb_set_opt_mode(priv); 1461 - if (err) 1462 - goto error; 1463 - 1464 - err = kvaser_usb_start_chip(priv); 1465 - if (err) { 1466 - netdev_warn(netdev, "Cannot start device, error %d\n", err); 1467 - goto error; 1468 - } 1469 - 1470 - priv->can.state = CAN_STATE_ERROR_ACTIVE; 1471 - 1472 - return 0; 1473 - 1474 - error: 1475 - close_candev(netdev); 1476 - return err; 1477 - } 1478 - 1479 - static void kvaser_usb_reset_tx_urb_contexts(struct kvaser_usb_net_priv *priv) 1480 - { 1481 - int i, max_tx_urbs; 1482 - 1483 - max_tx_urbs = priv->dev->max_tx_urbs; 1484 - 1485 - priv->active_tx_contexts = 0; 1486 - for (i = 0; i < max_tx_urbs; i++) 1487 - priv->tx_contexts[i].echo_index = max_tx_urbs; 1488 - } 1489 - 1490 - /* This method might sleep. Do not call it in the atomic context 1491 - * of URB completions. 1492 - */ 1493 - static void kvaser_usb_unlink_tx_urbs(struct kvaser_usb_net_priv *priv) 1494 - { 1495 - usb_kill_anchored_urbs(&priv->tx_submitted); 1496 - kvaser_usb_reset_tx_urb_contexts(priv); 1497 - } 1498 - 1499 - static void kvaser_usb_unlink_all_urbs(struct kvaser_usb *dev) 1500 - { 1501 - int i; 1502 - 1503 - usb_kill_anchored_urbs(&dev->rx_submitted); 1504 - 1505 - for (i = 0; i < MAX_RX_URBS; i++) 1506 - usb_free_coherent(dev->udev, RX_BUFFER_SIZE, 1507 - dev->rxbuf[i], 1508 - dev->rxbuf_dma[i]); 1509 - 1510 - for (i = 0; i < dev->nchannels; i++) { 1511 - struct kvaser_usb_net_priv *priv = dev->nets[i]; 1512 - 1513 - if (priv) 1514 - kvaser_usb_unlink_tx_urbs(priv); 1515 - } 1516 - } 1517 - 1518 - static int kvaser_usb_stop_chip(struct kvaser_usb_net_priv *priv) 1519 - { 1520 - int err; 1521 - 1522 - init_completion(&priv->stop_comp); 1523 - 1524 - err = kvaser_usb_send_simple_cmd(priv->dev, CMD_STOP_CHIP, 1525 - priv->channel); 1526 - if (err) 1527 - return err; 1528 - 1529 - if (!wait_for_completion_timeout(&priv->stop_comp, 1530 - msecs_to_jiffies(KVASER_USB_TIMEOUT))) 1531 - return -ETIMEDOUT; 1532 - 1533 - return 0; 1534 - } 1535 - 1536 - static int kvaser_usb_flush_queue(struct kvaser_usb_net_priv *priv) 1537 - { 1538 - struct kvaser_cmd *cmd; 1539 - int rc; 1540 - 1541 - cmd = kmalloc(sizeof(*cmd), GFP_KERNEL); 1542 - if (!cmd) 1543 - return -ENOMEM; 1544 - 1545 - cmd->id = CMD_FLUSH_QUEUE; 1546 - cmd->len = CMD_HEADER_LEN + sizeof(struct kvaser_cmd_flush_queue); 1547 - cmd->u.flush_queue.channel = priv->channel; 1548 - cmd->u.flush_queue.flags = 0x00; 1549 - 1550 - rc = kvaser_usb_send_cmd(priv->dev, cmd); 1551 - 1552 - kfree(cmd); 1553 - return rc; 1554 - } 1555 - 1556 - static int kvaser_usb_close(struct net_device *netdev) 1557 - { 1558 - struct kvaser_usb_net_priv *priv = netdev_priv(netdev); 1559 - struct kvaser_usb *dev = priv->dev; 1560 - int err; 1561 - 1562 - netif_stop_queue(netdev); 1563 - 1564 - err = kvaser_usb_flush_queue(priv); 1565 - if (err) 1566 - netdev_warn(netdev, "Cannot flush queue, error %d\n", err); 1567 - 1568 - err = kvaser_usb_send_simple_cmd(dev, CMD_RESET_CHIP, priv->channel); 1569 - if (err) 1570 - netdev_warn(netdev, "Cannot reset card, error %d\n", err); 1571 - 1572 - err = kvaser_usb_stop_chip(priv); 1573 - if (err) 1574 - netdev_warn(netdev, "Cannot stop device, error %d\n", err); 1575 - 1576 - /* reset tx contexts */ 1577 - kvaser_usb_unlink_tx_urbs(priv); 1578 - 1579 - priv->can.state = CAN_STATE_STOPPED; 1580 - close_candev(priv->netdev); 1581 - 1582 - return 0; 1583 - } 1584 - 1585 - static void kvaser_usb_write_bulk_callback(struct urb *urb) 1586 - { 1587 - struct kvaser_usb_tx_urb_context *context = urb->context; 1588 - struct kvaser_usb_net_priv *priv; 1589 - struct net_device *netdev; 1590 - 1591 - if (WARN_ON(!context)) 1592 - return; 1593 - 1594 - priv = context->priv; 1595 - netdev = priv->netdev; 1596 - 1597 - kfree(urb->transfer_buffer); 1598 - 1599 - if (!netif_device_present(netdev)) 1600 - return; 1601 - 1602 - if (urb->status) 1603 - netdev_info(netdev, "Tx URB aborted (%d)\n", urb->status); 1604 - } 1605 - 1606 - static netdev_tx_t kvaser_usb_start_xmit(struct sk_buff *skb, 1607 - struct net_device *netdev) 1608 - { 1609 - struct kvaser_usb_net_priv *priv = netdev_priv(netdev); 1610 - struct kvaser_usb *dev = priv->dev; 1611 - struct net_device_stats *stats = &netdev->stats; 1612 - struct can_frame *cf = (struct can_frame *)skb->data; 1613 - struct kvaser_usb_tx_urb_context *context = NULL; 1614 - struct urb *urb; 1615 - void *buf; 1616 - struct kvaser_cmd *cmd; 1617 - int i, err, ret = NETDEV_TX_OK; 1618 - u8 *cmd_tx_can_flags = NULL; /* GCC */ 1619 - unsigned long flags; 1620 - 1621 - if (can_dropped_invalid_skb(netdev, skb)) 1622 - return NETDEV_TX_OK; 1623 - 1624 - urb = usb_alloc_urb(0, GFP_ATOMIC); 1625 - if (!urb) { 1626 - stats->tx_dropped++; 1627 - dev_kfree_skb(skb); 1628 - return NETDEV_TX_OK; 1629 - } 1630 - 1631 - buf = kmalloc(sizeof(struct kvaser_cmd), GFP_ATOMIC); 1632 - if (!buf) { 1633 - stats->tx_dropped++; 1634 - dev_kfree_skb(skb); 1635 - goto freeurb; 1636 - } 1637 - 1638 - cmd = buf; 1639 - cmd->len = CMD_HEADER_LEN + sizeof(struct kvaser_cmd_tx_can); 1640 - cmd->u.tx_can.channel = priv->channel; 1641 - 1642 - switch (dev->family) { 1643 - case KVASER_LEAF: 1644 - cmd_tx_can_flags = &cmd->u.tx_can.leaf.flags; 1645 - break; 1646 - case KVASER_USBCAN: 1647 - cmd_tx_can_flags = &cmd->u.tx_can.usbcan.flags; 1648 - break; 1649 - } 1650 - 1651 - *cmd_tx_can_flags = 0; 1652 - 1653 - if (cf->can_id & CAN_EFF_FLAG) { 1654 - cmd->id = CMD_TX_EXT_MESSAGE; 1655 - cmd->u.tx_can.data[0] = (cf->can_id >> 24) & 0x1f; 1656 - cmd->u.tx_can.data[1] = (cf->can_id >> 18) & 0x3f; 1657 - cmd->u.tx_can.data[2] = (cf->can_id >> 14) & 0x0f; 1658 - cmd->u.tx_can.data[3] = (cf->can_id >> 6) & 0xff; 1659 - cmd->u.tx_can.data[4] = cf->can_id & 0x3f; 1660 - } else { 1661 - cmd->id = CMD_TX_STD_MESSAGE; 1662 - cmd->u.tx_can.data[0] = (cf->can_id >> 6) & 0x1f; 1663 - cmd->u.tx_can.data[1] = cf->can_id & 0x3f; 1664 - } 1665 - 1666 - cmd->u.tx_can.data[5] = cf->can_dlc; 1667 - memcpy(&cmd->u.tx_can.data[6], cf->data, cf->can_dlc); 1668 - 1669 - if (cf->can_id & CAN_RTR_FLAG) 1670 - *cmd_tx_can_flags |= MSG_FLAG_REMOTE_FRAME; 1671 - 1672 - spin_lock_irqsave(&priv->tx_contexts_lock, flags); 1673 - for (i = 0; i < dev->max_tx_urbs; i++) { 1674 - if (priv->tx_contexts[i].echo_index == dev->max_tx_urbs) { 1675 - context = &priv->tx_contexts[i]; 1676 - 1677 - context->echo_index = i; 1678 - can_put_echo_skb(skb, netdev, context->echo_index); 1679 - ++priv->active_tx_contexts; 1680 - if (priv->active_tx_contexts >= dev->max_tx_urbs) 1681 - netif_stop_queue(netdev); 1682 - 1683 - break; 1684 - } 1685 - } 1686 - spin_unlock_irqrestore(&priv->tx_contexts_lock, flags); 1687 - 1688 - /* This should never happen; it implies a flow control bug */ 1689 - if (!context) { 1690 - netdev_warn(netdev, "cannot find free context\n"); 1691 - 1692 - kfree(buf); 1693 - ret = NETDEV_TX_BUSY; 1694 - goto freeurb; 1695 - } 1696 - 1697 - context->priv = priv; 1698 - context->dlc = cf->can_dlc; 1699 - 1700 - cmd->u.tx_can.tid = context->echo_index; 1701 - 1702 - usb_fill_bulk_urb(urb, dev->udev, 1703 - usb_sndbulkpipe(dev->udev, 1704 - dev->bulk_out->bEndpointAddress), 1705 - buf, cmd->len, 1706 - kvaser_usb_write_bulk_callback, context); 1707 - usb_anchor_urb(urb, &priv->tx_submitted); 1708 - 1709 - err = usb_submit_urb(urb, GFP_ATOMIC); 1710 - if (unlikely(err)) { 1711 - spin_lock_irqsave(&priv->tx_contexts_lock, flags); 1712 - 1713 - can_free_echo_skb(netdev, context->echo_index); 1714 - context->echo_index = dev->max_tx_urbs; 1715 - --priv->active_tx_contexts; 1716 - netif_wake_queue(netdev); 1717 - 1718 - spin_unlock_irqrestore(&priv->tx_contexts_lock, flags); 1719 - 1720 - usb_unanchor_urb(urb); 1721 - kfree(buf); 1722 - 1723 - stats->tx_dropped++; 1724 - 1725 - if (err == -ENODEV) 1726 - netif_device_detach(netdev); 1727 - else 1728 - netdev_warn(netdev, "Failed tx_urb %d\n", err); 1729 - 1730 - goto freeurb; 1731 - } 1732 - 1733 - ret = NETDEV_TX_OK; 1734 - 1735 - freeurb: 1736 - usb_free_urb(urb); 1737 - return ret; 1738 - } 1739 - 1740 - static const struct net_device_ops kvaser_usb_netdev_ops = { 1741 - .ndo_open = kvaser_usb_open, 1742 - .ndo_stop = kvaser_usb_close, 1743 - .ndo_start_xmit = kvaser_usb_start_xmit, 1744 - .ndo_change_mtu = can_change_mtu, 1745 - }; 1746 - 1747 - static const struct can_bittiming_const kvaser_usb_bittiming_const = { 1748 - .name = "kvaser_usb", 1749 - .tseg1_min = KVASER_USB_TSEG1_MIN, 1750 - .tseg1_max = KVASER_USB_TSEG1_MAX, 1751 - .tseg2_min = KVASER_USB_TSEG2_MIN, 1752 - .tseg2_max = KVASER_USB_TSEG2_MAX, 1753 - .sjw_max = KVASER_USB_SJW_MAX, 1754 - .brp_min = KVASER_USB_BRP_MIN, 1755 - .brp_max = KVASER_USB_BRP_MAX, 1756 - .brp_inc = KVASER_USB_BRP_INC, 1757 - }; 1758 - 1759 - static int kvaser_usb_set_bittiming(struct net_device *netdev) 1760 - { 1761 - struct kvaser_usb_net_priv *priv = netdev_priv(netdev); 1762 - struct can_bittiming *bt = &priv->can.bittiming; 1763 - struct kvaser_usb *dev = priv->dev; 1764 - struct kvaser_cmd *cmd; 1765 - int rc; 1766 - 1767 - cmd = kmalloc(sizeof(*cmd), GFP_KERNEL); 1768 - if (!cmd) 1769 - return -ENOMEM; 1770 - 1771 - cmd->id = CMD_SET_BUS_PARAMS; 1772 - cmd->len = CMD_HEADER_LEN + sizeof(struct kvaser_cmd_busparams); 1773 - cmd->u.busparams.channel = priv->channel; 1774 - cmd->u.busparams.tid = 0xff; 1775 - cmd->u.busparams.bitrate = cpu_to_le32(bt->bitrate); 1776 - cmd->u.busparams.sjw = bt->sjw; 1777 - cmd->u.busparams.tseg1 = bt->prop_seg + bt->phase_seg1; 1778 - cmd->u.busparams.tseg2 = bt->phase_seg2; 1779 - 1780 - if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES) 1781 - cmd->u.busparams.no_samp = 3; 1782 - else 1783 - cmd->u.busparams.no_samp = 1; 1784 - 1785 - rc = kvaser_usb_send_cmd(dev, cmd); 1786 - 1787 - kfree(cmd); 1788 - return rc; 1789 - } 1790 - 1791 - static int kvaser_usb_set_mode(struct net_device *netdev, 1792 - enum can_mode mode) 1793 - { 1794 - struct kvaser_usb_net_priv *priv = netdev_priv(netdev); 1795 - int err; 1796 - 1797 - switch (mode) { 1798 - case CAN_MODE_START: 1799 - err = kvaser_usb_simple_cmd_async(priv, CMD_START_CHIP); 1800 - if (err) 1801 - return err; 1802 - break; 1803 - default: 1804 - return -EOPNOTSUPP; 1805 - } 1806 - 1807 - return 0; 1808 - } 1809 - 1810 - static int kvaser_usb_get_berr_counter(const struct net_device *netdev, 1811 - struct can_berr_counter *bec) 1812 - { 1813 - struct kvaser_usb_net_priv *priv = netdev_priv(netdev); 1814 - 1815 - *bec = priv->bec; 1816 - 1817 - return 0; 1818 - } 1819 - 1820 - static void kvaser_usb_remove_interfaces(struct kvaser_usb *dev) 1821 - { 1822 - int i; 1823 - 1824 - for (i = 0; i < dev->nchannels; i++) { 1825 - if (!dev->nets[i]) 1826 - continue; 1827 - 1828 - unregister_candev(dev->nets[i]->netdev); 1829 - } 1830 - 1831 - kvaser_usb_unlink_all_urbs(dev); 1832 - 1833 - for (i = 0; i < dev->nchannels; i++) { 1834 - if (!dev->nets[i]) 1835 - continue; 1836 - 1837 - free_candev(dev->nets[i]->netdev); 1838 - } 1839 - } 1840 - 1841 - static int kvaser_usb_init_one(struct kvaser_usb *dev, 1842 - const struct usb_device_id *id, int channel) 1843 - { 1844 - struct net_device *netdev; 1845 - struct kvaser_usb_net_priv *priv; 1846 - int err; 1847 - 1848 - err = kvaser_usb_send_simple_cmd(dev, CMD_RESET_CHIP, channel); 1849 - if (err) 1850 - return err; 1851 - 1852 - netdev = alloc_candev(sizeof(*priv) + 1853 - dev->max_tx_urbs * sizeof(*priv->tx_contexts), 1854 - dev->max_tx_urbs); 1855 - if (!netdev) { 1856 - dev_err(&dev->intf->dev, "Cannot alloc candev\n"); 1857 - return -ENOMEM; 1858 - } 1859 - 1860 - priv = netdev_priv(netdev); 1861 - 1862 - init_usb_anchor(&priv->tx_submitted); 1863 - init_completion(&priv->start_comp); 1864 - init_completion(&priv->stop_comp); 1865 - 1866 - priv->dev = dev; 1867 - priv->netdev = netdev; 1868 - priv->channel = channel; 1869 - 1870 - spin_lock_init(&priv->tx_contexts_lock); 1871 - kvaser_usb_reset_tx_urb_contexts(priv); 1872 - 1873 - priv->can.state = CAN_STATE_STOPPED; 1874 - priv->can.clock.freq = CAN_USB_CLOCK; 1875 - priv->can.bittiming_const = &kvaser_usb_bittiming_const; 1876 - priv->can.do_set_bittiming = kvaser_usb_set_bittiming; 1877 - priv->can.do_set_mode = kvaser_usb_set_mode; 1878 - if (id->driver_info & KVASER_HAS_TXRX_ERRORS) 1879 - priv->can.do_get_berr_counter = kvaser_usb_get_berr_counter; 1880 - priv->can.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES; 1881 - if (id->driver_info & KVASER_HAS_SILENT_MODE) 1882 - priv->can.ctrlmode_supported |= CAN_CTRLMODE_LISTENONLY; 1883 - 1884 - netdev->flags |= IFF_ECHO; 1885 - 1886 - netdev->netdev_ops = &kvaser_usb_netdev_ops; 1887 - 1888 - SET_NETDEV_DEV(netdev, &dev->intf->dev); 1889 - netdev->dev_id = channel; 1890 - 1891 - dev->nets[channel] = priv; 1892 - 1893 - err = register_candev(netdev); 1894 - if (err) { 1895 - dev_err(&dev->intf->dev, "Failed to register CAN device\n"); 1896 - free_candev(netdev); 1897 - dev->nets[channel] = NULL; 1898 - return err; 1899 - } 1900 - 1901 - netdev_dbg(netdev, "device registered\n"); 1902 - 1903 - return 0; 1904 - } 1905 - 1906 - static int kvaser_usb_setup_endpoints(struct kvaser_usb *dev) 1907 - { 1908 - const struct usb_host_interface *iface_desc; 1909 - struct usb_endpoint_descriptor *endpoint; 1910 - int i; 1911 - 1912 - iface_desc = &dev->intf->altsetting[0]; 1913 - 1914 - for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) { 1915 - endpoint = &iface_desc->endpoint[i].desc; 1916 - 1917 - if (!dev->bulk_in && usb_endpoint_is_bulk_in(endpoint)) 1918 - dev->bulk_in = endpoint; 1919 - 1920 - if (!dev->bulk_out && usb_endpoint_is_bulk_out(endpoint)) 1921 - dev->bulk_out = endpoint; 1922 - 1923 - /* use first bulk endpoint for in and out */ 1924 - if (dev->bulk_in && dev->bulk_out) 1925 - return 0; 1926 - } 1927 - 1928 - return -ENODEV; 1929 - } 1930 - 1931 - static int kvaser_usb_probe(struct usb_interface *intf, 1932 - const struct usb_device_id *id) 1933 - { 1934 - struct kvaser_usb *dev; 1935 - int err = -ENOMEM; 1936 - int i, retry = 3; 1937 - 1938 - dev = devm_kzalloc(&intf->dev, sizeof(*dev), GFP_KERNEL); 1939 - if (!dev) 1940 - return -ENOMEM; 1941 - 1942 - if (kvaser_is_leaf(id)) { 1943 - dev->family = KVASER_LEAF; 1944 - } else if (kvaser_is_usbcan(id)) { 1945 - dev->family = KVASER_USBCAN; 1946 - } else { 1947 - dev_err(&intf->dev, 1948 - "Product ID (%d) does not belong to any known Kvaser USB family", 1949 - id->idProduct); 1950 - return -ENODEV; 1951 - } 1952 - 1953 - dev->intf = intf; 1954 - 1955 - err = kvaser_usb_setup_endpoints(dev); 1956 - if (err) { 1957 - dev_err(&intf->dev, "Cannot get usb endpoint(s)"); 1958 - return err; 1959 - } 1960 - 1961 - dev->udev = interface_to_usbdev(intf); 1962 - 1963 - init_usb_anchor(&dev->rx_submitted); 1964 - 1965 - usb_set_intfdata(intf, dev); 1966 - 1967 - /* On some x86 laptops, plugging a Kvaser device again after 1968 - * an unplug makes the firmware always ignore the very first 1969 - * command. For such a case, provide some room for retries 1970 - * instead of completely exiting the driver. 1971 - */ 1972 - do { 1973 - err = kvaser_usb_get_software_info(dev); 1974 - } while (--retry && err == -ETIMEDOUT); 1975 - 1976 - if (err) { 1977 - dev_err(&intf->dev, 1978 - "Cannot get software infos, error %d\n", err); 1979 - return err; 1980 - } 1981 - 1982 - dev_dbg(&intf->dev, "Firmware version: %d.%d.%d\n", 1983 - ((dev->fw_version >> 24) & 0xff), 1984 - ((dev->fw_version >> 16) & 0xff), 1985 - (dev->fw_version & 0xffff)); 1986 - 1987 - dev_dbg(&intf->dev, "Max outstanding tx = %d URBs\n", dev->max_tx_urbs); 1988 - 1989 - err = kvaser_usb_get_card_info(dev); 1990 - if (err) { 1991 - dev_err(&intf->dev, 1992 - "Cannot get card infos, error %d\n", err); 1993 - return err; 1994 - } 1995 - 1996 - for (i = 0; i < dev->nchannels; i++) { 1997 - err = kvaser_usb_init_one(dev, id, i); 1998 - if (err) { 1999 - kvaser_usb_remove_interfaces(dev); 2000 - return err; 2001 - } 2002 - } 2003 - 2004 - return 0; 2005 - } 2006 - 2007 - static void kvaser_usb_disconnect(struct usb_interface *intf) 2008 - { 2009 - struct kvaser_usb *dev = usb_get_intfdata(intf); 2010 - 2011 - usb_set_intfdata(intf, NULL); 2012 - 2013 - if (!dev) 2014 - return; 2015 - 2016 - kvaser_usb_remove_interfaces(dev); 2017 - } 2018 - 2019 - static struct usb_driver kvaser_usb_driver = { 2020 - .name = "kvaser_usb", 2021 - .probe = kvaser_usb_probe, 2022 - .disconnect = kvaser_usb_disconnect, 2023 - .id_table = kvaser_usb_table, 2024 - }; 2025 - 2026 - module_usb_driver(kvaser_usb_driver); 2027 - 2028 - MODULE_AUTHOR("Olivier Sobrie <olivier@sobrie.be>"); 2029 - MODULE_DESCRIPTION("CAN driver for Kvaser CAN/USB devices"); 2030 - MODULE_LICENSE("GPL v2");
+2
drivers/net/can/usb/kvaser_usb/Makefile
··· 1 + obj-$(CONFIG_CAN_KVASER_USB) += kvaser_usb.o 2 + kvaser_usb-y = kvaser_usb_core.o kvaser_usb_leaf.o
+158
drivers/net/can/usb/kvaser_usb/kvaser_usb.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0 */ 2 + /* Parts of this driver are based on the following: 3 + * - Kvaser linux leaf driver (version 4.78) 4 + * - CAN driver for esd CAN-USB/2 5 + * - Kvaser linux usbcanII driver (version 5.3) 6 + * 7 + * Copyright (C) 2002-2018 KVASER AB, Sweden. All rights reserved. 8 + * Copyright (C) 2010 Matthias Fuchs <matthias.fuchs@esd.eu>, esd gmbh 9 + * Copyright (C) 2012 Olivier Sobrie <olivier@sobrie.be> 10 + * Copyright (C) 2015 Valeo S.A. 11 + */ 12 + 13 + #ifndef KVASER_USB_H 14 + #define KVASER_USB_H 15 + 16 + /* Kvaser USB CAN dongles are divided into two major families: 17 + * - Leaf: Based on Renesas M32C, running firmware labeled as 'filo' 18 + * - UsbcanII: Based on Renesas M16C, running firmware labeled as 'helios' 19 + */ 20 + 21 + #include <linux/completion.h> 22 + #include <linux/spinlock.h> 23 + #include <linux/types.h> 24 + #include <linux/usb.h> 25 + 26 + #include <linux/can.h> 27 + #include <linux/can/dev.h> 28 + 29 + #define KVASER_USB_MAX_RX_URBS 4 30 + #define KVASER_USB_MAX_TX_URBS 128 31 + #define KVASER_USB_TIMEOUT 1000 /* msecs */ 32 + #define KVASER_USB_RX_BUFFER_SIZE 3072 33 + #define KVASER_USB_MAX_NET_DEVICES 3 34 + 35 + /* USB devices features */ 36 + #define KVASER_USB_HAS_SILENT_MODE BIT(0) 37 + #define KVASER_USB_HAS_TXRX_ERRORS BIT(1) 38 + 39 + struct kvaser_usb_dev_cfg; 40 + 41 + enum kvaser_usb_leaf_family { 42 + KVASER_LEAF, 43 + KVASER_USBCAN, 44 + }; 45 + 46 + struct kvaser_usb_dev_card_data { 47 + u32 ctrlmode_supported; 48 + struct { 49 + enum kvaser_usb_leaf_family family; 50 + } leaf; 51 + }; 52 + 53 + /* Context for an outstanding, not yet ACKed, transmission */ 54 + struct kvaser_usb_tx_urb_context { 55 + struct kvaser_usb_net_priv *priv; 56 + u32 echo_index; 57 + int dlc; 58 + }; 59 + 60 + struct kvaser_usb { 61 + struct usb_device *udev; 62 + struct usb_interface *intf; 63 + struct kvaser_usb_net_priv *nets[KVASER_USB_MAX_NET_DEVICES]; 64 + const struct kvaser_usb_dev_ops *ops; 65 + const struct kvaser_usb_dev_cfg *cfg; 66 + 67 + struct usb_endpoint_descriptor *bulk_in, *bulk_out; 68 + struct usb_anchor rx_submitted; 69 + 70 + /* @max_tx_urbs: Firmware-reported maximum number of outstanding, 71 + * not yet ACKed, transmissions on this device. This value is 72 + * also used as a sentinel for marking free tx contexts. 73 + */ 74 + u32 fw_version; 75 + unsigned int nchannels; 76 + unsigned int max_tx_urbs; 77 + struct kvaser_usb_dev_card_data card_data; 78 + 79 + bool rxinitdone; 80 + void *rxbuf[KVASER_USB_MAX_RX_URBS]; 81 + dma_addr_t rxbuf_dma[KVASER_USB_MAX_RX_URBS]; 82 + }; 83 + 84 + struct kvaser_usb_net_priv { 85 + struct can_priv can; 86 + struct can_berr_counter bec; 87 + 88 + struct kvaser_usb *dev; 89 + struct net_device *netdev; 90 + int channel; 91 + 92 + struct completion start_comp, stop_comp; 93 + struct usb_anchor tx_submitted; 94 + 95 + spinlock_t tx_contexts_lock; /* lock for active_tx_contexts */ 96 + int active_tx_contexts; 97 + struct kvaser_usb_tx_urb_context tx_contexts[]; 98 + }; 99 + 100 + /** 101 + * struct kvaser_usb_dev_ops - Device specific functions 102 + * @dev_set_mode: used for can.do_set_mode 103 + * @dev_set_bittiming: used for can.do_set_bittiming 104 + * @dev_get_berr_counter: used for can.do_get_berr_counter 105 + * 106 + * @dev_setup_endpoints: setup USB in and out endpoints 107 + * @dev_init_card: initialize card 108 + * @dev_get_software_info: get software info 109 + * @dev_get_card_info: get card info 110 + * 111 + * @dev_set_opt_mode: set ctrlmod 112 + * @dev_start_chip: start the CAN controller 113 + * @dev_stop_chip: stop the CAN controller 114 + * @dev_reset_chip: reset the CAN controller 115 + * @dev_flush_queue: flush outstanding CAN messages 116 + * @dev_read_bulk_callback: handle incoming commands 117 + * @dev_frame_to_cmd: translate struct can_frame into device command 118 + */ 119 + struct kvaser_usb_dev_ops { 120 + int (*dev_set_mode)(struct net_device *netdev, enum can_mode mode); 121 + int (*dev_set_bittiming)(struct net_device *netdev); 122 + int (*dev_get_berr_counter)(const struct net_device *netdev, 123 + struct can_berr_counter *bec); 124 + int (*dev_setup_endpoints)(struct kvaser_usb *dev); 125 + int (*dev_init_card)(struct kvaser_usb *dev); 126 + int (*dev_get_software_info)(struct kvaser_usb *dev); 127 + int (*dev_get_card_info)(struct kvaser_usb *dev); 128 + int (*dev_set_opt_mode)(const struct kvaser_usb_net_priv *priv); 129 + int (*dev_start_chip)(struct kvaser_usb_net_priv *priv); 130 + int (*dev_stop_chip)(struct kvaser_usb_net_priv *priv); 131 + int (*dev_reset_chip)(struct kvaser_usb *dev, int channel); 132 + int (*dev_flush_queue)(struct kvaser_usb_net_priv *priv); 133 + void (*dev_read_bulk_callback)(struct kvaser_usb *dev, void *buf, 134 + int len); 135 + void *(*dev_frame_to_cmd)(const struct kvaser_usb_net_priv *priv, 136 + const struct sk_buff *skb, int *frame_len, 137 + int *cmd_len, u16 transid); 138 + }; 139 + 140 + struct kvaser_usb_dev_cfg { 141 + const struct can_clock clock; 142 + const unsigned int timestamp_freq; 143 + const struct can_bittiming_const * const bittiming_const; 144 + const struct can_bittiming_const * const data_bittiming_const; 145 + }; 146 + 147 + extern const struct kvaser_usb_dev_ops kvaser_usb_leaf_dev_ops; 148 + 149 + int kvaser_usb_recv_cmd(const struct kvaser_usb *dev, void *cmd, int len, 150 + int *actual_len); 151 + 152 + int kvaser_usb_send_cmd(const struct kvaser_usb *dev, void *cmd, int len); 153 + 154 + int kvaser_usb_send_cmd_async(struct kvaser_usb_net_priv *priv, void *cmd, 155 + int len); 156 + 157 + int kvaser_usb_can_rx_over_error(struct net_device *netdev); 158 + #endif /* KVASER_USB_H */
+769
drivers/net/can/usb/kvaser_usb/kvaser_usb_core.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + /* Parts of this driver are based on the following: 3 + * - Kvaser linux leaf driver (version 4.78) 4 + * - CAN driver for esd CAN-USB/2 5 + * - Kvaser linux usbcanII driver (version 5.3) 6 + * 7 + * Copyright (C) 2002-2018 KVASER AB, Sweden. All rights reserved. 8 + * Copyright (C) 2010 Matthias Fuchs <matthias.fuchs@esd.eu>, esd gmbh 9 + * Copyright (C) 2012 Olivier Sobrie <olivier@sobrie.be> 10 + * Copyright (C) 2015 Valeo S.A. 11 + */ 12 + 13 + #include <linux/completion.h> 14 + #include <linux/device.h> 15 + #include <linux/gfp.h> 16 + #include <linux/if.h> 17 + #include <linux/kernel.h> 18 + #include <linux/module.h> 19 + #include <linux/netdevice.h> 20 + #include <linux/spinlock.h> 21 + #include <linux/types.h> 22 + #include <linux/usb.h> 23 + 24 + #include <linux/can.h> 25 + #include <linux/can/dev.h> 26 + #include <linux/can/error.h> 27 + #include <linux/can/netlink.h> 28 + 29 + #include "kvaser_usb.h" 30 + 31 + /* Kvaser USB vendor id. */ 32 + #define KVASER_VENDOR_ID 0x0bfd 33 + 34 + /* Kvaser Leaf USB devices product ids */ 35 + #define USB_LEAF_DEVEL_PRODUCT_ID 10 36 + #define USB_LEAF_LITE_PRODUCT_ID 11 37 + #define USB_LEAF_PRO_PRODUCT_ID 12 38 + #define USB_LEAF_SPRO_PRODUCT_ID 14 39 + #define USB_LEAF_PRO_LS_PRODUCT_ID 15 40 + #define USB_LEAF_PRO_SWC_PRODUCT_ID 16 41 + #define USB_LEAF_PRO_LIN_PRODUCT_ID 17 42 + #define USB_LEAF_SPRO_LS_PRODUCT_ID 18 43 + #define USB_LEAF_SPRO_SWC_PRODUCT_ID 19 44 + #define USB_MEMO2_DEVEL_PRODUCT_ID 22 45 + #define USB_MEMO2_HSHS_PRODUCT_ID 23 46 + #define USB_UPRO_HSHS_PRODUCT_ID 24 47 + #define USB_LEAF_LITE_GI_PRODUCT_ID 25 48 + #define USB_LEAF_PRO_OBDII_PRODUCT_ID 26 49 + #define USB_MEMO2_HSLS_PRODUCT_ID 27 50 + #define USB_LEAF_LITE_CH_PRODUCT_ID 28 51 + #define USB_BLACKBIRD_SPRO_PRODUCT_ID 29 52 + #define USB_OEM_MERCURY_PRODUCT_ID 34 53 + #define USB_OEM_LEAF_PRODUCT_ID 35 54 + #define USB_CAN_R_PRODUCT_ID 39 55 + #define USB_LEAF_LITE_V2_PRODUCT_ID 288 56 + #define USB_MINI_PCIE_HS_PRODUCT_ID 289 57 + #define USB_LEAF_LIGHT_HS_V2_OEM_PRODUCT_ID 290 58 + #define USB_USBCAN_LIGHT_2HS_PRODUCT_ID 291 59 + #define USB_MINI_PCIE_2HS_PRODUCT_ID 292 60 + 61 + /* Kvaser USBCan-II devices product ids */ 62 + #define USB_USBCAN_REVB_PRODUCT_ID 2 63 + #define USB_VCI2_PRODUCT_ID 3 64 + #define USB_USBCAN2_PRODUCT_ID 4 65 + #define USB_MEMORATOR_PRODUCT_ID 5 66 + 67 + static inline bool kvaser_is_leaf(const struct usb_device_id *id) 68 + { 69 + return id->idProduct >= USB_LEAF_DEVEL_PRODUCT_ID && 70 + id->idProduct <= USB_MINI_PCIE_2HS_PRODUCT_ID; 71 + } 72 + 73 + static inline bool kvaser_is_usbcan(const struct usb_device_id *id) 74 + { 75 + return id->idProduct >= USB_USBCAN_REVB_PRODUCT_ID && 76 + id->idProduct <= USB_MEMORATOR_PRODUCT_ID; 77 + } 78 + 79 + static const struct usb_device_id kvaser_usb_table[] = { 80 + /* Leaf USB product IDs */ 81 + { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_DEVEL_PRODUCT_ID) }, 82 + { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_PRODUCT_ID) }, 83 + { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_PRODUCT_ID), 84 + .driver_info = KVASER_USB_HAS_TXRX_ERRORS | 85 + KVASER_USB_HAS_SILENT_MODE }, 86 + { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_SPRO_PRODUCT_ID), 87 + .driver_info = KVASER_USB_HAS_TXRX_ERRORS | 88 + KVASER_USB_HAS_SILENT_MODE }, 89 + { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_LS_PRODUCT_ID), 90 + .driver_info = KVASER_USB_HAS_TXRX_ERRORS | 91 + KVASER_USB_HAS_SILENT_MODE }, 92 + { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_SWC_PRODUCT_ID), 93 + .driver_info = KVASER_USB_HAS_TXRX_ERRORS | 94 + KVASER_USB_HAS_SILENT_MODE }, 95 + { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_LIN_PRODUCT_ID), 96 + .driver_info = KVASER_USB_HAS_TXRX_ERRORS | 97 + KVASER_USB_HAS_SILENT_MODE }, 98 + { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_SPRO_LS_PRODUCT_ID), 99 + .driver_info = KVASER_USB_HAS_TXRX_ERRORS | 100 + KVASER_USB_HAS_SILENT_MODE }, 101 + { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_SPRO_SWC_PRODUCT_ID), 102 + .driver_info = KVASER_USB_HAS_TXRX_ERRORS | 103 + KVASER_USB_HAS_SILENT_MODE }, 104 + { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO2_DEVEL_PRODUCT_ID), 105 + .driver_info = KVASER_USB_HAS_TXRX_ERRORS | 106 + KVASER_USB_HAS_SILENT_MODE }, 107 + { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO2_HSHS_PRODUCT_ID), 108 + .driver_info = KVASER_USB_HAS_TXRX_ERRORS | 109 + KVASER_USB_HAS_SILENT_MODE }, 110 + { USB_DEVICE(KVASER_VENDOR_ID, USB_UPRO_HSHS_PRODUCT_ID), 111 + .driver_info = KVASER_USB_HAS_TXRX_ERRORS }, 112 + { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_GI_PRODUCT_ID) }, 113 + { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_OBDII_PRODUCT_ID), 114 + .driver_info = KVASER_USB_HAS_TXRX_ERRORS | 115 + KVASER_USB_HAS_SILENT_MODE }, 116 + { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO2_HSLS_PRODUCT_ID), 117 + .driver_info = KVASER_USB_HAS_TXRX_ERRORS }, 118 + { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_CH_PRODUCT_ID), 119 + .driver_info = KVASER_USB_HAS_TXRX_ERRORS }, 120 + { USB_DEVICE(KVASER_VENDOR_ID, USB_BLACKBIRD_SPRO_PRODUCT_ID), 121 + .driver_info = KVASER_USB_HAS_TXRX_ERRORS }, 122 + { USB_DEVICE(KVASER_VENDOR_ID, USB_OEM_MERCURY_PRODUCT_ID), 123 + .driver_info = KVASER_USB_HAS_TXRX_ERRORS }, 124 + { USB_DEVICE(KVASER_VENDOR_ID, USB_OEM_LEAF_PRODUCT_ID), 125 + .driver_info = KVASER_USB_HAS_TXRX_ERRORS }, 126 + { USB_DEVICE(KVASER_VENDOR_ID, USB_CAN_R_PRODUCT_ID), 127 + .driver_info = KVASER_USB_HAS_TXRX_ERRORS }, 128 + { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_V2_PRODUCT_ID) }, 129 + { USB_DEVICE(KVASER_VENDOR_ID, USB_MINI_PCIE_HS_PRODUCT_ID) }, 130 + { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LIGHT_HS_V2_OEM_PRODUCT_ID) }, 131 + { USB_DEVICE(KVASER_VENDOR_ID, USB_USBCAN_LIGHT_2HS_PRODUCT_ID) }, 132 + { USB_DEVICE(KVASER_VENDOR_ID, USB_MINI_PCIE_2HS_PRODUCT_ID) }, 133 + 134 + /* USBCANII USB product IDs */ 135 + { USB_DEVICE(KVASER_VENDOR_ID, USB_USBCAN2_PRODUCT_ID), 136 + .driver_info = KVASER_USB_HAS_TXRX_ERRORS }, 137 + { USB_DEVICE(KVASER_VENDOR_ID, USB_USBCAN_REVB_PRODUCT_ID), 138 + .driver_info = KVASER_USB_HAS_TXRX_ERRORS }, 139 + { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMORATOR_PRODUCT_ID), 140 + .driver_info = KVASER_USB_HAS_TXRX_ERRORS }, 141 + { USB_DEVICE(KVASER_VENDOR_ID, USB_VCI2_PRODUCT_ID), 142 + .driver_info = KVASER_USB_HAS_TXRX_ERRORS }, 143 + { } 144 + }; 145 + MODULE_DEVICE_TABLE(usb, kvaser_usb_table); 146 + 147 + int kvaser_usb_send_cmd(const struct kvaser_usb *dev, void *cmd, int len) 148 + { 149 + int actual_len; /* Not used */ 150 + 151 + return usb_bulk_msg(dev->udev, 152 + usb_sndbulkpipe(dev->udev, 153 + dev->bulk_out->bEndpointAddress), 154 + cmd, len, &actual_len, KVASER_USB_TIMEOUT); 155 + } 156 + 157 + int kvaser_usb_recv_cmd(const struct kvaser_usb *dev, void *cmd, int len, 158 + int *actual_len) 159 + { 160 + return usb_bulk_msg(dev->udev, 161 + usb_rcvbulkpipe(dev->udev, 162 + dev->bulk_in->bEndpointAddress), 163 + cmd, len, actual_len, KVASER_USB_TIMEOUT); 164 + } 165 + 166 + static void kvaser_usb_send_cmd_callback(struct urb *urb) 167 + { 168 + struct net_device *netdev = urb->context; 169 + 170 + kfree(urb->transfer_buffer); 171 + 172 + if (urb->status) 173 + netdev_warn(netdev, "urb status received: %d\n", urb->status); 174 + } 175 + 176 + int kvaser_usb_send_cmd_async(struct kvaser_usb_net_priv *priv, void *cmd, 177 + int len) 178 + { 179 + struct kvaser_usb *dev = priv->dev; 180 + struct net_device *netdev = priv->netdev; 181 + struct urb *urb; 182 + int err; 183 + 184 + urb = usb_alloc_urb(0, GFP_ATOMIC); 185 + if (!urb) 186 + return -ENOMEM; 187 + 188 + usb_fill_bulk_urb(urb, dev->udev, 189 + usb_sndbulkpipe(dev->udev, 190 + dev->bulk_out->bEndpointAddress), 191 + cmd, len, kvaser_usb_send_cmd_callback, netdev); 192 + usb_anchor_urb(urb, &priv->tx_submitted); 193 + 194 + err = usb_submit_urb(urb, GFP_ATOMIC); 195 + if (err) { 196 + netdev_err(netdev, "Error transmitting URB\n"); 197 + usb_unanchor_urb(urb); 198 + } 199 + usb_free_urb(urb); 200 + 201 + return 0; 202 + } 203 + 204 + int kvaser_usb_can_rx_over_error(struct net_device *netdev) 205 + { 206 + struct net_device_stats *stats = &netdev->stats; 207 + struct can_frame *cf; 208 + struct sk_buff *skb; 209 + 210 + stats->rx_over_errors++; 211 + stats->rx_errors++; 212 + 213 + skb = alloc_can_err_skb(netdev, &cf); 214 + if (!skb) { 215 + stats->rx_dropped++; 216 + netdev_warn(netdev, "No memory left for err_skb\n"); 217 + return -ENOMEM; 218 + } 219 + 220 + cf->can_id |= CAN_ERR_CRTL; 221 + cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW; 222 + 223 + stats->rx_packets++; 224 + stats->rx_bytes += cf->can_dlc; 225 + netif_rx(skb); 226 + 227 + return 0; 228 + } 229 + 230 + static void kvaser_usb_read_bulk_callback(struct urb *urb) 231 + { 232 + struct kvaser_usb *dev = urb->context; 233 + int err; 234 + unsigned int i; 235 + 236 + switch (urb->status) { 237 + case 0: 238 + break; 239 + case -ENOENT: 240 + case -EPIPE: 241 + case -EPROTO: 242 + case -ESHUTDOWN: 243 + return; 244 + default: 245 + dev_info(&dev->intf->dev, "Rx URB aborted (%d)\n", urb->status); 246 + goto resubmit_urb; 247 + } 248 + 249 + dev->ops->dev_read_bulk_callback(dev, urb->transfer_buffer, 250 + urb->actual_length); 251 + 252 + resubmit_urb: 253 + usb_fill_bulk_urb(urb, dev->udev, 254 + usb_rcvbulkpipe(dev->udev, 255 + dev->bulk_in->bEndpointAddress), 256 + urb->transfer_buffer, KVASER_USB_RX_BUFFER_SIZE, 257 + kvaser_usb_read_bulk_callback, dev); 258 + 259 + err = usb_submit_urb(urb, GFP_ATOMIC); 260 + if (err == -ENODEV) { 261 + for (i = 0; i < dev->nchannels; i++) { 262 + if (!dev->nets[i]) 263 + continue; 264 + 265 + netif_device_detach(dev->nets[i]->netdev); 266 + } 267 + } else if (err) { 268 + dev_err(&dev->intf->dev, 269 + "Failed resubmitting read bulk urb: %d\n", err); 270 + } 271 + } 272 + 273 + static int kvaser_usb_setup_rx_urbs(struct kvaser_usb *dev) 274 + { 275 + int i, err = 0; 276 + 277 + if (dev->rxinitdone) 278 + return 0; 279 + 280 + for (i = 0; i < KVASER_USB_MAX_RX_URBS; i++) { 281 + struct urb *urb = NULL; 282 + u8 *buf = NULL; 283 + dma_addr_t buf_dma; 284 + 285 + urb = usb_alloc_urb(0, GFP_KERNEL); 286 + if (!urb) { 287 + err = -ENOMEM; 288 + break; 289 + } 290 + 291 + buf = usb_alloc_coherent(dev->udev, KVASER_USB_RX_BUFFER_SIZE, 292 + GFP_KERNEL, &buf_dma); 293 + if (!buf) { 294 + dev_warn(&dev->intf->dev, 295 + "No memory left for USB buffer\n"); 296 + usb_free_urb(urb); 297 + err = -ENOMEM; 298 + break; 299 + } 300 + 301 + usb_fill_bulk_urb(urb, dev->udev, 302 + usb_rcvbulkpipe 303 + (dev->udev, 304 + dev->bulk_in->bEndpointAddress), 305 + buf, KVASER_USB_RX_BUFFER_SIZE, 306 + kvaser_usb_read_bulk_callback, dev); 307 + urb->transfer_dma = buf_dma; 308 + urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; 309 + usb_anchor_urb(urb, &dev->rx_submitted); 310 + 311 + err = usb_submit_urb(urb, GFP_KERNEL); 312 + if (err) { 313 + usb_unanchor_urb(urb); 314 + usb_free_coherent(dev->udev, 315 + KVASER_USB_RX_BUFFER_SIZE, buf, 316 + buf_dma); 317 + usb_free_urb(urb); 318 + break; 319 + } 320 + 321 + dev->rxbuf[i] = buf; 322 + dev->rxbuf_dma[i] = buf_dma; 323 + 324 + usb_free_urb(urb); 325 + } 326 + 327 + if (i == 0) { 328 + dev_warn(&dev->intf->dev, "Cannot setup read URBs, error %d\n", 329 + err); 330 + return err; 331 + } else if (i < KVASER_USB_MAX_RX_URBS) { 332 + dev_warn(&dev->intf->dev, "RX performances may be slow\n"); 333 + } 334 + 335 + dev->rxinitdone = true; 336 + 337 + return 0; 338 + } 339 + 340 + static int kvaser_usb_open(struct net_device *netdev) 341 + { 342 + struct kvaser_usb_net_priv *priv = netdev_priv(netdev); 343 + struct kvaser_usb *dev = priv->dev; 344 + int err; 345 + 346 + err = open_candev(netdev); 347 + if (err) 348 + return err; 349 + 350 + err = kvaser_usb_setup_rx_urbs(dev); 351 + if (err) 352 + goto error; 353 + 354 + err = dev->ops->dev_set_opt_mode(priv); 355 + if (err) 356 + goto error; 357 + 358 + err = dev->ops->dev_start_chip(priv); 359 + if (err) { 360 + netdev_warn(netdev, "Cannot start device, error %d\n", err); 361 + goto error; 362 + } 363 + 364 + priv->can.state = CAN_STATE_ERROR_ACTIVE; 365 + 366 + return 0; 367 + 368 + error: 369 + close_candev(netdev); 370 + return err; 371 + } 372 + 373 + static void kvaser_usb_reset_tx_urb_contexts(struct kvaser_usb_net_priv *priv) 374 + { 375 + int i, max_tx_urbs; 376 + 377 + max_tx_urbs = priv->dev->max_tx_urbs; 378 + 379 + priv->active_tx_contexts = 0; 380 + for (i = 0; i < max_tx_urbs; i++) 381 + priv->tx_contexts[i].echo_index = max_tx_urbs; 382 + } 383 + 384 + /* This method might sleep. Do not call it in the atomic context 385 + * of URB completions. 386 + */ 387 + static void kvaser_usb_unlink_tx_urbs(struct kvaser_usb_net_priv *priv) 388 + { 389 + usb_kill_anchored_urbs(&priv->tx_submitted); 390 + kvaser_usb_reset_tx_urb_contexts(priv); 391 + } 392 + 393 + static void kvaser_usb_unlink_all_urbs(struct kvaser_usb *dev) 394 + { 395 + int i; 396 + 397 + usb_kill_anchored_urbs(&dev->rx_submitted); 398 + 399 + for (i = 0; i < KVASER_USB_MAX_RX_URBS; i++) 400 + usb_free_coherent(dev->udev, KVASER_USB_RX_BUFFER_SIZE, 401 + dev->rxbuf[i], dev->rxbuf_dma[i]); 402 + 403 + for (i = 0; i < dev->nchannels; i++) { 404 + struct kvaser_usb_net_priv *priv = dev->nets[i]; 405 + 406 + if (priv) 407 + kvaser_usb_unlink_tx_urbs(priv); 408 + } 409 + } 410 + 411 + static int kvaser_usb_close(struct net_device *netdev) 412 + { 413 + struct kvaser_usb_net_priv *priv = netdev_priv(netdev); 414 + struct kvaser_usb *dev = priv->dev; 415 + int err; 416 + 417 + netif_stop_queue(netdev); 418 + 419 + err = dev->ops->dev_flush_queue(priv); 420 + if (err) 421 + netdev_warn(netdev, "Cannot flush queue, error %d\n", err); 422 + 423 + if (dev->ops->dev_reset_chip) { 424 + err = dev->ops->dev_reset_chip(dev, priv->channel); 425 + if (err) 426 + netdev_warn(netdev, "Cannot reset card, error %d\n", 427 + err); 428 + } 429 + 430 + err = dev->ops->dev_stop_chip(priv); 431 + if (err) 432 + netdev_warn(netdev, "Cannot stop device, error %d\n", err); 433 + 434 + /* reset tx contexts */ 435 + kvaser_usb_unlink_tx_urbs(priv); 436 + 437 + priv->can.state = CAN_STATE_STOPPED; 438 + close_candev(priv->netdev); 439 + 440 + return 0; 441 + } 442 + 443 + static void kvaser_usb_write_bulk_callback(struct urb *urb) 444 + { 445 + struct kvaser_usb_tx_urb_context *context = urb->context; 446 + struct kvaser_usb_net_priv *priv; 447 + struct net_device *netdev; 448 + 449 + if (WARN_ON(!context)) 450 + return; 451 + 452 + priv = context->priv; 453 + netdev = priv->netdev; 454 + 455 + kfree(urb->transfer_buffer); 456 + 457 + if (!netif_device_present(netdev)) 458 + return; 459 + 460 + if (urb->status) 461 + netdev_info(netdev, "Tx URB aborted (%d)\n", urb->status); 462 + } 463 + 464 + static netdev_tx_t kvaser_usb_start_xmit(struct sk_buff *skb, 465 + struct net_device *netdev) 466 + { 467 + struct kvaser_usb_net_priv *priv = netdev_priv(netdev); 468 + struct kvaser_usb *dev = priv->dev; 469 + struct net_device_stats *stats = &netdev->stats; 470 + struct kvaser_usb_tx_urb_context *context = NULL; 471 + struct urb *urb; 472 + void *buf; 473 + int cmd_len = 0; 474 + int err, ret = NETDEV_TX_OK; 475 + unsigned int i; 476 + unsigned long flags; 477 + 478 + if (can_dropped_invalid_skb(netdev, skb)) 479 + return NETDEV_TX_OK; 480 + 481 + urb = usb_alloc_urb(0, GFP_ATOMIC); 482 + if (!urb) { 483 + stats->tx_dropped++; 484 + dev_kfree_skb(skb); 485 + return NETDEV_TX_OK; 486 + } 487 + 488 + spin_lock_irqsave(&priv->tx_contexts_lock, flags); 489 + for (i = 0; i < dev->max_tx_urbs; i++) { 490 + if (priv->tx_contexts[i].echo_index == dev->max_tx_urbs) { 491 + context = &priv->tx_contexts[i]; 492 + 493 + context->echo_index = i; 494 + can_put_echo_skb(skb, netdev, context->echo_index); 495 + ++priv->active_tx_contexts; 496 + if (priv->active_tx_contexts >= (int)dev->max_tx_urbs) 497 + netif_stop_queue(netdev); 498 + 499 + break; 500 + } 501 + } 502 + spin_unlock_irqrestore(&priv->tx_contexts_lock, flags); 503 + 504 + /* This should never happen; it implies a flow control bug */ 505 + if (!context) { 506 + netdev_warn(netdev, "cannot find free context\n"); 507 + 508 + ret = NETDEV_TX_BUSY; 509 + goto freeurb; 510 + } 511 + 512 + buf = dev->ops->dev_frame_to_cmd(priv, skb, &context->dlc, &cmd_len, 513 + context->echo_index); 514 + if (!buf) { 515 + stats->tx_dropped++; 516 + dev_kfree_skb(skb); 517 + spin_lock_irqsave(&priv->tx_contexts_lock, flags); 518 + 519 + can_free_echo_skb(netdev, context->echo_index); 520 + context->echo_index = dev->max_tx_urbs; 521 + --priv->active_tx_contexts; 522 + netif_wake_queue(netdev); 523 + 524 + spin_unlock_irqrestore(&priv->tx_contexts_lock, flags); 525 + goto freeurb; 526 + } 527 + 528 + context->priv = priv; 529 + 530 + usb_fill_bulk_urb(urb, dev->udev, 531 + usb_sndbulkpipe(dev->udev, 532 + dev->bulk_out->bEndpointAddress), 533 + buf, cmd_len, kvaser_usb_write_bulk_callback, 534 + context); 535 + usb_anchor_urb(urb, &priv->tx_submitted); 536 + 537 + err = usb_submit_urb(urb, GFP_ATOMIC); 538 + if (unlikely(err)) { 539 + spin_lock_irqsave(&priv->tx_contexts_lock, flags); 540 + 541 + can_free_echo_skb(netdev, context->echo_index); 542 + context->echo_index = dev->max_tx_urbs; 543 + --priv->active_tx_contexts; 544 + netif_wake_queue(netdev); 545 + 546 + spin_unlock_irqrestore(&priv->tx_contexts_lock, flags); 547 + 548 + usb_unanchor_urb(urb); 549 + kfree(buf); 550 + 551 + stats->tx_dropped++; 552 + 553 + if (err == -ENODEV) 554 + netif_device_detach(netdev); 555 + else 556 + netdev_warn(netdev, "Failed tx_urb %d\n", err); 557 + 558 + goto freeurb; 559 + } 560 + 561 + ret = NETDEV_TX_OK; 562 + 563 + freeurb: 564 + usb_free_urb(urb); 565 + return ret; 566 + } 567 + 568 + static const struct net_device_ops kvaser_usb_netdev_ops = { 569 + .ndo_open = kvaser_usb_open, 570 + .ndo_stop = kvaser_usb_close, 571 + .ndo_start_xmit = kvaser_usb_start_xmit, 572 + .ndo_change_mtu = can_change_mtu, 573 + }; 574 + 575 + static void kvaser_usb_remove_interfaces(struct kvaser_usb *dev) 576 + { 577 + int i; 578 + 579 + for (i = 0; i < dev->nchannels; i++) { 580 + if (!dev->nets[i]) 581 + continue; 582 + 583 + unregister_candev(dev->nets[i]->netdev); 584 + } 585 + 586 + kvaser_usb_unlink_all_urbs(dev); 587 + 588 + for (i = 0; i < dev->nchannels; i++) { 589 + if (!dev->nets[i]) 590 + continue; 591 + 592 + free_candev(dev->nets[i]->netdev); 593 + } 594 + } 595 + 596 + static int kvaser_usb_init_one(struct kvaser_usb *dev, 597 + const struct usb_device_id *id, int channel) 598 + { 599 + struct net_device *netdev; 600 + struct kvaser_usb_net_priv *priv; 601 + int err; 602 + 603 + if (dev->ops->dev_reset_chip) { 604 + err = dev->ops->dev_reset_chip(dev, channel); 605 + if (err) 606 + return err; 607 + } 608 + 609 + netdev = alloc_candev(sizeof(*priv) + 610 + dev->max_tx_urbs * sizeof(*priv->tx_contexts), 611 + dev->max_tx_urbs); 612 + if (!netdev) { 613 + dev_err(&dev->intf->dev, "Cannot alloc candev\n"); 614 + return -ENOMEM; 615 + } 616 + 617 + priv = netdev_priv(netdev); 618 + 619 + init_usb_anchor(&priv->tx_submitted); 620 + init_completion(&priv->start_comp); 621 + init_completion(&priv->stop_comp); 622 + priv->can.ctrlmode_supported = 0; 623 + 624 + priv->dev = dev; 625 + priv->netdev = netdev; 626 + priv->channel = channel; 627 + 628 + spin_lock_init(&priv->tx_contexts_lock); 629 + kvaser_usb_reset_tx_urb_contexts(priv); 630 + 631 + priv->can.state = CAN_STATE_STOPPED; 632 + priv->can.clock.freq = dev->cfg->clock.freq; 633 + priv->can.bittiming_const = dev->cfg->bittiming_const; 634 + priv->can.do_set_bittiming = dev->ops->dev_set_bittiming; 635 + priv->can.do_set_mode = dev->ops->dev_set_mode; 636 + if (id->driver_info & KVASER_USB_HAS_TXRX_ERRORS) 637 + priv->can.do_get_berr_counter = dev->ops->dev_get_berr_counter; 638 + if (id->driver_info & KVASER_USB_HAS_SILENT_MODE) 639 + priv->can.ctrlmode_supported |= CAN_CTRLMODE_LISTENONLY; 640 + 641 + priv->can.ctrlmode_supported |= dev->card_data.ctrlmode_supported; 642 + 643 + netdev->flags |= IFF_ECHO; 644 + 645 + netdev->netdev_ops = &kvaser_usb_netdev_ops; 646 + 647 + SET_NETDEV_DEV(netdev, &dev->intf->dev); 648 + netdev->dev_id = channel; 649 + 650 + dev->nets[channel] = priv; 651 + 652 + err = register_candev(netdev); 653 + if (err) { 654 + dev_err(&dev->intf->dev, "Failed to register CAN device\n"); 655 + free_candev(netdev); 656 + dev->nets[channel] = NULL; 657 + return err; 658 + } 659 + 660 + netdev_dbg(netdev, "device registered\n"); 661 + 662 + return 0; 663 + } 664 + 665 + static int kvaser_usb_probe(struct usb_interface *intf, 666 + const struct usb_device_id *id) 667 + { 668 + struct kvaser_usb *dev; 669 + int err; 670 + int i; 671 + 672 + dev = devm_kzalloc(&intf->dev, sizeof(*dev), GFP_KERNEL); 673 + if (!dev) 674 + return -ENOMEM; 675 + 676 + if (kvaser_is_leaf(id)) { 677 + dev->card_data.leaf.family = KVASER_LEAF; 678 + dev->ops = &kvaser_usb_leaf_dev_ops; 679 + } else if (kvaser_is_usbcan(id)) { 680 + dev->card_data.leaf.family = KVASER_USBCAN; 681 + dev->ops = &kvaser_usb_leaf_dev_ops; 682 + } else { 683 + dev_err(&intf->dev, 684 + "Product ID (%d) is not a supported Kvaser USB device\n", 685 + id->idProduct); 686 + return -ENODEV; 687 + } 688 + 689 + dev->intf = intf; 690 + 691 + err = dev->ops->dev_setup_endpoints(dev); 692 + if (err) { 693 + dev_err(&intf->dev, "Cannot get usb endpoint(s)"); 694 + return err; 695 + } 696 + 697 + dev->udev = interface_to_usbdev(intf); 698 + 699 + init_usb_anchor(&dev->rx_submitted); 700 + 701 + usb_set_intfdata(intf, dev); 702 + 703 + dev->card_data.ctrlmode_supported = 0; 704 + err = dev->ops->dev_init_card(dev); 705 + if (err) { 706 + dev_err(&intf->dev, 707 + "Failed to initialize card, error %d\n", err); 708 + return err; 709 + } 710 + 711 + err = dev->ops->dev_get_software_info(dev); 712 + if (err) { 713 + dev_err(&intf->dev, 714 + "Cannot get software info, error %d\n", err); 715 + return err; 716 + } 717 + 718 + if (WARN_ON(!dev->cfg)) 719 + return -ENODEV; 720 + 721 + dev_dbg(&intf->dev, "Firmware version: %d.%d.%d\n", 722 + ((dev->fw_version >> 24) & 0xff), 723 + ((dev->fw_version >> 16) & 0xff), 724 + (dev->fw_version & 0xffff)); 725 + 726 + dev_dbg(&intf->dev, "Max outstanding tx = %d URBs\n", dev->max_tx_urbs); 727 + 728 + err = dev->ops->dev_get_card_info(dev); 729 + if (err) { 730 + dev_err(&intf->dev, "Cannot get card info, error %d\n", err); 731 + return err; 732 + } 733 + 734 + for (i = 0; i < dev->nchannels; i++) { 735 + err = kvaser_usb_init_one(dev, id, i); 736 + if (err) { 737 + kvaser_usb_remove_interfaces(dev); 738 + return err; 739 + } 740 + } 741 + 742 + return 0; 743 + } 744 + 745 + static void kvaser_usb_disconnect(struct usb_interface *intf) 746 + { 747 + struct kvaser_usb *dev = usb_get_intfdata(intf); 748 + 749 + usb_set_intfdata(intf, NULL); 750 + 751 + if (!dev) 752 + return; 753 + 754 + kvaser_usb_remove_interfaces(dev); 755 + } 756 + 757 + static struct usb_driver kvaser_usb_driver = { 758 + .name = "kvaser_usb", 759 + .probe = kvaser_usb_probe, 760 + .disconnect = kvaser_usb_disconnect, 761 + .id_table = kvaser_usb_table, 762 + }; 763 + 764 + module_usb_driver(kvaser_usb_driver); 765 + 766 + MODULE_AUTHOR("Olivier Sobrie <olivier@sobrie.be>"); 767 + MODULE_AUTHOR("Kvaser AB <support@kvaser.com>"); 768 + MODULE_DESCRIPTION("CAN driver for Kvaser CAN/USB devices"); 769 + MODULE_LICENSE("GPL v2");
+1363
drivers/net/can/usb/kvaser_usb/kvaser_usb_leaf.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + /* Parts of this driver are based on the following: 3 + * - Kvaser linux leaf driver (version 4.78) 4 + * - CAN driver for esd CAN-USB/2 5 + * - Kvaser linux usbcanII driver (version 5.3) 6 + * 7 + * Copyright (C) 2002-2018 KVASER AB, Sweden. All rights reserved. 8 + * Copyright (C) 2010 Matthias Fuchs <matthias.fuchs@esd.eu>, esd gmbh 9 + * Copyright (C) 2012 Olivier Sobrie <olivier@sobrie.be> 10 + * Copyright (C) 2015 Valeo S.A. 11 + */ 12 + 13 + #include <linux/completion.h> 14 + #include <linux/device.h> 15 + #include <linux/gfp.h> 16 + #include <linux/jiffies.h> 17 + #include <linux/kernel.h> 18 + #include <linux/netdevice.h> 19 + #include <linux/spinlock.h> 20 + #include <linux/string.h> 21 + #include <linux/types.h> 22 + #include <linux/usb.h> 23 + 24 + #include <linux/can.h> 25 + #include <linux/can/dev.h> 26 + #include <linux/can/error.h> 27 + #include <linux/can/netlink.h> 28 + 29 + #include "kvaser_usb.h" 30 + 31 + /* Forward declaration */ 32 + static const struct kvaser_usb_dev_cfg kvaser_usb_leaf_dev_cfg; 33 + 34 + #define CAN_USB_CLOCK 8000000 35 + #define MAX_USBCAN_NET_DEVICES 2 36 + 37 + /* Command header size */ 38 + #define CMD_HEADER_LEN 2 39 + 40 + /* Kvaser CAN message flags */ 41 + #define MSG_FLAG_ERROR_FRAME BIT(0) 42 + #define MSG_FLAG_OVERRUN BIT(1) 43 + #define MSG_FLAG_NERR BIT(2) 44 + #define MSG_FLAG_WAKEUP BIT(3) 45 + #define MSG_FLAG_REMOTE_FRAME BIT(4) 46 + #define MSG_FLAG_RESERVED BIT(5) 47 + #define MSG_FLAG_TX_ACK BIT(6) 48 + #define MSG_FLAG_TX_REQUEST BIT(7) 49 + 50 + /* CAN states (M16C CxSTRH register) */ 51 + #define M16C_STATE_BUS_RESET BIT(0) 52 + #define M16C_STATE_BUS_ERROR BIT(4) 53 + #define M16C_STATE_BUS_PASSIVE BIT(5) 54 + #define M16C_STATE_BUS_OFF BIT(6) 55 + 56 + /* Leaf/usbcan command ids */ 57 + #define CMD_RX_STD_MESSAGE 12 58 + #define CMD_TX_STD_MESSAGE 13 59 + #define CMD_RX_EXT_MESSAGE 14 60 + #define CMD_TX_EXT_MESSAGE 15 61 + #define CMD_SET_BUS_PARAMS 16 62 + #define CMD_CHIP_STATE_EVENT 20 63 + #define CMD_SET_CTRL_MODE 21 64 + #define CMD_RESET_CHIP 24 65 + #define CMD_START_CHIP 26 66 + #define CMD_START_CHIP_REPLY 27 67 + #define CMD_STOP_CHIP 28 68 + #define CMD_STOP_CHIP_REPLY 29 69 + 70 + #define CMD_USBCAN_CLOCK_OVERFLOW_EVENT 33 71 + 72 + #define CMD_GET_CARD_INFO 34 73 + #define CMD_GET_CARD_INFO_REPLY 35 74 + #define CMD_GET_SOFTWARE_INFO 38 75 + #define CMD_GET_SOFTWARE_INFO_REPLY 39 76 + #define CMD_FLUSH_QUEUE 48 77 + #define CMD_TX_ACKNOWLEDGE 50 78 + #define CMD_CAN_ERROR_EVENT 51 79 + #define CMD_FLUSH_QUEUE_REPLY 68 80 + 81 + #define CMD_LEAF_LOG_MESSAGE 106 82 + 83 + /* error factors */ 84 + #define M16C_EF_ACKE BIT(0) 85 + #define M16C_EF_CRCE BIT(1) 86 + #define M16C_EF_FORME BIT(2) 87 + #define M16C_EF_STFE BIT(3) 88 + #define M16C_EF_BITE0 BIT(4) 89 + #define M16C_EF_BITE1 BIT(5) 90 + #define M16C_EF_RCVE BIT(6) 91 + #define M16C_EF_TRE BIT(7) 92 + 93 + /* Only Leaf-based devices can report M16C error factors, 94 + * thus define our own error status flags for USBCANII 95 + */ 96 + #define USBCAN_ERROR_STATE_NONE 0 97 + #define USBCAN_ERROR_STATE_TX_ERROR BIT(0) 98 + #define USBCAN_ERROR_STATE_RX_ERROR BIT(1) 99 + #define USBCAN_ERROR_STATE_BUSERROR BIT(2) 100 + 101 + /* bittiming parameters */ 102 + #define KVASER_USB_TSEG1_MIN 1 103 + #define KVASER_USB_TSEG1_MAX 16 104 + #define KVASER_USB_TSEG2_MIN 1 105 + #define KVASER_USB_TSEG2_MAX 8 106 + #define KVASER_USB_SJW_MAX 4 107 + #define KVASER_USB_BRP_MIN 1 108 + #define KVASER_USB_BRP_MAX 64 109 + #define KVASER_USB_BRP_INC 1 110 + 111 + /* ctrl modes */ 112 + #define KVASER_CTRL_MODE_NORMAL 1 113 + #define KVASER_CTRL_MODE_SILENT 2 114 + #define KVASER_CTRL_MODE_SELFRECEPTION 3 115 + #define KVASER_CTRL_MODE_OFF 4 116 + 117 + /* Extended CAN identifier flag */ 118 + #define KVASER_EXTENDED_FRAME BIT(31) 119 + 120 + struct kvaser_cmd_simple { 121 + u8 tid; 122 + u8 channel; 123 + } __packed; 124 + 125 + struct kvaser_cmd_cardinfo { 126 + u8 tid; 127 + u8 nchannels; 128 + union { 129 + struct { 130 + __le32 serial_number; 131 + __le32 padding; 132 + } __packed leaf0; 133 + struct { 134 + __le32 serial_number_low; 135 + __le32 serial_number_high; 136 + } __packed usbcan0; 137 + } __packed; 138 + __le32 clock_resolution; 139 + __le32 mfgdate; 140 + u8 ean[8]; 141 + u8 hw_revision; 142 + union { 143 + struct { 144 + u8 usb_hs_mode; 145 + } __packed leaf1; 146 + struct { 147 + u8 padding; 148 + } __packed usbcan1; 149 + } __packed; 150 + __le16 padding; 151 + } __packed; 152 + 153 + struct leaf_cmd_softinfo { 154 + u8 tid; 155 + u8 padding0; 156 + __le32 sw_options; 157 + __le32 fw_version; 158 + __le16 max_outstanding_tx; 159 + __le16 padding1[9]; 160 + } __packed; 161 + 162 + struct usbcan_cmd_softinfo { 163 + u8 tid; 164 + u8 fw_name[5]; 165 + __le16 max_outstanding_tx; 166 + u8 padding[6]; 167 + __le32 fw_version; 168 + __le16 checksum; 169 + __le16 sw_options; 170 + } __packed; 171 + 172 + struct kvaser_cmd_busparams { 173 + u8 tid; 174 + u8 channel; 175 + __le32 bitrate; 176 + u8 tseg1; 177 + u8 tseg2; 178 + u8 sjw; 179 + u8 no_samp; 180 + } __packed; 181 + 182 + struct kvaser_cmd_tx_can { 183 + u8 channel; 184 + u8 tid; 185 + u8 data[14]; 186 + union { 187 + struct { 188 + u8 padding; 189 + u8 flags; 190 + } __packed leaf; 191 + struct { 192 + u8 flags; 193 + u8 padding; 194 + } __packed usbcan; 195 + } __packed; 196 + } __packed; 197 + 198 + struct kvaser_cmd_rx_can_header { 199 + u8 channel; 200 + u8 flag; 201 + } __packed; 202 + 203 + struct leaf_cmd_rx_can { 204 + u8 channel; 205 + u8 flag; 206 + 207 + __le16 time[3]; 208 + u8 data[14]; 209 + } __packed; 210 + 211 + struct usbcan_cmd_rx_can { 212 + u8 channel; 213 + u8 flag; 214 + 215 + u8 data[14]; 216 + __le16 time; 217 + } __packed; 218 + 219 + struct leaf_cmd_chip_state_event { 220 + u8 tid; 221 + u8 channel; 222 + 223 + __le16 time[3]; 224 + u8 tx_errors_count; 225 + u8 rx_errors_count; 226 + 227 + u8 status; 228 + u8 padding[3]; 229 + } __packed; 230 + 231 + struct usbcan_cmd_chip_state_event { 232 + u8 tid; 233 + u8 channel; 234 + 235 + u8 tx_errors_count; 236 + u8 rx_errors_count; 237 + __le16 time; 238 + 239 + u8 status; 240 + u8 padding[3]; 241 + } __packed; 242 + 243 + struct kvaser_cmd_tx_acknowledge_header { 244 + u8 channel; 245 + u8 tid; 246 + } __packed; 247 + 248 + struct leaf_cmd_error_event { 249 + u8 tid; 250 + u8 flags; 251 + __le16 time[3]; 252 + u8 channel; 253 + u8 padding; 254 + u8 tx_errors_count; 255 + u8 rx_errors_count; 256 + u8 status; 257 + u8 error_factor; 258 + } __packed; 259 + 260 + struct usbcan_cmd_error_event { 261 + u8 tid; 262 + u8 padding; 263 + u8 tx_errors_count_ch0; 264 + u8 rx_errors_count_ch0; 265 + u8 tx_errors_count_ch1; 266 + u8 rx_errors_count_ch1; 267 + u8 status_ch0; 268 + u8 status_ch1; 269 + __le16 time; 270 + } __packed; 271 + 272 + struct kvaser_cmd_ctrl_mode { 273 + u8 tid; 274 + u8 channel; 275 + u8 ctrl_mode; 276 + u8 padding[3]; 277 + } __packed; 278 + 279 + struct kvaser_cmd_flush_queue { 280 + u8 tid; 281 + u8 channel; 282 + u8 flags; 283 + u8 padding[3]; 284 + } __packed; 285 + 286 + struct leaf_cmd_log_message { 287 + u8 channel; 288 + u8 flags; 289 + __le16 time[3]; 290 + u8 dlc; 291 + u8 time_offset; 292 + __le32 id; 293 + u8 data[8]; 294 + } __packed; 295 + 296 + struct kvaser_cmd { 297 + u8 len; 298 + u8 id; 299 + union { 300 + struct kvaser_cmd_simple simple; 301 + struct kvaser_cmd_cardinfo cardinfo; 302 + struct kvaser_cmd_busparams busparams; 303 + 304 + struct kvaser_cmd_rx_can_header rx_can_header; 305 + struct kvaser_cmd_tx_acknowledge_header tx_acknowledge_header; 306 + 307 + union { 308 + struct leaf_cmd_softinfo softinfo; 309 + struct leaf_cmd_rx_can rx_can; 310 + struct leaf_cmd_chip_state_event chip_state_event; 311 + struct leaf_cmd_error_event error_event; 312 + struct leaf_cmd_log_message log_message; 313 + } __packed leaf; 314 + 315 + union { 316 + struct usbcan_cmd_softinfo softinfo; 317 + struct usbcan_cmd_rx_can rx_can; 318 + struct usbcan_cmd_chip_state_event chip_state_event; 319 + struct usbcan_cmd_error_event error_event; 320 + } __packed usbcan; 321 + 322 + struct kvaser_cmd_tx_can tx_can; 323 + struct kvaser_cmd_ctrl_mode ctrl_mode; 324 + struct kvaser_cmd_flush_queue flush_queue; 325 + } u; 326 + } __packed; 327 + 328 + /* Summary of a kvaser error event, for a unified Leaf/Usbcan error 329 + * handling. Some discrepancies between the two families exist: 330 + * 331 + * - USBCAN firmware does not report M16C "error factors" 332 + * - USBCAN controllers has difficulties reporting if the raised error 333 + * event is for ch0 or ch1. They leave such arbitration to the OS 334 + * driver by letting it compare error counters with previous values 335 + * and decide the error event's channel. Thus for USBCAN, the channel 336 + * field is only advisory. 337 + */ 338 + struct kvaser_usb_err_summary { 339 + u8 channel, status, txerr, rxerr; 340 + union { 341 + struct { 342 + u8 error_factor; 343 + } leaf; 344 + struct { 345 + u8 other_ch_status; 346 + u8 error_state; 347 + } usbcan; 348 + }; 349 + }; 350 + 351 + static void * 352 + kvaser_usb_leaf_frame_to_cmd(const struct kvaser_usb_net_priv *priv, 353 + const struct sk_buff *skb, int *frame_len, 354 + int *cmd_len, u16 transid) 355 + { 356 + struct kvaser_usb *dev = priv->dev; 357 + struct kvaser_cmd *cmd; 358 + u8 *cmd_tx_can_flags = NULL; /* GCC */ 359 + struct can_frame *cf = (struct can_frame *)skb->data; 360 + 361 + *frame_len = cf->can_dlc; 362 + 363 + cmd = kmalloc(sizeof(*cmd), GFP_ATOMIC); 364 + if (cmd) { 365 + cmd->u.tx_can.tid = transid & 0xff; 366 + cmd->len = *cmd_len = CMD_HEADER_LEN + 367 + sizeof(struct kvaser_cmd_tx_can); 368 + cmd->u.tx_can.channel = priv->channel; 369 + 370 + switch (dev->card_data.leaf.family) { 371 + case KVASER_LEAF: 372 + cmd_tx_can_flags = &cmd->u.tx_can.leaf.flags; 373 + break; 374 + case KVASER_USBCAN: 375 + cmd_tx_can_flags = &cmd->u.tx_can.usbcan.flags; 376 + break; 377 + } 378 + 379 + *cmd_tx_can_flags = 0; 380 + 381 + if (cf->can_id & CAN_EFF_FLAG) { 382 + cmd->id = CMD_TX_EXT_MESSAGE; 383 + cmd->u.tx_can.data[0] = (cf->can_id >> 24) & 0x1f; 384 + cmd->u.tx_can.data[1] = (cf->can_id >> 18) & 0x3f; 385 + cmd->u.tx_can.data[2] = (cf->can_id >> 14) & 0x0f; 386 + cmd->u.tx_can.data[3] = (cf->can_id >> 6) & 0xff; 387 + cmd->u.tx_can.data[4] = cf->can_id & 0x3f; 388 + } else { 389 + cmd->id = CMD_TX_STD_MESSAGE; 390 + cmd->u.tx_can.data[0] = (cf->can_id >> 6) & 0x1f; 391 + cmd->u.tx_can.data[1] = cf->can_id & 0x3f; 392 + } 393 + 394 + cmd->u.tx_can.data[5] = cf->can_dlc; 395 + memcpy(&cmd->u.tx_can.data[6], cf->data, cf->can_dlc); 396 + 397 + if (cf->can_id & CAN_RTR_FLAG) 398 + *cmd_tx_can_flags |= MSG_FLAG_REMOTE_FRAME; 399 + } 400 + return cmd; 401 + } 402 + 403 + static int kvaser_usb_leaf_wait_cmd(const struct kvaser_usb *dev, u8 id, 404 + struct kvaser_cmd *cmd) 405 + { 406 + struct kvaser_cmd *tmp; 407 + void *buf; 408 + int actual_len; 409 + int err; 410 + int pos; 411 + unsigned long to = jiffies + msecs_to_jiffies(KVASER_USB_TIMEOUT); 412 + 413 + buf = kzalloc(KVASER_USB_RX_BUFFER_SIZE, GFP_KERNEL); 414 + if (!buf) 415 + return -ENOMEM; 416 + 417 + do { 418 + err = kvaser_usb_recv_cmd(dev, buf, KVASER_USB_RX_BUFFER_SIZE, 419 + &actual_len); 420 + if (err < 0) 421 + goto end; 422 + 423 + pos = 0; 424 + while (pos <= actual_len - CMD_HEADER_LEN) { 425 + tmp = buf + pos; 426 + 427 + /* Handle commands crossing the USB endpoint max packet 428 + * size boundary. Check kvaser_usb_read_bulk_callback() 429 + * for further details. 430 + */ 431 + if (tmp->len == 0) { 432 + pos = round_up(pos, 433 + le16_to_cpu 434 + (dev->bulk_in->wMaxPacketSize)); 435 + continue; 436 + } 437 + 438 + if (pos + tmp->len > actual_len) { 439 + dev_err_ratelimited(&dev->intf->dev, 440 + "Format error\n"); 441 + break; 442 + } 443 + 444 + if (tmp->id == id) { 445 + memcpy(cmd, tmp, tmp->len); 446 + goto end; 447 + } 448 + 449 + pos += tmp->len; 450 + } 451 + } while (time_before(jiffies, to)); 452 + 453 + err = -EINVAL; 454 + 455 + end: 456 + kfree(buf); 457 + 458 + return err; 459 + } 460 + 461 + static int kvaser_usb_leaf_send_simple_cmd(const struct kvaser_usb *dev, 462 + u8 cmd_id, int channel) 463 + { 464 + struct kvaser_cmd *cmd; 465 + int rc; 466 + 467 + cmd = kmalloc(sizeof(*cmd), GFP_KERNEL); 468 + if (!cmd) 469 + return -ENOMEM; 470 + 471 + cmd->id = cmd_id; 472 + cmd->len = CMD_HEADER_LEN + sizeof(struct kvaser_cmd_simple); 473 + cmd->u.simple.channel = channel; 474 + cmd->u.simple.tid = 0xff; 475 + 476 + rc = kvaser_usb_send_cmd(dev, cmd, cmd->len); 477 + 478 + kfree(cmd); 479 + return rc; 480 + } 481 + 482 + static int kvaser_usb_leaf_get_software_info_inner(struct kvaser_usb *dev) 483 + { 484 + struct kvaser_cmd cmd; 485 + int err; 486 + 487 + err = kvaser_usb_leaf_send_simple_cmd(dev, CMD_GET_SOFTWARE_INFO, 0); 488 + if (err) 489 + return err; 490 + 491 + err = kvaser_usb_leaf_wait_cmd(dev, CMD_GET_SOFTWARE_INFO_REPLY, &cmd); 492 + if (err) 493 + return err; 494 + 495 + switch (dev->card_data.leaf.family) { 496 + case KVASER_LEAF: 497 + dev->fw_version = le32_to_cpu(cmd.u.leaf.softinfo.fw_version); 498 + dev->max_tx_urbs = 499 + le16_to_cpu(cmd.u.leaf.softinfo.max_outstanding_tx); 500 + break; 501 + case KVASER_USBCAN: 502 + dev->fw_version = le32_to_cpu(cmd.u.usbcan.softinfo.fw_version); 503 + dev->max_tx_urbs = 504 + le16_to_cpu(cmd.u.usbcan.softinfo.max_outstanding_tx); 505 + break; 506 + } 507 + 508 + return 0; 509 + } 510 + 511 + static int kvaser_usb_leaf_get_software_info(struct kvaser_usb *dev) 512 + { 513 + int err; 514 + int retry = 3; 515 + 516 + /* On some x86 laptops, plugging a Kvaser device again after 517 + * an unplug makes the firmware always ignore the very first 518 + * command. For such a case, provide some room for retries 519 + * instead of completely exiting the driver. 520 + */ 521 + do { 522 + err = kvaser_usb_leaf_get_software_info_inner(dev); 523 + } while (--retry && err == -ETIMEDOUT); 524 + 525 + return err; 526 + } 527 + 528 + static int kvaser_usb_leaf_get_card_info(struct kvaser_usb *dev) 529 + { 530 + struct kvaser_cmd cmd; 531 + int err; 532 + 533 + err = kvaser_usb_leaf_send_simple_cmd(dev, CMD_GET_CARD_INFO, 0); 534 + if (err) 535 + return err; 536 + 537 + err = kvaser_usb_leaf_wait_cmd(dev, CMD_GET_CARD_INFO_REPLY, &cmd); 538 + if (err) 539 + return err; 540 + 541 + dev->nchannels = cmd.u.cardinfo.nchannels; 542 + if (dev->nchannels > KVASER_USB_MAX_NET_DEVICES || 543 + (dev->card_data.leaf.family == KVASER_USBCAN && 544 + dev->nchannels > MAX_USBCAN_NET_DEVICES)) 545 + return -EINVAL; 546 + 547 + return 0; 548 + } 549 + 550 + static void kvaser_usb_leaf_tx_acknowledge(const struct kvaser_usb *dev, 551 + const struct kvaser_cmd *cmd) 552 + { 553 + struct net_device_stats *stats; 554 + struct kvaser_usb_tx_urb_context *context; 555 + struct kvaser_usb_net_priv *priv; 556 + unsigned long flags; 557 + u8 channel, tid; 558 + 559 + channel = cmd->u.tx_acknowledge_header.channel; 560 + tid = cmd->u.tx_acknowledge_header.tid; 561 + 562 + if (channel >= dev->nchannels) { 563 + dev_err(&dev->intf->dev, 564 + "Invalid channel number (%d)\n", channel); 565 + return; 566 + } 567 + 568 + priv = dev->nets[channel]; 569 + 570 + if (!netif_device_present(priv->netdev)) 571 + return; 572 + 573 + stats = &priv->netdev->stats; 574 + 575 + context = &priv->tx_contexts[tid % dev->max_tx_urbs]; 576 + 577 + /* Sometimes the state change doesn't come after a bus-off event */ 578 + if (priv->can.restart_ms && priv->can.state >= CAN_STATE_BUS_OFF) { 579 + struct sk_buff *skb; 580 + struct can_frame *cf; 581 + 582 + skb = alloc_can_err_skb(priv->netdev, &cf); 583 + if (skb) { 584 + cf->can_id |= CAN_ERR_RESTARTED; 585 + 586 + stats->rx_packets++; 587 + stats->rx_bytes += cf->can_dlc; 588 + netif_rx(skb); 589 + } else { 590 + netdev_err(priv->netdev, 591 + "No memory left for err_skb\n"); 592 + } 593 + 594 + priv->can.can_stats.restarts++; 595 + netif_carrier_on(priv->netdev); 596 + 597 + priv->can.state = CAN_STATE_ERROR_ACTIVE; 598 + } 599 + 600 + stats->tx_packets++; 601 + stats->tx_bytes += context->dlc; 602 + 603 + spin_lock_irqsave(&priv->tx_contexts_lock, flags); 604 + 605 + can_get_echo_skb(priv->netdev, context->echo_index); 606 + context->echo_index = dev->max_tx_urbs; 607 + --priv->active_tx_contexts; 608 + netif_wake_queue(priv->netdev); 609 + 610 + spin_unlock_irqrestore(&priv->tx_contexts_lock, flags); 611 + } 612 + 613 + static int kvaser_usb_leaf_simple_cmd_async(struct kvaser_usb_net_priv *priv, 614 + u8 cmd_id) 615 + { 616 + struct kvaser_cmd *cmd; 617 + int err; 618 + 619 + cmd = kmalloc(sizeof(*cmd), GFP_ATOMIC); 620 + if (!cmd) 621 + return -ENOMEM; 622 + 623 + cmd->len = CMD_HEADER_LEN + sizeof(struct kvaser_cmd_simple); 624 + cmd->id = cmd_id; 625 + cmd->u.simple.channel = priv->channel; 626 + 627 + err = kvaser_usb_send_cmd_async(priv, cmd, cmd->len); 628 + if (err) 629 + kfree(cmd); 630 + 631 + return err; 632 + } 633 + 634 + static void 635 + kvaser_usb_leaf_rx_error_update_can_state(struct kvaser_usb_net_priv *priv, 636 + const struct kvaser_usb_err_summary *es, 637 + struct can_frame *cf) 638 + { 639 + struct kvaser_usb *dev = priv->dev; 640 + struct net_device_stats *stats = &priv->netdev->stats; 641 + enum can_state cur_state, new_state, tx_state, rx_state; 642 + 643 + netdev_dbg(priv->netdev, "Error status: 0x%02x\n", es->status); 644 + 645 + new_state = priv->can.state; 646 + cur_state = priv->can.state; 647 + 648 + if (es->status & (M16C_STATE_BUS_OFF | M16C_STATE_BUS_RESET)) { 649 + new_state = CAN_STATE_BUS_OFF; 650 + } else if (es->status & M16C_STATE_BUS_PASSIVE) { 651 + new_state = CAN_STATE_ERROR_PASSIVE; 652 + } else if (es->status & M16C_STATE_BUS_ERROR) { 653 + /* Guard against spurious error events after a busoff */ 654 + if (cur_state < CAN_STATE_BUS_OFF) { 655 + if (es->txerr >= 128 || es->rxerr >= 128) 656 + new_state = CAN_STATE_ERROR_PASSIVE; 657 + else if (es->txerr >= 96 || es->rxerr >= 96) 658 + new_state = CAN_STATE_ERROR_WARNING; 659 + else if (cur_state > CAN_STATE_ERROR_ACTIVE) 660 + new_state = CAN_STATE_ERROR_ACTIVE; 661 + } 662 + } 663 + 664 + if (!es->status) 665 + new_state = CAN_STATE_ERROR_ACTIVE; 666 + 667 + if (new_state != cur_state) { 668 + tx_state = (es->txerr >= es->rxerr) ? new_state : 0; 669 + rx_state = (es->txerr <= es->rxerr) ? new_state : 0; 670 + 671 + can_change_state(priv->netdev, cf, tx_state, rx_state); 672 + } 673 + 674 + if (priv->can.restart_ms && 675 + cur_state >= CAN_STATE_BUS_OFF && 676 + new_state < CAN_STATE_BUS_OFF) 677 + priv->can.can_stats.restarts++; 678 + 679 + switch (dev->card_data.leaf.family) { 680 + case KVASER_LEAF: 681 + if (es->leaf.error_factor) { 682 + priv->can.can_stats.bus_error++; 683 + stats->rx_errors++; 684 + } 685 + break; 686 + case KVASER_USBCAN: 687 + if (es->usbcan.error_state & USBCAN_ERROR_STATE_TX_ERROR) 688 + stats->tx_errors++; 689 + if (es->usbcan.error_state & USBCAN_ERROR_STATE_RX_ERROR) 690 + stats->rx_errors++; 691 + if (es->usbcan.error_state & USBCAN_ERROR_STATE_BUSERROR) 692 + priv->can.can_stats.bus_error++; 693 + break; 694 + } 695 + 696 + priv->bec.txerr = es->txerr; 697 + priv->bec.rxerr = es->rxerr; 698 + } 699 + 700 + static void kvaser_usb_leaf_rx_error(const struct kvaser_usb *dev, 701 + const struct kvaser_usb_err_summary *es) 702 + { 703 + struct can_frame *cf; 704 + struct can_frame tmp_cf = { .can_id = CAN_ERR_FLAG, 705 + .can_dlc = CAN_ERR_DLC }; 706 + struct sk_buff *skb; 707 + struct net_device_stats *stats; 708 + struct kvaser_usb_net_priv *priv; 709 + enum can_state old_state, new_state; 710 + 711 + if (es->channel >= dev->nchannels) { 712 + dev_err(&dev->intf->dev, 713 + "Invalid channel number (%d)\n", es->channel); 714 + return; 715 + } 716 + 717 + priv = dev->nets[es->channel]; 718 + stats = &priv->netdev->stats; 719 + 720 + /* Update all of the CAN interface's state and error counters before 721 + * trying any memory allocation that can actually fail with -ENOMEM. 722 + * 723 + * We send a temporary stack-allocated error CAN frame to 724 + * can_change_state() for the very same reason. 725 + * 726 + * TODO: Split can_change_state() responsibility between updating the 727 + * CAN interface's state and counters, and the setting up of CAN error 728 + * frame ID and data to userspace. Remove stack allocation afterwards. 729 + */ 730 + old_state = priv->can.state; 731 + kvaser_usb_leaf_rx_error_update_can_state(priv, es, &tmp_cf); 732 + new_state = priv->can.state; 733 + 734 + skb = alloc_can_err_skb(priv->netdev, &cf); 735 + if (!skb) { 736 + stats->rx_dropped++; 737 + return; 738 + } 739 + memcpy(cf, &tmp_cf, sizeof(*cf)); 740 + 741 + if (new_state != old_state) { 742 + if (es->status & 743 + (M16C_STATE_BUS_OFF | M16C_STATE_BUS_RESET)) { 744 + if (!priv->can.restart_ms) 745 + kvaser_usb_leaf_simple_cmd_async(priv, 746 + CMD_STOP_CHIP); 747 + netif_carrier_off(priv->netdev); 748 + } 749 + 750 + if (priv->can.restart_ms && 751 + old_state >= CAN_STATE_BUS_OFF && 752 + new_state < CAN_STATE_BUS_OFF) { 753 + cf->can_id |= CAN_ERR_RESTARTED; 754 + netif_carrier_on(priv->netdev); 755 + } 756 + } 757 + 758 + switch (dev->card_data.leaf.family) { 759 + case KVASER_LEAF: 760 + if (es->leaf.error_factor) { 761 + cf->can_id |= CAN_ERR_BUSERROR | CAN_ERR_PROT; 762 + 763 + if (es->leaf.error_factor & M16C_EF_ACKE) 764 + cf->data[3] = CAN_ERR_PROT_LOC_ACK; 765 + if (es->leaf.error_factor & M16C_EF_CRCE) 766 + cf->data[3] = CAN_ERR_PROT_LOC_CRC_SEQ; 767 + if (es->leaf.error_factor & M16C_EF_FORME) 768 + cf->data[2] |= CAN_ERR_PROT_FORM; 769 + if (es->leaf.error_factor & M16C_EF_STFE) 770 + cf->data[2] |= CAN_ERR_PROT_STUFF; 771 + if (es->leaf.error_factor & M16C_EF_BITE0) 772 + cf->data[2] |= CAN_ERR_PROT_BIT0; 773 + if (es->leaf.error_factor & M16C_EF_BITE1) 774 + cf->data[2] |= CAN_ERR_PROT_BIT1; 775 + if (es->leaf.error_factor & M16C_EF_TRE) 776 + cf->data[2] |= CAN_ERR_PROT_TX; 777 + } 778 + break; 779 + case KVASER_USBCAN: 780 + if (es->usbcan.error_state & USBCAN_ERROR_STATE_BUSERROR) 781 + cf->can_id |= CAN_ERR_BUSERROR; 782 + break; 783 + } 784 + 785 + cf->data[6] = es->txerr; 786 + cf->data[7] = es->rxerr; 787 + 788 + stats->rx_packets++; 789 + stats->rx_bytes += cf->can_dlc; 790 + netif_rx(skb); 791 + } 792 + 793 + /* For USBCAN, report error to userspace if the channels's errors counter 794 + * has changed, or we're the only channel seeing a bus error state. 795 + */ 796 + static void 797 + kvaser_usb_leaf_usbcan_conditionally_rx_error(const struct kvaser_usb *dev, 798 + struct kvaser_usb_err_summary *es) 799 + { 800 + struct kvaser_usb_net_priv *priv; 801 + unsigned int channel; 802 + bool report_error; 803 + 804 + channel = es->channel; 805 + if (channel >= dev->nchannels) { 806 + dev_err(&dev->intf->dev, 807 + "Invalid channel number (%d)\n", channel); 808 + return; 809 + } 810 + 811 + priv = dev->nets[channel]; 812 + report_error = false; 813 + 814 + if (es->txerr != priv->bec.txerr) { 815 + es->usbcan.error_state |= USBCAN_ERROR_STATE_TX_ERROR; 816 + report_error = true; 817 + } 818 + if (es->rxerr != priv->bec.rxerr) { 819 + es->usbcan.error_state |= USBCAN_ERROR_STATE_RX_ERROR; 820 + report_error = true; 821 + } 822 + if ((es->status & M16C_STATE_BUS_ERROR) && 823 + !(es->usbcan.other_ch_status & M16C_STATE_BUS_ERROR)) { 824 + es->usbcan.error_state |= USBCAN_ERROR_STATE_BUSERROR; 825 + report_error = true; 826 + } 827 + 828 + if (report_error) 829 + kvaser_usb_leaf_rx_error(dev, es); 830 + } 831 + 832 + static void kvaser_usb_leaf_usbcan_rx_error(const struct kvaser_usb *dev, 833 + const struct kvaser_cmd *cmd) 834 + { 835 + struct kvaser_usb_err_summary es = { }; 836 + 837 + switch (cmd->id) { 838 + /* Sometimes errors are sent as unsolicited chip state events */ 839 + case CMD_CHIP_STATE_EVENT: 840 + es.channel = cmd->u.usbcan.chip_state_event.channel; 841 + es.status = cmd->u.usbcan.chip_state_event.status; 842 + es.txerr = cmd->u.usbcan.chip_state_event.tx_errors_count; 843 + es.rxerr = cmd->u.usbcan.chip_state_event.rx_errors_count; 844 + kvaser_usb_leaf_usbcan_conditionally_rx_error(dev, &es); 845 + break; 846 + 847 + case CMD_CAN_ERROR_EVENT: 848 + es.channel = 0; 849 + es.status = cmd->u.usbcan.error_event.status_ch0; 850 + es.txerr = cmd->u.usbcan.error_event.tx_errors_count_ch0; 851 + es.rxerr = cmd->u.usbcan.error_event.rx_errors_count_ch0; 852 + es.usbcan.other_ch_status = 853 + cmd->u.usbcan.error_event.status_ch1; 854 + kvaser_usb_leaf_usbcan_conditionally_rx_error(dev, &es); 855 + 856 + /* The USBCAN firmware supports up to 2 channels. 857 + * Now that ch0 was checked, check if ch1 has any errors. 858 + */ 859 + if (dev->nchannels == MAX_USBCAN_NET_DEVICES) { 860 + es.channel = 1; 861 + es.status = cmd->u.usbcan.error_event.status_ch1; 862 + es.txerr = 863 + cmd->u.usbcan.error_event.tx_errors_count_ch1; 864 + es.rxerr = 865 + cmd->u.usbcan.error_event.rx_errors_count_ch1; 866 + es.usbcan.other_ch_status = 867 + cmd->u.usbcan.error_event.status_ch0; 868 + kvaser_usb_leaf_usbcan_conditionally_rx_error(dev, &es); 869 + } 870 + break; 871 + 872 + default: 873 + dev_err(&dev->intf->dev, "Invalid cmd id (%d)\n", cmd->id); 874 + } 875 + } 876 + 877 + static void kvaser_usb_leaf_leaf_rx_error(const struct kvaser_usb *dev, 878 + const struct kvaser_cmd *cmd) 879 + { 880 + struct kvaser_usb_err_summary es = { }; 881 + 882 + switch (cmd->id) { 883 + case CMD_CAN_ERROR_EVENT: 884 + es.channel = cmd->u.leaf.error_event.channel; 885 + es.status = cmd->u.leaf.error_event.status; 886 + es.txerr = cmd->u.leaf.error_event.tx_errors_count; 887 + es.rxerr = cmd->u.leaf.error_event.rx_errors_count; 888 + es.leaf.error_factor = cmd->u.leaf.error_event.error_factor; 889 + break; 890 + case CMD_LEAF_LOG_MESSAGE: 891 + es.channel = cmd->u.leaf.log_message.channel; 892 + es.status = cmd->u.leaf.log_message.data[0]; 893 + es.txerr = cmd->u.leaf.log_message.data[2]; 894 + es.rxerr = cmd->u.leaf.log_message.data[3]; 895 + es.leaf.error_factor = cmd->u.leaf.log_message.data[1]; 896 + break; 897 + case CMD_CHIP_STATE_EVENT: 898 + es.channel = cmd->u.leaf.chip_state_event.channel; 899 + es.status = cmd->u.leaf.chip_state_event.status; 900 + es.txerr = cmd->u.leaf.chip_state_event.tx_errors_count; 901 + es.rxerr = cmd->u.leaf.chip_state_event.rx_errors_count; 902 + es.leaf.error_factor = 0; 903 + break; 904 + default: 905 + dev_err(&dev->intf->dev, "Invalid cmd id (%d)\n", cmd->id); 906 + return; 907 + } 908 + 909 + kvaser_usb_leaf_rx_error(dev, &es); 910 + } 911 + 912 + static void kvaser_usb_leaf_rx_can_err(const struct kvaser_usb_net_priv *priv, 913 + const struct kvaser_cmd *cmd) 914 + { 915 + if (cmd->u.rx_can_header.flag & (MSG_FLAG_ERROR_FRAME | 916 + MSG_FLAG_NERR)) { 917 + struct net_device_stats *stats = &priv->netdev->stats; 918 + 919 + netdev_err(priv->netdev, "Unknown error (flags: 0x%02x)\n", 920 + cmd->u.rx_can_header.flag); 921 + 922 + stats->rx_errors++; 923 + return; 924 + } 925 + 926 + if (cmd->u.rx_can_header.flag & MSG_FLAG_OVERRUN) 927 + kvaser_usb_can_rx_over_error(priv->netdev); 928 + } 929 + 930 + static void kvaser_usb_leaf_rx_can_msg(const struct kvaser_usb *dev, 931 + const struct kvaser_cmd *cmd) 932 + { 933 + struct kvaser_usb_net_priv *priv; 934 + struct can_frame *cf; 935 + struct sk_buff *skb; 936 + struct net_device_stats *stats; 937 + u8 channel = cmd->u.rx_can_header.channel; 938 + const u8 *rx_data = NULL; /* GCC */ 939 + 940 + if (channel >= dev->nchannels) { 941 + dev_err(&dev->intf->dev, 942 + "Invalid channel number (%d)\n", channel); 943 + return; 944 + } 945 + 946 + priv = dev->nets[channel]; 947 + stats = &priv->netdev->stats; 948 + 949 + if ((cmd->u.rx_can_header.flag & MSG_FLAG_ERROR_FRAME) && 950 + (dev->card_data.leaf.family == KVASER_LEAF && 951 + cmd->id == CMD_LEAF_LOG_MESSAGE)) { 952 + kvaser_usb_leaf_leaf_rx_error(dev, cmd); 953 + return; 954 + } else if (cmd->u.rx_can_header.flag & (MSG_FLAG_ERROR_FRAME | 955 + MSG_FLAG_NERR | 956 + MSG_FLAG_OVERRUN)) { 957 + kvaser_usb_leaf_rx_can_err(priv, cmd); 958 + return; 959 + } else if (cmd->u.rx_can_header.flag & ~MSG_FLAG_REMOTE_FRAME) { 960 + netdev_warn(priv->netdev, 961 + "Unhandled frame (flags: 0x%02x)\n", 962 + cmd->u.rx_can_header.flag); 963 + return; 964 + } 965 + 966 + switch (dev->card_data.leaf.family) { 967 + case KVASER_LEAF: 968 + rx_data = cmd->u.leaf.rx_can.data; 969 + break; 970 + case KVASER_USBCAN: 971 + rx_data = cmd->u.usbcan.rx_can.data; 972 + break; 973 + } 974 + 975 + skb = alloc_can_skb(priv->netdev, &cf); 976 + if (!skb) { 977 + stats->rx_dropped++; 978 + return; 979 + } 980 + 981 + if (dev->card_data.leaf.family == KVASER_LEAF && cmd->id == 982 + CMD_LEAF_LOG_MESSAGE) { 983 + cf->can_id = le32_to_cpu(cmd->u.leaf.log_message.id); 984 + if (cf->can_id & KVASER_EXTENDED_FRAME) 985 + cf->can_id &= CAN_EFF_MASK | CAN_EFF_FLAG; 986 + else 987 + cf->can_id &= CAN_SFF_MASK; 988 + 989 + cf->can_dlc = get_can_dlc(cmd->u.leaf.log_message.dlc); 990 + 991 + if (cmd->u.leaf.log_message.flags & MSG_FLAG_REMOTE_FRAME) 992 + cf->can_id |= CAN_RTR_FLAG; 993 + else 994 + memcpy(cf->data, &cmd->u.leaf.log_message.data, 995 + cf->can_dlc); 996 + } else { 997 + cf->can_id = ((rx_data[0] & 0x1f) << 6) | (rx_data[1] & 0x3f); 998 + 999 + if (cmd->id == CMD_RX_EXT_MESSAGE) { 1000 + cf->can_id <<= 18; 1001 + cf->can_id |= ((rx_data[2] & 0x0f) << 14) | 1002 + ((rx_data[3] & 0xff) << 6) | 1003 + (rx_data[4] & 0x3f); 1004 + cf->can_id |= CAN_EFF_FLAG; 1005 + } 1006 + 1007 + cf->can_dlc = get_can_dlc(rx_data[5]); 1008 + 1009 + if (cmd->u.rx_can_header.flag & MSG_FLAG_REMOTE_FRAME) 1010 + cf->can_id |= CAN_RTR_FLAG; 1011 + else 1012 + memcpy(cf->data, &rx_data[6], cf->can_dlc); 1013 + } 1014 + 1015 + stats->rx_packets++; 1016 + stats->rx_bytes += cf->can_dlc; 1017 + netif_rx(skb); 1018 + } 1019 + 1020 + static void kvaser_usb_leaf_start_chip_reply(const struct kvaser_usb *dev, 1021 + const struct kvaser_cmd *cmd) 1022 + { 1023 + struct kvaser_usb_net_priv *priv; 1024 + u8 channel = cmd->u.simple.channel; 1025 + 1026 + if (channel >= dev->nchannels) { 1027 + dev_err(&dev->intf->dev, 1028 + "Invalid channel number (%d)\n", channel); 1029 + return; 1030 + } 1031 + 1032 + priv = dev->nets[channel]; 1033 + 1034 + if (completion_done(&priv->start_comp) && 1035 + netif_queue_stopped(priv->netdev)) { 1036 + netif_wake_queue(priv->netdev); 1037 + } else { 1038 + netif_start_queue(priv->netdev); 1039 + complete(&priv->start_comp); 1040 + } 1041 + } 1042 + 1043 + static void kvaser_usb_leaf_stop_chip_reply(const struct kvaser_usb *dev, 1044 + const struct kvaser_cmd *cmd) 1045 + { 1046 + struct kvaser_usb_net_priv *priv; 1047 + u8 channel = cmd->u.simple.channel; 1048 + 1049 + if (channel >= dev->nchannels) { 1050 + dev_err(&dev->intf->dev, 1051 + "Invalid channel number (%d)\n", channel); 1052 + return; 1053 + } 1054 + 1055 + priv = dev->nets[channel]; 1056 + 1057 + complete(&priv->stop_comp); 1058 + } 1059 + 1060 + static void kvaser_usb_leaf_handle_command(const struct kvaser_usb *dev, 1061 + const struct kvaser_cmd *cmd) 1062 + { 1063 + switch (cmd->id) { 1064 + case CMD_START_CHIP_REPLY: 1065 + kvaser_usb_leaf_start_chip_reply(dev, cmd); 1066 + break; 1067 + 1068 + case CMD_STOP_CHIP_REPLY: 1069 + kvaser_usb_leaf_stop_chip_reply(dev, cmd); 1070 + break; 1071 + 1072 + case CMD_RX_STD_MESSAGE: 1073 + case CMD_RX_EXT_MESSAGE: 1074 + kvaser_usb_leaf_rx_can_msg(dev, cmd); 1075 + break; 1076 + 1077 + case CMD_LEAF_LOG_MESSAGE: 1078 + if (dev->card_data.leaf.family != KVASER_LEAF) 1079 + goto warn; 1080 + kvaser_usb_leaf_rx_can_msg(dev, cmd); 1081 + break; 1082 + 1083 + case CMD_CHIP_STATE_EVENT: 1084 + case CMD_CAN_ERROR_EVENT: 1085 + if (dev->card_data.leaf.family == KVASER_LEAF) 1086 + kvaser_usb_leaf_leaf_rx_error(dev, cmd); 1087 + else 1088 + kvaser_usb_leaf_usbcan_rx_error(dev, cmd); 1089 + break; 1090 + 1091 + case CMD_TX_ACKNOWLEDGE: 1092 + kvaser_usb_leaf_tx_acknowledge(dev, cmd); 1093 + break; 1094 + 1095 + /* Ignored commands */ 1096 + case CMD_USBCAN_CLOCK_OVERFLOW_EVENT: 1097 + if (dev->card_data.leaf.family != KVASER_USBCAN) 1098 + goto warn; 1099 + break; 1100 + 1101 + case CMD_FLUSH_QUEUE_REPLY: 1102 + if (dev->card_data.leaf.family != KVASER_LEAF) 1103 + goto warn; 1104 + break; 1105 + 1106 + default: 1107 + warn: dev_warn(&dev->intf->dev, "Unhandled command (%d)\n", cmd->id); 1108 + break; 1109 + } 1110 + } 1111 + 1112 + static void kvaser_usb_leaf_read_bulk_callback(struct kvaser_usb *dev, 1113 + void *buf, int len) 1114 + { 1115 + struct kvaser_cmd *cmd; 1116 + int pos = 0; 1117 + 1118 + while (pos <= len - CMD_HEADER_LEN) { 1119 + cmd = buf + pos; 1120 + 1121 + /* The Kvaser firmware can only read and write commands that 1122 + * does not cross the USB's endpoint wMaxPacketSize boundary. 1123 + * If a follow-up command crosses such boundary, firmware puts 1124 + * a placeholder zero-length command in its place then aligns 1125 + * the real command to the next max packet size. 1126 + * 1127 + * Handle such cases or we're going to miss a significant 1128 + * number of events in case of a heavy rx load on the bus. 1129 + */ 1130 + if (cmd->len == 0) { 1131 + pos = round_up(pos, le16_to_cpu 1132 + (dev->bulk_in->wMaxPacketSize)); 1133 + continue; 1134 + } 1135 + 1136 + if (pos + cmd->len > len) { 1137 + dev_err_ratelimited(&dev->intf->dev, "Format error\n"); 1138 + break; 1139 + } 1140 + 1141 + kvaser_usb_leaf_handle_command(dev, cmd); 1142 + pos += cmd->len; 1143 + } 1144 + } 1145 + 1146 + static int kvaser_usb_leaf_set_opt_mode(const struct kvaser_usb_net_priv *priv) 1147 + { 1148 + struct kvaser_cmd *cmd; 1149 + int rc; 1150 + 1151 + cmd = kmalloc(sizeof(*cmd), GFP_KERNEL); 1152 + if (!cmd) 1153 + return -ENOMEM; 1154 + 1155 + cmd->id = CMD_SET_CTRL_MODE; 1156 + cmd->len = CMD_HEADER_LEN + sizeof(struct kvaser_cmd_ctrl_mode); 1157 + cmd->u.ctrl_mode.tid = 0xff; 1158 + cmd->u.ctrl_mode.channel = priv->channel; 1159 + 1160 + if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY) 1161 + cmd->u.ctrl_mode.ctrl_mode = KVASER_CTRL_MODE_SILENT; 1162 + else 1163 + cmd->u.ctrl_mode.ctrl_mode = KVASER_CTRL_MODE_NORMAL; 1164 + 1165 + rc = kvaser_usb_send_cmd(priv->dev, cmd, cmd->len); 1166 + 1167 + kfree(cmd); 1168 + return rc; 1169 + } 1170 + 1171 + static int kvaser_usb_leaf_start_chip(struct kvaser_usb_net_priv *priv) 1172 + { 1173 + int err; 1174 + 1175 + init_completion(&priv->start_comp); 1176 + 1177 + err = kvaser_usb_leaf_send_simple_cmd(priv->dev, CMD_START_CHIP, 1178 + priv->channel); 1179 + if (err) 1180 + return err; 1181 + 1182 + if (!wait_for_completion_timeout(&priv->start_comp, 1183 + msecs_to_jiffies(KVASER_USB_TIMEOUT))) 1184 + return -ETIMEDOUT; 1185 + 1186 + return 0; 1187 + } 1188 + 1189 + static int kvaser_usb_leaf_stop_chip(struct kvaser_usb_net_priv *priv) 1190 + { 1191 + int err; 1192 + 1193 + init_completion(&priv->stop_comp); 1194 + 1195 + err = kvaser_usb_leaf_send_simple_cmd(priv->dev, CMD_STOP_CHIP, 1196 + priv->channel); 1197 + if (err) 1198 + return err; 1199 + 1200 + if (!wait_for_completion_timeout(&priv->stop_comp, 1201 + msecs_to_jiffies(KVASER_USB_TIMEOUT))) 1202 + return -ETIMEDOUT; 1203 + 1204 + return 0; 1205 + } 1206 + 1207 + static int kvaser_usb_leaf_reset_chip(struct kvaser_usb *dev, int channel) 1208 + { 1209 + return kvaser_usb_leaf_send_simple_cmd(dev, CMD_RESET_CHIP, channel); 1210 + } 1211 + 1212 + static int kvaser_usb_leaf_flush_queue(struct kvaser_usb_net_priv *priv) 1213 + { 1214 + struct kvaser_cmd *cmd; 1215 + int rc; 1216 + 1217 + cmd = kmalloc(sizeof(*cmd), GFP_KERNEL); 1218 + if (!cmd) 1219 + return -ENOMEM; 1220 + 1221 + cmd->id = CMD_FLUSH_QUEUE; 1222 + cmd->len = CMD_HEADER_LEN + sizeof(struct kvaser_cmd_flush_queue); 1223 + cmd->u.flush_queue.channel = priv->channel; 1224 + cmd->u.flush_queue.flags = 0x00; 1225 + 1226 + rc = kvaser_usb_send_cmd(priv->dev, cmd, cmd->len); 1227 + 1228 + kfree(cmd); 1229 + return rc; 1230 + } 1231 + 1232 + static int kvaser_usb_leaf_init_card(struct kvaser_usb *dev) 1233 + { 1234 + struct kvaser_usb_dev_card_data *card_data = &dev->card_data; 1235 + 1236 + dev->cfg = &kvaser_usb_leaf_dev_cfg; 1237 + card_data->ctrlmode_supported |= CAN_CTRLMODE_3_SAMPLES; 1238 + 1239 + return 0; 1240 + } 1241 + 1242 + static const struct can_bittiming_const kvaser_usb_leaf_bittiming_const = { 1243 + .name = "kvaser_usb", 1244 + .tseg1_min = KVASER_USB_TSEG1_MIN, 1245 + .tseg1_max = KVASER_USB_TSEG1_MAX, 1246 + .tseg2_min = KVASER_USB_TSEG2_MIN, 1247 + .tseg2_max = KVASER_USB_TSEG2_MAX, 1248 + .sjw_max = KVASER_USB_SJW_MAX, 1249 + .brp_min = KVASER_USB_BRP_MIN, 1250 + .brp_max = KVASER_USB_BRP_MAX, 1251 + .brp_inc = KVASER_USB_BRP_INC, 1252 + }; 1253 + 1254 + static int kvaser_usb_leaf_set_bittiming(struct net_device *netdev) 1255 + { 1256 + struct kvaser_usb_net_priv *priv = netdev_priv(netdev); 1257 + struct can_bittiming *bt = &priv->can.bittiming; 1258 + struct kvaser_usb *dev = priv->dev; 1259 + struct kvaser_cmd *cmd; 1260 + int rc; 1261 + 1262 + cmd = kmalloc(sizeof(*cmd), GFP_KERNEL); 1263 + if (!cmd) 1264 + return -ENOMEM; 1265 + 1266 + cmd->id = CMD_SET_BUS_PARAMS; 1267 + cmd->len = CMD_HEADER_LEN + sizeof(struct kvaser_cmd_busparams); 1268 + cmd->u.busparams.channel = priv->channel; 1269 + cmd->u.busparams.tid = 0xff; 1270 + cmd->u.busparams.bitrate = cpu_to_le32(bt->bitrate); 1271 + cmd->u.busparams.sjw = bt->sjw; 1272 + cmd->u.busparams.tseg1 = bt->prop_seg + bt->phase_seg1; 1273 + cmd->u.busparams.tseg2 = bt->phase_seg2; 1274 + 1275 + if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES) 1276 + cmd->u.busparams.no_samp = 3; 1277 + else 1278 + cmd->u.busparams.no_samp = 1; 1279 + 1280 + rc = kvaser_usb_send_cmd(dev, cmd, cmd->len); 1281 + 1282 + kfree(cmd); 1283 + return rc; 1284 + } 1285 + 1286 + static int kvaser_usb_leaf_set_mode(struct net_device *netdev, 1287 + enum can_mode mode) 1288 + { 1289 + struct kvaser_usb_net_priv *priv = netdev_priv(netdev); 1290 + int err; 1291 + 1292 + switch (mode) { 1293 + case CAN_MODE_START: 1294 + err = kvaser_usb_leaf_simple_cmd_async(priv, CMD_START_CHIP); 1295 + if (err) 1296 + return err; 1297 + break; 1298 + default: 1299 + return -EOPNOTSUPP; 1300 + } 1301 + 1302 + return 0; 1303 + } 1304 + 1305 + static int kvaser_usb_leaf_get_berr_counter(const struct net_device *netdev, 1306 + struct can_berr_counter *bec) 1307 + { 1308 + struct kvaser_usb_net_priv *priv = netdev_priv(netdev); 1309 + 1310 + *bec = priv->bec; 1311 + 1312 + return 0; 1313 + } 1314 + 1315 + static int kvaser_usb_leaf_setup_endpoints(struct kvaser_usb *dev) 1316 + { 1317 + const struct usb_host_interface *iface_desc; 1318 + struct usb_endpoint_descriptor *endpoint; 1319 + int i; 1320 + 1321 + iface_desc = &dev->intf->altsetting[0]; 1322 + 1323 + for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) { 1324 + endpoint = &iface_desc->endpoint[i].desc; 1325 + 1326 + if (!dev->bulk_in && usb_endpoint_is_bulk_in(endpoint)) 1327 + dev->bulk_in = endpoint; 1328 + 1329 + if (!dev->bulk_out && usb_endpoint_is_bulk_out(endpoint)) 1330 + dev->bulk_out = endpoint; 1331 + 1332 + /* use first bulk endpoint for in and out */ 1333 + if (dev->bulk_in && dev->bulk_out) 1334 + return 0; 1335 + } 1336 + 1337 + return -ENODEV; 1338 + } 1339 + 1340 + const struct kvaser_usb_dev_ops kvaser_usb_leaf_dev_ops = { 1341 + .dev_set_mode = kvaser_usb_leaf_set_mode, 1342 + .dev_set_bittiming = kvaser_usb_leaf_set_bittiming, 1343 + .dev_get_berr_counter = kvaser_usb_leaf_get_berr_counter, 1344 + .dev_setup_endpoints = kvaser_usb_leaf_setup_endpoints, 1345 + .dev_init_card = kvaser_usb_leaf_init_card, 1346 + .dev_get_software_info = kvaser_usb_leaf_get_software_info, 1347 + .dev_get_card_info = kvaser_usb_leaf_get_card_info, 1348 + .dev_set_opt_mode = kvaser_usb_leaf_set_opt_mode, 1349 + .dev_start_chip = kvaser_usb_leaf_start_chip, 1350 + .dev_stop_chip = kvaser_usb_leaf_stop_chip, 1351 + .dev_reset_chip = kvaser_usb_leaf_reset_chip, 1352 + .dev_flush_queue = kvaser_usb_leaf_flush_queue, 1353 + .dev_read_bulk_callback = kvaser_usb_leaf_read_bulk_callback, 1354 + .dev_frame_to_cmd = kvaser_usb_leaf_frame_to_cmd, 1355 + }; 1356 + 1357 + static const struct kvaser_usb_dev_cfg kvaser_usb_leaf_dev_cfg = { 1358 + .clock = { 1359 + .freq = CAN_USB_CLOCK, 1360 + }, 1361 + .timestamp_freq = 1, 1362 + .bittiming_const = &kvaser_usb_leaf_bittiming_const, 1363 + };