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