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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | /* Descriptor fields */ |
| 162 | #define usb_descriptor_attr_le16(field, format_string) \ |
| 163 | static ssize_t \ |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 164 | show_##field(struct device *dev, struct device_attribute *attr, \ |
Alan Stern | b724ae7 | 2005-10-24 15:36:00 -0400 | [diff] [blame] | 165 | char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | { \ |
| 167 | struct usb_device *udev; \ |
| 168 | \ |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 169 | udev = to_usb_device(dev); \ |
| 170 | return sprintf(buf, format_string, \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | le16_to_cpu(udev->descriptor.field)); \ |
| 172 | } \ |
| 173 | static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL); |
| 174 | |
| 175 | usb_descriptor_attr_le16(idVendor, "%04x\n") |
| 176 | usb_descriptor_attr_le16(idProduct, "%04x\n") |
| 177 | usb_descriptor_attr_le16(bcdDevice, "%04x\n") |
| 178 | |
| 179 | #define usb_descriptor_attr(field, format_string) \ |
| 180 | static ssize_t \ |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 181 | show_##field(struct device *dev, struct device_attribute *attr, \ |
Alan Stern | b724ae7 | 2005-10-24 15:36:00 -0400 | [diff] [blame] | 182 | char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | { \ |
| 184 | struct usb_device *udev; \ |
| 185 | \ |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 186 | udev = to_usb_device(dev); \ |
| 187 | return sprintf(buf, format_string, udev->descriptor.field); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | } \ |
| 189 | static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL); |
| 190 | |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 191 | usb_descriptor_attr(bDeviceClass, "%02x\n") |
| 192 | usb_descriptor_attr(bDeviceSubClass, "%02x\n") |
| 193 | usb_descriptor_attr(bDeviceProtocol, "%02x\n") |
| 194 | usb_descriptor_attr(bNumConfigurations, "%d\n") |
| 195 | usb_descriptor_attr(bMaxPacketSize0, "%d\n") |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | |
| 197 | static struct attribute *dev_attrs[] = { |
| 198 | /* current configuration's attributes */ |
Alan Stern | b6eb2d8 | 2006-07-06 15:37:42 -0400 | [diff] [blame] | 199 | &dev_attr_configuration.attr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | &dev_attr_bNumInterfaces.attr, |
| 201 | &dev_attr_bConfigurationValue.attr, |
| 202 | &dev_attr_bmAttributes.attr, |
| 203 | &dev_attr_bMaxPower.attr, |
| 204 | /* device attributes */ |
| 205 | &dev_attr_idVendor.attr, |
| 206 | &dev_attr_idProduct.attr, |
| 207 | &dev_attr_bcdDevice.attr, |
| 208 | &dev_attr_bDeviceClass.attr, |
| 209 | &dev_attr_bDeviceSubClass.attr, |
| 210 | &dev_attr_bDeviceProtocol.attr, |
| 211 | &dev_attr_bNumConfigurations.attr, |
Greg Kroah-Hartman | cf5910b | 2005-06-29 16:53:29 -0700 | [diff] [blame] | 212 | &dev_attr_bMaxPacketSize0.attr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | &dev_attr_speed.attr, |
| 214 | &dev_attr_devnum.attr, |
| 215 | &dev_attr_version.attr, |
| 216 | &dev_attr_maxchild.attr, |
Oliver Neukum | 7ceec1f | 2007-01-26 14:26:21 +0100 | [diff] [blame^] | 217 | &dev_attr_quirks.attr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | NULL, |
| 219 | }; |
| 220 | static struct attribute_group dev_attr_grp = { |
| 221 | .attrs = dev_attrs, |
| 222 | }; |
| 223 | |
Greg Kroah-Hartman | 1b21d5e | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 224 | int usb_create_sysfs_dev_files(struct usb_device *udev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | { |
| 226 | struct device *dev = &udev->dev; |
Greg Kroah-Hartman | 1b21d5e | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 227 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | |
Greg Kroah-Hartman | 1b21d5e | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 229 | retval = sysfs_create_group(&dev->kobj, &dev_attr_grp); |
| 230 | if (retval) |
| 231 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | |
Greg Kroah-Hartman | 1b21d5e | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 233 | if (udev->manufacturer) { |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 234 | retval = device_create_file(dev, &dev_attr_manufacturer); |
Greg Kroah-Hartman | 1b21d5e | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 235 | if (retval) |
| 236 | goto error; |
| 237 | } |
| 238 | if (udev->product) { |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 239 | retval = device_create_file(dev, &dev_attr_product); |
Greg Kroah-Hartman | 1b21d5e | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 240 | if (retval) |
| 241 | goto error; |
| 242 | } |
| 243 | if (udev->serial) { |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 244 | retval = device_create_file(dev, &dev_attr_serial); |
Greg Kroah-Hartman | 1b21d5e | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 245 | if (retval) |
| 246 | goto error; |
| 247 | } |
| 248 | retval = usb_create_ep_files(dev, &udev->ep0, udev); |
| 249 | if (retval) |
| 250 | goto error; |
| 251 | return 0; |
| 252 | error: |
| 253 | usb_remove_ep_files(&udev->ep0); |
| 254 | device_remove_file(dev, &dev_attr_manufacturer); |
| 255 | device_remove_file(dev, &dev_attr_product); |
| 256 | device_remove_file(dev, &dev_attr_serial); |
| 257 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | } |
| 259 | |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 260 | void usb_remove_sysfs_dev_files(struct usb_device *udev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | { |
| 262 | struct device *dev = &udev->dev; |
| 263 | |
Alan Stern | be69e5b | 2005-10-25 15:56:06 -0400 | [diff] [blame] | 264 | usb_remove_ep_files(&udev->ep0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | sysfs_remove_group(&dev->kobj, &dev_attr_grp); |
| 266 | |
Alan Stern | 4f62efe | 2005-10-24 16:24:14 -0400 | [diff] [blame] | 267 | if (udev->manufacturer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | device_remove_file(dev, &dev_attr_manufacturer); |
Alan Stern | 4f62efe | 2005-10-24 16:24:14 -0400 | [diff] [blame] | 269 | if (udev->product) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | device_remove_file(dev, &dev_attr_product); |
Alan Stern | 4f62efe | 2005-10-24 16:24:14 -0400 | [diff] [blame] | 271 | if (udev->serial) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | device_remove_file(dev, &dev_attr_serial); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | /* Interface fields */ |
| 276 | #define usb_intf_attr(field, format_string) \ |
| 277 | static ssize_t \ |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 278 | show_##field(struct device *dev, struct device_attribute *attr, \ |
Alan Stern | b724ae7 | 2005-10-24 15:36:00 -0400 | [diff] [blame] | 279 | char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | { \ |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 281 | struct usb_interface *intf = to_usb_interface(dev); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | \ |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 283 | return sprintf(buf, format_string, \ |
Alan Stern | b724ae7 | 2005-10-24 15:36:00 -0400 | [diff] [blame] | 284 | intf->cur_altsetting->desc.field); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | } \ |
| 286 | static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL); |
| 287 | |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 288 | usb_intf_attr(bInterfaceNumber, "%02x\n") |
| 289 | usb_intf_attr(bAlternateSetting, "%2d\n") |
| 290 | usb_intf_attr(bNumEndpoints, "%02x\n") |
| 291 | usb_intf_attr(bInterfaceClass, "%02x\n") |
| 292 | usb_intf_attr(bInterfaceSubClass, "%02x\n") |
| 293 | usb_intf_attr(bInterfaceProtocol, "%02x\n") |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | |
Alan Stern | b724ae7 | 2005-10-24 15:36:00 -0400 | [diff] [blame] | 295 | static ssize_t show_interface_string(struct device *dev, |
| 296 | struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | { |
| 298 | struct usb_interface *intf; |
| 299 | struct usb_device *udev; |
| 300 | int len; |
| 301 | |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 302 | intf = to_usb_interface(dev); |
| 303 | udev = interface_to_usbdev(intf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | len = snprintf(buf, 256, "%s", intf->cur_altsetting->string); |
| 305 | if (len < 0) |
| 306 | return 0; |
| 307 | buf[len] = '\n'; |
| 308 | buf[len+1] = 0; |
| 309 | return len+1; |
| 310 | } |
| 311 | static DEVICE_ATTR(interface, S_IRUGO, show_interface_string, NULL); |
| 312 | |
Alan Stern | b724ae7 | 2005-10-24 15:36:00 -0400 | [diff] [blame] | 313 | static ssize_t show_modalias(struct device *dev, |
| 314 | struct device_attribute *attr, char *buf) |
Greg KH | 360b52b | 2005-05-10 06:45:10 -0700 | [diff] [blame] | 315 | { |
| 316 | struct usb_interface *intf; |
| 317 | struct usb_device *udev; |
Greg Kroah-Hartman | 7521803 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 318 | struct usb_host_interface *alt; |
Greg KH | 360b52b | 2005-05-10 06:45:10 -0700 | [diff] [blame] | 319 | |
| 320 | intf = to_usb_interface(dev); |
| 321 | udev = interface_to_usbdev(intf); |
Greg Kroah-Hartman | 7521803 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 322 | alt = intf->cur_altsetting; |
Greg KH | 360b52b | 2005-05-10 06:45:10 -0700 | [diff] [blame] | 323 | |
Greg Kroah-Hartman | 7521803 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 324 | return sprintf(buf, "usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02X" |
| 325 | "ic%02Xisc%02Xip%02X\n", |
| 326 | le16_to_cpu(udev->descriptor.idVendor), |
| 327 | le16_to_cpu(udev->descriptor.idProduct), |
| 328 | le16_to_cpu(udev->descriptor.bcdDevice), |
| 329 | udev->descriptor.bDeviceClass, |
| 330 | udev->descriptor.bDeviceSubClass, |
| 331 | udev->descriptor.bDeviceProtocol, |
| 332 | alt->desc.bInterfaceClass, |
| 333 | alt->desc.bInterfaceSubClass, |
| 334 | alt->desc.bInterfaceProtocol); |
Greg KH | 360b52b | 2005-05-10 06:45:10 -0700 | [diff] [blame] | 335 | } |
| 336 | static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL); |
| 337 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | static struct attribute *intf_attrs[] = { |
| 339 | &dev_attr_bInterfaceNumber.attr, |
| 340 | &dev_attr_bAlternateSetting.attr, |
| 341 | &dev_attr_bNumEndpoints.attr, |
| 342 | &dev_attr_bInterfaceClass.attr, |
| 343 | &dev_attr_bInterfaceSubClass.attr, |
| 344 | &dev_attr_bInterfaceProtocol.attr, |
Greg KH | 360b52b | 2005-05-10 06:45:10 -0700 | [diff] [blame] | 345 | &dev_attr_modalias.attr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | NULL, |
| 347 | }; |
| 348 | static struct attribute_group intf_attr_grp = { |
| 349 | .attrs = intf_attrs, |
| 350 | }; |
| 351 | |
Alan Stern | 4f62efe | 2005-10-24 16:24:14 -0400 | [diff] [blame] | 352 | static inline void usb_create_intf_ep_files(struct usb_interface *intf, |
| 353 | struct usb_device *udev) |
Greg Kroah-Hartman | 094f1649 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 354 | { |
| 355 | struct usb_host_interface *iface_desc; |
| 356 | int i; |
| 357 | |
| 358 | iface_desc = intf->cur_altsetting; |
| 359 | for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) |
Greg Kroah-Hartman | 36679ea | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 360 | usb_create_ep_files(&intf->dev, &iface_desc->endpoint[i], |
Alan Stern | 4f62efe | 2005-10-24 16:24:14 -0400 | [diff] [blame] | 361 | udev); |
Greg Kroah-Hartman | 094f1649 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 362 | } |
| 363 | |
Alan Stern | be69e5b | 2005-10-25 15:56:06 -0400 | [diff] [blame] | 364 | 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] | 365 | { |
| 366 | struct usb_host_interface *iface_desc; |
| 367 | int i; |
| 368 | |
| 369 | iface_desc = intf->cur_altsetting; |
| 370 | for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) |
Alan Stern | be69e5b | 2005-10-25 15:56:06 -0400 | [diff] [blame] | 371 | usb_remove_ep_files(&iface_desc->endpoint[i]); |
Greg Kroah-Hartman | 094f1649 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 372 | } |
| 373 | |
Greg Kroah-Hartman | 1b21d5e | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 374 | int usb_create_sysfs_intf_files(struct usb_interface *intf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | { |
Alan Stern | 4f62efe | 2005-10-24 16:24:14 -0400 | [diff] [blame] | 376 | struct usb_device *udev = interface_to_usbdev(intf); |
| 377 | struct usb_host_interface *alt = intf->cur_altsetting; |
Greg Kroah-Hartman | 1b21d5e | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 378 | int retval; |
Alan Stern | 4f62efe | 2005-10-24 16:24:14 -0400 | [diff] [blame] | 379 | |
Greg Kroah-Hartman | 1b21d5e | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 380 | retval = sysfs_create_group(&intf->dev.kobj, &intf_attr_grp); |
| 381 | if (retval) |
| 382 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | |
Alan Stern | 4f62efe | 2005-10-24 16:24:14 -0400 | [diff] [blame] | 384 | if (alt->string == NULL) |
| 385 | alt->string = usb_cache_string(udev, alt->desc.iInterface); |
| 386 | if (alt->string) |
Greg Kroah-Hartman | 1b21d5e | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 387 | retval = device_create_file(&intf->dev, &dev_attr_interface); |
Alan Stern | 4f62efe | 2005-10-24 16:24:14 -0400 | [diff] [blame] | 388 | usb_create_intf_ep_files(intf, udev); |
Greg Kroah-Hartman | 1b21d5e | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 389 | return 0; |
| 390 | error: |
| 391 | if (alt->string) |
| 392 | device_remove_file(&intf->dev, &dev_attr_interface); |
| 393 | sysfs_remove_group(&intf->dev.kobj, &intf_attr_grp); |
| 394 | usb_remove_intf_ep_files(intf); |
| 395 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | } |
| 397 | |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 398 | void usb_remove_sysfs_intf_files(struct usb_interface *intf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | { |
Greg Kroah-Hartman | 094f1649 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 400 | usb_remove_intf_ep_files(intf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | sysfs_remove_group(&intf->dev.kobj, &intf_attr_grp); |
| 402 | |
| 403 | if (intf->cur_altsetting->string) |
| 404 | device_remove_file(&intf->dev, &dev_attr_interface); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | } |