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