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 *
33 * This represents the internal data structure which is used by the UDC-class
34 * to hold information about udc driver and gadget together.
35 */
36 struct usb_udc {
37 struct usb_gadget_driver *driver;
38 struct usb_gadget *gadget;
39 struct device dev;
40 struct list_head list;
41 bool vbus;
42 };
43
44 static struct class *udc_class;
45 static LIST_HEAD(udc_list);
46 static LIST_HEAD(gadget_driver_pending_list);
47 static DEFINE_MUTEX(udc_lock);
48
49 static int udc_bind_to_driver(struct usb_udc *udc,
50 struct usb_gadget_driver *driver);
51
52 /* ------------------------------------------------------------------------- */
53
54 /**
55 * usb_ep_set_maxpacket_limit - set maximum packet size limit for endpoint
56 * @ep:the endpoint being configured
57 * @maxpacket_limit:value of maximum packet size limit
58 *
59 * This function should be used only in UDC drivers to initialize endpoint
60 * (usually in probe function).
61 */
usb_ep_set_maxpacket_limit(struct usb_ep * ep,unsigned maxpacket_limit)62 void usb_ep_set_maxpacket_limit(struct usb_ep *ep,
63 unsigned maxpacket_limit)
64 {
65 ep->maxpacket_limit = maxpacket_limit;
66 ep->maxpacket = maxpacket_limit;
67
68 trace_usb_ep_set_maxpacket_limit(ep, 0);
69 }
70 EXPORT_SYMBOL_GPL(usb_ep_set_maxpacket_limit);
71
72 /**
73 * usb_ep_enable - configure endpoint, making it usable
74 * @ep:the endpoint being configured. may not be the endpoint named "ep0".
75 * drivers discover endpoints through the ep_list of a usb_gadget.
76 *
77 * When configurations are set, or when interface settings change, the driver
78 * will enable or disable the relevant endpoints. while it is enabled, an
79 * endpoint may be used for i/o until the driver receives a disconnect() from
80 * the host or until the endpoint is disabled.
81 *
82 * the ep0 implementation (which calls this routine) must ensure that the
83 * hardware capabilities of each endpoint match the descriptor provided
84 * for it. for example, an endpoint named "ep2in-bulk" would be usable
85 * for interrupt transfers as well as bulk, but it likely couldn't be used
86 * for iso transfers or for endpoint 14. some endpoints are fully
87 * configurable, with more generic names like "ep-a". (remember that for
88 * USB, "in" means "towards the USB host".)
89 *
90 * This routine must be called in process context.
91 *
92 * returns zero, or a negative error code.
93 */
usb_ep_enable(struct usb_ep * ep)94 int usb_ep_enable(struct usb_ep *ep)
95 {
96 int ret = 0;
97
98 if (ep->enabled)
99 goto out;
100
101 /* UDC drivers can't handle endpoints with maxpacket size 0 */
102 if (usb_endpoint_maxp(ep->desc) == 0) {
103 /*
104 * We should log an error message here, but we can't call
105 * dev_err() because there's no way to find the gadget
106 * given only ep.
107 */
108 ret = -EINVAL;
109 goto out;
110 }
111
112 ret = ep->ops->enable(ep, ep->desc);
113 if (ret)
114 goto out;
115
116 ep->enabled = true;
117
118 out:
119 trace_usb_ep_enable(ep, ret);
120
121 return ret;
122 }
123 EXPORT_SYMBOL_GPL(usb_ep_enable);
124
125 /**
126 * usb_ep_disable - endpoint is no longer usable
127 * @ep:the endpoint being unconfigured. may not be the endpoint named "ep0".
128 *
129 * no other task may be using this endpoint when this is called.
130 * any pending and uncompleted requests will complete with status
131 * indicating disconnect (-ESHUTDOWN) before this call returns.
132 * gadget drivers must call usb_ep_enable() again before queueing
133 * requests to the endpoint.
134 *
135 * This routine must be called in process context.
136 *
137 * returns zero, or a negative error code.
138 */
usb_ep_disable(struct usb_ep * ep)139 int usb_ep_disable(struct usb_ep *ep)
140 {
141 int ret = 0;
142
143 if (!ep->enabled)
144 goto out;
145
146 ret = ep->ops->disable(ep);
147 if (ret)
148 goto out;
149
150 ep->enabled = false;
151
152 out:
153 trace_usb_ep_disable(ep, ret);
154
155 return ret;
156 }
157 EXPORT_SYMBOL_GPL(usb_ep_disable);
158
159 /**
160 * usb_ep_alloc_request - allocate a request object to use with this endpoint
161 * @ep:the endpoint to be used with with the request
162 * @gfp_flags:GFP_* flags to use
163 *
164 * Request objects must be allocated with this call, since they normally
165 * need controller-specific setup and may even need endpoint-specific
166 * resources such as allocation of DMA descriptors.
167 * Requests may be submitted with usb_ep_queue(), and receive a single
168 * completion callback. Free requests with usb_ep_free_request(), when
169 * they are no longer needed.
170 *
171 * Returns the request, or null if one could not be allocated.
172 */
usb_ep_alloc_request(struct usb_ep * ep,gfp_t gfp_flags)173 struct usb_request *usb_ep_alloc_request(struct usb_ep *ep,
174 gfp_t gfp_flags)
175 {
176 struct usb_request *req = NULL;
177
178 req = ep->ops->alloc_request(ep, gfp_flags);
179
180 trace_usb_ep_alloc_request(ep, req, req ? 0 : -ENOMEM);
181
182 return req;
183 }
184 EXPORT_SYMBOL_GPL(usb_ep_alloc_request);
185
186 /**
187 * usb_ep_free_request - frees a request object
188 * @ep:the endpoint associated with the request
189 * @req:the request being freed
190 *
191 * Reverses the effect of usb_ep_alloc_request().
192 * Caller guarantees the request is not queued, and that it will
193 * no longer be requeued (or otherwise used).
194 */
usb_ep_free_request(struct usb_ep * ep,struct usb_request * req)195 void usb_ep_free_request(struct usb_ep *ep,
196 struct usb_request *req)
197 {
198 trace_usb_ep_free_request(ep, req, 0);
199 ep->ops->free_request(ep, req);
200 }
201 EXPORT_SYMBOL_GPL(usb_ep_free_request);
202
203 /**
204 * usb_ep_queue - queues (submits) an I/O request to an endpoint.
205 * @ep:the endpoint associated with the request
206 * @req:the request being submitted
207 * @gfp_flags: GFP_* flags to use in case the lower level driver couldn't
208 * pre-allocate all necessary memory with the request.
209 *
210 * This tells the device controller to perform the specified request through
211 * that endpoint (reading or writing a buffer). When the request completes,
212 * including being canceled by usb_ep_dequeue(), the request's completion
213 * routine is called to return the request to the driver. Any endpoint
214 * (except control endpoints like ep0) may have more than one transfer
215 * request queued; they complete in FIFO order. Once a gadget driver
216 * submits a request, that request may not be examined or modified until it
217 * is given back to that driver through the completion callback.
218 *
219 * Each request is turned into one or more packets. The controller driver
220 * never merges adjacent requests into the same packet. OUT transfers
221 * will sometimes use data that's already buffered in the hardware.
222 * Drivers can rely on the fact that the first byte of the request's buffer
223 * always corresponds to the first byte of some USB packet, for both
224 * IN and OUT transfers.
225 *
226 * Bulk endpoints can queue any amount of data; the transfer is packetized
227 * automatically. The last packet will be short if the request doesn't fill it
228 * out completely. Zero length packets (ZLPs) should be avoided in portable
229 * protocols since not all usb hardware can successfully handle zero length
230 * packets. (ZLPs may be explicitly written, and may be implicitly written if
231 * the request 'zero' flag is set.) Bulk endpoints may also be used
232 * for interrupt transfers; but the reverse is not true, and some endpoints
233 * won't support every interrupt transfer. (Such as 768 byte packets.)
234 *
235 * Interrupt-only endpoints are less functional than bulk endpoints, for
236 * example by not supporting queueing or not handling buffers that are
237 * larger than the endpoint's maxpacket size. They may also treat data
238 * toggle differently.
239 *
240 * Control endpoints ... after getting a setup() callback, the driver queues
241 * one response (even if it would be zero length). That enables the
242 * status ack, after transferring data as specified in the response. Setup
243 * functions may return negative error codes to generate protocol stalls.
244 * (Note that some USB device controllers disallow protocol stall responses
245 * in some cases.) When control responses are deferred (the response is
246 * written after the setup callback returns), then usb_ep_set_halt() may be
247 * used on ep0 to trigger protocol stalls. Depending on the controller,
248 * it may not be possible to trigger a status-stage protocol stall when the
249 * data stage is over, that is, from within the response's completion
250 * routine.
251 *
252 * For periodic endpoints, like interrupt or isochronous ones, the usb host
253 * arranges to poll once per interval, and the gadget driver usually will
254 * have queued some data to transfer at that time.
255 *
256 * Note that @req's ->complete() callback must never be called from
257 * within usb_ep_queue() as that can create deadlock situations.
258 *
259 * This routine may be called in interrupt context.
260 *
261 * Returns zero, or a negative error code. Endpoints that are not enabled
262 * report errors; errors will also be
263 * reported when the usb peripheral is disconnected.
264 *
265 * If and only if @req is successfully queued (the return value is zero),
266 * @req->complete() will be called exactly once, when the Gadget core and
267 * UDC are finished with the request. When the completion function is called,
268 * control of the request is returned to the device driver which submitted it.
269 * The completion handler may then immediately free or reuse @req.
270 */
usb_ep_queue(struct usb_ep * ep,struct usb_request * req,gfp_t gfp_flags)271 int usb_ep_queue(struct usb_ep *ep,
272 struct usb_request *req, gfp_t gfp_flags)
273 {
274 int ret = 0;
275
276 if (!ep->enabled && ep->address) {
277 pr_debug("USB gadget: queue request to disabled ep 0x%x (%s)\n",
278 ep->address, ep->name);
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 gadget->udc->driver->disconnect(gadget);
736 }
737
738 out:
739 trace_usb_gadget_disconnect(gadget, ret);
740
741 return ret;
742 }
743 EXPORT_SYMBOL_GPL(usb_gadget_disconnect);
744
745 /**
746 * usb_gadget_deactivate - deactivate function which is not ready to work
747 * @gadget: the peripheral being deactivated
748 *
749 * This routine may be used during the gadget driver bind() call to prevent
750 * the peripheral from ever being visible to the USB host, unless later
751 * usb_gadget_activate() is called. For example, user mode components may
752 * need to be activated before the system can talk to hosts.
753 *
754 * Returns zero on success, else negative errno.
755 */
usb_gadget_deactivate(struct usb_gadget * gadget)756 int usb_gadget_deactivate(struct usb_gadget *gadget)
757 {
758 int ret = 0;
759
760 if (gadget->deactivated)
761 goto out;
762
763 if (gadget->connected) {
764 ret = usb_gadget_disconnect(gadget);
765 if (ret)
766 goto out;
767
768 /*
769 * If gadget was being connected before deactivation, we want
770 * to reconnect it in usb_gadget_activate().
771 */
772 gadget->connected = true;
773 }
774 gadget->deactivated = true;
775
776 out:
777 trace_usb_gadget_deactivate(gadget, ret);
778
779 return ret;
780 }
781 EXPORT_SYMBOL_GPL(usb_gadget_deactivate);
782
783 /**
784 * usb_gadget_activate - activate function which is not ready to work
785 * @gadget: the peripheral being activated
786 *
787 * This routine activates gadget which was previously deactivated with
788 * usb_gadget_deactivate() call. It calls usb_gadget_connect() if needed.
789 *
790 * Returns zero on success, else negative errno.
791 */
usb_gadget_activate(struct usb_gadget * gadget)792 int usb_gadget_activate(struct usb_gadget *gadget)
793 {
794 int ret = 0;
795
796 if (!gadget->deactivated)
797 goto out;
798
799 gadget->deactivated = false;
800
801 /*
802 * If gadget has been connected before deactivation, or became connected
803 * while it was being deactivated, we call usb_gadget_connect().
804 */
805 if (gadget->connected)
806 ret = usb_gadget_connect(gadget);
807
808 out:
809 trace_usb_gadget_activate(gadget, ret);
810
811 return ret;
812 }
813 EXPORT_SYMBOL_GPL(usb_gadget_activate);
814
815 /* ------------------------------------------------------------------------- */
816
817 #ifdef CONFIG_HAS_DMA
818
usb_gadget_map_request_by_dev(struct device * dev,struct usb_request * req,int is_in)819 int usb_gadget_map_request_by_dev(struct device *dev,
820 struct usb_request *req, int is_in)
821 {
822 if (req->length == 0)
823 return 0;
824
825 if (req->num_sgs) {
826 int mapped;
827
828 mapped = dma_map_sg(dev, req->sg, req->num_sgs,
829 is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
830 if (mapped == 0) {
831 dev_err(dev, "failed to map SGs\n");
832 return -EFAULT;
833 }
834
835 req->num_mapped_sgs = mapped;
836 } else {
837 if (is_vmalloc_addr(req->buf)) {
838 dev_err(dev, "buffer is not dma capable\n");
839 return -EFAULT;
840 } else if (object_is_on_stack(req->buf)) {
841 dev_err(dev, "buffer is on stack\n");
842 return -EFAULT;
843 }
844
845 req->dma = dma_map_single(dev, req->buf, req->length,
846 is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
847
848 if (dma_mapping_error(dev, req->dma)) {
849 dev_err(dev, "failed to map buffer\n");
850 return -EFAULT;
851 }
852
853 req->dma_mapped = 1;
854 }
855
856 return 0;
857 }
858 EXPORT_SYMBOL_GPL(usb_gadget_map_request_by_dev);
859
usb_gadget_map_request(struct usb_gadget * gadget,struct usb_request * req,int is_in)860 int usb_gadget_map_request(struct usb_gadget *gadget,
861 struct usb_request *req, int is_in)
862 {
863 return usb_gadget_map_request_by_dev(gadget->dev.parent, req, is_in);
864 }
865 EXPORT_SYMBOL_GPL(usb_gadget_map_request);
866
usb_gadget_unmap_request_by_dev(struct device * dev,struct usb_request * req,int is_in)867 void usb_gadget_unmap_request_by_dev(struct device *dev,
868 struct usb_request *req, int is_in)
869 {
870 if (req->length == 0)
871 return;
872
873 if (req->num_mapped_sgs) {
874 dma_unmap_sg(dev, req->sg, req->num_sgs,
875 is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
876
877 req->num_mapped_sgs = 0;
878 } else if (req->dma_mapped) {
879 dma_unmap_single(dev, req->dma, req->length,
880 is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
881 req->dma_mapped = 0;
882 }
883 }
884 EXPORT_SYMBOL_GPL(usb_gadget_unmap_request_by_dev);
885
usb_gadget_unmap_request(struct usb_gadget * gadget,struct usb_request * req,int is_in)886 void usb_gadget_unmap_request(struct usb_gadget *gadget,
887 struct usb_request *req, int is_in)
888 {
889 usb_gadget_unmap_request_by_dev(gadget->dev.parent, req, is_in);
890 }
891 EXPORT_SYMBOL_GPL(usb_gadget_unmap_request);
892
893 #endif /* CONFIG_HAS_DMA */
894
895 /* ------------------------------------------------------------------------- */
896
897 /**
898 * usb_gadget_giveback_request - give the request back to the gadget layer
899 * @ep: the endpoint to be used with with the request
900 * @req: the request being given back
901 *
902 * Context: in_interrupt()
903 *
904 * This is called by device controller drivers in order to return the
905 * completed request back to the gadget layer.
906 */
usb_gadget_giveback_request(struct usb_ep * ep,struct usb_request * req)907 void usb_gadget_giveback_request(struct usb_ep *ep,
908 struct usb_request *req)
909 {
910 if (likely(req->status == 0))
911 usb_led_activity(USB_LED_EVENT_GADGET);
912
913 trace_usb_gadget_giveback_request(ep, req, 0);
914
915 req->complete(ep, req);
916 }
917 EXPORT_SYMBOL_GPL(usb_gadget_giveback_request);
918
919 /* ------------------------------------------------------------------------- */
920
921 /**
922 * gadget_find_ep_by_name - returns ep whose name is the same as sting passed
923 * in second parameter or NULL if searched endpoint not found
924 * @g: controller to check for quirk
925 * @name: name of searched endpoint
926 */
gadget_find_ep_by_name(struct usb_gadget * g,const char * name)927 struct usb_ep *gadget_find_ep_by_name(struct usb_gadget *g, const char *name)
928 {
929 struct usb_ep *ep;
930
931 gadget_for_each_ep(ep, g) {
932 if (!strcmp(ep->name, name))
933 return ep;
934 }
935
936 return NULL;
937 }
938 EXPORT_SYMBOL_GPL(gadget_find_ep_by_name);
939
940 /* ------------------------------------------------------------------------- */
941
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)942 int usb_gadget_ep_match_desc(struct usb_gadget *gadget,
943 struct usb_ep *ep, struct usb_endpoint_descriptor *desc,
944 struct usb_ss_ep_comp_descriptor *ep_comp)
945 {
946 u8 type;
947 u16 max;
948 int num_req_streams = 0;
949
950 /* endpoint already claimed? */
951 if (ep->claimed)
952 return 0;
953
954 type = usb_endpoint_type(desc);
955 max = usb_endpoint_maxp(desc);
956
957 if (usb_endpoint_dir_in(desc) && !ep->caps.dir_in)
958 return 0;
959 if (usb_endpoint_dir_out(desc) && !ep->caps.dir_out)
960 return 0;
961
962 if (max > ep->maxpacket_limit)
963 return 0;
964
965 /* "high bandwidth" works only at high speed */
966 if (!gadget_is_dualspeed(gadget) && usb_endpoint_maxp_mult(desc) > 1)
967 return 0;
968
969 switch (type) {
970 case USB_ENDPOINT_XFER_CONTROL:
971 /* only support ep0 for portable CONTROL traffic */
972 return 0;
973 case USB_ENDPOINT_XFER_ISOC:
974 if (!ep->caps.type_iso)
975 return 0;
976 /* ISO: limit 1023 bytes full speed, 1024 high/super speed */
977 if (!gadget_is_dualspeed(gadget) && max > 1023)
978 return 0;
979 break;
980 case USB_ENDPOINT_XFER_BULK:
981 if (!ep->caps.type_bulk)
982 return 0;
983 if (ep_comp && gadget_is_superspeed(gadget)) {
984 /* Get the number of required streams from the
985 * EP companion descriptor and see if the EP
986 * matches it
987 */
988 num_req_streams = ep_comp->bmAttributes & 0x1f;
989 if (num_req_streams > ep->max_streams)
990 return 0;
991 }
992 break;
993 case USB_ENDPOINT_XFER_INT:
994 /* Bulk endpoints handle interrupt transfers,
995 * except the toggle-quirky iso-synch kind
996 */
997 if (!ep->caps.type_int && !ep->caps.type_bulk)
998 return 0;
999 /* INT: limit 64 bytes full speed, 1024 high/super speed */
1000 if (!gadget_is_dualspeed(gadget) && max > 64)
1001 return 0;
1002 break;
1003 }
1004
1005 return 1;
1006 }
1007 EXPORT_SYMBOL_GPL(usb_gadget_ep_match_desc);
1008
1009 /**
1010 * usb_gadget_check_config - checks if the UDC can support the binded
1011 * configuration
1012 * @gadget: controller to check the USB configuration
1013 *
1014 * Ensure that a UDC is able to support the requested resources by a
1015 * configuration, and that there are no resource limitations, such as
1016 * internal memory allocated to all requested endpoints.
1017 *
1018 * Returns zero on success, else a negative errno.
1019 */
usb_gadget_check_config(struct usb_gadget * gadget)1020 int usb_gadget_check_config(struct usb_gadget *gadget)
1021 {
1022 if (gadget->ops->check_config)
1023 return gadget->ops->check_config(gadget);
1024 return 0;
1025 }
1026 EXPORT_SYMBOL_GPL(usb_gadget_check_config);
1027
1028 /* ------------------------------------------------------------------------- */
1029
usb_gadget_state_work(struct work_struct * work)1030 static void usb_gadget_state_work(struct work_struct *work)
1031 {
1032 struct usb_gadget *gadget = work_to_gadget(work);
1033 struct usb_udc *udc = gadget->udc;
1034
1035 if (udc)
1036 sysfs_notify(&udc->dev.kobj, NULL, "state");
1037 }
1038
usb_gadget_set_state(struct usb_gadget * gadget,enum usb_device_state state)1039 void usb_gadget_set_state(struct usb_gadget *gadget,
1040 enum usb_device_state state)
1041 {
1042 gadget->state = state;
1043 schedule_work(&gadget->work);
1044 }
1045 EXPORT_SYMBOL_GPL(usb_gadget_set_state);
1046
1047 /* ------------------------------------------------------------------------- */
1048
usb_udc_connect_control(struct usb_udc * udc)1049 static void usb_udc_connect_control(struct usb_udc *udc)
1050 {
1051 if (udc->vbus)
1052 usb_gadget_connect(udc->gadget);
1053 else
1054 usb_gadget_disconnect(udc->gadget);
1055 }
1056
1057 /**
1058 * usb_udc_vbus_handler - updates the udc core vbus status, and try to
1059 * connect or disconnect gadget
1060 * @gadget: The gadget which vbus change occurs
1061 * @status: The vbus status
1062 *
1063 * The udc driver calls it when it wants to connect or disconnect gadget
1064 * according to vbus status.
1065 */
usb_udc_vbus_handler(struct usb_gadget * gadget,bool status)1066 void usb_udc_vbus_handler(struct usb_gadget *gadget, bool status)
1067 {
1068 struct usb_udc *udc = gadget->udc;
1069
1070 if (udc) {
1071 udc->vbus = status;
1072 usb_udc_connect_control(udc);
1073 }
1074 }
1075 EXPORT_SYMBOL_GPL(usb_udc_vbus_handler);
1076
1077 /**
1078 * usb_gadget_udc_reset - notifies the udc core that bus reset occurs
1079 * @gadget: The gadget which bus reset occurs
1080 * @driver: The gadget driver we want to notify
1081 *
1082 * If the udc driver has bus reset handler, it needs to call this when the bus
1083 * reset occurs, it notifies the gadget driver that the bus reset occurs as
1084 * well as updates gadget state.
1085 */
usb_gadget_udc_reset(struct usb_gadget * gadget,struct usb_gadget_driver * driver)1086 void usb_gadget_udc_reset(struct usb_gadget *gadget,
1087 struct usb_gadget_driver *driver)
1088 {
1089 driver->reset(gadget);
1090 usb_gadget_set_state(gadget, USB_STATE_DEFAULT);
1091 }
1092 EXPORT_SYMBOL_GPL(usb_gadget_udc_reset);
1093
1094 /**
1095 * usb_gadget_udc_start - tells usb device controller to start up
1096 * @udc: The UDC to be started
1097 *
1098 * This call is issued by the UDC Class driver when it's about
1099 * to register a gadget driver to the device controller, before
1100 * calling gadget driver's bind() method.
1101 *
1102 * It allows the controller to be powered off until strictly
1103 * necessary to have it powered on.
1104 *
1105 * Returns zero on success, else negative errno.
1106 */
usb_gadget_udc_start(struct usb_udc * udc)1107 static inline int usb_gadget_udc_start(struct usb_udc *udc)
1108 {
1109 return udc->gadget->ops->udc_start(udc->gadget, udc->driver);
1110 }
1111
1112 /**
1113 * usb_gadget_udc_stop - tells usb device controller we don't need it anymore
1114 * @udc: The UDC to be stopped
1115 *
1116 * This call is issued by the UDC Class driver after calling
1117 * gadget driver's unbind() method.
1118 *
1119 * The details are implementation specific, but it can go as
1120 * far as powering off UDC completely and disable its data
1121 * line pullups.
1122 */
usb_gadget_udc_stop(struct usb_udc * udc)1123 static inline void usb_gadget_udc_stop(struct usb_udc *udc)
1124 {
1125 udc->gadget->ops->udc_stop(udc->gadget);
1126 }
1127
1128 /**
1129 * usb_gadget_udc_set_speed - tells usb device controller speed supported by
1130 * current driver
1131 * @udc: The device we want to set maximum speed
1132 * @speed: The maximum speed to allowed to run
1133 *
1134 * This call is issued by the UDC Class driver before calling
1135 * usb_gadget_udc_start() in order to make sure that we don't try to
1136 * connect on speeds the gadget driver doesn't support.
1137 */
usb_gadget_udc_set_speed(struct usb_udc * udc,enum usb_device_speed speed)1138 static inline void usb_gadget_udc_set_speed(struct usb_udc *udc,
1139 enum usb_device_speed speed)
1140 {
1141 if (udc->gadget->ops->udc_set_speed) {
1142 enum usb_device_speed s;
1143
1144 s = min(speed, udc->gadget->max_speed);
1145 udc->gadget->ops->udc_set_speed(udc->gadget, s);
1146 }
1147 }
1148
1149 /**
1150 * usb_udc_release - release the usb_udc struct
1151 * @dev: the dev member within usb_udc
1152 *
1153 * This is called by driver's core in order to free memory once the last
1154 * reference is released.
1155 */
usb_udc_release(struct device * dev)1156 static void usb_udc_release(struct device *dev)
1157 {
1158 struct usb_udc *udc;
1159
1160 udc = container_of(dev, struct usb_udc, dev);
1161 dev_dbg(dev, "releasing '%s'\n", dev_name(dev));
1162 kfree(udc);
1163 }
1164
1165 static const struct attribute_group *usb_udc_attr_groups[];
1166
usb_udc_nop_release(struct device * dev)1167 static void usb_udc_nop_release(struct device *dev)
1168 {
1169 dev_vdbg(dev, "%s\n", __func__);
1170 }
1171
1172 /* should be called with udc_lock held */
check_pending_gadget_drivers(struct usb_udc * udc)1173 static int check_pending_gadget_drivers(struct usb_udc *udc)
1174 {
1175 struct usb_gadget_driver *driver;
1176 int ret = 0;
1177
1178 list_for_each_entry(driver, &gadget_driver_pending_list, pending)
1179 if (!driver->udc_name || strcmp(driver->udc_name,
1180 dev_name(&udc->dev)) == 0) {
1181 ret = udc_bind_to_driver(udc, driver);
1182 if (ret != -EPROBE_DEFER)
1183 list_del_init(&driver->pending);
1184 break;
1185 }
1186
1187 return ret;
1188 }
1189
1190 /**
1191 * usb_initialize_gadget - initialize a gadget and its embedded struct device
1192 * @parent: the parent device to this udc. Usually the controller driver's
1193 * device.
1194 * @gadget: the gadget to be initialized.
1195 * @release: a gadget release function.
1196 *
1197 * Returns zero on success, negative errno otherwise.
1198 * Calls the gadget release function in the latter case.
1199 */
usb_initialize_gadget(struct device * parent,struct usb_gadget * gadget,void (* release)(struct device * dev))1200 void usb_initialize_gadget(struct device *parent, struct usb_gadget *gadget,
1201 void (*release)(struct device *dev))
1202 {
1203 dev_set_name(&gadget->dev, "gadget");
1204 INIT_WORK(&gadget->work, usb_gadget_state_work);
1205 gadget->dev.parent = parent;
1206
1207 if (release)
1208 gadget->dev.release = release;
1209 else
1210 gadget->dev.release = usb_udc_nop_release;
1211
1212 device_initialize(&gadget->dev);
1213 }
1214 EXPORT_SYMBOL_GPL(usb_initialize_gadget);
1215
1216 /**
1217 * usb_add_gadget - adds a new gadget to the udc class driver list
1218 * @gadget: the gadget to be added to the list.
1219 *
1220 * Returns zero on success, negative errno otherwise.
1221 * Does not do a final usb_put_gadget() if an error occurs.
1222 */
usb_add_gadget(struct usb_gadget * gadget)1223 int usb_add_gadget(struct usb_gadget *gadget)
1224 {
1225 struct usb_udc *udc;
1226 int ret = -ENOMEM;
1227
1228 udc = kzalloc(sizeof(*udc), GFP_KERNEL);
1229 if (!udc)
1230 goto error;
1231
1232 device_initialize(&udc->dev);
1233 udc->dev.release = usb_udc_release;
1234 udc->dev.class = udc_class;
1235 udc->dev.groups = usb_udc_attr_groups;
1236 udc->dev.parent = gadget->dev.parent;
1237 ret = dev_set_name(&udc->dev, "%s",
1238 kobject_name(&gadget->dev.parent->kobj));
1239 if (ret)
1240 goto err_put_udc;
1241
1242 ret = device_add(&gadget->dev);
1243 if (ret)
1244 goto err_put_udc;
1245
1246 udc->gadget = gadget;
1247 gadget->udc = udc;
1248
1249 mutex_lock(&udc_lock);
1250 list_add_tail(&udc->list, &udc_list);
1251
1252 ret = device_add(&udc->dev);
1253 if (ret)
1254 goto err_unlist_udc;
1255
1256 usb_gadget_set_state(gadget, USB_STATE_NOTATTACHED);
1257 udc->vbus = true;
1258
1259 /* pick up one of pending gadget drivers */
1260 ret = check_pending_gadget_drivers(udc);
1261 if (ret)
1262 goto err_del_udc;
1263
1264 mutex_unlock(&udc_lock);
1265
1266 return 0;
1267
1268 err_del_udc:
1269 flush_work(&gadget->work);
1270 device_del(&udc->dev);
1271
1272 err_unlist_udc:
1273 list_del(&udc->list);
1274 mutex_unlock(&udc_lock);
1275
1276 device_del(&gadget->dev);
1277
1278 err_put_udc:
1279 put_device(&udc->dev);
1280
1281 error:
1282 return ret;
1283 }
1284 EXPORT_SYMBOL_GPL(usb_add_gadget);
1285
1286 /**
1287 * usb_add_gadget_udc_release - adds a new gadget to the udc class driver list
1288 * @parent: the parent device to this udc. Usually the controller driver's
1289 * device.
1290 * @gadget: the gadget to be added to the list.
1291 * @release: a gadget release function.
1292 *
1293 * Returns zero on success, negative errno otherwise.
1294 * Calls the gadget release function in the latter case.
1295 */
usb_add_gadget_udc_release(struct device * parent,struct usb_gadget * gadget,void (* release)(struct device * dev))1296 int usb_add_gadget_udc_release(struct device *parent, struct usb_gadget *gadget,
1297 void (*release)(struct device *dev))
1298 {
1299 int ret;
1300
1301 usb_initialize_gadget(parent, gadget, release);
1302 ret = usb_add_gadget(gadget);
1303 if (ret)
1304 usb_put_gadget(gadget);
1305 return ret;
1306 }
1307 EXPORT_SYMBOL_GPL(usb_add_gadget_udc_release);
1308
1309 /**
1310 * usb_get_gadget_udc_name - get the name of the first UDC controller
1311 * This functions returns the name of the first UDC controller in the system.
1312 * Please note that this interface is usefull only for legacy drivers which
1313 * assume that there is only one UDC controller in the system and they need to
1314 * get its name before initialization. There is no guarantee that the UDC
1315 * of the returned name will be still available, when gadget driver registers
1316 * itself.
1317 *
1318 * Returns pointer to string with UDC controller name on success, NULL
1319 * otherwise. Caller should kfree() returned string.
1320 */
usb_get_gadget_udc_name(void)1321 char *usb_get_gadget_udc_name(void)
1322 {
1323 struct usb_udc *udc;
1324 char *name = NULL;
1325
1326 /* For now we take the first available UDC */
1327 mutex_lock(&udc_lock);
1328 list_for_each_entry(udc, &udc_list, list) {
1329 if (!udc->driver) {
1330 name = kstrdup(udc->gadget->name, GFP_KERNEL);
1331 break;
1332 }
1333 }
1334 mutex_unlock(&udc_lock);
1335 return name;
1336 }
1337 EXPORT_SYMBOL_GPL(usb_get_gadget_udc_name);
1338
1339 /**
1340 * usb_add_gadget_udc - adds a new gadget to the udc class driver list
1341 * @parent: the parent device to this udc. Usually the controller
1342 * driver's device.
1343 * @gadget: the gadget to be added to the list
1344 *
1345 * Returns zero on success, negative errno otherwise.
1346 */
usb_add_gadget_udc(struct device * parent,struct usb_gadget * gadget)1347 int usb_add_gadget_udc(struct device *parent, struct usb_gadget *gadget)
1348 {
1349 return usb_add_gadget_udc_release(parent, gadget, NULL);
1350 }
1351 EXPORT_SYMBOL_GPL(usb_add_gadget_udc);
1352
usb_gadget_remove_driver(struct usb_udc * udc)1353 static void usb_gadget_remove_driver(struct usb_udc *udc)
1354 {
1355 dev_dbg(&udc->dev, "unregistering UDC driver [%s]\n",
1356 udc->driver->function);
1357
1358 usb_gadget_disconnect(udc->gadget);
1359 if (udc->gadget->irq)
1360 synchronize_irq(udc->gadget->irq);
1361 udc->driver->unbind(udc->gadget);
1362 usb_gadget_udc_stop(udc);
1363
1364 udc->driver = NULL;
1365 udc->gadget->dev.driver = NULL;
1366
1367 kobject_uevent(&udc->dev.kobj, KOBJ_CHANGE);
1368 }
1369
1370 /**
1371 * usb_del_gadget - deletes @udc from udc_list
1372 * @gadget: the gadget to be removed.
1373 *
1374 * This will call usb_gadget_unregister_driver() if
1375 * the @udc is still busy.
1376 * It will not do a final usb_put_gadget().
1377 */
usb_del_gadget(struct usb_gadget * gadget)1378 void usb_del_gadget(struct usb_gadget *gadget)
1379 {
1380 struct usb_udc *udc = gadget->udc;
1381
1382 if (!udc)
1383 return;
1384
1385 dev_vdbg(gadget->dev.parent, "unregistering gadget\n");
1386
1387 mutex_lock(&udc_lock);
1388 list_del(&udc->list);
1389
1390 if (udc->driver) {
1391 struct usb_gadget_driver *driver = udc->driver;
1392
1393 usb_gadget_remove_driver(udc);
1394 list_add(&driver->pending, &gadget_driver_pending_list);
1395 }
1396 mutex_unlock(&udc_lock);
1397
1398 kobject_uevent(&udc->dev.kobj, KOBJ_REMOVE);
1399 flush_work(&gadget->work);
1400 device_unregister(&udc->dev);
1401 device_del(&gadget->dev);
1402 }
1403 EXPORT_SYMBOL_GPL(usb_del_gadget);
1404
1405 /**
1406 * usb_del_gadget_udc - deletes @udc from udc_list
1407 * @gadget: the gadget to be removed.
1408 *
1409 * Calls usb_del_gadget() and does a final usb_put_gadget().
1410 */
usb_del_gadget_udc(struct usb_gadget * gadget)1411 void usb_del_gadget_udc(struct usb_gadget *gadget)
1412 {
1413 usb_del_gadget(gadget);
1414 usb_put_gadget(gadget);
1415 }
1416 EXPORT_SYMBOL_GPL(usb_del_gadget_udc);
1417
1418 /* ------------------------------------------------------------------------- */
1419
udc_bind_to_driver(struct usb_udc * udc,struct usb_gadget_driver * driver)1420 static int udc_bind_to_driver(struct usb_udc *udc, struct usb_gadget_driver *driver)
1421 {
1422 int ret;
1423
1424 dev_dbg(&udc->dev, "registering UDC driver [%s]\n",
1425 driver->function);
1426
1427 udc->driver = driver;
1428 udc->gadget->dev.driver = &driver->driver;
1429
1430 usb_gadget_udc_set_speed(udc, driver->max_speed);
1431
1432 ret = driver->bind(udc->gadget, driver);
1433 if (ret)
1434 goto err1;
1435 ret = usb_gadget_udc_start(udc);
1436 if (ret) {
1437 driver->unbind(udc->gadget);
1438 goto err1;
1439 }
1440 usb_udc_connect_control(udc);
1441
1442 kobject_uevent(&udc->dev.kobj, KOBJ_CHANGE);
1443 return 0;
1444 err1:
1445 if (ret != -EISNAM)
1446 dev_err(&udc->dev, "failed to start %s: %d\n",
1447 udc->driver->function, ret);
1448 udc->driver = NULL;
1449 udc->gadget->dev.driver = NULL;
1450 return ret;
1451 }
1452
usb_gadget_probe_driver(struct usb_gadget_driver * driver)1453 int usb_gadget_probe_driver(struct usb_gadget_driver *driver)
1454 {
1455 struct usb_udc *udc = NULL;
1456 int ret = -ENODEV;
1457
1458 if (!driver || !driver->bind || !driver->setup)
1459 return -EINVAL;
1460
1461 mutex_lock(&udc_lock);
1462 if (driver->udc_name) {
1463 list_for_each_entry(udc, &udc_list, list) {
1464 ret = strcmp(driver->udc_name, dev_name(&udc->dev));
1465 if (!ret)
1466 break;
1467 }
1468 if (ret)
1469 ret = -ENODEV;
1470 else if (udc->driver)
1471 ret = -EBUSY;
1472 else
1473 goto found;
1474 } else {
1475 list_for_each_entry(udc, &udc_list, list) {
1476 /* For now we take the first one */
1477 if (!udc->driver)
1478 goto found;
1479 }
1480 }
1481
1482 if (!driver->match_existing_only) {
1483 list_add_tail(&driver->pending, &gadget_driver_pending_list);
1484 pr_info("udc-core: couldn't find an available UDC - added [%s] to list of pending drivers\n",
1485 driver->function);
1486 ret = 0;
1487 }
1488
1489 mutex_unlock(&udc_lock);
1490 if (ret)
1491 pr_warn("udc-core: couldn't find an available UDC or it's busy\n");
1492 return ret;
1493 found:
1494 ret = udc_bind_to_driver(udc, driver);
1495 mutex_unlock(&udc_lock);
1496 return ret;
1497 }
1498 EXPORT_SYMBOL_GPL(usb_gadget_probe_driver);
1499
usb_gadget_unregister_driver(struct usb_gadget_driver * driver)1500 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
1501 {
1502 struct usb_udc *udc = NULL;
1503 int ret = -ENODEV;
1504
1505 if (!driver || !driver->unbind)
1506 return -EINVAL;
1507
1508 mutex_lock(&udc_lock);
1509 list_for_each_entry(udc, &udc_list, list) {
1510 if (udc->driver == driver) {
1511 usb_gadget_remove_driver(udc);
1512 usb_gadget_set_state(udc->gadget,
1513 USB_STATE_NOTATTACHED);
1514
1515 /* Maybe there is someone waiting for this UDC? */
1516 check_pending_gadget_drivers(udc);
1517 /*
1518 * For now we ignore bind errors as probably it's
1519 * not a valid reason to fail other's gadget unbind
1520 */
1521 ret = 0;
1522 break;
1523 }
1524 }
1525
1526 if (ret) {
1527 list_del(&driver->pending);
1528 ret = 0;
1529 }
1530 mutex_unlock(&udc_lock);
1531 return ret;
1532 }
1533 EXPORT_SYMBOL_GPL(usb_gadget_unregister_driver);
1534
1535 /* ------------------------------------------------------------------------- */
1536
srp_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t n)1537 static ssize_t srp_store(struct device *dev,
1538 struct device_attribute *attr, const char *buf, size_t n)
1539 {
1540 struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
1541
1542 if (sysfs_streq(buf, "1"))
1543 usb_gadget_wakeup(udc->gadget);
1544
1545 return n;
1546 }
1547 static DEVICE_ATTR_WO(srp);
1548
soft_connect_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t n)1549 static ssize_t soft_connect_store(struct device *dev,
1550 struct device_attribute *attr, const char *buf, size_t n)
1551 {
1552 struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
1553 ssize_t ret;
1554
1555 mutex_lock(&udc_lock);
1556 if (!udc->driver) {
1557 dev_err(dev, "soft-connect without a gadget driver\n");
1558 ret = -EOPNOTSUPP;
1559 goto out;
1560 }
1561
1562 if (sysfs_streq(buf, "connect")) {
1563 usb_gadget_udc_start(udc);
1564 usb_gadget_connect(udc->gadget);
1565 } else if (sysfs_streq(buf, "disconnect")) {
1566 usb_gadget_disconnect(udc->gadget);
1567 usb_gadget_udc_stop(udc);
1568 } else {
1569 dev_err(dev, "unsupported command '%s'\n", buf);
1570 ret = -EINVAL;
1571 goto out;
1572 }
1573
1574 ret = n;
1575 out:
1576 mutex_unlock(&udc_lock);
1577 return ret;
1578 }
1579 static DEVICE_ATTR_WO(soft_connect);
1580
state_show(struct device * dev,struct device_attribute * attr,char * buf)1581 static ssize_t state_show(struct device *dev, struct device_attribute *attr,
1582 char *buf)
1583 {
1584 struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
1585 struct usb_gadget *gadget = udc->gadget;
1586
1587 return sprintf(buf, "%s\n", usb_state_string(gadget->state));
1588 }
1589 static DEVICE_ATTR_RO(state);
1590
function_show(struct device * dev,struct device_attribute * attr,char * buf)1591 static ssize_t function_show(struct device *dev, struct device_attribute *attr,
1592 char *buf)
1593 {
1594 struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
1595 struct usb_gadget_driver *drv = udc->driver;
1596
1597 if (!drv || !drv->function)
1598 return 0;
1599 return scnprintf(buf, PAGE_SIZE, "%s\n", drv->function);
1600 }
1601 static DEVICE_ATTR_RO(function);
1602
1603 #define USB_UDC_SPEED_ATTR(name, param) \
1604 ssize_t name##_show(struct device *dev, \
1605 struct device_attribute *attr, char *buf) \
1606 { \
1607 struct usb_udc *udc = container_of(dev, struct usb_udc, dev); \
1608 return scnprintf(buf, PAGE_SIZE, "%s\n", \
1609 usb_speed_string(udc->gadget->param)); \
1610 } \
1611 static DEVICE_ATTR_RO(name)
1612
1613 static USB_UDC_SPEED_ATTR(current_speed, speed);
1614 static USB_UDC_SPEED_ATTR(maximum_speed, max_speed);
1615
1616 #define USB_UDC_ATTR(name) \
1617 ssize_t name##_show(struct device *dev, \
1618 struct device_attribute *attr, char *buf) \
1619 { \
1620 struct usb_udc *udc = container_of(dev, struct usb_udc, dev); \
1621 struct usb_gadget *gadget = udc->gadget; \
1622 \
1623 return scnprintf(buf, PAGE_SIZE, "%d\n", gadget->name); \
1624 } \
1625 static DEVICE_ATTR_RO(name)
1626
1627 static USB_UDC_ATTR(is_otg);
1628 static USB_UDC_ATTR(is_a_peripheral);
1629 static USB_UDC_ATTR(b_hnp_enable);
1630 static USB_UDC_ATTR(a_hnp_support);
1631 static USB_UDC_ATTR(a_alt_hnp_support);
1632 static USB_UDC_ATTR(is_selfpowered);
1633
1634 static struct attribute *usb_udc_attrs[] = {
1635 &dev_attr_srp.attr,
1636 &dev_attr_soft_connect.attr,
1637 &dev_attr_state.attr,
1638 &dev_attr_function.attr,
1639 &dev_attr_current_speed.attr,
1640 &dev_attr_maximum_speed.attr,
1641
1642 &dev_attr_is_otg.attr,
1643 &dev_attr_is_a_peripheral.attr,
1644 &dev_attr_b_hnp_enable.attr,
1645 &dev_attr_a_hnp_support.attr,
1646 &dev_attr_a_alt_hnp_support.attr,
1647 &dev_attr_is_selfpowered.attr,
1648 NULL,
1649 };
1650
1651 static const struct attribute_group usb_udc_attr_group = {
1652 .attrs = usb_udc_attrs,
1653 };
1654
1655 static const struct attribute_group *usb_udc_attr_groups[] = {
1656 &usb_udc_attr_group,
1657 NULL,
1658 };
1659
usb_udc_uevent(struct device * dev,struct kobj_uevent_env * env)1660 static int usb_udc_uevent(struct device *dev, struct kobj_uevent_env *env)
1661 {
1662 struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
1663 int ret;
1664
1665 ret = add_uevent_var(env, "USB_UDC_NAME=%s", udc->gadget->name);
1666 if (ret) {
1667 dev_err(dev, "failed to add uevent USB_UDC_NAME\n");
1668 return ret;
1669 }
1670
1671 if (udc->driver) {
1672 ret = add_uevent_var(env, "USB_UDC_DRIVER=%s",
1673 udc->driver->function);
1674 if (ret) {
1675 dev_err(dev, "failed to add uevent USB_UDC_DRIVER\n");
1676 return ret;
1677 }
1678 }
1679
1680 return 0;
1681 }
1682
usb_udc_init(void)1683 static int __init usb_udc_init(void)
1684 {
1685 udc_class = class_create(THIS_MODULE, "udc");
1686 if (IS_ERR(udc_class)) {
1687 pr_err("failed to create udc class --> %ld\n",
1688 PTR_ERR(udc_class));
1689 return PTR_ERR(udc_class);
1690 }
1691
1692 udc_class->dev_uevent = usb_udc_uevent;
1693 return 0;
1694 }
1695 subsys_initcall(usb_udc_init);
1696
usb_udc_exit(void)1697 static void __exit usb_udc_exit(void)
1698 {
1699 class_destroy(udc_class);
1700 }
1701 module_exit(usb_udc_exit);
1702
1703 MODULE_DESCRIPTION("UDC Framework");
1704 MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
1705 MODULE_LICENSE("GPL v2");
1706