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