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 * Returns zero on success, else negative errno. 826 */ 827 int usb_gadget_deactivate(struct usb_gadget *gadget) 828 { 829 int ret = 0; 830 831 mutex_lock(&gadget->udc->connect_lock); 832 if (gadget->deactivated) 833 goto unlock; 834 835 if (gadget->connected) { 836 ret = usb_gadget_disconnect_locked(gadget); 837 if (ret) 838 goto unlock; 839 840 /* 841 * If gadget was being connected before deactivation, we want 842 * to reconnect it in usb_gadget_activate(). 843 */ 844 gadget->connected = true; 845 } 846 gadget->deactivated = true; 847 848 unlock: 849 mutex_unlock(&gadget->udc->connect_lock); 850 trace_usb_gadget_deactivate(gadget, ret); 851 852 return ret; 853 } 854 EXPORT_SYMBOL_GPL(usb_gadget_deactivate); 855 856 /** 857 * usb_gadget_activate - activate function which is not ready to work 858 * @gadget: the peripheral being activated 859 * 860 * This routine activates gadget which was previously deactivated with 861 * usb_gadget_deactivate() call. It calls usb_gadget_connect() if needed. 862 * 863 * Returns zero on success, else negative errno. 864 */ 865 int usb_gadget_activate(struct usb_gadget *gadget) 866 { 867 int ret = 0; 868 869 mutex_lock(&gadget->udc->connect_lock); 870 if (!gadget->deactivated) 871 goto unlock; 872 873 gadget->deactivated = false; 874 875 /* 876 * If gadget has been connected before deactivation, or became connected 877 * while it was being deactivated, we call usb_gadget_connect(). 878 */ 879 if (gadget->connected) 880 ret = usb_gadget_connect_locked(gadget); 881 882 unlock: 883 mutex_unlock(&gadget->udc->connect_lock); 884 trace_usb_gadget_activate(gadget, ret); 885 886 return ret; 887 } 888 EXPORT_SYMBOL_GPL(usb_gadget_activate); 889 890 /* ------------------------------------------------------------------------- */ 891 892 #ifdef CONFIG_HAS_DMA 893 894 int usb_gadget_map_request_by_dev(struct device *dev, 895 struct usb_request *req, int is_in) 896 { 897 if (req->length == 0) 898 return 0; 899 900 if (req->num_sgs) { 901 int mapped; 902 903 mapped = dma_map_sg(dev, req->sg, req->num_sgs, 904 is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE); 905 if (mapped == 0) { 906 dev_err(dev, "failed to map SGs\n"); 907 return -EFAULT; 908 } 909 910 req->num_mapped_sgs = mapped; 911 } else { 912 if (is_vmalloc_addr(req->buf)) { 913 dev_err(dev, "buffer is not dma capable\n"); 914 return -EFAULT; 915 } else if (object_is_on_stack(req->buf)) { 916 dev_err(dev, "buffer is on stack\n"); 917 return -EFAULT; 918 } 919 920 req->dma = dma_map_single(dev, req->buf, req->length, 921 is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE); 922 923 if (dma_mapping_error(dev, req->dma)) { 924 dev_err(dev, "failed to map buffer\n"); 925 return -EFAULT; 926 } 927 928 req->dma_mapped = 1; 929 } 930 931 return 0; 932 } 933 EXPORT_SYMBOL_GPL(usb_gadget_map_request_by_dev); 934 935 int usb_gadget_map_request(struct usb_gadget *gadget, 936 struct usb_request *req, int is_in) 937 { 938 return usb_gadget_map_request_by_dev(gadget->dev.parent, req, is_in); 939 } 940 EXPORT_SYMBOL_GPL(usb_gadget_map_request); 941 942 void usb_gadget_unmap_request_by_dev(struct device *dev, 943 struct usb_request *req, int is_in) 944 { 945 if (req->length == 0) 946 return; 947 948 if (req->num_mapped_sgs) { 949 dma_unmap_sg(dev, req->sg, req->num_sgs, 950 is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE); 951 952 req->num_mapped_sgs = 0; 953 } else if (req->dma_mapped) { 954 dma_unmap_single(dev, req->dma, req->length, 955 is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE); 956 req->dma_mapped = 0; 957 } 958 } 959 EXPORT_SYMBOL_GPL(usb_gadget_unmap_request_by_dev); 960 961 void usb_gadget_unmap_request(struct usb_gadget *gadget, 962 struct usb_request *req, int is_in) 963 { 964 usb_gadget_unmap_request_by_dev(gadget->dev.parent, req, is_in); 965 } 966 EXPORT_SYMBOL_GPL(usb_gadget_unmap_request); 967 968 #endif /* CONFIG_HAS_DMA */ 969 970 /* ------------------------------------------------------------------------- */ 971 972 /** 973 * usb_gadget_giveback_request - give the request back to the gadget layer 974 * @ep: the endpoint to be used with with the request 975 * @req: the request being given back 976 * 977 * This is called by device controller drivers in order to return the 978 * completed request back to the gadget layer. 979 */ 980 void usb_gadget_giveback_request(struct usb_ep *ep, 981 struct usb_request *req) 982 { 983 if (likely(req->status == 0)) 984 usb_led_activity(USB_LED_EVENT_GADGET); 985 986 trace_usb_gadget_giveback_request(ep, req, 0); 987 988 req->complete(ep, req); 989 } 990 EXPORT_SYMBOL_GPL(usb_gadget_giveback_request); 991 992 /* ------------------------------------------------------------------------- */ 993 994 /** 995 * gadget_find_ep_by_name - returns ep whose name is the same as sting passed 996 * in second parameter or NULL if searched endpoint not found 997 * @g: controller to check for quirk 998 * @name: name of searched endpoint 999 */ 1000 struct usb_ep *gadget_find_ep_by_name(struct usb_gadget *g, const char *name) 1001 { 1002 struct usb_ep *ep; 1003 1004 gadget_for_each_ep(ep, g) { 1005 if (!strcmp(ep->name, name)) 1006 return ep; 1007 } 1008 1009 return NULL; 1010 } 1011 EXPORT_SYMBOL_GPL(gadget_find_ep_by_name); 1012 1013 /* ------------------------------------------------------------------------- */ 1014 1015 int usb_gadget_ep_match_desc(struct usb_gadget *gadget, 1016 struct usb_ep *ep, struct usb_endpoint_descriptor *desc, 1017 struct usb_ss_ep_comp_descriptor *ep_comp) 1018 { 1019 u8 type; 1020 u16 max; 1021 int num_req_streams = 0; 1022 1023 /* endpoint already claimed? */ 1024 if (ep->claimed) 1025 return 0; 1026 1027 type = usb_endpoint_type(desc); 1028 max = usb_endpoint_maxp(desc); 1029 1030 if (usb_endpoint_dir_in(desc) && !ep->caps.dir_in) 1031 return 0; 1032 if (usb_endpoint_dir_out(desc) && !ep->caps.dir_out) 1033 return 0; 1034 1035 if (max > ep->maxpacket_limit) 1036 return 0; 1037 1038 /* "high bandwidth" works only at high speed */ 1039 if (!gadget_is_dualspeed(gadget) && usb_endpoint_maxp_mult(desc) > 1) 1040 return 0; 1041 1042 switch (type) { 1043 case USB_ENDPOINT_XFER_CONTROL: 1044 /* only support ep0 for portable CONTROL traffic */ 1045 return 0; 1046 case USB_ENDPOINT_XFER_ISOC: 1047 if (!ep->caps.type_iso) 1048 return 0; 1049 /* ISO: limit 1023 bytes full speed, 1024 high/super speed */ 1050 if (!gadget_is_dualspeed(gadget) && max > 1023) 1051 return 0; 1052 break; 1053 case USB_ENDPOINT_XFER_BULK: 1054 if (!ep->caps.type_bulk) 1055 return 0; 1056 if (ep_comp && gadget_is_superspeed(gadget)) { 1057 /* Get the number of required streams from the 1058 * EP companion descriptor and see if the EP 1059 * matches it 1060 */ 1061 num_req_streams = ep_comp->bmAttributes & 0x1f; 1062 if (num_req_streams > ep->max_streams) 1063 return 0; 1064 } 1065 break; 1066 case USB_ENDPOINT_XFER_INT: 1067 /* Bulk endpoints handle interrupt transfers, 1068 * except the toggle-quirky iso-synch kind 1069 */ 1070 if (!ep->caps.type_int && !ep->caps.type_bulk) 1071 return 0; 1072 /* INT: limit 64 bytes full speed, 1024 high/super speed */ 1073 if (!gadget_is_dualspeed(gadget) && max > 64) 1074 return 0; 1075 break; 1076 } 1077 1078 return 1; 1079 } 1080 EXPORT_SYMBOL_GPL(usb_gadget_ep_match_desc); 1081 1082 /** 1083 * usb_gadget_check_config - checks if the UDC can support the binded 1084 * configuration 1085 * @gadget: controller to check the USB configuration 1086 * 1087 * Ensure that a UDC is able to support the requested resources by a 1088 * configuration, and that there are no resource limitations, such as 1089 * internal memory allocated to all requested endpoints. 1090 * 1091 * Returns zero on success, else a negative errno. 1092 */ 1093 int usb_gadget_check_config(struct usb_gadget *gadget) 1094 { 1095 if (gadget->ops->check_config) 1096 return gadget->ops->check_config(gadget); 1097 return 0; 1098 } 1099 EXPORT_SYMBOL_GPL(usb_gadget_check_config); 1100 1101 /* ------------------------------------------------------------------------- */ 1102 1103 static void usb_gadget_state_work(struct work_struct *work) 1104 { 1105 struct usb_gadget *gadget = work_to_gadget(work); 1106 struct usb_udc *udc = gadget->udc; 1107 1108 if (udc) 1109 sysfs_notify(&udc->dev.kobj, NULL, "state"); 1110 } 1111 1112 void usb_gadget_set_state(struct usb_gadget *gadget, 1113 enum usb_device_state state) 1114 { 1115 gadget->state = state; 1116 schedule_work(&gadget->work); 1117 } 1118 EXPORT_SYMBOL_GPL(usb_gadget_set_state); 1119 1120 /* ------------------------------------------------------------------------- */ 1121 1122 /* Acquire connect_lock before calling this function. */ 1123 static void usb_udc_connect_control_locked(struct usb_udc *udc) __must_hold(&udc->connect_lock) 1124 { 1125 if (udc->vbus) 1126 usb_gadget_connect_locked(udc->gadget); 1127 else 1128 usb_gadget_disconnect_locked(udc->gadget); 1129 } 1130 1131 static void vbus_event_work(struct work_struct *work) 1132 { 1133 struct usb_udc *udc = container_of(work, struct usb_udc, vbus_work); 1134 1135 mutex_lock(&udc->connect_lock); 1136 usb_udc_connect_control_locked(udc); 1137 mutex_unlock(&udc->connect_lock); 1138 } 1139 1140 /** 1141 * usb_udc_vbus_handler - updates the udc core vbus status, and try to 1142 * connect or disconnect gadget 1143 * @gadget: The gadget which vbus change occurs 1144 * @status: The vbus status 1145 * 1146 * The udc driver calls it when it wants to connect or disconnect gadget 1147 * according to vbus status. 1148 * 1149 * This function can be invoked from interrupt context by irq handlers of 1150 * the gadget drivers, however, usb_udc_connect_control() has to run in 1151 * non-atomic context due to the following: 1152 * a. Some of the gadget driver implementations expect the ->pullup 1153 * callback to be invoked in non-atomic context. 1154 * b. usb_gadget_disconnect() acquires udc_lock which is a mutex. 1155 * Hence offload invocation of usb_udc_connect_control() to workqueue. 1156 */ 1157 void usb_udc_vbus_handler(struct usb_gadget *gadget, bool status) 1158 { 1159 struct usb_udc *udc = gadget->udc; 1160 1161 if (udc) { 1162 udc->vbus = status; 1163 schedule_work(&udc->vbus_work); 1164 } 1165 } 1166 EXPORT_SYMBOL_GPL(usb_udc_vbus_handler); 1167 1168 /** 1169 * usb_gadget_udc_reset - notifies the udc core that bus reset occurs 1170 * @gadget: The gadget which bus reset occurs 1171 * @driver: The gadget driver we want to notify 1172 * 1173 * If the udc driver has bus reset handler, it needs to call this when the bus 1174 * reset occurs, it notifies the gadget driver that the bus reset occurs as 1175 * well as updates gadget state. 1176 */ 1177 void usb_gadget_udc_reset(struct usb_gadget *gadget, 1178 struct usb_gadget_driver *driver) 1179 { 1180 driver->reset(gadget); 1181 usb_gadget_set_state(gadget, USB_STATE_DEFAULT); 1182 } 1183 EXPORT_SYMBOL_GPL(usb_gadget_udc_reset); 1184 1185 /** 1186 * usb_gadget_udc_start_locked - tells usb device controller to start up 1187 * @udc: The UDC to be started 1188 * 1189 * This call is issued by the UDC Class driver when it's about 1190 * to register a gadget driver to the device controller, before 1191 * calling gadget driver's bind() method. 1192 * 1193 * It allows the controller to be powered off until strictly 1194 * necessary to have it powered on. 1195 * 1196 * Returns zero on success, else negative errno. 1197 * 1198 * Caller should acquire connect_lock before invoking this function. 1199 */ 1200 static inline int usb_gadget_udc_start_locked(struct usb_udc *udc) 1201 __must_hold(&udc->connect_lock) 1202 { 1203 int ret; 1204 1205 if (udc->started) { 1206 dev_err(&udc->dev, "UDC had already started\n"); 1207 return -EBUSY; 1208 } 1209 1210 ret = udc->gadget->ops->udc_start(udc->gadget, udc->driver); 1211 if (!ret) 1212 udc->started = true; 1213 1214 return ret; 1215 } 1216 1217 /** 1218 * usb_gadget_udc_stop_locked - tells usb device controller we don't need it anymore 1219 * @udc: The UDC to be stopped 1220 * 1221 * This call is issued by the UDC Class driver after calling 1222 * gadget driver's unbind() method. 1223 * 1224 * The details are implementation specific, but it can go as 1225 * far as powering off UDC completely and disable its data 1226 * line pullups. 1227 * 1228 * Caller should acquire connect lock before invoking this function. 1229 */ 1230 static inline void usb_gadget_udc_stop_locked(struct usb_udc *udc) 1231 __must_hold(&udc->connect_lock) 1232 { 1233 if (!udc->started) { 1234 dev_err(&udc->dev, "UDC had already stopped\n"); 1235 return; 1236 } 1237 1238 udc->gadget->ops->udc_stop(udc->gadget); 1239 udc->started = false; 1240 } 1241 1242 /** 1243 * usb_gadget_udc_set_speed - tells usb device controller speed supported by 1244 * current driver 1245 * @udc: The device we want to set maximum speed 1246 * @speed: The maximum speed to allowed to run 1247 * 1248 * This call is issued by the UDC Class driver before calling 1249 * usb_gadget_udc_start() in order to make sure that we don't try to 1250 * connect on speeds the gadget driver doesn't support. 1251 */ 1252 static inline void usb_gadget_udc_set_speed(struct usb_udc *udc, 1253 enum usb_device_speed speed) 1254 { 1255 struct usb_gadget *gadget = udc->gadget; 1256 enum usb_device_speed s; 1257 1258 if (speed == USB_SPEED_UNKNOWN) 1259 s = gadget->max_speed; 1260 else 1261 s = min(speed, gadget->max_speed); 1262 1263 if (s == USB_SPEED_SUPER_PLUS && gadget->ops->udc_set_ssp_rate) 1264 gadget->ops->udc_set_ssp_rate(gadget, gadget->max_ssp_rate); 1265 else if (gadget->ops->udc_set_speed) 1266 gadget->ops->udc_set_speed(gadget, s); 1267 } 1268 1269 /** 1270 * usb_gadget_enable_async_callbacks - tell usb device controller to enable asynchronous callbacks 1271 * @udc: The UDC which should enable async callbacks 1272 * 1273 * This routine is used when binding gadget drivers. It undoes the effect 1274 * of usb_gadget_disable_async_callbacks(); the UDC driver should enable IRQs 1275 * (if necessary) and resume issuing callbacks. 1276 * 1277 * This routine will always be called in process context. 1278 */ 1279 static inline void usb_gadget_enable_async_callbacks(struct usb_udc *udc) 1280 { 1281 struct usb_gadget *gadget = udc->gadget; 1282 1283 if (gadget->ops->udc_async_callbacks) 1284 gadget->ops->udc_async_callbacks(gadget, true); 1285 } 1286 1287 /** 1288 * usb_gadget_disable_async_callbacks - tell usb device controller to disable asynchronous callbacks 1289 * @udc: The UDC which should disable async callbacks 1290 * 1291 * This routine is used when unbinding gadget drivers. It prevents a race: 1292 * The UDC driver doesn't know when the gadget driver's ->unbind callback 1293 * runs, so unless it is told to disable asynchronous callbacks, it might 1294 * issue a callback (such as ->disconnect) after the unbind has completed. 1295 * 1296 * After this function runs, the UDC driver must suppress all ->suspend, 1297 * ->resume, ->disconnect, ->reset, and ->setup callbacks to the gadget driver 1298 * until async callbacks are again enabled. A simple-minded but effective 1299 * way to accomplish this is to tell the UDC hardware not to generate any 1300 * more IRQs. 1301 * 1302 * Request completion callbacks must still be issued. However, it's okay 1303 * to defer them until the request is cancelled, since the pull-up will be 1304 * turned off during the time period when async callbacks are disabled. 1305 * 1306 * This routine will always be called in process context. 1307 */ 1308 static inline void usb_gadget_disable_async_callbacks(struct usb_udc *udc) 1309 { 1310 struct usb_gadget *gadget = udc->gadget; 1311 1312 if (gadget->ops->udc_async_callbacks) 1313 gadget->ops->udc_async_callbacks(gadget, false); 1314 } 1315 1316 /** 1317 * usb_udc_release - release the usb_udc struct 1318 * @dev: the dev member within usb_udc 1319 * 1320 * This is called by driver's core in order to free memory once the last 1321 * reference is released. 1322 */ 1323 static void usb_udc_release(struct device *dev) 1324 { 1325 struct usb_udc *udc; 1326 1327 udc = container_of(dev, struct usb_udc, dev); 1328 dev_dbg(dev, "releasing '%s'\n", dev_name(dev)); 1329 kfree(udc); 1330 } 1331 1332 static const struct attribute_group *usb_udc_attr_groups[]; 1333 1334 static void usb_udc_nop_release(struct device *dev) 1335 { 1336 dev_vdbg(dev, "%s\n", __func__); 1337 } 1338 1339 /** 1340 * usb_initialize_gadget - initialize a gadget and its embedded struct device 1341 * @parent: the parent device to this udc. Usually the controller driver's 1342 * device. 1343 * @gadget: the gadget to be initialized. 1344 * @release: a gadget release function. 1345 */ 1346 void usb_initialize_gadget(struct device *parent, struct usb_gadget *gadget, 1347 void (*release)(struct device *dev)) 1348 { 1349 INIT_WORK(&gadget->work, usb_gadget_state_work); 1350 gadget->dev.parent = parent; 1351 1352 if (release) 1353 gadget->dev.release = release; 1354 else 1355 gadget->dev.release = usb_udc_nop_release; 1356 1357 device_initialize(&gadget->dev); 1358 gadget->dev.bus = &gadget_bus_type; 1359 } 1360 EXPORT_SYMBOL_GPL(usb_initialize_gadget); 1361 1362 /** 1363 * usb_add_gadget - adds a new gadget to the udc class driver list 1364 * @gadget: the gadget to be added to the list. 1365 * 1366 * Returns zero on success, negative errno otherwise. 1367 * Does not do a final usb_put_gadget() if an error occurs. 1368 */ 1369 int usb_add_gadget(struct usb_gadget *gadget) 1370 { 1371 struct usb_udc *udc; 1372 int ret = -ENOMEM; 1373 1374 udc = kzalloc(sizeof(*udc), GFP_KERNEL); 1375 if (!udc) 1376 goto error; 1377 1378 device_initialize(&udc->dev); 1379 udc->dev.release = usb_udc_release; 1380 udc->dev.class = &udc_class; 1381 udc->dev.groups = usb_udc_attr_groups; 1382 udc->dev.parent = gadget->dev.parent; 1383 ret = dev_set_name(&udc->dev, "%s", 1384 kobject_name(&gadget->dev.parent->kobj)); 1385 if (ret) 1386 goto err_put_udc; 1387 1388 udc->gadget = gadget; 1389 gadget->udc = udc; 1390 mutex_init(&udc->connect_lock); 1391 1392 udc->started = false; 1393 1394 mutex_lock(&udc_lock); 1395 list_add_tail(&udc->list, &udc_list); 1396 mutex_unlock(&udc_lock); 1397 INIT_WORK(&udc->vbus_work, vbus_event_work); 1398 1399 ret = device_add(&udc->dev); 1400 if (ret) 1401 goto err_unlist_udc; 1402 1403 usb_gadget_set_state(gadget, USB_STATE_NOTATTACHED); 1404 udc->vbus = true; 1405 1406 ret = ida_alloc(&gadget_id_numbers, GFP_KERNEL); 1407 if (ret < 0) 1408 goto err_del_udc; 1409 gadget->id_number = ret; 1410 dev_set_name(&gadget->dev, "gadget.%d", ret); 1411 1412 ret = device_add(&gadget->dev); 1413 if (ret) 1414 goto err_free_id; 1415 1416 return 0; 1417 1418 err_free_id: 1419 ida_free(&gadget_id_numbers, gadget->id_number); 1420 1421 err_del_udc: 1422 flush_work(&gadget->work); 1423 device_del(&udc->dev); 1424 1425 err_unlist_udc: 1426 mutex_lock(&udc_lock); 1427 list_del(&udc->list); 1428 mutex_unlock(&udc_lock); 1429 1430 err_put_udc: 1431 put_device(&udc->dev); 1432 1433 error: 1434 return ret; 1435 } 1436 EXPORT_SYMBOL_GPL(usb_add_gadget); 1437 1438 /** 1439 * usb_add_gadget_udc_release - adds a new gadget to the udc class driver list 1440 * @parent: the parent device to this udc. Usually the controller driver's 1441 * device. 1442 * @gadget: the gadget to be added to the list. 1443 * @release: a gadget release function. 1444 * 1445 * Returns zero on success, negative errno otherwise. 1446 * Calls the gadget release function in the latter case. 1447 */ 1448 int usb_add_gadget_udc_release(struct device *parent, struct usb_gadget *gadget, 1449 void (*release)(struct device *dev)) 1450 { 1451 int ret; 1452 1453 usb_initialize_gadget(parent, gadget, release); 1454 ret = usb_add_gadget(gadget); 1455 if (ret) 1456 usb_put_gadget(gadget); 1457 return ret; 1458 } 1459 EXPORT_SYMBOL_GPL(usb_add_gadget_udc_release); 1460 1461 /** 1462 * usb_get_gadget_udc_name - get the name of the first UDC controller 1463 * This functions returns the name of the first UDC controller in the system. 1464 * Please note that this interface is usefull only for legacy drivers which 1465 * assume that there is only one UDC controller in the system and they need to 1466 * get its name before initialization. There is no guarantee that the UDC 1467 * of the returned name will be still available, when gadget driver registers 1468 * itself. 1469 * 1470 * Returns pointer to string with UDC controller name on success, NULL 1471 * otherwise. Caller should kfree() returned string. 1472 */ 1473 char *usb_get_gadget_udc_name(void) 1474 { 1475 struct usb_udc *udc; 1476 char *name = NULL; 1477 1478 /* For now we take the first available UDC */ 1479 mutex_lock(&udc_lock); 1480 list_for_each_entry(udc, &udc_list, list) { 1481 if (!udc->driver) { 1482 name = kstrdup(udc->gadget->name, GFP_KERNEL); 1483 break; 1484 } 1485 } 1486 mutex_unlock(&udc_lock); 1487 return name; 1488 } 1489 EXPORT_SYMBOL_GPL(usb_get_gadget_udc_name); 1490 1491 /** 1492 * usb_add_gadget_udc - adds a new gadget to the udc class driver list 1493 * @parent: the parent device to this udc. Usually the controller 1494 * driver's device. 1495 * @gadget: the gadget to be added to the list 1496 * 1497 * Returns zero on success, negative errno otherwise. 1498 */ 1499 int usb_add_gadget_udc(struct device *parent, struct usb_gadget *gadget) 1500 { 1501 return usb_add_gadget_udc_release(parent, gadget, NULL); 1502 } 1503 EXPORT_SYMBOL_GPL(usb_add_gadget_udc); 1504 1505 /** 1506 * usb_del_gadget - deletes a gadget and unregisters its udc 1507 * @gadget: the gadget to be deleted. 1508 * 1509 * This will unbind @gadget, if it is bound. 1510 * It will not do a final usb_put_gadget(). 1511 */ 1512 void usb_del_gadget(struct usb_gadget *gadget) 1513 { 1514 struct usb_udc *udc = gadget->udc; 1515 1516 if (!udc) 1517 return; 1518 1519 dev_vdbg(gadget->dev.parent, "unregistering gadget\n"); 1520 1521 mutex_lock(&udc_lock); 1522 list_del(&udc->list); 1523 mutex_unlock(&udc_lock); 1524 1525 kobject_uevent(&udc->dev.kobj, KOBJ_REMOVE); 1526 flush_work(&gadget->work); 1527 device_del(&gadget->dev); 1528 ida_free(&gadget_id_numbers, gadget->id_number); 1529 cancel_work_sync(&udc->vbus_work); 1530 device_unregister(&udc->dev); 1531 } 1532 EXPORT_SYMBOL_GPL(usb_del_gadget); 1533 1534 /** 1535 * usb_del_gadget_udc - unregisters a gadget 1536 * @gadget: the gadget to be unregistered. 1537 * 1538 * Calls usb_del_gadget() and does a final usb_put_gadget(). 1539 */ 1540 void usb_del_gadget_udc(struct usb_gadget *gadget) 1541 { 1542 usb_del_gadget(gadget); 1543 usb_put_gadget(gadget); 1544 } 1545 EXPORT_SYMBOL_GPL(usb_del_gadget_udc); 1546 1547 /* ------------------------------------------------------------------------- */ 1548 1549 static int gadget_match_driver(struct device *dev, struct device_driver *drv) 1550 { 1551 struct usb_gadget *gadget = dev_to_usb_gadget(dev); 1552 struct usb_udc *udc = gadget->udc; 1553 struct usb_gadget_driver *driver = container_of(drv, 1554 struct usb_gadget_driver, driver); 1555 1556 /* If the driver specifies a udc_name, it must match the UDC's name */ 1557 if (driver->udc_name && 1558 strcmp(driver->udc_name, dev_name(&udc->dev)) != 0) 1559 return 0; 1560 1561 /* If the driver is already bound to a gadget, it doesn't match */ 1562 if (driver->is_bound) 1563 return 0; 1564 1565 /* Otherwise any gadget driver matches any UDC */ 1566 return 1; 1567 } 1568 1569 static int gadget_bind_driver(struct device *dev) 1570 { 1571 struct usb_gadget *gadget = dev_to_usb_gadget(dev); 1572 struct usb_udc *udc = gadget->udc; 1573 struct usb_gadget_driver *driver = container_of(dev->driver, 1574 struct usb_gadget_driver, driver); 1575 int ret = 0; 1576 1577 mutex_lock(&udc_lock); 1578 if (driver->is_bound) { 1579 mutex_unlock(&udc_lock); 1580 return -ENXIO; /* Driver binds to only one gadget */ 1581 } 1582 driver->is_bound = true; 1583 udc->driver = driver; 1584 mutex_unlock(&udc_lock); 1585 1586 dev_dbg(&udc->dev, "binding gadget driver [%s]\n", driver->function); 1587 1588 usb_gadget_udc_set_speed(udc, driver->max_speed); 1589 1590 ret = driver->bind(udc->gadget, driver); 1591 if (ret) 1592 goto err_bind; 1593 1594 mutex_lock(&udc->connect_lock); 1595 ret = usb_gadget_udc_start_locked(udc); 1596 if (ret) { 1597 mutex_unlock(&udc->connect_lock); 1598 goto err_start; 1599 } 1600 usb_gadget_enable_async_callbacks(udc); 1601 udc->allow_connect = true; 1602 usb_udc_connect_control_locked(udc); 1603 mutex_unlock(&udc->connect_lock); 1604 1605 kobject_uevent(&udc->dev.kobj, KOBJ_CHANGE); 1606 return 0; 1607 1608 err_start: 1609 driver->unbind(udc->gadget); 1610 1611 err_bind: 1612 if (ret != -EISNAM) 1613 dev_err(&udc->dev, "failed to start %s: %d\n", 1614 driver->function, ret); 1615 1616 mutex_lock(&udc_lock); 1617 udc->driver = NULL; 1618 driver->is_bound = false; 1619 mutex_unlock(&udc_lock); 1620 1621 return ret; 1622 } 1623 1624 static void gadget_unbind_driver(struct device *dev) 1625 { 1626 struct usb_gadget *gadget = dev_to_usb_gadget(dev); 1627 struct usb_udc *udc = gadget->udc; 1628 struct usb_gadget_driver *driver = udc->driver; 1629 1630 dev_dbg(&udc->dev, "unbinding gadget driver [%s]\n", driver->function); 1631 1632 kobject_uevent(&udc->dev.kobj, KOBJ_CHANGE); 1633 1634 udc->allow_connect = false; 1635 cancel_work_sync(&udc->vbus_work); 1636 mutex_lock(&udc->connect_lock); 1637 usb_gadget_disconnect_locked(gadget); 1638 usb_gadget_disable_async_callbacks(udc); 1639 if (gadget->irq) 1640 synchronize_irq(gadget->irq); 1641 udc->driver->unbind(gadget); 1642 usb_gadget_udc_stop_locked(udc); 1643 mutex_unlock(&udc->connect_lock); 1644 1645 mutex_lock(&udc_lock); 1646 driver->is_bound = false; 1647 udc->driver = NULL; 1648 mutex_unlock(&udc_lock); 1649 } 1650 1651 /* ------------------------------------------------------------------------- */ 1652 1653 int usb_gadget_register_driver_owner(struct usb_gadget_driver *driver, 1654 struct module *owner, const char *mod_name) 1655 { 1656 int ret; 1657 1658 if (!driver || !driver->bind || !driver->setup) 1659 return -EINVAL; 1660 1661 driver->driver.bus = &gadget_bus_type; 1662 driver->driver.owner = owner; 1663 driver->driver.mod_name = mod_name; 1664 ret = driver_register(&driver->driver); 1665 if (ret) { 1666 pr_warn("%s: driver registration failed: %d\n", 1667 driver->function, ret); 1668 return ret; 1669 } 1670 1671 mutex_lock(&udc_lock); 1672 if (!driver->is_bound) { 1673 if (driver->match_existing_only) { 1674 pr_warn("%s: couldn't find an available UDC or it's busy\n", 1675 driver->function); 1676 ret = -EBUSY; 1677 } else { 1678 pr_info("%s: couldn't find an available UDC\n", 1679 driver->function); 1680 ret = 0; 1681 } 1682 } 1683 mutex_unlock(&udc_lock); 1684 1685 if (ret) 1686 driver_unregister(&driver->driver); 1687 return ret; 1688 } 1689 EXPORT_SYMBOL_GPL(usb_gadget_register_driver_owner); 1690 1691 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver) 1692 { 1693 if (!driver || !driver->unbind) 1694 return -EINVAL; 1695 1696 driver_unregister(&driver->driver); 1697 return 0; 1698 } 1699 EXPORT_SYMBOL_GPL(usb_gadget_unregister_driver); 1700 1701 /* ------------------------------------------------------------------------- */ 1702 1703 static ssize_t srp_store(struct device *dev, 1704 struct device_attribute *attr, const char *buf, size_t n) 1705 { 1706 struct usb_udc *udc = container_of(dev, struct usb_udc, dev); 1707 1708 if (sysfs_streq(buf, "1")) 1709 usb_gadget_wakeup(udc->gadget); 1710 1711 return n; 1712 } 1713 static DEVICE_ATTR_WO(srp); 1714 1715 static ssize_t soft_connect_store(struct device *dev, 1716 struct device_attribute *attr, const char *buf, size_t n) 1717 { 1718 struct usb_udc *udc = container_of(dev, struct usb_udc, dev); 1719 ssize_t ret; 1720 1721 device_lock(&udc->gadget->dev); 1722 if (!udc->driver) { 1723 dev_err(dev, "soft-connect without a gadget driver\n"); 1724 ret = -EOPNOTSUPP; 1725 goto out; 1726 } 1727 1728 if (sysfs_streq(buf, "connect")) { 1729 mutex_lock(&udc->connect_lock); 1730 usb_gadget_udc_start_locked(udc); 1731 usb_gadget_connect_locked(udc->gadget); 1732 mutex_unlock(&udc->connect_lock); 1733 } else if (sysfs_streq(buf, "disconnect")) { 1734 mutex_lock(&udc->connect_lock); 1735 usb_gadget_disconnect_locked(udc->gadget); 1736 usb_gadget_udc_stop_locked(udc); 1737 mutex_unlock(&udc->connect_lock); 1738 } else { 1739 dev_err(dev, "unsupported command '%s'\n", buf); 1740 ret = -EINVAL; 1741 goto out; 1742 } 1743 1744 ret = n; 1745 out: 1746 device_unlock(&udc->gadget->dev); 1747 return ret; 1748 } 1749 static DEVICE_ATTR_WO(soft_connect); 1750 1751 static ssize_t state_show(struct device *dev, struct device_attribute *attr, 1752 char *buf) 1753 { 1754 struct usb_udc *udc = container_of(dev, struct usb_udc, dev); 1755 struct usb_gadget *gadget = udc->gadget; 1756 1757 return sprintf(buf, "%s\n", usb_state_string(gadget->state)); 1758 } 1759 static DEVICE_ATTR_RO(state); 1760 1761 static ssize_t function_show(struct device *dev, struct device_attribute *attr, 1762 char *buf) 1763 { 1764 struct usb_udc *udc = container_of(dev, struct usb_udc, dev); 1765 struct usb_gadget_driver *drv; 1766 int rc = 0; 1767 1768 mutex_lock(&udc_lock); 1769 drv = udc->driver; 1770 if (drv && drv->function) 1771 rc = scnprintf(buf, PAGE_SIZE, "%s\n", drv->function); 1772 mutex_unlock(&udc_lock); 1773 return rc; 1774 } 1775 static DEVICE_ATTR_RO(function); 1776 1777 #define USB_UDC_SPEED_ATTR(name, param) \ 1778 ssize_t name##_show(struct device *dev, \ 1779 struct device_attribute *attr, char *buf) \ 1780 { \ 1781 struct usb_udc *udc = container_of(dev, struct usb_udc, dev); \ 1782 return scnprintf(buf, PAGE_SIZE, "%s\n", \ 1783 usb_speed_string(udc->gadget->param)); \ 1784 } \ 1785 static DEVICE_ATTR_RO(name) 1786 1787 static USB_UDC_SPEED_ATTR(current_speed, speed); 1788 static USB_UDC_SPEED_ATTR(maximum_speed, max_speed); 1789 1790 #define USB_UDC_ATTR(name) \ 1791 ssize_t name##_show(struct device *dev, \ 1792 struct device_attribute *attr, char *buf) \ 1793 { \ 1794 struct usb_udc *udc = container_of(dev, struct usb_udc, dev); \ 1795 struct usb_gadget *gadget = udc->gadget; \ 1796 \ 1797 return scnprintf(buf, PAGE_SIZE, "%d\n", gadget->name); \ 1798 } \ 1799 static DEVICE_ATTR_RO(name) 1800 1801 static USB_UDC_ATTR(is_otg); 1802 static USB_UDC_ATTR(is_a_peripheral); 1803 static USB_UDC_ATTR(b_hnp_enable); 1804 static USB_UDC_ATTR(a_hnp_support); 1805 static USB_UDC_ATTR(a_alt_hnp_support); 1806 static USB_UDC_ATTR(is_selfpowered); 1807 1808 static struct attribute *usb_udc_attrs[] = { 1809 &dev_attr_srp.attr, 1810 &dev_attr_soft_connect.attr, 1811 &dev_attr_state.attr, 1812 &dev_attr_function.attr, 1813 &dev_attr_current_speed.attr, 1814 &dev_attr_maximum_speed.attr, 1815 1816 &dev_attr_is_otg.attr, 1817 &dev_attr_is_a_peripheral.attr, 1818 &dev_attr_b_hnp_enable.attr, 1819 &dev_attr_a_hnp_support.attr, 1820 &dev_attr_a_alt_hnp_support.attr, 1821 &dev_attr_is_selfpowered.attr, 1822 NULL, 1823 }; 1824 1825 static const struct attribute_group usb_udc_attr_group = { 1826 .attrs = usb_udc_attrs, 1827 }; 1828 1829 static const struct attribute_group *usb_udc_attr_groups[] = { 1830 &usb_udc_attr_group, 1831 NULL, 1832 }; 1833 1834 static int usb_udc_uevent(const struct device *dev, struct kobj_uevent_env *env) 1835 { 1836 const struct usb_udc *udc = container_of(dev, struct usb_udc, dev); 1837 int ret; 1838 1839 ret = add_uevent_var(env, "USB_UDC_NAME=%s", udc->gadget->name); 1840 if (ret) { 1841 dev_err(dev, "failed to add uevent USB_UDC_NAME\n"); 1842 return ret; 1843 } 1844 1845 mutex_lock(&udc_lock); 1846 if (udc->driver) 1847 ret = add_uevent_var(env, "USB_UDC_DRIVER=%s", 1848 udc->driver->function); 1849 mutex_unlock(&udc_lock); 1850 if (ret) { 1851 dev_err(dev, "failed to add uevent USB_UDC_DRIVER\n"); 1852 return ret; 1853 } 1854 1855 return 0; 1856 } 1857 1858 static const struct class udc_class = { 1859 .name = "udc", 1860 .dev_uevent = usb_udc_uevent, 1861 }; 1862 1863 static const struct bus_type gadget_bus_type = { 1864 .name = "gadget", 1865 .probe = gadget_bind_driver, 1866 .remove = gadget_unbind_driver, 1867 .match = gadget_match_driver, 1868 }; 1869 1870 static int __init usb_udc_init(void) 1871 { 1872 int rc; 1873 1874 rc = class_register(&udc_class); 1875 if (rc) 1876 return rc; 1877 1878 rc = bus_register(&gadget_bus_type); 1879 if (rc) 1880 class_unregister(&udc_class); 1881 return rc; 1882 } 1883 subsys_initcall(usb_udc_init); 1884 1885 static void __exit usb_udc_exit(void) 1886 { 1887 bus_unregister(&gadget_bus_type); 1888 class_unregister(&udc_class); 1889 } 1890 module_exit(usb_udc_exit); 1891 1892 MODULE_DESCRIPTION("UDC Framework"); 1893 MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>"); 1894 MODULE_LICENSE("GPL v2"); 1895