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 | |
b8bcf32a | 31 | * configuration infrastructure is working correctly; also, so that it can |
ca2bdf4b | 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 = { |
25d80151 AP |
67 | .isoc_interval = GZERO_ISOC_INTERVAL, |
68 | .isoc_maxpacket = GZERO_ISOC_MAXPACKET, | |
c0501f47 AP |
69 | .bulk_buflen = GZERO_BULK_BUFLEN, |
70 | .qlen = GZERO_QLEN, | |
0d6c3d96 PC |
71 | .ss_bulk_qlen = GZERO_SS_BULK_QLEN, |
72 | .ss_iso_qlen = GZERO_SS_ISO_QLEN, | |
cf9a08ae SAS |
73 | }; |
74 | ||
1da177e4 LT |
75 | /*-------------------------------------------------------------------------*/ |
76 | ||
77 | /* Thanks to NetChip Technologies for donating this product ID. | |
78 | * | |
79 | * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!! | |
80 | * Instead: allocate your own, using normal USB-IF procedures. | |
81 | */ | |
82 | #ifndef CONFIG_USB_ZERO_HNPTEST | |
83 | #define DRIVER_VENDOR_NUM 0x0525 /* NetChip */ | |
84 | #define DRIVER_PRODUCT_NUM 0xa4a0 /* Linux-USB "Gadget Zero" */ | |
ab943a2e | 85 | #define DEFAULT_AUTORESUME 0 |
1da177e4 LT |
86 | #else |
87 | #define DRIVER_VENDOR_NUM 0x1a0a /* OTG test device IDs */ | |
88 | #define DRIVER_PRODUCT_NUM 0xbadd | |
ab943a2e | 89 | #define DEFAULT_AUTORESUME 5 |
1da177e4 LT |
90 | #endif |
91 | ||
ab943a2e DB |
92 | /* If the optional "autoresume" mode is enabled, it provides good |
93 | * functional coverage for the "USBCV" test harness from USB-IF. | |
94 | * It's always set if OTG mode is enabled. | |
95 | */ | |
d07a5901 | 96 | static unsigned autoresume = DEFAULT_AUTORESUME; |
ab943a2e DB |
97 | module_param(autoresume, uint, S_IRUGO); |
98 | MODULE_PARM_DESC(autoresume, "zero, or seconds before remote wakeup"); | |
99 | ||
36ed03f7 | 100 | /* Maximum Autoresume time */ |
d07a5901 | 101 | static unsigned max_autoresume; |
36ed03f7 PC |
102 | module_param(max_autoresume, uint, S_IRUGO); |
103 | MODULE_PARM_DESC(max_autoresume, "maximum seconds before remote wakeup"); | |
104 | ||
105 | /* Interval between two remote wakeups */ | |
d07a5901 | 106 | static unsigned autoresume_interval_ms; |
36ed03f7 PC |
107 | module_param(autoresume_interval_ms, uint, S_IRUGO); |
108 | MODULE_PARM_DESC(autoresume_interval_ms, | |
109 | "milliseconds to increase successive wakeup delays"); | |
110 | ||
111 | static unsigned autoresume_step_ms; | |
1da177e4 LT |
112 | /*-------------------------------------------------------------------------*/ |
113 | ||
7472f38b | 114 | static struct usb_device_descriptor device_desc = { |
1da177e4 LT |
115 | .bLength = sizeof device_desc, |
116 | .bDescriptorType = USB_DT_DEVICE, | |
117 | ||
0aecfc1b | 118 | /* .bcdUSB = DYNAMIC */ |
1da177e4 LT |
119 | .bDeviceClass = USB_CLASS_VENDOR_SPEC, |
120 | ||
551509d2 HH |
121 | .idVendor = cpu_to_le16(DRIVER_VENDOR_NUM), |
122 | .idProduct = cpu_to_le16(DRIVER_PRODUCT_NUM), | |
1da177e4 LT |
123 | .bNumConfigurations = 2, |
124 | }; | |
125 | ||
9e832bf7 | 126 | static const struct usb_descriptor_header *otg_desc[2]; |
1da177e4 | 127 | |
097db1d0 | 128 | /* string IDs are assigned dynamically */ |
ca2bdf4b DB |
129 | /* default serial number takes at least two packets */ |
130 | static char serial[] = "0123456789.0123456789.0123456789"; | |
1da177e4 | 131 | |
eeae5407 | 132 | #define USB_GZERO_SS_DESC (USB_GADGET_FIRST_AVAIL_IDX + 0) |
78f46f09 | 133 | #define USB_GZERO_LB_DESC (USB_GADGET_FIRST_AVAIL_IDX + 1) |
eeae5407 | 134 | |
097db1d0 | 135 | static struct usb_string strings_dev[] = { |
cc2683c3 | 136 | [USB_GADGET_MANUFACTURER_IDX].s = "", |
276e2e4f SAS |
137 | [USB_GADGET_PRODUCT_IDX].s = longname, |
138 | [USB_GADGET_SERIAL_IDX].s = serial, | |
eeae5407 | 139 | [USB_GZERO_SS_DESC].s = "source and sink data", |
78f46f09 | 140 | [USB_GZERO_LB_DESC].s = "loop input to output", |
1da177e4 LT |
141 | { } /* end of list */ |
142 | }; | |
143 | ||
097db1d0 | 144 | static struct usb_gadget_strings stringtab_dev = { |
1da177e4 | 145 | .language = 0x0409, /* en-us */ |
097db1d0 | 146 | .strings = strings_dev, |
1da177e4 LT |
147 | }; |
148 | ||
097db1d0 DB |
149 | static struct usb_gadget_strings *dev_strings[] = { |
150 | &stringtab_dev, | |
151 | NULL, | |
152 | }; | |
1da177e4 LT |
153 | |
154 | /*-------------------------------------------------------------------------*/ | |
155 | ||
ab943a2e DB |
156 | static struct timer_list autoresume_timer; |
157 | ||
158 | static void zero_autoresume(unsigned long _c) | |
159 | { | |
160 | struct usb_composite_dev *cdev = (void *)_c; | |
161 | struct usb_gadget *g = cdev->gadget; | |
162 | ||
163 | /* unconfigured devices can't issue wakeups */ | |
164 | if (!cdev->config) | |
165 | return; | |
166 | ||
167 | /* Normally the host would be woken up for something | |
168 | * more significant than just a timer firing; likely | |
169 | * because of some direct user request. | |
170 | */ | |
171 | if (g->speed != USB_SPEED_UNKNOWN) { | |
172 | int status = usb_gadget_wakeup(g); | |
173 | INFO(cdev, "%s --> %d\n", __func__, status); | |
174 | } | |
175 | } | |
176 | ||
177 | static void zero_suspend(struct usb_composite_dev *cdev) | |
178 | { | |
179 | if (cdev->gadget->speed == USB_SPEED_UNKNOWN) | |
180 | return; | |
181 | ||
182 | if (autoresume) { | |
36ed03f7 PC |
183 | if (max_autoresume && |
184 | (autoresume_step_ms > max_autoresume * 1000)) | |
185 | autoresume_step_ms = autoresume * 1000; | |
186 | ||
187 | mod_timer(&autoresume_timer, jiffies + | |
188 | msecs_to_jiffies(autoresume_step_ms)); | |
189 | DBG(cdev, "suspend, wakeup in %d milliseconds\n", | |
190 | autoresume_step_ms); | |
191 | ||
192 | autoresume_step_ms += autoresume_interval_ms; | |
ab943a2e DB |
193 | } else |
194 | DBG(cdev, "%s\n", __func__); | |
195 | } | |
196 | ||
197 | static void zero_resume(struct usb_composite_dev *cdev) | |
198 | { | |
199 | DBG(cdev, "%s\n", __func__); | |
200 | del_timer(&autoresume_timer); | |
201 | } | |
202 | ||
203 | /*-------------------------------------------------------------------------*/ | |
204 | ||
78f46f09 SAS |
205 | static struct usb_configuration loopback_driver = { |
206 | .label = "loopback", | |
78f46f09 SAS |
207 | .bConfigurationValue = 2, |
208 | .bmAttributes = USB_CONFIG_ATT_SELFPOWER, | |
209 | /* .iConfiguration = DYNAMIC */ | |
210 | }; | |
211 | ||
cf9a08ae SAS |
212 | static struct usb_function *func_ss; |
213 | static struct usb_function_instance *func_inst_ss; | |
214 | ||
215 | static int ss_config_setup(struct usb_configuration *c, | |
216 | const struct usb_ctrlrequest *ctrl) | |
217 | { | |
218 | switch (ctrl->bRequest) { | |
219 | case 0x5b: | |
220 | case 0x5c: | |
221 | return func_ss->setup(func_ss, ctrl); | |
222 | default: | |
223 | return -EOPNOTSUPP; | |
224 | } | |
225 | } | |
226 | ||
eeae5407 SAS |
227 | static struct usb_configuration sourcesink_driver = { |
228 | .label = "source/sink", | |
eeae5407 SAS |
229 | .setup = ss_config_setup, |
230 | .bConfigurationValue = 3, | |
231 | .bmAttributes = USB_CONFIG_ATT_SELFPOWER, | |
232 | /* .iConfiguration = DYNAMIC */ | |
233 | }; | |
234 | ||
cf9a08ae SAS |
235 | module_param_named(buflen, gzero_options.bulk_buflen, uint, 0); |
236 | module_param_named(pattern, gzero_options.pattern, uint, S_IRUGO|S_IWUSR); | |
237 | MODULE_PARM_DESC(pattern, "0 = all zeroes, 1 = mod63, 2 = none"); | |
238 | ||
239 | module_param_named(isoc_interval, gzero_options.isoc_interval, uint, | |
240 | S_IRUGO|S_IWUSR); | |
241 | MODULE_PARM_DESC(isoc_interval, "1 - 16"); | |
242 | ||
243 | module_param_named(isoc_maxpacket, gzero_options.isoc_maxpacket, uint, | |
244 | S_IRUGO|S_IWUSR); | |
245 | MODULE_PARM_DESC(isoc_maxpacket, "0 - 1023 (fs), 0 - 1024 (hs/ss)"); | |
246 | ||
247 | module_param_named(isoc_mult, gzero_options.isoc_mult, uint, S_IRUGO|S_IWUSR); | |
248 | MODULE_PARM_DESC(isoc_mult, "0 - 2 (hs/ss only)"); | |
249 | ||
250 | module_param_named(isoc_maxburst, gzero_options.isoc_maxburst, uint, | |
251 | S_IRUGO|S_IWUSR); | |
252 | MODULE_PARM_DESC(isoc_maxburst, "0 - 15 (ss only)"); | |
253 | ||
254 | static struct usb_function *func_lb; | |
255 | static struct usb_function_instance *func_inst_lb; | |
256 | ||
257 | module_param_named(qlen, gzero_options.qlen, uint, S_IRUGO|S_IWUSR); | |
258 | MODULE_PARM_DESC(qlen, "depth of loopback queue"); | |
259 | ||
0d6c3d96 PC |
260 | module_param_named(ss_bulk_qlen, gzero_options.ss_bulk_qlen, uint, |
261 | S_IRUGO|S_IWUSR); | |
262 | MODULE_PARM_DESC(bulk_qlen, "depth of sourcesink queue for bulk transfer"); | |
263 | ||
264 | module_param_named(ss_iso_qlen, gzero_options.ss_iso_qlen, uint, | |
265 | S_IRUGO|S_IWUSR); | |
266 | MODULE_PARM_DESC(iso_qlen, "depth of sourcesink queue for iso transfer"); | |
267 | ||
c94e289f | 268 | static int zero_bind(struct usb_composite_dev *cdev) |
1da177e4 | 269 | { |
cf9a08ae SAS |
270 | struct f_ss_opts *ss_opts; |
271 | struct f_lb_opts *lb_opts; | |
e1f15ccb | 272 | int status; |
91e79c91 | 273 | |
097db1d0 DB |
274 | /* Allocate string descriptor numbers ... note that string |
275 | * contents can be overridden by the composite_dev glue. | |
91e79c91 | 276 | */ |
e1f15ccb SAS |
277 | status = usb_string_ids_tab(cdev, strings_dev); |
278 | if (status < 0) | |
279 | return status; | |
280 | ||
276e2e4f SAS |
281 | device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id; |
282 | device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id; | |
283 | device_desc.iSerialNumber = strings_dev[USB_GADGET_SERIAL_IDX].id; | |
097db1d0 | 284 | |
ab943a2e DB |
285 | setup_timer(&autoresume_timer, zero_autoresume, (unsigned long) cdev); |
286 | ||
cf9a08ae SAS |
287 | func_inst_ss = usb_get_function_instance("SourceSink"); |
288 | if (IS_ERR(func_inst_ss)) | |
289 | return PTR_ERR(func_inst_ss); | |
290 | ||
291 | ss_opts = container_of(func_inst_ss, struct f_ss_opts, func_inst); | |
292 | ss_opts->pattern = gzero_options.pattern; | |
293 | ss_opts->isoc_interval = gzero_options.isoc_interval; | |
294 | ss_opts->isoc_maxpacket = gzero_options.isoc_maxpacket; | |
295 | ss_opts->isoc_mult = gzero_options.isoc_mult; | |
9c1b7036 | 296 | ss_opts->isoc_maxburst = gzero_options.isoc_maxburst; |
cf9a08ae | 297 | ss_opts->bulk_buflen = gzero_options.bulk_buflen; |
0d6c3d96 PC |
298 | ss_opts->bulk_qlen = gzero_options.ss_bulk_qlen; |
299 | ss_opts->iso_qlen = gzero_options.ss_iso_qlen; | |
cf9a08ae SAS |
300 | |
301 | func_ss = usb_get_function(func_inst_ss); | |
c8c18883 WY |
302 | if (IS_ERR(func_ss)) { |
303 | status = PTR_ERR(func_ss); | |
cf9a08ae | 304 | goto err_put_func_inst_ss; |
c8c18883 | 305 | } |
cf9a08ae SAS |
306 | |
307 | func_inst_lb = usb_get_function_instance("Loopback"); | |
c8c18883 WY |
308 | if (IS_ERR(func_inst_lb)) { |
309 | status = PTR_ERR(func_inst_lb); | |
cf9a08ae | 310 | goto err_put_func_ss; |
c8c18883 | 311 | } |
cf9a08ae SAS |
312 | |
313 | lb_opts = container_of(func_inst_lb, struct f_lb_opts, func_inst); | |
314 | lb_opts->bulk_buflen = gzero_options.bulk_buflen; | |
315 | lb_opts->qlen = gzero_options.qlen; | |
316 | ||
317 | func_lb = usb_get_function(func_inst_lb); | |
318 | if (IS_ERR(func_lb)) { | |
319 | status = PTR_ERR(func_lb); | |
320 | goto err_put_func_inst_lb; | |
321 | } | |
322 | ||
eeae5407 | 323 | sourcesink_driver.iConfiguration = strings_dev[USB_GZERO_SS_DESC].id; |
78f46f09 SAS |
324 | loopback_driver.iConfiguration = strings_dev[USB_GZERO_LB_DESC].id; |
325 | ||
eeae5407 SAS |
326 | /* support autoresume for remote wakeup testing */ |
327 | sourcesink_driver.bmAttributes &= ~USB_CONFIG_ATT_WAKEUP; | |
78f46f09 | 328 | loopback_driver.bmAttributes &= ~USB_CONFIG_ATT_WAKEUP; |
eeae5407 | 329 | sourcesink_driver.descriptors = NULL; |
78f46f09 SAS |
330 | loopback_driver.descriptors = NULL; |
331 | if (autoresume) { | |
eeae5407 | 332 | sourcesink_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP; |
78f46f09 | 333 | loopback_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP; |
36ed03f7 | 334 | autoresume_step_ms = autoresume * 1000; |
78f46f09 | 335 | } |
eeae5407 SAS |
336 | |
337 | /* support OTG systems */ | |
338 | if (gadget_is_otg(cdev->gadget)) { | |
9e832bf7 LJ |
339 | if (!otg_desc[0]) { |
340 | struct usb_descriptor_header *usb_desc; | |
341 | ||
342 | usb_desc = usb_otg_descriptor_alloc(cdev->gadget); | |
343 | if (!usb_desc) { | |
344 | status = -ENOMEM; | |
345 | goto err_conf_flb; | |
346 | } | |
347 | usb_otg_descriptor_init(cdev->gadget, usb_desc); | |
348 | otg_desc[0] = usb_desc; | |
349 | otg_desc[1] = NULL; | |
350 | } | |
eeae5407 SAS |
351 | sourcesink_driver.descriptors = otg_desc; |
352 | sourcesink_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP; | |
78f46f09 SAS |
353 | loopback_driver.descriptors = otg_desc; |
354 | loopback_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP; | |
eeae5407 SAS |
355 | } |
356 | ||
097db1d0 DB |
357 | /* Register primary, then secondary configuration. Note that |
358 | * SH3 only allows one config... | |
1da177e4 | 359 | */ |
097db1d0 | 360 | if (loopdefault) { |
cf9a08ae SAS |
361 | usb_add_config_only(cdev, &loopback_driver); |
362 | usb_add_config_only(cdev, &sourcesink_driver); | |
097db1d0 | 363 | } else { |
cf9a08ae SAS |
364 | usb_add_config_only(cdev, &sourcesink_driver); |
365 | usb_add_config_only(cdev, &loopback_driver); | |
1da177e4 | 366 | } |
cf9a08ae SAS |
367 | status = usb_add_function(&sourcesink_driver, func_ss); |
368 | if (status) | |
9e832bf7 | 369 | goto err_free_otg_desc; |
1da177e4 | 370 | |
cf9a08ae SAS |
371 | usb_ep_autoconfig_reset(cdev->gadget); |
372 | status = usb_add_function(&loopback_driver, func_lb); | |
373 | if (status) | |
9e832bf7 | 374 | goto err_free_otg_desc; |
cf9a08ae SAS |
375 | |
376 | usb_ep_autoconfig_reset(cdev->gadget); | |
7d16e8d3 | 377 | usb_composite_overwrite_options(cdev, &coverwrite); |
1da177e4 | 378 | |
097db1d0 | 379 | INFO(cdev, "%s, version: " DRIVER_VERSION "\n", longname); |
1da177e4 | 380 | |
1da177e4 | 381 | return 0; |
cf9a08ae | 382 | |
9e832bf7 LJ |
383 | err_free_otg_desc: |
384 | kfree(otg_desc[0]); | |
385 | otg_desc[0] = NULL; | |
cf9a08ae SAS |
386 | err_conf_flb: |
387 | usb_put_function(func_lb); | |
388 | func_lb = NULL; | |
389 | err_put_func_inst_lb: | |
390 | usb_put_function_instance(func_inst_lb); | |
391 | func_inst_lb = NULL; | |
392 | err_put_func_ss: | |
393 | usb_put_function(func_ss); | |
394 | func_ss = NULL; | |
395 | err_put_func_inst_ss: | |
396 | usb_put_function_instance(func_inst_ss); | |
397 | func_inst_ss = NULL; | |
398 | return status; | |
1da177e4 LT |
399 | } |
400 | ||
ab943a2e DB |
401 | static int zero_unbind(struct usb_composite_dev *cdev) |
402 | { | |
403 | del_timer_sync(&autoresume_timer); | |
cf9a08ae SAS |
404 | if (!IS_ERR_OR_NULL(func_ss)) |
405 | usb_put_function(func_ss); | |
abe7a409 | 406 | usb_put_function_instance(func_inst_ss); |
cf9a08ae SAS |
407 | if (!IS_ERR_OR_NULL(func_lb)) |
408 | usb_put_function(func_lb); | |
abe7a409 | 409 | usb_put_function_instance(func_inst_lb); |
9e832bf7 LJ |
410 | kfree(otg_desc[0]); |
411 | otg_desc[0] = NULL; | |
412 | ||
ab943a2e DB |
413 | return 0; |
414 | } | |
415 | ||
c94e289f | 416 | static struct usb_composite_driver zero_driver = { |
097db1d0 DB |
417 | .name = "zero", |
418 | .dev = &device_desc, | |
419 | .strings = dev_strings, | |
57c97c02 | 420 | .max_speed = USB_SPEED_SUPER, |
03e42bd5 | 421 | .bind = zero_bind, |
ab943a2e DB |
422 | .unbind = zero_unbind, |
423 | .suspend = zero_suspend, | |
424 | .resume = zero_resume, | |
1da177e4 LT |
425 | }; |
426 | ||
909346a8 TK |
427 | module_usb_composite_driver(zero_driver); |
428 | ||
ca2bdf4b DB |
429 | MODULE_AUTHOR("David Brownell"); |
430 | MODULE_LICENSE("GPL"); |