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>
30 #include <linux/usb.h>
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
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
41 * This represents the internal data structure which is used by the UDC-class
42 * to hold information about udc driver and gadget together.
45 struct usb_gadget_driver
*driver
;
46 struct usb_gadget
*gadget
;
48 struct list_head list
;
52 static struct class *udc_class
;
53 static LIST_HEAD(udc_list
);
54 static DEFINE_MUTEX(udc_lock
);
56 /* ------------------------------------------------------------------------- */
60 int usb_gadget_map_request(struct usb_gadget
*gadget
,
61 struct usb_request
*req
, int is_in
)
63 struct device
*dev
= gadget
->dev
.parent
;
71 mapped
= dma_map_sg(dev
, req
->sg
, req
->num_sgs
,
72 is_in
? DMA_TO_DEVICE
: DMA_FROM_DEVICE
);
74 dev_err(&gadget
->dev
, "failed to map SGs\n");
78 req
->num_mapped_sgs
= mapped
;
80 req
->dma
= dma_map_single(dev
, req
->buf
, req
->length
,
81 is_in
? DMA_TO_DEVICE
: DMA_FROM_DEVICE
);
83 if (dma_mapping_error(dev
, req
->dma
)) {
84 dev_err(dev
, "failed to map buffer\n");
91 EXPORT_SYMBOL_GPL(usb_gadget_map_request
);
93 void usb_gadget_unmap_request(struct usb_gadget
*gadget
,
94 struct usb_request
*req
, int is_in
)
99 if (req
->num_mapped_sgs
) {
100 dma_unmap_sg(gadget
->dev
.parent
, req
->sg
, req
->num_mapped_sgs
,
101 is_in
? DMA_TO_DEVICE
: DMA_FROM_DEVICE
);
103 req
->num_mapped_sgs
= 0;
105 dma_unmap_single(gadget
->dev
.parent
, req
->dma
, req
->length
,
106 is_in
? DMA_TO_DEVICE
: DMA_FROM_DEVICE
);
109 EXPORT_SYMBOL_GPL(usb_gadget_unmap_request
);
111 #endif /* CONFIG_HAS_DMA */
113 /* ------------------------------------------------------------------------- */
116 * usb_gadget_giveback_request - give the request back to the gadget layer
117 * Context: in_interrupt()
119 * This is called by device controller drivers in order to return the
120 * completed request back to the gadget layer.
122 void usb_gadget_giveback_request(struct usb_ep
*ep
,
123 struct usb_request
*req
)
125 if (likely(req
->status
== 0))
126 usb_led_activity(USB_LED_EVENT_GADGET
);
128 req
->complete(ep
, req
);
130 EXPORT_SYMBOL_GPL(usb_gadget_giveback_request
);
132 /* ------------------------------------------------------------------------- */
134 static void usb_gadget_state_work(struct work_struct
*work
)
136 struct usb_gadget
*gadget
= work_to_gadget(work
);
137 struct usb_udc
*udc
= gadget
->udc
;
140 sysfs_notify(&udc
->dev
.kobj
, NULL
, "state");
143 void usb_gadget_set_state(struct usb_gadget
*gadget
,
144 enum usb_device_state state
)
146 gadget
->state
= state
;
147 schedule_work(&gadget
->work
);
149 EXPORT_SYMBOL_GPL(usb_gadget_set_state
);
151 /* ------------------------------------------------------------------------- */
153 static void usb_udc_connect_control(struct usb_udc
*udc
)
156 usb_gadget_connect(udc
->gadget
);
158 usb_gadget_disconnect(udc
->gadget
);
162 * usb_udc_vbus_handler - updates the udc core vbus status, and try to
163 * connect or disconnect gadget
164 * @gadget: The gadget which vbus change occurs
165 * @status: The vbus status
167 * The udc driver calls it when it wants to connect or disconnect gadget
168 * according to vbus status.
170 void usb_udc_vbus_handler(struct usb_gadget
*gadget
, bool status
)
172 struct usb_udc
*udc
= gadget
->udc
;
176 usb_udc_connect_control(udc
);
179 EXPORT_SYMBOL_GPL(usb_udc_vbus_handler
);
182 * usb_gadget_udc_reset - notifies the udc core that bus reset occurs
183 * @gadget: The gadget which bus reset occurs
184 * @driver: The gadget driver we want to notify
186 * If the udc driver has bus reset handler, it needs to call this when the bus
187 * reset occurs, it notifies the gadget driver that the bus reset occurs as
188 * well as updates gadget state.
190 void usb_gadget_udc_reset(struct usb_gadget
*gadget
,
191 struct usb_gadget_driver
*driver
)
193 driver
->reset(gadget
);
194 usb_gadget_set_state(gadget
, USB_STATE_DEFAULT
);
196 EXPORT_SYMBOL_GPL(usb_gadget_udc_reset
);
199 * usb_gadget_udc_start - tells usb device controller to start up
200 * @udc: The UDC to be started
202 * This call is issued by the UDC Class driver when it's about
203 * to register a gadget driver to the device controller, before
204 * calling gadget driver's bind() method.
206 * It allows the controller to be powered off until strictly
207 * necessary to have it powered on.
209 * Returns zero on success, else negative errno.
211 static inline int usb_gadget_udc_start(struct usb_udc
*udc
)
213 return udc
->gadget
->ops
->udc_start(udc
->gadget
, udc
->driver
);
217 * usb_gadget_udc_stop - tells usb device controller we don't need it anymore
218 * @gadget: The device we want to stop activity
219 * @driver: The driver to unbind from @gadget
221 * This call is issued by the UDC Class driver after calling
222 * gadget driver's unbind() method.
224 * The details are implementation specific, but it can go as
225 * far as powering off UDC completely and disable its data
228 static inline void usb_gadget_udc_stop(struct usb_udc
*udc
)
230 udc
->gadget
->ops
->udc_stop(udc
->gadget
);
234 * usb_udc_release - release the usb_udc struct
235 * @dev: the dev member within usb_udc
237 * This is called by driver's core in order to free memory once the last
238 * reference is released.
240 static void usb_udc_release(struct device
*dev
)
244 udc
= container_of(dev
, struct usb_udc
, dev
);
245 dev_dbg(dev
, "releasing '%s'\n", dev_name(dev
));
249 static const struct attribute_group
*usb_udc_attr_groups
[];
251 static void usb_udc_nop_release(struct device
*dev
)
253 dev_vdbg(dev
, "%s\n", __func__
);
257 * usb_add_gadget_udc_release - adds a new gadget to the udc class driver list
258 * @parent: the parent device to this udc. Usually the controller driver's
260 * @gadget: the gadget to be added to the list.
261 * @release: a gadget release function.
263 * Returns zero on success, negative errno otherwise.
265 int usb_add_gadget_udc_release(struct device
*parent
, struct usb_gadget
*gadget
,
266 void (*release
)(struct device
*dev
))
271 udc
= kzalloc(sizeof(*udc
), GFP_KERNEL
);
275 dev_set_name(&gadget
->dev
, "gadget");
276 INIT_WORK(&gadget
->work
, usb_gadget_state_work
);
277 gadget
->dev
.parent
= parent
;
279 #ifdef CONFIG_HAS_DMA
280 dma_set_coherent_mask(&gadget
->dev
, parent
->coherent_dma_mask
);
281 gadget
->dev
.dma_parms
= parent
->dma_parms
;
282 gadget
->dev
.dma_mask
= parent
->dma_mask
;
286 gadget
->dev
.release
= release
;
288 gadget
->dev
.release
= usb_udc_nop_release
;
290 ret
= device_register(&gadget
->dev
);
294 device_initialize(&udc
->dev
);
295 udc
->dev
.release
= usb_udc_release
;
296 udc
->dev
.class = udc_class
;
297 udc
->dev
.groups
= usb_udc_attr_groups
;
298 udc
->dev
.parent
= parent
;
299 ret
= dev_set_name(&udc
->dev
, "%s", kobject_name(&parent
->kobj
));
303 udc
->gadget
= gadget
;
306 mutex_lock(&udc_lock
);
307 list_add_tail(&udc
->list
, &udc_list
);
309 ret
= device_add(&udc
->dev
);
313 usb_gadget_set_state(gadget
, USB_STATE_NOTATTACHED
);
316 mutex_unlock(&udc_lock
);
321 list_del(&udc
->list
);
322 mutex_unlock(&udc_lock
);
325 put_device(&udc
->dev
);
326 device_del(&gadget
->dev
);
329 put_device(&gadget
->dev
);
335 EXPORT_SYMBOL_GPL(usb_add_gadget_udc_release
);
338 * usb_add_gadget_udc - adds a new gadget to the udc class driver list
339 * @parent: the parent device to this udc. Usually the controller
341 * @gadget: the gadget to be added to the list
343 * Returns zero on success, negative errno otherwise.
345 int usb_add_gadget_udc(struct device
*parent
, struct usb_gadget
*gadget
)
347 return usb_add_gadget_udc_release(parent
, gadget
, NULL
);
349 EXPORT_SYMBOL_GPL(usb_add_gadget_udc
);
351 static void usb_gadget_remove_driver(struct usb_udc
*udc
)
353 dev_dbg(&udc
->dev
, "unregistering UDC driver [%s]\n",
354 udc
->driver
->function
);
356 kobject_uevent(&udc
->dev
.kobj
, KOBJ_CHANGE
);
358 usb_gadget_disconnect(udc
->gadget
);
359 udc
->driver
->disconnect(udc
->gadget
);
360 udc
->driver
->unbind(udc
->gadget
);
361 usb_gadget_udc_stop(udc
);
364 udc
->dev
.driver
= NULL
;
365 udc
->gadget
->dev
.driver
= NULL
;
369 * usb_del_gadget_udc - deletes @udc from udc_list
370 * @gadget: the gadget to be removed.
372 * This, will call usb_gadget_unregister_driver() if
373 * the @udc is still busy.
375 void usb_del_gadget_udc(struct usb_gadget
*gadget
)
377 struct usb_udc
*udc
= gadget
->udc
;
382 dev_vdbg(gadget
->dev
.parent
, "unregistering gadget\n");
384 mutex_lock(&udc_lock
);
385 list_del(&udc
->list
);
386 mutex_unlock(&udc_lock
);
389 usb_gadget_remove_driver(udc
);
391 kobject_uevent(&udc
->dev
.kobj
, KOBJ_REMOVE
);
392 flush_work(&gadget
->work
);
393 device_unregister(&udc
->dev
);
394 device_unregister(&gadget
->dev
);
396 EXPORT_SYMBOL_GPL(usb_del_gadget_udc
);
398 /* ------------------------------------------------------------------------- */
400 static int udc_bind_to_driver(struct usb_udc
*udc
, struct usb_gadget_driver
*driver
)
404 dev_dbg(&udc
->dev
, "registering UDC driver [%s]\n",
407 udc
->driver
= driver
;
408 udc
->dev
.driver
= &driver
->driver
;
409 udc
->gadget
->dev
.driver
= &driver
->driver
;
411 ret
= driver
->bind(udc
->gadget
, driver
);
414 ret
= usb_gadget_udc_start(udc
);
416 driver
->unbind(udc
->gadget
);
419 usb_udc_connect_control(udc
);
421 kobject_uevent(&udc
->dev
.kobj
, KOBJ_CHANGE
);
425 dev_err(&udc
->dev
, "failed to start %s: %d\n",
426 udc
->driver
->function
, ret
);
428 udc
->dev
.driver
= NULL
;
429 udc
->gadget
->dev
.driver
= NULL
;
433 int usb_udc_attach_driver(const char *name
, struct usb_gadget_driver
*driver
)
435 struct usb_udc
*udc
= NULL
;
438 mutex_lock(&udc_lock
);
439 list_for_each_entry(udc
, &udc_list
, list
) {
440 ret
= strcmp(name
, dev_name(&udc
->dev
));
452 ret
= udc_bind_to_driver(udc
, driver
);
454 mutex_unlock(&udc_lock
);
457 EXPORT_SYMBOL_GPL(usb_udc_attach_driver
);
459 int usb_gadget_probe_driver(struct usb_gadget_driver
*driver
)
461 struct usb_udc
*udc
= NULL
;
464 if (!driver
|| !driver
->bind
|| !driver
->setup
)
467 mutex_lock(&udc_lock
);
468 list_for_each_entry(udc
, &udc_list
, list
) {
469 /* For now we take the first one */
474 pr_debug("couldn't find an available UDC\n");
475 mutex_unlock(&udc_lock
);
478 ret
= udc_bind_to_driver(udc
, driver
);
479 mutex_unlock(&udc_lock
);
482 EXPORT_SYMBOL_GPL(usb_gadget_probe_driver
);
484 int usb_gadget_unregister_driver(struct usb_gadget_driver
*driver
)
486 struct usb_udc
*udc
= NULL
;
489 if (!driver
|| !driver
->unbind
)
492 mutex_lock(&udc_lock
);
493 list_for_each_entry(udc
, &udc_list
, list
)
494 if (udc
->driver
== driver
) {
495 usb_gadget_remove_driver(udc
);
496 usb_gadget_set_state(udc
->gadget
,
497 USB_STATE_NOTATTACHED
);
502 mutex_unlock(&udc_lock
);
505 EXPORT_SYMBOL_GPL(usb_gadget_unregister_driver
);
507 /* ------------------------------------------------------------------------- */
509 static ssize_t
usb_udc_srp_store(struct device
*dev
,
510 struct device_attribute
*attr
, const char *buf
, size_t n
)
512 struct usb_udc
*udc
= container_of(dev
, struct usb_udc
, dev
);
514 if (sysfs_streq(buf
, "1"))
515 usb_gadget_wakeup(udc
->gadget
);
519 static DEVICE_ATTR(srp
, S_IWUSR
, NULL
, usb_udc_srp_store
);
521 static ssize_t
usb_udc_softconn_store(struct device
*dev
,
522 struct device_attribute
*attr
, const char *buf
, size_t n
)
524 struct usb_udc
*udc
= container_of(dev
, struct usb_udc
, dev
);
527 dev_err(dev
, "soft-connect without a gadget driver\n");
531 if (sysfs_streq(buf
, "connect")) {
532 usb_gadget_udc_start(udc
);
533 usb_gadget_connect(udc
->gadget
);
534 } else if (sysfs_streq(buf
, "disconnect")) {
535 usb_gadget_disconnect(udc
->gadget
);
536 udc
->driver
->disconnect(udc
->gadget
);
537 usb_gadget_udc_stop(udc
);
539 dev_err(dev
, "unsupported command '%s'\n", buf
);
545 static DEVICE_ATTR(soft_connect
, S_IWUSR
, NULL
, usb_udc_softconn_store
);
547 static ssize_t
state_show(struct device
*dev
, struct device_attribute
*attr
,
550 struct usb_udc
*udc
= container_of(dev
, struct usb_udc
, dev
);
551 struct usb_gadget
*gadget
= udc
->gadget
;
553 return sprintf(buf
, "%s\n", usb_state_string(gadget
->state
));
555 static DEVICE_ATTR_RO(state
);
557 #define USB_UDC_SPEED_ATTR(name, param) \
558 ssize_t name##_show(struct device *dev, \
559 struct device_attribute *attr, char *buf) \
561 struct usb_udc *udc = container_of(dev, struct usb_udc, dev); \
562 return snprintf(buf, PAGE_SIZE, "%s\n", \
563 usb_speed_string(udc->gadget->param)); \
565 static DEVICE_ATTR_RO(name)
567 static USB_UDC_SPEED_ATTR(current_speed
, speed
);
568 static USB_UDC_SPEED_ATTR(maximum_speed
, max_speed
);
570 #define USB_UDC_ATTR(name) \
571 ssize_t name##_show(struct device *dev, \
572 struct device_attribute *attr, char *buf) \
574 struct usb_udc *udc = container_of(dev, struct usb_udc, dev); \
575 struct usb_gadget *gadget = udc->gadget; \
577 return snprintf(buf, PAGE_SIZE, "%d\n", gadget->name); \
579 static DEVICE_ATTR_RO(name)
581 static USB_UDC_ATTR(is_otg
);
582 static USB_UDC_ATTR(is_a_peripheral
);
583 static USB_UDC_ATTR(b_hnp_enable
);
584 static USB_UDC_ATTR(a_hnp_support
);
585 static USB_UDC_ATTR(a_alt_hnp_support
);
586 static USB_UDC_ATTR(is_selfpowered
);
588 static struct attribute
*usb_udc_attrs
[] = {
590 &dev_attr_soft_connect
.attr
,
591 &dev_attr_state
.attr
,
592 &dev_attr_current_speed
.attr
,
593 &dev_attr_maximum_speed
.attr
,
595 &dev_attr_is_otg
.attr
,
596 &dev_attr_is_a_peripheral
.attr
,
597 &dev_attr_b_hnp_enable
.attr
,
598 &dev_attr_a_hnp_support
.attr
,
599 &dev_attr_a_alt_hnp_support
.attr
,
600 &dev_attr_is_selfpowered
.attr
,
604 static const struct attribute_group usb_udc_attr_group
= {
605 .attrs
= usb_udc_attrs
,
608 static const struct attribute_group
*usb_udc_attr_groups
[] = {
613 static int usb_udc_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
615 struct usb_udc
*udc
= container_of(dev
, struct usb_udc
, dev
);
618 ret
= add_uevent_var(env
, "USB_UDC_NAME=%s", udc
->gadget
->name
);
620 dev_err(dev
, "failed to add uevent USB_UDC_NAME\n");
625 ret
= add_uevent_var(env
, "USB_UDC_DRIVER=%s",
626 udc
->driver
->function
);
628 dev_err(dev
, "failed to add uevent USB_UDC_DRIVER\n");
636 static int __init
usb_udc_init(void)
638 udc_class
= class_create(THIS_MODULE
, "udc");
639 if (IS_ERR(udc_class
)) {
640 pr_err("failed to create udc class --> %ld\n",
642 return PTR_ERR(udc_class
);
645 udc_class
->dev_uevent
= usb_udc_uevent
;
648 subsys_initcall(usb_udc_init
);
650 static void __exit
usb_udc_exit(void)
652 class_destroy(udc_class
);
654 module_exit(usb_udc_exit
);
656 MODULE_DESCRIPTION("UDC Framework");
657 MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
658 MODULE_LICENSE("GPL v2");