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