Commit | Line | Data |
---|---|---|
2ccea03a FB |
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> | |
a698908d | 25 | #include <linux/dma-mapping.h> |
5702f753 | 26 | #include <linux/workqueue.h> |
2ccea03a FB |
27 | |
28 | #include <linux/usb/ch9.h> | |
29 | #include <linux/usb/gadget.h> | |
0cfbd328 | 30 | #include <linux/usb.h> |
2ccea03a FB |
31 | |
32 | /** | |
33 | * struct usb_udc - describes one usb device controller | |
34 | * @driver - the gadget driver pointer. For use by the class code | |
35 | * @dev - the child device to the actual controller | |
36 | * @gadget - the gadget. For use by the class code | |
37 | * @list - for use by the udc class driver | |
628ef0d2 PC |
38 | * @vbus - for udcs who care about vbus status, this value is real vbus status; |
39 | * for udcs who do not care about vbus status, this value is always true | |
2ccea03a FB |
40 | * |
41 | * This represents the internal data structure which is used by the UDC-class | |
42 | * to hold information about udc driver and gadget together. | |
43 | */ | |
44 | struct usb_udc { | |
45 | struct usb_gadget_driver *driver; | |
46 | struct usb_gadget *gadget; | |
47 | struct device dev; | |
48 | struct list_head list; | |
628ef0d2 | 49 | bool vbus; |
2ccea03a FB |
50 | }; |
51 | ||
52 | static struct class *udc_class; | |
2ccea03a FB |
53 | static LIST_HEAD(udc_list); |
54 | static DEFINE_MUTEX(udc_lock); | |
55 | ||
56 | /* ------------------------------------------------------------------------- */ | |
57 | ||
908b9613 AS |
58 | #ifdef CONFIG_HAS_DMA |
59 | ||
a698908d FB |
60 | int usb_gadget_map_request(struct usb_gadget *gadget, |
61 | struct usb_request *req, int is_in) | |
62 | { | |
7ace8fc8 YS |
63 | struct device *dev = gadget->dev.parent; |
64 | ||
a698908d FB |
65 | if (req->length == 0) |
66 | return 0; | |
67 | ||
68 | if (req->num_sgs) { | |
69 | int mapped; | |
70 | ||
7ace8fc8 | 71 | mapped = dma_map_sg(dev, req->sg, req->num_sgs, |
a698908d FB |
72 | is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE); |
73 | if (mapped == 0) { | |
74 | dev_err(&gadget->dev, "failed to map SGs\n"); | |
75 | return -EFAULT; | |
76 | } | |
77 | ||
78 | req->num_mapped_sgs = mapped; | |
79 | } else { | |
7ace8fc8 | 80 | req->dma = dma_map_single(dev, req->buf, req->length, |
a698908d FB |
81 | is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE); |
82 | ||
7ace8fc8 YS |
83 | if (dma_mapping_error(dev, req->dma)) { |
84 | dev_err(dev, "failed to map buffer\n"); | |
a698908d FB |
85 | return -EFAULT; |
86 | } | |
87 | } | |
88 | ||
89 | return 0; | |
90 | } | |
91 | EXPORT_SYMBOL_GPL(usb_gadget_map_request); | |
92 | ||
93 | void usb_gadget_unmap_request(struct usb_gadget *gadget, | |
94 | struct usb_request *req, int is_in) | |
95 | { | |
96 | if (req->length == 0) | |
97 | return; | |
98 | ||
99 | if (req->num_mapped_sgs) { | |
7ace8fc8 | 100 | dma_unmap_sg(gadget->dev.parent, req->sg, req->num_mapped_sgs, |
a698908d FB |
101 | is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE); |
102 | ||
103 | req->num_mapped_sgs = 0; | |
104 | } else { | |
7ace8fc8 | 105 | dma_unmap_single(gadget->dev.parent, req->dma, req->length, |
a698908d FB |
106 | is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE); |
107 | } | |
108 | } | |
109 | EXPORT_SYMBOL_GPL(usb_gadget_unmap_request); | |
110 | ||
908b9613 AS |
111 | #endif /* CONFIG_HAS_DMA */ |
112 | ||
a698908d FB |
113 | /* ------------------------------------------------------------------------- */ |
114 | ||
3fc2aa55 MS |
115 | /** |
116 | * usb_gadget_giveback_request - give the request back to the gadget layer | |
117 | * Context: in_interrupt() | |
118 | * | |
119 | * This is called by device controller drivers in order to return the | |
120 | * completed request back to the gadget layer. | |
121 | */ | |
122 | void usb_gadget_giveback_request(struct usb_ep *ep, | |
123 | struct usb_request *req) | |
124 | { | |
0cfbd328 MS |
125 | if (likely(req->status == 0)) |
126 | usb_led_activity(USB_LED_EVENT_GADGET); | |
127 | ||
3fc2aa55 MS |
128 | req->complete(ep, req); |
129 | } | |
130 | EXPORT_SYMBOL_GPL(usb_gadget_giveback_request); | |
131 | ||
132 | /* ------------------------------------------------------------------------- */ | |
133 | ||
4278c687 RB |
134 | int usb_gadget_ep_match_desc(struct usb_gadget *gadget, |
135 | struct usb_ep *ep, struct usb_endpoint_descriptor *desc, | |
136 | struct usb_ss_ep_comp_descriptor *ep_comp) | |
137 | { | |
138 | u8 type; | |
139 | u16 max; | |
140 | int num_req_streams = 0; | |
141 | ||
142 | /* endpoint already claimed? */ | |
143 | if (ep->claimed) | |
144 | return 0; | |
145 | ||
146 | type = usb_endpoint_type(desc); | |
147 | max = 0x7ff & usb_endpoint_maxp(desc); | |
148 | ||
149 | if (usb_endpoint_dir_in(desc) && !ep->caps.dir_in) | |
150 | return 0; | |
151 | if (usb_endpoint_dir_out(desc) && !ep->caps.dir_out) | |
152 | return 0; | |
153 | ||
154 | if (max > ep->maxpacket_limit) | |
155 | return 0; | |
156 | ||
157 | /* "high bandwidth" works only at high speed */ | |
158 | if (!gadget_is_dualspeed(gadget) && usb_endpoint_maxp(desc) & (3<<11)) | |
159 | return 0; | |
160 | ||
161 | switch (type) { | |
162 | case USB_ENDPOINT_XFER_CONTROL: | |
163 | /* only support ep0 for portable CONTROL traffic */ | |
164 | return 0; | |
165 | case USB_ENDPOINT_XFER_ISOC: | |
166 | if (!ep->caps.type_iso) | |
167 | return 0; | |
168 | /* ISO: limit 1023 bytes full speed, 1024 high/super speed */ | |
169 | if (!gadget_is_dualspeed(gadget) && max > 1023) | |
170 | return 0; | |
171 | break; | |
172 | case USB_ENDPOINT_XFER_BULK: | |
173 | if (!ep->caps.type_bulk) | |
174 | return 0; | |
175 | if (ep_comp && gadget_is_superspeed(gadget)) { | |
176 | /* Get the number of required streams from the | |
177 | * EP companion descriptor and see if the EP | |
178 | * matches it | |
179 | */ | |
180 | num_req_streams = ep_comp->bmAttributes & 0x1f; | |
181 | if (num_req_streams > ep->max_streams) | |
182 | return 0; | |
183 | } | |
184 | break; | |
185 | case USB_ENDPOINT_XFER_INT: | |
186 | /* Bulk endpoints handle interrupt transfers, | |
187 | * except the toggle-quirky iso-synch kind | |
188 | */ | |
189 | if (!ep->caps.type_int && !ep->caps.type_bulk) | |
190 | return 0; | |
191 | /* INT: limit 64 bytes full speed, 1024 high/super speed */ | |
192 | if (!gadget_is_dualspeed(gadget) && max > 64) | |
193 | return 0; | |
194 | break; | |
195 | } | |
196 | ||
197 | return 1; | |
198 | } | |
199 | EXPORT_SYMBOL_GPL(usb_gadget_ep_match_desc); | |
200 | ||
201 | /* ------------------------------------------------------------------------- */ | |
202 | ||
5702f753 FB |
203 | static void usb_gadget_state_work(struct work_struct *work) |
204 | { | |
dfea9c94 PC |
205 | struct usb_gadget *gadget = work_to_gadget(work); |
206 | struct usb_udc *udc = gadget->udc; | |
5702f753 | 207 | |
dfea9c94 PC |
208 | if (udc) |
209 | sysfs_notify(&udc->dev.kobj, NULL, "state"); | |
5702f753 FB |
210 | } |
211 | ||
49401f41 FB |
212 | void usb_gadget_set_state(struct usb_gadget *gadget, |
213 | enum usb_device_state state) | |
214 | { | |
215 | gadget->state = state; | |
5702f753 | 216 | schedule_work(&gadget->work); |
49401f41 FB |
217 | } |
218 | EXPORT_SYMBOL_GPL(usb_gadget_set_state); | |
219 | ||
220 | /* ------------------------------------------------------------------------- */ | |
221 | ||
628ef0d2 PC |
222 | static void usb_udc_connect_control(struct usb_udc *udc) |
223 | { | |
224 | if (udc->vbus) | |
225 | usb_gadget_connect(udc->gadget); | |
226 | else | |
227 | usb_gadget_disconnect(udc->gadget); | |
228 | } | |
229 | ||
230 | /** | |
231 | * usb_udc_vbus_handler - updates the udc core vbus status, and try to | |
232 | * connect or disconnect gadget | |
233 | * @gadget: The gadget which vbus change occurs | |
234 | * @status: The vbus status | |
235 | * | |
236 | * The udc driver calls it when it wants to connect or disconnect gadget | |
237 | * according to vbus status. | |
238 | */ | |
239 | void usb_udc_vbus_handler(struct usb_gadget *gadget, bool status) | |
240 | { | |
241 | struct usb_udc *udc = gadget->udc; | |
242 | ||
243 | if (udc) { | |
244 | udc->vbus = status; | |
245 | usb_udc_connect_control(udc); | |
246 | } | |
247 | } | |
248 | EXPORT_SYMBOL_GPL(usb_udc_vbus_handler); | |
249 | ||
974a70bd PC |
250 | /** |
251 | * usb_gadget_udc_reset - notifies the udc core that bus reset occurs | |
252 | * @gadget: The gadget which bus reset occurs | |
253 | * @driver: The gadget driver we want to notify | |
254 | * | |
255 | * If the udc driver has bus reset handler, it needs to call this when the bus | |
256 | * reset occurs, it notifies the gadget driver that the bus reset occurs as | |
257 | * well as updates gadget state. | |
258 | */ | |
259 | void usb_gadget_udc_reset(struct usb_gadget *gadget, | |
260 | struct usb_gadget_driver *driver) | |
261 | { | |
262 | driver->reset(gadget); | |
263 | usb_gadget_set_state(gadget, USB_STATE_DEFAULT); | |
264 | } | |
265 | EXPORT_SYMBOL_GPL(usb_gadget_udc_reset); | |
266 | ||
352c2dc8 SAS |
267 | /** |
268 | * usb_gadget_udc_start - tells usb device controller to start up | |
2c683347 | 269 | * @udc: The UDC to be started |
352c2dc8 SAS |
270 | * |
271 | * This call is issued by the UDC Class driver when it's about | |
272 | * to register a gadget driver to the device controller, before | |
273 | * calling gadget driver's bind() method. | |
274 | * | |
275 | * It allows the controller to be powered off until strictly | |
276 | * necessary to have it powered on. | |
277 | * | |
278 | * Returns zero on success, else negative errno. | |
279 | */ | |
2c683347 | 280 | static inline int usb_gadget_udc_start(struct usb_udc *udc) |
352c2dc8 | 281 | { |
2c683347 | 282 | return udc->gadget->ops->udc_start(udc->gadget, udc->driver); |
352c2dc8 SAS |
283 | } |
284 | ||
352c2dc8 SAS |
285 | /** |
286 | * usb_gadget_udc_stop - tells usb device controller we don't need it anymore | |
287 | * @gadget: The device we want to stop activity | |
288 | * @driver: The driver to unbind from @gadget | |
289 | * | |
290 | * This call is issued by the UDC Class driver after calling | |
291 | * gadget driver's unbind() method. | |
292 | * | |
293 | * The details are implementation specific, but it can go as | |
294 | * far as powering off UDC completely and disable its data | |
295 | * line pullups. | |
296 | */ | |
2c683347 | 297 | static inline void usb_gadget_udc_stop(struct usb_udc *udc) |
352c2dc8 | 298 | { |
22835b80 | 299 | udc->gadget->ops->udc_stop(udc->gadget); |
352c2dc8 SAS |
300 | } |
301 | ||
2ccea03a FB |
302 | /** |
303 | * usb_udc_release - release the usb_udc struct | |
304 | * @dev: the dev member within usb_udc | |
305 | * | |
306 | * This is called by driver's core in order to free memory once the last | |
307 | * reference is released. | |
308 | */ | |
309 | static void usb_udc_release(struct device *dev) | |
310 | { | |
311 | struct usb_udc *udc; | |
312 | ||
313 | udc = container_of(dev, struct usb_udc, dev); | |
314 | dev_dbg(dev, "releasing '%s'\n", dev_name(dev)); | |
315 | kfree(udc); | |
316 | } | |
317 | ||
019f976e | 318 | static const struct attribute_group *usb_udc_attr_groups[]; |
792bfcf7 FB |
319 | |
320 | static void usb_udc_nop_release(struct device *dev) | |
321 | { | |
322 | dev_vdbg(dev, "%s\n", __func__); | |
323 | } | |
324 | ||
2ccea03a | 325 | /** |
792bfcf7 FB |
326 | * usb_add_gadget_udc_release - adds a new gadget to the udc class driver list |
327 | * @parent: the parent device to this udc. Usually the controller driver's | |
328 | * device. | |
329 | * @gadget: the gadget to be added to the list. | |
330 | * @release: a gadget release function. | |
2ccea03a FB |
331 | * |
332 | * Returns zero on success, negative errno otherwise. | |
333 | */ | |
792bfcf7 FB |
334 | int usb_add_gadget_udc_release(struct device *parent, struct usb_gadget *gadget, |
335 | void (*release)(struct device *dev)) | |
2ccea03a FB |
336 | { |
337 | struct usb_udc *udc; | |
338 | int ret = -ENOMEM; | |
339 | ||
340 | udc = kzalloc(sizeof(*udc), GFP_KERNEL); | |
341 | if (!udc) | |
342 | goto err1; | |
343 | ||
7bce401c | 344 | dev_set_name(&gadget->dev, "gadget"); |
5702f753 | 345 | INIT_WORK(&gadget->work, usb_gadget_state_work); |
2ed14320 | 346 | gadget->dev.parent = parent; |
f07bd56b | 347 | |
908b9613 | 348 | #ifdef CONFIG_HAS_DMA |
658c8cf1 FB |
349 | dma_set_coherent_mask(&gadget->dev, parent->coherent_dma_mask); |
350 | gadget->dev.dma_parms = parent->dma_parms; | |
351 | gadget->dev.dma_mask = parent->dma_mask; | |
908b9613 | 352 | #endif |
658c8cf1 | 353 | |
ddf47ccb | 354 | if (release) |
792bfcf7 | 355 | gadget->dev.release = release; |
ddf47ccb FB |
356 | else |
357 | gadget->dev.release = usb_udc_nop_release; | |
792bfcf7 | 358 | |
7bce401c FB |
359 | ret = device_register(&gadget->dev); |
360 | if (ret) | |
361 | goto err2; | |
f07bd56b | 362 | |
2ccea03a FB |
363 | device_initialize(&udc->dev); |
364 | udc->dev.release = usb_udc_release; | |
365 | udc->dev.class = udc_class; | |
019f976e | 366 | udc->dev.groups = usb_udc_attr_groups; |
2ccea03a FB |
367 | udc->dev.parent = parent; |
368 | ret = dev_set_name(&udc->dev, "%s", kobject_name(&parent->kobj)); | |
369 | if (ret) | |
f07bd56b | 370 | goto err3; |
2ccea03a FB |
371 | |
372 | udc->gadget = gadget; | |
dfea9c94 | 373 | gadget->udc = udc; |
2ccea03a FB |
374 | |
375 | mutex_lock(&udc_lock); | |
376 | list_add_tail(&udc->list, &udc_list); | |
377 | ||
378 | ret = device_add(&udc->dev); | |
379 | if (ret) | |
f07bd56b | 380 | goto err4; |
2ccea03a | 381 | |
49401f41 | 382 | usb_gadget_set_state(gadget, USB_STATE_NOTATTACHED); |
628ef0d2 | 383 | udc->vbus = true; |
2ccea03a FB |
384 | |
385 | mutex_unlock(&udc_lock); | |
386 | ||
387 | return 0; | |
f07bd56b FB |
388 | |
389 | err4: | |
2ccea03a FB |
390 | list_del(&udc->list); |
391 | mutex_unlock(&udc_lock); | |
392 | ||
f07bd56b | 393 | err3: |
2ccea03a FB |
394 | put_device(&udc->dev); |
395 | ||
f07bd56b | 396 | err2: |
7bce401c | 397 | put_device(&gadget->dev); |
c5dbc220 | 398 | kfree(udc); |
7bce401c | 399 | |
2ccea03a FB |
400 | err1: |
401 | return ret; | |
402 | } | |
792bfcf7 FB |
403 | EXPORT_SYMBOL_GPL(usb_add_gadget_udc_release); |
404 | ||
405 | /** | |
406 | * usb_add_gadget_udc - adds a new gadget to the udc class driver list | |
407 | * @parent: the parent device to this udc. Usually the controller | |
408 | * driver's device. | |
409 | * @gadget: the gadget to be added to the list | |
410 | * | |
411 | * Returns zero on success, negative errno otherwise. | |
412 | */ | |
413 | int usb_add_gadget_udc(struct device *parent, struct usb_gadget *gadget) | |
414 | { | |
415 | return usb_add_gadget_udc_release(parent, gadget, NULL); | |
416 | } | |
2ccea03a FB |
417 | EXPORT_SYMBOL_GPL(usb_add_gadget_udc); |
418 | ||
419 | static void usb_gadget_remove_driver(struct usb_udc *udc) | |
420 | { | |
421 | dev_dbg(&udc->dev, "unregistering UDC driver [%s]\n", | |
8da9fe8a | 422 | udc->driver->function); |
2ccea03a FB |
423 | |
424 | kobject_uevent(&udc->dev.kobj, KOBJ_CHANGE); | |
425 | ||
2d7ebbb0 FB |
426 | usb_gadget_disconnect(udc->gadget); |
427 | udc->driver->disconnect(udc->gadget); | |
428 | udc->driver->unbind(udc->gadget); | |
2c683347 | 429 | usb_gadget_udc_stop(udc); |
2ccea03a FB |
430 | |
431 | udc->driver = NULL; | |
432 | udc->dev.driver = NULL; | |
70d3a498 | 433 | udc->gadget->dev.driver = NULL; |
2ccea03a FB |
434 | } |
435 | ||
436 | /** | |
437 | * usb_del_gadget_udc - deletes @udc from udc_list | |
438 | * @gadget: the gadget to be removed. | |
439 | * | |
440 | * This, will call usb_gadget_unregister_driver() if | |
441 | * the @udc is still busy. | |
442 | */ | |
443 | void usb_del_gadget_udc(struct usb_gadget *gadget) | |
444 | { | |
dfea9c94 | 445 | struct usb_udc *udc = gadget->udc; |
2ccea03a | 446 | |
dfea9c94 PC |
447 | if (!udc) |
448 | return; | |
2ccea03a | 449 | |
2ccea03a FB |
450 | dev_vdbg(gadget->dev.parent, "unregistering gadget\n"); |
451 | ||
dfea9c94 | 452 | mutex_lock(&udc_lock); |
2ccea03a FB |
453 | list_del(&udc->list); |
454 | mutex_unlock(&udc_lock); | |
455 | ||
456 | if (udc->driver) | |
457 | usb_gadget_remove_driver(udc); | |
458 | ||
459 | kobject_uevent(&udc->dev.kobj, KOBJ_REMOVE); | |
5702f753 | 460 | flush_work(&gadget->work); |
2ccea03a | 461 | device_unregister(&udc->dev); |
7bce401c | 462 | device_unregister(&gadget->dev); |
2ccea03a FB |
463 | } |
464 | EXPORT_SYMBOL_GPL(usb_del_gadget_udc); | |
465 | ||
466 | /* ------------------------------------------------------------------------- */ | |
467 | ||
4c49a5f0 | 468 | static int udc_bind_to_driver(struct usb_udc *udc, struct usb_gadget_driver *driver) |
2ccea03a | 469 | { |
4c49a5f0 | 470 | int ret; |
2ccea03a | 471 | |
2ccea03a FB |
472 | dev_dbg(&udc->dev, "registering UDC driver [%s]\n", |
473 | driver->function); | |
474 | ||
475 | udc->driver = driver; | |
476 | udc->dev.driver = &driver->driver; | |
70d3a498 | 477 | udc->gadget->dev.driver = &driver->driver; |
2ccea03a | 478 | |
2d7ebbb0 FB |
479 | ret = driver->bind(udc->gadget, driver); |
480 | if (ret) | |
481 | goto err1; | |
2c683347 | 482 | ret = usb_gadget_udc_start(udc); |
2d7ebbb0 FB |
483 | if (ret) { |
484 | driver->unbind(udc->gadget); | |
485 | goto err1; | |
352c2dc8 | 486 | } |
628ef0d2 | 487 | usb_udc_connect_control(udc); |
2ccea03a FB |
488 | |
489 | kobject_uevent(&udc->dev.kobj, KOBJ_CHANGE); | |
2ccea03a | 490 | return 0; |
2ccea03a | 491 | err1: |
f8cffc84 FE |
492 | if (ret != -EISNAM) |
493 | dev_err(&udc->dev, "failed to start %s: %d\n", | |
2ccea03a FB |
494 | udc->driver->function, ret); |
495 | udc->driver = NULL; | |
496 | udc->dev.driver = NULL; | |
70d3a498 | 497 | udc->gadget->dev.driver = NULL; |
4c49a5f0 SAS |
498 | return ret; |
499 | } | |
500 | ||
02e8c966 | 501 | int usb_udc_attach_driver(const char *name, struct usb_gadget_driver *driver) |
4c49a5f0 SAS |
502 | { |
503 | struct usb_udc *udc = NULL; | |
504 | int ret = -ENODEV; | |
505 | ||
506 | mutex_lock(&udc_lock); | |
507 | list_for_each_entry(udc, &udc_list, list) { | |
508 | ret = strcmp(name, dev_name(&udc->dev)); | |
509 | if (!ret) | |
510 | break; | |
511 | } | |
512 | if (ret) { | |
513 | ret = -ENODEV; | |
514 | goto out; | |
515 | } | |
516 | if (udc->driver) { | |
517 | ret = -EBUSY; | |
518 | goto out; | |
519 | } | |
520 | ret = udc_bind_to_driver(udc, driver); | |
521 | out: | |
522 | mutex_unlock(&udc_lock); | |
523 | return ret; | |
524 | } | |
02e8c966 | 525 | EXPORT_SYMBOL_GPL(usb_udc_attach_driver); |
4c49a5f0 SAS |
526 | |
527 | int usb_gadget_probe_driver(struct usb_gadget_driver *driver) | |
528 | { | |
529 | struct usb_udc *udc = NULL; | |
530 | int ret; | |
531 | ||
532 | if (!driver || !driver->bind || !driver->setup) | |
533 | return -EINVAL; | |
534 | ||
535 | mutex_lock(&udc_lock); | |
536 | list_for_each_entry(udc, &udc_list, list) { | |
537 | /* For now we take the first one */ | |
538 | if (!udc->driver) | |
539 | goto found; | |
540 | } | |
541 | ||
542 | pr_debug("couldn't find an available UDC\n"); | |
543 | mutex_unlock(&udc_lock); | |
544 | return -ENODEV; | |
545 | found: | |
546 | ret = udc_bind_to_driver(udc, driver); | |
2ccea03a FB |
547 | mutex_unlock(&udc_lock); |
548 | return ret; | |
549 | } | |
550 | EXPORT_SYMBOL_GPL(usb_gadget_probe_driver); | |
551 | ||
552 | int usb_gadget_unregister_driver(struct usb_gadget_driver *driver) | |
553 | { | |
554 | struct usb_udc *udc = NULL; | |
555 | int ret = -ENODEV; | |
556 | ||
557 | if (!driver || !driver->unbind) | |
558 | return -EINVAL; | |
559 | ||
560 | mutex_lock(&udc_lock); | |
561 | list_for_each_entry(udc, &udc_list, list) | |
562 | if (udc->driver == driver) { | |
563 | usb_gadget_remove_driver(udc); | |
b5fb8d0a PC |
564 | usb_gadget_set_state(udc->gadget, |
565 | USB_STATE_NOTATTACHED); | |
2ccea03a FB |
566 | ret = 0; |
567 | break; | |
568 | } | |
569 | ||
570 | mutex_unlock(&udc_lock); | |
571 | return ret; | |
572 | } | |
573 | EXPORT_SYMBOL_GPL(usb_gadget_unregister_driver); | |
574 | ||
575 | /* ------------------------------------------------------------------------- */ | |
576 | ||
577 | static ssize_t usb_udc_srp_store(struct device *dev, | |
578 | struct device_attribute *attr, const char *buf, size_t n) | |
579 | { | |
1d91a962 | 580 | struct usb_udc *udc = container_of(dev, struct usb_udc, dev); |
2ccea03a FB |
581 | |
582 | if (sysfs_streq(buf, "1")) | |
583 | usb_gadget_wakeup(udc->gadget); | |
584 | ||
585 | return n; | |
586 | } | |
587 | static DEVICE_ATTR(srp, S_IWUSR, NULL, usb_udc_srp_store); | |
588 | ||
589 | static ssize_t usb_udc_softconn_store(struct device *dev, | |
590 | struct device_attribute *attr, const char *buf, size_t n) | |
591 | { | |
865569ba | 592 | struct usb_udc *udc = container_of(dev, struct usb_udc, dev); |
2ccea03a | 593 | |
bfa6b18c FB |
594 | if (!udc->driver) { |
595 | dev_err(dev, "soft-connect without a gadget driver\n"); | |
596 | return -EOPNOTSUPP; | |
597 | } | |
598 | ||
2ccea03a | 599 | if (sysfs_streq(buf, "connect")) { |
2c683347 | 600 | usb_gadget_udc_start(udc); |
2ccea03a FB |
601 | usb_gadget_connect(udc->gadget); |
602 | } else if (sysfs_streq(buf, "disconnect")) { | |
83a787a7 | 603 | usb_gadget_disconnect(udc->gadget); |
0abd0696 | 604 | udc->driver->disconnect(udc->gadget); |
2c683347 | 605 | usb_gadget_udc_stop(udc); |
2ccea03a FB |
606 | } else { |
607 | dev_err(dev, "unsupported command '%s'\n", buf); | |
608 | return -EINVAL; | |
609 | } | |
610 | ||
611 | return n; | |
612 | } | |
613 | static DEVICE_ATTR(soft_connect, S_IWUSR, NULL, usb_udc_softconn_store); | |
614 | ||
ce26bd23 GKH |
615 | static ssize_t state_show(struct device *dev, struct device_attribute *attr, |
616 | char *buf) | |
49401f41 FB |
617 | { |
618 | struct usb_udc *udc = container_of(dev, struct usb_udc, dev); | |
619 | struct usb_gadget *gadget = udc->gadget; | |
620 | ||
621 | return sprintf(buf, "%s\n", usb_state_string(gadget->state)); | |
622 | } | |
ce26bd23 | 623 | static DEVICE_ATTR_RO(state); |
49401f41 | 624 | |
d327ab5b | 625 | #define USB_UDC_SPEED_ATTR(name, param) \ |
ce26bd23 | 626 | ssize_t name##_show(struct device *dev, \ |
d327ab5b MN |
627 | struct device_attribute *attr, char *buf) \ |
628 | { \ | |
629 | struct usb_udc *udc = container_of(dev, struct usb_udc, dev); \ | |
630 | return snprintf(buf, PAGE_SIZE, "%s\n", \ | |
631 | usb_speed_string(udc->gadget->param)); \ | |
632 | } \ | |
ce26bd23 | 633 | static DEVICE_ATTR_RO(name) |
d327ab5b MN |
634 | |
635 | static USB_UDC_SPEED_ATTR(current_speed, speed); | |
636 | static USB_UDC_SPEED_ATTR(maximum_speed, max_speed); | |
637 | ||
2ccea03a | 638 | #define USB_UDC_ATTR(name) \ |
ce26bd23 | 639 | ssize_t name##_show(struct device *dev, \ |
2ccea03a FB |
640 | struct device_attribute *attr, char *buf) \ |
641 | { \ | |
019f976e | 642 | struct usb_udc *udc = container_of(dev, struct usb_udc, dev); \ |
2ccea03a FB |
643 | struct usb_gadget *gadget = udc->gadget; \ |
644 | \ | |
645 | return snprintf(buf, PAGE_SIZE, "%d\n", gadget->name); \ | |
646 | } \ | |
ce26bd23 | 647 | static DEVICE_ATTR_RO(name) |
2ccea03a | 648 | |
2ccea03a FB |
649 | static USB_UDC_ATTR(is_otg); |
650 | static USB_UDC_ATTR(is_a_peripheral); | |
651 | static USB_UDC_ATTR(b_hnp_enable); | |
652 | static USB_UDC_ATTR(a_hnp_support); | |
653 | static USB_UDC_ATTR(a_alt_hnp_support); | |
3f6dd4fe | 654 | static USB_UDC_ATTR(is_selfpowered); |
2ccea03a FB |
655 | |
656 | static struct attribute *usb_udc_attrs[] = { | |
657 | &dev_attr_srp.attr, | |
658 | &dev_attr_soft_connect.attr, | |
49401f41 | 659 | &dev_attr_state.attr, |
d327ab5b MN |
660 | &dev_attr_current_speed.attr, |
661 | &dev_attr_maximum_speed.attr, | |
2ccea03a | 662 | |
2ccea03a FB |
663 | &dev_attr_is_otg.attr, |
664 | &dev_attr_is_a_peripheral.attr, | |
665 | &dev_attr_b_hnp_enable.attr, | |
666 | &dev_attr_a_hnp_support.attr, | |
667 | &dev_attr_a_alt_hnp_support.attr, | |
3f6dd4fe | 668 | &dev_attr_is_selfpowered.attr, |
2ccea03a FB |
669 | NULL, |
670 | }; | |
671 | ||
672 | static const struct attribute_group usb_udc_attr_group = { | |
673 | .attrs = usb_udc_attrs, | |
674 | }; | |
675 | ||
676 | static const struct attribute_group *usb_udc_attr_groups[] = { | |
677 | &usb_udc_attr_group, | |
678 | NULL, | |
679 | }; | |
680 | ||
681 | static int usb_udc_uevent(struct device *dev, struct kobj_uevent_env *env) | |
682 | { | |
683 | struct usb_udc *udc = container_of(dev, struct usb_udc, dev); | |
684 | int ret; | |
685 | ||
686 | ret = add_uevent_var(env, "USB_UDC_NAME=%s", udc->gadget->name); | |
687 | if (ret) { | |
688 | dev_err(dev, "failed to add uevent USB_UDC_NAME\n"); | |
689 | return ret; | |
690 | } | |
691 | ||
692 | if (udc->driver) { | |
693 | ret = add_uevent_var(env, "USB_UDC_DRIVER=%s", | |
694 | udc->driver->function); | |
695 | if (ret) { | |
696 | dev_err(dev, "failed to add uevent USB_UDC_DRIVER\n"); | |
697 | return ret; | |
698 | } | |
699 | } | |
700 | ||
701 | return 0; | |
702 | } | |
703 | ||
704 | static int __init usb_udc_init(void) | |
705 | { | |
706 | udc_class = class_create(THIS_MODULE, "udc"); | |
707 | if (IS_ERR(udc_class)) { | |
708 | pr_err("failed to create udc class --> %ld\n", | |
709 | PTR_ERR(udc_class)); | |
710 | return PTR_ERR(udc_class); | |
711 | } | |
712 | ||
713 | udc_class->dev_uevent = usb_udc_uevent; | |
2ccea03a FB |
714 | return 0; |
715 | } | |
716 | subsys_initcall(usb_udc_init); | |
717 | ||
718 | static void __exit usb_udc_exit(void) | |
719 | { | |
720 | class_destroy(udc_class); | |
721 | } | |
722 | module_exit(usb_udc_exit); | |
723 | ||
724 | MODULE_DESCRIPTION("UDC Framework"); | |
725 | MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>"); | |
726 | MODULE_LICENSE("GPL v2"); |