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 | 11 | * |
ca2bdf4b DB |
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. | |
1da177e4 | 16 | * |
ca2bdf4b DB |
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 | |
1da177e4 LT |
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. | |
7472f38b | 26 | * Some hardware details are visible, but don't affect most of the driver. |
1da177e4 LT |
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 | |
097db1d0 | 33 | * writes back, so the host can read it. |
1da177e4 LT |
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). | |
ca2bdf4b DB |
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. | |
1da177e4 LT |
43 | */ |
44 | ||
097db1d0 DB |
45 | /* |
46 | * driver assumes self-powered hardware, and | |
47 | * has no way for users to trigger remote wakeup. | |
48 | */ | |
49 | ||
ca2bdf4b | 50 | /* #define VERBOSE_DEBUG */ |
1da177e4 | 51 | |
1da177e4 | 52 | #include <linux/kernel.h> |
1da177e4 LT |
53 | #include <linux/utsname.h> |
54 | #include <linux/device.h> | |
1da177e4 | 55 | |
097db1d0 | 56 | #include "g_zero.h" |
1da177e4 LT |
57 | #include "gadget_chips.h" |
58 | ||
59 | ||
7e75bc0f DB |
60 | /*-------------------------------------------------------------------------*/ |
61 | ||
62 | /* | |
63 | * Kbuild is not very cooperative with respect to linking separately | |
64 | * compiled library objects into one module. So for now we won't use | |
65 | * separate compilation ... ensuring init/exit sections work to shrink | |
66 | * the runtime footprint, and giving us at least some parts of what | |
67 | * a "gcc --combine ... part1.c part2.c part3.c ... " build would. | |
68 | */ | |
69 | #include "composite.c" | |
70 | #include "usbstring.c" | |
71 | #include "config.c" | |
72 | #include "epautoconf.c" | |
73 | ||
74 | #include "f_sourcesink.c" | |
75 | #include "f_loopback.c" | |
76 | ||
1da177e4 LT |
77 | /*-------------------------------------------------------------------------*/ |
78 | ||
097db1d0 | 79 | #define DRIVER_VERSION "Cinco de Mayo 2008" |
1da177e4 | 80 | |
7472f38b | 81 | static const char longname[] = "Gadget Zero"; |
1da177e4 | 82 | |
097db1d0 DB |
83 | unsigned buflen = 4096; |
84 | module_param(buflen, uint, 0); | |
1da177e4 LT |
85 | |
86 | /* | |
87 | * Normally the "loopback" configuration is second (index 1) so | |
88 | * it's not the default. Here's where to change that order, to | |
097db1d0 DB |
89 | * work better with hosts where config changes are problematic or |
90 | * controllers (like original superh) that only support one config. | |
1da177e4 LT |
91 | */ |
92 | static int loopdefault = 0; | |
7472f38b | 93 | module_param(loopdefault, bool, S_IRUGO|S_IWUSR); |
1da177e4 LT |
94 | |
95 | /*-------------------------------------------------------------------------*/ | |
96 | ||
97 | /* Thanks to NetChip Technologies for donating this product ID. | |
98 | * | |
99 | * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!! | |
100 | * Instead: allocate your own, using normal USB-IF procedures. | |
101 | */ | |
102 | #ifndef CONFIG_USB_ZERO_HNPTEST | |
103 | #define DRIVER_VENDOR_NUM 0x0525 /* NetChip */ | |
104 | #define DRIVER_PRODUCT_NUM 0xa4a0 /* Linux-USB "Gadget Zero" */ | |
105 | #else | |
106 | #define DRIVER_VENDOR_NUM 0x1a0a /* OTG test device IDs */ | |
107 | #define DRIVER_PRODUCT_NUM 0xbadd | |
108 | #endif | |
109 | ||
110 | /*-------------------------------------------------------------------------*/ | |
111 | ||
7472f38b | 112 | static struct usb_device_descriptor device_desc = { |
1da177e4 LT |
113 | .bLength = sizeof device_desc, |
114 | .bDescriptorType = USB_DT_DEVICE, | |
115 | ||
7472f38b | 116 | .bcdUSB = __constant_cpu_to_le16(0x0200), |
1da177e4 LT |
117 | .bDeviceClass = USB_CLASS_VENDOR_SPEC, |
118 | ||
7472f38b DB |
119 | .idVendor = __constant_cpu_to_le16(DRIVER_VENDOR_NUM), |
120 | .idProduct = __constant_cpu_to_le16(DRIVER_PRODUCT_NUM), | |
1da177e4 LT |
121 | .bNumConfigurations = 2, |
122 | }; | |
123 | ||
097db1d0 | 124 | #ifdef CONFIG_USB_OTG |
7472f38b | 125 | static struct usb_otg_descriptor otg_descriptor = { |
1da177e4 LT |
126 | .bLength = sizeof otg_descriptor, |
127 | .bDescriptorType = USB_DT_OTG, | |
128 | ||
097db1d0 DB |
129 | /* REVISIT SRP-only hardware is possible, although |
130 | * it would not be called "OTG" ... | |
131 | */ | |
132 | .bmAttributes = USB_OTG_SRP | USB_OTG_HNP, | |
1da177e4 LT |
133 | }; |
134 | ||
097db1d0 | 135 | const struct usb_descriptor_header *otg_desc[] = { |
1da177e4 | 136 | (struct usb_descriptor_header *) &otg_descriptor, |
1da177e4 LT |
137 | NULL, |
138 | }; | |
097db1d0 | 139 | #endif |
1da177e4 | 140 | |
097db1d0 | 141 | /* string IDs are assigned dynamically */ |
1da177e4 | 142 | |
097db1d0 DB |
143 | #define STRING_MANUFACTURER_IDX 0 |
144 | #define STRING_PRODUCT_IDX 1 | |
145 | #define STRING_SERIAL_IDX 2 | |
1da177e4 | 146 | |
ca2bdf4b | 147 | static char manufacturer[50]; |
1da177e4 | 148 | |
ca2bdf4b DB |
149 | /* default serial number takes at least two packets */ |
150 | static char serial[] = "0123456789.0123456789.0123456789"; | |
1da177e4 | 151 | |
097db1d0 DB |
152 | static struct usb_string strings_dev[] = { |
153 | [STRING_MANUFACTURER_IDX].s = manufacturer, | |
154 | [STRING_PRODUCT_IDX].s = longname, | |
155 | [STRING_SERIAL_IDX].s = serial, | |
1da177e4 LT |
156 | { } /* end of list */ |
157 | }; | |
158 | ||
097db1d0 | 159 | static struct usb_gadget_strings stringtab_dev = { |
1da177e4 | 160 | .language = 0x0409, /* en-us */ |
097db1d0 | 161 | .strings = strings_dev, |
1da177e4 LT |
162 | }; |
163 | ||
097db1d0 DB |
164 | static struct usb_gadget_strings *dev_strings[] = { |
165 | &stringtab_dev, | |
166 | NULL, | |
167 | }; | |
1da177e4 LT |
168 | |
169 | /*-------------------------------------------------------------------------*/ | |
170 | ||
097db1d0 | 171 | struct usb_request *alloc_ep_req(struct usb_ep *ep) |
1da177e4 LT |
172 | { |
173 | struct usb_request *req; | |
174 | ||
7472f38b | 175 | req = usb_ep_alloc_request(ep, GFP_ATOMIC); |
1da177e4 | 176 | if (req) { |
097db1d0 DB |
177 | req->length = buflen; |
178 | req->buf = kmalloc(buflen, GFP_ATOMIC); | |
1da177e4 | 179 | if (!req->buf) { |
7472f38b | 180 | usb_ep_free_request(ep, req); |
1da177e4 LT |
181 | req = NULL; |
182 | } | |
183 | } | |
184 | return req; | |
185 | } | |
186 | ||
097db1d0 | 187 | void free_ep_req(struct usb_ep *ep, struct usb_request *req) |
1da177e4 | 188 | { |
9d8bab58 | 189 | kfree(req->buf); |
7472f38b | 190 | usb_ep_free_request(ep, req); |
1da177e4 LT |
191 | } |
192 | ||
097db1d0 | 193 | static void disable_ep(struct usb_composite_dev *cdev, struct usb_ep *ep) |
1da177e4 | 194 | { |
097db1d0 DB |
195 | int value; |
196 | ||
197 | if (ep->driver_data) { | |
198 | value = usb_ep_disable(ep); | |
199 | if (value < 0) | |
200 | DBG(cdev, "disable %s --> %d\n", | |
201 | ep->name, value); | |
202 | ep->driver_data = NULL; | |
1da177e4 | 203 | } |
1da177e4 LT |
204 | } |
205 | ||
097db1d0 DB |
206 | void disable_endpoints(struct usb_composite_dev *cdev, |
207 | struct usb_ep *in, struct usb_ep *out) | |
1da177e4 | 208 | { |
097db1d0 DB |
209 | disable_ep(cdev, in); |
210 | disable_ep(cdev, out); | |
1da177e4 LT |
211 | } |
212 | ||
213 | /*-------------------------------------------------------------------------*/ | |
214 | ||
097db1d0 | 215 | static int __init zero_bind(struct usb_composite_dev *cdev) |
1da177e4 | 216 | { |
91e79c91 | 217 | int gcnum; |
097db1d0 DB |
218 | struct usb_gadget *gadget = cdev->gadget; |
219 | int id; | |
91e79c91 | 220 | |
097db1d0 DB |
221 | /* Allocate string descriptor numbers ... note that string |
222 | * contents can be overridden by the composite_dev glue. | |
91e79c91 | 223 | */ |
097db1d0 DB |
224 | id = usb_string_id(cdev); |
225 | if (id < 0) | |
226 | return id; | |
227 | strings_dev[STRING_MANUFACTURER_IDX].id = id; | |
228 | device_desc.iManufacturer = id; | |
229 | ||
230 | id = usb_string_id(cdev); | |
231 | if (id < 0) | |
232 | return id; | |
233 | strings_dev[STRING_PRODUCT_IDX].id = id; | |
234 | device_desc.iProduct = id; | |
235 | ||
236 | id = usb_string_id(cdev); | |
237 | if (id < 0) | |
238 | return id; | |
239 | strings_dev[STRING_SERIAL_IDX].id = id; | |
240 | device_desc.iSerialNumber = id; | |
241 | ||
242 | /* Register primary, then secondary configuration. Note that | |
243 | * SH3 only allows one config... | |
1da177e4 | 244 | */ |
097db1d0 DB |
245 | if (loopdefault) { |
246 | loopback_add(cdev); | |
247 | if (!gadget_is_sh(gadget)) | |
248 | sourcesink_add(cdev); | |
249 | } else { | |
250 | sourcesink_add(cdev); | |
251 | if (!gadget_is_sh(gadget)) | |
252 | loopback_add(cdev); | |
1da177e4 | 253 | } |
1da177e4 | 254 | |
7472f38b | 255 | gcnum = usb_gadget_controller_number(gadget); |
91e79c91 | 256 | if (gcnum >= 0) |
7472f38b | 257 | device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum); |
91e79c91 | 258 | else { |
1da177e4 LT |
259 | /* gadget zero is so simple (for now, no altsettings) that |
260 | * it SHOULD NOT have problems with bulk-capable hardware. | |
097db1d0 | 261 | * so just warn about unrcognized controllers -- don't panic. |
1da177e4 LT |
262 | * |
263 | * things like configuration and altsetting numbering | |
264 | * can need hardware-specific attention though. | |
265 | */ | |
00274921 | 266 | pr_warning("%s: controller '%s' not recognized\n", |
097db1d0 | 267 | longname, gadget->name); |
7472f38b | 268 | device_desc.bcdDevice = __constant_cpu_to_le16(0x9999); |
1da177e4 LT |
269 | } |
270 | ||
271 | ||
097db1d0 | 272 | INFO(cdev, "%s, version: " DRIVER_VERSION "\n", longname); |
1da177e4 | 273 | |
7472f38b | 274 | snprintf(manufacturer, sizeof manufacturer, "%s %s with %s", |
96b644bd | 275 | init_utsname()->sysname, init_utsname()->release, |
1da177e4 LT |
276 | gadget->name); |
277 | ||
278 | return 0; | |
1da177e4 LT |
279 | } |
280 | ||
097db1d0 DB |
281 | static struct usb_composite_driver zero_driver = { |
282 | .name = "zero", | |
283 | .dev = &device_desc, | |
284 | .strings = dev_strings, | |
1da177e4 | 285 | .bind = zero_bind, |
1da177e4 LT |
286 | }; |
287 | ||
ca2bdf4b DB |
288 | MODULE_AUTHOR("David Brownell"); |
289 | MODULE_LICENSE("GPL"); | |
1da177e4 | 290 | |
7472f38b | 291 | static int __init init(void) |
1da177e4 | 292 | { |
097db1d0 | 293 | return usb_composite_register(&zero_driver); |
1da177e4 | 294 | } |
7472f38b | 295 | module_init(init); |
1da177e4 | 296 | |
7472f38b | 297 | static void __exit cleanup(void) |
1da177e4 | 298 | { |
097db1d0 | 299 | usb_composite_unregister(&zero_driver); |
1da177e4 | 300 | } |
7472f38b | 301 | module_exit(cleanup); |