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