2017-11-03 18:28:30 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
|
|
|
* dummy_hcd.c -- Dummy/Loopback USB host and device emulator driver.
|
|
|
|
*
|
|
|
|
* Maintainer: Alan Stern <stern@rowland.harvard.edu>
|
|
|
|
*
|
|
|
|
* Copyright (C) 2003 David Brownell
|
|
|
|
* Copyright (C) 2003-2005 Alan Stern
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This exposes a device side "USB gadget" API, driven by requests to a
|
|
|
|
* Linux-USB host controller driver. USB traffic is simulated; there's
|
|
|
|
* no need for USB hardware. Use this with two other drivers:
|
|
|
|
*
|
|
|
|
* - Gadget driver, responding to requests (slave);
|
|
|
|
* - Host-side device driver, as already familiar in Linux.
|
|
|
|
*
|
|
|
|
* Having this all in one kernel can help some stages of development,
|
|
|
|
* bypassing some hardware (and driver) issues. UML could help too.
|
2017-09-27 03:15:58 +08:00
|
|
|
*
|
|
|
|
* Note: The emulation does not include isochronous transfers!
|
2005-04-17 06:20:36 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/delay.h>
|
|
|
|
#include <linux/ioport.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/errno.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/timer.h>
|
|
|
|
#include <linux/list.h>
|
|
|
|
#include <linux/interrupt.h>
|
2005-10-30 02:07:23 +08:00
|
|
|
#include <linux/platform_device.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
#include <linux/usb.h>
|
2007-10-05 09:05:17 +08:00
|
|
|
#include <linux/usb/gadget.h>
|
2010-04-25 05:21:52 +08:00
|
|
|
#include <linux/usb/hcd.h>
|
2012-01-10 02:30:50 +08:00
|
|
|
#include <linux/scatterlist.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
#include <asm/byteorder.h>
|
2012-01-12 19:53:15 +08:00
|
|
|
#include <linux/io.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
#include <asm/irq.h>
|
|
|
|
#include <asm/unaligned.h>
|
|
|
|
|
|
|
|
#define DRIVER_DESC "USB Host+Gadget Emulator"
|
2005-05-11 03:34:16 +08:00
|
|
|
#define DRIVER_VERSION "02 May 2005"
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2007-12-07 00:10:39 +08:00
|
|
|
#define POWER_BUDGET 500 /* in mA; use 8 for low-power port testing */
|
2019-09-05 12:11:57 +08:00
|
|
|
#define POWER_BUDGET_3 900 /* in mA */
|
2007-12-07 00:10:39 +08:00
|
|
|
|
2012-01-12 19:53:15 +08:00
|
|
|
static const char driver_name[] = "dummy_hcd";
|
|
|
|
static const char driver_desc[] = "USB Host+Gadget Emulator";
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2012-01-12 19:53:15 +08:00
|
|
|
static const char gadget_name[] = "dummy_udc";
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2012-01-12 19:53:15 +08:00
|
|
|
MODULE_DESCRIPTION(DRIVER_DESC);
|
|
|
|
MODULE_AUTHOR("David Brownell");
|
|
|
|
MODULE_LICENSE("GPL");
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2011-06-29 21:41:52 +08:00
|
|
|
struct dummy_hcd_module_parameters {
|
|
|
|
bool is_super_speed;
|
2011-06-29 21:41:53 +08:00
|
|
|
bool is_high_speed;
|
2012-10-30 01:09:55 +08:00
|
|
|
unsigned int num;
|
2011-06-29 21:41:52 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct dummy_hcd_module_parameters mod_data = {
|
2011-06-29 21:41:53 +08:00
|
|
|
.is_super_speed = false,
|
|
|
|
.is_high_speed = true,
|
2012-10-30 01:09:55 +08:00
|
|
|
.num = 1,
|
2011-06-29 21:41:52 +08:00
|
|
|
};
|
|
|
|
module_param_named(is_super_speed, mod_data.is_super_speed, bool, S_IRUGO);
|
|
|
|
MODULE_PARM_DESC(is_super_speed, "true to simulate SuperSpeed connection");
|
2011-06-29 21:41:53 +08:00
|
|
|
module_param_named(is_high_speed, mod_data.is_high_speed, bool, S_IRUGO);
|
|
|
|
MODULE_PARM_DESC(is_high_speed, "true to simulate HighSpeed connection");
|
2012-10-30 01:09:55 +08:00
|
|
|
module_param_named(num, mod_data.num, uint, S_IRUGO);
|
|
|
|
MODULE_PARM_DESC(num, "number of emulated controllers");
|
2005-04-17 06:20:36 +08:00
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
/* gadget side driver data structres */
|
|
|
|
struct dummy_ep {
|
|
|
|
struct list_head queue;
|
|
|
|
unsigned long last_io; /* jiffies timestamp */
|
|
|
|
struct usb_gadget *gadget;
|
|
|
|
const struct usb_endpoint_descriptor *desc;
|
|
|
|
struct usb_ep ep;
|
2012-01-12 19:53:15 +08:00
|
|
|
unsigned halted:1;
|
|
|
|
unsigned wedged:1;
|
|
|
|
unsigned already_seen:1;
|
|
|
|
unsigned setup_stage:1;
|
2012-01-14 04:51:47 +08:00
|
|
|
unsigned stream_en:1;
|
2005-04-17 06:20:36 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct dummy_request {
|
|
|
|
struct list_head queue; /* ep's requests */
|
|
|
|
struct usb_request req;
|
|
|
|
};
|
|
|
|
|
2012-01-12 19:53:15 +08:00
|
|
|
static inline struct dummy_ep *usb_ep_to_dummy_ep(struct usb_ep *_ep)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2012-01-12 19:53:15 +08:00
|
|
|
return container_of(_ep, struct dummy_ep, ep);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline struct dummy_request *usb_request_to_dummy_request
|
|
|
|
(struct usb_request *_req)
|
|
|
|
{
|
2012-01-12 19:53:15 +08:00
|
|
|
return container_of(_req, struct dummy_request, req);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Every device has ep0 for control requests, plus up to 30 more endpoints,
|
|
|
|
* in one of two types:
|
|
|
|
*
|
|
|
|
* - Configurable: direction (in/out), type (bulk, iso, etc), and endpoint
|
|
|
|
* number can be changed. Names like "ep-a" are used for this type.
|
|
|
|
*
|
|
|
|
* - Fixed Function: in other cases. some characteristics may be mutable;
|
|
|
|
* that'd be hardware-specific. Names like "ep12out-bulk" are used.
|
|
|
|
*
|
|
|
|
* Gadget drivers are responsible for not setting up conflicting endpoint
|
|
|
|
* configurations, illegal or unsupported packet lengths, and so on.
|
|
|
|
*/
|
|
|
|
|
2012-01-12 19:53:15 +08:00
|
|
|
static const char ep0name[] = "ep0";
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2015-07-31 22:00:24 +08:00
|
|
|
static const struct {
|
|
|
|
const char *name;
|
|
|
|
const struct usb_ep_caps caps;
|
|
|
|
} ep_info[] = {
|
|
|
|
#define EP_INFO(_name, _caps) \
|
|
|
|
{ \
|
|
|
|
.name = _name, \
|
|
|
|
.caps = _caps, \
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2017-09-27 03:15:58 +08:00
|
|
|
/* we don't provide isochronous endpoints since we don't support them */
|
|
|
|
#define TYPE_BULK_OR_INT (USB_EP_CAPS_TYPE_BULK | USB_EP_CAPS_TYPE_INT)
|
|
|
|
|
2015-07-31 22:00:24 +08:00
|
|
|
/* everyone has ep0 */
|
|
|
|
EP_INFO(ep0name,
|
|
|
|
USB_EP_CAPS(USB_EP_CAPS_TYPE_CONTROL, USB_EP_CAPS_DIR_ALL)),
|
2012-11-20 20:23:15 +08:00
|
|
|
/* act like a pxa250: fifteen fixed function endpoints */
|
2015-07-31 22:00:24 +08:00
|
|
|
EP_INFO("ep1in-bulk",
|
|
|
|
USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_IN)),
|
|
|
|
EP_INFO("ep2out-bulk",
|
|
|
|
USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_OUT)),
|
2017-09-27 03:15:58 +08:00
|
|
|
/*
|
2015-07-31 22:00:24 +08:00
|
|
|
EP_INFO("ep3in-iso",
|
|
|
|
USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO, USB_EP_CAPS_DIR_IN)),
|
|
|
|
EP_INFO("ep4out-iso",
|
|
|
|
USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO, USB_EP_CAPS_DIR_OUT)),
|
2017-09-27 03:15:58 +08:00
|
|
|
*/
|
2015-07-31 22:00:24 +08:00
|
|
|
EP_INFO("ep5in-int",
|
|
|
|
USB_EP_CAPS(USB_EP_CAPS_TYPE_INT, USB_EP_CAPS_DIR_IN)),
|
|
|
|
EP_INFO("ep6in-bulk",
|
|
|
|
USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_IN)),
|
|
|
|
EP_INFO("ep7out-bulk",
|
|
|
|
USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_OUT)),
|
2017-09-27 03:15:58 +08:00
|
|
|
/*
|
2015-07-31 22:00:24 +08:00
|
|
|
EP_INFO("ep8in-iso",
|
|
|
|
USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO, USB_EP_CAPS_DIR_IN)),
|
|
|
|
EP_INFO("ep9out-iso",
|
|
|
|
USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO, USB_EP_CAPS_DIR_OUT)),
|
2017-09-27 03:15:58 +08:00
|
|
|
*/
|
2015-07-31 22:00:24 +08:00
|
|
|
EP_INFO("ep10in-int",
|
|
|
|
USB_EP_CAPS(USB_EP_CAPS_TYPE_INT, USB_EP_CAPS_DIR_IN)),
|
|
|
|
EP_INFO("ep11in-bulk",
|
|
|
|
USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_IN)),
|
|
|
|
EP_INFO("ep12out-bulk",
|
|
|
|
USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_OUT)),
|
2017-09-27 03:15:58 +08:00
|
|
|
/*
|
2015-07-31 22:00:24 +08:00
|
|
|
EP_INFO("ep13in-iso",
|
|
|
|
USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO, USB_EP_CAPS_DIR_IN)),
|
|
|
|
EP_INFO("ep14out-iso",
|
|
|
|
USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO, USB_EP_CAPS_DIR_OUT)),
|
2017-09-27 03:15:58 +08:00
|
|
|
*/
|
2015-07-31 22:00:24 +08:00
|
|
|
EP_INFO("ep15in-int",
|
|
|
|
USB_EP_CAPS(USB_EP_CAPS_TYPE_INT, USB_EP_CAPS_DIR_IN)),
|
2017-09-27 03:15:58 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/* or like sa1100: two fixed function endpoints */
|
2015-07-31 22:00:24 +08:00
|
|
|
EP_INFO("ep1out-bulk",
|
|
|
|
USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_OUT)),
|
|
|
|
EP_INFO("ep2in-bulk",
|
|
|
|
USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_IN)),
|
2017-09-27 03:15:58 +08:00
|
|
|
|
2012-11-20 20:23:15 +08:00
|
|
|
/* and now some generic EPs so we have enough in multi config */
|
2015-07-31 22:00:24 +08:00
|
|
|
EP_INFO("ep3out",
|
2017-09-27 03:15:58 +08:00
|
|
|
USB_EP_CAPS(TYPE_BULK_OR_INT, USB_EP_CAPS_DIR_OUT)),
|
2015-07-31 22:00:24 +08:00
|
|
|
EP_INFO("ep4in",
|
2017-09-27 03:15:58 +08:00
|
|
|
USB_EP_CAPS(TYPE_BULK_OR_INT, USB_EP_CAPS_DIR_IN)),
|
2015-07-31 22:00:24 +08:00
|
|
|
EP_INFO("ep5out",
|
2017-09-27 03:15:58 +08:00
|
|
|
USB_EP_CAPS(TYPE_BULK_OR_INT, USB_EP_CAPS_DIR_OUT)),
|
2015-07-31 22:00:24 +08:00
|
|
|
EP_INFO("ep6out",
|
2017-09-27 03:15:58 +08:00
|
|
|
USB_EP_CAPS(TYPE_BULK_OR_INT, USB_EP_CAPS_DIR_OUT)),
|
2015-07-31 22:00:24 +08:00
|
|
|
EP_INFO("ep7in",
|
2017-09-27 03:15:58 +08:00
|
|
|
USB_EP_CAPS(TYPE_BULK_OR_INT, USB_EP_CAPS_DIR_IN)),
|
2015-07-31 22:00:24 +08:00
|
|
|
EP_INFO("ep8out",
|
2017-09-27 03:15:58 +08:00
|
|
|
USB_EP_CAPS(TYPE_BULK_OR_INT, USB_EP_CAPS_DIR_OUT)),
|
2015-07-31 22:00:24 +08:00
|
|
|
EP_INFO("ep9in",
|
2017-09-27 03:15:58 +08:00
|
|
|
USB_EP_CAPS(TYPE_BULK_OR_INT, USB_EP_CAPS_DIR_IN)),
|
2015-07-31 22:00:24 +08:00
|
|
|
EP_INFO("ep10out",
|
2017-09-27 03:15:58 +08:00
|
|
|
USB_EP_CAPS(TYPE_BULK_OR_INT, USB_EP_CAPS_DIR_OUT)),
|
2015-07-31 22:00:24 +08:00
|
|
|
EP_INFO("ep11out",
|
2017-09-27 03:15:58 +08:00
|
|
|
USB_EP_CAPS(TYPE_BULK_OR_INT, USB_EP_CAPS_DIR_OUT)),
|
2015-07-31 22:00:24 +08:00
|
|
|
EP_INFO("ep12in",
|
2017-09-27 03:15:58 +08:00
|
|
|
USB_EP_CAPS(TYPE_BULK_OR_INT, USB_EP_CAPS_DIR_IN)),
|
2015-07-31 22:00:24 +08:00
|
|
|
EP_INFO("ep13out",
|
2017-09-27 03:15:58 +08:00
|
|
|
USB_EP_CAPS(TYPE_BULK_OR_INT, USB_EP_CAPS_DIR_OUT)),
|
2015-07-31 22:00:24 +08:00
|
|
|
EP_INFO("ep14in",
|
2017-09-27 03:15:58 +08:00
|
|
|
USB_EP_CAPS(TYPE_BULK_OR_INT, USB_EP_CAPS_DIR_IN)),
|
2015-07-31 22:00:24 +08:00
|
|
|
EP_INFO("ep15out",
|
2017-09-27 03:15:58 +08:00
|
|
|
USB_EP_CAPS(TYPE_BULK_OR_INT, USB_EP_CAPS_DIR_OUT)),
|
2015-07-31 22:00:24 +08:00
|
|
|
|
|
|
|
#undef EP_INFO
|
2005-04-17 06:20:36 +08:00
|
|
|
};
|
2015-07-31 22:00:24 +08:00
|
|
|
|
|
|
|
#define DUMMY_ENDPOINTS ARRAY_SIZE(ep_info)
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2005-05-04 04:15:43 +08:00
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
#define FIFO_SIZE 64
|
|
|
|
|
|
|
|
struct urbp {
|
|
|
|
struct urb *urb;
|
|
|
|
struct list_head urbp_list;
|
2012-01-10 02:30:50 +08:00
|
|
|
struct sg_mapping_iter miter;
|
|
|
|
u32 miter_started;
|
2005-04-17 06:20:36 +08:00
|
|
|
};
|
|
|
|
|
2005-05-11 03:34:16 +08:00
|
|
|
|
|
|
|
enum dummy_rh_state {
|
|
|
|
DUMMY_RH_RESET,
|
|
|
|
DUMMY_RH_SUSPENDED,
|
|
|
|
DUMMY_RH_RUNNING
|
|
|
|
};
|
|
|
|
|
2011-06-29 21:41:51 +08:00
|
|
|
struct dummy_hcd {
|
|
|
|
struct dummy *dum;
|
|
|
|
enum dummy_rh_state rh_state;
|
|
|
|
struct timer_list timer;
|
|
|
|
u32 port_status;
|
|
|
|
u32 old_status;
|
|
|
|
unsigned long re_timeout;
|
|
|
|
|
|
|
|
struct usb_device *udev;
|
|
|
|
struct list_head urbp_list;
|
2017-09-27 03:15:40 +08:00
|
|
|
struct urbp *next_frame_urbp;
|
|
|
|
|
2012-01-14 04:51:47 +08:00
|
|
|
u32 stream_en_ep;
|
|
|
|
u8 num_stream[30 / 2];
|
2011-06-29 21:41:51 +08:00
|
|
|
|
|
|
|
unsigned active:1;
|
|
|
|
unsigned old_active:1;
|
|
|
|
unsigned resuming:1;
|
|
|
|
};
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
struct dummy {
|
|
|
|
spinlock_t lock;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* SLAVE/GADGET side support
|
|
|
|
*/
|
2012-01-12 19:53:15 +08:00
|
|
|
struct dummy_ep ep[DUMMY_ENDPOINTS];
|
2005-04-17 06:20:36 +08:00
|
|
|
int address;
|
USB: dummy-hcd: Fix erroneous synchronization change
A recent change to the synchronization in dummy-hcd was incorrect.
The issue was that dummy_udc_stop() contained no locking and therefore
could race with various gadget driver callbacks, and the fix was to
add locking and issue the callbacks with the private spinlock held.
UDC drivers aren't supposed to do this. Gadget driver callback
routines are allowed to invoke functions in the UDC driver, and these
functions will generally try to acquire the private spinlock. This
would deadlock the driver.
The correct solution is to drop the spinlock before issuing callbacks,
and avoid races by emulating the synchronize_irq() call that all real
UDC drivers must perform in their ->udc_stop() routines after
disabling interrupts. This involves adding a flag to dummy-hcd's
private structure to keep track of whether interrupts are supposed to
be enabled, and adding a counter to keep track of ongoing callbacks so
that dummy_udc_stop() can wait for them all to finish.
A real UDC driver won't receive disconnect, reset, suspend, resume, or
setup events once it has disabled interrupts. dummy-hcd will receive
them but won't try to issue any gadget driver callbacks, which should
be just as good.
Signed-off-by: Alan Stern <stern@rowland.harvard.edu>
Fixes: f16443a034c7 ("USB: gadgetfs, dummy-hcd, net2280: fix locking for callbacks")
CC: <stable@vger.kernel.org>
Signed-off-by: Felipe Balbi <felipe.balbi@linux.intel.com>
2017-09-27 03:15:49 +08:00
|
|
|
int callback_usage;
|
2005-04-17 06:20:36 +08:00
|
|
|
struct usb_gadget gadget;
|
|
|
|
struct usb_gadget_driver *driver;
|
|
|
|
struct dummy_request fifo_req;
|
2012-01-12 19:53:15 +08:00
|
|
|
u8 fifo_buf[FIFO_SIZE];
|
2005-04-17 06:20:36 +08:00
|
|
|
u16 devstatus;
|
USB: dummy-hcd: Fix erroneous synchronization change
A recent change to the synchronization in dummy-hcd was incorrect.
The issue was that dummy_udc_stop() contained no locking and therefore
could race with various gadget driver callbacks, and the fix was to
add locking and issue the callbacks with the private spinlock held.
UDC drivers aren't supposed to do this. Gadget driver callback
routines are allowed to invoke functions in the UDC driver, and these
functions will generally try to acquire the private spinlock. This
would deadlock the driver.
The correct solution is to drop the spinlock before issuing callbacks,
and avoid races by emulating the synchronize_irq() call that all real
UDC drivers must perform in their ->udc_stop() routines after
disabling interrupts. This involves adding a flag to dummy-hcd's
private structure to keep track of whether interrupts are supposed to
be enabled, and adding a counter to keep track of ongoing callbacks so
that dummy_udc_stop() can wait for them all to finish.
A real UDC driver won't receive disconnect, reset, suspend, resume, or
setup events once it has disabled interrupts. dummy-hcd will receive
them but won't try to issue any gadget driver callbacks, which should
be just as good.
Signed-off-by: Alan Stern <stern@rowland.harvard.edu>
Fixes: f16443a034c7 ("USB: gadgetfs, dummy-hcd, net2280: fix locking for callbacks")
CC: <stable@vger.kernel.org>
Signed-off-by: Felipe Balbi <felipe.balbi@linux.intel.com>
2017-09-27 03:15:49 +08:00
|
|
|
unsigned ints_enabled:1;
|
2005-05-11 03:34:16 +08:00
|
|
|
unsigned udc_suspended:1;
|
2005-05-04 04:24:04 +08:00
|
|
|
unsigned pullup:1;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* MASTER/HOST side support
|
|
|
|
*/
|
2011-06-29 21:41:51 +08:00
|
|
|
struct dummy_hcd *hs_hcd;
|
2011-06-29 21:41:52 +08:00
|
|
|
struct dummy_hcd *ss_hcd;
|
2005-04-17 06:20:36 +08:00
|
|
|
};
|
|
|
|
|
2011-06-29 21:41:51 +08:00
|
|
|
static inline struct dummy_hcd *hcd_to_dummy_hcd(struct usb_hcd *hcd)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2011-06-29 21:41:51 +08:00
|
|
|
return (struct dummy_hcd *) (hcd->hcd_priv);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2011-06-29 21:41:51 +08:00
|
|
|
static inline struct usb_hcd *dummy_hcd_to_hcd(struct dummy_hcd *dum)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
return container_of((void *) dum, struct usb_hcd, hcd_priv);
|
|
|
|
}
|
|
|
|
|
2011-06-29 21:41:51 +08:00
|
|
|
static inline struct device *dummy_dev(struct dummy_hcd *dum)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2011-06-29 21:41:51 +08:00
|
|
|
return dummy_hcd_to_hcd(dum)->self.controller;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2012-01-12 19:53:15 +08:00
|
|
|
static inline struct device *udc_dev(struct dummy *dum)
|
2005-05-04 04:15:43 +08:00
|
|
|
{
|
|
|
|
return dum->gadget.dev.parent;
|
|
|
|
}
|
|
|
|
|
2012-01-12 19:53:15 +08:00
|
|
|
static inline struct dummy *ep_to_dummy(struct dummy_ep *ep)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2012-01-12 19:53:15 +08:00
|
|
|
return container_of(ep->gadget, struct dummy, gadget);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2011-06-29 21:41:51 +08:00
|
|
|
static inline struct dummy_hcd *gadget_to_dummy_hcd(struct usb_gadget *gadget)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2011-06-29 21:41:51 +08:00
|
|
|
struct dummy *dum = container_of(gadget, struct dummy, gadget);
|
2011-06-29 21:41:52 +08:00
|
|
|
if (dum->gadget.speed == USB_SPEED_SUPER)
|
|
|
|
return dum->ss_hcd;
|
|
|
|
else
|
|
|
|
return dum->hs_hcd;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2012-01-12 19:53:15 +08:00
|
|
|
static inline struct dummy *gadget_dev_to_dummy(struct device *dev)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2012-01-12 19:53:15 +08:00
|
|
|
return container_of(dev, struct dummy, gadget.dev);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
|
2005-05-04 04:24:04 +08:00
|
|
|
/* SLAVE/GADGET SIDE UTILITY ROUTINES */
|
|
|
|
|
|
|
|
/* called with spinlock held */
|
2012-01-12 19:53:15 +08:00
|
|
|
static void nuke(struct dummy *dum, struct dummy_ep *ep)
|
2005-05-04 04:24:04 +08:00
|
|
|
{
|
2012-01-12 19:53:15 +08:00
|
|
|
while (!list_empty(&ep->queue)) {
|
2005-05-04 04:24:04 +08:00
|
|
|
struct dummy_request *req;
|
|
|
|
|
2012-01-12 19:53:15 +08:00
|
|
|
req = list_entry(ep->queue.next, struct dummy_request, queue);
|
|
|
|
list_del_init(&req->queue);
|
2005-05-04 04:24:04 +08:00
|
|
|
req->req.status = -ESHUTDOWN;
|
|
|
|
|
2012-01-12 19:53:15 +08:00
|
|
|
spin_unlock(&dum->lock);
|
2014-09-25 04:43:19 +08:00
|
|
|
usb_gadget_giveback_request(&ep->ep, &req->req);
|
2012-01-12 19:53:15 +08:00
|
|
|
spin_lock(&dum->lock);
|
2005-05-04 04:24:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* caller must hold lock */
|
2012-01-12 19:53:15 +08:00
|
|
|
static void stop_activity(struct dummy *dum)
|
2005-05-04 04:24:04 +08:00
|
|
|
{
|
2016-12-15 03:55:56 +08:00
|
|
|
int i;
|
2005-05-04 04:24:04 +08:00
|
|
|
|
|
|
|
/* prevent any more requests */
|
|
|
|
dum->address = 0;
|
|
|
|
|
|
|
|
/* The timer is left running so that outstanding URBs can fail */
|
|
|
|
|
|
|
|
/* nuke any pending requests first, so driver i/o is quiesced */
|
2016-12-15 03:55:56 +08:00
|
|
|
for (i = 0; i < DUMMY_ENDPOINTS; ++i)
|
|
|
|
nuke(dum, &dum->ep[i]);
|
2005-05-04 04:24:04 +08:00
|
|
|
|
|
|
|
/* driver now does any non-usb quiescing necessary */
|
|
|
|
}
|
|
|
|
|
2011-06-29 21:41:52 +08:00
|
|
|
/**
|
|
|
|
* set_link_state_by_speed() - Sets the current state of the link according to
|
|
|
|
* the hcd speed
|
|
|
|
* @dum_hcd: pointer to the dummy_hcd structure to update the link state for
|
|
|
|
*
|
|
|
|
* This function updates the port_status according to the link state and the
|
|
|
|
* speed of the hcd.
|
|
|
|
*/
|
|
|
|
static void set_link_state_by_speed(struct dummy_hcd *dum_hcd)
|
|
|
|
{
|
|
|
|
struct dummy *dum = dum_hcd->dum;
|
|
|
|
|
|
|
|
if (dummy_hcd_to_hcd(dum_hcd)->speed == HCD_USB3) {
|
|
|
|
if ((dum_hcd->port_status & USB_SS_PORT_STAT_POWER) == 0) {
|
|
|
|
dum_hcd->port_status = 0;
|
|
|
|
} else if (!dum->pullup || dum->udc_suspended) {
|
|
|
|
/* UDC suspend must cause a disconnect */
|
|
|
|
dum_hcd->port_status &= ~(USB_PORT_STAT_CONNECTION |
|
|
|
|
USB_PORT_STAT_ENABLE);
|
|
|
|
if ((dum_hcd->old_status &
|
|
|
|
USB_PORT_STAT_CONNECTION) != 0)
|
|
|
|
dum_hcd->port_status |=
|
|
|
|
(USB_PORT_STAT_C_CONNECTION << 16);
|
|
|
|
} else {
|
|
|
|
/* device is connected and not suspended */
|
|
|
|
dum_hcd->port_status |= (USB_PORT_STAT_CONNECTION |
|
|
|
|
USB_PORT_STAT_SPEED_5GBPS) ;
|
|
|
|
if ((dum_hcd->old_status &
|
|
|
|
USB_PORT_STAT_CONNECTION) == 0)
|
|
|
|
dum_hcd->port_status |=
|
|
|
|
(USB_PORT_STAT_C_CONNECTION << 16);
|
2017-09-07 22:14:31 +08:00
|
|
|
if ((dum_hcd->port_status & USB_PORT_STAT_ENABLE) &&
|
|
|
|
(dum_hcd->port_status &
|
|
|
|
USB_PORT_STAT_LINK_STATE) == USB_SS_PORT_LS_U0 &&
|
|
|
|
dum_hcd->rh_state != DUMMY_RH_SUSPENDED)
|
2011-06-29 21:41:52 +08:00
|
|
|
dum_hcd->active = 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ((dum_hcd->port_status & USB_PORT_STAT_POWER) == 0) {
|
|
|
|
dum_hcd->port_status = 0;
|
|
|
|
} else if (!dum->pullup || dum->udc_suspended) {
|
|
|
|
/* UDC suspend must cause a disconnect */
|
|
|
|
dum_hcd->port_status &= ~(USB_PORT_STAT_CONNECTION |
|
|
|
|
USB_PORT_STAT_ENABLE |
|
|
|
|
USB_PORT_STAT_LOW_SPEED |
|
|
|
|
USB_PORT_STAT_HIGH_SPEED |
|
|
|
|
USB_PORT_STAT_SUSPEND);
|
|
|
|
if ((dum_hcd->old_status &
|
|
|
|
USB_PORT_STAT_CONNECTION) != 0)
|
|
|
|
dum_hcd->port_status |=
|
|
|
|
(USB_PORT_STAT_C_CONNECTION << 16);
|
|
|
|
} else {
|
|
|
|
dum_hcd->port_status |= USB_PORT_STAT_CONNECTION;
|
|
|
|
if ((dum_hcd->old_status &
|
|
|
|
USB_PORT_STAT_CONNECTION) == 0)
|
|
|
|
dum_hcd->port_status |=
|
|
|
|
(USB_PORT_STAT_C_CONNECTION << 16);
|
|
|
|
if ((dum_hcd->port_status & USB_PORT_STAT_ENABLE) == 0)
|
|
|
|
dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
|
|
|
|
else if ((dum_hcd->port_status &
|
|
|
|
USB_PORT_STAT_SUSPEND) == 0 &&
|
|
|
|
dum_hcd->rh_state != DUMMY_RH_SUSPENDED)
|
|
|
|
dum_hcd->active = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-05-04 04:24:04 +08:00
|
|
|
/* caller must hold lock */
|
2011-06-29 21:41:51 +08:00
|
|
|
static void set_link_state(struct dummy_hcd *dum_hcd)
|
2005-05-04 04:24:04 +08:00
|
|
|
{
|
2011-06-29 21:41:51 +08:00
|
|
|
struct dummy *dum = dum_hcd->dum;
|
2017-10-06 22:27:44 +08:00
|
|
|
unsigned int power_bit;
|
2011-06-29 21:41:51 +08:00
|
|
|
|
|
|
|
dum_hcd->active = 0;
|
2011-06-29 21:41:52 +08:00
|
|
|
if (dum->pullup)
|
|
|
|
if ((dummy_hcd_to_hcd(dum_hcd)->speed == HCD_USB3 &&
|
|
|
|
dum->gadget.speed != USB_SPEED_SUPER) ||
|
|
|
|
(dummy_hcd_to_hcd(dum_hcd)->speed != HCD_USB3 &&
|
|
|
|
dum->gadget.speed == USB_SPEED_SUPER))
|
|
|
|
return;
|
|
|
|
|
|
|
|
set_link_state_by_speed(dum_hcd);
|
2017-10-06 22:27:44 +08:00
|
|
|
power_bit = (dummy_hcd_to_hcd(dum_hcd)->speed == HCD_USB3 ?
|
|
|
|
USB_SS_PORT_STAT_POWER : USB_PORT_STAT_POWER);
|
2005-05-04 04:24:04 +08:00
|
|
|
|
2011-06-29 21:41:51 +08:00
|
|
|
if ((dum_hcd->port_status & USB_PORT_STAT_ENABLE) == 0 ||
|
|
|
|
dum_hcd->active)
|
|
|
|
dum_hcd->resuming = 0;
|
2005-05-04 04:24:04 +08:00
|
|
|
|
2014-11-06 14:27:55 +08:00
|
|
|
/* Currently !connected or in reset */
|
2017-10-06 22:27:44 +08:00
|
|
|
if ((dum_hcd->port_status & power_bit) == 0 ||
|
2011-06-29 21:41:51 +08:00
|
|
|
(dum_hcd->port_status & USB_PORT_STAT_RESET) != 0) {
|
2017-10-06 22:27:44 +08:00
|
|
|
unsigned int disconnect = power_bit &
|
2014-11-06 14:27:55 +08:00
|
|
|
dum_hcd->old_status & (~dum_hcd->port_status);
|
2017-10-06 22:27:44 +08:00
|
|
|
unsigned int reset = USB_PORT_STAT_RESET &
|
2014-11-06 14:27:55 +08:00
|
|
|
(~dum_hcd->old_status) & dum_hcd->port_status;
|
|
|
|
|
|
|
|
/* Report reset and disconnect events to the driver */
|
USB: dummy-hcd: Fix erroneous synchronization change
A recent change to the synchronization in dummy-hcd was incorrect.
The issue was that dummy_udc_stop() contained no locking and therefore
could race with various gadget driver callbacks, and the fix was to
add locking and issue the callbacks with the private spinlock held.
UDC drivers aren't supposed to do this. Gadget driver callback
routines are allowed to invoke functions in the UDC driver, and these
functions will generally try to acquire the private spinlock. This
would deadlock the driver.
The correct solution is to drop the spinlock before issuing callbacks,
and avoid races by emulating the synchronize_irq() call that all real
UDC drivers must perform in their ->udc_stop() routines after
disabling interrupts. This involves adding a flag to dummy-hcd's
private structure to keep track of whether interrupts are supposed to
be enabled, and adding a counter to keep track of ongoing callbacks so
that dummy_udc_stop() can wait for them all to finish.
A real UDC driver won't receive disconnect, reset, suspend, resume, or
setup events once it has disabled interrupts. dummy-hcd will receive
them but won't try to issue any gadget driver callbacks, which should
be just as good.
Signed-off-by: Alan Stern <stern@rowland.harvard.edu>
Fixes: f16443a034c7 ("USB: gadgetfs, dummy-hcd, net2280: fix locking for callbacks")
CC: <stable@vger.kernel.org>
Signed-off-by: Felipe Balbi <felipe.balbi@linux.intel.com>
2017-09-27 03:15:49 +08:00
|
|
|
if (dum->ints_enabled && (disconnect || reset)) {
|
2011-06-29 21:41:52 +08:00
|
|
|
stop_activity(dum);
|
USB: dummy-hcd: Fix erroneous synchronization change
A recent change to the synchronization in dummy-hcd was incorrect.
The issue was that dummy_udc_stop() contained no locking and therefore
could race with various gadget driver callbacks, and the fix was to
add locking and issue the callbacks with the private spinlock held.
UDC drivers aren't supposed to do this. Gadget driver callback
routines are allowed to invoke functions in the UDC driver, and these
functions will generally try to acquire the private spinlock. This
would deadlock the driver.
The correct solution is to drop the spinlock before issuing callbacks,
and avoid races by emulating the synchronize_irq() call that all real
UDC drivers must perform in their ->udc_stop() routines after
disabling interrupts. This involves adding a flag to dummy-hcd's
private structure to keep track of whether interrupts are supposed to
be enabled, and adding a counter to keep track of ongoing callbacks so
that dummy_udc_stop() can wait for them all to finish.
A real UDC driver won't receive disconnect, reset, suspend, resume, or
setup events once it has disabled interrupts. dummy-hcd will receive
them but won't try to issue any gadget driver callbacks, which should
be just as good.
Signed-off-by: Alan Stern <stern@rowland.harvard.edu>
Fixes: f16443a034c7 ("USB: gadgetfs, dummy-hcd, net2280: fix locking for callbacks")
CC: <stable@vger.kernel.org>
Signed-off-by: Felipe Balbi <felipe.balbi@linux.intel.com>
2017-09-27 03:15:49 +08:00
|
|
|
++dum->callback_usage;
|
|
|
|
spin_unlock(&dum->lock);
|
2014-11-06 14:27:55 +08:00
|
|
|
if (reset)
|
|
|
|
usb_gadget_udc_reset(&dum->gadget, dum->driver);
|
|
|
|
else
|
|
|
|
dum->driver->disconnect(&dum->gadget);
|
USB: dummy-hcd: Fix erroneous synchronization change
A recent change to the synchronization in dummy-hcd was incorrect.
The issue was that dummy_udc_stop() contained no locking and therefore
could race with various gadget driver callbacks, and the fix was to
add locking and issue the callbacks with the private spinlock held.
UDC drivers aren't supposed to do this. Gadget driver callback
routines are allowed to invoke functions in the UDC driver, and these
functions will generally try to acquire the private spinlock. This
would deadlock the driver.
The correct solution is to drop the spinlock before issuing callbacks,
and avoid races by emulating the synchronize_irq() call that all real
UDC drivers must perform in their ->udc_stop() routines after
disabling interrupts. This involves adding a flag to dummy-hcd's
private structure to keep track of whether interrupts are supposed to
be enabled, and adding a counter to keep track of ongoing callbacks so
that dummy_udc_stop() can wait for them all to finish.
A real UDC driver won't receive disconnect, reset, suspend, resume, or
setup events once it has disabled interrupts. dummy-hcd will receive
them but won't try to issue any gadget driver callbacks, which should
be just as good.
Signed-off-by: Alan Stern <stern@rowland.harvard.edu>
Fixes: f16443a034c7 ("USB: gadgetfs, dummy-hcd, net2280: fix locking for callbacks")
CC: <stable@vger.kernel.org>
Signed-off-by: Felipe Balbi <felipe.balbi@linux.intel.com>
2017-09-27 03:15:49 +08:00
|
|
|
spin_lock(&dum->lock);
|
|
|
|
--dum->callback_usage;
|
2005-05-04 04:24:04 +08:00
|
|
|
}
|
USB: dummy-hcd: Fix erroneous synchronization change
A recent change to the synchronization in dummy-hcd was incorrect.
The issue was that dummy_udc_stop() contained no locking and therefore
could race with various gadget driver callbacks, and the fix was to
add locking and issue the callbacks with the private spinlock held.
UDC drivers aren't supposed to do this. Gadget driver callback
routines are allowed to invoke functions in the UDC driver, and these
functions will generally try to acquire the private spinlock. This
would deadlock the driver.
The correct solution is to drop the spinlock before issuing callbacks,
and avoid races by emulating the synchronize_irq() call that all real
UDC drivers must perform in their ->udc_stop() routines after
disabling interrupts. This involves adding a flag to dummy-hcd's
private structure to keep track of whether interrupts are supposed to
be enabled, and adding a counter to keep track of ongoing callbacks so
that dummy_udc_stop() can wait for them all to finish.
A real UDC driver won't receive disconnect, reset, suspend, resume, or
setup events once it has disabled interrupts. dummy-hcd will receive
them but won't try to issue any gadget driver callbacks, which should
be just as good.
Signed-off-by: Alan Stern <stern@rowland.harvard.edu>
Fixes: f16443a034c7 ("USB: gadgetfs, dummy-hcd, net2280: fix locking for callbacks")
CC: <stable@vger.kernel.org>
Signed-off-by: Felipe Balbi <felipe.balbi@linux.intel.com>
2017-09-27 03:15:49 +08:00
|
|
|
} else if (dum_hcd->active != dum_hcd->old_active &&
|
|
|
|
dum->ints_enabled) {
|
|
|
|
++dum->callback_usage;
|
|
|
|
spin_unlock(&dum->lock);
|
USB: gadgetfs, dummy-hcd, net2280: fix locking for callbacks
Using the syzkaller kernel fuzzer, Andrey Konovalov generated the
following error in gadgetfs:
> BUG: KASAN: use-after-free in __lock_acquire+0x3069/0x3690
> kernel/locking/lockdep.c:3246
> Read of size 8 at addr ffff88003a2bdaf8 by task kworker/3:1/903
>
> CPU: 3 PID: 903 Comm: kworker/3:1 Not tainted 4.12.0-rc4+ #35
> Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS Bochs 01/01/2011
> Workqueue: usb_hub_wq hub_event
> Call Trace:
> __dump_stack lib/dump_stack.c:16 [inline]
> dump_stack+0x292/0x395 lib/dump_stack.c:52
> print_address_description+0x78/0x280 mm/kasan/report.c:252
> kasan_report_error mm/kasan/report.c:351 [inline]
> kasan_report+0x230/0x340 mm/kasan/report.c:408
> __asan_report_load8_noabort+0x19/0x20 mm/kasan/report.c:429
> __lock_acquire+0x3069/0x3690 kernel/locking/lockdep.c:3246
> lock_acquire+0x22d/0x560 kernel/locking/lockdep.c:3855
> __raw_spin_lock include/linux/spinlock_api_smp.h:142 [inline]
> _raw_spin_lock+0x2f/0x40 kernel/locking/spinlock.c:151
> spin_lock include/linux/spinlock.h:299 [inline]
> gadgetfs_suspend+0x89/0x130 drivers/usb/gadget/legacy/inode.c:1682
> set_link_state+0x88e/0xae0 drivers/usb/gadget/udc/dummy_hcd.c:455
> dummy_hub_control+0xd7e/0x1fb0 drivers/usb/gadget/udc/dummy_hcd.c:2074
> rh_call_control drivers/usb/core/hcd.c:689 [inline]
> rh_urb_enqueue drivers/usb/core/hcd.c:846 [inline]
> usb_hcd_submit_urb+0x92f/0x20b0 drivers/usb/core/hcd.c:1650
> usb_submit_urb+0x8b2/0x12c0 drivers/usb/core/urb.c:542
> usb_start_wait_urb+0x148/0x5b0 drivers/usb/core/message.c:56
> usb_internal_control_msg drivers/usb/core/message.c:100 [inline]
> usb_control_msg+0x341/0x4d0 drivers/usb/core/message.c:151
> usb_clear_port_feature+0x74/0xa0 drivers/usb/core/hub.c:412
> hub_port_disable+0x123/0x510 drivers/usb/core/hub.c:4177
> hub_port_init+0x1ed/0x2940 drivers/usb/core/hub.c:4648
> hub_port_connect drivers/usb/core/hub.c:4826 [inline]
> hub_port_connect_change drivers/usb/core/hub.c:4999 [inline]
> port_event drivers/usb/core/hub.c:5105 [inline]
> hub_event+0x1ae1/0x3d40 drivers/usb/core/hub.c:5185
> process_one_work+0xc08/0x1bd0 kernel/workqueue.c:2097
> process_scheduled_works kernel/workqueue.c:2157 [inline]
> worker_thread+0xb2b/0x1860 kernel/workqueue.c:2233
> kthread+0x363/0x440 kernel/kthread.c:231
> ret_from_fork+0x2a/0x40 arch/x86/entry/entry_64.S:424
>
> Allocated by task 9958:
> save_stack_trace+0x1b/0x20 arch/x86/kernel/stacktrace.c:59
> save_stack+0x43/0xd0 mm/kasan/kasan.c:513
> set_track mm/kasan/kasan.c:525 [inline]
> kasan_kmalloc+0xad/0xe0 mm/kasan/kasan.c:617
> kmem_cache_alloc_trace+0x87/0x280 mm/slub.c:2745
> kmalloc include/linux/slab.h:492 [inline]
> kzalloc include/linux/slab.h:665 [inline]
> dev_new drivers/usb/gadget/legacy/inode.c:170 [inline]
> gadgetfs_fill_super+0x24f/0x540 drivers/usb/gadget/legacy/inode.c:1993
> mount_single+0xf6/0x160 fs/super.c:1192
> gadgetfs_mount+0x31/0x40 drivers/usb/gadget/legacy/inode.c:2019
> mount_fs+0x9c/0x2d0 fs/super.c:1223
> vfs_kern_mount.part.25+0xcb/0x490 fs/namespace.c:976
> vfs_kern_mount fs/namespace.c:2509 [inline]
> do_new_mount fs/namespace.c:2512 [inline]
> do_mount+0x41b/0x2d90 fs/namespace.c:2834
> SYSC_mount fs/namespace.c:3050 [inline]
> SyS_mount+0xb0/0x120 fs/namespace.c:3027
> entry_SYSCALL_64_fastpath+0x1f/0xbe
>
> Freed by task 9960:
> save_stack_trace+0x1b/0x20 arch/x86/kernel/stacktrace.c:59
> save_stack+0x43/0xd0 mm/kasan/kasan.c:513
> set_track mm/kasan/kasan.c:525 [inline]
> kasan_slab_free+0x72/0xc0 mm/kasan/kasan.c:590
> slab_free_hook mm/slub.c:1357 [inline]
> slab_free_freelist_hook mm/slub.c:1379 [inline]
> slab_free mm/slub.c:2961 [inline]
> kfree+0xed/0x2b0 mm/slub.c:3882
> put_dev+0x124/0x160 drivers/usb/gadget/legacy/inode.c:163
> gadgetfs_kill_sb+0x33/0x60 drivers/usb/gadget/legacy/inode.c:2027
> deactivate_locked_super+0x8d/0xd0 fs/super.c:309
> deactivate_super+0x21e/0x310 fs/super.c:340
> cleanup_mnt+0xb7/0x150 fs/namespace.c:1112
> __cleanup_mnt+0x1b/0x20 fs/namespace.c:1119
> task_work_run+0x1a0/0x280 kernel/task_work.c:116
> exit_task_work include/linux/task_work.h:21 [inline]
> do_exit+0x18a8/0x2820 kernel/exit.c:878
> do_group_exit+0x14e/0x420 kernel/exit.c:982
> get_signal+0x784/0x1780 kernel/signal.c:2318
> do_signal+0xd7/0x2130 arch/x86/kernel/signal.c:808
> exit_to_usermode_loop+0x1ac/0x240 arch/x86/entry/common.c:157
> prepare_exit_to_usermode arch/x86/entry/common.c:194 [inline]
> syscall_return_slowpath+0x3ba/0x410 arch/x86/entry/common.c:263
> entry_SYSCALL_64_fastpath+0xbc/0xbe
>
> The buggy address belongs to the object at ffff88003a2bdae0
> which belongs to the cache kmalloc-1024 of size 1024
> The buggy address is located 24 bytes inside of
> 1024-byte region [ffff88003a2bdae0, ffff88003a2bdee0)
> The buggy address belongs to the page:
> page:ffffea0000e8ae00 count:1 mapcount:0 mapping: (null)
> index:0x0 compound_mapcount: 0
> flags: 0x100000000008100(slab|head)
> raw: 0100000000008100 0000000000000000 0000000000000000 0000000100170017
> raw: ffffea0000ed3020 ffffea0000f5f820 ffff88003e80efc0 0000000000000000
> page dumped because: kasan: bad access detected
>
> Memory state around the buggy address:
> ffff88003a2bd980: fb fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc
> ffff88003a2bda00: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc
> >ffff88003a2bda80: fc fc fc fc fc fc fc fc fc fc fc fc fb fb fb fb
> ^
> ffff88003a2bdb00: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
> ffff88003a2bdb80: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
> ==================================================================
What this means is that the gadgetfs_suspend() routine was trying to
access dev->lock after it had been deallocated. The root cause is a
race in the dummy_hcd driver; the dummy_udc_stop() routine can race
with the rest of the driver because it contains no locking. And even
when proper locking is added, it can still race with the
set_link_state() function because that function incorrectly drops the
private spinlock before invoking any gadget driver callbacks.
The result of this race, as seen above, is that set_link_state() can
invoke a callback in gadgetfs even after gadgetfs has been unbound
from dummy_hcd's UDC and its private data structures have been
deallocated.
include/linux/usb/gadget.h documents that the ->reset, ->disconnect,
->suspend, and ->resume callbacks may be invoked in interrupt context.
In general this is necessary, to prevent races with gadget driver
removal. This patch fixes dummy_hcd to retain the spinlock across
these calls, and it adds a spinlock acquisition to dummy_udc_stop() to
prevent the race.
The net2280 driver makes the same mistake of dropping the private
spinlock for its ->disconnect and ->reset callback invocations. The
patch fixes it too.
Lastly, since gadgetfs_suspend() may be invoked in interrupt context,
it cannot assume that interrupts are enabled when it runs. It must
use spin_lock_irqsave() instead of spin_lock_irq(). The patch fixes
that bug as well.
Signed-off-by: Alan Stern <stern@rowland.harvard.edu>
Reported-and-tested-by: Andrey Konovalov <andreyknvl@google.com>
CC: <stable@vger.kernel.org>
Acked-by: Felipe Balbi <felipe.balbi@linux.intel.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2017-06-14 03:23:42 +08:00
|
|
|
if (dum_hcd->old_active && dum->driver->suspend)
|
2011-06-29 21:41:52 +08:00
|
|
|
dum->driver->suspend(&dum->gadget);
|
USB: gadgetfs, dummy-hcd, net2280: fix locking for callbacks
Using the syzkaller kernel fuzzer, Andrey Konovalov generated the
following error in gadgetfs:
> BUG: KASAN: use-after-free in __lock_acquire+0x3069/0x3690
> kernel/locking/lockdep.c:3246
> Read of size 8 at addr ffff88003a2bdaf8 by task kworker/3:1/903
>
> CPU: 3 PID: 903 Comm: kworker/3:1 Not tainted 4.12.0-rc4+ #35
> Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS Bochs 01/01/2011
> Workqueue: usb_hub_wq hub_event
> Call Trace:
> __dump_stack lib/dump_stack.c:16 [inline]
> dump_stack+0x292/0x395 lib/dump_stack.c:52
> print_address_description+0x78/0x280 mm/kasan/report.c:252
> kasan_report_error mm/kasan/report.c:351 [inline]
> kasan_report+0x230/0x340 mm/kasan/report.c:408
> __asan_report_load8_noabort+0x19/0x20 mm/kasan/report.c:429
> __lock_acquire+0x3069/0x3690 kernel/locking/lockdep.c:3246
> lock_acquire+0x22d/0x560 kernel/locking/lockdep.c:3855
> __raw_spin_lock include/linux/spinlock_api_smp.h:142 [inline]
> _raw_spin_lock+0x2f/0x40 kernel/locking/spinlock.c:151
> spin_lock include/linux/spinlock.h:299 [inline]
> gadgetfs_suspend+0x89/0x130 drivers/usb/gadget/legacy/inode.c:1682
> set_link_state+0x88e/0xae0 drivers/usb/gadget/udc/dummy_hcd.c:455
> dummy_hub_control+0xd7e/0x1fb0 drivers/usb/gadget/udc/dummy_hcd.c:2074
> rh_call_control drivers/usb/core/hcd.c:689 [inline]
> rh_urb_enqueue drivers/usb/core/hcd.c:846 [inline]
> usb_hcd_submit_urb+0x92f/0x20b0 drivers/usb/core/hcd.c:1650
> usb_submit_urb+0x8b2/0x12c0 drivers/usb/core/urb.c:542
> usb_start_wait_urb+0x148/0x5b0 drivers/usb/core/message.c:56
> usb_internal_control_msg drivers/usb/core/message.c:100 [inline]
> usb_control_msg+0x341/0x4d0 drivers/usb/core/message.c:151
> usb_clear_port_feature+0x74/0xa0 drivers/usb/core/hub.c:412
> hub_port_disable+0x123/0x510 drivers/usb/core/hub.c:4177
> hub_port_init+0x1ed/0x2940 drivers/usb/core/hub.c:4648
> hub_port_connect drivers/usb/core/hub.c:4826 [inline]
> hub_port_connect_change drivers/usb/core/hub.c:4999 [inline]
> port_event drivers/usb/core/hub.c:5105 [inline]
> hub_event+0x1ae1/0x3d40 drivers/usb/core/hub.c:5185
> process_one_work+0xc08/0x1bd0 kernel/workqueue.c:2097
> process_scheduled_works kernel/workqueue.c:2157 [inline]
> worker_thread+0xb2b/0x1860 kernel/workqueue.c:2233
> kthread+0x363/0x440 kernel/kthread.c:231
> ret_from_fork+0x2a/0x40 arch/x86/entry/entry_64.S:424
>
> Allocated by task 9958:
> save_stack_trace+0x1b/0x20 arch/x86/kernel/stacktrace.c:59
> save_stack+0x43/0xd0 mm/kasan/kasan.c:513
> set_track mm/kasan/kasan.c:525 [inline]
> kasan_kmalloc+0xad/0xe0 mm/kasan/kasan.c:617
> kmem_cache_alloc_trace+0x87/0x280 mm/slub.c:2745
> kmalloc include/linux/slab.h:492 [inline]
> kzalloc include/linux/slab.h:665 [inline]
> dev_new drivers/usb/gadget/legacy/inode.c:170 [inline]
> gadgetfs_fill_super+0x24f/0x540 drivers/usb/gadget/legacy/inode.c:1993
> mount_single+0xf6/0x160 fs/super.c:1192
> gadgetfs_mount+0x31/0x40 drivers/usb/gadget/legacy/inode.c:2019
> mount_fs+0x9c/0x2d0 fs/super.c:1223
> vfs_kern_mount.part.25+0xcb/0x490 fs/namespace.c:976
> vfs_kern_mount fs/namespace.c:2509 [inline]
> do_new_mount fs/namespace.c:2512 [inline]
> do_mount+0x41b/0x2d90 fs/namespace.c:2834
> SYSC_mount fs/namespace.c:3050 [inline]
> SyS_mount+0xb0/0x120 fs/namespace.c:3027
> entry_SYSCALL_64_fastpath+0x1f/0xbe
>
> Freed by task 9960:
> save_stack_trace+0x1b/0x20 arch/x86/kernel/stacktrace.c:59
> save_stack+0x43/0xd0 mm/kasan/kasan.c:513
> set_track mm/kasan/kasan.c:525 [inline]
> kasan_slab_free+0x72/0xc0 mm/kasan/kasan.c:590
> slab_free_hook mm/slub.c:1357 [inline]
> slab_free_freelist_hook mm/slub.c:1379 [inline]
> slab_free mm/slub.c:2961 [inline]
> kfree+0xed/0x2b0 mm/slub.c:3882
> put_dev+0x124/0x160 drivers/usb/gadget/legacy/inode.c:163
> gadgetfs_kill_sb+0x33/0x60 drivers/usb/gadget/legacy/inode.c:2027
> deactivate_locked_super+0x8d/0xd0 fs/super.c:309
> deactivate_super+0x21e/0x310 fs/super.c:340
> cleanup_mnt+0xb7/0x150 fs/namespace.c:1112
> __cleanup_mnt+0x1b/0x20 fs/namespace.c:1119
> task_work_run+0x1a0/0x280 kernel/task_work.c:116
> exit_task_work include/linux/task_work.h:21 [inline]
> do_exit+0x18a8/0x2820 kernel/exit.c:878
> do_group_exit+0x14e/0x420 kernel/exit.c:982
> get_signal+0x784/0x1780 kernel/signal.c:2318
> do_signal+0xd7/0x2130 arch/x86/kernel/signal.c:808
> exit_to_usermode_loop+0x1ac/0x240 arch/x86/entry/common.c:157
> prepare_exit_to_usermode arch/x86/entry/common.c:194 [inline]
> syscall_return_slowpath+0x3ba/0x410 arch/x86/entry/common.c:263
> entry_SYSCALL_64_fastpath+0xbc/0xbe
>
> The buggy address belongs to the object at ffff88003a2bdae0
> which belongs to the cache kmalloc-1024 of size 1024
> The buggy address is located 24 bytes inside of
> 1024-byte region [ffff88003a2bdae0, ffff88003a2bdee0)
> The buggy address belongs to the page:
> page:ffffea0000e8ae00 count:1 mapcount:0 mapping: (null)
> index:0x0 compound_mapcount: 0
> flags: 0x100000000008100(slab|head)
> raw: 0100000000008100 0000000000000000 0000000000000000 0000000100170017
> raw: ffffea0000ed3020 ffffea0000f5f820 ffff88003e80efc0 0000000000000000
> page dumped because: kasan: bad access detected
>
> Memory state around the buggy address:
> ffff88003a2bd980: fb fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc
> ffff88003a2bda00: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc
> >ffff88003a2bda80: fc fc fc fc fc fc fc fc fc fc fc fc fb fb fb fb
> ^
> ffff88003a2bdb00: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
> ffff88003a2bdb80: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
> ==================================================================
What this means is that the gadgetfs_suspend() routine was trying to
access dev->lock after it had been deallocated. The root cause is a
race in the dummy_hcd driver; the dummy_udc_stop() routine can race
with the rest of the driver because it contains no locking. And even
when proper locking is added, it can still race with the
set_link_state() function because that function incorrectly drops the
private spinlock before invoking any gadget driver callbacks.
The result of this race, as seen above, is that set_link_state() can
invoke a callback in gadgetfs even after gadgetfs has been unbound
from dummy_hcd's UDC and its private data structures have been
deallocated.
include/linux/usb/gadget.h documents that the ->reset, ->disconnect,
->suspend, and ->resume callbacks may be invoked in interrupt context.
In general this is necessary, to prevent races with gadget driver
removal. This patch fixes dummy_hcd to retain the spinlock across
these calls, and it adds a spinlock acquisition to dummy_udc_stop() to
prevent the race.
The net2280 driver makes the same mistake of dropping the private
spinlock for its ->disconnect and ->reset callback invocations. The
patch fixes it too.
Lastly, since gadgetfs_suspend() may be invoked in interrupt context,
it cannot assume that interrupts are enabled when it runs. It must
use spin_lock_irqsave() instead of spin_lock_irq(). The patch fixes
that bug as well.
Signed-off-by: Alan Stern <stern@rowland.harvard.edu>
Reported-and-tested-by: Andrey Konovalov <andreyknvl@google.com>
CC: <stable@vger.kernel.org>
Acked-by: Felipe Balbi <felipe.balbi@linux.intel.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2017-06-14 03:23:42 +08:00
|
|
|
else if (!dum_hcd->old_active && dum->driver->resume)
|
2011-06-29 21:41:52 +08:00
|
|
|
dum->driver->resume(&dum->gadget);
|
USB: dummy-hcd: Fix erroneous synchronization change
A recent change to the synchronization in dummy-hcd was incorrect.
The issue was that dummy_udc_stop() contained no locking and therefore
could race with various gadget driver callbacks, and the fix was to
add locking and issue the callbacks with the private spinlock held.
UDC drivers aren't supposed to do this. Gadget driver callback
routines are allowed to invoke functions in the UDC driver, and these
functions will generally try to acquire the private spinlock. This
would deadlock the driver.
The correct solution is to drop the spinlock before issuing callbacks,
and avoid races by emulating the synchronize_irq() call that all real
UDC drivers must perform in their ->udc_stop() routines after
disabling interrupts. This involves adding a flag to dummy-hcd's
private structure to keep track of whether interrupts are supposed to
be enabled, and adding a counter to keep track of ongoing callbacks so
that dummy_udc_stop() can wait for them all to finish.
A real UDC driver won't receive disconnect, reset, suspend, resume, or
setup events once it has disabled interrupts. dummy-hcd will receive
them but won't try to issue any gadget driver callbacks, which should
be just as good.
Signed-off-by: Alan Stern <stern@rowland.harvard.edu>
Fixes: f16443a034c7 ("USB: gadgetfs, dummy-hcd, net2280: fix locking for callbacks")
CC: <stable@vger.kernel.org>
Signed-off-by: Felipe Balbi <felipe.balbi@linux.intel.com>
2017-09-27 03:15:49 +08:00
|
|
|
spin_lock(&dum->lock);
|
|
|
|
--dum->callback_usage;
|
2005-05-04 04:24:04 +08:00
|
|
|
}
|
|
|
|
|
2011-06-29 21:41:51 +08:00
|
|
|
dum_hcd->old_status = dum_hcd->port_status;
|
|
|
|
dum_hcd->old_active = dum_hcd->active;
|
2005-05-04 04:24:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/* SLAVE/GADGET SIDE DRIVER
|
|
|
|
*
|
|
|
|
* This only tracks gadget state. All the work is done when the host
|
|
|
|
* side tries some (emulated) i/o operation. Real device controller
|
|
|
|
* drivers would do real i/o using dma, fifos, irqs, timers, etc.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define is_enabled(dum) \
|
|
|
|
(dum->port_status & USB_PORT_STAT_ENABLE)
|
|
|
|
|
2012-01-12 19:53:15 +08:00
|
|
|
static int dummy_enable(struct usb_ep *_ep,
|
|
|
|
const struct usb_endpoint_descriptor *desc)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct dummy *dum;
|
2011-06-29 21:41:51 +08:00
|
|
|
struct dummy_hcd *dum_hcd;
|
2005-04-17 06:20:36 +08:00
|
|
|
struct dummy_ep *ep;
|
|
|
|
unsigned max;
|
|
|
|
int retval;
|
|
|
|
|
2012-01-12 19:53:15 +08:00
|
|
|
ep = usb_ep_to_dummy_ep(_ep);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (!_ep || !desc || ep->desc || _ep->name == ep0name
|
|
|
|
|| desc->bDescriptorType != USB_DT_ENDPOINT)
|
|
|
|
return -EINVAL;
|
2012-01-12 19:53:15 +08:00
|
|
|
dum = ep_to_dummy(ep);
|
2011-06-29 21:41:51 +08:00
|
|
|
if (!dum->driver)
|
|
|
|
return -ESHUTDOWN;
|
2011-06-17 02:36:57 +08:00
|
|
|
|
|
|
|
dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
|
2011-06-29 21:41:51 +08:00
|
|
|
if (!is_enabled(dum_hcd))
|
2005-04-17 06:20:36 +08:00
|
|
|
return -ESHUTDOWN;
|
2011-06-29 21:41:51 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* For HS/FS devices only bits 0..10 of the wMaxPacketSize represent the
|
|
|
|
* maximum packet size.
|
2011-06-29 21:41:52 +08:00
|
|
|
* For SS devices the wMaxPacketSize is limited by 1024.
|
2011-06-29 21:41:51 +08:00
|
|
|
*/
|
2016-09-28 19:17:38 +08:00
|
|
|
max = usb_endpoint_maxp(desc);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/* drivers must not request bad settings, since lower levels
|
|
|
|
* (hardware or its drivers) may not check. some endpoints
|
|
|
|
* can't do iso, many have maxpacket limitations, etc.
|
|
|
|
*
|
|
|
|
* since this "hardware" driver is here to help debugging, we
|
|
|
|
* have some extra sanity checks. (there could be more though,
|
|
|
|
* especially for "ep9out" style fixed function ones.)
|
|
|
|
*/
|
|
|
|
retval = -EINVAL;
|
2012-01-12 19:53:14 +08:00
|
|
|
switch (usb_endpoint_type(desc)) {
|
2005-04-17 06:20:36 +08:00
|
|
|
case USB_ENDPOINT_XFER_BULK:
|
2012-01-12 19:53:15 +08:00
|
|
|
if (strstr(ep->ep.name, "-iso")
|
|
|
|
|| strstr(ep->ep.name, "-int")) {
|
2005-04-17 06:20:36 +08:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
switch (dum->gadget.speed) {
|
2011-06-29 21:41:52 +08:00
|
|
|
case USB_SPEED_SUPER:
|
|
|
|
if (max == 1024)
|
|
|
|
break;
|
|
|
|
goto done;
|
2005-04-17 06:20:36 +08:00
|
|
|
case USB_SPEED_HIGH:
|
|
|
|
if (max == 512)
|
|
|
|
break;
|
2008-03-29 05:50:26 +08:00
|
|
|
goto done;
|
|
|
|
case USB_SPEED_FULL:
|
|
|
|
if (max == 8 || max == 16 || max == 32 || max == 64)
|
2005-04-17 06:20:36 +08:00
|
|
|
/* we'll fake any legal size */
|
|
|
|
break;
|
2008-03-29 05:50:26 +08:00
|
|
|
/* save a return statement */
|
|
|
|
default:
|
|
|
|
goto done;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case USB_ENDPOINT_XFER_INT:
|
2012-01-12 19:53:15 +08:00
|
|
|
if (strstr(ep->ep.name, "-iso")) /* bulk is ok */
|
2005-04-17 06:20:36 +08:00
|
|
|
goto done;
|
|
|
|
/* real hardware might not handle all packet sizes */
|
|
|
|
switch (dum->gadget.speed) {
|
2011-06-29 21:41:52 +08:00
|
|
|
case USB_SPEED_SUPER:
|
2005-04-17 06:20:36 +08:00
|
|
|
case USB_SPEED_HIGH:
|
|
|
|
if (max <= 1024)
|
|
|
|
break;
|
|
|
|
/* save a return statement */
|
2017-10-26 01:28:00 +08:00
|
|
|
/* fall through */
|
2005-04-17 06:20:36 +08:00
|
|
|
case USB_SPEED_FULL:
|
|
|
|
if (max <= 64)
|
|
|
|
break;
|
|
|
|
/* save a return statement */
|
2017-10-26 01:28:00 +08:00
|
|
|
/* fall through */
|
2005-04-17 06:20:36 +08:00
|
|
|
default:
|
|
|
|
if (max <= 8)
|
|
|
|
break;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case USB_ENDPOINT_XFER_ISOC:
|
2012-01-12 19:53:15 +08:00
|
|
|
if (strstr(ep->ep.name, "-bulk")
|
|
|
|
|| strstr(ep->ep.name, "-int"))
|
2005-04-17 06:20:36 +08:00
|
|
|
goto done;
|
|
|
|
/* real hardware might not handle all packet sizes */
|
|
|
|
switch (dum->gadget.speed) {
|
2011-06-29 21:41:52 +08:00
|
|
|
case USB_SPEED_SUPER:
|
2005-04-17 06:20:36 +08:00
|
|
|
case USB_SPEED_HIGH:
|
|
|
|
if (max <= 1024)
|
|
|
|
break;
|
|
|
|
/* save a return statement */
|
2017-10-26 01:28:00 +08:00
|
|
|
/* fall through */
|
2005-04-17 06:20:36 +08:00
|
|
|
case USB_SPEED_FULL:
|
|
|
|
if (max <= 1023)
|
|
|
|
break;
|
|
|
|
/* save a return statement */
|
|
|
|
default:
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* few chips support control except on ep0 */
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
_ep->maxpacket = max;
|
2012-01-14 04:51:47 +08:00
|
|
|
if (usb_ss_max_streams(_ep->comp_desc)) {
|
|
|
|
if (!usb_endpoint_xfer_bulk(desc)) {
|
|
|
|
dev_err(udc_dev(dum), "Can't enable stream support on "
|
|
|
|
"non-bulk ep %s\n", _ep->name);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
ep->stream_en = 1;
|
|
|
|
}
|
2012-01-25 18:51:19 +08:00
|
|
|
ep->desc = desc;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2012-01-14 04:51:47 +08:00
|
|
|
dev_dbg(udc_dev(dum), "enabled %s (ep%d%s-%s) maxpacket %d stream %s\n",
|
2005-04-17 06:20:36 +08:00
|
|
|
_ep->name,
|
|
|
|
desc->bEndpointAddress & 0x0f,
|
|
|
|
(desc->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
|
2019-03-21 10:27:56 +08:00
|
|
|
usb_ep_type_string(usb_endpoint_type(desc)),
|
2012-01-14 04:51:47 +08:00
|
|
|
max, ep->stream_en ? "enabled" : "disabled");
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/* at this point real hardware should be NAKing transfers
|
|
|
|
* to that endpoint, until a buffer is queued to it.
|
|
|
|
*/
|
2008-08-15 03:48:30 +08:00
|
|
|
ep->halted = ep->wedged = 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
retval = 0;
|
|
|
|
done:
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2012-01-12 19:53:15 +08:00
|
|
|
static int dummy_disable(struct usb_ep *_ep)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct dummy_ep *ep;
|
|
|
|
struct dummy *dum;
|
|
|
|
unsigned long flags;
|
|
|
|
|
2012-01-12 19:53:15 +08:00
|
|
|
ep = usb_ep_to_dummy_ep(_ep);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (!_ep || !ep->desc || _ep->name == ep0name)
|
|
|
|
return -EINVAL;
|
2012-01-12 19:53:15 +08:00
|
|
|
dum = ep_to_dummy(ep);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2012-01-12 19:53:15 +08:00
|
|
|
spin_lock_irqsave(&dum->lock, flags);
|
2005-04-17 06:20:36 +08:00
|
|
|
ep->desc = NULL;
|
2012-01-14 04:51:47 +08:00
|
|
|
ep->stream_en = 0;
|
2012-01-12 19:53:15 +08:00
|
|
|
nuke(dum, ep);
|
|
|
|
spin_unlock_irqrestore(&dum->lock, flags);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2012-01-12 19:53:15 +08:00
|
|
|
dev_dbg(udc_dev(dum), "disabled %s\n", _ep->name);
|
2014-05-19 12:31:07 +08:00
|
|
|
return 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2012-01-12 19:53:15 +08:00
|
|
|
static struct usb_request *dummy_alloc_request(struct usb_ep *_ep,
|
|
|
|
gfp_t mem_flags)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct dummy_request *req;
|
|
|
|
|
|
|
|
if (!_ep)
|
|
|
|
return NULL;
|
|
|
|
|
2006-02-28 05:34:10 +08:00
|
|
|
req = kzalloc(sizeof(*req), mem_flags);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (!req)
|
|
|
|
return NULL;
|
2012-01-12 19:53:15 +08:00
|
|
|
INIT_LIST_HEAD(&req->queue);
|
2005-04-17 06:20:36 +08:00
|
|
|
return &req->req;
|
|
|
|
}
|
|
|
|
|
2012-01-12 19:53:15 +08:00
|
|
|
static void dummy_free_request(struct usb_ep *_ep, struct usb_request *_req)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct dummy_request *req;
|
|
|
|
|
2012-02-09 16:24:59 +08:00
|
|
|
if (!_ep || !_req) {
|
2012-05-11 12:39:37 +08:00
|
|
|
WARN_ON(1);
|
2005-04-17 06:20:36 +08:00
|
|
|
return;
|
2012-02-09 16:24:59 +08:00
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2012-01-12 19:53:15 +08:00
|
|
|
req = usb_request_to_dummy_request(_req);
|
|
|
|
WARN_ON(!list_empty(&req->queue));
|
|
|
|
kfree(req);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2012-01-12 19:53:15 +08:00
|
|
|
static void fifo_complete(struct usb_ep *ep, struct usb_request *req)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-01-12 19:53:15 +08:00
|
|
|
static int dummy_queue(struct usb_ep *_ep, struct usb_request *_req,
|
2005-10-21 15:21:58 +08:00
|
|
|
gfp_t mem_flags)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct dummy_ep *ep;
|
|
|
|
struct dummy_request *req;
|
|
|
|
struct dummy *dum;
|
2011-06-29 21:41:51 +08:00
|
|
|
struct dummy_hcd *dum_hcd;
|
2005-04-17 06:20:36 +08:00
|
|
|
unsigned long flags;
|
|
|
|
|
2012-01-12 19:53:15 +08:00
|
|
|
req = usb_request_to_dummy_request(_req);
|
|
|
|
if (!_req || !list_empty(&req->queue) || !_req->complete)
|
2005-04-17 06:20:36 +08:00
|
|
|
return -EINVAL;
|
|
|
|
|
2012-01-12 19:53:15 +08:00
|
|
|
ep = usb_ep_to_dummy_ep(_ep);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (!_ep || (!ep->desc && _ep->name != ep0name))
|
|
|
|
return -EINVAL;
|
|
|
|
|
2012-01-12 19:53:15 +08:00
|
|
|
dum = ep_to_dummy(ep);
|
2011-06-17 02:36:57 +08:00
|
|
|
dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
|
2011-06-29 21:41:51 +08:00
|
|
|
if (!dum->driver || !is_enabled(dum_hcd))
|
2005-04-17 06:20:36 +08:00
|
|
|
return -ESHUTDOWN;
|
|
|
|
|
|
|
|
#if 0
|
2012-01-12 19:53:15 +08:00
|
|
|
dev_dbg(udc_dev(dum), "ep %p queue req %p to %s, len %d buf %p\n",
|
2005-04-17 06:20:36 +08:00
|
|
|
ep, _req, _ep->name, _req->length, _req->buf);
|
|
|
|
#endif
|
|
|
|
_req->status = -EINPROGRESS;
|
|
|
|
_req->actual = 0;
|
2012-01-12 19:53:15 +08:00
|
|
|
spin_lock_irqsave(&dum->lock, flags);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/* implement an emulated single-request FIFO */
|
|
|
|
if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) &&
|
2012-01-12 19:53:15 +08:00
|
|
|
list_empty(&dum->fifo_req.queue) &&
|
|
|
|
list_empty(&ep->queue) &&
|
2005-04-17 06:20:36 +08:00
|
|
|
_req->length <= FIFO_SIZE) {
|
|
|
|
req = &dum->fifo_req;
|
|
|
|
req->req = *_req;
|
|
|
|
req->req.buf = dum->fifo_buf;
|
2012-01-12 19:53:15 +08:00
|
|
|
memcpy(dum->fifo_buf, _req->buf, _req->length);
|
2005-04-17 06:20:36 +08:00
|
|
|
req->req.context = dum;
|
|
|
|
req->req.complete = fifo_complete;
|
|
|
|
|
2008-07-26 23:06:24 +08:00
|
|
|
list_add_tail(&req->queue, &ep->queue);
|
2012-01-12 19:53:15 +08:00
|
|
|
spin_unlock(&dum->lock);
|
2005-04-17 06:20:36 +08:00
|
|
|
_req->actual = _req->length;
|
|
|
|
_req->status = 0;
|
2014-09-25 04:43:19 +08:00
|
|
|
usb_gadget_giveback_request(_ep, _req);
|
2012-01-12 19:53:15 +08:00
|
|
|
spin_lock(&dum->lock);
|
2008-07-26 23:06:24 +08:00
|
|
|
} else
|
|
|
|
list_add_tail(&req->queue, &ep->queue);
|
2012-01-12 19:53:15 +08:00
|
|
|
spin_unlock_irqrestore(&dum->lock, flags);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/* real hardware would likely enable transfers here, in case
|
|
|
|
* it'd been left NAKing.
|
|
|
|
*/
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-01-12 19:53:15 +08:00
|
|
|
static int dummy_dequeue(struct usb_ep *_ep, struct usb_request *_req)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct dummy_ep *ep;
|
|
|
|
struct dummy *dum;
|
|
|
|
int retval = -EINVAL;
|
|
|
|
unsigned long flags;
|
|
|
|
struct dummy_request *req = NULL;
|
|
|
|
|
|
|
|
if (!_ep || !_req)
|
|
|
|
return retval;
|
2012-01-12 19:53:15 +08:00
|
|
|
ep = usb_ep_to_dummy_ep(_ep);
|
|
|
|
dum = ep_to_dummy(ep);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
if (!dum->driver)
|
|
|
|
return -ESHUTDOWN;
|
|
|
|
|
2012-01-12 19:53:15 +08:00
|
|
|
local_irq_save(flags);
|
|
|
|
spin_lock(&dum->lock);
|
|
|
|
list_for_each_entry(req, &ep->queue, queue) {
|
2005-04-17 06:20:36 +08:00
|
|
|
if (&req->req == _req) {
|
2012-01-12 19:53:15 +08:00
|
|
|
list_del_init(&req->queue);
|
2005-04-17 06:20:36 +08:00
|
|
|
_req->status = -ECONNRESET;
|
|
|
|
retval = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-01-12 19:53:15 +08:00
|
|
|
spin_unlock(&dum->lock);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
if (retval == 0) {
|
2012-01-12 19:53:15 +08:00
|
|
|
dev_dbg(udc_dev(dum),
|
2005-04-17 06:20:36 +08:00
|
|
|
"dequeued req %p from %s, len %d buf %p\n",
|
|
|
|
req, _ep->name, _req->length, _req->buf);
|
2014-09-25 04:43:19 +08:00
|
|
|
usb_gadget_giveback_request(_ep, _req);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2012-01-12 19:53:15 +08:00
|
|
|
local_irq_restore(flags);
|
2005-04-17 06:20:36 +08:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2008-08-15 03:48:30 +08:00
|
|
|
dummy_set_halt_and_wedge(struct usb_ep *_ep, int value, int wedged)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct dummy_ep *ep;
|
|
|
|
struct dummy *dum;
|
|
|
|
|
|
|
|
if (!_ep)
|
|
|
|
return -EINVAL;
|
2012-01-12 19:53:15 +08:00
|
|
|
ep = usb_ep_to_dummy_ep(_ep);
|
|
|
|
dum = ep_to_dummy(ep);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (!dum->driver)
|
|
|
|
return -ESHUTDOWN;
|
|
|
|
if (!value)
|
2008-08-15 03:48:30 +08:00
|
|
|
ep->halted = ep->wedged = 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
else if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) &&
|
2012-01-12 19:53:15 +08:00
|
|
|
!list_empty(&ep->queue))
|
2005-04-17 06:20:36 +08:00
|
|
|
return -EAGAIN;
|
2008-08-15 03:48:30 +08:00
|
|
|
else {
|
2005-04-17 06:20:36 +08:00
|
|
|
ep->halted = 1;
|
2008-08-15 03:48:30 +08:00
|
|
|
if (wedged)
|
|
|
|
ep->wedged = 1;
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
/* FIXME clear emulated data toggle too */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-08-15 03:48:30 +08:00
|
|
|
static int
|
|
|
|
dummy_set_halt(struct usb_ep *_ep, int value)
|
|
|
|
{
|
|
|
|
return dummy_set_halt_and_wedge(_ep, value, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int dummy_set_wedge(struct usb_ep *_ep)
|
|
|
|
{
|
|
|
|
if (!_ep || _ep->name == ep0name)
|
|
|
|
return -EINVAL;
|
|
|
|
return dummy_set_halt_and_wedge(_ep, 1, 1);
|
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
static const struct usb_ep_ops dummy_ep_ops = {
|
|
|
|
.enable = dummy_enable,
|
|
|
|
.disable = dummy_disable,
|
|
|
|
|
|
|
|
.alloc_request = dummy_alloc_request,
|
|
|
|
.free_request = dummy_free_request,
|
|
|
|
|
|
|
|
.queue = dummy_queue,
|
|
|
|
.dequeue = dummy_dequeue,
|
|
|
|
|
|
|
|
.set_halt = dummy_set_halt,
|
2008-08-15 03:48:30 +08:00
|
|
|
.set_wedge = dummy_set_wedge,
|
2005-04-17 06:20:36 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
/* there are both host and device side versions of this call ... */
|
2012-01-12 19:53:15 +08:00
|
|
|
static int dummy_g_get_frame(struct usb_gadget *_gadget)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2015-09-18 10:51:26 +08:00
|
|
|
struct timespec64 ts64;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2015-09-18 10:51:26 +08:00
|
|
|
ktime_get_ts64(&ts64);
|
|
|
|
return ts64.tv_nsec / NSEC_PER_MSEC;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2012-01-12 19:53:15 +08:00
|
|
|
static int dummy_wakeup(struct usb_gadget *_gadget)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2011-06-29 21:41:51 +08:00
|
|
|
struct dummy_hcd *dum_hcd;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2011-06-29 21:41:51 +08:00
|
|
|
dum_hcd = gadget_to_dummy_hcd(_gadget);
|
|
|
|
if (!(dum_hcd->dum->devstatus & ((1 << USB_DEVICE_B_HNP_ENABLE)
|
2005-05-02 23:25:17 +08:00
|
|
|
| (1 << USB_DEVICE_REMOTE_WAKEUP))))
|
2005-04-17 06:20:36 +08:00
|
|
|
return -EINVAL;
|
2011-06-29 21:41:51 +08:00
|
|
|
if ((dum_hcd->port_status & USB_PORT_STAT_CONNECTION) == 0)
|
2005-05-11 03:34:16 +08:00
|
|
|
return -ENOLINK;
|
2011-06-29 21:41:51 +08:00
|
|
|
if ((dum_hcd->port_status & USB_PORT_STAT_SUSPEND) == 0 &&
|
|
|
|
dum_hcd->rh_state != DUMMY_RH_SUSPENDED)
|
2005-05-11 03:34:16 +08:00
|
|
|
return -EIO;
|
|
|
|
|
|
|
|
/* FIXME: What if the root hub is suspended but the port isn't? */
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/* hub notices our request, issues downstream resume, etc */
|
2011-06-29 21:41:51 +08:00
|
|
|
dum_hcd->resuming = 1;
|
|
|
|
dum_hcd->re_timeout = jiffies + msecs_to_jiffies(20);
|
|
|
|
mod_timer(&dummy_hcd_to_hcd(dum_hcd)->rh_timer, dum_hcd->re_timeout);
|
2005-04-17 06:20:36 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-01-12 19:53:15 +08:00
|
|
|
static int dummy_set_selfpowered(struct usb_gadget *_gadget, int value)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct dummy *dum;
|
|
|
|
|
2015-01-28 16:32:30 +08:00
|
|
|
_gadget->is_selfpowered = (value != 0);
|
2012-01-12 19:53:15 +08:00
|
|
|
dum = gadget_to_dummy_hcd(_gadget)->dum;
|
2005-04-17 06:20:36 +08:00
|
|
|
if (value)
|
|
|
|
dum->devstatus |= (1 << USB_DEVICE_SELF_POWERED);
|
|
|
|
else
|
|
|
|
dum->devstatus &= ~(1 << USB_DEVICE_SELF_POWERED);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-01-09 20:14:59 +08:00
|
|
|
static void dummy_udc_update_ep0(struct dummy *dum)
|
2011-06-23 20:26:14 +08:00
|
|
|
{
|
2012-01-09 20:14:56 +08:00
|
|
|
if (dum->gadget.speed == USB_SPEED_SUPER)
|
2011-06-23 20:26:14 +08:00
|
|
|
dum->ep[0].ep.maxpacket = 9;
|
2012-01-09 20:14:56 +08:00
|
|
|
else
|
2011-06-23 20:26:14 +08:00
|
|
|
dum->ep[0].ep.maxpacket = 64;
|
|
|
|
}
|
|
|
|
|
2012-01-12 19:53:15 +08:00
|
|
|
static int dummy_pullup(struct usb_gadget *_gadget, int value)
|
2005-05-04 04:24:04 +08:00
|
|
|
{
|
2011-06-17 16:11:41 +08:00
|
|
|
struct dummy_hcd *dum_hcd;
|
2005-05-04 04:24:04 +08:00
|
|
|
struct dummy *dum;
|
|
|
|
unsigned long flags;
|
|
|
|
|
2011-06-23 20:26:14 +08:00
|
|
|
dum = gadget_dev_to_dummy(&_gadget->dev);
|
2011-06-17 16:11:41 +08:00
|
|
|
dum_hcd = gadget_to_dummy_hcd(_gadget);
|
|
|
|
|
2012-01-12 19:53:15 +08:00
|
|
|
spin_lock_irqsave(&dum->lock, flags);
|
2005-05-04 04:24:04 +08:00
|
|
|
dum->pullup = (value != 0);
|
2011-06-17 16:11:41 +08:00
|
|
|
set_link_state(dum_hcd);
|
2024-06-11 20:26:44 +08:00
|
|
|
if (value == 0) {
|
|
|
|
/*
|
|
|
|
* Emulate synchronize_irq(): wait for callbacks to finish.
|
|
|
|
* This seems to be the best place to emulate the call to
|
|
|
|
* synchronize_irq() that's in usb_gadget_remove_driver().
|
|
|
|
* Doing it in dummy_udc_stop() would be too late since it
|
|
|
|
* is called after the unbind callback and unbind shouldn't
|
|
|
|
* be invoked until all the other callbacks are finished.
|
|
|
|
*/
|
|
|
|
while (dum->callback_usage > 0) {
|
|
|
|
spin_unlock_irqrestore(&dum->lock, flags);
|
|
|
|
usleep_range(1000, 2000);
|
|
|
|
spin_lock_irqsave(&dum->lock, flags);
|
|
|
|
}
|
|
|
|
}
|
2012-01-12 19:53:15 +08:00
|
|
|
spin_unlock_irqrestore(&dum->lock, flags);
|
2011-06-23 20:26:14 +08:00
|
|
|
|
2011-06-17 16:11:41 +08:00
|
|
|
usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
|
2005-05-04 04:24:04 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-06-07 18:52:54 +08:00
|
|
|
static void dummy_udc_set_speed(struct usb_gadget *_gadget,
|
|
|
|
enum usb_device_speed speed)
|
|
|
|
{
|
|
|
|
struct dummy *dum;
|
|
|
|
|
|
|
|
dum = gadget_dev_to_dummy(&_gadget->dev);
|
2017-11-02 17:09:17 +08:00
|
|
|
dum->gadget.speed = speed;
|
2017-06-07 18:52:54 +08:00
|
|
|
dummy_udc_update_ep0(dum);
|
|
|
|
}
|
|
|
|
|
2011-06-23 20:26:17 +08:00
|
|
|
static int dummy_udc_start(struct usb_gadget *g,
|
|
|
|
struct usb_gadget_driver *driver);
|
2014-10-18 01:05:12 +08:00
|
|
|
static int dummy_udc_stop(struct usb_gadget *g);
|
2011-06-28 21:33:47 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
static const struct usb_gadget_ops dummy_ops = {
|
|
|
|
.get_frame = dummy_g_get_frame,
|
|
|
|
.wakeup = dummy_wakeup,
|
|
|
|
.set_selfpowered = dummy_set_selfpowered,
|
2005-05-04 04:24:04 +08:00
|
|
|
.pullup = dummy_pullup,
|
2011-06-23 20:26:17 +08:00
|
|
|
.udc_start = dummy_udc_start,
|
|
|
|
.udc_stop = dummy_udc_stop,
|
2017-06-07 18:52:54 +08:00
|
|
|
.udc_set_speed = dummy_udc_set_speed,
|
2005-04-17 06:20:36 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
/* "function" sysfs attribute */
|
2013-08-24 07:34:43 +08:00
|
|
|
static ssize_t function_show(struct device *dev, struct device_attribute *attr,
|
2012-01-12 19:53:15 +08:00
|
|
|
char *buf)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2012-01-12 19:53:15 +08:00
|
|
|
struct dummy *dum = gadget_dev_to_dummy(dev);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
if (!dum->driver || !dum->driver->function)
|
|
|
|
return 0;
|
2012-01-12 19:53:15 +08:00
|
|
|
return scnprintf(buf, PAGE_SIZE, "%s\n", dum->driver->function);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2013-08-24 07:34:43 +08:00
|
|
|
static DEVICE_ATTR_RO(function);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Driver registration/unregistration.
|
|
|
|
*
|
|
|
|
* This is basically hardware-specific; there's usually only one real USB
|
|
|
|
* device (not host) controller since that's how USB devices are intended
|
|
|
|
* to work. So most implementations of these api calls will rely on the
|
|
|
|
* fact that only one driver will ever bind to the hardware. But curious
|
|
|
|
* hardware can be built with discrete components, so the gadget API doesn't
|
|
|
|
* require that assumption.
|
|
|
|
*
|
|
|
|
* For this emulator, it might be convenient to create a usb slave device
|
|
|
|
* for each driver that registers: just add to a big root hub.
|
|
|
|
*/
|
|
|
|
|
2011-06-23 20:26:17 +08:00
|
|
|
static int dummy_udc_start(struct usb_gadget *g,
|
|
|
|
struct usb_gadget_driver *driver)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2011-06-23 20:26:17 +08:00
|
|
|
struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(g);
|
|
|
|
struct dummy *dum = dum_hcd->dum;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2019-04-19 01:12:07 +08:00
|
|
|
switch (g->speed) {
|
|
|
|
/* All the speeds we support */
|
|
|
|
case USB_SPEED_LOW:
|
|
|
|
case USB_SPEED_FULL:
|
|
|
|
case USB_SPEED_HIGH:
|
|
|
|
case USB_SPEED_SUPER:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
dev_err(dummy_dev(dum_hcd), "Unsupported driver max speed %d\n",
|
|
|
|
driver->max_speed);
|
2005-04-17 06:20:36 +08:00
|
|
|
return -EINVAL;
|
2019-04-19 01:12:07 +08:00
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* SLAVE side init ... the layer above hardware, which
|
|
|
|
* can't enumerate without help from the driver we're binding.
|
|
|
|
*/
|
2005-05-02 23:25:17 +08:00
|
|
|
|
USB: dummy-hcd: Fix erroneous synchronization change
A recent change to the synchronization in dummy-hcd was incorrect.
The issue was that dummy_udc_stop() contained no locking and therefore
could race with various gadget driver callbacks, and the fix was to
add locking and issue the callbacks with the private spinlock held.
UDC drivers aren't supposed to do this. Gadget driver callback
routines are allowed to invoke functions in the UDC driver, and these
functions will generally try to acquire the private spinlock. This
would deadlock the driver.
The correct solution is to drop the spinlock before issuing callbacks,
and avoid races by emulating the synchronize_irq() call that all real
UDC drivers must perform in their ->udc_stop() routines after
disabling interrupts. This involves adding a flag to dummy-hcd's
private structure to keep track of whether interrupts are supposed to
be enabled, and adding a counter to keep track of ongoing callbacks so
that dummy_udc_stop() can wait for them all to finish.
A real UDC driver won't receive disconnect, reset, suspend, resume, or
setup events once it has disabled interrupts. dummy-hcd will receive
them but won't try to issue any gadget driver callbacks, which should
be just as good.
Signed-off-by: Alan Stern <stern@rowland.harvard.edu>
Fixes: f16443a034c7 ("USB: gadgetfs, dummy-hcd, net2280: fix locking for callbacks")
CC: <stable@vger.kernel.org>
Signed-off-by: Felipe Balbi <felipe.balbi@linux.intel.com>
2017-09-27 03:15:49 +08:00
|
|
|
spin_lock_irq(&dum->lock);
|
2005-04-17 06:20:36 +08:00
|
|
|
dum->devstatus = 0;
|
|
|
|
dum->driver = driver;
|
USB: dummy-hcd: Fix erroneous synchronization change
A recent change to the synchronization in dummy-hcd was incorrect.
The issue was that dummy_udc_stop() contained no locking and therefore
could race with various gadget driver callbacks, and the fix was to
add locking and issue the callbacks with the private spinlock held.
UDC drivers aren't supposed to do this. Gadget driver callback
routines are allowed to invoke functions in the UDC driver, and these
functions will generally try to acquire the private spinlock. This
would deadlock the driver.
The correct solution is to drop the spinlock before issuing callbacks,
and avoid races by emulating the synchronize_irq() call that all real
UDC drivers must perform in their ->udc_stop() routines after
disabling interrupts. This involves adding a flag to dummy-hcd's
private structure to keep track of whether interrupts are supposed to
be enabled, and adding a counter to keep track of ongoing callbacks so
that dummy_udc_stop() can wait for them all to finish.
A real UDC driver won't receive disconnect, reset, suspend, resume, or
setup events once it has disabled interrupts. dummy-hcd will receive
them but won't try to issue any gadget driver callbacks, which should
be just as good.
Signed-off-by: Alan Stern <stern@rowland.harvard.edu>
Fixes: f16443a034c7 ("USB: gadgetfs, dummy-hcd, net2280: fix locking for callbacks")
CC: <stable@vger.kernel.org>
Signed-off-by: Felipe Balbi <felipe.balbi@linux.intel.com>
2017-09-27 03:15:49 +08:00
|
|
|
dum->ints_enabled = 1;
|
|
|
|
spin_unlock_irq(&dum->lock);
|
2014-10-18 00:37:38 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-10-18 01:05:12 +08:00
|
|
|
static int dummy_udc_stop(struct usb_gadget *g)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2011-06-23 20:26:17 +08:00
|
|
|
struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(g);
|
|
|
|
struct dummy *dum = dum_hcd->dum;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
USB: gadgetfs, dummy-hcd, net2280: fix locking for callbacks
Using the syzkaller kernel fuzzer, Andrey Konovalov generated the
following error in gadgetfs:
> BUG: KASAN: use-after-free in __lock_acquire+0x3069/0x3690
> kernel/locking/lockdep.c:3246
> Read of size 8 at addr ffff88003a2bdaf8 by task kworker/3:1/903
>
> CPU: 3 PID: 903 Comm: kworker/3:1 Not tainted 4.12.0-rc4+ #35
> Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS Bochs 01/01/2011
> Workqueue: usb_hub_wq hub_event
> Call Trace:
> __dump_stack lib/dump_stack.c:16 [inline]
> dump_stack+0x292/0x395 lib/dump_stack.c:52
> print_address_description+0x78/0x280 mm/kasan/report.c:252
> kasan_report_error mm/kasan/report.c:351 [inline]
> kasan_report+0x230/0x340 mm/kasan/report.c:408
> __asan_report_load8_noabort+0x19/0x20 mm/kasan/report.c:429
> __lock_acquire+0x3069/0x3690 kernel/locking/lockdep.c:3246
> lock_acquire+0x22d/0x560 kernel/locking/lockdep.c:3855
> __raw_spin_lock include/linux/spinlock_api_smp.h:142 [inline]
> _raw_spin_lock+0x2f/0x40 kernel/locking/spinlock.c:151
> spin_lock include/linux/spinlock.h:299 [inline]
> gadgetfs_suspend+0x89/0x130 drivers/usb/gadget/legacy/inode.c:1682
> set_link_state+0x88e/0xae0 drivers/usb/gadget/udc/dummy_hcd.c:455
> dummy_hub_control+0xd7e/0x1fb0 drivers/usb/gadget/udc/dummy_hcd.c:2074
> rh_call_control drivers/usb/core/hcd.c:689 [inline]
> rh_urb_enqueue drivers/usb/core/hcd.c:846 [inline]
> usb_hcd_submit_urb+0x92f/0x20b0 drivers/usb/core/hcd.c:1650
> usb_submit_urb+0x8b2/0x12c0 drivers/usb/core/urb.c:542
> usb_start_wait_urb+0x148/0x5b0 drivers/usb/core/message.c:56
> usb_internal_control_msg drivers/usb/core/message.c:100 [inline]
> usb_control_msg+0x341/0x4d0 drivers/usb/core/message.c:151
> usb_clear_port_feature+0x74/0xa0 drivers/usb/core/hub.c:412
> hub_port_disable+0x123/0x510 drivers/usb/core/hub.c:4177
> hub_port_init+0x1ed/0x2940 drivers/usb/core/hub.c:4648
> hub_port_connect drivers/usb/core/hub.c:4826 [inline]
> hub_port_connect_change drivers/usb/core/hub.c:4999 [inline]
> port_event drivers/usb/core/hub.c:5105 [inline]
> hub_event+0x1ae1/0x3d40 drivers/usb/core/hub.c:5185
> process_one_work+0xc08/0x1bd0 kernel/workqueue.c:2097
> process_scheduled_works kernel/workqueue.c:2157 [inline]
> worker_thread+0xb2b/0x1860 kernel/workqueue.c:2233
> kthread+0x363/0x440 kernel/kthread.c:231
> ret_from_fork+0x2a/0x40 arch/x86/entry/entry_64.S:424
>
> Allocated by task 9958:
> save_stack_trace+0x1b/0x20 arch/x86/kernel/stacktrace.c:59
> save_stack+0x43/0xd0 mm/kasan/kasan.c:513
> set_track mm/kasan/kasan.c:525 [inline]
> kasan_kmalloc+0xad/0xe0 mm/kasan/kasan.c:617
> kmem_cache_alloc_trace+0x87/0x280 mm/slub.c:2745
> kmalloc include/linux/slab.h:492 [inline]
> kzalloc include/linux/slab.h:665 [inline]
> dev_new drivers/usb/gadget/legacy/inode.c:170 [inline]
> gadgetfs_fill_super+0x24f/0x540 drivers/usb/gadget/legacy/inode.c:1993
> mount_single+0xf6/0x160 fs/super.c:1192
> gadgetfs_mount+0x31/0x40 drivers/usb/gadget/legacy/inode.c:2019
> mount_fs+0x9c/0x2d0 fs/super.c:1223
> vfs_kern_mount.part.25+0xcb/0x490 fs/namespace.c:976
> vfs_kern_mount fs/namespace.c:2509 [inline]
> do_new_mount fs/namespace.c:2512 [inline]
> do_mount+0x41b/0x2d90 fs/namespace.c:2834
> SYSC_mount fs/namespace.c:3050 [inline]
> SyS_mount+0xb0/0x120 fs/namespace.c:3027
> entry_SYSCALL_64_fastpath+0x1f/0xbe
>
> Freed by task 9960:
> save_stack_trace+0x1b/0x20 arch/x86/kernel/stacktrace.c:59
> save_stack+0x43/0xd0 mm/kasan/kasan.c:513
> set_track mm/kasan/kasan.c:525 [inline]
> kasan_slab_free+0x72/0xc0 mm/kasan/kasan.c:590
> slab_free_hook mm/slub.c:1357 [inline]
> slab_free_freelist_hook mm/slub.c:1379 [inline]
> slab_free mm/slub.c:2961 [inline]
> kfree+0xed/0x2b0 mm/slub.c:3882
> put_dev+0x124/0x160 drivers/usb/gadget/legacy/inode.c:163
> gadgetfs_kill_sb+0x33/0x60 drivers/usb/gadget/legacy/inode.c:2027
> deactivate_locked_super+0x8d/0xd0 fs/super.c:309
> deactivate_super+0x21e/0x310 fs/super.c:340
> cleanup_mnt+0xb7/0x150 fs/namespace.c:1112
> __cleanup_mnt+0x1b/0x20 fs/namespace.c:1119
> task_work_run+0x1a0/0x280 kernel/task_work.c:116
> exit_task_work include/linux/task_work.h:21 [inline]
> do_exit+0x18a8/0x2820 kernel/exit.c:878
> do_group_exit+0x14e/0x420 kernel/exit.c:982
> get_signal+0x784/0x1780 kernel/signal.c:2318
> do_signal+0xd7/0x2130 arch/x86/kernel/signal.c:808
> exit_to_usermode_loop+0x1ac/0x240 arch/x86/entry/common.c:157
> prepare_exit_to_usermode arch/x86/entry/common.c:194 [inline]
> syscall_return_slowpath+0x3ba/0x410 arch/x86/entry/common.c:263
> entry_SYSCALL_64_fastpath+0xbc/0xbe
>
> The buggy address belongs to the object at ffff88003a2bdae0
> which belongs to the cache kmalloc-1024 of size 1024
> The buggy address is located 24 bytes inside of
> 1024-byte region [ffff88003a2bdae0, ffff88003a2bdee0)
> The buggy address belongs to the page:
> page:ffffea0000e8ae00 count:1 mapcount:0 mapping: (null)
> index:0x0 compound_mapcount: 0
> flags: 0x100000000008100(slab|head)
> raw: 0100000000008100 0000000000000000 0000000000000000 0000000100170017
> raw: ffffea0000ed3020 ffffea0000f5f820 ffff88003e80efc0 0000000000000000
> page dumped because: kasan: bad access detected
>
> Memory state around the buggy address:
> ffff88003a2bd980: fb fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc
> ffff88003a2bda00: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc
> >ffff88003a2bda80: fc fc fc fc fc fc fc fc fc fc fc fc fb fb fb fb
> ^
> ffff88003a2bdb00: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
> ffff88003a2bdb80: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
> ==================================================================
What this means is that the gadgetfs_suspend() routine was trying to
access dev->lock after it had been deallocated. The root cause is a
race in the dummy_hcd driver; the dummy_udc_stop() routine can race
with the rest of the driver because it contains no locking. And even
when proper locking is added, it can still race with the
set_link_state() function because that function incorrectly drops the
private spinlock before invoking any gadget driver callbacks.
The result of this race, as seen above, is that set_link_state() can
invoke a callback in gadgetfs even after gadgetfs has been unbound
from dummy_hcd's UDC and its private data structures have been
deallocated.
include/linux/usb/gadget.h documents that the ->reset, ->disconnect,
->suspend, and ->resume callbacks may be invoked in interrupt context.
In general this is necessary, to prevent races with gadget driver
removal. This patch fixes dummy_hcd to retain the spinlock across
these calls, and it adds a spinlock acquisition to dummy_udc_stop() to
prevent the race.
The net2280 driver makes the same mistake of dropping the private
spinlock for its ->disconnect and ->reset callback invocations. The
patch fixes it too.
Lastly, since gadgetfs_suspend() may be invoked in interrupt context,
it cannot assume that interrupts are enabled when it runs. It must
use spin_lock_irqsave() instead of spin_lock_irq(). The patch fixes
that bug as well.
Signed-off-by: Alan Stern <stern@rowland.harvard.edu>
Reported-and-tested-by: Andrey Konovalov <andreyknvl@google.com>
CC: <stable@vger.kernel.org>
Acked-by: Felipe Balbi <felipe.balbi@linux.intel.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2017-06-14 03:23:42 +08:00
|
|
|
spin_lock_irq(&dum->lock);
|
USB: dummy-hcd: Fix erroneous synchronization change
A recent change to the synchronization in dummy-hcd was incorrect.
The issue was that dummy_udc_stop() contained no locking and therefore
could race with various gadget driver callbacks, and the fix was to
add locking and issue the callbacks with the private spinlock held.
UDC drivers aren't supposed to do this. Gadget driver callback
routines are allowed to invoke functions in the UDC driver, and these
functions will generally try to acquire the private spinlock. This
would deadlock the driver.
The correct solution is to drop the spinlock before issuing callbacks,
and avoid races by emulating the synchronize_irq() call that all real
UDC drivers must perform in their ->udc_stop() routines after
disabling interrupts. This involves adding a flag to dummy-hcd's
private structure to keep track of whether interrupts are supposed to
be enabled, and adding a counter to keep track of ongoing callbacks so
that dummy_udc_stop() can wait for them all to finish.
A real UDC driver won't receive disconnect, reset, suspend, resume, or
setup events once it has disabled interrupts. dummy-hcd will receive
them but won't try to issue any gadget driver callbacks, which should
be just as good.
Signed-off-by: Alan Stern <stern@rowland.harvard.edu>
Fixes: f16443a034c7 ("USB: gadgetfs, dummy-hcd, net2280: fix locking for callbacks")
CC: <stable@vger.kernel.org>
Signed-off-by: Felipe Balbi <felipe.balbi@linux.intel.com>
2017-09-27 03:15:49 +08:00
|
|
|
dum->ints_enabled = 0;
|
|
|
|
stop_activity(dum);
|
2005-04-17 06:20:36 +08:00
|
|
|
dum->driver = NULL;
|
USB: gadgetfs, dummy-hcd, net2280: fix locking for callbacks
Using the syzkaller kernel fuzzer, Andrey Konovalov generated the
following error in gadgetfs:
> BUG: KASAN: use-after-free in __lock_acquire+0x3069/0x3690
> kernel/locking/lockdep.c:3246
> Read of size 8 at addr ffff88003a2bdaf8 by task kworker/3:1/903
>
> CPU: 3 PID: 903 Comm: kworker/3:1 Not tainted 4.12.0-rc4+ #35
> Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS Bochs 01/01/2011
> Workqueue: usb_hub_wq hub_event
> Call Trace:
> __dump_stack lib/dump_stack.c:16 [inline]
> dump_stack+0x292/0x395 lib/dump_stack.c:52
> print_address_description+0x78/0x280 mm/kasan/report.c:252
> kasan_report_error mm/kasan/report.c:351 [inline]
> kasan_report+0x230/0x340 mm/kasan/report.c:408
> __asan_report_load8_noabort+0x19/0x20 mm/kasan/report.c:429
> __lock_acquire+0x3069/0x3690 kernel/locking/lockdep.c:3246
> lock_acquire+0x22d/0x560 kernel/locking/lockdep.c:3855
> __raw_spin_lock include/linux/spinlock_api_smp.h:142 [inline]
> _raw_spin_lock+0x2f/0x40 kernel/locking/spinlock.c:151
> spin_lock include/linux/spinlock.h:299 [inline]
> gadgetfs_suspend+0x89/0x130 drivers/usb/gadget/legacy/inode.c:1682
> set_link_state+0x88e/0xae0 drivers/usb/gadget/udc/dummy_hcd.c:455
> dummy_hub_control+0xd7e/0x1fb0 drivers/usb/gadget/udc/dummy_hcd.c:2074
> rh_call_control drivers/usb/core/hcd.c:689 [inline]
> rh_urb_enqueue drivers/usb/core/hcd.c:846 [inline]
> usb_hcd_submit_urb+0x92f/0x20b0 drivers/usb/core/hcd.c:1650
> usb_submit_urb+0x8b2/0x12c0 drivers/usb/core/urb.c:542
> usb_start_wait_urb+0x148/0x5b0 drivers/usb/core/message.c:56
> usb_internal_control_msg drivers/usb/core/message.c:100 [inline]
> usb_control_msg+0x341/0x4d0 drivers/usb/core/message.c:151
> usb_clear_port_feature+0x74/0xa0 drivers/usb/core/hub.c:412
> hub_port_disable+0x123/0x510 drivers/usb/core/hub.c:4177
> hub_port_init+0x1ed/0x2940 drivers/usb/core/hub.c:4648
> hub_port_connect drivers/usb/core/hub.c:4826 [inline]
> hub_port_connect_change drivers/usb/core/hub.c:4999 [inline]
> port_event drivers/usb/core/hub.c:5105 [inline]
> hub_event+0x1ae1/0x3d40 drivers/usb/core/hub.c:5185
> process_one_work+0xc08/0x1bd0 kernel/workqueue.c:2097
> process_scheduled_works kernel/workqueue.c:2157 [inline]
> worker_thread+0xb2b/0x1860 kernel/workqueue.c:2233
> kthread+0x363/0x440 kernel/kthread.c:231
> ret_from_fork+0x2a/0x40 arch/x86/entry/entry_64.S:424
>
> Allocated by task 9958:
> save_stack_trace+0x1b/0x20 arch/x86/kernel/stacktrace.c:59
> save_stack+0x43/0xd0 mm/kasan/kasan.c:513
> set_track mm/kasan/kasan.c:525 [inline]
> kasan_kmalloc+0xad/0xe0 mm/kasan/kasan.c:617
> kmem_cache_alloc_trace+0x87/0x280 mm/slub.c:2745
> kmalloc include/linux/slab.h:492 [inline]
> kzalloc include/linux/slab.h:665 [inline]
> dev_new drivers/usb/gadget/legacy/inode.c:170 [inline]
> gadgetfs_fill_super+0x24f/0x540 drivers/usb/gadget/legacy/inode.c:1993
> mount_single+0xf6/0x160 fs/super.c:1192
> gadgetfs_mount+0x31/0x40 drivers/usb/gadget/legacy/inode.c:2019
> mount_fs+0x9c/0x2d0 fs/super.c:1223
> vfs_kern_mount.part.25+0xcb/0x490 fs/namespace.c:976
> vfs_kern_mount fs/namespace.c:2509 [inline]
> do_new_mount fs/namespace.c:2512 [inline]
> do_mount+0x41b/0x2d90 fs/namespace.c:2834
> SYSC_mount fs/namespace.c:3050 [inline]
> SyS_mount+0xb0/0x120 fs/namespace.c:3027
> entry_SYSCALL_64_fastpath+0x1f/0xbe
>
> Freed by task 9960:
> save_stack_trace+0x1b/0x20 arch/x86/kernel/stacktrace.c:59
> save_stack+0x43/0xd0 mm/kasan/kasan.c:513
> set_track mm/kasan/kasan.c:525 [inline]
> kasan_slab_free+0x72/0xc0 mm/kasan/kasan.c:590
> slab_free_hook mm/slub.c:1357 [inline]
> slab_free_freelist_hook mm/slub.c:1379 [inline]
> slab_free mm/slub.c:2961 [inline]
> kfree+0xed/0x2b0 mm/slub.c:3882
> put_dev+0x124/0x160 drivers/usb/gadget/legacy/inode.c:163
> gadgetfs_kill_sb+0x33/0x60 drivers/usb/gadget/legacy/inode.c:2027
> deactivate_locked_super+0x8d/0xd0 fs/super.c:309
> deactivate_super+0x21e/0x310 fs/super.c:340
> cleanup_mnt+0xb7/0x150 fs/namespace.c:1112
> __cleanup_mnt+0x1b/0x20 fs/namespace.c:1119
> task_work_run+0x1a0/0x280 kernel/task_work.c:116
> exit_task_work include/linux/task_work.h:21 [inline]
> do_exit+0x18a8/0x2820 kernel/exit.c:878
> do_group_exit+0x14e/0x420 kernel/exit.c:982
> get_signal+0x784/0x1780 kernel/signal.c:2318
> do_signal+0xd7/0x2130 arch/x86/kernel/signal.c:808
> exit_to_usermode_loop+0x1ac/0x240 arch/x86/entry/common.c:157
> prepare_exit_to_usermode arch/x86/entry/common.c:194 [inline]
> syscall_return_slowpath+0x3ba/0x410 arch/x86/entry/common.c:263
> entry_SYSCALL_64_fastpath+0xbc/0xbe
>
> The buggy address belongs to the object at ffff88003a2bdae0
> which belongs to the cache kmalloc-1024 of size 1024
> The buggy address is located 24 bytes inside of
> 1024-byte region [ffff88003a2bdae0, ffff88003a2bdee0)
> The buggy address belongs to the page:
> page:ffffea0000e8ae00 count:1 mapcount:0 mapping: (null)
> index:0x0 compound_mapcount: 0
> flags: 0x100000000008100(slab|head)
> raw: 0100000000008100 0000000000000000 0000000000000000 0000000100170017
> raw: ffffea0000ed3020 ffffea0000f5f820 ffff88003e80efc0 0000000000000000
> page dumped because: kasan: bad access detected
>
> Memory state around the buggy address:
> ffff88003a2bd980: fb fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc
> ffff88003a2bda00: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc
> >ffff88003a2bda80: fc fc fc fc fc fc fc fc fc fc fc fc fb fb fb fb
> ^
> ffff88003a2bdb00: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
> ffff88003a2bdb80: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
> ==================================================================
What this means is that the gadgetfs_suspend() routine was trying to
access dev->lock after it had been deallocated. The root cause is a
race in the dummy_hcd driver; the dummy_udc_stop() routine can race
with the rest of the driver because it contains no locking. And even
when proper locking is added, it can still race with the
set_link_state() function because that function incorrectly drops the
private spinlock before invoking any gadget driver callbacks.
The result of this race, as seen above, is that set_link_state() can
invoke a callback in gadgetfs even after gadgetfs has been unbound
from dummy_hcd's UDC and its private data structures have been
deallocated.
include/linux/usb/gadget.h documents that the ->reset, ->disconnect,
->suspend, and ->resume callbacks may be invoked in interrupt context.
In general this is necessary, to prevent races with gadget driver
removal. This patch fixes dummy_hcd to retain the spinlock across
these calls, and it adds a spinlock acquisition to dummy_udc_stop() to
prevent the race.
The net2280 driver makes the same mistake of dropping the private
spinlock for its ->disconnect and ->reset callback invocations. The
patch fixes it too.
Lastly, since gadgetfs_suspend() may be invoked in interrupt context,
it cannot assume that interrupts are enabled when it runs. It must
use spin_lock_irqsave() instead of spin_lock_irq(). The patch fixes
that bug as well.
Signed-off-by: Alan Stern <stern@rowland.harvard.edu>
Reported-and-tested-by: Andrey Konovalov <andreyknvl@google.com>
CC: <stable@vger.kernel.org>
Acked-by: Felipe Balbi <felipe.balbi@linux.intel.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2017-06-14 03:23:42 +08:00
|
|
|
spin_unlock_irq(&dum->lock);
|
2011-06-17 02:36:55 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef is_enabled
|
|
|
|
|
2005-05-04 04:15:43 +08:00
|
|
|
/* The gadget structure is stored inside the hcd structure and will be
|
|
|
|
* released along with it. */
|
2011-06-23 20:26:12 +08:00
|
|
|
static void init_dummy_udc_hw(struct dummy *dum)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
INIT_LIST_HEAD(&dum->gadget.ep_list);
|
|
|
|
for (i = 0; i < DUMMY_ENDPOINTS; i++) {
|
|
|
|
struct dummy_ep *ep = &dum->ep[i];
|
|
|
|
|
2015-07-31 22:00:24 +08:00
|
|
|
if (!ep_info[i].name)
|
2011-06-23 20:26:12 +08:00
|
|
|
break;
|
2015-07-31 22:00:24 +08:00
|
|
|
ep->ep.name = ep_info[i].name;
|
|
|
|
ep->ep.caps = ep_info[i].caps;
|
2011-06-23 20:26:12 +08:00
|
|
|
ep->ep.ops = &dummy_ep_ops;
|
|
|
|
list_add_tail(&ep->ep.ep_list, &dum->gadget.ep_list);
|
|
|
|
ep->halted = ep->wedged = ep->already_seen =
|
|
|
|
ep->setup_stage = 0;
|
2013-12-13 19:23:38 +08:00
|
|
|
usb_ep_set_maxpacket_limit(&ep->ep, ~0);
|
2012-01-09 20:14:56 +08:00
|
|
|
ep->ep.max_streams = 16;
|
2011-06-23 20:26:12 +08:00
|
|
|
ep->last_io = jiffies;
|
|
|
|
ep->gadget = &dum->gadget;
|
|
|
|
ep->desc = NULL;
|
|
|
|
INIT_LIST_HEAD(&ep->queue);
|
|
|
|
}
|
|
|
|
|
|
|
|
dum->gadget.ep0 = &dum->ep[0].ep;
|
|
|
|
list_del_init(&dum->ep[0].ep.ep_list);
|
|
|
|
INIT_LIST_HEAD(&dum->fifo_req.queue);
|
2011-06-23 20:26:13 +08:00
|
|
|
|
|
|
|
#ifdef CONFIG_USB_OTG
|
|
|
|
dum->gadget.is_otg = 1;
|
|
|
|
#endif
|
2011-06-23 20:26:12 +08:00
|
|
|
}
|
|
|
|
|
2012-01-12 19:53:15 +08:00
|
|
|
static int dummy_udc_probe(struct platform_device *pdev)
|
2005-05-04 04:15:43 +08:00
|
|
|
{
|
2012-10-30 01:09:56 +08:00
|
|
|
struct dummy *dum;
|
2005-05-04 04:15:43 +08:00
|
|
|
int rc;
|
|
|
|
|
2012-10-30 01:09:56 +08:00
|
|
|
dum = *((void **)dev_get_platdata(&pdev->dev));
|
2017-02-28 14:25:45 +08:00
|
|
|
/* Clear usb_gadget region for new registration to udc-core */
|
|
|
|
memzero_explicit(&dum->gadget, sizeof(struct usb_gadget));
|
2005-05-04 04:15:43 +08:00
|
|
|
dum->gadget.name = gadget_name;
|
|
|
|
dum->gadget.ops = &dummy_ops;
|
2017-09-27 03:15:22 +08:00
|
|
|
if (mod_data.is_super_speed)
|
|
|
|
dum->gadget.max_speed = USB_SPEED_SUPER;
|
|
|
|
else if (mod_data.is_high_speed)
|
|
|
|
dum->gadget.max_speed = USB_SPEED_HIGH;
|
|
|
|
else
|
|
|
|
dum->gadget.max_speed = USB_SPEED_FULL;
|
2005-05-04 04:15:43 +08:00
|
|
|
|
2005-11-15 01:16:30 +08:00
|
|
|
dum->gadget.dev.parent = &pdev->dev;
|
2011-06-23 20:26:12 +08:00
|
|
|
init_dummy_udc_hw(dum);
|
|
|
|
|
2011-06-28 21:33:47 +08:00
|
|
|
rc = usb_add_gadget_udc(&pdev->dev, &dum->gadget);
|
|
|
|
if (rc < 0)
|
|
|
|
goto err_udc;
|
|
|
|
|
2012-01-12 19:53:15 +08:00
|
|
|
rc = device_create_file(&dum->gadget.dev, &dev_attr_function);
|
2006-09-25 23:55:56 +08:00
|
|
|
if (rc < 0)
|
2011-06-28 21:33:47 +08:00
|
|
|
goto err_dev;
|
|
|
|
platform_set_drvdata(pdev, dum);
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
err_dev:
|
|
|
|
usb_del_gadget_udc(&dum->gadget);
|
|
|
|
err_udc:
|
2005-05-04 04:15:43 +08:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2012-01-12 19:53:15 +08:00
|
|
|
static int dummy_udc_remove(struct platform_device *pdev)
|
2005-05-04 04:15:43 +08:00
|
|
|
{
|
2012-01-12 19:53:15 +08:00
|
|
|
struct dummy *dum = platform_get_drvdata(pdev);
|
2005-05-04 04:15:43 +08:00
|
|
|
|
2012-01-12 19:53:15 +08:00
|
|
|
device_remove_file(&dum->gadget.dev, &dev_attr_function);
|
2013-07-31 03:18:15 +08:00
|
|
|
usb_del_gadget_udc(&dum->gadget);
|
2005-05-04 04:15:43 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-06-18 01:43:13 +08:00
|
|
|
static void dummy_udc_pm(struct dummy *dum, struct dummy_hcd *dum_hcd,
|
|
|
|
int suspend)
|
2005-05-11 03:34:16 +08:00
|
|
|
{
|
2011-06-18 01:43:13 +08:00
|
|
|
spin_lock_irq(&dum->lock);
|
|
|
|
dum->udc_suspended = suspend;
|
2011-06-17 16:11:41 +08:00
|
|
|
set_link_state(dum_hcd);
|
2011-06-18 01:43:13 +08:00
|
|
|
spin_unlock_irq(&dum->lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int dummy_udc_suspend(struct platform_device *pdev, pm_message_t state)
|
|
|
|
{
|
|
|
|
struct dummy *dum = platform_get_drvdata(pdev);
|
|
|
|
struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
|
2005-05-11 03:34:16 +08:00
|
|
|
|
2011-06-18 01:43:13 +08:00
|
|
|
dev_dbg(&pdev->dev, "%s\n", __func__);
|
|
|
|
dummy_udc_pm(dum, dum_hcd, 1);
|
2011-06-17 16:11:41 +08:00
|
|
|
usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
|
2005-05-11 03:34:16 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-06-18 01:43:13 +08:00
|
|
|
static int dummy_udc_resume(struct platform_device *pdev)
|
2005-05-11 03:34:16 +08:00
|
|
|
{
|
2011-06-18 01:43:13 +08:00
|
|
|
struct dummy *dum = platform_get_drvdata(pdev);
|
|
|
|
struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
|
2005-05-11 03:34:16 +08:00
|
|
|
|
2011-06-18 01:43:13 +08:00
|
|
|
dev_dbg(&pdev->dev, "%s\n", __func__);
|
|
|
|
dummy_udc_pm(dum, dum_hcd, 0);
|
2011-06-17 16:11:41 +08:00
|
|
|
usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
|
2005-05-11 03:34:16 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-11-10 06:32:44 +08:00
|
|
|
static struct platform_driver dummy_udc_driver = {
|
2005-05-04 04:15:43 +08:00
|
|
|
.probe = dummy_udc_probe,
|
|
|
|
.remove = dummy_udc_remove,
|
2005-05-11 03:34:16 +08:00
|
|
|
.suspend = dummy_udc_suspend,
|
|
|
|
.resume = dummy_udc_resume,
|
2005-11-10 06:32:44 +08:00
|
|
|
.driver = {
|
|
|
|
.name = (char *) gadget_name,
|
|
|
|
},
|
2005-05-04 04:15:43 +08:00
|
|
|
};
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
|
2012-01-14 04:51:47 +08:00
|
|
|
static unsigned int dummy_get_ep_idx(const struct usb_endpoint_descriptor *desc)
|
|
|
|
{
|
|
|
|
unsigned int index;
|
|
|
|
|
|
|
|
index = usb_endpoint_num(desc) << 1;
|
|
|
|
if (usb_endpoint_dir_in(desc))
|
|
|
|
index |= 1;
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/* MASTER/HOST SIDE DRIVER
|
|
|
|
*
|
|
|
|
* this uses the hcd framework to hook up to host side drivers.
|
|
|
|
* its root hub will only have one device, otherwise it acts like
|
|
|
|
* a normal host controller.
|
|
|
|
*
|
|
|
|
* when urbs are queued, they're just stuck on a list that we
|
|
|
|
* scan in a timer callback. that callback connects writes from
|
|
|
|
* the host with reads from the device, and so on, based on the
|
|
|
|
* usb 2.0 rules.
|
|
|
|
*/
|
|
|
|
|
2012-01-14 04:51:47 +08:00
|
|
|
static int dummy_ep_stream_en(struct dummy_hcd *dum_hcd, struct urb *urb)
|
|
|
|
{
|
|
|
|
const struct usb_endpoint_descriptor *desc = &urb->ep->desc;
|
|
|
|
u32 index;
|
|
|
|
|
|
|
|
if (!usb_endpoint_xfer_bulk(desc))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
index = dummy_get_ep_idx(desc);
|
|
|
|
return (1 << index) & dum_hcd->stream_en_ep;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The max stream number is saved as a nibble so for the 30 possible endpoints
|
|
|
|
* we only 15 bytes of memory. Therefore we are limited to max 16 streams (0
|
|
|
|
* means we use only 1 stream). The maximum according to the spec is 16bit so
|
|
|
|
* if the 16 stream limit is about to go, the array size should be incremented
|
|
|
|
* to 30 elements of type u16.
|
|
|
|
*/
|
|
|
|
static int get_max_streams_for_pipe(struct dummy_hcd *dum_hcd,
|
|
|
|
unsigned int pipe)
|
|
|
|
{
|
|
|
|
int max_streams;
|
|
|
|
|
|
|
|
max_streams = dum_hcd->num_stream[usb_pipeendpoint(pipe)];
|
|
|
|
if (usb_pipeout(pipe))
|
|
|
|
max_streams >>= 4;
|
|
|
|
else
|
|
|
|
max_streams &= 0xf;
|
|
|
|
max_streams++;
|
|
|
|
return max_streams;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void set_max_streams_for_pipe(struct dummy_hcd *dum_hcd,
|
|
|
|
unsigned int pipe, unsigned int streams)
|
|
|
|
{
|
|
|
|
int max_streams;
|
|
|
|
|
|
|
|
streams--;
|
|
|
|
max_streams = dum_hcd->num_stream[usb_pipeendpoint(pipe)];
|
|
|
|
if (usb_pipeout(pipe)) {
|
|
|
|
streams <<= 4;
|
|
|
|
max_streams &= 0xf;
|
|
|
|
} else {
|
|
|
|
max_streams &= 0xf0;
|
|
|
|
}
|
|
|
|
max_streams |= streams;
|
|
|
|
dum_hcd->num_stream[usb_pipeendpoint(pipe)] = max_streams;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int dummy_validate_stream(struct dummy_hcd *dum_hcd, struct urb *urb)
|
|
|
|
{
|
|
|
|
unsigned int max_streams;
|
|
|
|
int enabled;
|
|
|
|
|
|
|
|
enabled = dummy_ep_stream_en(dum_hcd, urb);
|
|
|
|
if (!urb->stream_id) {
|
|
|
|
if (enabled)
|
|
|
|
return -EINVAL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (!enabled)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
max_streams = get_max_streams_for_pipe(dum_hcd,
|
|
|
|
usb_pipeendpoint(urb->pipe));
|
|
|
|
if (urb->stream_id > max_streams) {
|
|
|
|
dev_err(dummy_dev(dum_hcd), "Stream id %d is out of range.\n",
|
|
|
|
urb->stream_id);
|
|
|
|
BUG();
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-01-12 19:53:15 +08:00
|
|
|
static int dummy_urb_enqueue(
|
2005-04-17 06:20:36 +08:00
|
|
|
struct usb_hcd *hcd,
|
|
|
|
struct urb *urb,
|
2005-10-21 15:21:58 +08:00
|
|
|
gfp_t mem_flags
|
2005-04-17 06:20:36 +08:00
|
|
|
) {
|
2011-06-29 21:41:51 +08:00
|
|
|
struct dummy_hcd *dum_hcd;
|
2005-04-17 06:20:36 +08:00
|
|
|
struct urbp *urbp;
|
|
|
|
unsigned long flags;
|
2007-08-08 23:48:02 +08:00
|
|
|
int rc;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2012-01-12 19:53:15 +08:00
|
|
|
urbp = kmalloc(sizeof *urbp, mem_flags);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (!urbp)
|
|
|
|
return -ENOMEM;
|
|
|
|
urbp->urb = urb;
|
2012-01-10 02:30:50 +08:00
|
|
|
urbp->miter_started = 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2011-06-29 21:41:51 +08:00
|
|
|
dum_hcd = hcd_to_dummy_hcd(hcd);
|
|
|
|
spin_lock_irqsave(&dum_hcd->dum->lock, flags);
|
2012-01-14 04:51:47 +08:00
|
|
|
|
|
|
|
rc = dummy_validate_stream(dum_hcd, urb);
|
|
|
|
if (rc) {
|
|
|
|
kfree(urbp);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2007-08-08 23:48:02 +08:00
|
|
|
rc = usb_hcd_link_urb_to_ep(hcd, urb);
|
|
|
|
if (rc) {
|
|
|
|
kfree(urbp);
|
|
|
|
goto done;
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2011-06-29 21:41:51 +08:00
|
|
|
if (!dum_hcd->udev) {
|
|
|
|
dum_hcd->udev = urb->dev;
|
|
|
|
usb_get_dev(dum_hcd->udev);
|
|
|
|
} else if (unlikely(dum_hcd->udev != urb->dev))
|
|
|
|
dev_err(dummy_dev(dum_hcd), "usb_device address has changed!\n");
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2011-06-29 21:41:51 +08:00
|
|
|
list_add_tail(&urbp->urbp_list, &dum_hcd->urbp_list);
|
2005-04-17 06:20:36 +08:00
|
|
|
urb->hcpriv = urbp;
|
2017-09-27 03:15:40 +08:00
|
|
|
if (!dum_hcd->next_frame_urbp)
|
|
|
|
dum_hcd->next_frame_urbp = urbp;
|
2012-01-12 19:53:15 +08:00
|
|
|
if (usb_pipetype(urb->pipe) == PIPE_CONTROL)
|
2005-04-17 06:20:36 +08:00
|
|
|
urb->error_count = 1; /* mark as a new urb */
|
|
|
|
|
|
|
|
/* kick the scheduler, it'll do the rest */
|
2011-06-29 21:41:51 +08:00
|
|
|
if (!timer_pending(&dum_hcd->timer))
|
|
|
|
mod_timer(&dum_hcd->timer, jiffies + 1);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2007-08-08 23:48:02 +08:00
|
|
|
done:
|
2011-06-29 21:41:51 +08:00
|
|
|
spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
|
2007-08-08 23:48:02 +08:00
|
|
|
return rc;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2007-08-08 23:48:02 +08:00
|
|
|
static int dummy_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2011-06-29 21:41:51 +08:00
|
|
|
struct dummy_hcd *dum_hcd;
|
2005-05-11 03:34:16 +08:00
|
|
|
unsigned long flags;
|
2007-08-08 23:48:02 +08:00
|
|
|
int rc;
|
2005-05-11 03:34:16 +08:00
|
|
|
|
|
|
|
/* giveback happens automatically in timer callback,
|
|
|
|
* so make sure the callback happens */
|
2011-06-29 21:41:51 +08:00
|
|
|
dum_hcd = hcd_to_dummy_hcd(hcd);
|
|
|
|
spin_lock_irqsave(&dum_hcd->dum->lock, flags);
|
2007-08-08 23:48:02 +08:00
|
|
|
|
|
|
|
rc = usb_hcd_check_unlink_urb(hcd, urb, status);
|
2011-06-29 21:41:51 +08:00
|
|
|
if (!rc && dum_hcd->rh_state != DUMMY_RH_RUNNING &&
|
|
|
|
!list_empty(&dum_hcd->urbp_list))
|
|
|
|
mod_timer(&dum_hcd->timer, jiffies);
|
2007-08-08 23:48:02 +08:00
|
|
|
|
2011-06-29 21:41:51 +08:00
|
|
|
spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
|
2007-08-08 23:48:02 +08:00
|
|
|
return rc;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2012-01-09 20:14:57 +08:00
|
|
|
static int dummy_perform_transfer(struct urb *urb, struct dummy_request *req,
|
|
|
|
u32 len)
|
|
|
|
{
|
|
|
|
void *ubuf, *rbuf;
|
2012-01-10 02:30:50 +08:00
|
|
|
struct urbp *urbp = urb->hcpriv;
|
2012-01-09 20:14:57 +08:00
|
|
|
int to_host;
|
2012-01-10 02:30:50 +08:00
|
|
|
struct sg_mapping_iter *miter = &urbp->miter;
|
|
|
|
u32 trans = 0;
|
|
|
|
u32 this_sg;
|
|
|
|
bool next_sg;
|
2012-01-09 20:14:57 +08:00
|
|
|
|
2024-06-11 20:08:33 +08:00
|
|
|
to_host = usb_urb_dir_in(urb);
|
2012-01-09 20:14:57 +08:00
|
|
|
rbuf = req->req.buf + req->req.actual;
|
|
|
|
|
2012-01-10 02:30:50 +08:00
|
|
|
if (!urb->num_sgs) {
|
|
|
|
ubuf = urb->transfer_buffer + urb->actual_length;
|
|
|
|
if (to_host)
|
|
|
|
memcpy(ubuf, rbuf, len);
|
|
|
|
else
|
|
|
|
memcpy(rbuf, ubuf, len);
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!urbp->miter_started) {
|
|
|
|
u32 flags = SG_MITER_ATOMIC;
|
|
|
|
|
|
|
|
if (to_host)
|
|
|
|
flags |= SG_MITER_TO_SG;
|
|
|
|
else
|
|
|
|
flags |= SG_MITER_FROM_SG;
|
|
|
|
|
|
|
|
sg_miter_start(miter, urb->sg, urb->num_sgs, flags);
|
|
|
|
urbp->miter_started = 1;
|
|
|
|
}
|
|
|
|
next_sg = sg_miter_next(miter);
|
|
|
|
if (next_sg == false) {
|
|
|
|
WARN_ON_ONCE(1);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
do {
|
|
|
|
ubuf = miter->addr;
|
|
|
|
this_sg = min_t(u32, len, miter->length);
|
|
|
|
miter->consumed = this_sg;
|
|
|
|
trans += this_sg;
|
|
|
|
|
|
|
|
if (to_host)
|
|
|
|
memcpy(ubuf, rbuf, this_sg);
|
|
|
|
else
|
|
|
|
memcpy(rbuf, ubuf, this_sg);
|
|
|
|
len -= this_sg;
|
|
|
|
|
|
|
|
if (!len)
|
|
|
|
break;
|
|
|
|
next_sg = sg_miter_next(miter);
|
|
|
|
if (next_sg == false) {
|
|
|
|
WARN_ON_ONCE(1);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
rbuf += this_sg;
|
|
|
|
} while (1);
|
|
|
|
|
|
|
|
sg_miter_stop(miter);
|
|
|
|
return trans;
|
2012-01-09 20:14:57 +08:00
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/* transfer up to a frame's worth; caller must own lock */
|
2012-01-14 04:51:47 +08:00
|
|
|
static int transfer(struct dummy_hcd *dum_hcd, struct urb *urb,
|
|
|
|
struct dummy_ep *ep, int limit, int *status)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2012-01-14 04:51:47 +08:00
|
|
|
struct dummy *dum = dum_hcd->dum;
|
2005-04-17 06:20:36 +08:00
|
|
|
struct dummy_request *req;
|
2015-09-15 22:55:32 +08:00
|
|
|
int sent = 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
top:
|
|
|
|
/* if there's no request queued, the device is NAKing; return */
|
2012-01-12 19:53:15 +08:00
|
|
|
list_for_each_entry(req, &ep->queue, queue) {
|
2005-04-17 06:20:36 +08:00
|
|
|
unsigned host_len, dev_len, len;
|
|
|
|
int is_short, to_host;
|
|
|
|
int rescan = 0;
|
|
|
|
|
2012-01-14 04:51:47 +08:00
|
|
|
if (dummy_ep_stream_en(dum_hcd, urb)) {
|
|
|
|
if ((urb->stream_id != req->req.stream_id))
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/* 1..N packets of ep->ep.maxpacket each ... the last one
|
|
|
|
* may be short (including zero length).
|
|
|
|
*
|
|
|
|
* writer can send a zlp explicitly (length 0) or implicitly
|
|
|
|
* (length mod maxpacket zero, and 'zero' flag); they always
|
|
|
|
* terminate reads.
|
|
|
|
*/
|
|
|
|
host_len = urb->transfer_buffer_length - urb->actual_length;
|
|
|
|
dev_len = req->req.length - req->req.actual;
|
2012-01-12 19:53:15 +08:00
|
|
|
len = min(host_len, dev_len);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/* FIXME update emulated data toggle too */
|
|
|
|
|
2024-06-11 20:08:33 +08:00
|
|
|
to_host = usb_urb_dir_in(urb);
|
2012-01-12 19:53:15 +08:00
|
|
|
if (unlikely(len == 0))
|
2005-04-17 06:20:36 +08:00
|
|
|
is_short = 1;
|
|
|
|
else {
|
|
|
|
/* not enough bandwidth left? */
|
|
|
|
if (limit < ep->ep.maxpacket && limit < len)
|
|
|
|
break;
|
2012-01-12 19:53:15 +08:00
|
|
|
len = min_t(unsigned, len, limit);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (len == 0)
|
|
|
|
break;
|
|
|
|
|
2015-09-15 22:55:31 +08:00
|
|
|
/* send multiple of maxpacket first, then remainder */
|
|
|
|
if (len >= ep->ep.maxpacket) {
|
|
|
|
is_short = 0;
|
|
|
|
if (len % ep->ep.maxpacket)
|
|
|
|
rescan = 1;
|
|
|
|
len -= len % ep->ep.maxpacket;
|
|
|
|
} else {
|
|
|
|
is_short = 1;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2012-01-09 20:14:57 +08:00
|
|
|
len = dummy_perform_transfer(urb, req, len);
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
ep->last_io = jiffies;
|
2012-03-03 02:51:00 +08:00
|
|
|
if ((int)len < 0) {
|
2012-01-10 02:30:50 +08:00
|
|
|
req->req.status = len;
|
|
|
|
} else {
|
|
|
|
limit -= len;
|
2015-09-15 22:55:32 +08:00
|
|
|
sent += len;
|
2012-01-10 02:30:50 +08:00
|
|
|
urb->actual_length += len;
|
|
|
|
req->req.actual += len;
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* short packets terminate, maybe with overflow/underflow.
|
|
|
|
* it's only really an error to write too much.
|
|
|
|
*
|
|
|
|
* partially filling a buffer optionally blocks queue advances
|
|
|
|
* (so completion handlers can clean up the queue) but we don't
|
2007-08-22 03:39:21 +08:00
|
|
|
* need to emulate such data-in-flight.
|
2005-04-17 06:20:36 +08:00
|
|
|
*/
|
|
|
|
if (is_short) {
|
|
|
|
if (host_len == dev_len) {
|
|
|
|
req->req.status = 0;
|
2007-08-25 03:40:10 +08:00
|
|
|
*status = 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
} else if (to_host) {
|
|
|
|
req->req.status = 0;
|
|
|
|
if (dev_len > host_len)
|
2007-08-25 03:40:10 +08:00
|
|
|
*status = -EOVERFLOW;
|
2005-04-17 06:20:36 +08:00
|
|
|
else
|
2007-08-25 03:40:10 +08:00
|
|
|
*status = 0;
|
2015-09-15 22:55:30 +08:00
|
|
|
} else {
|
2007-08-25 03:40:10 +08:00
|
|
|
*status = 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
if (host_len > dev_len)
|
|
|
|
req->req.status = -EOVERFLOW;
|
|
|
|
else
|
|
|
|
req->req.status = 0;
|
|
|
|
}
|
|
|
|
|
2015-09-15 22:55:29 +08:00
|
|
|
/*
|
|
|
|
* many requests terminate without a short packet.
|
|
|
|
* send a zlp if demanded by flags.
|
|
|
|
*/
|
2005-04-17 06:20:36 +08:00
|
|
|
} else {
|
2015-09-15 22:55:29 +08:00
|
|
|
if (req->req.length == req->req.actual) {
|
|
|
|
if (req->req.zero && to_host)
|
|
|
|
rescan = 1;
|
|
|
|
else
|
|
|
|
req->req.status = 0;
|
|
|
|
}
|
|
|
|
if (urb->transfer_buffer_length == urb->actual_length) {
|
|
|
|
if (urb->transfer_flags & URB_ZERO_PACKET &&
|
|
|
|
!to_host)
|
|
|
|
rescan = 1;
|
|
|
|
else
|
|
|
|
*status = 0;
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* device side completion --> continuable */
|
|
|
|
if (req->req.status != -EINPROGRESS) {
|
2012-01-12 19:53:15 +08:00
|
|
|
list_del_init(&req->queue);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2012-01-12 19:53:15 +08:00
|
|
|
spin_unlock(&dum->lock);
|
2014-09-25 04:43:19 +08:00
|
|
|
usb_gadget_giveback_request(&ep->ep, &req->req);
|
2012-01-12 19:53:15 +08:00
|
|
|
spin_lock(&dum->lock);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/* requests might have been unlinked... */
|
|
|
|
rescan = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* host side completion --> terminate */
|
2007-08-25 03:40:10 +08:00
|
|
|
if (*status != -EINPROGRESS)
|
2005-04-17 06:20:36 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* rescan to continue with any other queued i/o */
|
|
|
|
if (rescan)
|
|
|
|
goto top;
|
|
|
|
}
|
2015-09-15 22:55:32 +08:00
|
|
|
return sent;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2012-01-12 19:53:15 +08:00
|
|
|
static int periodic_bytes(struct dummy *dum, struct dummy_ep *ep)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
int limit = ep->ep.maxpacket;
|
|
|
|
|
|
|
|
if (dum->gadget.speed == USB_SPEED_HIGH) {
|
|
|
|
int tmp;
|
|
|
|
|
|
|
|
/* high bandwidth mode */
|
2016-09-28 18:30:59 +08:00
|
|
|
tmp = usb_endpoint_maxp_mult(ep->desc);
|
2005-04-17 06:20:36 +08:00
|
|
|
tmp *= 8 /* applies to entire frame */;
|
|
|
|
limit += limit * tmp;
|
|
|
|
}
|
2011-06-29 21:41:52 +08:00
|
|
|
if (dum->gadget.speed == USB_SPEED_SUPER) {
|
2012-01-12 19:53:14 +08:00
|
|
|
switch (usb_endpoint_type(ep->desc)) {
|
2011-06-29 21:41:52 +08:00
|
|
|
case USB_ENDPOINT_XFER_ISOC:
|
|
|
|
/* Sec. 4.4.8.2 USB3.0 Spec */
|
|
|
|
limit = 3 * 16 * 1024 * 8;
|
|
|
|
break;
|
|
|
|
case USB_ENDPOINT_XFER_INT:
|
|
|
|
/* Sec. 4.4.7.2 USB3.0 Spec */
|
|
|
|
limit = 3 * 1024 * 8;
|
|
|
|
break;
|
|
|
|
case USB_ENDPOINT_XFER_BULK:
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
return limit;
|
|
|
|
}
|
|
|
|
|
2011-06-29 21:41:51 +08:00
|
|
|
#define is_active(dum_hcd) ((dum_hcd->port_status & \
|
2005-04-17 06:20:36 +08:00
|
|
|
(USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE | \
|
|
|
|
USB_PORT_STAT_SUSPEND)) \
|
|
|
|
== (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE))
|
|
|
|
|
2012-01-12 19:53:15 +08:00
|
|
|
static struct dummy_ep *find_endpoint(struct dummy *dum, u8 address)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2011-06-29 21:41:52 +08:00
|
|
|
if (!is_active((dum->gadget.speed == USB_SPEED_SUPER ?
|
|
|
|
dum->ss_hcd : dum->hs_hcd)))
|
2005-04-17 06:20:36 +08:00
|
|
|
return NULL;
|
USB: dummy-hcd: Fix erroneous synchronization change
A recent change to the synchronization in dummy-hcd was incorrect.
The issue was that dummy_udc_stop() contained no locking and therefore
could race with various gadget driver callbacks, and the fix was to
add locking and issue the callbacks with the private spinlock held.
UDC drivers aren't supposed to do this. Gadget driver callback
routines are allowed to invoke functions in the UDC driver, and these
functions will generally try to acquire the private spinlock. This
would deadlock the driver.
The correct solution is to drop the spinlock before issuing callbacks,
and avoid races by emulating the synchronize_irq() call that all real
UDC drivers must perform in their ->udc_stop() routines after
disabling interrupts. This involves adding a flag to dummy-hcd's
private structure to keep track of whether interrupts are supposed to
be enabled, and adding a counter to keep track of ongoing callbacks so
that dummy_udc_stop() can wait for them all to finish.
A real UDC driver won't receive disconnect, reset, suspend, resume, or
setup events once it has disabled interrupts. dummy-hcd will receive
them but won't try to issue any gadget driver callbacks, which should
be just as good.
Signed-off-by: Alan Stern <stern@rowland.harvard.edu>
Fixes: f16443a034c7 ("USB: gadgetfs, dummy-hcd, net2280: fix locking for callbacks")
CC: <stable@vger.kernel.org>
Signed-off-by: Felipe Balbi <felipe.balbi@linux.intel.com>
2017-09-27 03:15:49 +08:00
|
|
|
if (!dum->ints_enabled)
|
|
|
|
return NULL;
|
2005-04-17 06:20:36 +08:00
|
|
|
if ((address & ~USB_DIR_IN) == 0)
|
2012-01-12 19:53:15 +08:00
|
|
|
return &dum->ep[0];
|
2005-04-17 06:20:36 +08:00
|
|
|
for (i = 1; i < DUMMY_ENDPOINTS; i++) {
|
2012-01-12 19:53:15 +08:00
|
|
|
struct dummy_ep *ep = &dum->ep[i];
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
if (!ep->desc)
|
|
|
|
continue;
|
|
|
|
if (ep->desc->bEndpointAddress == address)
|
|
|
|
return ep;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef is_active
|
|
|
|
|
|
|
|
#define Dev_Request (USB_TYPE_STANDARD | USB_RECIP_DEVICE)
|
|
|
|
#define Dev_InRequest (Dev_Request | USB_DIR_IN)
|
|
|
|
#define Intf_Request (USB_TYPE_STANDARD | USB_RECIP_INTERFACE)
|
|
|
|
#define Intf_InRequest (Intf_Request | USB_DIR_IN)
|
|
|
|
#define Ep_Request (USB_TYPE_STANDARD | USB_RECIP_ENDPOINT)
|
|
|
|
#define Ep_InRequest (Ep_Request | USB_DIR_IN)
|
|
|
|
|
2010-11-01 23:38:05 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* handle_control_request() - handles all control transfers
|
|
|
|
* @dum: pointer to dummy (the_controller)
|
|
|
|
* @urb: the urb request to handle
|
|
|
|
* @setup: pointer to the setup data for a USB device control
|
|
|
|
* request
|
|
|
|
* @status: pointer to request handling status
|
|
|
|
*
|
|
|
|
* Return 0 - if the request was handled
|
|
|
|
* 1 - if the request wasn't handles
|
|
|
|
* error code on error
|
|
|
|
*/
|
2011-06-29 21:41:51 +08:00
|
|
|
static int handle_control_request(struct dummy_hcd *dum_hcd, struct urb *urb,
|
2010-11-01 23:38:05 +08:00
|
|
|
struct usb_ctrlrequest *setup,
|
|
|
|
int *status)
|
|
|
|
{
|
|
|
|
struct dummy_ep *ep2;
|
2011-06-29 21:41:51 +08:00
|
|
|
struct dummy *dum = dum_hcd->dum;
|
2010-11-01 23:38:05 +08:00
|
|
|
int ret_val = 1;
|
|
|
|
unsigned w_index;
|
|
|
|
unsigned w_value;
|
|
|
|
|
|
|
|
w_index = le16_to_cpu(setup->wIndex);
|
|
|
|
w_value = le16_to_cpu(setup->wValue);
|
|
|
|
switch (setup->bRequest) {
|
|
|
|
case USB_REQ_SET_ADDRESS:
|
|
|
|
if (setup->bRequestType != Dev_Request)
|
|
|
|
break;
|
|
|
|
dum->address = w_value;
|
|
|
|
*status = 0;
|
|
|
|
dev_dbg(udc_dev(dum), "set_address = %d\n",
|
|
|
|
w_value);
|
|
|
|
ret_val = 0;
|
|
|
|
break;
|
|
|
|
case USB_REQ_SET_FEATURE:
|
|
|
|
if (setup->bRequestType == Dev_Request) {
|
|
|
|
ret_val = 0;
|
|
|
|
switch (w_value) {
|
|
|
|
case USB_DEVICE_REMOTE_WAKEUP:
|
|
|
|
break;
|
|
|
|
case USB_DEVICE_B_HNP_ENABLE:
|
|
|
|
dum->gadget.b_hnp_enable = 1;
|
|
|
|
break;
|
|
|
|
case USB_DEVICE_A_HNP_SUPPORT:
|
|
|
|
dum->gadget.a_hnp_support = 1;
|
|
|
|
break;
|
|
|
|
case USB_DEVICE_A_ALT_HNP_SUPPORT:
|
|
|
|
dum->gadget.a_alt_hnp_support = 1;
|
|
|
|
break;
|
2011-06-29 21:41:52 +08:00
|
|
|
case USB_DEVICE_U1_ENABLE:
|
|
|
|
if (dummy_hcd_to_hcd(dum_hcd)->speed ==
|
|
|
|
HCD_USB3)
|
|
|
|
w_value = USB_DEV_STAT_U1_ENABLED;
|
|
|
|
else
|
|
|
|
ret_val = -EOPNOTSUPP;
|
|
|
|
break;
|
|
|
|
case USB_DEVICE_U2_ENABLE:
|
|
|
|
if (dummy_hcd_to_hcd(dum_hcd)->speed ==
|
|
|
|
HCD_USB3)
|
|
|
|
w_value = USB_DEV_STAT_U2_ENABLED;
|
|
|
|
else
|
|
|
|
ret_val = -EOPNOTSUPP;
|
|
|
|
break;
|
|
|
|
case USB_DEVICE_LTM_ENABLE:
|
|
|
|
if (dummy_hcd_to_hcd(dum_hcd)->speed ==
|
|
|
|
HCD_USB3)
|
|
|
|
w_value = USB_DEV_STAT_LTM_ENABLED;
|
|
|
|
else
|
|
|
|
ret_val = -EOPNOTSUPP;
|
|
|
|
break;
|
2010-11-01 23:38:05 +08:00
|
|
|
default:
|
|
|
|
ret_val = -EOPNOTSUPP;
|
|
|
|
}
|
|
|
|
if (ret_val == 0) {
|
|
|
|
dum->devstatus |= (1 << w_value);
|
|
|
|
*status = 0;
|
|
|
|
}
|
|
|
|
} else if (setup->bRequestType == Ep_Request) {
|
|
|
|
/* endpoint halt */
|
|
|
|
ep2 = find_endpoint(dum, w_index);
|
|
|
|
if (!ep2 || ep2->ep.name == ep0name) {
|
|
|
|
ret_val = -EOPNOTSUPP;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ep2->halted = 1;
|
|
|
|
ret_val = 0;
|
|
|
|
*status = 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case USB_REQ_CLEAR_FEATURE:
|
|
|
|
if (setup->bRequestType == Dev_Request) {
|
|
|
|
ret_val = 0;
|
|
|
|
switch (w_value) {
|
|
|
|
case USB_DEVICE_REMOTE_WAKEUP:
|
|
|
|
w_value = USB_DEVICE_REMOTE_WAKEUP;
|
|
|
|
break;
|
2011-06-29 21:41:52 +08:00
|
|
|
case USB_DEVICE_U1_ENABLE:
|
|
|
|
if (dummy_hcd_to_hcd(dum_hcd)->speed ==
|
|
|
|
HCD_USB3)
|
|
|
|
w_value = USB_DEV_STAT_U1_ENABLED;
|
|
|
|
else
|
|
|
|
ret_val = -EOPNOTSUPP;
|
|
|
|
break;
|
|
|
|
case USB_DEVICE_U2_ENABLE:
|
|
|
|
if (dummy_hcd_to_hcd(dum_hcd)->speed ==
|
|
|
|
HCD_USB3)
|
|
|
|
w_value = USB_DEV_STAT_U2_ENABLED;
|
|
|
|
else
|
|
|
|
ret_val = -EOPNOTSUPP;
|
|
|
|
break;
|
|
|
|
case USB_DEVICE_LTM_ENABLE:
|
|
|
|
if (dummy_hcd_to_hcd(dum_hcd)->speed ==
|
|
|
|
HCD_USB3)
|
|
|
|
w_value = USB_DEV_STAT_LTM_ENABLED;
|
|
|
|
else
|
|
|
|
ret_val = -EOPNOTSUPP;
|
|
|
|
break;
|
2010-11-01 23:38:05 +08:00
|
|
|
default:
|
|
|
|
ret_val = -EOPNOTSUPP;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (ret_val == 0) {
|
|
|
|
dum->devstatus &= ~(1 << w_value);
|
|
|
|
*status = 0;
|
|
|
|
}
|
|
|
|
} else if (setup->bRequestType == Ep_Request) {
|
|
|
|
/* endpoint halt */
|
|
|
|
ep2 = find_endpoint(dum, w_index);
|
|
|
|
if (!ep2) {
|
|
|
|
ret_val = -EOPNOTSUPP;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!ep2->wedged)
|
|
|
|
ep2->halted = 0;
|
|
|
|
ret_val = 0;
|
|
|
|
*status = 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case USB_REQ_GET_STATUS:
|
|
|
|
if (setup->bRequestType == Dev_InRequest
|
|
|
|
|| setup->bRequestType == Intf_InRequest
|
|
|
|
|| setup->bRequestType == Ep_InRequest) {
|
|
|
|
char *buf;
|
|
|
|
/*
|
|
|
|
* device: remote wakeup, selfpowered
|
|
|
|
* interface: nothing
|
|
|
|
* endpoint: halt
|
|
|
|
*/
|
|
|
|
buf = (char *)urb->transfer_buffer;
|
|
|
|
if (urb->transfer_buffer_length > 0) {
|
|
|
|
if (setup->bRequestType == Ep_InRequest) {
|
|
|
|
ep2 = find_endpoint(dum, w_index);
|
|
|
|
if (!ep2) {
|
|
|
|
ret_val = -EOPNOTSUPP;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
buf[0] = ep2->halted;
|
|
|
|
} else if (setup->bRequestType ==
|
|
|
|
Dev_InRequest) {
|
|
|
|
buf[0] = (u8)dum->devstatus;
|
|
|
|
} else
|
|
|
|
buf[0] = 0;
|
|
|
|
}
|
|
|
|
if (urb->transfer_buffer_length > 1)
|
|
|
|
buf[1] = 0;
|
|
|
|
urb->actual_length = min_t(u32, 2,
|
|
|
|
urb->transfer_buffer_length);
|
|
|
|
ret_val = 0;
|
|
|
|
*status = 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return ret_val;
|
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/* drive both sides of the transfers; looks like irq handlers to
|
|
|
|
* both drivers except the callbacks aren't in_irq().
|
|
|
|
*/
|
treewide: setup_timer() -> timer_setup()
This converts all remaining cases of the old setup_timer() API into using
timer_setup(), where the callback argument is the structure already
holding the struct timer_list. These should have no behavioral changes,
since they just change which pointer is passed into the callback with
the same available pointers after conversion. It handles the following
examples, in addition to some other variations.
Casting from unsigned long:
void my_callback(unsigned long data)
{
struct something *ptr = (struct something *)data;
...
}
...
setup_timer(&ptr->my_timer, my_callback, ptr);
and forced object casts:
void my_callback(struct something *ptr)
{
...
}
...
setup_timer(&ptr->my_timer, my_callback, (unsigned long)ptr);
become:
void my_callback(struct timer_list *t)
{
struct something *ptr = from_timer(ptr, t, my_timer);
...
}
...
timer_setup(&ptr->my_timer, my_callback, 0);
Direct function assignments:
void my_callback(unsigned long data)
{
struct something *ptr = (struct something *)data;
...
}
...
ptr->my_timer.function = my_callback;
have a temporary cast added, along with converting the args:
void my_callback(struct timer_list *t)
{
struct something *ptr = from_timer(ptr, t, my_timer);
...
}
...
ptr->my_timer.function = (TIMER_FUNC_TYPE)my_callback;
And finally, callbacks without a data assignment:
void my_callback(unsigned long data)
{
...
}
...
setup_timer(&ptr->my_timer, my_callback, 0);
have their argument renamed to verify they're unused during conversion:
void my_callback(struct timer_list *unused)
{
...
}
...
timer_setup(&ptr->my_timer, my_callback, 0);
The conversion is done with the following Coccinelle script:
spatch --very-quiet --all-includes --include-headers \
-I ./arch/x86/include -I ./arch/x86/include/generated \
-I ./include -I ./arch/x86/include/uapi \
-I ./arch/x86/include/generated/uapi -I ./include/uapi \
-I ./include/generated/uapi --include ./include/linux/kconfig.h \
--dir . \
--cocci-file ~/src/data/timer_setup.cocci
@fix_address_of@
expression e;
@@
setup_timer(
-&(e)
+&e
, ...)
// Update any raw setup_timer() usages that have a NULL callback, but
// would otherwise match change_timer_function_usage, since the latter
// will update all function assignments done in the face of a NULL
// function initialization in setup_timer().
@change_timer_function_usage_NULL@
expression _E;
identifier _timer;
type _cast_data;
@@
(
-setup_timer(&_E->_timer, NULL, _E);
+timer_setup(&_E->_timer, NULL, 0);
|
-setup_timer(&_E->_timer, NULL, (_cast_data)_E);
+timer_setup(&_E->_timer, NULL, 0);
|
-setup_timer(&_E._timer, NULL, &_E);
+timer_setup(&_E._timer, NULL, 0);
|
-setup_timer(&_E._timer, NULL, (_cast_data)&_E);
+timer_setup(&_E._timer, NULL, 0);
)
@change_timer_function_usage@
expression _E;
identifier _timer;
struct timer_list _stl;
identifier _callback;
type _cast_func, _cast_data;
@@
(
-setup_timer(&_E->_timer, _callback, _E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, &_callback, _E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, _callback, (_cast_data)_E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, &_callback, (_cast_data)_E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, (_cast_func)_callback, _E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, (_cast_func)&_callback, _E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, (_cast_func)_callback, (_cast_data)_E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, (_cast_func)&_callback, (_cast_data)_E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, (_cast_data)_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, (_cast_data)&_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, &_callback, (_cast_data)_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, &_callback, (_cast_data)&_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, (_cast_func)_callback, (_cast_data)_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, (_cast_func)_callback, (_cast_data)&_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, (_cast_func)&_callback, (_cast_data)_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, (_cast_func)&_callback, (_cast_data)&_E);
+timer_setup(&_E._timer, _callback, 0);
|
_E->_timer@_stl.function = _callback;
|
_E->_timer@_stl.function = &_callback;
|
_E->_timer@_stl.function = (_cast_func)_callback;
|
_E->_timer@_stl.function = (_cast_func)&_callback;
|
_E._timer@_stl.function = _callback;
|
_E._timer@_stl.function = &_callback;
|
_E._timer@_stl.function = (_cast_func)_callback;
|
_E._timer@_stl.function = (_cast_func)&_callback;
)
// callback(unsigned long arg)
@change_callback_handle_cast
depends on change_timer_function_usage@
identifier change_timer_function_usage._callback;
identifier change_timer_function_usage._timer;
type _origtype;
identifier _origarg;
type _handletype;
identifier _handle;
@@
void _callback(
-_origtype _origarg
+struct timer_list *t
)
{
(
... when != _origarg
_handletype *_handle =
-(_handletype *)_origarg;
+from_timer(_handle, t, _timer);
... when != _origarg
|
... when != _origarg
_handletype *_handle =
-(void *)_origarg;
+from_timer(_handle, t, _timer);
... when != _origarg
|
... when != _origarg
_handletype *_handle;
... when != _handle
_handle =
-(_handletype *)_origarg;
+from_timer(_handle, t, _timer);
... when != _origarg
|
... when != _origarg
_handletype *_handle;
... when != _handle
_handle =
-(void *)_origarg;
+from_timer(_handle, t, _timer);
... when != _origarg
)
}
// callback(unsigned long arg) without existing variable
@change_callback_handle_cast_no_arg
depends on change_timer_function_usage &&
!change_callback_handle_cast@
identifier change_timer_function_usage._callback;
identifier change_timer_function_usage._timer;
type _origtype;
identifier _origarg;
type _handletype;
@@
void _callback(
-_origtype _origarg
+struct timer_list *t
)
{
+ _handletype *_origarg = from_timer(_origarg, t, _timer);
+
... when != _origarg
- (_handletype *)_origarg
+ _origarg
... when != _origarg
}
// Avoid already converted callbacks.
@match_callback_converted
depends on change_timer_function_usage &&
!change_callback_handle_cast &&
!change_callback_handle_cast_no_arg@
identifier change_timer_function_usage._callback;
identifier t;
@@
void _callback(struct timer_list *t)
{ ... }
// callback(struct something *handle)
@change_callback_handle_arg
depends on change_timer_function_usage &&
!match_callback_converted &&
!change_callback_handle_cast &&
!change_callback_handle_cast_no_arg@
identifier change_timer_function_usage._callback;
identifier change_timer_function_usage._timer;
type _handletype;
identifier _handle;
@@
void _callback(
-_handletype *_handle
+struct timer_list *t
)
{
+ _handletype *_handle = from_timer(_handle, t, _timer);
...
}
// If change_callback_handle_arg ran on an empty function, remove
// the added handler.
@unchange_callback_handle_arg
depends on change_timer_function_usage &&
change_callback_handle_arg@
identifier change_timer_function_usage._callback;
identifier change_timer_function_usage._timer;
type _handletype;
identifier _handle;
identifier t;
@@
void _callback(struct timer_list *t)
{
- _handletype *_handle = from_timer(_handle, t, _timer);
}
// We only want to refactor the setup_timer() data argument if we've found
// the matching callback. This undoes changes in change_timer_function_usage.
@unchange_timer_function_usage
depends on change_timer_function_usage &&
!change_callback_handle_cast &&
!change_callback_handle_cast_no_arg &&
!change_callback_handle_arg@
expression change_timer_function_usage._E;
identifier change_timer_function_usage._timer;
identifier change_timer_function_usage._callback;
type change_timer_function_usage._cast_data;
@@
(
-timer_setup(&_E->_timer, _callback, 0);
+setup_timer(&_E->_timer, _callback, (_cast_data)_E);
|
-timer_setup(&_E._timer, _callback, 0);
+setup_timer(&_E._timer, _callback, (_cast_data)&_E);
)
// If we fixed a callback from a .function assignment, fix the
// assignment cast now.
@change_timer_function_assignment
depends on change_timer_function_usage &&
(change_callback_handle_cast ||
change_callback_handle_cast_no_arg ||
change_callback_handle_arg)@
expression change_timer_function_usage._E;
identifier change_timer_function_usage._timer;
identifier change_timer_function_usage._callback;
type _cast_func;
typedef TIMER_FUNC_TYPE;
@@
(
_E->_timer.function =
-_callback
+(TIMER_FUNC_TYPE)_callback
;
|
_E->_timer.function =
-&_callback
+(TIMER_FUNC_TYPE)_callback
;
|
_E->_timer.function =
-(_cast_func)_callback;
+(TIMER_FUNC_TYPE)_callback
;
|
_E->_timer.function =
-(_cast_func)&_callback
+(TIMER_FUNC_TYPE)_callback
;
|
_E._timer.function =
-_callback
+(TIMER_FUNC_TYPE)_callback
;
|
_E._timer.function =
-&_callback;
+(TIMER_FUNC_TYPE)_callback
;
|
_E._timer.function =
-(_cast_func)_callback
+(TIMER_FUNC_TYPE)_callback
;
|
_E._timer.function =
-(_cast_func)&_callback
+(TIMER_FUNC_TYPE)_callback
;
)
// Sometimes timer functions are called directly. Replace matched args.
@change_timer_function_calls
depends on change_timer_function_usage &&
(change_callback_handle_cast ||
change_callback_handle_cast_no_arg ||
change_callback_handle_arg)@
expression _E;
identifier change_timer_function_usage._timer;
identifier change_timer_function_usage._callback;
type _cast_data;
@@
_callback(
(
-(_cast_data)_E
+&_E->_timer
|
-(_cast_data)&_E
+&_E._timer
|
-_E
+&_E->_timer
)
)
// If a timer has been configured without a data argument, it can be
// converted without regard to the callback argument, since it is unused.
@match_timer_function_unused_data@
expression _E;
identifier _timer;
identifier _callback;
@@
(
-setup_timer(&_E->_timer, _callback, 0);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, _callback, 0L);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, _callback, 0UL);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, 0);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, 0L);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, 0UL);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_timer, _callback, 0);
+timer_setup(&_timer, _callback, 0);
|
-setup_timer(&_timer, _callback, 0L);
+timer_setup(&_timer, _callback, 0);
|
-setup_timer(&_timer, _callback, 0UL);
+timer_setup(&_timer, _callback, 0);
|
-setup_timer(_timer, _callback, 0);
+timer_setup(_timer, _callback, 0);
|
-setup_timer(_timer, _callback, 0L);
+timer_setup(_timer, _callback, 0);
|
-setup_timer(_timer, _callback, 0UL);
+timer_setup(_timer, _callback, 0);
)
@change_callback_unused_data
depends on match_timer_function_unused_data@
identifier match_timer_function_unused_data._callback;
type _origtype;
identifier _origarg;
@@
void _callback(
-_origtype _origarg
+struct timer_list *unused
)
{
... when != _origarg
}
Signed-off-by: Kees Cook <keescook@chromium.org>
2017-10-17 05:43:17 +08:00
|
|
|
static void dummy_timer(struct timer_list *t)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
treewide: setup_timer() -> timer_setup()
This converts all remaining cases of the old setup_timer() API into using
timer_setup(), where the callback argument is the structure already
holding the struct timer_list. These should have no behavioral changes,
since they just change which pointer is passed into the callback with
the same available pointers after conversion. It handles the following
examples, in addition to some other variations.
Casting from unsigned long:
void my_callback(unsigned long data)
{
struct something *ptr = (struct something *)data;
...
}
...
setup_timer(&ptr->my_timer, my_callback, ptr);
and forced object casts:
void my_callback(struct something *ptr)
{
...
}
...
setup_timer(&ptr->my_timer, my_callback, (unsigned long)ptr);
become:
void my_callback(struct timer_list *t)
{
struct something *ptr = from_timer(ptr, t, my_timer);
...
}
...
timer_setup(&ptr->my_timer, my_callback, 0);
Direct function assignments:
void my_callback(unsigned long data)
{
struct something *ptr = (struct something *)data;
...
}
...
ptr->my_timer.function = my_callback;
have a temporary cast added, along with converting the args:
void my_callback(struct timer_list *t)
{
struct something *ptr = from_timer(ptr, t, my_timer);
...
}
...
ptr->my_timer.function = (TIMER_FUNC_TYPE)my_callback;
And finally, callbacks without a data assignment:
void my_callback(unsigned long data)
{
...
}
...
setup_timer(&ptr->my_timer, my_callback, 0);
have their argument renamed to verify they're unused during conversion:
void my_callback(struct timer_list *unused)
{
...
}
...
timer_setup(&ptr->my_timer, my_callback, 0);
The conversion is done with the following Coccinelle script:
spatch --very-quiet --all-includes --include-headers \
-I ./arch/x86/include -I ./arch/x86/include/generated \
-I ./include -I ./arch/x86/include/uapi \
-I ./arch/x86/include/generated/uapi -I ./include/uapi \
-I ./include/generated/uapi --include ./include/linux/kconfig.h \
--dir . \
--cocci-file ~/src/data/timer_setup.cocci
@fix_address_of@
expression e;
@@
setup_timer(
-&(e)
+&e
, ...)
// Update any raw setup_timer() usages that have a NULL callback, but
// would otherwise match change_timer_function_usage, since the latter
// will update all function assignments done in the face of a NULL
// function initialization in setup_timer().
@change_timer_function_usage_NULL@
expression _E;
identifier _timer;
type _cast_data;
@@
(
-setup_timer(&_E->_timer, NULL, _E);
+timer_setup(&_E->_timer, NULL, 0);
|
-setup_timer(&_E->_timer, NULL, (_cast_data)_E);
+timer_setup(&_E->_timer, NULL, 0);
|
-setup_timer(&_E._timer, NULL, &_E);
+timer_setup(&_E._timer, NULL, 0);
|
-setup_timer(&_E._timer, NULL, (_cast_data)&_E);
+timer_setup(&_E._timer, NULL, 0);
)
@change_timer_function_usage@
expression _E;
identifier _timer;
struct timer_list _stl;
identifier _callback;
type _cast_func, _cast_data;
@@
(
-setup_timer(&_E->_timer, _callback, _E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, &_callback, _E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, _callback, (_cast_data)_E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, &_callback, (_cast_data)_E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, (_cast_func)_callback, _E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, (_cast_func)&_callback, _E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, (_cast_func)_callback, (_cast_data)_E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, (_cast_func)&_callback, (_cast_data)_E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, (_cast_data)_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, (_cast_data)&_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, &_callback, (_cast_data)_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, &_callback, (_cast_data)&_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, (_cast_func)_callback, (_cast_data)_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, (_cast_func)_callback, (_cast_data)&_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, (_cast_func)&_callback, (_cast_data)_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, (_cast_func)&_callback, (_cast_data)&_E);
+timer_setup(&_E._timer, _callback, 0);
|
_E->_timer@_stl.function = _callback;
|
_E->_timer@_stl.function = &_callback;
|
_E->_timer@_stl.function = (_cast_func)_callback;
|
_E->_timer@_stl.function = (_cast_func)&_callback;
|
_E._timer@_stl.function = _callback;
|
_E._timer@_stl.function = &_callback;
|
_E._timer@_stl.function = (_cast_func)_callback;
|
_E._timer@_stl.function = (_cast_func)&_callback;
)
// callback(unsigned long arg)
@change_callback_handle_cast
depends on change_timer_function_usage@
identifier change_timer_function_usage._callback;
identifier change_timer_function_usage._timer;
type _origtype;
identifier _origarg;
type _handletype;
identifier _handle;
@@
void _callback(
-_origtype _origarg
+struct timer_list *t
)
{
(
... when != _origarg
_handletype *_handle =
-(_handletype *)_origarg;
+from_timer(_handle, t, _timer);
... when != _origarg
|
... when != _origarg
_handletype *_handle =
-(void *)_origarg;
+from_timer(_handle, t, _timer);
... when != _origarg
|
... when != _origarg
_handletype *_handle;
... when != _handle
_handle =
-(_handletype *)_origarg;
+from_timer(_handle, t, _timer);
... when != _origarg
|
... when != _origarg
_handletype *_handle;
... when != _handle
_handle =
-(void *)_origarg;
+from_timer(_handle, t, _timer);
... when != _origarg
)
}
// callback(unsigned long arg) without existing variable
@change_callback_handle_cast_no_arg
depends on change_timer_function_usage &&
!change_callback_handle_cast@
identifier change_timer_function_usage._callback;
identifier change_timer_function_usage._timer;
type _origtype;
identifier _origarg;
type _handletype;
@@
void _callback(
-_origtype _origarg
+struct timer_list *t
)
{
+ _handletype *_origarg = from_timer(_origarg, t, _timer);
+
... when != _origarg
- (_handletype *)_origarg
+ _origarg
... when != _origarg
}
// Avoid already converted callbacks.
@match_callback_converted
depends on change_timer_function_usage &&
!change_callback_handle_cast &&
!change_callback_handle_cast_no_arg@
identifier change_timer_function_usage._callback;
identifier t;
@@
void _callback(struct timer_list *t)
{ ... }
// callback(struct something *handle)
@change_callback_handle_arg
depends on change_timer_function_usage &&
!match_callback_converted &&
!change_callback_handle_cast &&
!change_callback_handle_cast_no_arg@
identifier change_timer_function_usage._callback;
identifier change_timer_function_usage._timer;
type _handletype;
identifier _handle;
@@
void _callback(
-_handletype *_handle
+struct timer_list *t
)
{
+ _handletype *_handle = from_timer(_handle, t, _timer);
...
}
// If change_callback_handle_arg ran on an empty function, remove
// the added handler.
@unchange_callback_handle_arg
depends on change_timer_function_usage &&
change_callback_handle_arg@
identifier change_timer_function_usage._callback;
identifier change_timer_function_usage._timer;
type _handletype;
identifier _handle;
identifier t;
@@
void _callback(struct timer_list *t)
{
- _handletype *_handle = from_timer(_handle, t, _timer);
}
// We only want to refactor the setup_timer() data argument if we've found
// the matching callback. This undoes changes in change_timer_function_usage.
@unchange_timer_function_usage
depends on change_timer_function_usage &&
!change_callback_handle_cast &&
!change_callback_handle_cast_no_arg &&
!change_callback_handle_arg@
expression change_timer_function_usage._E;
identifier change_timer_function_usage._timer;
identifier change_timer_function_usage._callback;
type change_timer_function_usage._cast_data;
@@
(
-timer_setup(&_E->_timer, _callback, 0);
+setup_timer(&_E->_timer, _callback, (_cast_data)_E);
|
-timer_setup(&_E._timer, _callback, 0);
+setup_timer(&_E._timer, _callback, (_cast_data)&_E);
)
// If we fixed a callback from a .function assignment, fix the
// assignment cast now.
@change_timer_function_assignment
depends on change_timer_function_usage &&
(change_callback_handle_cast ||
change_callback_handle_cast_no_arg ||
change_callback_handle_arg)@
expression change_timer_function_usage._E;
identifier change_timer_function_usage._timer;
identifier change_timer_function_usage._callback;
type _cast_func;
typedef TIMER_FUNC_TYPE;
@@
(
_E->_timer.function =
-_callback
+(TIMER_FUNC_TYPE)_callback
;
|
_E->_timer.function =
-&_callback
+(TIMER_FUNC_TYPE)_callback
;
|
_E->_timer.function =
-(_cast_func)_callback;
+(TIMER_FUNC_TYPE)_callback
;
|
_E->_timer.function =
-(_cast_func)&_callback
+(TIMER_FUNC_TYPE)_callback
;
|
_E._timer.function =
-_callback
+(TIMER_FUNC_TYPE)_callback
;
|
_E._timer.function =
-&_callback;
+(TIMER_FUNC_TYPE)_callback
;
|
_E._timer.function =
-(_cast_func)_callback
+(TIMER_FUNC_TYPE)_callback
;
|
_E._timer.function =
-(_cast_func)&_callback
+(TIMER_FUNC_TYPE)_callback
;
)
// Sometimes timer functions are called directly. Replace matched args.
@change_timer_function_calls
depends on change_timer_function_usage &&
(change_callback_handle_cast ||
change_callback_handle_cast_no_arg ||
change_callback_handle_arg)@
expression _E;
identifier change_timer_function_usage._timer;
identifier change_timer_function_usage._callback;
type _cast_data;
@@
_callback(
(
-(_cast_data)_E
+&_E->_timer
|
-(_cast_data)&_E
+&_E._timer
|
-_E
+&_E->_timer
)
)
// If a timer has been configured without a data argument, it can be
// converted without regard to the callback argument, since it is unused.
@match_timer_function_unused_data@
expression _E;
identifier _timer;
identifier _callback;
@@
(
-setup_timer(&_E->_timer, _callback, 0);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, _callback, 0L);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, _callback, 0UL);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, 0);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, 0L);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, 0UL);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_timer, _callback, 0);
+timer_setup(&_timer, _callback, 0);
|
-setup_timer(&_timer, _callback, 0L);
+timer_setup(&_timer, _callback, 0);
|
-setup_timer(&_timer, _callback, 0UL);
+timer_setup(&_timer, _callback, 0);
|
-setup_timer(_timer, _callback, 0);
+timer_setup(_timer, _callback, 0);
|
-setup_timer(_timer, _callback, 0L);
+timer_setup(_timer, _callback, 0);
|
-setup_timer(_timer, _callback, 0UL);
+timer_setup(_timer, _callback, 0);
)
@change_callback_unused_data
depends on match_timer_function_unused_data@
identifier match_timer_function_unused_data._callback;
type _origtype;
identifier _origarg;
@@
void _callback(
-_origtype _origarg
+struct timer_list *unused
)
{
... when != _origarg
}
Signed-off-by: Kees Cook <keescook@chromium.org>
2017-10-17 05:43:17 +08:00
|
|
|
struct dummy_hcd *dum_hcd = from_timer(dum_hcd, t, timer);
|
2011-06-29 21:41:51 +08:00
|
|
|
struct dummy *dum = dum_hcd->dum;
|
2005-04-17 06:20:36 +08:00
|
|
|
struct urbp *urbp, *tmp;
|
|
|
|
unsigned long flags;
|
|
|
|
int limit, total;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* simplistic model for one frame's bandwidth */
|
2017-09-27 03:16:05 +08:00
|
|
|
/* FIXME: account for transaction and packet overhead */
|
2005-04-17 06:20:36 +08:00
|
|
|
switch (dum->gadget.speed) {
|
|
|
|
case USB_SPEED_LOW:
|
|
|
|
total = 8/*bytes*/ * 12/*packets*/;
|
|
|
|
break;
|
|
|
|
case USB_SPEED_FULL:
|
|
|
|
total = 64/*bytes*/ * 19/*packets*/;
|
|
|
|
break;
|
|
|
|
case USB_SPEED_HIGH:
|
|
|
|
total = 512/*bytes*/ * 13/*packets*/ * 8/*uframes*/;
|
|
|
|
break;
|
2011-06-29 21:41:52 +08:00
|
|
|
case USB_SPEED_SUPER:
|
|
|
|
/* Bus speed is 500000 bytes/ms, so use a little less */
|
|
|
|
total = 490000;
|
|
|
|
break;
|
2019-04-19 01:12:07 +08:00
|
|
|
default: /* Can't happen */
|
2011-06-29 21:41:51 +08:00
|
|
|
dev_err(dummy_dev(dum_hcd), "bogus device speed\n");
|
2019-04-19 01:12:07 +08:00
|
|
|
total = 0;
|
|
|
|
break;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* FIXME if HZ != 1000 this will probably misbehave ... */
|
|
|
|
|
|
|
|
/* look at each urb queued by the host side driver */
|
2012-01-12 19:53:15 +08:00
|
|
|
spin_lock_irqsave(&dum->lock, flags);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2011-06-29 21:41:51 +08:00
|
|
|
if (!dum_hcd->udev) {
|
|
|
|
dev_err(dummy_dev(dum_hcd),
|
2005-04-17 06:20:36 +08:00
|
|
|
"timer fired with no URBs pending?\n");
|
2012-01-12 19:53:15 +08:00
|
|
|
spin_unlock_irqrestore(&dum->lock, flags);
|
2005-04-17 06:20:36 +08:00
|
|
|
return;
|
|
|
|
}
|
2017-09-27 03:15:40 +08:00
|
|
|
dum_hcd->next_frame_urbp = NULL;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
for (i = 0; i < DUMMY_ENDPOINTS; i++) {
|
2015-07-31 22:00:24 +08:00
|
|
|
if (!ep_info[i].name)
|
2005-04-17 06:20:36 +08:00
|
|
|
break;
|
2012-01-12 19:53:15 +08:00
|
|
|
dum->ep[i].already_seen = 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
restart:
|
2011-06-29 21:41:51 +08:00
|
|
|
list_for_each_entry_safe(urbp, tmp, &dum_hcd->urbp_list, urbp_list) {
|
2005-04-17 06:20:36 +08:00
|
|
|
struct urb *urb;
|
|
|
|
struct dummy_request *req;
|
|
|
|
u8 address;
|
|
|
|
struct dummy_ep *ep = NULL;
|
2007-08-25 03:40:10 +08:00
|
|
|
int status = -EINPROGRESS;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2017-09-27 03:15:40 +08:00
|
|
|
/* stop when we reach URBs queued after the timer interrupt */
|
|
|
|
if (urbp == dum_hcd->next_frame_urbp)
|
|
|
|
break;
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
urb = urbp->urb;
|
2007-08-22 03:40:36 +08:00
|
|
|
if (urb->unlinked)
|
2005-04-17 06:20:36 +08:00
|
|
|
goto return_urb;
|
2011-06-29 21:41:51 +08:00
|
|
|
else if (dum_hcd->rh_state != DUMMY_RH_RUNNING)
|
2005-05-11 03:34:16 +08:00
|
|
|
continue;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2017-09-27 03:16:05 +08:00
|
|
|
/* Used up this frame's bandwidth? */
|
|
|
|
if (total <= 0)
|
2019-04-19 01:12:07 +08:00
|
|
|
continue;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/* find the gadget's ep for this request (if configured) */
|
|
|
|
address = usb_pipeendpoint (urb->pipe);
|
2024-06-11 20:08:33 +08:00
|
|
|
if (usb_urb_dir_in(urb))
|
2005-04-17 06:20:36 +08:00
|
|
|
address |= USB_DIR_IN;
|
|
|
|
ep = find_endpoint(dum, address);
|
|
|
|
if (!ep) {
|
|
|
|
/* set_configuration() disagreement */
|
2011-06-29 21:41:51 +08:00
|
|
|
dev_dbg(dummy_dev(dum_hcd),
|
2005-04-17 06:20:36 +08:00
|
|
|
"no ep configured for urb %p\n",
|
|
|
|
urb);
|
2007-08-25 03:40:10 +08:00
|
|
|
status = -EPROTO;
|
2005-04-17 06:20:36 +08:00
|
|
|
goto return_urb;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ep->already_seen)
|
|
|
|
continue;
|
|
|
|
ep->already_seen = 1;
|
2012-01-12 19:53:15 +08:00
|
|
|
if (ep == &dum->ep[0] && urb->error_count) {
|
2005-04-17 06:20:36 +08:00
|
|
|
ep->setup_stage = 1; /* a new urb */
|
|
|
|
urb->error_count = 0;
|
|
|
|
}
|
|
|
|
if (ep->halted && !ep->setup_stage) {
|
|
|
|
/* NOTE: must not be iso! */
|
2011-06-29 21:41:51 +08:00
|
|
|
dev_dbg(dummy_dev(dum_hcd), "ep %s halted, urb %p\n",
|
2005-04-17 06:20:36 +08:00
|
|
|
ep->ep.name, urb);
|
2007-08-25 03:40:10 +08:00
|
|
|
status = -EPIPE;
|
2005-04-17 06:20:36 +08:00
|
|
|
goto return_urb;
|
|
|
|
}
|
|
|
|
/* FIXME make sure both ends agree on maxpacket */
|
|
|
|
|
|
|
|
/* handle control requests */
|
2012-01-12 19:53:15 +08:00
|
|
|
if (ep == &dum->ep[0] && ep->setup_stage) {
|
2005-04-17 06:20:36 +08:00
|
|
|
struct usb_ctrlrequest setup;
|
|
|
|
int value = 1;
|
|
|
|
|
2012-01-12 19:53:15 +08:00
|
|
|
setup = *(struct usb_ctrlrequest *) urb->setup_packet;
|
2005-04-17 06:20:36 +08:00
|
|
|
/* paranoia, in case of stale queued data */
|
2012-01-12 19:53:15 +08:00
|
|
|
list_for_each_entry(req, &ep->queue, queue) {
|
|
|
|
list_del_init(&req->queue);
|
2005-04-17 06:20:36 +08:00
|
|
|
req->req.status = -EOVERFLOW;
|
2012-01-12 19:53:15 +08:00
|
|
|
dev_dbg(udc_dev(dum), "stale req = %p\n",
|
2005-04-17 06:20:36 +08:00
|
|
|
req);
|
|
|
|
|
2012-01-12 19:53:15 +08:00
|
|
|
spin_unlock(&dum->lock);
|
2014-09-25 04:43:19 +08:00
|
|
|
usb_gadget_giveback_request(&ep->ep, &req->req);
|
2012-01-12 19:53:15 +08:00
|
|
|
spin_lock(&dum->lock);
|
2005-04-17 06:20:36 +08:00
|
|
|
ep->already_seen = 0;
|
|
|
|
goto restart;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* gadget driver never sees set_address or operations
|
|
|
|
* on standard feature flags. some hardware doesn't
|
|
|
|
* even expose them.
|
|
|
|
*/
|
|
|
|
ep->last_io = jiffies;
|
|
|
|
ep->setup_stage = 0;
|
|
|
|
ep->halted = 0;
|
|
|
|
|
2011-06-29 21:41:51 +08:00
|
|
|
value = handle_control_request(dum_hcd, urb, &setup,
|
2010-11-01 23:38:05 +08:00
|
|
|
&status);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/* gadget driver handles all other requests. block
|
|
|
|
* until setup() returns; no reentrancy issues etc.
|
|
|
|
*/
|
|
|
|
if (value > 0) {
|
USB: dummy-hcd: Fix erroneous synchronization change
A recent change to the synchronization in dummy-hcd was incorrect.
The issue was that dummy_udc_stop() contained no locking and therefore
could race with various gadget driver callbacks, and the fix was to
add locking and issue the callbacks with the private spinlock held.
UDC drivers aren't supposed to do this. Gadget driver callback
routines are allowed to invoke functions in the UDC driver, and these
functions will generally try to acquire the private spinlock. This
would deadlock the driver.
The correct solution is to drop the spinlock before issuing callbacks,
and avoid races by emulating the synchronize_irq() call that all real
UDC drivers must perform in their ->udc_stop() routines after
disabling interrupts. This involves adding a flag to dummy-hcd's
private structure to keep track of whether interrupts are supposed to
be enabled, and adding a counter to keep track of ongoing callbacks so
that dummy_udc_stop() can wait for them all to finish.
A real UDC driver won't receive disconnect, reset, suspend, resume, or
setup events once it has disabled interrupts. dummy-hcd will receive
them but won't try to issue any gadget driver callbacks, which should
be just as good.
Signed-off-by: Alan Stern <stern@rowland.harvard.edu>
Fixes: f16443a034c7 ("USB: gadgetfs, dummy-hcd, net2280: fix locking for callbacks")
CC: <stable@vger.kernel.org>
Signed-off-by: Felipe Balbi <felipe.balbi@linux.intel.com>
2017-09-27 03:15:49 +08:00
|
|
|
++dum->callback_usage;
|
2012-01-12 19:53:15 +08:00
|
|
|
spin_unlock(&dum->lock);
|
|
|
|
value = dum->driver->setup(&dum->gadget,
|
2005-04-17 06:20:36 +08:00
|
|
|
&setup);
|
2012-01-12 19:53:15 +08:00
|
|
|
spin_lock(&dum->lock);
|
USB: dummy-hcd: Fix erroneous synchronization change
A recent change to the synchronization in dummy-hcd was incorrect.
The issue was that dummy_udc_stop() contained no locking and therefore
could race with various gadget driver callbacks, and the fix was to
add locking and issue the callbacks with the private spinlock held.
UDC drivers aren't supposed to do this. Gadget driver callback
routines are allowed to invoke functions in the UDC driver, and these
functions will generally try to acquire the private spinlock. This
would deadlock the driver.
The correct solution is to drop the spinlock before issuing callbacks,
and avoid races by emulating the synchronize_irq() call that all real
UDC drivers must perform in their ->udc_stop() routines after
disabling interrupts. This involves adding a flag to dummy-hcd's
private structure to keep track of whether interrupts are supposed to
be enabled, and adding a counter to keep track of ongoing callbacks so
that dummy_udc_stop() can wait for them all to finish.
A real UDC driver won't receive disconnect, reset, suspend, resume, or
setup events once it has disabled interrupts. dummy-hcd will receive
them but won't try to issue any gadget driver callbacks, which should
be just as good.
Signed-off-by: Alan Stern <stern@rowland.harvard.edu>
Fixes: f16443a034c7 ("USB: gadgetfs, dummy-hcd, net2280: fix locking for callbacks")
CC: <stable@vger.kernel.org>
Signed-off-by: Felipe Balbi <felipe.balbi@linux.intel.com>
2017-09-27 03:15:49 +08:00
|
|
|
--dum->callback_usage;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
if (value >= 0) {
|
|
|
|
/* no delays (max 64KB data stage) */
|
|
|
|
limit = 64*1024;
|
|
|
|
goto treat_control_like_bulk;
|
|
|
|
}
|
|
|
|
/* error, see below */
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value < 0) {
|
|
|
|
if (value != -EOPNOTSUPP)
|
2012-01-12 19:53:15 +08:00
|
|
|
dev_dbg(udc_dev(dum),
|
2005-04-17 06:20:36 +08:00
|
|
|
"setup --> %d\n",
|
|
|
|
value);
|
2007-08-25 03:40:10 +08:00
|
|
|
status = -EPIPE;
|
2005-04-17 06:20:36 +08:00
|
|
|
urb->actual_length = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
goto return_urb;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* non-control requests */
|
|
|
|
limit = total;
|
2012-01-12 19:53:15 +08:00
|
|
|
switch (usb_pipetype(urb->pipe)) {
|
2005-04-17 06:20:36 +08:00
|
|
|
case PIPE_ISOCHRONOUS:
|
2017-09-27 03:15:58 +08:00
|
|
|
/*
|
|
|
|
* We don't support isochronous. But if we did,
|
|
|
|
* here are some of the issues we'd have to face:
|
|
|
|
*
|
|
|
|
* Is it urb->interval since the last xfer?
|
|
|
|
* Use urb->iso_frame_desc[i].
|
|
|
|
* Complete whether or not ep has requests queued.
|
|
|
|
* Report random errors, to debug drivers.
|
2005-04-17 06:20:36 +08:00
|
|
|
*/
|
2012-01-12 19:53:15 +08:00
|
|
|
limit = max(limit, periodic_bytes(dum, ep));
|
2017-09-27 03:15:58 +08:00
|
|
|
status = -EINVAL; /* fail all xfers */
|
2005-04-17 06:20:36 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PIPE_INTERRUPT:
|
|
|
|
/* FIXME is it urb->interval since the last xfer?
|
|
|
|
* this almost certainly polls too fast.
|
|
|
|
*/
|
2012-01-12 19:53:15 +08:00
|
|
|
limit = max(limit, periodic_bytes(dum, ep));
|
2005-04-17 06:20:36 +08:00
|
|
|
/* FALLTHROUGH */
|
|
|
|
|
|
|
|
default:
|
2012-01-12 19:53:15 +08:00
|
|
|
treat_control_like_bulk:
|
2005-04-17 06:20:36 +08:00
|
|
|
ep->last_io = jiffies;
|
2015-09-15 22:55:32 +08:00
|
|
|
total -= transfer(dum_hcd, urb, ep, limit, &status);
|
2005-04-17 06:20:36 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* incomplete transfer? */
|
2007-08-25 03:40:10 +08:00
|
|
|
if (status == -EINPROGRESS)
|
2005-04-17 06:20:36 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
return_urb:
|
2012-01-12 19:53:15 +08:00
|
|
|
list_del(&urbp->urbp_list);
|
|
|
|
kfree(urbp);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (ep)
|
|
|
|
ep->already_seen = ep->setup_stage = 0;
|
|
|
|
|
2011-06-29 21:41:51 +08:00
|
|
|
usb_hcd_unlink_urb_from_ep(dummy_hcd_to_hcd(dum_hcd), urb);
|
2012-01-12 19:53:15 +08:00
|
|
|
spin_unlock(&dum->lock);
|
2011-06-29 21:41:51 +08:00
|
|
|
usb_hcd_giveback_urb(dummy_hcd_to_hcd(dum_hcd), urb, status);
|
2012-01-12 19:53:15 +08:00
|
|
|
spin_lock(&dum->lock);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
goto restart;
|
|
|
|
}
|
|
|
|
|
2011-06-29 21:41:51 +08:00
|
|
|
if (list_empty(&dum_hcd->urbp_list)) {
|
|
|
|
usb_put_dev(dum_hcd->udev);
|
|
|
|
dum_hcd->udev = NULL;
|
|
|
|
} else if (dum_hcd->rh_state == DUMMY_RH_RUNNING) {
|
2005-05-11 03:34:16 +08:00
|
|
|
/* want a 1 msec delay here */
|
2011-06-29 21:41:51 +08:00
|
|
|
mod_timer(&dum_hcd->timer, jiffies + msecs_to_jiffies(1));
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2012-01-12 19:53:15 +08:00
|
|
|
spin_unlock_irqrestore(&dum->lock, flags);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
#define PORT_C_MASK \
|
2005-04-30 04:30:48 +08:00
|
|
|
((USB_PORT_STAT_C_CONNECTION \
|
|
|
|
| USB_PORT_STAT_C_ENABLE \
|
|
|
|
| USB_PORT_STAT_C_SUSPEND \
|
|
|
|
| USB_PORT_STAT_C_OVERCURRENT \
|
|
|
|
| USB_PORT_STAT_C_RESET) << 16)
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2012-01-12 19:53:15 +08:00
|
|
|
static int dummy_hub_status(struct usb_hcd *hcd, char *buf)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2011-06-29 21:41:51 +08:00
|
|
|
struct dummy_hcd *dum_hcd;
|
2005-04-17 06:20:36 +08:00
|
|
|
unsigned long flags;
|
2005-05-11 03:34:16 +08:00
|
|
|
int retval = 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2011-06-29 21:41:51 +08:00
|
|
|
dum_hcd = hcd_to_dummy_hcd(hcd);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2011-06-29 21:41:51 +08:00
|
|
|
spin_lock_irqsave(&dum_hcd->dum->lock, flags);
|
2010-06-23 04:39:10 +08:00
|
|
|
if (!HCD_HW_ACCESSIBLE(hcd))
|
2005-05-11 03:34:16 +08:00
|
|
|
goto done;
|
2005-05-04 04:24:04 +08:00
|
|
|
|
2011-06-29 21:41:51 +08:00
|
|
|
if (dum_hcd->resuming && time_after_eq(jiffies, dum_hcd->re_timeout)) {
|
|
|
|
dum_hcd->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
|
|
|
|
dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
|
|
|
|
set_link_state(dum_hcd);
|
2005-05-04 04:24:04 +08:00
|
|
|
}
|
|
|
|
|
2011-06-29 21:41:51 +08:00
|
|
|
if ((dum_hcd->port_status & PORT_C_MASK) != 0) {
|
2005-04-17 06:20:36 +08:00
|
|
|
*buf = (1 << 1);
|
2011-06-29 21:41:51 +08:00
|
|
|
dev_dbg(dummy_dev(dum_hcd), "port status 0x%08x has changes\n",
|
|
|
|
dum_hcd->port_status);
|
2005-04-17 06:20:36 +08:00
|
|
|
retval = 1;
|
2011-06-29 21:41:51 +08:00
|
|
|
if (dum_hcd->rh_state == DUMMY_RH_SUSPENDED)
|
2012-01-12 19:53:15 +08:00
|
|
|
usb_hcd_resume_root_hub(hcd);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2005-05-11 03:34:16 +08:00
|
|
|
done:
|
2011-06-29 21:41:51 +08:00
|
|
|
spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
|
2005-04-17 06:20:36 +08:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2012-08-20 03:54:59 +08:00
|
|
|
/* usb 3.0 root hub device descriptor */
|
2013-03-22 22:50:47 +08:00
|
|
|
static struct {
|
2012-08-20 03:54:59 +08:00
|
|
|
struct usb_bos_descriptor bos;
|
|
|
|
struct usb_ss_cap_descriptor ss_cap;
|
|
|
|
} __packed usb3_bos_desc = {
|
|
|
|
|
|
|
|
.bos = {
|
|
|
|
.bLength = USB_DT_BOS_SIZE,
|
|
|
|
.bDescriptorType = USB_DT_BOS,
|
|
|
|
.wTotalLength = cpu_to_le16(sizeof(usb3_bos_desc)),
|
|
|
|
.bNumDeviceCaps = 1,
|
|
|
|
},
|
|
|
|
.ss_cap = {
|
|
|
|
.bLength = USB_DT_USB_SS_CAP_SIZE,
|
|
|
|
.bDescriptorType = USB_DT_DEVICE_CAPABILITY,
|
|
|
|
.bDevCapabilityType = USB_SS_CAP_TYPE,
|
|
|
|
.wSpeedSupported = cpu_to_le16(USB_5GBPS_OPERATION),
|
|
|
|
.bFunctionalitySupport = ilog2(USB_5GBPS_OPERATION),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2011-06-29 21:41:52 +08:00
|
|
|
static inline void
|
|
|
|
ss_hub_descriptor(struct usb_hub_descriptor *desc)
|
|
|
|
{
|
|
|
|
memset(desc, 0, sizeof *desc);
|
2015-03-29 06:39:09 +08:00
|
|
|
desc->bDescriptorType = USB_DT_SS_HUB;
|
2011-06-29 21:41:52 +08:00
|
|
|
desc->bDescLength = 12;
|
2015-01-19 06:55:55 +08:00
|
|
|
desc->wHubCharacteristics = cpu_to_le16(
|
|
|
|
HUB_CHAR_INDV_PORT_LPSM |
|
|
|
|
HUB_CHAR_COMMON_OCPM);
|
2011-06-29 21:41:52 +08:00
|
|
|
desc->bNbrPorts = 1;
|
|
|
|
desc->u.ss.bHubHdrDecLat = 0x04; /* Worst case: 0.4 micro sec*/
|
2017-05-11 00:18:25 +08:00
|
|
|
desc->u.ss.DeviceRemovable = 0;
|
2011-06-29 21:41:52 +08:00
|
|
|
}
|
|
|
|
|
2012-01-12 19:53:15 +08:00
|
|
|
static inline void hub_descriptor(struct usb_hub_descriptor *desc)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2012-01-12 19:53:15 +08:00
|
|
|
memset(desc, 0, sizeof *desc);
|
2015-03-29 06:39:09 +08:00
|
|
|
desc->bDescriptorType = USB_DT_HUB;
|
2005-04-17 06:20:36 +08:00
|
|
|
desc->bDescLength = 9;
|
2015-01-19 06:55:55 +08:00
|
|
|
desc->wHubCharacteristics = cpu_to_le16(
|
|
|
|
HUB_CHAR_INDV_PORT_LPSM |
|
|
|
|
HUB_CHAR_COMMON_OCPM);
|
2005-04-17 06:20:36 +08:00
|
|
|
desc->bNbrPorts = 1;
|
2017-05-11 00:18:25 +08:00
|
|
|
desc->u.hs.DeviceRemovable[0] = 0;
|
|
|
|
desc->u.hs.DeviceRemovable[1] = 0xff; /* PortPwrCtrlMask */
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2012-01-12 19:53:15 +08:00
|
|
|
static int dummy_hub_control(
|
2005-04-17 06:20:36 +08:00
|
|
|
struct usb_hcd *hcd,
|
|
|
|
u16 typeReq,
|
|
|
|
u16 wValue,
|
|
|
|
u16 wIndex,
|
|
|
|
char *buf,
|
|
|
|
u16 wLength
|
|
|
|
) {
|
2011-06-29 21:41:51 +08:00
|
|
|
struct dummy_hcd *dum_hcd;
|
2005-04-17 06:20:36 +08:00
|
|
|
int retval = 0;
|
|
|
|
unsigned long flags;
|
|
|
|
|
2010-06-23 04:39:10 +08:00
|
|
|
if (!HCD_HW_ACCESSIBLE(hcd))
|
2005-05-11 03:34:16 +08:00
|
|
|
return -ETIMEDOUT;
|
|
|
|
|
2011-06-29 21:41:51 +08:00
|
|
|
dum_hcd = hcd_to_dummy_hcd(hcd);
|
|
|
|
|
|
|
|
spin_lock_irqsave(&dum_hcd->dum->lock, flags);
|
2005-04-17 06:20:36 +08:00
|
|
|
switch (typeReq) {
|
|
|
|
case ClearHubFeature:
|
|
|
|
break;
|
|
|
|
case ClearPortFeature:
|
|
|
|
switch (wValue) {
|
|
|
|
case USB_PORT_FEAT_SUSPEND:
|
2011-06-29 21:41:52 +08:00
|
|
|
if (hcd->speed == HCD_USB3) {
|
|
|
|
dev_dbg(dummy_dev(dum_hcd),
|
|
|
|
"USB_PORT_FEAT_SUSPEND req not "
|
|
|
|
"supported for USB 3.0 roothub\n");
|
|
|
|
goto error;
|
|
|
|
}
|
2011-06-29 21:41:51 +08:00
|
|
|
if (dum_hcd->port_status & USB_PORT_STAT_SUSPEND) {
|
2005-04-17 06:20:36 +08:00
|
|
|
/* 20msec resume signaling */
|
2011-06-29 21:41:51 +08:00
|
|
|
dum_hcd->resuming = 1;
|
|
|
|
dum_hcd->re_timeout = jiffies +
|
2005-05-04 04:24:04 +08:00
|
|
|
msecs_to_jiffies(20);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case USB_PORT_FEAT_POWER:
|
2017-03-24 04:06:11 +08:00
|
|
|
dev_dbg(dummy_dev(dum_hcd), "power-off\n");
|
|
|
|
if (hcd->speed == HCD_USB3)
|
|
|
|
dum_hcd->port_status &= ~USB_SS_PORT_STAT_POWER;
|
|
|
|
else
|
|
|
|
dum_hcd->port_status &= ~USB_PORT_STAT_POWER;
|
|
|
|
set_link_state(dum_hcd);
|
|
|
|
break;
|
2005-04-17 06:20:36 +08:00
|
|
|
default:
|
2011-06-29 21:41:51 +08:00
|
|
|
dum_hcd->port_status &= ~(1 << wValue);
|
|
|
|
set_link_state(dum_hcd);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case GetHubDescriptor:
|
2011-06-29 21:41:52 +08:00
|
|
|
if (hcd->speed == HCD_USB3 &&
|
|
|
|
(wLength < USB_DT_SS_HUB_SIZE ||
|
|
|
|
wValue != (USB_DT_SS_HUB << 8))) {
|
|
|
|
dev_dbg(dummy_dev(dum_hcd),
|
|
|
|
"Wrong hub descriptor type for "
|
|
|
|
"USB 3.0 roothub.\n");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
if (hcd->speed == HCD_USB3)
|
|
|
|
ss_hub_descriptor((struct usb_hub_descriptor *) buf);
|
|
|
|
else
|
|
|
|
hub_descriptor((struct usb_hub_descriptor *) buf);
|
2005-04-17 06:20:36 +08:00
|
|
|
break;
|
2012-08-20 03:54:59 +08:00
|
|
|
|
|
|
|
case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
|
|
|
|
if (hcd->speed != HCD_USB3)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
if ((wValue >> 8) != USB_DT_BOS)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
memcpy(buf, &usb3_bos_desc, sizeof(usb3_bos_desc));
|
|
|
|
retval = sizeof(usb3_bos_desc);
|
|
|
|
break;
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
case GetHubStatus:
|
2012-01-12 19:53:15 +08:00
|
|
|
*(__le32 *) buf = cpu_to_le32(0);
|
2005-04-17 06:20:36 +08:00
|
|
|
break;
|
|
|
|
case GetPortStatus:
|
|
|
|
if (wIndex != 1)
|
|
|
|
retval = -EPIPE;
|
|
|
|
|
|
|
|
/* whoever resets or resumes must GetPortStatus to
|
|
|
|
* complete it!!
|
|
|
|
*/
|
2011-06-29 21:41:51 +08:00
|
|
|
if (dum_hcd->resuming &&
|
|
|
|
time_after_eq(jiffies, dum_hcd->re_timeout)) {
|
|
|
|
dum_hcd->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
|
|
|
|
dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2011-06-29 21:41:51 +08:00
|
|
|
if ((dum_hcd->port_status & USB_PORT_STAT_RESET) != 0 &&
|
|
|
|
time_after_eq(jiffies, dum_hcd->re_timeout)) {
|
|
|
|
dum_hcd->port_status |= (USB_PORT_STAT_C_RESET << 16);
|
|
|
|
dum_hcd->port_status &= ~USB_PORT_STAT_RESET;
|
|
|
|
if (dum_hcd->dum->pullup) {
|
|
|
|
dum_hcd->port_status |= USB_PORT_STAT_ENABLE;
|
2011-06-29 21:41:52 +08:00
|
|
|
|
|
|
|
if (hcd->speed < HCD_USB3) {
|
|
|
|
switch (dum_hcd->dum->gadget.speed) {
|
|
|
|
case USB_SPEED_HIGH:
|
|
|
|
dum_hcd->port_status |=
|
|
|
|
USB_PORT_STAT_HIGH_SPEED;
|
|
|
|
break;
|
|
|
|
case USB_SPEED_LOW:
|
|
|
|
dum_hcd->dum->gadget.ep0->
|
|
|
|
maxpacket = 8;
|
|
|
|
dum_hcd->port_status |=
|
|
|
|
USB_PORT_STAT_LOW_SPEED;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-06-29 21:41:51 +08:00
|
|
|
set_link_state(dum_hcd);
|
2012-01-12 19:53:15 +08:00
|
|
|
((__le16 *) buf)[0] = cpu_to_le16(dum_hcd->port_status);
|
|
|
|
((__le16 *) buf)[1] = cpu_to_le16(dum_hcd->port_status >> 16);
|
2005-04-17 06:20:36 +08:00
|
|
|
break;
|
|
|
|
case SetHubFeature:
|
|
|
|
retval = -EPIPE;
|
|
|
|
break;
|
|
|
|
case SetPortFeature:
|
|
|
|
switch (wValue) {
|
2011-06-29 21:41:52 +08:00
|
|
|
case USB_PORT_FEAT_LINK_STATE:
|
|
|
|
if (hcd->speed != HCD_USB3) {
|
|
|
|
dev_dbg(dummy_dev(dum_hcd),
|
|
|
|
"USB_PORT_FEAT_LINK_STATE req not "
|
|
|
|
"supported for USB 2.0 roothub\n");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Since this is dummy we don't have an actual link so
|
|
|
|
* there is nothing to do for the SET_LINK_STATE cmd
|
|
|
|
*/
|
|
|
|
break;
|
|
|
|
case USB_PORT_FEAT_U1_TIMEOUT:
|
|
|
|
case USB_PORT_FEAT_U2_TIMEOUT:
|
|
|
|
/* TODO: add suspend/resume support! */
|
|
|
|
if (hcd->speed != HCD_USB3) {
|
|
|
|
dev_dbg(dummy_dev(dum_hcd),
|
|
|
|
"USB_PORT_FEAT_U1/2_TIMEOUT req not "
|
|
|
|
"supported for USB 2.0 roothub\n");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
break;
|
2005-04-17 06:20:36 +08:00
|
|
|
case USB_PORT_FEAT_SUSPEND:
|
2011-06-29 21:41:52 +08:00
|
|
|
/* Applicable only for USB2.0 hub */
|
|
|
|
if (hcd->speed == HCD_USB3) {
|
|
|
|
dev_dbg(dummy_dev(dum_hcd),
|
|
|
|
"USB_PORT_FEAT_SUSPEND req not "
|
|
|
|
"supported for USB 3.0 roothub\n");
|
|
|
|
goto error;
|
|
|
|
}
|
2011-06-29 21:41:51 +08:00
|
|
|
if (dum_hcd->active) {
|
|
|
|
dum_hcd->port_status |= USB_PORT_STAT_SUSPEND;
|
2005-05-04 04:24:04 +08:00
|
|
|
|
|
|
|
/* HNP would happen here; for now we
|
|
|
|
* assume b_bus_req is always true.
|
|
|
|
*/
|
2011-06-29 21:41:51 +08:00
|
|
|
set_link_state(dum_hcd);
|
2005-05-04 04:24:04 +08:00
|
|
|
if (((1 << USB_DEVICE_B_HNP_ENABLE)
|
2011-06-29 21:41:51 +08:00
|
|
|
& dum_hcd->dum->devstatus) != 0)
|
|
|
|
dev_dbg(dummy_dev(dum_hcd),
|
2005-05-02 23:25:17 +08:00
|
|
|
"no HNP yet!\n");
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
break;
|
2005-05-04 04:24:04 +08:00
|
|
|
case USB_PORT_FEAT_POWER:
|
2011-06-29 21:41:52 +08:00
|
|
|
if (hcd->speed == HCD_USB3)
|
|
|
|
dum_hcd->port_status |= USB_SS_PORT_STAT_POWER;
|
|
|
|
else
|
|
|
|
dum_hcd->port_status |= USB_PORT_STAT_POWER;
|
2011-06-29 21:41:51 +08:00
|
|
|
set_link_state(dum_hcd);
|
2005-05-04 04:24:04 +08:00
|
|
|
break;
|
2011-06-29 21:41:52 +08:00
|
|
|
case USB_PORT_FEAT_BH_PORT_RESET:
|
|
|
|
/* Applicable only for USB3.0 hub */
|
|
|
|
if (hcd->speed != HCD_USB3) {
|
|
|
|
dev_dbg(dummy_dev(dum_hcd),
|
|
|
|
"USB_PORT_FEAT_BH_PORT_RESET req not "
|
|
|
|
"supported for USB 2.0 roothub\n");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
/* FALLS THROUGH */
|
2005-04-17 06:20:36 +08:00
|
|
|
case USB_PORT_FEAT_RESET:
|
2005-05-04 04:24:04 +08:00
|
|
|
/* if it's already enabled, disable */
|
2011-06-29 21:41:52 +08:00
|
|
|
if (hcd->speed == HCD_USB3) {
|
|
|
|
dum_hcd->port_status = 0;
|
|
|
|
dum_hcd->port_status =
|
|
|
|
(USB_SS_PORT_STAT_POWER |
|
|
|
|
USB_PORT_STAT_CONNECTION |
|
|
|
|
USB_PORT_STAT_RESET);
|
|
|
|
} else
|
|
|
|
dum_hcd->port_status &= ~(USB_PORT_STAT_ENABLE
|
2005-05-04 04:24:04 +08:00
|
|
|
| USB_PORT_STAT_LOW_SPEED
|
|
|
|
| USB_PORT_STAT_HIGH_SPEED);
|
2011-06-29 21:41:51 +08:00
|
|
|
/*
|
|
|
|
* We want to reset device status. All but the
|
|
|
|
* Self powered feature
|
|
|
|
*/
|
|
|
|
dum_hcd->dum->devstatus &=
|
|
|
|
(1 << USB_DEVICE_SELF_POWERED);
|
2011-06-29 21:41:52 +08:00
|
|
|
/*
|
|
|
|
* FIXME USB3.0: what is the correct reset signaling
|
|
|
|
* interval? Is it still 50msec as for HS?
|
|
|
|
*/
|
2011-06-29 21:41:51 +08:00
|
|
|
dum_hcd->re_timeout = jiffies + msecs_to_jiffies(50);
|
2005-05-04 04:24:04 +08:00
|
|
|
/* FALLS THROUGH */
|
2005-04-17 06:20:36 +08:00
|
|
|
default:
|
2011-06-29 21:41:52 +08:00
|
|
|
if (hcd->speed == HCD_USB3) {
|
|
|
|
if ((dum_hcd->port_status &
|
|
|
|
USB_SS_PORT_STAT_POWER) != 0) {
|
|
|
|
dum_hcd->port_status |= (1 << wValue);
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
if ((dum_hcd->port_status &
|
|
|
|
USB_PORT_STAT_POWER) != 0) {
|
|
|
|
dum_hcd->port_status |= (1 << wValue);
|
|
|
|
}
|
2017-03-24 04:06:11 +08:00
|
|
|
set_link_state(dum_hcd);
|
2011-06-29 21:41:52 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case GetPortErrorCount:
|
|
|
|
if (hcd->speed != HCD_USB3) {
|
|
|
|
dev_dbg(dummy_dev(dum_hcd),
|
|
|
|
"GetPortErrorCount req not "
|
|
|
|
"supported for USB 2.0 roothub\n");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
/* We'll always return 0 since this is a dummy hub */
|
|
|
|
*(__le32 *) buf = cpu_to_le32(0);
|
|
|
|
break;
|
|
|
|
case SetHubDepth:
|
|
|
|
if (hcd->speed != HCD_USB3) {
|
|
|
|
dev_dbg(dummy_dev(dum_hcd),
|
|
|
|
"SetHubDepth req not supported for "
|
|
|
|
"USB 2.0 roothub\n");
|
|
|
|
goto error;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2011-06-29 21:41:51 +08:00
|
|
|
dev_dbg(dummy_dev(dum_hcd),
|
2005-04-17 06:20:36 +08:00
|
|
|
"hub control req%04x v%04x i%04x l%d\n",
|
|
|
|
typeReq, wValue, wIndex, wLength);
|
2011-06-29 21:41:52 +08:00
|
|
|
error:
|
2005-04-17 06:20:36 +08:00
|
|
|
/* "protocol stall" on error */
|
|
|
|
retval = -EPIPE;
|
|
|
|
}
|
2011-06-29 21:41:51 +08:00
|
|
|
spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
|
2005-05-04 04:27:26 +08:00
|
|
|
|
2011-06-29 21:41:51 +08:00
|
|
|
if ((dum_hcd->port_status & PORT_C_MASK) != 0)
|
2012-01-12 19:53:15 +08:00
|
|
|
usb_hcd_poll_rh_status(hcd);
|
2005-04-17 06:20:36 +08:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2012-01-12 19:53:15 +08:00
|
|
|
static int dummy_bus_suspend(struct usb_hcd *hcd)
|
2005-05-11 03:34:16 +08:00
|
|
|
{
|
2011-06-29 21:41:51 +08:00
|
|
|
struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
|
2005-05-11 03:34:16 +08:00
|
|
|
|
2012-01-12 19:53:15 +08:00
|
|
|
dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__);
|
2005-11-30 01:08:15 +08:00
|
|
|
|
2011-06-29 21:41:51 +08:00
|
|
|
spin_lock_irq(&dum_hcd->dum->lock);
|
|
|
|
dum_hcd->rh_state = DUMMY_RH_SUSPENDED;
|
|
|
|
set_link_state(dum_hcd);
|
2005-11-30 01:08:15 +08:00
|
|
|
hcd->state = HC_STATE_SUSPENDED;
|
2011-06-29 21:41:51 +08:00
|
|
|
spin_unlock_irq(&dum_hcd->dum->lock);
|
2005-05-11 03:34:16 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-01-12 19:53:15 +08:00
|
|
|
static int dummy_bus_resume(struct usb_hcd *hcd)
|
2005-05-11 03:34:16 +08:00
|
|
|
{
|
2011-06-29 21:41:51 +08:00
|
|
|
struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
|
2005-11-30 01:08:15 +08:00
|
|
|
int rc = 0;
|
|
|
|
|
2012-01-12 19:53:15 +08:00
|
|
|
dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__);
|
2005-05-11 03:34:16 +08:00
|
|
|
|
2011-06-29 21:41:51 +08:00
|
|
|
spin_lock_irq(&dum_hcd->dum->lock);
|
2010-06-23 04:39:10 +08:00
|
|
|
if (!HCD_HW_ACCESSIBLE(hcd)) {
|
2007-06-22 04:25:35 +08:00
|
|
|
rc = -ESHUTDOWN;
|
2005-11-30 01:08:15 +08:00
|
|
|
} else {
|
2011-06-29 21:41:51 +08:00
|
|
|
dum_hcd->rh_state = DUMMY_RH_RUNNING;
|
|
|
|
set_link_state(dum_hcd);
|
|
|
|
if (!list_empty(&dum_hcd->urbp_list))
|
|
|
|
mod_timer(&dum_hcd->timer, jiffies);
|
2005-11-30 01:08:15 +08:00
|
|
|
hcd->state = HC_STATE_RUNNING;
|
|
|
|
}
|
2011-06-29 21:41:51 +08:00
|
|
|
spin_unlock_irq(&dum_hcd->dum->lock);
|
2005-11-30 01:08:15 +08:00
|
|
|
return rc;
|
2005-05-11 03:34:16 +08:00
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
|
2012-01-12 19:53:15 +08:00
|
|
|
static inline ssize_t show_urb(char *buf, size_t size, struct urb *urb)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2012-01-12 19:53:15 +08:00
|
|
|
int ep = usb_pipeendpoint(urb->pipe);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2018-02-27 10:04:22 +08:00
|
|
|
return scnprintf(buf, size,
|
2005-04-17 06:20:36 +08:00
|
|
|
"urb/%p %s ep%d%s%s len %d/%d\n",
|
|
|
|
urb,
|
|
|
|
({ char *s;
|
2012-01-12 19:53:15 +08:00
|
|
|
switch (urb->dev->speed) {
|
|
|
|
case USB_SPEED_LOW:
|
2011-06-28 21:33:52 +08:00
|
|
|
s = "ls";
|
|
|
|
break;
|
2012-01-12 19:53:15 +08:00
|
|
|
case USB_SPEED_FULL:
|
2011-06-28 21:33:52 +08:00
|
|
|
s = "fs";
|
|
|
|
break;
|
2012-01-12 19:53:15 +08:00
|
|
|
case USB_SPEED_HIGH:
|
2011-06-28 21:33:52 +08:00
|
|
|
s = "hs";
|
|
|
|
break;
|
2012-01-12 19:53:15 +08:00
|
|
|
case USB_SPEED_SUPER:
|
2011-06-29 21:41:52 +08:00
|
|
|
s = "ss";
|
|
|
|
break;
|
2012-01-12 19:53:15 +08:00
|
|
|
default:
|
2011-06-28 21:33:52 +08:00
|
|
|
s = "?";
|
|
|
|
break;
|
2013-10-09 07:01:37 +08:00
|
|
|
} s; }),
|
2024-06-11 20:08:33 +08:00
|
|
|
ep, ep ? (usb_urb_dir_in(urb) ? "in" : "out") : "",
|
2005-04-17 06:20:36 +08:00
|
|
|
({ char *s; \
|
2012-01-12 19:53:15 +08:00
|
|
|
switch (usb_pipetype(urb->pipe)) { \
|
|
|
|
case PIPE_CONTROL: \
|
2011-06-28 21:33:52 +08:00
|
|
|
s = ""; \
|
|
|
|
break; \
|
2012-01-12 19:53:15 +08:00
|
|
|
case PIPE_BULK: \
|
2011-06-28 21:33:52 +08:00
|
|
|
s = "-bulk"; \
|
|
|
|
break; \
|
2012-01-12 19:53:15 +08:00
|
|
|
case PIPE_INTERRUPT: \
|
2011-06-28 21:33:52 +08:00
|
|
|
s = "-int"; \
|
|
|
|
break; \
|
2012-01-12 19:53:15 +08:00
|
|
|
default: \
|
2011-06-28 21:33:52 +08:00
|
|
|
s = "-iso"; \
|
|
|
|
break; \
|
2013-10-09 07:01:37 +08:00
|
|
|
} s; }),
|
2005-04-17 06:20:36 +08:00
|
|
|
urb->actual_length, urb->transfer_buffer_length);
|
|
|
|
}
|
|
|
|
|
2013-08-24 07:34:43 +08:00
|
|
|
static ssize_t urbs_show(struct device *dev, struct device_attribute *attr,
|
2012-01-12 19:53:15 +08:00
|
|
|
char *buf)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2012-01-12 19:53:15 +08:00
|
|
|
struct usb_hcd *hcd = dev_get_drvdata(dev);
|
2011-06-29 21:41:51 +08:00
|
|
|
struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
|
2005-04-17 06:20:36 +08:00
|
|
|
struct urbp *urbp;
|
|
|
|
size_t size = 0;
|
|
|
|
unsigned long flags;
|
|
|
|
|
2011-06-29 21:41:51 +08:00
|
|
|
spin_lock_irqsave(&dum_hcd->dum->lock, flags);
|
|
|
|
list_for_each_entry(urbp, &dum_hcd->urbp_list, urbp_list) {
|
2005-04-17 06:20:36 +08:00
|
|
|
size_t temp;
|
|
|
|
|
2012-01-12 19:53:15 +08:00
|
|
|
temp = show_urb(buf, PAGE_SIZE - size, urbp->urb);
|
2005-04-17 06:20:36 +08:00
|
|
|
buf += temp;
|
|
|
|
size += temp;
|
|
|
|
}
|
2011-06-29 21:41:51 +08:00
|
|
|
spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
2013-08-24 07:34:43 +08:00
|
|
|
static DEVICE_ATTR_RO(urbs);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2011-06-29 21:41:52 +08:00
|
|
|
static int dummy_start_ss(struct dummy_hcd *dum_hcd)
|
|
|
|
{
|
treewide: setup_timer() -> timer_setup()
This converts all remaining cases of the old setup_timer() API into using
timer_setup(), where the callback argument is the structure already
holding the struct timer_list. These should have no behavioral changes,
since they just change which pointer is passed into the callback with
the same available pointers after conversion. It handles the following
examples, in addition to some other variations.
Casting from unsigned long:
void my_callback(unsigned long data)
{
struct something *ptr = (struct something *)data;
...
}
...
setup_timer(&ptr->my_timer, my_callback, ptr);
and forced object casts:
void my_callback(struct something *ptr)
{
...
}
...
setup_timer(&ptr->my_timer, my_callback, (unsigned long)ptr);
become:
void my_callback(struct timer_list *t)
{
struct something *ptr = from_timer(ptr, t, my_timer);
...
}
...
timer_setup(&ptr->my_timer, my_callback, 0);
Direct function assignments:
void my_callback(unsigned long data)
{
struct something *ptr = (struct something *)data;
...
}
...
ptr->my_timer.function = my_callback;
have a temporary cast added, along with converting the args:
void my_callback(struct timer_list *t)
{
struct something *ptr = from_timer(ptr, t, my_timer);
...
}
...
ptr->my_timer.function = (TIMER_FUNC_TYPE)my_callback;
And finally, callbacks without a data assignment:
void my_callback(unsigned long data)
{
...
}
...
setup_timer(&ptr->my_timer, my_callback, 0);
have their argument renamed to verify they're unused during conversion:
void my_callback(struct timer_list *unused)
{
...
}
...
timer_setup(&ptr->my_timer, my_callback, 0);
The conversion is done with the following Coccinelle script:
spatch --very-quiet --all-includes --include-headers \
-I ./arch/x86/include -I ./arch/x86/include/generated \
-I ./include -I ./arch/x86/include/uapi \
-I ./arch/x86/include/generated/uapi -I ./include/uapi \
-I ./include/generated/uapi --include ./include/linux/kconfig.h \
--dir . \
--cocci-file ~/src/data/timer_setup.cocci
@fix_address_of@
expression e;
@@
setup_timer(
-&(e)
+&e
, ...)
// Update any raw setup_timer() usages that have a NULL callback, but
// would otherwise match change_timer_function_usage, since the latter
// will update all function assignments done in the face of a NULL
// function initialization in setup_timer().
@change_timer_function_usage_NULL@
expression _E;
identifier _timer;
type _cast_data;
@@
(
-setup_timer(&_E->_timer, NULL, _E);
+timer_setup(&_E->_timer, NULL, 0);
|
-setup_timer(&_E->_timer, NULL, (_cast_data)_E);
+timer_setup(&_E->_timer, NULL, 0);
|
-setup_timer(&_E._timer, NULL, &_E);
+timer_setup(&_E._timer, NULL, 0);
|
-setup_timer(&_E._timer, NULL, (_cast_data)&_E);
+timer_setup(&_E._timer, NULL, 0);
)
@change_timer_function_usage@
expression _E;
identifier _timer;
struct timer_list _stl;
identifier _callback;
type _cast_func, _cast_data;
@@
(
-setup_timer(&_E->_timer, _callback, _E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, &_callback, _E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, _callback, (_cast_data)_E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, &_callback, (_cast_data)_E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, (_cast_func)_callback, _E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, (_cast_func)&_callback, _E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, (_cast_func)_callback, (_cast_data)_E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, (_cast_func)&_callback, (_cast_data)_E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, (_cast_data)_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, (_cast_data)&_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, &_callback, (_cast_data)_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, &_callback, (_cast_data)&_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, (_cast_func)_callback, (_cast_data)_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, (_cast_func)_callback, (_cast_data)&_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, (_cast_func)&_callback, (_cast_data)_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, (_cast_func)&_callback, (_cast_data)&_E);
+timer_setup(&_E._timer, _callback, 0);
|
_E->_timer@_stl.function = _callback;
|
_E->_timer@_stl.function = &_callback;
|
_E->_timer@_stl.function = (_cast_func)_callback;
|
_E->_timer@_stl.function = (_cast_func)&_callback;
|
_E._timer@_stl.function = _callback;
|
_E._timer@_stl.function = &_callback;
|
_E._timer@_stl.function = (_cast_func)_callback;
|
_E._timer@_stl.function = (_cast_func)&_callback;
)
// callback(unsigned long arg)
@change_callback_handle_cast
depends on change_timer_function_usage@
identifier change_timer_function_usage._callback;
identifier change_timer_function_usage._timer;
type _origtype;
identifier _origarg;
type _handletype;
identifier _handle;
@@
void _callback(
-_origtype _origarg
+struct timer_list *t
)
{
(
... when != _origarg
_handletype *_handle =
-(_handletype *)_origarg;
+from_timer(_handle, t, _timer);
... when != _origarg
|
... when != _origarg
_handletype *_handle =
-(void *)_origarg;
+from_timer(_handle, t, _timer);
... when != _origarg
|
... when != _origarg
_handletype *_handle;
... when != _handle
_handle =
-(_handletype *)_origarg;
+from_timer(_handle, t, _timer);
... when != _origarg
|
... when != _origarg
_handletype *_handle;
... when != _handle
_handle =
-(void *)_origarg;
+from_timer(_handle, t, _timer);
... when != _origarg
)
}
// callback(unsigned long arg) without existing variable
@change_callback_handle_cast_no_arg
depends on change_timer_function_usage &&
!change_callback_handle_cast@
identifier change_timer_function_usage._callback;
identifier change_timer_function_usage._timer;
type _origtype;
identifier _origarg;
type _handletype;
@@
void _callback(
-_origtype _origarg
+struct timer_list *t
)
{
+ _handletype *_origarg = from_timer(_origarg, t, _timer);
+
... when != _origarg
- (_handletype *)_origarg
+ _origarg
... when != _origarg
}
// Avoid already converted callbacks.
@match_callback_converted
depends on change_timer_function_usage &&
!change_callback_handle_cast &&
!change_callback_handle_cast_no_arg@
identifier change_timer_function_usage._callback;
identifier t;
@@
void _callback(struct timer_list *t)
{ ... }
// callback(struct something *handle)
@change_callback_handle_arg
depends on change_timer_function_usage &&
!match_callback_converted &&
!change_callback_handle_cast &&
!change_callback_handle_cast_no_arg@
identifier change_timer_function_usage._callback;
identifier change_timer_function_usage._timer;
type _handletype;
identifier _handle;
@@
void _callback(
-_handletype *_handle
+struct timer_list *t
)
{
+ _handletype *_handle = from_timer(_handle, t, _timer);
...
}
// If change_callback_handle_arg ran on an empty function, remove
// the added handler.
@unchange_callback_handle_arg
depends on change_timer_function_usage &&
change_callback_handle_arg@
identifier change_timer_function_usage._callback;
identifier change_timer_function_usage._timer;
type _handletype;
identifier _handle;
identifier t;
@@
void _callback(struct timer_list *t)
{
- _handletype *_handle = from_timer(_handle, t, _timer);
}
// We only want to refactor the setup_timer() data argument if we've found
// the matching callback. This undoes changes in change_timer_function_usage.
@unchange_timer_function_usage
depends on change_timer_function_usage &&
!change_callback_handle_cast &&
!change_callback_handle_cast_no_arg &&
!change_callback_handle_arg@
expression change_timer_function_usage._E;
identifier change_timer_function_usage._timer;
identifier change_timer_function_usage._callback;
type change_timer_function_usage._cast_data;
@@
(
-timer_setup(&_E->_timer, _callback, 0);
+setup_timer(&_E->_timer, _callback, (_cast_data)_E);
|
-timer_setup(&_E._timer, _callback, 0);
+setup_timer(&_E._timer, _callback, (_cast_data)&_E);
)
// If we fixed a callback from a .function assignment, fix the
// assignment cast now.
@change_timer_function_assignment
depends on change_timer_function_usage &&
(change_callback_handle_cast ||
change_callback_handle_cast_no_arg ||
change_callback_handle_arg)@
expression change_timer_function_usage._E;
identifier change_timer_function_usage._timer;
identifier change_timer_function_usage._callback;
type _cast_func;
typedef TIMER_FUNC_TYPE;
@@
(
_E->_timer.function =
-_callback
+(TIMER_FUNC_TYPE)_callback
;
|
_E->_timer.function =
-&_callback
+(TIMER_FUNC_TYPE)_callback
;
|
_E->_timer.function =
-(_cast_func)_callback;
+(TIMER_FUNC_TYPE)_callback
;
|
_E->_timer.function =
-(_cast_func)&_callback
+(TIMER_FUNC_TYPE)_callback
;
|
_E._timer.function =
-_callback
+(TIMER_FUNC_TYPE)_callback
;
|
_E._timer.function =
-&_callback;
+(TIMER_FUNC_TYPE)_callback
;
|
_E._timer.function =
-(_cast_func)_callback
+(TIMER_FUNC_TYPE)_callback
;
|
_E._timer.function =
-(_cast_func)&_callback
+(TIMER_FUNC_TYPE)_callback
;
)
// Sometimes timer functions are called directly. Replace matched args.
@change_timer_function_calls
depends on change_timer_function_usage &&
(change_callback_handle_cast ||
change_callback_handle_cast_no_arg ||
change_callback_handle_arg)@
expression _E;
identifier change_timer_function_usage._timer;
identifier change_timer_function_usage._callback;
type _cast_data;
@@
_callback(
(
-(_cast_data)_E
+&_E->_timer
|
-(_cast_data)&_E
+&_E._timer
|
-_E
+&_E->_timer
)
)
// If a timer has been configured without a data argument, it can be
// converted without regard to the callback argument, since it is unused.
@match_timer_function_unused_data@
expression _E;
identifier _timer;
identifier _callback;
@@
(
-setup_timer(&_E->_timer, _callback, 0);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, _callback, 0L);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, _callback, 0UL);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, 0);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, 0L);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, 0UL);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_timer, _callback, 0);
+timer_setup(&_timer, _callback, 0);
|
-setup_timer(&_timer, _callback, 0L);
+timer_setup(&_timer, _callback, 0);
|
-setup_timer(&_timer, _callback, 0UL);
+timer_setup(&_timer, _callback, 0);
|
-setup_timer(_timer, _callback, 0);
+timer_setup(_timer, _callback, 0);
|
-setup_timer(_timer, _callback, 0L);
+timer_setup(_timer, _callback, 0);
|
-setup_timer(_timer, _callback, 0UL);
+timer_setup(_timer, _callback, 0);
)
@change_callback_unused_data
depends on match_timer_function_unused_data@
identifier match_timer_function_unused_data._callback;
type _origtype;
identifier _origarg;
@@
void _callback(
-_origtype _origarg
+struct timer_list *unused
)
{
... when != _origarg
}
Signed-off-by: Kees Cook <keescook@chromium.org>
2017-10-17 05:43:17 +08:00
|
|
|
timer_setup(&dum_hcd->timer, dummy_timer, 0);
|
2011-06-29 21:41:52 +08:00
|
|
|
dum_hcd->rh_state = DUMMY_RH_RUNNING;
|
2012-01-14 04:51:47 +08:00
|
|
|
dum_hcd->stream_en_ep = 0;
|
2011-06-29 21:41:52 +08:00
|
|
|
INIT_LIST_HEAD(&dum_hcd->urbp_list);
|
2019-09-05 12:11:57 +08:00
|
|
|
dummy_hcd_to_hcd(dum_hcd)->power_budget = POWER_BUDGET_3;
|
2011-06-29 21:41:52 +08:00
|
|
|
dummy_hcd_to_hcd(dum_hcd)->state = HC_STATE_RUNNING;
|
|
|
|
dummy_hcd_to_hcd(dum_hcd)->uses_new_polling = 1;
|
|
|
|
#ifdef CONFIG_USB_OTG
|
|
|
|
dummy_hcd_to_hcd(dum_hcd)->self.otg_port = 1;
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* FIXME 'urbs' should be a per-device thing, maybe in usbcore */
|
|
|
|
return device_create_file(dummy_dev(dum_hcd), &dev_attr_urbs);
|
|
|
|
}
|
|
|
|
|
2011-06-29 21:41:51 +08:00
|
|
|
static int dummy_start(struct usb_hcd *hcd)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2011-06-29 21:41:51 +08:00
|
|
|
struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* MASTER side init ... we emulate a root hub that'll only ever
|
|
|
|
* talk to one device (the slave side). Also appears in sysfs,
|
|
|
|
* just like more familiar pci-based HCDs.
|
|
|
|
*/
|
2011-06-29 21:41:52 +08:00
|
|
|
if (!usb_hcd_is_primary_hcd(hcd))
|
|
|
|
return dummy_start_ss(dum_hcd);
|
|
|
|
|
2011-06-29 21:41:51 +08:00
|
|
|
spin_lock_init(&dum_hcd->dum->lock);
|
treewide: setup_timer() -> timer_setup()
This converts all remaining cases of the old setup_timer() API into using
timer_setup(), where the callback argument is the structure already
holding the struct timer_list. These should have no behavioral changes,
since they just change which pointer is passed into the callback with
the same available pointers after conversion. It handles the following
examples, in addition to some other variations.
Casting from unsigned long:
void my_callback(unsigned long data)
{
struct something *ptr = (struct something *)data;
...
}
...
setup_timer(&ptr->my_timer, my_callback, ptr);
and forced object casts:
void my_callback(struct something *ptr)
{
...
}
...
setup_timer(&ptr->my_timer, my_callback, (unsigned long)ptr);
become:
void my_callback(struct timer_list *t)
{
struct something *ptr = from_timer(ptr, t, my_timer);
...
}
...
timer_setup(&ptr->my_timer, my_callback, 0);
Direct function assignments:
void my_callback(unsigned long data)
{
struct something *ptr = (struct something *)data;
...
}
...
ptr->my_timer.function = my_callback;
have a temporary cast added, along with converting the args:
void my_callback(struct timer_list *t)
{
struct something *ptr = from_timer(ptr, t, my_timer);
...
}
...
ptr->my_timer.function = (TIMER_FUNC_TYPE)my_callback;
And finally, callbacks without a data assignment:
void my_callback(unsigned long data)
{
...
}
...
setup_timer(&ptr->my_timer, my_callback, 0);
have their argument renamed to verify they're unused during conversion:
void my_callback(struct timer_list *unused)
{
...
}
...
timer_setup(&ptr->my_timer, my_callback, 0);
The conversion is done with the following Coccinelle script:
spatch --very-quiet --all-includes --include-headers \
-I ./arch/x86/include -I ./arch/x86/include/generated \
-I ./include -I ./arch/x86/include/uapi \
-I ./arch/x86/include/generated/uapi -I ./include/uapi \
-I ./include/generated/uapi --include ./include/linux/kconfig.h \
--dir . \
--cocci-file ~/src/data/timer_setup.cocci
@fix_address_of@
expression e;
@@
setup_timer(
-&(e)
+&e
, ...)
// Update any raw setup_timer() usages that have a NULL callback, but
// would otherwise match change_timer_function_usage, since the latter
// will update all function assignments done in the face of a NULL
// function initialization in setup_timer().
@change_timer_function_usage_NULL@
expression _E;
identifier _timer;
type _cast_data;
@@
(
-setup_timer(&_E->_timer, NULL, _E);
+timer_setup(&_E->_timer, NULL, 0);
|
-setup_timer(&_E->_timer, NULL, (_cast_data)_E);
+timer_setup(&_E->_timer, NULL, 0);
|
-setup_timer(&_E._timer, NULL, &_E);
+timer_setup(&_E._timer, NULL, 0);
|
-setup_timer(&_E._timer, NULL, (_cast_data)&_E);
+timer_setup(&_E._timer, NULL, 0);
)
@change_timer_function_usage@
expression _E;
identifier _timer;
struct timer_list _stl;
identifier _callback;
type _cast_func, _cast_data;
@@
(
-setup_timer(&_E->_timer, _callback, _E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, &_callback, _E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, _callback, (_cast_data)_E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, &_callback, (_cast_data)_E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, (_cast_func)_callback, _E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, (_cast_func)&_callback, _E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, (_cast_func)_callback, (_cast_data)_E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, (_cast_func)&_callback, (_cast_data)_E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, (_cast_data)_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, (_cast_data)&_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, &_callback, (_cast_data)_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, &_callback, (_cast_data)&_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, (_cast_func)_callback, (_cast_data)_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, (_cast_func)_callback, (_cast_data)&_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, (_cast_func)&_callback, (_cast_data)_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, (_cast_func)&_callback, (_cast_data)&_E);
+timer_setup(&_E._timer, _callback, 0);
|
_E->_timer@_stl.function = _callback;
|
_E->_timer@_stl.function = &_callback;
|
_E->_timer@_stl.function = (_cast_func)_callback;
|
_E->_timer@_stl.function = (_cast_func)&_callback;
|
_E._timer@_stl.function = _callback;
|
_E._timer@_stl.function = &_callback;
|
_E._timer@_stl.function = (_cast_func)_callback;
|
_E._timer@_stl.function = (_cast_func)&_callback;
)
// callback(unsigned long arg)
@change_callback_handle_cast
depends on change_timer_function_usage@
identifier change_timer_function_usage._callback;
identifier change_timer_function_usage._timer;
type _origtype;
identifier _origarg;
type _handletype;
identifier _handle;
@@
void _callback(
-_origtype _origarg
+struct timer_list *t
)
{
(
... when != _origarg
_handletype *_handle =
-(_handletype *)_origarg;
+from_timer(_handle, t, _timer);
... when != _origarg
|
... when != _origarg
_handletype *_handle =
-(void *)_origarg;
+from_timer(_handle, t, _timer);
... when != _origarg
|
... when != _origarg
_handletype *_handle;
... when != _handle
_handle =
-(_handletype *)_origarg;
+from_timer(_handle, t, _timer);
... when != _origarg
|
... when != _origarg
_handletype *_handle;
... when != _handle
_handle =
-(void *)_origarg;
+from_timer(_handle, t, _timer);
... when != _origarg
)
}
// callback(unsigned long arg) without existing variable
@change_callback_handle_cast_no_arg
depends on change_timer_function_usage &&
!change_callback_handle_cast@
identifier change_timer_function_usage._callback;
identifier change_timer_function_usage._timer;
type _origtype;
identifier _origarg;
type _handletype;
@@
void _callback(
-_origtype _origarg
+struct timer_list *t
)
{
+ _handletype *_origarg = from_timer(_origarg, t, _timer);
+
... when != _origarg
- (_handletype *)_origarg
+ _origarg
... when != _origarg
}
// Avoid already converted callbacks.
@match_callback_converted
depends on change_timer_function_usage &&
!change_callback_handle_cast &&
!change_callback_handle_cast_no_arg@
identifier change_timer_function_usage._callback;
identifier t;
@@
void _callback(struct timer_list *t)
{ ... }
// callback(struct something *handle)
@change_callback_handle_arg
depends on change_timer_function_usage &&
!match_callback_converted &&
!change_callback_handle_cast &&
!change_callback_handle_cast_no_arg@
identifier change_timer_function_usage._callback;
identifier change_timer_function_usage._timer;
type _handletype;
identifier _handle;
@@
void _callback(
-_handletype *_handle
+struct timer_list *t
)
{
+ _handletype *_handle = from_timer(_handle, t, _timer);
...
}
// If change_callback_handle_arg ran on an empty function, remove
// the added handler.
@unchange_callback_handle_arg
depends on change_timer_function_usage &&
change_callback_handle_arg@
identifier change_timer_function_usage._callback;
identifier change_timer_function_usage._timer;
type _handletype;
identifier _handle;
identifier t;
@@
void _callback(struct timer_list *t)
{
- _handletype *_handle = from_timer(_handle, t, _timer);
}
// We only want to refactor the setup_timer() data argument if we've found
// the matching callback. This undoes changes in change_timer_function_usage.
@unchange_timer_function_usage
depends on change_timer_function_usage &&
!change_callback_handle_cast &&
!change_callback_handle_cast_no_arg &&
!change_callback_handle_arg@
expression change_timer_function_usage._E;
identifier change_timer_function_usage._timer;
identifier change_timer_function_usage._callback;
type change_timer_function_usage._cast_data;
@@
(
-timer_setup(&_E->_timer, _callback, 0);
+setup_timer(&_E->_timer, _callback, (_cast_data)_E);
|
-timer_setup(&_E._timer, _callback, 0);
+setup_timer(&_E._timer, _callback, (_cast_data)&_E);
)
// If we fixed a callback from a .function assignment, fix the
// assignment cast now.
@change_timer_function_assignment
depends on change_timer_function_usage &&
(change_callback_handle_cast ||
change_callback_handle_cast_no_arg ||
change_callback_handle_arg)@
expression change_timer_function_usage._E;
identifier change_timer_function_usage._timer;
identifier change_timer_function_usage._callback;
type _cast_func;
typedef TIMER_FUNC_TYPE;
@@
(
_E->_timer.function =
-_callback
+(TIMER_FUNC_TYPE)_callback
;
|
_E->_timer.function =
-&_callback
+(TIMER_FUNC_TYPE)_callback
;
|
_E->_timer.function =
-(_cast_func)_callback;
+(TIMER_FUNC_TYPE)_callback
;
|
_E->_timer.function =
-(_cast_func)&_callback
+(TIMER_FUNC_TYPE)_callback
;
|
_E._timer.function =
-_callback
+(TIMER_FUNC_TYPE)_callback
;
|
_E._timer.function =
-&_callback;
+(TIMER_FUNC_TYPE)_callback
;
|
_E._timer.function =
-(_cast_func)_callback
+(TIMER_FUNC_TYPE)_callback
;
|
_E._timer.function =
-(_cast_func)&_callback
+(TIMER_FUNC_TYPE)_callback
;
)
// Sometimes timer functions are called directly. Replace matched args.
@change_timer_function_calls
depends on change_timer_function_usage &&
(change_callback_handle_cast ||
change_callback_handle_cast_no_arg ||
change_callback_handle_arg)@
expression _E;
identifier change_timer_function_usage._timer;
identifier change_timer_function_usage._callback;
type _cast_data;
@@
_callback(
(
-(_cast_data)_E
+&_E->_timer
|
-(_cast_data)&_E
+&_E._timer
|
-_E
+&_E->_timer
)
)
// If a timer has been configured without a data argument, it can be
// converted without regard to the callback argument, since it is unused.
@match_timer_function_unused_data@
expression _E;
identifier _timer;
identifier _callback;
@@
(
-setup_timer(&_E->_timer, _callback, 0);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, _callback, 0L);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, _callback, 0UL);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, 0);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, 0L);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, 0UL);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_timer, _callback, 0);
+timer_setup(&_timer, _callback, 0);
|
-setup_timer(&_timer, _callback, 0L);
+timer_setup(&_timer, _callback, 0);
|
-setup_timer(&_timer, _callback, 0UL);
+timer_setup(&_timer, _callback, 0);
|
-setup_timer(_timer, _callback, 0);
+timer_setup(_timer, _callback, 0);
|
-setup_timer(_timer, _callback, 0L);
+timer_setup(_timer, _callback, 0);
|
-setup_timer(_timer, _callback, 0UL);
+timer_setup(_timer, _callback, 0);
)
@change_callback_unused_data
depends on match_timer_function_unused_data@
identifier match_timer_function_unused_data._callback;
type _origtype;
identifier _origarg;
@@
void _callback(
-_origtype _origarg
+struct timer_list *unused
)
{
... when != _origarg
}
Signed-off-by: Kees Cook <keescook@chromium.org>
2017-10-17 05:43:17 +08:00
|
|
|
timer_setup(&dum_hcd->timer, dummy_timer, 0);
|
2011-06-29 21:41:51 +08:00
|
|
|
dum_hcd->rh_state = DUMMY_RH_RUNNING;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2011-06-29 21:41:51 +08:00
|
|
|
INIT_LIST_HEAD(&dum_hcd->urbp_list);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2007-12-07 00:10:39 +08:00
|
|
|
hcd->power_budget = POWER_BUDGET;
|
2005-04-17 06:20:36 +08:00
|
|
|
hcd->state = HC_STATE_RUNNING;
|
2005-05-04 04:27:26 +08:00
|
|
|
hcd->uses_new_polling = 1;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2005-05-02 23:25:17 +08:00
|
|
|
#ifdef CONFIG_USB_OTG
|
|
|
|
hcd->self.otg_port = 1;
|
|
|
|
#endif
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/* FIXME 'urbs' should be a per-device thing, maybe in usbcore */
|
2011-06-29 21:41:51 +08:00
|
|
|
return device_create_file(dummy_dev(dum_hcd), &dev_attr_urbs);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2012-01-12 19:53:15 +08:00
|
|
|
static void dummy_stop(struct usb_hcd *hcd)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2011-06-29 21:41:51 +08:00
|
|
|
device_remove_file(dummy_dev(hcd_to_dummy_hcd(hcd)), &dev_attr_urbs);
|
|
|
|
dev_info(dummy_dev(hcd_to_dummy_hcd(hcd)), "stopped\n");
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
|
2012-01-12 19:53:15 +08:00
|
|
|
static int dummy_h_get_frame(struct usb_hcd *hcd)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2012-01-12 19:53:15 +08:00
|
|
|
return dummy_g_get_frame(NULL);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2011-06-29 21:41:51 +08:00
|
|
|
static int dummy_setup(struct usb_hcd *hcd)
|
|
|
|
{
|
2012-10-30 01:09:56 +08:00
|
|
|
struct dummy *dum;
|
|
|
|
|
|
|
|
dum = *((void **)dev_get_platdata(hcd->self.controller));
|
2012-01-10 02:30:50 +08:00
|
|
|
hcd->self.sg_tablesize = ~0;
|
2011-06-29 21:41:51 +08:00
|
|
|
if (usb_hcd_is_primary_hcd(hcd)) {
|
2012-10-30 01:09:56 +08:00
|
|
|
dum->hs_hcd = hcd_to_dummy_hcd(hcd);
|
|
|
|
dum->hs_hcd->dum = dum;
|
2011-06-29 21:41:52 +08:00
|
|
|
/*
|
|
|
|
* Mark the first roothub as being USB 2.0.
|
|
|
|
* The USB 3.0 roothub will be registered later by
|
|
|
|
* dummy_hcd_probe()
|
|
|
|
*/
|
2011-06-29 21:41:51 +08:00
|
|
|
hcd->speed = HCD_USB2;
|
|
|
|
hcd->self.root_hub->speed = USB_SPEED_HIGH;
|
2011-06-29 21:41:52 +08:00
|
|
|
} else {
|
2012-10-30 01:09:56 +08:00
|
|
|
dum->ss_hcd = hcd_to_dummy_hcd(hcd);
|
|
|
|
dum->ss_hcd->dum = dum;
|
2011-06-29 21:41:52 +08:00
|
|
|
hcd->speed = HCD_USB3;
|
|
|
|
hcd->self.root_hub->speed = USB_SPEED_SUPER;
|
2011-06-29 21:41:51 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-06-29 21:41:52 +08:00
|
|
|
/* Change a group of bulk endpoints to support multiple stream IDs */
|
2012-01-09 20:15:00 +08:00
|
|
|
static int dummy_alloc_streams(struct usb_hcd *hcd, struct usb_device *udev,
|
2011-06-29 21:41:52 +08:00
|
|
|
struct usb_host_endpoint **eps, unsigned int num_eps,
|
|
|
|
unsigned int num_streams, gfp_t mem_flags)
|
|
|
|
{
|
2012-01-14 04:51:47 +08:00
|
|
|
struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
|
|
|
|
unsigned long flags;
|
|
|
|
int max_stream;
|
|
|
|
int ret_streams = num_streams;
|
|
|
|
unsigned int index;
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
if (!num_eps)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&dum_hcd->dum->lock, flags);
|
|
|
|
for (i = 0; i < num_eps; i++) {
|
|
|
|
index = dummy_get_ep_idx(&eps[i]->desc);
|
|
|
|
if ((1 << index) & dum_hcd->stream_en_ep) {
|
|
|
|
ret_streams = -EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
max_stream = usb_ss_max_streams(&eps[i]->ss_ep_comp);
|
|
|
|
if (!max_stream) {
|
|
|
|
ret_streams = -EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (max_stream < ret_streams) {
|
|
|
|
dev_dbg(dummy_dev(dum_hcd), "Ep 0x%x only supports %u "
|
|
|
|
"stream IDs.\n",
|
|
|
|
eps[i]->desc.bEndpointAddress,
|
|
|
|
max_stream);
|
|
|
|
ret_streams = max_stream;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < num_eps; i++) {
|
|
|
|
index = dummy_get_ep_idx(&eps[i]->desc);
|
|
|
|
dum_hcd->stream_en_ep |= 1 << index;
|
|
|
|
set_max_streams_for_pipe(dum_hcd,
|
|
|
|
usb_endpoint_num(&eps[i]->desc), ret_streams);
|
|
|
|
}
|
|
|
|
out:
|
|
|
|
spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
|
|
|
|
return ret_streams;
|
2011-06-29 21:41:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Reverts a group of bulk endpoints back to not using stream IDs. */
|
2012-01-09 20:15:00 +08:00
|
|
|
static int dummy_free_streams(struct usb_hcd *hcd, struct usb_device *udev,
|
2011-06-29 21:41:52 +08:00
|
|
|
struct usb_host_endpoint **eps, unsigned int num_eps,
|
|
|
|
gfp_t mem_flags)
|
|
|
|
{
|
2012-01-14 04:51:47 +08:00
|
|
|
struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
|
|
|
|
unsigned long flags;
|
|
|
|
int ret;
|
|
|
|
unsigned int index;
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&dum_hcd->dum->lock, flags);
|
|
|
|
for (i = 0; i < num_eps; i++) {
|
|
|
|
index = dummy_get_ep_idx(&eps[i]->desc);
|
|
|
|
if (!((1 << index) & dum_hcd->stream_en_ep)) {
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < num_eps; i++) {
|
|
|
|
index = dummy_get_ep_idx(&eps[i]->desc);
|
|
|
|
dum_hcd->stream_en_ep &= ~(1 << index);
|
|
|
|
set_max_streams_for_pipe(dum_hcd,
|
|
|
|
usb_endpoint_num(&eps[i]->desc), 0);
|
|
|
|
}
|
|
|
|
ret = 0;
|
|
|
|
out:
|
|
|
|
spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
|
|
|
|
return ret;
|
2011-06-29 21:41:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct hc_driver dummy_hcd = {
|
2005-04-17 06:20:36 +08:00
|
|
|
.description = (char *) driver_name,
|
|
|
|
.product_desc = "Dummy host controller",
|
2011-06-29 21:41:51 +08:00
|
|
|
.hcd_priv_size = sizeof(struct dummy_hcd),
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2011-06-29 21:41:51 +08:00
|
|
|
.reset = dummy_setup,
|
2005-04-17 06:20:36 +08:00
|
|
|
.start = dummy_start,
|
|
|
|
.stop = dummy_stop,
|
|
|
|
|
2012-01-12 19:53:15 +08:00
|
|
|
.urb_enqueue = dummy_urb_enqueue,
|
|
|
|
.urb_dequeue = dummy_urb_dequeue,
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2012-01-12 19:53:15 +08:00
|
|
|
.get_frame_number = dummy_h_get_frame,
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2012-01-12 19:53:15 +08:00
|
|
|
.hub_status_data = dummy_hub_status,
|
|
|
|
.hub_control = dummy_hub_control,
|
2005-10-14 05:08:02 +08:00
|
|
|
.bus_suspend = dummy_bus_suspend,
|
|
|
|
.bus_resume = dummy_bus_resume,
|
2011-06-29 21:41:52 +08:00
|
|
|
|
|
|
|
.alloc_streams = dummy_alloc_streams,
|
|
|
|
.free_streams = dummy_free_streams,
|
2005-04-17 06:20:36 +08:00
|
|
|
};
|
|
|
|
|
2005-11-15 01:16:30 +08:00
|
|
|
static int dummy_hcd_probe(struct platform_device *pdev)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2012-10-30 01:09:56 +08:00
|
|
|
struct dummy *dum;
|
2011-06-29 21:41:51 +08:00
|
|
|
struct usb_hcd *hs_hcd;
|
2011-06-29 21:41:52 +08:00
|
|
|
struct usb_hcd *ss_hcd;
|
2005-04-17 06:20:36 +08:00
|
|
|
int retval;
|
|
|
|
|
2005-11-15 01:16:30 +08:00
|
|
|
dev_info(&pdev->dev, "%s, driver " DRIVER_VERSION "\n", driver_desc);
|
2012-10-30 01:09:56 +08:00
|
|
|
dum = *((void **)dev_get_platdata(&pdev->dev));
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2017-09-27 03:15:22 +08:00
|
|
|
if (mod_data.is_super_speed)
|
|
|
|
dummy_hcd.flags = HCD_USB3 | HCD_SHARED;
|
|
|
|
else if (mod_data.is_high_speed)
|
2011-06-29 21:41:52 +08:00
|
|
|
dummy_hcd.flags = HCD_USB2;
|
2017-09-27 03:15:22 +08:00
|
|
|
else
|
|
|
|
dummy_hcd.flags = HCD_USB11;
|
2011-06-29 21:41:51 +08:00
|
|
|
hs_hcd = usb_create_hcd(&dummy_hcd, &pdev->dev, dev_name(&pdev->dev));
|
|
|
|
if (!hs_hcd)
|
2005-04-17 06:20:36 +08:00
|
|
|
return -ENOMEM;
|
2011-06-29 21:41:51 +08:00
|
|
|
hs_hcd->has_tt = 1;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2011-06-29 21:41:51 +08:00
|
|
|
retval = usb_add_hcd(hs_hcd, 0, 0);
|
2012-08-20 03:54:58 +08:00
|
|
|
if (retval)
|
|
|
|
goto put_usb2_hcd;
|
2011-06-29 21:41:52 +08:00
|
|
|
|
|
|
|
if (mod_data.is_super_speed) {
|
|
|
|
ss_hcd = usb_create_shared_hcd(&dummy_hcd, &pdev->dev,
|
|
|
|
dev_name(&pdev->dev), hs_hcd);
|
|
|
|
if (!ss_hcd) {
|
|
|
|
retval = -ENOMEM;
|
|
|
|
goto dealloc_usb2_hcd;
|
|
|
|
}
|
|
|
|
|
|
|
|
retval = usb_add_hcd(ss_hcd, 0, 0);
|
|
|
|
if (retval)
|
|
|
|
goto put_usb3_hcd;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
put_usb3_hcd:
|
|
|
|
usb_put_hcd(ss_hcd);
|
|
|
|
dealloc_usb2_hcd:
|
2012-08-20 03:54:58 +08:00
|
|
|
usb_remove_hcd(hs_hcd);
|
|
|
|
put_usb2_hcd:
|
2011-06-29 21:41:52 +08:00
|
|
|
usb_put_hcd(hs_hcd);
|
2012-10-30 01:09:56 +08:00
|
|
|
dum->hs_hcd = dum->ss_hcd = NULL;
|
2005-04-17 06:20:36 +08:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2011-06-29 21:41:51 +08:00
|
|
|
static int dummy_hcd_remove(struct platform_device *pdev)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2011-06-29 21:41:51 +08:00
|
|
|
struct dummy *dum;
|
|
|
|
|
2012-01-12 19:53:15 +08:00
|
|
|
dum = hcd_to_dummy_hcd(platform_get_drvdata(pdev))->dum;
|
2011-06-29 21:41:52 +08:00
|
|
|
|
|
|
|
if (dum->ss_hcd) {
|
|
|
|
usb_remove_hcd(dummy_hcd_to_hcd(dum->ss_hcd));
|
|
|
|
usb_put_hcd(dummy_hcd_to_hcd(dum->ss_hcd));
|
|
|
|
}
|
|
|
|
|
2011-06-29 21:41:51 +08:00
|
|
|
usb_remove_hcd(dummy_hcd_to_hcd(dum->hs_hcd));
|
|
|
|
usb_put_hcd(dummy_hcd_to_hcd(dum->hs_hcd));
|
2011-06-29 21:41:52 +08:00
|
|
|
|
2012-10-30 01:09:56 +08:00
|
|
|
dum->hs_hcd = NULL;
|
|
|
|
dum->ss_hcd = NULL;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2005-05-04 04:15:43 +08:00
|
|
|
return 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2012-01-12 19:53:15 +08:00
|
|
|
static int dummy_hcd_suspend(struct platform_device *pdev, pm_message_t state)
|
2005-05-11 03:34:16 +08:00
|
|
|
{
|
|
|
|
struct usb_hcd *hcd;
|
2011-06-29 21:41:51 +08:00
|
|
|
struct dummy_hcd *dum_hcd;
|
2005-11-30 01:08:15 +08:00
|
|
|
int rc = 0;
|
2005-05-11 03:34:16 +08:00
|
|
|
|
2012-01-12 19:53:15 +08:00
|
|
|
dev_dbg(&pdev->dev, "%s\n", __func__);
|
2005-05-11 03:34:16 +08:00
|
|
|
|
2012-01-12 19:53:15 +08:00
|
|
|
hcd = platform_get_drvdata(pdev);
|
2011-06-29 21:41:51 +08:00
|
|
|
dum_hcd = hcd_to_dummy_hcd(hcd);
|
|
|
|
if (dum_hcd->rh_state == DUMMY_RH_RUNNING) {
|
2005-11-30 01:08:15 +08:00
|
|
|
dev_warn(&pdev->dev, "Root hub isn't suspended!\n");
|
|
|
|
rc = -EBUSY;
|
|
|
|
} else
|
|
|
|
clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
|
|
|
|
return rc;
|
2005-05-11 03:34:16 +08:00
|
|
|
}
|
|
|
|
|
2012-01-12 19:53:15 +08:00
|
|
|
static int dummy_hcd_resume(struct platform_device *pdev)
|
2005-05-11 03:34:16 +08:00
|
|
|
{
|
|
|
|
struct usb_hcd *hcd;
|
|
|
|
|
2012-01-12 19:53:15 +08:00
|
|
|
dev_dbg(&pdev->dev, "%s\n", __func__);
|
2005-05-11 03:34:16 +08:00
|
|
|
|
2012-01-12 19:53:15 +08:00
|
|
|
hcd = platform_get_drvdata(pdev);
|
2005-11-30 01:08:15 +08:00
|
|
|
set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
|
2012-01-12 19:53:15 +08:00
|
|
|
usb_hcd_poll_rh_status(hcd);
|
2005-05-11 03:34:16 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-11-10 06:32:44 +08:00
|
|
|
static struct platform_driver dummy_hcd_driver = {
|
2005-05-04 04:15:43 +08:00
|
|
|
.probe = dummy_hcd_probe,
|
|
|
|
.remove = dummy_hcd_remove,
|
2005-05-11 03:34:16 +08:00
|
|
|
.suspend = dummy_hcd_suspend,
|
|
|
|
.resume = dummy_hcd_resume,
|
2005-11-10 06:32:44 +08:00
|
|
|
.driver = {
|
|
|
|
.name = (char *) driver_name,
|
|
|
|
},
|
2005-05-04 04:15:43 +08:00
|
|
|
};
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2005-05-04 04:15:43 +08:00
|
|
|
/*-------------------------------------------------------------------------*/
|
2024-06-11 20:08:33 +08:00
|
|
|
#define MAX_NUM_UDC 32
|
2012-10-30 19:53:17 +08:00
|
|
|
static struct platform_device *the_udc_pdev[MAX_NUM_UDC];
|
|
|
|
static struct platform_device *the_hcd_pdev[MAX_NUM_UDC];
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2012-01-12 19:53:15 +08:00
|
|
|
static int __init init(void)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2008-04-08 03:03:25 +08:00
|
|
|
int retval = -ENOMEM;
|
2012-10-30 01:09:55 +08:00
|
|
|
int i;
|
2024-06-11 20:26:44 +08:00
|
|
|
struct dummy *dum[MAX_NUM_UDC] = {};
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2012-01-12 19:53:15 +08:00
|
|
|
if (usb_disabled())
|
2005-04-17 06:20:36 +08:00
|
|
|
return -ENODEV;
|
2005-05-04 04:15:43 +08:00
|
|
|
|
2011-06-29 21:41:53 +08:00
|
|
|
if (!mod_data.is_high_speed && mod_data.is_super_speed)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2012-10-30 01:09:55 +08:00
|
|
|
if (mod_data.num < 1 || mod_data.num > MAX_NUM_UDC) {
|
2015-02-06 07:42:00 +08:00
|
|
|
pr_err("Number of emulated UDC must be in range of 1...%d\n",
|
2012-10-30 01:09:55 +08:00
|
|
|
MAX_NUM_UDC);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2012-10-30 01:09:56 +08:00
|
|
|
|
2012-10-30 01:09:55 +08:00
|
|
|
for (i = 0; i < mod_data.num; i++) {
|
|
|
|
the_hcd_pdev[i] = platform_device_alloc(driver_name, i);
|
|
|
|
if (!the_hcd_pdev[i]) {
|
|
|
|
i--;
|
|
|
|
while (i >= 0)
|
|
|
|
platform_device_put(the_hcd_pdev[i--]);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (i = 0; i < mod_data.num; i++) {
|
|
|
|
the_udc_pdev[i] = platform_device_alloc(gadget_name, i);
|
|
|
|
if (!the_udc_pdev[i]) {
|
|
|
|
i--;
|
|
|
|
while (i >= 0)
|
|
|
|
platform_device_put(the_udc_pdev[i--]);
|
|
|
|
goto err_alloc_udc;
|
|
|
|
}
|
|
|
|
}
|
2012-10-30 01:09:56 +08:00
|
|
|
for (i = 0; i < mod_data.num; i++) {
|
|
|
|
dum[i] = kzalloc(sizeof(struct dummy), GFP_KERNEL);
|
2013-05-07 19:50:06 +08:00
|
|
|
if (!dum[i]) {
|
|
|
|
retval = -ENOMEM;
|
2012-10-30 01:09:56 +08:00
|
|
|
goto err_add_pdata;
|
2013-05-07 19:50:06 +08:00
|
|
|
}
|
2012-10-30 01:09:56 +08:00
|
|
|
retval = platform_device_add_data(the_hcd_pdev[i], &dum[i],
|
|
|
|
sizeof(void *));
|
|
|
|
if (retval)
|
|
|
|
goto err_add_pdata;
|
|
|
|
retval = platform_device_add_data(the_udc_pdev[i], &dum[i],
|
|
|
|
sizeof(void *));
|
|
|
|
if (retval)
|
|
|
|
goto err_add_pdata;
|
|
|
|
}
|
2005-05-04 04:15:43 +08:00
|
|
|
|
2008-04-08 03:03:25 +08:00
|
|
|
retval = platform_driver_register(&dummy_hcd_driver);
|
|
|
|
if (retval < 0)
|
2012-10-30 01:09:56 +08:00
|
|
|
goto err_add_pdata;
|
2008-04-08 03:03:25 +08:00
|
|
|
retval = platform_driver_register(&dummy_udc_driver);
|
2005-05-04 04:15:43 +08:00
|
|
|
if (retval < 0)
|
|
|
|
goto err_register_udc_driver;
|
|
|
|
|
2012-10-30 01:09:55 +08:00
|
|
|
for (i = 0; i < mod_data.num; i++) {
|
|
|
|
retval = platform_device_add(the_hcd_pdev[i]);
|
|
|
|
if (retval < 0) {
|
|
|
|
i--;
|
|
|
|
while (i >= 0)
|
|
|
|
platform_device_del(the_hcd_pdev[i--]);
|
|
|
|
goto err_add_hcd;
|
|
|
|
}
|
|
|
|
}
|
2012-10-30 01:09:56 +08:00
|
|
|
for (i = 0; i < mod_data.num; i++) {
|
|
|
|
if (!dum[i]->hs_hcd ||
|
|
|
|
(!dum[i]->ss_hcd && mod_data.is_super_speed)) {
|
|
|
|
/*
|
|
|
|
* The hcd was added successfully but its probe
|
|
|
|
* function failed for some reason.
|
|
|
|
*/
|
|
|
|
retval = -EINVAL;
|
|
|
|
goto err_add_udc;
|
|
|
|
}
|
2011-04-16 02:37:06 +08:00
|
|
|
}
|
2012-10-30 01:09:55 +08:00
|
|
|
|
|
|
|
for (i = 0; i < mod_data.num; i++) {
|
|
|
|
retval = platform_device_add(the_udc_pdev[i]);
|
|
|
|
if (retval < 0) {
|
|
|
|
i--;
|
|
|
|
while (i >= 0)
|
2017-08-15 15:38:45 +08:00
|
|
|
platform_device_del(the_udc_pdev[i--]);
|
2012-10-30 01:09:55 +08:00
|
|
|
goto err_add_udc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < mod_data.num; i++) {
|
|
|
|
if (!platform_get_drvdata(the_udc_pdev[i])) {
|
|
|
|
/*
|
|
|
|
* The udc was added successfully but its probe
|
|
|
|
* function failed for some reason.
|
|
|
|
*/
|
|
|
|
retval = -EINVAL;
|
|
|
|
goto err_probe_udc;
|
|
|
|
}
|
2011-04-16 02:37:06 +08:00
|
|
|
}
|
2005-05-04 04:15:43 +08:00
|
|
|
return retval;
|
|
|
|
|
2011-04-16 02:37:06 +08:00
|
|
|
err_probe_udc:
|
2012-10-30 01:09:55 +08:00
|
|
|
for (i = 0; i < mod_data.num; i++)
|
|
|
|
platform_device_del(the_udc_pdev[i]);
|
2008-04-08 03:03:25 +08:00
|
|
|
err_add_udc:
|
2012-10-30 01:09:55 +08:00
|
|
|
for (i = 0; i < mod_data.num; i++)
|
|
|
|
platform_device_del(the_hcd_pdev[i]);
|
2008-04-08 03:03:25 +08:00
|
|
|
err_add_hcd:
|
|
|
|
platform_driver_unregister(&dummy_udc_driver);
|
2005-05-04 04:15:43 +08:00
|
|
|
err_register_udc_driver:
|
2008-04-08 03:03:25 +08:00
|
|
|
platform_driver_unregister(&dummy_hcd_driver);
|
2012-10-30 01:09:56 +08:00
|
|
|
err_add_pdata:
|
|
|
|
for (i = 0; i < mod_data.num; i++)
|
|
|
|
kfree(dum[i]);
|
2012-10-30 01:09:55 +08:00
|
|
|
for (i = 0; i < mod_data.num; i++)
|
|
|
|
platform_device_put(the_udc_pdev[i]);
|
2008-04-08 03:03:25 +08:00
|
|
|
err_alloc_udc:
|
2012-10-30 01:09:55 +08:00
|
|
|
for (i = 0; i < mod_data.num; i++)
|
|
|
|
platform_device_put(the_hcd_pdev[i]);
|
2005-04-17 06:20:36 +08:00
|
|
|
return retval;
|
|
|
|
}
|
2012-01-12 19:53:15 +08:00
|
|
|
module_init(init);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2012-01-12 19:53:15 +08:00
|
|
|
static void __exit cleanup(void)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2012-10-30 01:09:55 +08:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < mod_data.num; i++) {
|
2012-10-30 01:09:56 +08:00
|
|
|
struct dummy *dum;
|
|
|
|
|
|
|
|
dum = *((void **)dev_get_platdata(&the_udc_pdev[i]->dev));
|
|
|
|
|
2012-10-30 01:09:55 +08:00
|
|
|
platform_device_unregister(the_udc_pdev[i]);
|
|
|
|
platform_device_unregister(the_hcd_pdev[i]);
|
2012-10-30 01:09:56 +08:00
|
|
|
kfree(dum);
|
2012-10-30 01:09:55 +08:00
|
|
|
}
|
2008-04-08 03:03:25 +08:00
|
|
|
platform_driver_unregister(&dummy_udc_driver);
|
|
|
|
platform_driver_unregister(&dummy_hcd_driver);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2012-01-12 19:53:15 +08:00
|
|
|
module_exit(cleanup);
|