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