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