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