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