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 | |
| 13 | #include <linux/config.h> |
| 14 | #include <linux/kernel.h> |
| 15 | |
| 16 | #ifdef CONFIG_USB_DEBUG |
| 17 | #define DEBUG |
| 18 | #else |
| 19 | #undef DEBUG |
| 20 | #endif |
| 21 | #include <linux/usb.h> |
| 22 | |
| 23 | #include "usb.h" |
| 24 | |
Greg Kroah-Hartman | 094f1649 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 25 | /* endpoint stuff */ |
Alan Stern | be69e5b | 2005-10-25 15:56:06 -0400 | [diff] [blame] | 26 | struct ep_object { |
| 27 | struct usb_endpoint_descriptor *desc; |
Greg Kroah-Hartman | 094f1649 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 28 | struct usb_device *udev; |
Alan Stern | be69e5b | 2005-10-25 15:56:06 -0400 | [diff] [blame] | 29 | struct kobject kobj; |
Greg Kroah-Hartman | 094f1649 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 30 | }; |
Alan Stern | be69e5b | 2005-10-25 15:56:06 -0400 | [diff] [blame] | 31 | #define to_ep_object(_kobj) \ |
| 32 | container_of(_kobj, struct ep_object, kobj) |
Greg Kroah-Hartman | 094f1649 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 33 | |
Alan Stern | be69e5b | 2005-10-25 15:56:06 -0400 | [diff] [blame] | 34 | struct ep_attribute { |
| 35 | struct attribute attr; |
| 36 | ssize_t (*show)(struct usb_device *, |
| 37 | struct usb_endpoint_descriptor *, char *); |
| 38 | }; |
| 39 | #define to_ep_attribute(_attr) \ |
| 40 | container_of(_attr, struct ep_attribute, attr) |
| 41 | |
| 42 | #define EP_ATTR(_name) \ |
| 43 | struct ep_attribute ep_##_name = { \ |
| 44 | .attr = {.name = #_name, .owner = THIS_MODULE, \ |
| 45 | .mode = S_IRUGO}, \ |
| 46 | .show = show_ep_##_name} |
| 47 | |
| 48 | #define usb_ep_attr(field, format_string) \ |
| 49 | static ssize_t show_ep_##field(struct usb_device *udev, \ |
| 50 | struct usb_endpoint_descriptor *desc, \ |
| 51 | char *buf) \ |
| 52 | { \ |
| 53 | return sprintf(buf, format_string, desc->field); \ |
| 54 | } \ |
| 55 | static EP_ATTR(field); |
| 56 | |
Greg Kroah-Hartman | 094f1649 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 57 | usb_ep_attr(bLength, "%02x\n") |
Greg Kroah-Hartman | 094f1649 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 58 | usb_ep_attr(bEndpointAddress, "%02x\n") |
| 59 | usb_ep_attr(bmAttributes, "%02x\n") |
| 60 | usb_ep_attr(bInterval, "%02x\n") |
| 61 | |
Alan Stern | be69e5b | 2005-10-25 15:56:06 -0400 | [diff] [blame] | 62 | static ssize_t show_ep_wMaxPacketSize(struct usb_device *udev, |
| 63 | struct usb_endpoint_descriptor *desc, char *buf) |
Greg Kroah-Hartman | 094f1649 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 64 | { |
Greg Kroah-Hartman | 094f1649 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 65 | return sprintf(buf, "%04x\n", |
Alan Stern | be69e5b | 2005-10-25 15:56:06 -0400 | [diff] [blame] | 66 | le16_to_cpu(desc->wMaxPacketSize) & 0x07ff); |
Greg Kroah-Hartman | 094f1649 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 67 | } |
Alan Stern | be69e5b | 2005-10-25 15:56:06 -0400 | [diff] [blame] | 68 | static EP_ATTR(wMaxPacketSize); |
Greg Kroah-Hartman | 094f1649 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 69 | |
Alan Stern | be69e5b | 2005-10-25 15:56:06 -0400 | [diff] [blame] | 70 | static ssize_t show_ep_type(struct usb_device *udev, |
| 71 | struct usb_endpoint_descriptor *desc, char *buf) |
Greg Kroah-Hartman | 094f1649 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 72 | { |
Greg Kroah-Hartman | 094f1649 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 73 | char *type = "unknown"; |
| 74 | |
Alan Stern | be69e5b | 2005-10-25 15:56:06 -0400 | [diff] [blame] | 75 | switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) { |
Greg Kroah-Hartman | 094f1649 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 76 | case USB_ENDPOINT_XFER_CONTROL: |
| 77 | type = "Control"; |
| 78 | break; |
| 79 | case USB_ENDPOINT_XFER_ISOC: |
| 80 | type = "Isoc"; |
| 81 | break; |
| 82 | case USB_ENDPOINT_XFER_BULK: |
| 83 | type = "Bulk"; |
| 84 | break; |
| 85 | case USB_ENDPOINT_XFER_INT: |
| 86 | type = "Interrupt"; |
| 87 | break; |
| 88 | } |
| 89 | return sprintf(buf, "%s\n", type); |
| 90 | } |
Alan Stern | be69e5b | 2005-10-25 15:56:06 -0400 | [diff] [blame] | 91 | static EP_ATTR(type); |
Greg Kroah-Hartman | 094f1649 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 92 | |
Alan Stern | be69e5b | 2005-10-25 15:56:06 -0400 | [diff] [blame] | 93 | static ssize_t show_ep_interval(struct usb_device *udev, |
| 94 | struct usb_endpoint_descriptor *desc, char *buf) |
Greg Kroah-Hartman | 094f1649 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 95 | { |
Greg Kroah-Hartman | 094f1649 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 96 | char unit; |
| 97 | unsigned interval = 0; |
| 98 | unsigned in; |
| 99 | |
Alan Stern | be69e5b | 2005-10-25 15:56:06 -0400 | [diff] [blame] | 100 | in = (desc->bEndpointAddress & USB_DIR_IN); |
Greg Kroah-Hartman | 094f1649 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 101 | |
Alan Stern | be69e5b | 2005-10-25 15:56:06 -0400 | [diff] [blame] | 102 | switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) { |
Greg Kroah-Hartman | 094f1649 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 103 | case USB_ENDPOINT_XFER_CONTROL: |
| 104 | if (udev->speed == USB_SPEED_HIGH) /* uframes per NAK */ |
Alan Stern | be69e5b | 2005-10-25 15:56:06 -0400 | [diff] [blame] | 105 | interval = desc->bInterval; |
Greg Kroah-Hartman | 094f1649 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 106 | break; |
| 107 | case USB_ENDPOINT_XFER_ISOC: |
Alan Stern | be69e5b | 2005-10-25 15:56:06 -0400 | [diff] [blame] | 108 | interval = 1 << (desc->bInterval - 1); |
Greg Kroah-Hartman | 094f1649 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 109 | break; |
| 110 | case USB_ENDPOINT_XFER_BULK: |
Alan Stern | be69e5b | 2005-10-25 15:56:06 -0400 | [diff] [blame] | 111 | if (udev->speed == USB_SPEED_HIGH && !in) /* uframes per NAK */ |
| 112 | interval = desc->bInterval; |
Greg Kroah-Hartman | 094f1649 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 113 | break; |
| 114 | case USB_ENDPOINT_XFER_INT: |
Alan Stern | be69e5b | 2005-10-25 15:56:06 -0400 | [diff] [blame] | 115 | if (udev->speed == USB_SPEED_HIGH) |
| 116 | interval = 1 << (desc->bInterval - 1); |
| 117 | else |
| 118 | interval = desc->bInterval; |
Greg Kroah-Hartman | 094f1649 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 119 | break; |
| 120 | } |
| 121 | interval *= (udev->speed == USB_SPEED_HIGH) ? 125 : 1000; |
| 122 | if (interval % 1000) |
| 123 | unit = 'u'; |
| 124 | else { |
| 125 | unit = 'm'; |
| 126 | interval /= 1000; |
| 127 | } |
| 128 | |
| 129 | return sprintf(buf, "%d%cs\n", interval, unit); |
| 130 | } |
Alan Stern | be69e5b | 2005-10-25 15:56:06 -0400 | [diff] [blame] | 131 | static EP_ATTR(interval); |
Greg Kroah-Hartman | 094f1649 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 132 | |
Alan Stern | be69e5b | 2005-10-25 15:56:06 -0400 | [diff] [blame] | 133 | static ssize_t show_ep_direction(struct usb_device *udev, |
| 134 | struct usb_endpoint_descriptor *desc, char *buf) |
Greg Kroah-Hartman | 094f1649 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 135 | { |
Greg Kroah-Hartman | 094f1649 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 136 | char *direction; |
| 137 | |
Alan Stern | be69e5b | 2005-10-25 15:56:06 -0400 | [diff] [blame] | 138 | if ((desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == |
| 139 | USB_ENDPOINT_XFER_CONTROL) |
Greg Kroah-Hartman | 094f1649 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 140 | direction = "both"; |
Alan Stern | be69e5b | 2005-10-25 15:56:06 -0400 | [diff] [blame] | 141 | else if (desc->bEndpointAddress & USB_DIR_IN) |
Greg Kroah-Hartman | 094f1649 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 142 | direction = "in"; |
| 143 | else |
| 144 | direction = "out"; |
| 145 | return sprintf(buf, "%s\n", direction); |
| 146 | } |
Alan Stern | be69e5b | 2005-10-25 15:56:06 -0400 | [diff] [blame] | 147 | static EP_ATTR(direction); |
Greg Kroah-Hartman | 094f1649 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 148 | |
Alan Stern | be69e5b | 2005-10-25 15:56:06 -0400 | [diff] [blame] | 149 | static struct attribute *ep_attrs[] = { |
| 150 | &ep_bLength.attr, |
| 151 | &ep_bEndpointAddress.attr, |
| 152 | &ep_bmAttributes.attr, |
| 153 | &ep_bInterval.attr, |
| 154 | &ep_wMaxPacketSize.attr, |
| 155 | &ep_type.attr, |
| 156 | &ep_interval.attr, |
| 157 | &ep_direction.attr, |
| 158 | NULL, |
| 159 | }; |
| 160 | |
| 161 | static void ep_object_release(struct kobject *kobj) |
Greg Kroah-Hartman | 094f1649 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 162 | { |
Alan Stern | be69e5b | 2005-10-25 15:56:06 -0400 | [diff] [blame] | 163 | kfree(to_ep_object(kobj)); |
| 164 | } |
Greg Kroah-Hartman | 094f1649 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 165 | |
Alan Stern | be69e5b | 2005-10-25 15:56:06 -0400 | [diff] [blame] | 166 | static ssize_t ep_object_show(struct kobject *kobj, struct attribute *attr, |
| 167 | char *buf) |
| 168 | { |
| 169 | struct ep_object *ep_obj = to_ep_object(kobj); |
| 170 | struct ep_attribute *ep_attr = to_ep_attribute(attr); |
| 171 | |
| 172 | return (ep_attr->show)(ep_obj->udev, ep_obj->desc, buf); |
| 173 | } |
| 174 | |
| 175 | static struct sysfs_ops ep_object_sysfs_ops = { |
| 176 | .show = ep_object_show, |
| 177 | }; |
| 178 | |
| 179 | static struct kobj_type ep_object_ktype = { |
| 180 | .release = ep_object_release, |
| 181 | .sysfs_ops = &ep_object_sysfs_ops, |
| 182 | .default_attrs = ep_attrs, |
| 183 | }; |
| 184 | |
| 185 | static void usb_create_ep_files(struct kobject *parent, |
| 186 | struct usb_host_endpoint *endpoint, |
| 187 | struct usb_device *udev) |
| 188 | { |
| 189 | struct ep_object *ep_obj; |
| 190 | struct kobject *kobj; |
| 191 | |
| 192 | ep_obj = kzalloc(sizeof(struct ep_object), GFP_KERNEL); |
| 193 | if (!ep_obj) |
| 194 | return; |
| 195 | |
| 196 | ep_obj->desc = &endpoint->desc; |
| 197 | ep_obj->udev = udev; |
| 198 | |
| 199 | kobj = &ep_obj->kobj; |
| 200 | kobject_set_name(kobj, "ep_%02x", endpoint->desc.bEndpointAddress); |
| 201 | kobj->parent = parent; |
| 202 | kobj->ktype = &ep_object_ktype; |
| 203 | |
| 204 | /* Don't use kobject_register, because it generates a hotplug event */ |
| 205 | kobject_init(kobj); |
| 206 | if (kobject_add(kobj) == 0) |
| 207 | endpoint->kobj = kobj; |
| 208 | else |
| 209 | kobject_put(kobj); |
| 210 | } |
| 211 | |
| 212 | static void usb_remove_ep_files(struct usb_host_endpoint *endpoint) |
| 213 | { |
| 214 | |
| 215 | if (endpoint->kobj) { |
| 216 | kobject_del(endpoint->kobj); |
| 217 | kobject_put(endpoint->kobj); |
| 218 | endpoint->kobj = NULL; |
Greg Kroah-Hartman | 094f1649 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 219 | } |
Greg Kroah-Hartman | 094f1649 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 220 | } |
| 221 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | /* Active configuration fields */ |
| 223 | #define usb_actconfig_show(field, multiplier, format_string) \ |
Alan Stern | b724ae7 | 2005-10-24 15:36:00 -0400 | [diff] [blame] | 224 | static ssize_t show_##field (struct device *dev, \ |
| 225 | struct device_attribute *attr, char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | { \ |
| 227 | struct usb_device *udev; \ |
| 228 | struct usb_host_config *actconfig; \ |
| 229 | \ |
| 230 | udev = to_usb_device (dev); \ |
| 231 | actconfig = udev->actconfig; \ |
| 232 | if (actconfig) \ |
| 233 | return sprintf (buf, format_string, \ |
| 234 | actconfig->desc.field * multiplier); \ |
| 235 | else \ |
| 236 | return 0; \ |
| 237 | } \ |
| 238 | |
| 239 | #define usb_actconfig_attr(field, multiplier, format_string) \ |
| 240 | usb_actconfig_show(field, multiplier, format_string) \ |
| 241 | static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL); |
| 242 | |
| 243 | usb_actconfig_attr (bNumInterfaces, 1, "%2d\n") |
| 244 | usb_actconfig_attr (bmAttributes, 1, "%2x\n") |
| 245 | usb_actconfig_attr (bMaxPower, 2, "%3dmA\n") |
| 246 | |
Alan Stern | b724ae7 | 2005-10-24 15:36:00 -0400 | [diff] [blame] | 247 | static ssize_t show_configuration_string(struct device *dev, |
| 248 | struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | { |
| 250 | struct usb_device *udev; |
| 251 | struct usb_host_config *actconfig; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | |
| 253 | udev = to_usb_device (dev); |
| 254 | actconfig = udev->actconfig; |
| 255 | if ((!actconfig) || (!actconfig->string)) |
| 256 | return 0; |
Alan Stern | 4f62efe | 2005-10-24 16:24:14 -0400 | [diff] [blame^] | 257 | return sprintf(buf, "%s\n", actconfig->string); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | } |
| 259 | static DEVICE_ATTR(configuration, S_IRUGO, show_configuration_string, NULL); |
| 260 | |
| 261 | /* configuration value is always present, and r/w */ |
| 262 | usb_actconfig_show(bConfigurationValue, 1, "%u\n"); |
| 263 | |
| 264 | static ssize_t |
Alan Stern | b724ae7 | 2005-10-24 15:36:00 -0400 | [diff] [blame] | 265 | set_bConfigurationValue (struct device *dev, struct device_attribute *attr, |
| 266 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | { |
| 268 | struct usb_device *udev = udev = to_usb_device (dev); |
| 269 | int config, value; |
| 270 | |
| 271 | if (sscanf (buf, "%u", &config) != 1 || config > 255) |
| 272 | return -EINVAL; |
| 273 | usb_lock_device(udev); |
| 274 | value = usb_set_configuration (udev, config); |
| 275 | usb_unlock_device(udev); |
| 276 | return (value < 0) ? value : count; |
| 277 | } |
| 278 | |
| 279 | static DEVICE_ATTR(bConfigurationValue, S_IRUGO | S_IWUSR, |
| 280 | show_bConfigurationValue, set_bConfigurationValue); |
| 281 | |
| 282 | /* String fields */ |
| 283 | #define usb_string_attr(name) \ |
Alan Stern | b724ae7 | 2005-10-24 15:36:00 -0400 | [diff] [blame] | 284 | static ssize_t show_##name(struct device *dev, \ |
| 285 | struct device_attribute *attr, char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | { \ |
| 287 | struct usb_device *udev; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | \ |
| 289 | udev = to_usb_device (dev); \ |
Alan Stern | 4f62efe | 2005-10-24 16:24:14 -0400 | [diff] [blame^] | 290 | return sprintf(buf, "%s\n", udev->name); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | } \ |
| 292 | static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL); |
| 293 | |
| 294 | usb_string_attr(product); |
| 295 | usb_string_attr(manufacturer); |
| 296 | usb_string_attr(serial); |
| 297 | |
| 298 | static ssize_t |
Yani Ioannou | 10523b3 | 2005-05-17 06:43:37 -0400 | [diff] [blame] | 299 | show_speed (struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | { |
| 301 | struct usb_device *udev; |
| 302 | char *speed; |
| 303 | |
| 304 | udev = to_usb_device (dev); |
| 305 | |
| 306 | switch (udev->speed) { |
| 307 | case USB_SPEED_LOW: |
| 308 | speed = "1.5"; |
| 309 | break; |
| 310 | case USB_SPEED_UNKNOWN: |
| 311 | case USB_SPEED_FULL: |
| 312 | speed = "12"; |
| 313 | break; |
| 314 | case USB_SPEED_HIGH: |
| 315 | speed = "480"; |
| 316 | break; |
| 317 | default: |
| 318 | speed = "unknown"; |
| 319 | } |
| 320 | return sprintf (buf, "%s\n", speed); |
| 321 | } |
| 322 | static DEVICE_ATTR(speed, S_IRUGO, show_speed, NULL); |
| 323 | |
| 324 | static ssize_t |
Yani Ioannou | 10523b3 | 2005-05-17 06:43:37 -0400 | [diff] [blame] | 325 | show_devnum (struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | { |
| 327 | struct usb_device *udev; |
| 328 | |
| 329 | udev = to_usb_device (dev); |
| 330 | return sprintf (buf, "%d\n", udev->devnum); |
| 331 | } |
| 332 | static DEVICE_ATTR(devnum, S_IRUGO, show_devnum, NULL); |
| 333 | |
| 334 | static ssize_t |
Yani Ioannou | 10523b3 | 2005-05-17 06:43:37 -0400 | [diff] [blame] | 335 | show_version (struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | { |
| 337 | struct usb_device *udev; |
| 338 | u16 bcdUSB; |
| 339 | |
| 340 | udev = to_usb_device(dev); |
| 341 | bcdUSB = le16_to_cpu(udev->descriptor.bcdUSB); |
| 342 | return sprintf(buf, "%2x.%02x\n", bcdUSB >> 8, bcdUSB & 0xff); |
| 343 | } |
| 344 | static DEVICE_ATTR(version, S_IRUGO, show_version, NULL); |
| 345 | |
| 346 | static ssize_t |
Yani Ioannou | 10523b3 | 2005-05-17 06:43:37 -0400 | [diff] [blame] | 347 | show_maxchild (struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | { |
| 349 | struct usb_device *udev; |
| 350 | |
| 351 | udev = to_usb_device (dev); |
| 352 | return sprintf (buf, "%d\n", udev->maxchild); |
| 353 | } |
| 354 | static DEVICE_ATTR(maxchild, S_IRUGO, show_maxchild, NULL); |
| 355 | |
| 356 | /* Descriptor fields */ |
| 357 | #define usb_descriptor_attr_le16(field, format_string) \ |
| 358 | static ssize_t \ |
Alan Stern | b724ae7 | 2005-10-24 15:36:00 -0400 | [diff] [blame] | 359 | show_##field (struct device *dev, struct device_attribute *attr, \ |
| 360 | char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | { \ |
| 362 | struct usb_device *udev; \ |
| 363 | \ |
| 364 | udev = to_usb_device (dev); \ |
| 365 | return sprintf (buf, format_string, \ |
| 366 | le16_to_cpu(udev->descriptor.field)); \ |
| 367 | } \ |
| 368 | static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL); |
| 369 | |
| 370 | usb_descriptor_attr_le16(idVendor, "%04x\n") |
| 371 | usb_descriptor_attr_le16(idProduct, "%04x\n") |
| 372 | usb_descriptor_attr_le16(bcdDevice, "%04x\n") |
| 373 | |
| 374 | #define usb_descriptor_attr(field, format_string) \ |
| 375 | static ssize_t \ |
Alan Stern | b724ae7 | 2005-10-24 15:36:00 -0400 | [diff] [blame] | 376 | show_##field (struct device *dev, struct device_attribute *attr, \ |
| 377 | char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | { \ |
| 379 | struct usb_device *udev; \ |
| 380 | \ |
| 381 | udev = to_usb_device (dev); \ |
| 382 | return sprintf (buf, format_string, udev->descriptor.field); \ |
| 383 | } \ |
| 384 | static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL); |
| 385 | |
| 386 | usb_descriptor_attr (bDeviceClass, "%02x\n") |
| 387 | usb_descriptor_attr (bDeviceSubClass, "%02x\n") |
| 388 | usb_descriptor_attr (bDeviceProtocol, "%02x\n") |
| 389 | usb_descriptor_attr (bNumConfigurations, "%d\n") |
Greg Kroah-Hartman | cf5910b | 2005-06-29 16:53:29 -0700 | [diff] [blame] | 390 | usb_descriptor_attr (bMaxPacketSize0, "%d\n") |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | |
| 392 | static struct attribute *dev_attrs[] = { |
| 393 | /* current configuration's attributes */ |
| 394 | &dev_attr_bNumInterfaces.attr, |
| 395 | &dev_attr_bConfigurationValue.attr, |
| 396 | &dev_attr_bmAttributes.attr, |
| 397 | &dev_attr_bMaxPower.attr, |
| 398 | /* device attributes */ |
| 399 | &dev_attr_idVendor.attr, |
| 400 | &dev_attr_idProduct.attr, |
| 401 | &dev_attr_bcdDevice.attr, |
| 402 | &dev_attr_bDeviceClass.attr, |
| 403 | &dev_attr_bDeviceSubClass.attr, |
| 404 | &dev_attr_bDeviceProtocol.attr, |
| 405 | &dev_attr_bNumConfigurations.attr, |
Greg Kroah-Hartman | cf5910b | 2005-06-29 16:53:29 -0700 | [diff] [blame] | 406 | &dev_attr_bMaxPacketSize0.attr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | &dev_attr_speed.attr, |
| 408 | &dev_attr_devnum.attr, |
| 409 | &dev_attr_version.attr, |
| 410 | &dev_attr_maxchild.attr, |
| 411 | NULL, |
| 412 | }; |
| 413 | static struct attribute_group dev_attr_grp = { |
| 414 | .attrs = dev_attrs, |
| 415 | }; |
| 416 | |
| 417 | void usb_create_sysfs_dev_files (struct usb_device *udev) |
| 418 | { |
| 419 | struct device *dev = &udev->dev; |
| 420 | |
| 421 | sysfs_create_group(&dev->kobj, &dev_attr_grp); |
| 422 | |
| 423 | if (udev->manufacturer) |
| 424 | device_create_file (dev, &dev_attr_manufacturer); |
| 425 | if (udev->product) |
| 426 | device_create_file (dev, &dev_attr_product); |
| 427 | if (udev->serial) |
| 428 | device_create_file (dev, &dev_attr_serial); |
| 429 | device_create_file (dev, &dev_attr_configuration); |
Greg Kroah-Hartman | 094f1649 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 430 | usb_create_ep_files(&dev->kobj, &udev->ep0, udev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | void usb_remove_sysfs_dev_files (struct usb_device *udev) |
| 434 | { |
| 435 | struct device *dev = &udev->dev; |
| 436 | |
Alan Stern | be69e5b | 2005-10-25 15:56:06 -0400 | [diff] [blame] | 437 | usb_remove_ep_files(&udev->ep0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | sysfs_remove_group(&dev->kobj, &dev_attr_grp); |
| 439 | |
Alan Stern | 4f62efe | 2005-10-24 16:24:14 -0400 | [diff] [blame^] | 440 | if (udev->manufacturer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | device_remove_file(dev, &dev_attr_manufacturer); |
Alan Stern | 4f62efe | 2005-10-24 16:24:14 -0400 | [diff] [blame^] | 442 | if (udev->product) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | device_remove_file(dev, &dev_attr_product); |
Alan Stern | 4f62efe | 2005-10-24 16:24:14 -0400 | [diff] [blame^] | 444 | if (udev->serial) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | device_remove_file(dev, &dev_attr_serial); |
| 446 | device_remove_file (dev, &dev_attr_configuration); |
| 447 | } |
| 448 | |
| 449 | /* Interface fields */ |
| 450 | #define usb_intf_attr(field, format_string) \ |
| 451 | static ssize_t \ |
Alan Stern | b724ae7 | 2005-10-24 15:36:00 -0400 | [diff] [blame] | 452 | show_##field (struct device *dev, struct device_attribute *attr, \ |
| 453 | char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | { \ |
| 455 | struct usb_interface *intf = to_usb_interface (dev); \ |
| 456 | \ |
Alan Stern | b724ae7 | 2005-10-24 15:36:00 -0400 | [diff] [blame] | 457 | return sprintf (buf, format_string, \ |
| 458 | intf->cur_altsetting->desc.field); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | } \ |
| 460 | static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL); |
| 461 | |
| 462 | usb_intf_attr (bInterfaceNumber, "%02x\n") |
| 463 | usb_intf_attr (bAlternateSetting, "%2d\n") |
| 464 | usb_intf_attr (bNumEndpoints, "%02x\n") |
| 465 | usb_intf_attr (bInterfaceClass, "%02x\n") |
| 466 | usb_intf_attr (bInterfaceSubClass, "%02x\n") |
| 467 | usb_intf_attr (bInterfaceProtocol, "%02x\n") |
| 468 | |
Alan Stern | b724ae7 | 2005-10-24 15:36:00 -0400 | [diff] [blame] | 469 | static ssize_t show_interface_string(struct device *dev, |
| 470 | struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | { |
| 472 | struct usb_interface *intf; |
| 473 | struct usb_device *udev; |
| 474 | int len; |
| 475 | |
| 476 | intf = to_usb_interface (dev); |
| 477 | udev = interface_to_usbdev (intf); |
| 478 | len = snprintf(buf, 256, "%s", intf->cur_altsetting->string); |
| 479 | if (len < 0) |
| 480 | return 0; |
| 481 | buf[len] = '\n'; |
| 482 | buf[len+1] = 0; |
| 483 | return len+1; |
| 484 | } |
| 485 | static DEVICE_ATTR(interface, S_IRUGO, show_interface_string, NULL); |
| 486 | |
Alan Stern | b724ae7 | 2005-10-24 15:36:00 -0400 | [diff] [blame] | 487 | static ssize_t show_modalias(struct device *dev, |
| 488 | struct device_attribute *attr, char *buf) |
Greg KH | 360b52b | 2005-05-10 06:45:10 -0700 | [diff] [blame] | 489 | { |
| 490 | struct usb_interface *intf; |
| 491 | struct usb_device *udev; |
Greg Kroah-Hartman | 7521803 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 492 | struct usb_host_interface *alt; |
Greg KH | 360b52b | 2005-05-10 06:45:10 -0700 | [diff] [blame] | 493 | |
| 494 | intf = to_usb_interface(dev); |
| 495 | udev = interface_to_usbdev(intf); |
Greg Kroah-Hartman | 7521803 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 496 | alt = intf->cur_altsetting; |
Greg KH | 360b52b | 2005-05-10 06:45:10 -0700 | [diff] [blame] | 497 | |
Greg Kroah-Hartman | 7521803 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 498 | return sprintf(buf, "usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02X" |
| 499 | "ic%02Xisc%02Xip%02X\n", |
| 500 | le16_to_cpu(udev->descriptor.idVendor), |
| 501 | le16_to_cpu(udev->descriptor.idProduct), |
| 502 | le16_to_cpu(udev->descriptor.bcdDevice), |
| 503 | udev->descriptor.bDeviceClass, |
| 504 | udev->descriptor.bDeviceSubClass, |
| 505 | udev->descriptor.bDeviceProtocol, |
| 506 | alt->desc.bInterfaceClass, |
| 507 | alt->desc.bInterfaceSubClass, |
| 508 | alt->desc.bInterfaceProtocol); |
Greg KH | 360b52b | 2005-05-10 06:45:10 -0700 | [diff] [blame] | 509 | } |
| 510 | static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL); |
| 511 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | static struct attribute *intf_attrs[] = { |
| 513 | &dev_attr_bInterfaceNumber.attr, |
| 514 | &dev_attr_bAlternateSetting.attr, |
| 515 | &dev_attr_bNumEndpoints.attr, |
| 516 | &dev_attr_bInterfaceClass.attr, |
| 517 | &dev_attr_bInterfaceSubClass.attr, |
| 518 | &dev_attr_bInterfaceProtocol.attr, |
Greg KH | 360b52b | 2005-05-10 06:45:10 -0700 | [diff] [blame] | 519 | &dev_attr_modalias.attr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | NULL, |
| 521 | }; |
| 522 | static struct attribute_group intf_attr_grp = { |
| 523 | .attrs = intf_attrs, |
| 524 | }; |
| 525 | |
Alan Stern | 4f62efe | 2005-10-24 16:24:14 -0400 | [diff] [blame^] | 526 | static inline void usb_create_intf_ep_files(struct usb_interface *intf, |
| 527 | struct usb_device *udev) |
Greg Kroah-Hartman | 094f1649 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 528 | { |
| 529 | struct usb_host_interface *iface_desc; |
| 530 | int i; |
| 531 | |
| 532 | iface_desc = intf->cur_altsetting; |
| 533 | for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) |
| 534 | usb_create_ep_files(&intf->dev.kobj, &iface_desc->endpoint[i], |
Alan Stern | 4f62efe | 2005-10-24 16:24:14 -0400 | [diff] [blame^] | 535 | udev); |
Greg Kroah-Hartman | 094f1649 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 536 | } |
| 537 | |
Alan Stern | be69e5b | 2005-10-25 15:56:06 -0400 | [diff] [blame] | 538 | 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] | 539 | { |
| 540 | struct usb_host_interface *iface_desc; |
| 541 | int i; |
| 542 | |
| 543 | iface_desc = intf->cur_altsetting; |
| 544 | for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) |
Alan Stern | be69e5b | 2005-10-25 15:56:06 -0400 | [diff] [blame] | 545 | usb_remove_ep_files(&iface_desc->endpoint[i]); |
Greg Kroah-Hartman | 094f1649 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 546 | } |
| 547 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | void usb_create_sysfs_intf_files (struct usb_interface *intf) |
| 549 | { |
Alan Stern | 4f62efe | 2005-10-24 16:24:14 -0400 | [diff] [blame^] | 550 | struct usb_device *udev = interface_to_usbdev(intf); |
| 551 | struct usb_host_interface *alt = intf->cur_altsetting; |
| 552 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | sysfs_create_group(&intf->dev.kobj, &intf_attr_grp); |
| 554 | |
Alan Stern | 4f62efe | 2005-10-24 16:24:14 -0400 | [diff] [blame^] | 555 | if (alt->string == NULL) |
| 556 | alt->string = usb_cache_string(udev, alt->desc.iInterface); |
| 557 | if (alt->string) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | device_create_file(&intf->dev, &dev_attr_interface); |
Alan Stern | 4f62efe | 2005-10-24 16:24:14 -0400 | [diff] [blame^] | 559 | usb_create_intf_ep_files(intf, udev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | void usb_remove_sysfs_intf_files (struct usb_interface *intf) |
| 563 | { |
Greg Kroah-Hartman | 094f1649 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 564 | usb_remove_intf_ep_files(intf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | sysfs_remove_group(&intf->dev.kobj, &intf_attr_grp); |
| 566 | |
| 567 | if (intf->cur_altsetting->string) |
| 568 | device_remove_file(&intf->dev, &dev_attr_interface); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | } |