2 * udc.c - Core UDC Framework
4 * Copyright (C) 2010 Texas Instruments
5 * Author: Felipe Balbi <balbi@ti.com>
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.
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.
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/>.
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>
25 #include <linux/dma-mapping.h>
26 #include <linux/workqueue.h>
28 #include <linux/usb/ch9.h>
29 #include <linux/usb/gadget.h>
32 * struct usb_udc - describes one usb device controller
33 * @driver - the gadget driver pointer. For use by the class code
34 * @dev - the child device to the actual controller
35 * @gadget - the gadget. For use by the class code
36 * @list - for use by the udc class driver
38 * This represents the internal data structure which is used by the UDC-class
39 * to hold information about udc driver and gadget together.
42 struct usb_gadget_driver
*driver
;
43 struct usb_gadget
*gadget
;
45 struct list_head list
;
48 static struct class *udc_class
;
49 static LIST_HEAD(udc_list
);
50 static DEFINE_MUTEX(udc_lock
);
52 /* ------------------------------------------------------------------------- */
56 int usb_gadget_map_request(struct usb_gadget
*gadget
,
57 struct usb_request
*req
, int is_in
)
65 mapped
= dma_map_sg(&gadget
->dev
, req
->sg
, req
->num_sgs
,
66 is_in
? DMA_TO_DEVICE
: DMA_FROM_DEVICE
);
68 dev_err(&gadget
->dev
, "failed to map SGs\n");
72 req
->num_mapped_sgs
= mapped
;
74 req
->dma
= dma_map_single(&gadget
->dev
, req
->buf
, req
->length
,
75 is_in
? DMA_TO_DEVICE
: DMA_FROM_DEVICE
);
77 if (dma_mapping_error(&gadget
->dev
, req
->dma
)) {
78 dev_err(&gadget
->dev
, "failed to map buffer\n");
85 EXPORT_SYMBOL_GPL(usb_gadget_map_request
);
87 void usb_gadget_unmap_request(struct usb_gadget
*gadget
,
88 struct usb_request
*req
, int is_in
)
93 if (req
->num_mapped_sgs
) {
94 dma_unmap_sg(&gadget
->dev
, req
->sg
, req
->num_mapped_sgs
,
95 is_in
? DMA_TO_DEVICE
: DMA_FROM_DEVICE
);
97 req
->num_mapped_sgs
= 0;
99 dma_unmap_single(&gadget
->dev
, req
->dma
, req
->length
,
100 is_in
? DMA_TO_DEVICE
: DMA_FROM_DEVICE
);
103 EXPORT_SYMBOL_GPL(usb_gadget_unmap_request
);
105 #endif /* CONFIG_HAS_DMA */
107 /* ------------------------------------------------------------------------- */
109 static void usb_gadget_state_work(struct work_struct
*work
)
111 struct usb_gadget
*gadget
= work_to_gadget(work
);
113 sysfs_notify(&gadget
->dev
.kobj
, NULL
, "state");
116 void usb_gadget_set_state(struct usb_gadget
*gadget
,
117 enum usb_device_state state
)
119 gadget
->state
= state
;
120 schedule_work(&gadget
->work
);
122 EXPORT_SYMBOL_GPL(usb_gadget_set_state
);
124 /* ------------------------------------------------------------------------- */
127 * usb_gadget_udc_start - tells usb device controller to start up
128 * @gadget: The gadget we want to get started
129 * @driver: The driver we want to bind to @gadget
131 * This call is issued by the UDC Class driver when it's about
132 * to register a gadget driver to the device controller, before
133 * calling gadget driver's bind() method.
135 * It allows the controller to be powered off until strictly
136 * necessary to have it powered on.
138 * Returns zero on success, else negative errno.
140 static inline int usb_gadget_udc_start(struct usb_gadget
*gadget
,
141 struct usb_gadget_driver
*driver
)
143 return gadget
->ops
->udc_start(gadget
, driver
);
147 * usb_gadget_udc_stop - tells usb device controller we don't need it anymore
148 * @gadget: The device we want to stop activity
149 * @driver: The driver to unbind from @gadget
151 * This call is issued by the UDC Class driver after calling
152 * gadget driver's unbind() method.
154 * The details are implementation specific, but it can go as
155 * far as powering off UDC completely and disable its data
158 static inline void usb_gadget_udc_stop(struct usb_gadget
*gadget
,
159 struct usb_gadget_driver
*driver
)
161 gadget
->ops
->udc_stop(gadget
, driver
);
165 * usb_udc_release - release the usb_udc struct
166 * @dev: the dev member within usb_udc
168 * This is called by driver's core in order to free memory once the last
169 * reference is released.
171 static void usb_udc_release(struct device
*dev
)
175 udc
= container_of(dev
, struct usb_udc
, dev
);
176 dev_dbg(dev
, "releasing '%s'\n", dev_name(dev
));
180 static const struct attribute_group
*usb_udc_attr_groups
[];
182 static void usb_udc_nop_release(struct device
*dev
)
184 dev_vdbg(dev
, "%s\n", __func__
);
188 * usb_add_gadget_udc_release - adds a new gadget to the udc class driver list
189 * @parent: the parent device to this udc. Usually the controller driver's
191 * @gadget: the gadget to be added to the list.
192 * @release: a gadget release function.
194 * Returns zero on success, negative errno otherwise.
196 int usb_add_gadget_udc_release(struct device
*parent
, struct usb_gadget
*gadget
,
197 void (*release
)(struct device
*dev
))
202 udc
= kzalloc(sizeof(*udc
), GFP_KERNEL
);
206 dev_set_name(&gadget
->dev
, "gadget");
207 INIT_WORK(&gadget
->work
, usb_gadget_state_work
);
208 gadget
->dev
.parent
= parent
;
210 #ifdef CONFIG_HAS_DMA
211 dma_set_coherent_mask(&gadget
->dev
, parent
->coherent_dma_mask
);
212 gadget
->dev
.dma_parms
= parent
->dma_parms
;
213 gadget
->dev
.dma_mask
= parent
->dma_mask
;
217 gadget
->dev
.release
= release
;
219 gadget
->dev
.release
= usb_udc_nop_release
;
221 ret
= device_register(&gadget
->dev
);
225 device_initialize(&udc
->dev
);
226 udc
->dev
.release
= usb_udc_release
;
227 udc
->dev
.class = udc_class
;
228 udc
->dev
.groups
= usb_udc_attr_groups
;
229 udc
->dev
.parent
= parent
;
230 ret
= dev_set_name(&udc
->dev
, "%s", kobject_name(&parent
->kobj
));
234 udc
->gadget
= gadget
;
236 mutex_lock(&udc_lock
);
237 list_add_tail(&udc
->list
, &udc_list
);
239 ret
= device_add(&udc
->dev
);
243 usb_gadget_set_state(gadget
, USB_STATE_NOTATTACHED
);
245 mutex_unlock(&udc_lock
);
250 list_del(&udc
->list
);
251 mutex_unlock(&udc_lock
);
254 put_device(&udc
->dev
);
257 put_device(&gadget
->dev
);
263 EXPORT_SYMBOL_GPL(usb_add_gadget_udc_release
);
266 * usb_add_gadget_udc - adds a new gadget to the udc class driver list
267 * @parent: the parent device to this udc. Usually the controller
269 * @gadget: the gadget to be added to the list
271 * Returns zero on success, negative errno otherwise.
273 int usb_add_gadget_udc(struct device
*parent
, struct usb_gadget
*gadget
)
275 return usb_add_gadget_udc_release(parent
, gadget
, NULL
);
277 EXPORT_SYMBOL_GPL(usb_add_gadget_udc
);
279 static void usb_gadget_remove_driver(struct usb_udc
*udc
)
281 dev_dbg(&udc
->dev
, "unregistering UDC driver [%s]\n",
284 kobject_uevent(&udc
->dev
.kobj
, KOBJ_CHANGE
);
286 usb_gadget_disconnect(udc
->gadget
);
287 udc
->driver
->disconnect(udc
->gadget
);
288 udc
->driver
->unbind(udc
->gadget
);
289 usb_gadget_udc_stop(udc
->gadget
, NULL
);
292 udc
->dev
.driver
= NULL
;
293 udc
->gadget
->dev
.driver
= NULL
;
297 * usb_del_gadget_udc - deletes @udc from udc_list
298 * @gadget: the gadget to be removed.
300 * This, will call usb_gadget_unregister_driver() if
301 * the @udc is still busy.
303 void usb_del_gadget_udc(struct usb_gadget
*gadget
)
305 struct usb_udc
*udc
= NULL
;
307 mutex_lock(&udc_lock
);
308 list_for_each_entry(udc
, &udc_list
, list
)
309 if (udc
->gadget
== gadget
)
312 dev_err(gadget
->dev
.parent
, "gadget not registered.\n");
313 mutex_unlock(&udc_lock
);
318 dev_vdbg(gadget
->dev
.parent
, "unregistering gadget\n");
320 list_del(&udc
->list
);
321 mutex_unlock(&udc_lock
);
324 usb_gadget_remove_driver(udc
);
326 kobject_uevent(&udc
->dev
.kobj
, KOBJ_REMOVE
);
327 flush_work(&gadget
->work
);
328 device_unregister(&udc
->dev
);
329 device_unregister(&gadget
->dev
);
331 EXPORT_SYMBOL_GPL(usb_del_gadget_udc
);
333 /* ------------------------------------------------------------------------- */
335 static int udc_bind_to_driver(struct usb_udc
*udc
, struct usb_gadget_driver
*driver
)
339 dev_dbg(&udc
->dev
, "registering UDC driver [%s]\n",
342 udc
->driver
= driver
;
343 udc
->dev
.driver
= &driver
->driver
;
344 udc
->gadget
->dev
.driver
= &driver
->driver
;
346 ret
= driver
->bind(udc
->gadget
, driver
);
349 ret
= usb_gadget_udc_start(udc
->gadget
, driver
);
351 driver
->unbind(udc
->gadget
);
354 usb_gadget_connect(udc
->gadget
);
356 kobject_uevent(&udc
->dev
.kobj
, KOBJ_CHANGE
);
360 dev_err(&udc
->dev
, "failed to start %s: %d\n",
361 udc
->driver
->function
, ret
);
363 udc
->dev
.driver
= NULL
;
364 udc
->gadget
->dev
.driver
= NULL
;
368 int udc_attach_driver(const char *name
, struct usb_gadget_driver
*driver
)
370 struct usb_udc
*udc
= NULL
;
373 mutex_lock(&udc_lock
);
374 list_for_each_entry(udc
, &udc_list
, list
) {
375 ret
= strcmp(name
, dev_name(&udc
->dev
));
387 ret
= udc_bind_to_driver(udc
, driver
);
389 mutex_unlock(&udc_lock
);
392 EXPORT_SYMBOL_GPL(udc_attach_driver
);
394 int usb_gadget_probe_driver(struct usb_gadget_driver
*driver
)
396 struct usb_udc
*udc
= NULL
;
399 if (!driver
|| !driver
->bind
|| !driver
->setup
)
402 mutex_lock(&udc_lock
);
403 list_for_each_entry(udc
, &udc_list
, list
) {
404 /* For now we take the first one */
409 pr_debug("couldn't find an available UDC\n");
410 mutex_unlock(&udc_lock
);
413 ret
= udc_bind_to_driver(udc
, driver
);
414 mutex_unlock(&udc_lock
);
417 EXPORT_SYMBOL_GPL(usb_gadget_probe_driver
);
419 int usb_gadget_unregister_driver(struct usb_gadget_driver
*driver
)
421 struct usb_udc
*udc
= NULL
;
424 if (!driver
|| !driver
->unbind
)
427 mutex_lock(&udc_lock
);
428 list_for_each_entry(udc
, &udc_list
, list
)
429 if (udc
->driver
== driver
) {
430 usb_gadget_remove_driver(udc
);
435 mutex_unlock(&udc_lock
);
438 EXPORT_SYMBOL_GPL(usb_gadget_unregister_driver
);
440 /* ------------------------------------------------------------------------- */
442 static ssize_t
usb_udc_srp_store(struct device
*dev
,
443 struct device_attribute
*attr
, const char *buf
, size_t n
)
445 struct usb_udc
*udc
= container_of(dev
, struct usb_udc
, dev
);
447 if (sysfs_streq(buf
, "1"))
448 usb_gadget_wakeup(udc
->gadget
);
452 static DEVICE_ATTR(srp
, S_IWUSR
, NULL
, usb_udc_srp_store
);
454 static ssize_t
usb_udc_softconn_store(struct device
*dev
,
455 struct device_attribute
*attr
, const char *buf
, size_t n
)
457 struct usb_udc
*udc
= container_of(dev
, struct usb_udc
, dev
);
459 if (sysfs_streq(buf
, "connect")) {
460 usb_gadget_udc_start(udc
->gadget
, udc
->driver
);
461 usb_gadget_connect(udc
->gadget
);
462 } else if (sysfs_streq(buf
, "disconnect")) {
463 usb_gadget_disconnect(udc
->gadget
);
464 usb_gadget_udc_stop(udc
->gadget
, udc
->driver
);
466 dev_err(dev
, "unsupported command '%s'\n", buf
);
472 static DEVICE_ATTR(soft_connect
, S_IWUSR
, NULL
, usb_udc_softconn_store
);
474 static ssize_t
state_show(struct device
*dev
, struct device_attribute
*attr
,
477 struct usb_udc
*udc
= container_of(dev
, struct usb_udc
, dev
);
478 struct usb_gadget
*gadget
= udc
->gadget
;
480 return sprintf(buf
, "%s\n", usb_state_string(gadget
->state
));
482 static DEVICE_ATTR_RO(state
);
484 #define USB_UDC_SPEED_ATTR(name, param) \
485 ssize_t name##_show(struct device *dev, \
486 struct device_attribute *attr, char *buf) \
488 struct usb_udc *udc = container_of(dev, struct usb_udc, dev); \
489 return snprintf(buf, PAGE_SIZE, "%s\n", \
490 usb_speed_string(udc->gadget->param)); \
492 static DEVICE_ATTR_RO(name)
494 static USB_UDC_SPEED_ATTR(current_speed
, speed
);
495 static USB_UDC_SPEED_ATTR(maximum_speed
, max_speed
);
497 #define USB_UDC_ATTR(name) \
498 ssize_t name##_show(struct device *dev, \
499 struct device_attribute *attr, char *buf) \
501 struct usb_udc *udc = container_of(dev, struct usb_udc, dev); \
502 struct usb_gadget *gadget = udc->gadget; \
504 return snprintf(buf, PAGE_SIZE, "%d\n", gadget->name); \
506 static DEVICE_ATTR_RO(name)
508 static USB_UDC_ATTR(is_otg
);
509 static USB_UDC_ATTR(is_a_peripheral
);
510 static USB_UDC_ATTR(b_hnp_enable
);
511 static USB_UDC_ATTR(a_hnp_support
);
512 static USB_UDC_ATTR(a_alt_hnp_support
);
514 static struct attribute
*usb_udc_attrs
[] = {
516 &dev_attr_soft_connect
.attr
,
517 &dev_attr_state
.attr
,
518 &dev_attr_current_speed
.attr
,
519 &dev_attr_maximum_speed
.attr
,
521 &dev_attr_is_otg
.attr
,
522 &dev_attr_is_a_peripheral
.attr
,
523 &dev_attr_b_hnp_enable
.attr
,
524 &dev_attr_a_hnp_support
.attr
,
525 &dev_attr_a_alt_hnp_support
.attr
,
529 static const struct attribute_group usb_udc_attr_group
= {
530 .attrs
= usb_udc_attrs
,
533 static const struct attribute_group
*usb_udc_attr_groups
[] = {
538 static int usb_udc_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
540 struct usb_udc
*udc
= container_of(dev
, struct usb_udc
, dev
);
543 ret
= add_uevent_var(env
, "USB_UDC_NAME=%s", udc
->gadget
->name
);
545 dev_err(dev
, "failed to add uevent USB_UDC_NAME\n");
550 ret
= add_uevent_var(env
, "USB_UDC_DRIVER=%s",
551 udc
->driver
->function
);
553 dev_err(dev
, "failed to add uevent USB_UDC_DRIVER\n");
561 static int __init
usb_udc_init(void)
563 udc_class
= class_create(THIS_MODULE
, "udc");
564 if (IS_ERR(udc_class
)) {
565 pr_err("failed to create udc class --> %ld\n",
567 return PTR_ERR(udc_class
);
570 udc_class
->dev_uevent
= usb_udc_uevent
;
573 subsys_initcall(usb_udc_init
);
575 static void __exit
usb_udc_exit(void)
577 class_destroy(udc_class
);
579 module_exit(usb_udc_exit
);
581 MODULE_DESCRIPTION("UDC Framework");
582 MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
583 MODULE_LICENSE("GPL v2");