linux_old1/drivers/usb/core/port.c

291 lines
7.1 KiB
C
Raw Normal View History

/*
* usb port device code
*
* Copyright (C) 2012 Intel Corp
*
* Author: Lan Tianyu <tianyu.lan@intel.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
*/
usb: fix compilation error and warning of driver/usb/core/port.c on arm and blackfin This patch is to fix compilation error and warning on the arm and blackfin. Add linux/slab.h head file to driver/usb/core/port.c. These are reported from 0-DAY kernel build testing backend. head: 6e30d7cba992d626c9d16b3873a7b90c700d0e95 commit: 6e30d7cba992d626c9d16b3873a7b90c700d0e95 [26/26] usb: Add driver/usb/core/(port.c,hub.h) files config: make ARCH=arm at91_dt_defconfig All error/warnings: drivers/usb/core/port.c: In function 'usb_port_device_release': >> drivers/usb/core/port.c:25:2: error: implicit declaration of function 'kfree' [-Werror=implicit-function-declaration] drivers/usb/core/port.c: In function 'usb_hub_create_port_device': >> drivers/usb/core/port.c:38:2: error: implicit declaration of function 'kzalloc' [-Werror=implicit-function-declaration] >> drivers/usb/core/port.c:38:40: error: 'GFP_KERNEL' undeclared (first use in this function) drivers/usb/core/port.c:38:40: note: each undeclared identifier is reported only once for each function it appears in cc1: some warnings being treated as errors head: 6e30d7cba992d626c9d16b3873a7b90c700d0e95 commit: 6e30d7cba992d626c9d16b3873a7b90c700d0e95 [26/26] usb: Add driver/usb/core/(port.c,hub.h) files config: make ARCH=blackfin BF526-EZBRD_defconfig All warnings: drivers/usb/core/port.c: In function 'usb_port_device_release': drivers/usb/core/port.c:25:2: error: implicit declaration of function 'kfree' [-Werror=implicit-function-declaration] drivers/usb/core/port.c: In function 'usb_hub_create_port_device': drivers/usb/core/port.c:38:2: error: implicit declaration of function 'kzalloc' [-Werror=implicit-function-declaration] >> drivers/usb/core/port.c:38:11: warning: assignment makes pointer from integer without a cast [enabled by default] cc1: some warnings being treated as errors Reported-by: Fengguang Wu <wfg@linux.intel.com> Signed-off-by: Lan Tianyu <tianyu.lan@intel.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2013-01-19 22:30:19 +08:00
#include <linux/slab.h>
#include <linux/pm_qos.h>
usb: fix compilation error and warning of driver/usb/core/port.c on arm and blackfin This patch is to fix compilation error and warning on the arm and blackfin. Add linux/slab.h head file to driver/usb/core/port.c. These are reported from 0-DAY kernel build testing backend. head: 6e30d7cba992d626c9d16b3873a7b90c700d0e95 commit: 6e30d7cba992d626c9d16b3873a7b90c700d0e95 [26/26] usb: Add driver/usb/core/(port.c,hub.h) files config: make ARCH=arm at91_dt_defconfig All error/warnings: drivers/usb/core/port.c: In function 'usb_port_device_release': >> drivers/usb/core/port.c:25:2: error: implicit declaration of function 'kfree' [-Werror=implicit-function-declaration] drivers/usb/core/port.c: In function 'usb_hub_create_port_device': >> drivers/usb/core/port.c:38:2: error: implicit declaration of function 'kzalloc' [-Werror=implicit-function-declaration] >> drivers/usb/core/port.c:38:40: error: 'GFP_KERNEL' undeclared (first use in this function) drivers/usb/core/port.c:38:40: note: each undeclared identifier is reported only once for each function it appears in cc1: some warnings being treated as errors head: 6e30d7cba992d626c9d16b3873a7b90c700d0e95 commit: 6e30d7cba992d626c9d16b3873a7b90c700d0e95 [26/26] usb: Add driver/usb/core/(port.c,hub.h) files config: make ARCH=blackfin BF526-EZBRD_defconfig All warnings: drivers/usb/core/port.c: In function 'usb_port_device_release': drivers/usb/core/port.c:25:2: error: implicit declaration of function 'kfree' [-Werror=implicit-function-declaration] drivers/usb/core/port.c: In function 'usb_hub_create_port_device': drivers/usb/core/port.c:38:2: error: implicit declaration of function 'kzalloc' [-Werror=implicit-function-declaration] >> drivers/usb/core/port.c:38:11: warning: assignment makes pointer from integer without a cast [enabled by default] cc1: some warnings being treated as errors Reported-by: Fengguang Wu <wfg@linux.intel.com> Signed-off-by: Lan Tianyu <tianyu.lan@intel.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2013-01-19 22:30:19 +08:00
#include "hub.h"
static const struct attribute_group *port_dev_group[];
static ssize_t connect_type_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct usb_port *port_dev = to_usb_port(dev);
char *result;
switch (port_dev->connect_type) {
case USB_PORT_CONNECT_TYPE_HOT_PLUG:
result = "hotplug";
break;
case USB_PORT_CONNECT_TYPE_HARD_WIRED:
result = "hardwired";
break;
case USB_PORT_NOT_USED:
result = "not used";
break;
default:
result = "unknown";
break;
}
return sprintf(buf, "%s\n", result);
}
static DEVICE_ATTR_RO(connect_type);
static struct attribute *port_dev_attrs[] = {
&dev_attr_connect_type.attr,
NULL,
};
static struct attribute_group port_dev_attr_grp = {
.attrs = port_dev_attrs,
};
static const struct attribute_group *port_dev_group[] = {
&port_dev_attr_grp,
NULL,
};
static void usb_port_device_release(struct device *dev)
{
struct usb_port *port_dev = to_usb_port(dev);
kfree(port_dev);
}
#ifdef CONFIG_PM_RUNTIME
static int usb_port_runtime_resume(struct device *dev)
{
struct usb_port *port_dev = to_usb_port(dev);
struct usb_device *hdev = to_usb_device(dev->parent->parent);
struct usb_interface *intf = to_usb_interface(dev->parent);
2013-01-23 04:26:30 +08:00
struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
int port1 = port_dev->portnum;
int retval;
2013-01-23 04:26:30 +08:00
if (!hub)
return -EINVAL;
if (hub->in_reset) {
port_dev->power_is_on = 1;
return 0;
}
2013-01-23 04:26:30 +08:00
usb_autopm_get_interface(intf);
2013-01-23 04:26:30 +08:00
set_bit(port1, hub->busy_bits);
retval = usb_hub_set_port_power(hdev, hub, port1, true);
2013-01-23 04:26:30 +08:00
if (port_dev->child && !retval) {
/*
usb: Don't fail port power resume on device disconnect. Userspace can tell the kernel to power off any USB port, including ones that are visible and connectible to users. When an attached USB device goes into suspend, the port will be powered off if the pm_qos_no_port_poweroff file for its port is set to 0, the device does not have remote wakeup enabled, and the device is marked as persistent. If the user disconnects the USB device while the port is powered off, the current code does not handle that properly. If you disconnect a device, and then run `lsusb -v -s` for the device, the device disconnect does not get handled by the USB core. The runtime resume of the port fails, because hub_port_debounce_be_connected() returns -ETIMEDOUT. This means the port resume fails and khubd doesn't handle the USB device disconnect. This leaves the device listed in lsusb, and the port's runtime_status will be permanently marked as "error". Fix this by ignoring the return value of hub_port_debounce_be_connected. Users can disconnect USB devices while the ports are powered off, and we must be able to handle that. This patch should be backported to kernels as old as 3.9, that contain the commit ad493e5e580546e6c3024b76a41535476da1546a "usb: add usb port auto power off mechanism" Signed-off-by: Sarah Sharp <sarah.a.sharp@linux.intel.com> Cc: Lan Tianyu <tianyu.lan@intel.com> Cc: Alan Stern <stern@rowland.harvard.edu> Cc: Rafael J. Wysocki <rafael.j.wysocki@intel.com> Cc: stable@vger.kernel.org Signed-off-by: Sarah Sharp <sarah.a.sharp@linux.intel.com>
2013-08-06 09:58:15 +08:00
* Attempt to wait for usb hub port to be reconnected in order
* to make the resume procedure successful. The device may have
* disconnected while the port was powered off, so ignore the
* return status.
2013-01-23 04:26:30 +08:00
*/
retval = hub_port_debounce_be_connected(hub, port1);
usb: Don't fail port power resume on device disconnect. Userspace can tell the kernel to power off any USB port, including ones that are visible and connectible to users. When an attached USB device goes into suspend, the port will be powered off if the pm_qos_no_port_poweroff file for its port is set to 0, the device does not have remote wakeup enabled, and the device is marked as persistent. If the user disconnects the USB device while the port is powered off, the current code does not handle that properly. If you disconnect a device, and then run `lsusb -v -s` for the device, the device disconnect does not get handled by the USB core. The runtime resume of the port fails, because hub_port_debounce_be_connected() returns -ETIMEDOUT. This means the port resume fails and khubd doesn't handle the USB device disconnect. This leaves the device listed in lsusb, and the port's runtime_status will be permanently marked as "error". Fix this by ignoring the return value of hub_port_debounce_be_connected. Users can disconnect USB devices while the ports are powered off, and we must be able to handle that. This patch should be backported to kernels as old as 3.9, that contain the commit ad493e5e580546e6c3024b76a41535476da1546a "usb: add usb port auto power off mechanism" Signed-off-by: Sarah Sharp <sarah.a.sharp@linux.intel.com> Cc: Lan Tianyu <tianyu.lan@intel.com> Cc: Alan Stern <stern@rowland.harvard.edu> Cc: Rafael J. Wysocki <rafael.j.wysocki@intel.com> Cc: stable@vger.kernel.org Signed-off-by: Sarah Sharp <sarah.a.sharp@linux.intel.com>
2013-08-06 09:58:15 +08:00
if (retval < 0)
2013-01-23 04:26:30 +08:00
dev_dbg(&port_dev->dev, "can't get reconnection after setting port power on, status %d\n",
retval);
usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_ENABLE);
retval = 0;
}
clear_bit(port1, hub->busy_bits);
usb_autopm_put_interface(intf);
return retval;
}
static int usb_port_runtime_suspend(struct device *dev)
{
struct usb_port *port_dev = to_usb_port(dev);
struct usb_device *hdev = to_usb_device(dev->parent->parent);
struct usb_interface *intf = to_usb_interface(dev->parent);
2013-01-23 04:26:30 +08:00
struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
int port1 = port_dev->portnum;
int retval;
2013-01-23 04:26:30 +08:00
if (!hub)
return -EINVAL;
if (hub->in_reset)
return -EBUSY;
2013-01-23 04:26:30 +08:00
if (dev_pm_qos_flags(&port_dev->dev, PM_QOS_FLAG_NO_POWER_OFF)
== PM_QOS_FLAGS_ALL)
return -EAGAIN;
usb_autopm_get_interface(intf);
2013-01-23 04:26:30 +08:00
set_bit(port1, hub->busy_bits);
retval = usb_hub_set_port_power(hdev, hub, port1, false);
2013-01-23 04:26:30 +08:00
usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_CONNECTION);
usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_ENABLE);
clear_bit(port1, hub->busy_bits);
usb_autopm_put_interface(intf);
return retval;
}
#endif
static const struct dev_pm_ops usb_port_pm_ops = {
#ifdef CONFIG_PM_RUNTIME
.runtime_suspend = usb_port_runtime_suspend,
.runtime_resume = usb_port_runtime_resume,
#endif
};
struct device_type usb_port_device_type = {
.name = "usb_port",
.release = usb_port_device_release,
.pm = &usb_port_pm_ops,
};
static struct device_driver usb_port_driver = {
.name = "usb",
.owner = THIS_MODULE,
};
static void link_peers(struct usb_port *left, struct usb_port *right)
{
if (left->peer == right && right->peer == left)
return;
if (left->peer || right->peer) {
struct usb_port *lpeer = left->peer;
struct usb_port *rpeer = right->peer;
WARN(1, "failed to peer %s and %s (%s -> %p) (%s -> %p)\n",
dev_name(&left->dev), dev_name(&right->dev),
dev_name(&left->dev), lpeer,
dev_name(&right->dev), rpeer);
return;
}
left->peer = right;
right->peer = left;
}
static void unlink_peers(struct usb_port *left, struct usb_port *right)
{
WARN(right->peer != left || left->peer != right,
"%s and %s are not peers?\n",
dev_name(&left->dev), dev_name(&right->dev));
right->peer = NULL;
left->peer = NULL;
}
/*
* Set the default peer port for root hubs, or via the upstream peer
* relationship for all other hubs
*/
static void find_and_link_peer(struct usb_hub *hub, int port1)
{
struct usb_port *port_dev = hub->ports[port1 - 1], *peer;
struct usb_device *hdev = hub->hdev;
struct usb_device *peer_hdev;
struct usb_hub *peer_hub;
if (!hdev->parent) {
struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
struct usb_hcd *peer_hcd = hcd->shared_hcd;
if (!peer_hcd)
return;
peer_hdev = peer_hcd->self.root_hub;
} else {
struct usb_port *upstream;
struct usb_device *parent = hdev->parent;
struct usb_hub *parent_hub = usb_hub_to_struct_hub(parent);
if (!parent_hub)
return;
upstream = parent_hub->ports[hdev->portnum - 1];
if (!upstream || !upstream->peer)
return;
peer_hdev = upstream->peer->child;
}
peer_hub = usb_hub_to_struct_hub(peer_hdev);
if (!peer_hub || port1 > peer_hdev->maxchild)
return;
peer = peer_hub->ports[port1 - 1];
if (peer)
link_peers(port_dev, peer);
}
int usb_hub_create_port_device(struct usb_hub *hub, int port1)
{
struct usb_port *port_dev;
int retval;
port_dev = kzalloc(sizeof(*port_dev), GFP_KERNEL);
if (!port_dev) {
retval = -ENOMEM;
goto exit;
}
hub->ports[port1 - 1] = port_dev;
port_dev->portnum = port1;
2013-01-23 04:26:30 +08:00
port_dev->power_is_on = true;
port_dev->dev.parent = hub->intfdev;
port_dev->dev.groups = port_dev_group;
port_dev->dev.type = &usb_port_device_type;
port_dev->dev.driver = &usb_port_driver;
dev_set_name(&port_dev->dev, "%s-port%d", dev_name(&hub->hdev->dev),
port1);
retval = device_register(&port_dev->dev);
if (retval)
goto error_register;
find_and_link_peer(hub, port1);
pm_runtime_set_active(&port_dev->dev);
/*
* Do not enable port runtime pm if the hub does not support
* power switching. Also, userspace must have final say of
* whether a port is permitted to power-off. Do not enable
* runtime pm if we fail to expose pm_qos_no_power_off.
*/
if (hub_is_port_power_switchable(hub)
&& dev_pm_qos_expose_flags(&port_dev->dev,
PM_QOS_FLAG_NO_POWER_OFF) == 0)
pm_runtime_enable(&port_dev->dev);
device_enable_async_suspend(&port_dev->dev);
return 0;
error_register:
put_device(&port_dev->dev);
exit:
return retval;
}
void usb_hub_remove_port_device(struct usb_hub *hub, int port1)
{
struct usb_port *port_dev = hub->ports[port1 - 1];
struct usb_port *peer;
peer = port_dev->peer;
if (peer)
unlink_peers(port_dev, peer);
device_unregister(&port_dev->dev);
}