Loading...
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 * 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.
16 *
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
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.
26 * Some hardware details are visible, but don't affect most of the driver.
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
33 * writes back, so the host can read it.
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).
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.
43 */
44
45/*
46 * driver assumes self-powered hardware, and
47 * has no way for users to trigger remote wakeup.
48 */
49
50/* #define VERBOSE_DEBUG */
51
52#include <linux/kernel.h>
53#include <linux/slab.h>
54#include <linux/utsname.h>
55#include <linux/device.h>
56
57#include "g_zero.h"
58#include "gadget_chips.h"
59
60
61/*-------------------------------------------------------------------------*/
62
63/*
64 * Kbuild is not very cooperative with respect to linking separately
65 * compiled library objects into one module. So for now we won't use
66 * separate compilation ... ensuring init/exit sections work to shrink
67 * the runtime footprint, and giving us at least some parts of what
68 * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
69 */
70#include "composite.c"
71#include "usbstring.c"
72#include "config.c"
73#include "epautoconf.c"
74
75#include "f_sourcesink.c"
76#include "f_loopback.c"
77
78/*-------------------------------------------------------------------------*/
79
80#define DRIVER_VERSION "Cinco de Mayo 2008"
81
82static const char longname[] = "Gadget Zero";
83
84unsigned buflen = 4096;
85module_param(buflen, uint, 0);
86
87/*
88 * Normally the "loopback" configuration is second (index 1) so
89 * it's not the default. Here's where to change that order, to
90 * work better with hosts where config changes are problematic or
91 * controllers (like original superh) that only support one config.
92 */
93static int loopdefault = 0;
94module_param(loopdefault, bool, S_IRUGO|S_IWUSR);
95
96/*-------------------------------------------------------------------------*/
97
98/* Thanks to NetChip Technologies for donating this product ID.
99 *
100 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
101 * Instead: allocate your own, using normal USB-IF procedures.
102 */
103#ifndef CONFIG_USB_ZERO_HNPTEST
104#define DRIVER_VENDOR_NUM 0x0525 /* NetChip */
105#define DRIVER_PRODUCT_NUM 0xa4a0 /* Linux-USB "Gadget Zero" */
106#define DEFAULT_AUTORESUME 0
107#else
108#define DRIVER_VENDOR_NUM 0x1a0a /* OTG test device IDs */
109#define DRIVER_PRODUCT_NUM 0xbadd
110#define DEFAULT_AUTORESUME 5
111#endif
112
113/* If the optional "autoresume" mode is enabled, it provides good
114 * functional coverage for the "USBCV" test harness from USB-IF.
115 * It's always set if OTG mode is enabled.
116 */
117unsigned autoresume = DEFAULT_AUTORESUME;
118module_param(autoresume, uint, S_IRUGO);
119MODULE_PARM_DESC(autoresume, "zero, or seconds before remote wakeup");
120
121/*-------------------------------------------------------------------------*/
122
123static struct usb_device_descriptor device_desc = {
124 .bLength = sizeof device_desc,
125 .bDescriptorType = USB_DT_DEVICE,
126
127 .bcdUSB = cpu_to_le16(0x0200),
128 .bDeviceClass = USB_CLASS_VENDOR_SPEC,
129
130 .idVendor = cpu_to_le16(DRIVER_VENDOR_NUM),
131 .idProduct = cpu_to_le16(DRIVER_PRODUCT_NUM),
132 .bNumConfigurations = 2,
133};
134
135#ifdef CONFIG_USB_OTG
136static struct usb_otg_descriptor otg_descriptor = {
137 .bLength = sizeof otg_descriptor,
138 .bDescriptorType = USB_DT_OTG,
139
140 /* REVISIT SRP-only hardware is possible, although
141 * it would not be called "OTG" ...
142 */
143 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
144};
145
146const struct usb_descriptor_header *otg_desc[] = {
147 (struct usb_descriptor_header *) &otg_descriptor,
148 NULL,
149};
150#endif
151
152/* string IDs are assigned dynamically */
153
154#define STRING_MANUFACTURER_IDX 0
155#define STRING_PRODUCT_IDX 1
156#define STRING_SERIAL_IDX 2
157
158static char manufacturer[50];
159
160/* default serial number takes at least two packets */
161static char serial[] = "0123456789.0123456789.0123456789";
162
163static struct usb_string strings_dev[] = {
164 [STRING_MANUFACTURER_IDX].s = manufacturer,
165 [STRING_PRODUCT_IDX].s = longname,
166 [STRING_SERIAL_IDX].s = serial,
167 { } /* end of list */
168};
169
170static struct usb_gadget_strings stringtab_dev = {
171 .language = 0x0409, /* en-us */
172 .strings = strings_dev,
173};
174
175static struct usb_gadget_strings *dev_strings[] = {
176 &stringtab_dev,
177 NULL,
178};
179
180/*-------------------------------------------------------------------------*/
181
182struct usb_request *alloc_ep_req(struct usb_ep *ep)
183{
184 struct usb_request *req;
185
186 req = usb_ep_alloc_request(ep, GFP_ATOMIC);
187 if (req) {
188 req->length = buflen;
189 req->buf = kmalloc(buflen, GFP_ATOMIC);
190 if (!req->buf) {
191 usb_ep_free_request(ep, req);
192 req = NULL;
193 }
194 }
195 return req;
196}
197
198void free_ep_req(struct usb_ep *ep, struct usb_request *req)
199{
200 kfree(req->buf);
201 usb_ep_free_request(ep, req);
202}
203
204static void disable_ep(struct usb_composite_dev *cdev, struct usb_ep *ep)
205{
206 int value;
207
208 if (ep->driver_data) {
209 value = usb_ep_disable(ep);
210 if (value < 0)
211 DBG(cdev, "disable %s --> %d\n",
212 ep->name, value);
213 ep->driver_data = NULL;
214 }
215}
216
217void disable_endpoints(struct usb_composite_dev *cdev,
218 struct usb_ep *in, struct usb_ep *out)
219{
220 disable_ep(cdev, in);
221 disable_ep(cdev, out);
222}
223
224/*-------------------------------------------------------------------------*/
225
226static struct timer_list autoresume_timer;
227
228static void zero_autoresume(unsigned long _c)
229{
230 struct usb_composite_dev *cdev = (void *)_c;
231 struct usb_gadget *g = cdev->gadget;
232
233 /* unconfigured devices can't issue wakeups */
234 if (!cdev->config)
235 return;
236
237 /* Normally the host would be woken up for something
238 * more significant than just a timer firing; likely
239 * because of some direct user request.
240 */
241 if (g->speed != USB_SPEED_UNKNOWN) {
242 int status = usb_gadget_wakeup(g);
243 INFO(cdev, "%s --> %d\n", __func__, status);
244 }
245}
246
247static void zero_suspend(struct usb_composite_dev *cdev)
248{
249 if (cdev->gadget->speed == USB_SPEED_UNKNOWN)
250 return;
251
252 if (autoresume) {
253 mod_timer(&autoresume_timer, jiffies + (HZ * autoresume));
254 DBG(cdev, "suspend, wakeup in %d seconds\n", autoresume);
255 } else
256 DBG(cdev, "%s\n", __func__);
257}
258
259static void zero_resume(struct usb_composite_dev *cdev)
260{
261 DBG(cdev, "%s\n", __func__);
262 del_timer(&autoresume_timer);
263}
264
265/*-------------------------------------------------------------------------*/
266
267static int __init zero_bind(struct usb_composite_dev *cdev)
268{
269 int gcnum;
270 struct usb_gadget *gadget = cdev->gadget;
271 int id;
272
273 /* Allocate string descriptor numbers ... note that string
274 * contents can be overridden by the composite_dev glue.
275 */
276 id = usb_string_id(cdev);
277 if (id < 0)
278 return id;
279 strings_dev[STRING_MANUFACTURER_IDX].id = id;
280 device_desc.iManufacturer = id;
281
282 id = usb_string_id(cdev);
283 if (id < 0)
284 return id;
285 strings_dev[STRING_PRODUCT_IDX].id = id;
286 device_desc.iProduct = id;
287
288 id = usb_string_id(cdev);
289 if (id < 0)
290 return id;
291 strings_dev[STRING_SERIAL_IDX].id = id;
292 device_desc.iSerialNumber = id;
293
294 setup_timer(&autoresume_timer, zero_autoresume, (unsigned long) cdev);
295
296 /* Register primary, then secondary configuration. Note that
297 * SH3 only allows one config...
298 */
299 if (loopdefault) {
300 loopback_add(cdev, autoresume != 0);
301 sourcesink_add(cdev, autoresume != 0);
302 } else {
303 sourcesink_add(cdev, autoresume != 0);
304 loopback_add(cdev, autoresume != 0);
305 }
306
307 gcnum = usb_gadget_controller_number(gadget);
308 if (gcnum >= 0)
309 device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
310 else {
311 /* gadget zero is so simple (for now, no altsettings) that
312 * it SHOULD NOT have problems with bulk-capable hardware.
313 * so just warn about unrcognized controllers -- don't panic.
314 *
315 * things like configuration and altsetting numbering
316 * can need hardware-specific attention though.
317 */
318 pr_warning("%s: controller '%s' not recognized\n",
319 longname, gadget->name);
320 device_desc.bcdDevice = cpu_to_le16(0x9999);
321 }
322
323
324 INFO(cdev, "%s, version: " DRIVER_VERSION "\n", longname);
325
326 snprintf(manufacturer, sizeof manufacturer, "%s %s with %s",
327 init_utsname()->sysname, init_utsname()->release,
328 gadget->name);
329
330 return 0;
331}
332
333static int zero_unbind(struct usb_composite_dev *cdev)
334{
335 del_timer_sync(&autoresume_timer);
336 return 0;
337}
338
339static struct usb_composite_driver zero_driver = {
340 .name = "zero",
341 .dev = &device_desc,
342 .strings = dev_strings,
343 .max_speed = USB_SPEED_SUPER,
344 .unbind = zero_unbind,
345 .suspend = zero_suspend,
346 .resume = zero_resume,
347};
348
349MODULE_AUTHOR("David Brownell");
350MODULE_LICENSE("GPL");
351
352static int __init init(void)
353{
354 return usb_composite_probe(&zero_driver, zero_bind);
355}
356module_init(init);
357
358static void __exit cleanup(void)
359{
360 usb_composite_unregister(&zero_driver);
361}
362module_exit(cleanup);
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 infrastucture 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/*-------------------------------------------------------------------------*/
51USB_GADGET_COMPOSITE_OPTIONS();
52
53#define DRIVER_VERSION "Cinco de Mayo 2008"
54
55static 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 */
63static bool loopdefault = 0;
64module_param(loopdefault, bool, S_IRUGO|S_IWUSR);
65
66static 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 */
94static unsigned autoresume = DEFAULT_AUTORESUME;
95module_param(autoresume, uint, S_IRUGO);
96MODULE_PARM_DESC(autoresume, "zero, or seconds before remote wakeup");
97
98/* Maximum Autoresume time */
99static unsigned max_autoresume;
100module_param(max_autoresume, uint, S_IRUGO);
101MODULE_PARM_DESC(max_autoresume, "maximum seconds before remote wakeup");
102
103/* Interval between two remote wakeups */
104static unsigned autoresume_interval_ms;
105module_param(autoresume_interval_ms, uint, S_IRUGO);
106MODULE_PARM_DESC(autoresume_interval_ms,
107 "milliseconds to increase successive wakeup delays");
108
109static unsigned autoresume_step_ms;
110/*-------------------------------------------------------------------------*/
111
112static 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#ifdef CONFIG_USB_OTG
125static struct usb_otg_descriptor otg_descriptor = {
126 .bLength = sizeof otg_descriptor,
127 .bDescriptorType = USB_DT_OTG,
128
129 /* REVISIT SRP-only hardware is possible, although
130 * it would not be called "OTG" ...
131 */
132 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
133};
134
135static const struct usb_descriptor_header *otg_desc[] = {
136 (struct usb_descriptor_header *) &otg_descriptor,
137 NULL,
138};
139#else
140#define otg_desc NULL
141#endif
142
143/* string IDs are assigned dynamically */
144/* default serial number takes at least two packets */
145static char serial[] = "0123456789.0123456789.0123456789";
146
147#define USB_GZERO_SS_DESC (USB_GADGET_FIRST_AVAIL_IDX + 0)
148#define USB_GZERO_LB_DESC (USB_GADGET_FIRST_AVAIL_IDX + 1)
149
150static struct usb_string strings_dev[] = {
151 [USB_GADGET_MANUFACTURER_IDX].s = "",
152 [USB_GADGET_PRODUCT_IDX].s = longname,
153 [USB_GADGET_SERIAL_IDX].s = serial,
154 [USB_GZERO_SS_DESC].s = "source and sink data",
155 [USB_GZERO_LB_DESC].s = "loop input to output",
156 { } /* end of list */
157};
158
159static struct usb_gadget_strings stringtab_dev = {
160 .language = 0x0409, /* en-us */
161 .strings = strings_dev,
162};
163
164static struct usb_gadget_strings *dev_strings[] = {
165 &stringtab_dev,
166 NULL,
167};
168
169/*-------------------------------------------------------------------------*/
170
171static struct timer_list autoresume_timer;
172
173static void zero_autoresume(unsigned long _c)
174{
175 struct usb_composite_dev *cdev = (void *)_c;
176 struct usb_gadget *g = cdev->gadget;
177
178 /* unconfigured devices can't issue wakeups */
179 if (!cdev->config)
180 return;
181
182 /* Normally the host would be woken up for something
183 * more significant than just a timer firing; likely
184 * because of some direct user request.
185 */
186 if (g->speed != USB_SPEED_UNKNOWN) {
187 int status = usb_gadget_wakeup(g);
188 INFO(cdev, "%s --> %d\n", __func__, status);
189 }
190}
191
192static void zero_suspend(struct usb_composite_dev *cdev)
193{
194 if (cdev->gadget->speed == USB_SPEED_UNKNOWN)
195 return;
196
197 if (autoresume) {
198 if (max_autoresume &&
199 (autoresume_step_ms > max_autoresume * 1000))
200 autoresume_step_ms = autoresume * 1000;
201
202 mod_timer(&autoresume_timer, jiffies +
203 msecs_to_jiffies(autoresume_step_ms));
204 DBG(cdev, "suspend, wakeup in %d milliseconds\n",
205 autoresume_step_ms);
206
207 autoresume_step_ms += autoresume_interval_ms;
208 } else
209 DBG(cdev, "%s\n", __func__);
210}
211
212static void zero_resume(struct usb_composite_dev *cdev)
213{
214 DBG(cdev, "%s\n", __func__);
215 del_timer(&autoresume_timer);
216}
217
218/*-------------------------------------------------------------------------*/
219
220static struct usb_configuration loopback_driver = {
221 .label = "loopback",
222 .bConfigurationValue = 2,
223 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
224 /* .iConfiguration = DYNAMIC */
225};
226
227static struct usb_function *func_ss;
228static struct usb_function_instance *func_inst_ss;
229
230static int ss_config_setup(struct usb_configuration *c,
231 const struct usb_ctrlrequest *ctrl)
232{
233 switch (ctrl->bRequest) {
234 case 0x5b:
235 case 0x5c:
236 return func_ss->setup(func_ss, ctrl);
237 default:
238 return -EOPNOTSUPP;
239 }
240}
241
242static struct usb_configuration sourcesink_driver = {
243 .label = "source/sink",
244 .setup = ss_config_setup,
245 .bConfigurationValue = 3,
246 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
247 /* .iConfiguration = DYNAMIC */
248};
249
250module_param_named(buflen, gzero_options.bulk_buflen, uint, 0);
251module_param_named(pattern, gzero_options.pattern, uint, S_IRUGO|S_IWUSR);
252MODULE_PARM_DESC(pattern, "0 = all zeroes, 1 = mod63, 2 = none");
253
254module_param_named(isoc_interval, gzero_options.isoc_interval, uint,
255 S_IRUGO|S_IWUSR);
256MODULE_PARM_DESC(isoc_interval, "1 - 16");
257
258module_param_named(isoc_maxpacket, gzero_options.isoc_maxpacket, uint,
259 S_IRUGO|S_IWUSR);
260MODULE_PARM_DESC(isoc_maxpacket, "0 - 1023 (fs), 0 - 1024 (hs/ss)");
261
262module_param_named(isoc_mult, gzero_options.isoc_mult, uint, S_IRUGO|S_IWUSR);
263MODULE_PARM_DESC(isoc_mult, "0 - 2 (hs/ss only)");
264
265module_param_named(isoc_maxburst, gzero_options.isoc_maxburst, uint,
266 S_IRUGO|S_IWUSR);
267MODULE_PARM_DESC(isoc_maxburst, "0 - 15 (ss only)");
268
269static struct usb_function *func_lb;
270static struct usb_function_instance *func_inst_lb;
271
272module_param_named(qlen, gzero_options.qlen, uint, S_IRUGO|S_IWUSR);
273MODULE_PARM_DESC(qlen, "depth of loopback queue");
274
275static int __init zero_bind(struct usb_composite_dev *cdev)
276{
277 struct f_ss_opts *ss_opts;
278 struct f_lb_opts *lb_opts;
279 int status;
280
281 /* Allocate string descriptor numbers ... note that string
282 * contents can be overridden by the composite_dev glue.
283 */
284 status = usb_string_ids_tab(cdev, strings_dev);
285 if (status < 0)
286 return status;
287
288 device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
289 device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
290 device_desc.iSerialNumber = strings_dev[USB_GADGET_SERIAL_IDX].id;
291
292 setup_timer(&autoresume_timer, zero_autoresume, (unsigned long) cdev);
293
294 func_inst_ss = usb_get_function_instance("SourceSink");
295 if (IS_ERR(func_inst_ss))
296 return PTR_ERR(func_inst_ss);
297
298 ss_opts = container_of(func_inst_ss, struct f_ss_opts, func_inst);
299 ss_opts->pattern = gzero_options.pattern;
300 ss_opts->isoc_interval = gzero_options.isoc_interval;
301 ss_opts->isoc_maxpacket = gzero_options.isoc_maxpacket;
302 ss_opts->isoc_mult = gzero_options.isoc_mult;
303 ss_opts->isoc_maxburst = gzero_options.isoc_maxburst;
304 ss_opts->bulk_buflen = gzero_options.bulk_buflen;
305
306 func_ss = usb_get_function(func_inst_ss);
307 if (IS_ERR(func_ss)) {
308 status = PTR_ERR(func_ss);
309 goto err_put_func_inst_ss;
310 }
311
312 func_inst_lb = usb_get_function_instance("Loopback");
313 if (IS_ERR(func_inst_lb)) {
314 status = PTR_ERR(func_inst_lb);
315 goto err_put_func_ss;
316 }
317
318 lb_opts = container_of(func_inst_lb, struct f_lb_opts, func_inst);
319 lb_opts->bulk_buflen = gzero_options.bulk_buflen;
320 lb_opts->qlen = gzero_options.qlen;
321
322 func_lb = usb_get_function(func_inst_lb);
323 if (IS_ERR(func_lb)) {
324 status = PTR_ERR(func_lb);
325 goto err_put_func_inst_lb;
326 }
327
328 sourcesink_driver.iConfiguration = strings_dev[USB_GZERO_SS_DESC].id;
329 loopback_driver.iConfiguration = strings_dev[USB_GZERO_LB_DESC].id;
330
331 /* support autoresume for remote wakeup testing */
332 sourcesink_driver.bmAttributes &= ~USB_CONFIG_ATT_WAKEUP;
333 loopback_driver.bmAttributes &= ~USB_CONFIG_ATT_WAKEUP;
334 sourcesink_driver.descriptors = NULL;
335 loopback_driver.descriptors = NULL;
336 if (autoresume) {
337 sourcesink_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
338 loopback_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
339 autoresume_step_ms = autoresume * 1000;
340 }
341
342 /* support OTG systems */
343 if (gadget_is_otg(cdev->gadget)) {
344 sourcesink_driver.descriptors = otg_desc;
345 sourcesink_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
346 loopback_driver.descriptors = otg_desc;
347 loopback_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
348 }
349
350 /* Register primary, then secondary configuration. Note that
351 * SH3 only allows one config...
352 */
353 if (loopdefault) {
354 usb_add_config_only(cdev, &loopback_driver);
355 usb_add_config_only(cdev, &sourcesink_driver);
356 } else {
357 usb_add_config_only(cdev, &sourcesink_driver);
358 usb_add_config_only(cdev, &loopback_driver);
359 }
360 status = usb_add_function(&sourcesink_driver, func_ss);
361 if (status)
362 goto err_conf_flb;
363
364 usb_ep_autoconfig_reset(cdev->gadget);
365 status = usb_add_function(&loopback_driver, func_lb);
366 if (status)
367 goto err_conf_flb;
368
369 usb_ep_autoconfig_reset(cdev->gadget);
370 usb_composite_overwrite_options(cdev, &coverwrite);
371
372 INFO(cdev, "%s, version: " DRIVER_VERSION "\n", longname);
373
374 return 0;
375
376err_conf_flb:
377 usb_put_function(func_lb);
378 func_lb = NULL;
379err_put_func_inst_lb:
380 usb_put_function_instance(func_inst_lb);
381 func_inst_lb = NULL;
382err_put_func_ss:
383 usb_put_function(func_ss);
384 func_ss = NULL;
385err_put_func_inst_ss:
386 usb_put_function_instance(func_inst_ss);
387 func_inst_ss = NULL;
388 return status;
389}
390
391static int zero_unbind(struct usb_composite_dev *cdev)
392{
393 del_timer_sync(&autoresume_timer);
394 if (!IS_ERR_OR_NULL(func_ss))
395 usb_put_function(func_ss);
396 usb_put_function_instance(func_inst_ss);
397 if (!IS_ERR_OR_NULL(func_lb))
398 usb_put_function(func_lb);
399 usb_put_function_instance(func_inst_lb);
400 return 0;
401}
402
403static __refdata struct usb_composite_driver zero_driver = {
404 .name = "zero",
405 .dev = &device_desc,
406 .strings = dev_strings,
407 .max_speed = USB_SPEED_SUPER,
408 .bind = zero_bind,
409 .unbind = zero_unbind,
410 .suspend = zero_suspend,
411 .resume = zero_resume,
412};
413
414MODULE_AUTHOR("David Brownell");
415MODULE_LICENSE("GPL");
416
417static int __init init(void)
418{
419 return usb_composite_probe(&zero_driver);
420}
421module_init(init);
422
423static void __exit cleanup(void)
424{
425 usb_composite_unregister(&zero_driver);
426}
427module_exit(cleanup);