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