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