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