blob: 188961780b8a72b668e7035d6110d812eb56495b [file] [log] [blame]
Felipe Balbi2ccea032011-06-28 16:33:46 +03001/**
2 * udc.c - Core UDC Framework
3 *
4 * Copyright (C) 2010 Texas Instruments
5 * Author: Felipe Balbi <balbi@ti.com>
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 of
9 * the License as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <linux/kernel.h>
21#include <linux/module.h>
22#include <linux/device.h>
23#include <linux/list.h>
24#include <linux/err.h>
Felipe Balbia6989082011-12-15 13:31:48 +020025#include <linux/dma-mapping.h>
Felipe Balbi5702f752013-07-17 11:09:49 +030026#include <linux/workqueue.h>
Felipe Balbi2ccea032011-06-28 16:33:46 +030027
28#include <linux/usb/ch9.h>
29#include <linux/usb/gadget.h>
Michal Sojka0cfbd322014-09-24 22:43:21 +020030#include <linux/usb.h>
Felipe Balbi2ccea032011-06-28 16:33:46 +030031
Felipe Balbi5e42d712016-05-31 13:39:21 +030032#include "trace.h"
33
Felipe Balbi2ccea032011-06-28 16:33:46 +030034/**
35 * struct usb_udc - describes one usb device controller
36 * @driver - the gadget driver pointer. For use by the class code
37 * @dev - the child device to the actual controller
38 * @gadget - the gadget. For use by the class code
39 * @list - for use by the udc class driver
Peter Chen628ef0d2015-03-06 10:36:03 +080040 * @vbus - for udcs who care about vbus status, this value is real vbus status;
41 * for udcs who do not care about vbus status, this value is always true
Felipe Balbi2ccea032011-06-28 16:33:46 +030042 *
43 * This represents the internal data structure which is used by the UDC-class
44 * to hold information about udc driver and gadget together.
45 */
46struct usb_udc {
47 struct usb_gadget_driver *driver;
48 struct usb_gadget *gadget;
49 struct device dev;
50 struct list_head list;
Peter Chen628ef0d2015-03-06 10:36:03 +080051 bool vbus;
Felipe Balbi2ccea032011-06-28 16:33:46 +030052};
53
54static struct class *udc_class;
Felipe Balbi2ccea032011-06-28 16:33:46 +030055static LIST_HEAD(udc_list);
Ruslan Bilovol855ed042015-11-23 09:56:38 +010056static LIST_HEAD(gadget_driver_pending_list);
Felipe Balbi2ccea032011-06-28 16:33:46 +030057static DEFINE_MUTEX(udc_lock);
58
Ruslan Bilovol855ed042015-11-23 09:56:38 +010059static int udc_bind_to_driver(struct usb_udc *udc,
60 struct usb_gadget_driver *driver);
61
Felipe Balbi2ccea032011-06-28 16:33:46 +030062/* ------------------------------------------------------------------------- */
63
Felipe Balbi5a8d6512016-05-31 13:07:47 +030064/**
65 * usb_ep_set_maxpacket_limit - set maximum packet size limit for endpoint
66 * @ep:the endpoint being configured
67 * @maxpacket_limit:value of maximum packet size limit
68 *
69 * This function should be used only in UDC drivers to initialize endpoint
70 * (usually in probe function).
71 */
72void usb_ep_set_maxpacket_limit(struct usb_ep *ep,
73 unsigned maxpacket_limit)
74{
75 ep->maxpacket_limit = maxpacket_limit;
76 ep->maxpacket = maxpacket_limit;
Felipe Balbi5e42d712016-05-31 13:39:21 +030077
78 trace_usb_ep_set_maxpacket_limit(ep, 0);
Felipe Balbi5a8d6512016-05-31 13:07:47 +030079}
80EXPORT_SYMBOL_GPL(usb_ep_set_maxpacket_limit);
81
82/**
83 * usb_ep_enable - configure endpoint, making it usable
84 * @ep:the endpoint being configured. may not be the endpoint named "ep0".
85 * drivers discover endpoints through the ep_list of a usb_gadget.
86 *
87 * When configurations are set, or when interface settings change, the driver
88 * will enable or disable the relevant endpoints. while it is enabled, an
89 * endpoint may be used for i/o until the driver receives a disconnect() from
90 * the host or until the endpoint is disabled.
91 *
92 * the ep0 implementation (which calls this routine) must ensure that the
93 * hardware capabilities of each endpoint match the descriptor provided
94 * for it. for example, an endpoint named "ep2in-bulk" would be usable
95 * for interrupt transfers as well as bulk, but it likely couldn't be used
96 * for iso transfers or for endpoint 14. some endpoints are fully
97 * configurable, with more generic names like "ep-a". (remember that for
98 * USB, "in" means "towards the USB master".)
99 *
100 * returns zero, or a negative error code.
101 */
102int usb_ep_enable(struct usb_ep *ep)
103{
Felipe Balbi5e42d712016-05-31 13:39:21 +0300104 int ret = 0;
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300105
106 if (ep->enabled)
Felipe Balbi5e42d712016-05-31 13:39:21 +0300107 goto out;
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300108
109 ret = ep->ops->enable(ep, ep->desc);
Colin Ian Kingf510b5a2016-07-25 22:57:36 +0100110 if (ret)
Felipe Balbi5e42d712016-05-31 13:39:21 +0300111 goto out;
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300112
113 ep->enabled = true;
114
Felipe Balbi5e42d712016-05-31 13:39:21 +0300115out:
116 trace_usb_ep_enable(ep, ret);
117
118 return ret;
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300119}
120EXPORT_SYMBOL_GPL(usb_ep_enable);
121
122/**
123 * usb_ep_disable - endpoint is no longer usable
124 * @ep:the endpoint being unconfigured. may not be the endpoint named "ep0".
125 *
126 * no other task may be using this endpoint when this is called.
127 * any pending and uncompleted requests will complete with status
128 * indicating disconnect (-ESHUTDOWN) before this call returns.
129 * gadget drivers must call usb_ep_enable() again before queueing
130 * requests to the endpoint.
131 *
132 * returns zero, or a negative error code.
133 */
134int usb_ep_disable(struct usb_ep *ep)
135{
Felipe Balbi5e42d712016-05-31 13:39:21 +0300136 int ret = 0;
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300137
138 if (!ep->enabled)
Felipe Balbi5e42d712016-05-31 13:39:21 +0300139 goto out;
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300140
141 ret = ep->ops->disable(ep);
Stefan Agner17af7982017-04-16 20:12:50 -0700142 if (ret)
Felipe Balbi5e42d712016-05-31 13:39:21 +0300143 goto out;
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300144
145 ep->enabled = false;
146
Felipe Balbi5e42d712016-05-31 13:39:21 +0300147out:
148 trace_usb_ep_disable(ep, ret);
149
150 return ret;
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300151}
152EXPORT_SYMBOL_GPL(usb_ep_disable);
153
154/**
155 * usb_ep_alloc_request - allocate a request object to use with this endpoint
156 * @ep:the endpoint to be used with with the request
157 * @gfp_flags:GFP_* flags to use
158 *
159 * Request objects must be allocated with this call, since they normally
160 * need controller-specific setup and may even need endpoint-specific
161 * resources such as allocation of DMA descriptors.
162 * Requests may be submitted with usb_ep_queue(), and receive a single
163 * completion callback. Free requests with usb_ep_free_request(), when
164 * they are no longer needed.
165 *
166 * Returns the request, or null if one could not be allocated.
167 */
168struct usb_request *usb_ep_alloc_request(struct usb_ep *ep,
169 gfp_t gfp_flags)
170{
Felipe Balbi5e42d712016-05-31 13:39:21 +0300171 struct usb_request *req = NULL;
172
173 req = ep->ops->alloc_request(ep, gfp_flags);
174
175 trace_usb_ep_alloc_request(ep, req, req ? 0 : -ENOMEM);
176
177 return req;
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300178}
179EXPORT_SYMBOL_GPL(usb_ep_alloc_request);
180
181/**
182 * usb_ep_free_request - frees a request object
183 * @ep:the endpoint associated with the request
184 * @req:the request being freed
185 *
186 * Reverses the effect of usb_ep_alloc_request().
187 * Caller guarantees the request is not queued, and that it will
188 * no longer be requeued (or otherwise used).
189 */
190void usb_ep_free_request(struct usb_ep *ep,
191 struct usb_request *req)
192{
193 ep->ops->free_request(ep, req);
Felipe Balbi5e42d712016-05-31 13:39:21 +0300194 trace_usb_ep_free_request(ep, req, 0);
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300195}
196EXPORT_SYMBOL_GPL(usb_ep_free_request);
197
198/**
199 * usb_ep_queue - queues (submits) an I/O request to an endpoint.
200 * @ep:the endpoint associated with the request
201 * @req:the request being submitted
202 * @gfp_flags: GFP_* flags to use in case the lower level driver couldn't
203 * pre-allocate all necessary memory with the request.
204 *
205 * This tells the device controller to perform the specified request through
206 * that endpoint (reading or writing a buffer). When the request completes,
207 * including being canceled by usb_ep_dequeue(), the request's completion
208 * routine is called to return the request to the driver. Any endpoint
209 * (except control endpoints like ep0) may have more than one transfer
210 * request queued; they complete in FIFO order. Once a gadget driver
211 * submits a request, that request may not be examined or modified until it
212 * is given back to that driver through the completion callback.
213 *
214 * Each request is turned into one or more packets. The controller driver
215 * never merges adjacent requests into the same packet. OUT transfers
216 * will sometimes use data that's already buffered in the hardware.
217 * Drivers can rely on the fact that the first byte of the request's buffer
218 * always corresponds to the first byte of some USB packet, for both
219 * IN and OUT transfers.
220 *
221 * Bulk endpoints can queue any amount of data; the transfer is packetized
222 * automatically. The last packet will be short if the request doesn't fill it
223 * out completely. Zero length packets (ZLPs) should be avoided in portable
224 * protocols since not all usb hardware can successfully handle zero length
225 * packets. (ZLPs may be explicitly written, and may be implicitly written if
226 * the request 'zero' flag is set.) Bulk endpoints may also be used
227 * for interrupt transfers; but the reverse is not true, and some endpoints
228 * won't support every interrupt transfer. (Such as 768 byte packets.)
229 *
230 * Interrupt-only endpoints are less functional than bulk endpoints, for
231 * example by not supporting queueing or not handling buffers that are
232 * larger than the endpoint's maxpacket size. They may also treat data
233 * toggle differently.
234 *
235 * Control endpoints ... after getting a setup() callback, the driver queues
236 * one response (even if it would be zero length). That enables the
237 * status ack, after transferring data as specified in the response. Setup
238 * functions may return negative error codes to generate protocol stalls.
239 * (Note that some USB device controllers disallow protocol stall responses
240 * in some cases.) When control responses are deferred (the response is
241 * written after the setup callback returns), then usb_ep_set_halt() may be
242 * used on ep0 to trigger protocol stalls. Depending on the controller,
243 * it may not be possible to trigger a status-stage protocol stall when the
244 * data stage is over, that is, from within the response's completion
245 * routine.
246 *
247 * For periodic endpoints, like interrupt or isochronous ones, the usb host
248 * arranges to poll once per interval, and the gadget driver usually will
249 * have queued some data to transfer at that time.
250 *
Felipe Balbiba260ce2018-03-26 13:14:46 +0300251 * Note that @req's ->complete() callback must never be called from
252 * within usb_ep_queue() as that can create deadlock situations.
253 *
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300254 * Returns zero, or a negative error code. Endpoints that are not enabled
255 * report errors; errors will also be
256 * reported when the usb peripheral is disconnected.
257 */
258int usb_ep_queue(struct usb_ep *ep,
259 struct usb_request *req, gfp_t gfp_flags)
260{
Felipe Balbi5e42d712016-05-31 13:39:21 +0300261 int ret = 0;
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300262
Felipe Balbi5e42d712016-05-31 13:39:21 +0300263 if (WARN_ON_ONCE(!ep->enabled && ep->address)) {
264 ret = -ESHUTDOWN;
265 goto out;
266 }
267
268 ret = ep->ops->queue(ep, req, gfp_flags);
269
270out:
271 trace_usb_ep_queue(ep, req, ret);
272
273 return ret;
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300274}
275EXPORT_SYMBOL_GPL(usb_ep_queue);
276
277/**
278 * usb_ep_dequeue - dequeues (cancels, unlinks) an I/O request from an endpoint
279 * @ep:the endpoint associated with the request
280 * @req:the request being canceled
281 *
282 * If the request is still active on the endpoint, it is dequeued and its
283 * completion routine is called (with status -ECONNRESET); else a negative
284 * error code is returned. This is guaranteed to happen before the call to
285 * usb_ep_dequeue() returns.
286 *
287 * Note that some hardware can't clear out write fifos (to unlink the request
288 * at the head of the queue) except as part of disconnecting from usb. Such
289 * restrictions prevent drivers from supporting configuration changes,
290 * even to configuration zero (a "chapter 9" requirement).
291 */
292int usb_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
293{
Felipe Balbi5e42d712016-05-31 13:39:21 +0300294 int ret;
295
296 ret = ep->ops->dequeue(ep, req);
297 trace_usb_ep_dequeue(ep, req, ret);
298
299 return ret;
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300300}
301EXPORT_SYMBOL_GPL(usb_ep_dequeue);
302
303/**
304 * usb_ep_set_halt - sets the endpoint halt feature.
305 * @ep: the non-isochronous endpoint being stalled
306 *
307 * Use this to stall an endpoint, perhaps as an error report.
308 * Except for control endpoints,
309 * the endpoint stays halted (will not stream any data) until the host
310 * clears this feature; drivers may need to empty the endpoint's request
311 * queue first, to make sure no inappropriate transfers happen.
312 *
313 * Note that while an endpoint CLEAR_FEATURE will be invisible to the
314 * gadget driver, a SET_INTERFACE will not be. To reset endpoints for the
315 * current altsetting, see usb_ep_clear_halt(). When switching altsettings,
316 * it's simplest to use usb_ep_enable() or usb_ep_disable() for the endpoints.
317 *
318 * Returns zero, or a negative error code. On success, this call sets
319 * underlying hardware state that blocks data transfers.
320 * Attempts to halt IN endpoints will fail (returning -EAGAIN) if any
321 * transfer requests are still queued, or if the controller hardware
322 * (usually a FIFO) still holds bytes that the host hasn't collected.
323 */
324int usb_ep_set_halt(struct usb_ep *ep)
325{
Felipe Balbi5e42d712016-05-31 13:39:21 +0300326 int ret;
327
328 ret = ep->ops->set_halt(ep, 1);
329 trace_usb_ep_set_halt(ep, ret);
330
331 return ret;
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300332}
333EXPORT_SYMBOL_GPL(usb_ep_set_halt);
334
335/**
336 * usb_ep_clear_halt - clears endpoint halt, and resets toggle
337 * @ep:the bulk or interrupt endpoint being reset
338 *
339 * Use this when responding to the standard usb "set interface" request,
340 * for endpoints that aren't reconfigured, after clearing any other state
341 * in the endpoint's i/o queue.
342 *
343 * Returns zero, or a negative error code. On success, this call clears
344 * the underlying hardware state reflecting endpoint halt and data toggle.
345 * Note that some hardware can't support this request (like pxa2xx_udc),
346 * and accordingly can't correctly implement interface altsettings.
347 */
348int usb_ep_clear_halt(struct usb_ep *ep)
349{
Felipe Balbi5e42d712016-05-31 13:39:21 +0300350 int ret;
351
352 ret = ep->ops->set_halt(ep, 0);
353 trace_usb_ep_clear_halt(ep, ret);
354
355 return ret;
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300356}
357EXPORT_SYMBOL_GPL(usb_ep_clear_halt);
358
359/**
360 * usb_ep_set_wedge - sets the halt feature and ignores clear requests
361 * @ep: the endpoint being wedged
362 *
363 * Use this to stall an endpoint and ignore CLEAR_FEATURE(HALT_ENDPOINT)
364 * requests. If the gadget driver clears the halt status, it will
365 * automatically unwedge the endpoint.
366 *
367 * Returns zero on success, else negative errno.
368 */
369int usb_ep_set_wedge(struct usb_ep *ep)
370{
Felipe Balbi5e42d712016-05-31 13:39:21 +0300371 int ret;
372
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300373 if (ep->ops->set_wedge)
Felipe Balbi5e42d712016-05-31 13:39:21 +0300374 ret = ep->ops->set_wedge(ep);
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300375 else
Felipe Balbi5e42d712016-05-31 13:39:21 +0300376 ret = ep->ops->set_halt(ep, 1);
377
378 trace_usb_ep_set_wedge(ep, ret);
379
380 return ret;
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300381}
382EXPORT_SYMBOL_GPL(usb_ep_set_wedge);
383
384/**
385 * usb_ep_fifo_status - returns number of bytes in fifo, or error
386 * @ep: the endpoint whose fifo status is being checked.
387 *
388 * FIFO endpoints may have "unclaimed data" in them in certain cases,
389 * such as after aborted transfers. Hosts may not have collected all
390 * the IN data written by the gadget driver (and reported by a request
391 * completion). The gadget driver may not have collected all the data
392 * written OUT to it by the host. Drivers that need precise handling for
393 * fault reporting or recovery may need to use this call.
394 *
395 * This returns the number of such bytes in the fifo, or a negative
396 * errno if the endpoint doesn't use a FIFO or doesn't support such
397 * precise handling.
398 */
399int usb_ep_fifo_status(struct usb_ep *ep)
400{
Felipe Balbi5e42d712016-05-31 13:39:21 +0300401 int ret;
402
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300403 if (ep->ops->fifo_status)
Felipe Balbi5e42d712016-05-31 13:39:21 +0300404 ret = ep->ops->fifo_status(ep);
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300405 else
Felipe Balbi5e42d712016-05-31 13:39:21 +0300406 ret = -EOPNOTSUPP;
407
408 trace_usb_ep_fifo_status(ep, ret);
409
410 return ret;
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300411}
412EXPORT_SYMBOL_GPL(usb_ep_fifo_status);
413
414/**
415 * usb_ep_fifo_flush - flushes contents of a fifo
416 * @ep: the endpoint whose fifo is being flushed.
417 *
418 * This call may be used to flush the "unclaimed data" that may exist in
419 * an endpoint fifo after abnormal transaction terminations. The call
420 * must never be used except when endpoint is not being used for any
421 * protocol translation.
422 */
423void usb_ep_fifo_flush(struct usb_ep *ep)
424{
425 if (ep->ops->fifo_flush)
426 ep->ops->fifo_flush(ep);
Felipe Balbi5e42d712016-05-31 13:39:21 +0300427
428 trace_usb_ep_fifo_flush(ep, 0);
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300429}
430EXPORT_SYMBOL_GPL(usb_ep_fifo_flush);
431
432/* ------------------------------------------------------------------------- */
433
434/**
435 * usb_gadget_frame_number - returns the current frame number
436 * @gadget: controller that reports the frame number
437 *
438 * Returns the usb frame number, normally eleven bits from a SOF packet,
439 * or negative errno if this device doesn't support this capability.
440 */
441int usb_gadget_frame_number(struct usb_gadget *gadget)
442{
Felipe Balbi5e42d712016-05-31 13:39:21 +0300443 int ret;
444
445 ret = gadget->ops->get_frame(gadget);
446
447 trace_usb_gadget_frame_number(gadget, ret);
448
449 return ret;
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300450}
451EXPORT_SYMBOL_GPL(usb_gadget_frame_number);
452
453/**
454 * usb_gadget_wakeup - tries to wake up the host connected to this gadget
455 * @gadget: controller used to wake up the host
456 *
457 * Returns zero on success, else negative error code if the hardware
458 * doesn't support such attempts, or its support has not been enabled
459 * by the usb host. Drivers must return device descriptors that report
460 * their ability to support this, or hosts won't enable it.
461 *
462 * This may also try to use SRP to wake the host and start enumeration,
463 * even if OTG isn't otherwise in use. OTG devices may also start
464 * remote wakeup even when hosts don't explicitly enable it.
465 */
466int usb_gadget_wakeup(struct usb_gadget *gadget)
467{
Felipe Balbi5e42d712016-05-31 13:39:21 +0300468 int ret = 0;
469
470 if (!gadget->ops->wakeup) {
471 ret = -EOPNOTSUPP;
472 goto out;
473 }
474
475 ret = gadget->ops->wakeup(gadget);
476
477out:
478 trace_usb_gadget_wakeup(gadget, ret);
479
480 return ret;
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300481}
482EXPORT_SYMBOL_GPL(usb_gadget_wakeup);
483
484/**
485 * usb_gadget_set_selfpowered - sets the device selfpowered feature.
486 * @gadget:the device being declared as self-powered
487 *
488 * this affects the device status reported by the hardware driver
489 * to reflect that it now has a local power supply.
490 *
491 * returns zero on success, else negative errno.
492 */
493int usb_gadget_set_selfpowered(struct usb_gadget *gadget)
494{
Felipe Balbi5e42d712016-05-31 13:39:21 +0300495 int ret = 0;
496
497 if (!gadget->ops->set_selfpowered) {
498 ret = -EOPNOTSUPP;
499 goto out;
500 }
501
502 ret = gadget->ops->set_selfpowered(gadget, 1);
503
504out:
505 trace_usb_gadget_set_selfpowered(gadget, ret);
506
507 return ret;
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300508}
509EXPORT_SYMBOL_GPL(usb_gadget_set_selfpowered);
510
511/**
512 * usb_gadget_clear_selfpowered - clear the device selfpowered feature.
513 * @gadget:the device being declared as bus-powered
514 *
515 * this affects the device status reported by the hardware driver.
516 * some hardware may not support bus-powered operation, in which
517 * case this feature's value can never change.
518 *
519 * returns zero on success, else negative errno.
520 */
521int usb_gadget_clear_selfpowered(struct usb_gadget *gadget)
522{
Felipe Balbi5e42d712016-05-31 13:39:21 +0300523 int ret = 0;
524
525 if (!gadget->ops->set_selfpowered) {
526 ret = -EOPNOTSUPP;
527 goto out;
528 }
529
530 ret = gadget->ops->set_selfpowered(gadget, 0);
531
532out:
533 trace_usb_gadget_clear_selfpowered(gadget, ret);
534
535 return ret;
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300536}
537EXPORT_SYMBOL_GPL(usb_gadget_clear_selfpowered);
538
539/**
540 * usb_gadget_vbus_connect - Notify controller that VBUS is powered
541 * @gadget:The device which now has VBUS power.
542 * Context: can sleep
543 *
544 * This call is used by a driver for an external transceiver (or GPIO)
545 * that detects a VBUS power session starting. Common responses include
546 * resuming the controller, activating the D+ (or D-) pullup to let the
547 * host detect that a USB device is attached, and starting to draw power
548 * (8mA or possibly more, especially after SET_CONFIGURATION).
549 *
550 * Returns zero on success, else negative errno.
551 */
552int usb_gadget_vbus_connect(struct usb_gadget *gadget)
553{
Felipe Balbi5e42d712016-05-31 13:39:21 +0300554 int ret = 0;
555
556 if (!gadget->ops->vbus_session) {
557 ret = -EOPNOTSUPP;
558 goto out;
559 }
560
561 ret = gadget->ops->vbus_session(gadget, 1);
562
563out:
564 trace_usb_gadget_vbus_connect(gadget, ret);
565
566 return ret;
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300567}
568EXPORT_SYMBOL_GPL(usb_gadget_vbus_connect);
569
570/**
571 * usb_gadget_vbus_draw - constrain controller's VBUS power usage
572 * @gadget:The device whose VBUS usage is being described
573 * @mA:How much current to draw, in milliAmperes. This should be twice
574 * the value listed in the configuration descriptor bMaxPower field.
575 *
576 * This call is used by gadget drivers during SET_CONFIGURATION calls,
577 * reporting how much power the device may consume. For example, this
578 * could affect how quickly batteries are recharged.
579 *
580 * Returns zero on success, else negative errno.
581 */
582int usb_gadget_vbus_draw(struct usb_gadget *gadget, unsigned mA)
583{
Felipe Balbi5e42d712016-05-31 13:39:21 +0300584 int ret = 0;
585
586 if (!gadget->ops->vbus_draw) {
587 ret = -EOPNOTSUPP;
588 goto out;
589 }
590
591 ret = gadget->ops->vbus_draw(gadget, mA);
592 if (!ret)
593 gadget->mA = mA;
594
595out:
596 trace_usb_gadget_vbus_draw(gadget, ret);
597
598 return ret;
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300599}
600EXPORT_SYMBOL_GPL(usb_gadget_vbus_draw);
601
602/**
603 * usb_gadget_vbus_disconnect - notify controller about VBUS session end
604 * @gadget:the device whose VBUS supply is being described
605 * Context: can sleep
606 *
607 * This call is used by a driver for an external transceiver (or GPIO)
608 * that detects a VBUS power session ending. Common responses include
609 * reversing everything done in usb_gadget_vbus_connect().
610 *
611 * Returns zero on success, else negative errno.
612 */
613int usb_gadget_vbus_disconnect(struct usb_gadget *gadget)
614{
Felipe Balbi5e42d712016-05-31 13:39:21 +0300615 int ret = 0;
616
617 if (!gadget->ops->vbus_session) {
618 ret = -EOPNOTSUPP;
619 goto out;
620 }
621
622 ret = gadget->ops->vbus_session(gadget, 0);
623
624out:
625 trace_usb_gadget_vbus_disconnect(gadget, ret);
626
627 return ret;
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300628}
629EXPORT_SYMBOL_GPL(usb_gadget_vbus_disconnect);
630
631/**
632 * usb_gadget_connect - software-controlled connect to USB host
633 * @gadget:the peripheral being connected
634 *
635 * Enables the D+ (or potentially D-) pullup. The host will start
636 * enumerating this gadget when the pullup is active and a VBUS session
637 * is active (the link is powered). This pullup is always enabled unless
638 * usb_gadget_disconnect() has been used to disable it.
639 *
640 * Returns zero on success, else negative errno.
641 */
642int usb_gadget_connect(struct usb_gadget *gadget)
643{
Felipe Balbi5e42d712016-05-31 13:39:21 +0300644 int ret = 0;
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300645
Felipe Balbi5e42d712016-05-31 13:39:21 +0300646 if (!gadget->ops->pullup) {
647 ret = -EOPNOTSUPP;
648 goto out;
649 }
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300650
651 if (gadget->deactivated) {
652 /*
653 * If gadget is deactivated we only save new state.
654 * Gadget will be connected automatically after activation.
655 */
656 gadget->connected = true;
Felipe Balbi5e42d712016-05-31 13:39:21 +0300657 goto out;
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300658 }
659
660 ret = gadget->ops->pullup(gadget, 1);
661 if (!ret)
662 gadget->connected = 1;
Felipe Balbi5e42d712016-05-31 13:39:21 +0300663
664out:
665 trace_usb_gadget_connect(gadget, ret);
666
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300667 return ret;
668}
669EXPORT_SYMBOL_GPL(usb_gadget_connect);
670
671/**
672 * usb_gadget_disconnect - software-controlled disconnect from USB host
673 * @gadget:the peripheral being disconnected
674 *
675 * Disables the D+ (or potentially D-) pullup, which the host may see
676 * as a disconnect (when a VBUS session is active). Not all systems
677 * support software pullup controls.
678 *
679 * Returns zero on success, else negative errno.
680 */
681int usb_gadget_disconnect(struct usb_gadget *gadget)
682{
Felipe Balbi5e42d712016-05-31 13:39:21 +0300683 int ret = 0;
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300684
Felipe Balbi5e42d712016-05-31 13:39:21 +0300685 if (!gadget->ops->pullup) {
686 ret = -EOPNOTSUPP;
687 goto out;
688 }
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300689
690 if (gadget->deactivated) {
691 /*
692 * If gadget is deactivated we only save new state.
693 * Gadget will stay disconnected after activation.
694 */
695 gadget->connected = false;
Felipe Balbi5e42d712016-05-31 13:39:21 +0300696 goto out;
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300697 }
698
699 ret = gadget->ops->pullup(gadget, 0);
700 if (!ret)
701 gadget->connected = 0;
Felipe Balbi5e42d712016-05-31 13:39:21 +0300702
703out:
704 trace_usb_gadget_disconnect(gadget, ret);
705
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300706 return ret;
707}
708EXPORT_SYMBOL_GPL(usb_gadget_disconnect);
709
710/**
711 * usb_gadget_deactivate - deactivate function which is not ready to work
712 * @gadget: the peripheral being deactivated
713 *
714 * This routine may be used during the gadget driver bind() call to prevent
715 * the peripheral from ever being visible to the USB host, unless later
716 * usb_gadget_activate() is called. For example, user mode components may
717 * need to be activated before the system can talk to hosts.
718 *
719 * Returns zero on success, else negative errno.
720 */
721int usb_gadget_deactivate(struct usb_gadget *gadget)
722{
Felipe Balbi5e42d712016-05-31 13:39:21 +0300723 int ret = 0;
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300724
725 if (gadget->deactivated)
Felipe Balbi5e42d712016-05-31 13:39:21 +0300726 goto out;
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300727
728 if (gadget->connected) {
729 ret = usb_gadget_disconnect(gadget);
730 if (ret)
Felipe Balbi5e42d712016-05-31 13:39:21 +0300731 goto out;
732
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300733 /*
734 * If gadget was being connected before deactivation, we want
735 * to reconnect it in usb_gadget_activate().
736 */
737 gadget->connected = true;
738 }
739 gadget->deactivated = true;
740
Felipe Balbi5e42d712016-05-31 13:39:21 +0300741out:
742 trace_usb_gadget_deactivate(gadget, ret);
743
744 return ret;
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300745}
746EXPORT_SYMBOL_GPL(usb_gadget_deactivate);
747
748/**
749 * usb_gadget_activate - activate function which is not ready to work
750 * @gadget: the peripheral being activated
751 *
752 * This routine activates gadget which was previously deactivated with
753 * usb_gadget_deactivate() call. It calls usb_gadget_connect() if needed.
754 *
755 * Returns zero on success, else negative errno.
756 */
757int usb_gadget_activate(struct usb_gadget *gadget)
758{
Felipe Balbi5e42d712016-05-31 13:39:21 +0300759 int ret = 0;
760
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300761 if (!gadget->deactivated)
Felipe Balbi5e42d712016-05-31 13:39:21 +0300762 goto out;
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300763
764 gadget->deactivated = false;
765
766 /*
767 * If gadget has been connected before deactivation, or became connected
768 * while it was being deactivated, we call usb_gadget_connect().
769 */
770 if (gadget->connected)
Felipe Balbi5e42d712016-05-31 13:39:21 +0300771 ret = usb_gadget_connect(gadget);
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300772
Felipe Balbi5e42d712016-05-31 13:39:21 +0300773out:
774 trace_usb_gadget_activate(gadget, ret);
775
776 return ret;
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300777}
778EXPORT_SYMBOL_GPL(usb_gadget_activate);
779
780/* ------------------------------------------------------------------------- */
781
Alan Stern908b9612013-07-12 11:06:21 -0400782#ifdef CONFIG_HAS_DMA
783
Yoshihiro Shimoda679ca392016-04-18 16:53:39 +0900784int usb_gadget_map_request_by_dev(struct device *dev,
Felipe Balbia6989082011-12-15 13:31:48 +0200785 struct usb_request *req, int is_in)
786{
787 if (req->length == 0)
788 return 0;
789
790 if (req->num_sgs) {
791 int mapped;
792
Yoshihiro Shimoda7ace8fc2015-07-13 18:10:05 +0900793 mapped = dma_map_sg(dev, req->sg, req->num_sgs,
Felipe Balbia6989082011-12-15 13:31:48 +0200794 is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
795 if (mapped == 0) {
Yoshihiro Shimoda5096c4d2016-04-18 16:53:38 +0900796 dev_err(dev, "failed to map SGs\n");
Felipe Balbia6989082011-12-15 13:31:48 +0200797 return -EFAULT;
798 }
799
800 req->num_mapped_sgs = mapped;
801 } else {
Yoshihiro Shimoda7ace8fc2015-07-13 18:10:05 +0900802 req->dma = dma_map_single(dev, req->buf, req->length,
Felipe Balbia6989082011-12-15 13:31:48 +0200803 is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
804
Yoshihiro Shimoda7ace8fc2015-07-13 18:10:05 +0900805 if (dma_mapping_error(dev, req->dma)) {
806 dev_err(dev, "failed to map buffer\n");
Felipe Balbia6989082011-12-15 13:31:48 +0200807 return -EFAULT;
808 }
809 }
810
811 return 0;
812}
Yoshihiro Shimoda679ca392016-04-18 16:53:39 +0900813EXPORT_SYMBOL_GPL(usb_gadget_map_request_by_dev);
814
815int usb_gadget_map_request(struct usb_gadget *gadget,
816 struct usb_request *req, int is_in)
817{
818 return usb_gadget_map_request_by_dev(gadget->dev.parent, req, is_in);
819}
Felipe Balbia6989082011-12-15 13:31:48 +0200820EXPORT_SYMBOL_GPL(usb_gadget_map_request);
821
Yoshihiro Shimoda679ca392016-04-18 16:53:39 +0900822void usb_gadget_unmap_request_by_dev(struct device *dev,
Felipe Balbia6989082011-12-15 13:31:48 +0200823 struct usb_request *req, int is_in)
824{
825 if (req->length == 0)
826 return;
827
828 if (req->num_mapped_sgs) {
Felipe Balbi23fd5372016-08-24 14:33:27 +0300829 dma_unmap_sg(dev, req->sg, req->num_sgs,
Felipe Balbia6989082011-12-15 13:31:48 +0200830 is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
831
832 req->num_mapped_sgs = 0;
833 } else {
Yoshihiro Shimoda679ca392016-04-18 16:53:39 +0900834 dma_unmap_single(dev, req->dma, req->length,
Felipe Balbia6989082011-12-15 13:31:48 +0200835 is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
836 }
837}
Yoshihiro Shimoda679ca392016-04-18 16:53:39 +0900838EXPORT_SYMBOL_GPL(usb_gadget_unmap_request_by_dev);
839
840void usb_gadget_unmap_request(struct usb_gadget *gadget,
841 struct usb_request *req, int is_in)
842{
843 usb_gadget_unmap_request_by_dev(gadget->dev.parent, req, is_in);
844}
Felipe Balbia6989082011-12-15 13:31:48 +0200845EXPORT_SYMBOL_GPL(usb_gadget_unmap_request);
846
Alan Stern908b9612013-07-12 11:06:21 -0400847#endif /* CONFIG_HAS_DMA */
848
Felipe Balbia6989082011-12-15 13:31:48 +0200849/* ------------------------------------------------------------------------- */
850
Michal Sojka3fc2aa52014-09-24 22:43:18 +0200851/**
852 * usb_gadget_giveback_request - give the request back to the gadget layer
853 * Context: in_interrupt()
854 *
855 * This is called by device controller drivers in order to return the
856 * completed request back to the gadget layer.
857 */
858void usb_gadget_giveback_request(struct usb_ep *ep,
859 struct usb_request *req)
860{
Michal Sojka0cfbd322014-09-24 22:43:21 +0200861 if (likely(req->status == 0))
862 usb_led_activity(USB_LED_EVENT_GADGET);
863
Felipe Balbi5e42d712016-05-31 13:39:21 +0300864 trace_usb_gadget_giveback_request(ep, req, 0);
865
Michal Sojka3fc2aa52014-09-24 22:43:18 +0200866 req->complete(ep, req);
867}
868EXPORT_SYMBOL_GPL(usb_gadget_giveback_request);
869
870/* ------------------------------------------------------------------------- */
871
Robert Baldygab0aea002015-08-06 14:11:12 +0200872/**
873 * gadget_find_ep_by_name - returns ep whose name is the same as sting passed
874 * in second parameter or NULL if searched endpoint not found
875 * @g: controller to check for quirk
876 * @name: name of searched endpoint
877 */
878struct usb_ep *gadget_find_ep_by_name(struct usb_gadget *g, const char *name)
879{
880 struct usb_ep *ep;
881
882 gadget_for_each_ep(ep, g) {
883 if (!strcmp(ep->name, name))
884 return ep;
885 }
886
887 return NULL;
888}
889EXPORT_SYMBOL_GPL(gadget_find_ep_by_name);
890
891/* ------------------------------------------------------------------------- */
892
Robert Baldyga4278c682015-08-06 14:11:11 +0200893int usb_gadget_ep_match_desc(struct usb_gadget *gadget,
894 struct usb_ep *ep, struct usb_endpoint_descriptor *desc,
895 struct usb_ss_ep_comp_descriptor *ep_comp)
896{
897 u8 type;
898 u16 max;
899 int num_req_streams = 0;
900
901 /* endpoint already claimed? */
902 if (ep->claimed)
903 return 0;
904
905 type = usb_endpoint_type(desc);
906 max = 0x7ff & usb_endpoint_maxp(desc);
907
908 if (usb_endpoint_dir_in(desc) && !ep->caps.dir_in)
909 return 0;
910 if (usb_endpoint_dir_out(desc) && !ep->caps.dir_out)
911 return 0;
912
913 if (max > ep->maxpacket_limit)
914 return 0;
915
916 /* "high bandwidth" works only at high speed */
Benjamin Herrenschmidt57d4bb12018-01-12 17:50:02 +1100917 if (!gadget_is_dualspeed(gadget) && usb_endpoint_maxp_mult(desc) > 1)
Robert Baldyga4278c682015-08-06 14:11:11 +0200918 return 0;
919
920 switch (type) {
921 case USB_ENDPOINT_XFER_CONTROL:
922 /* only support ep0 for portable CONTROL traffic */
923 return 0;
924 case USB_ENDPOINT_XFER_ISOC:
925 if (!ep->caps.type_iso)
926 return 0;
927 /* ISO: limit 1023 bytes full speed, 1024 high/super speed */
928 if (!gadget_is_dualspeed(gadget) && max > 1023)
929 return 0;
930 break;
931 case USB_ENDPOINT_XFER_BULK:
932 if (!ep->caps.type_bulk)
933 return 0;
934 if (ep_comp && gadget_is_superspeed(gadget)) {
935 /* Get the number of required streams from the
936 * EP companion descriptor and see if the EP
937 * matches it
938 */
939 num_req_streams = ep_comp->bmAttributes & 0x1f;
940 if (num_req_streams > ep->max_streams)
941 return 0;
942 }
943 break;
944 case USB_ENDPOINT_XFER_INT:
945 /* Bulk endpoints handle interrupt transfers,
946 * except the toggle-quirky iso-synch kind
947 */
948 if (!ep->caps.type_int && !ep->caps.type_bulk)
949 return 0;
950 /* INT: limit 64 bytes full speed, 1024 high/super speed */
951 if (!gadget_is_dualspeed(gadget) && max > 64)
952 return 0;
953 break;
954 }
955
956 return 1;
957}
958EXPORT_SYMBOL_GPL(usb_gadget_ep_match_desc);
959
960/* ------------------------------------------------------------------------- */
961
Felipe Balbi5702f752013-07-17 11:09:49 +0300962static void usb_gadget_state_work(struct work_struct *work)
963{
Peter Chendfea9c92015-03-06 10:36:02 +0800964 struct usb_gadget *gadget = work_to_gadget(work);
965 struct usb_udc *udc = gadget->udc;
Felipe Balbi5702f752013-07-17 11:09:49 +0300966
Peter Chendfea9c92015-03-06 10:36:02 +0800967 if (udc)
968 sysfs_notify(&udc->dev.kobj, NULL, "state");
Felipe Balbi5702f752013-07-17 11:09:49 +0300969}
970
Felipe Balbi49401f42011-12-19 12:57:04 +0200971void usb_gadget_set_state(struct usb_gadget *gadget,
972 enum usb_device_state state)
973{
974 gadget->state = state;
Felipe Balbi5702f752013-07-17 11:09:49 +0300975 schedule_work(&gadget->work);
Felipe Balbi49401f42011-12-19 12:57:04 +0200976}
977EXPORT_SYMBOL_GPL(usb_gadget_set_state);
978
979/* ------------------------------------------------------------------------- */
980
Peter Chen628ef0d2015-03-06 10:36:03 +0800981static void usb_udc_connect_control(struct usb_udc *udc)
982{
983 if (udc->vbus)
984 usb_gadget_connect(udc->gadget);
985 else
986 usb_gadget_disconnect(udc->gadget);
987}
988
989/**
990 * usb_udc_vbus_handler - updates the udc core vbus status, and try to
991 * connect or disconnect gadget
992 * @gadget: The gadget which vbus change occurs
993 * @status: The vbus status
994 *
995 * The udc driver calls it when it wants to connect or disconnect gadget
996 * according to vbus status.
997 */
998void usb_udc_vbus_handler(struct usb_gadget *gadget, bool status)
999{
1000 struct usb_udc *udc = gadget->udc;
1001
1002 if (udc) {
1003 udc->vbus = status;
1004 usb_udc_connect_control(udc);
1005 }
1006}
1007EXPORT_SYMBOL_GPL(usb_udc_vbus_handler);
1008
Felipe Balbi2ccea032011-06-28 16:33:46 +03001009/**
Peter Chen974a70b2014-09-12 09:32:41 +08001010 * usb_gadget_udc_reset - notifies the udc core that bus reset occurs
1011 * @gadget: The gadget which bus reset occurs
1012 * @driver: The gadget driver we want to notify
1013 *
1014 * If the udc driver has bus reset handler, it needs to call this when the bus
1015 * reset occurs, it notifies the gadget driver that the bus reset occurs as
1016 * well as updates gadget state.
1017 */
1018void usb_gadget_udc_reset(struct usb_gadget *gadget,
1019 struct usb_gadget_driver *driver)
1020{
1021 driver->reset(gadget);
1022 usb_gadget_set_state(gadget, USB_STATE_DEFAULT);
1023}
1024EXPORT_SYMBOL_GPL(usb_gadget_udc_reset);
1025
1026/**
Sebastian Andrzej Siewior352c2dc2011-06-23 14:26:15 +02001027 * usb_gadget_udc_start - tells usb device controller to start up
Felipe Balbi2c683342014-10-17 11:34:07 -05001028 * @udc: The UDC to be started
Sebastian Andrzej Siewior352c2dc2011-06-23 14:26:15 +02001029 *
1030 * This call is issued by the UDC Class driver when it's about
1031 * to register a gadget driver to the device controller, before
1032 * calling gadget driver's bind() method.
1033 *
1034 * It allows the controller to be powered off until strictly
1035 * necessary to have it powered on.
1036 *
1037 * Returns zero on success, else negative errno.
1038 */
Felipe Balbi2c683342014-10-17 11:34:07 -05001039static inline int usb_gadget_udc_start(struct usb_udc *udc)
Sebastian Andrzej Siewior352c2dc2011-06-23 14:26:15 +02001040{
Felipe Balbi2c683342014-10-17 11:34:07 -05001041 return udc->gadget->ops->udc_start(udc->gadget, udc->driver);
Sebastian Andrzej Siewior352c2dc2011-06-23 14:26:15 +02001042}
1043
1044/**
Sebastian Andrzej Siewior352c2dc2011-06-23 14:26:15 +02001045 * usb_gadget_udc_stop - tells usb device controller we don't need it anymore
1046 * @gadget: The device we want to stop activity
1047 * @driver: The driver to unbind from @gadget
1048 *
1049 * This call is issued by the UDC Class driver after calling
1050 * gadget driver's unbind() method.
1051 *
1052 * The details are implementation specific, but it can go as
1053 * far as powering off UDC completely and disable its data
1054 * line pullups.
1055 */
Felipe Balbi2c683342014-10-17 11:34:07 -05001056static inline void usb_gadget_udc_stop(struct usb_udc *udc)
Sebastian Andrzej Siewior352c2dc2011-06-23 14:26:15 +02001057{
Felipe Balbi22835b82014-10-17 12:05:12 -05001058 udc->gadget->ops->udc_stop(udc->gadget);
Sebastian Andrzej Siewior352c2dc2011-06-23 14:26:15 +02001059}
1060
1061/**
Felipe Balbi2ccea032011-06-28 16:33:46 +03001062 * usb_udc_release - release the usb_udc struct
1063 * @dev: the dev member within usb_udc
1064 *
1065 * This is called by driver's core in order to free memory once the last
1066 * reference is released.
1067 */
1068static void usb_udc_release(struct device *dev)
1069{
1070 struct usb_udc *udc;
1071
1072 udc = container_of(dev, struct usb_udc, dev);
1073 dev_dbg(dev, "releasing '%s'\n", dev_name(dev));
1074 kfree(udc);
1075}
1076
Sebastian Andrzej Siewior019f9762011-06-23 14:28:38 +02001077static const struct attribute_group *usb_udc_attr_groups[];
Felipe Balbi792bfcf2013-02-26 14:47:44 +02001078
1079static void usb_udc_nop_release(struct device *dev)
1080{
1081 dev_vdbg(dev, "%s\n", __func__);
1082}
1083
Krzysztof Opasiak881b5222017-01-16 08:40:57 +01001084/* should be called with udc_lock held */
1085static int check_pending_gadget_drivers(struct usb_udc *udc)
1086{
1087 struct usb_gadget_driver *driver;
1088 int ret = 0;
1089
1090 list_for_each_entry(driver, &gadget_driver_pending_list, pending)
1091 if (!driver->udc_name || strcmp(driver->udc_name,
1092 dev_name(&udc->dev)) == 0) {
1093 ret = udc_bind_to_driver(udc, driver);
1094 if (ret != -EPROBE_DEFER)
1095 list_del(&driver->pending);
1096 break;
1097 }
1098
1099 return ret;
1100}
1101
Felipe Balbi2ccea032011-06-28 16:33:46 +03001102/**
Felipe Balbi792bfcf2013-02-26 14:47:44 +02001103 * usb_add_gadget_udc_release - adds a new gadget to the udc class driver list
1104 * @parent: the parent device to this udc. Usually the controller driver's
1105 * device.
1106 * @gadget: the gadget to be added to the list.
1107 * @release: a gadget release function.
Felipe Balbi2ccea032011-06-28 16:33:46 +03001108 *
1109 * Returns zero on success, negative errno otherwise.
1110 */
Felipe Balbi792bfcf2013-02-26 14:47:44 +02001111int usb_add_gadget_udc_release(struct device *parent, struct usb_gadget *gadget,
1112 void (*release)(struct device *dev))
Felipe Balbi2ccea032011-06-28 16:33:46 +03001113{
1114 struct usb_udc *udc;
1115 int ret = -ENOMEM;
1116
1117 udc = kzalloc(sizeof(*udc), GFP_KERNEL);
1118 if (!udc)
1119 goto err1;
1120
Felipe Balbi7bce4012013-01-24 17:41:00 +02001121 dev_set_name(&gadget->dev, "gadget");
Felipe Balbi5702f752013-07-17 11:09:49 +03001122 INIT_WORK(&gadget->work, usb_gadget_state_work);
Felipe Balbi2ed14322013-02-26 10:59:41 +02001123 gadget->dev.parent = parent;
Felipe Balbif07bd56b2013-01-24 14:52:24 +02001124
Felipe Balbiddf47cc2013-02-26 15:25:41 +02001125 if (release)
Felipe Balbi792bfcf2013-02-26 14:47:44 +02001126 gadget->dev.release = release;
Felipe Balbiddf47cc2013-02-26 15:25:41 +02001127 else
1128 gadget->dev.release = usb_udc_nop_release;
Felipe Balbi792bfcf2013-02-26 14:47:44 +02001129
Felipe Balbi7bce4012013-01-24 17:41:00 +02001130 ret = device_register(&gadget->dev);
1131 if (ret)
1132 goto err2;
Felipe Balbif07bd56b2013-01-24 14:52:24 +02001133
Felipe Balbi2ccea032011-06-28 16:33:46 +03001134 device_initialize(&udc->dev);
1135 udc->dev.release = usb_udc_release;
1136 udc->dev.class = udc_class;
Sebastian Andrzej Siewior019f9762011-06-23 14:28:38 +02001137 udc->dev.groups = usb_udc_attr_groups;
Felipe Balbi2ccea032011-06-28 16:33:46 +03001138 udc->dev.parent = parent;
1139 ret = dev_set_name(&udc->dev, "%s", kobject_name(&parent->kobj));
1140 if (ret)
Felipe Balbif07bd56b2013-01-24 14:52:24 +02001141 goto err3;
Felipe Balbi2ccea032011-06-28 16:33:46 +03001142
1143 udc->gadget = gadget;
Peter Chendfea9c92015-03-06 10:36:02 +08001144 gadget->udc = udc;
Felipe Balbi2ccea032011-06-28 16:33:46 +03001145
1146 mutex_lock(&udc_lock);
1147 list_add_tail(&udc->list, &udc_list);
1148
1149 ret = device_add(&udc->dev);
1150 if (ret)
Felipe Balbif07bd56b2013-01-24 14:52:24 +02001151 goto err4;
Felipe Balbi2ccea032011-06-28 16:33:46 +03001152
Felipe Balbi49401f42011-12-19 12:57:04 +02001153 usb_gadget_set_state(gadget, USB_STATE_NOTATTACHED);
Peter Chen628ef0d2015-03-06 10:36:03 +08001154 udc->vbus = true;
Felipe Balbi49401f42011-12-19 12:57:04 +02001155
Ruslan Bilovol855ed042015-11-23 09:56:38 +01001156 /* pick up one of pending gadget drivers */
Krzysztof Opasiak881b5222017-01-16 08:40:57 +01001157 ret = check_pending_gadget_drivers(udc);
1158 if (ret)
1159 goto err5;
Ruslan Bilovol855ed042015-11-23 09:56:38 +01001160
Felipe Balbi2ccea032011-06-28 16:33:46 +03001161 mutex_unlock(&udc_lock);
1162
1163 return 0;
Felipe Balbif07bd56b2013-01-24 14:52:24 +02001164
Peter Chen17a1dc52016-07-11 09:58:16 +08001165err5:
1166 device_del(&udc->dev);
1167
Felipe Balbif07bd56b2013-01-24 14:52:24 +02001168err4:
Felipe Balbi2ccea032011-06-28 16:33:46 +03001169 list_del(&udc->list);
1170 mutex_unlock(&udc_lock);
1171
Felipe Balbif07bd56b2013-01-24 14:52:24 +02001172err3:
Felipe Balbi2ccea032011-06-28 16:33:46 +03001173 put_device(&udc->dev);
Alan Sternc93e64e2015-01-16 11:32:51 -05001174 device_del(&gadget->dev);
Felipe Balbi2ccea032011-06-28 16:33:46 +03001175
Felipe Balbif07bd56b2013-01-24 14:52:24 +02001176err2:
Felipe Balbi7bce4012013-01-24 17:41:00 +02001177 put_device(&gadget->dev);
Felipe Balbic5dbc222013-04-02 17:06:28 +03001178 kfree(udc);
Felipe Balbi7bce4012013-01-24 17:41:00 +02001179
Felipe Balbi2ccea032011-06-28 16:33:46 +03001180err1:
1181 return ret;
1182}
Felipe Balbi792bfcf2013-02-26 14:47:44 +02001183EXPORT_SYMBOL_GPL(usb_add_gadget_udc_release);
1184
Felipe Balbi2ccea032011-06-28 16:33:46 +03001185/**
Marek Szyprowski175f7122016-02-18 11:34:43 +01001186 * usb_get_gadget_udc_name - get the name of the first UDC controller
1187 * This functions returns the name of the first UDC controller in the system.
1188 * Please note that this interface is usefull only for legacy drivers which
1189 * assume that there is only one UDC controller in the system and they need to
1190 * get its name before initialization. There is no guarantee that the UDC
1191 * of the returned name will be still available, when gadget driver registers
1192 * itself.
1193 *
1194 * Returns pointer to string with UDC controller name on success, NULL
1195 * otherwise. Caller should kfree() returned string.
1196 */
1197char *usb_get_gadget_udc_name(void)
1198{
1199 struct usb_udc *udc;
1200 char *name = NULL;
1201
1202 /* For now we take the first available UDC */
1203 mutex_lock(&udc_lock);
1204 list_for_each_entry(udc, &udc_list, list) {
1205 if (!udc->driver) {
1206 name = kstrdup(udc->gadget->name, GFP_KERNEL);
1207 break;
1208 }
1209 }
1210 mutex_unlock(&udc_lock);
1211 return name;
1212}
1213EXPORT_SYMBOL_GPL(usb_get_gadget_udc_name);
1214
1215/**
Felipe Balbi2ccea032011-06-28 16:33:46 +03001216 * usb_add_gadget_udc - adds a new gadget to the udc class driver list
1217 * @parent: the parent device to this udc. Usually the controller
1218 * driver's device.
1219 * @gadget: the gadget to be added to the list
1220 *
1221 * Returns zero on success, negative errno otherwise.
1222 */
1223int usb_add_gadget_udc(struct device *parent, struct usb_gadget *gadget)
1224{
Felipe Balbi792bfcf2013-02-26 14:47:44 +02001225 return usb_add_gadget_udc_release(parent, gadget, NULL);
Felipe Balbi2ccea032011-06-28 16:33:46 +03001226}
1227EXPORT_SYMBOL_GPL(usb_add_gadget_udc);
1228
1229static void usb_gadget_remove_driver(struct usb_udc *udc)
1230{
1231 dev_dbg(&udc->dev, "unregistering UDC driver [%s]\n",
Felipe Balbi8da9fe82014-10-17 11:54:46 -05001232 udc->driver->function);
Felipe Balbi2ccea032011-06-28 16:33:46 +03001233
1234 kobject_uevent(&udc->dev.kobj, KOBJ_CHANGE);
1235
Felipe Balbi2d7ebbb2013-01-24 11:00:15 +02001236 usb_gadget_disconnect(udc->gadget);
1237 udc->driver->disconnect(udc->gadget);
1238 udc->driver->unbind(udc->gadget);
Felipe Balbi2c683342014-10-17 11:34:07 -05001239 usb_gadget_udc_stop(udc);
Felipe Balbi2ccea032011-06-28 16:33:46 +03001240
1241 udc->driver = NULL;
1242 udc->dev.driver = NULL;
Felipe Balbi70d3a492013-02-26 13:51:24 +02001243 udc->gadget->dev.driver = NULL;
Felipe Balbi2ccea032011-06-28 16:33:46 +03001244}
1245
1246/**
1247 * usb_del_gadget_udc - deletes @udc from udc_list
1248 * @gadget: the gadget to be removed.
1249 *
1250 * This, will call usb_gadget_unregister_driver() if
1251 * the @udc is still busy.
1252 */
1253void usb_del_gadget_udc(struct usb_gadget *gadget)
1254{
Peter Chendfea9c92015-03-06 10:36:02 +08001255 struct usb_udc *udc = gadget->udc;
Felipe Balbi2ccea032011-06-28 16:33:46 +03001256
Peter Chendfea9c92015-03-06 10:36:02 +08001257 if (!udc)
1258 return;
Felipe Balbi2ccea032011-06-28 16:33:46 +03001259
Felipe Balbi2ccea032011-06-28 16:33:46 +03001260 dev_vdbg(gadget->dev.parent, "unregistering gadget\n");
1261
Peter Chendfea9c92015-03-06 10:36:02 +08001262 mutex_lock(&udc_lock);
Felipe Balbi2ccea032011-06-28 16:33:46 +03001263 list_del(&udc->list);
Felipe Balbi2ccea032011-06-28 16:33:46 +03001264
Ruslan Bilovol855ed042015-11-23 09:56:38 +01001265 if (udc->driver) {
1266 struct usb_gadget_driver *driver = udc->driver;
1267
Felipe Balbi2ccea032011-06-28 16:33:46 +03001268 usb_gadget_remove_driver(udc);
Ruslan Bilovol855ed042015-11-23 09:56:38 +01001269 list_add(&driver->pending, &gadget_driver_pending_list);
1270 }
1271 mutex_unlock(&udc_lock);
Felipe Balbi2ccea032011-06-28 16:33:46 +03001272
1273 kobject_uevent(&udc->dev.kobj, KOBJ_REMOVE);
Felipe Balbi5702f752013-07-17 11:09:49 +03001274 flush_work(&gadget->work);
Felipe Balbi2ccea032011-06-28 16:33:46 +03001275 device_unregister(&udc->dev);
Felipe Balbi7bce4012013-01-24 17:41:00 +02001276 device_unregister(&gadget->dev);
Felipe Balbi2ccea032011-06-28 16:33:46 +03001277}
1278EXPORT_SYMBOL_GPL(usb_del_gadget_udc);
1279
1280/* ------------------------------------------------------------------------- */
1281
Sebastian Andrzej Siewior4c49a5f2012-12-23 21:10:19 +01001282static int udc_bind_to_driver(struct usb_udc *udc, struct usb_gadget_driver *driver)
Felipe Balbi2ccea032011-06-28 16:33:46 +03001283{
Sebastian Andrzej Siewior4c49a5f2012-12-23 21:10:19 +01001284 int ret;
Felipe Balbi2ccea032011-06-28 16:33:46 +03001285
Felipe Balbi2ccea032011-06-28 16:33:46 +03001286 dev_dbg(&udc->dev, "registering UDC driver [%s]\n",
1287 driver->function);
1288
1289 udc->driver = driver;
1290 udc->dev.driver = &driver->driver;
Felipe Balbi70d3a492013-02-26 13:51:24 +02001291 udc->gadget->dev.driver = &driver->driver;
Felipe Balbi2ccea032011-06-28 16:33:46 +03001292
Felipe Balbi2d7ebbb2013-01-24 11:00:15 +02001293 ret = driver->bind(udc->gadget, driver);
1294 if (ret)
1295 goto err1;
Felipe Balbi2c683342014-10-17 11:34:07 -05001296 ret = usb_gadget_udc_start(udc);
Felipe Balbi2d7ebbb2013-01-24 11:00:15 +02001297 if (ret) {
1298 driver->unbind(udc->gadget);
1299 goto err1;
Sebastian Andrzej Siewior352c2dc2011-06-23 14:26:15 +02001300 }
Peter Chen628ef0d2015-03-06 10:36:03 +08001301 usb_udc_connect_control(udc);
Felipe Balbi2ccea032011-06-28 16:33:46 +03001302
1303 kobject_uevent(&udc->dev.kobj, KOBJ_CHANGE);
Felipe Balbi2ccea032011-06-28 16:33:46 +03001304 return 0;
Felipe Balbi2ccea032011-06-28 16:33:46 +03001305err1:
Fabio Estevamf8cffc82013-09-28 00:05:34 -03001306 if (ret != -EISNAM)
1307 dev_err(&udc->dev, "failed to start %s: %d\n",
Felipe Balbi2ccea032011-06-28 16:33:46 +03001308 udc->driver->function, ret);
1309 udc->driver = NULL;
1310 udc->dev.driver = NULL;
Felipe Balbi70d3a492013-02-26 13:51:24 +02001311 udc->gadget->dev.driver = NULL;
Sebastian Andrzej Siewior4c49a5f2012-12-23 21:10:19 +01001312 return ret;
1313}
1314
Sebastian Andrzej Siewior4c49a5f2012-12-23 21:10:19 +01001315int usb_gadget_probe_driver(struct usb_gadget_driver *driver)
1316{
1317 struct usb_udc *udc = NULL;
Ruslan Bilovol2284b292015-11-23 09:56:35 +01001318 int ret = -ENODEV;
Sebastian Andrzej Siewior4c49a5f2012-12-23 21:10:19 +01001319
1320 if (!driver || !driver->bind || !driver->setup)
1321 return -EINVAL;
1322
1323 mutex_lock(&udc_lock);
Ruslan Bilovol2284b292015-11-23 09:56:35 +01001324 if (driver->udc_name) {
1325 list_for_each_entry(udc, &udc_list, list) {
1326 ret = strcmp(driver->udc_name, dev_name(&udc->dev));
1327 if (!ret)
1328 break;
1329 }
Felix Hädicke64244ed2016-12-29 23:02:11 +01001330 if (ret)
1331 ret = -ENODEV;
1332 else if (udc->driver)
1333 ret = -EBUSY;
1334 else
Sebastian Andrzej Siewior4c49a5f2012-12-23 21:10:19 +01001335 goto found;
Ruslan Bilovol2284b292015-11-23 09:56:35 +01001336 } else {
1337 list_for_each_entry(udc, &udc_list, list) {
1338 /* For now we take the first one */
1339 if (!udc->driver)
1340 goto found;
1341 }
Sebastian Andrzej Siewior4c49a5f2012-12-23 21:10:19 +01001342 }
1343
Krzysztof Opasiakf1bddbb2016-05-05 10:46:05 +02001344 if (!driver->match_existing_only) {
1345 list_add_tail(&driver->pending, &gadget_driver_pending_list);
1346 pr_info("udc-core: couldn't find an available UDC - added [%s] to list of pending drivers\n",
1347 driver->function);
1348 ret = 0;
1349 }
1350
Sebastian Andrzej Siewior4c49a5f2012-12-23 21:10:19 +01001351 mutex_unlock(&udc_lock);
Krzysztof Opasiakf1bddbb2016-05-05 10:46:05 +02001352 return ret;
Sebastian Andrzej Siewior4c49a5f2012-12-23 21:10:19 +01001353found:
1354 ret = udc_bind_to_driver(udc, driver);
Felipe Balbi2ccea032011-06-28 16:33:46 +03001355 mutex_unlock(&udc_lock);
1356 return ret;
1357}
1358EXPORT_SYMBOL_GPL(usb_gadget_probe_driver);
1359
1360int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
1361{
1362 struct usb_udc *udc = NULL;
1363 int ret = -ENODEV;
1364
1365 if (!driver || !driver->unbind)
1366 return -EINVAL;
1367
1368 mutex_lock(&udc_lock);
Krzysztof Opasiak881b5222017-01-16 08:40:57 +01001369 list_for_each_entry(udc, &udc_list, list) {
Felipe Balbi2ccea032011-06-28 16:33:46 +03001370 if (udc->driver == driver) {
1371 usb_gadget_remove_driver(udc);
Peter Chenb5fb8d02014-04-29 13:26:29 +08001372 usb_gadget_set_state(udc->gadget,
Krzysztof Opasiak881b5222017-01-16 08:40:57 +01001373 USB_STATE_NOTATTACHED);
1374
1375 /* Maybe there is someone waiting for this UDC? */
1376 check_pending_gadget_drivers(udc);
1377 /*
1378 * For now we ignore bind errors as probably it's
1379 * not a valid reason to fail other's gadget unbind
1380 */
Felipe Balbi2ccea032011-06-28 16:33:46 +03001381 ret = 0;
1382 break;
1383 }
Krzysztof Opasiak881b5222017-01-16 08:40:57 +01001384 }
Felipe Balbi2ccea032011-06-28 16:33:46 +03001385
Ruslan Bilovol855ed042015-11-23 09:56:38 +01001386 if (ret) {
1387 list_del(&driver->pending);
1388 ret = 0;
1389 }
Felipe Balbi2ccea032011-06-28 16:33:46 +03001390 mutex_unlock(&udc_lock);
1391 return ret;
1392}
1393EXPORT_SYMBOL_GPL(usb_gadget_unregister_driver);
1394
1395/* ------------------------------------------------------------------------- */
1396
1397static ssize_t usb_udc_srp_store(struct device *dev,
1398 struct device_attribute *attr, const char *buf, size_t n)
1399{
Felipe Balbi1d91a962011-09-08 14:11:17 +03001400 struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
Felipe Balbi2ccea032011-06-28 16:33:46 +03001401
1402 if (sysfs_streq(buf, "1"))
1403 usb_gadget_wakeup(udc->gadget);
1404
1405 return n;
1406}
1407static DEVICE_ATTR(srp, S_IWUSR, NULL, usb_udc_srp_store);
1408
1409static ssize_t usb_udc_softconn_store(struct device *dev,
1410 struct device_attribute *attr, const char *buf, size_t n)
1411{
Felipe Balbi865569b2011-09-08 14:12:18 +03001412 struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
Felipe Balbi2ccea032011-06-28 16:33:46 +03001413
Felipe Balbibfa6b182014-10-17 11:10:25 -05001414 if (!udc->driver) {
1415 dev_err(dev, "soft-connect without a gadget driver\n");
1416 return -EOPNOTSUPP;
1417 }
1418
Felipe Balbi2ccea032011-06-28 16:33:46 +03001419 if (sysfs_streq(buf, "connect")) {
Felipe Balbi2c683342014-10-17 11:34:07 -05001420 usb_gadget_udc_start(udc);
Felipe Balbi2ccea032011-06-28 16:33:46 +03001421 usb_gadget_connect(udc->gadget);
1422 } else if (sysfs_streq(buf, "disconnect")) {
Felipe Balbi83a787a2012-04-27 11:02:15 +03001423 usb_gadget_disconnect(udc->gadget);
Felipe Balbi0abd0692014-10-09 10:12:24 -05001424 udc->driver->disconnect(udc->gadget);
Felipe Balbi2c683342014-10-17 11:34:07 -05001425 usb_gadget_udc_stop(udc);
Felipe Balbi2ccea032011-06-28 16:33:46 +03001426 } else {
1427 dev_err(dev, "unsupported command '%s'\n", buf);
1428 return -EINVAL;
1429 }
1430
1431 return n;
1432}
1433static DEVICE_ATTR(soft_connect, S_IWUSR, NULL, usb_udc_softconn_store);
1434
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07001435static ssize_t state_show(struct device *dev, struct device_attribute *attr,
1436 char *buf)
Felipe Balbi49401f42011-12-19 12:57:04 +02001437{
1438 struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
1439 struct usb_gadget *gadget = udc->gadget;
1440
1441 return sprintf(buf, "%s\n", usb_state_string(gadget->state));
1442}
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07001443static DEVICE_ATTR_RO(state);
Felipe Balbi49401f42011-12-19 12:57:04 +02001444
Michal Nazarewiczd327ab52011-11-19 18:27:37 +01001445#define USB_UDC_SPEED_ATTR(name, param) \
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07001446ssize_t name##_show(struct device *dev, \
Michal Nazarewiczd327ab52011-11-19 18:27:37 +01001447 struct device_attribute *attr, char *buf) \
1448{ \
1449 struct usb_udc *udc = container_of(dev, struct usb_udc, dev); \
1450 return snprintf(buf, PAGE_SIZE, "%s\n", \
1451 usb_speed_string(udc->gadget->param)); \
1452} \
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07001453static DEVICE_ATTR_RO(name)
Michal Nazarewiczd327ab52011-11-19 18:27:37 +01001454
1455static USB_UDC_SPEED_ATTR(current_speed, speed);
1456static USB_UDC_SPEED_ATTR(maximum_speed, max_speed);
1457
Felipe Balbi2ccea032011-06-28 16:33:46 +03001458#define USB_UDC_ATTR(name) \
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07001459ssize_t name##_show(struct device *dev, \
Felipe Balbi2ccea032011-06-28 16:33:46 +03001460 struct device_attribute *attr, char *buf) \
1461{ \
Sebastian Andrzej Siewior019f9762011-06-23 14:28:38 +02001462 struct usb_udc *udc = container_of(dev, struct usb_udc, dev); \
Felipe Balbi2ccea032011-06-28 16:33:46 +03001463 struct usb_gadget *gadget = udc->gadget; \
1464 \
1465 return snprintf(buf, PAGE_SIZE, "%d\n", gadget->name); \
1466} \
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07001467static DEVICE_ATTR_RO(name)
Felipe Balbi2ccea032011-06-28 16:33:46 +03001468
Felipe Balbi2ccea032011-06-28 16:33:46 +03001469static USB_UDC_ATTR(is_otg);
1470static USB_UDC_ATTR(is_a_peripheral);
1471static USB_UDC_ATTR(b_hnp_enable);
1472static USB_UDC_ATTR(a_hnp_support);
1473static USB_UDC_ATTR(a_alt_hnp_support);
Peter Chen3f6dd4f2015-01-28 16:32:42 +08001474static USB_UDC_ATTR(is_selfpowered);
Felipe Balbi2ccea032011-06-28 16:33:46 +03001475
1476static struct attribute *usb_udc_attrs[] = {
1477 &dev_attr_srp.attr,
1478 &dev_attr_soft_connect.attr,
Felipe Balbi49401f42011-12-19 12:57:04 +02001479 &dev_attr_state.attr,
Michal Nazarewiczd327ab52011-11-19 18:27:37 +01001480 &dev_attr_current_speed.attr,
1481 &dev_attr_maximum_speed.attr,
Felipe Balbi2ccea032011-06-28 16:33:46 +03001482
Felipe Balbi2ccea032011-06-28 16:33:46 +03001483 &dev_attr_is_otg.attr,
1484 &dev_attr_is_a_peripheral.attr,
1485 &dev_attr_b_hnp_enable.attr,
1486 &dev_attr_a_hnp_support.attr,
1487 &dev_attr_a_alt_hnp_support.attr,
Peter Chen3f6dd4f2015-01-28 16:32:42 +08001488 &dev_attr_is_selfpowered.attr,
Felipe Balbi2ccea032011-06-28 16:33:46 +03001489 NULL,
1490};
1491
1492static const struct attribute_group usb_udc_attr_group = {
1493 .attrs = usb_udc_attrs,
1494};
1495
1496static const struct attribute_group *usb_udc_attr_groups[] = {
1497 &usb_udc_attr_group,
1498 NULL,
1499};
1500
1501static int usb_udc_uevent(struct device *dev, struct kobj_uevent_env *env)
1502{
1503 struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
1504 int ret;
1505
1506 ret = add_uevent_var(env, "USB_UDC_NAME=%s", udc->gadget->name);
1507 if (ret) {
1508 dev_err(dev, "failed to add uevent USB_UDC_NAME\n");
1509 return ret;
1510 }
1511
1512 if (udc->driver) {
1513 ret = add_uevent_var(env, "USB_UDC_DRIVER=%s",
1514 udc->driver->function);
1515 if (ret) {
1516 dev_err(dev, "failed to add uevent USB_UDC_DRIVER\n");
1517 return ret;
1518 }
1519 }
1520
1521 return 0;
1522}
1523
1524static int __init usb_udc_init(void)
1525{
1526 udc_class = class_create(THIS_MODULE, "udc");
1527 if (IS_ERR(udc_class)) {
1528 pr_err("failed to create udc class --> %ld\n",
1529 PTR_ERR(udc_class));
1530 return PTR_ERR(udc_class);
1531 }
1532
1533 udc_class->dev_uevent = usb_udc_uevent;
Felipe Balbi2ccea032011-06-28 16:33:46 +03001534 return 0;
1535}
1536subsys_initcall(usb_udc_init);
1537
1538static void __exit usb_udc_exit(void)
1539{
1540 class_destroy(udc_class);
1541}
1542module_exit(usb_udc_exit);
1543
1544MODULE_DESCRIPTION("UDC Framework");
1545MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
1546MODULE_LICENSE("GPL v2");