This patch adds some ifdefs to make the serial gadget work
        as a usb_function module.

Signed-off-by: Felipe Balbi <[EMAIL PROTECTED]>
Signed-off-by: Ragner Magalhes <[EMAIL PROTECTED]>
---

Index: linux-2.6/drivers/usb/gadget/serial.c
===================================================================
--- linux-2.6.orig/drivers/usb/gadget/serial.c
+++ linux-2.6/drivers/usb/gadget/serial.c
@@ -47,2 +47,3 @@
 #include <linux/usb_gadget.h>
+#include <linux/usb/composite.h>
 
@@ -220,3 +221,2 @@ static int gs_setup_class(struct usb_gad
        const struct usb_ctrlrequest *ctrl);
-static void gs_setup_complete(struct usb_ep *ep, struct usb_request *req);
 static void gs_disconnect(struct usb_gadget *gadget);
@@ -224,4 +224,7 @@ static int gs_set_config(struct gs_dev *
 static void gs_reset_config(struct gs_dev *dev);
+#ifndef USB_COMPOSITE_DEVICE
+static void gs_setup_complete(struct usb_ep *ep, struct usb_request *req);
 static int gs_build_config_buf(u8 *buf, enum usb_device_speed speed,
                u8 type, unsigned int index, int is_otg);
+#endif /* !USB_COMPOSITE_DEVICE */
 
@@ -287,3 +290,15 @@ static const struct tty_operations gs_tt
 static struct tty_driver *gs_tty_driver;
+static struct usb_gadget_strings gs_string_table;
 
+#ifdef USB_COMPOSITE_DEVICE
+/* USB_FUNCTION */
+struct usb_function gs_usb_function = {
+       .name           = GS_LONG_NAME,
+       .strings        = &gs_string_table,
+       .bind           = gs_bind,
+       .unbind         = gs_unbind,
+       .setup          = gs_setup,
+       .disconnect     = gs_disconnect,
+};
+#else
 /* gadget driver struct */
@@ -304,2 +319,3 @@ static struct usb_gadget_driver gs_gadge
 };
+#endif /* USB_COMPOSITE_DEVICE */
 
@@ -308,2 +324,6 @@ static struct usb_gadget_driver gs_gadge
 
+#ifdef USB_COMPOSITE_DEVICE
+#define GS_CONTROL_STR_ID      1
+#define GS_DATA_STR_ID         2
+#else
 #define GS_MANUFACTURER_STR_ID 1
@@ -315,2 +335,3 @@ static struct usb_gadget_driver gs_gadge
 #define GS_DATA_STR_ID         7
+#endif /* USB_COMPOSITE_DEVICE */
 
@@ -318,3 +339,8 @@ static struct usb_gadget_driver gs_gadge
 static char manufacturer[50];
+
 static struct usb_string gs_strings[] = {
+#ifdef USB_COMPOSITE_DEVICE
+       { GS_CONTROL_STR_ID, "Serial Control" },
+       { GS_DATA_STR_ID, "Serial Data" },
+#else
        { GS_MANUFACTURER_STR_ID, manufacturer },
@@ -326,2 +352,3 @@ static struct usb_string gs_strings[] = 
        { GS_DATA_STR_ID, "Gadget Serial Data" },
+#endif /* USB_COMPOSITE_DEVICE */
        {  } /* end of list */
@@ -333,3 +360,6 @@ static struct usb_gadget_strings gs_stri
 };
-
+#ifndef USB_COMPOSITE_DEVICE
+/* if defined composite, use the device descriptor
+ * from composite for all gadgets.
+ */
 static struct usb_device_descriptor gs_device_desc = {
@@ -347,2 +377,3 @@ static struct usb_device_descriptor gs_d
 };
+#endif /* !USB_COMPOSITE_DEVICE */
 
@@ -354,2 +385,6 @@ static struct usb_otg_descriptor gs_otg_
 
+#ifndef USB_COMPOSITE_DEVICE
+/* if defined composite, use the config descriptor
+ * from composite for all gadgets.
+ */
 static struct usb_config_descriptor gs_bulk_config_desc = {
@@ -375,4 +410,5 @@ static struct usb_config_descriptor gs_a
 };
