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