usb: gadget: use usb_string_ids_tab instead multiple usb_string_id()
[deliverable/linux.git] / drivers / usb / gadget / zero.c
1 /*
2 * zero.c -- Gadget Zero, for USB development
3 *
4 * Copyright (C) 2003-2008 David Brownell
5 * Copyright (C) 2008 by 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
14 /*
15 * Gadget Zero only needs two bulk endpoints, and is an example of how you
16 * can write a hardware-agnostic gadget driver running inside a USB device.
17 * Some hardware details are visible, but don't affect most of the driver.
18 *
19 * Use it with the Linux host/master side "usbtest" driver to get a basic
20 * functional test of your device-side usb stack, or with "usb-skeleton".
21 *
22 * It supports two similar configurations. One sinks whatever the usb host
23 * writes, and in return sources zeroes. The other loops whatever the host
24 * writes back, so the host can read it.
25 *
26 * Many drivers will only have one configuration, letting them be much
27 * simpler if they also don't support high speed operation (like this
28 * driver does).
29 *
30 * Why is *this* driver using two configurations, rather than setting up
31 * two interfaces with different functions? To help verify that multiple
32 * configuration infrastucture is working correctly; also, so that it can
33 * work with low capability USB controllers without four bulk endpoints.
34 */
35
36 /*
37 * driver assumes self-powered hardware, and
38 * has no way for users to trigger remote wakeup.
39 */
40
41 /* #define VERBOSE_DEBUG */
42
43 #include <linux/kernel.h>
44 #include <linux/slab.h>
45 #include <linux/utsname.h>
46 #include <linux/device.h>
47
48 #include "g_zero.h"
49 #include "gadget_chips.h"
50
51
52 /*-------------------------------------------------------------------------*/
53
54 /*
55 * Kbuild is not very cooperative with respect to linking separately
56 * compiled library objects into one module. So for now we won't use
57 * separate compilation ... ensuring init/exit sections work to shrink
58 * the runtime footprint, and giving us at least some parts of what
59 * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
60 */
61 #include "composite.c"
62
63 #include "f_sourcesink.c"
64 #include "f_loopback.c"
65
66 /*-------------------------------------------------------------------------*/
67
68 #define DRIVER_VERSION "Cinco de Mayo 2008"
69
70 static const char longname[] = "Gadget Zero";
71
72 unsigned buflen = 4096; /* only used for bulk endpoints */
73 module_param(buflen, uint, 0);
74
75 /*
76 * Normally the "loopback" configuration is second (index 1) so
77 * it's not the default. Here's where to change that order, to
78 * work better with hosts where config changes are problematic or
79 * controllers (like original superh) that only support one config.
80 */
81 static bool loopdefault = 0;
82 module_param(loopdefault, bool, S_IRUGO|S_IWUSR);
83
84 /*-------------------------------------------------------------------------*/
85
86 /* Thanks to NetChip Technologies for donating this product ID.
87 *
88 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
89 * Instead: allocate your own, using normal USB-IF procedures.
90 */
91 #ifndef CONFIG_USB_ZERO_HNPTEST
92 #define DRIVER_VENDOR_NUM 0x0525 /* NetChip */
93 #define DRIVER_PRODUCT_NUM 0xa4a0 /* Linux-USB "Gadget Zero" */
94 #define DEFAULT_AUTORESUME 0
95 #else
96 #define DRIVER_VENDOR_NUM 0x1a0a /* OTG test device IDs */
97 #define DRIVER_PRODUCT_NUM 0xbadd
98 #define DEFAULT_AUTORESUME 5
99 #endif
100
101 /* If the optional "autoresume" mode is enabled, it provides good
102 * functional coverage for the "USBCV" test harness from USB-IF.
103 * It's always set if OTG mode is enabled.
104 */
105 unsigned autoresume = DEFAULT_AUTORESUME;
106 module_param(autoresume, uint, S_IRUGO);
107 MODULE_PARM_DESC(autoresume, "zero, or seconds before remote wakeup");
108
109 /*-------------------------------------------------------------------------*/
110
111 static struct usb_device_descriptor device_desc = {
112 .bLength = sizeof device_desc,
113 .bDescriptorType = USB_DT_DEVICE,
114
115 .bcdUSB = cpu_to_le16(0x0200),
116 .bDeviceClass = USB_CLASS_VENDOR_SPEC,
117
118 .idVendor = cpu_to_le16(DRIVER_VENDOR_NUM),
119 .idProduct = cpu_to_le16(DRIVER_PRODUCT_NUM),
120 .bNumConfigurations = 2,
121 };
122
123 #ifdef CONFIG_USB_OTG
124 static struct usb_otg_descriptor otg_descriptor = {
125 .bLength = sizeof otg_descriptor,
126 .bDescriptorType = USB_DT_OTG,
127
128 /* REVISIT SRP-only hardware is possible, although
129 * it would not be called "OTG" ...
130 */
131 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
132 };
133
134 const struct usb_descriptor_header *otg_desc[] = {
135 (struct usb_descriptor_header *) &otg_descriptor,
136 NULL,
137 };
138 #endif
139
140 /* string IDs are assigned dynamically */
141
142 #define STRING_MANUFACTURER_IDX 0
143 #define STRING_PRODUCT_IDX 1
144 #define STRING_SERIAL_IDX 2
145
146 static char manufacturer[50];
147
148 /* default serial number takes at least two packets */
149 static char serial[] = "0123456789.0123456789.0123456789";
150
151 static struct usb_string strings_dev[] = {
152 [STRING_MANUFACTURER_IDX].s = manufacturer,
153 [STRING_PRODUCT_IDX].s = longname,
154 [STRING_SERIAL_IDX].s = serial,
155 { } /* end of list */
156 };
157
158 static struct usb_gadget_strings stringtab_dev = {
159 .language = 0x0409, /* en-us */
160 .strings = strings_dev,
161 };
162
163 static struct usb_gadget_strings *dev_strings[] = {
164 &stringtab_dev,
165 NULL,
166 };
167
168 /*-------------------------------------------------------------------------*/
169
170 struct usb_request *alloc_ep_req(struct usb_ep *ep, int len)
171 {
172 struct usb_request *req;
173
174 req = usb_ep_alloc_request(ep, GFP_ATOMIC);
175 if (req) {
176 if (len)
177 req->length = len;
178 else
179 req->length = buflen;
180 req->buf = kmalloc(req->length, GFP_ATOMIC);
181 if (!req->buf) {
182 usb_ep_free_request(ep, req);
183 req = NULL;
184 }
185 }
186 return req;
187 }
188
189 void free_ep_req(struct usb_ep *ep, struct usb_request *req)
190 {
191 kfree(req->buf);
192 usb_ep_free_request(ep, req);
193 }
194
195 static void disable_ep(struct usb_composite_dev *cdev, struct usb_ep *ep)
196 {
197 int value;
198
199 if (ep->driver_data) {
200 value = usb_ep_disable(ep);
201 if (value < 0)
202 DBG(cdev, "disable %s --> %d\n",
203 ep->name, value);
204 ep->driver_data = NULL;
205 }
206 }
207
208 void disable_endpoints(struct usb_composite_dev *cdev,
209 struct usb_ep *in, struct usb_ep *out,
210 struct usb_ep *iso_in, struct usb_ep *iso_out)
211 {
212 disable_ep(cdev, in);
213 disable_ep(cdev, out);
214 if (iso_in)
215 disable_ep(cdev, iso_in);
216 if (iso_out)
217 disable_ep(cdev, iso_out);
218 }
219
220 /*-------------------------------------------------------------------------*/
221
222 static struct timer_list autoresume_timer;
223
224 static void zero_autoresume(unsigned long _c)
225 {
226 struct usb_composite_dev *cdev = (void *)_c;
227 struct usb_gadget *g = cdev->gadget;
228
229 /* unconfigured devices can't issue wakeups */
230 if (!cdev->config)
231 return;
232
233 /* Normally the host would be woken up for something
234 * more significant than just a timer firing; likely
235 * because of some direct user request.
236 */
237 if (g->speed != USB_SPEED_UNKNOWN) {
238 int status = usb_gadget_wakeup(g);
239 INFO(cdev, "%s --> %d\n", __func__, status);
240 }
241 }
242
243 static void zero_suspend(struct usb_composite_dev *cdev)
244 {
245 if (cdev->gadget->speed == USB_SPEED_UNKNOWN)
246 return;
247
248 if (autoresume) {
249 mod_timer(&autoresume_timer, jiffies + (HZ * autoresume));
250 DBG(cdev, "suspend, wakeup in %d seconds\n", autoresume);
251 } else
252 DBG(cdev, "%s\n", __func__);
253 }
254
255 static void zero_resume(struct usb_composite_dev *cdev)
256 {
257 DBG(cdev, "%s\n", __func__);
258 del_timer(&autoresume_timer);
259 }
260
261 /*-------------------------------------------------------------------------*/
262
263 static int __init zero_bind(struct usb_composite_dev *cdev)
264 {
265 int gcnum;
266 struct usb_gadget *gadget = cdev->gadget;
267 int status;
268
269 /* Allocate string descriptor numbers ... note that string
270 * contents can be overridden by the composite_dev glue.
271 */
272 status = usb_string_ids_tab(cdev, strings_dev);
273 if (status < 0)
274 return status;
275
276 device_desc.iManufacturer = strings_dev[STRING_MANUFACTURER_IDX].id;
277 device_desc.iProduct = strings_dev[STRING_PRODUCT_IDX].id;
278 device_desc.iSerialNumber = strings_dev[STRING_SERIAL_IDX].id;
279
280 setup_timer(&autoresume_timer, zero_autoresume, (unsigned long) cdev);
281
282 /* Register primary, then secondary configuration. Note that
283 * SH3 only allows one config...
284 */
285 if (loopdefault) {
286 loopback_add(cdev, autoresume != 0);
287 sourcesink_add(cdev, autoresume != 0);
288 } else {
289 sourcesink_add(cdev, autoresume != 0);
290 loopback_add(cdev, autoresume != 0);
291 }
292
293 gcnum = usb_gadget_controller_number(gadget);
294 if (gcnum >= 0)
295 device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
296 else {
297 /* gadget zero is so simple (for now, no altsettings) that
298 * it SHOULD NOT have problems with bulk-capable hardware.
299 * so just warn about unrcognized controllers -- don't panic.
300 *
301 * things like configuration and altsetting numbering
302 * can need hardware-specific attention though.
303 */
304 pr_warning("%s: controller '%s' not recognized\n",
305 longname, gadget->name);
306 device_desc.bcdDevice = cpu_to_le16(0x9999);
307 }
308
309 INFO(cdev, "%s, version: " DRIVER_VERSION "\n", longname);
310
311 snprintf(manufacturer, sizeof manufacturer, "%s %s with %s",
312 init_utsname()->sysname, init_utsname()->release,
313 gadget->name);
314
315 return 0;
316 }
317
318 static int zero_unbind(struct usb_composite_dev *cdev)
319 {
320 del_timer_sync(&autoresume_timer);
321 return 0;
322 }
323
324 static __refdata struct usb_composite_driver zero_driver = {
325 .name = "zero",
326 .dev = &device_desc,
327 .strings = dev_strings,
328 .max_speed = USB_SPEED_SUPER,
329 .bind = zero_bind,
330 .unbind = zero_unbind,
331 .suspend = zero_suspend,
332 .resume = zero_resume,
333 };
334
335 MODULE_AUTHOR("David Brownell");
336 MODULE_LICENSE("GPL");
337
338 static int __init init(void)
339 {
340 return usb_composite_probe(&zero_driver);
341 }
342 module_init(init);
343
344 static void __exit cleanup(void)
345 {
346 usb_composite_unregister(&zero_driver);
347 }
348 module_exit(cleanup);
This page took 0.037436 seconds and 5 git commands to generate.