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