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