+#endif /* !USB_COMPOSITE_DEVICE */
 
-static const struct usb_interface_descriptor gs_bulk_interface_desc = {
+struct usb_interface_descriptor gs_bulk_interface_desc = {
        .bLength =              USB_DT_INTERFACE_SIZE,
@@ -387,3 +423,3 @@ static const struct usb_interface_descri
 
-static const struct usb_interface_descriptor gs_control_interface_desc = {
+struct usb_interface_descriptor gs_control_interface_desc = {
        .bLength =              USB_DT_INTERFACE_SIZE,
@@ -398,3 +434,3 @@ static const struct usb_interface_descri
 
-static const struct usb_interface_descriptor gs_data_interface_desc = {
+struct usb_interface_descriptor gs_data_interface_desc = {
        .bLength =              USB_DT_INTERFACE_SIZE,
@@ -431,3 +467,3 @@ static struct usb_cdc_acm_descriptor gs_
 
-static const struct usb_cdc_union_desc gs_union_desc = {
+static struct usb_cdc_union_desc gs_union_desc = {
        .bLength =              sizeof(gs_union_desc),
@@ -462,3 +498,3 @@ static struct usb_endpoint_descriptor gs
 
-static const struct usb_descriptor_header *gs_bulk_fullspeed_function[] = {
+static struct usb_descriptor_header *gs_bulk_fullspeed_function[] = {
        (struct usb_descriptor_header *) &gs_otg_descriptor,
@@ -470,3 +506,3 @@ static const struct usb_descriptor_heade
 
-static const struct usb_descriptor_header *gs_acm_fullspeed_function[] = {
+struct usb_descriptor_header *gs_acm_fullspeed_function[] = {
        (struct usb_descriptor_header *) &gs_otg_descriptor,
@@ -508,2 +544,3 @@ static struct usb_endpoint_descriptor gs
 
+#ifndef USB_COMPOSITE_DEVICE
 static struct usb_qualifier_descriptor gs_qualifier_desc = {
@@ -515,4 +552,5 @@ static struct usb_qualifier_descriptor g
 };
+#endif /* !USB_COMPOSITE_DEVICE */
 
-static const struct usb_descriptor_header *gs_bulk_highspeed_function[] = {
+static struct usb_descriptor_header *gs_bulk_highspeed_function[] = {
        (struct usb_descriptor_header *) &gs_otg_descriptor,
@@ -524,3 +562,3 @@ static const struct usb_descriptor_heade
 
-static const struct usb_descriptor_header *gs_acm_highspeed_function[] = {
+struct usb_descriptor_header *gs_acm_highspeed_function[] = {
        (struct usb_descriptor_header *) &gs_otg_descriptor,
@@ -576,5 +614,10 @@ static int __init gs_module_init(void)
 
+#ifdef USB_COMPOSITE_DEVICE
+       retval = usb_function_register(&gs_usb_function);
+#else
        retval = usb_gadget_register_driver(&gs_gadget_driver);
+#endif /* USB_COMPOSITE_DEVICE */
        if (retval) {
-               printk(KERN_ERR "gs_module_init: cannot register gadget driver, 
ret=%d\n", retval);
+               printk(KERN_ERR "gs_module_init: cannot register gadget"
+                                               " driver, ret=%d\n", retval);
                return retval;
@@ -602,5 +645,10 @@ static int __init gs_module_init(void)
        if (retval) {
+#ifdef USB_COMPOSITE_DEVICE
+               usb_function_unregister(&gs_usb_function);
+#else
                usb_gadget_unregister_driver(&gs_gadget_driver);
+#endif /* USB_COMPOSITE_DEVICE */
                put_tty_driver(gs_tty_driver);
-               printk(KERN_ERR "gs_module_init: cannot register tty driver, 
ret=%d\n", retval);
+               printk(KERN_ERR "gs_module_init: cannot register tty driver,"
+                                                       " ret=%d\n", retval);
                return retval;
@@ -608,3 +656,4 @@ static int __init gs_module_init(void)
 
-       printk(KERN_INFO "gs_module_init: %s %s loaded\n", GS_LONG_NAME, 
GS_VERSION_STR);
+       printk(KERN_INFO "gs_module_init: %s %s loaded\n",
+                                       GS_LONG_NAME, GS_VERSION_STR);
        return 0;
@@ -621,3 +670,7 @@ static void __exit gs_module_exit(void)
        put_tty_driver(gs_tty_driver);
+#ifdef USB_COMPOSITE_DEVICE
+       usb_function_unregister(&gs_usb_function);
+#else
        usb_gadget_unregister_driver(&gs_gadget_driver);
+#endif /* USB_COMPOSITE_DEVICE */
 
@@ -1352,4 +1405,9 @@ requeue:
  */
-static int __init gs_bind(struct usb_gadget *gadget)
+static int __devinit gs_bind(struct usb_gadget *gadget)
 {
+#ifdef USB_COMPOSITE_DEVICE
+       struct usb_composite_dev *cdev = get_gadget_data(gadget);
+#else
+       int gcnum;
+#endif /* USB_COMPOSITE_DEVICE */
        int ret;
@@ -1357,3 +1415,2 @@ static int __init gs_bind(struct usb_gad
        struct gs_dev *dev;
-       int gcnum;
 
@@ -1366,2 +1423,10 @@ static int __init gs_bind(struct usb_gad
 
+#ifdef USB_COMPOSITE_DEVICE
+       gs_usb_function.descriptors = use_acm ?
+               gs_acm_fullspeed_function : gs_bulk_fullspeed_function;
+#ifdef CONFIG_USB_GADGET_DUALSPEED
+       gs_usb_function.hs_descriptors = use_acm ?
+               gs_acm_highspeed_function : gs_bulk_highspeed_function;
+#endif
+#else
        gcnum = usb_gadget_controller_number(gadget);
@@ -1379,2 +1444,3 @@ static int __init gs_bind(struct usb_gad
        usb_ep_autoconfig_reset(gadget);
+#endif /* USB_COMPOSITE_DEVICE */
 
@@ -1398,4 +1464,6 @@ static int __init gs_bind(struct usb_gad
                }
+#ifndef USB_COMPOSITE_DEVICE
                gs_device_desc.idProduct = __constant_cpu_to_le16(
                                                GS_CDC_PRODUCT_ID),
+#endif /* !USB_COMPOSITE_DEVICE */
                EP_NOTIFY_NAME = ep->name;
@@ -1404,2 +1472,3 @@ static int __init gs_bind(struct usb_gad
 
+#ifndef USB_COMPOSITE_DEVICE
        gs_device_desc.bDeviceClass = use_acm
@@ -1407,4 +1476,6 @@ static int __init gs_bind(struct usb_gad
        gs_device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
+#endif /* !USB_COMPOSITE_DEVICE */
 
 #ifdef CONFIG_USB_GADGET_DUALSPEED
+#ifndef USB_COMPOSITE_DEVICE
        gs_qualifier_desc.bDeviceClass = use_acm
@@ -1413,2 +1484,3 @@ static int __init gs_bind(struct usb_gad
        gs_qualifier_desc.bMaxPacketSize0 = gs_device_desc.bMaxPacketSize0;
+#endif /* !USB_COMPOSITE_DEVICE */
        /* assume endpoints are dual-speed */
@@ -1422,5 +1494,10 @@ static int __init gs_bind(struct usb_gad
 
+#ifndef USB_COMPOSITE_DEVICE
        usb_gadget_set_selfpowered(gadget);
+#endif /* !USB_COMPOSITE_DEVICE */
 
        if (gadget->is_otg) {
+#ifdef USB_COMPOSITE_DEVICE
+               gs_otg_descriptor.bmAttributes |= USB_OTG_HNP;
+#else
                gs_otg_descriptor.bmAttributes |= USB_OTG_HNP,
@@ -1428,2 +1505,3 @@ static int __init gs_bind(struct usb_gad
                gs_acm_config_desc.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
+#endif /* !USB_COMPOSITE_DEVICE */
        }
@@ -1442,3 +1520,7 @@ static int __init gs_bind(struct usb_gad
        INIT_LIST_HEAD(&dev->dev_req_list);
+#ifdef USB_COMPOSITE_DEVICE
+       set_composite_data(cdev, dev);
+#else
        set_gadget_data(gadget, dev);
+#endif /* USB_COMPOSITE_DEVICE */
 
@@ -1451,2 +1533,5 @@ static int __init gs_bind(struct usb_gad
        /* preallocate control response and buffer */
+#ifdef USB_COMPOSITE_DEVICE
+       dev->dev_ctrl_req = cdev->req;
+#else
        dev->dev_ctrl_req = gs_alloc_req(gadget->ep0, GS_MAX_DESC_LEN,
@@ -1460,2 +1545,4 @@ static int __init gs_bind(struct usb_gad
        gadget->ep0->driver_data = dev;
+#endif /* USB_COMPOSITE_DEVICE */
+
 
@@ -1479,4 +1566,9 @@ static void /* __init_or_exit */ gs_unbi
 {
+#ifdef USB_COMPOSITE_DEVICE
+       struct usb_composite_dev *cdev = get_gadget_data(gadget);
+       struct gs_dev *dev = get_composite_data(cdev);
+#else
        struct gs_dev *dev = get_gadget_data(gadget);
 
+#endif /* USB_COMPOSITE_DEVICE */
        gs_device = NULL;
@@ -1485,2 +1577,12 @@ static void /* __init_or_exit */ gs_unbi
        if (dev != NULL) {
+#ifdef USB_COMPOSITE_DEVICE
+               usb_composite_ep_reset(dev->dev_notify_ep);
+               usb_composite_ep_reset(dev->dev_in_ep);
+               usb_composite_ep_reset(dev->dev_out_ep);
+               gs_reset_config(dev);
+
+               gs_free_ports(dev);
+               kfree(dev);
+               set_composite_data(cdev, NULL);
+#else
                if (dev->dev_ctrl_req != NULL) {
@@ -1492,2 +1594,3 @@ static void /* __init_or_exit */ gs_unbi
                set_gadget_data(gadget, NULL);
+#endif /* USB_COMPOSITE_DEVICE */
        }
@@ -1511,4 +1614,6 @@ static int gs_setup(struct usb_gadget *g
        int ret = -EOPNOTSUPP;
+#ifndef USB_COMPOSITE_DEVICE
        struct gs_dev *dev = get_gadget_data(gadget);
        struct usb_request *req = dev->dev_ctrl_req;
+#endif /* USB_COMPOSITE_DEVICE */
        u16 wIndex = le16_to_cpu(ctrl->wIndex);
@@ -1533,2 +1638,3 @@ static int gs_setup(struct usb_gadget *g
 
+#ifndef USB_COMPOSITE_DEVICE
        /* respond with data transfer before status phase? */
@@ -1546,2 +1652,3 @@ static int gs_setup(struct usb_gadget *g
        }
+#endif /* !USB_COMPOSITE_DEVICE */
 
@@ -1555,3 +1662,8 @@ static int gs_setup_standard(struct usb_
        int ret = -EOPNOTSUPP;
+#ifdef USB_COMPOSITE_DEVICE
+       struct usb_composite_dev *cdev = get_gadget_data(gadget);
+       struct gs_dev *dev = get_composite_data(cdev);
+#else
        struct gs_dev *dev = get_gadget_data(gadget);
+#endif
        struct usb_request *req = dev->dev_ctrl_req;
@@ -1562,2 +1674,4 @@ static int gs_setup_standard(struct usb_
        switch (ctrl->bRequest) {
+
+#ifndef USB_COMPOSITE_DEVICE
        case USB_REQ_GET_DESCRIPTOR:
@@ -1604,2 +1718,3 @@ static int gs_setup_standard(struct usb_
                break;
+#endif /* !USB_COMPOSITE_DEVICE */
 
@@ -1608,2 +1723,8 @@ static int gs_setup_standard(struct usb_
                        break;
+#ifdef USB_COMPOSITE_DEVICE
+               if (wValue == COMPOSITE_CONFIG_VALUE) {
+                       wValue = use_acm ?
+                               GS_ACM_CONFIG_ID : GS_BULK_CONFIG_ID;
+               }
+#endif /* USB_COMPOSITE_DEVICE */
                spin_lock(&dev->dev_lock);
@@ -1613,2 +1734,3 @@ static int gs_setup_standard(struct usb_
 
+#ifndef USB_COMPOSITE_DEVICE
        case USB_REQ_GET_CONFIGURATION:
@@ -1619,4 +1741,8 @@ static int gs_setup_standard(struct usb_
                break;
+#endif /* !USB_COMPOSITE_DEVICE */
 
        case USB_REQ_SET_INTERFACE:
+#ifdef USB_COMPOSITE_DEVICE
+               wIndex -= cdev->current_func->interface_shift;
+#endif /* USB_COMPOSITE_DEVICE */
                if (ctrl->bRequestType != USB_RECIP_INTERFACE
@@ -1657,2 +1783,5 @@ set_interface_done:
        case USB_REQ_GET_INTERFACE:
+#ifdef USB_COMPOSITE_DEVICE
+               wIndex -= cdev->current_func->interface_shift;
+#endif /* USB_COMPOSITE_DEVICE */
                if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)
@@ -1685,3 +1814,8 @@ static int gs_setup_class(struct usb_gad
        int ret = -EOPNOTSUPP;
+#ifdef USB_COMPOSITE_DEVICE
+       struct usb_composite_dev *cdev = get_gadget_data(gadget);
+       struct gs_dev *dev = get_composite_data(cdev);
+#else
        struct gs_dev *dev = get_gadget_data(gadget);
+#endif
        struct gs_port *port = dev->dev_port[0];        /* ACM only has one 
port */
@@ -1692,2 +1826,6 @@ static int gs_setup_class(struct usb_gad
 
+#ifdef USB_COMPOSITE_DEVICE
+               wIndex -= cdev->current_func->interface_shift;
+#endif /* USB_COMPOSITE_DEVICE */
+
        switch (ctrl->bRequest) {
@@ -1729,2 +1867,3 @@ static int gs_setup_class(struct usb_gad
 
+#ifndef USB_COMPOSITE_DEVICE
 /*
@@ -1739,2 +1878,3 @@ static void gs_setup_complete(struct usb
 }
+#endif /* !USB_COMPOSITE_DEVICE */
 
@@ -1749,4 +1889,9 @@ static void gs_disconnect(struct usb_gad
 {
-       unsigned long flags;
+#ifdef USB_COMPOSITE_DEVICE
+       struct usb_composite_dev *cdev = get_gadget_data(gadget);
+       struct gs_dev *dev = get_composite_data(cdev);
+#else
        struct gs_dev *dev = get_gadget_data(gadget);
+#endif /* USB_COMPOSITE_DEVICE */
+       unsigned long flags;
 
@@ -1912,3 +2057,3 @@ static int gs_set_config(struct gs_dev *
        }
-
+#ifndef USB_COMPOSITE_DEVICE
        printk(KERN_INFO "gs_set_config: %s configured, %s speed %s config\n",
@@ -1917,3 +2062,3 @@ static int gs_set_config(struct gs_dev *
                config == GS_BULK_CONFIG_ID ? "BULK" : "CDC-ACM");
-
+#endif /* !USB_COMPOSITE_DEVICE */
        return 0;
@@ -1972,3 +2117,6 @@ static void gs_reset_config(struct gs_de
 }
-
+/* if defined composite, build the config
+ * buf for all gadgets in the composite.
+ */
+#ifndef USB_COMPOSITE_DEVICE
 /*
@@ -1985,3 +2133,3 @@ static int gs_build_config_buf(u8 *buf, 
        const struct usb_config_descriptor *config_desc;
-       const struct usb_descriptor_header **function;
+       struct usb_descriptor_header **function;
 
@@ -2019,2 +2167,3 @@ static int gs_build_config_buf(u8 *buf, 
 }
+#endif /* !USB_COMPOSITE_DEVICE */
 

--
Best Regards,

Felipe Balbi
[EMAIL PROTECTED]

Nokia Institute of Technology - INdT
Kernel Developers Team

+55 92 2126 1003


-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
linux-usb-devel@lists.sourceforge.net
To unsubscribe, use the last form field at:
https://lists.sourceforge.net/lists/listinfo/linux-usb-devel

Reply via email to