usb: gadget: use usb_string_ids_tab instead multiple usb_string_id()
[deliverable/linux.git] / drivers / usb / gadget / serial.c
1 /*
2 * serial.c -- USB gadget serial driver
3 *
4 * Copyright (C) 2003 Al Borchers (alborchers@steinerpoint.com)
5 * Copyright (C) 2008 by David Brownell
6 * Copyright (C) 2008 by Nokia Corporation
7 *
8 * This software is distributed under the terms of the GNU General
9 * Public License ("GPL") as published by the Free Software Foundation,
10 * either version 2 of that License or (at your option) any later version.
11 */
12
13 #include <linux/kernel.h>
14 #include <linux/utsname.h>
15 #include <linux/device.h>
16 #include <linux/tty.h>
17 #include <linux/tty_flip.h>
18
19 #include "u_serial.h"
20 #include "gadget_chips.h"
21
22
23 /* Defines */
24
25 #define GS_VERSION_STR "v2.4"
26 #define GS_VERSION_NUM 0x2400
27
28 #define GS_LONG_NAME "Gadget Serial"
29 #define GS_VERSION_NAME GS_LONG_NAME " " GS_VERSION_STR
30
31 /*-------------------------------------------------------------------------*/
32
33 /*
34 * Kbuild is not very cooperative with respect to linking separately
35 * compiled library objects into one module. So for now we won't use
36 * separate compilation ... ensuring init/exit sections work to shrink
37 * the runtime footprint, and giving us at least some parts of what
38 * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
39 */
40 #include "composite.c"
41
42 #include "f_acm.c"
43 #include "f_obex.c"
44 #include "f_serial.c"
45 #include "u_serial.c"
46
47 /*-------------------------------------------------------------------------*/
48
49 /* Thanks to NetChip Technologies for donating this product ID.
50 *
51 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
52 * Instead: allocate your own, using normal USB-IF procedures.
53 */
54 #define GS_VENDOR_ID 0x0525 /* NetChip */
55 #define GS_PRODUCT_ID 0xa4a6 /* Linux-USB Serial Gadget */
56 #define GS_CDC_PRODUCT_ID 0xa4a7 /* ... as CDC-ACM */
57 #define GS_CDC_OBEX_PRODUCT_ID 0xa4a9 /* ... as CDC-OBEX */
58
59 /* string IDs are assigned dynamically */
60
61 #define STRING_MANUFACTURER_IDX 0
62 #define STRING_PRODUCT_IDX 1
63 #define STRING_DESCRIPTION_IDX 2
64
65 static char manufacturer[50];
66
67 static struct usb_string strings_dev[] = {
68 [STRING_MANUFACTURER_IDX].s = manufacturer,
69 [STRING_PRODUCT_IDX].s = GS_VERSION_NAME,
70 [STRING_DESCRIPTION_IDX].s = NULL /* updated; f(use_acm) */,
71 { } /* end of list */
72 };
73
74 static struct usb_gadget_strings stringtab_dev = {
75 .language = 0x0409, /* en-us */
76 .strings = strings_dev,
77 };
78
79 static struct usb_gadget_strings *dev_strings[] = {
80 &stringtab_dev,
81 NULL,
82 };
83
84 static struct usb_device_descriptor device_desc = {
85 .bLength = USB_DT_DEVICE_SIZE,
86 .bDescriptorType = USB_DT_DEVICE,
87 .bcdUSB = cpu_to_le16(0x0200),
88 /* .bDeviceClass = f(use_acm) */
89 .bDeviceSubClass = 0,
90 .bDeviceProtocol = 0,
91 /* .bMaxPacketSize0 = f(hardware) */
92 .idVendor = cpu_to_le16(GS_VENDOR_ID),
93 /* .idProduct = f(use_acm) */
94 /* .bcdDevice = f(hardware) */
95 /* .iManufacturer = DYNAMIC */
96 /* .iProduct = DYNAMIC */
97 .bNumConfigurations = 1,
98 };
99
100 static struct usb_otg_descriptor otg_descriptor = {
101 .bLength = sizeof otg_descriptor,
102 .bDescriptorType = USB_DT_OTG,
103
104 /* REVISIT SRP-only hardware is possible, although
105 * it would not be called "OTG" ...
106 */
107 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
108 };
109
110 static const struct usb_descriptor_header *otg_desc[] = {
111 (struct usb_descriptor_header *) &otg_descriptor,
112 NULL,
113 };
114
115 /*-------------------------------------------------------------------------*/
116
117 /* Module */
118 MODULE_DESCRIPTION(GS_VERSION_NAME);
119 MODULE_AUTHOR("Al Borchers");
120 MODULE_AUTHOR("David Brownell");
121 MODULE_LICENSE("GPL");
122
123 static bool use_acm = true;
124 module_param(use_acm, bool, 0);
125 MODULE_PARM_DESC(use_acm, "Use CDC ACM, default=yes");
126
127 static bool use_obex = false;
128 module_param(use_obex, bool, 0);
129 MODULE_PARM_DESC(use_obex, "Use CDC OBEX, default=no");
130
131 static unsigned n_ports = 1;
132 module_param(n_ports, uint, 0);
133 MODULE_PARM_DESC(n_ports, "number of ports to create, default=1");
134
135 /*-------------------------------------------------------------------------*/
136
137 static int __init serial_bind_config(struct usb_configuration *c)
138 {
139 unsigned i;
140 int status = 0;
141
142 for (i = 0; i < n_ports && status == 0; i++) {
143 if (use_acm)
144 status = acm_bind_config(c, i);
145 else if (use_obex)
146 status = obex_bind_config(c, i);
147 else
148 status = gser_bind_config(c, i);
149 }
150 return status;
151 }
152
153 static struct usb_configuration serial_config_driver = {
154 /* .label = f(use_acm) */
155 /* .bConfigurationValue = f(use_acm) */
156 /* .iConfiguration = DYNAMIC */
157 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
158 };
159
160 static int __init gs_bind(struct usb_composite_dev *cdev)
161 {
162 int gcnum;
163 struct usb_gadget *gadget = cdev->gadget;
164 int status;
165
166 status = gserial_setup(cdev->gadget, n_ports);
167 if (status < 0)
168 return status;
169
170 /* Allocate string descriptor numbers ... note that string
171 * contents can be overridden by the composite_dev glue.
172 */
173
174 /* device description: manufacturer, product */
175 snprintf(manufacturer, sizeof manufacturer, "%s %s with %s",
176 init_utsname()->sysname, init_utsname()->release,
177 gadget->name);
178 status = usb_string_ids_tab(cdev, strings_dev);
179 if (status < 0)
180 goto fail;
181 device_desc.iManufacturer = strings_dev[STRING_MANUFACTURER_IDX].id;
182 device_desc.iProduct = strings_dev[STRING_PRODUCT_IDX].id;
183 status = strings_dev[STRING_DESCRIPTION_IDX].id;
184 serial_config_driver.iConfiguration = status;
185
186 /* set up other descriptors */
187 gcnum = usb_gadget_controller_number(gadget);
188 if (gcnum >= 0)
189 device_desc.bcdDevice = cpu_to_le16(GS_VERSION_NUM | gcnum);
190 else {
191 /* this is so simple (for now, no altsettings) that it
192 * SHOULD NOT have problems with bulk-capable hardware.
193 * so warn about unrcognized controllers -- don't panic.
194 *
195 * things like configuration and altsetting numbering
196 * can need hardware-specific attention though.
197 */
198 pr_warning("gs_bind: controller '%s' not recognized\n",
199 gadget->name);
200 device_desc.bcdDevice =
201 cpu_to_le16(GS_VERSION_NUM | 0x0099);
202 }
203
204 if (gadget_is_otg(cdev->gadget)) {
205 serial_config_driver.descriptors = otg_desc;
206 serial_config_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
207 }
208
209 /* register our configuration */
210 status = usb_add_config(cdev, &serial_config_driver,
211 serial_bind_config);
212 if (status < 0)
213 goto fail;
214
215 INFO(cdev, "%s\n", GS_VERSION_NAME);
216
217 return 0;
218
219 fail:
220 gserial_cleanup();
221 return status;
222 }
223
224 static __refdata struct usb_composite_driver gserial_driver = {
225 .name = "g_serial",
226 .dev = &device_desc,
227 .strings = dev_strings,
228 .max_speed = USB_SPEED_SUPER,
229 .bind = gs_bind,
230 };
231
232 static int __init init(void)
233 {
234 /* We *could* export two configs; that'd be much cleaner...
235 * but neither of these product IDs was defined that way.
236 */
237 if (use_acm) {
238 serial_config_driver.label = "CDC ACM config";
239 serial_config_driver.bConfigurationValue = 2;
240 device_desc.bDeviceClass = USB_CLASS_COMM;
241 device_desc.idProduct =
242 cpu_to_le16(GS_CDC_PRODUCT_ID);
243 } else if (use_obex) {
244 serial_config_driver.label = "CDC OBEX config";
245 serial_config_driver.bConfigurationValue = 3;
246 device_desc.bDeviceClass = USB_CLASS_COMM;
247 device_desc.idProduct =
248 cpu_to_le16(GS_CDC_OBEX_PRODUCT_ID);
249 } else {
250 serial_config_driver.label = "Generic Serial config";
251 serial_config_driver.bConfigurationValue = 1;
252 device_desc.bDeviceClass = USB_CLASS_VENDOR_SPEC;
253 device_desc.idProduct =
254 cpu_to_le16(GS_PRODUCT_ID);
255 }
256 strings_dev[STRING_DESCRIPTION_IDX].s = serial_config_driver.label;
257
258 return usb_composite_probe(&gserial_driver);
259 }
260 module_init(init);
261
262 static void __exit cleanup(void)
263 {
264 usb_composite_unregister(&gserial_driver);
265 gserial_cleanup();
266 }
267 module_exit(cleanup);
This page took 0.035991 seconds and 5 git commands to generate.