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

source code of linux/drivers/usb/gadget/udc/core.c