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