treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / usb / gadget / legacy / zero.c
blob6e84b44c8a3bca144886203e2f745e4057043dc8
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * zero.c -- Gadget Zero, for USB development
5 * Copyright (C) 2003-2008 David Brownell
6 * Copyright (C) 2008 by Nokia Corporation
7 */
9 /*
10 * Gadget Zero only needs two bulk endpoints, and is an example of how you
11 * can write a hardware-agnostic gadget driver running inside a USB device.
12 * Some hardware details are visible, but don't affect most of the driver.
14 * Use it with the Linux host/master side "usbtest" driver to get a basic
15 * functional test of your device-side usb stack, or with "usb-skeleton".
17 * It supports two similar configurations. One sinks whatever the usb host
18 * writes, and in return sources zeroes. The other loops whatever the host
19 * writes back, so the host can read it.
21 * Many drivers will only have one configuration, letting them be much
22 * simpler if they also don't support high speed operation (like this
23 * driver does).
25 * Why is *this* driver using two configurations, rather than setting up
26 * two interfaces with different functions? To help verify that multiple
27 * configuration infrastructure is working correctly; also, so that it can
28 * work with low capability USB controllers without four bulk endpoints.
32 * driver assumes self-powered hardware, and
33 * has no way for users to trigger remote wakeup.
36 /* #define VERBOSE_DEBUG */
38 #include <linux/kernel.h>
39 #include <linux/slab.h>
40 #include <linux/device.h>
41 #include <linux/module.h>
42 #include <linux/err.h>
43 #include <linux/usb/composite.h>
45 #include "g_zero.h"
46 /*-------------------------------------------------------------------------*/
47 USB_GADGET_COMPOSITE_OPTIONS();
49 #define DRIVER_VERSION "Cinco de Mayo 2008"
51 static const char longname[] = "Gadget Zero";
54 * Normally the "loopback" configuration is second (index 1) so
55 * it's not the default. Here's where to change that order, to
56 * work better with hosts where config changes are problematic or
57 * controllers (like original superh) that only support one config.
59 static bool loopdefault = 0;
60 module_param(loopdefault, bool, S_IRUGO|S_IWUSR);
62 static struct usb_zero_options gzero_options = {
63 .isoc_interval = GZERO_ISOC_INTERVAL,
64 .isoc_maxpacket = GZERO_ISOC_MAXPACKET,
65 .bulk_buflen = GZERO_BULK_BUFLEN,
66 .qlen = GZERO_QLEN,
67 .ss_bulk_qlen = GZERO_SS_BULK_QLEN,
68 .ss_iso_qlen = GZERO_SS_ISO_QLEN,
71 /*-------------------------------------------------------------------------*/
73 /* Thanks to NetChip Technologies for donating this product ID.
75 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
76 * Instead: allocate your own, using normal USB-IF procedures.
78 #ifndef CONFIG_USB_ZERO_HNPTEST
79 #define DRIVER_VENDOR_NUM 0x0525 /* NetChip */
80 #define DRIVER_PRODUCT_NUM 0xa4a0 /* Linux-USB "Gadget Zero" */
81 #define DEFAULT_AUTORESUME 0
82 #else
83 #define DRIVER_VENDOR_NUM 0x1a0a /* OTG test device IDs */
84 #define DRIVER_PRODUCT_NUM 0xbadd
85 #define DEFAULT_AUTORESUME 5
86 #endif
88 /* If the optional "autoresume" mode is enabled, it provides good
89 * functional coverage for the "USBCV" test harness from USB-IF.
90 * It's always set if OTG mode is enabled.
92 static unsigned autoresume = DEFAULT_AUTORESUME;
93 module_param(autoresume, uint, S_IRUGO);
94 MODULE_PARM_DESC(autoresume, "zero, or seconds before remote wakeup");
96 /* Maximum Autoresume time */
97 static unsigned max_autoresume;
98 module_param(max_autoresume, uint, S_IRUGO);
99 MODULE_PARM_DESC(max_autoresume, "maximum seconds before remote wakeup");
101 /* Interval between two remote wakeups */
102 static unsigned autoresume_interval_ms;
103 module_param(autoresume_interval_ms, uint, S_IRUGO);
104 MODULE_PARM_DESC(autoresume_interval_ms,
105 "milliseconds to increase successive wakeup delays");
107 static unsigned autoresume_step_ms;
108 /*-------------------------------------------------------------------------*/
110 static struct usb_device_descriptor device_desc = {
111 .bLength = sizeof device_desc,
112 .bDescriptorType = USB_DT_DEVICE,
114 /* .bcdUSB = DYNAMIC */
115 .bDeviceClass = USB_CLASS_VENDOR_SPEC,
117 .idVendor = cpu_to_le16(DRIVER_VENDOR_NUM),
118 .idProduct = cpu_to_le16(DRIVER_PRODUCT_NUM),
119 .bNumConfigurations = 2,
122 static const struct usb_descriptor_header *otg_desc[2];
124 /* string IDs are assigned dynamically */
125 /* default serial number takes at least two packets */
126 static char serial[] = "0123456789.0123456789.0123456789";
128 #define USB_GZERO_SS_DESC (USB_GADGET_FIRST_AVAIL_IDX + 0)
129 #define USB_GZERO_LB_DESC (USB_GADGET_FIRST_AVAIL_IDX + 1)
131 static struct usb_string strings_dev[] = {
132 [USB_GADGET_MANUFACTURER_IDX].s = "",
133 [USB_GADGET_PRODUCT_IDX].s = longname,
134 [USB_GADGET_SERIAL_IDX].s = serial,
135 [USB_GZERO_SS_DESC].s = "source and sink data",
136 [USB_GZERO_LB_DESC].s = "loop input to output",
137 { } /* end of list */
140 static struct usb_gadget_strings stringtab_dev = {
141 .language = 0x0409, /* en-us */
142 .strings = strings_dev,
145 static struct usb_gadget_strings *dev_strings[] = {
146 &stringtab_dev,
147 NULL,
150 /*-------------------------------------------------------------------------*/
152 static struct timer_list autoresume_timer;
153 static struct usb_composite_dev *autoresume_cdev;
155 static void zero_autoresume(struct timer_list *unused)
157 struct usb_composite_dev *cdev = autoresume_cdev;
158 struct usb_gadget *g = cdev->gadget;
160 /* unconfigured devices can't issue wakeups */
161 if (!cdev->config)
162 return;
164 /* Normally the host would be woken up for something
165 * more significant than just a timer firing; likely
166 * because of some direct user request.
168 if (g->speed != USB_SPEED_UNKNOWN) {
169 int status = usb_gadget_wakeup(g);
170 INFO(cdev, "%s --> %d\n", __func__, status);
174 static void zero_suspend(struct usb_composite_dev *cdev)
176 if (cdev->gadget->speed == USB_SPEED_UNKNOWN)
177 return;
179 if (autoresume) {
180 if (max_autoresume &&
181 (autoresume_step_ms > max_autoresume * 1000))
182 autoresume_step_ms = autoresume * 1000;
184 mod_timer(&autoresume_timer, jiffies +
185 msecs_to_jiffies(autoresume_step_ms));
186 DBG(cdev, "suspend, wakeup in %d milliseconds\n",
187 autoresume_step_ms);
189 autoresume_step_ms += autoresume_interval_ms;
190 } else
191 DBG(cdev, "%s\n", __func__);
194 static void zero_resume(struct usb_composite_dev *cdev)
196 DBG(cdev, "%s\n", __func__);
197 del_timer(&autoresume_timer);
200 /*-------------------------------------------------------------------------*/
202 static struct usb_configuration loopback_driver = {
203 .label = "loopback",
204 .bConfigurationValue = 2,
205 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
206 /* .iConfiguration = DYNAMIC */
209 static struct usb_function *func_ss;
210 static struct usb_function_instance *func_inst_ss;
212 static int ss_config_setup(struct usb_configuration *c,
213 const struct usb_ctrlrequest *ctrl)
215 switch (ctrl->bRequest) {
216 case 0x5b:
217 case 0x5c:
218 return func_ss->setup(func_ss, ctrl);
219 default:
220 return -EOPNOTSUPP;
224 static struct usb_configuration sourcesink_driver = {
225 .label = "source/sink",
226 .setup = ss_config_setup,
227 .bConfigurationValue = 3,
228 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
229 /* .iConfiguration = DYNAMIC */
232 module_param_named(buflen, gzero_options.bulk_buflen, uint, 0);
233 module_param_named(pattern, gzero_options.pattern, uint, S_IRUGO|S_IWUSR);
234 MODULE_PARM_DESC(pattern, "0 = all zeroes, 1 = mod63, 2 = none");
236 module_param_named(isoc_interval, gzero_options.isoc_interval, uint,
237 S_IRUGO|S_IWUSR);
238 MODULE_PARM_DESC(isoc_interval, "1 - 16");
240 module_param_named(isoc_maxpacket, gzero_options.isoc_maxpacket, uint,
241 S_IRUGO|S_IWUSR);
242 MODULE_PARM_DESC(isoc_maxpacket, "0 - 1023 (fs), 0 - 1024 (hs/ss)");
244 module_param_named(isoc_mult, gzero_options.isoc_mult, uint, S_IRUGO|S_IWUSR);
245 MODULE_PARM_DESC(isoc_mult, "0 - 2 (hs/ss only)");
247 module_param_named(isoc_maxburst, gzero_options.isoc_maxburst, uint,
248 S_IRUGO|S_IWUSR);
249 MODULE_PARM_DESC(isoc_maxburst, "0 - 15 (ss only)");
251 static struct usb_function *func_lb;
252 static struct usb_function_instance *func_inst_lb;
254 module_param_named(qlen, gzero_options.qlen, uint, S_IRUGO|S_IWUSR);
255 MODULE_PARM_DESC(qlen, "depth of loopback queue");
257 module_param_named(ss_bulk_qlen, gzero_options.ss_bulk_qlen, uint,
258 S_IRUGO|S_IWUSR);
259 MODULE_PARM_DESC(bulk_qlen, "depth of sourcesink queue for bulk transfer");
261 module_param_named(ss_iso_qlen, gzero_options.ss_iso_qlen, uint,
262 S_IRUGO|S_IWUSR);
263 MODULE_PARM_DESC(iso_qlen, "depth of sourcesink queue for iso transfer");
265 static int zero_bind(struct usb_composite_dev *cdev)
267 struct f_ss_opts *ss_opts;
268 struct f_lb_opts *lb_opts;
269 int status;
271 /* Allocate string descriptor numbers ... note that string
272 * contents can be overridden by the composite_dev glue.
274 status = usb_string_ids_tab(cdev, strings_dev);
275 if (status < 0)
276 return status;
278 device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
279 device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
280 device_desc.iSerialNumber = strings_dev[USB_GADGET_SERIAL_IDX].id;
282 autoresume_cdev = cdev;
283 timer_setup(&autoresume_timer, zero_autoresume, 0);
285 func_inst_ss = usb_get_function_instance("SourceSink");
286 if (IS_ERR(func_inst_ss))
287 return PTR_ERR(func_inst_ss);
289 ss_opts = container_of(func_inst_ss, struct f_ss_opts, func_inst);
290 ss_opts->pattern = gzero_options.pattern;
291 ss_opts->isoc_interval = gzero_options.isoc_interval;
292 ss_opts->isoc_maxpacket = gzero_options.isoc_maxpacket;
293 ss_opts->isoc_mult = gzero_options.isoc_mult;
294 ss_opts->isoc_maxburst = gzero_options.isoc_maxburst;
295 ss_opts->bulk_buflen = gzero_options.bulk_buflen;
296 ss_opts->bulk_qlen = gzero_options.ss_bulk_qlen;
297 ss_opts->iso_qlen = gzero_options.ss_iso_qlen;
299 func_ss = usb_get_function(func_inst_ss);
300 if (IS_ERR(func_ss)) {
301 status = PTR_ERR(func_ss);
302 goto err_put_func_inst_ss;
305 func_inst_lb = usb_get_function_instance("Loopback");
306 if (IS_ERR(func_inst_lb)) {
307 status = PTR_ERR(func_inst_lb);
308 goto err_put_func_ss;
311 lb_opts = container_of(func_inst_lb, struct f_lb_opts, func_inst);
312 lb_opts->bulk_buflen = gzero_options.bulk_buflen;
313 lb_opts->qlen = gzero_options.qlen;
315 func_lb = usb_get_function(func_inst_lb);
316 if (IS_ERR(func_lb)) {
317 status = PTR_ERR(func_lb);
318 goto err_put_func_inst_lb;
321 sourcesink_driver.iConfiguration = strings_dev[USB_GZERO_SS_DESC].id;
322 loopback_driver.iConfiguration = strings_dev[USB_GZERO_LB_DESC].id;
324 /* support autoresume for remote wakeup testing */
325 sourcesink_driver.bmAttributes &= ~USB_CONFIG_ATT_WAKEUP;
326 loopback_driver.bmAttributes &= ~USB_CONFIG_ATT_WAKEUP;
327 sourcesink_driver.descriptors = NULL;
328 loopback_driver.descriptors = NULL;
329 if (autoresume) {
330 sourcesink_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
331 loopback_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
332 autoresume_step_ms = autoresume * 1000;
335 /* support OTG systems */
336 if (gadget_is_otg(cdev->gadget)) {
337 if (!otg_desc[0]) {
338 struct usb_descriptor_header *usb_desc;
340 usb_desc = usb_otg_descriptor_alloc(cdev->gadget);
341 if (!usb_desc) {
342 status = -ENOMEM;
343 goto err_conf_flb;
345 usb_otg_descriptor_init(cdev->gadget, usb_desc);
346 otg_desc[0] = usb_desc;
347 otg_desc[1] = NULL;
349 sourcesink_driver.descriptors = otg_desc;
350 sourcesink_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
351 loopback_driver.descriptors = otg_desc;
352 loopback_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
355 /* Register primary, then secondary configuration. Note that
356 * SH3 only allows one config...
358 if (loopdefault) {
359 usb_add_config_only(cdev, &loopback_driver);
360 usb_add_config_only(cdev, &sourcesink_driver);
361 } else {
362 usb_add_config_only(cdev, &sourcesink_driver);
363 usb_add_config_only(cdev, &loopback_driver);
365 status = usb_add_function(&sourcesink_driver, func_ss);
366 if (status)
367 goto err_free_otg_desc;
369 usb_ep_autoconfig_reset(cdev->gadget);
370 status = usb_add_function(&loopback_driver, func_lb);
371 if (status)
372 goto err_free_otg_desc;
374 usb_ep_autoconfig_reset(cdev->gadget);
375 usb_composite_overwrite_options(cdev, &coverwrite);
377 INFO(cdev, "%s, version: " DRIVER_VERSION "\n", longname);
379 return 0;
381 err_free_otg_desc:
382 kfree(otg_desc[0]);
383 otg_desc[0] = NULL;
384 err_conf_flb:
385 usb_put_function(func_lb);
386 func_lb = NULL;
387 err_put_func_inst_lb:
388 usb_put_function_instance(func_inst_lb);
389 func_inst_lb = NULL;
390 err_put_func_ss:
391 usb_put_function(func_ss);
392 func_ss = NULL;
393 err_put_func_inst_ss:
394 usb_put_function_instance(func_inst_ss);
395 func_inst_ss = NULL;
396 return status;
399 static int zero_unbind(struct usb_composite_dev *cdev)
401 del_timer_sync(&autoresume_timer);
402 if (!IS_ERR_OR_NULL(func_ss))
403 usb_put_function(func_ss);
404 usb_put_function_instance(func_inst_ss);
405 if (!IS_ERR_OR_NULL(func_lb))
406 usb_put_function(func_lb);
407 usb_put_function_instance(func_inst_lb);
408 kfree(otg_desc[0]);
409 otg_desc[0] = NULL;
411 return 0;
414 static struct usb_composite_driver zero_driver = {
415 .name = "zero",
416 .dev = &device_desc,
417 .strings = dev_strings,
418 .max_speed = USB_SPEED_SUPER,
419 .bind = zero_bind,
420 .unbind = zero_unbind,
421 .suspend = zero_suspend,
422 .resume = zero_resume,
425 module_usb_composite_driver(zero_driver);
427 MODULE_AUTHOR("David Brownell");
428 MODULE_LICENSE("GPL");