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