1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * udc.c - Core UDC Framework 4 * 5 * Copyright (C) 2010 Texas Instruments 6 * Author: Felipe Balbi <balbi@ti.com> 7 */ 8 9 #define pr_fmt(fmt) "UDC core: " fmt 10 11 #include <linux/kernel.h> 12 #include <linux/module.h> 13 #include <linux/device.h> 14 #include <linux/list.h> 15 #include <linux/idr.h> 16 #include <linux/err.h> 17 #include <linux/dma-mapping.h> 18 #include <linux/sched/task_stack.h> 19 #include <linux/workqueue.h> 20 21 #include <linux/usb/ch9.h> 22 #include <linux/usb/gadget.h> 23 #include <linux/usb.h> 24 25 #include "trace.h" 26 27 static DEFINE_IDA(gadget_id_numbers); 28 29 static const struct bus_type gadget_bus_type; 30 31 /** 32 * struct usb_udc - describes one usb device controller 33 * @driver: the gadget driver pointer. For use by the class code 34 * @gadget: the gadget. For use by the class code 35 * @gadget_release: the gadget's release routine 36 * @dev: the child device to the actual controller 37 * @list: for use by the udc class driver 38 * @vbus: for udcs who care about vbus status, this value is real vbus status; 39 * for udcs who do not care about vbus status, this value is always true 40 * @started: the UDC's started state. True if the UDC had started. 41 * @allow_connect: Indicates whether UDC is allowed to be pulled up. 42 * Set/cleared by gadget_(un)bind_driver() after gadget driver is bound or 43 * unbound. 44 * @vbus_work: work routine to handle VBUS status change notifications. 45 * @connect_lock: protects udc->started, gadget->connect, 46 * gadget->allow_connect and gadget->deactivate. The routines 47 * usb_gadget_connect_locked(), usb_gadget_disconnect_locked(), 48 * usb_udc_connect_control_locked(), usb_gadget_udc_start_locked() and 49 * usb_gadget_udc_stop_locked() are called with this lock held. 50 * 51 * This represents the internal data structure which is used by the UDC-class 52 * to hold information about udc driver and gadget together. 53 */ 54 struct usb_udc { 55 struct usb_gadget_driver *driver; 56 struct usb_gadget *gadget; 57 void (*gadget_release)(struct device *dev); 58 struct device dev; 59 struct list_head list; 60 bool vbus; 61 bool started; 62 bool allow_connect; 63 struct work_struct vbus_work; 64 struct mutex connect_lock; 65 }; 66 67 static const struct class udc_class; 68 static LIST_HEAD(udc_list); 69 70 /* Protects udc_list, udc->driver, driver->is_bound, and related calls */ 71 static DEFINE_MUTEX(udc_lock); 72 73 /* ------------------------------------------------------------------------- */ 74 75 /** 76 * usb_ep_set_maxpacket_limit - set maximum packet size limit for endpoint 77 * @ep:the endpoint being configured 78 * @maxpacket_limit:value of maximum packet size limit 79 * 80 * This function should be used only in UDC drivers to initialize endpoint 81 * (usually in probe function). 82 */ 83 void usb_ep_set_maxpacket_limit(struct usb_ep *ep, 84 unsigned maxpacket_limit) 85 { 86 ep->maxpacket_limit = maxpacket_limit; 87 ep->maxpacket = maxpacket_limit; 88 89 trace_usb_ep_set_maxpacket_limit(ep, 0); 90 } 91 EXPORT_SYMBOL_GPL(usb_ep_set_maxpacket_limit); 92 93 /** 94 * usb_ep_enable - configure endpoint, making it usable 95 * @ep:the endpoint being configured. may not be the endpoint named "ep0". 96 * drivers discover endpoints through the ep_list of a usb_gadget. 97 * 98 * When configurations are set, or when interface settings change, the driver 99 * will enable or disable the relevant endpoints. while it is enabled, an 100 * endpoint may be used for i/o until the driver receives a disconnect() from 101 * the host or until the endpoint is disabled. 102 * 103 * the ep0 implementation (which calls this routine) must ensure that the 104 * hardware capabilities of each endpoint match the descriptor provided 105 * for it. for example, an endpoint named "ep2in-bulk" would be usable 106 * for interrupt transfers as well as bulk, but it likely couldn't be used 107 * for iso transfers or for endpoint 14. some endpoints are fully 108 * configurable, with more generic names like "ep-a". (remember that for 109 * USB, "in" means "towards the USB host".) 110 * 111 * This routine may be called in an atomic (interrupt) context. 112 * 113 * returns zero, or a negative error code. 114 */ 115 int usb_ep_enable(struct usb_ep *ep) 116 { 117 int ret = 0; 118 119 if (ep->enabled) 120 goto out; 121 122 /* UDC drivers can't handle endpoints with maxpacket size 0 */ 123 if (!ep->desc || usb_endpoint_maxp(ep->desc) == 0) { 124 WARN_ONCE(1, "%s: ep%d (%s) has %s\n", __func__, ep->address, ep->name, 125 (!ep->desc) ? "NULL descriptor" : "maxpacket 0"); 126 127 ret = -EINVAL; 128 goto out; 129 } 130 131 ret = ep->ops->enable(ep, ep->desc); 132 if (ret) 133 goto out; 134 135 ep->enabled = true; 136 137 out: 138 trace_usb_ep_enable(ep, ret); 139 140 return ret; 141 } 142 EXPORT_SYMBOL_GPL(usb_ep_enable); 143 144 /** 145 * usb_ep_disable - endpoint is no longer usable 146 * @ep:the endpoint being unconfigured. may not be the endpoint named "ep0". 147 * 148 * no other task may be using this endpoint when this is called. 149 * any pending and uncompleted requests will complete with status 150 * indicating disconnect (-ESHUTDOWN) before this call returns. 151 * gadget drivers must call usb_ep_enable() again before queueing 152 * requests to the endpoint. 153 * 154 * This routine may be called in an atomic (interrupt) context. 155 * 156 * returns zero, or a negative error code. 157 */ 158 int usb_ep_disable(struct usb_ep *ep) 159 { 160 int ret = 0; 161 162 if (!ep->enabled) 163 goto out; 164 165 ret = ep->ops->disable(ep); 166 if (ret) 167 goto out; 168 169 ep->enabled = false; 170 171 out: 172 trace_usb_ep_disable(ep, ret); 173 174 return ret; 175 } 176 EXPORT_SYMBOL_GPL(usb_ep_disable); 177 178 /** 179 * usb_ep_alloc_request - allocate a request object to use with this endpoint 180 * @ep:the endpoint to be used with with the request 181 * @gfp_flags:GFP_* flags to use 182 * 183 * Request objects must be allocated with this call, since they normally 184 * need controller-specific setup and may even need endpoint-specific 185 * resources such as allocation of DMA descriptors. 186 * Requests may be submitted with usb_ep_queue(), and receive a single 187 * completion callback. Free requests with usb_ep_free_request(), when 188 * they are no longer needed. 189 * 190 * Returns the request, or null if one could not be allocated. 191 */ 192 struct usb_request *usb_ep_alloc_request(struct usb_ep *ep, 193 gfp_t gfp_flags) 194 { 195 struct usb_request *req = NULL; 196 197 req = ep->ops->alloc_request(ep, gfp_flags); 198 199 if (req) 200 req->ep = ep; 201 202 trace_usb_ep_alloc_request(ep, req, req ? 0 : -ENOMEM); 203 204 return req; 205 } 206 EXPORT_SYMBOL_GPL(usb_ep_alloc_request); 207 208 /** 209 * usb_ep_free_request - frees a request object 210 * @ep:the endpoint associated with the request 211 * @req:the request being freed 212 * 213 * Reverses the effect of usb_ep_alloc_request(). 214 * Caller guarantees the request is not queued, and that it will 215 * no longer be requeued (or otherwise used). 216 */ 217 void usb_ep_free_request(struct usb_ep *ep, 218 struct usb_request *req) 219 { 220 trace_usb_ep_free_request(ep, req, 0); 221 ep->ops->free_request(ep, req); 222 } 223 EXPORT_SYMBOL_GPL(usb_ep_free_request); 224 225 /** 226 * usb_ep_queue - queues (submits) an I/O request to an endpoint. 227 * @ep:the endpoint associated with the request 228 * @req:the request being submitted 229 * @gfp_flags: GFP_* flags to use in case the lower level driver couldn't 230 * pre-allocate all necessary memory with the request. 231 * 232 * This tells the device controller to perform the specified request through 233 * that endpoint (reading or writing a buffer). When the request completes, 234 * including being canceled by usb_ep_dequeue(), the request's completion 235 * routine is called to return the request to the driver. Any endpoint 236 * (except control endpoints like ep0) may have more than one transfer 237 * request queued; they complete in FIFO order. Once a gadget driver 238 * submits a request, that request may not be examined or modified until it 239 * is given back to that driver through the completion callback. 240 * 241 * Each request is turned into one or more packets. The controller driver 242 * never merges adjacent requests into the same packet. OUT transfers 243 * will sometimes use data that's already buffered in the hardware. 244 * Drivers can rely on the fact that the first byte of the request's buffer 245 * always corresponds to the first byte of some USB packet, for both 246 * IN and OUT transfers. 247 * 248 * Bulk endpoints can queue any amount of data; the transfer is packetized 249 * automatically. The last packet will be short if the request doesn't fill it 250 * out completely. Zero length packets (ZLPs) should be avoided in portable 251 * protocols since not all usb hardware can successfully handle zero length 252 * packets. (ZLPs may be explicitly written, and may be implicitly written if 253 * the request 'zero' flag is set.) Bulk endpoints may also be used 254 * for interrupt transfers; but the reverse is not true, and some endpoints 255 * won't support every interrupt transfer. (Such as 768 byte packets.) 256 * 257 * Interrupt-only endpoints are less functional than bulk endpoints, for 258 * example by not supporting queueing or not handling buffers that are 259 * larger than the endpoint's maxpacket size. They may also treat data 260 * toggle differently. 261 * 262 * Control endpoints ... after getting a setup() callback, the driver queues 263 * one response (even if it would be zero length). That enables the 264 * status ack, after transferring data as specified in the response. Setup 265 * functions may return negative error codes to generate protocol stalls. 266 * (Note that some USB device controllers disallow protocol stall responses 267 * in some cases.) When control responses are deferred (the response is 268 * written after the setup callback returns), then usb_ep_set_halt() may be 269 * used on ep0 to trigger protocol stalls. Depending on the controller, 270 * it may not be possible to trigger a status-stage protocol stall when the 271 * data stage is over, that is, from within the response's completion 272 * routine. 273 * 274 * For periodic endpoints, like interrupt or isochronous ones, the usb host 275 * arranges to poll once per interval, and the gadget driver usually will 276 * have queued some data to transfer at that time. 277 * 278 * Note that @req's ->complete() callback must never be called from 279 * within usb_ep_queue() as that can create deadlock situations. 280 * 281 * This routine may be called in interrupt context. 282 * 283 * Returns zero, or a negative error code. Endpoints that are not enabled 284 * report errors; errors will also be 285 * reported when the usb peripheral is disconnected. 286 * 287 * If and only if @req is successfully queued (the return value is zero), 288 * @req->complete() will be called exactly once, when the Gadget core and 289 * UDC are finished with the request. When the completion function is called, 290 * control of the request is returned to the device driver which submitted it. 291 * The completion handler may then immediately free or reuse @req. 292 */ 293 int usb_ep_queue(struct usb_ep *ep, 294 struct usb_request *req, gfp_t gfp_flags) 295 { 296 int ret = 0; 297 298 if (!ep->enabled && ep->address) { 299 pr_debug("USB gadget: queue request to disabled ep 0x%x (%s)\n", 300 ep->address, ep->name); 301 ret = -ESHUTDOWN; 302 goto out; 303 } 304 305 ret = ep->ops->queue(ep, req, gfp_flags); 306 307 out: 308 trace_usb_ep_queue(ep, req, ret); 309 310 return ret; 311 } 312 EXPORT_SYMBOL_GPL(usb_ep_queue); 313 314 /** 315 * usb_ep_dequeue - dequeues (cancels, unlinks) an I/O request from an endpoint 316 * @ep:the endpoint associated with the request 317 * @req:the request being canceled 318 * 319 * If the request is still active on the endpoint, it is dequeued and 320 * eventually its completion routine is called (with status -ECONNRESET); 321 * else a negative error code is returned. This routine is asynchronous, 322 * that is, it may return before the completion routine runs. 323 * 324 * Note that some hardware can't clear out write fifos (to unlink the request 325 * at the head of the queue) except as part of disconnecting from usb. Such 326 * restrictions prevent drivers from supporting configuration changes, 327 * even to configuration zero (a "chapter 9" requirement). 328 * 329 * This routine may be called in interrupt context. 330 */ 331 int usb_ep_dequeue(struct usb_ep *ep, struct usb_request *req) 332 { 333 int ret; 334 335 ret = ep->ops->dequeue(ep, req); 336 trace_usb_ep_dequeue(ep, req, ret); 337 338 return ret; 339 } 340 EXPORT_SYMBOL_GPL(usb_ep_dequeue); 341 342 /** 343 * usb_ep_set_halt - sets the endpoint halt feature. 344 * @ep: the non-isochronous endpoint being stalled 345 * 346 * Use this to stall an endpoint, perhaps as an error report. 347 * Except for control endpoints, 348 * the endpoint stays halted (will not stream any data) until the host 349 * clears this feature; drivers may need to empty the endpoint's request 350 * queue first, to make sure no inappropriate transfers happen. 351 * 352 * Note that while an endpoint CLEAR_FEATURE will be invisible to the 353 * gadget driver, a SET_INTERFACE will not be. To reset endpoints for the 354 * current altsetting, see usb_ep_clear_halt(). When switching altsettings, 355 * it's simplest to use usb_ep_enable() or usb_ep_disable() for the endpoints. 356 * 357 * This routine may be called in interrupt context. 358 * 359 * Returns zero, or a negative error code. On success, this call sets 360 * underlying hardware state that blocks data transfers. 361 * Attempts to halt IN endpoints will fail (returning -EAGAIN) if any 362 * transfer requests are still queued, or if the controller hardware 363 * (usually a FIFO) still holds bytes that the host hasn't collected. 364 */ 365 int usb_ep_set_halt(struct usb_ep *ep) 366 { 367 int ret; 368 369 ret = ep->ops->set_halt(ep, 1); 370 trace_usb_ep_set_halt(ep, ret); 371 372 return ret; 373 } 374 EXPORT_SYMBOL_GPL(usb_ep_set_halt); 375 376 /** 377 * usb_ep_clear_halt - clears endpoint halt, and resets toggle 378 * @ep:the bulk or interrupt endpoint being reset 379 * 380 * Use this when responding to the standard usb "set interface" request, 381 * for endpoints that aren't reconfigured, after clearing any other state 382 * in the endpoint's i/o queue. 383 * 384 * This routine may be called in interrupt context. 385 * 386 * Returns zero, or a negative error code. On success, this call clears 387 * the underlying hardware state reflecting endpoint halt and data toggle. 388 * Note that some hardware can't support this request (like pxa2xx_udc), 389 * and accordingly can't correctly implement interface altsettings. 390 */ 391 int usb_ep_clear_halt(struct usb_ep *ep) 392 { 393 int ret; 394 395 ret = ep->ops->set_halt(ep, 0); 396 trace_usb_ep_clear_halt(ep, ret); 397 398 return ret; 399 } 400 EXPORT_SYMBOL_GPL(usb_ep_clear_halt); 401 402 /** 403 * usb_ep_set_wedge - sets the halt feature and ignores clear requests 404 * @ep: the endpoint being wedged 405 * 406 * Use this to stall an endpoint and ignore CLEAR_FEATURE(HALT_ENDPOINT) 407 * requests. If the gadget driver clears the halt status, it will 408 * automatically unwedge the endpoint. 409 * 410 * This routine may be called in interrupt context. 411 * 412 * Returns zero on success, else negative errno. 413 */ 414 int usb_ep_set_wedge(struct usb_ep *ep) 415 { 416 int ret; 417 418 if (ep->ops->set_wedge) 419 ret = ep->ops->set_wedge(ep); 420 else 421 ret = ep->ops->set_halt(ep, 1); 422 423 trace_usb_ep_set_wedge(ep, ret); 424 425 return ret; 426 } 427 EXPORT_SYMBOL_GPL(usb_ep_set_wedge); 428 429 /** 430 * usb_ep_fifo_status - returns number of bytes in fifo, or error 431 * @ep: the endpoint whose fifo status is being checked. 432 * 433 * FIFO endpoints may have "unclaimed data" in them in certain cases, 434 * such as after aborted transfers. Hosts may not have collected all 435 * the IN data written by the gadget driver (and reported by a request 436 * completion). The gadget driver may not have collected all the data 437 * written OUT to it by the host. Drivers that need precise handling for 438 * fault reporting or recovery may need to use this call. 439 * 440 * This routine may be called in interrupt context. 441 * 442 * This returns the number of such bytes in the fifo, or a negative 443 * errno if the endpoint doesn't use a FIFO or doesn't support such 444 * precise handling. 445 */ 446 int usb_ep_fifo_status(struct usb_ep *ep) 447 { 448 int ret; 449 450 if (ep->ops->fifo_status) 451 ret = ep->ops->fifo_status(ep); 452 else 453 ret = -EOPNOTSUPP; 454 455 trace_usb_ep_fifo_status(ep, ret); 456 457 return ret; 458 } 459 EXPORT_SYMBOL_GPL(usb_ep_fifo_status); 460 461 /** 462 * usb_ep_fifo_flush - flushes contents of a fifo 463 * @ep: the endpoint whose fifo is being flushed. 464 * 465 * This call may be used to flush the "unclaimed data" that may exist in 466 * an endpoint fifo after abnormal transaction terminations. The call 467 * must never be used except when endpoint is not being used for any 468 * protocol translation. 469 * 470 * This routine may be called in interrupt context. 471 */ 472 void usb_ep_fifo_flush(struct usb_ep *ep) 473 { 474 if (ep->ops->fifo_flush) 475 ep->ops->fifo_flush(ep); 476 477 trace_usb_ep_fifo_flush(ep, 0); 478 } 479 EXPORT_SYMBOL_GPL(usb_ep_fifo_flush); 480 481 /* ------------------------------------------------------------------------- */ 482 483 /** 484 * usb_gadget_frame_number - returns the current frame number 485 * @gadget: controller that reports the frame number 486 * 487 * Returns the usb frame number, normally eleven bits from a SOF packet, 488 * or negative errno if this device doesn't support this capability. 489 */ 490 int usb_gadget_frame_number(struct usb_gadget *gadget) 491 { 492 int ret; 493 494 ret = gadget->ops->get_frame(gadget); 495 496 trace_usb_gadget_frame_number(gadget, ret); 497 498 return ret; 499 } 500 EXPORT_SYMBOL_GPL(usb_gadget_frame_number); 501 502 /** 503 * usb_gadget_wakeup - tries to wake up the host connected to this gadget 504 * @gadget: controller used to wake up the host 505 * 506 * Returns zero on success, else negative error code if the hardware 507 * doesn't support such attempts, or its support has not been enabled 508 * by the usb host. Drivers must return device descriptors that report 509 * their ability to support this, or hosts won't enable it. 510 * 511 * This may also try to use SRP to wake the host and start enumeration, 512 * even if OTG isn't otherwise in use. OTG devices may also start 513 * remote wakeup even when hosts don't explicitly enable it. 514 */ 515 int usb_gadget_wakeup(struct usb_gadget *gadget) 516 { 517 int ret = 0; 518 519 if (!gadget->ops->wakeup) { 520 ret = -EOPNOTSUPP; 521 goto out; 522 } 523 524 ret = gadget->ops->wakeup(gadget); 525 526 out: 527 trace_usb_gadget_wakeup(gadget, ret); 528 529 return ret; 530 } 531 EXPORT_SYMBOL_GPL(usb_gadget_wakeup); 532 533 /** 534 * usb_gadget_set_remote_wakeup - configures the device remote wakeup feature. 535 * @gadget:the device being configured for remote wakeup 536 * @set:value to be configured. 537 * 538 * set to one to enable remote wakeup feature and zero to disable it. 539 * 540 * returns zero on success, else negative errno. 541 */ 542 int usb_gadget_set_remote_wakeup(struct usb_gadget *gadget, int set) 543 { 544 int ret = 0; 545 546 if (!gadget->ops->set_remote_wakeup) { 547 ret = -EOPNOTSUPP; 548 goto out; 549 } 550 551 ret = gadget->ops->set_remote_wakeup(gadget, set); 552 553 out: 554 trace_usb_gadget_set_remote_wakeup(gadget, ret); 555 556 return ret; 557 } 558 EXPORT_SYMBOL_GPL(usb_gadget_set_remote_wakeup); 559 560 /** 561 * usb_gadget_set_selfpowered - sets the device selfpowered feature. 562 * @gadget:the device being declared as self-powered 563 * 564 * this affects the device status reported by the hardware driver 565 * to reflect that it now has a local power supply. 566 * 567 * returns zero on success, else negative errno. 568 */ 569 int usb_gadget_set_selfpowered(struct usb_gadget *gadget) 570 { 571 int ret = 0; 572 573 if (!gadget->ops->set_selfpowered) { 574 ret = -EOPNOTSUPP; 575 goto out; 576 } 577 578 ret = gadget->ops->set_selfpowered(gadget, 1); 579 580 out: 581 trace_usb_gadget_set_selfpowered(gadget, ret); 582 583 return ret; 584 } 585 EXPORT_SYMBOL_GPL(usb_gadget_set_selfpowered); 586 587 /** 588 * usb_gadget_clear_selfpowered - clear the device selfpowered feature. 589 * @gadget:the device being declared as bus-powered 590 * 591 * this affects the device status reported by the hardware driver. 592 * some hardware may not support bus-powered operation, in which 593 * case this feature's value can never change. 594 * 595 * returns zero on success, else negative errno. 596 */ 597 int usb_gadget_clear_selfpowered(struct usb_gadget *gadget) 598 { 599 int ret = 0; 600 601 if (!gadget->ops->set_selfpowered) { 602 ret = -EOPNOTSUPP; 603 goto out; 604 } 605 606 ret = gadget->ops->set_selfpowered(gadget, 0); 607 608 out: 609 trace_usb_gadget_clear_selfpowered(gadget, ret); 610 611 return ret; 612 } 613 EXPORT_SYMBOL_GPL(usb_gadget_clear_selfpowered); 614 615 /** 616 * usb_gadget_vbus_connect - Notify controller that VBUS is powered 617 * @gadget:The device which now has VBUS power. 618 * Context: can sleep 619 * 620 * This call is used by a driver for an external transceiver (or GPIO) 621 * that detects a VBUS power session starting. Common responses include 622 * resuming the controller, activating the D+ (or D-) pullup to let the 623 * host detect that a USB device is attached, and starting to draw power 624 * (8mA or possibly more, especially after SET_CONFIGURATION). 625 * 626 * Returns zero on success, else negative errno. 627 */ 628 int usb_gadget_vbus_connect(struct usb_gadget *gadget) 629 { 630 int ret = 0; 631 632 if (!gadget->ops->vbus_session) { 633 ret = -EOPNOTSUPP; 634 goto out; 635 } 636 637 ret = gadget->ops->vbus_session(gadget, 1); 638 639 out: 640 trace_usb_gadget_vbus_connect(gadget, ret); 641 642 return ret; 643 } 644 EXPORT_SYMBOL_GPL(usb_gadget_vbus_connect); 645 646 /** 647 * usb_gadget_vbus_draw - constrain controller's VBUS power usage 648 * @gadget:The device whose VBUS usage is being described 649 * @mA:How much current to draw, in milliAmperes. This should be twice 650 * the value listed in the configuration descriptor bMaxPower field. 651 * 652 * This call is used by gadget drivers during SET_CONFIGURATION calls, 653 * reporting how much power the device may consume. For example, this 654 * could affect how quickly batteries are recharged. 655 * 656 * Returns zero on success, else negative errno. 657 */ 658 int usb_gadget_vbus_draw(struct usb_gadget *gadget, unsigned mA) 659 { 660 int ret = 0; 661 662 if (!gadget->ops->vbus_draw) { 663 ret = -EOPNOTSUPP; 664 goto out; 665 } 666 667 ret = gadget->ops->vbus_draw(gadget, mA); 668 if (!ret) 669 gadget->mA = mA; 670 671 out: 672 trace_usb_gadget_vbus_draw(gadget, ret); 673 674 return ret; 675 } 676 EXPORT_SYMBOL_GPL(usb_gadget_vbus_draw); 677 678 /** 679 * usb_gadget_vbus_disconnect - notify controller about VBUS session end 680 * @gadget:the device whose VBUS supply is being described 681 * Context: can sleep 682 * 683 * This call is used by a driver for an external transceiver (or GPIO) 684 * that detects a VBUS power session ending. Common responses include 685 * reversing everything done in usb_gadget_vbus_connect(). 686 * 687 * Returns zero on success, else negative errno. 688 */ 689 int usb_gadget_vbus_disconnect(struct usb_gadget *gadget) 690 { 691 int ret = 0; 692 693 if (!gadget->ops->vbus_session) { 694 ret = -EOPNOTSUPP; 695 goto out; 696 } 697 698 ret = gadget->ops->vbus_session(gadget, 0); 699 700 out: 701 trace_usb_gadget_vbus_disconnect(gadget, ret); 702 703 return ret; 704 } 705 EXPORT_SYMBOL_GPL(usb_gadget_vbus_disconnect); 706 707 static int usb_gadget_connect_locked(struct usb_gadget *gadget) 708 __must_hold(&gadget->udc->connect_lock) 709 { 710 int ret = 0; 711 712 if (!gadget->ops->pullup) { 713 ret = -EOPNOTSUPP; 714 goto out; 715 } 716 717 if (gadget->connected) 718 goto out; 719 720 if (gadget->deactivated || !gadget->udc->allow_connect || !gadget->udc->started) { 721 /* 722 * If the gadget isn't usable (because it is deactivated, 723 * unbound, or not yet started), we only save the new state. 724 * The gadget will be connected automatically when it is 725 * activated/bound/started. 726 */ 727 gadget->connected = true; 728 goto out; 729 } 730 731 ret = gadget->ops->pullup(gadget, 1); 732 if (!ret) 733 gadget->connected = 1; 734 735 out: 736 trace_usb_gadget_connect(gadget, ret); 737 738 return ret; 739 } 740 741 /** 742 * usb_gadget_connect - software-controlled connect to USB host 743 * @gadget:the peripheral being connected 744 * 745 * Enables the D+ (or potentially D-) pullup. The host will start 746 * enumerating this gadget when the pullup is active and a VBUS session 747 * is active (the link is powered). 748 * 749 * Returns zero on success, else negative errno. 750 */ 751 int usb_gadget_connect(struct usb_gadget *gadget) 752 { 753 int ret; 754 755 mutex_lock(&gadget->udc->connect_lock); 756 ret = usb_gadget_connect_locked(gadget); 757 mutex_unlock(&gadget->udc->connect_lock); 758 759 return ret; 760 } 761 EXPORT_SYMBOL_GPL(usb_gadget_connect); 762 763 static int usb_gadget_disconnect_locked(struct usb_gadget *gadget) 764 __must_hold(&gadget->udc->connect_lock) 765 { 766 int ret = 0; 767 768 if (!gadget->ops->pullup) { 769 ret = -EOPNOTSUPP; 770 goto out; 771 } 772 773 if (!gadget->connected) 774 goto out; 775 776 if (gadget->deactivated || !gadget->udc->started) { 777 /* 778 * If gadget is deactivated we only save new state. 779 * Gadget will stay disconnected after activation. 780 */ 781 gadget->connected = false; 782 goto out; 783 } 784 785 ret = gadget->ops->pullup(gadget, 0); 786 if (!ret) 787 gadget->connected = 0; 788 789 mutex_lock(&udc_lock); 790 if (gadget->udc->driver) 791 gadget->udc->driver->disconnect(gadget); 792 mutex_unlock(&udc_lock); 793 794 out: 795 trace_usb_gadget_disconnect(gadget, ret); 796 797 return ret; 798 } 799 800 /** 801 * usb_gadget_disconnect - software-controlled disconnect from USB host 802 * @gadget:the peripheral being disconnected 803 * 804 * Disables the D+ (or potentially D-) pullup, which the host may see 805 * as a disconnect (when a VBUS session is active). Not all systems 806 * support software pullup controls. 807 * 808 * Following a successful disconnect, invoke the ->disconnect() callback 809 * for the current gadget driver so that UDC drivers don't need to. 810 * 811 * Returns zero on success, else negative errno. 812 */ 813 int usb_gadget_disconnect(struct usb_gadget *gadget) 814 { 815 int ret; 816 817 mutex_lock(&gadget->udc->connect_lock); 818 ret = usb_gadget_disconnect_locked(gadget); 819 mutex_unlock(&gadget->udc->connect_lock); 820 821 return ret; 822 } 823 EXPORT_SYMBOL_GPL(usb_gadget_disconnect); 824 825 /** 826 * usb_gadget_deactivate - deactivate function which is not ready to work 827 * @gadget: the peripheral being deactivated 828 * 829 * This routine may be used during the gadget driver bind() call to prevent 830 * the peripheral from ever being visible to the USB host, unless later 831 * usb_gadget_activate() is called. For example, user mode components may 832 * need to be activated before the system can talk to hosts. 833 * 834 * This routine may sleep; it must not be called in interrupt context 835 * (such as from within a gadget driver's disconnect() callback). 836 * 837 * Returns zero on success, else negative errno. 838 */ 839 int usb_gadget_deactivate(struct usb_gadget *gadget) 840 { 841 int ret = 0; 842 843 mutex_lock(&gadget->udc->connect_lock); 844 if (gadget->deactivated) 845 goto unlock; 846 847 if (gadget->connected) { 848 ret = usb_gadget_disconnect_locked(gadget); 849 if (ret) 850 goto unlock; 851 852 /* 853 * If gadget was being connected before deactivation, we want 854 * to reconnect it in usb_gadget_activate(). 855 */ 856 gadget->connected = true; 857 } 858 gadget->deactivated = true; 859 860 unlock: 861 mutex_unlock(&gadget->udc->connect_lock); 862 trace_usb_gadget_deactivate(gadget, ret); 863 864 return ret; 865 } 866 EXPORT_SYMBOL_GPL(usb_gadget_deactivate); 867 868 /** 869 * usb_gadget_activate - activate function which is not ready to work 870 * @gadget: the peripheral being activated 871 * 872 * This routine activates gadget which was previously deactivated with 873 * usb_gadget_deactivate() call. It calls usb_gadget_connect() if needed. 874 * 875 * This routine may sleep; it must not be called in interrupt context. 876 * 877 * Returns zero on success, else negative errno. 878 */ 879 int usb_gadget_activate(struct usb_gadget *gadget) 880 { 881 int ret = 0; 882 883 mutex_lock(&gadget->udc->connect_lock); 884 if (!gadget->deactivated) 885 goto unlock; 886 887 gadget->deactivated = false; 888 889 /* 890 * If gadget has been connected before deactivation, or became connected 891 * while it was being deactivated, we call usb_gadget_connect(). 892 */ 893 if (gadget->connected) { 894 gadget->connected = false; 895 ret = usb_gadget_connect_locked(gadget); 896 } 897 898 unlock: 899 mutex_unlock(&gadget->udc->connect_lock); 900 trace_usb_gadget_activate(gadget, ret); 901 902 return ret; 903 } 904 EXPORT_SYMBOL_GPL(usb_gadget_activate); 905 906 /* ------------------------------------------------------------------------- */ 907 908 #ifdef CONFIG_HAS_DMA 909 910 int usb_gadget_map_request_by_dev(struct device *dev, 911 struct usb_request *req, int is_in) 912 { 913 if (req->length == 0) 914 return 0; 915 916 if (req->sg_was_mapped) { 917 req->num_mapped_sgs = req->num_sgs; 918 return 0; 919 } 920 921 if (req->num_sgs) { 922 int mapped; 923 924 mapped = dma_map_sg(dev, req->sg, req->num_sgs, 925 is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE); 926 if (mapped == 0) { 927 dev_err(dev, "failed to map SGs\n"); 928 return -EFAULT; 929 } 930 931 req->num_mapped_sgs = mapped; 932 } else { 933 if (is_vmalloc_addr(req->buf)) { 934 dev_err(dev, "buffer is not dma capable\n"); 935 return -EFAULT; 936 } else if (object_is_on_stack(req->buf)) { 937 dev_err(dev, "buffer is on stack\n"); 938 return -EFAULT; 939 } 940 941 req->dma = dma_map_single(dev, req->buf, req->length, 942 is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE); 943 944 if (dma_mapping_error(dev, req->dma)) { 945 dev_err(dev, "failed to map buffer\n"); 946 return -EFAULT; 947 } 948 949 req->dma_mapped = 1; 950 } 951 952 return 0; 953 } 954 EXPORT_SYMBOL_GPL(usb_gadget_map_request_by_dev); 955 956 int usb_gadget_map_request(struct usb_gadget *gadget, 957 struct usb_request *req, int is_in) 958 { 959 return usb_gadget_map_request_by_dev(gadget->dev.parent, req, is_in); 960 } 961 EXPORT_SYMBOL_GPL(usb_gadget_map_request); 962 963 void usb_gadget_unmap_request_by_dev(struct device *dev, 964 struct usb_request *req, int is_in) 965 { 966 if (req->length == 0 || req->sg_was_mapped) 967 return; 968 969 if (req->num_mapped_sgs) { 970 dma_unmap_sg(dev, req->sg, req->num_sgs, 971 is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE); 972 973 req->num_mapped_sgs = 0; 974 } else if (req->dma_mapped) { 975 dma_unmap_single(dev, req->dma, req->length, 976 is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE); 977 req->dma_mapped = 0; 978 } 979 } 980 EXPORT_SYMBOL_GPL(usb_gadget_unmap_request_by_dev); 981 982 void usb_gadget_unmap_request(struct usb_gadget *gadget, 983 struct usb_request *req, int is_in) 984 { 985 usb_gadget_unmap_request_by_dev(gadget->dev.parent, req, is_in); 986 } 987 EXPORT_SYMBOL_GPL(usb_gadget_unmap_request); 988 989 #endif /* CONFIG_HAS_DMA */ 990 991 /* ------------------------------------------------------------------------- */ 992 993 /** 994 * usb_gadget_giveback_request - give the request back to the gadget layer 995 * @ep: the endpoint to be used with with the request 996 * @req: the request being given back 997 * 998 * This is called by device controller drivers in order to return the 999 * completed request back to the gadget layer. 1000 */ 1001 void usb_gadget_giveback_request(struct usb_ep *ep, 1002 struct usb_request *req) 1003 { 1004 if (likely(req->status == 0)) 1005 usb_led_activity(USB_LED_EVENT_GADGET); 1006 1007 trace_usb_gadget_giveback_request(ep, req, 0); 1008 1009 req->complete(ep, req); 1010 } 1011 EXPORT_SYMBOL_GPL(usb_gadget_giveback_request); 1012 1013 /* ------------------------------------------------------------------------- */ 1014 1015 /** 1016 * gadget_find_ep_by_name - returns ep whose name is the same as sting passed 1017 * in second parameter or NULL if searched endpoint not found 1018 * @g: controller to check for quirk 1019 * @name: name of searched endpoint 1020 */ 1021 struct usb_ep *gadget_find_ep_by_name(struct usb_gadget *g, const char *name) 1022 { 1023 struct usb_ep *ep; 1024 1025 gadget_for_each_ep(ep, g) { 1026 if (!strcmp(ep->name, name)) 1027 return ep; 1028 } 1029 1030 return NULL; 1031 } 1032 EXPORT_SYMBOL_GPL(gadget_find_ep_by_name); 1033 1034 /* ------------------------------------------------------------------------- */ 1035 1036 int usb_gadget_ep_match_desc(struct usb_gadget *gadget, 1037 struct usb_ep *ep, struct usb_endpoint_descriptor *desc, 1038 struct usb_ss_ep_comp_descriptor *ep_comp) 1039 { 1040 u8 type; 1041 u16 max; 1042 int num_req_streams = 0; 1043 1044 /* endpoint already claimed? */ 1045 if (ep->claimed) 1046 return 0; 1047 1048 type = usb_endpoint_type(desc); 1049 max = usb_endpoint_maxp(desc); 1050 1051 if (usb_endpoint_dir_in(desc) && !ep->caps.dir_in) 1052 return 0; 1053 if (usb_endpoint_dir_out(desc) && !ep->caps.dir_out) 1054 return 0; 1055 1056 if (max > ep->maxpacket_limit) 1057 return 0; 1058 1059 /* "high bandwidth" works only at high speed */ 1060 if (!gadget_is_dualspeed(gadget) && usb_endpoint_maxp_mult(desc) > 1) 1061 return 0; 1062 1063 switch (type) { 1064 case USB_ENDPOINT_XFER_CONTROL: 1065 /* only support ep0 for portable CONTROL traffic */ 1066 return 0; 1067 case USB_ENDPOINT_XFER_ISOC: 1068 if (!ep->caps.type_iso) 1069 return 0; 1070 /* ISO: limit 1023 bytes full speed, 1024 high/super speed */ 1071 if (!gadget_is_dualspeed(gadget) && max > 1023) 1072 return 0; 1073 break; 1074 case USB_ENDPOINT_XFER_BULK: 1075 if (!ep->caps.type_bulk) 1076 return 0; 1077 if (ep_comp && gadget_is_superspeed(gadget)) { 1078 /* Get the number of required streams from the 1079 * EP companion descriptor and see if the EP 1080 * matches it 1081 */ 1082 num_req_streams = ep_comp->bmAttributes & 0x1f; 1083 if (num_req_streams > ep->max_streams) 1084 return 0; 1085 } 1086 break; 1087 case USB_ENDPOINT_XFER_INT: 1088 /* Bulk endpoints handle interrupt transfers, 1089 * except the toggle-quirky iso-synch kind 1090 */ 1091 if (!ep->caps.type_int && !ep->caps.type_bulk) 1092 return 0; 1093 /* INT: limit 64 bytes full speed, 1024 high/super speed */ 1094 if (!gadget_is_dualspeed(gadget) && max > 64) 1095 return 0; 1096 break; 1097 } 1098 1099 return 1; 1100 } 1101 EXPORT_SYMBOL_GPL(usb_gadget_ep_match_desc); 1102 1103 /** 1104 * usb_gadget_check_config - checks if the UDC can support the binded 1105 * configuration 1106 * @gadget: controller to check the USB configuration 1107 * 1108 * Ensure that a UDC is able to support the requested resources by a 1109 * configuration, and that there are no resource limitations, such as 1110 * internal memory allocated to all requested endpoints. 1111 * 1112 * Returns zero on success, else a negative errno. 1113 */ 1114 int usb_gadget_check_config(struct usb_gadget *gadget) 1115 { 1116 if (gadget->ops->check_config) 1117 return gadget->ops->check_config(gadget); 1118 return 0; 1119 } 1120 EXPORT_SYMBOL_GPL(usb_gadget_check_config); 1121 1122 /* ------------------------------------------------------------------------- */ 1123 1124 static void usb_gadget_state_work(struct work_struct *work) 1125 { 1126 struct usb_gadget *gadget = work_to_gadget(work); 1127 struct usb_udc *udc = gadget->udc; 1128 1129 if (udc) 1130 sysfs_notify(&udc->dev.kobj, NULL, "state"); 1131 } 1132 1133 void usb_gadget_set_state(struct usb_gadget *gadget, 1134 enum usb_device_state state) 1135 { 1136 unsigned long flags; 1137 1138 spin_lock_irqsave(&gadget->state_lock, flags); 1139 gadget->state = state; 1140 if (!gadget->teardown) 1141 schedule_work(&gadget->work); 1142 spin_unlock_irqrestore(&gadget->state_lock, flags); 1143 trace_usb_gadget_set_state(gadget, 0); 1144 } 1145 EXPORT_SYMBOL_GPL(usb_gadget_set_state); 1146 1147 /* ------------------------------------------------------------------------- */ 1148 1149 /* Acquire connect_lock before calling this function. */ 1150 static int usb_udc_connect_control_locked(struct usb_udc *udc) __must_hold(&udc->connect_lock) 1151 { 1152 if (udc->vbus) 1153 return usb_gadget_connect_locked(udc->gadget); 1154 else 1155 return usb_gadget_disconnect_locked(udc->gadget); 1156 } 1157 1158 static void vbus_event_work(struct work_struct *work) 1159 { 1160 struct usb_udc *udc = container_of(work, struct usb_udc, vbus_work); 1161 1162 mutex_lock(&udc->connect_lock); 1163 usb_udc_connect_control_locked(udc); 1164 mutex_unlock(&udc->connect_lock); 1165 } 1166 1167 /** 1168 * usb_udc_vbus_handler - updates the udc core vbus status, and try to 1169 * connect or disconnect gadget 1170 * @gadget: The gadget which vbus change occurs 1171 * @status: The vbus status 1172 * 1173 * The udc driver calls it when it wants to connect or disconnect gadget 1174 * according to vbus status. 1175 * 1176 * This function can be invoked from interrupt context by irq handlers of 1177 * the gadget drivers, however, usb_udc_connect_control() has to run in 1178 * non-atomic context due to the following: 1179 * a. Some of the gadget driver implementations expect the ->pullup 1180 * callback to be invoked in non-atomic context. 1181 * b. usb_gadget_disconnect() acquires udc_lock which is a mutex. 1182 * Hence offload invocation of usb_udc_connect_control() to workqueue. 1183 */ 1184 void usb_udc_vbus_handler(struct usb_gadget *gadget, bool status) 1185 { 1186 struct usb_udc *udc = gadget->udc; 1187 1188 if (udc) { 1189 udc->vbus = status; 1190 schedule_work(&udc->vbus_work); 1191 } 1192 } 1193 EXPORT_SYMBOL_GPL(usb_udc_vbus_handler); 1194 1195 /** 1196 * usb_gadget_udc_reset - notifies the udc core that bus reset occurs 1197 * @gadget: The gadget which bus reset occurs 1198 * @driver: The gadget driver we want to notify 1199 * 1200 * If the udc driver has bus reset handler, it needs to call this when the bus 1201 * reset occurs, it notifies the gadget driver that the bus reset occurs as 1202 * well as updates gadget state. 1203 */ 1204 void usb_gadget_udc_reset(struct usb_gadget *gadget, 1205 struct usb_gadget_driver *driver) 1206 { 1207 driver->reset(gadget); 1208 usb_gadget_set_state(gadget, USB_STATE_DEFAULT); 1209 } 1210 EXPORT_SYMBOL_GPL(usb_gadget_udc_reset); 1211 1212 /** 1213 * usb_gadget_udc_start_locked - tells usb device controller to start up 1214 * @udc: The UDC to be started 1215 * 1216 * This call is issued by the UDC Class driver when it's about 1217 * to register a gadget driver to the device controller, before 1218 * calling gadget driver's bind() method. 1219 * 1220 * It allows the controller to be powered off until strictly 1221 * necessary to have it powered on. 1222 * 1223 * Returns zero on success, else negative errno. 1224 * 1225 * Caller should acquire connect_lock before invoking this function. 1226 */ 1227 static inline int usb_gadget_udc_start_locked(struct usb_udc *udc) 1228 __must_hold(&udc->connect_lock) 1229 { 1230 int ret; 1231 1232 if (udc->started) { 1233 dev_err(&udc->dev, "UDC had already started\n"); 1234 return -EBUSY; 1235 } 1236 1237 ret = udc->gadget->ops->udc_start(udc->gadget, udc->driver); 1238 if (!ret) 1239 udc->started = true; 1240 1241 return ret; 1242 } 1243 1244 /** 1245 * usb_gadget_udc_stop_locked - tells usb device controller we don't need it anymore 1246 * @udc: The UDC to be stopped 1247 * 1248 * This call is issued by the UDC Class driver after calling 1249 * gadget driver's unbind() method. 1250 * 1251 * The details are implementation specific, but it can go as 1252 * far as powering off UDC completely and disable its data 1253 * line pullups. 1254 * 1255 * Caller should acquire connect lock before invoking this function. 1256 */ 1257 static inline void usb_gadget_udc_stop_locked(struct usb_udc *udc) 1258 __must_hold(&udc->connect_lock) 1259 { 1260 if (!udc->started) { 1261 dev_err(&udc->dev, "UDC had already stopped\n"); 1262 return; 1263 } 1264 1265 udc->gadget->ops->udc_stop(udc->gadget); 1266 udc->started = false; 1267 } 1268 1269 /** 1270 * usb_gadget_udc_set_speed - tells usb device controller speed supported by 1271 * current driver 1272 * @udc: The device we want to set maximum speed 1273 * @speed: The maximum speed to allowed to run 1274 * 1275 * This call is issued by the UDC Class driver before calling 1276 * usb_gadget_udc_start_locked() in order to make sure that 1277 * we don't try to connect on speeds the gadget driver 1278 * doesn't support. 1279 */ 1280 static inline void usb_gadget_udc_set_speed(struct usb_udc *udc, 1281 enum usb_device_speed speed) 1282 { 1283 struct usb_gadget *gadget = udc->gadget; 1284 enum usb_device_speed s; 1285 1286 if (speed == USB_SPEED_UNKNOWN) 1287 s = gadget->max_speed; 1288 else 1289 s = min(speed, gadget->max_speed); 1290 1291 if (s == USB_SPEED_SUPER_PLUS && gadget->ops->udc_set_ssp_rate) 1292 gadget->ops->udc_set_ssp_rate(gadget, gadget->max_ssp_rate); 1293 else if (gadget->ops->udc_set_speed) 1294 gadget->ops->udc_set_speed(gadget, s); 1295 } 1296 1297 /** 1298 * usb_gadget_enable_async_callbacks - tell usb device controller to enable asynchronous callbacks 1299 * @udc: The UDC which should enable async callbacks 1300 * 1301 * This routine is used when binding gadget drivers. It undoes the effect 1302 * of usb_gadget_disable_async_callbacks(); the UDC driver should enable IRQs 1303 * (if necessary) and resume issuing callbacks. 1304 * 1305 * This routine will always be called in process context. 1306 */ 1307 static inline void usb_gadget_enable_async_callbacks(struct usb_udc *udc) 1308 { 1309 struct usb_gadget *gadget = udc->gadget; 1310 1311 if (gadget->ops->udc_async_callbacks) 1312 gadget->ops->udc_async_callbacks(gadget, true); 1313 } 1314 1315 /** 1316 * usb_gadget_disable_async_callbacks - tell usb device controller to disable asynchronous callbacks 1317 * @udc: The UDC which should disable async callbacks 1318 * 1319 * This routine is used when unbinding gadget drivers. It prevents a race: 1320 * The UDC driver doesn't know when the gadget driver's ->unbind callback 1321 * runs, so unless it is told to disable asynchronous callbacks, it might 1322 * issue a callback (such as ->disconnect) after the unbind has completed. 1323 * 1324 * After this function runs, the UDC driver must suppress all ->suspend, 1325 * ->resume, ->disconnect, ->reset, and ->setup callbacks to the gadget driver 1326 * until async callbacks are again enabled. A simple-minded but effective 1327 * way to accomplish this is to tell the UDC hardware not to generate any 1328 * more IRQs. 1329 * 1330 * Request completion callbacks must still be issued. However, it's okay 1331 * to defer them until the request is cancelled, since the pull-up will be 1332 * turned off during the time period when async callbacks are disabled. 1333 * 1334 * This routine will always be called in process context. 1335 */ 1336 static inline void usb_gadget_disable_async_callbacks(struct usb_udc *udc) 1337 { 1338 struct usb_gadget *gadget = udc->gadget; 1339 1340 if (gadget->ops->udc_async_callbacks) 1341 gadget->ops->udc_async_callbacks(gadget, false); 1342 } 1343 1344 /** 1345 * usb_udc_release - release the usb_udc struct 1346 * @dev: the dev member within usb_udc 1347 * 1348 * This is called by driver's core in order to free memory once the last 1349 * reference is released. 1350 */ 1351 static void usb_udc_release(struct device *dev) 1352 { 1353 struct usb_udc *udc; 1354 1355 udc = container_of(dev, struct usb_udc, dev); 1356 dev_dbg(dev, "releasing '%s'\n", dev_name(dev)); 1357 kfree(udc); 1358 } 1359 1360 static const struct attribute_group *usb_udc_attr_groups[]; 1361 1362 static void usb_udc_nop_release(struct device *dev) 1363 { 1364 dev_vdbg(dev, "%s\n", __func__); 1365 } 1366 1367 static void usb_gadget_release(struct device *dev) 1368 { 1369 struct usb_gadget *gadget = dev_to_usb_gadget(dev); 1370 struct usb_udc *udc = gadget->udc; 1371 /* Cache the gadget's release routine to prevent UAF */ 1372 void (*release)(struct device *dev) = udc->gadget_release; 1373 1374 put_device(&udc->dev); 1375 release(dev); 1376 } 1377 1378 /** 1379 * usb_initialize_gadget - initialize a gadget and its embedded struct device 1380 * @parent: the parent device to this udc. Usually the controller driver's 1381 * device. 1382 * @gadget: the gadget to be initialized. 1383 * @release: a gadget release function. 1384 */ 1385 void usb_initialize_gadget(struct device *parent, struct usb_gadget *gadget, 1386 void (*release)(struct device *dev)) 1387 { 1388 spin_lock_init(&gadget->state_lock); 1389 gadget->teardown = false; 1390 INIT_WORK(&gadget->work, usb_gadget_state_work); 1391 gadget->dev.parent = parent; 1392 1393 if (release) 1394 gadget->dev.release = release; 1395 else 1396 gadget->dev.release = usb_udc_nop_release; 1397 1398 device_initialize(&gadget->dev); 1399 gadget->dev.bus = &gadget_bus_type; 1400 } 1401 EXPORT_SYMBOL_GPL(usb_initialize_gadget); 1402 1403 /** 1404 * usb_add_gadget - adds a new gadget to the udc class driver list 1405 * @gadget: the gadget to be added to the list. 1406 * 1407 * Returns zero on success, negative errno otherwise. 1408 * Does not do a final usb_put_gadget() if an error occurs. 1409 */ 1410 int usb_add_gadget(struct usb_gadget *gadget) 1411 { 1412 struct usb_udc *udc; 1413 int ret = -ENOMEM; 1414 1415 udc = kzalloc_obj(*udc); 1416 if (!udc) 1417 goto error; 1418 1419 device_initialize(&udc->dev); 1420 udc->dev.release = usb_udc_release; 1421 udc->dev.class = &udc_class; 1422 udc->dev.groups = usb_udc_attr_groups; 1423 udc->dev.parent = gadget->dev.parent; 1424 ret = dev_set_name(&udc->dev, "%s", 1425 kobject_name(&gadget->dev.parent->kobj)); 1426 if (ret) 1427 goto err_put_udc; 1428 1429 udc->gadget = gadget; 1430 gadget->udc = udc; 1431 mutex_init(&udc->connect_lock); 1432 1433 udc->started = false; 1434 /* 1435 * Align decoupled lifecycles: take a UDC reference to ensure it 1436 * remains allocated until the gadget is released, requiring an 1437 * override of the gadget's release routine to drop it. 1438 */ 1439 udc->gadget_release = gadget->dev.release; 1440 gadget->dev.release = usb_gadget_release; 1441 get_device(&udc->dev); 1442 1443 mutex_lock(&udc_lock); 1444 list_add_tail(&udc->list, &udc_list); 1445 mutex_unlock(&udc_lock); 1446 INIT_WORK(&udc->vbus_work, vbus_event_work); 1447 1448 ret = device_add(&udc->dev); 1449 if (ret) 1450 goto err_unlist_udc; 1451 1452 usb_gadget_set_state(gadget, USB_STATE_NOTATTACHED); 1453 udc->vbus = true; 1454 1455 ret = ida_alloc(&gadget_id_numbers, GFP_KERNEL); 1456 if (ret < 0) 1457 goto err_del_udc; 1458 gadget->id_number = ret; 1459 dev_set_name(&gadget->dev, "gadget.%d", ret); 1460 1461 ret = device_add(&gadget->dev); 1462 if (ret) 1463 goto err_free_id; 1464 1465 ret = sysfs_create_link(&udc->dev.kobj, 1466 &gadget->dev.kobj, "gadget"); 1467 if (ret) 1468 goto err_del_gadget; 1469 1470 return 0; 1471 1472 err_del_gadget: 1473 device_del(&gadget->dev); 1474 1475 err_free_id: 1476 ida_free(&gadget_id_numbers, gadget->id_number); 1477 1478 err_del_udc: 1479 flush_work(&gadget->work); 1480 device_del(&udc->dev); 1481 1482 err_unlist_udc: 1483 mutex_lock(&udc_lock); 1484 list_del(&udc->list); 1485 mutex_unlock(&udc_lock); 1486 /* 1487 * Revert the override and drop the UDC reference to prevent 1488 * leaking the UDC if the gadget was statically allocated. 1489 */ 1490 gadget->dev.release = udc->gadget_release; 1491 put_device(&udc->dev); 1492 1493 err_put_udc: 1494 put_device(&udc->dev); 1495 1496 error: 1497 return ret; 1498 } 1499 EXPORT_SYMBOL_GPL(usb_add_gadget); 1500 1501 /** 1502 * usb_add_gadget_udc_release - adds a new gadget to the udc class driver list 1503 * @parent: the parent device to this udc. Usually the controller driver's 1504 * device. 1505 * @gadget: the gadget to be added to the list. 1506 * @release: a gadget release function. 1507 * 1508 * Returns zero on success, negative errno otherwise. 1509 * Calls the gadget release function in the latter case. 1510 */ 1511 int usb_add_gadget_udc_release(struct device *parent, struct usb_gadget *gadget, 1512 void (*release)(struct device *dev)) 1513 { 1514 int ret; 1515 1516 usb_initialize_gadget(parent, gadget, release); 1517 ret = usb_add_gadget(gadget); 1518 if (ret) 1519 usb_put_gadget(gadget); 1520 return ret; 1521 } 1522 EXPORT_SYMBOL_GPL(usb_add_gadget_udc_release); 1523 1524 /** 1525 * usb_get_gadget_udc_name - get the name of the first UDC controller 1526 * This functions returns the name of the first UDC controller in the system. 1527 * Please note that this interface is usefull only for legacy drivers which 1528 * assume that there is only one UDC controller in the system and they need to 1529 * get its name before initialization. There is no guarantee that the UDC 1530 * of the returned name will be still available, when gadget driver registers 1531 * itself. 1532 * 1533 * Returns pointer to string with UDC controller name on success, NULL 1534 * otherwise. Caller should kfree() returned string. 1535 */ 1536 char *usb_get_gadget_udc_name(void) 1537 { 1538 struct usb_udc *udc; 1539 char *name = NULL; 1540 1541 /* For now we take the first available UDC */ 1542 mutex_lock(&udc_lock); 1543 list_for_each_entry(udc, &udc_list, list) { 1544 if (!udc->driver) { 1545 name = kstrdup(udc->gadget->name, GFP_KERNEL); 1546 break; 1547 } 1548 } 1549 mutex_unlock(&udc_lock); 1550 return name; 1551 } 1552 EXPORT_SYMBOL_GPL(usb_get_gadget_udc_name); 1553 1554 /** 1555 * usb_add_gadget_udc - adds a new gadget to the udc class driver list 1556 * @parent: the parent device to this udc. Usually the controller 1557 * driver's device. 1558 * @gadget: the gadget to be added to the list 1559 * 1560 * Returns zero on success, negative errno otherwise. 1561 */ 1562 int usb_add_gadget_udc(struct device *parent, struct usb_gadget *gadget) 1563 { 1564 return usb_add_gadget_udc_release(parent, gadget, NULL); 1565 } 1566 EXPORT_SYMBOL_GPL(usb_add_gadget_udc); 1567 1568 /** 1569 * usb_del_gadget - deletes a gadget and unregisters its udc 1570 * @gadget: the gadget to be deleted. 1571 * 1572 * This will unbind @gadget, if it is bound. 1573 * It will not do a final usb_put_gadget(). 1574 */ 1575 void usb_del_gadget(struct usb_gadget *gadget) 1576 { 1577 struct usb_udc *udc = gadget->udc; 1578 unsigned long flags; 1579 1580 if (!udc) 1581 return; 1582 1583 dev_vdbg(gadget->dev.parent, "unregistering gadget\n"); 1584 1585 mutex_lock(&udc_lock); 1586 list_del(&udc->list); 1587 mutex_unlock(&udc_lock); 1588 1589 kobject_uevent(&udc->dev.kobj, KOBJ_REMOVE); 1590 sysfs_remove_link(&udc->dev.kobj, "gadget"); 1591 device_del(&gadget->dev); 1592 /* 1593 * Set the teardown flag before flushing the work to prevent new work 1594 * from being scheduled while we are cleaning up. 1595 */ 1596 spin_lock_irqsave(&gadget->state_lock, flags); 1597 gadget->teardown = true; 1598 spin_unlock_irqrestore(&gadget->state_lock, flags); 1599 flush_work(&gadget->work); 1600 ida_free(&gadget_id_numbers, gadget->id_number); 1601 cancel_work_sync(&udc->vbus_work); 1602 device_unregister(&udc->dev); 1603 } 1604 EXPORT_SYMBOL_GPL(usb_del_gadget); 1605 1606 /** 1607 * usb_del_gadget_udc - unregisters a gadget 1608 * @gadget: the gadget to be unregistered. 1609 * 1610 * Calls usb_del_gadget() and does a final usb_put_gadget(). 1611 */ 1612 void usb_del_gadget_udc(struct usb_gadget *gadget) 1613 { 1614 usb_del_gadget(gadget); 1615 usb_put_gadget(gadget); 1616 } 1617 EXPORT_SYMBOL_GPL(usb_del_gadget_udc); 1618 1619 /* ------------------------------------------------------------------------- */ 1620 1621 static int gadget_match_driver(struct device *dev, const struct device_driver *drv) 1622 { 1623 struct usb_gadget *gadget = dev_to_usb_gadget(dev); 1624 struct usb_udc *udc = gadget->udc; 1625 const struct usb_gadget_driver *driver = container_of(drv, 1626 struct usb_gadget_driver, driver); 1627 1628 /* If the driver specifies a udc_name, it must match the UDC's name */ 1629 if (driver->udc_name && 1630 strcmp(driver->udc_name, dev_name(&udc->dev)) != 0) 1631 return 0; 1632 1633 /* If the driver is already bound to a gadget, it doesn't match */ 1634 if (driver->is_bound) 1635 return 0; 1636 1637 /* Otherwise any gadget driver matches any UDC */ 1638 return 1; 1639 } 1640 1641 static int gadget_bind_driver(struct device *dev) 1642 { 1643 struct usb_gadget *gadget = dev_to_usb_gadget(dev); 1644 struct usb_udc *udc = gadget->udc; 1645 struct usb_gadget_driver *driver = container_of(dev->driver, 1646 struct usb_gadget_driver, driver); 1647 int ret = 0; 1648 1649 mutex_lock(&udc_lock); 1650 if (driver->is_bound) { 1651 mutex_unlock(&udc_lock); 1652 return -ENXIO; /* Driver binds to only one gadget */ 1653 } 1654 driver->is_bound = true; 1655 udc->driver = driver; 1656 mutex_unlock(&udc_lock); 1657 1658 dev_dbg(&udc->dev, "binding gadget driver [%s]\n", driver->function); 1659 1660 usb_gadget_udc_set_speed(udc, driver->max_speed); 1661 1662 ret = driver->bind(udc->gadget, driver); 1663 if (ret) 1664 goto err_bind; 1665 1666 mutex_lock(&udc->connect_lock); 1667 ret = usb_gadget_udc_start_locked(udc); 1668 if (ret) { 1669 mutex_unlock(&udc->connect_lock); 1670 goto err_start; 1671 } 1672 usb_gadget_enable_async_callbacks(udc); 1673 udc->allow_connect = true; 1674 ret = usb_udc_connect_control_locked(udc); 1675 if (ret) 1676 goto err_connect_control; 1677 1678 mutex_unlock(&udc->connect_lock); 1679 1680 kobject_uevent(&udc->dev.kobj, KOBJ_CHANGE); 1681 return 0; 1682 1683 err_connect_control: 1684 udc->allow_connect = false; 1685 usb_gadget_disable_async_callbacks(udc); 1686 if (gadget->irq) 1687 synchronize_irq(gadget->irq); 1688 usb_gadget_udc_stop_locked(udc); 1689 mutex_unlock(&udc->connect_lock); 1690 1691 err_start: 1692 driver->unbind(udc->gadget); 1693 1694 err_bind: 1695 if (ret != -EISNAM) 1696 dev_err(&udc->dev, "failed to start %s: %d\n", 1697 driver->function, ret); 1698 1699 mutex_lock(&udc_lock); 1700 udc->driver = NULL; 1701 driver->is_bound = false; 1702 mutex_unlock(&udc_lock); 1703 1704 return ret; 1705 } 1706 1707 static void gadget_unbind_driver(struct device *dev) 1708 { 1709 struct usb_gadget *gadget = dev_to_usb_gadget(dev); 1710 struct usb_udc *udc = gadget->udc; 1711 struct usb_gadget_driver *driver = udc->driver; 1712 1713 dev_dbg(&udc->dev, "unbinding gadget driver [%s]\n", driver->function); 1714 1715 udc->allow_connect = false; 1716 cancel_work_sync(&udc->vbus_work); 1717 mutex_lock(&udc->connect_lock); 1718 usb_gadget_disconnect_locked(gadget); 1719 usb_gadget_disable_async_callbacks(udc); 1720 if (gadget->irq) 1721 synchronize_irq(gadget->irq); 1722 mutex_unlock(&udc->connect_lock); 1723 1724 udc->driver->unbind(gadget); 1725 1726 mutex_lock(&udc->connect_lock); 1727 usb_gadget_udc_stop_locked(udc); 1728 mutex_unlock(&udc->connect_lock); 1729 1730 mutex_lock(&udc_lock); 1731 driver->is_bound = false; 1732 udc->driver = NULL; 1733 mutex_unlock(&udc_lock); 1734 1735 kobject_uevent(&udc->dev.kobj, KOBJ_CHANGE); 1736 } 1737 1738 /* ------------------------------------------------------------------------- */ 1739 1740 int usb_gadget_register_driver_owner(struct usb_gadget_driver *driver, 1741 struct module *owner, const char *mod_name) 1742 { 1743 int ret; 1744 1745 if (!driver || !driver->bind || !driver->setup) 1746 return -EINVAL; 1747 1748 driver->driver.bus = &gadget_bus_type; 1749 driver->driver.owner = owner; 1750 driver->driver.mod_name = mod_name; 1751 driver->driver.probe_type = PROBE_FORCE_SYNCHRONOUS; 1752 ret = driver_register(&driver->driver); 1753 if (ret) { 1754 pr_warn("%s: driver registration failed: %d\n", 1755 driver->function, ret); 1756 return ret; 1757 } 1758 1759 mutex_lock(&udc_lock); 1760 if (!driver->is_bound) { 1761 if (driver->match_existing_only) { 1762 pr_warn("%s: couldn't find an available UDC or it's busy\n", 1763 driver->function); 1764 ret = -EBUSY; 1765 } else { 1766 pr_info("%s: couldn't find an available UDC\n", 1767 driver->function); 1768 ret = 0; 1769 } 1770 } 1771 mutex_unlock(&udc_lock); 1772 1773 if (ret) 1774 driver_unregister(&driver->driver); 1775 return ret; 1776 } 1777 EXPORT_SYMBOL_GPL(usb_gadget_register_driver_owner); 1778 1779 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver) 1780 { 1781 if (!driver || !driver->unbind) 1782 return -EINVAL; 1783 1784 driver_unregister(&driver->driver); 1785 return 0; 1786 } 1787 EXPORT_SYMBOL_GPL(usb_gadget_unregister_driver); 1788 1789 /* ------------------------------------------------------------------------- */ 1790 1791 static ssize_t srp_store(struct device *dev, 1792 struct device_attribute *attr, const char *buf, size_t n) 1793 { 1794 struct usb_udc *udc = container_of(dev, struct usb_udc, dev); 1795 1796 if (sysfs_streq(buf, "1")) 1797 usb_gadget_wakeup(udc->gadget); 1798 1799 return n; 1800 } 1801 static DEVICE_ATTR_WO(srp); 1802 1803 static ssize_t soft_connect_store(struct device *dev, 1804 struct device_attribute *attr, const char *buf, size_t n) 1805 { 1806 struct usb_udc *udc = container_of(dev, struct usb_udc, dev); 1807 ssize_t ret; 1808 1809 device_lock(&udc->gadget->dev); 1810 if (!udc->driver) { 1811 dev_err(dev, "soft-connect without a gadget driver\n"); 1812 ret = -EOPNOTSUPP; 1813 goto out; 1814 } 1815 1816 if (sysfs_streq(buf, "connect")) { 1817 mutex_lock(&udc->connect_lock); 1818 usb_gadget_udc_start_locked(udc); 1819 usb_gadget_connect_locked(udc->gadget); 1820 mutex_unlock(&udc->connect_lock); 1821 } else if (sysfs_streq(buf, "disconnect")) { 1822 mutex_lock(&udc->connect_lock); 1823 usb_gadget_disconnect_locked(udc->gadget); 1824 usb_gadget_udc_stop_locked(udc); 1825 mutex_unlock(&udc->connect_lock); 1826 } else { 1827 dev_err(dev, "unsupported command '%s'\n", buf); 1828 ret = -EINVAL; 1829 goto out; 1830 } 1831 1832 ret = n; 1833 out: 1834 device_unlock(&udc->gadget->dev); 1835 return ret; 1836 } 1837 static DEVICE_ATTR_WO(soft_connect); 1838 1839 static ssize_t state_show(struct device *dev, struct device_attribute *attr, 1840 char *buf) 1841 { 1842 struct usb_udc *udc = container_of(dev, struct usb_udc, dev); 1843 struct usb_gadget *gadget = udc->gadget; 1844 1845 return sprintf(buf, "%s\n", usb_state_string(gadget->state)); 1846 } 1847 static DEVICE_ATTR_RO(state); 1848 1849 static ssize_t function_show(struct device *dev, struct device_attribute *attr, 1850 char *buf) 1851 { 1852 struct usb_udc *udc = container_of(dev, struct usb_udc, dev); 1853 struct usb_gadget_driver *drv; 1854 int rc = 0; 1855 1856 mutex_lock(&udc_lock); 1857 drv = udc->driver; 1858 if (drv && drv->function) 1859 rc = scnprintf(buf, PAGE_SIZE, "%s\n", drv->function); 1860 mutex_unlock(&udc_lock); 1861 return rc; 1862 } 1863 static DEVICE_ATTR_RO(function); 1864 1865 #define USB_UDC_SPEED_ATTR(name, param) \ 1866 ssize_t name##_show(struct device *dev, \ 1867 struct device_attribute *attr, char *buf) \ 1868 { \ 1869 struct usb_udc *udc = container_of(dev, struct usb_udc, dev); \ 1870 return scnprintf(buf, PAGE_SIZE, "%s\n", \ 1871 usb_speed_string(udc->gadget->param)); \ 1872 } \ 1873 static DEVICE_ATTR_RO(name) 1874 1875 static USB_UDC_SPEED_ATTR(current_speed, speed); 1876 static USB_UDC_SPEED_ATTR(maximum_speed, max_speed); 1877 1878 #define USB_UDC_ATTR(name) \ 1879 ssize_t name##_show(struct device *dev, \ 1880 struct device_attribute *attr, char *buf) \ 1881 { \ 1882 struct usb_udc *udc = container_of(dev, struct usb_udc, dev); \ 1883 struct usb_gadget *gadget = udc->gadget; \ 1884 \ 1885 return scnprintf(buf, PAGE_SIZE, "%d\n", gadget->name); \ 1886 } \ 1887 static DEVICE_ATTR_RO(name) 1888 1889 static USB_UDC_ATTR(is_otg); 1890 static USB_UDC_ATTR(is_a_peripheral); 1891 static USB_UDC_ATTR(b_hnp_enable); 1892 static USB_UDC_ATTR(a_hnp_support); 1893 static USB_UDC_ATTR(a_alt_hnp_support); 1894 static USB_UDC_ATTR(is_selfpowered); 1895 1896 static struct attribute *usb_udc_attrs[] = { 1897 &dev_attr_srp.attr, 1898 &dev_attr_soft_connect.attr, 1899 &dev_attr_state.attr, 1900 &dev_attr_function.attr, 1901 &dev_attr_current_speed.attr, 1902 &dev_attr_maximum_speed.attr, 1903 1904 &dev_attr_is_otg.attr, 1905 &dev_attr_is_a_peripheral.attr, 1906 &dev_attr_b_hnp_enable.attr, 1907 &dev_attr_a_hnp_support.attr, 1908 &dev_attr_a_alt_hnp_support.attr, 1909 &dev_attr_is_selfpowered.attr, 1910 NULL, 1911 }; 1912 1913 static const struct attribute_group usb_udc_attr_group = { 1914 .attrs = usb_udc_attrs, 1915 }; 1916 1917 static const struct attribute_group *usb_udc_attr_groups[] = { 1918 &usb_udc_attr_group, 1919 NULL, 1920 }; 1921 1922 static int usb_udc_uevent(const struct device *dev, struct kobj_uevent_env *env) 1923 { 1924 const struct usb_udc *udc = container_of(dev, struct usb_udc, dev); 1925 int ret; 1926 1927 ret = add_uevent_var(env, "USB_UDC_NAME=%s", udc->gadget->name); 1928 if (ret) { 1929 dev_err(dev, "failed to add uevent USB_UDC_NAME\n"); 1930 return ret; 1931 } 1932 1933 mutex_lock(&udc_lock); 1934 if (udc->driver) 1935 ret = add_uevent_var(env, "USB_UDC_DRIVER=%s", 1936 udc->driver->function); 1937 mutex_unlock(&udc_lock); 1938 if (ret) { 1939 dev_err(dev, "failed to add uevent USB_UDC_DRIVER\n"); 1940 return ret; 1941 } 1942 1943 return 0; 1944 } 1945 1946 static const struct class udc_class = { 1947 .name = "udc", 1948 .dev_uevent = usb_udc_uevent, 1949 }; 1950 1951 static const struct bus_type gadget_bus_type = { 1952 .name = "gadget", 1953 .probe = gadget_bind_driver, 1954 .remove = gadget_unbind_driver, 1955 .match = gadget_match_driver, 1956 }; 1957 1958 static int __init usb_udc_init(void) 1959 { 1960 int rc; 1961 1962 rc = class_register(&udc_class); 1963 if (rc) 1964 return rc; 1965 1966 rc = bus_register(&gadget_bus_type); 1967 if (rc) 1968 class_unregister(&udc_class); 1969 return rc; 1970 } 1971 subsys_initcall(usb_udc_init); 1972 1973 static void __exit usb_udc_exit(void) 1974 { 1975 bus_unregister(&gadget_bus_type); 1976 class_unregister(&udc_class); 1977 } 1978 module_exit(usb_udc_exit); 1979 1980 MODULE_DESCRIPTION("UDC Framework"); 1981 MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>"); 1982 MODULE_LICENSE("GPL v2"); 1983