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