20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/device.h>
23 #include <linux/list.h>
27 #include <linux/usb/ch9.h>
47 static struct class *udc_class;
75 dev_err(&gadget->
dev,
"failed to map buffer\n");
119 static inline int usb_gadget_start(
struct usb_gadget *gadget,
123 return gadget->
ops->start(driver, bind);
140 static inline int usb_gadget_udc_start(
struct usb_gadget *gadget,
143 return gadget->
ops->udc_start(gadget, driver);
158 static inline void usb_gadget_stop(
struct usb_gadget *gadget,
161 gadget->
ops->stop(driver);
176 static inline void usb_gadget_udc_stop(
struct usb_gadget *gadget,
179 gadget->
ops->udc_stop(gadget, driver);
189 static void usb_udc_release(
struct device *
dev)
194 dev_dbg(dev,
"releasing '%s'\n", dev_name(dev));
217 udc->
dev.release = usb_udc_release;
218 udc->
dev.class = udc_class;
219 udc->
dev.groups = usb_udc_attr_groups;
220 udc->
dev.parent = parent;
249 static int udc_is_newstyle(
struct usb_udc *udc)
251 if (udc->
gadget->ops->udc_start && udc->
gadget->ops->udc_stop)
257 static void usb_gadget_remove_driver(
struct usb_udc *udc)
259 dev_dbg(&udc->
dev,
"unregistering UDC driver [%s]\n",
264 if (udc_is_newstyle(udc)) {
265 usb_gadget_disconnect(udc->
gadget);
290 if (udc->
gadget == gadget)
293 dev_err(gadget->
dev.parent,
"gadget not registered.\n");
299 dev_vdbg(gadget->
dev.parent,
"unregistering gadget\n");
305 usb_gadget_remove_driver(udc);
319 if (!driver || !driver->
bind || !driver->
setup)
329 pr_debug(
"couldn't find an available UDC\n");
334 dev_dbg(&udc->
dev,
"registering UDC driver [%s]\n",
340 if (udc_is_newstyle(udc)) {
344 ret = usb_gadget_udc_start(udc->
gadget, driver);
349 usb_gadget_connect(udc->
gadget);
352 ret = usb_gadget_start(udc->
gadget, driver, driver->
bind);
363 dev_err(&udc->
dev,
"failed to start %s: %d\n",
364 udc->
driver->function, ret);
377 if (!driver || !driver->
unbind)
382 if (udc->
driver == driver) {
383 usb_gadget_remove_driver(udc);
401 usb_gadget_wakeup(udc->
gadget);
413 if (udc_is_newstyle(udc))
415 usb_gadget_connect(udc->
gadget);
417 usb_gadget_disconnect(udc->
gadget);
418 if (udc_is_newstyle(udc))
421 dev_err(dev,
"unsupported command '%s'\n", buf);
429 #define USB_UDC_SPEED_ATTR(name, param) \
430 ssize_t usb_udc_##param##_show(struct device *dev, \
431 struct device_attribute *attr, char *buf) \
433 struct usb_udc *udc = container_of(dev, struct usb_udc, dev); \
434 return snprintf(buf, PAGE_SIZE, "%s\n", \
435 usb_speed_string(udc->gadget->param)); \
437 static DEVICE_ATTR(name, S_IRUSR, usb_udc_##param##_show, NULL)
448 gadget_is_dualspeed(udc->
gadget));
452 #define USB_UDC_ATTR(name) \
453 ssize_t usb_udc_##name##_show(struct device *dev, \
454 struct device_attribute *attr, char *buf) \
456 struct usb_udc *udc = container_of(dev, struct usb_udc, dev); \
457 struct usb_gadget *gadget = udc->gadget; \
459 return snprintf(buf, PAGE_SIZE, "%d\n", gadget->name); \
461 static DEVICE_ATTR(name, S_IRUGO, usb_udc_##name##_show, NULL)
469 static struct attribute *usb_udc_attrs[] = {
471 &dev_attr_soft_connect.attr,
472 &dev_attr_current_speed.attr,
473 &dev_attr_maximum_speed.attr,
475 &dev_attr_is_dualspeed.attr,
476 &dev_attr_is_otg.attr,
477 &dev_attr_is_a_peripheral.attr,
478 &dev_attr_b_hnp_enable.attr,
479 &dev_attr_a_hnp_support.attr,
480 &dev_attr_a_alt_hnp_support.attr,
485 .attrs = usb_udc_attrs,
500 dev_err(dev,
"failed to add uevent USB_UDC_NAME\n");
508 dev_err(dev,
"failed to add uevent USB_UDC_DRIVER\n");
516 static int __init usb_udc_init(
void)
519 if (IS_ERR(udc_class)) {
520 pr_err(
"failed to create udc class --> %ld\n",
522 return PTR_ERR(udc_class);
530 static void __exit usb_udc_exit(
void)