1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * dummy_hcd.c -- Dummy/Loopback USB host and device emulator driver. 4 * 5 * Maintainer: Alan Stern <stern@rowland.harvard.edu> 6 * 7 * Copyright (C) 2003 David Brownell 8 * Copyright (C) 2003-2005 Alan Stern 9 */ 10 11 12 /* 13 * This exposes a device side "USB gadget" API, driven by requests to a 14 * Linux-USB host controller driver. USB traffic is simulated; there's 15 * no need for USB hardware. Use this with two other drivers: 16 * 17 * - Gadget driver, responding to requests (device); 18 * - Host-side device driver, as already familiar in Linux. 19 * 20 * Having this all in one kernel can help some stages of development, 21 * bypassing some hardware (and driver) issues. UML could help too. 22 * 23 * Note: The emulation does not include isochronous transfers! 24 */ 25 26 #include <linux/module.h> 27 #include <linux/kernel.h> 28 #include <linux/delay.h> 29 #include <linux/ioport.h> 30 #include <linux/slab.h> 31 #include <linux/string_choices.h> 32 #include <linux/errno.h> 33 #include <linux/init.h> 34 #include <linux/hrtimer.h> 35 #include <linux/list.h> 36 #include <linux/interrupt.h> 37 #include <linux/platform_device.h> 38 #include <linux/usb.h> 39 #include <linux/usb/gadget.h> 40 #include <linux/usb/hcd.h> 41 #include <linux/scatterlist.h> 42 43 #include <asm/byteorder.h> 44 #include <linux/io.h> 45 #include <asm/irq.h> 46 #include <linux/unaligned.h> 47 48 #define DRIVER_DESC "USB Host+Gadget Emulator" 49 #define DRIVER_VERSION "02 May 2005" 50 51 #define POWER_BUDGET 500 /* in mA; use 8 for low-power port testing */ 52 #define POWER_BUDGET_3 900 /* in mA */ 53 54 #define DUMMY_TIMER_INT_NSECS 125000 /* 1 microframe */ 55 56 static const char driver_name[] = "dummy_hcd"; 57 static const char driver_desc[] = "USB Host+Gadget Emulator"; 58 59 static const char gadget_name[] = "dummy_udc"; 60 61 MODULE_DESCRIPTION(DRIVER_DESC); 62 MODULE_AUTHOR("David Brownell"); 63 MODULE_LICENSE("GPL"); 64 65 struct dummy_hcd_module_parameters { 66 bool is_super_speed; 67 bool is_high_speed; 68 unsigned int num; 69 }; 70 71 static struct dummy_hcd_module_parameters mod_data = { 72 .is_super_speed = false, 73 .is_high_speed = true, 74 .num = 1, 75 }; 76 module_param_named(is_super_speed, mod_data.is_super_speed, bool, S_IRUGO); 77 MODULE_PARM_DESC(is_super_speed, "true to simulate SuperSpeed connection"); 78 module_param_named(is_high_speed, mod_data.is_high_speed, bool, S_IRUGO); 79 MODULE_PARM_DESC(is_high_speed, "true to simulate HighSpeed connection"); 80 module_param_named(num, mod_data.num, uint, S_IRUGO); 81 MODULE_PARM_DESC(num, "number of emulated controllers"); 82 /*-------------------------------------------------------------------------*/ 83 84 /* gadget side driver data structures */ 85 struct dummy_ep { 86 struct list_head queue; 87 unsigned long last_io; /* jiffies timestamp */ 88 struct usb_gadget *gadget; 89 const struct usb_endpoint_descriptor *desc; 90 struct usb_ep ep; 91 unsigned halted:1; 92 unsigned wedged:1; 93 unsigned already_seen:1; 94 unsigned setup_stage:1; 95 unsigned stream_en:1; 96 }; 97 98 struct dummy_request { 99 struct list_head queue; /* ep's requests */ 100 struct usb_request req; 101 }; 102 103 static inline struct dummy_ep *usb_ep_to_dummy_ep(struct usb_ep *_ep) 104 { 105 return container_of(_ep, struct dummy_ep, ep); 106 } 107 108 static inline struct dummy_request *usb_request_to_dummy_request 109 (struct usb_request *_req) 110 { 111 return container_of(_req, struct dummy_request, req); 112 } 113 114 /*-------------------------------------------------------------------------*/ 115 116 /* 117 * Every device has ep0 for control requests, plus up to 30 more endpoints, 118 * in one of two types: 119 * 120 * - Configurable: direction (in/out), type (bulk, iso, etc), and endpoint 121 * number can be changed. Names like "ep-a" are used for this type. 122 * 123 * - Fixed Function: in other cases. some characteristics may be mutable; 124 * that'd be hardware-specific. Names like "ep12out-bulk" are used. 125 * 126 * Gadget drivers are responsible for not setting up conflicting endpoint 127 * configurations, illegal or unsupported packet lengths, and so on. 128 */ 129 130 static const char ep0name[] = "ep0"; 131 132 static const struct { 133 const char *name; 134 const struct usb_ep_caps caps; 135 } ep_info[] = { 136 #define EP_INFO(_name, _caps) \ 137 { \ 138 .name = _name, \ 139 .caps = _caps, \ 140 } 141 142 /* we don't provide isochronous endpoints since we don't support them */ 143 #define TYPE_BULK_OR_INT (USB_EP_CAPS_TYPE_BULK | USB_EP_CAPS_TYPE_INT) 144 145 /* everyone has ep0 */ 146 EP_INFO(ep0name, 147 USB_EP_CAPS(USB_EP_CAPS_TYPE_CONTROL, USB_EP_CAPS_DIR_ALL)), 148 /* act like a pxa250: fifteen fixed function endpoints */ 149 EP_INFO("ep1in-bulk", 150 USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_IN)), 151 EP_INFO("ep2out-bulk", 152 USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_OUT)), 153 /* 154 EP_INFO("ep3in-iso", 155 USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO, USB_EP_CAPS_DIR_IN)), 156 EP_INFO("ep4out-iso", 157 USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO, USB_EP_CAPS_DIR_OUT)), 158 */ 159 EP_INFO("ep5in-int", 160 USB_EP_CAPS(USB_EP_CAPS_TYPE_INT, USB_EP_CAPS_DIR_IN)), 161 EP_INFO("ep6in-bulk", 162 USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_IN)), 163 EP_INFO("ep7out-bulk", 164 USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_OUT)), 165 /* 166 EP_INFO("ep8in-iso", 167 USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO, USB_EP_CAPS_DIR_IN)), 168 EP_INFO("ep9out-iso", 169 USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO, USB_EP_CAPS_DIR_OUT)), 170 */ 171 EP_INFO("ep10in-int", 172 USB_EP_CAPS(USB_EP_CAPS_TYPE_INT, USB_EP_CAPS_DIR_IN)), 173 EP_INFO("ep11in-bulk", 174 USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_IN)), 175 EP_INFO("ep12out-bulk", 176 USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_OUT)), 177 /* 178 EP_INFO("ep13in-iso", 179 USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO, USB_EP_CAPS_DIR_IN)), 180 EP_INFO("ep14out-iso", 181 USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO, USB_EP_CAPS_DIR_OUT)), 182 */ 183 EP_INFO("ep15in-int", 184 USB_EP_CAPS(USB_EP_CAPS_TYPE_INT, USB_EP_CAPS_DIR_IN)), 185 186 /* or like sa1100: two fixed function endpoints */ 187 EP_INFO("ep1out-bulk", 188 USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_OUT)), 189 EP_INFO("ep2in-bulk", 190 USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_IN)), 191 192 /* and now some generic EPs so we have enough in multi config */ 193 EP_INFO("ep-aout", 194 USB_EP_CAPS(TYPE_BULK_OR_INT, USB_EP_CAPS_DIR_OUT)), 195 EP_INFO("ep-bin", 196 USB_EP_CAPS(TYPE_BULK_OR_INT, USB_EP_CAPS_DIR_IN)), 197 EP_INFO("ep-cout", 198 USB_EP_CAPS(TYPE_BULK_OR_INT, USB_EP_CAPS_DIR_OUT)), 199 EP_INFO("ep-dout", 200 USB_EP_CAPS(TYPE_BULK_OR_INT, USB_EP_CAPS_DIR_OUT)), 201 EP_INFO("ep-ein", 202 USB_EP_CAPS(TYPE_BULK_OR_INT, USB_EP_CAPS_DIR_IN)), 203 EP_INFO("ep-fout", 204 USB_EP_CAPS(TYPE_BULK_OR_INT, USB_EP_CAPS_DIR_OUT)), 205 EP_INFO("ep-gin", 206 USB_EP_CAPS(TYPE_BULK_OR_INT, USB_EP_CAPS_DIR_IN)), 207 EP_INFO("ep-hout", 208 USB_EP_CAPS(TYPE_BULK_OR_INT, USB_EP_CAPS_DIR_OUT)), 209 EP_INFO("ep-iout", 210 USB_EP_CAPS(TYPE_BULK_OR_INT, USB_EP_CAPS_DIR_OUT)), 211 EP_INFO("ep-jin", 212 USB_EP_CAPS(TYPE_BULK_OR_INT, USB_EP_CAPS_DIR_IN)), 213 EP_INFO("ep-kout", 214 USB_EP_CAPS(TYPE_BULK_OR_INT, USB_EP_CAPS_DIR_OUT)), 215 EP_INFO("ep-lin", 216 USB_EP_CAPS(TYPE_BULK_OR_INT, USB_EP_CAPS_DIR_IN)), 217 EP_INFO("ep-mout", 218 USB_EP_CAPS(TYPE_BULK_OR_INT, USB_EP_CAPS_DIR_OUT)), 219 220 #undef EP_INFO 221 }; 222 223 #define DUMMY_ENDPOINTS ARRAY_SIZE(ep_info) 224 225 /*-------------------------------------------------------------------------*/ 226 227 #define FIFO_SIZE 64 228 229 struct urbp { 230 struct urb *urb; 231 struct list_head urbp_list; 232 struct sg_mapping_iter miter; 233 u32 miter_started; 234 }; 235 236 237 enum dummy_rh_state { 238 DUMMY_RH_RESET, 239 DUMMY_RH_SUSPENDED, 240 DUMMY_RH_RUNNING 241 }; 242 243 struct dummy_hcd { 244 struct dummy *dum; 245 enum dummy_rh_state rh_state; 246 struct hrtimer timer; 247 u32 port_status; 248 u32 old_status; 249 unsigned long re_timeout; 250 251 struct usb_device *udev; 252 struct list_head urbp_list; 253 struct urbp *next_frame_urbp; 254 255 u32 stream_en_ep; 256 u8 num_stream[30 / 2]; 257 258 unsigned timer_pending:1; 259 unsigned active:1; 260 unsigned old_active:1; 261 unsigned resuming:1; 262 }; 263 264 struct dummy { 265 spinlock_t lock; 266 267 /* 268 * DEVICE/GADGET side support 269 */ 270 struct dummy_ep ep[DUMMY_ENDPOINTS]; 271 int address; 272 int callback_usage; 273 struct usb_gadget gadget; 274 struct usb_gadget_driver *driver; 275 struct dummy_request fifo_req; 276 u8 fifo_buf[FIFO_SIZE]; 277 u16 devstatus; 278 unsigned ints_enabled:1; 279 unsigned udc_suspended:1; 280 unsigned pullup:1; 281 282 /* 283 * HOST side support 284 */ 285 struct dummy_hcd *hs_hcd; 286 struct dummy_hcd *ss_hcd; 287 }; 288 289 static inline struct dummy_hcd *hcd_to_dummy_hcd(struct usb_hcd *hcd) 290 { 291 return (struct dummy_hcd *) (hcd->hcd_priv); 292 } 293 294 static inline struct usb_hcd *dummy_hcd_to_hcd(struct dummy_hcd *dum) 295 { 296 return container_of((void *) dum, struct usb_hcd, hcd_priv); 297 } 298 299 static inline struct device *dummy_dev(struct dummy_hcd *dum) 300 { 301 return dummy_hcd_to_hcd(dum)->self.controller; 302 } 303 304 static inline struct device *udc_dev(struct dummy *dum) 305 { 306 return dum->gadget.dev.parent; 307 } 308 309 static inline struct dummy *ep_to_dummy(struct dummy_ep *ep) 310 { 311 return container_of(ep->gadget, struct dummy, gadget); 312 } 313 314 static inline struct dummy_hcd *gadget_to_dummy_hcd(struct usb_gadget *gadget) 315 { 316 struct dummy *dum = container_of(gadget, struct dummy, gadget); 317 if (dum->gadget.speed == USB_SPEED_SUPER) 318 return dum->ss_hcd; 319 else 320 return dum->hs_hcd; 321 } 322 323 static inline struct dummy *gadget_dev_to_dummy(struct device *dev) 324 { 325 return container_of(dev, struct dummy, gadget.dev); 326 } 327 328 /*-------------------------------------------------------------------------*/ 329 330 /* DEVICE/GADGET SIDE UTILITY ROUTINES */ 331 332 /* called with spinlock held */ 333 static void nuke(struct dummy *dum, struct dummy_ep *ep) 334 { 335 while (!list_empty(&ep->queue)) { 336 struct dummy_request *req; 337 338 req = list_entry(ep->queue.next, struct dummy_request, queue); 339 list_del_init(&req->queue); 340 req->req.status = -ESHUTDOWN; 341 342 spin_unlock(&dum->lock); 343 usb_gadget_giveback_request(&ep->ep, &req->req); 344 spin_lock(&dum->lock); 345 } 346 } 347 348 /* caller must hold lock */ 349 static void stop_activity(struct dummy *dum) 350 { 351 int i; 352 353 /* prevent any more requests */ 354 dum->address = 0; 355 356 /* The timer is left running so that outstanding URBs can fail */ 357 358 /* nuke any pending requests first, so driver i/o is quiesced */ 359 for (i = 0; i < DUMMY_ENDPOINTS; ++i) 360 nuke(dum, &dum->ep[i]); 361 362 /* driver now does any non-usb quiescing necessary */ 363 } 364 365 /** 366 * set_link_state_by_speed() - Sets the current state of the link according to 367 * the hcd speed 368 * @dum_hcd: pointer to the dummy_hcd structure to update the link state for 369 * 370 * This function updates the port_status according to the link state and the 371 * speed of the hcd. 372 */ 373 static void set_link_state_by_speed(struct dummy_hcd *dum_hcd) 374 { 375 struct dummy *dum = dum_hcd->dum; 376 377 if (dummy_hcd_to_hcd(dum_hcd)->speed == HCD_USB3) { 378 if ((dum_hcd->port_status & USB_SS_PORT_STAT_POWER) == 0) { 379 dum_hcd->port_status = 0; 380 } else if (!dum->pullup || dum->udc_suspended) { 381 /* UDC suspend must cause a disconnect */ 382 dum_hcd->port_status &= ~(USB_PORT_STAT_CONNECTION | 383 USB_PORT_STAT_ENABLE); 384 if ((dum_hcd->old_status & 385 USB_PORT_STAT_CONNECTION) != 0) 386 dum_hcd->port_status |= 387 (USB_PORT_STAT_C_CONNECTION << 16); 388 } else { 389 /* device is connected and not suspended */ 390 dum_hcd->port_status |= (USB_PORT_STAT_CONNECTION | 391 USB_PORT_STAT_SPEED_5GBPS) ; 392 if ((dum_hcd->old_status & 393 USB_PORT_STAT_CONNECTION) == 0) 394 dum_hcd->port_status |= 395 (USB_PORT_STAT_C_CONNECTION << 16); 396 if ((dum_hcd->port_status & USB_PORT_STAT_ENABLE) && 397 (dum_hcd->port_status & 398 USB_PORT_STAT_LINK_STATE) == USB_SS_PORT_LS_U0 && 399 dum_hcd->rh_state != DUMMY_RH_SUSPENDED) 400 dum_hcd->active = 1; 401 } 402 } else { 403 if ((dum_hcd->port_status & USB_PORT_STAT_POWER) == 0) { 404 dum_hcd->port_status = 0; 405 } else if (!dum->pullup || dum->udc_suspended) { 406 /* UDC suspend must cause a disconnect */ 407 dum_hcd->port_status &= ~(USB_PORT_STAT_CONNECTION | 408 USB_PORT_STAT_ENABLE | 409 USB_PORT_STAT_LOW_SPEED | 410 USB_PORT_STAT_HIGH_SPEED | 411 USB_PORT_STAT_SUSPEND); 412 if ((dum_hcd->old_status & 413 USB_PORT_STAT_CONNECTION) != 0) 414 dum_hcd->port_status |= 415 (USB_PORT_STAT_C_CONNECTION << 16); 416 } else { 417 dum_hcd->port_status |= USB_PORT_STAT_CONNECTION; 418 if ((dum_hcd->old_status & 419 USB_PORT_STAT_CONNECTION) == 0) 420 dum_hcd->port_status |= 421 (USB_PORT_STAT_C_CONNECTION << 16); 422 if ((dum_hcd->port_status & USB_PORT_STAT_ENABLE) == 0) 423 dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND; 424 else if ((dum_hcd->port_status & 425 USB_PORT_STAT_SUSPEND) == 0 && 426 dum_hcd->rh_state != DUMMY_RH_SUSPENDED) 427 dum_hcd->active = 1; 428 } 429 } 430 } 431 432 /* caller must hold lock */ 433 static void set_link_state(struct dummy_hcd *dum_hcd) 434 __must_hold(&dum->lock) 435 { 436 struct dummy *dum = dum_hcd->dum; 437 unsigned int power_bit; 438 439 dum_hcd->active = 0; 440 if (dum->pullup) 441 if ((dummy_hcd_to_hcd(dum_hcd)->speed == HCD_USB3 && 442 dum->gadget.speed != USB_SPEED_SUPER) || 443 (dummy_hcd_to_hcd(dum_hcd)->speed != HCD_USB3 && 444 dum->gadget.speed == USB_SPEED_SUPER)) 445 return; 446 447 set_link_state_by_speed(dum_hcd); 448 power_bit = (dummy_hcd_to_hcd(dum_hcd)->speed == HCD_USB3 ? 449 USB_SS_PORT_STAT_POWER : USB_PORT_STAT_POWER); 450 451 if ((dum_hcd->port_status & USB_PORT_STAT_ENABLE) == 0 || 452 dum_hcd->active) 453 dum_hcd->resuming = 0; 454 455 /* Currently !connected or in reset */ 456 if ((dum_hcd->port_status & power_bit) == 0 || 457 (dum_hcd->port_status & USB_PORT_STAT_RESET) != 0) { 458 unsigned int disconnect = power_bit & 459 dum_hcd->old_status & (~dum_hcd->port_status); 460 unsigned int reset = USB_PORT_STAT_RESET & 461 (~dum_hcd->old_status) & dum_hcd->port_status; 462 463 /* Report reset and disconnect events to the driver */ 464 if (dum->ints_enabled && (disconnect || reset)) { 465 stop_activity(dum); 466 ++dum->callback_usage; 467 spin_unlock(&dum->lock); 468 if (reset) 469 usb_gadget_udc_reset(&dum->gadget, dum->driver); 470 else 471 dum->driver->disconnect(&dum->gadget); 472 spin_lock(&dum->lock); 473 --dum->callback_usage; 474 } 475 } else if (dum_hcd->active != dum_hcd->old_active && 476 dum->ints_enabled) { 477 ++dum->callback_usage; 478 spin_unlock(&dum->lock); 479 if (dum_hcd->old_active && dum->driver->suspend) 480 dum->driver->suspend(&dum->gadget); 481 else if (!dum_hcd->old_active && dum->driver->resume) 482 dum->driver->resume(&dum->gadget); 483 spin_lock(&dum->lock); 484 --dum->callback_usage; 485 } 486 487 dum_hcd->old_status = dum_hcd->port_status; 488 dum_hcd->old_active = dum_hcd->active; 489 } 490 491 /*-------------------------------------------------------------------------*/ 492 493 /* DEVICE/GADGET SIDE DRIVER 494 * 495 * This only tracks gadget state. All the work is done when the host 496 * side tries some (emulated) i/o operation. Real device controller 497 * drivers would do real i/o using dma, fifos, irqs, timers, etc. 498 */ 499 500 #define is_enabled(dum) \ 501 (dum->port_status & USB_PORT_STAT_ENABLE) 502 503 static int dummy_enable(struct usb_ep *_ep, 504 const struct usb_endpoint_descriptor *desc) 505 { 506 struct dummy *dum; 507 struct dummy_hcd *dum_hcd; 508 struct dummy_ep *ep; 509 unsigned max; 510 int retval; 511 512 ep = usb_ep_to_dummy_ep(_ep); 513 if (!_ep || !desc || ep->desc || _ep->name == ep0name 514 || desc->bDescriptorType != USB_DT_ENDPOINT) 515 return -EINVAL; 516 dum = ep_to_dummy(ep); 517 if (!dum->driver) 518 return -ESHUTDOWN; 519 520 dum_hcd = gadget_to_dummy_hcd(&dum->gadget); 521 if (!is_enabled(dum_hcd)) 522 return -ESHUTDOWN; 523 524 /* 525 * For HS/FS devices only bits 0..10 of the wMaxPacketSize represent the 526 * maximum packet size. 527 * For SS devices the wMaxPacketSize is limited by 1024. 528 */ 529 max = usb_endpoint_maxp(desc); 530 531 /* drivers must not request bad settings, since lower levels 532 * (hardware or its drivers) may not check. some endpoints 533 * can't do iso, many have maxpacket limitations, etc. 534 * 535 * since this "hardware" driver is here to help debugging, we 536 * have some extra sanity checks. (there could be more though, 537 * especially for "ep9out" style fixed function ones.) 538 */ 539 retval = -EINVAL; 540 switch (usb_endpoint_type(desc)) { 541 case USB_ENDPOINT_XFER_BULK: 542 if (strstr(ep->ep.name, "-iso") 543 || strstr(ep->ep.name, "-int")) { 544 goto done; 545 } 546 switch (dum->gadget.speed) { 547 case USB_SPEED_SUPER: 548 if (max == 1024) 549 break; 550 goto done; 551 case USB_SPEED_HIGH: 552 if (max == 512) 553 break; 554 goto done; 555 case USB_SPEED_FULL: 556 if (max == 8 || max == 16 || max == 32 || max == 64) 557 /* we'll fake any legal size */ 558 break; 559 /* save a return statement */ 560 fallthrough; 561 default: 562 goto done; 563 } 564 break; 565 case USB_ENDPOINT_XFER_INT: 566 if (strstr(ep->ep.name, "-iso")) /* bulk is ok */ 567 goto done; 568 /* real hardware might not handle all packet sizes */ 569 switch (dum->gadget.speed) { 570 case USB_SPEED_SUPER: 571 case USB_SPEED_HIGH: 572 if (max <= 1024) 573 break; 574 /* save a return statement */ 575 fallthrough; 576 case USB_SPEED_FULL: 577 if (max <= 64) 578 break; 579 /* save a return statement */ 580 fallthrough; 581 default: 582 if (max <= 8) 583 break; 584 goto done; 585 } 586 break; 587 case USB_ENDPOINT_XFER_ISOC: 588 if (strstr(ep->ep.name, "-bulk") 589 || strstr(ep->ep.name, "-int")) 590 goto done; 591 /* real hardware might not handle all packet sizes */ 592 switch (dum->gadget.speed) { 593 case USB_SPEED_SUPER: 594 case USB_SPEED_HIGH: 595 if (max <= 1024) 596 break; 597 /* save a return statement */ 598 fallthrough; 599 case USB_SPEED_FULL: 600 if (max <= 1023) 601 break; 602 /* save a return statement */ 603 fallthrough; 604 default: 605 goto done; 606 } 607 break; 608 default: 609 /* few chips support control except on ep0 */ 610 goto done; 611 } 612 613 _ep->maxpacket = max; 614 if (usb_ss_max_streams(_ep->comp_desc)) { 615 if (!usb_endpoint_xfer_bulk(desc)) { 616 dev_err(udc_dev(dum), "Can't enable stream support on " 617 "non-bulk ep %s\n", _ep->name); 618 return -EINVAL; 619 } 620 ep->stream_en = 1; 621 } 622 ep->desc = desc; 623 624 dev_dbg(udc_dev(dum), "enabled %s (ep%d%s-%s) maxpacket %d stream %s\n", 625 _ep->name, 626 desc->bEndpointAddress & 0x0f, 627 (desc->bEndpointAddress & USB_DIR_IN) ? "in" : "out", 628 usb_ep_type_string(usb_endpoint_type(desc)), 629 max, str_enabled_disabled(ep->stream_en)); 630 631 /* at this point real hardware should be NAKing transfers 632 * to that endpoint, until a buffer is queued to it. 633 */ 634 ep->halted = ep->wedged = 0; 635 retval = 0; 636 done: 637 return retval; 638 } 639 640 static int dummy_disable(struct usb_ep *_ep) 641 { 642 struct dummy_ep *ep; 643 struct dummy *dum; 644 unsigned long flags; 645 646 ep = usb_ep_to_dummy_ep(_ep); 647 if (!_ep || !ep->desc || _ep->name == ep0name) 648 return -EINVAL; 649 dum = ep_to_dummy(ep); 650 651 spin_lock_irqsave(&dum->lock, flags); 652 ep->desc = NULL; 653 ep->stream_en = 0; 654 nuke(dum, ep); 655 spin_unlock_irqrestore(&dum->lock, flags); 656 657 dev_dbg(udc_dev(dum), "disabled %s\n", _ep->name); 658 return 0; 659 } 660 661 static struct usb_request *dummy_alloc_request(struct usb_ep *_ep, 662 gfp_t mem_flags) 663 { 664 struct dummy_request *req; 665 666 if (!_ep) 667 return NULL; 668 669 req = kzalloc(sizeof(*req), mem_flags); 670 if (!req) 671 return NULL; 672 INIT_LIST_HEAD(&req->queue); 673 return &req->req; 674 } 675 676 static void dummy_free_request(struct usb_ep *_ep, struct usb_request *_req) 677 { 678 struct dummy_request *req; 679 680 if (!_ep || !_req) { 681 WARN_ON(1); 682 return; 683 } 684 685 req = usb_request_to_dummy_request(_req); 686 WARN_ON(!list_empty(&req->queue)); 687 kfree(req); 688 } 689 690 static void fifo_complete(struct usb_ep *ep, struct usb_request *req) 691 { 692 } 693 694 static int dummy_queue(struct usb_ep *_ep, struct usb_request *_req, 695 gfp_t mem_flags) 696 { 697 struct dummy_ep *ep; 698 struct dummy_request *req; 699 struct dummy *dum; 700 struct dummy_hcd *dum_hcd; 701 unsigned long flags; 702 703 req = usb_request_to_dummy_request(_req); 704 if (!_req || !list_empty(&req->queue) || !_req->complete) 705 return -EINVAL; 706 707 ep = usb_ep_to_dummy_ep(_ep); 708 if (!_ep || (!ep->desc && _ep->name != ep0name)) 709 return -EINVAL; 710 711 dum = ep_to_dummy(ep); 712 dum_hcd = gadget_to_dummy_hcd(&dum->gadget); 713 if (!dum->driver || !is_enabled(dum_hcd)) 714 return -ESHUTDOWN; 715 716 #if 0 717 dev_dbg(udc_dev(dum), "ep %p queue req %p to %s, len %d buf %p\n", 718 ep, _req, _ep->name, _req->length, _req->buf); 719 #endif 720 _req->status = -EINPROGRESS; 721 _req->actual = 0; 722 spin_lock_irqsave(&dum->lock, flags); 723 724 /* implement an emulated single-request FIFO */ 725 if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) && 726 list_empty(&dum->fifo_req.queue) && 727 list_empty(&ep->queue) && 728 _req->length <= FIFO_SIZE) { 729 req = &dum->fifo_req; 730 req->req = *_req; 731 req->req.buf = dum->fifo_buf; 732 memcpy(dum->fifo_buf, _req->buf, _req->length); 733 req->req.context = dum; 734 req->req.complete = fifo_complete; 735 736 list_add_tail(&req->queue, &ep->queue); 737 spin_unlock(&dum->lock); 738 _req->actual = _req->length; 739 _req->status = 0; 740 usb_gadget_giveback_request(_ep, _req); 741 spin_lock(&dum->lock); 742 } else 743 list_add_tail(&req->queue, &ep->queue); 744 spin_unlock_irqrestore(&dum->lock, flags); 745 746 /* real hardware would likely enable transfers here, in case 747 * it'd been left NAKing. 748 */ 749 return 0; 750 } 751 752 static int dummy_dequeue(struct usb_ep *_ep, struct usb_request *_req) 753 { 754 struct dummy_ep *ep; 755 struct dummy *dum; 756 int retval = -EINVAL; 757 unsigned long flags; 758 struct dummy_request *req = NULL, *iter; 759 760 if (!_ep || !_req) 761 return retval; 762 ep = usb_ep_to_dummy_ep(_ep); 763 dum = ep_to_dummy(ep); 764 765 if (!dum->driver) 766 return -ESHUTDOWN; 767 768 local_irq_save(flags); 769 spin_lock(&dum->lock); 770 list_for_each_entry(iter, &ep->queue, queue) { 771 if (&iter->req != _req) 772 continue; 773 list_del_init(&iter->queue); 774 _req->status = -ECONNRESET; 775 req = iter; 776 retval = 0; 777 break; 778 } 779 spin_unlock(&dum->lock); 780 781 if (retval == 0) { 782 dev_dbg(udc_dev(dum), 783 "dequeued req %p from %s, len %d buf %p\n", 784 req, _ep->name, _req->length, _req->buf); 785 usb_gadget_giveback_request(_ep, _req); 786 } 787 local_irq_restore(flags); 788 return retval; 789 } 790 791 static int 792 dummy_set_halt_and_wedge(struct usb_ep *_ep, int value, int wedged) 793 { 794 struct dummy_ep *ep; 795 struct dummy *dum; 796 797 if (!_ep) 798 return -EINVAL; 799 ep = usb_ep_to_dummy_ep(_ep); 800 dum = ep_to_dummy(ep); 801 if (!dum->driver) 802 return -ESHUTDOWN; 803 if (!value) 804 ep->halted = ep->wedged = 0; 805 else if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) && 806 !list_empty(&ep->queue)) 807 return -EAGAIN; 808 else { 809 ep->halted = 1; 810 if (wedged) 811 ep->wedged = 1; 812 } 813 /* FIXME clear emulated data toggle too */ 814 return 0; 815 } 816 817 static int 818 dummy_set_halt(struct usb_ep *_ep, int value) 819 { 820 return dummy_set_halt_and_wedge(_ep, value, 0); 821 } 822 823 static int dummy_set_wedge(struct usb_ep *_ep) 824 { 825 if (!_ep || _ep->name == ep0name) 826 return -EINVAL; 827 return dummy_set_halt_and_wedge(_ep, 1, 1); 828 } 829 830 static const struct usb_ep_ops dummy_ep_ops = { 831 .enable = dummy_enable, 832 .disable = dummy_disable, 833 834 .alloc_request = dummy_alloc_request, 835 .free_request = dummy_free_request, 836 837 .queue = dummy_queue, 838 .dequeue = dummy_dequeue, 839 840 .set_halt = dummy_set_halt, 841 .set_wedge = dummy_set_wedge, 842 }; 843 844 /*-------------------------------------------------------------------------*/ 845 846 /* there are both host and device side versions of this call ... */ 847 static int dummy_g_get_frame(struct usb_gadget *_gadget) 848 { 849 struct timespec64 ts64; 850 851 ktime_get_ts64(&ts64); 852 return ts64.tv_nsec / NSEC_PER_MSEC; 853 } 854 855 static int dummy_wakeup(struct usb_gadget *_gadget) 856 { 857 struct dummy_hcd *dum_hcd; 858 859 dum_hcd = gadget_to_dummy_hcd(_gadget); 860 if (!(dum_hcd->dum->devstatus & ((1 << USB_DEVICE_B_HNP_ENABLE) 861 | (1 << USB_DEVICE_REMOTE_WAKEUP)))) 862 return -EINVAL; 863 if ((dum_hcd->port_status & USB_PORT_STAT_CONNECTION) == 0) 864 return -ENOLINK; 865 if ((dum_hcd->port_status & USB_PORT_STAT_SUSPEND) == 0 && 866 dum_hcd->rh_state != DUMMY_RH_SUSPENDED) 867 return -EIO; 868 869 /* FIXME: What if the root hub is suspended but the port isn't? */ 870 871 /* hub notices our request, issues downstream resume, etc */ 872 dum_hcd->resuming = 1; 873 dum_hcd->re_timeout = jiffies + msecs_to_jiffies(20); 874 mod_timer(&dummy_hcd_to_hcd(dum_hcd)->rh_timer, dum_hcd->re_timeout); 875 return 0; 876 } 877 878 static int dummy_set_selfpowered(struct usb_gadget *_gadget, int value) 879 { 880 struct dummy *dum; 881 882 _gadget->is_selfpowered = (value != 0); 883 dum = gadget_to_dummy_hcd(_gadget)->dum; 884 if (value) 885 dum->devstatus |= (1 << USB_DEVICE_SELF_POWERED); 886 else 887 dum->devstatus &= ~(1 << USB_DEVICE_SELF_POWERED); 888 return 0; 889 } 890 891 static void dummy_udc_update_ep0(struct dummy *dum) 892 { 893 if (dum->gadget.speed == USB_SPEED_SUPER) 894 dum->ep[0].ep.maxpacket = 9; 895 else 896 dum->ep[0].ep.maxpacket = 64; 897 } 898 899 static int dummy_pullup(struct usb_gadget *_gadget, int value) 900 { 901 struct dummy_hcd *dum_hcd; 902 struct dummy *dum; 903 unsigned long flags; 904 905 dum = gadget_dev_to_dummy(&_gadget->dev); 906 dum_hcd = gadget_to_dummy_hcd(_gadget); 907 908 spin_lock_irqsave(&dum->lock, flags); 909 dum->pullup = (value != 0); 910 set_link_state(dum_hcd); 911 if (value == 0) { 912 /* 913 * Emulate synchronize_irq(): wait for callbacks to finish. 914 * This seems to be the best place to emulate the call to 915 * synchronize_irq() that's in usb_gadget_remove_driver(). 916 * Doing it in dummy_udc_stop() would be too late since it 917 * is called after the unbind callback and unbind shouldn't 918 * be invoked until all the other callbacks are finished. 919 */ 920 while (dum->callback_usage > 0) { 921 spin_unlock_irqrestore(&dum->lock, flags); 922 usleep_range(1000, 2000); 923 spin_lock_irqsave(&dum->lock, flags); 924 } 925 } 926 spin_unlock_irqrestore(&dum->lock, flags); 927 928 usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd)); 929 return 0; 930 } 931 932 static void dummy_udc_set_speed(struct usb_gadget *_gadget, 933 enum usb_device_speed speed) 934 { 935 struct dummy *dum; 936 937 dum = gadget_dev_to_dummy(&_gadget->dev); 938 dum->gadget.speed = speed; 939 dummy_udc_update_ep0(dum); 940 } 941 942 static void dummy_udc_async_callbacks(struct usb_gadget *_gadget, bool enable) 943 { 944 struct dummy *dum = gadget_dev_to_dummy(&_gadget->dev); 945 946 spin_lock_irq(&dum->lock); 947 dum->ints_enabled = enable; 948 spin_unlock_irq(&dum->lock); 949 } 950 951 static int dummy_udc_start(struct usb_gadget *g, 952 struct usb_gadget_driver *driver); 953 static int dummy_udc_stop(struct usb_gadget *g); 954 955 static const struct usb_gadget_ops dummy_ops = { 956 .get_frame = dummy_g_get_frame, 957 .wakeup = dummy_wakeup, 958 .set_selfpowered = dummy_set_selfpowered, 959 .pullup = dummy_pullup, 960 .udc_start = dummy_udc_start, 961 .udc_stop = dummy_udc_stop, 962 .udc_set_speed = dummy_udc_set_speed, 963 .udc_async_callbacks = dummy_udc_async_callbacks, 964 }; 965 966 /*-------------------------------------------------------------------------*/ 967 968 /* "function" sysfs attribute */ 969 static ssize_t function_show(struct device *dev, struct device_attribute *attr, 970 char *buf) 971 { 972 struct dummy *dum = gadget_dev_to_dummy(dev); 973 974 if (!dum->driver || !dum->driver->function) 975 return 0; 976 return scnprintf(buf, PAGE_SIZE, "%s\n", dum->driver->function); 977 } 978 static DEVICE_ATTR_RO(function); 979 980 /*-------------------------------------------------------------------------*/ 981 982 /* 983 * Driver registration/unregistration. 984 * 985 * This is basically hardware-specific; there's usually only one real USB 986 * device (not host) controller since that's how USB devices are intended 987 * to work. So most implementations of these api calls will rely on the 988 * fact that only one driver will ever bind to the hardware. But curious 989 * hardware can be built with discrete components, so the gadget API doesn't 990 * require that assumption. 991 * 992 * For this emulator, it might be convenient to create a usb device 993 * for each driver that registers: just add to a big root hub. 994 */ 995 996 static int dummy_udc_start(struct usb_gadget *g, 997 struct usb_gadget_driver *driver) 998 { 999 struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(g); 1000 struct dummy *dum = dum_hcd->dum; 1001 1002 switch (g->speed) { 1003 /* All the speeds we support */ 1004 case USB_SPEED_LOW: 1005 case USB_SPEED_FULL: 1006 case USB_SPEED_HIGH: 1007 case USB_SPEED_SUPER: 1008 break; 1009 default: 1010 dev_err(dummy_dev(dum_hcd), "Unsupported driver max speed %d\n", 1011 driver->max_speed); 1012 return -EINVAL; 1013 } 1014 1015 /* 1016 * DEVICE side init ... the layer above hardware, which 1017 * can't enumerate without help from the driver we're binding. 1018 */ 1019 1020 spin_lock_irq(&dum->lock); 1021 dum->devstatus = 0; 1022 dum->driver = driver; 1023 spin_unlock_irq(&dum->lock); 1024 1025 return 0; 1026 } 1027 1028 static int dummy_udc_stop(struct usb_gadget *g) 1029 { 1030 struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(g); 1031 struct dummy *dum = dum_hcd->dum; 1032 1033 spin_lock_irq(&dum->lock); 1034 dum->ints_enabled = 0; 1035 stop_activity(dum); 1036 dum->driver = NULL; 1037 spin_unlock_irq(&dum->lock); 1038 1039 return 0; 1040 } 1041 1042 #undef is_enabled 1043 1044 /* The gadget structure is stored inside the hcd structure and will be 1045 * released along with it. */ 1046 static void init_dummy_udc_hw(struct dummy *dum) 1047 { 1048 int i; 1049 1050 INIT_LIST_HEAD(&dum->gadget.ep_list); 1051 for (i = 0; i < DUMMY_ENDPOINTS; i++) { 1052 struct dummy_ep *ep = &dum->ep[i]; 1053 1054 if (!ep_info[i].name) 1055 break; 1056 ep->ep.name = ep_info[i].name; 1057 ep->ep.caps = ep_info[i].caps; 1058 ep->ep.ops = &dummy_ep_ops; 1059 list_add_tail(&ep->ep.ep_list, &dum->gadget.ep_list); 1060 ep->halted = ep->wedged = ep->already_seen = 1061 ep->setup_stage = 0; 1062 usb_ep_set_maxpacket_limit(&ep->ep, ~0); 1063 ep->ep.max_streams = 16; 1064 ep->last_io = jiffies; 1065 ep->gadget = &dum->gadget; 1066 ep->desc = NULL; 1067 INIT_LIST_HEAD(&ep->queue); 1068 } 1069 1070 dum->gadget.ep0 = &dum->ep[0].ep; 1071 list_del_init(&dum->ep[0].ep.ep_list); 1072 INIT_LIST_HEAD(&dum->fifo_req.queue); 1073 1074 #ifdef CONFIG_USB_OTG 1075 dum->gadget.is_otg = 1; 1076 #endif 1077 } 1078 1079 static int dummy_udc_probe(struct platform_device *pdev) 1080 { 1081 struct dummy *dum; 1082 int rc; 1083 1084 dum = *((void **)dev_get_platdata(&pdev->dev)); 1085 /* Clear usb_gadget region for new registration to udc-core */ 1086 memzero_explicit(&dum->gadget, sizeof(struct usb_gadget)); 1087 dum->gadget.name = gadget_name; 1088 dum->gadget.ops = &dummy_ops; 1089 if (mod_data.is_super_speed) 1090 dum->gadget.max_speed = USB_SPEED_SUPER; 1091 else if (mod_data.is_high_speed) 1092 dum->gadget.max_speed = USB_SPEED_HIGH; 1093 else 1094 dum->gadget.max_speed = USB_SPEED_FULL; 1095 1096 dum->gadget.dev.parent = &pdev->dev; 1097 init_dummy_udc_hw(dum); 1098 1099 rc = usb_add_gadget_udc(&pdev->dev, &dum->gadget); 1100 if (rc < 0) 1101 goto err_udc; 1102 1103 rc = device_create_file(&dum->gadget.dev, &dev_attr_function); 1104 if (rc < 0) 1105 goto err_dev; 1106 platform_set_drvdata(pdev, dum); 1107 return rc; 1108 1109 err_dev: 1110 usb_del_gadget_udc(&dum->gadget); 1111 err_udc: 1112 return rc; 1113 } 1114 1115 static void dummy_udc_remove(struct platform_device *pdev) 1116 { 1117 struct dummy *dum = platform_get_drvdata(pdev); 1118 1119 device_remove_file(&dum->gadget.dev, &dev_attr_function); 1120 usb_del_gadget_udc(&dum->gadget); 1121 } 1122 1123 static void dummy_udc_pm(struct dummy *dum, struct dummy_hcd *dum_hcd, 1124 int suspend) 1125 { 1126 spin_lock_irq(&dum->lock); 1127 dum->udc_suspended = suspend; 1128 set_link_state(dum_hcd); 1129 spin_unlock_irq(&dum->lock); 1130 } 1131 1132 static int dummy_udc_suspend(struct platform_device *pdev, pm_message_t state) 1133 { 1134 struct dummy *dum = platform_get_drvdata(pdev); 1135 struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(&dum->gadget); 1136 1137 dev_dbg(&pdev->dev, "%s\n", __func__); 1138 dummy_udc_pm(dum, dum_hcd, 1); 1139 usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd)); 1140 return 0; 1141 } 1142 1143 static int dummy_udc_resume(struct platform_device *pdev) 1144 { 1145 struct dummy *dum = platform_get_drvdata(pdev); 1146 struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(&dum->gadget); 1147 1148 dev_dbg(&pdev->dev, "%s\n", __func__); 1149 dummy_udc_pm(dum, dum_hcd, 0); 1150 usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd)); 1151 return 0; 1152 } 1153 1154 static struct platform_driver dummy_udc_driver = { 1155 .probe = dummy_udc_probe, 1156 .remove = dummy_udc_remove, 1157 .suspend = dummy_udc_suspend, 1158 .resume = dummy_udc_resume, 1159 .driver = { 1160 .name = gadget_name, 1161 }, 1162 }; 1163 1164 /*-------------------------------------------------------------------------*/ 1165 1166 static unsigned int dummy_get_ep_idx(const struct usb_endpoint_descriptor *desc) 1167 { 1168 unsigned int index; 1169 1170 index = usb_endpoint_num(desc) << 1; 1171 if (usb_endpoint_dir_in(desc)) 1172 index |= 1; 1173 return index; 1174 } 1175 1176 /* HOST SIDE DRIVER 1177 * 1178 * this uses the hcd framework to hook up to host side drivers. 1179 * its root hub will only have one device, otherwise it acts like 1180 * a normal host controller. 1181 * 1182 * when urbs are queued, they're just stuck on a list that we 1183 * scan in a timer callback. that callback connects writes from 1184 * the host with reads from the device, and so on, based on the 1185 * usb 2.0 rules. 1186 */ 1187 1188 static int dummy_ep_stream_en(struct dummy_hcd *dum_hcd, struct urb *urb) 1189 { 1190 const struct usb_endpoint_descriptor *desc = &urb->ep->desc; 1191 u32 index; 1192 1193 if (!usb_endpoint_xfer_bulk(desc)) 1194 return 0; 1195 1196 index = dummy_get_ep_idx(desc); 1197 return (1 << index) & dum_hcd->stream_en_ep; 1198 } 1199 1200 /* 1201 * The max stream number is saved as a nibble so for the 30 possible endpoints 1202 * we only 15 bytes of memory. Therefore we are limited to max 16 streams (0 1203 * means we use only 1 stream). The maximum according to the spec is 16bit so 1204 * if the 16 stream limit is about to go, the array size should be incremented 1205 * to 30 elements of type u16. 1206 */ 1207 static int get_max_streams_for_pipe(struct dummy_hcd *dum_hcd, 1208 unsigned int pipe) 1209 { 1210 int max_streams; 1211 1212 max_streams = dum_hcd->num_stream[usb_pipeendpoint(pipe)]; 1213 if (usb_pipeout(pipe)) 1214 max_streams >>= 4; 1215 else 1216 max_streams &= 0xf; 1217 max_streams++; 1218 return max_streams; 1219 } 1220 1221 static void set_max_streams_for_pipe(struct dummy_hcd *dum_hcd, 1222 unsigned int pipe, unsigned int streams) 1223 { 1224 int max_streams; 1225 1226 streams--; 1227 max_streams = dum_hcd->num_stream[usb_pipeendpoint(pipe)]; 1228 if (usb_pipeout(pipe)) { 1229 streams <<= 4; 1230 max_streams &= 0xf; 1231 } else { 1232 max_streams &= 0xf0; 1233 } 1234 max_streams |= streams; 1235 dum_hcd->num_stream[usb_pipeendpoint(pipe)] = max_streams; 1236 } 1237 1238 static int dummy_validate_stream(struct dummy_hcd *dum_hcd, struct urb *urb) 1239 { 1240 unsigned int max_streams; 1241 int enabled; 1242 1243 enabled = dummy_ep_stream_en(dum_hcd, urb); 1244 if (!urb->stream_id) { 1245 if (enabled) 1246 return -EINVAL; 1247 return 0; 1248 } 1249 if (!enabled) 1250 return -EINVAL; 1251 1252 max_streams = get_max_streams_for_pipe(dum_hcd, 1253 usb_pipeendpoint(urb->pipe)); 1254 if (urb->stream_id > max_streams) { 1255 dev_err(dummy_dev(dum_hcd), "Stream id %d is out of range.\n", 1256 urb->stream_id); 1257 BUG(); 1258 return -EINVAL; 1259 } 1260 return 0; 1261 } 1262 1263 static int dummy_urb_enqueue( 1264 struct usb_hcd *hcd, 1265 struct urb *urb, 1266 gfp_t mem_flags 1267 ) { 1268 struct dummy_hcd *dum_hcd; 1269 struct urbp *urbp; 1270 unsigned long flags; 1271 int rc; 1272 1273 urbp = kmalloc(sizeof *urbp, mem_flags); 1274 if (!urbp) 1275 return -ENOMEM; 1276 urbp->urb = urb; 1277 urbp->miter_started = 0; 1278 1279 dum_hcd = hcd_to_dummy_hcd(hcd); 1280 spin_lock_irqsave(&dum_hcd->dum->lock, flags); 1281 1282 rc = dummy_validate_stream(dum_hcd, urb); 1283 if (rc) { 1284 kfree(urbp); 1285 goto done; 1286 } 1287 1288 rc = usb_hcd_link_urb_to_ep(hcd, urb); 1289 if (rc) { 1290 kfree(urbp); 1291 goto done; 1292 } 1293 1294 if (!dum_hcd->udev) { 1295 dum_hcd->udev = urb->dev; 1296 usb_get_dev(dum_hcd->udev); 1297 } else if (unlikely(dum_hcd->udev != urb->dev)) 1298 dev_err(dummy_dev(dum_hcd), "usb_device address has changed!\n"); 1299 1300 list_add_tail(&urbp->urbp_list, &dum_hcd->urbp_list); 1301 urb->hcpriv = urbp; 1302 if (!dum_hcd->next_frame_urbp) 1303 dum_hcd->next_frame_urbp = urbp; 1304 if (usb_pipetype(urb->pipe) == PIPE_CONTROL) 1305 urb->error_count = 1; /* mark as a new urb */ 1306 1307 /* kick the scheduler, it'll do the rest */ 1308 if (!dum_hcd->timer_pending) { 1309 dum_hcd->timer_pending = 1; 1310 hrtimer_start(&dum_hcd->timer, ns_to_ktime(DUMMY_TIMER_INT_NSECS), 1311 HRTIMER_MODE_REL_SOFT); 1312 } 1313 1314 done: 1315 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags); 1316 return rc; 1317 } 1318 1319 static int dummy_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status) 1320 { 1321 struct dummy_hcd *dum_hcd; 1322 unsigned long flags; 1323 int rc; 1324 1325 /* giveback happens automatically in timer callback, 1326 * so make sure the callback happens */ 1327 dum_hcd = hcd_to_dummy_hcd(hcd); 1328 spin_lock_irqsave(&dum_hcd->dum->lock, flags); 1329 1330 rc = usb_hcd_check_unlink_urb(hcd, urb, status); 1331 if (rc == 0 && !dum_hcd->timer_pending) { 1332 dum_hcd->timer_pending = 1; 1333 hrtimer_start(&dum_hcd->timer, ns_to_ktime(0), HRTIMER_MODE_REL_SOFT); 1334 } 1335 1336 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags); 1337 return rc; 1338 } 1339 1340 static int dummy_perform_transfer(struct urb *urb, struct dummy_request *req, 1341 u32 len) 1342 { 1343 void *ubuf, *rbuf; 1344 struct urbp *urbp = urb->hcpriv; 1345 int to_host; 1346 struct sg_mapping_iter *miter = &urbp->miter; 1347 u32 trans = 0; 1348 u32 this_sg; 1349 bool next_sg; 1350 1351 to_host = usb_urb_dir_in(urb); 1352 rbuf = req->req.buf + req->req.actual; 1353 1354 if (!urb->num_sgs) { 1355 ubuf = urb->transfer_buffer + urb->actual_length; 1356 if (to_host) 1357 memcpy(ubuf, rbuf, len); 1358 else 1359 memcpy(rbuf, ubuf, len); 1360 return len; 1361 } 1362 1363 if (!urbp->miter_started) { 1364 u32 flags = SG_MITER_ATOMIC; 1365 1366 if (to_host) 1367 flags |= SG_MITER_TO_SG; 1368 else 1369 flags |= SG_MITER_FROM_SG; 1370 1371 sg_miter_start(miter, urb->sg, urb->num_sgs, flags); 1372 urbp->miter_started = 1; 1373 } 1374 next_sg = sg_miter_next(miter); 1375 if (next_sg == false) { 1376 WARN_ON_ONCE(1); 1377 return -EINVAL; 1378 } 1379 do { 1380 ubuf = miter->addr; 1381 this_sg = min_t(u32, len, miter->length); 1382 miter->consumed = this_sg; 1383 trans += this_sg; 1384 1385 if (to_host) 1386 memcpy(ubuf, rbuf, this_sg); 1387 else 1388 memcpy(rbuf, ubuf, this_sg); 1389 len -= this_sg; 1390 1391 if (!len) 1392 break; 1393 next_sg = sg_miter_next(miter); 1394 if (next_sg == false) { 1395 WARN_ON_ONCE(1); 1396 return -EINVAL; 1397 } 1398 1399 rbuf += this_sg; 1400 } while (1); 1401 1402 sg_miter_stop(miter); 1403 return trans; 1404 } 1405 1406 /* transfer up to a frame's worth; caller must own lock */ 1407 static int transfer(struct dummy_hcd *dum_hcd, struct urb *urb, 1408 struct dummy_ep *ep, int limit, int *status) 1409 { 1410 struct dummy *dum = dum_hcd->dum; 1411 struct dummy_request *req; 1412 int sent = 0; 1413 1414 top: 1415 /* if there's no request queued, the device is NAKing; return */ 1416 list_for_each_entry(req, &ep->queue, queue) { 1417 unsigned host_len, dev_len, len; 1418 int is_short, to_host; 1419 int rescan = 0; 1420 1421 if (dummy_ep_stream_en(dum_hcd, urb)) { 1422 if ((urb->stream_id != req->req.stream_id)) 1423 continue; 1424 } 1425 1426 /* 1..N packets of ep->ep.maxpacket each ... the last one 1427 * may be short (including zero length). 1428 * 1429 * writer can send a zlp explicitly (length 0) or implicitly 1430 * (length mod maxpacket zero, and 'zero' flag); they always 1431 * terminate reads. 1432 */ 1433 host_len = urb->transfer_buffer_length - urb->actual_length; 1434 dev_len = req->req.length - req->req.actual; 1435 len = min(host_len, dev_len); 1436 1437 /* FIXME update emulated data toggle too */ 1438 1439 to_host = usb_urb_dir_in(urb); 1440 if (unlikely(len == 0)) 1441 is_short = 1; 1442 else { 1443 /* not enough bandwidth left? */ 1444 if (limit < ep->ep.maxpacket && limit < len) 1445 break; 1446 len = min_t(unsigned, len, limit); 1447 if (len == 0) 1448 break; 1449 1450 /* send multiple of maxpacket first, then remainder */ 1451 if (len >= ep->ep.maxpacket) { 1452 is_short = 0; 1453 if (len % ep->ep.maxpacket) 1454 rescan = 1; 1455 len -= len % ep->ep.maxpacket; 1456 } else { 1457 is_short = 1; 1458 } 1459 1460 len = dummy_perform_transfer(urb, req, len); 1461 1462 ep->last_io = jiffies; 1463 if ((int)len < 0) { 1464 req->req.status = len; 1465 } else { 1466 limit -= len; 1467 sent += len; 1468 urb->actual_length += len; 1469 req->req.actual += len; 1470 } 1471 } 1472 1473 /* short packets terminate, maybe with overflow/underflow. 1474 * it's only really an error to write too much. 1475 * 1476 * partially filling a buffer optionally blocks queue advances 1477 * (so completion handlers can clean up the queue) but we don't 1478 * need to emulate such data-in-flight. 1479 */ 1480 if (is_short) { 1481 if (host_len == dev_len) { 1482 req->req.status = 0; 1483 *status = 0; 1484 } else if (to_host) { 1485 req->req.status = 0; 1486 if (dev_len > host_len) 1487 *status = -EOVERFLOW; 1488 else 1489 *status = 0; 1490 } else { 1491 *status = 0; 1492 if (host_len > dev_len) 1493 req->req.status = -EOVERFLOW; 1494 else 1495 req->req.status = 0; 1496 } 1497 1498 /* 1499 * many requests terminate without a short packet. 1500 * send a zlp if demanded by flags. 1501 */ 1502 } else { 1503 if (req->req.length == req->req.actual) { 1504 if (req->req.zero && to_host) 1505 rescan = 1; 1506 else 1507 req->req.status = 0; 1508 } 1509 if (urb->transfer_buffer_length == urb->actual_length) { 1510 if (urb->transfer_flags & URB_ZERO_PACKET && 1511 !to_host) 1512 rescan = 1; 1513 else 1514 *status = 0; 1515 } 1516 } 1517 1518 /* device side completion --> continuable */ 1519 if (req->req.status != -EINPROGRESS) { 1520 list_del_init(&req->queue); 1521 1522 spin_unlock(&dum->lock); 1523 usb_gadget_giveback_request(&ep->ep, &req->req); 1524 spin_lock(&dum->lock); 1525 1526 /* requests might have been unlinked... */ 1527 rescan = 1; 1528 } 1529 1530 /* host side completion --> terminate */ 1531 if (*status != -EINPROGRESS) 1532 break; 1533 1534 /* rescan to continue with any other queued i/o */ 1535 if (rescan) 1536 goto top; 1537 } 1538 return sent; 1539 } 1540 1541 static int periodic_bytes(struct dummy *dum, struct dummy_ep *ep) 1542 { 1543 int limit = ep->ep.maxpacket; 1544 1545 if (dum->gadget.speed == USB_SPEED_HIGH) { 1546 int tmp; 1547 1548 /* high bandwidth mode */ 1549 tmp = usb_endpoint_maxp_mult(ep->desc); 1550 tmp *= 8 /* applies to entire frame */; 1551 limit += limit * tmp; 1552 } 1553 if (dum->gadget.speed == USB_SPEED_SUPER) { 1554 switch (usb_endpoint_type(ep->desc)) { 1555 case USB_ENDPOINT_XFER_ISOC: 1556 /* Sec. 4.4.8.2 USB3.0 Spec */ 1557 limit = 3 * 16 * 1024 * 8; 1558 break; 1559 case USB_ENDPOINT_XFER_INT: 1560 /* Sec. 4.4.7.2 USB3.0 Spec */ 1561 limit = 3 * 1024 * 8; 1562 break; 1563 case USB_ENDPOINT_XFER_BULK: 1564 default: 1565 break; 1566 } 1567 } 1568 return limit; 1569 } 1570 1571 #define is_active(dum_hcd) ((dum_hcd->port_status & \ 1572 (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE | \ 1573 USB_PORT_STAT_SUSPEND)) \ 1574 == (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE)) 1575 1576 static struct dummy_ep *find_endpoint(struct dummy *dum, u8 address) 1577 { 1578 int i; 1579 1580 if (!is_active((dum->gadget.speed == USB_SPEED_SUPER ? 1581 dum->ss_hcd : dum->hs_hcd))) 1582 return NULL; 1583 if (!dum->ints_enabled) 1584 return NULL; 1585 if ((address & ~USB_DIR_IN) == 0) 1586 return &dum->ep[0]; 1587 for (i = 1; i < DUMMY_ENDPOINTS; i++) { 1588 struct dummy_ep *ep = &dum->ep[i]; 1589 1590 if (!ep->desc) 1591 continue; 1592 if (ep->desc->bEndpointAddress == address) 1593 return ep; 1594 } 1595 return NULL; 1596 } 1597 1598 #undef is_active 1599 1600 #define Dev_Request (USB_TYPE_STANDARD | USB_RECIP_DEVICE) 1601 #define Dev_InRequest (Dev_Request | USB_DIR_IN) 1602 #define Intf_Request (USB_TYPE_STANDARD | USB_RECIP_INTERFACE) 1603 #define Intf_InRequest (Intf_Request | USB_DIR_IN) 1604 #define Ep_Request (USB_TYPE_STANDARD | USB_RECIP_ENDPOINT) 1605 #define Ep_InRequest (Ep_Request | USB_DIR_IN) 1606 1607 1608 /** 1609 * handle_control_request() - handles all control transfers 1610 * @dum_hcd: pointer to dummy (the_controller) 1611 * @urb: the urb request to handle 1612 * @setup: pointer to the setup data for a USB device control 1613 * request 1614 * @status: pointer to request handling status 1615 * 1616 * Return 0 - if the request was handled 1617 * 1 - if the request wasn't handles 1618 * error code on error 1619 */ 1620 static int handle_control_request(struct dummy_hcd *dum_hcd, struct urb *urb, 1621 struct usb_ctrlrequest *setup, 1622 int *status) 1623 { 1624 struct dummy_ep *ep2; 1625 struct dummy *dum = dum_hcd->dum; 1626 int ret_val = 1; 1627 unsigned w_index; 1628 unsigned w_value; 1629 1630 w_index = le16_to_cpu(setup->wIndex); 1631 w_value = le16_to_cpu(setup->wValue); 1632 switch (setup->bRequest) { 1633 case USB_REQ_SET_ADDRESS: 1634 if (setup->bRequestType != Dev_Request) 1635 break; 1636 dum->address = w_value; 1637 *status = 0; 1638 dev_dbg(udc_dev(dum), "set_address = %d\n", 1639 w_value); 1640 ret_val = 0; 1641 break; 1642 case USB_REQ_SET_FEATURE: 1643 if (setup->bRequestType == Dev_Request) { 1644 ret_val = 0; 1645 switch (w_value) { 1646 case USB_DEVICE_REMOTE_WAKEUP: 1647 break; 1648 case USB_DEVICE_B_HNP_ENABLE: 1649 dum->gadget.b_hnp_enable = 1; 1650 break; 1651 case USB_DEVICE_A_HNP_SUPPORT: 1652 dum->gadget.a_hnp_support = 1; 1653 break; 1654 case USB_DEVICE_A_ALT_HNP_SUPPORT: 1655 dum->gadget.a_alt_hnp_support = 1; 1656 break; 1657 case USB_DEVICE_U1_ENABLE: 1658 if (dummy_hcd_to_hcd(dum_hcd)->speed == 1659 HCD_USB3) 1660 w_value = USB_DEV_STAT_U1_ENABLED; 1661 else 1662 ret_val = -EOPNOTSUPP; 1663 break; 1664 case USB_DEVICE_U2_ENABLE: 1665 if (dummy_hcd_to_hcd(dum_hcd)->speed == 1666 HCD_USB3) 1667 w_value = USB_DEV_STAT_U2_ENABLED; 1668 else 1669 ret_val = -EOPNOTSUPP; 1670 break; 1671 case USB_DEVICE_LTM_ENABLE: 1672 if (dummy_hcd_to_hcd(dum_hcd)->speed == 1673 HCD_USB3) 1674 w_value = USB_DEV_STAT_LTM_ENABLED; 1675 else 1676 ret_val = -EOPNOTSUPP; 1677 break; 1678 default: 1679 ret_val = -EOPNOTSUPP; 1680 } 1681 if (ret_val == 0) { 1682 dum->devstatus |= (1 << w_value); 1683 *status = 0; 1684 } 1685 } else if (setup->bRequestType == Ep_Request) { 1686 /* endpoint halt */ 1687 ep2 = find_endpoint(dum, w_index); 1688 if (!ep2 || ep2->ep.name == ep0name) { 1689 ret_val = -EOPNOTSUPP; 1690 break; 1691 } 1692 ep2->halted = 1; 1693 ret_val = 0; 1694 *status = 0; 1695 } 1696 break; 1697 case USB_REQ_CLEAR_FEATURE: 1698 if (setup->bRequestType == Dev_Request) { 1699 ret_val = 0; 1700 switch (w_value) { 1701 case USB_DEVICE_REMOTE_WAKEUP: 1702 w_value = USB_DEVICE_REMOTE_WAKEUP; 1703 break; 1704 case USB_DEVICE_U1_ENABLE: 1705 if (dummy_hcd_to_hcd(dum_hcd)->speed == 1706 HCD_USB3) 1707 w_value = USB_DEV_STAT_U1_ENABLED; 1708 else 1709 ret_val = -EOPNOTSUPP; 1710 break; 1711 case USB_DEVICE_U2_ENABLE: 1712 if (dummy_hcd_to_hcd(dum_hcd)->speed == 1713 HCD_USB3) 1714 w_value = USB_DEV_STAT_U2_ENABLED; 1715 else 1716 ret_val = -EOPNOTSUPP; 1717 break; 1718 case USB_DEVICE_LTM_ENABLE: 1719 if (dummy_hcd_to_hcd(dum_hcd)->speed == 1720 HCD_USB3) 1721 w_value = USB_DEV_STAT_LTM_ENABLED; 1722 else 1723 ret_val = -EOPNOTSUPP; 1724 break; 1725 default: 1726 ret_val = -EOPNOTSUPP; 1727 break; 1728 } 1729 if (ret_val == 0) { 1730 dum->devstatus &= ~(1 << w_value); 1731 *status = 0; 1732 } 1733 } else if (setup->bRequestType == Ep_Request) { 1734 /* endpoint halt */ 1735 ep2 = find_endpoint(dum, w_index); 1736 if (!ep2) { 1737 ret_val = -EOPNOTSUPP; 1738 break; 1739 } 1740 if (!ep2->wedged) 1741 ep2->halted = 0; 1742 ret_val = 0; 1743 *status = 0; 1744 } 1745 break; 1746 case USB_REQ_GET_STATUS: 1747 if (setup->bRequestType == Dev_InRequest 1748 || setup->bRequestType == Intf_InRequest 1749 || setup->bRequestType == Ep_InRequest) { 1750 char *buf; 1751 /* 1752 * device: remote wakeup, selfpowered 1753 * interface: nothing 1754 * endpoint: halt 1755 */ 1756 buf = (char *)urb->transfer_buffer; 1757 if (urb->transfer_buffer_length > 0) { 1758 if (setup->bRequestType == Ep_InRequest) { 1759 ep2 = find_endpoint(dum, w_index); 1760 if (!ep2) { 1761 ret_val = -EOPNOTSUPP; 1762 break; 1763 } 1764 buf[0] = ep2->halted; 1765 } else if (setup->bRequestType == 1766 Dev_InRequest) { 1767 buf[0] = (u8)dum->devstatus; 1768 } else 1769 buf[0] = 0; 1770 } 1771 if (urb->transfer_buffer_length > 1) 1772 buf[1] = 0; 1773 urb->actual_length = min_t(u32, 2, 1774 urb->transfer_buffer_length); 1775 ret_val = 0; 1776 *status = 0; 1777 } 1778 break; 1779 } 1780 return ret_val; 1781 } 1782 1783 /* 1784 * Drive both sides of the transfers; looks like irq handlers to both 1785 * drivers except that the callbacks are invoked from soft interrupt 1786 * context. 1787 */ 1788 static enum hrtimer_restart dummy_timer(struct hrtimer *t) 1789 { 1790 struct dummy_hcd *dum_hcd = timer_container_of(dum_hcd, t, 1791 timer); 1792 struct dummy *dum = dum_hcd->dum; 1793 struct urbp *urbp, *tmp; 1794 unsigned long flags; 1795 int limit, total; 1796 int i; 1797 1798 /* simplistic model for one frame's bandwidth */ 1799 /* FIXME: account for transaction and packet overhead */ 1800 switch (dum->gadget.speed) { 1801 case USB_SPEED_LOW: 1802 total = 8/*bytes*/ * 12/*packets*/; 1803 break; 1804 case USB_SPEED_FULL: 1805 total = 64/*bytes*/ * 19/*packets*/; 1806 break; 1807 case USB_SPEED_HIGH: 1808 total = 512/*bytes*/ * 13/*packets*/ * 8/*uframes*/; 1809 break; 1810 case USB_SPEED_SUPER: 1811 /* Bus speed is 500000 bytes/ms, so use a little less */ 1812 total = 490000; 1813 break; 1814 default: /* Can't happen */ 1815 dev_err(dummy_dev(dum_hcd), "bogus device speed\n"); 1816 total = 0; 1817 break; 1818 } 1819 1820 /* look at each urb queued by the host side driver */ 1821 spin_lock_irqsave(&dum->lock, flags); 1822 dum_hcd->timer_pending = 0; 1823 1824 if (!dum_hcd->udev) { 1825 dev_err(dummy_dev(dum_hcd), 1826 "timer fired with no URBs pending?\n"); 1827 spin_unlock_irqrestore(&dum->lock, flags); 1828 return HRTIMER_NORESTART; 1829 } 1830 dum_hcd->next_frame_urbp = NULL; 1831 1832 for (i = 0; i < DUMMY_ENDPOINTS; i++) { 1833 if (!ep_info[i].name) 1834 break; 1835 dum->ep[i].already_seen = 0; 1836 } 1837 1838 restart: 1839 list_for_each_entry_safe(urbp, tmp, &dum_hcd->urbp_list, urbp_list) { 1840 struct urb *urb; 1841 struct dummy_request *req; 1842 u8 address; 1843 struct dummy_ep *ep = NULL; 1844 int status = -EINPROGRESS; 1845 1846 /* stop when we reach URBs queued after the timer interrupt */ 1847 if (urbp == dum_hcd->next_frame_urbp) 1848 break; 1849 1850 urb = urbp->urb; 1851 if (urb->unlinked) 1852 goto return_urb; 1853 else if (dum_hcd->rh_state != DUMMY_RH_RUNNING) 1854 continue; 1855 1856 /* Used up this frame's bandwidth? */ 1857 if (total <= 0) 1858 continue; 1859 1860 /* find the gadget's ep for this request (if configured) */ 1861 address = usb_pipeendpoint (urb->pipe); 1862 if (usb_urb_dir_in(urb)) 1863 address |= USB_DIR_IN; 1864 ep = find_endpoint(dum, address); 1865 if (!ep) { 1866 /* set_configuration() disagreement */ 1867 dev_dbg(dummy_dev(dum_hcd), 1868 "no ep configured for urb %p\n", 1869 urb); 1870 status = -EPROTO; 1871 goto return_urb; 1872 } 1873 1874 if (ep->already_seen) 1875 continue; 1876 ep->already_seen = 1; 1877 if (ep == &dum->ep[0] && urb->error_count) { 1878 ep->setup_stage = 1; /* a new urb */ 1879 urb->error_count = 0; 1880 } 1881 if (ep->halted && !ep->setup_stage) { 1882 /* NOTE: must not be iso! */ 1883 dev_dbg(dummy_dev(dum_hcd), "ep %s halted, urb %p\n", 1884 ep->ep.name, urb); 1885 status = -EPIPE; 1886 goto return_urb; 1887 } 1888 /* FIXME make sure both ends agree on maxpacket */ 1889 1890 /* handle control requests */ 1891 if (ep == &dum->ep[0] && ep->setup_stage) { 1892 struct usb_ctrlrequest setup; 1893 int value; 1894 1895 setup = *(struct usb_ctrlrequest *) urb->setup_packet; 1896 /* paranoia, in case of stale queued data */ 1897 list_for_each_entry(req, &ep->queue, queue) { 1898 list_del_init(&req->queue); 1899 req->req.status = -EOVERFLOW; 1900 dev_dbg(udc_dev(dum), "stale req = %p\n", 1901 req); 1902 1903 spin_unlock(&dum->lock); 1904 usb_gadget_giveback_request(&ep->ep, &req->req); 1905 spin_lock(&dum->lock); 1906 ep->already_seen = 0; 1907 goto restart; 1908 } 1909 1910 /* gadget driver never sees set_address or operations 1911 * on standard feature flags. some hardware doesn't 1912 * even expose them. 1913 */ 1914 ep->last_io = jiffies; 1915 ep->setup_stage = 0; 1916 ep->halted = 0; 1917 1918 value = handle_control_request(dum_hcd, urb, &setup, 1919 &status); 1920 1921 /* gadget driver handles all other requests. block 1922 * until setup() returns; no reentrancy issues etc. 1923 */ 1924 if (value > 0) { 1925 ++dum->callback_usage; 1926 spin_unlock(&dum->lock); 1927 value = dum->driver->setup(&dum->gadget, 1928 &setup); 1929 spin_lock(&dum->lock); 1930 --dum->callback_usage; 1931 1932 if (value >= 0) { 1933 /* no delays (max 64KB data stage) */ 1934 limit = 64*1024; 1935 goto treat_control_like_bulk; 1936 } 1937 /* error, see below */ 1938 } 1939 1940 if (value < 0) { 1941 if (value != -EOPNOTSUPP) 1942 dev_dbg(udc_dev(dum), 1943 "setup --> %d\n", 1944 value); 1945 status = -EPIPE; 1946 urb->actual_length = 0; 1947 } 1948 1949 goto return_urb; 1950 } 1951 1952 /* non-control requests */ 1953 limit = total; 1954 switch (usb_pipetype(urb->pipe)) { 1955 case PIPE_ISOCHRONOUS: 1956 /* 1957 * We don't support isochronous. But if we did, 1958 * here are some of the issues we'd have to face: 1959 * 1960 * Is it urb->interval since the last xfer? 1961 * Use urb->iso_frame_desc[i]. 1962 * Complete whether or not ep has requests queued. 1963 * Report random errors, to debug drivers. 1964 */ 1965 limit = max(limit, periodic_bytes(dum, ep)); 1966 status = -EINVAL; /* fail all xfers */ 1967 break; 1968 1969 case PIPE_INTERRUPT: 1970 /* FIXME is it urb->interval since the last xfer? 1971 * this almost certainly polls too fast. 1972 */ 1973 limit = max(limit, periodic_bytes(dum, ep)); 1974 fallthrough; 1975 1976 default: 1977 treat_control_like_bulk: 1978 ep->last_io = jiffies; 1979 total -= transfer(dum_hcd, urb, ep, limit, &status); 1980 break; 1981 } 1982 1983 /* incomplete transfer? */ 1984 if (status == -EINPROGRESS) 1985 continue; 1986 1987 return_urb: 1988 list_del(&urbp->urbp_list); 1989 kfree(urbp); 1990 if (ep) 1991 ep->already_seen = ep->setup_stage = 0; 1992 1993 usb_hcd_unlink_urb_from_ep(dummy_hcd_to_hcd(dum_hcd), urb); 1994 spin_unlock(&dum->lock); 1995 usb_hcd_giveback_urb(dummy_hcd_to_hcd(dum_hcd), urb, status); 1996 spin_lock(&dum->lock); 1997 1998 goto restart; 1999 } 2000 2001 if (list_empty(&dum_hcd->urbp_list)) { 2002 usb_put_dev(dum_hcd->udev); 2003 dum_hcd->udev = NULL; 2004 } else if (!dum_hcd->timer_pending && 2005 dum_hcd->rh_state == DUMMY_RH_RUNNING) { 2006 /* want a 1 msec delay here */ 2007 dum_hcd->timer_pending = 1; 2008 hrtimer_start(&dum_hcd->timer, ns_to_ktime(DUMMY_TIMER_INT_NSECS), 2009 HRTIMER_MODE_REL_SOFT); 2010 } 2011 2012 spin_unlock_irqrestore(&dum->lock, flags); 2013 2014 return HRTIMER_NORESTART; 2015 } 2016 2017 /*-------------------------------------------------------------------------*/ 2018 2019 #define PORT_C_MASK \ 2020 ((USB_PORT_STAT_C_CONNECTION \ 2021 | USB_PORT_STAT_C_ENABLE \ 2022 | USB_PORT_STAT_C_SUSPEND \ 2023 | USB_PORT_STAT_C_OVERCURRENT \ 2024 | USB_PORT_STAT_C_RESET) << 16) 2025 2026 static int dummy_hub_status(struct usb_hcd *hcd, char *buf) 2027 { 2028 struct dummy_hcd *dum_hcd; 2029 unsigned long flags; 2030 int retval = 0; 2031 2032 dum_hcd = hcd_to_dummy_hcd(hcd); 2033 2034 spin_lock_irqsave(&dum_hcd->dum->lock, flags); 2035 if (!HCD_HW_ACCESSIBLE(hcd)) 2036 goto done; 2037 2038 if (dum_hcd->resuming && time_after_eq(jiffies, dum_hcd->re_timeout)) { 2039 dum_hcd->port_status |= (USB_PORT_STAT_C_SUSPEND << 16); 2040 dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND; 2041 set_link_state(dum_hcd); 2042 } 2043 2044 if ((dum_hcd->port_status & PORT_C_MASK) != 0) { 2045 *buf = (1 << 1); 2046 dev_dbg(dummy_dev(dum_hcd), "port status 0x%08x has changes\n", 2047 dum_hcd->port_status); 2048 retval = 1; 2049 if (dum_hcd->rh_state == DUMMY_RH_SUSPENDED) 2050 usb_hcd_resume_root_hub(hcd); 2051 } 2052 done: 2053 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags); 2054 return retval; 2055 } 2056 2057 /* usb 3.0 root hub device descriptor */ 2058 static struct { 2059 struct usb_bos_descriptor bos; 2060 struct usb_ss_cap_descriptor ss_cap; 2061 } __packed usb3_bos_desc = { 2062 2063 .bos = { 2064 .bLength = USB_DT_BOS_SIZE, 2065 .bDescriptorType = USB_DT_BOS, 2066 .wTotalLength = cpu_to_le16(sizeof(usb3_bos_desc)), 2067 .bNumDeviceCaps = 1, 2068 }, 2069 .ss_cap = { 2070 .bLength = USB_DT_USB_SS_CAP_SIZE, 2071 .bDescriptorType = USB_DT_DEVICE_CAPABILITY, 2072 .bDevCapabilityType = USB_SS_CAP_TYPE, 2073 .wSpeedSupported = cpu_to_le16(USB_5GBPS_OPERATION), 2074 .bFunctionalitySupport = ilog2(USB_5GBPS_OPERATION), 2075 }, 2076 }; 2077 2078 static inline void 2079 ss_hub_descriptor(struct usb_hub_descriptor *desc) 2080 { 2081 memset(desc, 0, sizeof *desc); 2082 desc->bDescriptorType = USB_DT_SS_HUB; 2083 desc->bDescLength = 12; 2084 desc->wHubCharacteristics = cpu_to_le16( 2085 HUB_CHAR_INDV_PORT_LPSM | 2086 HUB_CHAR_COMMON_OCPM); 2087 desc->bNbrPorts = 1; 2088 desc->u.ss.bHubHdrDecLat = 0x04; /* Worst case: 0.4 micro sec*/ 2089 desc->u.ss.DeviceRemovable = 0; 2090 } 2091 2092 static inline void hub_descriptor(struct usb_hub_descriptor *desc) 2093 { 2094 memset(desc, 0, sizeof *desc); 2095 desc->bDescriptorType = USB_DT_HUB; 2096 desc->bDescLength = 9; 2097 desc->wHubCharacteristics = cpu_to_le16( 2098 HUB_CHAR_INDV_PORT_LPSM | 2099 HUB_CHAR_COMMON_OCPM); 2100 desc->bNbrPorts = 1; 2101 desc->u.hs.DeviceRemovable[0] = 0; 2102 desc->u.hs.DeviceRemovable[1] = 0xff; /* PortPwrCtrlMask */ 2103 } 2104 2105 static int dummy_hub_control( 2106 struct usb_hcd *hcd, 2107 u16 typeReq, 2108 u16 wValue, 2109 u16 wIndex, 2110 char *buf, 2111 u16 wLength 2112 ) { 2113 struct dummy_hcd *dum_hcd; 2114 int retval = 0; 2115 unsigned long flags; 2116 2117 if (!HCD_HW_ACCESSIBLE(hcd)) 2118 return -ETIMEDOUT; 2119 2120 dum_hcd = hcd_to_dummy_hcd(hcd); 2121 2122 spin_lock_irqsave(&dum_hcd->dum->lock, flags); 2123 switch (typeReq) { 2124 case ClearHubFeature: 2125 break; 2126 case ClearPortFeature: 2127 switch (wValue) { 2128 case USB_PORT_FEAT_SUSPEND: 2129 if (hcd->speed == HCD_USB3) { 2130 dev_dbg(dummy_dev(dum_hcd), 2131 "USB_PORT_FEAT_SUSPEND req not " 2132 "supported for USB 3.0 roothub\n"); 2133 goto error; 2134 } 2135 if (dum_hcd->port_status & USB_PORT_STAT_SUSPEND) { 2136 /* 20msec resume signaling */ 2137 dum_hcd->resuming = 1; 2138 dum_hcd->re_timeout = jiffies + 2139 msecs_to_jiffies(20); 2140 } 2141 break; 2142 case USB_PORT_FEAT_POWER: 2143 dev_dbg(dummy_dev(dum_hcd), "power-off\n"); 2144 if (hcd->speed == HCD_USB3) 2145 dum_hcd->port_status &= ~USB_SS_PORT_STAT_POWER; 2146 else 2147 dum_hcd->port_status &= ~USB_PORT_STAT_POWER; 2148 set_link_state(dum_hcd); 2149 break; 2150 case USB_PORT_FEAT_ENABLE: 2151 case USB_PORT_FEAT_C_ENABLE: 2152 case USB_PORT_FEAT_C_SUSPEND: 2153 /* Not allowed for USB-3 */ 2154 if (hcd->speed == HCD_USB3) 2155 goto error; 2156 fallthrough; 2157 case USB_PORT_FEAT_C_CONNECTION: 2158 case USB_PORT_FEAT_C_RESET: 2159 dum_hcd->port_status &= ~(1 << wValue); 2160 set_link_state(dum_hcd); 2161 break; 2162 default: 2163 /* Disallow INDICATOR and C_OVER_CURRENT */ 2164 goto error; 2165 } 2166 break; 2167 case GetHubDescriptor: 2168 if (hcd->speed == HCD_USB3 && 2169 (wLength < USB_DT_SS_HUB_SIZE || 2170 wValue != (USB_DT_SS_HUB << 8))) { 2171 dev_dbg(dummy_dev(dum_hcd), 2172 "Wrong hub descriptor type for " 2173 "USB 3.0 roothub.\n"); 2174 goto error; 2175 } 2176 if (hcd->speed == HCD_USB3) 2177 ss_hub_descriptor((struct usb_hub_descriptor *) buf); 2178 else 2179 hub_descriptor((struct usb_hub_descriptor *) buf); 2180 break; 2181 2182 case DeviceRequest | USB_REQ_GET_DESCRIPTOR: 2183 if (hcd->speed != HCD_USB3) 2184 goto error; 2185 2186 if ((wValue >> 8) != USB_DT_BOS) 2187 goto error; 2188 2189 memcpy(buf, &usb3_bos_desc, sizeof(usb3_bos_desc)); 2190 retval = sizeof(usb3_bos_desc); 2191 break; 2192 2193 case GetHubStatus: 2194 *(__le32 *) buf = cpu_to_le32(0); 2195 break; 2196 case GetPortStatus: 2197 if (wIndex != 1) 2198 retval = -EPIPE; 2199 2200 /* whoever resets or resumes must GetPortStatus to 2201 * complete it!! 2202 */ 2203 if (dum_hcd->resuming && 2204 time_after_eq(jiffies, dum_hcd->re_timeout)) { 2205 dum_hcd->port_status |= (USB_PORT_STAT_C_SUSPEND << 16); 2206 dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND; 2207 } 2208 if ((dum_hcd->port_status & USB_PORT_STAT_RESET) != 0 && 2209 time_after_eq(jiffies, dum_hcd->re_timeout)) { 2210 dum_hcd->port_status |= (USB_PORT_STAT_C_RESET << 16); 2211 dum_hcd->port_status &= ~USB_PORT_STAT_RESET; 2212 if (dum_hcd->dum->pullup) { 2213 dum_hcd->port_status |= USB_PORT_STAT_ENABLE; 2214 2215 if (hcd->speed < HCD_USB3) { 2216 switch (dum_hcd->dum->gadget.speed) { 2217 case USB_SPEED_HIGH: 2218 dum_hcd->port_status |= 2219 USB_PORT_STAT_HIGH_SPEED; 2220 break; 2221 case USB_SPEED_LOW: 2222 dum_hcd->dum->gadget.ep0-> 2223 maxpacket = 8; 2224 dum_hcd->port_status |= 2225 USB_PORT_STAT_LOW_SPEED; 2226 break; 2227 default: 2228 break; 2229 } 2230 } 2231 } 2232 } 2233 set_link_state(dum_hcd); 2234 ((__le16 *) buf)[0] = cpu_to_le16(dum_hcd->port_status); 2235 ((__le16 *) buf)[1] = cpu_to_le16(dum_hcd->port_status >> 16); 2236 break; 2237 case SetHubFeature: 2238 retval = -EPIPE; 2239 break; 2240 case SetPortFeature: 2241 switch (wValue) { 2242 case USB_PORT_FEAT_LINK_STATE: 2243 if (hcd->speed != HCD_USB3) { 2244 dev_dbg(dummy_dev(dum_hcd), 2245 "USB_PORT_FEAT_LINK_STATE req not " 2246 "supported for USB 2.0 roothub\n"); 2247 goto error; 2248 } 2249 /* 2250 * Since this is dummy we don't have an actual link so 2251 * there is nothing to do for the SET_LINK_STATE cmd 2252 */ 2253 break; 2254 case USB_PORT_FEAT_U1_TIMEOUT: 2255 case USB_PORT_FEAT_U2_TIMEOUT: 2256 /* TODO: add suspend/resume support! */ 2257 if (hcd->speed != HCD_USB3) { 2258 dev_dbg(dummy_dev(dum_hcd), 2259 "USB_PORT_FEAT_U1/2_TIMEOUT req not " 2260 "supported for USB 2.0 roothub\n"); 2261 goto error; 2262 } 2263 break; 2264 case USB_PORT_FEAT_SUSPEND: 2265 /* Applicable only for USB2.0 hub */ 2266 if (hcd->speed == HCD_USB3) { 2267 dev_dbg(dummy_dev(dum_hcd), 2268 "USB_PORT_FEAT_SUSPEND req not " 2269 "supported for USB 3.0 roothub\n"); 2270 goto error; 2271 } 2272 if (dum_hcd->active) { 2273 dum_hcd->port_status |= USB_PORT_STAT_SUSPEND; 2274 2275 /* HNP would happen here; for now we 2276 * assume b_bus_req is always true. 2277 */ 2278 set_link_state(dum_hcd); 2279 if (((1 << USB_DEVICE_B_HNP_ENABLE) 2280 & dum_hcd->dum->devstatus) != 0) 2281 dev_dbg(dummy_dev(dum_hcd), 2282 "no HNP yet!\n"); 2283 } 2284 break; 2285 case USB_PORT_FEAT_POWER: 2286 if (hcd->speed == HCD_USB3) 2287 dum_hcd->port_status |= USB_SS_PORT_STAT_POWER; 2288 else 2289 dum_hcd->port_status |= USB_PORT_STAT_POWER; 2290 set_link_state(dum_hcd); 2291 break; 2292 case USB_PORT_FEAT_BH_PORT_RESET: 2293 /* Applicable only for USB3.0 hub */ 2294 if (hcd->speed != HCD_USB3) { 2295 dev_dbg(dummy_dev(dum_hcd), 2296 "USB_PORT_FEAT_BH_PORT_RESET req not " 2297 "supported for USB 2.0 roothub\n"); 2298 goto error; 2299 } 2300 fallthrough; 2301 case USB_PORT_FEAT_RESET: 2302 if (!(dum_hcd->port_status & USB_PORT_STAT_CONNECTION)) 2303 break; 2304 /* if it's already enabled, disable */ 2305 if (hcd->speed == HCD_USB3) { 2306 dum_hcd->port_status = 2307 (USB_SS_PORT_STAT_POWER | 2308 USB_PORT_STAT_CONNECTION | 2309 USB_PORT_STAT_RESET); 2310 } else { 2311 dum_hcd->port_status &= ~(USB_PORT_STAT_ENABLE 2312 | USB_PORT_STAT_LOW_SPEED 2313 | USB_PORT_STAT_HIGH_SPEED); 2314 dum_hcd->port_status |= USB_PORT_STAT_RESET; 2315 } 2316 /* 2317 * We want to reset device status. All but the 2318 * Self powered feature 2319 */ 2320 dum_hcd->dum->devstatus &= 2321 (1 << USB_DEVICE_SELF_POWERED); 2322 /* 2323 * FIXME USB3.0: what is the correct reset signaling 2324 * interval? Is it still 50msec as for HS? 2325 */ 2326 dum_hcd->re_timeout = jiffies + msecs_to_jiffies(50); 2327 set_link_state(dum_hcd); 2328 break; 2329 case USB_PORT_FEAT_C_CONNECTION: 2330 case USB_PORT_FEAT_C_RESET: 2331 case USB_PORT_FEAT_C_ENABLE: 2332 case USB_PORT_FEAT_C_SUSPEND: 2333 /* Not allowed for USB-3, and ignored for USB-2 */ 2334 if (hcd->speed == HCD_USB3) 2335 goto error; 2336 break; 2337 default: 2338 /* Disallow TEST, INDICATOR, and C_OVER_CURRENT */ 2339 goto error; 2340 } 2341 break; 2342 case GetPortErrorCount: 2343 if (hcd->speed != HCD_USB3) { 2344 dev_dbg(dummy_dev(dum_hcd), 2345 "GetPortErrorCount req not " 2346 "supported for USB 2.0 roothub\n"); 2347 goto error; 2348 } 2349 /* We'll always return 0 since this is a dummy hub */ 2350 *(__le32 *) buf = cpu_to_le32(0); 2351 break; 2352 case SetHubDepth: 2353 if (hcd->speed != HCD_USB3) { 2354 dev_dbg(dummy_dev(dum_hcd), 2355 "SetHubDepth req not supported for " 2356 "USB 2.0 roothub\n"); 2357 goto error; 2358 } 2359 break; 2360 default: 2361 dev_dbg(dummy_dev(dum_hcd), 2362 "hub control req%04x v%04x i%04x l%d\n", 2363 typeReq, wValue, wIndex, wLength); 2364 error: 2365 /* "protocol stall" on error */ 2366 retval = -EPIPE; 2367 } 2368 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags); 2369 2370 if ((dum_hcd->port_status & PORT_C_MASK) != 0) 2371 usb_hcd_poll_rh_status(hcd); 2372 return retval; 2373 } 2374 2375 static int dummy_bus_suspend(struct usb_hcd *hcd) 2376 { 2377 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd); 2378 2379 dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__); 2380 2381 spin_lock_irq(&dum_hcd->dum->lock); 2382 dum_hcd->rh_state = DUMMY_RH_SUSPENDED; 2383 set_link_state(dum_hcd); 2384 hcd->state = HC_STATE_SUSPENDED; 2385 spin_unlock_irq(&dum_hcd->dum->lock); 2386 return 0; 2387 } 2388 2389 static int dummy_bus_resume(struct usb_hcd *hcd) 2390 { 2391 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd); 2392 int rc = 0; 2393 2394 dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__); 2395 2396 spin_lock_irq(&dum_hcd->dum->lock); 2397 if (!HCD_HW_ACCESSIBLE(hcd)) { 2398 rc = -ESHUTDOWN; 2399 } else { 2400 dum_hcd->rh_state = DUMMY_RH_RUNNING; 2401 set_link_state(dum_hcd); 2402 if (!list_empty(&dum_hcd->urbp_list)) { 2403 dum_hcd->timer_pending = 1; 2404 hrtimer_start(&dum_hcd->timer, ns_to_ktime(0), HRTIMER_MODE_REL_SOFT); 2405 } 2406 hcd->state = HC_STATE_RUNNING; 2407 } 2408 spin_unlock_irq(&dum_hcd->dum->lock); 2409 return rc; 2410 } 2411 2412 /*-------------------------------------------------------------------------*/ 2413 2414 static inline ssize_t show_urb(char *buf, size_t size, struct urb *urb) 2415 { 2416 int ep = usb_pipeendpoint(urb->pipe); 2417 2418 return scnprintf(buf, size, 2419 "urb/%p %s ep%d%s%s len %d/%d\n", 2420 urb, 2421 ({ char *s; 2422 switch (urb->dev->speed) { 2423 case USB_SPEED_LOW: 2424 s = "ls"; 2425 break; 2426 case USB_SPEED_FULL: 2427 s = "fs"; 2428 break; 2429 case USB_SPEED_HIGH: 2430 s = "hs"; 2431 break; 2432 case USB_SPEED_SUPER: 2433 s = "ss"; 2434 break; 2435 default: 2436 s = "?"; 2437 break; 2438 } s; }), 2439 ep, ep ? (usb_urb_dir_in(urb) ? "in" : "out") : "", 2440 ({ char *s; \ 2441 switch (usb_pipetype(urb->pipe)) { \ 2442 case PIPE_CONTROL: \ 2443 s = ""; \ 2444 break; \ 2445 case PIPE_BULK: \ 2446 s = "-bulk"; \ 2447 break; \ 2448 case PIPE_INTERRUPT: \ 2449 s = "-int"; \ 2450 break; \ 2451 default: \ 2452 s = "-iso"; \ 2453 break; \ 2454 } s; }), 2455 urb->actual_length, urb->transfer_buffer_length); 2456 } 2457 2458 static ssize_t urbs_show(struct device *dev, struct device_attribute *attr, 2459 char *buf) 2460 { 2461 struct usb_hcd *hcd = dev_get_drvdata(dev); 2462 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd); 2463 struct urbp *urbp; 2464 size_t size = 0; 2465 unsigned long flags; 2466 2467 spin_lock_irqsave(&dum_hcd->dum->lock, flags); 2468 list_for_each_entry(urbp, &dum_hcd->urbp_list, urbp_list) { 2469 size_t temp; 2470 2471 temp = show_urb(buf, PAGE_SIZE - size, urbp->urb); 2472 buf += temp; 2473 size += temp; 2474 } 2475 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags); 2476 2477 return size; 2478 } 2479 static DEVICE_ATTR_RO(urbs); 2480 2481 static int dummy_start_ss(struct dummy_hcd *dum_hcd) 2482 { 2483 hrtimer_setup(&dum_hcd->timer, dummy_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_SOFT); 2484 dum_hcd->rh_state = DUMMY_RH_RUNNING; 2485 dum_hcd->stream_en_ep = 0; 2486 INIT_LIST_HEAD(&dum_hcd->urbp_list); 2487 dummy_hcd_to_hcd(dum_hcd)->power_budget = POWER_BUDGET_3; 2488 dummy_hcd_to_hcd(dum_hcd)->state = HC_STATE_RUNNING; 2489 dummy_hcd_to_hcd(dum_hcd)->uses_new_polling = 1; 2490 #ifdef CONFIG_USB_OTG 2491 dummy_hcd_to_hcd(dum_hcd)->self.otg_port = 1; 2492 #endif 2493 return 0; 2494 2495 /* FIXME 'urbs' should be a per-device thing, maybe in usbcore */ 2496 return device_create_file(dummy_dev(dum_hcd), &dev_attr_urbs); 2497 } 2498 2499 static int dummy_start(struct usb_hcd *hcd) 2500 { 2501 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd); 2502 2503 /* 2504 * HOST side init ... we emulate a root hub that'll only ever 2505 * talk to one device (the gadget side). Also appears in sysfs, 2506 * just like more familiar pci-based HCDs. 2507 */ 2508 if (!usb_hcd_is_primary_hcd(hcd)) 2509 return dummy_start_ss(dum_hcd); 2510 2511 spin_lock_init(&dum_hcd->dum->lock); 2512 hrtimer_setup(&dum_hcd->timer, dummy_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_SOFT); 2513 dum_hcd->rh_state = DUMMY_RH_RUNNING; 2514 2515 INIT_LIST_HEAD(&dum_hcd->urbp_list); 2516 2517 hcd->power_budget = POWER_BUDGET; 2518 hcd->state = HC_STATE_RUNNING; 2519 hcd->uses_new_polling = 1; 2520 2521 #ifdef CONFIG_USB_OTG 2522 hcd->self.otg_port = 1; 2523 #endif 2524 2525 /* FIXME 'urbs' should be a per-device thing, maybe in usbcore */ 2526 return device_create_file(dummy_dev(dum_hcd), &dev_attr_urbs); 2527 } 2528 2529 static void dummy_stop(struct usb_hcd *hcd) 2530 { 2531 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd); 2532 2533 hrtimer_cancel(&dum_hcd->timer); 2534 dum_hcd->timer_pending = 0; 2535 device_remove_file(dummy_dev(dum_hcd), &dev_attr_urbs); 2536 dev_info(dummy_dev(dum_hcd), "stopped\n"); 2537 } 2538 2539 /*-------------------------------------------------------------------------*/ 2540 2541 static int dummy_h_get_frame(struct usb_hcd *hcd) 2542 { 2543 return dummy_g_get_frame(NULL); 2544 } 2545 2546 static int dummy_setup(struct usb_hcd *hcd) 2547 { 2548 struct dummy *dum; 2549 2550 dum = *((void **)dev_get_platdata(hcd->self.controller)); 2551 hcd->self.sg_tablesize = ~0; 2552 if (usb_hcd_is_primary_hcd(hcd)) { 2553 dum->hs_hcd = hcd_to_dummy_hcd(hcd); 2554 dum->hs_hcd->dum = dum; 2555 /* 2556 * Mark the first roothub as being USB 2.0. 2557 * The USB 3.0 roothub will be registered later by 2558 * dummy_hcd_probe() 2559 */ 2560 hcd->speed = HCD_USB2; 2561 hcd->self.root_hub->speed = USB_SPEED_HIGH; 2562 } else { 2563 dum->ss_hcd = hcd_to_dummy_hcd(hcd); 2564 dum->ss_hcd->dum = dum; 2565 hcd->speed = HCD_USB3; 2566 hcd->self.root_hub->speed = USB_SPEED_SUPER; 2567 } 2568 return 0; 2569 } 2570 2571 /* Change a group of bulk endpoints to support multiple stream IDs */ 2572 static int dummy_alloc_streams(struct usb_hcd *hcd, struct usb_device *udev, 2573 struct usb_host_endpoint **eps, unsigned int num_eps, 2574 unsigned int num_streams, gfp_t mem_flags) 2575 { 2576 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd); 2577 unsigned long flags; 2578 int max_stream; 2579 int ret_streams = num_streams; 2580 unsigned int index; 2581 unsigned int i; 2582 2583 if (!num_eps) 2584 return -EINVAL; 2585 2586 spin_lock_irqsave(&dum_hcd->dum->lock, flags); 2587 for (i = 0; i < num_eps; i++) { 2588 index = dummy_get_ep_idx(&eps[i]->desc); 2589 if ((1 << index) & dum_hcd->stream_en_ep) { 2590 ret_streams = -EINVAL; 2591 goto out; 2592 } 2593 max_stream = usb_ss_max_streams(&eps[i]->ss_ep_comp); 2594 if (!max_stream) { 2595 ret_streams = -EINVAL; 2596 goto out; 2597 } 2598 if (max_stream < ret_streams) { 2599 dev_dbg(dummy_dev(dum_hcd), "Ep 0x%x only supports %u " 2600 "stream IDs.\n", 2601 eps[i]->desc.bEndpointAddress, 2602 max_stream); 2603 ret_streams = max_stream; 2604 } 2605 } 2606 2607 for (i = 0; i < num_eps; i++) { 2608 index = dummy_get_ep_idx(&eps[i]->desc); 2609 dum_hcd->stream_en_ep |= 1 << index; 2610 set_max_streams_for_pipe(dum_hcd, 2611 usb_endpoint_num(&eps[i]->desc), ret_streams); 2612 } 2613 out: 2614 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags); 2615 return ret_streams; 2616 } 2617 2618 /* Reverts a group of bulk endpoints back to not using stream IDs. */ 2619 static int dummy_free_streams(struct usb_hcd *hcd, struct usb_device *udev, 2620 struct usb_host_endpoint **eps, unsigned int num_eps, 2621 gfp_t mem_flags) 2622 { 2623 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd); 2624 unsigned long flags; 2625 int ret; 2626 unsigned int index; 2627 unsigned int i; 2628 2629 spin_lock_irqsave(&dum_hcd->dum->lock, flags); 2630 for (i = 0; i < num_eps; i++) { 2631 index = dummy_get_ep_idx(&eps[i]->desc); 2632 if (!((1 << index) & dum_hcd->stream_en_ep)) { 2633 ret = -EINVAL; 2634 goto out; 2635 } 2636 } 2637 2638 for (i = 0; i < num_eps; i++) { 2639 index = dummy_get_ep_idx(&eps[i]->desc); 2640 dum_hcd->stream_en_ep &= ~(1 << index); 2641 set_max_streams_for_pipe(dum_hcd, 2642 usb_endpoint_num(&eps[i]->desc), 0); 2643 } 2644 ret = 0; 2645 out: 2646 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags); 2647 return ret; 2648 } 2649 2650 static struct hc_driver dummy_hcd = { 2651 .description = (char *) driver_name, 2652 .product_desc = "Dummy host controller", 2653 .hcd_priv_size = sizeof(struct dummy_hcd), 2654 2655 .reset = dummy_setup, 2656 .start = dummy_start, 2657 .stop = dummy_stop, 2658 2659 .urb_enqueue = dummy_urb_enqueue, 2660 .urb_dequeue = dummy_urb_dequeue, 2661 2662 .get_frame_number = dummy_h_get_frame, 2663 2664 .hub_status_data = dummy_hub_status, 2665 .hub_control = dummy_hub_control, 2666 .bus_suspend = dummy_bus_suspend, 2667 .bus_resume = dummy_bus_resume, 2668 2669 .alloc_streams = dummy_alloc_streams, 2670 .free_streams = dummy_free_streams, 2671 }; 2672 2673 static int dummy_hcd_probe(struct platform_device *pdev) 2674 { 2675 struct dummy *dum; 2676 struct usb_hcd *hs_hcd; 2677 struct usb_hcd *ss_hcd; 2678 int retval; 2679 2680 dev_info(&pdev->dev, "%s, driver " DRIVER_VERSION "\n", driver_desc); 2681 dum = *((void **)dev_get_platdata(&pdev->dev)); 2682 2683 if (mod_data.is_super_speed) 2684 dummy_hcd.flags = HCD_USB3 | HCD_SHARED; 2685 else if (mod_data.is_high_speed) 2686 dummy_hcd.flags = HCD_USB2; 2687 else 2688 dummy_hcd.flags = HCD_USB11; 2689 hs_hcd = usb_create_hcd(&dummy_hcd, &pdev->dev, dev_name(&pdev->dev)); 2690 if (!hs_hcd) 2691 return -ENOMEM; 2692 hs_hcd->has_tt = 1; 2693 2694 retval = usb_add_hcd(hs_hcd, 0, 0); 2695 if (retval) 2696 goto put_usb2_hcd; 2697 2698 if (mod_data.is_super_speed) { 2699 ss_hcd = usb_create_shared_hcd(&dummy_hcd, &pdev->dev, 2700 dev_name(&pdev->dev), hs_hcd); 2701 if (!ss_hcd) { 2702 retval = -ENOMEM; 2703 goto dealloc_usb2_hcd; 2704 } 2705 2706 retval = usb_add_hcd(ss_hcd, 0, 0); 2707 if (retval) 2708 goto put_usb3_hcd; 2709 } 2710 return 0; 2711 2712 put_usb3_hcd: 2713 usb_put_hcd(ss_hcd); 2714 dealloc_usb2_hcd: 2715 usb_remove_hcd(hs_hcd); 2716 put_usb2_hcd: 2717 usb_put_hcd(hs_hcd); 2718 dum->hs_hcd = dum->ss_hcd = NULL; 2719 return retval; 2720 } 2721 2722 static void dummy_hcd_remove(struct platform_device *pdev) 2723 { 2724 struct dummy *dum; 2725 2726 dum = hcd_to_dummy_hcd(platform_get_drvdata(pdev))->dum; 2727 2728 if (dum->ss_hcd) { 2729 usb_remove_hcd(dummy_hcd_to_hcd(dum->ss_hcd)); 2730 usb_put_hcd(dummy_hcd_to_hcd(dum->ss_hcd)); 2731 } 2732 2733 usb_remove_hcd(dummy_hcd_to_hcd(dum->hs_hcd)); 2734 usb_put_hcd(dummy_hcd_to_hcd(dum->hs_hcd)); 2735 2736 dum->hs_hcd = NULL; 2737 dum->ss_hcd = NULL; 2738 } 2739 2740 static int dummy_hcd_suspend(struct platform_device *pdev, pm_message_t state) 2741 { 2742 struct usb_hcd *hcd; 2743 struct dummy_hcd *dum_hcd; 2744 int rc = 0; 2745 2746 dev_dbg(&pdev->dev, "%s\n", __func__); 2747 2748 hcd = platform_get_drvdata(pdev); 2749 dum_hcd = hcd_to_dummy_hcd(hcd); 2750 if (dum_hcd->rh_state == DUMMY_RH_RUNNING) { 2751 dev_warn(&pdev->dev, "Root hub isn't suspended!\n"); 2752 rc = -EBUSY; 2753 } else 2754 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags); 2755 return rc; 2756 } 2757 2758 static int dummy_hcd_resume(struct platform_device *pdev) 2759 { 2760 struct usb_hcd *hcd; 2761 2762 dev_dbg(&pdev->dev, "%s\n", __func__); 2763 2764 hcd = platform_get_drvdata(pdev); 2765 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags); 2766 usb_hcd_poll_rh_status(hcd); 2767 return 0; 2768 } 2769 2770 static struct platform_driver dummy_hcd_driver = { 2771 .probe = dummy_hcd_probe, 2772 .remove = dummy_hcd_remove, 2773 .suspend = dummy_hcd_suspend, 2774 .resume = dummy_hcd_resume, 2775 .driver = { 2776 .name = driver_name, 2777 }, 2778 }; 2779 2780 /*-------------------------------------------------------------------------*/ 2781 #define MAX_NUM_UDC 32 2782 static struct platform_device *the_udc_pdev[MAX_NUM_UDC]; 2783 static struct platform_device *the_hcd_pdev[MAX_NUM_UDC]; 2784 2785 static int __init dummy_hcd_init(void) 2786 { 2787 int retval = -ENOMEM; 2788 int i; 2789 struct dummy *dum[MAX_NUM_UDC] = {}; 2790 2791 if (usb_disabled()) 2792 return -ENODEV; 2793 2794 if (!mod_data.is_high_speed && mod_data.is_super_speed) 2795 return -EINVAL; 2796 2797 if (mod_data.num < 1 || mod_data.num > MAX_NUM_UDC) { 2798 pr_err("Number of emulated UDC must be in range of 1...%d\n", 2799 MAX_NUM_UDC); 2800 return -EINVAL; 2801 } 2802 2803 for (i = 0; i < mod_data.num; i++) { 2804 the_hcd_pdev[i] = platform_device_alloc(driver_name, i); 2805 if (!the_hcd_pdev[i]) { 2806 i--; 2807 while (i >= 0) 2808 platform_device_put(the_hcd_pdev[i--]); 2809 return retval; 2810 } 2811 } 2812 for (i = 0; i < mod_data.num; i++) { 2813 the_udc_pdev[i] = platform_device_alloc(gadget_name, i); 2814 if (!the_udc_pdev[i]) { 2815 i--; 2816 while (i >= 0) 2817 platform_device_put(the_udc_pdev[i--]); 2818 goto err_alloc_udc; 2819 } 2820 } 2821 for (i = 0; i < mod_data.num; i++) { 2822 dum[i] = kzalloc(sizeof(struct dummy), GFP_KERNEL); 2823 if (!dum[i]) { 2824 retval = -ENOMEM; 2825 goto err_add_pdata; 2826 } 2827 retval = platform_device_add_data(the_hcd_pdev[i], &dum[i], 2828 sizeof(void *)); 2829 if (retval) 2830 goto err_add_pdata; 2831 retval = platform_device_add_data(the_udc_pdev[i], &dum[i], 2832 sizeof(void *)); 2833 if (retval) 2834 goto err_add_pdata; 2835 } 2836 2837 retval = platform_driver_register(&dummy_hcd_driver); 2838 if (retval < 0) 2839 goto err_add_pdata; 2840 retval = platform_driver_register(&dummy_udc_driver); 2841 if (retval < 0) 2842 goto err_register_udc_driver; 2843 2844 for (i = 0; i < mod_data.num; i++) { 2845 retval = platform_device_add(the_hcd_pdev[i]); 2846 if (retval < 0) { 2847 i--; 2848 while (i >= 0) 2849 platform_device_del(the_hcd_pdev[i--]); 2850 goto err_add_hcd; 2851 } 2852 } 2853 for (i = 0; i < mod_data.num; i++) { 2854 if (!dum[i]->hs_hcd || 2855 (!dum[i]->ss_hcd && mod_data.is_super_speed)) { 2856 /* 2857 * The hcd was added successfully but its probe 2858 * function failed for some reason. 2859 */ 2860 retval = -EINVAL; 2861 goto err_add_udc; 2862 } 2863 } 2864 2865 for (i = 0; i < mod_data.num; i++) { 2866 retval = platform_device_add(the_udc_pdev[i]); 2867 if (retval < 0) { 2868 i--; 2869 while (i >= 0) 2870 platform_device_del(the_udc_pdev[i--]); 2871 goto err_add_udc; 2872 } 2873 } 2874 2875 for (i = 0; i < mod_data.num; i++) { 2876 if (!platform_get_drvdata(the_udc_pdev[i])) { 2877 /* 2878 * The udc was added successfully but its probe 2879 * function failed for some reason. 2880 */ 2881 retval = -EINVAL; 2882 goto err_probe_udc; 2883 } 2884 } 2885 return retval; 2886 2887 err_probe_udc: 2888 for (i = 0; i < mod_data.num; i++) 2889 platform_device_del(the_udc_pdev[i]); 2890 err_add_udc: 2891 for (i = 0; i < mod_data.num; i++) 2892 platform_device_del(the_hcd_pdev[i]); 2893 err_add_hcd: 2894 platform_driver_unregister(&dummy_udc_driver); 2895 err_register_udc_driver: 2896 platform_driver_unregister(&dummy_hcd_driver); 2897 err_add_pdata: 2898 for (i = 0; i < mod_data.num; i++) 2899 kfree(dum[i]); 2900 for (i = 0; i < mod_data.num; i++) 2901 platform_device_put(the_udc_pdev[i]); 2902 err_alloc_udc: 2903 for (i = 0; i < mod_data.num; i++) 2904 platform_device_put(the_hcd_pdev[i]); 2905 return retval; 2906 } 2907 module_init(dummy_hcd_init); 2908 2909 static void __exit dummy_hcd_cleanup(void) 2910 { 2911 int i; 2912 2913 for (i = 0; i < mod_data.num; i++) { 2914 struct dummy *dum; 2915 2916 dum = *((void **)dev_get_platdata(&the_udc_pdev[i]->dev)); 2917 2918 platform_device_unregister(the_udc_pdev[i]); 2919 platform_device_unregister(the_hcd_pdev[i]); 2920 kfree(dum); 2921 } 2922 platform_driver_unregister(&dummy_udc_driver); 2923 platform_driver_unregister(&dummy_hcd_driver); 2924 } 2925 module_exit(dummy_hcd_cleanup); 2926