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