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