Add linux-next specific files for 20110831
[linux-2.6/next.git] / drivers / usb / gadget / cdc2.c
blobec8007521787decea372ec705bd267ec3ceb00ad
1 /*
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.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <linux/kernel.h>
23 #include <linux/utsname.h>
24 #include <linux/module.h>
26 #include "u_ether.h"
27 #include "u_serial.h"
30 #define DRIVER_DESC "CDC Composite Gadget"
31 #define DRIVER_VERSION "King Kamehameha Day 2008"
33 /*-------------------------------------------------------------------------*/
35 /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
36 * Instead: allocate your own, using normal USB-IF procedures.
39 /* Thanks to NetChip Technologies for donating this product ID.
40 * It's for devices with only this composite CDC configuration.
42 #define CDC_VENDOR_NUM 0x0525 /* NetChip */
43 #define CDC_PRODUCT_NUM 0xa4aa /* CDC Composite: ECM + ACM */
45 /*-------------------------------------------------------------------------*/
48 * Kbuild is not very cooperative with respect to linking separately
49 * compiled library objects into one module. So for now we won't use
50 * separate compilation ... ensuring init/exit sections work to shrink
51 * the runtime footprint, and giving us at least some parts of what
52 * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
55 #include "composite.c"
56 #include "usbstring.c"
57 #include "config.c"
58 #include "epautoconf.c"
59 #include "u_serial.c"
60 #include "f_acm.c"
61 #include "f_ecm.c"
62 #include "u_ether.c"
64 /*-------------------------------------------------------------------------*/
66 static struct usb_device_descriptor device_desc = {
67 .bLength = sizeof device_desc,
68 .bDescriptorType = USB_DT_DEVICE,
70 .bcdUSB = cpu_to_le16(0x0200),
72 .bDeviceClass = USB_CLASS_COMM,
73 .bDeviceSubClass = 0,
74 .bDeviceProtocol = 0,
75 /* .bMaxPacketSize0 = f(hardware) */
77 /* Vendor and product id can be overridden by module parameters. */
78 .idVendor = cpu_to_le16(CDC_VENDOR_NUM),
79 .idProduct = cpu_to_le16(CDC_PRODUCT_NUM),
80 /* .bcdDevice = f(hardware) */
81 /* .iManufacturer = DYNAMIC */
82 /* .iProduct = DYNAMIC */
83 /* NO SERIAL NUMBER */
84 .bNumConfigurations = 1,
87 static struct usb_otg_descriptor otg_descriptor = {
88 .bLength = sizeof otg_descriptor,
89 .bDescriptorType = USB_DT_OTG,
91 /* REVISIT SRP-only hardware is possible, although
92 * it would not be called "OTG" ...
94 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
97 static const struct usb_descriptor_header *otg_desc[] = {
98 (struct usb_descriptor_header *) &otg_descriptor,
99 NULL,
103 /* string IDs are assigned dynamically */
105 #define STRING_MANUFACTURER_IDX 0
106 #define STRING_PRODUCT_IDX 1
108 static char manufacturer[50];
110 static struct usb_string strings_dev[] = {
111 [STRING_MANUFACTURER_IDX].s = manufacturer,
112 [STRING_PRODUCT_IDX].s = DRIVER_DESC,
113 { } /* end of list */
116 static struct usb_gadget_strings stringtab_dev = {
117 .language = 0x0409, /* en-us */
118 .strings = strings_dev,
121 static struct usb_gadget_strings *dev_strings[] = {
122 &stringtab_dev,
123 NULL,
126 static u8 hostaddr[ETH_ALEN];
128 /*-------------------------------------------------------------------------*/
131 * We _always_ have both CDC ECM and CDC ACM functions.
133 static int __init cdc_do_config(struct usb_configuration *c)
135 int status;
137 if (gadget_is_otg(c->cdev->gadget)) {
138 c->descriptors = otg_desc;
139 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
142 status = ecm_bind_config(c, hostaddr);
143 if (status < 0)
144 return status;
146 status = acm_bind_config(c, 0);
147 if (status < 0)
148 return status;
150 return 0;
153 static struct usb_configuration cdc_config_driver = {
154 .label = "CDC Composite (ECM + ACM)",
155 .bConfigurationValue = 1,
156 /* .iConfiguration = DYNAMIC */
157 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
160 /*-------------------------------------------------------------------------*/
162 static int __init cdc_bind(struct usb_composite_dev *cdev)
164 int gcnum;
165 struct usb_gadget *gadget = cdev->gadget;
166 int status;
168 if (!can_support_ecm(cdev->gadget)) {
169 dev_err(&gadget->dev, "controller '%s' not usable\n",
170 gadget->name);
171 return -EINVAL;
174 /* set up network link layer */
175 status = gether_setup(cdev->gadget, hostaddr);
176 if (status < 0)
177 return status;
179 /* set up serial link layer */
180 status = gserial_setup(cdev->gadget, 1);
181 if (status < 0)
182 goto fail0;
184 gcnum = usb_gadget_controller_number(gadget);
185 if (gcnum >= 0)
186 device_desc.bcdDevice = cpu_to_le16(0x0300 | gcnum);
187 else {
188 /* We assume that can_support_ecm() tells the truth;
189 * but if the controller isn't recognized at all then
190 * that assumption is a bit more likely to be wrong.
192 WARNING(cdev, "controller '%s' not recognized; trying %s\n",
193 gadget->name,
194 cdc_config_driver.label);
195 device_desc.bcdDevice =
196 cpu_to_le16(0x0300 | 0x0099);
200 /* Allocate string descriptor numbers ... note that string
201 * contents can be overridden by the composite_dev glue.
204 /* device descriptor strings: manufacturer, product */
205 snprintf(manufacturer, sizeof manufacturer, "%s %s with %s",
206 init_utsname()->sysname, init_utsname()->release,
207 gadget->name);
208 status = usb_string_id(cdev);
209 if (status < 0)
210 goto fail1;
211 strings_dev[STRING_MANUFACTURER_IDX].id = status;
212 device_desc.iManufacturer = status;
214 status = usb_string_id(cdev);
215 if (status < 0)
216 goto fail1;
217 strings_dev[STRING_PRODUCT_IDX].id = status;
218 device_desc.iProduct = status;
220 /* register our configuration */
221 status = usb_add_config(cdev, &cdc_config_driver, cdc_do_config);
222 if (status < 0)
223 goto fail1;
225 dev_info(&gadget->dev, "%s, version: " DRIVER_VERSION "\n",
226 DRIVER_DESC);
228 return 0;
230 fail1:
231 gserial_cleanup();
232 fail0:
233 gether_cleanup();
234 return status;
237 static int __exit cdc_unbind(struct usb_composite_dev *cdev)
239 gserial_cleanup();
240 gether_cleanup();
241 return 0;
244 static struct usb_composite_driver cdc_driver = {
245 .name = "g_cdc",
246 .dev = &device_desc,
247 .strings = dev_strings,
248 .max_speed = USB_SPEED_HIGH,
249 .unbind = __exit_p(cdc_unbind),
252 MODULE_DESCRIPTION(DRIVER_DESC);
253 MODULE_AUTHOR("David Brownell");
254 MODULE_LICENSE("GPL");
256 static int __init init(void)
258 return usb_composite_probe(&cdc_driver, cdc_bind);
260 module_init(init);
262 static void __exit cleanup(void)
264 usb_composite_unregister(&cdc_driver);
266 module_exit(cleanup);