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>
26 #include <linux/usb/ch9.h>
27 #include <linux/usb/gadget.h>
30 * struct usb_udc - describes one usb device controller
31 * @driver - the gadget driver pointer. For use by the class code
32 * @dev - the child device to the actual controller
33 * @gadget - the gadget. For use by the class code
34 * @list - for use by the udc class driver
36 * This represents the internal data structure which is used by the UDC-class
37 * to hold information about udc driver and gadget together.
40 struct usb_gadget_driver
*driver
;
41 struct usb_gadget
*gadget
;
43 struct list_head list
;
46 static struct class *udc_class
;
47 static struct device_type udc_device_type
;
48 static LIST_HEAD(udc_list
);
49 static DEFINE_MUTEX(udc_lock
);
51 /* ------------------------------------------------------------------------- */
54 * usb_gadget_start - tells usb device controller to start up
55 * @gadget: The gadget we want to get started
56 * @driver: The driver we want to bind to @gadget
57 * @bind: The bind function for @driver
59 * This call is issued by the UDC Class driver when it's about
60 * to register a gadget driver to the device controller, before
61 * calling gadget driver's bind() method.
63 * It allows the controller to be powered off until strictly
64 * necessary to have it powered on.
66 * Returns zero on success, else negative errno.
68 static inline int usb_gadget_start(struct usb_gadget
*gadget
,
69 struct usb_gadget_driver
*driver
,
70 int (*bind
)(struct usb_gadget
*))
72 return gadget
->ops
->start(driver
, bind
);
76 * usb_gadget_stop - tells usb device controller we don't need it anymore
77 * @gadget: The device we want to stop activity
78 * @driver: The driver to unbind from @gadget
80 * This call is issued by the UDC Class driver after calling
81 * gadget driver's unbind() method.
83 * The details are implementation specific, but it can go as
84 * far as powering off UDC completely and disable its data
87 static inline void usb_gadget_stop(struct usb_gadget
*gadget
,
88 struct usb_gadget_driver
*driver
)
90 gadget
->ops
->stop(driver
);
94 * usb_udc_release - release the usb_udc struct
95 * @dev: the dev member within usb_udc
97 * This is called by driver's core in order to free memory once the last
98 * reference is released.
100 static void usb_udc_release(struct device
*dev
)
104 udc
= container_of(dev
, struct usb_udc
, dev
);
105 dev_dbg(dev
, "releasing '%s'\n", dev_name(dev
));
110 * usb_add_gadget_udc - adds a new gadget to the udc class driver list
111 * @parent: the parent device to this udc. Usually the controller
113 * @gadget: the gadget to be added to the list
115 * Returns zero on success, negative errno otherwise.
117 int usb_add_gadget_udc(struct device
*parent
, struct usb_gadget
*gadget
)
122 udc
= kzalloc(sizeof(*udc
), GFP_KERNEL
);
126 device_initialize(&udc
->dev
);
127 udc
->dev
.release
= usb_udc_release
;
128 udc
->dev
.class = udc_class
;
129 udc
->dev
.parent
= parent
;
130 ret
= dev_set_name(&udc
->dev
, "%s", kobject_name(&parent
->kobj
));
134 udc
->gadget
= gadget
;
136 mutex_lock(&udc_lock
);
137 list_add_tail(&udc
->list
, &udc_list
);
139 ret
= device_add(&udc
->dev
);
143 mutex_unlock(&udc_lock
);
147 list_del(&udc
->list
);
148 mutex_unlock(&udc_lock
);
151 put_device(&udc
->dev
);
156 EXPORT_SYMBOL_GPL(usb_add_gadget_udc
);
158 static void usb_gadget_remove_driver(struct usb_udc
*udc
)
160 dev_dbg(&udc
->dev
, "unregistering UDC driver [%s]\n",
163 kobject_uevent(&udc
->dev
.kobj
, KOBJ_CHANGE
);
165 usb_gadget_stop(udc
->gadget
, udc
->driver
);
168 udc
->dev
.driver
= NULL
;
172 * usb_del_gadget_udc - deletes @udc from udc_list
173 * @gadget: the gadget to be removed.
175 * This, will call usb_gadget_unregister_driver() if
176 * the @udc is still busy.
178 void usb_del_gadget_udc(struct usb_gadget
*gadget
)
180 struct usb_udc
*udc
= NULL
;
182 mutex_lock(&udc_lock
);
183 list_for_each_entry(udc
, &udc_list
, list
)
184 if (udc
->gadget
== gadget
)
187 dev_err(gadget
->dev
.parent
, "gadget not registered.\n");
188 mutex_unlock(&udc_lock
);
193 dev_vdbg(gadget
->dev
.parent
, "unregistering gadget\n");
195 list_del(&udc
->list
);
196 mutex_unlock(&udc_lock
);
199 usb_gadget_remove_driver(udc
);
201 kobject_uevent(&udc
->dev
.kobj
, KOBJ_REMOVE
);
202 device_unregister(&udc
->dev
);
204 EXPORT_SYMBOL_GPL(usb_del_gadget_udc
);
206 /* ------------------------------------------------------------------------- */
208 int usb_gadget_probe_driver(struct usb_gadget_driver
*driver
,
209 int (*bind
)(struct usb_gadget
*))
211 struct usb_udc
*udc
= NULL
;
214 if (!driver
|| !bind
|| !driver
->setup
)
217 mutex_lock(&udc_lock
);
218 list_for_each_entry(udc
, &udc_list
, list
) {
219 /* For now we take the first one */
224 pr_debug("couldn't find an available UDC\n");
225 mutex_unlock(&udc_lock
);
229 dev_dbg(&udc
->dev
, "registering UDC driver [%s]\n",
232 udc
->driver
= driver
;
233 udc
->dev
.driver
= &driver
->driver
;
235 ret
= usb_gadget_start(udc
->gadget
, driver
, bind
);
239 kobject_uevent(&udc
->dev
.kobj
, KOBJ_CHANGE
);
240 mutex_unlock(&udc_lock
);
244 dev_err(&udc
->dev
, "failed to start %s: %d\n",
245 udc
->driver
->function
, ret
);
247 udc
->dev
.driver
= NULL
;
248 mutex_unlock(&udc_lock
);
251 EXPORT_SYMBOL_GPL(usb_gadget_probe_driver
);
253 int usb_gadget_unregister_driver(struct usb_gadget_driver
*driver
)
255 struct usb_udc
*udc
= NULL
;
258 if (!driver
|| !driver
->unbind
)
261 mutex_lock(&udc_lock
);
262 list_for_each_entry(udc
, &udc_list
, list
)
263 if (udc
->driver
== driver
) {
264 usb_gadget_remove_driver(udc
);
269 mutex_unlock(&udc_lock
);
272 EXPORT_SYMBOL_GPL(usb_gadget_unregister_driver
);
274 /* ------------------------------------------------------------------------- */
276 static ssize_t
usb_udc_srp_store(struct device
*dev
,
277 struct device_attribute
*attr
, const char *buf
, size_t n
)
279 struct usb_udc
*udc
= dev_get_drvdata(dev
);
281 if (sysfs_streq(buf
, "1"))
282 usb_gadget_wakeup(udc
->gadget
);
286 static DEVICE_ATTR(srp
, S_IWUSR
, NULL
, usb_udc_srp_store
);
288 static ssize_t
usb_udc_softconn_store(struct device
*dev
,
289 struct device_attribute
*attr
, const char *buf
, size_t n
)
291 struct usb_udc
*udc
= dev_get_drvdata(dev
);
293 if (sysfs_streq(buf
, "connect")) {
294 usb_gadget_connect(udc
->gadget
);
295 } else if (sysfs_streq(buf
, "disconnect")) {
296 usb_gadget_disconnect(udc
->gadget
);
298 dev_err(dev
, "unsupported command '%s'\n", buf
);
304 static DEVICE_ATTR(soft_connect
, S_IWUSR
, NULL
, usb_udc_softconn_store
);
306 static ssize_t
usb_udc_speed_show(struct device
*dev
,
307 struct device_attribute
*attr
, char *buf
)
309 struct usb_udc
*udc
= dev_get_drvdata(dev
);
310 struct usb_gadget
*gadget
= udc
->gadget
;
312 switch (gadget
->speed
) {
314 return snprintf(buf
, PAGE_SIZE
, "low-speed\n");
316 return snprintf(buf
, PAGE_SIZE
, "full-speed\n");
318 return snprintf(buf
, PAGE_SIZE
, "high-speed\n");
319 case USB_SPEED_WIRELESS
:
320 return snprintf(buf
, PAGE_SIZE
, "wireless\n");
321 case USB_SPEED_SUPER
:
322 return snprintf(buf
, PAGE_SIZE
, "super-speed\n");
323 case USB_SPEED_UNKNOWN
: /* FALLTHROUGH */
325 return snprintf(buf
, PAGE_SIZE
, "UNKNOWN\n");
328 static DEVICE_ATTR(speed
, S_IRUSR
, usb_udc_speed_show
, NULL
);
330 #define USB_UDC_ATTR(name) \
331 ssize_t usb_udc_##name##_show(struct device *dev, \
332 struct device_attribute *attr, char *buf) \
334 struct usb_udc *udc = dev_get_drvdata(dev); \
335 struct usb_gadget *gadget = udc->gadget; \
337 return snprintf(buf, PAGE_SIZE, "%d\n", gadget->name); \
339 static DEVICE_ATTR(name, S_IRUSR, usb_udc_##name##_show, NULL)
341 static USB_UDC_ATTR(is_dualspeed
);
342 static USB_UDC_ATTR(is_otg
);
343 static USB_UDC_ATTR(is_a_peripheral
);
344 static USB_UDC_ATTR(b_hnp_enable
);
345 static USB_UDC_ATTR(a_hnp_support
);
346 static USB_UDC_ATTR(a_alt_hnp_support
);
348 static struct attribute
*usb_udc_attrs
[] = {
350 &dev_attr_soft_connect
.attr
,
351 &dev_attr_speed
.attr
,
353 &dev_attr_is_dualspeed
.attr
,
354 &dev_attr_is_otg
.attr
,
355 &dev_attr_is_a_peripheral
.attr
,
356 &dev_attr_b_hnp_enable
.attr
,
357 &dev_attr_a_hnp_support
.attr
,
358 &dev_attr_a_alt_hnp_support
.attr
,
362 static const struct attribute_group usb_udc_attr_group
= {
363 .attrs
= usb_udc_attrs
,
366 static const struct attribute_group
*usb_udc_attr_groups
[] = {
371 static int usb_udc_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
373 struct usb_udc
*udc
= container_of(dev
, struct usb_udc
, dev
);
376 ret
= add_uevent_var(env
, "USB_UDC_NAME=%s", udc
->gadget
->name
);
378 dev_err(dev
, "failed to add uevent USB_UDC_NAME\n");
383 ret
= add_uevent_var(env
, "USB_UDC_DRIVER=%s",
384 udc
->driver
->function
);
386 dev_err(dev
, "failed to add uevent USB_UDC_DRIVER\n");
394 static int __init
usb_udc_init(void)
396 udc_class
= class_create(THIS_MODULE
, "udc");
397 if (IS_ERR(udc_class
)) {
398 pr_err("failed to create udc class --> %ld\n",
400 return PTR_ERR(udc_class
);
403 udc_class
->dev_uevent
= usb_udc_uevent
;
404 udc_device_type
.groups
= usb_udc_attr_groups
;
408 subsys_initcall(usb_udc_init
);
410 static void __exit
usb_udc_exit(void)
412 class_destroy(udc_class
);
414 module_exit(usb_udc_exit
);
416 MODULE_DESCRIPTION("UDC Framework");
417 MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
418 MODULE_LICENSE("GPL v2");