blob: 9c5057c633271b05428923a89efc5545f4e4138d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * zero.c -- Gadget Zero, for USB development
3 *
David Brownellca2bdf42007-08-02 12:20:05 -07004 * Copyright (C) 2003-2007 David Brownell
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * All rights reserved.
6 *
David Brownellca2bdf42007-08-02 12:20:05 -07007 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
David Brownellca2bdf42007-08-02 12:20:05 -070012 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 *
David Brownellca2bdf42007-08-02 12:20:05 -070017 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 */
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.
David Brownell7472f382008-04-18 18:47:54 -070026 * Some hardware details are visible, but don't affect most of the driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 *
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. Module options include:
34 *
35 * buflen=N default N=4096, buffer size used
36 * qlen=N default N=32, how many buffers in the loopback queue
37 * loopdefault default false, list loopback config first
David Brownell7472f382008-04-18 18:47:54 -070038 * autoresume=N default N=0, seconds before triggering remote wakeup
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 *
40 * Many drivers will only have one configuration, letting them be much
41 * simpler if they also don't support high speed operation (like this
42 * driver does).
David Brownellca2bdf42007-08-02 12:20:05 -070043 *
44 * Why is *this* driver using two configurations, rather than setting up
45 * two interfaces with different functions? To help verify that multiple
46 * configuration infrastucture is working correctly; also, so that it can
47 * work with low capability USB controllers without four bulk endpoints.
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 */
49
David Brownellca2bdf42007-08-02 12:20:05 -070050/* #define VERBOSE_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <linux/utsname.h>
54#include <linux/device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
David Brownell5f848132006-12-16 15:34:53 -080056#include <linux/usb/ch9.h>
David Brownell9454a572007-10-04 18:05:17 -070057#include <linux/usb/gadget.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59#include "gadget_chips.h"
60
61
62/*-------------------------------------------------------------------------*/
63
David Brownell7472f382008-04-18 18:47:54 -070064#define DRIVER_VERSION "Earth Day 2008"
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
David Brownell7472f382008-04-18 18:47:54 -070066static const char shortname[] = "zero";
67static const char longname[] = "Gadget Zero";
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
David Brownell7472f382008-04-18 18:47:54 -070069static const char source_sink[] = "source and sink data";
70static const char loopback[] = "loop input to output";
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72/*-------------------------------------------------------------------------*/
73
74/*
75 * driver assumes self-powered hardware, and
76 * has no way for users to trigger remote wakeup.
77 *
78 * this version autoconfigures as much as possible,
79 * which is reasonable for most "bulk-only" drivers.
80 */
81static const char *EP_IN_NAME; /* source */
82static const char *EP_OUT_NAME; /* sink */
83
84/*-------------------------------------------------------------------------*/
85
86/* big enough to hold our biggest descriptor */
87#define USB_BUFSIZ 256
88
89struct zero_dev {
90 spinlock_t lock;
91 struct usb_gadget *gadget;
92 struct usb_request *req; /* for control responses */
93
94 /* when configured, we have one of two configs:
95 * - source data (in to host) and sink it (out from host)
96 * - or loop it back (out from host back in to host)
97 */
98 u8 config;
99 struct usb_ep *in_ep, *out_ep;
100
101 /* autoresume timer */
102 struct timer_list resume;
103};
104
David Brownellca2bdf42007-08-02 12:20:05 -0700105#define DBG(d, fmt, args...) \
106 dev_dbg(&(d)->gadget->dev , fmt , ## args)
107#define VDBG(d, fmt, args...) \
108 dev_vdbg(&(d)->gadget->dev , fmt , ## args)
109#define ERROR(d, fmt, args...) \
110 dev_err(&(d)->gadget->dev , fmt , ## args)
111#define WARN(d, fmt, args...) \
112 dev_warn(&(d)->gadget->dev , fmt , ## args)
113#define INFO(d, fmt, args...) \
114 dev_info(&(d)->gadget->dev , fmt , ## args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116/*-------------------------------------------------------------------------*/
117
118static unsigned buflen = 4096;
119static unsigned qlen = 32;
120static unsigned pattern = 0;
121
David Brownell7472f382008-04-18 18:47:54 -0700122module_param(buflen, uint, S_IRUGO);
123module_param(qlen, uint, S_IRUGO);
124module_param(pattern, uint, S_IRUGO|S_IWUSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
126/*
127 * if it's nonzero, autoresume says how many seconds to wait
128 * before trying to wake up the host after suspend.
129 */
130static unsigned autoresume = 0;
David Brownell7472f382008-04-18 18:47:54 -0700131module_param(autoresume, uint, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133/*
134 * Normally the "loopback" configuration is second (index 1) so
135 * it's not the default. Here's where to change that order, to
136 * work better with hosts where config changes are problematic.
137 * Or controllers (like superh) that only support one config.
138 */
139static int loopdefault = 0;
David Brownell7472f382008-04-18 18:47:54 -0700140module_param(loopdefault, bool, S_IRUGO|S_IWUSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142/*-------------------------------------------------------------------------*/
143
144/* Thanks to NetChip Technologies for donating this product ID.
145 *
146 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
147 * Instead: allocate your own, using normal USB-IF procedures.
148 */
149#ifndef CONFIG_USB_ZERO_HNPTEST
150#define DRIVER_VENDOR_NUM 0x0525 /* NetChip */
151#define DRIVER_PRODUCT_NUM 0xa4a0 /* Linux-USB "Gadget Zero" */
152#else
153#define DRIVER_VENDOR_NUM 0x1a0a /* OTG test device IDs */
154#define DRIVER_PRODUCT_NUM 0xbadd
155#endif
156
157/*-------------------------------------------------------------------------*/
158
159/*
160 * DESCRIPTORS ... most are static, but strings and (full)
161 * configuration descriptors are built on demand.
162 */
163
164#define STRING_MANUFACTURER 25
165#define STRING_PRODUCT 42
166#define STRING_SERIAL 101
167#define STRING_SOURCE_SINK 250
168#define STRING_LOOPBACK 251
169
170/*
171 * This device advertises two configurations; these numbers work
172 * on a pxa250 as well as more flexible hardware.
173 */
174#define CONFIG_SOURCE_SINK 3
175#define CONFIG_LOOPBACK 2
176
David Brownell7472f382008-04-18 18:47:54 -0700177static struct usb_device_descriptor device_desc = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 .bLength = sizeof device_desc,
179 .bDescriptorType = USB_DT_DEVICE,
180
David Brownell7472f382008-04-18 18:47:54 -0700181 .bcdUSB = __constant_cpu_to_le16(0x0200),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 .bDeviceClass = USB_CLASS_VENDOR_SPEC,
183
David Brownell7472f382008-04-18 18:47:54 -0700184 .idVendor = __constant_cpu_to_le16(DRIVER_VENDOR_NUM),
185 .idProduct = __constant_cpu_to_le16(DRIVER_PRODUCT_NUM),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 .iManufacturer = STRING_MANUFACTURER,
187 .iProduct = STRING_PRODUCT,
188 .iSerialNumber = STRING_SERIAL,
189 .bNumConfigurations = 2,
190};
191
David Brownell7472f382008-04-18 18:47:54 -0700192static struct usb_config_descriptor source_sink_config = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 .bLength = sizeof source_sink_config,
194 .bDescriptorType = USB_DT_CONFIG,
195
196 /* compute wTotalLength on the fly */
197 .bNumInterfaces = 1,
198 .bConfigurationValue = CONFIG_SOURCE_SINK,
199 .iConfiguration = STRING_SOURCE_SINK,
200 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
201 .bMaxPower = 1, /* self-powered */
202};
203
David Brownell7472f382008-04-18 18:47:54 -0700204static struct usb_config_descriptor loopback_config = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 .bLength = sizeof loopback_config,
206 .bDescriptorType = USB_DT_CONFIG,
207
208 /* compute wTotalLength on the fly */
209 .bNumInterfaces = 1,
210 .bConfigurationValue = CONFIG_LOOPBACK,
211 .iConfiguration = STRING_LOOPBACK,
212 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
213 .bMaxPower = 1, /* self-powered */
214};
215
David Brownell7472f382008-04-18 18:47:54 -0700216static struct usb_otg_descriptor otg_descriptor = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 .bLength = sizeof otg_descriptor,
218 .bDescriptorType = USB_DT_OTG,
219
220 .bmAttributes = USB_OTG_SRP,
221};
222
223/* one interface in each configuration */
224
David Brownell7472f382008-04-18 18:47:54 -0700225static const struct usb_interface_descriptor source_sink_intf = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 .bLength = sizeof source_sink_intf,
227 .bDescriptorType = USB_DT_INTERFACE,
228
229 .bNumEndpoints = 2,
230 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
231 .iInterface = STRING_SOURCE_SINK,
232};
233
David Brownell7472f382008-04-18 18:47:54 -0700234static const struct usb_interface_descriptor loopback_intf = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 .bLength = sizeof loopback_intf,
236 .bDescriptorType = USB_DT_INTERFACE,
237
238 .bNumEndpoints = 2,
239 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
240 .iInterface = STRING_LOOPBACK,
241};
242
243/* two full speed bulk endpoints; their use is config-dependent */
244
David Brownell7472f382008-04-18 18:47:54 -0700245static struct usb_endpoint_descriptor fs_source_desc = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 .bLength = USB_DT_ENDPOINT_SIZE,
247 .bDescriptorType = USB_DT_ENDPOINT,
248
249 .bEndpointAddress = USB_DIR_IN,
250 .bmAttributes = USB_ENDPOINT_XFER_BULK,
251};
252
David Brownell7472f382008-04-18 18:47:54 -0700253static struct usb_endpoint_descriptor fs_sink_desc = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 .bLength = USB_DT_ENDPOINT_SIZE,
255 .bDescriptorType = USB_DT_ENDPOINT,
256
257 .bEndpointAddress = USB_DIR_OUT,
258 .bmAttributes = USB_ENDPOINT_XFER_BULK,
259};
260
David Brownell7472f382008-04-18 18:47:54 -0700261static const struct usb_descriptor_header *fs_source_sink_function[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 (struct usb_descriptor_header *) &otg_descriptor,
263 (struct usb_descriptor_header *) &source_sink_intf,
264 (struct usb_descriptor_header *) &fs_sink_desc,
265 (struct usb_descriptor_header *) &fs_source_desc,
266 NULL,
267};
268
David Brownell7472f382008-04-18 18:47:54 -0700269static const struct usb_descriptor_header *fs_loopback_function[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 (struct usb_descriptor_header *) &otg_descriptor,
271 (struct usb_descriptor_header *) &loopback_intf,
272 (struct usb_descriptor_header *) &fs_sink_desc,
273 (struct usb_descriptor_header *) &fs_source_desc,
274 NULL,
275};
276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277/*
278 * usb 2.0 devices need to expose both high speed and full speed
279 * descriptors, unless they only run at full speed.
280 *
281 * that means alternate endpoint descriptors (bigger packets)
282 * and a "device qualifier" ... plus more construction options
283 * for the config descriptor.
284 */
285
David Brownell7472f382008-04-18 18:47:54 -0700286static struct usb_endpoint_descriptor hs_source_desc = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 .bLength = USB_DT_ENDPOINT_SIZE,
288 .bDescriptorType = USB_DT_ENDPOINT,
289
290 .bmAttributes = USB_ENDPOINT_XFER_BULK,
David Brownell7472f382008-04-18 18:47:54 -0700291 .wMaxPacketSize = __constant_cpu_to_le16(512),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292};
293
David Brownell7472f382008-04-18 18:47:54 -0700294static struct usb_endpoint_descriptor hs_sink_desc = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 .bLength = USB_DT_ENDPOINT_SIZE,
296 .bDescriptorType = USB_DT_ENDPOINT,
297
298 .bmAttributes = USB_ENDPOINT_XFER_BULK,
David Brownell7472f382008-04-18 18:47:54 -0700299 .wMaxPacketSize = __constant_cpu_to_le16(512),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300};
301
David Brownell7472f382008-04-18 18:47:54 -0700302static struct usb_qualifier_descriptor dev_qualifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 .bLength = sizeof dev_qualifier,
304 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
305
David Brownell7472f382008-04-18 18:47:54 -0700306 .bcdUSB = __constant_cpu_to_le16(0x0200),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 .bDeviceClass = USB_CLASS_VENDOR_SPEC,
308
309 .bNumConfigurations = 2,
310};
311
David Brownell7472f382008-04-18 18:47:54 -0700312static const struct usb_descriptor_header *hs_source_sink_function[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 (struct usb_descriptor_header *) &otg_descriptor,
314 (struct usb_descriptor_header *) &source_sink_intf,
315 (struct usb_descriptor_header *) &hs_source_desc,
316 (struct usb_descriptor_header *) &hs_sink_desc,
317 NULL,
318};
319
David Brownell7472f382008-04-18 18:47:54 -0700320static const struct usb_descriptor_header *hs_loopback_function[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 (struct usb_descriptor_header *) &otg_descriptor,
322 (struct usb_descriptor_header *) &loopback_intf,
323 (struct usb_descriptor_header *) &hs_source_desc,
324 (struct usb_descriptor_header *) &hs_sink_desc,
325 NULL,
326};
327
328/* maxpacket and other transfer characteristics vary by speed. */
David Brownellca2bdf42007-08-02 12:20:05 -0700329static inline struct usb_endpoint_descriptor *
330ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs,
331 struct usb_endpoint_descriptor *fs)
332{
333 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
334 return hs;
335 return fs;
336}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
David Brownellca2bdf42007-08-02 12:20:05 -0700338static char manufacturer[50];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
David Brownellca2bdf42007-08-02 12:20:05 -0700340/* default serial number takes at least two packets */
341static char serial[] = "0123456789.0123456789.0123456789";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
344/* static strings, in UTF-8 */
David Brownell7472f382008-04-18 18:47:54 -0700345static struct usb_string strings[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 { STRING_MANUFACTURER, manufacturer, },
347 { STRING_PRODUCT, longname, },
348 { STRING_SERIAL, serial, },
349 { STRING_LOOPBACK, loopback, },
350 { STRING_SOURCE_SINK, source_sink, },
351 { } /* end of list */
352};
353
David Brownell7472f382008-04-18 18:47:54 -0700354static struct usb_gadget_strings stringtab = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 .language = 0x0409, /* en-us */
356 .strings = strings,
357};
358
359/*
360 * config descriptors are also handcrafted. these must agree with code
361 * that sets configurations, and with code managing interfaces and their
362 * altsettings. other complexity may come from:
363 *
364 * - high speed support, including "other speed config" rules
365 * - multiple configurations
366 * - interfaces with alternate settings
367 * - embedded class or vendor-specific descriptors
368 *
369 * this handles high speed, and has a second config that could as easily
370 * have been an alternate interface setting (on most hardware).
371 *
372 * NOTE: to demonstrate (and test) more USB capabilities, this driver
373 * should include an altsetting to test interrupt transfers, including
374 * high bandwidth modes at high speed. (Maybe work like Intel's test
375 * device?)
376 */
David Brownell7472f382008-04-18 18:47:54 -0700377static int config_buf(struct usb_gadget *gadget,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 u8 *buf, u8 type, unsigned index)
379{
380 int is_source_sink;
381 int len;
382 const struct usb_descriptor_header **function;
David Brownellca2bdf42007-08-02 12:20:05 -0700383 int hs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
385 /* two configurations will always be index 0 and index 1 */
386 if (index > 1)
387 return -EINVAL;
388 is_source_sink = loopdefault ? (index == 1) : (index == 0);
389
David Brownellca2bdf42007-08-02 12:20:05 -0700390 if (gadget_is_dualspeed(gadget)) {
391 hs = (gadget->speed == USB_SPEED_HIGH);
392 if (type == USB_DT_OTHER_SPEED_CONFIG)
393 hs = !hs;
394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 if (hs)
396 function = is_source_sink
397 ? hs_source_sink_function
398 : hs_loopback_function;
399 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 function = is_source_sink
401 ? fs_source_sink_function
402 : fs_loopback_function;
403
404 /* for now, don't advertise srp-only devices */
David Brownellca2bdf42007-08-02 12:20:05 -0700405 if (!gadget_is_otg(gadget))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 function++;
407
David Brownell7472f382008-04-18 18:47:54 -0700408 len = usb_gadget_config_buf(is_source_sink
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 ? &source_sink_config
410 : &loopback_config,
411 buf, USB_BUFSIZ, function);
412 if (len < 0)
413 return len;
414 ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
415 return len;
416}
417
418/*-------------------------------------------------------------------------*/
419
David Brownell7472f382008-04-18 18:47:54 -0700420static struct usb_request *alloc_ep_req(struct usb_ep *ep, unsigned length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
422 struct usb_request *req;
423
David Brownell7472f382008-04-18 18:47:54 -0700424 req = usb_ep_alloc_request(ep, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 if (req) {
426 req->length = length;
David Brownell9d8bab52007-07-01 11:04:54 -0700427 req->buf = kmalloc(length, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 if (!req->buf) {
David Brownell7472f382008-04-18 18:47:54 -0700429 usb_ep_free_request(ep, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 req = NULL;
431 }
432 }
433 return req;
434}
435
David Brownell7472f382008-04-18 18:47:54 -0700436static void free_ep_req(struct usb_ep *ep, struct usb_request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
David Brownell9d8bab52007-07-01 11:04:54 -0700438 kfree(req->buf);
David Brownell7472f382008-04-18 18:47:54 -0700439 usb_ep_free_request(ep, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440}
441
442/*-------------------------------------------------------------------------*/
443
David Brownellca2bdf42007-08-02 12:20:05 -0700444/*
445 * SOURCE/SINK FUNCTION ... a primary testing vehicle for USB peripherals,
446 * this just sinks bulk packets OUT to the peripheral and sources them IN
447 * to the host, optionally with specific data patterns.
448 *
449 * In terms of control messaging, this supports all the standard requests
450 * plus two that support control-OUT tests.
451 *
452 * Note that because this doesn't queue more than one request at a time,
453 * some other function must be used to test queueing logic. The network
454 * link (g_ether) is probably the best option for that.
455 */
456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457/* optionally require specific source/sink data patterns */
458
459static int
David Brownell7472f382008-04-18 18:47:54 -0700460check_read_data(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 struct zero_dev *dev,
462 struct usb_ep *ep,
463 struct usb_request *req
464)
465{
466 unsigned i;
467 u8 *buf = req->buf;
468
469 for (i = 0; i < req->actual; i++, buf++) {
470 switch (pattern) {
471 /* all-zeroes has no synchronization issues */
472 case 0:
473 if (*buf == 0)
474 continue;
475 break;
476 /* mod63 stays in sync with short-terminated transfers,
477 * or otherwise when host and gadget agree on how large
478 * each usb transfer request should be. resync is done
479 * with set_interface or set_config.
480 */
481 case 1:
482 if (*buf == (u8)(i % 63))
483 continue;
484 break;
485 }
David Brownell7472f382008-04-18 18:47:54 -0700486 ERROR(dev, "bad OUT byte, buf[%d] = %d\n", i, *buf);
487 usb_ep_set_halt(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 return -EINVAL;
489 }
490 return 0;
491}
492
David Brownellca2bdf42007-08-02 12:20:05 -0700493static void reinit_write_data(struct usb_ep *ep, struct usb_request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
495 unsigned i;
496 u8 *buf = req->buf;
497
498 switch (pattern) {
499 case 0:
David Brownell7472f382008-04-18 18:47:54 -0700500 memset(req->buf, 0, req->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 break;
502 case 1:
503 for (i = 0; i < req->length; i++)
504 *buf++ = (u8) (i % 63);
505 break;
506 }
507}
508
509/* if there is only one request in the queue, there'll always be an
510 * irq delay between end of one request and start of the next.
511 * that prevents using hardware dma queues.
512 */
David Brownell7472f382008-04-18 18:47:54 -0700513static void source_sink_complete(struct usb_ep *ep, struct usb_request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514{
515 struct zero_dev *dev = ep->driver_data;
516 int status = req->status;
517
518 switch (status) {
519
David Brownellca2bdf42007-08-02 12:20:05 -0700520 case 0: /* normal completion? */
David Brownell35fcca42006-04-02 10:19:43 -0800521 if (ep == dev->out_ep) {
David Brownell7472f382008-04-18 18:47:54 -0700522 check_read_data(dev, ep, req);
523 memset(req->buf, 0x55, req->length);
David Brownell35fcca42006-04-02 10:19:43 -0800524 } else
David Brownellca2bdf42007-08-02 12:20:05 -0700525 reinit_write_data(ep, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 break;
527
528 /* this endpoint is normally active while we're configured */
David Brownellca2bdf42007-08-02 12:20:05 -0700529 case -ECONNABORTED: /* hardware forced ep reset */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 case -ECONNRESET: /* request dequeued */
531 case -ESHUTDOWN: /* disconnect from host */
David Brownell7472f382008-04-18 18:47:54 -0700532 VDBG(dev, "%s gone (%d), %d/%d\n", ep->name, status,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 req->actual, req->length);
534 if (ep == dev->out_ep)
David Brownell7472f382008-04-18 18:47:54 -0700535 check_read_data(dev, ep, req);
536 free_ep_req(ep, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 return;
538
539 case -EOVERFLOW: /* buffer overrun on read means that
540 * we didn't provide a big enough
541 * buffer.
542 */
543 default:
544#if 1
David Brownell7472f382008-04-18 18:47:54 -0700545 DBG(dev, "%s complete --> %d, %d/%d\n", ep->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 status, req->actual, req->length);
547#endif
548 case -EREMOTEIO: /* short read */
549 break;
550 }
551
David Brownell7472f382008-04-18 18:47:54 -0700552 status = usb_ep_queue(ep, req, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 if (status) {
David Brownell7472f382008-04-18 18:47:54 -0700554 ERROR(dev, "kill %s: resubmit %d bytes --> %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 ep->name, req->length, status);
David Brownell7472f382008-04-18 18:47:54 -0700556 usb_ep_set_halt(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 /* FIXME recover later ... somehow */
558 }
559}
560
David Brownellca2bdf42007-08-02 12:20:05 -0700561static struct usb_request *source_sink_start_ep(struct usb_ep *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
563 struct usb_request *req;
564 int status;
565
David Brownell7472f382008-04-18 18:47:54 -0700566 req = alloc_ep_req(ep, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 if (!req)
568 return NULL;
569
David Brownell7472f382008-04-18 18:47:54 -0700570 memset(req->buf, 0, req->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 req->complete = source_sink_complete;
572
David Brownell7472f382008-04-18 18:47:54 -0700573 if (strcmp(ep->name, EP_IN_NAME) == 0)
David Brownellca2bdf42007-08-02 12:20:05 -0700574 reinit_write_data(ep, req);
David Brownell35fcca42006-04-02 10:19:43 -0800575 else
David Brownell7472f382008-04-18 18:47:54 -0700576 memset(req->buf, 0x55, req->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
David Brownellca2bdf42007-08-02 12:20:05 -0700578 status = usb_ep_queue(ep, req, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 if (status) {
580 struct zero_dev *dev = ep->driver_data;
581
David Brownell7472f382008-04-18 18:47:54 -0700582 ERROR(dev, "start %s --> %d\n", ep->name, status);
583 free_ep_req(ep, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 req = NULL;
585 }
586
587 return req;
588}
589
David Brownellca2bdf42007-08-02 12:20:05 -0700590static int set_source_sink_config(struct zero_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
592 int result = 0;
593 struct usb_ep *ep;
594 struct usb_gadget *gadget = dev->gadget;
595
David Brownell7472f382008-04-18 18:47:54 -0700596 gadget_for_each_ep(ep, gadget) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 const struct usb_endpoint_descriptor *d;
598
599 /* one endpoint writes (sources) zeroes in (to the host) */
David Brownell7472f382008-04-18 18:47:54 -0700600 if (strcmp(ep->name, EP_IN_NAME) == 0) {
601 d = ep_desc(gadget, &hs_source_desc, &fs_source_desc);
602 result = usb_ep_enable(ep, d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 if (result == 0) {
604 ep->driver_data = dev;
David Brownellca2bdf42007-08-02 12:20:05 -0700605 if (source_sink_start_ep(ep) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 dev->in_ep = ep;
607 continue;
608 }
David Brownell7472f382008-04-18 18:47:54 -0700609 usb_ep_disable(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 result = -EIO;
611 }
612
613 /* one endpoint reads (sinks) anything out (from the host) */
David Brownell7472f382008-04-18 18:47:54 -0700614 } else if (strcmp(ep->name, EP_OUT_NAME) == 0) {
615 d = ep_desc(gadget, &hs_sink_desc, &fs_sink_desc);
616 result = usb_ep_enable(ep, d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 if (result == 0) {
618 ep->driver_data = dev;
David Brownellca2bdf42007-08-02 12:20:05 -0700619 if (source_sink_start_ep(ep) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 dev->out_ep = ep;
621 continue;
622 }
David Brownell7472f382008-04-18 18:47:54 -0700623 usb_ep_disable(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 result = -EIO;
625 }
626
627 /* ignore any other endpoints */
628 } else
629 continue;
630
631 /* stop on error */
David Brownell7472f382008-04-18 18:47:54 -0700632 ERROR(dev, "can't start %s, result %d\n", ep->name, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 break;
634 }
635 if (result == 0)
David Brownell7472f382008-04-18 18:47:54 -0700636 DBG(dev, "buflen %d\n", buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
638 /* caller is responsible for cleanup on error */
639 return result;
640}
641
642/*-------------------------------------------------------------------------*/
643
David Brownell7472f382008-04-18 18:47:54 -0700644static void loopback_complete(struct usb_ep *ep, struct usb_request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645{
646 struct zero_dev *dev = ep->driver_data;
647 int status = req->status;
648
649 switch (status) {
650
David Brownellca2bdf42007-08-02 12:20:05 -0700651 case 0: /* normal completion? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 if (ep == dev->out_ep) {
653 /* loop this OUT packet back IN to the host */
654 req->zero = (req->actual < req->length);
655 req->length = req->actual;
David Brownell7472f382008-04-18 18:47:54 -0700656 status = usb_ep_queue(dev->in_ep, req, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 if (status == 0)
658 return;
659
660 /* "should never get here" */
David Brownell7472f382008-04-18 18:47:54 -0700661 ERROR(dev, "can't loop %s to %s: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 ep->name, dev->in_ep->name,
663 status);
664 }
665
666 /* queue the buffer for some later OUT packet */
667 req->length = buflen;
David Brownell7472f382008-04-18 18:47:54 -0700668 status = usb_ep_queue(dev->out_ep, req, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 if (status == 0)
670 return;
671
672 /* "should never get here" */
673 /* FALLTHROUGH */
674
675 default:
David Brownell7472f382008-04-18 18:47:54 -0700676 ERROR(dev, "%s loop complete --> %d, %d/%d\n", ep->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 status, req->actual, req->length);
678 /* FALLTHROUGH */
679
680 /* NOTE: since this driver doesn't maintain an explicit record
681 * of requests it submitted (just maintains qlen count), we
682 * rely on the hardware driver to clean up on disconnect or
683 * endpoint disable.
684 */
David Brownellca2bdf42007-08-02 12:20:05 -0700685 case -ECONNABORTED: /* hardware forced ep reset */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 case -ECONNRESET: /* request dequeued */
687 case -ESHUTDOWN: /* disconnect from host */
David Brownell7472f382008-04-18 18:47:54 -0700688 free_ep_req(ep, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 return;
690 }
691}
692
David Brownellca2bdf42007-08-02 12:20:05 -0700693static int set_loopback_config(struct zero_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694{
695 int result = 0;
696 struct usb_ep *ep;
697 struct usb_gadget *gadget = dev->gadget;
698
David Brownell7472f382008-04-18 18:47:54 -0700699 gadget_for_each_ep(ep, gadget) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 const struct usb_endpoint_descriptor *d;
701
702 /* one endpoint writes data back IN to the host */
David Brownell7472f382008-04-18 18:47:54 -0700703 if (strcmp(ep->name, EP_IN_NAME) == 0) {
704 d = ep_desc(gadget, &hs_source_desc, &fs_source_desc);
705 result = usb_ep_enable(ep, d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 if (result == 0) {
707 ep->driver_data = dev;
708 dev->in_ep = ep;
709 continue;
710 }
711
712 /* one endpoint just reads OUT packets */
David Brownell7472f382008-04-18 18:47:54 -0700713 } else if (strcmp(ep->name, EP_OUT_NAME) == 0) {
714 d = ep_desc(gadget, &hs_sink_desc, &fs_sink_desc);
715 result = usb_ep_enable(ep, d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 if (result == 0) {
717 ep->driver_data = dev;
718 dev->out_ep = ep;
719 continue;
720 }
721
722 /* ignore any other endpoints */
723 } else
724 continue;
725
726 /* stop on error */
David Brownell7472f382008-04-18 18:47:54 -0700727 ERROR(dev, "can't enable %s, result %d\n", ep->name, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 break;
729 }
730
731 /* allocate a bunch of read buffers and queue them all at once.
732 * we buffer at most 'qlen' transfers; fewer if any need more
733 * than 'buflen' bytes each.
734 */
735 if (result == 0) {
736 struct usb_request *req;
737 unsigned i;
738
739 ep = dev->out_ep;
740 for (i = 0; i < qlen && result == 0; i++) {
David Brownell7472f382008-04-18 18:47:54 -0700741 req = alloc_ep_req(ep, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 if (req) {
743 req->complete = loopback_complete;
David Brownell7472f382008-04-18 18:47:54 -0700744 result = usb_ep_queue(ep, req, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 if (result)
David Brownell7472f382008-04-18 18:47:54 -0700746 DBG(dev, "%s queue req --> %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 ep->name, result);
748 } else
749 result = -ENOMEM;
750 }
751 }
752 if (result == 0)
David Brownell7472f382008-04-18 18:47:54 -0700753 DBG(dev, "qlen %d, buflen %d\n", qlen, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
755 /* caller is responsible for cleanup on error */
756 return result;
757}
758
759/*-------------------------------------------------------------------------*/
760
David Brownell7472f382008-04-18 18:47:54 -0700761static void zero_reset_config(struct zero_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762{
763 if (dev->config == 0)
764 return;
765
David Brownell7472f382008-04-18 18:47:54 -0700766 DBG(dev, "reset config\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
768 /* just disable endpoints, forcing completion of pending i/o.
769 * all our completion handlers free their requests in this case.
770 */
771 if (dev->in_ep) {
David Brownell7472f382008-04-18 18:47:54 -0700772 usb_ep_disable(dev->in_ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 dev->in_ep = NULL;
774 }
775 if (dev->out_ep) {
David Brownell7472f382008-04-18 18:47:54 -0700776 usb_ep_disable(dev->out_ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 dev->out_ep = NULL;
778 }
779 dev->config = 0;
David Brownell7472f382008-04-18 18:47:54 -0700780 del_timer(&dev->resume);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781}
782
783/* change our operational config. this code must agree with the code
784 * that returns config descriptors, and altsetting code.
785 *
786 * it's also responsible for power management interactions. some
787 * configurations might not work with our current power sources.
788 *
789 * note that some device controller hardware will constrain what this
790 * code can do, perhaps by disallowing more than one configuration or
791 * by limiting configuration choices (like the pxa2xx).
792 */
David Brownellca2bdf42007-08-02 12:20:05 -0700793static int zero_set_config(struct zero_dev *dev, unsigned number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794{
795 int result = 0;
796 struct usb_gadget *gadget = dev->gadget;
797
798 if (number == dev->config)
799 return 0;
800
David Brownell7472f382008-04-18 18:47:54 -0700801 if (gadget_is_sa1100(gadget) && dev->config) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 /* tx fifo is full, but we can't clear it...*/
David Brownellca2bdf42007-08-02 12:20:05 -0700803 ERROR(dev, "can't change configurations\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 return -ESPIPE;
805 }
David Brownell7472f382008-04-18 18:47:54 -0700806 zero_reset_config(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
808 switch (number) {
809 case CONFIG_SOURCE_SINK:
David Brownellca2bdf42007-08-02 12:20:05 -0700810 result = set_source_sink_config(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 break;
812 case CONFIG_LOOPBACK:
David Brownellca2bdf42007-08-02 12:20:05 -0700813 result = set_loopback_config(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 break;
815 default:
816 result = -EINVAL;
817 /* FALL THROUGH */
818 case 0:
819 return result;
820 }
821
822 if (!result && (!dev->in_ep || !dev->out_ep))
823 result = -ENODEV;
824 if (result)
David Brownell7472f382008-04-18 18:47:54 -0700825 zero_reset_config(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 else {
827 char *speed;
828
829 switch (gadget->speed) {
830 case USB_SPEED_LOW: speed = "low"; break;
831 case USB_SPEED_FULL: speed = "full"; break;
832 case USB_SPEED_HIGH: speed = "high"; break;
David Brownellca2bdf42007-08-02 12:20:05 -0700833 default: speed = "?"; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 }
835
836 dev->config = number;
David Brownell7472f382008-04-18 18:47:54 -0700837 INFO(dev, "%s speed config #%d: %s\n", speed, number,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 (number == CONFIG_SOURCE_SINK)
839 ? source_sink : loopback);
840 }
841 return result;
842}
843
844/*-------------------------------------------------------------------------*/
845
David Brownell7472f382008-04-18 18:47:54 -0700846static void zero_setup_complete(struct usb_ep *ep, struct usb_request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
848 if (req->status || req->actual != req->length)
David Brownell7472f382008-04-18 18:47:54 -0700849 DBG((struct zero_dev *) ep->driver_data,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 "setup complete --> %d, %d/%d\n",
851 req->status, req->actual, req->length);
852}
853
854/*
855 * The setup() callback implements all the ep0 functionality that's
856 * not handled lower down, in hardware or the hardware driver (like
857 * device and endpoint feature flags, and their status). It's all
858 * housekeeping for the gadget function we're implementing. Most of
859 * the work is in config-specific setup.
860 */
861static int
David Brownell7472f382008-04-18 18:47:54 -0700862zero_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863{
David Brownell7472f382008-04-18 18:47:54 -0700864 struct zero_dev *dev = get_gadget_data(gadget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 struct usb_request *req = dev->req;
866 int value = -EOPNOTSUPP;
David Brownell1bbc1692005-05-07 13:05:13 -0700867 u16 w_index = le16_to_cpu(ctrl->wIndex);
868 u16 w_value = le16_to_cpu(ctrl->wValue);
869 u16 w_length = le16_to_cpu(ctrl->wLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
871 /* usually this stores reply data in the pre-allocated ep0 buffer,
872 * but config change events will reconfigure hardware.
873 */
874 req->zero = 0;
875 switch (ctrl->bRequest) {
876
877 case USB_REQ_GET_DESCRIPTOR:
878 if (ctrl->bRequestType != USB_DIR_IN)
879 goto unknown;
880 switch (w_value >> 8) {
881
882 case USB_DT_DEVICE:
David Brownell7472f382008-04-18 18:47:54 -0700883 value = min(w_length, (u16) sizeof device_desc);
884 memcpy(req->buf, &device_desc, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 case USB_DT_DEVICE_QUALIFIER:
David Brownellca2bdf42007-08-02 12:20:05 -0700887 if (!gadget_is_dualspeed(gadget))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 break;
David Brownell7472f382008-04-18 18:47:54 -0700889 value = min(w_length, (u16) sizeof dev_qualifier);
890 memcpy(req->buf, &dev_qualifier, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 break;
892
893 case USB_DT_OTHER_SPEED_CONFIG:
David Brownellca2bdf42007-08-02 12:20:05 -0700894 if (!gadget_is_dualspeed(gadget))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 break;
896 // FALLTHROUGH
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 case USB_DT_CONFIG:
David Brownell7472f382008-04-18 18:47:54 -0700898 value = config_buf(gadget, req->buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 w_value >> 8,
900 w_value & 0xff);
901 if (value >= 0)
David Brownell7472f382008-04-18 18:47:54 -0700902 value = min(w_length, (u16) value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 break;
904
905 case USB_DT_STRING:
906 /* wIndex == language code.
907 * this driver only handles one language, you can
908 * add string tables for other languages, using
909 * any UTF-8 characters
910 */
David Brownell7472f382008-04-18 18:47:54 -0700911 value = usb_gadget_get_string(&stringtab,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 w_value & 0xff, req->buf);
913 if (value >= 0)
David Brownell7472f382008-04-18 18:47:54 -0700914 value = min(w_length, (u16) value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 break;
916 }
917 break;
918
919 /* currently two configs, two speeds */
920 case USB_REQ_SET_CONFIGURATION:
921 if (ctrl->bRequestType != 0)
922 goto unknown;
923 if (gadget->a_hnp_support)
David Brownell7472f382008-04-18 18:47:54 -0700924 DBG(dev, "HNP available\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 else if (gadget->a_alt_hnp_support)
David Brownell7472f382008-04-18 18:47:54 -0700926 DBG(dev, "HNP needs a different root port\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 else
David Brownell7472f382008-04-18 18:47:54 -0700928 VDBG(dev, "HNP inactive\n");
929 spin_lock(&dev->lock);
David Brownellca2bdf42007-08-02 12:20:05 -0700930 value = zero_set_config(dev, w_value);
David Brownell7472f382008-04-18 18:47:54 -0700931 spin_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 break;
933 case USB_REQ_GET_CONFIGURATION:
934 if (ctrl->bRequestType != USB_DIR_IN)
935 goto unknown;
936 *(u8 *)req->buf = dev->config;
David Brownell7472f382008-04-18 18:47:54 -0700937 value = min(w_length, (u16) 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 break;
939
940 /* until we add altsetting support, or other interfaces,
941 * only 0/0 are possible. pxa2xx only supports 0/0 (poorly)
942 * and already killed pending endpoint I/O.
943 */
944 case USB_REQ_SET_INTERFACE:
945 if (ctrl->bRequestType != USB_RECIP_INTERFACE)
946 goto unknown;
David Brownell7472f382008-04-18 18:47:54 -0700947 spin_lock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 if (dev->config && w_index == 0 && w_value == 0) {
949 u8 config = dev->config;
950
951 /* resets interface configuration, forgets about
952 * previous transaction state (queued bufs, etc)
953 * and re-inits endpoint state (toggle etc)
954 * no response queued, just zero status == success.
955 * if we had more than one interface we couldn't
956 * use this "reset the config" shortcut.
957 */
David Brownell7472f382008-04-18 18:47:54 -0700958 zero_reset_config(dev);
David Brownellca2bdf42007-08-02 12:20:05 -0700959 zero_set_config(dev, config);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 value = 0;
961 }
David Brownell7472f382008-04-18 18:47:54 -0700962 spin_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 break;
964 case USB_REQ_GET_INTERFACE:
965 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
966 goto unknown;
967 if (!dev->config)
968 break;
969 if (w_index != 0) {
970 value = -EDOM;
971 break;
972 }
973 *(u8 *)req->buf = 0;
David Brownell7472f382008-04-18 18:47:54 -0700974 value = min(w_length, (u16) 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 break;
976
977 /*
978 * These are the same vendor-specific requests supported by
979 * Intel's USB 2.0 compliance test devices. We exceed that
980 * device spec by allowing multiple-packet requests.
981 */
982 case 0x5b: /* control WRITE test -- fill the buffer */
983 if (ctrl->bRequestType != (USB_DIR_OUT|USB_TYPE_VENDOR))
984 goto unknown;
985 if (w_value || w_index)
986 break;
987 /* just read that many bytes into the buffer */
988 if (w_length > USB_BUFSIZ)
989 break;
990 value = w_length;
991 break;
992 case 0x5c: /* control READ test -- return the buffer */
993 if (ctrl->bRequestType != (USB_DIR_IN|USB_TYPE_VENDOR))
994 goto unknown;
995 if (w_value || w_index)
996 break;
997 /* expect those bytes are still in the buffer; send back */
998 if (w_length > USB_BUFSIZ
999 || w_length != req->length)
1000 break;
1001 value = w_length;
1002 break;
1003
1004 default:
1005unknown:
David Brownell7472f382008-04-18 18:47:54 -07001006 VDBG(dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 "unknown control req%02x.%02x v%04x i%04x l%d\n",
1008 ctrl->bRequestType, ctrl->bRequest,
1009 w_value, w_index, w_length);
1010 }
1011
1012 /* respond with data transfer before status phase? */
1013 if (value >= 0) {
1014 req->length = value;
1015 req->zero = value < w_length;
David Brownell7472f382008-04-18 18:47:54 -07001016 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 if (value < 0) {
David Brownell7472f382008-04-18 18:47:54 -07001018 DBG(dev, "ep_queue --> %d\n", value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 req->status = 0;
David Brownell7472f382008-04-18 18:47:54 -07001020 zero_setup_complete(gadget->ep0, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 }
1022 }
1023
1024 /* device either stalls (value < 0) or reports success */
1025 return value;
1026}
1027
David Brownell7472f382008-04-18 18:47:54 -07001028static void zero_disconnect(struct usb_gadget *gadget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029{
David Brownell7472f382008-04-18 18:47:54 -07001030 struct zero_dev *dev = get_gadget_data(gadget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 unsigned long flags;
1032
David Brownell7472f382008-04-18 18:47:54 -07001033 spin_lock_irqsave(&dev->lock, flags);
1034 zero_reset_config(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
1036 /* a more significant application might have some non-usb
1037 * activities to quiesce here, saving resources like power
1038 * or pushing the notification up a network stack.
1039 */
David Brownell7472f382008-04-18 18:47:54 -07001040 spin_unlock_irqrestore(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
1042 /* next we may get setup() calls to enumerate new connections;
1043 * or an unbind() during shutdown (including removing module).
1044 */
1045}
1046
David Brownell7472f382008-04-18 18:47:54 -07001047static void zero_autoresume(unsigned long _dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048{
1049 struct zero_dev *dev = (struct zero_dev *) _dev;
1050 int status;
1051
1052 /* normally the host would be woken up for something
1053 * more significant than just a timer firing...
1054 */
1055 if (dev->gadget->speed != USB_SPEED_UNKNOWN) {
David Brownell7472f382008-04-18 18:47:54 -07001056 status = usb_gadget_wakeup(dev->gadget);
1057 DBG(dev, "wakeup --> %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 }
1059}
1060
1061/*-------------------------------------------------------------------------*/
1062
David Brownell7472f382008-04-18 18:47:54 -07001063static void zero_unbind(struct usb_gadget *gadget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064{
David Brownell7472f382008-04-18 18:47:54 -07001065 struct zero_dev *dev = get_gadget_data(gadget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
David Brownell7472f382008-04-18 18:47:54 -07001067 DBG(dev, "unbind\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
1069 /* we've already been disconnected ... no i/o is active */
David Brownell69396dc2006-01-20 14:38:49 -08001070 if (dev->req) {
1071 dev->req->length = USB_BUFSIZ;
David Brownell7472f382008-04-18 18:47:54 -07001072 free_ep_req(gadget->ep0, dev->req);
David Brownell69396dc2006-01-20 14:38:49 -08001073 }
David Brownell7472f382008-04-18 18:47:54 -07001074 del_timer_sync(&dev->resume);
1075 kfree(dev);
1076 set_gadget_data(gadget, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077}
1078
David Brownell7472f382008-04-18 18:47:54 -07001079static int __init zero_bind(struct usb_gadget *gadget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080{
1081 struct zero_dev *dev;
1082 struct usb_ep *ep;
David Brownell91e79c92005-07-13 15:18:30 -07001083 int gcnum;
1084
1085 /* FIXME this can't yet work right with SH ... it has only
1086 * one configuration, numbered one.
1087 */
1088 if (gadget_is_sh(gadget))
1089 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
1091 /* Bulk-only drivers like this one SHOULD be able to
1092 * autoconfigure on any sane usb controller driver,
1093 * but there may also be important quirks to address.
1094 */
David Brownell7472f382008-04-18 18:47:54 -07001095 usb_ep_autoconfig_reset(gadget);
1096 ep = usb_ep_autoconfig(gadget, &fs_source_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 if (!ep) {
1098autoconf_fail:
David Brownell00274922007-11-19 12:58:36 -08001099 pr_err("%s: can't autoconfigure on %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 shortname, gadget->name);
1101 return -ENODEV;
1102 }
1103 EP_IN_NAME = ep->name;
1104 ep->driver_data = ep; /* claim */
David Brownellca2bdf42007-08-02 12:20:05 -07001105
David Brownell7472f382008-04-18 18:47:54 -07001106 ep = usb_ep_autoconfig(gadget, &fs_sink_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 if (!ep)
1108 goto autoconf_fail;
1109 EP_OUT_NAME = ep->name;
1110 ep->driver_data = ep; /* claim */
1111
David Brownell7472f382008-04-18 18:47:54 -07001112 gcnum = usb_gadget_controller_number(gadget);
David Brownell91e79c92005-07-13 15:18:30 -07001113 if (gcnum >= 0)
David Brownell7472f382008-04-18 18:47:54 -07001114 device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
David Brownell91e79c92005-07-13 15:18:30 -07001115 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 /* gadget zero is so simple (for now, no altsettings) that
1117 * it SHOULD NOT have problems with bulk-capable hardware.
1118 * so warn about unrcognized controllers, don't panic.
1119 *
1120 * things like configuration and altsetting numbering
1121 * can need hardware-specific attention though.
1122 */
David Brownell00274922007-11-19 12:58:36 -08001123 pr_warning("%s: controller '%s' not recognized\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 shortname, gadget->name);
David Brownell7472f382008-04-18 18:47:54 -07001125 device_desc.bcdDevice = __constant_cpu_to_le16(0x9999);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 }
1127
1128
1129 /* ok, we made sense of the hardware ... */
Christoph Lametere94b1762006-12-06 20:33:17 -08001130 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 if (!dev)
1132 return -ENOMEM;
David Brownell7472f382008-04-18 18:47:54 -07001133 spin_lock_init(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 dev->gadget = gadget;
David Brownell7472f382008-04-18 18:47:54 -07001135 set_gadget_data(gadget, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
1137 /* preallocate control response and buffer */
David Brownell7472f382008-04-18 18:47:54 -07001138 dev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 if (!dev->req)
1140 goto enomem;
David Brownell9d8bab52007-07-01 11:04:54 -07001141 dev->req->buf = kmalloc(USB_BUFSIZ, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 if (!dev->req->buf)
1143 goto enomem;
1144
1145 dev->req->complete = zero_setup_complete;
1146
1147 device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
1148
David Brownellca2bdf42007-08-02 12:20:05 -07001149 if (gadget_is_dualspeed(gadget)) {
1150 /* assume ep0 uses the same value for both speeds ... */
1151 dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
David Brownellca2bdf42007-08-02 12:20:05 -07001153 /* and that all endpoints are dual-speed */
1154 hs_source_desc.bEndpointAddress =
1155 fs_source_desc.bEndpointAddress;
1156 hs_sink_desc.bEndpointAddress =
1157 fs_sink_desc.bEndpointAddress;
1158 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
David Brownellca2bdf42007-08-02 12:20:05 -07001160 if (gadget_is_otg(gadget)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 otg_descriptor.bmAttributes |= USB_OTG_HNP,
1162 source_sink_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1163 loopback_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1164 }
1165
David Brownell7472f382008-04-18 18:47:54 -07001166 usb_gadget_set_selfpowered(gadget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
David Brownell7472f382008-04-18 18:47:54 -07001168 init_timer(&dev->resume);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 dev->resume.function = zero_autoresume;
1170 dev->resume.data = (unsigned long) dev;
1171 if (autoresume) {
1172 source_sink_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1173 loopback_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1174 }
1175
1176 gadget->ep0->driver_data = dev;
1177
David Brownell7472f382008-04-18 18:47:54 -07001178 INFO(dev, "%s, version: " DRIVER_VERSION "\n", longname);
1179 INFO(dev, "using %s, OUT %s IN %s\n", gadget->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 EP_OUT_NAME, EP_IN_NAME);
1181
David Brownell7472f382008-04-18 18:47:54 -07001182 snprintf(manufacturer, sizeof manufacturer, "%s %s with %s",
Serge E. Hallyn96b644b2006-10-02 02:18:13 -07001183 init_utsname()->sysname, init_utsname()->release,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 gadget->name);
1185
1186 return 0;
1187
1188enomem:
David Brownell7472f382008-04-18 18:47:54 -07001189 zero_unbind(gadget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 return -ENOMEM;
1191}
1192
1193/*-------------------------------------------------------------------------*/
1194
David Brownell7472f382008-04-18 18:47:54 -07001195static void zero_suspend(struct usb_gadget *gadget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196{
David Brownell7472f382008-04-18 18:47:54 -07001197 struct zero_dev *dev = get_gadget_data(gadget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
1199 if (gadget->speed == USB_SPEED_UNKNOWN)
1200 return;
1201
1202 if (autoresume) {
David Brownell7472f382008-04-18 18:47:54 -07001203 mod_timer(&dev->resume, jiffies + (HZ * autoresume));
1204 DBG(dev, "suspend, wakeup in %d seconds\n", autoresume);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 } else
David Brownell7472f382008-04-18 18:47:54 -07001206 DBG(dev, "suspend\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207}
1208
David Brownell7472f382008-04-18 18:47:54 -07001209static void zero_resume(struct usb_gadget *gadget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210{
David Brownell7472f382008-04-18 18:47:54 -07001211 struct zero_dev *dev = get_gadget_data(gadget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
David Brownell7472f382008-04-18 18:47:54 -07001213 DBG(dev, "resume\n");
1214 del_timer(&dev->resume);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215}
1216
1217
1218/*-------------------------------------------------------------------------*/
1219
1220static struct usb_gadget_driver zero_driver = {
1221#ifdef CONFIG_USB_GADGET_DUALSPEED
1222 .speed = USB_SPEED_HIGH,
1223#else
1224 .speed = USB_SPEED_FULL,
1225#endif
1226 .function = (char *) longname,
1227 .bind = zero_bind,
David Brownell329af282006-02-18 12:31:05 -08001228 .unbind = __exit_p(zero_unbind),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
1230 .setup = zero_setup,
1231 .disconnect = zero_disconnect,
1232
1233 .suspend = zero_suspend,
1234 .resume = zero_resume,
1235
David Brownellca2bdf42007-08-02 12:20:05 -07001236 .driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 .name = (char *) shortname,
Ben Dooksd0d50492005-10-10 10:52:33 +01001238 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 },
1240};
1241
David Brownellca2bdf42007-08-02 12:20:05 -07001242MODULE_AUTHOR("David Brownell");
1243MODULE_LICENSE("GPL");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
1245
David Brownell7472f382008-04-18 18:47:54 -07001246static int __init init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247{
David Brownell7472f382008-04-18 18:47:54 -07001248 return usb_gadget_register_driver(&zero_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249}
David Brownell7472f382008-04-18 18:47:54 -07001250module_init(init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
David Brownell7472f382008-04-18 18:47:54 -07001252static void __exit cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253{
David Brownell7472f382008-04-18 18:47:54 -07001254 usb_gadget_unregister_driver(&zero_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255}
David Brownell7472f382008-04-18 18:47:54 -07001256module_exit(cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257