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