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