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