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