1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * CAN driver for esd electronics gmbh CAN-USB/2, CAN-USB/3 and CAN-USB/Micro 4 * 5 * Copyright (C) 2010-2012 esd electronic system design gmbh, Matthias Fuchs <socketcan@esd.eu> 6 * Copyright (C) 2022-2024 esd electronics gmbh, Frank Jungclaus <frank.jungclaus@esd.eu> 7 */ 8 9 #include <linux/can.h> 10 #include <linux/can/dev.h> 11 #include <linux/can/error.h> 12 #include <linux/ethtool.h> 13 #include <linux/module.h> 14 #include <linux/netdevice.h> 15 #include <linux/signal.h> 16 #include <linux/slab.h> 17 #include <linux/units.h> 18 #include <linux/usb.h> 19 20 MODULE_AUTHOR("Matthias Fuchs <socketcan@esd.eu>"); 21 MODULE_AUTHOR("Frank Jungclaus <frank.jungclaus@esd.eu>"); 22 MODULE_DESCRIPTION("CAN driver for esd electronics gmbh CAN-USB/2, CAN-USB/3 and CAN-USB/Micro interfaces"); 23 MODULE_LICENSE("GPL v2"); 24 25 /* USB vendor and product ID */ 26 #define ESD_USB_ESDGMBH_VENDOR_ID 0x0ab4 27 #define ESD_USB_CANUSB2_PRODUCT_ID 0x0010 28 #define ESD_USB_CANUSBM_PRODUCT_ID 0x0011 29 #define ESD_USB_CANUSB3_PRODUCT_ID 0x0014 30 31 /* CAN controller clock frequencies */ 32 #define ESD_USB_2_CAN_CLOCK (60 * MEGA) /* Hz */ 33 #define ESD_USB_M_CAN_CLOCK (36 * MEGA) /* Hz */ 34 #define ESD_USB_3_CAN_CLOCK (80 * MEGA) /* Hz */ 35 36 /* Maximum number of CAN nets */ 37 #define ESD_USB_MAX_NETS 2 38 39 /* USB commands */ 40 #define ESD_USB_CMD_VERSION 1 /* also used for VERSION_REPLY */ 41 #define ESD_USB_CMD_CAN_RX 2 /* device to host only */ 42 #define ESD_USB_CMD_CAN_TX 3 /* also used for TX_DONE */ 43 #define ESD_USB_CMD_SETBAUD 4 /* also used for SETBAUD_REPLY */ 44 #define ESD_USB_CMD_TS 5 /* also used for TS_REPLY */ 45 #define ESD_USB_CMD_IDADD 6 /* also used for IDADD_REPLY */ 46 47 /* esd CAN message flags - dlc field */ 48 #define ESD_USB_RTR BIT(4) 49 #define ESD_USB_NO_BRS BIT(4) 50 #define ESD_USB_ESI BIT(5) 51 #define ESD_USB_FD BIT(7) 52 53 /* esd CAN message flags - id field */ 54 #define ESD_USB_EXTID BIT(29) 55 #define ESD_USB_EVENT BIT(30) 56 #define ESD_USB_IDMASK GENMASK(28, 0) 57 58 /* esd CAN event ids */ 59 #define ESD_USB_EV_CAN_ERROR_EXT 2 /* CAN controller specific diagnostic data */ 60 61 /* baudrate message flags */ 62 #define ESD_USB_LOM BIT(30) /* Listen Only Mode */ 63 #define ESD_USB_UBR BIT(31) /* User Bit Rate (controller BTR) in bits 0..27 */ 64 #define ESD_USB_NO_BAUDRATE GENMASK(30, 0) /* bit rate unconfigured */ 65 66 /* bit timing esd CAN-USB */ 67 #define ESD_USB_2_TSEG1_SHIFT 16 68 #define ESD_USB_2_TSEG2_SHIFT 20 69 #define ESD_USB_2_SJW_SHIFT 14 70 #define ESD_USB_M_SJW_SHIFT 24 71 #define ESD_USB_TRIPLE_SAMPLES BIT(23) 72 73 /* Transmitter Delay Compensation */ 74 #define ESD_USB_3_TDC_MODE_AUTO 0 75 76 /* esd IDADD message */ 77 #define ESD_USB_ID_ENABLE BIT(7) 78 #define ESD_USB_MAX_ID_SEGMENT 64 79 80 /* SJA1000 ECC register (emulated by usb firmware) */ 81 #define ESD_USB_SJA1000_ECC_SEG GENMASK(4, 0) 82 #define ESD_USB_SJA1000_ECC_DIR BIT(5) 83 #define ESD_USB_SJA1000_ECC_ERR BIT(2, 1) 84 #define ESD_USB_SJA1000_ECC_BIT 0x00 85 #define ESD_USB_SJA1000_ECC_FORM BIT(6) 86 #define ESD_USB_SJA1000_ECC_STUFF BIT(7) 87 #define ESD_USB_SJA1000_ECC_MASK GENMASK(7, 6) 88 89 /* esd bus state event codes */ 90 #define ESD_USB_BUSSTATE_MASK GENMASK(7, 6) 91 #define ESD_USB_BUSSTATE_WARN BIT(6) 92 #define ESD_USB_BUSSTATE_ERRPASSIVE BIT(7) 93 #define ESD_USB_BUSSTATE_BUSOFF GENMASK(7, 6) 94 95 #define ESD_USB_RX_BUFFER_SIZE 1024 96 #define ESD_USB_MAX_RX_URBS 4 97 #define ESD_USB_MAX_TX_URBS 16 /* must be power of 2 */ 98 99 /* Modes for CAN-USB/3, to be used for esd_usb_3_set_baudrate_msg_x.mode */ 100 #define ESD_USB_3_BAUDRATE_MODE_DISABLE 0 /* remove from bus */ 101 #define ESD_USB_3_BAUDRATE_MODE_INDEX 1 /* ESD (CiA) bit rate idx */ 102 #define ESD_USB_3_BAUDRATE_MODE_BTR_CTRL 2 /* BTR values (controller)*/ 103 #define ESD_USB_3_BAUDRATE_MODE_BTR_CANONICAL 3 /* BTR values (canonical) */ 104 #define ESD_USB_3_BAUDRATE_MODE_NUM 4 /* numerical bit rate */ 105 #define ESD_USB_3_BAUDRATE_MODE_AUTOBAUD 5 /* autobaud */ 106 107 /* Flags for CAN-USB/3, to be used for esd_usb_3_set_baudrate_msg_x.flags */ 108 #define ESD_USB_3_BAUDRATE_FLAG_FD BIT(0) /* enable CAN FD mode */ 109 #define ESD_USB_3_BAUDRATE_FLAG_LOM BIT(1) /* enable listen only mode */ 110 #define ESD_USB_3_BAUDRATE_FLAG_STM BIT(2) /* enable self test mode */ 111 #define ESD_USB_3_BAUDRATE_FLAG_TRS BIT(3) /* enable triple sampling */ 112 #define ESD_USB_3_BAUDRATE_FLAG_TXP BIT(4) /* enable transmit pause */ 113 114 struct esd_usb_header_msg { 115 u8 len; /* total message length in 32bit words */ 116 u8 cmd; 117 u8 rsvd[2]; 118 }; 119 120 struct esd_usb_version_msg { 121 u8 len; /* total message length in 32bit words */ 122 u8 cmd; 123 u8 rsvd; 124 u8 flags; 125 __le32 drv_version; 126 }; 127 128 struct esd_usb_version_reply_msg { 129 u8 len; /* total message length in 32bit words */ 130 u8 cmd; 131 u8 nets; 132 u8 features; 133 __le32 version; 134 u8 name[16]; 135 __le32 rsvd; 136 __le32 ts; 137 }; 138 139 struct esd_usb_rx_msg { 140 u8 len; /* total message length in 32bit words */ 141 u8 cmd; 142 u8 net; 143 u8 dlc; 144 __le32 ts; 145 __le32 id; /* upper 3 bits contain flags */ 146 union { 147 u8 data[CAN_MAX_DLEN]; 148 u8 data_fd[CANFD_MAX_DLEN]; 149 struct { 150 u8 status; /* CAN Controller Status */ 151 u8 ecc; /* Error Capture Register */ 152 u8 rec; /* RX Error Counter */ 153 u8 tec; /* TX Error Counter */ 154 } ev_can_err_ext; /* For ESD_EV_CAN_ERROR_EXT */ 155 }; 156 }; 157 158 struct esd_usb_tx_msg { 159 u8 len; /* total message length in 32bit words */ 160 u8 cmd; 161 u8 net; 162 u8 dlc; 163 u32 hnd; /* opaque handle, not used by device */ 164 __le32 id; /* upper 3 bits contain flags */ 165 union { 166 u8 data[CAN_MAX_DLEN]; 167 u8 data_fd[CANFD_MAX_DLEN]; 168 }; 169 }; 170 171 struct esd_usb_tx_done_msg { 172 u8 len; /* total message length in 32bit words */ 173 u8 cmd; 174 u8 net; 175 u8 status; 176 u32 hnd; /* opaque handle, not used by device */ 177 __le32 ts; 178 }; 179 180 struct esd_usb_id_filter_msg { 181 u8 len; /* total message length in 32bit words */ 182 u8 cmd; 183 u8 net; 184 u8 option; 185 __le32 mask[ESD_USB_MAX_ID_SEGMENT + 1]; /* +1 for 29bit extended IDs */ 186 }; 187 188 struct esd_usb_set_baudrate_msg { 189 u8 len; /* total message length in 32bit words */ 190 u8 cmd; 191 u8 net; 192 u8 rsvd; 193 __le32 baud; 194 }; 195 196 /* CAN-USB/3 baudrate configuration, used for nominal as well as for data bit rate */ 197 struct esd_usb_3_baudrate_cfg { 198 __le16 brp; /* bit rate pre-scaler */ 199 __le16 tseg1; /* time segment before sample point */ 200 __le16 tseg2; /* time segment after sample point */ 201 __le16 sjw; /* synchronization jump Width */ 202 }; 203 204 /* In principle, the esd CAN-USB/3 supports Transmitter Delay Compensation (TDC), 205 * but currently only the automatic TDC mode is supported by this driver. 206 * An implementation for manual TDC configuration will follow. 207 * 208 * For information about struct esd_usb_3_tdc_cfg, see 209 * NTCAN Application Developers Manual, 6.2.25 NTCAN_TDC_CFG + related chapters 210 * https://esd.eu/fileadmin/esd/docs/manuals/NTCAN_Part1_Function_API_Manual_en_56.pdf 211 */ 212 struct esd_usb_3_tdc_cfg { 213 u8 tdc_mode; /* transmitter delay compensation mode */ 214 u8 ssp_offset; /* secondary sample point offset in mtq */ 215 s8 ssp_shift; /* secondary sample point shift in mtq */ 216 u8 tdc_filter; /* TDC filter in mtq */ 217 }; 218 219 /* Extended version of the above set_baudrate_msg for a CAN-USB/3 220 * to define the CAN bit timing configuration of the CAN controller in 221 * CAN FD mode as well as in Classical CAN mode. 222 * 223 * The payload of this command is a NTCAN_BAUDRATE_X structure according to 224 * esd electronics gmbh, NTCAN Application Developers Manual, 6.2.15 NTCAN_BAUDRATE_X 225 * https://esd.eu/fileadmin/esd/docs/manuals/NTCAN_Part1_Function_API_Manual_en_56.pdf 226 */ 227 struct esd_usb_3_set_baudrate_msg_x { 228 u8 len; /* total message length in 32bit words */ 229 u8 cmd; 230 u8 net; 231 u8 rsvd; /*reserved */ 232 /* Payload ... */ 233 __le16 mode; /* mode word, see ESD_USB_3_BAUDRATE_MODE_xxx */ 234 __le16 flags; /* control flags, see ESD_USB_3_BAUDRATE_FLAG_xxx */ 235 struct esd_usb_3_tdc_cfg tdc; /* TDC configuration */ 236 struct esd_usb_3_baudrate_cfg nom; /* nominal bit rate */ 237 struct esd_usb_3_baudrate_cfg data; /* data bit rate */ 238 }; 239 240 /* Main message type used between library and application */ 241 union __packed esd_usb_msg { 242 struct esd_usb_header_msg hdr; 243 struct esd_usb_version_msg version; 244 struct esd_usb_version_reply_msg version_reply; 245 struct esd_usb_rx_msg rx; 246 struct esd_usb_tx_msg tx; 247 struct esd_usb_tx_done_msg txdone; 248 struct esd_usb_set_baudrate_msg setbaud; 249 struct esd_usb_3_set_baudrate_msg_x setbaud_x; 250 struct esd_usb_id_filter_msg filter; 251 }; 252 253 static struct usb_device_id esd_usb_table[] = { 254 {USB_DEVICE(ESD_USB_ESDGMBH_VENDOR_ID, ESD_USB_CANUSB2_PRODUCT_ID)}, 255 {USB_DEVICE(ESD_USB_ESDGMBH_VENDOR_ID, ESD_USB_CANUSBM_PRODUCT_ID)}, 256 {USB_DEVICE(ESD_USB_ESDGMBH_VENDOR_ID, ESD_USB_CANUSB3_PRODUCT_ID)}, 257 {} 258 }; 259 MODULE_DEVICE_TABLE(usb, esd_usb_table); 260 261 struct esd_usb_net_priv; 262 263 struct esd_tx_urb_context { 264 struct esd_usb_net_priv *priv; 265 u32 echo_index; 266 }; 267 268 struct esd_usb { 269 struct usb_device *udev; 270 struct esd_usb_net_priv *nets[ESD_USB_MAX_NETS]; 271 272 struct usb_anchor rx_submitted; 273 274 int net_count; 275 u32 version; 276 int rxinitdone; 277 void *rxbuf[ESD_USB_MAX_RX_URBS]; 278 dma_addr_t rxbuf_dma[ESD_USB_MAX_RX_URBS]; 279 }; 280 281 struct esd_usb_net_priv { 282 struct can_priv can; /* must be the first member */ 283 284 atomic_t active_tx_jobs; 285 struct usb_anchor tx_submitted; 286 struct esd_tx_urb_context tx_contexts[ESD_USB_MAX_TX_URBS]; 287 288 struct esd_usb *usb; 289 struct net_device *netdev; 290 int index; 291 u8 old_state; 292 struct can_berr_counter bec; 293 }; 294 295 static void esd_usb_rx_event(struct esd_usb_net_priv *priv, 296 union esd_usb_msg *msg) 297 { 298 struct net_device_stats *stats = &priv->netdev->stats; 299 struct can_frame *cf; 300 struct sk_buff *skb; 301 u32 id = le32_to_cpu(msg->rx.id) & ESD_USB_IDMASK; 302 303 if (id == ESD_USB_EV_CAN_ERROR_EXT) { 304 u8 state = msg->rx.ev_can_err_ext.status; 305 u8 ecc = msg->rx.ev_can_err_ext.ecc; 306 307 priv->bec.rxerr = msg->rx.ev_can_err_ext.rec; 308 priv->bec.txerr = msg->rx.ev_can_err_ext.tec; 309 310 netdev_dbg(priv->netdev, 311 "CAN_ERR_EV_EXT: dlc=%#02x state=%02x ecc=%02x rec=%02x tec=%02x\n", 312 msg->rx.dlc, state, ecc, 313 priv->bec.rxerr, priv->bec.txerr); 314 315 /* if berr-reporting is off, only pass through on state change ... */ 316 if (!(priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING) && 317 state == priv->old_state) 318 return; 319 320 skb = alloc_can_err_skb(priv->netdev, &cf); 321 if (!skb) 322 stats->rx_dropped++; 323 324 if (state != priv->old_state) { 325 enum can_state tx_state, rx_state; 326 enum can_state new_state = CAN_STATE_ERROR_ACTIVE; 327 328 priv->old_state = state; 329 330 switch (state & ESD_USB_BUSSTATE_MASK) { 331 case ESD_USB_BUSSTATE_BUSOFF: 332 new_state = CAN_STATE_BUS_OFF; 333 can_bus_off(priv->netdev); 334 break; 335 case ESD_USB_BUSSTATE_WARN: 336 new_state = CAN_STATE_ERROR_WARNING; 337 break; 338 case ESD_USB_BUSSTATE_ERRPASSIVE: 339 new_state = CAN_STATE_ERROR_PASSIVE; 340 break; 341 default: 342 new_state = CAN_STATE_ERROR_ACTIVE; 343 priv->bec.txerr = 0; 344 priv->bec.rxerr = 0; 345 break; 346 } 347 348 if (new_state != priv->can.state) { 349 tx_state = (priv->bec.txerr >= priv->bec.rxerr) ? new_state : 0; 350 rx_state = (priv->bec.txerr <= priv->bec.rxerr) ? new_state : 0; 351 can_change_state(priv->netdev, cf, 352 tx_state, rx_state); 353 } 354 } else if (skb) { 355 priv->can.can_stats.bus_error++; 356 stats->rx_errors++; 357 358 cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR; 359 360 switch (ecc & ESD_USB_SJA1000_ECC_MASK) { 361 case ESD_USB_SJA1000_ECC_BIT: 362 cf->data[2] |= CAN_ERR_PROT_BIT; 363 break; 364 case ESD_USB_SJA1000_ECC_FORM: 365 cf->data[2] |= CAN_ERR_PROT_FORM; 366 break; 367 case ESD_USB_SJA1000_ECC_STUFF: 368 cf->data[2] |= CAN_ERR_PROT_STUFF; 369 break; 370 default: 371 break; 372 } 373 374 /* Error occurred during transmission? */ 375 if (!(ecc & ESD_USB_SJA1000_ECC_DIR)) 376 cf->data[2] |= CAN_ERR_PROT_TX; 377 378 /* Bit stream position in CAN frame as the error was detected */ 379 cf->data[3] = ecc & ESD_USB_SJA1000_ECC_SEG; 380 } 381 382 if (skb) { 383 cf->can_id |= CAN_ERR_CNT; 384 cf->data[6] = priv->bec.txerr; 385 cf->data[7] = priv->bec.rxerr; 386 387 netif_rx(skb); 388 } 389 } 390 } 391 392 static void esd_usb_rx_can_msg(struct esd_usb_net_priv *priv, 393 union esd_usb_msg *msg) 394 { 395 struct net_device_stats *stats = &priv->netdev->stats; 396 struct can_frame *cf; 397 struct canfd_frame *cfd; 398 struct sk_buff *skb; 399 u32 id; 400 u8 len; 401 402 if (!netif_device_present(priv->netdev)) 403 return; 404 405 id = le32_to_cpu(msg->rx.id); 406 407 if (id & ESD_USB_EVENT) { 408 esd_usb_rx_event(priv, msg); 409 } else { 410 if (msg->rx.dlc & ESD_USB_FD) { 411 skb = alloc_canfd_skb(priv->netdev, &cfd); 412 } else { 413 skb = alloc_can_skb(priv->netdev, &cf); 414 cfd = (struct canfd_frame *)cf; 415 } 416 417 if (skb == NULL) { 418 stats->rx_dropped++; 419 return; 420 } 421 422 cfd->can_id = id & ESD_USB_IDMASK; 423 424 if (msg->rx.dlc & ESD_USB_FD) { 425 /* masking by 0x0F is already done within can_fd_dlc2len() */ 426 cfd->len = can_fd_dlc2len(msg->rx.dlc); 427 len = cfd->len; 428 if ((msg->rx.dlc & ESD_USB_NO_BRS) == 0) 429 cfd->flags |= CANFD_BRS; 430 if (msg->rx.dlc & ESD_USB_ESI) 431 cfd->flags |= CANFD_ESI; 432 } else { 433 can_frame_set_cc_len(cf, msg->rx.dlc & ~ESD_USB_RTR, priv->can.ctrlmode); 434 len = cf->len; 435 if (msg->rx.dlc & ESD_USB_RTR) { 436 cf->can_id |= CAN_RTR_FLAG; 437 len = 0; 438 } 439 } 440 441 if (id & ESD_USB_EXTID) 442 cfd->can_id |= CAN_EFF_FLAG; 443 444 memcpy(cfd->data, msg->rx.data_fd, len); 445 stats->rx_bytes += len; 446 stats->rx_packets++; 447 448 netif_rx(skb); 449 } 450 } 451 452 static void esd_usb_tx_done_msg(struct esd_usb_net_priv *priv, 453 union esd_usb_msg *msg) 454 { 455 struct net_device_stats *stats = &priv->netdev->stats; 456 struct net_device *netdev = priv->netdev; 457 struct esd_tx_urb_context *context; 458 459 if (!netif_device_present(netdev)) 460 return; 461 462 context = &priv->tx_contexts[msg->txdone.hnd & (ESD_USB_MAX_TX_URBS - 1)]; 463 464 if (!msg->txdone.status) { 465 stats->tx_packets++; 466 stats->tx_bytes += can_get_echo_skb(netdev, context->echo_index, 467 NULL); 468 } else { 469 stats->tx_errors++; 470 can_free_echo_skb(netdev, context->echo_index, NULL); 471 } 472 473 /* Release context */ 474 context->echo_index = ESD_USB_MAX_TX_URBS; 475 atomic_dec(&priv->active_tx_jobs); 476 477 netif_wake_queue(netdev); 478 } 479 480 static void esd_usb_read_bulk_callback(struct urb *urb) 481 { 482 struct esd_usb *dev = urb->context; 483 int retval; 484 int pos = 0; 485 int i; 486 487 switch (urb->status) { 488 case 0: /* success */ 489 break; 490 491 case -ENOENT: 492 case -EPIPE: 493 case -EPROTO: 494 case -ESHUTDOWN: 495 return; 496 497 default: 498 dev_info(dev->udev->dev.parent, 499 "Rx URB aborted (%d)\n", urb->status); 500 goto resubmit_urb; 501 } 502 503 while (pos < urb->actual_length) { 504 union esd_usb_msg *msg; 505 506 msg = (union esd_usb_msg *)(urb->transfer_buffer + pos); 507 508 switch (msg->hdr.cmd) { 509 case ESD_USB_CMD_CAN_RX: 510 if (msg->rx.net >= dev->net_count) { 511 dev_err(dev->udev->dev.parent, "format error\n"); 512 break; 513 } 514 515 esd_usb_rx_can_msg(dev->nets[msg->rx.net], msg); 516 break; 517 518 case ESD_USB_CMD_CAN_TX: 519 if (msg->txdone.net >= dev->net_count) { 520 dev_err(dev->udev->dev.parent, "format error\n"); 521 break; 522 } 523 524 esd_usb_tx_done_msg(dev->nets[msg->txdone.net], 525 msg); 526 break; 527 } 528 529 pos += msg->hdr.len * sizeof(u32); /* convert to # of bytes */ 530 531 if (pos > urb->actual_length) { 532 dev_err(dev->udev->dev.parent, "format error\n"); 533 break; 534 } 535 } 536 537 resubmit_urb: 538 usb_fill_bulk_urb(urb, dev->udev, usb_rcvbulkpipe(dev->udev, 1), 539 urb->transfer_buffer, ESD_USB_RX_BUFFER_SIZE, 540 esd_usb_read_bulk_callback, dev); 541 542 retval = usb_submit_urb(urb, GFP_ATOMIC); 543 if (retval == -ENODEV) { 544 for (i = 0; i < dev->net_count; i++) { 545 if (dev->nets[i]) 546 netif_device_detach(dev->nets[i]->netdev); 547 } 548 } else if (retval) { 549 dev_err(dev->udev->dev.parent, 550 "failed resubmitting read bulk urb: %d\n", retval); 551 } 552 } 553 554 /* callback for bulk IN urb */ 555 static void esd_usb_write_bulk_callback(struct urb *urb) 556 { 557 struct esd_tx_urb_context *context = urb->context; 558 struct esd_usb_net_priv *priv; 559 struct net_device *netdev; 560 size_t size = sizeof(union esd_usb_msg); 561 562 WARN_ON(!context); 563 564 priv = context->priv; 565 netdev = priv->netdev; 566 567 /* free up our allocated buffer */ 568 usb_free_coherent(urb->dev, size, 569 urb->transfer_buffer, urb->transfer_dma); 570 571 if (!netif_device_present(netdev)) 572 return; 573 574 if (urb->status) 575 netdev_info(netdev, "Tx URB aborted (%d)\n", urb->status); 576 577 netif_trans_update(netdev); 578 } 579 580 static ssize_t firmware_show(struct device *d, 581 struct device_attribute *attr, char *buf) 582 { 583 struct usb_interface *intf = to_usb_interface(d); 584 struct esd_usb *dev = usb_get_intfdata(intf); 585 586 return sprintf(buf, "%d.%d.%d\n", 587 (dev->version >> 12) & 0xf, 588 (dev->version >> 8) & 0xf, 589 dev->version & 0xff); 590 } 591 static DEVICE_ATTR_RO(firmware); 592 593 static ssize_t hardware_show(struct device *d, 594 struct device_attribute *attr, char *buf) 595 { 596 struct usb_interface *intf = to_usb_interface(d); 597 struct esd_usb *dev = usb_get_intfdata(intf); 598 599 return sprintf(buf, "%d.%d.%d\n", 600 (dev->version >> 28) & 0xf, 601 (dev->version >> 24) & 0xf, 602 (dev->version >> 16) & 0xff); 603 } 604 static DEVICE_ATTR_RO(hardware); 605 606 static ssize_t nets_show(struct device *d, 607 struct device_attribute *attr, char *buf) 608 { 609 struct usb_interface *intf = to_usb_interface(d); 610 struct esd_usb *dev = usb_get_intfdata(intf); 611 612 return sprintf(buf, "%d", dev->net_count); 613 } 614 static DEVICE_ATTR_RO(nets); 615 616 static int esd_usb_send_msg(struct esd_usb *dev, union esd_usb_msg *msg) 617 { 618 int actual_length; 619 620 return usb_bulk_msg(dev->udev, 621 usb_sndbulkpipe(dev->udev, 2), 622 msg, 623 msg->hdr.len * sizeof(u32), /* convert to # of bytes */ 624 &actual_length, 625 1000); 626 } 627 628 static int esd_usb_wait_msg(struct esd_usb *dev, 629 union esd_usb_msg *msg) 630 { 631 int actual_length; 632 633 return usb_bulk_msg(dev->udev, 634 usb_rcvbulkpipe(dev->udev, 1), 635 msg, 636 sizeof(*msg), 637 &actual_length, 638 1000); 639 } 640 641 static int esd_usb_setup_rx_urbs(struct esd_usb *dev) 642 { 643 int i, err = 0; 644 645 if (dev->rxinitdone) 646 return 0; 647 648 for (i = 0; i < ESD_USB_MAX_RX_URBS; i++) { 649 struct urb *urb = NULL; 650 u8 *buf = NULL; 651 dma_addr_t buf_dma; 652 653 /* create a URB, and a buffer for it */ 654 urb = usb_alloc_urb(0, GFP_KERNEL); 655 if (!urb) { 656 err = -ENOMEM; 657 break; 658 } 659 660 buf = usb_alloc_coherent(dev->udev, ESD_USB_RX_BUFFER_SIZE, GFP_KERNEL, 661 &buf_dma); 662 if (!buf) { 663 dev_warn(dev->udev->dev.parent, 664 "No memory left for USB buffer\n"); 665 err = -ENOMEM; 666 goto freeurb; 667 } 668 669 urb->transfer_dma = buf_dma; 670 671 usb_fill_bulk_urb(urb, dev->udev, 672 usb_rcvbulkpipe(dev->udev, 1), 673 buf, ESD_USB_RX_BUFFER_SIZE, 674 esd_usb_read_bulk_callback, dev); 675 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; 676 usb_anchor_urb(urb, &dev->rx_submitted); 677 678 err = usb_submit_urb(urb, GFP_KERNEL); 679 if (err) { 680 usb_unanchor_urb(urb); 681 usb_free_coherent(dev->udev, ESD_USB_RX_BUFFER_SIZE, buf, 682 urb->transfer_dma); 683 goto freeurb; 684 } 685 686 dev->rxbuf[i] = buf; 687 dev->rxbuf_dma[i] = buf_dma; 688 689 freeurb: 690 /* Drop reference, USB core will take care of freeing it */ 691 usb_free_urb(urb); 692 if (err) 693 break; 694 } 695 696 /* Did we submit any URBs */ 697 if (i == 0) { 698 dev_err(dev->udev->dev.parent, "couldn't setup read URBs\n"); 699 return err; 700 } 701 702 /* Warn if we've couldn't transmit all the URBs */ 703 if (i < ESD_USB_MAX_RX_URBS) { 704 dev_warn(dev->udev->dev.parent, 705 "rx performance may be slow\n"); 706 } 707 708 dev->rxinitdone = 1; 709 return 0; 710 } 711 712 /* Start interface */ 713 static int esd_usb_start(struct esd_usb_net_priv *priv) 714 { 715 struct esd_usb *dev = priv->usb; 716 struct net_device *netdev = priv->netdev; 717 union esd_usb_msg *msg; 718 int err, i; 719 720 msg = kmalloc(sizeof(*msg), GFP_KERNEL); 721 if (!msg) { 722 err = -ENOMEM; 723 goto out; 724 } 725 726 /* Enable all IDs 727 * The IDADD message takes up to 64 32 bit bitmasks (2048 bits). 728 * Each bit represents one 11 bit CAN identifier. A set bit 729 * enables reception of the corresponding CAN identifier. A cleared 730 * bit disabled this identifier. An additional bitmask value 731 * following the CAN 2.0A bits is used to enable reception of 732 * extended CAN frames. Only the LSB of this final mask is checked 733 * for the complete 29 bit ID range. The IDADD message also allows 734 * filter configuration for an ID subset. In this case you can add 735 * the number of the starting bitmask (0..64) to the filter.option 736 * field followed by only some bitmasks. 737 */ 738 msg->hdr.cmd = ESD_USB_CMD_IDADD; 739 msg->hdr.len = sizeof(struct esd_usb_id_filter_msg) / sizeof(u32); /* # of 32bit words */ 740 msg->filter.net = priv->index; 741 msg->filter.option = ESD_USB_ID_ENABLE; /* start with segment 0 */ 742 for (i = 0; i < ESD_USB_MAX_ID_SEGMENT; i++) 743 msg->filter.mask[i] = cpu_to_le32(GENMASK(31, 0)); 744 /* enable 29bit extended IDs */ 745 msg->filter.mask[ESD_USB_MAX_ID_SEGMENT] = cpu_to_le32(BIT(0)); 746 747 err = esd_usb_send_msg(dev, msg); 748 if (err) 749 goto out; 750 751 err = esd_usb_setup_rx_urbs(dev); 752 if (err) 753 goto out; 754 755 priv->can.state = CAN_STATE_ERROR_ACTIVE; 756 757 out: 758 if (err == -ENODEV) 759 netif_device_detach(netdev); 760 if (err) 761 netdev_err(netdev, "couldn't start device: %d\n", err); 762 763 kfree(msg); 764 return err; 765 } 766 767 static void unlink_all_urbs(struct esd_usb *dev) 768 { 769 struct esd_usb_net_priv *priv; 770 int i, j; 771 772 usb_kill_anchored_urbs(&dev->rx_submitted); 773 774 for (i = 0; i < ESD_USB_MAX_RX_URBS; ++i) 775 usb_free_coherent(dev->udev, ESD_USB_RX_BUFFER_SIZE, 776 dev->rxbuf[i], dev->rxbuf_dma[i]); 777 778 for (i = 0; i < dev->net_count; i++) { 779 priv = dev->nets[i]; 780 if (priv) { 781 usb_kill_anchored_urbs(&priv->tx_submitted); 782 atomic_set(&priv->active_tx_jobs, 0); 783 784 for (j = 0; j < ESD_USB_MAX_TX_URBS; j++) 785 priv->tx_contexts[j].echo_index = ESD_USB_MAX_TX_URBS; 786 } 787 } 788 } 789 790 static int esd_usb_open(struct net_device *netdev) 791 { 792 struct esd_usb_net_priv *priv = netdev_priv(netdev); 793 int err; 794 795 /* common open */ 796 err = open_candev(netdev); 797 if (err) 798 return err; 799 800 /* finally start device */ 801 err = esd_usb_start(priv); 802 if (err) { 803 netdev_warn(netdev, "couldn't start device: %d\n", err); 804 close_candev(netdev); 805 return err; 806 } 807 808 netif_start_queue(netdev); 809 810 return 0; 811 } 812 813 static netdev_tx_t esd_usb_start_xmit(struct sk_buff *skb, 814 struct net_device *netdev) 815 { 816 struct esd_usb_net_priv *priv = netdev_priv(netdev); 817 struct esd_usb *dev = priv->usb; 818 struct esd_tx_urb_context *context = NULL; 819 struct net_device_stats *stats = &netdev->stats; 820 struct canfd_frame *cfd = (struct canfd_frame *)skb->data; 821 union esd_usb_msg *msg; 822 struct urb *urb; 823 u8 *buf; 824 int i, err; 825 int ret = NETDEV_TX_OK; 826 size_t size = sizeof(union esd_usb_msg); 827 828 if (can_dev_dropped_skb(netdev, skb)) 829 return NETDEV_TX_OK; 830 831 /* create a URB, and a buffer for it, and copy the data to the URB */ 832 urb = usb_alloc_urb(0, GFP_ATOMIC); 833 if (!urb) { 834 stats->tx_dropped++; 835 dev_kfree_skb(skb); 836 goto nourbmem; 837 } 838 839 buf = usb_alloc_coherent(dev->udev, size, GFP_ATOMIC, 840 &urb->transfer_dma); 841 if (!buf) { 842 netdev_err(netdev, "No memory left for USB buffer\n"); 843 stats->tx_dropped++; 844 dev_kfree_skb(skb); 845 goto nobufmem; 846 } 847 848 msg = (union esd_usb_msg *)buf; 849 850 /* minimal length as # of 32bit words */ 851 msg->hdr.len = offsetof(struct esd_usb_tx_msg, data) / sizeof(u32); 852 msg->hdr.cmd = ESD_USB_CMD_CAN_TX; 853 msg->tx.net = priv->index; 854 855 if (can_is_canfd_skb(skb)) { 856 msg->tx.dlc = can_fd_len2dlc(cfd->len); 857 msg->tx.dlc |= ESD_USB_FD; 858 859 if ((cfd->flags & CANFD_BRS) == 0) 860 msg->tx.dlc |= ESD_USB_NO_BRS; 861 } else { 862 msg->tx.dlc = can_get_cc_dlc((struct can_frame *)cfd, priv->can.ctrlmode); 863 864 if (cfd->can_id & CAN_RTR_FLAG) 865 msg->tx.dlc |= ESD_USB_RTR; 866 } 867 868 msg->tx.id = cpu_to_le32(cfd->can_id & CAN_ERR_MASK); 869 870 if (cfd->can_id & CAN_EFF_FLAG) 871 msg->tx.id |= cpu_to_le32(ESD_USB_EXTID); 872 873 memcpy(msg->tx.data_fd, cfd->data, cfd->len); 874 875 /* round up, then divide by 4 to add the payload length as # of 32bit words */ 876 msg->hdr.len += DIV_ROUND_UP(cfd->len, sizeof(u32)); 877 878 for (i = 0; i < ESD_USB_MAX_TX_URBS; i++) { 879 if (priv->tx_contexts[i].echo_index == ESD_USB_MAX_TX_URBS) { 880 context = &priv->tx_contexts[i]; 881 break; 882 } 883 } 884 885 /* This may never happen */ 886 if (!context) { 887 netdev_warn(netdev, "couldn't find free context\n"); 888 ret = NETDEV_TX_BUSY; 889 goto releasebuf; 890 } 891 892 context->priv = priv; 893 context->echo_index = i; 894 895 /* hnd must not be 0 - MSB is stripped in txdone handling */ 896 msg->tx.hnd = BIT(31) | i; /* returned in TX done message */ 897 898 usb_fill_bulk_urb(urb, dev->udev, usb_sndbulkpipe(dev->udev, 2), buf, 899 msg->hdr.len * sizeof(u32), /* convert to # of bytes */ 900 esd_usb_write_bulk_callback, context); 901 902 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; 903 904 usb_anchor_urb(urb, &priv->tx_submitted); 905 906 can_put_echo_skb(skb, netdev, context->echo_index, 0); 907 908 atomic_inc(&priv->active_tx_jobs); 909 910 /* Slow down tx path */ 911 if (atomic_read(&priv->active_tx_jobs) >= ESD_USB_MAX_TX_URBS) 912 netif_stop_queue(netdev); 913 914 err = usb_submit_urb(urb, GFP_ATOMIC); 915 if (err) { 916 can_free_echo_skb(netdev, context->echo_index, NULL); 917 918 atomic_dec(&priv->active_tx_jobs); 919 usb_unanchor_urb(urb); 920 921 stats->tx_dropped++; 922 923 if (err == -ENODEV) 924 netif_device_detach(netdev); 925 else 926 netdev_warn(netdev, "failed tx_urb %d\n", err); 927 928 goto releasebuf; 929 } 930 931 netif_trans_update(netdev); 932 933 /* Release our reference to this URB, the USB core will eventually free 934 * it entirely. 935 */ 936 usb_free_urb(urb); 937 938 return NETDEV_TX_OK; 939 940 releasebuf: 941 usb_free_coherent(dev->udev, size, buf, urb->transfer_dma); 942 943 nobufmem: 944 usb_free_urb(urb); 945 946 nourbmem: 947 return ret; 948 } 949 950 static int esd_usb_close(struct net_device *netdev) 951 { 952 struct esd_usb_net_priv *priv = netdev_priv(netdev); 953 union esd_usb_msg *msg; 954 int i; 955 956 msg = kmalloc(sizeof(*msg), GFP_KERNEL); 957 if (!msg) 958 return -ENOMEM; 959 960 /* Disable all IDs (see esd_usb_start()) */ 961 msg->hdr.cmd = ESD_USB_CMD_IDADD; 962 msg->hdr.len = sizeof(struct esd_usb_id_filter_msg) / sizeof(u32);/* # of 32bit words */ 963 msg->filter.net = priv->index; 964 msg->filter.option = ESD_USB_ID_ENABLE; /* start with segment 0 */ 965 for (i = 0; i <= ESD_USB_MAX_ID_SEGMENT; i++) 966 msg->filter.mask[i] = 0; 967 if (esd_usb_send_msg(priv->usb, msg) < 0) 968 netdev_err(netdev, "sending idadd message failed\n"); 969 970 /* set CAN controller to reset mode */ 971 msg->hdr.len = sizeof(struct esd_usb_set_baudrate_msg) / sizeof(u32); /* # of 32bit words */ 972 msg->hdr.cmd = ESD_USB_CMD_SETBAUD; 973 msg->setbaud.net = priv->index; 974 msg->setbaud.rsvd = 0; 975 msg->setbaud.baud = cpu_to_le32(ESD_USB_NO_BAUDRATE); 976 if (esd_usb_send_msg(priv->usb, msg) < 0) 977 netdev_err(netdev, "sending setbaud message failed\n"); 978 979 priv->can.state = CAN_STATE_STOPPED; 980 981 netif_stop_queue(netdev); 982 983 close_candev(netdev); 984 985 kfree(msg); 986 987 return 0; 988 } 989 990 static const struct net_device_ops esd_usb_netdev_ops = { 991 .ndo_open = esd_usb_open, 992 .ndo_stop = esd_usb_close, 993 .ndo_start_xmit = esd_usb_start_xmit, 994 .ndo_change_mtu = can_change_mtu, 995 }; 996 997 static const struct ethtool_ops esd_usb_ethtool_ops = { 998 .get_ts_info = ethtool_op_get_ts_info, 999 }; 1000 1001 static const struct can_bittiming_const esd_usb_2_bittiming_const = { 1002 .name = "esd_usb_2", 1003 .tseg1_min = 1, 1004 .tseg1_max = 16, 1005 .tseg2_min = 1, 1006 .tseg2_max = 8, 1007 .sjw_max = 4, 1008 .brp_min = 1, 1009 .brp_max = 1024, 1010 .brp_inc = 1, 1011 }; 1012 1013 static int esd_usb_2_set_bittiming(struct net_device *netdev) 1014 { 1015 const struct can_bittiming_const *btc = &esd_usb_2_bittiming_const; 1016 struct esd_usb_net_priv *priv = netdev_priv(netdev); 1017 struct can_bittiming *bt = &priv->can.bittiming; 1018 union esd_usb_msg *msg; 1019 int err; 1020 u32 canbtr; 1021 int sjw_shift; 1022 1023 canbtr = ESD_USB_UBR; 1024 if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY) 1025 canbtr |= ESD_USB_LOM; 1026 1027 canbtr |= (bt->brp - 1) & (btc->brp_max - 1); 1028 1029 if (le16_to_cpu(priv->usb->udev->descriptor.idProduct) == 1030 ESD_USB_CANUSBM_PRODUCT_ID) 1031 sjw_shift = ESD_USB_M_SJW_SHIFT; 1032 else 1033 sjw_shift = ESD_USB_2_SJW_SHIFT; 1034 1035 canbtr |= ((bt->sjw - 1) & (btc->sjw_max - 1)) 1036 << sjw_shift; 1037 canbtr |= ((bt->prop_seg + bt->phase_seg1 - 1) 1038 & (btc->tseg1_max - 1)) 1039 << ESD_USB_2_TSEG1_SHIFT; 1040 canbtr |= ((bt->phase_seg2 - 1) & (btc->tseg2_max - 1)) 1041 << ESD_USB_2_TSEG2_SHIFT; 1042 if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES) 1043 canbtr |= ESD_USB_TRIPLE_SAMPLES; 1044 1045 msg = kmalloc(sizeof(*msg), GFP_KERNEL); 1046 if (!msg) 1047 return -ENOMEM; 1048 1049 msg->hdr.len = sizeof(struct esd_usb_set_baudrate_msg) / sizeof(u32); /* # of 32bit words */ 1050 msg->hdr.cmd = ESD_USB_CMD_SETBAUD; 1051 msg->setbaud.net = priv->index; 1052 msg->setbaud.rsvd = 0; 1053 msg->setbaud.baud = cpu_to_le32(canbtr); 1054 1055 netdev_dbg(netdev, "setting BTR=%#x\n", canbtr); 1056 1057 err = esd_usb_send_msg(priv->usb, msg); 1058 1059 kfree(msg); 1060 return err; 1061 } 1062 1063 /* Nominal bittiming constants, see 1064 * Microchip SAM E70/S70/V70/V71, Data Sheet, Rev. G - 07/2022 1065 * 48.6.8 MCAN Nominal Bit Timing and Prescaler Register 1066 */ 1067 static const struct can_bittiming_const esd_usb_3_nom_bittiming_const = { 1068 .name = "esd_usb_3", 1069 .tseg1_min = 2, 1070 .tseg1_max = 256, 1071 .tseg2_min = 2, 1072 .tseg2_max = 128, 1073 .sjw_max = 128, 1074 .brp_min = 1, 1075 .brp_max = 512, 1076 .brp_inc = 1, 1077 }; 1078 1079 /* Data bittiming constants, see 1080 * Microchip SAM E70/S70/V70/V71, Data Sheet, Rev. G - 07/2022 1081 * 48.6.4 MCAN Data Bit Timing and Prescaler Register 1082 */ 1083 static const struct can_bittiming_const esd_usb_3_data_bittiming_const = { 1084 .name = "esd_usb_3", 1085 .tseg1_min = 2, 1086 .tseg1_max = 32, 1087 .tseg2_min = 1, 1088 .tseg2_max = 16, 1089 .sjw_max = 8, 1090 .brp_min = 1, 1091 .brp_max = 32, 1092 .brp_inc = 1, 1093 }; 1094 1095 static int esd_usb_3_set_bittiming(struct net_device *netdev) 1096 { 1097 const struct can_bittiming_const *nom_btc = &esd_usb_3_nom_bittiming_const; 1098 const struct can_bittiming_const *data_btc = &esd_usb_3_data_bittiming_const; 1099 struct esd_usb_net_priv *priv = netdev_priv(netdev); 1100 struct can_bittiming *nom_bt = &priv->can.bittiming; 1101 struct can_bittiming *data_bt = &priv->can.data_bittiming; 1102 struct esd_usb_3_set_baudrate_msg_x *baud_x; 1103 union esd_usb_msg *msg; 1104 u16 flags = 0; 1105 int err; 1106 1107 msg = kmalloc(sizeof(*msg), GFP_KERNEL); 1108 if (!msg) 1109 return -ENOMEM; 1110 1111 baud_x = &msg->setbaud_x; 1112 1113 /* Canonical is the most reasonable mode for SocketCAN on CAN-USB/3 ... */ 1114 baud_x->mode = cpu_to_le16(ESD_USB_3_BAUDRATE_MODE_BTR_CANONICAL); 1115 1116 if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY) 1117 flags |= ESD_USB_3_BAUDRATE_FLAG_LOM; 1118 1119 baud_x->nom.brp = cpu_to_le16(nom_bt->brp & (nom_btc->brp_max - 1)); 1120 baud_x->nom.sjw = cpu_to_le16(nom_bt->sjw & (nom_btc->sjw_max - 1)); 1121 baud_x->nom.tseg1 = cpu_to_le16((nom_bt->prop_seg + nom_bt->phase_seg1) 1122 & (nom_btc->tseg1_max - 1)); 1123 baud_x->nom.tseg2 = cpu_to_le16(nom_bt->phase_seg2 & (nom_btc->tseg2_max - 1)); 1124 1125 if (priv->can.ctrlmode & CAN_CTRLMODE_FD) { 1126 baud_x->data.brp = cpu_to_le16(data_bt->brp & (data_btc->brp_max - 1)); 1127 baud_x->data.sjw = cpu_to_le16(data_bt->sjw & (data_btc->sjw_max - 1)); 1128 baud_x->data.tseg1 = cpu_to_le16((data_bt->prop_seg + data_bt->phase_seg1) 1129 & (data_btc->tseg1_max - 1)); 1130 baud_x->data.tseg2 = cpu_to_le16(data_bt->phase_seg2 & (data_btc->tseg2_max - 1)); 1131 flags |= ESD_USB_3_BAUDRATE_FLAG_FD; 1132 } 1133 1134 /* Currently this driver only supports the automatic TDC mode */ 1135 baud_x->tdc.tdc_mode = ESD_USB_3_TDC_MODE_AUTO; 1136 baud_x->tdc.ssp_offset = 0; 1137 baud_x->tdc.ssp_shift = 0; 1138 baud_x->tdc.tdc_filter = 0; 1139 1140 baud_x->flags = cpu_to_le16(flags); 1141 baud_x->net = priv->index; 1142 baud_x->rsvd = 0; 1143 1144 /* set len as # of 32bit words */ 1145 msg->hdr.len = sizeof(struct esd_usb_3_set_baudrate_msg_x) / sizeof(u32); 1146 msg->hdr.cmd = ESD_USB_CMD_SETBAUD; 1147 1148 netdev_dbg(netdev, 1149 "ctrlmode=%#x/%#x, esd-net=%u, esd-mode=%#x, esd-flags=%#x\n", 1150 priv->can.ctrlmode, priv->can.ctrlmode_supported, 1151 priv->index, le16_to_cpu(baud_x->mode), flags); 1152 1153 err = esd_usb_send_msg(priv->usb, msg); 1154 1155 kfree(msg); 1156 return err; 1157 } 1158 1159 static int esd_usb_get_berr_counter(const struct net_device *netdev, 1160 struct can_berr_counter *bec) 1161 { 1162 struct esd_usb_net_priv *priv = netdev_priv(netdev); 1163 1164 bec->txerr = priv->bec.txerr; 1165 bec->rxerr = priv->bec.rxerr; 1166 1167 return 0; 1168 } 1169 1170 static int esd_usb_set_mode(struct net_device *netdev, enum can_mode mode) 1171 { 1172 switch (mode) { 1173 case CAN_MODE_START: 1174 netif_wake_queue(netdev); 1175 break; 1176 1177 default: 1178 return -EOPNOTSUPP; 1179 } 1180 1181 return 0; 1182 } 1183 1184 static int esd_usb_probe_one_net(struct usb_interface *intf, int index) 1185 { 1186 struct esd_usb *dev = usb_get_intfdata(intf); 1187 struct net_device *netdev; 1188 struct esd_usb_net_priv *priv; 1189 int err = 0; 1190 int i; 1191 1192 netdev = alloc_candev(sizeof(*priv), ESD_USB_MAX_TX_URBS); 1193 if (!netdev) { 1194 dev_err(&intf->dev, "couldn't alloc candev\n"); 1195 err = -ENOMEM; 1196 goto done; 1197 } 1198 1199 priv = netdev_priv(netdev); 1200 1201 init_usb_anchor(&priv->tx_submitted); 1202 atomic_set(&priv->active_tx_jobs, 0); 1203 1204 for (i = 0; i < ESD_USB_MAX_TX_URBS; i++) 1205 priv->tx_contexts[i].echo_index = ESD_USB_MAX_TX_URBS; 1206 1207 priv->usb = dev; 1208 priv->netdev = netdev; 1209 priv->index = index; 1210 1211 priv->can.state = CAN_STATE_STOPPED; 1212 priv->can.ctrlmode_supported = CAN_CTRLMODE_LISTENONLY | 1213 CAN_CTRLMODE_CC_LEN8_DLC | 1214 CAN_CTRLMODE_BERR_REPORTING; 1215 1216 switch (le16_to_cpu(dev->udev->descriptor.idProduct)) { 1217 case ESD_USB_CANUSB3_PRODUCT_ID: 1218 priv->can.clock.freq = ESD_USB_3_CAN_CLOCK; 1219 priv->can.ctrlmode_supported |= CAN_CTRLMODE_FD; 1220 priv->can.bittiming_const = &esd_usb_3_nom_bittiming_const; 1221 priv->can.data_bittiming_const = &esd_usb_3_data_bittiming_const; 1222 priv->can.do_set_bittiming = esd_usb_3_set_bittiming; 1223 priv->can.do_set_data_bittiming = esd_usb_3_set_bittiming; 1224 break; 1225 1226 case ESD_USB_CANUSBM_PRODUCT_ID: 1227 priv->can.clock.freq = ESD_USB_M_CAN_CLOCK; 1228 priv->can.bittiming_const = &esd_usb_2_bittiming_const; 1229 priv->can.do_set_bittiming = esd_usb_2_set_bittiming; 1230 break; 1231 1232 case ESD_USB_CANUSB2_PRODUCT_ID: 1233 default: 1234 priv->can.clock.freq = ESD_USB_2_CAN_CLOCK; 1235 priv->can.ctrlmode_supported |= CAN_CTRLMODE_3_SAMPLES; 1236 priv->can.bittiming_const = &esd_usb_2_bittiming_const; 1237 priv->can.do_set_bittiming = esd_usb_2_set_bittiming; 1238 break; 1239 } 1240 1241 priv->can.do_set_mode = esd_usb_set_mode; 1242 priv->can.do_get_berr_counter = esd_usb_get_berr_counter; 1243 1244 netdev->flags |= IFF_ECHO; /* we support local echo */ 1245 1246 netdev->netdev_ops = &esd_usb_netdev_ops; 1247 netdev->ethtool_ops = &esd_usb_ethtool_ops; 1248 1249 SET_NETDEV_DEV(netdev, &intf->dev); 1250 netdev->dev_id = index; 1251 1252 err = register_candev(netdev); 1253 if (err) { 1254 dev_err(&intf->dev, "couldn't register CAN device: %d\n", err); 1255 free_candev(netdev); 1256 err = -ENOMEM; 1257 goto done; 1258 } 1259 1260 dev->nets[index] = priv; 1261 netdev_info(netdev, "device %s registered\n", netdev->name); 1262 1263 done: 1264 return err; 1265 } 1266 1267 /* probe function for new USB devices 1268 * 1269 * check version information and number of available 1270 * CAN interfaces 1271 */ 1272 static int esd_usb_probe(struct usb_interface *intf, 1273 const struct usb_device_id *id) 1274 { 1275 struct esd_usb *dev; 1276 union esd_usb_msg *msg; 1277 int i, err; 1278 1279 dev = kzalloc(sizeof(*dev), GFP_KERNEL); 1280 if (!dev) { 1281 err = -ENOMEM; 1282 goto done; 1283 } 1284 1285 dev->udev = interface_to_usbdev(intf); 1286 1287 init_usb_anchor(&dev->rx_submitted); 1288 1289 usb_set_intfdata(intf, dev); 1290 1291 msg = kmalloc(sizeof(*msg), GFP_KERNEL); 1292 if (!msg) { 1293 err = -ENOMEM; 1294 goto free_msg; 1295 } 1296 1297 /* query number of CAN interfaces (nets) */ 1298 msg->hdr.cmd = ESD_USB_CMD_VERSION; 1299 msg->hdr.len = sizeof(struct esd_usb_version_msg) / sizeof(u32); /* # of 32bit words */ 1300 msg->version.rsvd = 0; 1301 msg->version.flags = 0; 1302 msg->version.drv_version = 0; 1303 1304 err = esd_usb_send_msg(dev, msg); 1305 if (err < 0) { 1306 dev_err(&intf->dev, "sending version message failed\n"); 1307 goto free_msg; 1308 } 1309 1310 err = esd_usb_wait_msg(dev, msg); 1311 if (err < 0) { 1312 dev_err(&intf->dev, "no version message answer\n"); 1313 goto free_msg; 1314 } 1315 1316 dev->net_count = (int)msg->version_reply.nets; 1317 dev->version = le32_to_cpu(msg->version_reply.version); 1318 1319 if (device_create_file(&intf->dev, &dev_attr_firmware)) 1320 dev_err(&intf->dev, 1321 "Couldn't create device file for firmware\n"); 1322 1323 if (device_create_file(&intf->dev, &dev_attr_hardware)) 1324 dev_err(&intf->dev, 1325 "Couldn't create device file for hardware\n"); 1326 1327 if (device_create_file(&intf->dev, &dev_attr_nets)) 1328 dev_err(&intf->dev, 1329 "Couldn't create device file for nets\n"); 1330 1331 /* do per device probing */ 1332 for (i = 0; i < dev->net_count; i++) 1333 esd_usb_probe_one_net(intf, i); 1334 1335 free_msg: 1336 kfree(msg); 1337 if (err) 1338 kfree(dev); 1339 done: 1340 return err; 1341 } 1342 1343 /* called by the usb core when the device is removed from the system */ 1344 static void esd_usb_disconnect(struct usb_interface *intf) 1345 { 1346 struct esd_usb *dev = usb_get_intfdata(intf); 1347 struct net_device *netdev; 1348 int i; 1349 1350 device_remove_file(&intf->dev, &dev_attr_firmware); 1351 device_remove_file(&intf->dev, &dev_attr_hardware); 1352 device_remove_file(&intf->dev, &dev_attr_nets); 1353 1354 usb_set_intfdata(intf, NULL); 1355 1356 if (dev) { 1357 for (i = 0; i < dev->net_count; i++) { 1358 if (dev->nets[i]) { 1359 netdev = dev->nets[i]->netdev; 1360 unregister_netdev(netdev); 1361 free_candev(netdev); 1362 } 1363 } 1364 unlink_all_urbs(dev); 1365 kfree(dev); 1366 } 1367 } 1368 1369 /* usb specific object needed to register this driver with the usb subsystem */ 1370 static struct usb_driver esd_usb_driver = { 1371 .name = KBUILD_MODNAME, 1372 .probe = esd_usb_probe, 1373 .disconnect = esd_usb_disconnect, 1374 .id_table = esd_usb_table, 1375 }; 1376 1377 module_usb_driver(esd_usb_driver); 1378