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