1 // SPDX-License-Identifier: GPL-2.0 2 3 /* Driver for Theobroma Systems UCAN devices, Protocol Version 3 4 * 5 * Copyright (C) 2018 Theobroma Systems Design und Consulting GmbH 6 * 7 * 8 * General Description: 9 * 10 * The USB Device uses three Endpoints: 11 * 12 * CONTROL Endpoint: Is used the setup the device (start, stop, 13 * info, configure). 14 * 15 * IN Endpoint: The device sends CAN Frame Messages and Device 16 * Information using the IN endpoint. 17 * 18 * OUT Endpoint: The driver sends configuration requests, and CAN 19 * Frames on the out endpoint. 20 * 21 * Error Handling: 22 * 23 * If error reporting is turned on the device encodes error into CAN 24 * error frames (see uapi/linux/can/error.h) and sends it using the 25 * IN Endpoint. The driver updates statistics and forward it. 26 */ 27 28 #include <linux/can.h> 29 #include <linux/can/dev.h> 30 #include <linux/can/error.h> 31 #include <linux/ethtool.h> 32 #include <linux/module.h> 33 #include <linux/netdevice.h> 34 #include <linux/signal.h> 35 #include <linux/skbuff.h> 36 #include <linux/slab.h> 37 #include <linux/usb.h> 38 39 #define UCAN_DRIVER_NAME "ucan" 40 #define UCAN_MAX_RX_URBS 8 41 /* the CAN controller needs a while to enable/disable the bus */ 42 #define UCAN_USB_CTL_PIPE_TIMEOUT 1000 43 /* this driver currently supports protocol version 3 only */ 44 #define UCAN_PROTOCOL_VERSION_MIN 3 45 #define UCAN_PROTOCOL_VERSION_MAX 3 46 47 /* UCAN Message Definitions 48 * ------------------------ 49 * 50 * ucan_message_out_t and ucan_message_in_t define the messages 51 * transmitted on the OUT and IN endpoint. 52 * 53 * Multibyte fields are transmitted with little endianness 54 * 55 * INTR Endpoint: a single uint32_t storing the current space in the fifo 56 * 57 * OUT Endpoint: single message of type ucan_message_out_t is 58 * transmitted on the out endpoint 59 * 60 * IN Endpoint: multiple messages ucan_message_in_t concateted in 61 * the following way: 62 * 63 * m[n].len <=> the length if message n(including the header in bytes) 64 * m[n] is is aligned to a 4 byte boundary, hence 65 * offset(m[0]) := 0; 66 * offset(m[n+1]) := offset(m[n]) + (m[n].len + 3) & 3 67 * 68 * this implies that 69 * offset(m[n]) % 4 <=> 0 70 */ 71 72 /* Device Global Commands */ 73 enum { 74 UCAN_DEVICE_GET_FW_STRING = 0, 75 }; 76 77 /* UCAN Commands */ 78 enum { 79 /* start the can transceiver - val defines the operation mode */ 80 UCAN_COMMAND_START = 0, 81 /* cancel pending transmissions and stop the can transceiver */ 82 UCAN_COMMAND_STOP = 1, 83 /* send can transceiver into low-power sleep mode */ 84 UCAN_COMMAND_SLEEP = 2, 85 /* wake up can transceiver from low-power sleep mode */ 86 UCAN_COMMAND_WAKEUP = 3, 87 /* reset the can transceiver */ 88 UCAN_COMMAND_RESET = 4, 89 /* get piece of info from the can transceiver - subcmd defines what 90 * piece 91 */ 92 UCAN_COMMAND_GET = 5, 93 /* clear or disable hardware filter - subcmd defines which of the two */ 94 UCAN_COMMAND_FILTER = 6, 95 /* Setup bittiming */ 96 UCAN_COMMAND_SET_BITTIMING = 7, 97 /* recover from bus-off state */ 98 UCAN_COMMAND_RESTART = 8, 99 }; 100 101 /* UCAN_COMMAND_START and UCAN_COMMAND_GET_INFO operation modes (bitmap). 102 * Undefined bits must be set to 0. 103 */ 104 enum { 105 UCAN_MODE_LOOPBACK = BIT(0), 106 UCAN_MODE_SILENT = BIT(1), 107 UCAN_MODE_3_SAMPLES = BIT(2), 108 UCAN_MODE_ONE_SHOT = BIT(3), 109 UCAN_MODE_BERR_REPORT = BIT(4), 110 }; 111 112 /* UCAN_COMMAND_GET subcommands */ 113 enum { 114 UCAN_COMMAND_GET_INFO = 0, 115 UCAN_COMMAND_GET_PROTOCOL_VERSION = 1, 116 }; 117 118 /* UCAN_COMMAND_FILTER subcommands */ 119 enum { 120 UCAN_FILTER_CLEAR = 0, 121 UCAN_FILTER_DISABLE = 1, 122 UCAN_FILTER_ENABLE = 2, 123 }; 124 125 /* OUT endpoint message types */ 126 enum { 127 UCAN_OUT_TX = 2, /* transmit a CAN frame */ 128 }; 129 130 /* IN endpoint message types */ 131 enum { 132 UCAN_IN_TX_COMPLETE = 1, /* CAN frame transmission completed */ 133 UCAN_IN_RX = 2, /* CAN frame received */ 134 }; 135 136 struct ucan_ctl_cmd_start { 137 __le16 mode; /* OR-ing any of UCAN_MODE_* */ 138 } __packed; 139 140 struct ucan_ctl_cmd_set_bittiming { 141 __le32 tq; /* Time quanta (TQ) in nanoseconds */ 142 __le16 brp; /* TQ Prescaler */ 143 __le16 sample_point; /* Samplepoint on tenth percent */ 144 u8 prop_seg; /* Propagation segment in TQs */ 145 u8 phase_seg1; /* Phase buffer segment 1 in TQs */ 146 u8 phase_seg2; /* Phase buffer segment 2 in TQs */ 147 u8 sjw; /* Synchronisation jump width in TQs */ 148 } __packed; 149 150 struct ucan_ctl_cmd_device_info { 151 __le32 freq; /* Clock Frequency for tq generation */ 152 u8 tx_fifo; /* Size of the transmission fifo */ 153 u8 sjw_max; /* can_bittiming fields... */ 154 u8 tseg1_min; 155 u8 tseg1_max; 156 u8 tseg2_min; 157 u8 tseg2_max; 158 __le16 brp_inc; 159 __le32 brp_min; 160 __le32 brp_max; /* ...can_bittiming fields */ 161 __le16 ctrlmodes; /* supported control modes */ 162 __le16 hwfilter; /* Number of HW filter banks */ 163 __le16 rxmboxes; /* Number of receive Mailboxes */ 164 } __packed; 165 166 struct ucan_ctl_cmd_get_protocol_version { 167 __le32 version; 168 } __packed; 169 170 union ucan_ctl_payload { 171 /* Setup Bittiming 172 * bmRequest == UCAN_COMMAND_START 173 */ 174 struct ucan_ctl_cmd_start cmd_start; 175 /* Setup Bittiming 176 * bmRequest == UCAN_COMMAND_SET_BITTIMING 177 */ 178 struct ucan_ctl_cmd_set_bittiming cmd_set_bittiming; 179 /* Get Device Information 180 * bmRequest == UCAN_COMMAND_GET; wValue = UCAN_COMMAND_GET_INFO 181 */ 182 struct ucan_ctl_cmd_device_info cmd_get_device_info; 183 /* Get Protocol Version 184 * bmRequest == UCAN_COMMAND_GET; 185 * wValue = UCAN_COMMAND_GET_PROTOCOL_VERSION 186 */ 187 struct ucan_ctl_cmd_get_protocol_version cmd_get_protocol_version; 188 189 u8 raw[128]; 190 } __packed; 191 192 enum { 193 UCAN_TX_COMPLETE_SUCCESS = BIT(0), 194 }; 195 196 /* Transmission Complete within ucan_message_in */ 197 struct ucan_tx_complete_entry_t { 198 u8 echo_index; 199 u8 flags; 200 } __packed __aligned(0x2); 201 202 /* CAN Data message format within ucan_message_in/out */ 203 struct ucan_can_msg { 204 /* note DLC is computed by 205 * msg.len - sizeof (msg.len) 206 * - sizeof (msg.type) 207 * - sizeof (msg.can_msg.id) 208 */ 209 __le32 id; 210 211 union { 212 u8 data[CAN_MAX_DLEN]; /* Data of CAN frames */ 213 u8 dlc; /* RTR dlc */ 214 }; 215 } __packed; 216 217 /* OUT Endpoint, outbound messages */ 218 struct ucan_message_out { 219 __le16 len; /* Length of the content include header */ 220 u8 type; /* UCAN_OUT_TX and friends */ 221 u8 subtype; /* command sub type */ 222 223 union { 224 /* Transmit CAN frame 225 * (type == UCAN_TX) && ((msg.can_msg.id & CAN_RTR_FLAG) == 0) 226 * subtype stores the echo id 227 */ 228 struct ucan_can_msg can_msg; 229 } msg; 230 } __packed __aligned(0x4); 231 232 /* IN Endpoint, inbound messages */ 233 struct ucan_message_in { 234 __le16 len; /* Length of the content include header */ 235 u8 type; /* UCAN_IN_RX and friends */ 236 u8 subtype; /* command sub type */ 237 238 union { 239 /* CAN Frame received 240 * (type == UCAN_IN_RX) 241 * && ((msg.can_msg.id & CAN_RTR_FLAG) == 0) 242 */ 243 struct ucan_can_msg can_msg; 244 245 /* CAN transmission complete 246 * (type == UCAN_IN_TX_COMPLETE) 247 */ 248 DECLARE_FLEX_ARRAY(struct ucan_tx_complete_entry_t, 249 can_tx_complete_msg); 250 } __aligned(0x4) msg; 251 } __packed __aligned(0x4); 252 253 /* Macros to calculate message lengths */ 254 #define UCAN_OUT_HDR_SIZE offsetof(struct ucan_message_out, msg) 255 256 #define UCAN_IN_HDR_SIZE offsetof(struct ucan_message_in, msg) 257 #define UCAN_IN_LEN(member) (UCAN_OUT_HDR_SIZE + sizeof(member)) 258 259 struct ucan_priv; 260 261 /* Context Information for transmission URBs */ 262 struct ucan_urb_context { 263 struct ucan_priv *up; 264 bool allocated; 265 }; 266 267 /* Information reported by the USB device */ 268 struct ucan_device_info { 269 struct can_bittiming_const bittiming_const; 270 u8 tx_fifo; 271 }; 272 273 /* Driver private data */ 274 struct ucan_priv { 275 /* must be the first member */ 276 struct can_priv can; 277 278 /* linux USB device structures */ 279 struct usb_device *udev; 280 struct usb_interface *intf; 281 struct net_device *netdev; 282 283 /* lock for can->echo_skb (used around 284 * can_put/get/free_echo_skb 285 */ 286 spinlock_t echo_skb_lock; 287 288 /* usb device information information */ 289 u8 intf_index; 290 u8 in_ep_addr; 291 u8 out_ep_addr; 292 u16 in_ep_size; 293 294 /* transmission and reception buffers */ 295 struct usb_anchor rx_urbs; 296 struct usb_anchor tx_urbs; 297 298 union ucan_ctl_payload *ctl_msg_buffer; 299 struct ucan_device_info device_info; 300 301 /* transmission control information and locks */ 302 spinlock_t context_lock; 303 unsigned int available_tx_urbs; 304 struct ucan_urb_context *context_array; 305 }; 306 307 static u8 ucan_can_cc_dlc2len(struct ucan_can_msg *msg, u16 len) 308 { 309 if (le32_to_cpu(msg->id) & CAN_RTR_FLAG) 310 return can_cc_dlc2len(msg->dlc); 311 else 312 return can_cc_dlc2len(len - (UCAN_IN_HDR_SIZE + sizeof(msg->id))); 313 } 314 315 static void ucan_release_context_array(struct ucan_priv *up) 316 { 317 if (!up->context_array) 318 return; 319 320 /* lock is not needed because, driver is currently opening or closing */ 321 up->available_tx_urbs = 0; 322 323 kfree(up->context_array); 324 up->context_array = NULL; 325 } 326 327 static int ucan_alloc_context_array(struct ucan_priv *up) 328 { 329 int i; 330 331 /* release contexts if any */ 332 ucan_release_context_array(up); 333 334 up->context_array = kcalloc(up->device_info.tx_fifo, 335 sizeof(*up->context_array), 336 GFP_KERNEL); 337 if (!up->context_array) { 338 netdev_err(up->netdev, 339 "Not enough memory to allocate tx contexts\n"); 340 return -ENOMEM; 341 } 342 343 for (i = 0; i < up->device_info.tx_fifo; i++) { 344 up->context_array[i].allocated = false; 345 up->context_array[i].up = up; 346 } 347 348 /* lock is not needed because, driver is currently opening */ 349 up->available_tx_urbs = up->device_info.tx_fifo; 350 351 return 0; 352 } 353 354 static struct ucan_urb_context *ucan_alloc_context(struct ucan_priv *up) 355 { 356 int i; 357 unsigned long flags; 358 struct ucan_urb_context *ret = NULL; 359 360 if (WARN_ON_ONCE(!up->context_array)) 361 return NULL; 362 363 /* execute context operation atomically */ 364 spin_lock_irqsave(&up->context_lock, flags); 365 366 for (i = 0; i < up->device_info.tx_fifo; i++) { 367 if (!up->context_array[i].allocated) { 368 /* update context */ 369 ret = &up->context_array[i]; 370 up->context_array[i].allocated = true; 371 372 /* stop queue if necessary */ 373 up->available_tx_urbs--; 374 if (!up->available_tx_urbs) 375 netif_stop_queue(up->netdev); 376 377 break; 378 } 379 } 380 381 spin_unlock_irqrestore(&up->context_lock, flags); 382 return ret; 383 } 384 385 static bool ucan_release_context(struct ucan_priv *up, 386 struct ucan_urb_context *ctx) 387 { 388 unsigned long flags; 389 bool ret = false; 390 391 if (WARN_ON_ONCE(!up->context_array)) 392 return false; 393 394 /* execute context operation atomically */ 395 spin_lock_irqsave(&up->context_lock, flags); 396 397 /* context was not allocated, maybe the device sent garbage */ 398 if (ctx->allocated) { 399 ctx->allocated = false; 400 401 /* check if the queue needs to be woken */ 402 if (!up->available_tx_urbs) 403 netif_wake_queue(up->netdev); 404 up->available_tx_urbs++; 405 406 ret = true; 407 } 408 409 spin_unlock_irqrestore(&up->context_lock, flags); 410 return ret; 411 } 412 413 static int ucan_ctrl_command_out(struct ucan_priv *up, 414 u8 cmd, u16 subcmd, u16 datalen) 415 { 416 return usb_control_msg(up->udev, 417 usb_sndctrlpipe(up->udev, 0), 418 cmd, 419 USB_DIR_OUT | USB_TYPE_VENDOR | 420 USB_RECIP_INTERFACE, 421 subcmd, 422 up->intf_index, 423 up->ctl_msg_buffer, 424 datalen, 425 UCAN_USB_CTL_PIPE_TIMEOUT); 426 } 427 428 static int ucan_device_request_in(struct ucan_priv *up, 429 u8 cmd, u16 subcmd, u16 datalen) 430 { 431 return usb_control_msg(up->udev, 432 usb_rcvctrlpipe(up->udev, 0), 433 cmd, 434 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE, 435 subcmd, 436 0, 437 up->ctl_msg_buffer, 438 datalen, 439 UCAN_USB_CTL_PIPE_TIMEOUT); 440 } 441 442 /* Parse the device information structure reported by the device and 443 * setup private variables accordingly 444 */ 445 static void ucan_parse_device_info(struct ucan_priv *up, 446 struct ucan_ctl_cmd_device_info *device_info) 447 { 448 struct can_bittiming_const *bittiming = 449 &up->device_info.bittiming_const; 450 u16 ctrlmodes; 451 452 /* store the data */ 453 up->can.clock.freq = le32_to_cpu(device_info->freq); 454 up->device_info.tx_fifo = device_info->tx_fifo; 455 strcpy(bittiming->name, "ucan"); 456 bittiming->tseg1_min = device_info->tseg1_min; 457 bittiming->tseg1_max = device_info->tseg1_max; 458 bittiming->tseg2_min = device_info->tseg2_min; 459 bittiming->tseg2_max = device_info->tseg2_max; 460 bittiming->sjw_max = device_info->sjw_max; 461 bittiming->brp_min = le32_to_cpu(device_info->brp_min); 462 bittiming->brp_max = le32_to_cpu(device_info->brp_max); 463 bittiming->brp_inc = le16_to_cpu(device_info->brp_inc); 464 465 ctrlmodes = le16_to_cpu(device_info->ctrlmodes); 466 467 up->can.ctrlmode_supported = 0; 468 469 if (ctrlmodes & UCAN_MODE_LOOPBACK) 470 up->can.ctrlmode_supported |= CAN_CTRLMODE_LOOPBACK; 471 if (ctrlmodes & UCAN_MODE_SILENT) 472 up->can.ctrlmode_supported |= CAN_CTRLMODE_LISTENONLY; 473 if (ctrlmodes & UCAN_MODE_3_SAMPLES) 474 up->can.ctrlmode_supported |= CAN_CTRLMODE_3_SAMPLES; 475 if (ctrlmodes & UCAN_MODE_ONE_SHOT) 476 up->can.ctrlmode_supported |= CAN_CTRLMODE_ONE_SHOT; 477 if (ctrlmodes & UCAN_MODE_BERR_REPORT) 478 up->can.ctrlmode_supported |= CAN_CTRLMODE_BERR_REPORTING; 479 } 480 481 /* Handle a CAN error frame that we have received from the device. 482 * Returns true if the can state has changed. 483 */ 484 static bool ucan_handle_error_frame(struct ucan_priv *up, 485 struct ucan_message_in *m, 486 canid_t canid) 487 { 488 enum can_state new_state = up->can.state; 489 struct net_device_stats *net_stats = &up->netdev->stats; 490 struct can_device_stats *can_stats = &up->can.can_stats; 491 492 if (canid & CAN_ERR_LOSTARB) 493 can_stats->arbitration_lost++; 494 495 if (canid & CAN_ERR_BUSERROR) 496 can_stats->bus_error++; 497 498 if (canid & CAN_ERR_ACK) 499 net_stats->tx_errors++; 500 501 if (canid & CAN_ERR_BUSOFF) 502 new_state = CAN_STATE_BUS_OFF; 503 504 /* controller problems, details in data[1] */ 505 if (canid & CAN_ERR_CRTL) { 506 u8 d1 = m->msg.can_msg.data[1]; 507 508 if (d1 & CAN_ERR_CRTL_RX_OVERFLOW) 509 net_stats->rx_over_errors++; 510 511 /* controller state bits: if multiple are set the worst wins */ 512 if (d1 & CAN_ERR_CRTL_ACTIVE) 513 new_state = CAN_STATE_ERROR_ACTIVE; 514 515 if (d1 & (CAN_ERR_CRTL_RX_WARNING | CAN_ERR_CRTL_TX_WARNING)) 516 new_state = CAN_STATE_ERROR_WARNING; 517 518 if (d1 & (CAN_ERR_CRTL_RX_PASSIVE | CAN_ERR_CRTL_TX_PASSIVE)) 519 new_state = CAN_STATE_ERROR_PASSIVE; 520 } 521 522 /* protocol error, details in data[2] */ 523 if (canid & CAN_ERR_PROT) { 524 u8 d2 = m->msg.can_msg.data[2]; 525 526 if (d2 & CAN_ERR_PROT_TX) 527 net_stats->tx_errors++; 528 else 529 net_stats->rx_errors++; 530 } 531 532 /* no state change - we are done */ 533 if (up->can.state == new_state) 534 return false; 535 536 /* we switched into a better state */ 537 if (up->can.state > new_state) { 538 up->can.state = new_state; 539 return true; 540 } 541 542 /* we switched into a worse state */ 543 up->can.state = new_state; 544 switch (new_state) { 545 case CAN_STATE_BUS_OFF: 546 can_stats->bus_off++; 547 can_bus_off(up->netdev); 548 break; 549 case CAN_STATE_ERROR_PASSIVE: 550 can_stats->error_passive++; 551 break; 552 case CAN_STATE_ERROR_WARNING: 553 can_stats->error_warning++; 554 break; 555 default: 556 break; 557 } 558 return true; 559 } 560 561 /* Callback on reception of a can frame via the IN endpoint 562 * 563 * This function allocates an skb and transferres it to the Linux 564 * network stack 565 */ 566 static void ucan_rx_can_msg(struct ucan_priv *up, struct ucan_message_in *m) 567 { 568 int len; 569 canid_t canid; 570 struct can_frame *cf; 571 struct sk_buff *skb; 572 struct net_device_stats *stats = &up->netdev->stats; 573 574 /* get the contents of the length field */ 575 len = le16_to_cpu(m->len); 576 577 /* check sanity */ 578 if (len < UCAN_IN_HDR_SIZE + sizeof(m->msg.can_msg.id)) { 579 netdev_warn(up->netdev, "invalid input message len: %d\n", len); 580 return; 581 } 582 583 /* handle error frames */ 584 canid = le32_to_cpu(m->msg.can_msg.id); 585 if (canid & CAN_ERR_FLAG) { 586 bool busstate_changed = ucan_handle_error_frame(up, m, canid); 587 588 /* if berr-reporting is off only state changes get through */ 589 if (!(up->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING) && 590 !busstate_changed) 591 return; 592 } else { 593 canid_t canid_mask; 594 /* compute the mask for canid */ 595 canid_mask = CAN_RTR_FLAG; 596 if (canid & CAN_EFF_FLAG) 597 canid_mask |= CAN_EFF_MASK | CAN_EFF_FLAG; 598 else 599 canid_mask |= CAN_SFF_MASK; 600 601 if (canid & ~canid_mask) 602 netdev_warn(up->netdev, 603 "unexpected bits set (canid %x, mask %x)", 604 canid, canid_mask); 605 606 canid &= canid_mask; 607 } 608 609 /* allocate skb */ 610 skb = alloc_can_skb(up->netdev, &cf); 611 if (!skb) 612 return; 613 614 /* fill the can frame */ 615 cf->can_id = canid; 616 617 /* compute DLC taking RTR_FLAG into account */ 618 cf->len = ucan_can_cc_dlc2len(&m->msg.can_msg, len); 619 620 /* copy the payload of non RTR frames */ 621 if (!(cf->can_id & CAN_RTR_FLAG) || (cf->can_id & CAN_ERR_FLAG)) 622 memcpy(cf->data, m->msg.can_msg.data, cf->len); 623 624 /* don't count error frames as real packets */ 625 if (!(cf->can_id & CAN_ERR_FLAG)) { 626 stats->rx_packets++; 627 if (!(cf->can_id & CAN_RTR_FLAG)) 628 stats->rx_bytes += cf->len; 629 } 630 631 /* pass it to Linux */ 632 netif_rx(skb); 633 } 634 635 /* callback indicating completed transmission */ 636 static void ucan_tx_complete_msg(struct ucan_priv *up, 637 struct ucan_message_in *m) 638 { 639 unsigned long flags; 640 u16 count, i; 641 u8 echo_index; 642 u16 len = le16_to_cpu(m->len); 643 644 struct ucan_urb_context *context; 645 646 if (len < UCAN_IN_HDR_SIZE || (len % 2 != 0)) { 647 netdev_err(up->netdev, "invalid tx complete length\n"); 648 return; 649 } 650 651 count = (len - UCAN_IN_HDR_SIZE) / 2; 652 for (i = 0; i < count; i++) { 653 /* we did not submit such echo ids */ 654 echo_index = m->msg.can_tx_complete_msg[i].echo_index; 655 if (echo_index >= up->device_info.tx_fifo) { 656 up->netdev->stats.tx_errors++; 657 netdev_err(up->netdev, 658 "invalid echo_index %d received\n", 659 echo_index); 660 continue; 661 } 662 663 /* gather information from the context */ 664 context = &up->context_array[echo_index]; 665 666 /* Release context and restart queue if necessary. 667 * Also check if the context was allocated 668 */ 669 if (!ucan_release_context(up, context)) 670 continue; 671 672 spin_lock_irqsave(&up->echo_skb_lock, flags); 673 if (m->msg.can_tx_complete_msg[i].flags & 674 UCAN_TX_COMPLETE_SUCCESS) { 675 /* update statistics */ 676 up->netdev->stats.tx_packets++; 677 up->netdev->stats.tx_bytes += 678 can_get_echo_skb(up->netdev, echo_index, NULL); 679 } else { 680 up->netdev->stats.tx_dropped++; 681 can_free_echo_skb(up->netdev, echo_index, NULL); 682 } 683 spin_unlock_irqrestore(&up->echo_skb_lock, flags); 684 } 685 } 686 687 /* callback on reception of a USB message */ 688 static void ucan_read_bulk_callback(struct urb *urb) 689 { 690 int ret; 691 int pos; 692 struct ucan_priv *up = urb->context; 693 struct net_device *netdev = up->netdev; 694 struct ucan_message_in *m; 695 696 /* the device is not up and the driver should not receive any 697 * data on the bulk in pipe 698 */ 699 if (WARN_ON(!up->context_array)) { 700 usb_free_coherent(up->udev, 701 up->in_ep_size, 702 urb->transfer_buffer, 703 urb->transfer_dma); 704 return; 705 } 706 707 /* check URB status */ 708 switch (urb->status) { 709 case 0: 710 break; 711 case -ENOENT: 712 case -EPIPE: 713 case -EPROTO: 714 case -ESHUTDOWN: 715 case -ETIME: 716 /* urb is not resubmitted -> free dma data */ 717 usb_free_coherent(up->udev, 718 up->in_ep_size, 719 urb->transfer_buffer, 720 urb->transfer_dma); 721 netdev_dbg(up->netdev, "not resubmitting urb; status: %d\n", 722 urb->status); 723 return; 724 default: 725 goto resubmit; 726 } 727 728 /* sanity check */ 729 if (!netif_device_present(netdev)) 730 return; 731 732 /* iterate over input */ 733 pos = 0; 734 while (pos < urb->actual_length) { 735 int len; 736 737 /* check sanity (length of header) */ 738 if ((urb->actual_length - pos) < UCAN_IN_HDR_SIZE) { 739 netdev_warn(up->netdev, 740 "invalid message (short; no hdr; l:%d)\n", 741 urb->actual_length); 742 goto resubmit; 743 } 744 745 /* setup the message address */ 746 m = (struct ucan_message_in *) 747 ((u8 *)urb->transfer_buffer + pos); 748 len = le16_to_cpu(m->len); 749 750 /* check sanity (length of content) */ 751 if (urb->actual_length - pos < len) { 752 netdev_warn(up->netdev, 753 "invalid message (short; no data; l:%d)\n", 754 urb->actual_length); 755 print_hex_dump(KERN_WARNING, 756 "raw data: ", 757 DUMP_PREFIX_ADDRESS, 758 16, 759 1, 760 urb->transfer_buffer, 761 urb->actual_length, 762 true); 763 764 goto resubmit; 765 } 766 767 switch (m->type) { 768 case UCAN_IN_RX: 769 ucan_rx_can_msg(up, m); 770 break; 771 case UCAN_IN_TX_COMPLETE: 772 ucan_tx_complete_msg(up, m); 773 break; 774 default: 775 netdev_warn(up->netdev, 776 "invalid message (type; t:%d)\n", 777 m->type); 778 break; 779 } 780 781 /* proceed to next message */ 782 pos += len; 783 /* align to 4 byte boundary */ 784 pos = round_up(pos, 4); 785 } 786 787 resubmit: 788 /* resubmit urb when done */ 789 usb_fill_bulk_urb(urb, up->udev, 790 usb_rcvbulkpipe(up->udev, 791 up->in_ep_addr), 792 urb->transfer_buffer, 793 up->in_ep_size, 794 ucan_read_bulk_callback, 795 up); 796 797 usb_anchor_urb(urb, &up->rx_urbs); 798 ret = usb_submit_urb(urb, GFP_ATOMIC); 799 800 if (ret < 0) { 801 netdev_err(up->netdev, 802 "failed resubmitting read bulk urb: %d\n", 803 ret); 804 805 usb_unanchor_urb(urb); 806 usb_free_coherent(up->udev, 807 up->in_ep_size, 808 urb->transfer_buffer, 809 urb->transfer_dma); 810 811 if (ret == -ENODEV) 812 netif_device_detach(netdev); 813 } 814 } 815 816 /* callback after transmission of a USB message */ 817 static void ucan_write_bulk_callback(struct urb *urb) 818 { 819 unsigned long flags; 820 struct ucan_priv *up; 821 struct ucan_urb_context *context = urb->context; 822 823 /* get the urb context */ 824 if (WARN_ON_ONCE(!context)) 825 return; 826 827 /* free up our allocated buffer */ 828 usb_free_coherent(urb->dev, 829 sizeof(struct ucan_message_out), 830 urb->transfer_buffer, 831 urb->transfer_dma); 832 833 up = context->up; 834 if (WARN_ON_ONCE(!up)) 835 return; 836 837 /* sanity check */ 838 if (!netif_device_present(up->netdev)) 839 return; 840 841 /* transmission failed (USB - the device will not send a TX complete) */ 842 if (urb->status) { 843 netdev_warn(up->netdev, 844 "failed to transmit USB message to device: %d\n", 845 urb->status); 846 847 /* update counters an cleanup */ 848 spin_lock_irqsave(&up->echo_skb_lock, flags); 849 can_free_echo_skb(up->netdev, context - up->context_array, NULL); 850 spin_unlock_irqrestore(&up->echo_skb_lock, flags); 851 852 up->netdev->stats.tx_dropped++; 853 854 /* release context and restart the queue if necessary */ 855 if (!ucan_release_context(up, context)) 856 netdev_err(up->netdev, 857 "urb failed, failed to release context\n"); 858 } 859 } 860 861 static void ucan_cleanup_rx_urbs(struct ucan_priv *up, struct urb **urbs) 862 { 863 int i; 864 865 for (i = 0; i < UCAN_MAX_RX_URBS; i++) { 866 if (urbs[i]) { 867 usb_unanchor_urb(urbs[i]); 868 usb_free_coherent(up->udev, 869 up->in_ep_size, 870 urbs[i]->transfer_buffer, 871 urbs[i]->transfer_dma); 872 usb_free_urb(urbs[i]); 873 } 874 } 875 876 memset(urbs, 0, sizeof(*urbs) * UCAN_MAX_RX_URBS); 877 } 878 879 static int ucan_prepare_and_anchor_rx_urbs(struct ucan_priv *up, 880 struct urb **urbs) 881 { 882 int i; 883 884 memset(urbs, 0, sizeof(*urbs) * UCAN_MAX_RX_URBS); 885 886 for (i = 0; i < UCAN_MAX_RX_URBS; i++) { 887 void *buf; 888 889 urbs[i] = usb_alloc_urb(0, GFP_KERNEL); 890 if (!urbs[i]) 891 goto err; 892 893 buf = usb_alloc_coherent(up->udev, 894 up->in_ep_size, 895 GFP_KERNEL, &urbs[i]->transfer_dma); 896 if (!buf) { 897 /* cleanup this urb */ 898 usb_free_urb(urbs[i]); 899 urbs[i] = NULL; 900 goto err; 901 } 902 903 usb_fill_bulk_urb(urbs[i], up->udev, 904 usb_rcvbulkpipe(up->udev, 905 up->in_ep_addr), 906 buf, 907 up->in_ep_size, 908 ucan_read_bulk_callback, 909 up); 910 911 urbs[i]->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; 912 913 usb_anchor_urb(urbs[i], &up->rx_urbs); 914 } 915 return 0; 916 917 err: 918 /* cleanup other unsubmitted urbs */ 919 ucan_cleanup_rx_urbs(up, urbs); 920 return -ENOMEM; 921 } 922 923 /* Submits rx urbs with the semantic: Either submit all, or cleanup 924 * everything. I case of errors submitted urbs are killed and all urbs in 925 * the array are freed. I case of no errors every entry in the urb 926 * array is set to NULL. 927 */ 928 static int ucan_submit_rx_urbs(struct ucan_priv *up, struct urb **urbs) 929 { 930 int i, ret; 931 932 /* Iterate over all urbs to submit. On success remove the urb 933 * from the list. 934 */ 935 for (i = 0; i < UCAN_MAX_RX_URBS; i++) { 936 ret = usb_submit_urb(urbs[i], GFP_KERNEL); 937 if (ret) { 938 netdev_err(up->netdev, 939 "could not submit urb; code: %d\n", 940 ret); 941 goto err; 942 } 943 944 /* Anchor URB and drop reference, USB core will take 945 * care of freeing it 946 */ 947 usb_free_urb(urbs[i]); 948 urbs[i] = NULL; 949 } 950 return 0; 951 952 err: 953 /* Cleanup unsubmitted urbs */ 954 ucan_cleanup_rx_urbs(up, urbs); 955 956 /* Kill urbs that are already submitted */ 957 usb_kill_anchored_urbs(&up->rx_urbs); 958 959 return ret; 960 } 961 962 /* Open the network device */ 963 static int ucan_open(struct net_device *netdev) 964 { 965 int ret, ret_cleanup; 966 u16 ctrlmode; 967 struct urb *urbs[UCAN_MAX_RX_URBS]; 968 struct ucan_priv *up = netdev_priv(netdev); 969 970 ret = ucan_alloc_context_array(up); 971 if (ret) 972 return ret; 973 974 /* Allocate and prepare IN URBS - allocated and anchored 975 * urbs are stored in urbs[] for clean 976 */ 977 ret = ucan_prepare_and_anchor_rx_urbs(up, urbs); 978 if (ret) 979 goto err_contexts; 980 981 /* Check the control mode */ 982 ctrlmode = 0; 983 if (up->can.ctrlmode & CAN_CTRLMODE_LOOPBACK) 984 ctrlmode |= UCAN_MODE_LOOPBACK; 985 if (up->can.ctrlmode & CAN_CTRLMODE_LISTENONLY) 986 ctrlmode |= UCAN_MODE_SILENT; 987 if (up->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES) 988 ctrlmode |= UCAN_MODE_3_SAMPLES; 989 if (up->can.ctrlmode & CAN_CTRLMODE_ONE_SHOT) 990 ctrlmode |= UCAN_MODE_ONE_SHOT; 991 992 /* Enable this in any case - filtering is down within the 993 * receive path 994 */ 995 ctrlmode |= UCAN_MODE_BERR_REPORT; 996 up->ctl_msg_buffer->cmd_start.mode = cpu_to_le16(ctrlmode); 997 998 /* Driver is ready to receive data - start the USB device */ 999 ret = ucan_ctrl_command_out(up, UCAN_COMMAND_START, 0, 2); 1000 if (ret < 0) { 1001 netdev_err(up->netdev, 1002 "could not start device, code: %d\n", 1003 ret); 1004 goto err_reset; 1005 } 1006 1007 /* Call CAN layer open */ 1008 ret = open_candev(netdev); 1009 if (ret) 1010 goto err_stop; 1011 1012 /* Driver is ready to receive data. Submit RX URBS */ 1013 ret = ucan_submit_rx_urbs(up, urbs); 1014 if (ret) 1015 goto err_stop; 1016 1017 up->can.state = CAN_STATE_ERROR_ACTIVE; 1018 1019 /* Start the network queue */ 1020 netif_start_queue(netdev); 1021 1022 return 0; 1023 1024 err_stop: 1025 /* The device have started already stop it */ 1026 ret_cleanup = ucan_ctrl_command_out(up, UCAN_COMMAND_STOP, 0, 0); 1027 if (ret_cleanup < 0) 1028 netdev_err(up->netdev, 1029 "could not stop device, code: %d\n", 1030 ret_cleanup); 1031 1032 err_reset: 1033 /* The device might have received data, reset it for 1034 * consistent state 1035 */ 1036 ret_cleanup = ucan_ctrl_command_out(up, UCAN_COMMAND_RESET, 0, 0); 1037 if (ret_cleanup < 0) 1038 netdev_err(up->netdev, 1039 "could not reset device, code: %d\n", 1040 ret_cleanup); 1041 1042 /* clean up unsubmitted urbs */ 1043 ucan_cleanup_rx_urbs(up, urbs); 1044 1045 err_contexts: 1046 ucan_release_context_array(up); 1047 return ret; 1048 } 1049 1050 static struct urb *ucan_prepare_tx_urb(struct ucan_priv *up, 1051 struct ucan_urb_context *context, 1052 struct can_frame *cf, 1053 u8 echo_index) 1054 { 1055 int mlen; 1056 struct urb *urb; 1057 struct ucan_message_out *m; 1058 1059 /* create a URB, and a buffer for it, and copy the data to the URB */ 1060 urb = usb_alloc_urb(0, GFP_ATOMIC); 1061 if (!urb) { 1062 netdev_err(up->netdev, "no memory left for URBs\n"); 1063 return NULL; 1064 } 1065 1066 m = usb_alloc_coherent(up->udev, 1067 sizeof(struct ucan_message_out), 1068 GFP_ATOMIC, 1069 &urb->transfer_dma); 1070 if (!m) { 1071 netdev_err(up->netdev, "no memory left for USB buffer\n"); 1072 usb_free_urb(urb); 1073 return NULL; 1074 } 1075 1076 /* build the USB message */ 1077 m->type = UCAN_OUT_TX; 1078 m->msg.can_msg.id = cpu_to_le32(cf->can_id); 1079 1080 if (cf->can_id & CAN_RTR_FLAG) { 1081 mlen = UCAN_OUT_HDR_SIZE + 1082 offsetof(struct ucan_can_msg, dlc) + 1083 sizeof(m->msg.can_msg.dlc); 1084 m->msg.can_msg.dlc = cf->len; 1085 } else { 1086 mlen = UCAN_OUT_HDR_SIZE + 1087 sizeof(m->msg.can_msg.id) + cf->len; 1088 memcpy(m->msg.can_msg.data, cf->data, cf->len); 1089 } 1090 m->len = cpu_to_le16(mlen); 1091 1092 m->subtype = echo_index; 1093 1094 /* build the urb */ 1095 usb_fill_bulk_urb(urb, up->udev, 1096 usb_sndbulkpipe(up->udev, 1097 up->out_ep_addr), 1098 m, mlen, ucan_write_bulk_callback, context); 1099 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; 1100 1101 return urb; 1102 } 1103 1104 static void ucan_clean_up_tx_urb(struct ucan_priv *up, struct urb *urb) 1105 { 1106 usb_free_coherent(up->udev, sizeof(struct ucan_message_out), 1107 urb->transfer_buffer, urb->transfer_dma); 1108 usb_free_urb(urb); 1109 } 1110 1111 /* callback when Linux needs to send a can frame */ 1112 static netdev_tx_t ucan_start_xmit(struct sk_buff *skb, 1113 struct net_device *netdev) 1114 { 1115 unsigned long flags; 1116 int ret; 1117 u8 echo_index; 1118 struct urb *urb; 1119 struct ucan_urb_context *context; 1120 struct ucan_priv *up = netdev_priv(netdev); 1121 struct can_frame *cf = (struct can_frame *)skb->data; 1122 1123 /* check skb */ 1124 if (can_dev_dropped_skb(netdev, skb)) 1125 return NETDEV_TX_OK; 1126 1127 /* allocate a context and slow down tx path, if fifo state is low */ 1128 context = ucan_alloc_context(up); 1129 echo_index = context - up->context_array; 1130 1131 if (WARN_ON_ONCE(!context)) 1132 return NETDEV_TX_BUSY; 1133 1134 /* prepare urb for transmission */ 1135 urb = ucan_prepare_tx_urb(up, context, cf, echo_index); 1136 if (!urb) 1137 goto drop; 1138 1139 /* put the skb on can loopback stack */ 1140 spin_lock_irqsave(&up->echo_skb_lock, flags); 1141 can_put_echo_skb(skb, up->netdev, echo_index, 0); 1142 spin_unlock_irqrestore(&up->echo_skb_lock, flags); 1143 1144 /* transmit it */ 1145 usb_anchor_urb(urb, &up->tx_urbs); 1146 ret = usb_submit_urb(urb, GFP_ATOMIC); 1147 1148 /* cleanup urb */ 1149 if (ret) { 1150 /* on error, clean up */ 1151 usb_unanchor_urb(urb); 1152 ucan_clean_up_tx_urb(up, urb); 1153 if (!ucan_release_context(up, context)) 1154 netdev_err(up->netdev, 1155 "xmit err: failed to release context\n"); 1156 1157 /* remove the skb from the echo stack - this also 1158 * frees the skb 1159 */ 1160 spin_lock_irqsave(&up->echo_skb_lock, flags); 1161 can_free_echo_skb(up->netdev, echo_index, NULL); 1162 spin_unlock_irqrestore(&up->echo_skb_lock, flags); 1163 1164 if (ret == -ENODEV) { 1165 netif_device_detach(up->netdev); 1166 } else { 1167 netdev_warn(up->netdev, 1168 "xmit err: failed to submit urb %d\n", 1169 ret); 1170 up->netdev->stats.tx_dropped++; 1171 } 1172 return NETDEV_TX_OK; 1173 } 1174 1175 netif_trans_update(netdev); 1176 1177 /* release ref, as we do not need the urb anymore */ 1178 usb_free_urb(urb); 1179 1180 return NETDEV_TX_OK; 1181 1182 drop: 1183 if (!ucan_release_context(up, context)) 1184 netdev_err(up->netdev, 1185 "xmit drop: failed to release context\n"); 1186 dev_kfree_skb(skb); 1187 up->netdev->stats.tx_dropped++; 1188 1189 return NETDEV_TX_OK; 1190 } 1191 1192 /* Device goes down 1193 * 1194 * Clean up used resources 1195 */ 1196 static int ucan_close(struct net_device *netdev) 1197 { 1198 int ret; 1199 struct ucan_priv *up = netdev_priv(netdev); 1200 1201 up->can.state = CAN_STATE_STOPPED; 1202 1203 /* stop sending data */ 1204 usb_kill_anchored_urbs(&up->tx_urbs); 1205 1206 /* stop receiving data */ 1207 usb_kill_anchored_urbs(&up->rx_urbs); 1208 1209 /* stop and reset can device */ 1210 ret = ucan_ctrl_command_out(up, UCAN_COMMAND_STOP, 0, 0); 1211 if (ret < 0) 1212 netdev_err(up->netdev, 1213 "could not stop device, code: %d\n", 1214 ret); 1215 1216 ret = ucan_ctrl_command_out(up, UCAN_COMMAND_RESET, 0, 0); 1217 if (ret < 0) 1218 netdev_err(up->netdev, 1219 "could not reset device, code: %d\n", 1220 ret); 1221 1222 netif_stop_queue(netdev); 1223 1224 ucan_release_context_array(up); 1225 1226 close_candev(up->netdev); 1227 return 0; 1228 } 1229 1230 /* CAN driver callbacks */ 1231 static const struct net_device_ops ucan_netdev_ops = { 1232 .ndo_open = ucan_open, 1233 .ndo_stop = ucan_close, 1234 .ndo_start_xmit = ucan_start_xmit, 1235 .ndo_change_mtu = can_change_mtu, 1236 }; 1237 1238 static const struct ethtool_ops ucan_ethtool_ops = { 1239 .get_ts_info = ethtool_op_get_ts_info, 1240 }; 1241 1242 /* Request to set bittiming 1243 * 1244 * This function generates an USB set bittiming message and transmits 1245 * it to the device 1246 */ 1247 static int ucan_set_bittiming(struct net_device *netdev) 1248 { 1249 int ret; 1250 struct ucan_priv *up = netdev_priv(netdev); 1251 struct ucan_ctl_cmd_set_bittiming *cmd_set_bittiming; 1252 1253 cmd_set_bittiming = &up->ctl_msg_buffer->cmd_set_bittiming; 1254 cmd_set_bittiming->tq = cpu_to_le32(up->can.bittiming.tq); 1255 cmd_set_bittiming->brp = cpu_to_le16(up->can.bittiming.brp); 1256 cmd_set_bittiming->sample_point = 1257 cpu_to_le16(up->can.bittiming.sample_point); 1258 cmd_set_bittiming->prop_seg = up->can.bittiming.prop_seg; 1259 cmd_set_bittiming->phase_seg1 = up->can.bittiming.phase_seg1; 1260 cmd_set_bittiming->phase_seg2 = up->can.bittiming.phase_seg2; 1261 cmd_set_bittiming->sjw = up->can.bittiming.sjw; 1262 1263 ret = ucan_ctrl_command_out(up, UCAN_COMMAND_SET_BITTIMING, 0, 1264 sizeof(*cmd_set_bittiming)); 1265 return (ret < 0) ? ret : 0; 1266 } 1267 1268 /* Restart the device to get it out of BUS-OFF state. 1269 * Called when the user runs "ip link set can1 type can restart". 1270 */ 1271 static int ucan_set_mode(struct net_device *netdev, enum can_mode mode) 1272 { 1273 int ret; 1274 unsigned long flags; 1275 struct ucan_priv *up = netdev_priv(netdev); 1276 1277 switch (mode) { 1278 case CAN_MODE_START: 1279 netdev_dbg(up->netdev, "restarting device\n"); 1280 1281 ret = ucan_ctrl_command_out(up, UCAN_COMMAND_RESTART, 0, 0); 1282 up->can.state = CAN_STATE_ERROR_ACTIVE; 1283 1284 /* check if queue can be restarted, 1285 * up->available_tx_urbs must be protected by the 1286 * lock 1287 */ 1288 spin_lock_irqsave(&up->context_lock, flags); 1289 1290 if (up->available_tx_urbs > 0) 1291 netif_wake_queue(up->netdev); 1292 1293 spin_unlock_irqrestore(&up->context_lock, flags); 1294 1295 return ret; 1296 default: 1297 return -EOPNOTSUPP; 1298 } 1299 } 1300 1301 /* Probe the device, reset it and gather general device information */ 1302 static int ucan_probe(struct usb_interface *intf, 1303 const struct usb_device_id *id) 1304 { 1305 int ret; 1306 int i; 1307 u32 protocol_version; 1308 struct usb_device *udev; 1309 struct net_device *netdev; 1310 struct usb_host_interface *iface_desc; 1311 struct ucan_priv *up; 1312 struct usb_endpoint_descriptor *ep; 1313 u16 in_ep_size; 1314 u16 out_ep_size; 1315 u8 in_ep_addr; 1316 u8 out_ep_addr; 1317 union ucan_ctl_payload *ctl_msg_buffer; 1318 char firmware_str[sizeof(union ucan_ctl_payload) + 1]; 1319 1320 udev = interface_to_usbdev(intf); 1321 1322 /* Stage 1 - Interface Parsing 1323 * --------------------------- 1324 * 1325 * Identifie the device USB interface descriptor and its 1326 * endpoints. Probing is aborted on errors. 1327 */ 1328 1329 /* check if the interface is sane */ 1330 iface_desc = intf->cur_altsetting; 1331 if (!iface_desc) 1332 return -ENODEV; 1333 1334 dev_info(&udev->dev, 1335 "%s: probing device on interface #%d\n", 1336 UCAN_DRIVER_NAME, 1337 iface_desc->desc.bInterfaceNumber); 1338 1339 /* interface sanity check */ 1340 if (iface_desc->desc.bNumEndpoints != 2) { 1341 dev_err(&udev->dev, 1342 "%s: invalid EP count (%d)", 1343 UCAN_DRIVER_NAME, iface_desc->desc.bNumEndpoints); 1344 goto err_firmware_needs_update; 1345 } 1346 1347 /* check interface endpoints */ 1348 in_ep_addr = 0; 1349 out_ep_addr = 0; 1350 in_ep_size = 0; 1351 out_ep_size = 0; 1352 for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) { 1353 ep = &iface_desc->endpoint[i].desc; 1354 1355 if (((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK) != 0) && 1356 ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == 1357 USB_ENDPOINT_XFER_BULK)) { 1358 /* In Endpoint */ 1359 in_ep_addr = ep->bEndpointAddress; 1360 in_ep_addr &= USB_ENDPOINT_NUMBER_MASK; 1361 in_ep_size = le16_to_cpu(ep->wMaxPacketSize); 1362 } else if (((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == 1363 0) && 1364 ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == 1365 USB_ENDPOINT_XFER_BULK)) { 1366 /* Out Endpoint */ 1367 out_ep_addr = ep->bEndpointAddress; 1368 out_ep_addr &= USB_ENDPOINT_NUMBER_MASK; 1369 out_ep_size = le16_to_cpu(ep->wMaxPacketSize); 1370 } 1371 } 1372 1373 /* check if interface is sane */ 1374 if (!in_ep_addr || !out_ep_addr) { 1375 dev_err(&udev->dev, "%s: invalid endpoint configuration\n", 1376 UCAN_DRIVER_NAME); 1377 goto err_firmware_needs_update; 1378 } 1379 if (in_ep_size < sizeof(struct ucan_message_in)) { 1380 dev_err(&udev->dev, "%s: invalid in_ep MaxPacketSize\n", 1381 UCAN_DRIVER_NAME); 1382 goto err_firmware_needs_update; 1383 } 1384 if (out_ep_size < sizeof(struct ucan_message_out)) { 1385 dev_err(&udev->dev, "%s: invalid out_ep MaxPacketSize\n", 1386 UCAN_DRIVER_NAME); 1387 goto err_firmware_needs_update; 1388 } 1389 1390 /* Stage 2 - Device Identification 1391 * ------------------------------- 1392 * 1393 * The device interface seems to be a ucan device. Do further 1394 * compatibility checks. On error probing is aborted, on 1395 * success this stage leaves the ctl_msg_buffer with the 1396 * reported contents of a GET_INFO command (supported 1397 * bittimings, tx_fifo depth). This information is used in 1398 * Stage 3 for the final driver initialisation. 1399 */ 1400 1401 /* Prepare Memory for control transfers */ 1402 ctl_msg_buffer = devm_kzalloc(&udev->dev, 1403 sizeof(union ucan_ctl_payload), 1404 GFP_KERNEL); 1405 if (!ctl_msg_buffer) { 1406 dev_err(&udev->dev, 1407 "%s: failed to allocate control pipe memory\n", 1408 UCAN_DRIVER_NAME); 1409 return -ENOMEM; 1410 } 1411 1412 /* get protocol version 1413 * 1414 * note: ucan_ctrl_command_* wrappers cannot be used yet 1415 * because `up` is initialised in Stage 3 1416 */ 1417 ret = usb_control_msg(udev, 1418 usb_rcvctrlpipe(udev, 0), 1419 UCAN_COMMAND_GET, 1420 USB_DIR_IN | USB_TYPE_VENDOR | 1421 USB_RECIP_INTERFACE, 1422 UCAN_COMMAND_GET_PROTOCOL_VERSION, 1423 iface_desc->desc.bInterfaceNumber, 1424 ctl_msg_buffer, 1425 sizeof(union ucan_ctl_payload), 1426 UCAN_USB_CTL_PIPE_TIMEOUT); 1427 1428 /* older firmware version do not support this command - those 1429 * are not supported by this drive 1430 */ 1431 if (ret != 4) { 1432 dev_err(&udev->dev, 1433 "%s: could not read protocol version, ret=%d\n", 1434 UCAN_DRIVER_NAME, ret); 1435 if (ret >= 0) 1436 ret = -EINVAL; 1437 goto err_firmware_needs_update; 1438 } 1439 1440 /* this driver currently supports protocol version 3 only */ 1441 protocol_version = 1442 le32_to_cpu(ctl_msg_buffer->cmd_get_protocol_version.version); 1443 if (protocol_version < UCAN_PROTOCOL_VERSION_MIN || 1444 protocol_version > UCAN_PROTOCOL_VERSION_MAX) { 1445 dev_err(&udev->dev, 1446 "%s: device protocol version %d is not supported\n", 1447 UCAN_DRIVER_NAME, protocol_version); 1448 goto err_firmware_needs_update; 1449 } 1450 1451 /* request the device information and store it in ctl_msg_buffer 1452 * 1453 * note: ucan_ctrl_command_* wrappers cannot be used yet 1454 * because `up` is initialised in Stage 3 1455 */ 1456 ret = usb_control_msg(udev, 1457 usb_rcvctrlpipe(udev, 0), 1458 UCAN_COMMAND_GET, 1459 USB_DIR_IN | USB_TYPE_VENDOR | 1460 USB_RECIP_INTERFACE, 1461 UCAN_COMMAND_GET_INFO, 1462 iface_desc->desc.bInterfaceNumber, 1463 ctl_msg_buffer, 1464 sizeof(ctl_msg_buffer->cmd_get_device_info), 1465 UCAN_USB_CTL_PIPE_TIMEOUT); 1466 1467 if (ret < 0) { 1468 dev_err(&udev->dev, "%s: failed to retrieve device info\n", 1469 UCAN_DRIVER_NAME); 1470 goto err_firmware_needs_update; 1471 } 1472 if (ret < sizeof(ctl_msg_buffer->cmd_get_device_info)) { 1473 dev_err(&udev->dev, "%s: device reported invalid device info\n", 1474 UCAN_DRIVER_NAME); 1475 goto err_firmware_needs_update; 1476 } 1477 if (ctl_msg_buffer->cmd_get_device_info.tx_fifo == 0) { 1478 dev_err(&udev->dev, 1479 "%s: device reported invalid tx-fifo size\n", 1480 UCAN_DRIVER_NAME); 1481 goto err_firmware_needs_update; 1482 } 1483 1484 /* Stage 3 - Driver Initialisation 1485 * ------------------------------- 1486 * 1487 * Register device to Linux, prepare private structures and 1488 * reset the device. 1489 */ 1490 1491 /* allocate driver resources */ 1492 netdev = alloc_candev(sizeof(struct ucan_priv), 1493 ctl_msg_buffer->cmd_get_device_info.tx_fifo); 1494 if (!netdev) { 1495 dev_err(&udev->dev, 1496 "%s: cannot allocate candev\n", UCAN_DRIVER_NAME); 1497 return -ENOMEM; 1498 } 1499 1500 up = netdev_priv(netdev); 1501 1502 /* initialize data */ 1503 up->udev = udev; 1504 up->intf = intf; 1505 up->netdev = netdev; 1506 up->intf_index = iface_desc->desc.bInterfaceNumber; 1507 up->in_ep_addr = in_ep_addr; 1508 up->out_ep_addr = out_ep_addr; 1509 up->in_ep_size = in_ep_size; 1510 up->ctl_msg_buffer = ctl_msg_buffer; 1511 up->context_array = NULL; 1512 up->available_tx_urbs = 0; 1513 1514 up->can.state = CAN_STATE_STOPPED; 1515 up->can.bittiming_const = &up->device_info.bittiming_const; 1516 up->can.do_set_bittiming = ucan_set_bittiming; 1517 up->can.do_set_mode = &ucan_set_mode; 1518 spin_lock_init(&up->context_lock); 1519 spin_lock_init(&up->echo_skb_lock); 1520 netdev->netdev_ops = &ucan_netdev_ops; 1521 netdev->ethtool_ops = &ucan_ethtool_ops; 1522 1523 usb_set_intfdata(intf, up); 1524 SET_NETDEV_DEV(netdev, &intf->dev); 1525 1526 /* parse device information 1527 * the data retrieved in Stage 2 is still available in 1528 * up->ctl_msg_buffer 1529 */ 1530 ucan_parse_device_info(up, &ctl_msg_buffer->cmd_get_device_info); 1531 1532 /* just print some device information - if available */ 1533 ret = ucan_device_request_in(up, UCAN_DEVICE_GET_FW_STRING, 0, 1534 sizeof(union ucan_ctl_payload)); 1535 if (ret > 0) { 1536 /* copy string while ensuring zero termination */ 1537 strncpy(firmware_str, up->ctl_msg_buffer->raw, 1538 sizeof(union ucan_ctl_payload)); 1539 firmware_str[sizeof(union ucan_ctl_payload)] = '\0'; 1540 } else { 1541 strcpy(firmware_str, "unknown"); 1542 } 1543 1544 /* device is compatible, reset it */ 1545 ret = ucan_ctrl_command_out(up, UCAN_COMMAND_RESET, 0, 0); 1546 if (ret < 0) 1547 goto err_free_candev; 1548 1549 init_usb_anchor(&up->rx_urbs); 1550 init_usb_anchor(&up->tx_urbs); 1551 1552 up->can.state = CAN_STATE_STOPPED; 1553 1554 /* register the device */ 1555 ret = register_candev(netdev); 1556 if (ret) 1557 goto err_free_candev; 1558 1559 /* initialisation complete, log device info */ 1560 netdev_info(up->netdev, "registered device\n"); 1561 netdev_info(up->netdev, "firmware string: %s\n", firmware_str); 1562 1563 /* success */ 1564 return 0; 1565 1566 err_free_candev: 1567 free_candev(netdev); 1568 return ret; 1569 1570 err_firmware_needs_update: 1571 dev_err(&udev->dev, 1572 "%s: probe failed; try to update the device firmware\n", 1573 UCAN_DRIVER_NAME); 1574 return -ENODEV; 1575 } 1576 1577 /* disconnect the device */ 1578 static void ucan_disconnect(struct usb_interface *intf) 1579 { 1580 struct ucan_priv *up = usb_get_intfdata(intf); 1581 1582 usb_set_intfdata(intf, NULL); 1583 1584 if (up) { 1585 unregister_candev(up->netdev); 1586 free_candev(up->netdev); 1587 } 1588 } 1589 1590 static struct usb_device_id ucan_table[] = { 1591 /* Mule (soldered onto compute modules) */ 1592 {USB_DEVICE_INTERFACE_NUMBER(0x2294, 0x425a, 0)}, 1593 /* Seal (standalone USB stick) */ 1594 {USB_DEVICE_INTERFACE_NUMBER(0x2294, 0x425b, 0)}, 1595 {} /* Terminating entry */ 1596 }; 1597 1598 MODULE_DEVICE_TABLE(usb, ucan_table); 1599 /* driver callbacks */ 1600 static struct usb_driver ucan_driver = { 1601 .name = UCAN_DRIVER_NAME, 1602 .probe = ucan_probe, 1603 .disconnect = ucan_disconnect, 1604 .id_table = ucan_table, 1605 }; 1606 1607 module_usb_driver(ucan_driver); 1608 1609 MODULE_LICENSE("GPL v2"); 1610 MODULE_AUTHOR("Martin Elshuber <martin.elshuber@theobroma-systems.com>"); 1611 MODULE_AUTHOR("Jakob Unterwurzacher <jakob.unterwurzacher@theobroma-systems.com>"); 1612 MODULE_DESCRIPTION("Driver for Theobroma Systems UCAN devices"); 1613