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