Merge branch 'stable/vmalloc-3.2' of git://git.kernel.org/pub/scm/linux/kernel/git...
[pandora-kernel.git] / drivers / usb / gadget / cdc2.c
1 /*
2  * cdc2.c -- CDC Composite driver, with ECM and ACM support
3  *
4  * Copyright (C) 2008 David Brownell
5  * Copyright (C) 2008 Nokia Corporation
6  *
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.
11  */
12
13 #include <linux/kernel.h>
14 #include <linux/utsname.h>
15
16 #include "u_ether.h"
17 #include "u_serial.h"
18
19
20 #define DRIVER_DESC             "CDC Composite Gadget"
21 #define DRIVER_VERSION          "King Kamehameha Day 2008"
22
23 /*-------------------------------------------------------------------------*/
24
25 /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!!  Ever!!
26  * Instead:  allocate your own, using normal USB-IF procedures.
27  */
28
29 /* Thanks to NetChip Technologies for donating this product ID.
30  * It's for devices with only this composite CDC configuration.
31  */
32 #define CDC_VENDOR_NUM          0x0525  /* NetChip */
33 #define CDC_PRODUCT_NUM         0xa4aa  /* CDC Composite: ECM + ACM */
34
35 /*-------------------------------------------------------------------------*/
36
37 /*
38  * Kbuild is not very cooperative with respect to linking separately
39  * compiled library objects into one module.  So for now we won't use
40  * separate compilation ... ensuring init/exit sections work to shrink
41  * the runtime footprint, and giving us at least some parts of what
42  * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
43  */
44
45 #include "composite.c"
46 #include "usbstring.c"
47 #include "config.c"
48 #include "epautoconf.c"
49 #include "u_serial.c"
50 #include "f_acm.c"
51 #include "f_ecm.c"
52 #include "u_ether.c"
53
54 /*-------------------------------------------------------------------------*/
55
56 static struct usb_device_descriptor device_desc = {
57         .bLength =              sizeof device_desc,
58         .bDescriptorType =      USB_DT_DEVICE,
59
60         .bcdUSB =               cpu_to_le16(0x0200),
61
62         .bDeviceClass =         USB_CLASS_COMM,
63         .bDeviceSubClass =      0,
64         .bDeviceProtocol =      0,
65         /* .bMaxPacketSize0 = f(hardware) */
66
67         /* Vendor and product id can be overridden by module parameters.  */
68         .idVendor =             cpu_to_le16(CDC_VENDOR_NUM),
69         .idProduct =            cpu_to_le16(CDC_PRODUCT_NUM),
70         /* .bcdDevice = f(hardware) */
71         /* .iManufacturer = DYNAMIC */
72         /* .iProduct = DYNAMIC */
73         /* NO SERIAL NUMBER */
74         .bNumConfigurations =   1,
75 };
76
77 static struct usb_otg_descriptor otg_descriptor = {
78         .bLength =              sizeof otg_descriptor,
79         .bDescriptorType =      USB_DT_OTG,
80
81         /* REVISIT SRP-only hardware is possible, although
82          * it would not be called "OTG" ...
83          */
84         .bmAttributes =         USB_OTG_SRP | USB_OTG_HNP,
85 };
86
87 static const struct usb_descriptor_header *otg_desc[] = {
88         (struct usb_descriptor_header *) &otg_descriptor,
89         NULL,
90 };
91
92
93 /* string IDs are assigned dynamically */
94
95 #define STRING_MANUFACTURER_IDX         0
96 #define STRING_PRODUCT_IDX              1
97
98 static char manufacturer[50];
99
100 static struct usb_string strings_dev[] = {
101         [STRING_MANUFACTURER_IDX].s = manufacturer,
102         [STRING_PRODUCT_IDX].s = DRIVER_DESC,
103         {  } /* end of list */
104 };
105
106 static struct usb_gadget_strings stringtab_dev = {
107         .language       = 0x0409,       /* en-us */
108         .strings        = strings_dev,
109 };
110
111 static struct usb_gadget_strings *dev_strings[] = {
112         &stringtab_dev,
113         NULL,
114 };
115
116 static u8 hostaddr[ETH_ALEN];
117
118 /*-------------------------------------------------------------------------*/
119
120 /*
121  * We _always_ have both CDC ECM and CDC ACM functions.
122  */
123 static int __init cdc_do_config(struct usb_configuration *c)
124 {
125         int     status;
126
127         if (gadget_is_otg(c->cdev->gadget)) {
128                 c->descriptors = otg_desc;
129                 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
130         }
131
132         status = ecm_bind_config(c, hostaddr);
133         if (status < 0)
134                 return status;
135
136         status = acm_bind_config(c, 0);
137         if (status < 0)
138                 return status;
139
140         return 0;
141 }
142
143 static struct usb_configuration cdc_config_driver = {
144         .label                  = "CDC Composite (ECM + ACM)",
145         .bConfigurationValue    = 1,
146         /* .iConfiguration = DYNAMIC */
147         .bmAttributes           = USB_CONFIG_ATT_SELFPOWER,
148 };
149
150 /*-------------------------------------------------------------------------*/
151
152 static int __init cdc_bind(struct usb_composite_dev *cdev)
153 {
154         int                     gcnum;
155         struct usb_gadget       *gadget = cdev->gadget;
156         int                     status;
157
158         if (!can_support_ecm(cdev->gadget)) {
159                 dev_err(&gadget->dev, "controller '%s' not usable\n",
160                                 gadget->name);
161                 return -EINVAL;
162         }
163
164         /* set up network link layer */
165         status = gether_setup(cdev->gadget, hostaddr);
166         if (status < 0)
167                 return status;
168
169         /* set up serial link layer */
170         status = gserial_setup(cdev->gadget, 1);
171         if (status < 0)
172                 goto fail0;
173
174         gcnum = usb_gadget_controller_number(gadget);
175         if (gcnum >= 0)
176                 device_desc.bcdDevice = cpu_to_le16(0x0300 | gcnum);
177         else {
178                 /* We assume that can_support_ecm() tells the truth;
179                  * but if the controller isn't recognized at all then
180                  * that assumption is a bit more likely to be wrong.
181                  */
182                 WARNING(cdev, "controller '%s' not recognized; trying %s\n",
183                                 gadget->name,
184                                 cdc_config_driver.label);
185                 device_desc.bcdDevice =
186                         cpu_to_le16(0x0300 | 0x0099);
187         }
188
189
190         /* Allocate string descriptor numbers ... note that string
191          * contents can be overridden by the composite_dev glue.
192          */
193
194         /* device descriptor strings: manufacturer, product */
195         snprintf(manufacturer, sizeof manufacturer, "%s %s with %s",
196                 init_utsname()->sysname, init_utsname()->release,
197                 gadget->name);
198         status = usb_string_id(cdev);
199         if (status < 0)
200                 goto fail1;
201         strings_dev[STRING_MANUFACTURER_IDX].id = status;
202         device_desc.iManufacturer = status;
203
204         status = usb_string_id(cdev);
205         if (status < 0)
206                 goto fail1;
207         strings_dev[STRING_PRODUCT_IDX].id = status;
208         device_desc.iProduct = status;
209
210         /* register our configuration */
211         status = usb_add_config(cdev, &cdc_config_driver, cdc_do_config);
212         if (status < 0)
213                 goto fail1;
214
215         dev_info(&gadget->dev, "%s, version: " DRIVER_VERSION "\n",
216                         DRIVER_DESC);
217
218         return 0;
219
220 fail1:
221         gserial_cleanup();
222 fail0:
223         gether_cleanup();
224         return status;
225 }
226
227 static int __exit cdc_unbind(struct usb_composite_dev *cdev)
228 {
229         gserial_cleanup();
230         gether_cleanup();
231         return 0;
232 }
233
234 static struct usb_composite_driver cdc_driver = {
235         .name           = "g_cdc",
236         .dev            = &device_desc,
237         .strings        = dev_strings,
238         .max_speed      = USB_SPEED_HIGH,
239         .unbind         = __exit_p(cdc_unbind),
240 };
241
242 MODULE_DESCRIPTION(DRIVER_DESC);
243 MODULE_AUTHOR("David Brownell");
244 MODULE_LICENSE("GPL");
245
246 static int __init init(void)
247 {
248         return usb_composite_probe(&cdc_driver, cdc_bind);
249 }
250 module_init(init);
251
252 static void __exit cleanup(void)
253 {
254         usb_composite_unregister(&cdc_driver);
255 }
256 module_exit(cleanup);