2 * cdc2.c -- CDC Composite driver, with ECM and ACM support
4 * Copyright (C) 2008 David Brownell
5 * Copyright (C) 2008 Nokia Corporation
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
13 #include <linux/kernel.h>
14 #include <linux/module.h>
20 #define DRIVER_DESC "CDC Composite Gadget"
21 #define DRIVER_VERSION "King Kamehameha Day 2008"
23 /*-------------------------------------------------------------------------*/
25 /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
26 * Instead: allocate your own, using normal USB-IF procedures.
29 /* Thanks to NetChip Technologies for donating this product ID.
30 * It's for devices with only this composite CDC configuration.
32 #define CDC_VENDOR_NUM 0x0525 /* NetChip */
33 #define CDC_PRODUCT_NUM 0xa4aa /* CDC Composite: ECM + ACM */
35 /*-------------------------------------------------------------------------*/
36 USB_GADGET_COMPOSITE_OPTIONS();
39 * Kbuild is not very cooperative with respect to linking separately
40 * compiled library objects into one module. So for now we won't use
41 * separate compilation ... ensuring init/exit sections work to shrink
42 * the runtime footprint, and giving us at least some parts of what
43 * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
48 /*-------------------------------------------------------------------------*/
50 static struct usb_device_descriptor device_desc = {
51 .bLength = sizeof device_desc,
52 .bDescriptorType = USB_DT_DEVICE,
54 .bcdUSB = cpu_to_le16(0x0200),
56 .bDeviceClass = USB_CLASS_COMM,
59 /* .bMaxPacketSize0 = f(hardware) */
61 /* Vendor and product id can be overridden by module parameters. */
62 .idVendor = cpu_to_le16(CDC_VENDOR_NUM),
63 .idProduct = cpu_to_le16(CDC_PRODUCT_NUM),
64 /* .bcdDevice = f(hardware) */
65 /* .iManufacturer = DYNAMIC */
66 /* .iProduct = DYNAMIC */
67 /* NO SERIAL NUMBER */
68 .bNumConfigurations = 1,
71 static struct usb_otg_descriptor otg_descriptor = {
72 .bLength = sizeof otg_descriptor,
73 .bDescriptorType = USB_DT_OTG,
75 /* REVISIT SRP-only hardware is possible, although
76 * it would not be called "OTG" ...
78 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
81 static const struct usb_descriptor_header *otg_desc[] = {
82 (struct usb_descriptor_header *) &otg_descriptor,
87 /* string IDs are assigned dynamically */
88 static struct usb_string strings_dev[] = {
89 [USB_GADGET_MANUFACTURER_IDX].s = "",
90 [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC,
91 [USB_GADGET_SERIAL_IDX].s = "",
95 static struct usb_gadget_strings stringtab_dev = {
96 .language = 0x0409, /* en-us */
97 .strings = strings_dev,
100 static struct usb_gadget_strings *dev_strings[] = {
105 static u8 hostaddr[ETH_ALEN];
106 static struct eth_dev *the_dev;
107 /*-------------------------------------------------------------------------*/
108 static struct usb_function *f_acm;
109 static struct usb_function_instance *fi_serial;
111 static unsigned char tty_line;
113 * We _always_ have both CDC ECM and CDC ACM functions.
115 static int __init cdc_do_config(struct usb_configuration *c)
117 struct f_serial_opts *opts;
120 if (gadget_is_otg(c->cdev->gadget)) {
121 c->descriptors = otg_desc;
122 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
125 status = ecm_bind_config(c, hostaddr, the_dev);
129 fi_serial = usb_get_function_instance("acm");
130 if (IS_ERR(fi_serial))
131 return PTR_ERR(fi_serial);
133 opts = container_of(fi_serial, struct f_serial_opts, func_inst);
134 opts->port_num = tty_line;
136 f_acm = usb_get_function(fi_serial);
140 status = usb_add_function(c, f_acm);
145 usb_put_function(f_acm);
147 usb_put_function_instance(fi_serial);
151 static struct usb_configuration cdc_config_driver = {
152 .label = "CDC Composite (ECM + ACM)",
153 .bConfigurationValue = 1,
154 /* .iConfiguration = DYNAMIC */
155 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
158 /*-------------------------------------------------------------------------*/
160 static int __init cdc_bind(struct usb_composite_dev *cdev)
162 struct usb_gadget *gadget = cdev->gadget;
165 if (!can_support_ecm(cdev->gadget)) {
166 dev_err(&gadget->dev, "controller '%s' not usable\n",
171 /* set up network link layer */
172 the_dev = gether_setup(cdev->gadget, hostaddr);
174 return PTR_ERR(the_dev);
176 /* set up serial link layer */
177 status = gserial_alloc_line(&tty_line);
181 /* Allocate string descriptor numbers ... note that string
182 * contents can be overridden by the composite_dev glue.
185 status = usb_string_ids_tab(cdev, strings_dev);
188 device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
189 device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
191 /* register our configuration */
192 status = usb_add_config(cdev, &cdc_config_driver, cdc_do_config);
196 usb_composite_overwrite_options(cdev, &coverwrite);
197 dev_info(&gadget->dev, "%s, version: " DRIVER_VERSION "\n",
203 gserial_free_line(tty_line);
205 gether_cleanup(the_dev);
209 static int __exit cdc_unbind(struct usb_composite_dev *cdev)
211 usb_put_function(f_acm);
212 usb_put_function_instance(fi_serial);
213 gserial_free_line(tty_line);
214 gether_cleanup(the_dev);
218 static __refdata struct usb_composite_driver cdc_driver = {
221 .strings = dev_strings,
222 .max_speed = USB_SPEED_HIGH,
224 .unbind = __exit_p(cdc_unbind),
227 MODULE_DESCRIPTION(DRIVER_DESC);
228 MODULE_AUTHOR("David Brownell");
229 MODULE_LICENSE("GPL");
231 static int __init init(void)
233 return usb_composite_probe(&cdc_driver);
237 static void __exit cleanup(void)
239 usb_composite_unregister(&cdc_driver);
241 module_exit(cleanup);