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 its 285 * completion routine is called (with status -ECONNRESET); else a negative 286 * error code is returned. This is guaranteed to happen before the call to 287 * usb_ep_dequeue() returns. 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 * Returns zero on success, else negative errno. 694 */ 695 int usb_gadget_disconnect(struct usb_gadget *gadget) 696 { 697 int ret = 0; 698 699 if (!gadget->ops->pullup) { 700 ret = -EOPNOTSUPP; 701 goto out; 702 } 703 704 if (gadget->deactivated) { 705 /* 706 * If gadget is deactivated we only save new state. 707 * Gadget will stay disconnected after activation. 708 */ 709 gadget->connected = false; 710 goto out; 711 } 712 713 ret = gadget->ops->pullup(gadget, 0); 714 if (!ret) 715 gadget->connected = 0; 716 717 out: 718 trace_usb_gadget_disconnect(gadget, ret); 719 720 return ret; 721 } 722 EXPORT_SYMBOL_GPL(usb_gadget_disconnect); 723 724 /** 725 * usb_gadget_deactivate - deactivate function which is not ready to work 726 * @gadget: the peripheral being deactivated 727 * 728 * This routine may be used during the gadget driver bind() call to prevent 729 * the peripheral from ever being visible to the USB host, unless later 730 * usb_gadget_activate() is called. For example, user mode components may 731 * need to be activated before the system can talk to hosts. 732 * 733 * Returns zero on success, else negative errno. 734 */ 735 int usb_gadget_deactivate(struct usb_gadget *gadget) 736 { 737 int ret = 0; 738 739 if (gadget->deactivated) 740 goto out; 741 742 if (gadget->connected) { 743 ret = usb_gadget_disconnect(gadget); 744 if (ret) 745 goto out; 746 747 /* 748 * If gadget was being connected before deactivation, we want 749 * to reconnect it in usb_gadget_activate(). 750 */ 751 gadget->connected = true; 752 } 753 gadget->deactivated = true; 754 755 out: 756 trace_usb_gadget_deactivate(gadget, ret); 757 758 return ret; 759 } 760 EXPORT_SYMBOL_GPL(usb_gadget_deactivate); 761 762 /** 763 * usb_gadget_activate - activate function which is not ready to work 764 * @gadget: the peripheral being activated 765 * 766 * This routine activates gadget which was previously deactivated with 767 * usb_gadget_deactivate() call. It calls usb_gadget_connect() if needed. 768 * 769 * Returns zero on success, else negative errno. 770 */ 771 int usb_gadget_activate(struct usb_gadget *gadget) 772 { 773 int ret = 0; 774 775 if (!gadget->deactivated) 776 goto out; 777 778 gadget->deactivated = false; 779 780 /* 781 * If gadget has been connected before deactivation, or became connected 782 * while it was being deactivated, we call usb_gadget_connect(). 783 */ 784 if (gadget->connected) 785 ret = usb_gadget_connect(gadget); 786 787 out: 788 trace_usb_gadget_activate(gadget, ret); 789 790 return ret; 791 } 792 EXPORT_SYMBOL_GPL(usb_gadget_activate); 793 794 /* ------------------------------------------------------------------------- */ 795 796 #ifdef CONFIG_HAS_DMA 797 798 int usb_gadget_map_request_by_dev(struct device *dev, 799 struct usb_request *req, int is_in) 800 { 801 if (req->length == 0) 802 return 0; 803 804 if (req->num_sgs) { 805 int mapped; 806 807 mapped = dma_map_sg(dev, req->sg, req->num_sgs, 808 is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE); 809 if (mapped == 0) { 810 dev_err(dev, "failed to map SGs\n"); 811 return -EFAULT; 812 } 813 814 req->num_mapped_sgs = mapped; 815 } else { 816 if (is_vmalloc_addr(req->buf)) { 817 dev_err(dev, "buffer is not dma capable\n"); 818 return -EFAULT; 819 } else if (object_is_on_stack(req->buf)) { 820 dev_err(dev, "buffer is on stack\n"); 821 return -EFAULT; 822 } 823 824 req->dma = dma_map_single(dev, req->buf, req->length, 825 is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE); 826 827 if (dma_mapping_error(dev, req->dma)) { 828 dev_err(dev, "failed to map buffer\n"); 829 return -EFAULT; 830 } 831 832 req->dma_mapped = 1; 833 } 834 835 return 0; 836 } 837 EXPORT_SYMBOL_GPL(usb_gadget_map_request_by_dev); 838 839 int usb_gadget_map_request(struct usb_gadget *gadget, 840 struct usb_request *req, int is_in) 841 { 842 return usb_gadget_map_request_by_dev(gadget->dev.parent, req, is_in); 843 } 844 EXPORT_SYMBOL_GPL(usb_gadget_map_request); 845 846 void usb_gadget_unmap_request_by_dev(struct device *dev, 847 struct usb_request *req, int is_in) 848 { 849 if (req->length == 0) 850 return; 851 852 if (req->num_mapped_sgs) { 853 dma_unmap_sg(dev, req->sg, req->num_sgs, 854 is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE); 855 856 req->num_mapped_sgs = 0; 857 } else if (req->dma_mapped) { 858 dma_unmap_single(dev, req->dma, req->length, 859 is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE); 860 req->dma_mapped = 0; 861 } 862 } 863 EXPORT_SYMBOL_GPL(usb_gadget_unmap_request_by_dev); 864 865 void usb_gadget_unmap_request(struct usb_gadget *gadget, 866 struct usb_request *req, int is_in) 867 { 868 usb_gadget_unmap_request_by_dev(gadget->dev.parent, req, is_in); 869 } 870 EXPORT_SYMBOL_GPL(usb_gadget_unmap_request); 871 872 #endif /* CONFIG_HAS_DMA */ 873 874 /* ------------------------------------------------------------------------- */ 875 876 /** 877 * usb_gadget_giveback_request - give the request back to the gadget layer 878 * Context: in_interrupt() 879 * 880 * This is called by device controller drivers in order to return the 881 * completed request back to the gadget layer. 882 */ 883 void usb_gadget_giveback_request(struct usb_ep *ep, 884 struct usb_request *req) 885 { 886 if (likely(req->status == 0)) 887 usb_led_activity(USB_LED_EVENT_GADGET); 888 889 trace_usb_gadget_giveback_request(ep, req, 0); 890 891 req->complete(ep, req); 892 } 893 EXPORT_SYMBOL_GPL(usb_gadget_giveback_request); 894 895 /* ------------------------------------------------------------------------- */ 896 897 /** 898 * gadget_find_ep_by_name - returns ep whose name is the same as sting passed 899 * in second parameter or NULL if searched endpoint not found 900 * @g: controller to check for quirk 901 * @name: name of searched endpoint 902 */ 903 struct usb_ep *gadget_find_ep_by_name(struct usb_gadget *g, const char *name) 904 { 905 struct usb_ep *ep; 906 907 gadget_for_each_ep(ep, g) { 908 if (!strcmp(ep->name, name)) 909 return ep; 910 } 911 912 return NULL; 913 } 914 EXPORT_SYMBOL_GPL(gadget_find_ep_by_name); 915 916 /* ------------------------------------------------------------------------- */ 917 918 int usb_gadget_ep_match_desc(struct usb_gadget *gadget, 919 struct usb_ep *ep, struct usb_endpoint_descriptor *desc, 920 struct usb_ss_ep_comp_descriptor *ep_comp) 921 { 922 u8 type; 923 u16 max; 924 int num_req_streams = 0; 925 926 /* endpoint already claimed? */ 927 if (ep->claimed) 928 return 0; 929 930 type = usb_endpoint_type(desc); 931 max = usb_endpoint_maxp(desc); 932 933 if (usb_endpoint_dir_in(desc) && !ep->caps.dir_in) 934 return 0; 935 if (usb_endpoint_dir_out(desc) && !ep->caps.dir_out) 936 return 0; 937 938 if (max > ep->maxpacket_limit) 939 return 0; 940 941 /* "high bandwidth" works only at high speed */ 942 if (!gadget_is_dualspeed(gadget) && usb_endpoint_maxp_mult(desc) > 1) 943 return 0; 944 945 switch (type) { 946 case USB_ENDPOINT_XFER_CONTROL: 947 /* only support ep0 for portable CONTROL traffic */ 948 return 0; 949 case USB_ENDPOINT_XFER_ISOC: 950 if (!ep->caps.type_iso) 951 return 0; 952 /* ISO: limit 1023 bytes full speed, 1024 high/super speed */ 953 if (!gadget_is_dualspeed(gadget) && max > 1023) 954 return 0; 955 break; 956 case USB_ENDPOINT_XFER_BULK: 957 if (!ep->caps.type_bulk) 958 return 0; 959 if (ep_comp && gadget_is_superspeed(gadget)) { 960 /* Get the number of required streams from the 961 * EP companion descriptor and see if the EP 962 * matches it 963 */ 964 num_req_streams = ep_comp->bmAttributes & 0x1f; 965 if (num_req_streams > ep->max_streams) 966 return 0; 967 } 968 break; 969 case USB_ENDPOINT_XFER_INT: 970 /* Bulk endpoints handle interrupt transfers, 971 * except the toggle-quirky iso-synch kind 972 */ 973 if (!ep->caps.type_int && !ep->caps.type_bulk) 974 return 0; 975 /* INT: limit 64 bytes full speed, 1024 high/super speed */ 976 if (!gadget_is_dualspeed(gadget) && max > 64) 977 return 0; 978 break; 979 } 980 981 return 1; 982 } 983 EXPORT_SYMBOL_GPL(usb_gadget_ep_match_desc); 984 985 /* ------------------------------------------------------------------------- */ 986 987 static void usb_gadget_state_work(struct work_struct *work) 988 { 989 struct usb_gadget *gadget = work_to_gadget(work); 990 struct usb_udc *udc = gadget->udc; 991 992 if (udc) 993 sysfs_notify(&udc->dev.kobj, NULL, "state"); 994 } 995 996 void usb_gadget_set_state(struct usb_gadget *gadget, 997 enum usb_device_state state) 998 { 999 gadget->state = state; 1000 schedule_work(&gadget->work); 1001 } 1002 EXPORT_SYMBOL_GPL(usb_gadget_set_state); 1003 1004 /* ------------------------------------------------------------------------- */ 1005 1006 static void usb_udc_connect_control(struct usb_udc *udc) 1007 { 1008 if (udc->vbus) 1009 usb_gadget_connect(udc->gadget); 1010 else 1011 usb_gadget_disconnect(udc->gadget); 1012 } 1013 1014 /** 1015 * usb_udc_vbus_handler - updates the udc core vbus status, and try to 1016 * connect or disconnect gadget 1017 * @gadget: The gadget which vbus change occurs 1018 * @status: The vbus status 1019 * 1020 * The udc driver calls it when it wants to connect or disconnect gadget 1021 * according to vbus status. 1022 */ 1023 void usb_udc_vbus_handler(struct usb_gadget *gadget, bool status) 1024 { 1025 struct usb_udc *udc = gadget->udc; 1026 1027 if (udc) { 1028 udc->vbus = status; 1029 usb_udc_connect_control(udc); 1030 } 1031 } 1032 EXPORT_SYMBOL_GPL(usb_udc_vbus_handler); 1033 1034 /** 1035 * usb_gadget_udc_reset - notifies the udc core that bus reset occurs 1036 * @gadget: The gadget which bus reset occurs 1037 * @driver: The gadget driver we want to notify 1038 * 1039 * If the udc driver has bus reset handler, it needs to call this when the bus 1040 * reset occurs, it notifies the gadget driver that the bus reset occurs as 1041 * well as updates gadget state. 1042 */ 1043 void usb_gadget_udc_reset(struct usb_gadget *gadget, 1044 struct usb_gadget_driver *driver) 1045 { 1046 driver->reset(gadget); 1047 usb_gadget_set_state(gadget, USB_STATE_DEFAULT); 1048 } 1049 EXPORT_SYMBOL_GPL(usb_gadget_udc_reset); 1050 1051 /** 1052 * usb_gadget_udc_start - tells usb device controller to start up 1053 * @udc: The UDC to be started 1054 * 1055 * This call is issued by the UDC Class driver when it's about 1056 * to register a gadget driver to the device controller, before 1057 * calling gadget driver's bind() method. 1058 * 1059 * It allows the controller to be powered off until strictly 1060 * necessary to have it powered on. 1061 * 1062 * Returns zero on success, else negative errno. 1063 */ 1064 static inline int usb_gadget_udc_start(struct usb_udc *udc) 1065 { 1066 return udc->gadget->ops->udc_start(udc->gadget, udc->driver); 1067 } 1068 1069 /** 1070 * usb_gadget_udc_stop - tells usb device controller we don't need it anymore 1071 * @gadget: The device we want to stop activity 1072 * @driver: The driver to unbind from @gadget 1073 * 1074 * This call is issued by the UDC Class driver after calling 1075 * gadget driver's unbind() method. 1076 * 1077 * The details are implementation specific, but it can go as 1078 * far as powering off UDC completely and disable its data 1079 * line pullups. 1080 */ 1081 static inline void usb_gadget_udc_stop(struct usb_udc *udc) 1082 { 1083 udc->gadget->ops->udc_stop(udc->gadget); 1084 } 1085 1086 /** 1087 * usb_gadget_udc_set_speed - tells usb device controller speed supported by 1088 * current driver 1089 * @udc: The device we want to set maximum speed 1090 * @speed: The maximum speed to allowed to run 1091 * 1092 * This call is issued by the UDC Class driver before calling 1093 * usb_gadget_udc_start() in order to make sure that we don't try to 1094 * connect on speeds the gadget driver doesn't support. 1095 */ 1096 static inline void usb_gadget_udc_set_speed(struct usb_udc *udc, 1097 enum usb_device_speed speed) 1098 { 1099 if (udc->gadget->ops->udc_set_speed) { 1100 enum usb_device_speed s; 1101 1102 s = min(speed, udc->gadget->max_speed); 1103 udc->gadget->ops->udc_set_speed(udc->gadget, s); 1104 } 1105 } 1106 1107 /** 1108 * usb_udc_release - release the usb_udc struct 1109 * @dev: the dev member within usb_udc 1110 * 1111 * This is called by driver's core in order to free memory once the last 1112 * reference is released. 1113 */ 1114 static void usb_udc_release(struct device *dev) 1115 { 1116 struct usb_udc *udc; 1117 1118 udc = container_of(dev, struct usb_udc, dev); 1119 dev_dbg(dev, "releasing '%s'\n", dev_name(dev)); 1120 kfree(udc); 1121 } 1122 1123 static const struct attribute_group *usb_udc_attr_groups[]; 1124 1125 static void usb_udc_nop_release(struct device *dev) 1126 { 1127 dev_vdbg(dev, "%s\n", __func__); 1128 } 1129 1130 /* should be called with udc_lock held */ 1131 static int check_pending_gadget_drivers(struct usb_udc *udc) 1132 { 1133 struct usb_gadget_driver *driver; 1134 int ret = 0; 1135 1136 list_for_each_entry(driver, &gadget_driver_pending_list, pending) 1137 if (!driver->udc_name || strcmp(driver->udc_name, 1138 dev_name(&udc->dev)) == 0) { 1139 ret = udc_bind_to_driver(udc, driver); 1140 if (ret != -EPROBE_DEFER) 1141 list_del(&driver->pending); 1142 break; 1143 } 1144 1145 return ret; 1146 } 1147 1148 /** 1149 * usb_add_gadget_udc_release - adds a new gadget to the udc class driver list 1150 * @parent: the parent device to this udc. Usually the controller driver's 1151 * device. 1152 * @gadget: the gadget to be added to the list. 1153 * @release: a gadget release function. 1154 * 1155 * Returns zero on success, negative errno otherwise. 1156 * Calls the gadget release function in the latter case. 1157 */ 1158 int usb_add_gadget_udc_release(struct device *parent, struct usb_gadget *gadget, 1159 void (*release)(struct device *dev)) 1160 { 1161 struct usb_udc *udc; 1162 int ret = -ENOMEM; 1163 1164 dev_set_name(&gadget->dev, "gadget"); 1165 INIT_WORK(&gadget->work, usb_gadget_state_work); 1166 gadget->dev.parent = parent; 1167 1168 if (release) 1169 gadget->dev.release = release; 1170 else 1171 gadget->dev.release = usb_udc_nop_release; 1172 1173 device_initialize(&gadget->dev); 1174 1175 udc = kzalloc(sizeof(*udc), GFP_KERNEL); 1176 if (!udc) 1177 goto err_put_gadget; 1178 1179 device_initialize(&udc->dev); 1180 udc->dev.release = usb_udc_release; 1181 udc->dev.class = udc_class; 1182 udc->dev.groups = usb_udc_attr_groups; 1183 udc->dev.parent = parent; 1184 ret = dev_set_name(&udc->dev, "%s", kobject_name(&parent->kobj)); 1185 if (ret) 1186 goto err_put_udc; 1187 1188 ret = device_add(&gadget->dev); 1189 if (ret) 1190 goto err_put_udc; 1191 1192 udc->gadget = gadget; 1193 gadget->udc = udc; 1194 1195 mutex_lock(&udc_lock); 1196 list_add_tail(&udc->list, &udc_list); 1197 1198 ret = device_add(&udc->dev); 1199 if (ret) 1200 goto err_unlist_udc; 1201 1202 usb_gadget_set_state(gadget, USB_STATE_NOTATTACHED); 1203 udc->vbus = true; 1204 1205 /* pick up one of pending gadget drivers */ 1206 ret = check_pending_gadget_drivers(udc); 1207 if (ret) 1208 goto err_del_udc; 1209 1210 mutex_unlock(&udc_lock); 1211 1212 return 0; 1213 1214 err_del_udc: 1215 device_del(&udc->dev); 1216 1217 err_unlist_udc: 1218 list_del(&udc->list); 1219 mutex_unlock(&udc_lock); 1220 1221 device_del(&gadget->dev); 1222 1223 err_put_udc: 1224 put_device(&udc->dev); 1225 1226 err_put_gadget: 1227 put_device(&gadget->dev); 1228 return ret; 1229 } 1230 EXPORT_SYMBOL_GPL(usb_add_gadget_udc_release); 1231 1232 /** 1233 * usb_get_gadget_udc_name - get the name of the first UDC controller 1234 * This functions returns the name of the first UDC controller in the system. 1235 * Please note that this interface is usefull only for legacy drivers which 1236 * assume that there is only one UDC controller in the system and they need to 1237 * get its name before initialization. There is no guarantee that the UDC 1238 * of the returned name will be still available, when gadget driver registers 1239 * itself. 1240 * 1241 * Returns pointer to string with UDC controller name on success, NULL 1242 * otherwise. Caller should kfree() returned string. 1243 */ 1244 char *usb_get_gadget_udc_name(void) 1245 { 1246 struct usb_udc *udc; 1247 char *name = NULL; 1248 1249 /* For now we take the first available UDC */ 1250 mutex_lock(&udc_lock); 1251 list_for_each_entry(udc, &udc_list, list) { 1252 if (!udc->driver) { 1253 name = kstrdup(udc->gadget->name, GFP_KERNEL); 1254 break; 1255 } 1256 } 1257 mutex_unlock(&udc_lock); 1258 return name; 1259 } 1260 EXPORT_SYMBOL_GPL(usb_get_gadget_udc_name); 1261 1262 /** 1263 * usb_add_gadget_udc - adds a new gadget to the udc class driver list 1264 * @parent: the parent device to this udc. Usually the controller 1265 * driver's device. 1266 * @gadget: the gadget to be added to the list 1267 * 1268 * Returns zero on success, negative errno otherwise. 1269 */ 1270 int usb_add_gadget_udc(struct device *parent, struct usb_gadget *gadget) 1271 { 1272 return usb_add_gadget_udc_release(parent, gadget, NULL); 1273 } 1274 EXPORT_SYMBOL_GPL(usb_add_gadget_udc); 1275 1276 static void usb_gadget_remove_driver(struct usb_udc *udc) 1277 { 1278 dev_dbg(&udc->dev, "unregistering UDC driver [%s]\n", 1279 udc->driver->function); 1280 1281 kobject_uevent(&udc->dev.kobj, KOBJ_CHANGE); 1282 1283 usb_gadget_disconnect(udc->gadget); 1284 udc->driver->disconnect(udc->gadget); 1285 udc->driver->unbind(udc->gadget); 1286 usb_gadget_udc_stop(udc); 1287 1288 udc->driver = NULL; 1289 udc->dev.driver = NULL; 1290 udc->gadget->dev.driver = NULL; 1291 } 1292 1293 /** 1294 * usb_del_gadget_udc - deletes @udc from udc_list 1295 * @gadget: the gadget to be removed. 1296 * 1297 * This, will call usb_gadget_unregister_driver() if 1298 * the @udc is still busy. 1299 */ 1300 void usb_del_gadget_udc(struct usb_gadget *gadget) 1301 { 1302 struct usb_udc *udc = gadget->udc; 1303 1304 if (!udc) 1305 return; 1306 1307 dev_vdbg(gadget->dev.parent, "unregistering gadget\n"); 1308 1309 mutex_lock(&udc_lock); 1310 list_del(&udc->list); 1311 1312 if (udc->driver) { 1313 struct usb_gadget_driver *driver = udc->driver; 1314 1315 usb_gadget_remove_driver(udc); 1316 list_add(&driver->pending, &gadget_driver_pending_list); 1317 } 1318 mutex_unlock(&udc_lock); 1319 1320 kobject_uevent(&udc->dev.kobj, KOBJ_REMOVE); 1321 flush_work(&gadget->work); 1322 device_unregister(&udc->dev); 1323 device_unregister(&gadget->dev); 1324 memset(&gadget->dev, 0x00, sizeof(gadget->dev)); 1325 } 1326 EXPORT_SYMBOL_GPL(usb_del_gadget_udc); 1327 1328 /* ------------------------------------------------------------------------- */ 1329 1330 static int udc_bind_to_driver(struct usb_udc *udc, struct usb_gadget_driver *driver) 1331 { 1332 int ret; 1333 1334 dev_dbg(&udc->dev, "registering UDC driver [%s]\n", 1335 driver->function); 1336 1337 udc->driver = driver; 1338 udc->dev.driver = &driver->driver; 1339 udc->gadget->dev.driver = &driver->driver; 1340 1341 usb_gadget_udc_set_speed(udc, driver->max_speed); 1342 1343 ret = driver->bind(udc->gadget, driver); 1344 if (ret) 1345 goto err1; 1346 ret = usb_gadget_udc_start(udc); 1347 if (ret) { 1348 driver->unbind(udc->gadget); 1349 goto err1; 1350 } 1351 usb_udc_connect_control(udc); 1352 1353 kobject_uevent(&udc->dev.kobj, KOBJ_CHANGE); 1354 return 0; 1355 err1: 1356 if (ret != -EISNAM) 1357 dev_err(&udc->dev, "failed to start %s: %d\n", 1358 udc->driver->function, ret); 1359 udc->driver = NULL; 1360 udc->dev.driver = NULL; 1361 udc->gadget->dev.driver = NULL; 1362 return ret; 1363 } 1364 1365 int usb_gadget_probe_driver(struct usb_gadget_driver *driver) 1366 { 1367 struct usb_udc *udc = NULL; 1368 int ret = -ENODEV; 1369 1370 if (!driver || !driver->bind || !driver->setup) 1371 return -EINVAL; 1372 1373 mutex_lock(&udc_lock); 1374 if (driver->udc_name) { 1375 list_for_each_entry(udc, &udc_list, list) { 1376 ret = strcmp(driver->udc_name, dev_name(&udc->dev)); 1377 if (!ret) 1378 break; 1379 } 1380 if (ret) 1381 ret = -ENODEV; 1382 else if (udc->driver) 1383 ret = -EBUSY; 1384 else 1385 goto found; 1386 } else { 1387 list_for_each_entry(udc, &udc_list, list) { 1388 /* For now we take the first one */ 1389 if (!udc->driver) 1390 goto found; 1391 } 1392 } 1393 1394 if (!driver->match_existing_only) { 1395 list_add_tail(&driver->pending, &gadget_driver_pending_list); 1396 pr_info("udc-core: couldn't find an available UDC - added [%s] to list of pending drivers\n", 1397 driver->function); 1398 ret = 0; 1399 } 1400 1401 mutex_unlock(&udc_lock); 1402 return ret; 1403 found: 1404 ret = udc_bind_to_driver(udc, driver); 1405 mutex_unlock(&udc_lock); 1406 return ret; 1407 } 1408 EXPORT_SYMBOL_GPL(usb_gadget_probe_driver); 1409 1410 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver) 1411 { 1412 struct usb_udc *udc = NULL; 1413 int ret = -ENODEV; 1414 1415 if (!driver || !driver->unbind) 1416 return -EINVAL; 1417 1418 mutex_lock(&udc_lock); 1419 list_for_each_entry(udc, &udc_list, list) { 1420 if (udc->driver == driver) { 1421 usb_gadget_remove_driver(udc); 1422 usb_gadget_set_state(udc->gadget, 1423 USB_STATE_NOTATTACHED); 1424 1425 /* Maybe there is someone waiting for this UDC? */ 1426 check_pending_gadget_drivers(udc); 1427 /* 1428 * For now we ignore bind errors as probably it's 1429 * not a valid reason to fail other's gadget unbind 1430 */ 1431 ret = 0; 1432 break; 1433 } 1434 } 1435 1436 if (ret) { 1437 list_del(&driver->pending); 1438 ret = 0; 1439 } 1440 mutex_unlock(&udc_lock); 1441 return ret; 1442 } 1443 EXPORT_SYMBOL_GPL(usb_gadget_unregister_driver); 1444 1445 /* ------------------------------------------------------------------------- */ 1446 1447 static ssize_t srp_store(struct device *dev, 1448 struct device_attribute *attr, const char *buf, size_t n) 1449 { 1450 struct usb_udc *udc = container_of(dev, struct usb_udc, dev); 1451 1452 if (sysfs_streq(buf, "1")) 1453 usb_gadget_wakeup(udc->gadget); 1454 1455 return n; 1456 } 1457 static DEVICE_ATTR_WO(srp); 1458 1459 static ssize_t soft_connect_store(struct device *dev, 1460 struct device_attribute *attr, const char *buf, size_t n) 1461 { 1462 struct usb_udc *udc = container_of(dev, struct usb_udc, dev); 1463 1464 if (!udc->driver) { 1465 dev_err(dev, "soft-connect without a gadget driver\n"); 1466 return -EOPNOTSUPP; 1467 } 1468 1469 if (sysfs_streq(buf, "connect")) { 1470 usb_gadget_udc_start(udc); 1471 usb_gadget_connect(udc->gadget); 1472 } else if (sysfs_streq(buf, "disconnect")) { 1473 usb_gadget_disconnect(udc->gadget); 1474 udc->driver->disconnect(udc->gadget); 1475 usb_gadget_udc_stop(udc); 1476 } else { 1477 dev_err(dev, "unsupported command '%s'\n", buf); 1478 return -EINVAL; 1479 } 1480 1481 return n; 1482 } 1483 static DEVICE_ATTR_WO(soft_connect); 1484 1485 static ssize_t state_show(struct device *dev, struct device_attribute *attr, 1486 char *buf) 1487 { 1488 struct usb_udc *udc = container_of(dev, struct usb_udc, dev); 1489 struct usb_gadget *gadget = udc->gadget; 1490 1491 return sprintf(buf, "%s\n", usb_state_string(gadget->state)); 1492 } 1493 static DEVICE_ATTR_RO(state); 1494 1495 static ssize_t function_show(struct device *dev, struct device_attribute *attr, 1496 char *buf) 1497 { 1498 struct usb_udc *udc = container_of(dev, struct usb_udc, dev); 1499 struct usb_gadget_driver *drv = udc->driver; 1500 1501 if (!drv || !drv->function) 1502 return 0; 1503 return scnprintf(buf, PAGE_SIZE, "%s\n", drv->function); 1504 } 1505 static DEVICE_ATTR_RO(function); 1506 1507 #define USB_UDC_SPEED_ATTR(name, param) \ 1508 ssize_t name##_show(struct device *dev, \ 1509 struct device_attribute *attr, char *buf) \ 1510 { \ 1511 struct usb_udc *udc = container_of(dev, struct usb_udc, dev); \ 1512 return scnprintf(buf, PAGE_SIZE, "%s\n", \ 1513 usb_speed_string(udc->gadget->param)); \ 1514 } \ 1515 static DEVICE_ATTR_RO(name) 1516 1517 static USB_UDC_SPEED_ATTR(current_speed, speed); 1518 static USB_UDC_SPEED_ATTR(maximum_speed, max_speed); 1519 1520 #define USB_UDC_ATTR(name) \ 1521 ssize_t name##_show(struct device *dev, \ 1522 struct device_attribute *attr, char *buf) \ 1523 { \ 1524 struct usb_udc *udc = container_of(dev, struct usb_udc, dev); \ 1525 struct usb_gadget *gadget = udc->gadget; \ 1526 \ 1527 return scnprintf(buf, PAGE_SIZE, "%d\n", gadget->name); \ 1528 } \ 1529 static DEVICE_ATTR_RO(name) 1530 1531 static USB_UDC_ATTR(is_otg); 1532 static USB_UDC_ATTR(is_a_peripheral); 1533 static USB_UDC_ATTR(b_hnp_enable); 1534 static USB_UDC_ATTR(a_hnp_support); 1535 static USB_UDC_ATTR(a_alt_hnp_support); 1536 static USB_UDC_ATTR(is_selfpowered); 1537 1538 static struct attribute *usb_udc_attrs[] = { 1539 &dev_attr_srp.attr, 1540 &dev_attr_soft_connect.attr, 1541 &dev_attr_state.attr, 1542 &dev_attr_function.attr, 1543 &dev_attr_current_speed.attr, 1544 &dev_attr_maximum_speed.attr, 1545 1546 &dev_attr_is_otg.attr, 1547 &dev_attr_is_a_peripheral.attr, 1548 &dev_attr_b_hnp_enable.attr, 1549 &dev_attr_a_hnp_support.attr, 1550 &dev_attr_a_alt_hnp_support.attr, 1551 &dev_attr_is_selfpowered.attr, 1552 NULL, 1553 }; 1554 1555 static const struct attribute_group usb_udc_attr_group = { 1556 .attrs = usb_udc_attrs, 1557 }; 1558 1559 static const struct attribute_group *usb_udc_attr_groups[] = { 1560 &usb_udc_attr_group, 1561 NULL, 1562 }; 1563 1564 static int usb_udc_uevent(struct device *dev, struct kobj_uevent_env *env) 1565 { 1566 struct usb_udc *udc = container_of(dev, struct usb_udc, dev); 1567 int ret; 1568 1569 ret = add_uevent_var(env, "USB_UDC_NAME=%s", udc->gadget->name); 1570 if (ret) { 1571 dev_err(dev, "failed to add uevent USB_UDC_NAME\n"); 1572 return ret; 1573 } 1574 1575 if (udc->driver) { 1576 ret = add_uevent_var(env, "USB_UDC_DRIVER=%s", 1577 udc->driver->function); 1578 if (ret) { 1579 dev_err(dev, "failed to add uevent USB_UDC_DRIVER\n"); 1580 return ret; 1581 } 1582 } 1583 1584 return 0; 1585 } 1586 1587 static int __init usb_udc_init(void) 1588 { 1589 udc_class = class_create(THIS_MODULE, "udc"); 1590 if (IS_ERR(udc_class)) { 1591 pr_err("failed to create udc class --> %ld\n", 1592 PTR_ERR(udc_class)); 1593 return PTR_ERR(udc_class); 1594 } 1595 1596 udc_class->dev_uevent = usb_udc_uevent; 1597 return 0; 1598 } 1599 subsys_initcall(usb_udc_init); 1600 1601 static void __exit usb_udc_exit(void) 1602 { 1603 class_destroy(udc_class); 1604 } 1605 module_exit(usb_udc_exit); 1606 1607 MODULE_DESCRIPTION("UDC Framework"); 1608 MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>"); 1609 MODULE_LICENSE("GPL v2"); 1610