Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * zero.c -- Gadget Zero, for USB development | |
3 | * | |
097db1d0 DB |
4 | * Copyright (C) 2003-2008 David Brownell |
5 | * Copyright (C) 2008 by Nokia Corporation | |
1da177e4 | 6 | * |
ca2bdf4b DB |
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. | |
1da177e4 LT |
11 | */ |
12 | ||
1da177e4 LT |
13 | /* |
14 | * Gadget Zero only needs two bulk endpoints, and is an example of how you | |
15 | * can write a hardware-agnostic gadget driver running inside a USB device. | |
7472f38b | 16 | * Some hardware details are visible, but don't affect most of the driver. |
1da177e4 LT |
17 | * |
18 | * Use it with the Linux host/master side "usbtest" driver to get a basic | |
19 | * functional test of your device-side usb stack, or with "usb-skeleton". | |
20 | * | |
21 | * It supports two similar configurations. One sinks whatever the usb host | |
22 | * writes, and in return sources zeroes. The other loops whatever the host | |
097db1d0 | 23 | * writes back, so the host can read it. |
1da177e4 LT |
24 | * |
25 | * Many drivers will only have one configuration, letting them be much | |
26 | * simpler if they also don't support high speed operation (like this | |
27 | * driver does). | |
ca2bdf4b DB |
28 | * |
29 | * Why is *this* driver using two configurations, rather than setting up | |
30 | * two interfaces with different functions? To help verify that multiple | |
31 | * configuration infrastucture is working correctly; also, so that it can | |
32 | * work with low capability USB controllers without four bulk endpoints. | |
1da177e4 LT |
33 | */ |
34 | ||
097db1d0 DB |
35 | /* |
36 | * driver assumes self-powered hardware, and | |
37 | * has no way for users to trigger remote wakeup. | |
38 | */ | |
39 | ||
ca2bdf4b | 40 | /* #define VERBOSE_DEBUG */ |
1da177e4 | 41 | |
1da177e4 | 42 | #include <linux/kernel.h> |
5a0e3ad6 | 43 | #include <linux/slab.h> |
1da177e4 | 44 | #include <linux/device.h> |
cf9a08ae SAS |
45 | #include <linux/module.h> |
46 | #include <linux/err.h> | |
47 | #include <linux/usb/composite.h> | |
1da177e4 | 48 | |
097db1d0 | 49 | #include "g_zero.h" |
1da177e4 | 50 | /*-------------------------------------------------------------------------*/ |
7d16e8d3 | 51 | USB_GADGET_COMPOSITE_OPTIONS(); |
1da177e4 | 52 | |
097db1d0 | 53 | #define DRIVER_VERSION "Cinco de Mayo 2008" |
1da177e4 | 54 | |
7472f38b | 55 | static const char longname[] = "Gadget Zero"; |
1da177e4 | 56 | |
1da177e4 LT |
57 | /* |
58 | * Normally the "loopback" configuration is second (index 1) so | |
59 | * it's not the default. Here's where to change that order, to | |
097db1d0 DB |
60 | * work better with hosts where config changes are problematic or |
61 | * controllers (like original superh) that only support one config. | |
1da177e4 | 62 | */ |
90ab5ee9 | 63 | static bool loopdefault = 0; |
7472f38b | 64 | module_param(loopdefault, bool, S_IRUGO|S_IWUSR); |
1da177e4 | 65 | |
38b3ad56 | 66 | static struct usb_zero_options gzero_options = { |
cf9a08ae SAS |
67 | .isoc_interval = 4, |
68 | .isoc_maxpacket = 1024, | |
69 | .bulk_buflen = 4096, | |
70 | .qlen = 32, | |
71 | }; | |
72 | ||
1da177e4 LT |
73 | /*-------------------------------------------------------------------------*/ |
74 | ||
75 | /* Thanks to NetChip Technologies for donating this product ID. | |
76 | * | |
77 | * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!! | |
78 | * Instead: allocate your own, using normal USB-IF procedures. | |
79 | */ | |
80 | #ifndef CONFIG_USB_ZERO_HNPTEST | |
81 | #define DRIVER_VENDOR_NUM 0x0525 /* NetChip */ | |
82 | #define DRIVER_PRODUCT_NUM 0xa4a0 /* Linux-USB "Gadget Zero" */ | |
ab943a2e | 83 | #define DEFAULT_AUTORESUME 0 |
1da177e4 LT |
84 | #else |
85 | #define DRIVER_VENDOR_NUM 0x1a0a /* OTG test device IDs */ | |
86 | #define DRIVER_PRODUCT_NUM 0xbadd | |
ab943a2e | 87 | #define DEFAULT_AUTORESUME 5 |
1da177e4 LT |
88 | #endif |
89 | ||
ab943a2e DB |
90 | /* If the optional "autoresume" mode is enabled, it provides good |
91 | * functional coverage for the "USBCV" test harness from USB-IF. | |
92 | * It's always set if OTG mode is enabled. | |
93 | */ | |
94 | unsigned autoresume = DEFAULT_AUTORESUME; | |
95 | module_param(autoresume, uint, S_IRUGO); | |
96 | MODULE_PARM_DESC(autoresume, "zero, or seconds before remote wakeup"); | |
97 | ||
1da177e4 LT |
98 | /*-------------------------------------------------------------------------*/ |
99 | ||
7472f38b | 100 | static struct usb_device_descriptor device_desc = { |
1da177e4 LT |
101 | .bLength = sizeof device_desc, |
102 | .bDescriptorType = USB_DT_DEVICE, | |
103 | ||
551509d2 | 104 | .bcdUSB = cpu_to_le16(0x0200), |
1da177e4 LT |
105 | .bDeviceClass = USB_CLASS_VENDOR_SPEC, |
106 | ||
551509d2 HH |
107 | .idVendor = cpu_to_le16(DRIVER_VENDOR_NUM), |
108 | .idProduct = cpu_to_le16(DRIVER_PRODUCT_NUM), | |
1da177e4 LT |
109 | .bNumConfigurations = 2, |
110 | }; | |
111 | ||
097db1d0 | 112 | #ifdef CONFIG_USB_OTG |
7472f38b | 113 | static struct usb_otg_descriptor otg_descriptor = { |
1da177e4 LT |
114 | .bLength = sizeof otg_descriptor, |
115 | .bDescriptorType = USB_DT_OTG, | |
116 | ||
097db1d0 DB |
117 | /* REVISIT SRP-only hardware is possible, although |
118 | * it would not be called "OTG" ... | |
119 | */ | |
120 | .bmAttributes = USB_OTG_SRP | USB_OTG_HNP, | |
1da177e4 LT |
121 | }; |
122 | ||
cf9a08ae | 123 | static const struct usb_descriptor_header *otg_desc[] = { |
1da177e4 | 124 | (struct usb_descriptor_header *) &otg_descriptor, |
1da177e4 LT |
125 | NULL, |
126 | }; | |
cf9a08ae SAS |
127 | #else |
128 | #define otg_desc NULL | |
097db1d0 | 129 | #endif |
1da177e4 | 130 | |
097db1d0 | 131 | /* string IDs are assigned dynamically */ |
ca2bdf4b DB |
132 | /* default serial number takes at least two packets */ |
133 | static char serial[] = "0123456789.0123456789.0123456789"; | |
1da177e4 | 134 | |
eeae5407 | 135 | #define USB_GZERO_SS_DESC (USB_GADGET_FIRST_AVAIL_IDX + 0) |
78f46f09 | 136 | #define USB_GZERO_LB_DESC (USB_GADGET_FIRST_AVAIL_IDX + 1) |
eeae5407 | 137 | |
097db1d0 | 138 | static struct usb_string strings_dev[] = { |
cc2683c3 | 139 | [USB_GADGET_MANUFACTURER_IDX].s = "", |
276e2e4f SAS |
140 | [USB_GADGET_PRODUCT_IDX].s = longname, |
141 | [USB_GADGET_SERIAL_IDX].s = serial, | |
eeae5407 | 142 | [USB_GZERO_SS_DESC].s = "source and sink data", |
78f46f09 | 143 | [USB_GZERO_LB_DESC].s = "loop input to output", |
1da177e4 LT |
144 | { } /* end of list */ |
145 | }; | |
146 | ||
097db1d0 | 147 | static struct usb_gadget_strings stringtab_dev = { |
1da177e4 | 148 | .language = 0x0409, /* en-us */ |
097db1d0 | 149 | .strings = strings_dev, |
1da177e4 LT |
150 | }; |
151 | ||
097db1d0 DB |
152 | static struct usb_gadget_strings *dev_strings[] = { |
153 | &stringtab_dev, | |
154 | NULL, | |
155 | }; | |
1da177e4 LT |
156 | |
157 | /*-------------------------------------------------------------------------*/ | |
158 | ||
ab943a2e DB |
159 | static struct timer_list autoresume_timer; |
160 | ||
161 | static void zero_autoresume(unsigned long _c) | |
162 | { | |
163 | struct usb_composite_dev *cdev = (void *)_c; | |
164 | struct usb_gadget *g = cdev->gadget; | |
165 | ||
166 | /* unconfigured devices can't issue wakeups */ | |
167 | if (!cdev->config) | |
168 | return; | |
169 | ||
170 | /* Normally the host would be woken up for something | |
171 | * more significant than just a timer firing; likely | |
172 | * because of some direct user request. | |
173 | */ | |
174 | if (g->speed != USB_SPEED_UNKNOWN) { | |
175 | int status = usb_gadget_wakeup(g); | |
176 | INFO(cdev, "%s --> %d\n", __func__, status); | |
177 | } | |
178 | } | |
179 | ||
180 | static void zero_suspend(struct usb_composite_dev *cdev) | |
181 | { | |
182 | if (cdev->gadget->speed == USB_SPEED_UNKNOWN) | |
183 | return; | |
184 | ||
185 | if (autoresume) { | |
186 | mod_timer(&autoresume_timer, jiffies + (HZ * autoresume)); | |
187 | DBG(cdev, "suspend, wakeup in %d seconds\n", autoresume); | |
188 | } else | |
189 | DBG(cdev, "%s\n", __func__); | |
190 | } | |
191 | ||
192 | static void zero_resume(struct usb_composite_dev *cdev) | |
193 | { | |
194 | DBG(cdev, "%s\n", __func__); | |
195 | del_timer(&autoresume_timer); | |
196 | } | |
197 | ||
198 | /*-------------------------------------------------------------------------*/ | |
199 | ||
78f46f09 SAS |
200 | static struct usb_configuration loopback_driver = { |
201 | .label = "loopback", | |
78f46f09 SAS |
202 | .bConfigurationValue = 2, |
203 | .bmAttributes = USB_CONFIG_ATT_SELFPOWER, | |
204 | /* .iConfiguration = DYNAMIC */ | |
205 | }; | |
206 | ||
cf9a08ae SAS |
207 | static struct usb_function *func_ss; |
208 | static struct usb_function_instance *func_inst_ss; | |
209 | ||
210 | static int ss_config_setup(struct usb_configuration *c, | |
211 | const struct usb_ctrlrequest *ctrl) | |
212 | { | |
213 | switch (ctrl->bRequest) { | |
214 | case 0x5b: | |
215 | case 0x5c: | |
216 | return func_ss->setup(func_ss, ctrl); | |
217 | default: | |
218 | return -EOPNOTSUPP; | |
219 | } | |
220 | } | |
221 | ||
eeae5407 SAS |
222 | static struct usb_configuration sourcesink_driver = { |
223 | .label = "source/sink", | |
eeae5407 SAS |
224 | .setup = ss_config_setup, |
225 | .bConfigurationValue = 3, | |
226 | .bmAttributes = USB_CONFIG_ATT_SELFPOWER, | |
227 | /* .iConfiguration = DYNAMIC */ | |
228 | }; | |
229 | ||
cf9a08ae SAS |
230 | module_param_named(buflen, gzero_options.bulk_buflen, uint, 0); |
231 | module_param_named(pattern, gzero_options.pattern, uint, S_IRUGO|S_IWUSR); | |
232 | MODULE_PARM_DESC(pattern, "0 = all zeroes, 1 = mod63, 2 = none"); | |
233 | ||
234 | module_param_named(isoc_interval, gzero_options.isoc_interval, uint, | |
235 | S_IRUGO|S_IWUSR); | |
236 | MODULE_PARM_DESC(isoc_interval, "1 - 16"); | |
237 | ||
238 | module_param_named(isoc_maxpacket, gzero_options.isoc_maxpacket, uint, | |
239 | S_IRUGO|S_IWUSR); | |
240 | MODULE_PARM_DESC(isoc_maxpacket, "0 - 1023 (fs), 0 - 1024 (hs/ss)"); | |
241 | ||
242 | module_param_named(isoc_mult, gzero_options.isoc_mult, uint, S_IRUGO|S_IWUSR); | |
243 | MODULE_PARM_DESC(isoc_mult, "0 - 2 (hs/ss only)"); | |
244 | ||
245 | module_param_named(isoc_maxburst, gzero_options.isoc_maxburst, uint, | |
246 | S_IRUGO|S_IWUSR); | |
247 | MODULE_PARM_DESC(isoc_maxburst, "0 - 15 (ss only)"); | |
248 | ||
249 | static struct usb_function *func_lb; | |
250 | static struct usb_function_instance *func_inst_lb; | |
251 | ||
252 | module_param_named(qlen, gzero_options.qlen, uint, S_IRUGO|S_IWUSR); | |
253 | MODULE_PARM_DESC(qlen, "depth of loopback queue"); | |
254 | ||
e12995ec | 255 | static int __init zero_bind(struct usb_composite_dev *cdev) |
1da177e4 | 256 | { |
cf9a08ae SAS |
257 | struct f_ss_opts *ss_opts; |
258 | struct f_lb_opts *lb_opts; | |
e1f15ccb | 259 | int status; |
91e79c91 | 260 | |
097db1d0 DB |
261 | /* Allocate string descriptor numbers ... note that string |
262 | * contents can be overridden by the composite_dev glue. | |
91e79c91 | 263 | */ |
e1f15ccb SAS |
264 | status = usb_string_ids_tab(cdev, strings_dev); |
265 | if (status < 0) | |
266 | return status; | |
267 | ||
276e2e4f SAS |
268 | device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id; |
269 | device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id; | |
270 | device_desc.iSerialNumber = strings_dev[USB_GADGET_SERIAL_IDX].id; | |
097db1d0 | 271 | |
ab943a2e DB |
272 | setup_timer(&autoresume_timer, zero_autoresume, (unsigned long) cdev); |
273 | ||
cf9a08ae SAS |
274 | func_inst_ss = usb_get_function_instance("SourceSink"); |
275 | if (IS_ERR(func_inst_ss)) | |
276 | return PTR_ERR(func_inst_ss); | |
277 | ||
278 | ss_opts = container_of(func_inst_ss, struct f_ss_opts, func_inst); | |
279 | ss_opts->pattern = gzero_options.pattern; | |
280 | ss_opts->isoc_interval = gzero_options.isoc_interval; | |
281 | ss_opts->isoc_maxpacket = gzero_options.isoc_maxpacket; | |
282 | ss_opts->isoc_mult = gzero_options.isoc_mult; | |
283 | ss_opts->isoc_maxburst = gzero_options.isoc_maxpacket; | |
284 | ss_opts->bulk_buflen = gzero_options.bulk_buflen; | |
285 | ||
286 | func_ss = usb_get_function(func_inst_ss); | |
c8c18883 WY |
287 | if (IS_ERR(func_ss)) { |
288 | status = PTR_ERR(func_ss); | |
cf9a08ae | 289 | goto err_put_func_inst_ss; |
c8c18883 | 290 | } |
cf9a08ae SAS |
291 | |
292 | func_inst_lb = usb_get_function_instance("Loopback"); | |
c8c18883 WY |
293 | if (IS_ERR(func_inst_lb)) { |
294 | status = PTR_ERR(func_inst_lb); | |
cf9a08ae | 295 | goto err_put_func_ss; |
c8c18883 | 296 | } |
cf9a08ae SAS |
297 | |
298 | lb_opts = container_of(func_inst_lb, struct f_lb_opts, func_inst); | |
299 | lb_opts->bulk_buflen = gzero_options.bulk_buflen; | |
300 | lb_opts->qlen = gzero_options.qlen; | |
301 | ||
302 | func_lb = usb_get_function(func_inst_lb); | |
303 | if (IS_ERR(func_lb)) { | |
304 | status = PTR_ERR(func_lb); | |
305 | goto err_put_func_inst_lb; | |
306 | } | |
307 | ||
eeae5407 | 308 | sourcesink_driver.iConfiguration = strings_dev[USB_GZERO_SS_DESC].id; |
78f46f09 SAS |
309 | loopback_driver.iConfiguration = strings_dev[USB_GZERO_LB_DESC].id; |
310 | ||
eeae5407 SAS |
311 | /* support autoresume for remote wakeup testing */ |
312 | sourcesink_driver.bmAttributes &= ~USB_CONFIG_ATT_WAKEUP; | |
78f46f09 | 313 | loopback_driver.bmAttributes &= ~USB_CONFIG_ATT_WAKEUP; |
eeae5407 | 314 | sourcesink_driver.descriptors = NULL; |
78f46f09 SAS |
315 | loopback_driver.descriptors = NULL; |
316 | if (autoresume) { | |
eeae5407 | 317 | sourcesink_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP; |
78f46f09 SAS |
318 | loopback_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP; |
319 | } | |
eeae5407 SAS |
320 | |
321 | /* support OTG systems */ | |
322 | if (gadget_is_otg(cdev->gadget)) { | |
323 | sourcesink_driver.descriptors = otg_desc; | |
324 | sourcesink_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP; | |
78f46f09 SAS |
325 | loopback_driver.descriptors = otg_desc; |
326 | loopback_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP; | |
eeae5407 SAS |
327 | } |
328 | ||
097db1d0 DB |
329 | /* Register primary, then secondary configuration. Note that |
330 | * SH3 only allows one config... | |
1da177e4 | 331 | */ |
097db1d0 | 332 | if (loopdefault) { |
cf9a08ae SAS |
333 | usb_add_config_only(cdev, &loopback_driver); |
334 | usb_add_config_only(cdev, &sourcesink_driver); | |
097db1d0 | 335 | } else { |
cf9a08ae SAS |
336 | usb_add_config_only(cdev, &sourcesink_driver); |
337 | usb_add_config_only(cdev, &loopback_driver); | |
1da177e4 | 338 | } |
cf9a08ae SAS |
339 | status = usb_add_function(&sourcesink_driver, func_ss); |
340 | if (status) | |
341 | goto err_conf_flb; | |
1da177e4 | 342 | |
cf9a08ae SAS |
343 | usb_ep_autoconfig_reset(cdev->gadget); |
344 | status = usb_add_function(&loopback_driver, func_lb); | |
345 | if (status) | |
346 | goto err_conf_flb; | |
347 | ||
348 | usb_ep_autoconfig_reset(cdev->gadget); | |
7d16e8d3 | 349 | usb_composite_overwrite_options(cdev, &coverwrite); |
1da177e4 | 350 | |
097db1d0 | 351 | INFO(cdev, "%s, version: " DRIVER_VERSION "\n", longname); |
1da177e4 | 352 | |
1da177e4 | 353 | return 0; |
cf9a08ae SAS |
354 | |
355 | err_conf_flb: | |
356 | usb_put_function(func_lb); | |
357 | func_lb = NULL; | |
358 | err_put_func_inst_lb: | |
359 | usb_put_function_instance(func_inst_lb); | |
360 | func_inst_lb = NULL; | |
361 | err_put_func_ss: | |
362 | usb_put_function(func_ss); | |
363 | func_ss = NULL; | |
364 | err_put_func_inst_ss: | |
365 | usb_put_function_instance(func_inst_ss); | |
366 | func_inst_ss = NULL; | |
367 | return status; | |
1da177e4 LT |
368 | } |
369 | ||
ab943a2e DB |
370 | static int zero_unbind(struct usb_composite_dev *cdev) |
371 | { | |
372 | del_timer_sync(&autoresume_timer); | |
cf9a08ae SAS |
373 | if (!IS_ERR_OR_NULL(func_ss)) |
374 | usb_put_function(func_ss); | |
abe7a409 | 375 | usb_put_function_instance(func_inst_ss); |
cf9a08ae SAS |
376 | if (!IS_ERR_OR_NULL(func_lb)) |
377 | usb_put_function(func_lb); | |
abe7a409 | 378 | usb_put_function_instance(func_inst_lb); |
ab943a2e DB |
379 | return 0; |
380 | } | |
381 | ||
c2ec75c2 | 382 | static __refdata struct usb_composite_driver zero_driver = { |
097db1d0 DB |
383 | .name = "zero", |
384 | .dev = &device_desc, | |
385 | .strings = dev_strings, | |
57c97c02 | 386 | .max_speed = USB_SPEED_SUPER, |
03e42bd5 | 387 | .bind = zero_bind, |
ab943a2e DB |
388 | .unbind = zero_unbind, |
389 | .suspend = zero_suspend, | |
390 | .resume = zero_resume, | |
1da177e4 LT |
391 | }; |
392 | ||
ca2bdf4b DB |
393 | MODULE_AUTHOR("David Brownell"); |
394 | MODULE_LICENSE("GPL"); | |
1da177e4 | 395 | |
7472f38b | 396 | static int __init init(void) |
1da177e4 | 397 | { |
03e42bd5 | 398 | return usb_composite_probe(&zero_driver); |
1da177e4 | 399 | } |
7472f38b | 400 | module_init(init); |
1da177e4 | 401 | |
7472f38b | 402 | static void __exit cleanup(void) |
1da177e4 | 403 | { |
097db1d0 | 404 | usb_composite_unregister(&zero_driver); |
1da177e4 | 405 | } |
7472f38b | 406 | module_exit(cleanup); |