blob: 2dd30d0c177a186937a9d42d55b7b855353b54bd [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
Alan Sternfc71e392019-10-28 10:54:26 -0400109 /* UDC drivers can't handle endpoints with maxpacket size 0 */
110 if (usb_endpoint_maxp(ep->desc) == 0) {
111 /*
112 * We should log an error message here, but we can't call
113 * dev_err() because there's no way to find the gadget
114 * given only ep.
115 */
116 ret = -EINVAL;
117 goto out;
118 }
119
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300120 ret = ep->ops->enable(ep, ep->desc);
Colin Ian Kingf510b5a2016-07-25 22:57:36 +0100121 if (ret)
Felipe Balbi5e42d712016-05-31 13:39:21 +0300122 goto out;
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300123
124 ep->enabled = true;
125
Felipe Balbi5e42d712016-05-31 13:39:21 +0300126out:
127 trace_usb_ep_enable(ep, ret);
128
129 return ret;
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300130}
131EXPORT_SYMBOL_GPL(usb_ep_enable);
132
133/**
134 * usb_ep_disable - endpoint is no longer usable
135 * @ep:the endpoint being unconfigured. may not be the endpoint named "ep0".
136 *
137 * no other task may be using this endpoint when this is called.
138 * any pending and uncompleted requests will complete with status
139 * indicating disconnect (-ESHUTDOWN) before this call returns.
140 * gadget drivers must call usb_ep_enable() again before queueing
141 * requests to the endpoint.
142 *
143 * returns zero, or a negative error code.
144 */
145int usb_ep_disable(struct usb_ep *ep)
146{
Felipe Balbi5e42d712016-05-31 13:39:21 +0300147 int ret = 0;
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300148
149 if (!ep->enabled)
Felipe Balbi5e42d712016-05-31 13:39:21 +0300150 goto out;
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300151
152 ret = ep->ops->disable(ep);
Stefan Agner17af7982017-04-16 20:12:50 -0700153 if (ret)
Felipe Balbi5e42d712016-05-31 13:39:21 +0300154 goto out;
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300155
156 ep->enabled = false;
157
Felipe Balbi5e42d712016-05-31 13:39:21 +0300158out:
159 trace_usb_ep_disable(ep, ret);
160
161 return ret;
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300162}
163EXPORT_SYMBOL_GPL(usb_ep_disable);
164
165/**
166 * usb_ep_alloc_request - allocate a request object to use with this endpoint
167 * @ep:the endpoint to be used with with the request
168 * @gfp_flags:GFP_* flags to use
169 *
170 * Request objects must be allocated with this call, since they normally
171 * need controller-specific setup and may even need endpoint-specific
172 * resources such as allocation of DMA descriptors.
173 * Requests may be submitted with usb_ep_queue(), and receive a single
174 * completion callback. Free requests with usb_ep_free_request(), when
175 * they are no longer needed.
176 *
177 * Returns the request, or null if one could not be allocated.
178 */
179struct usb_request *usb_ep_alloc_request(struct usb_ep *ep,
180 gfp_t gfp_flags)
181{
Felipe Balbi5e42d712016-05-31 13:39:21 +0300182 struct usb_request *req = NULL;
183
184 req = ep->ops->alloc_request(ep, gfp_flags);
185
186 trace_usb_ep_alloc_request(ep, req, req ? 0 : -ENOMEM);
187
188 return req;
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300189}
190EXPORT_SYMBOL_GPL(usb_ep_alloc_request);
191
192/**
193 * usb_ep_free_request - frees a request object
194 * @ep:the endpoint associated with the request
195 * @req:the request being freed
196 *
197 * Reverses the effect of usb_ep_alloc_request().
198 * Caller guarantees the request is not queued, and that it will
199 * no longer be requeued (or otherwise used).
200 */
201void usb_ep_free_request(struct usb_ep *ep,
202 struct usb_request *req)
203{
Felipe Balbi5e42d712016-05-31 13:39:21 +0300204 trace_usb_ep_free_request(ep, req, 0);
Sai Krishna Juturie888fa72017-12-20 16:59:24 +0530205 ep->ops->free_request(ep, req);
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300206}
207EXPORT_SYMBOL_GPL(usb_ep_free_request);
208
209/**
210 * usb_ep_queue - queues (submits) an I/O request to an endpoint.
211 * @ep:the endpoint associated with the request
212 * @req:the request being submitted
213 * @gfp_flags: GFP_* flags to use in case the lower level driver couldn't
214 * pre-allocate all necessary memory with the request.
215 *
216 * This tells the device controller to perform the specified request through
217 * that endpoint (reading or writing a buffer). When the request completes,
218 * including being canceled by usb_ep_dequeue(), the request's completion
219 * routine is called to return the request to the driver. Any endpoint
220 * (except control endpoints like ep0) may have more than one transfer
221 * request queued; they complete in FIFO order. Once a gadget driver
222 * submits a request, that request may not be examined or modified until it
223 * is given back to that driver through the completion callback.
224 *
225 * Each request is turned into one or more packets. The controller driver
226 * never merges adjacent requests into the same packet. OUT transfers
227 * will sometimes use data that's already buffered in the hardware.
228 * Drivers can rely on the fact that the first byte of the request's buffer
229 * always corresponds to the first byte of some USB packet, for both
230 * IN and OUT transfers.
231 *
232 * Bulk endpoints can queue any amount of data; the transfer is packetized
233 * automatically. The last packet will be short if the request doesn't fill it
234 * out completely. Zero length packets (ZLPs) should be avoided in portable
235 * protocols since not all usb hardware can successfully handle zero length
236 * packets. (ZLPs may be explicitly written, and may be implicitly written if
237 * the request 'zero' flag is set.) Bulk endpoints may also be used
238 * for interrupt transfers; but the reverse is not true, and some endpoints
239 * won't support every interrupt transfer. (Such as 768 byte packets.)
240 *
241 * Interrupt-only endpoints are less functional than bulk endpoints, for
242 * example by not supporting queueing or not handling buffers that are
243 * larger than the endpoint's maxpacket size. They may also treat data
244 * toggle differently.
245 *
246 * Control endpoints ... after getting a setup() callback, the driver queues
247 * one response (even if it would be zero length). That enables the
248 * status ack, after transferring data as specified in the response. Setup
249 * functions may return negative error codes to generate protocol stalls.
250 * (Note that some USB device controllers disallow protocol stall responses
251 * in some cases.) When control responses are deferred (the response is
252 * written after the setup callback returns), then usb_ep_set_halt() may be
253 * used on ep0 to trigger protocol stalls. Depending on the controller,
254 * it may not be possible to trigger a status-stage protocol stall when the
255 * data stage is over, that is, from within the response's completion
256 * routine.
257 *
258 * For periodic endpoints, like interrupt or isochronous ones, the usb host
259 * arranges to poll once per interval, and the gadget driver usually will
260 * have queued some data to transfer at that time.
261 *
Felipe Balbiba260ce2018-03-26 13:14:46 +0300262 * Note that @req's ->complete() callback must never be called from
263 * within usb_ep_queue() as that can create deadlock situations.
264 *
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300265 * Returns zero, or a negative error code. Endpoints that are not enabled
266 * report errors; errors will also be
267 * reported when the usb peripheral is disconnected.
268 */
269int usb_ep_queue(struct usb_ep *ep,
270 struct usb_request *req, gfp_t gfp_flags)
271{
Felipe Balbi5e42d712016-05-31 13:39:21 +0300272 int ret = 0;
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300273
Felipe Balbi5e42d712016-05-31 13:39:21 +0300274 if (WARN_ON_ONCE(!ep->enabled && ep->address)) {
275 ret = -ESHUTDOWN;
276 goto out;
277 }
278
279 ret = ep->ops->queue(ep, req, gfp_flags);
280
281out:
282 trace_usb_ep_queue(ep, req, ret);
283
284 return ret;
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300285}
286EXPORT_SYMBOL_GPL(usb_ep_queue);
287
288/**
289 * usb_ep_dequeue - dequeues (cancels, unlinks) an I/O request from an endpoint
290 * @ep:the endpoint associated with the request
291 * @req:the request being canceled
292 *
293 * If the request is still active on the endpoint, it is dequeued and its
294 * completion routine is called (with status -ECONNRESET); else a negative
295 * error code is returned. This is guaranteed to happen before the call to
296 * usb_ep_dequeue() returns.
297 *
298 * Note that some hardware can't clear out write fifos (to unlink the request
299 * at the head of the queue) except as part of disconnecting from usb. Such
300 * restrictions prevent drivers from supporting configuration changes,
301 * even to configuration zero (a "chapter 9" requirement).
302 */
303int usb_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
304{
Felipe Balbi5e42d712016-05-31 13:39:21 +0300305 int ret;
306
307 ret = ep->ops->dequeue(ep, req);
308 trace_usb_ep_dequeue(ep, req, ret);
309
310 return ret;
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300311}
312EXPORT_SYMBOL_GPL(usb_ep_dequeue);
313
314/**
315 * usb_ep_set_halt - sets the endpoint halt feature.
316 * @ep: the non-isochronous endpoint being stalled
317 *
318 * Use this to stall an endpoint, perhaps as an error report.
319 * Except for control endpoints,
320 * the endpoint stays halted (will not stream any data) until the host
321 * clears this feature; drivers may need to empty the endpoint's request
322 * queue first, to make sure no inappropriate transfers happen.
323 *
324 * Note that while an endpoint CLEAR_FEATURE will be invisible to the
325 * gadget driver, a SET_INTERFACE will not be. To reset endpoints for the
326 * current altsetting, see usb_ep_clear_halt(). When switching altsettings,
327 * it's simplest to use usb_ep_enable() or usb_ep_disable() for the endpoints.
328 *
329 * Returns zero, or a negative error code. On success, this call sets
330 * underlying hardware state that blocks data transfers.
331 * Attempts to halt IN endpoints will fail (returning -EAGAIN) if any
332 * transfer requests are still queued, or if the controller hardware
333 * (usually a FIFO) still holds bytes that the host hasn't collected.
334 */
335int usb_ep_set_halt(struct usb_ep *ep)
336{
Felipe Balbi5e42d712016-05-31 13:39:21 +0300337 int ret;
338
339 ret = ep->ops->set_halt(ep, 1);
340 trace_usb_ep_set_halt(ep, ret);
341
342 return ret;
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300343}
344EXPORT_SYMBOL_GPL(usb_ep_set_halt);
345
346/**
347 * usb_ep_clear_halt - clears endpoint halt, and resets toggle
348 * @ep:the bulk or interrupt endpoint being reset
349 *
350 * Use this when responding to the standard usb "set interface" request,
351 * for endpoints that aren't reconfigured, after clearing any other state
352 * in the endpoint's i/o queue.
353 *
354 * Returns zero, or a negative error code. On success, this call clears
355 * the underlying hardware state reflecting endpoint halt and data toggle.
356 * Note that some hardware can't support this request (like pxa2xx_udc),
357 * and accordingly can't correctly implement interface altsettings.
358 */
359int usb_ep_clear_halt(struct usb_ep *ep)
360{
Felipe Balbi5e42d712016-05-31 13:39:21 +0300361 int ret;
362
363 ret = ep->ops->set_halt(ep, 0);
364 trace_usb_ep_clear_halt(ep, ret);
365
366 return ret;
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300367}
368EXPORT_SYMBOL_GPL(usb_ep_clear_halt);
369
370/**
371 * usb_ep_set_wedge - sets the halt feature and ignores clear requests
372 * @ep: the endpoint being wedged
373 *
374 * Use this to stall an endpoint and ignore CLEAR_FEATURE(HALT_ENDPOINT)
375 * requests. If the gadget driver clears the halt status, it will
376 * automatically unwedge the endpoint.
377 *
378 * Returns zero on success, else negative errno.
379 */
380int usb_ep_set_wedge(struct usb_ep *ep)
381{
Felipe Balbi5e42d712016-05-31 13:39:21 +0300382 int ret;
383
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300384 if (ep->ops->set_wedge)
Felipe Balbi5e42d712016-05-31 13:39:21 +0300385 ret = ep->ops->set_wedge(ep);
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300386 else
Felipe Balbi5e42d712016-05-31 13:39:21 +0300387 ret = ep->ops->set_halt(ep, 1);
388
389 trace_usb_ep_set_wedge(ep, ret);
390
391 return ret;
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300392}
393EXPORT_SYMBOL_GPL(usb_ep_set_wedge);
394
395/**
396 * usb_ep_fifo_status - returns number of bytes in fifo, or error
397 * @ep: the endpoint whose fifo status is being checked.
398 *
399 * FIFO endpoints may have "unclaimed data" in them in certain cases,
400 * such as after aborted transfers. Hosts may not have collected all
401 * the IN data written by the gadget driver (and reported by a request
402 * completion). The gadget driver may not have collected all the data
403 * written OUT to it by the host. Drivers that need precise handling for
404 * fault reporting or recovery may need to use this call.
405 *
406 * This returns the number of such bytes in the fifo, or a negative
407 * errno if the endpoint doesn't use a FIFO or doesn't support such
408 * precise handling.
409 */
410int usb_ep_fifo_status(struct usb_ep *ep)
411{
Felipe Balbi5e42d712016-05-31 13:39:21 +0300412 int ret;
413
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300414 if (ep->ops->fifo_status)
Felipe Balbi5e42d712016-05-31 13:39:21 +0300415 ret = ep->ops->fifo_status(ep);
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300416 else
Felipe Balbi5e42d712016-05-31 13:39:21 +0300417 ret = -EOPNOTSUPP;
418
419 trace_usb_ep_fifo_status(ep, ret);
420
421 return ret;
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300422}
423EXPORT_SYMBOL_GPL(usb_ep_fifo_status);
424
425/**
426 * usb_ep_fifo_flush - flushes contents of a fifo
427 * @ep: the endpoint whose fifo is being flushed.
428 *
429 * This call may be used to flush the "unclaimed data" that may exist in
430 * an endpoint fifo after abnormal transaction terminations. The call
431 * must never be used except when endpoint is not being used for any
432 * protocol translation.
433 */
434void usb_ep_fifo_flush(struct usb_ep *ep)
435{
436 if (ep->ops->fifo_flush)
437 ep->ops->fifo_flush(ep);
Felipe Balbi5e42d712016-05-31 13:39:21 +0300438
439 trace_usb_ep_fifo_flush(ep, 0);
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300440}
441EXPORT_SYMBOL_GPL(usb_ep_fifo_flush);
442
443/* ------------------------------------------------------------------------- */
444
445/**
446 * usb_gadget_frame_number - returns the current frame number
447 * @gadget: controller that reports the frame number
448 *
449 * Returns the usb frame number, normally eleven bits from a SOF packet,
450 * or negative errno if this device doesn't support this capability.
451 */
452int usb_gadget_frame_number(struct usb_gadget *gadget)
453{
Felipe Balbi5e42d712016-05-31 13:39:21 +0300454 int ret;
455
456 ret = gadget->ops->get_frame(gadget);
457
458 trace_usb_gadget_frame_number(gadget, ret);
459
460 return ret;
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300461}
462EXPORT_SYMBOL_GPL(usb_gadget_frame_number);
463
464/**
465 * usb_gadget_wakeup - tries to wake up the host connected to this gadget
466 * @gadget: controller used to wake up the host
467 *
468 * Returns zero on success, else negative error code if the hardware
469 * doesn't support such attempts, or its support has not been enabled
470 * by the usb host. Drivers must return device descriptors that report
471 * their ability to support this, or hosts won't enable it.
472 *
473 * This may also try to use SRP to wake the host and start enumeration,
474 * even if OTG isn't otherwise in use. OTG devices may also start
475 * remote wakeup even when hosts don't explicitly enable it.
476 */
477int usb_gadget_wakeup(struct usb_gadget *gadget)
478{
Felipe Balbi5e42d712016-05-31 13:39:21 +0300479 int ret = 0;
480
481 if (!gadget->ops->wakeup) {
482 ret = -EOPNOTSUPP;
483 goto out;
484 }
485
486 ret = gadget->ops->wakeup(gadget);
487
488out:
489 trace_usb_gadget_wakeup(gadget, ret);
490
491 return ret;
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300492}
493EXPORT_SYMBOL_GPL(usb_gadget_wakeup);
494
495/**
496 * usb_gadget_set_selfpowered - sets the device selfpowered feature.
497 * @gadget:the device being declared as self-powered
498 *
499 * this affects the device status reported by the hardware driver
500 * to reflect that it now has a local power supply.
501 *
502 * returns zero on success, else negative errno.
503 */
504int usb_gadget_set_selfpowered(struct usb_gadget *gadget)
505{
Felipe Balbi5e42d712016-05-31 13:39:21 +0300506 int ret = 0;
507
508 if (!gadget->ops->set_selfpowered) {
509 ret = -EOPNOTSUPP;
510 goto out;
511 }
512
513 ret = gadget->ops->set_selfpowered(gadget, 1);
514
515out:
516 trace_usb_gadget_set_selfpowered(gadget, ret);
517
518 return ret;
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300519}
520EXPORT_SYMBOL_GPL(usb_gadget_set_selfpowered);
521
522/**
523 * usb_gadget_clear_selfpowered - clear the device selfpowered feature.
524 * @gadget:the device being declared as bus-powered
525 *
526 * this affects the device status reported by the hardware driver.
527 * some hardware may not support bus-powered operation, in which
528 * case this feature's value can never change.
529 *
530 * returns zero on success, else negative errno.
531 */
532int usb_gadget_clear_selfpowered(struct usb_gadget *gadget)
533{
Felipe Balbi5e42d712016-05-31 13:39:21 +0300534 int ret = 0;
535
536 if (!gadget->ops->set_selfpowered) {
537 ret = -EOPNOTSUPP;
538 goto out;
539 }
540
541 ret = gadget->ops->set_selfpowered(gadget, 0);
542
543out:
544 trace_usb_gadget_clear_selfpowered(gadget, ret);
545
546 return ret;
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300547}
548EXPORT_SYMBOL_GPL(usb_gadget_clear_selfpowered);
549
550/**
551 * usb_gadget_vbus_connect - Notify controller that VBUS is powered
552 * @gadget:The device which now has VBUS power.
553 * Context: can sleep
554 *
555 * This call is used by a driver for an external transceiver (or GPIO)
556 * that detects a VBUS power session starting. Common responses include
557 * resuming the controller, activating the D+ (or D-) pullup to let the
558 * host detect that a USB device is attached, and starting to draw power
559 * (8mA or possibly more, especially after SET_CONFIGURATION).
560 *
561 * Returns zero on success, else negative errno.
562 */
563int usb_gadget_vbus_connect(struct usb_gadget *gadget)
564{
Felipe Balbi5e42d712016-05-31 13:39:21 +0300565 int ret = 0;
566
567 if (!gadget->ops->vbus_session) {
568 ret = -EOPNOTSUPP;
569 goto out;
570 }
571
572 ret = gadget->ops->vbus_session(gadget, 1);
573
574out:
575 trace_usb_gadget_vbus_connect(gadget, ret);
576
577 return ret;
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300578}
579EXPORT_SYMBOL_GPL(usb_gadget_vbus_connect);
580
581/**
582 * usb_gadget_vbus_draw - constrain controller's VBUS power usage
583 * @gadget:The device whose VBUS usage is being described
584 * @mA:How much current to draw, in milliAmperes. This should be twice
585 * the value listed in the configuration descriptor bMaxPower field.
586 *
587 * This call is used by gadget drivers during SET_CONFIGURATION calls,
588 * reporting how much power the device may consume. For example, this
589 * could affect how quickly batteries are recharged.
590 *
591 * Returns zero on success, else negative errno.
592 */
593int usb_gadget_vbus_draw(struct usb_gadget *gadget, unsigned mA)
594{
Felipe Balbi5e42d712016-05-31 13:39:21 +0300595 int ret = 0;
596
597 if (!gadget->ops->vbus_draw) {
598 ret = -EOPNOTSUPP;
599 goto out;
600 }
601
602 ret = gadget->ops->vbus_draw(gadget, mA);
603 if (!ret)
604 gadget->mA = mA;
605
606out:
607 trace_usb_gadget_vbus_draw(gadget, ret);
608
609 return ret;
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300610}
611EXPORT_SYMBOL_GPL(usb_gadget_vbus_draw);
612
613/**
614 * usb_gadget_vbus_disconnect - notify controller about VBUS session end
615 * @gadget:the device whose VBUS supply is being described
616 * Context: can sleep
617 *
618 * This call is used by a driver for an external transceiver (or GPIO)
619 * that detects a VBUS power session ending. Common responses include
620 * reversing everything done in usb_gadget_vbus_connect().
621 *
622 * Returns zero on success, else negative errno.
623 */
624int usb_gadget_vbus_disconnect(struct usb_gadget *gadget)
625{
Felipe Balbi5e42d712016-05-31 13:39:21 +0300626 int ret = 0;
627
628 if (!gadget->ops->vbus_session) {
629 ret = -EOPNOTSUPP;
630 goto out;
631 }
632
633 ret = gadget->ops->vbus_session(gadget, 0);
634
635out:
636 trace_usb_gadget_vbus_disconnect(gadget, ret);
637
638 return ret;
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300639}
640EXPORT_SYMBOL_GPL(usb_gadget_vbus_disconnect);
641
642/**
643 * usb_gadget_connect - software-controlled connect to USB host
644 * @gadget:the peripheral being connected
645 *
646 * Enables the D+ (or potentially D-) pullup. The host will start
647 * enumerating this gadget when the pullup is active and a VBUS session
648 * is active (the link is powered). This pullup is always enabled unless
649 * usb_gadget_disconnect() has been used to disable it.
650 *
651 * Returns zero on success, else negative errno.
652 */
653int usb_gadget_connect(struct usb_gadget *gadget)
654{
Felipe Balbi5e42d712016-05-31 13:39:21 +0300655 int ret = 0;
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300656
Felipe Balbi5e42d712016-05-31 13:39:21 +0300657 if (!gadget->ops->pullup) {
658 ret = -EOPNOTSUPP;
659 goto out;
660 }
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300661
662 if (gadget->deactivated) {
663 /*
664 * If gadget is deactivated we only save new state.
665 * Gadget will be connected automatically after activation.
666 */
667 gadget->connected = true;
Felipe Balbi5e42d712016-05-31 13:39:21 +0300668 goto out;
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300669 }
670
671 ret = gadget->ops->pullup(gadget, 1);
672 if (!ret)
673 gadget->connected = 1;
Felipe Balbi5e42d712016-05-31 13:39:21 +0300674
675out:
676 trace_usb_gadget_connect(gadget, ret);
677
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300678 return ret;
679}
680EXPORT_SYMBOL_GPL(usb_gadget_connect);
681
682/**
683 * usb_gadget_disconnect - software-controlled disconnect from USB host
684 * @gadget:the peripheral being disconnected
685 *
686 * Disables the D+ (or potentially D-) pullup, which the host may see
687 * as a disconnect (when a VBUS session is active). Not all systems
688 * support software pullup controls.
689 *
690 * Returns zero on success, else negative errno.
691 */
692int usb_gadget_disconnect(struct usb_gadget *gadget)
693{
Felipe Balbi5e42d712016-05-31 13:39:21 +0300694 int ret = 0;
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300695
Felipe Balbi5e42d712016-05-31 13:39:21 +0300696 if (!gadget->ops->pullup) {
697 ret = -EOPNOTSUPP;
698 goto out;
699 }
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300700
701 if (gadget->deactivated) {
702 /*
703 * If gadget is deactivated we only save new state.
704 * Gadget will stay disconnected after activation.
705 */
706 gadget->connected = false;
Felipe Balbi5e42d712016-05-31 13:39:21 +0300707 goto out;
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300708 }
709
710 ret = gadget->ops->pullup(gadget, 0);
711 if (!ret)
712 gadget->connected = 0;
Felipe Balbi5e42d712016-05-31 13:39:21 +0300713
714out:
715 trace_usb_gadget_disconnect(gadget, ret);
716
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300717 return ret;
718}
719EXPORT_SYMBOL_GPL(usb_gadget_disconnect);
720
721/**
Mayank Ranaa99689a2016-08-10 17:39:47 -0700722 * usb_gsi_ep_op - performs operation on GSI accelerated EP based on EP op code
723 *
724 * Operations such as EP configuration, TRB allocation, StartXfer etc.
725 * See gsi_ep_op for more details.
726 */
727int usb_gsi_ep_op(struct usb_ep *ep,
728 struct usb_gsi_request *req, enum gsi_ep_op op)
729{
730 if (ep->ops->gsi_ep_op)
731 return ep->ops->gsi_ep_op(ep, req, op);
732
733 return -EOPNOTSUPP;
734}
735EXPORT_SYMBOL(usb_gsi_ep_op);
736
737/**
738 * usb_gadget_func_wakeup - send a function remote wakeup up notification
739 * to the host connected to this gadget
740 * @gadget: controller used to wake up the host
741 * @interface_id: the interface which triggered the remote wakeup event
742 *
743 * Returns zero on success. Otherwise, negative error code is returned.
744 */
745int usb_gadget_func_wakeup(struct usb_gadget *gadget,
746 int interface_id)
747{
748 if (gadget->speed != USB_SPEED_SUPER)
749 return -EOPNOTSUPP;
750
751 if (!gadget->ops->func_wakeup)
752 return -EOPNOTSUPP;
753
754 return gadget->ops->func_wakeup(gadget, interface_id);
755}
756EXPORT_SYMBOL(usb_gadget_func_wakeup);
757
758/**
759 * usb_gadget_restart - software-controlled reset of USB peripheral connection
760 * @gadget:the peripheral being reset
761 *
762 * Informs controller driver for Vbus LOW followed by Vbus HIGH notification.
763 * This performs full hardware reset and re-initialization.
764 */
765int usb_gadget_restart(struct usb_gadget *gadget)
766{
767 if (!gadget->ops->restart)
768 return -EOPNOTSUPP;
769 return gadget->ops->restart(gadget);
770}
771EXPORT_SYMBOL(usb_gadget_restart);
772/**
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300773 * usb_gadget_deactivate - deactivate function which is not ready to work
774 * @gadget: the peripheral being deactivated
775 *
776 * This routine may be used during the gadget driver bind() call to prevent
777 * the peripheral from ever being visible to the USB host, unless later
778 * usb_gadget_activate() is called. For example, user mode components may
779 * need to be activated before the system can talk to hosts.
780 *
781 * Returns zero on success, else negative errno.
782 */
783int usb_gadget_deactivate(struct usb_gadget *gadget)
784{
Felipe Balbi5e42d712016-05-31 13:39:21 +0300785 int ret = 0;
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300786
787 if (gadget->deactivated)
Felipe Balbi5e42d712016-05-31 13:39:21 +0300788 goto out;
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300789
790 if (gadget->connected) {
791 ret = usb_gadget_disconnect(gadget);
792 if (ret)
Felipe Balbi5e42d712016-05-31 13:39:21 +0300793 goto out;
794
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300795 /*
796 * If gadget was being connected before deactivation, we want
797 * to reconnect it in usb_gadget_activate().
798 */
799 gadget->connected = true;
800 }
801 gadget->deactivated = true;
802
Felipe Balbi5e42d712016-05-31 13:39:21 +0300803out:
804 trace_usb_gadget_deactivate(gadget, ret);
805
806 return ret;
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300807}
808EXPORT_SYMBOL_GPL(usb_gadget_deactivate);
809
810/**
811 * usb_gadget_activate - activate function which is not ready to work
812 * @gadget: the peripheral being activated
813 *
814 * This routine activates gadget which was previously deactivated with
815 * usb_gadget_deactivate() call. It calls usb_gadget_connect() if needed.
816 *
817 * Returns zero on success, else negative errno.
818 */
819int usb_gadget_activate(struct usb_gadget *gadget)
820{
Felipe Balbi5e42d712016-05-31 13:39:21 +0300821 int ret = 0;
822
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300823 if (!gadget->deactivated)
Felipe Balbi5e42d712016-05-31 13:39:21 +0300824 goto out;
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300825
826 gadget->deactivated = false;
827
828 /*
829 * If gadget has been connected before deactivation, or became connected
830 * while it was being deactivated, we call usb_gadget_connect().
831 */
832 if (gadget->connected)
Felipe Balbi5e42d712016-05-31 13:39:21 +0300833 ret = usb_gadget_connect(gadget);
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300834
Felipe Balbi5e42d712016-05-31 13:39:21 +0300835out:
836 trace_usb_gadget_activate(gadget, ret);
837
838 return ret;
Felipe Balbi5a8d6512016-05-31 13:07:47 +0300839}
840EXPORT_SYMBOL_GPL(usb_gadget_activate);
841
842/* ------------------------------------------------------------------------- */
843
Alan Stern908b9612013-07-12 11:06:21 -0400844#ifdef CONFIG_HAS_DMA
845
Yoshihiro Shimoda679ca392016-04-18 16:53:39 +0900846int usb_gadget_map_request_by_dev(struct device *dev,
Felipe Balbia6989082011-12-15 13:31:48 +0200847 struct usb_request *req, int is_in)
848{
849 if (req->length == 0)
850 return 0;
851
852 if (req->num_sgs) {
853 int mapped;
854
Yoshihiro Shimoda7ace8fc2015-07-13 18:10:05 +0900855 mapped = dma_map_sg(dev, req->sg, req->num_sgs,
Felipe Balbia6989082011-12-15 13:31:48 +0200856 is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
857 if (mapped == 0) {
Yoshihiro Shimoda5096c4d2016-04-18 16:53:38 +0900858 dev_err(dev, "failed to map SGs\n");
Felipe Balbia6989082011-12-15 13:31:48 +0200859 return -EFAULT;
860 }
861
862 req->num_mapped_sgs = mapped;
863 } else {
Yoshihiro Shimoda7ace8fc2015-07-13 18:10:05 +0900864 req->dma = dma_map_single(dev, req->buf, req->length,
Felipe Balbia6989082011-12-15 13:31:48 +0200865 is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
866
Yoshihiro Shimoda7ace8fc2015-07-13 18:10:05 +0900867 if (dma_mapping_error(dev, req->dma)) {
868 dev_err(dev, "failed to map buffer\n");
Felipe Balbia6989082011-12-15 13:31:48 +0200869 return -EFAULT;
870 }
Chandana Kishori Chiluverue92b17c2019-09-10 16:31:24 +0530871
872 req->dma_mapped = 1;
Felipe Balbia6989082011-12-15 13:31:48 +0200873 }
874
875 return 0;
876}
Yoshihiro Shimoda679ca392016-04-18 16:53:39 +0900877EXPORT_SYMBOL_GPL(usb_gadget_map_request_by_dev);
878
879int usb_gadget_map_request(struct usb_gadget *gadget,
880 struct usb_request *req, int is_in)
881{
882 return usb_gadget_map_request_by_dev(gadget->dev.parent, req, is_in);
883}
Felipe Balbia6989082011-12-15 13:31:48 +0200884EXPORT_SYMBOL_GPL(usb_gadget_map_request);
885
Yoshihiro Shimoda679ca392016-04-18 16:53:39 +0900886void usb_gadget_unmap_request_by_dev(struct device *dev,
Felipe Balbia6989082011-12-15 13:31:48 +0200887 struct usb_request *req, int is_in)
888{
889 if (req->length == 0)
890 return;
891
892 if (req->num_mapped_sgs) {
Felipe Balbi23fd5372016-08-24 14:33:27 +0300893 dma_unmap_sg(dev, req->sg, req->num_sgs,
Felipe Balbia6989082011-12-15 13:31:48 +0200894 is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
895
896 req->num_mapped_sgs = 0;
Chandana Kishori Chiluverue92b17c2019-09-10 16:31:24 +0530897 } else if (req->dma_mapped) {
Yoshihiro Shimoda679ca392016-04-18 16:53:39 +0900898 dma_unmap_single(dev, req->dma, req->length,
Felipe Balbia6989082011-12-15 13:31:48 +0200899 is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
Chandana Kishori Chiluverue92b17c2019-09-10 16:31:24 +0530900 req->dma_mapped = 0;
Felipe Balbia6989082011-12-15 13:31:48 +0200901 }
902}
Yoshihiro Shimoda679ca392016-04-18 16:53:39 +0900903EXPORT_SYMBOL_GPL(usb_gadget_unmap_request_by_dev);
904
905void usb_gadget_unmap_request(struct usb_gadget *gadget,
906 struct usb_request *req, int is_in)
907{
908 usb_gadget_unmap_request_by_dev(gadget->dev.parent, req, is_in);
909}
Felipe Balbia6989082011-12-15 13:31:48 +0200910EXPORT_SYMBOL_GPL(usb_gadget_unmap_request);
911
Alan Stern908b9612013-07-12 11:06:21 -0400912#endif /* CONFIG_HAS_DMA */
913
Felipe Balbia6989082011-12-15 13:31:48 +0200914/* ------------------------------------------------------------------------- */
915
Michal Sojka3fc2aa52014-09-24 22:43:18 +0200916/**
917 * usb_gadget_giveback_request - give the request back to the gadget layer
918 * Context: in_interrupt()
919 *
920 * This is called by device controller drivers in order to return the
921 * completed request back to the gadget layer.
922 */
923void usb_gadget_giveback_request(struct usb_ep *ep,
924 struct usb_request *req)
925{
Michal Sojka0cfbd322014-09-24 22:43:21 +0200926 if (likely(req->status == 0))
927 usb_led_activity(USB_LED_EVENT_GADGET);
928
Felipe Balbi5e42d712016-05-31 13:39:21 +0300929 trace_usb_gadget_giveback_request(ep, req, 0);
930
Michal Sojka3fc2aa52014-09-24 22:43:18 +0200931 req->complete(ep, req);
932}
933EXPORT_SYMBOL_GPL(usb_gadget_giveback_request);
934
935/* ------------------------------------------------------------------------- */
936
Robert Baldygab0aea002015-08-06 14:11:12 +0200937/**
938 * gadget_find_ep_by_name - returns ep whose name is the same as sting passed
939 * in second parameter or NULL if searched endpoint not found
940 * @g: controller to check for quirk
941 * @name: name of searched endpoint
942 */
943struct usb_ep *gadget_find_ep_by_name(struct usb_gadget *g, const char *name)
944{
945 struct usb_ep *ep;
946
947 gadget_for_each_ep(ep, g) {
948 if (!strcmp(ep->name, name))
949 return ep;
950 }
951
952 return NULL;
953}
954EXPORT_SYMBOL_GPL(gadget_find_ep_by_name);
955
956/* ------------------------------------------------------------------------- */
957
Robert Baldyga4278c682015-08-06 14:11:11 +0200958int usb_gadget_ep_match_desc(struct usb_gadget *gadget,
959 struct usb_ep *ep, struct usb_endpoint_descriptor *desc,
960 struct usb_ss_ep_comp_descriptor *ep_comp)
961{
962 u8 type;
963 u16 max;
964 int num_req_streams = 0;
965
966 /* endpoint already claimed? */
967 if (ep->claimed)
968 return 0;
969
970 type = usb_endpoint_type(desc);
971 max = 0x7ff & usb_endpoint_maxp(desc);
972
973 if (usb_endpoint_dir_in(desc) && !ep->caps.dir_in)
974 return 0;
975 if (usb_endpoint_dir_out(desc) && !ep->caps.dir_out)
976 return 0;
977
978 if (max > ep->maxpacket_limit)
979 return 0;
980
981 /* "high bandwidth" works only at high speed */
Benjamin Herrenschmidt57d4bb12018-01-12 17:50:02 +1100982 if (!gadget_is_dualspeed(gadget) && usb_endpoint_maxp_mult(desc) > 1)
Robert Baldyga4278c682015-08-06 14:11:11 +0200983 return 0;
984
985 switch (type) {
986 case USB_ENDPOINT_XFER_CONTROL:
987 /* only support ep0 for portable CONTROL traffic */
988 return 0;
989 case USB_ENDPOINT_XFER_ISOC:
990 if (!ep->caps.type_iso)
991 return 0;
992 /* ISO: limit 1023 bytes full speed, 1024 high/super speed */
993 if (!gadget_is_dualspeed(gadget) && max > 1023)
994 return 0;
995 break;
996 case USB_ENDPOINT_XFER_BULK:
997 if (!ep->caps.type_bulk)
998 return 0;
999 if (ep_comp && gadget_is_superspeed(gadget)) {
1000 /* Get the number of required streams from the
1001 * EP companion descriptor and see if the EP
1002 * matches it
1003 */
1004 num_req_streams = ep_comp->bmAttributes & 0x1f;
1005 if (num_req_streams > ep->max_streams)
1006 return 0;
1007 }
1008 break;
1009 case USB_ENDPOINT_XFER_INT:
1010 /* Bulk endpoints handle interrupt transfers,
1011 * except the toggle-quirky iso-synch kind
1012 */
1013 if (!ep->caps.type_int && !ep->caps.type_bulk)
1014 return 0;
1015 /* INT: limit 64 bytes full speed, 1024 high/super speed */
1016 if (!gadget_is_dualspeed(gadget) && max > 64)
1017 return 0;
1018 break;
1019 }
1020
1021 return 1;
1022}
1023EXPORT_SYMBOL_GPL(usb_gadget_ep_match_desc);
1024
1025/* ------------------------------------------------------------------------- */
1026
Felipe Balbi5702f752013-07-17 11:09:49 +03001027static void usb_gadget_state_work(struct work_struct *work)
1028{
Peter Chendfea9c92015-03-06 10:36:02 +08001029 struct usb_gadget *gadget = work_to_gadget(work);
1030 struct usb_udc *udc = gadget->udc;
Felipe Balbi5702f752013-07-17 11:09:49 +03001031
Peter Chendfea9c92015-03-06 10:36:02 +08001032 if (udc)
1033 sysfs_notify(&udc->dev.kobj, NULL, "state");
Felipe Balbi5702f752013-07-17 11:09:49 +03001034}
1035
Felipe Balbi49401f42011-12-19 12:57:04 +02001036void usb_gadget_set_state(struct usb_gadget *gadget,
1037 enum usb_device_state state)
1038{
1039 gadget->state = state;
Felipe Balbi5702f752013-07-17 11:09:49 +03001040 schedule_work(&gadget->work);
Felipe Balbi49401f42011-12-19 12:57:04 +02001041}
1042EXPORT_SYMBOL_GPL(usb_gadget_set_state);
1043
1044/* ------------------------------------------------------------------------- */
1045
Peter Chen628ef0d2015-03-06 10:36:03 +08001046static void usb_udc_connect_control(struct usb_udc *udc)
1047{
1048 if (udc->vbus)
1049 usb_gadget_connect(udc->gadget);
1050 else
1051 usb_gadget_disconnect(udc->gadget);
1052}
1053
1054/**
1055 * usb_udc_vbus_handler - updates the udc core vbus status, and try to
1056 * connect or disconnect gadget
1057 * @gadget: The gadget which vbus change occurs
1058 * @status: The vbus status
1059 *
1060 * The udc driver calls it when it wants to connect or disconnect gadget
1061 * according to vbus status.
1062 */
1063void usb_udc_vbus_handler(struct usb_gadget *gadget, bool status)
1064{
1065 struct usb_udc *udc = gadget->udc;
1066
1067 if (udc) {
1068 udc->vbus = status;
1069 usb_udc_connect_control(udc);
1070 }
1071}
1072EXPORT_SYMBOL_GPL(usb_udc_vbus_handler);
1073
Felipe Balbi2ccea032011-06-28 16:33:46 +03001074/**
Peter Chen974a70b2014-09-12 09:32:41 +08001075 * usb_gadget_udc_reset - notifies the udc core that bus reset occurs
1076 * @gadget: The gadget which bus reset occurs
1077 * @driver: The gadget driver we want to notify
1078 *
1079 * If the udc driver has bus reset handler, it needs to call this when the bus
1080 * reset occurs, it notifies the gadget driver that the bus reset occurs as
1081 * well as updates gadget state.
1082 */
1083void usb_gadget_udc_reset(struct usb_gadget *gadget,
1084 struct usb_gadget_driver *driver)
1085{
1086 driver->reset(gadget);
1087 usb_gadget_set_state(gadget, USB_STATE_DEFAULT);
1088}
1089EXPORT_SYMBOL_GPL(usb_gadget_udc_reset);
1090
1091/**
Sebastian Andrzej Siewior352c2dc2011-06-23 14:26:15 +02001092 * usb_gadget_udc_start - tells usb device controller to start up
Felipe Balbi2c683342014-10-17 11:34:07 -05001093 * @udc: The UDC to be started
Sebastian Andrzej Siewior352c2dc2011-06-23 14:26:15 +02001094 *
1095 * This call is issued by the UDC Class driver when it's about
1096 * to register a gadget driver to the device controller, before
1097 * calling gadget driver's bind() method.
1098 *
1099 * It allows the controller to be powered off until strictly
1100 * necessary to have it powered on.
1101 *
1102 * Returns zero on success, else negative errno.
1103 */
Felipe Balbi2c683342014-10-17 11:34:07 -05001104static inline int usb_gadget_udc_start(struct usb_udc *udc)
Sebastian Andrzej Siewior352c2dc2011-06-23 14:26:15 +02001105{
Felipe Balbi2c683342014-10-17 11:34:07 -05001106 return udc->gadget->ops->udc_start(udc->gadget, udc->driver);
Sebastian Andrzej Siewior352c2dc2011-06-23 14:26:15 +02001107}
1108
1109/**
Sebastian Andrzej Siewior352c2dc2011-06-23 14:26:15 +02001110 * usb_gadget_udc_stop - tells usb device controller we don't need it anymore
1111 * @gadget: The device we want to stop activity
1112 * @driver: The driver to unbind from @gadget
1113 *
1114 * This call is issued by the UDC Class driver after calling
1115 * gadget driver's unbind() method.
1116 *
1117 * The details are implementation specific, but it can go as
1118 * far as powering off UDC completely and disable its data
1119 * line pullups.
1120 */
Felipe Balbi2c683342014-10-17 11:34:07 -05001121static inline void usb_gadget_udc_stop(struct usb_udc *udc)
Sebastian Andrzej Siewior352c2dc2011-06-23 14:26:15 +02001122{
Felipe Balbi22835b82014-10-17 12:05:12 -05001123 udc->gadget->ops->udc_stop(udc->gadget);
Sebastian Andrzej Siewior352c2dc2011-06-23 14:26:15 +02001124}
1125
1126/**
Felipe Balbi2ccea032011-06-28 16:33:46 +03001127 * usb_udc_release - release the usb_udc struct
1128 * @dev: the dev member within usb_udc
1129 *
1130 * This is called by driver's core in order to free memory once the last
1131 * reference is released.
1132 */
1133static void usb_udc_release(struct device *dev)
1134{
1135 struct usb_udc *udc;
1136
1137 udc = container_of(dev, struct usb_udc, dev);
1138 dev_dbg(dev, "releasing '%s'\n", dev_name(dev));
1139 kfree(udc);
1140}
1141
Sebastian Andrzej Siewior019f9762011-06-23 14:28:38 +02001142static const struct attribute_group *usb_udc_attr_groups[];
Felipe Balbi792bfcf2013-02-26 14:47:44 +02001143
1144static void usb_udc_nop_release(struct device *dev)
1145{
1146 dev_vdbg(dev, "%s\n", __func__);
1147}
1148
Krzysztof Opasiak881b5222017-01-16 08:40:57 +01001149/* should be called with udc_lock held */
1150static int check_pending_gadget_drivers(struct usb_udc *udc)
1151{
1152 struct usb_gadget_driver *driver;
1153 int ret = 0;
1154
1155 list_for_each_entry(driver, &gadget_driver_pending_list, pending)
1156 if (!driver->udc_name || strcmp(driver->udc_name,
1157 dev_name(&udc->dev)) == 0) {
1158 ret = udc_bind_to_driver(udc, driver);
1159 if (ret != -EPROBE_DEFER)
1160 list_del(&driver->pending);
1161 break;
1162 }
1163
1164 return ret;
1165}
1166
Felipe Balbi2ccea032011-06-28 16:33:46 +03001167/**
Felipe Balbi792bfcf2013-02-26 14:47:44 +02001168 * usb_add_gadget_udc_release - adds a new gadget to the udc class driver list
1169 * @parent: the parent device to this udc. Usually the controller driver's
1170 * device.
1171 * @gadget: the gadget to be added to the list.
1172 * @release: a gadget release function.
Felipe Balbi2ccea032011-06-28 16:33:46 +03001173 *
1174 * Returns zero on success, negative errno otherwise.
1175 */
Felipe Balbi792bfcf2013-02-26 14:47:44 +02001176int usb_add_gadget_udc_release(struct device *parent, struct usb_gadget *gadget,
1177 void (*release)(struct device *dev))
Felipe Balbi2ccea032011-06-28 16:33:46 +03001178{
1179 struct usb_udc *udc;
1180 int ret = -ENOMEM;
1181
1182 udc = kzalloc(sizeof(*udc), GFP_KERNEL);
1183 if (!udc)
1184 goto err1;
1185
Felipe Balbi7bce4012013-01-24 17:41:00 +02001186 dev_set_name(&gadget->dev, "gadget");
Felipe Balbi5702f752013-07-17 11:09:49 +03001187 INIT_WORK(&gadget->work, usb_gadget_state_work);
Felipe Balbi2ed14322013-02-26 10:59:41 +02001188 gadget->dev.parent = parent;
Felipe Balbif07bd56b2013-01-24 14:52:24 +02001189
Ajay Agarwalf0189292018-02-12 18:27:00 +05301190 dma_set_coherent_mask(&gadget->dev, parent->coherent_dma_mask);
1191 gadget->dev.dma_parms = parent->dma_parms;
1192 gadget->dev.dma_mask = parent->dma_mask;
1193
Felipe Balbiddf47cc2013-02-26 15:25:41 +02001194 if (release)
Felipe Balbi792bfcf2013-02-26 14:47:44 +02001195 gadget->dev.release = release;
Felipe Balbiddf47cc2013-02-26 15:25:41 +02001196 else
1197 gadget->dev.release = usb_udc_nop_release;
Felipe Balbi792bfcf2013-02-26 14:47:44 +02001198
Felipe Balbi7bce4012013-01-24 17:41:00 +02001199 ret = device_register(&gadget->dev);
1200 if (ret)
1201 goto err2;
Felipe Balbif07bd56b2013-01-24 14:52:24 +02001202
Felipe Balbi2ccea032011-06-28 16:33:46 +03001203 device_initialize(&udc->dev);
1204 udc->dev.release = usb_udc_release;
1205 udc->dev.class = udc_class;
Sebastian Andrzej Siewior019f9762011-06-23 14:28:38 +02001206 udc->dev.groups = usb_udc_attr_groups;
Felipe Balbi2ccea032011-06-28 16:33:46 +03001207 udc->dev.parent = parent;
1208 ret = dev_set_name(&udc->dev, "%s", kobject_name(&parent->kobj));
1209 if (ret)
Felipe Balbif07bd56b2013-01-24 14:52:24 +02001210 goto err3;
Felipe Balbi2ccea032011-06-28 16:33:46 +03001211
1212 udc->gadget = gadget;
Peter Chendfea9c92015-03-06 10:36:02 +08001213 gadget->udc = udc;
Felipe Balbi2ccea032011-06-28 16:33:46 +03001214
1215 mutex_lock(&udc_lock);
1216 list_add_tail(&udc->list, &udc_list);
1217
1218 ret = device_add(&udc->dev);
1219 if (ret)
Felipe Balbif07bd56b2013-01-24 14:52:24 +02001220 goto err4;
Felipe Balbi2ccea032011-06-28 16:33:46 +03001221
Felipe Balbi49401f42011-12-19 12:57:04 +02001222 usb_gadget_set_state(gadget, USB_STATE_NOTATTACHED);
Peter Chen628ef0d2015-03-06 10:36:03 +08001223 udc->vbus = true;
Felipe Balbi49401f42011-12-19 12:57:04 +02001224
Ruslan Bilovol855ed042015-11-23 09:56:38 +01001225 /* pick up one of pending gadget drivers */
Krzysztof Opasiak881b5222017-01-16 08:40:57 +01001226 ret = check_pending_gadget_drivers(udc);
1227 if (ret)
1228 goto err5;
Ruslan Bilovol855ed042015-11-23 09:56:38 +01001229
Felipe Balbi2ccea032011-06-28 16:33:46 +03001230 mutex_unlock(&udc_lock);
1231
1232 return 0;
Felipe Balbif07bd56b2013-01-24 14:52:24 +02001233
Peter Chen17a1dc52016-07-11 09:58:16 +08001234err5:
1235 device_del(&udc->dev);
1236
Felipe Balbif07bd56b2013-01-24 14:52:24 +02001237err4:
Felipe Balbi2ccea032011-06-28 16:33:46 +03001238 list_del(&udc->list);
1239 mutex_unlock(&udc_lock);
1240
Felipe Balbif07bd56b2013-01-24 14:52:24 +02001241err3:
Felipe Balbi2ccea032011-06-28 16:33:46 +03001242 put_device(&udc->dev);
Alan Sternc93e64e2015-01-16 11:32:51 -05001243 device_del(&gadget->dev);
Felipe Balbi2ccea032011-06-28 16:33:46 +03001244
Felipe Balbif07bd56b2013-01-24 14:52:24 +02001245err2:
Felipe Balbi7bce4012013-01-24 17:41:00 +02001246 put_device(&gadget->dev);
Felipe Balbic5dbc222013-04-02 17:06:28 +03001247 kfree(udc);
Felipe Balbi7bce4012013-01-24 17:41:00 +02001248
Felipe Balbi2ccea032011-06-28 16:33:46 +03001249err1:
1250 return ret;
1251}
Felipe Balbi792bfcf2013-02-26 14:47:44 +02001252EXPORT_SYMBOL_GPL(usb_add_gadget_udc_release);
1253
Felipe Balbi2ccea032011-06-28 16:33:46 +03001254/**
Marek Szyprowski175f7122016-02-18 11:34:43 +01001255 * usb_get_gadget_udc_name - get the name of the first UDC controller
1256 * This functions returns the name of the first UDC controller in the system.
1257 * Please note that this interface is usefull only for legacy drivers which
1258 * assume that there is only one UDC controller in the system and they need to
1259 * get its name before initialization. There is no guarantee that the UDC
1260 * of the returned name will be still available, when gadget driver registers
1261 * itself.
1262 *
1263 * Returns pointer to string with UDC controller name on success, NULL
1264 * otherwise. Caller should kfree() returned string.
1265 */
1266char *usb_get_gadget_udc_name(void)
1267{
1268 struct usb_udc *udc;
1269 char *name = NULL;
1270
1271 /* For now we take the first available UDC */
1272 mutex_lock(&udc_lock);
1273 list_for_each_entry(udc, &udc_list, list) {
1274 if (!udc->driver) {
1275 name = kstrdup(udc->gadget->name, GFP_KERNEL);
1276 break;
1277 }
1278 }
1279 mutex_unlock(&udc_lock);
1280 return name;
1281}
1282EXPORT_SYMBOL_GPL(usb_get_gadget_udc_name);
1283
1284/**
Felipe Balbi2ccea032011-06-28 16:33:46 +03001285 * usb_add_gadget_udc - adds a new gadget to the udc class driver list
1286 * @parent: the parent device to this udc. Usually the controller
1287 * driver's device.
1288 * @gadget: the gadget to be added to the list
1289 *
1290 * Returns zero on success, negative errno otherwise.
1291 */
1292int usb_add_gadget_udc(struct device *parent, struct usb_gadget *gadget)
1293{
Felipe Balbi792bfcf2013-02-26 14:47:44 +02001294 return usb_add_gadget_udc_release(parent, gadget, NULL);
Felipe Balbi2ccea032011-06-28 16:33:46 +03001295}
1296EXPORT_SYMBOL_GPL(usb_add_gadget_udc);
1297
1298static void usb_gadget_remove_driver(struct usb_udc *udc)
1299{
1300 dev_dbg(&udc->dev, "unregistering UDC driver [%s]\n",
Felipe Balbi8da9fe82014-10-17 11:54:46 -05001301 udc->driver->function);
Felipe Balbi2ccea032011-06-28 16:33:46 +03001302
1303 kobject_uevent(&udc->dev.kobj, KOBJ_CHANGE);
1304
Felipe Balbi2d7ebbb2013-01-24 11:00:15 +02001305 usb_gadget_disconnect(udc->gadget);
1306 udc->driver->disconnect(udc->gadget);
1307 udc->driver->unbind(udc->gadget);
Felipe Balbi2c683342014-10-17 11:34:07 -05001308 usb_gadget_udc_stop(udc);
Felipe Balbi2ccea032011-06-28 16:33:46 +03001309
1310 udc->driver = NULL;
1311 udc->dev.driver = NULL;
Felipe Balbi70d3a492013-02-26 13:51:24 +02001312 udc->gadget->dev.driver = NULL;
Felipe Balbi2ccea032011-06-28 16:33:46 +03001313}
1314
1315/**
1316 * usb_del_gadget_udc - deletes @udc from udc_list
1317 * @gadget: the gadget to be removed.
1318 *
1319 * This, will call usb_gadget_unregister_driver() if
1320 * the @udc is still busy.
1321 */
1322void usb_del_gadget_udc(struct usb_gadget *gadget)
1323{
Peter Chendfea9c92015-03-06 10:36:02 +08001324 struct usb_udc *udc = gadget->udc;
Felipe Balbi2ccea032011-06-28 16:33:46 +03001325
Peter Chendfea9c92015-03-06 10:36:02 +08001326 if (!udc)
1327 return;
Felipe Balbi2ccea032011-06-28 16:33:46 +03001328
Felipe Balbi2ccea032011-06-28 16:33:46 +03001329 dev_vdbg(gadget->dev.parent, "unregistering gadget\n");
1330
Peter Chendfea9c92015-03-06 10:36:02 +08001331 mutex_lock(&udc_lock);
Felipe Balbi2ccea032011-06-28 16:33:46 +03001332 list_del(&udc->list);
Felipe Balbi2ccea032011-06-28 16:33:46 +03001333
Ruslan Bilovol855ed042015-11-23 09:56:38 +01001334 if (udc->driver) {
1335 struct usb_gadget_driver *driver = udc->driver;
1336
Felipe Balbi2ccea032011-06-28 16:33:46 +03001337 usb_gadget_remove_driver(udc);
Ruslan Bilovol855ed042015-11-23 09:56:38 +01001338 list_add(&driver->pending, &gadget_driver_pending_list);
1339 }
1340 mutex_unlock(&udc_lock);
Felipe Balbi2ccea032011-06-28 16:33:46 +03001341
1342 kobject_uevent(&udc->dev.kobj, KOBJ_REMOVE);
Felipe Balbi5702f752013-07-17 11:09:49 +03001343 flush_work(&gadget->work);
Felipe Balbi2ccea032011-06-28 16:33:46 +03001344 device_unregister(&udc->dev);
Felipe Balbi7bce4012013-01-24 17:41:00 +02001345 device_unregister(&gadget->dev);
Felipe Balbi2ccea032011-06-28 16:33:46 +03001346}
1347EXPORT_SYMBOL_GPL(usb_del_gadget_udc);
1348
1349/* ------------------------------------------------------------------------- */
1350
Sebastian Andrzej Siewior4c49a5f2012-12-23 21:10:19 +01001351static int udc_bind_to_driver(struct usb_udc *udc, struct usb_gadget_driver *driver)
Felipe Balbi2ccea032011-06-28 16:33:46 +03001352{
Sebastian Andrzej Siewior4c49a5f2012-12-23 21:10:19 +01001353 int ret;
Felipe Balbi2ccea032011-06-28 16:33:46 +03001354
Felipe Balbi2ccea032011-06-28 16:33:46 +03001355 dev_dbg(&udc->dev, "registering UDC driver [%s]\n",
1356 driver->function);
1357
1358 udc->driver = driver;
1359 udc->dev.driver = &driver->driver;
Felipe Balbi70d3a492013-02-26 13:51:24 +02001360 udc->gadget->dev.driver = &driver->driver;
Felipe Balbi2ccea032011-06-28 16:33:46 +03001361
Felipe Balbi2d7ebbb2013-01-24 11:00:15 +02001362 ret = driver->bind(udc->gadget, driver);
1363 if (ret)
1364 goto err1;
Felipe Balbi2c683342014-10-17 11:34:07 -05001365 ret = usb_gadget_udc_start(udc);
Felipe Balbi2d7ebbb2013-01-24 11:00:15 +02001366 if (ret) {
1367 driver->unbind(udc->gadget);
1368 goto err1;
Sebastian Andrzej Siewior352c2dc2011-06-23 14:26:15 +02001369 }
Peter Chen628ef0d2015-03-06 10:36:03 +08001370 usb_udc_connect_control(udc);
Felipe Balbi2ccea032011-06-28 16:33:46 +03001371
1372 kobject_uevent(&udc->dev.kobj, KOBJ_CHANGE);
Felipe Balbi2ccea032011-06-28 16:33:46 +03001373 return 0;
Felipe Balbi2ccea032011-06-28 16:33:46 +03001374err1:
Fabio Estevamf8cffc82013-09-28 00:05:34 -03001375 if (ret != -EISNAM)
1376 dev_err(&udc->dev, "failed to start %s: %d\n",
Felipe Balbi2ccea032011-06-28 16:33:46 +03001377 udc->driver->function, ret);
1378 udc->driver = NULL;
1379 udc->dev.driver = NULL;
Felipe Balbi70d3a492013-02-26 13:51:24 +02001380 udc->gadget->dev.driver = NULL;
Sebastian Andrzej Siewior4c49a5f2012-12-23 21:10:19 +01001381 return ret;
1382}
1383
Sebastian Andrzej Siewior4c49a5f2012-12-23 21:10:19 +01001384int usb_gadget_probe_driver(struct usb_gadget_driver *driver)
1385{
1386 struct usb_udc *udc = NULL;
Ruslan Bilovol2284b292015-11-23 09:56:35 +01001387 int ret = -ENODEV;
Sebastian Andrzej Siewior4c49a5f2012-12-23 21:10:19 +01001388
1389 if (!driver || !driver->bind || !driver->setup)
1390 return -EINVAL;
1391
1392 mutex_lock(&udc_lock);
Ruslan Bilovol2284b292015-11-23 09:56:35 +01001393 if (driver->udc_name) {
1394 list_for_each_entry(udc, &udc_list, list) {
1395 ret = strcmp(driver->udc_name, dev_name(&udc->dev));
1396 if (!ret)
1397 break;
1398 }
Felix Hädicke64244ed2016-12-29 23:02:11 +01001399 if (ret)
1400 ret = -ENODEV;
1401 else if (udc->driver)
1402 ret = -EBUSY;
1403 else
Sebastian Andrzej Siewior4c49a5f2012-12-23 21:10:19 +01001404 goto found;
Ruslan Bilovol2284b292015-11-23 09:56:35 +01001405 } else {
1406 list_for_each_entry(udc, &udc_list, list) {
1407 /* For now we take the first one */
1408 if (!udc->driver)
1409 goto found;
1410 }
Sebastian Andrzej Siewior4c49a5f2012-12-23 21:10:19 +01001411 }
1412
Krzysztof Opasiakf1bddbb2016-05-05 10:46:05 +02001413 if (!driver->match_existing_only) {
1414 list_add_tail(&driver->pending, &gadget_driver_pending_list);
1415 pr_info("udc-core: couldn't find an available UDC - added [%s] to list of pending drivers\n",
1416 driver->function);
1417 ret = 0;
1418 }
1419
Sebastian Andrzej Siewior4c49a5f2012-12-23 21:10:19 +01001420 mutex_unlock(&udc_lock);
Krzysztof Opasiakf1bddbb2016-05-05 10:46:05 +02001421 return ret;
Sebastian Andrzej Siewior4c49a5f2012-12-23 21:10:19 +01001422found:
1423 ret = udc_bind_to_driver(udc, driver);
Felipe Balbi2ccea032011-06-28 16:33:46 +03001424 mutex_unlock(&udc_lock);
1425 return ret;
1426}
1427EXPORT_SYMBOL_GPL(usb_gadget_probe_driver);
1428
1429int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
1430{
1431 struct usb_udc *udc = NULL;
1432 int ret = -ENODEV;
1433
1434 if (!driver || !driver->unbind)
1435 return -EINVAL;
1436
1437 mutex_lock(&udc_lock);
Krzysztof Opasiak881b5222017-01-16 08:40:57 +01001438 list_for_each_entry(udc, &udc_list, list) {
Felipe Balbi2ccea032011-06-28 16:33:46 +03001439 if (udc->driver == driver) {
1440 usb_gadget_remove_driver(udc);
Peter Chenb5fb8d02014-04-29 13:26:29 +08001441 usb_gadget_set_state(udc->gadget,
Krzysztof Opasiak881b5222017-01-16 08:40:57 +01001442 USB_STATE_NOTATTACHED);
1443
1444 /* Maybe there is someone waiting for this UDC? */
1445 check_pending_gadget_drivers(udc);
1446 /*
1447 * For now we ignore bind errors as probably it's
1448 * not a valid reason to fail other's gadget unbind
1449 */
Felipe Balbi2ccea032011-06-28 16:33:46 +03001450 ret = 0;
1451 break;
1452 }
Krzysztof Opasiak881b5222017-01-16 08:40:57 +01001453 }
Felipe Balbi2ccea032011-06-28 16:33:46 +03001454
Ruslan Bilovol855ed042015-11-23 09:56:38 +01001455 if (ret) {
1456 list_del(&driver->pending);
1457 ret = 0;
1458 }
Felipe Balbi2ccea032011-06-28 16:33:46 +03001459 mutex_unlock(&udc_lock);
1460 return ret;
1461}
1462EXPORT_SYMBOL_GPL(usb_gadget_unregister_driver);
1463
1464/* ------------------------------------------------------------------------- */
1465
1466static ssize_t usb_udc_srp_store(struct device *dev,
1467 struct device_attribute *attr, const char *buf, size_t n)
1468{
Felipe Balbi1d91a962011-09-08 14:11:17 +03001469 struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
Felipe Balbi2ccea032011-06-28 16:33:46 +03001470
1471 if (sysfs_streq(buf, "1"))
1472 usb_gadget_wakeup(udc->gadget);
1473
1474 return n;
1475}
1476static DEVICE_ATTR(srp, S_IWUSR, NULL, usb_udc_srp_store);
1477
1478static ssize_t usb_udc_softconn_store(struct device *dev,
1479 struct device_attribute *attr, const char *buf, size_t n)
1480{
Felipe Balbi865569b2011-09-08 14:12:18 +03001481 struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
Felipe Balbi2ccea032011-06-28 16:33:46 +03001482
Felipe Balbibfa6b182014-10-17 11:10:25 -05001483 if (!udc->driver) {
1484 dev_err(dev, "soft-connect without a gadget driver\n");
1485 return -EOPNOTSUPP;
1486 }
1487
Felipe Balbi2ccea032011-06-28 16:33:46 +03001488 if (sysfs_streq(buf, "connect")) {
Felipe Balbi2c683342014-10-17 11:34:07 -05001489 usb_gadget_udc_start(udc);
Felipe Balbi2ccea032011-06-28 16:33:46 +03001490 usb_gadget_connect(udc->gadget);
1491 } else if (sysfs_streq(buf, "disconnect")) {
Felipe Balbi83a787a2012-04-27 11:02:15 +03001492 usb_gadget_disconnect(udc->gadget);
Felipe Balbi0abd0692014-10-09 10:12:24 -05001493 udc->driver->disconnect(udc->gadget);
Felipe Balbi2c683342014-10-17 11:34:07 -05001494 usb_gadget_udc_stop(udc);
Felipe Balbi2ccea032011-06-28 16:33:46 +03001495 } else {
1496 dev_err(dev, "unsupported command '%s'\n", buf);
1497 return -EINVAL;
1498 }
1499
1500 return n;
1501}
1502static DEVICE_ATTR(soft_connect, S_IWUSR, NULL, usb_udc_softconn_store);
1503
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07001504static ssize_t state_show(struct device *dev, struct device_attribute *attr,
1505 char *buf)
Felipe Balbi49401f42011-12-19 12:57:04 +02001506{
1507 struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
1508 struct usb_gadget *gadget = udc->gadget;
1509
1510 return sprintf(buf, "%s\n", usb_state_string(gadget->state));
1511}
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07001512static DEVICE_ATTR_RO(state);
Felipe Balbi49401f42011-12-19 12:57:04 +02001513
Michal Nazarewiczd327ab52011-11-19 18:27:37 +01001514#define USB_UDC_SPEED_ATTR(name, param) \
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07001515ssize_t name##_show(struct device *dev, \
Michal Nazarewiczd327ab52011-11-19 18:27:37 +01001516 struct device_attribute *attr, char *buf) \
1517{ \
1518 struct usb_udc *udc = container_of(dev, struct usb_udc, dev); \
1519 return snprintf(buf, PAGE_SIZE, "%s\n", \
1520 usb_speed_string(udc->gadget->param)); \
1521} \
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07001522static DEVICE_ATTR_RO(name)
Michal Nazarewiczd327ab52011-11-19 18:27:37 +01001523
1524static USB_UDC_SPEED_ATTR(current_speed, speed);
1525static USB_UDC_SPEED_ATTR(maximum_speed, max_speed);
1526
Felipe Balbi2ccea032011-06-28 16:33:46 +03001527#define USB_UDC_ATTR(name) \
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07001528ssize_t name##_show(struct device *dev, \
Felipe Balbi2ccea032011-06-28 16:33:46 +03001529 struct device_attribute *attr, char *buf) \
1530{ \
Sebastian Andrzej Siewior019f9762011-06-23 14:28:38 +02001531 struct usb_udc *udc = container_of(dev, struct usb_udc, dev); \
Felipe Balbi2ccea032011-06-28 16:33:46 +03001532 struct usb_gadget *gadget = udc->gadget; \
1533 \
1534 return snprintf(buf, PAGE_SIZE, "%d\n", gadget->name); \
1535} \
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07001536static DEVICE_ATTR_RO(name)
Felipe Balbi2ccea032011-06-28 16:33:46 +03001537
Felipe Balbi2ccea032011-06-28 16:33:46 +03001538static USB_UDC_ATTR(is_otg);
1539static USB_UDC_ATTR(is_a_peripheral);
1540static USB_UDC_ATTR(b_hnp_enable);
1541static USB_UDC_ATTR(a_hnp_support);
1542static USB_UDC_ATTR(a_alt_hnp_support);
Peter Chen3f6dd4f2015-01-28 16:32:42 +08001543static USB_UDC_ATTR(is_selfpowered);
Felipe Balbi2ccea032011-06-28 16:33:46 +03001544
1545static struct attribute *usb_udc_attrs[] = {
1546 &dev_attr_srp.attr,
1547 &dev_attr_soft_connect.attr,
Felipe Balbi49401f42011-12-19 12:57:04 +02001548 &dev_attr_state.attr,
Michal Nazarewiczd327ab52011-11-19 18:27:37 +01001549 &dev_attr_current_speed.attr,
1550 &dev_attr_maximum_speed.attr,
Felipe Balbi2ccea032011-06-28 16:33:46 +03001551
Felipe Balbi2ccea032011-06-28 16:33:46 +03001552 &dev_attr_is_otg.attr,
1553 &dev_attr_is_a_peripheral.attr,
1554 &dev_attr_b_hnp_enable.attr,
1555 &dev_attr_a_hnp_support.attr,
1556 &dev_attr_a_alt_hnp_support.attr,
Peter Chen3f6dd4f2015-01-28 16:32:42 +08001557 &dev_attr_is_selfpowered.attr,
Felipe Balbi2ccea032011-06-28 16:33:46 +03001558 NULL,
1559};
1560
1561static const struct attribute_group usb_udc_attr_group = {
1562 .attrs = usb_udc_attrs,
1563};
1564
1565static const struct attribute_group *usb_udc_attr_groups[] = {
1566 &usb_udc_attr_group,
1567 NULL,
1568};
1569
1570static int usb_udc_uevent(struct device *dev, struct kobj_uevent_env *env)
1571{
1572 struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
1573 int ret;
1574
1575 ret = add_uevent_var(env, "USB_UDC_NAME=%s", udc->gadget->name);
1576 if (ret) {
1577 dev_err(dev, "failed to add uevent USB_UDC_NAME\n");
1578 return ret;
1579 }
1580
1581 if (udc->driver) {
1582 ret = add_uevent_var(env, "USB_UDC_DRIVER=%s",
1583 udc->driver->function);
1584 if (ret) {
1585 dev_err(dev, "failed to add uevent USB_UDC_DRIVER\n");
1586 return ret;
1587 }
1588 }
1589
1590 return 0;
1591}
1592
1593static int __init usb_udc_init(void)
1594{
1595 udc_class = class_create(THIS_MODULE, "udc");
1596 if (IS_ERR(udc_class)) {
1597 pr_err("failed to create udc class --> %ld\n",
1598 PTR_ERR(udc_class));
1599 return PTR_ERR(udc_class);
1600 }
1601
1602 udc_class->dev_uevent = usb_udc_uevent;
Felipe Balbi2ccea032011-06-28 16:33:46 +03001603 return 0;
1604}
1605subsys_initcall(usb_udc_init);
1606
1607static void __exit usb_udc_exit(void)
1608{
1609 class_destroy(udc_class);
1610}
1611module_exit(usb_udc_exit);
1612
1613MODULE_DESCRIPTION("UDC Framework");
1614MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
1615MODULE_LICENSE("GPL v2");