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