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