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