947 lines · 23.6 KB · C
RawDownload
/* * drivers/usb/core/sysfs.c * * (C) Copyright 2002 David Brownell * (C) Copyright 2002,2004 Greg Kroah-Hartman * (C) Copyright 2002,2004 IBM Corp. * * All of the sysfs file attributes for usb devices and interfaces. * */  #include <linux/kernel.h>#include <linux/string.h>#include <linux/usb.h>#include <linux/usb/quirks.h>#include "usb.h" /* Active configuration fields */#define usb_actconfig_show(field, multiplier, format_string)		\static ssize_t  show_##field(struct device *dev,			\		struct device_attribute *attr, char *buf)		\{									\	struct usb_device *udev;					\	struct usb_host_config *actconfig;				\									\	udev = to_usb_device(dev);					\	actconfig = udev->actconfig;					\	if (actconfig)							\		return sprintf(buf, format_string,			\				actconfig->desc.field * multiplier);	\	else								\		return 0;						\}									\ #define usb_actconfig_attr(field, multiplier, format_string)		\usb_actconfig_show(field, multiplier, format_string)			\static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL); usb_actconfig_attr(bNumInterfaces, 1, "%2d\n")usb_actconfig_attr(bmAttributes, 1, "%2x\n")usb_actconfig_attr(bMaxPower, 2, "%3dmA\n") static ssize_t show_configuration_string(struct device *dev,		struct device_attribute *attr, char *buf){	struct usb_device *udev;	struct usb_host_config *actconfig; 	udev = to_usb_device(dev);	actconfig = udev->actconfig;	if ((!actconfig) || (!actconfig->string))		return 0;	return sprintf(buf, "%s\n", actconfig->string);}static DEVICE_ATTR(configuration, S_IRUGO, show_configuration_string, NULL); /* configuration value is always present, and r/w */usb_actconfig_show(bConfigurationValue, 1, "%u\n"); static ssize_tset_bConfigurationValue(struct device *dev, struct device_attribute *attr,		const char *buf, size_t count){	struct usb_device	*udev = to_usb_device(dev);	int			config, value; 	if (sscanf(buf, "%d", &config) != 1 || config < -1 || config > 255)		return -EINVAL;	usb_lock_device(udev);	value = usb_set_configuration(udev, config);	usb_unlock_device(udev);	return (value < 0) ? value : count;} static DEVICE_ATTR_IGNORE_LOCKDEP(bConfigurationValue, S_IRUGO | S_IWUSR,		show_bConfigurationValue, set_bConfigurationValue); /* String fields */#define usb_string_attr(name)						\static ssize_t  show_##name(struct device *dev,				\		struct device_attribute *attr, char *buf)		\{									\	struct usb_device *udev;					\	int retval;							\									\	udev = to_usb_device(dev);					\	usb_lock_device(udev);						\	retval = sprintf(buf, "%s\n", udev->name);			\	usb_unlock_device(udev);					\	return retval;							\}									\static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL); usb_string_attr(product);usb_string_attr(manufacturer);usb_string_attr(serial); static ssize_tshow_speed(struct device *dev, struct device_attribute *attr, char *buf){	struct usb_device *udev;	char *speed; 	udev = to_usb_device(dev); 	switch (udev->speed) {	case USB_SPEED_LOW:		speed = "1.5";		break;	case USB_SPEED_UNKNOWN:	case USB_SPEED_FULL:		speed = "12";		break;	case USB_SPEED_HIGH:		speed = "480";		break;	case USB_SPEED_WIRELESS:		speed = "480";		break;	case USB_SPEED_SUPER:		speed = "5000";		break;	default:		speed = "unknown";	}	return sprintf(buf, "%s\n", speed);}static DEVICE_ATTR(speed, S_IRUGO, show_speed, NULL); static ssize_tshow_busnum(struct device *dev, struct device_attribute *attr, char *buf){	struct usb_device *udev; 	udev = to_usb_device(dev);	return sprintf(buf, "%d\n", udev->bus->busnum);}static DEVICE_ATTR(busnum, S_IRUGO, show_busnum, NULL); static ssize_tshow_devnum(struct device *dev, struct device_attribute *attr, char *buf){	struct usb_device *udev; 	udev = to_usb_device(dev);	return sprintf(buf, "%d\n", udev->devnum);}static DEVICE_ATTR(devnum, S_IRUGO, show_devnum, NULL); static ssize_tshow_devpath(struct device *dev, struct device_attribute *attr, char *buf){	struct usb_device *udev; 	udev = to_usb_device(dev);	return sprintf(buf, "%s\n", udev->devpath);}static DEVICE_ATTR(devpath, S_IRUGO, show_devpath, NULL); static ssize_tshow_version(struct device *dev, struct device_attribute *attr, char *buf){	struct usb_device *udev;	u16 bcdUSB; 	udev = to_usb_device(dev);	bcdUSB = le16_to_cpu(udev->descriptor.bcdUSB);	return sprintf(buf, "%2x.%02x\n", bcdUSB >> 8, bcdUSB & 0xff);}static DEVICE_ATTR(version, S_IRUGO, show_version, NULL); static ssize_tshow_maxchild(struct device *dev, struct device_attribute *attr, char *buf){	struct usb_device *udev; 	udev = to_usb_device(dev);	return sprintf(buf, "%d\n", udev->maxchild);}static DEVICE_ATTR(maxchild, S_IRUGO, show_maxchild, NULL); static ssize_tshow_quirks(struct device *dev, struct device_attribute *attr, char *buf){	struct usb_device *udev; 	udev = to_usb_device(dev);	return sprintf(buf, "0x%x\n", udev->quirks);}static DEVICE_ATTR(quirks, S_IRUGO, show_quirks, NULL); static ssize_tshow_avoid_reset_quirk(struct device *dev, struct device_attribute *attr, char *buf){	struct usb_device *udev; 	udev = to_usb_device(dev);	return sprintf(buf, "%d\n", !!(udev->quirks & USB_QUIRK_RESET_MORPHS));} static ssize_tset_avoid_reset_quirk(struct device *dev, struct device_attribute *attr,		const char *buf, size_t count){	struct usb_device	*udev = to_usb_device(dev);	int			config; 	if (sscanf(buf, "%d", &config) != 1 || config < 0 || config > 1)		return -EINVAL;	usb_lock_device(udev);	if (config)		udev->quirks |= USB_QUIRK_RESET_MORPHS;	else		udev->quirks &= ~USB_QUIRK_RESET_MORPHS;	usb_unlock_device(udev);	return count;} static DEVICE_ATTR(avoid_reset_quirk, S_IRUGO | S_IWUSR,		show_avoid_reset_quirk, set_avoid_reset_quirk); static ssize_tshow_urbnum(struct device *dev, struct device_attribute *attr, char *buf){	struct usb_device *udev; 	udev = to_usb_device(dev);	return sprintf(buf, "%d\n", atomic_read(&udev->urbnum));}static DEVICE_ATTR(urbnum, S_IRUGO, show_urbnum, NULL); static ssize_tshow_removable(struct device *dev, struct device_attribute *attr, char *buf){	struct usb_device *udev;	char *state; 	udev = to_usb_device(dev); 	switch (udev->removable) {	case USB_DEVICE_REMOVABLE:		state = "removable";		break;	case USB_DEVICE_FIXED:		state = "fixed";		break;	default:		state = "unknown";	} 	return sprintf(buf, "%s\n", state);}static DEVICE_ATTR(removable, S_IRUGO, show_removable, NULL); #ifdef	CONFIG_PM static ssize_tshow_persist(struct device *dev, struct device_attribute *attr, char *buf){	struct usb_device *udev = to_usb_device(dev); 	return sprintf(buf, "%d\n", udev->persist_enabled);} static ssize_tset_persist(struct device *dev, struct device_attribute *attr,		const char *buf, size_t count){	struct usb_device *udev = to_usb_device(dev);	int value; 	/* Hubs are always enabled for USB_PERSIST */	if (udev->descriptor.bDeviceClass == USB_CLASS_HUB)		return -EPERM; 	if (sscanf(buf, "%d", &value) != 1)		return -EINVAL; 	usb_lock_device(udev);	udev->persist_enabled = !!value;	usb_unlock_device(udev);	return count;} static DEVICE_ATTR(persist, S_IRUGO | S_IWUSR, show_persist, set_persist); static int add_persist_attributes(struct device *dev){	int rc = 0; 	if (is_usb_device(dev)) {		struct usb_device *udev = to_usb_device(dev); 		/* Hubs are automatically enabled for USB_PERSIST,		 * no point in creating the attribute file.		 */		if (udev->descriptor.bDeviceClass != USB_CLASS_HUB)			rc = sysfs_add_file_to_group(&dev->kobj,					&dev_attr_persist.attr,					power_group_name);	}	return rc;} static void remove_persist_attributes(struct device *dev){	sysfs_remove_file_from_group(&dev->kobj,			&dev_attr_persist.attr,			power_group_name);}#else #define add_persist_attributes(dev)	0#define remove_persist_attributes(dev)	do {} while (0) #endif	/* CONFIG_PM */ #ifdef	CONFIG_USB_SUSPEND static ssize_tshow_connected_duration(struct device *dev, struct device_attribute *attr,		char *buf){	struct usb_device *udev = to_usb_device(dev); 	return sprintf(buf, "%u\n",			jiffies_to_msecs(jiffies - udev->connect_time));} static DEVICE_ATTR(connected_duration, S_IRUGO, show_connected_duration, NULL); /* * If the device is resumed, the last time the device was suspended has * been pre-subtracted from active_duration.  We add the current time to * get the duration that the device was actually active. * * If the device is suspended, the active_duration is up-to-date. */static ssize_tshow_active_duration(struct device *dev, struct device_attribute *attr,		char *buf){	struct usb_device *udev = to_usb_device(dev);	int duration; 	if (udev->state != USB_STATE_SUSPENDED)		duration = jiffies_to_msecs(jiffies + udev->active_duration);	else		duration = jiffies_to_msecs(udev->active_duration);	return sprintf(buf, "%u\n", duration);} static DEVICE_ATTR(active_duration, S_IRUGO, show_active_duration, NULL); static ssize_tshow_autosuspend(struct device *dev, struct device_attribute *attr, char *buf){	return sprintf(buf, "%d\n", dev->power.autosuspend_delay / 1000);} static ssize_tset_autosuspend(struct device *dev, struct device_attribute *attr,		const char *buf, size_t count){	int value; 	if (sscanf(buf, "%d", &value) != 1 || value >= INT_MAX/1000 ||			value <= -INT_MAX/1000)		return -EINVAL; 	pm_runtime_set_autosuspend_delay(dev, value * 1000);	return count;} static DEVICE_ATTR(autosuspend, S_IRUGO | S_IWUSR,		show_autosuspend, set_autosuspend); static const char on_string[] = "on";static const char auto_string[] = "auto"; static void warn_level(void) {	static int level_warned; 	if (!level_warned) {		level_warned = 1;		printk(KERN_WARNING "WARNING! power/level is deprecated; "				"use power/control instead\n");	}} static ssize_tshow_level(struct device *dev, struct device_attribute *attr, char *buf){	struct usb_device *udev = to_usb_device(dev);	const char *p = auto_string; 	warn_level();	if (udev->state != USB_STATE_SUSPENDED && !udev->dev.power.runtime_auto)		p = on_string;	return sprintf(buf, "%s\n", p);} static ssize_tset_level(struct device *dev, struct device_attribute *attr,		const char *buf, size_t count){	struct usb_device *udev = to_usb_device(dev);	int len = count;	char *cp;	int rc = count; 	warn_level();	cp = memchr(buf, '\n', count);	if (cp)		len = cp - buf; 	usb_lock_device(udev); 	if (len == sizeof on_string - 1 &&			strncmp(buf, on_string, len) == 0)		usb_disable_autosuspend(udev); 	else if (len == sizeof auto_string - 1 &&			strncmp(buf, auto_string, len) == 0)		usb_enable_autosuspend(udev); 	else		rc = -EINVAL; 	usb_unlock_device(udev);	return rc;} static DEVICE_ATTR(level, S_IRUGO | S_IWUSR, show_level, set_level); static ssize_tshow_usb2_hardware_lpm(struct device *dev, struct device_attribute *attr,				char *buf){	struct usb_device *udev = to_usb_device(dev);	const char *p; 	if (udev->usb2_hw_lpm_enabled == 1)		p = "enabled";	else		p = "disabled"; 	return sprintf(buf, "%s\n", p);} static ssize_tset_usb2_hardware_lpm(struct device *dev, struct device_attribute *attr,		const char *buf, size_t count){	struct usb_device *udev = to_usb_device(dev);	bool value;	int ret; 	usb_lock_device(udev); 	ret = strtobool(buf, &value); 	if (!ret)		ret = usb_set_usb2_hardware_lpm(udev, value); 	usb_unlock_device(udev); 	if (!ret)		return count; 	return ret;} static DEVICE_ATTR(usb2_hardware_lpm, S_IRUGO | S_IWUSR, show_usb2_hardware_lpm,			set_usb2_hardware_lpm); static struct attribute *usb2_hardware_lpm_attr[] = {	&dev_attr_usb2_hardware_lpm.attr,	NULL,};static struct attribute_group usb2_hardware_lpm_attr_group = {	.name	= power_group_name,	.attrs	= usb2_hardware_lpm_attr,}; static struct attribute *power_attrs[] = {	&dev_attr_autosuspend.attr,	&dev_attr_level.attr,	&dev_attr_connected_duration.attr,	&dev_attr_active_duration.attr,	NULL,};static struct attribute_group power_attr_group = {	.name	= power_group_name,	.attrs	= power_attrs,}; static int add_power_attributes(struct device *dev){

Lines 1–500 of 947.