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 LIST_HEAD(udc_list);
48
static DEFINE_MUTEX(udc_lock);
50
/* ------------------------------------------------------------------------- */
53
* usb_gadget_start - tells usb device controller to start up
54
* @gadget: The gadget we want to get started
55
* @driver: The driver we want to bind to @gadget
56
* @bind: The bind function for @driver
58
* This call is issued by the UDC Class driver when it's about
59
* to register a gadget driver to the device controller, before
60
* calling gadget driver's bind() method.
62
* It allows the controller to be powered off until strictly
63
* necessary to have it powered on.
65
* Returns zero on success, else negative errno.
67
static inline int usb_gadget_start(struct usb_gadget *gadget,
68
struct usb_gadget_driver *driver,
69
int (*bind)(struct usb_gadget *))
71
return gadget->ops->start(driver, bind);
75
* usb_gadget_udc_start - tells usb device controller to start up
76
* @gadget: The gadget we want to get started
77
* @driver: The driver we want to bind to @gadget
79
* This call is issued by the UDC Class driver when it's about
80
* to register a gadget driver to the device controller, before
81
* calling gadget driver's bind() method.
83
* It allows the controller to be powered off until strictly
84
* necessary to have it powered on.
86
* Returns zero on success, else negative errno.
88
static inline int usb_gadget_udc_start(struct usb_gadget *gadget,
89
struct usb_gadget_driver *driver)
91
return gadget->ops->udc_start(gadget, driver);
95
* usb_gadget_stop - tells usb device controller we don't need it anymore
96
* @gadget: The device we want to stop activity
97
* @driver: The driver to unbind from @gadget
99
* This call is issued by the UDC Class driver after calling
100
* gadget driver's unbind() method.
102
* The details are implementation specific, but it can go as
103
* far as powering off UDC completely and disable its data
106
static inline void usb_gadget_stop(struct usb_gadget *gadget,
107
struct usb_gadget_driver *driver)
109
gadget->ops->stop(driver);
113
* usb_gadget_udc_stop - tells usb device controller we don't need it anymore
114
* @gadget: The device we want to stop activity
115
* @driver: The driver to unbind from @gadget
117
* This call is issued by the UDC Class driver after calling
118
* gadget driver's unbind() method.
120
* The details are implementation specific, but it can go as
121
* far as powering off UDC completely and disable its data
124
static inline void usb_gadget_udc_stop(struct usb_gadget *gadget,
125
struct usb_gadget_driver *driver)
127
gadget->ops->udc_stop(gadget, driver);
131
* usb_udc_release - release the usb_udc struct
132
* @dev: the dev member within usb_udc
134
* This is called by driver's core in order to free memory once the last
135
* reference is released.
137
static void usb_udc_release(struct device *dev)
141
udc = container_of(dev, struct usb_udc, dev);
142
dev_dbg(dev, "releasing '%s'\n", dev_name(dev));
146
static const struct attribute_group *usb_udc_attr_groups[];
148
* usb_add_gadget_udc - adds a new gadget to the udc class driver list
149
* @parent: the parent device to this udc. Usually the controller
151
* @gadget: the gadget to be added to the list
153
* Returns zero on success, negative errno otherwise.
155
int usb_add_gadget_udc(struct device *parent, struct usb_gadget *gadget)
160
udc = kzalloc(sizeof(*udc), GFP_KERNEL);
164
device_initialize(&udc->dev);
165
udc->dev.release = usb_udc_release;
166
udc->dev.class = udc_class;
167
udc->dev.groups = usb_udc_attr_groups;
168
udc->dev.parent = parent;
169
ret = dev_set_name(&udc->dev, "%s", kobject_name(&parent->kobj));
173
udc->gadget = gadget;
175
mutex_lock(&udc_lock);
176
list_add_tail(&udc->list, &udc_list);
178
ret = device_add(&udc->dev);
182
mutex_unlock(&udc_lock);
186
list_del(&udc->list);
187
mutex_unlock(&udc_lock);
190
put_device(&udc->dev);
195
EXPORT_SYMBOL_GPL(usb_add_gadget_udc);
197
static int udc_is_newstyle(struct usb_udc *udc)
199
if (udc->gadget->ops->udc_start && udc->gadget->ops->udc_stop)
205
static void usb_gadget_remove_driver(struct usb_udc *udc)
207
dev_dbg(&udc->dev, "unregistering UDC driver [%s]\n",
210
kobject_uevent(&udc->dev.kobj, KOBJ_CHANGE);
212
if (udc_is_newstyle(udc)) {
213
udc->driver->disconnect(udc->gadget);
214
udc->driver->unbind(udc->gadget);
215
usb_gadget_udc_stop(udc->gadget, udc->driver);
216
usb_gadget_disconnect(udc->gadget);
218
usb_gadget_stop(udc->gadget, udc->driver);
222
udc->dev.driver = NULL;
226
* usb_del_gadget_udc - deletes @udc from udc_list
227
* @gadget: the gadget to be removed.
229
* This, will call usb_gadget_unregister_driver() if
230
* the @udc is still busy.
232
void usb_del_gadget_udc(struct usb_gadget *gadget)
234
struct usb_udc *udc = NULL;
236
mutex_lock(&udc_lock);
237
list_for_each_entry(udc, &udc_list, list)
238
if (udc->gadget == gadget)
241
dev_err(gadget->dev.parent, "gadget not registered.\n");
242
mutex_unlock(&udc_lock);
247
dev_vdbg(gadget->dev.parent, "unregistering gadget\n");
249
list_del(&udc->list);
250
mutex_unlock(&udc_lock);
253
usb_gadget_remove_driver(udc);
255
kobject_uevent(&udc->dev.kobj, KOBJ_REMOVE);
256
device_unregister(&udc->dev);
258
EXPORT_SYMBOL_GPL(usb_del_gadget_udc);
260
/* ------------------------------------------------------------------------- */
262
int usb_gadget_probe_driver(struct usb_gadget_driver *driver,
263
int (*bind)(struct usb_gadget *))
265
struct usb_udc *udc = NULL;
268
if (!driver || !bind || !driver->setup)
271
mutex_lock(&udc_lock);
272
list_for_each_entry(udc, &udc_list, list) {
273
/* For now we take the first one */
278
pr_debug("couldn't find an available UDC\n");
279
mutex_unlock(&udc_lock);
283
dev_dbg(&udc->dev, "registering UDC driver [%s]\n",
286
udc->driver = driver;
287
udc->dev.driver = &driver->driver;
289
if (udc_is_newstyle(udc)) {
290
ret = bind(udc->gadget);
293
ret = usb_gadget_udc_start(udc->gadget, driver);
295
driver->unbind(udc->gadget);
298
usb_gadget_connect(udc->gadget);
301
ret = usb_gadget_start(udc->gadget, driver, bind);
307
kobject_uevent(&udc->dev.kobj, KOBJ_CHANGE);
308
mutex_unlock(&udc_lock);
312
dev_err(&udc->dev, "failed to start %s: %d\n",
313
udc->driver->function, ret);
315
udc->dev.driver = NULL;
316
mutex_unlock(&udc_lock);
319
EXPORT_SYMBOL_GPL(usb_gadget_probe_driver);
321
int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
323
struct usb_udc *udc = NULL;
326
if (!driver || !driver->unbind)
329
mutex_lock(&udc_lock);
330
list_for_each_entry(udc, &udc_list, list)
331
if (udc->driver == driver) {
332
usb_gadget_remove_driver(udc);
337
mutex_unlock(&udc_lock);
340
EXPORT_SYMBOL_GPL(usb_gadget_unregister_driver);
342
/* ------------------------------------------------------------------------- */
344
static ssize_t usb_udc_srp_store(struct device *dev,
345
struct device_attribute *attr, const char *buf, size_t n)
347
struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
349
if (sysfs_streq(buf, "1"))
350
usb_gadget_wakeup(udc->gadget);
354
static DEVICE_ATTR(srp, S_IWUSR, NULL, usb_udc_srp_store);
356
static ssize_t usb_udc_softconn_store(struct device *dev,
357
struct device_attribute *attr, const char *buf, size_t n)
359
struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
361
if (sysfs_streq(buf, "connect")) {
362
usb_gadget_connect(udc->gadget);
363
} else if (sysfs_streq(buf, "disconnect")) {
364
usb_gadget_disconnect(udc->gadget);
366
dev_err(dev, "unsupported command '%s'\n", buf);
372
static DEVICE_ATTR(soft_connect, S_IWUSR, NULL, usb_udc_softconn_store);
374
static ssize_t usb_udc_speed_show(struct device *dev,
375
struct device_attribute *attr, char *buf)
377
struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
378
return snprintf(buf, PAGE_SIZE, "%s\n",
379
usb_speed_string(udc->gadget->speed));
381
static DEVICE_ATTR(speed, S_IRUGO, usb_udc_speed_show, NULL);
383
#define USB_UDC_ATTR(name) \
384
ssize_t usb_udc_##name##_show(struct device *dev, \
385
struct device_attribute *attr, char *buf) \
387
struct usb_udc *udc = container_of(dev, struct usb_udc, dev); \
388
struct usb_gadget *gadget = udc->gadget; \
390
return snprintf(buf, PAGE_SIZE, "%d\n", gadget->name); \
392
static DEVICE_ATTR(name, S_IRUGO, usb_udc_##name##_show, NULL)
394
static USB_UDC_ATTR(is_dualspeed);
395
static USB_UDC_ATTR(is_otg);
396
static USB_UDC_ATTR(is_a_peripheral);
397
static USB_UDC_ATTR(b_hnp_enable);
398
static USB_UDC_ATTR(a_hnp_support);
399
static USB_UDC_ATTR(a_alt_hnp_support);
401
static struct attribute *usb_udc_attrs[] = {
403
&dev_attr_soft_connect.attr,
404
&dev_attr_speed.attr,
406
&dev_attr_is_dualspeed.attr,
407
&dev_attr_is_otg.attr,
408
&dev_attr_is_a_peripheral.attr,
409
&dev_attr_b_hnp_enable.attr,
410
&dev_attr_a_hnp_support.attr,
411
&dev_attr_a_alt_hnp_support.attr,
415
static const struct attribute_group usb_udc_attr_group = {
416
.attrs = usb_udc_attrs,
419
static const struct attribute_group *usb_udc_attr_groups[] = {
424
static int usb_udc_uevent(struct device *dev, struct kobj_uevent_env *env)
426
struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
429
ret = add_uevent_var(env, "USB_UDC_NAME=%s", udc->gadget->name);
431
dev_err(dev, "failed to add uevent USB_UDC_NAME\n");
436
ret = add_uevent_var(env, "USB_UDC_DRIVER=%s",
437
udc->driver->function);
439
dev_err(dev, "failed to add uevent USB_UDC_DRIVER\n");
447
static int __init usb_udc_init(void)
449
udc_class = class_create(THIS_MODULE, "udc");
450
if (IS_ERR(udc_class)) {
451
pr_err("failed to create udc class --> %ld\n",
453
return PTR_ERR(udc_class);
456
udc_class->dev_uevent = usb_udc_uevent;
459
subsys_initcall(usb_udc_init);
461
static void __exit usb_udc_exit(void)
463
class_destroy(udc_class);
465
module_exit(usb_udc_exit);
467
MODULE_DESCRIPTION("UDC Framework");
468
MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
469
MODULE_LICENSE("GPL v2");