Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * drivers/usb/core/sysfs.c |
| 3 | * |
| 4 | * (C) Copyright 2002 David Brownell |
| 5 | * (C) Copyright 2002,2004 Greg Kroah-Hartman |
| 6 | * (C) Copyright 2002,2004 IBM Corp. |
| 7 | * |
| 8 | * All of the sysfs file attributes for usb devices and interfaces. |
| 9 | * |
| 10 | */ |
| 11 | |
| 12 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/usb.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include "usb.h" |
| 16 | |
| 17 | /* Active configuration fields */ |
| 18 | #define usb_actconfig_show(field, multiplier, format_string) \ |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 19 | static ssize_t show_##field(struct device *dev, \ |
Alan Stern | b724ae7 | 2005-10-24 15:36:00 -0400 | [diff] [blame] | 20 | struct device_attribute *attr, char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | { \ |
| 22 | struct usb_device *udev; \ |
| 23 | struct usb_host_config *actconfig; \ |
| 24 | \ |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 25 | udev = to_usb_device(dev); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | actconfig = udev->actconfig; \ |
| 27 | if (actconfig) \ |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 28 | return sprintf(buf, format_string, \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | actconfig->desc.field * multiplier); \ |
| 30 | else \ |
| 31 | return 0; \ |
| 32 | } \ |
| 33 | |
| 34 | #define usb_actconfig_attr(field, multiplier, format_string) \ |
| 35 | usb_actconfig_show(field, multiplier, format_string) \ |
| 36 | static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL); |
| 37 | |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 38 | usb_actconfig_attr(bNumInterfaces, 1, "%2d\n") |
| 39 | usb_actconfig_attr(bmAttributes, 1, "%2x\n") |
| 40 | usb_actconfig_attr(bMaxPower, 2, "%3dmA\n") |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | |
Alan Stern | b724ae7 | 2005-10-24 15:36:00 -0400 | [diff] [blame] | 42 | static ssize_t show_configuration_string(struct device *dev, |
| 43 | struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | { |
| 45 | struct usb_device *udev; |
| 46 | struct usb_host_config *actconfig; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 48 | udev = to_usb_device(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | actconfig = udev->actconfig; |
| 50 | if ((!actconfig) || (!actconfig->string)) |
| 51 | return 0; |
Alan Stern | 4f62efe | 2005-10-24 16:24:14 -0400 | [diff] [blame] | 52 | return sprintf(buf, "%s\n", actconfig->string); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | } |
| 54 | static DEVICE_ATTR(configuration, S_IRUGO, show_configuration_string, NULL); |
| 55 | |
| 56 | /* configuration value is always present, and r/w */ |
| 57 | usb_actconfig_show(bConfigurationValue, 1, "%u\n"); |
| 58 | |
| 59 | static ssize_t |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 60 | set_bConfigurationValue(struct device *dev, struct device_attribute *attr, |
Alan Stern | b724ae7 | 2005-10-24 15:36:00 -0400 | [diff] [blame] | 61 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | { |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 63 | struct usb_device *udev = to_usb_device(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | int config, value; |
| 65 | |
Alan Stern | 3f141e2 | 2007-02-08 16:40:43 -0500 | [diff] [blame] | 66 | if (sscanf(buf, "%d", &config) != 1 || config < -1 || config > 255) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | return -EINVAL; |
| 68 | usb_lock_device(udev); |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 69 | value = usb_set_configuration(udev, config); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | usb_unlock_device(udev); |
| 71 | return (value < 0) ? value : count; |
| 72 | } |
| 73 | |
| 74 | static DEVICE_ATTR(bConfigurationValue, S_IRUGO | S_IWUSR, |
| 75 | show_bConfigurationValue, set_bConfigurationValue); |
| 76 | |
| 77 | /* String fields */ |
| 78 | #define usb_string_attr(name) \ |
Alan Stern | b724ae7 | 2005-10-24 15:36:00 -0400 | [diff] [blame] | 79 | static ssize_t show_##name(struct device *dev, \ |
| 80 | struct device_attribute *attr, char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | { \ |
| 82 | struct usb_device *udev; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | \ |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 84 | udev = to_usb_device(dev); \ |
Alan Stern | 4f62efe | 2005-10-24 16:24:14 -0400 | [diff] [blame] | 85 | return sprintf(buf, "%s\n", udev->name); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | } \ |
| 87 | static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL); |
| 88 | |
| 89 | usb_string_attr(product); |
| 90 | usb_string_attr(manufacturer); |
| 91 | usb_string_attr(serial); |
| 92 | |
| 93 | static ssize_t |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 94 | show_speed(struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | { |
| 96 | struct usb_device *udev; |
| 97 | char *speed; |
| 98 | |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 99 | udev = to_usb_device(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | |
| 101 | switch (udev->speed) { |
| 102 | case USB_SPEED_LOW: |
| 103 | speed = "1.5"; |
| 104 | break; |
| 105 | case USB_SPEED_UNKNOWN: |
| 106 | case USB_SPEED_FULL: |
| 107 | speed = "12"; |
| 108 | break; |
| 109 | case USB_SPEED_HIGH: |
| 110 | speed = "480"; |
| 111 | break; |
| 112 | default: |
| 113 | speed = "unknown"; |
| 114 | } |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 115 | return sprintf(buf, "%s\n", speed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | } |
| 117 | static DEVICE_ATTR(speed, S_IRUGO, show_speed, NULL); |
| 118 | |
| 119 | static ssize_t |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 120 | show_devnum(struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | { |
| 122 | struct usb_device *udev; |
| 123 | |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 124 | udev = to_usb_device(dev); |
| 125 | return sprintf(buf, "%d\n", udev->devnum); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | } |
| 127 | static DEVICE_ATTR(devnum, S_IRUGO, show_devnum, NULL); |
| 128 | |
| 129 | static ssize_t |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 130 | show_version(struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | { |
| 132 | struct usb_device *udev; |
| 133 | u16 bcdUSB; |
| 134 | |
| 135 | udev = to_usb_device(dev); |
| 136 | bcdUSB = le16_to_cpu(udev->descriptor.bcdUSB); |
| 137 | return sprintf(buf, "%2x.%02x\n", bcdUSB >> 8, bcdUSB & 0xff); |
| 138 | } |
| 139 | static DEVICE_ATTR(version, S_IRUGO, show_version, NULL); |
| 140 | |
| 141 | static ssize_t |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 142 | show_maxchild(struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | { |
| 144 | struct usb_device *udev; |
| 145 | |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 146 | udev = to_usb_device(dev); |
| 147 | return sprintf(buf, "%d\n", udev->maxchild); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | } |
| 149 | static DEVICE_ATTR(maxchild, S_IRUGO, show_maxchild, NULL); |
| 150 | |
Oliver Neukum | 7ceec1f | 2007-01-26 14:26:21 +0100 | [diff] [blame] | 151 | static ssize_t |
| 152 | show_quirks(struct device *dev, struct device_attribute *attr, char *buf) |
| 153 | { |
| 154 | struct usb_device *udev; |
| 155 | |
| 156 | udev = to_usb_device(dev); |
| 157 | return sprintf(buf, "0x%x\n", udev->quirks); |
| 158 | } |
| 159 | static DEVICE_ATTR(quirks, S_IRUGO, show_quirks, NULL); |
| 160 | |
Alan Stern | 19c2623 | 2007-02-20 15:03:32 -0500 | [diff] [blame] | 161 | #ifdef CONFIG_USB_SUSPEND |
| 162 | |
| 163 | static ssize_t |
| 164 | show_autosuspend(struct device *dev, struct device_attribute *attr, char *buf) |
| 165 | { |
| 166 | struct usb_device *udev = to_usb_device(dev); |
| 167 | |
Alan Stern | eaafbc3 | 2007-03-13 16:39:15 -0400 | [diff] [blame^] | 168 | return sprintf(buf, "%d\n", udev->autosuspend_delay / HZ); |
Alan Stern | 19c2623 | 2007-02-20 15:03:32 -0500 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | static ssize_t |
| 172 | set_autosuspend(struct device *dev, struct device_attribute *attr, |
| 173 | const char *buf, size_t count) |
| 174 | { |
| 175 | struct usb_device *udev = to_usb_device(dev); |
Alan Stern | eaafbc3 | 2007-03-13 16:39:15 -0400 | [diff] [blame^] | 176 | int value; |
Alan Stern | 19c2623 | 2007-02-20 15:03:32 -0500 | [diff] [blame] | 177 | |
Alan Stern | eaafbc3 | 2007-03-13 16:39:15 -0400 | [diff] [blame^] | 178 | if (sscanf(buf, "%d", &value) != 1 || value >= INT_MAX/HZ || |
| 179 | value <= - INT_MAX/HZ) |
Alan Stern | 19c2623 | 2007-02-20 15:03:32 -0500 | [diff] [blame] | 180 | return -EINVAL; |
| 181 | value *= HZ; |
| 182 | |
Alan Stern | 19c2623 | 2007-02-20 15:03:32 -0500 | [diff] [blame] | 183 | udev->autosuspend_delay = value; |
Alan Stern | eaafbc3 | 2007-03-13 16:39:15 -0400 | [diff] [blame^] | 184 | if (value >= 0) |
Alan Stern | 19c2623 | 2007-02-20 15:03:32 -0500 | [diff] [blame] | 185 | usb_try_autosuspend_device(udev); |
Alan Stern | eaafbc3 | 2007-03-13 16:39:15 -0400 | [diff] [blame^] | 186 | else { |
| 187 | usb_lock_device(udev); |
| 188 | usb_external_resume_device(udev); |
| 189 | usb_unlock_device(udev); |
| 190 | } |
Alan Stern | 19c2623 | 2007-02-20 15:03:32 -0500 | [diff] [blame] | 191 | return count; |
| 192 | } |
| 193 | |
| 194 | static DEVICE_ATTR(autosuspend, S_IRUGO | S_IWUSR, |
| 195 | show_autosuspend, set_autosuspend); |
| 196 | |
| 197 | static char power_group[] = "power"; |
| 198 | |
| 199 | static int add_power_attributes(struct device *dev) |
| 200 | { |
| 201 | int rc = 0; |
| 202 | |
| 203 | if (is_usb_device(dev)) |
| 204 | rc = sysfs_add_file_to_group(&dev->kobj, |
| 205 | &dev_attr_autosuspend.attr, |
| 206 | power_group); |
| 207 | return rc; |
| 208 | } |
| 209 | |
| 210 | static void remove_power_attributes(struct device *dev) |
| 211 | { |
| 212 | sysfs_remove_file_from_group(&dev->kobj, |
| 213 | &dev_attr_autosuspend.attr, |
| 214 | power_group); |
| 215 | } |
| 216 | |
| 217 | #else |
| 218 | |
| 219 | #define add_power_attributes(dev) 0 |
| 220 | #define remove_power_attributes(dev) do {} while (0) |
| 221 | |
| 222 | #endif /* CONFIG_USB_SUSPEND */ |
| 223 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | /* Descriptor fields */ |
| 225 | #define usb_descriptor_attr_le16(field, format_string) \ |
| 226 | static ssize_t \ |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 227 | show_##field(struct device *dev, struct device_attribute *attr, \ |
Alan Stern | b724ae7 | 2005-10-24 15:36:00 -0400 | [diff] [blame] | 228 | char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | { \ |
| 230 | struct usb_device *udev; \ |
| 231 | \ |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 232 | udev = to_usb_device(dev); \ |
| 233 | return sprintf(buf, format_string, \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | le16_to_cpu(udev->descriptor.field)); \ |
| 235 | } \ |
| 236 | static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL); |
| 237 | |
| 238 | usb_descriptor_attr_le16(idVendor, "%04x\n") |
| 239 | usb_descriptor_attr_le16(idProduct, "%04x\n") |
| 240 | usb_descriptor_attr_le16(bcdDevice, "%04x\n") |
| 241 | |
| 242 | #define usb_descriptor_attr(field, format_string) \ |
| 243 | static ssize_t \ |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 244 | show_##field(struct device *dev, struct device_attribute *attr, \ |
Alan Stern | b724ae7 | 2005-10-24 15:36:00 -0400 | [diff] [blame] | 245 | char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | { \ |
| 247 | struct usb_device *udev; \ |
| 248 | \ |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 249 | udev = to_usb_device(dev); \ |
| 250 | return sprintf(buf, format_string, udev->descriptor.field); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | } \ |
| 252 | static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL); |
| 253 | |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 254 | usb_descriptor_attr(bDeviceClass, "%02x\n") |
| 255 | usb_descriptor_attr(bDeviceSubClass, "%02x\n") |
| 256 | usb_descriptor_attr(bDeviceProtocol, "%02x\n") |
| 257 | usb_descriptor_attr(bNumConfigurations, "%d\n") |
| 258 | usb_descriptor_attr(bMaxPacketSize0, "%d\n") |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | |
| 260 | static struct attribute *dev_attrs[] = { |
| 261 | /* current configuration's attributes */ |
Alan Stern | b6eb2d8 | 2006-07-06 15:37:42 -0400 | [diff] [blame] | 262 | &dev_attr_configuration.attr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | &dev_attr_bNumInterfaces.attr, |
| 264 | &dev_attr_bConfigurationValue.attr, |
| 265 | &dev_attr_bmAttributes.attr, |
| 266 | &dev_attr_bMaxPower.attr, |
| 267 | /* device attributes */ |
| 268 | &dev_attr_idVendor.attr, |
| 269 | &dev_attr_idProduct.attr, |
| 270 | &dev_attr_bcdDevice.attr, |
| 271 | &dev_attr_bDeviceClass.attr, |
| 272 | &dev_attr_bDeviceSubClass.attr, |
| 273 | &dev_attr_bDeviceProtocol.attr, |
| 274 | &dev_attr_bNumConfigurations.attr, |
Greg Kroah-Hartman | cf5910b | 2005-06-29 16:53:29 -0700 | [diff] [blame] | 275 | &dev_attr_bMaxPacketSize0.attr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | &dev_attr_speed.attr, |
| 277 | &dev_attr_devnum.attr, |
| 278 | &dev_attr_version.attr, |
| 279 | &dev_attr_maxchild.attr, |
Oliver Neukum | 7ceec1f | 2007-01-26 14:26:21 +0100 | [diff] [blame] | 280 | &dev_attr_quirks.attr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | NULL, |
| 282 | }; |
| 283 | static struct attribute_group dev_attr_grp = { |
| 284 | .attrs = dev_attrs, |
| 285 | }; |
| 286 | |
Greg Kroah-Hartman | 1b21d5e | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 287 | int usb_create_sysfs_dev_files(struct usb_device *udev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | { |
| 289 | struct device *dev = &udev->dev; |
Greg Kroah-Hartman | 1b21d5e | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 290 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | |
Greg Kroah-Hartman | 1b21d5e | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 292 | retval = sysfs_create_group(&dev->kobj, &dev_attr_grp); |
| 293 | if (retval) |
| 294 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | |
Alan Stern | 19c2623 | 2007-02-20 15:03:32 -0500 | [diff] [blame] | 296 | retval = add_power_attributes(dev); |
| 297 | if (retval) |
| 298 | goto error; |
| 299 | |
Greg Kroah-Hartman | 1b21d5e | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 300 | if (udev->manufacturer) { |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 301 | retval = device_create_file(dev, &dev_attr_manufacturer); |
Greg Kroah-Hartman | 1b21d5e | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 302 | if (retval) |
| 303 | goto error; |
| 304 | } |
| 305 | if (udev->product) { |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 306 | retval = device_create_file(dev, &dev_attr_product); |
Greg Kroah-Hartman | 1b21d5e | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 307 | if (retval) |
| 308 | goto error; |
| 309 | } |
| 310 | if (udev->serial) { |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 311 | retval = device_create_file(dev, &dev_attr_serial); |
Greg Kroah-Hartman | 1b21d5e | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 312 | if (retval) |
| 313 | goto error; |
| 314 | } |
| 315 | retval = usb_create_ep_files(dev, &udev->ep0, udev); |
| 316 | if (retval) |
| 317 | goto error; |
| 318 | return 0; |
| 319 | error: |
Alan Stern | aa084f3 | 2007-02-20 14:59:59 -0500 | [diff] [blame] | 320 | usb_remove_sysfs_dev_files(udev); |
Greg Kroah-Hartman | 1b21d5e | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 321 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | } |
| 323 | |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 324 | void usb_remove_sysfs_dev_files(struct usb_device *udev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | { |
| 326 | struct device *dev = &udev->dev; |
| 327 | |
Alan Stern | be69e5b | 2005-10-25 15:56:06 -0400 | [diff] [blame] | 328 | usb_remove_ep_files(&udev->ep0); |
Alan Stern | aa084f3 | 2007-02-20 14:59:59 -0500 | [diff] [blame] | 329 | device_remove_file(dev, &dev_attr_manufacturer); |
| 330 | device_remove_file(dev, &dev_attr_product); |
| 331 | device_remove_file(dev, &dev_attr_serial); |
Alan Stern | 19c2623 | 2007-02-20 15:03:32 -0500 | [diff] [blame] | 332 | remove_power_attributes(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | sysfs_remove_group(&dev->kobj, &dev_attr_grp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | /* Interface fields */ |
| 337 | #define usb_intf_attr(field, format_string) \ |
| 338 | static ssize_t \ |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 339 | show_##field(struct device *dev, struct device_attribute *attr, \ |
Alan Stern | b724ae7 | 2005-10-24 15:36:00 -0400 | [diff] [blame] | 340 | char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | { \ |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 342 | struct usb_interface *intf = to_usb_interface(dev); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | \ |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 344 | return sprintf(buf, format_string, \ |
Alan Stern | b724ae7 | 2005-10-24 15:36:00 -0400 | [diff] [blame] | 345 | intf->cur_altsetting->desc.field); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | } \ |
| 347 | static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL); |
| 348 | |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 349 | usb_intf_attr(bInterfaceNumber, "%02x\n") |
| 350 | usb_intf_attr(bAlternateSetting, "%2d\n") |
| 351 | usb_intf_attr(bNumEndpoints, "%02x\n") |
| 352 | usb_intf_attr(bInterfaceClass, "%02x\n") |
| 353 | usb_intf_attr(bInterfaceSubClass, "%02x\n") |
| 354 | usb_intf_attr(bInterfaceProtocol, "%02x\n") |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | |
Alan Stern | b724ae7 | 2005-10-24 15:36:00 -0400 | [diff] [blame] | 356 | static ssize_t show_interface_string(struct device *dev, |
| 357 | struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | { |
| 359 | struct usb_interface *intf; |
| 360 | struct usb_device *udev; |
| 361 | int len; |
| 362 | |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 363 | intf = to_usb_interface(dev); |
| 364 | udev = interface_to_usbdev(intf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | len = snprintf(buf, 256, "%s", intf->cur_altsetting->string); |
| 366 | if (len < 0) |
| 367 | return 0; |
| 368 | buf[len] = '\n'; |
| 369 | buf[len+1] = 0; |
| 370 | return len+1; |
| 371 | } |
| 372 | static DEVICE_ATTR(interface, S_IRUGO, show_interface_string, NULL); |
| 373 | |
Alan Stern | b724ae7 | 2005-10-24 15:36:00 -0400 | [diff] [blame] | 374 | static ssize_t show_modalias(struct device *dev, |
| 375 | struct device_attribute *attr, char *buf) |
Greg KH | 360b52b | 2005-05-10 06:45:10 -0700 | [diff] [blame] | 376 | { |
| 377 | struct usb_interface *intf; |
| 378 | struct usb_device *udev; |
Greg Kroah-Hartman | 7521803 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 379 | struct usb_host_interface *alt; |
Greg KH | 360b52b | 2005-05-10 06:45:10 -0700 | [diff] [blame] | 380 | |
| 381 | intf = to_usb_interface(dev); |
| 382 | udev = interface_to_usbdev(intf); |
Greg Kroah-Hartman | 7521803 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 383 | alt = intf->cur_altsetting; |
Greg KH | 360b52b | 2005-05-10 06:45:10 -0700 | [diff] [blame] | 384 | |
Greg Kroah-Hartman | 7521803 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 385 | return sprintf(buf, "usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02X" |
| 386 | "ic%02Xisc%02Xip%02X\n", |
| 387 | le16_to_cpu(udev->descriptor.idVendor), |
| 388 | le16_to_cpu(udev->descriptor.idProduct), |
| 389 | le16_to_cpu(udev->descriptor.bcdDevice), |
| 390 | udev->descriptor.bDeviceClass, |
| 391 | udev->descriptor.bDeviceSubClass, |
| 392 | udev->descriptor.bDeviceProtocol, |
| 393 | alt->desc.bInterfaceClass, |
| 394 | alt->desc.bInterfaceSubClass, |
| 395 | alt->desc.bInterfaceProtocol); |
Greg KH | 360b52b | 2005-05-10 06:45:10 -0700 | [diff] [blame] | 396 | } |
| 397 | static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL); |
| 398 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | static struct attribute *intf_attrs[] = { |
| 400 | &dev_attr_bInterfaceNumber.attr, |
| 401 | &dev_attr_bAlternateSetting.attr, |
| 402 | &dev_attr_bNumEndpoints.attr, |
| 403 | &dev_attr_bInterfaceClass.attr, |
| 404 | &dev_attr_bInterfaceSubClass.attr, |
| 405 | &dev_attr_bInterfaceProtocol.attr, |
Greg KH | 360b52b | 2005-05-10 06:45:10 -0700 | [diff] [blame] | 406 | &dev_attr_modalias.attr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | NULL, |
| 408 | }; |
| 409 | static struct attribute_group intf_attr_grp = { |
| 410 | .attrs = intf_attrs, |
| 411 | }; |
| 412 | |
Alan Stern | 4f62efe | 2005-10-24 16:24:14 -0400 | [diff] [blame] | 413 | static inline void usb_create_intf_ep_files(struct usb_interface *intf, |
| 414 | struct usb_device *udev) |
Greg Kroah-Hartman | 094f1649 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 415 | { |
| 416 | struct usb_host_interface *iface_desc; |
| 417 | int i; |
| 418 | |
| 419 | iface_desc = intf->cur_altsetting; |
| 420 | for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) |
Greg Kroah-Hartman | 36679ea | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 421 | usb_create_ep_files(&intf->dev, &iface_desc->endpoint[i], |
Alan Stern | 4f62efe | 2005-10-24 16:24:14 -0400 | [diff] [blame] | 422 | udev); |
Greg Kroah-Hartman | 094f1649 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 423 | } |
| 424 | |
Alan Stern | be69e5b | 2005-10-25 15:56:06 -0400 | [diff] [blame] | 425 | static inline void usb_remove_intf_ep_files(struct usb_interface *intf) |
Greg Kroah-Hartman | 094f1649 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 426 | { |
| 427 | struct usb_host_interface *iface_desc; |
| 428 | int i; |
| 429 | |
| 430 | iface_desc = intf->cur_altsetting; |
| 431 | for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) |
Alan Stern | be69e5b | 2005-10-25 15:56:06 -0400 | [diff] [blame] | 432 | usb_remove_ep_files(&iface_desc->endpoint[i]); |
Greg Kroah-Hartman | 094f1649 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 433 | } |
| 434 | |
Greg Kroah-Hartman | 1b21d5e | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 435 | int usb_create_sysfs_intf_files(struct usb_interface *intf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | { |
Alan Stern | aa084f3 | 2007-02-20 14:59:59 -0500 | [diff] [blame] | 437 | struct device *dev = &intf->dev; |
Alan Stern | 4f62efe | 2005-10-24 16:24:14 -0400 | [diff] [blame] | 438 | struct usb_device *udev = interface_to_usbdev(intf); |
| 439 | struct usb_host_interface *alt = intf->cur_altsetting; |
Greg Kroah-Hartman | 1b21d5e | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 440 | int retval; |
Alan Stern | 4f62efe | 2005-10-24 16:24:14 -0400 | [diff] [blame] | 441 | |
Alan Stern | aa084f3 | 2007-02-20 14:59:59 -0500 | [diff] [blame] | 442 | retval = sysfs_create_group(&dev->kobj, &intf_attr_grp); |
Greg Kroah-Hartman | 1b21d5e | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 443 | if (retval) |
Alan Stern | aa084f3 | 2007-02-20 14:59:59 -0500 | [diff] [blame] | 444 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | |
Alan Stern | 4f62efe | 2005-10-24 16:24:14 -0400 | [diff] [blame] | 446 | if (alt->string == NULL) |
| 447 | alt->string = usb_cache_string(udev, alt->desc.iInterface); |
| 448 | if (alt->string) |
Alan Stern | aa084f3 | 2007-02-20 14:59:59 -0500 | [diff] [blame] | 449 | retval = device_create_file(dev, &dev_attr_interface); |
Alan Stern | 4f62efe | 2005-10-24 16:24:14 -0400 | [diff] [blame] | 450 | usb_create_intf_ep_files(intf, udev); |
Greg Kroah-Hartman | 1b21d5e | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 451 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | } |
| 453 | |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 454 | void usb_remove_sysfs_intf_files(struct usb_interface *intf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | { |
Alan Stern | aa084f3 | 2007-02-20 14:59:59 -0500 | [diff] [blame] | 456 | struct device *dev = &intf->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | |
Alan Stern | aa084f3 | 2007-02-20 14:59:59 -0500 | [diff] [blame] | 458 | usb_remove_intf_ep_files(intf); |
| 459 | device_remove_file(dev, &dev_attr_interface); |
| 460 | sysfs_remove_group(&dev->kobj, &intf_attr_grp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | } |