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