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