// SPDX-License-Identifier: GPL-2.0
/*
* usb port device code
*
* Copyright (C) 2012 Intel Corp
*
* Author: Lan Tianyu <tianyu.lan@intel.com>
*/
#include <linux/kstrtox.h>
#include <linux/slab.h>
#include <linux/string_choices.h>
#include <linux/sysfs.h>
#include <linux/pm_qos.h>
#include <linux/component.h>
#include <linux/usb/of.h>
#include "hub.h"
static int usb_port_block_power_off;
static const struct attribute_group *port_dev_group[];
static ssize_t early_stop_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct usb_port *port_dev = to_usb_port(dev);
return sysfs_emit(buf, "%s\n", str_yes_no(port_dev->early_stop));
}
static ssize_t early_stop_store(struct device *dev, struct device_attribute *attr,
const char *buf, size_t count)
{
struct usb_port *port_dev = to_usb_port(dev);
bool value;
if (kstrtobool(buf, &value))
return -EINVAL;
if (value)
port_dev->early_stop = 1;
else
port_dev->early_stop = 0;
return count;
}
static DEVICE_ATTR_RW(early_stop);
static ssize_t disable_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct usb_port *port_dev = to_usb_port(dev);
struct usb_device *hdev = to_usb_device(dev->parent->parent);
struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
struct usb_interface *intf = to_usb_interface(dev->parent);
int port1 = port_dev->portnum;
u16 portstatus, unused;
bool disabled;
int rc;
struct kernfs_node *kn;
if (!hub)
return -ENODEV;
hub_get(hub);
rc = usb_autopm_get_interface(intf);
if (rc < 0)
goto out_hub_get;
/*
* Prevent deadlock if another process is concurrently
* trying to unregister hdev.
*/
kn = sysfs_break_active_protection(&dev->kobj, &attr->attr);
if (!kn) {
rc = -ENODEV;
goto out_autopm;
}
usb_lock_device(hdev);
if (hub->disconnected) {
rc = -ENODEV;
goto out_hdev_lock;
}
usb_hub_port_status(hub, port1, &portstatus, &unused);
disabled = !usb_port_is_power_on(hub, portstatus);
out_hdev_lock:
usb_unlock_device(hdev);
sysfs_unbreak_active_protection(kn);
out_autopm:
usb_autopm_put_interface(intf);
out_hub_get:
hub_put(hub);
if (rc)
return rc;
return sysfs_emit(buf, "%s\n", disabled ? "1" : "0");
}
static ssize_t disable_store(struct device *dev, struct device_attribute *attr,
const char *buf, size_t count)
{
struct usb_port *port_dev = to_usb_port(dev);
struct usb_device *hdev = to_usb_device(dev->parent->parent);
struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
struct usb_interface *intf = to_usb_interface(dev->parent);
int port1 = port_dev->portnum;
bool disabled;
int rc;
struct kernfs_node *kn;
if (!hub)
return -ENODEV;
rc = kstrtobool(buf, &disabled);
if (rc)
return rc;
hub_get(hub);
rc = usb_autopm_get_interface(intf);
if (rc < 0)
goto out_hub_get;
/*
* Prevent deadlock if another process is concurrently
* trying to unregister hdev.
*/
kn = sysfs_break_active_protection(&dev->kobj, &attr->attr);
if (!kn) {
rc = -ENODEV;
goto out_autopm;
}
usb_lock_device(hdev);
if (hub->disconnected) {
rc = -ENODEV;
goto out_hdev_lock;
}
if (disabled && port_dev->child)
usb_disconnect(&port_dev->child);
rc = usb_hub_set_port_power(hdev, hub, port1, !disabled);
if (disabled) {
usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_CONNECTION);
if (!port_dev->is_superspeed)
usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_ENABLE);
}
if (!rc)
rc = count;
out_hdev_lock:
usb_unlock_device(hdev);
sysfs_unbreak_active_protection(kn);
out_autopm:
usb_autopm_put_interface(intf);
out_hub_get:
hub_put(hub);
return rc;
}
static DEVICE_ATTR_RW(disable);
static ssize_t location_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct usb_port *port_dev = to_usb_port(dev);
return sysfs_emit(buf, "0x%08x\n