WIP FPC-III support
[linux/fpc-iii.git] / drivers / usb / gadget / udc / core.c
blob6a62bbd01324f5bb99998a489188a5949a72dd3e
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * udc.c - Core UDC Framework
5 * Copyright (C) 2010 Texas Instruments
6 * Author: Felipe Balbi <balbi@ti.com>
7 */
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/device.h>
12 #include <linux/list.h>
13 #include <linux/err.h>
14 #include <linux/dma-mapping.h>
15 #include <linux/sched/task_stack.h>
16 #include <linux/workqueue.h>
18 #include <linux/usb/ch9.h>
19 #include <linux/usb/gadget.h>
20 #include <linux/usb.h>
22 #include "trace.h"
24 /**
25 * struct usb_udc - describes one usb device controller
26 * @driver: the gadget driver pointer. For use by the class code
27 * @dev: the child device to the actual controller
28 * @gadget: the gadget. For use by the class code
29 * @list: for use by the udc class driver
30 * @vbus: for udcs who care about vbus status, this value is real vbus status;
31 * for udcs who do not care about vbus status, this value is always true
33 * This represents the internal data structure which is used by the UDC-class
34 * to hold information about udc driver and gadget together.
36 struct usb_udc {
37 struct usb_gadget_driver *driver;
38 struct usb_gadget *gadget;
39 struct device dev;
40 struct list_head list;
41 bool vbus;
44 static struct class *udc_class;
45 static LIST_HEAD(udc_list);
46 static LIST_HEAD(gadget_driver_pending_list);
47 static DEFINE_MUTEX(udc_lock);
49 static int udc_bind_to_driver(struct usb_udc *udc,
50 struct usb_gadget_driver *driver);
52 /* ------------------------------------------------------------------------- */
54 /**
55 * usb_ep_set_maxpacket_limit - set maximum packet size limit for endpoint
56 * @ep:the endpoint being configured
57 * @maxpacket_limit:value of maximum packet size limit
59 * This function should be used only in UDC drivers to initialize endpoint
60 * (usually in probe function).
62 void usb_ep_set_maxpacket_limit(struct usb_ep *ep,
63 unsigned maxpacket_limit)
65 ep->maxpacket_limit = maxpacket_limit;
66 ep->maxpacket = maxpacket_limit;
68 trace_usb_ep_set_maxpacket_limit(ep, 0);
70 EXPORT_SYMBOL_GPL(usb_ep_set_maxpacket_limit);
72 /**
73 * usb_ep_enable - configure endpoint, making it usable
74 * @ep:the endpoint being configured. may not be the endpoint named "ep0".
75 * drivers discover endpoints through the ep_list of a usb_gadget.
77 * When configurations are set, or when interface settings change, the driver
78 * will enable or disable the relevant endpoints. while it is enabled, an
79 * endpoint may be used for i/o until the driver receives a disconnect() from
80 * the host or until the endpoint is disabled.
82 * the ep0 implementation (which calls this routine) must ensure that the
83 * hardware capabilities of each endpoint match the descriptor provided
84 * for it. for example, an endpoint named "ep2in-bulk" would be usable
85 * for interrupt transfers as well as bulk, but it likely couldn't be used
86 * for iso transfers or for endpoint 14. some endpoints are fully
87 * configurable, with more generic names like "ep-a". (remember that for
88 * USB, "in" means "towards the USB host".)
90 * This routine must be called in process context.
92 * returns zero, or a negative error code.
94 int usb_ep_enable(struct usb_ep *ep)
96 int ret = 0;
98 if (ep->enabled)
99 goto out;
101 /* UDC drivers can't handle endpoints with maxpacket size 0 */
102 if (usb_endpoint_maxp(ep->desc) == 0) {
104 * We should log an error message here, but we can't call
105 * dev_err() because there's no way to find the gadget
106 * given only ep.
108 ret = -EINVAL;
109 goto out;
112 ret = ep->ops->enable(ep, ep->desc);
113 if (ret)
114 goto out;
116 ep->enabled = true;
118 out:
119 trace_usb_ep_enable(ep, ret);
121 return ret;
123 EXPORT_SYMBOL_GPL(usb_ep_enable);
126 * usb_ep_disable - endpoint is no longer usable
127 * @ep:the endpoint being unconfigured. may not be the endpoint named "ep0".
129 * no other task may be using this endpoint when this is called.
130 * any pending and uncompleted requests will complete with status
131 * indicating disconnect (-ESHUTDOWN) before this call returns.
132 * gadget drivers must call usb_ep_enable() again before queueing
133 * requests to the endpoint.
135 * This routine must be called in process context.
137 * returns zero, or a negative error code.
139 int usb_ep_disable(struct usb_ep *ep)
141 int ret = 0;
143 if (!ep->enabled)
144 goto out;
146 ret = ep->ops->disable(ep);
147 if (ret)
148 goto out;
150 ep->enabled = false;
152 out:
153 trace_usb_ep_disable(ep, ret);
155 return ret;
157 EXPORT_SYMBOL_GPL(usb_ep_disable);
160 * usb_ep_alloc_request - allocate a request object to use with this endpoint
161 * @ep:the endpoint to be used with with the request
162 * @gfp_flags:GFP_* flags to use
164 * Request objects must be allocated with this call, since they normally
165 * need controller-specific setup and may even need endpoint-specific
166 * resources such as allocation of DMA descriptors.
167 * Requests may be submitted with usb_ep_queue(), and receive a single
168 * completion callback. Free requests with usb_ep_free_request(), when
169 * they are no longer needed.
171 * Returns the request, or null if one could not be allocated.
173 struct usb_request *usb_ep_alloc_request(struct usb_ep *ep,
174 gfp_t gfp_flags)
176 struct usb_request *req = NULL;
178 req = ep->ops->alloc_request(ep, gfp_flags);
180 trace_usb_ep_alloc_request(ep, req, req ? 0 : -ENOMEM);
182 return req;
184 EXPORT_SYMBOL_GPL(usb_ep_alloc_request);
187 * usb_ep_free_request - frees a request object
188 * @ep:the endpoint associated with the request
189 * @req:the request being freed
191 * Reverses the effect of usb_ep_alloc_request().
192 * Caller guarantees the request is not queued, and that it will
193 * no longer be requeued (or otherwise used).
195 void usb_ep_free_request(struct usb_ep *ep,
196 struct usb_request *req)
198 trace_usb_ep_free_request(ep, req, 0);
199 ep->ops->free_request(ep, req);
201 EXPORT_SYMBOL_GPL(usb_ep_free_request);
204 * usb_ep_queue - queues (submits) an I/O request to an endpoint.
205 * @ep:the endpoint associated with the request
206 * @req:the request being submitted
207 * @gfp_flags: GFP_* flags to use in case the lower level driver couldn't
208 * pre-allocate all necessary memory with the request.
210 * This tells the device controller to perform the specified request through
211 * that endpoint (reading or writing a buffer). When the request completes,
212 * including being canceled by usb_ep_dequeue(), the request's completion
213 * routine is called to return the request to the driver. Any endpoint
214 * (except control endpoints like ep0) may have more than one transfer
215 * request queued; they complete in FIFO order. Once a gadget driver
216 * submits a request, that request may not be examined or modified until it
217 * is given back to that driver through the completion callback.
219 * Each request is turned into one or more packets. The controller driver
220 * never merges adjacent requests into the same packet. OUT transfers
221 * will sometimes use data that's already buffered in the hardware.
222 * Drivers can rely on the fact that the first byte of the request's buffer
223 * always corresponds to the first byte of some USB packet, for both
224 * IN and OUT transfers.
226 * Bulk endpoints can queue any amount of data; the transfer is packetized
227 * automatically. The last packet will be short if the request doesn't fill it
228 * out completely. Zero length packets (ZLPs) should be avoided in portable
229 * protocols since not all usb hardware can successfully handle zero length
230 * packets. (ZLPs may be explicitly written, and may be implicitly written if
231 * the request 'zero' flag is set.) Bulk endpoints may also be used
232 * for interrupt transfers; but the reverse is not true, and some endpoints
233 * won't support every interrupt transfer. (Such as 768 byte packets.)
235 * Interrupt-only endpoints are less functional than bulk endpoints, for
236 * example by not supporting queueing or not handling buffers that are
237 * larger than the endpoint's maxpacket size. They may also treat data
238 * toggle differently.
240 * Control endpoints ... after getting a setup() callback, the driver queues
241 * one response (even if it would be zero length). That enables the
242 * status ack, after transferring data as specified in the response. Setup
243 * functions may return negative error codes to generate protocol stalls.
244 * (Note that some USB device controllers disallow protocol stall responses
245 * in some cases.) When control responses are deferred (the response is
246 * written after the setup callback returns), then usb_ep_set_halt() may be
247 * used on ep0 to trigger protocol stalls. Depending on the controller,
248 * it may not be possible to trigger a status-stage protocol stall when the
249 * data stage is over, that is, from within the response's completion
250 * routine.
252 * For periodic endpoints, like interrupt or isochronous ones, the usb host
253 * arranges to poll once per interval, and the gadget driver usually will
254 * have queued some data to transfer at that time.
256 * Note that @req's ->complete() callback must never be called from
257 * within usb_ep_queue() as that can create deadlock situations.
259 * This routine may be called in interrupt context.
261 * Returns zero, or a negative error code. Endpoints that are not enabled
262 * report errors; errors will also be
263 * reported when the usb peripheral is disconnected.
265 * If and only if @req is successfully queued (the return value is zero),
266 * @req->complete() will be called exactly once, when the Gadget core and
267 * UDC are finished with the request. When the completion function is called,
268 * control of the request is returned to the device driver which submitted it.
269 * The completion handler may then immediately free or reuse @req.
271 int usb_ep_queue(struct usb_ep *ep,
272 struct usb_request *req, gfp_t gfp_flags)
274 int ret = 0;
276 if (WARN_ON_ONCE(!ep->enabled && ep->address)) {
277 ret = -ESHUTDOWN;
278 goto out;
281 ret = ep->ops->queue(ep, req, gfp_flags);
283 out:
284 trace_usb_ep_queue(ep, req, ret);
286 return ret;
288 EXPORT_SYMBOL_GPL(usb_ep_queue);
291 * usb_ep_dequeue - dequeues (cancels, unlinks) an I/O request from an endpoint
292 * @ep:the endpoint associated with the request
293 * @req:the request being canceled
295 * If the request is still active on the endpoint, it is dequeued and
296 * eventually its completion routine is called (with status -ECONNRESET);
297 * else a negative error code is returned. This routine is asynchronous,
298 * that is, it may return before the completion routine runs.
300 * Note that some hardware can't clear out write fifos (to unlink the request
301 * at the head of the queue) except as part of disconnecting from usb. Such
302 * restrictions prevent drivers from supporting configuration changes,
303 * even to configuration zero (a "chapter 9" requirement).
305 * This routine may be called in interrupt context.
307 int usb_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
309 int ret;
311 ret = ep->ops->dequeue(ep, req);
312 trace_usb_ep_dequeue(ep, req, ret);
314 return ret;
316 EXPORT_SYMBOL_GPL(usb_ep_dequeue);
319 * usb_ep_set_halt - sets the endpoint halt feature.
320 * @ep: the non-isochronous endpoint being stalled
322 * Use this to stall an endpoint, perhaps as an error report.
323 * Except for control endpoints,
324 * the endpoint stays halted (will not stream any data) until the host
325 * clears this feature; drivers may need to empty the endpoint's request
326 * queue first, to make sure no inappropriate transfers happen.
328 * Note that while an endpoint CLEAR_FEATURE will be invisible to the
329 * gadget driver, a SET_INTERFACE will not be. To reset endpoints for the
330 * current altsetting, see usb_ep_clear_halt(). When switching altsettings,
331 * it's simplest to use usb_ep_enable() or usb_ep_disable() for the endpoints.
333 * This routine may be called in interrupt context.
335 * Returns zero, or a negative error code. On success, this call sets
336 * underlying hardware state that blocks data transfers.
337 * Attempts to halt IN endpoints will fail (returning -EAGAIN) if any
338 * transfer requests are still queued, or if the controller hardware
339 * (usually a FIFO) still holds bytes that the host hasn't collected.
341 int usb_ep_set_halt(struct usb_ep *ep)
343 int ret;
345 ret = ep->ops->set_halt(ep, 1);
346 trace_usb_ep_set_halt(ep, ret);
348 return ret;
350 EXPORT_SYMBOL_GPL(usb_ep_set_halt);
353 * usb_ep_clear_halt - clears endpoint halt, and resets toggle
354 * @ep:the bulk or interrupt endpoint being reset
356 * Use this when responding to the standard usb "set interface" request,
357 * for endpoints that aren't reconfigured, after clearing any other state
358 * in the endpoint's i/o queue.
360 * This routine may be called in interrupt context.
362 * Returns zero, or a negative error code. On success, this call clears
363 * the underlying hardware state reflecting endpoint halt and data toggle.
364 * Note that some hardware can't support this request (like pxa2xx_udc),
365 * and accordingly can't correctly implement interface altsettings.
367 int usb_ep_clear_halt(struct usb_ep *ep)
369 int ret;
371 ret = ep->ops->set_halt(ep, 0);
372 trace_usb_ep_clear_halt(ep, ret);
374 return ret;
376 EXPORT_SYMBOL_GPL(usb_ep_clear_halt);
379 * usb_ep_set_wedge - sets the halt feature and ignores clear requests
380 * @ep: the endpoint being wedged
382 * Use this to stall an endpoint and ignore CLEAR_FEATURE(HALT_ENDPOINT)
383 * requests. If the gadget driver clears the halt status, it will
384 * automatically unwedge the endpoint.
386 * This routine may be called in interrupt context.
388 * Returns zero on success, else negative errno.
390 int usb_ep_set_wedge(struct usb_ep *ep)
392 int ret;
394 if (ep->ops->set_wedge)
395 ret = ep->ops->set_wedge(ep);
396 else
397 ret = ep->ops->set_halt(ep, 1);
399 trace_usb_ep_set_wedge(ep, ret);
401 return ret;
403 EXPORT_SYMBOL_GPL(usb_ep_set_wedge);
406 * usb_ep_fifo_status - returns number of bytes in fifo, or error
407 * @ep: the endpoint whose fifo status is being checked.
409 * FIFO endpoints may have "unclaimed data" in them in certain cases,
410 * such as after aborted transfers. Hosts may not have collected all
411 * the IN data written by the gadget driver (and reported by a request
412 * completion). The gadget driver may not have collected all the data
413 * written OUT to it by the host. Drivers that need precise handling for
414 * fault reporting or recovery may need to use this call.
416 * This routine may be called in interrupt context.
418 * This returns the number of such bytes in the fifo, or a negative
419 * errno if the endpoint doesn't use a FIFO or doesn't support such
420 * precise handling.
422 int usb_ep_fifo_status(struct usb_ep *ep)
424 int ret;
426 if (ep->ops->fifo_status)
427 ret = ep->ops->fifo_status(ep);
428 else
429 ret = -EOPNOTSUPP;
431 trace_usb_ep_fifo_status(ep, ret);
433 return ret;
435 EXPORT_SYMBOL_GPL(usb_ep_fifo_status);
438 * usb_ep_fifo_flush - flushes contents of a fifo
439 * @ep: the endpoint whose fifo is being flushed.
441 * This call may be used to flush the "unclaimed data" that may exist in
442 * an endpoint fifo after abnormal transaction terminations. The call
443 * must never be used except when endpoint is not being used for any
444 * protocol translation.
446 * This routine may be called in interrupt context.
448 void usb_ep_fifo_flush(struct usb_ep *ep)
450 if (ep->ops->fifo_flush)
451 ep->ops->fifo_flush(ep);
453 trace_usb_ep_fifo_flush(ep, 0);
455 EXPORT_SYMBOL_GPL(usb_ep_fifo_flush);
457 /* ------------------------------------------------------------------------- */
460 * usb_gadget_frame_number - returns the current frame number
461 * @gadget: controller that reports the frame number
463 * Returns the usb frame number, normally eleven bits from a SOF packet,
464 * or negative errno if this device doesn't support this capability.
466 int usb_gadget_frame_number(struct usb_gadget *gadget)
468 int ret;
470 ret = gadget->ops->get_frame(gadget);
472 trace_usb_gadget_frame_number(gadget, ret);
474 return ret;
476 EXPORT_SYMBOL_GPL(usb_gadget_frame_number);
479 * usb_gadget_wakeup - tries to wake up the host connected to this gadget
480 * @gadget: controller used to wake up the host
482 * Returns zero on success, else negative error code if the hardware
483 * doesn't support such attempts, or its support has not been enabled
484 * by the usb host. Drivers must return device descriptors that report
485 * their ability to support this, or hosts won't enable it.
487 * This may also try to use SRP to wake the host and start enumeration,
488 * even if OTG isn't otherwise in use. OTG devices may also start
489 * remote wakeup even when hosts don't explicitly enable it.
491 int usb_gadget_wakeup(struct usb_gadget *gadget)
493 int ret = 0;
495 if (!gadget->ops->wakeup) {
496 ret = -EOPNOTSUPP;
497 goto out;
500 ret = gadget->ops->wakeup(gadget);
502 out:
503 trace_usb_gadget_wakeup(gadget, ret);
505 return ret;
507 EXPORT_SYMBOL_GPL(usb_gadget_wakeup);
510 * usb_gadget_set_selfpowered - sets the device selfpowered feature.
511 * @gadget:the device being declared as self-powered
513 * this affects the device status reported by the hardware driver
514 * to reflect that it now has a local power supply.
516 * returns zero on success, else negative errno.
518 int usb_gadget_set_selfpowered(struct usb_gadget *gadget)
520 int ret = 0;
522 if (!gadget->ops->set_selfpowered) {
523 ret = -EOPNOTSUPP;
524 goto out;
527 ret = gadget->ops->set_selfpowered(gadget, 1);
529 out:
530 trace_usb_gadget_set_selfpowered(gadget, ret);
532 return ret;
534 EXPORT_SYMBOL_GPL(usb_gadget_set_selfpowered);
537 * usb_gadget_clear_selfpowered - clear the device selfpowered feature.
538 * @gadget:the device being declared as bus-powered
540 * this affects the device status reported by the hardware driver.
541 * some hardware may not support bus-powered operation, in which
542 * case this feature's value can never change.
544 * returns zero on success, else negative errno.
546 int usb_gadget_clear_selfpowered(struct usb_gadget *gadget)
548 int ret = 0;
550 if (!gadget->ops->set_selfpowered) {
551 ret = -EOPNOTSUPP;
552 goto out;
555 ret = gadget->ops->set_selfpowered(gadget, 0);
557 out:
558 trace_usb_gadget_clear_selfpowered(gadget, ret);
560 return ret;
562 EXPORT_SYMBOL_GPL(usb_gadget_clear_selfpowered);
565 * usb_gadget_vbus_connect - Notify controller that VBUS is powered
566 * @gadget:The device which now has VBUS power.
567 * Context: can sleep
569 * This call is used by a driver for an external transceiver (or GPIO)
570 * that detects a VBUS power session starting. Common responses include
571 * resuming the controller, activating the D+ (or D-) pullup to let the
572 * host detect that a USB device is attached, and starting to draw power
573 * (8mA or possibly more, especially after SET_CONFIGURATION).
575 * Returns zero on success, else negative errno.
577 int usb_gadget_vbus_connect(struct usb_gadget *gadget)
579 int ret = 0;
581 if (!gadget->ops->vbus_session) {
582 ret = -EOPNOTSUPP;
583 goto out;
586 ret = gadget->ops->vbus_session(gadget, 1);
588 out:
589 trace_usb_gadget_vbus_connect(gadget, ret);
591 return ret;
593 EXPORT_SYMBOL_GPL(usb_gadget_vbus_connect);
596 * usb_gadget_vbus_draw - constrain controller's VBUS power usage
597 * @gadget:The device whose VBUS usage is being described
598 * @mA:How much current to draw, in milliAmperes. This should be twice
599 * the value listed in the configuration descriptor bMaxPower field.
601 * This call is used by gadget drivers during SET_CONFIGURATION calls,
602 * reporting how much power the device may consume. For example, this
603 * could affect how quickly batteries are recharged.
605 * Returns zero on success, else negative errno.
607 int usb_gadget_vbus_draw(struct usb_gadget *gadget, unsigned mA)
609 int ret = 0;
611 if (!gadget->ops->vbus_draw) {
612 ret = -EOPNOTSUPP;
613 goto out;
616 ret = gadget->ops->vbus_draw(gadget, mA);
617 if (!ret)
618 gadget->mA = mA;
620 out:
621 trace_usb_gadget_vbus_draw(gadget, ret);
623 return ret;
625 EXPORT_SYMBOL_GPL(usb_gadget_vbus_draw);
628 * usb_gadget_vbus_disconnect - notify controller about VBUS session end
629 * @gadget:the device whose VBUS supply is being described
630 * Context: can sleep
632 * This call is used by a driver for an external transceiver (or GPIO)
633 * that detects a VBUS power session ending. Common responses include
634 * reversing everything done in usb_gadget_vbus_connect().
636 * Returns zero on success, else negative errno.
638 int usb_gadget_vbus_disconnect(struct usb_gadget *gadget)
640 int ret = 0;
642 if (!gadget->ops->vbus_session) {
643 ret = -EOPNOTSUPP;
644 goto out;
647 ret = gadget->ops->vbus_session(gadget, 0);
649 out:
650 trace_usb_gadget_vbus_disconnect(gadget, ret);
652 return ret;
654 EXPORT_SYMBOL_GPL(usb_gadget_vbus_disconnect);
657 * usb_gadget_connect - software-controlled connect to USB host
658 * @gadget:the peripheral being connected
660 * Enables the D+ (or potentially D-) pullup. The host will start
661 * enumerating this gadget when the pullup is active and a VBUS session
662 * is active (the link is powered).
664 * Returns zero on success, else negative errno.
666 int usb_gadget_connect(struct usb_gadget *gadget)
668 int ret = 0;
670 if (!gadget->ops->pullup) {
671 ret = -EOPNOTSUPP;
672 goto out;
675 if (gadget->deactivated) {
677 * If gadget is deactivated we only save new state.
678 * Gadget will be connected automatically after activation.
680 gadget->connected = true;
681 goto out;
684 ret = gadget->ops->pullup(gadget, 1);
685 if (!ret)
686 gadget->connected = 1;
688 out:
689 trace_usb_gadget_connect(gadget, ret);
691 return ret;
693 EXPORT_SYMBOL_GPL(usb_gadget_connect);
696 * usb_gadget_disconnect - software-controlled disconnect from USB host
697 * @gadget:the peripheral being disconnected
699 * Disables the D+ (or potentially D-) pullup, which the host may see
700 * as a disconnect (when a VBUS session is active). Not all systems
701 * support software pullup controls.
703 * Following a successful disconnect, invoke the ->disconnect() callback
704 * for the current gadget driver so that UDC drivers don't need to.
706 * Returns zero on success, else negative errno.
708 int usb_gadget_disconnect(struct usb_gadget *gadget)
710 int ret = 0;
712 if (!gadget->ops->pullup) {
713 ret = -EOPNOTSUPP;
714 goto out;
717 if (!gadget->connected)
718 goto out;
720 if (gadget->deactivated) {
722 * If gadget is deactivated we only save new state.
723 * Gadget will stay disconnected after activation.
725 gadget->connected = false;
726 goto out;
729 ret = gadget->ops->pullup(gadget, 0);
730 if (!ret) {
731 gadget->connected = 0;
732 gadget->udc->driver->disconnect(gadget);
735 out:
736 trace_usb_gadget_disconnect(gadget, ret);
738 return ret;
740 EXPORT_SYMBOL_GPL(usb_gadget_disconnect);
743 * usb_gadget_deactivate - deactivate function which is not ready to work
744 * @gadget: the peripheral being deactivated
746 * This routine may be used during the gadget driver bind() call to prevent
747 * the peripheral from ever being visible to the USB host, unless later
748 * usb_gadget_activate() is called. For example, user mode components may
749 * need to be activated before the system can talk to hosts.
751 * Returns zero on success, else negative errno.
753 int usb_gadget_deactivate(struct usb_gadget *gadget)
755 int ret = 0;
757 if (gadget->deactivated)
758 goto out;
760 if (gadget->connected) {
761 ret = usb_gadget_disconnect(gadget);
762 if (ret)
763 goto out;
766 * If gadget was being connected before deactivation, we want
767 * to reconnect it in usb_gadget_activate().
769 gadget->connected = true;
771 gadget->deactivated = true;
773 out:
774 trace_usb_gadget_deactivate(gadget, ret);
776 return ret;
778 EXPORT_SYMBOL_GPL(usb_gadget_deactivate);
781 * usb_gadget_activate - activate function which is not ready to work
782 * @gadget: the peripheral being activated
784 * This routine activates gadget which was previously deactivated with
785 * usb_gadget_deactivate() call. It calls usb_gadget_connect() if needed.
787 * Returns zero on success, else negative errno.
789 int usb_gadget_activate(struct usb_gadget *gadget)
791 int ret = 0;
793 if (!gadget->deactivated)
794 goto out;
796 gadget->deactivated = false;
799 * If gadget has been connected before deactivation, or became connected
800 * while it was being deactivated, we call usb_gadget_connect().
802 if (gadget->connected)
803 ret = usb_gadget_connect(gadget);
805 out:
806 trace_usb_gadget_activate(gadget, ret);
808 return ret;
810 EXPORT_SYMBOL_GPL(usb_gadget_activate);
812 /* ------------------------------------------------------------------------- */
814 #ifdef CONFIG_HAS_DMA
816 int usb_gadget_map_request_by_dev(struct device *dev,
817 struct usb_request *req, int is_in)
819 if (req->length == 0)
820 return 0;
822 if (req->num_sgs) {
823 int mapped;
825 mapped = dma_map_sg(dev, req->sg, req->num_sgs,
826 is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
827 if (mapped == 0) {
828 dev_err(dev, "failed to map SGs\n");
829 return -EFAULT;
832 req->num_mapped_sgs = mapped;
833 } else {
834 if (is_vmalloc_addr(req->buf)) {
835 dev_err(dev, "buffer is not dma capable\n");
836 return -EFAULT;
837 } else if (object_is_on_stack(req->buf)) {
838 dev_err(dev, "buffer is on stack\n");
839 return -EFAULT;
842 req->dma = dma_map_single(dev, req->buf, req->length,
843 is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
845 if (dma_mapping_error(dev, req->dma)) {
846 dev_err(dev, "failed to map buffer\n");
847 return -EFAULT;
850 req->dma_mapped = 1;
853 return 0;
855 EXPORT_SYMBOL_GPL(usb_gadget_map_request_by_dev);
857 int usb_gadget_map_request(struct usb_gadget *gadget,
858 struct usb_request *req, int is_in)
860 return usb_gadget_map_request_by_dev(gadget->dev.parent, req, is_in);
862 EXPORT_SYMBOL_GPL(usb_gadget_map_request);
864 void usb_gadget_unmap_request_by_dev(struct device *dev,
865 struct usb_request *req, int is_in)
867 if (req->length == 0)
868 return;
870 if (req->num_mapped_sgs) {
871 dma_unmap_sg(dev, req->sg, req->num_sgs,
872 is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
874 req->num_mapped_sgs = 0;
875 } else if (req->dma_mapped) {
876 dma_unmap_single(dev, req->dma, req->length,
877 is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
878 req->dma_mapped = 0;
881 EXPORT_SYMBOL_GPL(usb_gadget_unmap_request_by_dev);
883 void usb_gadget_unmap_request(struct usb_gadget *gadget,
884 struct usb_request *req, int is_in)
886 usb_gadget_unmap_request_by_dev(gadget->dev.parent, req, is_in);
888 EXPORT_SYMBOL_GPL(usb_gadget_unmap_request);
890 #endif /* CONFIG_HAS_DMA */
892 /* ------------------------------------------------------------------------- */
895 * usb_gadget_giveback_request - give the request back to the gadget layer
896 * @ep: the endpoint to be used with with the request
897 * @req: the request being given back
899 * This is called by device controller drivers in order to return the
900 * completed request back to the gadget layer.
902 void usb_gadget_giveback_request(struct usb_ep *ep,
903 struct usb_request *req)
905 if (likely(req->status == 0))
906 usb_led_activity(USB_LED_EVENT_GADGET);
908 trace_usb_gadget_giveback_request(ep, req, 0);
910 req->complete(ep, req);
912 EXPORT_SYMBOL_GPL(usb_gadget_giveback_request);
914 /* ------------------------------------------------------------------------- */
917 * gadget_find_ep_by_name - returns ep whose name is the same as sting passed
918 * in second parameter or NULL if searched endpoint not found
919 * @g: controller to check for quirk
920 * @name: name of searched endpoint
922 struct usb_ep *gadget_find_ep_by_name(struct usb_gadget *g, const char *name)
924 struct usb_ep *ep;
926 gadget_for_each_ep(ep, g) {
927 if (!strcmp(ep->name, name))
928 return ep;
931 return NULL;
933 EXPORT_SYMBOL_GPL(gadget_find_ep_by_name);
935 /* ------------------------------------------------------------------------- */
937 int usb_gadget_ep_match_desc(struct usb_gadget *gadget,
938 struct usb_ep *ep, struct usb_endpoint_descriptor *desc,
939 struct usb_ss_ep_comp_descriptor *ep_comp)
941 u8 type;
942 u16 max;
943 int num_req_streams = 0;
945 /* endpoint already claimed? */
946 if (ep->claimed)
947 return 0;
949 type = usb_endpoint_type(desc);
950 max = usb_endpoint_maxp(desc);
952 if (usb_endpoint_dir_in(desc) && !ep->caps.dir_in)
953 return 0;
954 if (usb_endpoint_dir_out(desc) && !ep->caps.dir_out)
955 return 0;
957 if (max > ep->maxpacket_limit)
958 return 0;
960 /* "high bandwidth" works only at high speed */
961 if (!gadget_is_dualspeed(gadget) && usb_endpoint_maxp_mult(desc) > 1)
962 return 0;
964 switch (type) {
965 case USB_ENDPOINT_XFER_CONTROL:
966 /* only support ep0 for portable CONTROL traffic */
967 return 0;
968 case USB_ENDPOINT_XFER_ISOC:
969 if (!ep->caps.type_iso)
970 return 0;
971 /* ISO: limit 1023 bytes full speed, 1024 high/super speed */
972 if (!gadget_is_dualspeed(gadget) && max > 1023)
973 return 0;
974 break;
975 case USB_ENDPOINT_XFER_BULK:
976 if (!ep->caps.type_bulk)
977 return 0;
978 if (ep_comp && gadget_is_superspeed(gadget)) {
979 /* Get the number of required streams from the
980 * EP companion descriptor and see if the EP
981 * matches it
983 num_req_streams = ep_comp->bmAttributes & 0x1f;
984 if (num_req_streams > ep->max_streams)
985 return 0;
987 break;
988 case USB_ENDPOINT_XFER_INT:
989 /* Bulk endpoints handle interrupt transfers,
990 * except the toggle-quirky iso-synch kind
992 if (!ep->caps.type_int && !ep->caps.type_bulk)
993 return 0;
994 /* INT: limit 64 bytes full speed, 1024 high/super speed */
995 if (!gadget_is_dualspeed(gadget) && max > 64)
996 return 0;
997 break;
1000 return 1;
1002 EXPORT_SYMBOL_GPL(usb_gadget_ep_match_desc);
1004 /* ------------------------------------------------------------------------- */
1006 static void usb_gadget_state_work(struct work_struct *work)
1008 struct usb_gadget *gadget = work_to_gadget(work);
1009 struct usb_udc *udc = gadget->udc;
1011 if (udc)
1012 sysfs_notify(&udc->dev.kobj, NULL, "state");
1015 void usb_gadget_set_state(struct usb_gadget *gadget,
1016 enum usb_device_state state)
1018 gadget->state = state;
1019 schedule_work(&gadget->work);
1021 EXPORT_SYMBOL_GPL(usb_gadget_set_state);
1023 /* ------------------------------------------------------------------------- */
1025 static void usb_udc_connect_control(struct usb_udc *udc)
1027 if (udc->vbus)
1028 usb_gadget_connect(udc->gadget);
1029 else
1030 usb_gadget_disconnect(udc->gadget);
1034 * usb_udc_vbus_handler - updates the udc core vbus status, and try to
1035 * connect or disconnect gadget
1036 * @gadget: The gadget which vbus change occurs
1037 * @status: The vbus status
1039 * The udc driver calls it when it wants to connect or disconnect gadget
1040 * according to vbus status.
1042 void usb_udc_vbus_handler(struct usb_gadget *gadget, bool status)
1044 struct usb_udc *udc = gadget->udc;
1046 if (udc) {
1047 udc->vbus = status;
1048 usb_udc_connect_control(udc);
1051 EXPORT_SYMBOL_GPL(usb_udc_vbus_handler);
1054 * usb_gadget_udc_reset - notifies the udc core that bus reset occurs
1055 * @gadget: The gadget which bus reset occurs
1056 * @driver: The gadget driver we want to notify
1058 * If the udc driver has bus reset handler, it needs to call this when the bus
1059 * reset occurs, it notifies the gadget driver that the bus reset occurs as
1060 * well as updates gadget state.
1062 void usb_gadget_udc_reset(struct usb_gadget *gadget,
1063 struct usb_gadget_driver *driver)
1065 driver->reset(gadget);
1066 usb_gadget_set_state(gadget, USB_STATE_DEFAULT);
1068 EXPORT_SYMBOL_GPL(usb_gadget_udc_reset);
1071 * usb_gadget_udc_start - tells usb device controller to start up
1072 * @udc: The UDC to be started
1074 * This call is issued by the UDC Class driver when it's about
1075 * to register a gadget driver to the device controller, before
1076 * calling gadget driver's bind() method.
1078 * It allows the controller to be powered off until strictly
1079 * necessary to have it powered on.
1081 * Returns zero on success, else negative errno.
1083 static inline int usb_gadget_udc_start(struct usb_udc *udc)
1085 return udc->gadget->ops->udc_start(udc->gadget, udc->driver);
1089 * usb_gadget_udc_stop - tells usb device controller we don't need it anymore
1090 * @udc: The UDC to be stopped
1092 * This call is issued by the UDC Class driver after calling
1093 * gadget driver's unbind() method.
1095 * The details are implementation specific, but it can go as
1096 * far as powering off UDC completely and disable its data
1097 * line pullups.
1099 static inline void usb_gadget_udc_stop(struct usb_udc *udc)
1101 udc->gadget->ops->udc_stop(udc->gadget);
1105 * usb_gadget_udc_set_speed - tells usb device controller speed supported by
1106 * current driver
1107 * @udc: The device we want to set maximum speed
1108 * @speed: The maximum speed to allowed to run
1110 * This call is issued by the UDC Class driver before calling
1111 * usb_gadget_udc_start() in order to make sure that we don't try to
1112 * connect on speeds the gadget driver doesn't support.
1114 static inline void usb_gadget_udc_set_speed(struct usb_udc *udc,
1115 enum usb_device_speed speed)
1117 if (udc->gadget->ops->udc_set_speed) {
1118 enum usb_device_speed s;
1120 s = min(speed, udc->gadget->max_speed);
1121 udc->gadget->ops->udc_set_speed(udc->gadget, s);
1126 * usb_udc_release - release the usb_udc struct
1127 * @dev: the dev member within usb_udc
1129 * This is called by driver's core in order to free memory once the last
1130 * reference is released.
1132 static void usb_udc_release(struct device *dev)
1134 struct usb_udc *udc;
1136 udc = container_of(dev, struct usb_udc, dev);
1137 dev_dbg(dev, "releasing '%s'\n", dev_name(dev));
1138 kfree(udc);
1141 static const struct attribute_group *usb_udc_attr_groups[];
1143 static void usb_udc_nop_release(struct device *dev)
1145 dev_vdbg(dev, "%s\n", __func__);
1148 /* should be called with udc_lock held */
1149 static int check_pending_gadget_drivers(struct usb_udc *udc)
1151 struct usb_gadget_driver *driver;
1152 int ret = 0;
1154 list_for_each_entry(driver, &gadget_driver_pending_list, pending)
1155 if (!driver->udc_name || strcmp(driver->udc_name,
1156 dev_name(&udc->dev)) == 0) {
1157 ret = udc_bind_to_driver(udc, driver);
1158 if (ret != -EPROBE_DEFER)
1159 list_del_init(&driver->pending);
1160 break;
1163 return ret;
1167 * usb_initialize_gadget - initialize a gadget and its embedded struct device
1168 * @parent: the parent device to this udc. Usually the controller driver's
1169 * device.
1170 * @gadget: the gadget to be initialized.
1171 * @release: a gadget release function.
1173 * Returns zero on success, negative errno otherwise.
1174 * Calls the gadget release function in the latter case.
1176 void usb_initialize_gadget(struct device *parent, struct usb_gadget *gadget,
1177 void (*release)(struct device *dev))
1179 dev_set_name(&gadget->dev, "gadget");
1180 INIT_WORK(&gadget->work, usb_gadget_state_work);
1181 gadget->dev.parent = parent;
1183 if (release)
1184 gadget->dev.release = release;
1185 else
1186 gadget->dev.release = usb_udc_nop_release;
1188 device_initialize(&gadget->dev);
1190 EXPORT_SYMBOL_GPL(usb_initialize_gadget);
1193 * usb_add_gadget - adds a new gadget to the udc class driver list
1194 * @gadget: the gadget to be added to the list.
1196 * Returns zero on success, negative errno otherwise.
1197 * Does not do a final usb_put_gadget() if an error occurs.
1199 int usb_add_gadget(struct usb_gadget *gadget)
1201 struct usb_udc *udc;
1202 int ret = -ENOMEM;
1204 udc = kzalloc(sizeof(*udc), GFP_KERNEL);
1205 if (!udc)
1206 goto error;
1208 device_initialize(&udc->dev);
1209 udc->dev.release = usb_udc_release;
1210 udc->dev.class = udc_class;
1211 udc->dev.groups = usb_udc_attr_groups;
1212 udc->dev.parent = gadget->dev.parent;
1213 ret = dev_set_name(&udc->dev, "%s",
1214 kobject_name(&gadget->dev.parent->kobj));
1215 if (ret)
1216 goto err_put_udc;
1218 ret = device_add(&gadget->dev);
1219 if (ret)
1220 goto err_put_udc;
1222 udc->gadget = gadget;
1223 gadget->udc = udc;
1225 mutex_lock(&udc_lock);
1226 list_add_tail(&udc->list, &udc_list);
1228 ret = device_add(&udc->dev);
1229 if (ret)
1230 goto err_unlist_udc;
1232 usb_gadget_set_state(gadget, USB_STATE_NOTATTACHED);
1233 udc->vbus = true;
1235 /* pick up one of pending gadget drivers */
1236 ret = check_pending_gadget_drivers(udc);
1237 if (ret)
1238 goto err_del_udc;
1240 mutex_unlock(&udc_lock);
1242 return 0;
1244 err_del_udc:
1245 flush_work(&gadget->work);
1246 device_del(&udc->dev);
1248 err_unlist_udc:
1249 list_del(&udc->list);
1250 mutex_unlock(&udc_lock);
1252 device_del(&gadget->dev);
1254 err_put_udc:
1255 put_device(&udc->dev);
1257 error:
1258 return ret;
1260 EXPORT_SYMBOL_GPL(usb_add_gadget);
1263 * usb_add_gadget_udc_release - adds a new gadget to the udc class driver list
1264 * @parent: the parent device to this udc. Usually the controller driver's
1265 * device.
1266 * @gadget: the gadget to be added to the list.
1267 * @release: a gadget release function.
1269 * Returns zero on success, negative errno otherwise.
1270 * Calls the gadget release function in the latter case.
1272 int usb_add_gadget_udc_release(struct device *parent, struct usb_gadget *gadget,
1273 void (*release)(struct device *dev))
1275 int ret;
1277 usb_initialize_gadget(parent, gadget, release);
1278 ret = usb_add_gadget(gadget);
1279 if (ret)
1280 usb_put_gadget(gadget);
1281 return ret;
1283 EXPORT_SYMBOL_GPL(usb_add_gadget_udc_release);
1286 * usb_get_gadget_udc_name - get the name of the first UDC controller
1287 * This functions returns the name of the first UDC controller in the system.
1288 * Please note that this interface is usefull only for legacy drivers which
1289 * assume that there is only one UDC controller in the system and they need to
1290 * get its name before initialization. There is no guarantee that the UDC
1291 * of the returned name will be still available, when gadget driver registers
1292 * itself.
1294 * Returns pointer to string with UDC controller name on success, NULL
1295 * otherwise. Caller should kfree() returned string.
1297 char *usb_get_gadget_udc_name(void)
1299 struct usb_udc *udc;
1300 char *name = NULL;
1302 /* For now we take the first available UDC */
1303 mutex_lock(&udc_lock);
1304 list_for_each_entry(udc, &udc_list, list) {
1305 if (!udc->driver) {
1306 name = kstrdup(udc->gadget->name, GFP_KERNEL);
1307 break;
1310 mutex_unlock(&udc_lock);
1311 return name;
1313 EXPORT_SYMBOL_GPL(usb_get_gadget_udc_name);
1316 * usb_add_gadget_udc - adds a new gadget to the udc class driver list
1317 * @parent: the parent device to this udc. Usually the controller
1318 * driver's device.
1319 * @gadget: the gadget to be added to the list
1321 * Returns zero on success, negative errno otherwise.
1323 int usb_add_gadget_udc(struct device *parent, struct usb_gadget *gadget)
1325 return usb_add_gadget_udc_release(parent, gadget, NULL);
1327 EXPORT_SYMBOL_GPL(usb_add_gadget_udc);
1329 static void usb_gadget_remove_driver(struct usb_udc *udc)
1331 dev_dbg(&udc->dev, "unregistering UDC driver [%s]\n",
1332 udc->driver->function);
1334 kobject_uevent(&udc->dev.kobj, KOBJ_CHANGE);
1336 usb_gadget_disconnect(udc->gadget);
1337 if (udc->gadget->irq)
1338 synchronize_irq(udc->gadget->irq);
1339 udc->driver->unbind(udc->gadget);
1340 usb_gadget_udc_stop(udc);
1342 udc->driver = NULL;
1343 udc->dev.driver = NULL;
1344 udc->gadget->dev.driver = NULL;
1348 * usb_del_gadget - deletes @udc from udc_list
1349 * @gadget: the gadget to be removed.
1351 * This will call usb_gadget_unregister_driver() if
1352 * the @udc is still busy.
1353 * It will not do a final usb_put_gadget().
1355 void usb_del_gadget(struct usb_gadget *gadget)
1357 struct usb_udc *udc = gadget->udc;
1359 if (!udc)
1360 return;
1362 dev_vdbg(gadget->dev.parent, "unregistering gadget\n");
1364 mutex_lock(&udc_lock);
1365 list_del(&udc->list);
1367 if (udc->driver) {
1368 struct usb_gadget_driver *driver = udc->driver;
1370 usb_gadget_remove_driver(udc);
1371 list_add(&driver->pending, &gadget_driver_pending_list);
1373 mutex_unlock(&udc_lock);
1375 kobject_uevent(&udc->dev.kobj, KOBJ_REMOVE);
1376 flush_work(&gadget->work);
1377 device_unregister(&udc->dev);
1378 device_del(&gadget->dev);
1380 EXPORT_SYMBOL_GPL(usb_del_gadget);
1383 * usb_del_gadget_udc - deletes @udc from udc_list
1384 * @gadget: the gadget to be removed.
1386 * Calls usb_del_gadget() and does a final usb_put_gadget().
1388 void usb_del_gadget_udc(struct usb_gadget *gadget)
1390 usb_del_gadget(gadget);
1391 usb_put_gadget(gadget);
1393 EXPORT_SYMBOL_GPL(usb_del_gadget_udc);
1395 /* ------------------------------------------------------------------------- */
1397 static int udc_bind_to_driver(struct usb_udc *udc, struct usb_gadget_driver *driver)
1399 int ret;
1401 dev_dbg(&udc->dev, "registering UDC driver [%s]\n",
1402 driver->function);
1404 udc->driver = driver;
1405 udc->dev.driver = &driver->driver;
1406 udc->gadget->dev.driver = &driver->driver;
1408 usb_gadget_udc_set_speed(udc, driver->max_speed);
1410 ret = driver->bind(udc->gadget, driver);
1411 if (ret)
1412 goto err1;
1413 ret = usb_gadget_udc_start(udc);
1414 if (ret) {
1415 driver->unbind(udc->gadget);
1416 goto err1;
1418 usb_udc_connect_control(udc);
1420 kobject_uevent(&udc->dev.kobj, KOBJ_CHANGE);
1421 return 0;
1422 err1:
1423 if (ret != -EISNAM)
1424 dev_err(&udc->dev, "failed to start %s: %d\n",
1425 udc->driver->function, ret);
1426 udc->driver = NULL;
1427 udc->dev.driver = NULL;
1428 udc->gadget->dev.driver = NULL;
1429 return ret;
1432 int usb_gadget_probe_driver(struct usb_gadget_driver *driver)
1434 struct usb_udc *udc = NULL;
1435 int ret = -ENODEV;
1437 if (!driver || !driver->bind || !driver->setup)
1438 return -EINVAL;
1440 mutex_lock(&udc_lock);
1441 if (driver->udc_name) {
1442 list_for_each_entry(udc, &udc_list, list) {
1443 ret = strcmp(driver->udc_name, dev_name(&udc->dev));
1444 if (!ret)
1445 break;
1447 if (ret)
1448 ret = -ENODEV;
1449 else if (udc->driver)
1450 ret = -EBUSY;
1451 else
1452 goto found;
1453 } else {
1454 list_for_each_entry(udc, &udc_list, list) {
1455 /* For now we take the first one */
1456 if (!udc->driver)
1457 goto found;
1461 if (!driver->match_existing_only) {
1462 list_add_tail(&driver->pending, &gadget_driver_pending_list);
1463 pr_info("udc-core: couldn't find an available UDC - added [%s] to list of pending drivers\n",
1464 driver->function);
1465 ret = 0;
1468 mutex_unlock(&udc_lock);
1469 if (ret)
1470 pr_warn("udc-core: couldn't find an available UDC or it's busy\n");
1471 return ret;
1472 found:
1473 ret = udc_bind_to_driver(udc, driver);
1474 mutex_unlock(&udc_lock);
1475 return ret;
1477 EXPORT_SYMBOL_GPL(usb_gadget_probe_driver);
1479 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
1481 struct usb_udc *udc = NULL;
1482 int ret = -ENODEV;
1484 if (!driver || !driver->unbind)
1485 return -EINVAL;
1487 mutex_lock(&udc_lock);
1488 list_for_each_entry(udc, &udc_list, list) {
1489 if (udc->driver == driver) {
1490 usb_gadget_remove_driver(udc);
1491 usb_gadget_set_state(udc->gadget,
1492 USB_STATE_NOTATTACHED);
1494 /* Maybe there is someone waiting for this UDC? */
1495 check_pending_gadget_drivers(udc);
1497 * For now we ignore bind errors as probably it's
1498 * not a valid reason to fail other's gadget unbind
1500 ret = 0;
1501 break;
1505 if (ret) {
1506 list_del(&driver->pending);
1507 ret = 0;
1509 mutex_unlock(&udc_lock);
1510 return ret;
1512 EXPORT_SYMBOL_GPL(usb_gadget_unregister_driver);
1514 /* ------------------------------------------------------------------------- */
1516 static ssize_t srp_store(struct device *dev,
1517 struct device_attribute *attr, const char *buf, size_t n)
1519 struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
1521 if (sysfs_streq(buf, "1"))
1522 usb_gadget_wakeup(udc->gadget);
1524 return n;
1526 static DEVICE_ATTR_WO(srp);
1528 static ssize_t soft_connect_store(struct device *dev,
1529 struct device_attribute *attr, const char *buf, size_t n)
1531 struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
1533 if (!udc->driver) {
1534 dev_err(dev, "soft-connect without a gadget driver\n");
1535 return -EOPNOTSUPP;
1538 if (sysfs_streq(buf, "connect")) {
1539 usb_gadget_udc_start(udc);
1540 usb_gadget_connect(udc->gadget);
1541 } else if (sysfs_streq(buf, "disconnect")) {
1542 usb_gadget_disconnect(udc->gadget);
1543 usb_gadget_udc_stop(udc);
1544 } else {
1545 dev_err(dev, "unsupported command '%s'\n", buf);
1546 return -EINVAL;
1549 return n;
1551 static DEVICE_ATTR_WO(soft_connect);
1553 static ssize_t state_show(struct device *dev, struct device_attribute *attr,
1554 char *buf)
1556 struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
1557 struct usb_gadget *gadget = udc->gadget;
1559 return sprintf(buf, "%s\n", usb_state_string(gadget->state));
1561 static DEVICE_ATTR_RO(state);
1563 static ssize_t function_show(struct device *dev, struct device_attribute *attr,
1564 char *buf)
1566 struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
1567 struct usb_gadget_driver *drv = udc->driver;
1569 if (!drv || !drv->function)
1570 return 0;
1571 return scnprintf(buf, PAGE_SIZE, "%s\n", drv->function);
1573 static DEVICE_ATTR_RO(function);
1575 #define USB_UDC_SPEED_ATTR(name, param) \
1576 ssize_t name##_show(struct device *dev, \
1577 struct device_attribute *attr, char *buf) \
1579 struct usb_udc *udc = container_of(dev, struct usb_udc, dev); \
1580 return scnprintf(buf, PAGE_SIZE, "%s\n", \
1581 usb_speed_string(udc->gadget->param)); \
1583 static DEVICE_ATTR_RO(name)
1585 static USB_UDC_SPEED_ATTR(current_speed, speed);
1586 static USB_UDC_SPEED_ATTR(maximum_speed, max_speed);
1588 #define USB_UDC_ATTR(name) \
1589 ssize_t name##_show(struct device *dev, \
1590 struct device_attribute *attr, char *buf) \
1592 struct usb_udc *udc = container_of(dev, struct usb_udc, dev); \
1593 struct usb_gadget *gadget = udc->gadget; \
1595 return scnprintf(buf, PAGE_SIZE, "%d\n", gadget->name); \
1597 static DEVICE_ATTR_RO(name)
1599 static USB_UDC_ATTR(is_otg);
1600 static USB_UDC_ATTR(is_a_peripheral);
1601 static USB_UDC_ATTR(b_hnp_enable);
1602 static USB_UDC_ATTR(a_hnp_support);
1603 static USB_UDC_ATTR(a_alt_hnp_support);
1604 static USB_UDC_ATTR(is_selfpowered);
1606 static struct attribute *usb_udc_attrs[] = {
1607 &dev_attr_srp.attr,
1608 &dev_attr_soft_connect.attr,
1609 &dev_attr_state.attr,
1610 &dev_attr_function.attr,
1611 &dev_attr_current_speed.attr,
1612 &dev_attr_maximum_speed.attr,
1614 &dev_attr_is_otg.attr,
1615 &dev_attr_is_a_peripheral.attr,
1616 &dev_attr_b_hnp_enable.attr,
1617 &dev_attr_a_hnp_support.attr,
1618 &dev_attr_a_alt_hnp_support.attr,
1619 &dev_attr_is_selfpowered.attr,
1620 NULL,
1623 static const struct attribute_group usb_udc_attr_group = {
1624 .attrs = usb_udc_attrs,
1627 static const struct attribute_group *usb_udc_attr_groups[] = {
1628 &usb_udc_attr_group,
1629 NULL,
1632 static int usb_udc_uevent(struct device *dev, struct kobj_uevent_env *env)
1634 struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
1635 int ret;
1637 ret = add_uevent_var(env, "USB_UDC_NAME=%s", udc->gadget->name);
1638 if (ret) {
1639 dev_err(dev, "failed to add uevent USB_UDC_NAME\n");
1640 return ret;
1643 if (udc->driver) {
1644 ret = add_uevent_var(env, "USB_UDC_DRIVER=%s",
1645 udc->driver->function);
1646 if (ret) {
1647 dev_err(dev, "failed to add uevent USB_UDC_DRIVER\n");
1648 return ret;
1652 return 0;
1655 static int __init usb_udc_init(void)
1657 udc_class = class_create(THIS_MODULE, "udc");
1658 if (IS_ERR(udc_class)) {
1659 pr_err("failed to create udc class --> %ld\n",
1660 PTR_ERR(udc_class));
1661 return PTR_ERR(udc_class);
1664 udc_class->dev_uevent = usb_udc_uevent;
1665 return 0;
1667 subsys_initcall(usb_udc_init);
1669 static void __exit usb_udc_exit(void)
1671 class_destroy(udc_class);
1673 module_exit(usb_udc_exit);
1675 MODULE_DESCRIPTION("UDC Framework");
1676 MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
1677 MODULE_LICENSE("GPL v2");