blob: c953a0f1c69513a066ceab7bb7372f946b19e8ed [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/kernel.h>
Alan Stern2add5222007-03-20 14:59:39 -040014#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/usb.h>
Alan Stern1662e3a2009-03-18 14:28:53 -040016#include <linux/usb/quirks.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include "usb.h"
18
19/* Active configuration fields */
Sebastian Andrzej Siewior8d8479d2012-12-18 15:25:46 +010020#define usb_actconfig_show(field, format_string) \
Greg Kroah-Hartmand03f2542013-08-23 16:05:26 -070021static ssize_t field##_show(struct device *dev, \
22 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -070023{ \
24 struct usb_device *udev; \
25 struct usb_host_config *actconfig; \
Oliver Neukum7dd9cba2016-01-21 15:18:47 +010026 ssize_t rc; \
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 \
Oliver Neukum92516442007-01-23 15:55:28 -050028 udev = to_usb_device(dev); \
Oliver Neukum7dd9cba2016-01-21 15:18:47 +010029 rc = usb_lock_device_interruptible(udev); \
30 if (rc < 0) \
31 return -EINTR; \
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 actconfig = udev->actconfig; \
33 if (actconfig) \
Alan Stern232275a2013-09-24 15:43:39 -040034 rc = sprintf(buf, format_string, \
Sebastian Andrzej Siewior8d8479d2012-12-18 15:25:46 +010035 actconfig->desc.field); \
Alan Stern232275a2013-09-24 15:43:39 -040036 usb_unlock_device(udev); \
37 return rc; \
Linus Torvalds1da177e2005-04-16 15:20:36 -070038} \
39
Sebastian Andrzej Siewior8d8479d2012-12-18 15:25:46 +010040#define usb_actconfig_attr(field, format_string) \
41 usb_actconfig_show(field, format_string) \
Greg Kroah-Hartmand03f2542013-08-23 16:05:26 -070042 static DEVICE_ATTR_RO(field)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Greg Kroah-Hartmand03f2542013-08-23 16:05:26 -070044usb_actconfig_attr(bNumInterfaces, "%2d\n");
45usb_actconfig_attr(bmAttributes, "%2x\n");
Sebastian Andrzej Siewior8d8479d2012-12-18 15:25:46 +010046
Greg Kroah-Hartmand03f2542013-08-23 16:05:26 -070047static ssize_t bMaxPower_show(struct device *dev,
Sebastian Andrzej Siewior8d8479d2012-12-18 15:25:46 +010048 struct device_attribute *attr, char *buf)
49{
50 struct usb_device *udev;
51 struct usb_host_config *actconfig;
Oliver Neukum7dd9cba2016-01-21 15:18:47 +010052 ssize_t rc;
Sebastian Andrzej Siewior8d8479d2012-12-18 15:25:46 +010053
54 udev = to_usb_device(dev);
Oliver Neukum7dd9cba2016-01-21 15:18:47 +010055 rc = usb_lock_device_interruptible(udev);
56 if (rc < 0)
57 return -EINTR;
Sebastian Andrzej Siewior8d8479d2012-12-18 15:25:46 +010058 actconfig = udev->actconfig;
Alan Stern232275a2013-09-24 15:43:39 -040059 if (actconfig)
60 rc = sprintf(buf, "%dmA\n", usb_get_max_power(udev, actconfig));
61 usb_unlock_device(udev);
62 return rc;
Sebastian Andrzej Siewior8d8479d2012-12-18 15:25:46 +010063}
Greg Kroah-Hartmand03f2542013-08-23 16:05:26 -070064static DEVICE_ATTR_RO(bMaxPower);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Greg Kroah-Hartmand03f2542013-08-23 16:05:26 -070066static ssize_t configuration_show(struct device *dev,
Alan Sternb724ae72005-10-24 15:36:00 -040067 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{
69 struct usb_device *udev;
70 struct usb_host_config *actconfig;
Oliver Neukum7dd9cba2016-01-21 15:18:47 +010071 ssize_t rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Oliver Neukum92516442007-01-23 15:55:28 -050073 udev = to_usb_device(dev);
Oliver Neukum7dd9cba2016-01-21 15:18:47 +010074 rc = usb_lock_device_interruptible(udev);
75 if (rc < 0)
76 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 actconfig = udev->actconfig;
Alan Stern232275a2013-09-24 15:43:39 -040078 if (actconfig && actconfig->string)
79 rc = sprintf(buf, "%s\n", actconfig->string);
80 usb_unlock_device(udev);
81 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082}
Greg Kroah-Hartmand03f2542013-08-23 16:05:26 -070083static DEVICE_ATTR_RO(configuration);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85/* configuration value is always present, and r/w */
Sebastian Andrzej Siewior8d8479d2012-12-18 15:25:46 +010086usb_actconfig_show(bConfigurationValue, "%u\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Greg Kroah-Hartmand03f2542013-08-23 16:05:26 -070088static ssize_t bConfigurationValue_store(struct device *dev,
89 struct device_attribute *attr,
90 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
Oliver Neukum92516442007-01-23 15:55:28 -050092 struct usb_device *udev = to_usb_device(dev);
Oliver Neukum7dd9cba2016-01-21 15:18:47 +010093 int config, value, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Alan Stern3f141e22007-02-08 16:40:43 -050095 if (sscanf(buf, "%d", &config) != 1 || config < -1 || config > 255)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 return -EINVAL;
Oliver Neukum7dd9cba2016-01-21 15:18:47 +010097 rc = usb_lock_device_interruptible(udev);
98 if (rc < 0)
99 return -EINTR;
Oliver Neukum92516442007-01-23 15:55:28 -0500100 value = usb_set_configuration(udev, config);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 usb_unlock_device(udev);
102 return (value < 0) ? value : count;
103}
Alan Stern356c05d2012-05-14 13:30:03 -0400104static DEVICE_ATTR_IGNORE_LOCKDEP(bConfigurationValue, S_IRUGO | S_IWUSR,
Greg Kroah-Hartmand03f2542013-08-23 16:05:26 -0700105 bConfigurationValue_show, bConfigurationValue_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107/* String fields */
108#define usb_string_attr(name) \
Greg Kroah-Hartmand03f2542013-08-23 16:05:26 -0700109static ssize_t name##_show(struct device *dev, \
Alan Sternb724ae72005-10-24 15:36:00 -0400110 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{ \
112 struct usb_device *udev; \
Alan Sternda307122009-12-08 15:54:44 -0500113 int retval; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 \
Oliver Neukum92516442007-01-23 15:55:28 -0500115 udev = to_usb_device(dev); \
Oliver Neukum7dd9cba2016-01-21 15:18:47 +0100116 retval = usb_lock_device_interruptible(udev); \
117 if (retval < 0) \
118 return -EINTR; \
Alan Sternda307122009-12-08 15:54:44 -0500119 retval = sprintf(buf, "%s\n", udev->name); \
120 usb_unlock_device(udev); \
121 return retval; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122} \
Greg Kroah-Hartmand03f2542013-08-23 16:05:26 -0700123static DEVICE_ATTR_RO(name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
125usb_string_attr(product);
126usb_string_attr(manufacturer);
127usb_string_attr(serial);
128
Greg Kroah-Hartmand03f2542013-08-23 16:05:26 -0700129static ssize_t speed_show(struct device *dev, struct device_attribute *attr,
130 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
132 struct usb_device *udev;
133 char *speed;
134
Oliver Neukum92516442007-01-23 15:55:28 -0500135 udev = to_usb_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137 switch (udev->speed) {
138 case USB_SPEED_LOW:
139 speed = "1.5";
140 break;
141 case USB_SPEED_UNKNOWN:
142 case USB_SPEED_FULL:
143 speed = "12";
144 break;
145 case USB_SPEED_HIGH:
146 speed = "480";
147 break;
Greg Kroah-Hartman551cdbb2010-01-14 11:08:04 -0800148 case USB_SPEED_WIRELESS:
Greg Kroah-Hartmanb132b042010-01-14 10:33:19 -0800149 speed = "480";
150 break;
151 case USB_SPEED_SUPER:
152 speed = "5000";
153 break;
Mathias Nymanb2316642015-12-10 09:59:27 +0200154 case USB_SPEED_SUPER_PLUS:
155 speed = "10000";
156 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 default:
158 speed = "unknown";
159 }
Oliver Neukum92516442007-01-23 15:55:28 -0500160 return sprintf(buf, "%s\n", speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161}
Greg Kroah-Hartmand03f2542013-08-23 16:05:26 -0700162static DEVICE_ATTR_RO(speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Greg Kroah-Hartmand03f2542013-08-23 16:05:26 -0700164static ssize_t busnum_show(struct device *dev, struct device_attribute *attr,
165 char *buf)
Alan Stern83f7d952007-04-25 15:15:43 -0400166{
167 struct usb_device *udev;
168
169 udev = to_usb_device(dev);
170 return sprintf(buf, "%d\n", udev->bus->busnum);
171}
Greg Kroah-Hartmand03f2542013-08-23 16:05:26 -0700172static DEVICE_ATTR_RO(busnum);
Alan Stern83f7d952007-04-25 15:15:43 -0400173
Greg Kroah-Hartmand03f2542013-08-23 16:05:26 -0700174static ssize_t devnum_show(struct device *dev, struct device_attribute *attr,
175 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
177 struct usb_device *udev;
178
Oliver Neukum92516442007-01-23 15:55:28 -0500179 udev = to_usb_device(dev);
180 return sprintf(buf, "%d\n", udev->devnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181}
Greg Kroah-Hartmand03f2542013-08-23 16:05:26 -0700182static DEVICE_ATTR_RO(devnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Greg Kroah-Hartmand03f2542013-08-23 16:05:26 -0700184static ssize_t devpath_show(struct device *dev, struct device_attribute *attr,
185 char *buf)
Greg Kroah-Hartman9af23622009-11-30 11:15:02 -0800186{
187 struct usb_device *udev;
188
189 udev = to_usb_device(dev);
190 return sprintf(buf, "%s\n", udev->devpath);
191}
Greg Kroah-Hartmand03f2542013-08-23 16:05:26 -0700192static DEVICE_ATTR_RO(devpath);
Greg Kroah-Hartman9af23622009-11-30 11:15:02 -0800193
Greg Kroah-Hartmand03f2542013-08-23 16:05:26 -0700194static ssize_t version_show(struct device *dev, struct device_attribute *attr,
195 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
197 struct usb_device *udev;
198 u16 bcdUSB;
199
200 udev = to_usb_device(dev);
201 bcdUSB = le16_to_cpu(udev->descriptor.bcdUSB);
202 return sprintf(buf, "%2x.%02x\n", bcdUSB >> 8, bcdUSB & 0xff);
203}
Greg Kroah-Hartmand03f2542013-08-23 16:05:26 -0700204static DEVICE_ATTR_RO(version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Greg Kroah-Hartmand03f2542013-08-23 16:05:26 -0700206static ssize_t maxchild_show(struct device *dev, struct device_attribute *attr,
207 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
209 struct usb_device *udev;
210
Oliver Neukum92516442007-01-23 15:55:28 -0500211 udev = to_usb_device(dev);
212 return sprintf(buf, "%d\n", udev->maxchild);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213}
Greg Kroah-Hartmand03f2542013-08-23 16:05:26 -0700214static DEVICE_ATTR_RO(maxchild);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Greg Kroah-Hartmand03f2542013-08-23 16:05:26 -0700216static ssize_t quirks_show(struct device *dev, struct device_attribute *attr,
217 char *buf)
Oliver Neukum7ceec1f2007-01-26 14:26:21 +0100218{
219 struct usb_device *udev;
220
221 udev = to_usb_device(dev);
222 return sprintf(buf, "0x%x\n", udev->quirks);
223}
Greg Kroah-Hartmand03f2542013-08-23 16:05:26 -0700224static DEVICE_ATTR_RO(quirks);
Oliver Neukum7ceec1f2007-01-26 14:26:21 +0100225
Greg Kroah-Hartmand03f2542013-08-23 16:05:26 -0700226static ssize_t avoid_reset_quirk_show(struct device *dev,
227 struct device_attribute *attr, char *buf)
Oliver Neukumef955342010-01-16 01:33:03 +0100228{
229 struct usb_device *udev;
230
231 udev = to_usb_device(dev);
Lan Tianyu7fda9532012-08-17 16:44:56 +0800232 return sprintf(buf, "%d\n", !!(udev->quirks & USB_QUIRK_RESET));
Oliver Neukumef955342010-01-16 01:33:03 +0100233}
234
Greg Kroah-Hartmand03f2542013-08-23 16:05:26 -0700235static ssize_t avoid_reset_quirk_store(struct device *dev,
236 struct device_attribute *attr,
237 const char *buf, size_t count)
Oliver Neukumef955342010-01-16 01:33:03 +0100238{
239 struct usb_device *udev = to_usb_device(dev);
Oliver Neukum7dd9cba2016-01-21 15:18:47 +0100240 int val, rc;
Oliver Neukumef955342010-01-16 01:33:03 +0100241
Lan Tianyu7fc2cc32012-08-03 16:30:35 +0800242 if (sscanf(buf, "%d", &val) != 1 || val < 0 || val > 1)
Oliver Neukumef955342010-01-16 01:33:03 +0100243 return -EINVAL;
Oliver Neukum7dd9cba2016-01-21 15:18:47 +0100244 rc = usb_lock_device_interruptible(udev);
245 if (rc < 0)
246 return -EINTR;
Lan Tianyu7fc2cc32012-08-03 16:30:35 +0800247 if (val)
Lan Tianyu7fda9532012-08-17 16:44:56 +0800248 udev->quirks |= USB_QUIRK_RESET;
Oliver Neukumef955342010-01-16 01:33:03 +0100249 else
Lan Tianyu7fda9532012-08-17 16:44:56 +0800250 udev->quirks &= ~USB_QUIRK_RESET;
Oliver Neukumef955342010-01-16 01:33:03 +0100251 usb_unlock_device(udev);
252 return count;
253}
Greg Kroah-Hartmand03f2542013-08-23 16:05:26 -0700254static DEVICE_ATTR_RW(avoid_reset_quirk);
Oliver Neukumef955342010-01-16 01:33:03 +0100255
Greg Kroah-Hartmand03f2542013-08-23 16:05:26 -0700256static ssize_t urbnum_show(struct device *dev, struct device_attribute *attr,
257 char *buf)
Sarah Sharp4d59d8a2007-10-03 14:56:03 -0700258{
259 struct usb_device *udev;
260
261 udev = to_usb_device(dev);
262 return sprintf(buf, "%d\n", atomic_read(&udev->urbnum));
263}
Greg Kroah-Hartmand03f2542013-08-23 16:05:26 -0700264static DEVICE_ATTR_RO(urbnum);
Sarah Sharp4d59d8a2007-10-03 14:56:03 -0700265
Greg Kroah-Hartmand03f2542013-08-23 16:05:26 -0700266static ssize_t removable_show(struct device *dev, struct device_attribute *attr,
267 char *buf)
Matthew Garrett0846e7e2012-02-03 17:11:54 -0500268{
269 struct usb_device *udev;
270 char *state;
271
272 udev = to_usb_device(dev);
273
274 switch (udev->removable) {
275 case USB_DEVICE_REMOVABLE:
276 state = "removable";
277 break;
278 case USB_DEVICE_FIXED:
279 state = "fixed";
280 break;
281 default:
282 state = "unknown";
283 }
284
285 return sprintf(buf, "%s\n", state);
286}
Greg Kroah-Hartmand03f2542013-08-23 16:05:26 -0700287static DEVICE_ATTR_RO(removable);
Alan Sternb41a60e2007-05-30 15:39:33 -0400288
Greg Kroah-Hartmand03f2542013-08-23 16:05:26 -0700289static ssize_t ltm_capable_show(struct device *dev,
290 struct device_attribute *attr, char *buf)
Sarah Sharp024f1172012-07-05 17:17:24 -0700291{
292 if (usb_device_supports_ltm(to_usb_device(dev)))
293 return sprintf(buf, "%s\n", "yes");
294 return sprintf(buf, "%s\n", "no");
295}
Greg Kroah-Hartmand03f2542013-08-23 16:05:26 -0700296static DEVICE_ATTR_RO(ltm_capable);
Sarah Sharp024f1172012-07-05 17:17:24 -0700297
Alan Sternfeccc302008-03-03 15:15:59 -0500298#ifdef CONFIG_PM
Alan Sternb41a60e2007-05-30 15:39:33 -0400299
Greg Kroah-Hartmand03f2542013-08-23 16:05:26 -0700300static ssize_t persist_show(struct device *dev, struct device_attribute *attr,
301 char *buf)
Alan Sternb41a60e2007-05-30 15:39:33 -0400302{
303 struct usb_device *udev = to_usb_device(dev);
304
305 return sprintf(buf, "%d\n", udev->persist_enabled);
306}
307
Greg Kroah-Hartmand03f2542013-08-23 16:05:26 -0700308static ssize_t persist_store(struct device *dev, struct device_attribute *attr,
309 const char *buf, size_t count)
Alan Sternb41a60e2007-05-30 15:39:33 -0400310{
311 struct usb_device *udev = to_usb_device(dev);
Oliver Neukum7dd9cba2016-01-21 15:18:47 +0100312 int value, rc;
Alan Sternb41a60e2007-05-30 15:39:33 -0400313
314 /* Hubs are always enabled for USB_PERSIST */
315 if (udev->descriptor.bDeviceClass == USB_CLASS_HUB)
316 return -EPERM;
317
318 if (sscanf(buf, "%d", &value) != 1)
319 return -EINVAL;
Alan Stern0c4db6d2010-01-08 12:56:42 -0500320
Oliver Neukum7dd9cba2016-01-21 15:18:47 +0100321 rc = usb_lock_device_interruptible(udev);
322 if (rc < 0)
323 return -EINTR;
Alan Sternb41a60e2007-05-30 15:39:33 -0400324 udev->persist_enabled = !!value;
Alan Stern0c4db6d2010-01-08 12:56:42 -0500325 usb_unlock_device(udev);
Alan Sternb41a60e2007-05-30 15:39:33 -0400326 return count;
327}
Greg Kroah-Hartmand03f2542013-08-23 16:05:26 -0700328static DEVICE_ATTR_RW(persist);
Alan Sternb41a60e2007-05-30 15:39:33 -0400329
330static int add_persist_attributes(struct device *dev)
331{
332 int rc = 0;
333
334 if (is_usb_device(dev)) {
335 struct usb_device *udev = to_usb_device(dev);
336
Alan Sternfeccc302008-03-03 15:15:59 -0500337 /* Hubs are automatically enabled for USB_PERSIST,
338 * no point in creating the attribute file.
339 */
340 if (udev->descriptor.bDeviceClass != USB_CLASS_HUB)
341 rc = sysfs_add_file_to_group(&dev->kobj,
342 &dev_attr_persist.attr,
Alan Stern045cac62010-11-15 15:57:07 -0500343 power_group_name);
Alan Sternb41a60e2007-05-30 15:39:33 -0400344 }
345 return rc;
346}
347
348static void remove_persist_attributes(struct device *dev)
349{
350 sysfs_remove_file_from_group(&dev->kobj,
351 &dev_attr_persist.attr,
Alan Stern045cac62010-11-15 15:57:07 -0500352 power_group_name);
Alan Sternb41a60e2007-05-30 15:39:33 -0400353}
Alan Stern19c26232007-02-20 15:03:32 -0500354
Greg Kroah-Hartmand03f2542013-08-23 16:05:26 -0700355static ssize_t connected_duration_show(struct device *dev,
356 struct device_attribute *attr, char *buf)
Sarah Sharp15123002007-12-21 16:54:15 -0800357{
358 struct usb_device *udev = to_usb_device(dev);
359
360 return sprintf(buf, "%u\n",
361 jiffies_to_msecs(jiffies - udev->connect_time));
362}
Greg Kroah-Hartmand03f2542013-08-23 16:05:26 -0700363static DEVICE_ATTR_RO(connected_duration);
Sarah Sharp15123002007-12-21 16:54:15 -0800364
365/*
366 * If the device is resumed, the last time the device was suspended has
367 * been pre-subtracted from active_duration. We add the current time to
368 * get the duration that the device was actually active.
369 *
370 * If the device is suspended, the active_duration is up-to-date.
371 */
Greg Kroah-Hartmand03f2542013-08-23 16:05:26 -0700372static ssize_t active_duration_show(struct device *dev,
373 struct device_attribute *attr, char *buf)
Sarah Sharp15123002007-12-21 16:54:15 -0800374{
375 struct usb_device *udev = to_usb_device(dev);
376 int duration;
377
378 if (udev->state != USB_STATE_SUSPENDED)
379 duration = jiffies_to_msecs(jiffies + udev->active_duration);
380 else
381 duration = jiffies_to_msecs(udev->active_duration);
382 return sprintf(buf, "%u\n", duration);
383}
Greg Kroah-Hartmand03f2542013-08-23 16:05:26 -0700384static DEVICE_ATTR_RO(active_duration);
Sarah Sharp15123002007-12-21 16:54:15 -0800385
Greg Kroah-Hartmand03f2542013-08-23 16:05:26 -0700386static ssize_t autosuspend_show(struct device *dev,
387 struct device_attribute *attr, char *buf)
Alan Stern19c26232007-02-20 15:03:32 -0500388{
Alan Sternfcc4a012010-11-15 15:57:51 -0500389 return sprintf(buf, "%d\n", dev->power.autosuspend_delay / 1000);
Alan Stern19c26232007-02-20 15:03:32 -0500390}
391
Greg Kroah-Hartmand03f2542013-08-23 16:05:26 -0700392static ssize_t autosuspend_store(struct device *dev,
393 struct device_attribute *attr, const char *buf,
394 size_t count)
Alan Stern19c26232007-02-20 15:03:32 -0500395{
Alan Sternfcc4a012010-11-15 15:57:51 -0500396 int value;
Alan Stern19c26232007-02-20 15:03:32 -0500397
Alan Sternfcc4a012010-11-15 15:57:51 -0500398 if (sscanf(buf, "%d", &value) != 1 || value >= INT_MAX/1000 ||
399 value <= -INT_MAX/1000)
Alan Stern19c26232007-02-20 15:03:32 -0500400 return -EINVAL;
Alan Stern19c26232007-02-20 15:03:32 -0500401
Alan Sternfcc4a012010-11-15 15:57:51 -0500402 pm_runtime_set_autosuspend_delay(dev, value * 1000);
Alan Stern19c26232007-02-20 15:03:32 -0500403 return count;
404}
Greg Kroah-Hartmand03f2542013-08-23 16:05:26 -0700405static DEVICE_ATTR_RW(autosuspend);
Alan Stern19c26232007-02-20 15:03:32 -0500406
Alan Stern2add5222007-03-20 14:59:39 -0400407static const char on_string[] = "on";
408static const char auto_string[] = "auto";
Alan Stern2add5222007-03-20 14:59:39 -0400409
Matthias Beyer469271f2013-10-10 23:41:27 +0200410static void warn_level(void)
411{
Alan Sterna9030982010-04-02 13:22:16 -0400412 static int level_warned;
413
414 if (!level_warned) {
415 level_warned = 1;
416 printk(KERN_WARNING "WARNING! power/level is deprecated; "
417 "use power/control instead\n");
418 }
419}
420
Greg Kroah-Hartmand03f2542013-08-23 16:05:26 -0700421static ssize_t level_show(struct device *dev, struct device_attribute *attr,
422 char *buf)
Alan Stern2add5222007-03-20 14:59:39 -0400423{
424 struct usb_device *udev = to_usb_device(dev);
425 const char *p = auto_string;
426
Alan Sterna9030982010-04-02 13:22:16 -0400427 warn_level();
Alan Stern9e18c822010-04-02 13:22:09 -0400428 if (udev->state != USB_STATE_SUSPENDED && !udev->dev.power.runtime_auto)
Alan Stern8e4ceb32009-12-07 13:01:37 -0500429 p = on_string;
Alan Stern2add5222007-03-20 14:59:39 -0400430 return sprintf(buf, "%s\n", p);
431}
432
Greg Kroah-Hartmand03f2542013-08-23 16:05:26 -0700433static ssize_t level_store(struct device *dev, struct device_attribute *attr,
434 const char *buf, size_t count)
Alan Stern2add5222007-03-20 14:59:39 -0400435{
436 struct usb_device *udev = to_usb_device(dev);
437 int len = count;
438 char *cp;
Alan Stern9e18c822010-04-02 13:22:09 -0400439 int rc = count;
Oliver Neukum7dd9cba2016-01-21 15:18:47 +0100440 int rv;
Alan Stern2add5222007-03-20 14:59:39 -0400441
Alan Sterna9030982010-04-02 13:22:16 -0400442 warn_level();
Alan Stern2add5222007-03-20 14:59:39 -0400443 cp = memchr(buf, '\n', count);
444 if (cp)
445 len = cp - buf;
446
Oliver Neukum7dd9cba2016-01-21 15:18:47 +0100447 rv = usb_lock_device_interruptible(udev);
448 if (rv < 0)
449 return -EINTR;
Alan Stern2add5222007-03-20 14:59:39 -0400450
Alan Stern2add5222007-03-20 14:59:39 -0400451 if (len == sizeof on_string - 1 &&
Alan Stern088f7fe2010-01-08 12:56:54 -0500452 strncmp(buf, on_string, len) == 0)
Alan Stern9e18c822010-04-02 13:22:09 -0400453 usb_disable_autosuspend(udev);
Alan Stern2add5222007-03-20 14:59:39 -0400454
Alan Stern088f7fe2010-01-08 12:56:54 -0500455 else if (len == sizeof auto_string - 1 &&
456 strncmp(buf, auto_string, len) == 0)
Alan Stern9e18c822010-04-02 13:22:09 -0400457 usb_enable_autosuspend(udev);
Alan Stern2add5222007-03-20 14:59:39 -0400458
Alan Stern088f7fe2010-01-08 12:56:54 -0500459 else
Alan Stern2add5222007-03-20 14:59:39 -0400460 rc = -EINVAL;
461
462 usb_unlock_device(udev);
Alan Stern9e18c822010-04-02 13:22:09 -0400463 return rc;
Alan Stern2add5222007-03-20 14:59:39 -0400464}
Greg Kroah-Hartmand03f2542013-08-23 16:05:26 -0700465static DEVICE_ATTR_RW(level);
Alan Stern2add5222007-03-20 14:59:39 -0400466
Greg Kroah-Hartmand03f2542013-08-23 16:05:26 -0700467static ssize_t usb2_hardware_lpm_show(struct device *dev,
468 struct device_attribute *attr, char *buf)
Andiry Xuc1045e82011-09-23 14:19:53 -0700469{
470 struct usb_device *udev = to_usb_device(dev);
471 const char *p;
472
Sarah Sharpde68bab2013-09-30 17:26:28 +0300473 if (udev->usb2_hw_lpm_allowed == 1)
Andiry Xuc1045e82011-09-23 14:19:53 -0700474 p = "enabled";
475 else
476 p = "disabled";
477
478 return sprintf(buf, "%s\n", p);
479}
480
Greg Kroah-Hartmand03f2542013-08-23 16:05:26 -0700481static ssize_t usb2_hardware_lpm_store(struct device *dev,
482 struct device_attribute *attr,
483 const char *buf, size_t count)
Andiry Xuc1045e82011-09-23 14:19:53 -0700484{
485 struct usb_device *udev = to_usb_device(dev);
486 bool value;
487 int ret;
488
Oliver Neukum7dd9cba2016-01-21 15:18:47 +0100489 ret = usb_lock_device_interruptible(udev);
490 if (ret < 0)
491 return -EINTR;
Andiry Xuc1045e82011-09-23 14:19:53 -0700492
493 ret = strtobool(buf, &value);
494
Sarah Sharpde68bab2013-09-30 17:26:28 +0300495 if (!ret) {
496 udev->usb2_hw_lpm_allowed = value;
Andiry Xuc1045e82011-09-23 14:19:53 -0700497 ret = usb_set_usb2_hardware_lpm(udev, value);
Sarah Sharpde68bab2013-09-30 17:26:28 +0300498 }
Andiry Xuc1045e82011-09-23 14:19:53 -0700499
500 usb_unlock_device(udev);
501
502 if (!ret)
503 return count;
504
505 return ret;
506}
Greg Kroah-Hartmand03f2542013-08-23 16:05:26 -0700507static DEVICE_ATTR_RW(usb2_hardware_lpm);
Andiry Xuc1045e82011-09-23 14:19:53 -0700508
Greg Kroah-Hartmand03f2542013-08-23 16:05:26 -0700509static ssize_t usb2_lpm_l1_timeout_show(struct device *dev,
510 struct device_attribute *attr,
511 char *buf)
Mathias Nyman17f34862013-05-23 17:14:31 +0300512{
513 struct usb_device *udev = to_usb_device(dev);
514 return sprintf(buf, "%d\n", udev->l1_params.timeout);
515}
516
Greg Kroah-Hartmand03f2542013-08-23 16:05:26 -0700517static ssize_t usb2_lpm_l1_timeout_store(struct device *dev,
518 struct device_attribute *attr,
519 const char *buf, size_t count)
Mathias Nyman17f34862013-05-23 17:14:31 +0300520{
521 struct usb_device *udev = to_usb_device(dev);
522 u16 timeout;
523
524 if (kstrtou16(buf, 0, &timeout))
525 return -EINVAL;
526
527 udev->l1_params.timeout = timeout;
528
529 return count;
530}
Greg Kroah-Hartmand03f2542013-08-23 16:05:26 -0700531static DEVICE_ATTR_RW(usb2_lpm_l1_timeout);
Mathias Nyman17f34862013-05-23 17:14:31 +0300532
Greg Kroah-Hartmand03f2542013-08-23 16:05:26 -0700533static ssize_t usb2_lpm_besl_show(struct device *dev,
534 struct device_attribute *attr, char *buf)
Mathias Nyman17f34862013-05-23 17:14:31 +0300535{
536 struct usb_device *udev = to_usb_device(dev);
537 return sprintf(buf, "%d\n", udev->l1_params.besl);
538}
539
Greg Kroah-Hartmand03f2542013-08-23 16:05:26 -0700540static ssize_t usb2_lpm_besl_store(struct device *dev,
541 struct device_attribute *attr,
542 const char *buf, size_t count)
Mathias Nyman17f34862013-05-23 17:14:31 +0300543{
544 struct usb_device *udev = to_usb_device(dev);
545 u8 besl;
546
547 if (kstrtou8(buf, 0, &besl) || besl > 15)
548 return -EINVAL;
549
550 udev->l1_params.besl = besl;
551
552 return count;
553}
Greg Kroah-Hartmand03f2542013-08-23 16:05:26 -0700554static DEVICE_ATTR_RW(usb2_lpm_besl);
Mathias Nyman17f34862013-05-23 17:14:31 +0300555
Lu Baolubf5ce5b2015-11-14 16:26:32 +0800556static ssize_t usb3_hardware_lpm_u1_show(struct device *dev,
Kevin Strasser655fe4e2015-06-16 10:35:30 -0700557 struct device_attribute *attr, char *buf)
558{
559 struct usb_device *udev = to_usb_device(dev);
560 const char *p;
Oliver Neukum7dd9cba2016-01-21 15:18:47 +0100561 int rc;
Kevin Strasser655fe4e2015-06-16 10:35:30 -0700562
Oliver Neukum7dd9cba2016-01-21 15:18:47 +0100563 rc = usb_lock_device_interruptible(udev);
564 if (rc < 0)
565 return -EINTR;
Kevin Strasser655fe4e2015-06-16 10:35:30 -0700566
Lu Baolubf5ce5b2015-11-14 16:26:32 +0800567 if (udev->usb3_lpm_u1_enabled)
Kevin Strasser655fe4e2015-06-16 10:35:30 -0700568 p = "enabled";
569 else
570 p = "disabled";
571
572 usb_unlock_device(udev);
573
574 return sprintf(buf, "%s\n", p);
575}
Lu Baolubf5ce5b2015-11-14 16:26:32 +0800576static DEVICE_ATTR_RO(usb3_hardware_lpm_u1);
577
578static ssize_t usb3_hardware_lpm_u2_show(struct device *dev,
579 struct device_attribute *attr, char *buf)
580{
581 struct usb_device *udev = to_usb_device(dev);
582 const char *p;
Oliver Neukum7dd9cba2016-01-21 15:18:47 +0100583 int rc;
Lu Baolubf5ce5b2015-11-14 16:26:32 +0800584
Oliver Neukum7dd9cba2016-01-21 15:18:47 +0100585 rc = usb_lock_device_interruptible(udev);
586 if (rc < 0)
587 return -EINTR;
Lu Baolubf5ce5b2015-11-14 16:26:32 +0800588
589 if (udev->usb3_lpm_u2_enabled)
590 p = "enabled";
591 else
592 p = "disabled";
593
594 usb_unlock_device(udev);
595
596 return sprintf(buf, "%s\n", p);
597}
598static DEVICE_ATTR_RO(usb3_hardware_lpm_u2);
Kevin Strasser655fe4e2015-06-16 10:35:30 -0700599
Andiry Xuc1045e82011-09-23 14:19:53 -0700600static struct attribute *usb2_hardware_lpm_attr[] = {
601 &dev_attr_usb2_hardware_lpm.attr,
Mathias Nyman17f34862013-05-23 17:14:31 +0300602 &dev_attr_usb2_lpm_l1_timeout.attr,
603 &dev_attr_usb2_lpm_besl.attr,
Andiry Xuc1045e82011-09-23 14:19:53 -0700604 NULL,
605};
606static struct attribute_group usb2_hardware_lpm_attr_group = {
607 .name = power_group_name,
608 .attrs = usb2_hardware_lpm_attr,
609};
610
Kevin Strasser655fe4e2015-06-16 10:35:30 -0700611static struct attribute *usb3_hardware_lpm_attr[] = {
Lu Baolubf5ce5b2015-11-14 16:26:32 +0800612 &dev_attr_usb3_hardware_lpm_u1.attr,
613 &dev_attr_usb3_hardware_lpm_u2.attr,
Kevin Strasser655fe4e2015-06-16 10:35:30 -0700614 NULL,
615};
616static struct attribute_group usb3_hardware_lpm_attr_group = {
617 .name = power_group_name,
618 .attrs = usb3_hardware_lpm_attr,
619};
620
Alan Stern045cac62010-11-15 15:57:07 -0500621static struct attribute *power_attrs[] = {
622 &dev_attr_autosuspend.attr,
623 &dev_attr_level.attr,
624 &dev_attr_connected_duration.attr,
625 &dev_attr_active_duration.attr,
626 NULL,
627};
628static struct attribute_group power_attr_group = {
629 .name = power_group_name,
630 .attrs = power_attrs,
631};
632
Alan Stern19c26232007-02-20 15:03:32 -0500633static int add_power_attributes(struct device *dev)
634{
635 int rc = 0;
636
Andiry Xuc1045e82011-09-23 14:19:53 -0700637 if (is_usb_device(dev)) {
638 struct usb_device *udev = to_usb_device(dev);
Alan Stern045cac62010-11-15 15:57:07 -0500639 rc = sysfs_merge_group(&dev->kobj, &power_attr_group);
Andiry Xuc1045e82011-09-23 14:19:53 -0700640 if (udev->usb2_hw_lpm_capable == 1)
641 rc = sysfs_merge_group(&dev->kobj,
642 &usb2_hardware_lpm_attr_group);
Lu Baolubf5ce5b2015-11-14 16:26:32 +0800643 if (udev->speed == USB_SPEED_SUPER &&
644 udev->lpm_capable == 1)
Kevin Strasser655fe4e2015-06-16 10:35:30 -0700645 rc = sysfs_merge_group(&dev->kobj,
646 &usb3_hardware_lpm_attr_group);
Andiry Xuc1045e82011-09-23 14:19:53 -0700647 }
648
Alan Stern19c26232007-02-20 15:03:32 -0500649 return rc;
650}
651
652static void remove_power_attributes(struct device *dev)
653{
Andiry Xuc1045e82011-09-23 14:19:53 -0700654 sysfs_unmerge_group(&dev->kobj, &usb2_hardware_lpm_attr_group);
Alan Stern045cac62010-11-15 15:57:07 -0500655 sysfs_unmerge_group(&dev->kobj, &power_attr_group);
Alan Stern19c26232007-02-20 15:03:32 -0500656}
657
658#else
659
Rafael J. Wysockiceb6c9c2014-11-29 23:47:05 +0100660#define add_persist_attributes(dev) 0
661#define remove_persist_attributes(dev) do {} while (0)
662
Alan Stern19c26232007-02-20 15:03:32 -0500663#define add_power_attributes(dev) 0
664#define remove_power_attributes(dev) do {} while (0)
665
Rafael J. Wysockiceb6c9c2014-11-29 23:47:05 +0100666#endif /* CONFIG_PM */
Alan Stern19c26232007-02-20 15:03:32 -0500667
Alan Sternb41a60e2007-05-30 15:39:33 -0400668
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669/* Descriptor fields */
670#define usb_descriptor_attr_le16(field, format_string) \
671static ssize_t \
Greg Kroah-Hartmand03f2542013-08-23 16:05:26 -0700672field##_show(struct device *dev, struct device_attribute *attr, \
Alan Sternb724ae72005-10-24 15:36:00 -0400673 char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674{ \
675 struct usb_device *udev; \
676 \
Oliver Neukum92516442007-01-23 15:55:28 -0500677 udev = to_usb_device(dev); \
678 return sprintf(buf, format_string, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 le16_to_cpu(udev->descriptor.field)); \
680} \
Greg Kroah-Hartmand03f2542013-08-23 16:05:26 -0700681static DEVICE_ATTR_RO(field)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Greg Kroah-Hartmand03f2542013-08-23 16:05:26 -0700683usb_descriptor_attr_le16(idVendor, "%04x\n");
684usb_descriptor_attr_le16(idProduct, "%04x\n");
685usb_descriptor_attr_le16(bcdDevice, "%04x\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
687#define usb_descriptor_attr(field, format_string) \
688static ssize_t \
Greg Kroah-Hartmand03f2542013-08-23 16:05:26 -0700689field##_show(struct device *dev, struct device_attribute *attr, \
Alan Sternb724ae72005-10-24 15:36:00 -0400690 char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{ \
692 struct usb_device *udev; \
693 \
Oliver Neukum92516442007-01-23 15:55:28 -0500694 udev = to_usb_device(dev); \
695 return sprintf(buf, format_string, udev->descriptor.field); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696} \
Greg Kroah-Hartmand03f2542013-08-23 16:05:26 -0700697static DEVICE_ATTR_RO(field)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Greg Kroah-Hartmand03f2542013-08-23 16:05:26 -0700699usb_descriptor_attr(bDeviceClass, "%02x\n");
700usb_descriptor_attr(bDeviceSubClass, "%02x\n");
701usb_descriptor_attr(bDeviceProtocol, "%02x\n");
702usb_descriptor_attr(bNumConfigurations, "%d\n");
703usb_descriptor_attr(bMaxPacketSize0, "%d\n");
Inaky Perez-Gonzaleze03f2e82007-07-31 20:34:07 -0700704
705
706/* show if the device is authorized (1) or not (0) */
Greg Kroah-Hartmand03f2542013-08-23 16:05:26 -0700707static ssize_t authorized_show(struct device *dev,
708 struct device_attribute *attr, char *buf)
Inaky Perez-Gonzaleze03f2e82007-07-31 20:34:07 -0700709{
710 struct usb_device *usb_dev = to_usb_device(dev);
711 return snprintf(buf, PAGE_SIZE, "%u\n", usb_dev->authorized);
712}
713
Inaky Perez-Gonzaleze03f2e82007-07-31 20:34:07 -0700714/*
715 * Authorize a device to be used in the system
716 *
717 * Writing a 0 deauthorizes the device, writing a 1 authorizes it.
718 */
Greg Kroah-Hartmand03f2542013-08-23 16:05:26 -0700719static ssize_t authorized_store(struct device *dev,
720 struct device_attribute *attr, const char *buf,
721 size_t size)
Inaky Perez-Gonzaleze03f2e82007-07-31 20:34:07 -0700722{
723 ssize_t result;
724 struct usb_device *usb_dev = to_usb_device(dev);
725 unsigned val;
726 result = sscanf(buf, "%u\n", &val);
727 if (result != 1)
728 result = -EINVAL;
729 else if (val == 0)
730 result = usb_deauthorize_device(usb_dev);
731 else
732 result = usb_authorize_device(usb_dev);
Matthias Beyer469271f2013-10-10 23:41:27 +0200733 return result < 0 ? result : size;
Inaky Perez-Gonzaleze03f2e82007-07-31 20:34:07 -0700734}
Greg Kroah-Hartmand03f2542013-08-23 16:05:26 -0700735static DEVICE_ATTR_IGNORE_LOCKDEP(authorized, S_IRUGO | S_IWUSR,
736 authorized_show, authorized_store);
Inaky Perez-Gonzaleze03f2e82007-07-31 20:34:07 -0700737
Alan Stern253e0572009-10-27 15:20:13 -0400738/* "Safely remove a device" */
Greg Kroah-Hartmand03f2542013-08-23 16:05:26 -0700739static ssize_t remove_store(struct device *dev, struct device_attribute *attr,
740 const char *buf, size_t count)
Alan Stern253e0572009-10-27 15:20:13 -0400741{
742 struct usb_device *udev = to_usb_device(dev);
743 int rc = 0;
744
745 usb_lock_device(udev);
746 if (udev->state != USB_STATE_NOTATTACHED) {
747
748 /* To avoid races, first unconfigure and then remove */
749 usb_set_configuration(udev, -1);
750 rc = usb_remove_device(udev);
751 }
752 if (rc == 0)
753 rc = count;
754 usb_unlock_device(udev);
755 return rc;
756}
Greg Kroah-Hartmand03f2542013-08-23 16:05:26 -0700757static DEVICE_ATTR_IGNORE_LOCKDEP(remove, S_IWUSR, NULL, remove_store);
Alan Stern253e0572009-10-27 15:20:13 -0400758
Inaky Perez-Gonzaleze03f2e82007-07-31 20:34:07 -0700759
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760static struct attribute *dev_attrs[] = {
761 /* current configuration's attributes */
Alan Sternb6eb2d82006-07-06 15:37:42 -0400762 &dev_attr_configuration.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 &dev_attr_bNumInterfaces.attr,
764 &dev_attr_bConfigurationValue.attr,
765 &dev_attr_bmAttributes.attr,
766 &dev_attr_bMaxPower.attr,
767 /* device attributes */
Greg Kroah-Hartman9af23622009-11-30 11:15:02 -0800768 &dev_attr_urbnum.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 &dev_attr_idVendor.attr,
770 &dev_attr_idProduct.attr,
771 &dev_attr_bcdDevice.attr,
772 &dev_attr_bDeviceClass.attr,
773 &dev_attr_bDeviceSubClass.attr,
774 &dev_attr_bDeviceProtocol.attr,
775 &dev_attr_bNumConfigurations.attr,
Greg Kroah-Hartmancf5910b2005-06-29 16:53:29 -0700776 &dev_attr_bMaxPacketSize0.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 &dev_attr_speed.attr,
Alan Stern83f7d952007-04-25 15:15:43 -0400778 &dev_attr_busnum.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 &dev_attr_devnum.attr,
Greg Kroah-Hartman9af23622009-11-30 11:15:02 -0800780 &dev_attr_devpath.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 &dev_attr_version.attr,
782 &dev_attr_maxchild.attr,
Oliver Neukum7ceec1f2007-01-26 14:26:21 +0100783 &dev_attr_quirks.attr,
Oliver Neukumef955342010-01-16 01:33:03 +0100784 &dev_attr_avoid_reset_quirk.attr,
Inaky Perez-Gonzaleze03f2e82007-07-31 20:34:07 -0700785 &dev_attr_authorized.attr,
Alan Stern253e0572009-10-27 15:20:13 -0400786 &dev_attr_remove.attr,
Matthew Garrett0846e7e2012-02-03 17:11:54 -0500787 &dev_attr_removable.attr,
Sarah Sharp024f1172012-07-05 17:17:24 -0700788 &dev_attr_ltm_capable.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 NULL,
790};
791static struct attribute_group dev_attr_grp = {
792 .attrs = dev_attrs,
793};
794
Alan Stern2e5f10e2008-04-30 15:37:19 -0400795/* When modifying this list, be sure to modify dev_string_attrs_are_visible()
796 * accordingly.
797 */
798static struct attribute *dev_string_attrs[] = {
799 &dev_attr_manufacturer.attr,
800 &dev_attr_product.attr,
801 &dev_attr_serial.attr,
802 NULL
803};
804
Al Viro587a1f12011-07-23 23:11:19 -0400805static umode_t dev_string_attrs_are_visible(struct kobject *kobj,
Alan Stern2e5f10e2008-04-30 15:37:19 -0400806 struct attribute *a, int n)
807{
H Hartley Sweetena864e3aa52009-04-15 21:34:40 -0400808 struct device *dev = container_of(kobj, struct device, kobj);
809 struct usb_device *udev = to_usb_device(dev);
Alan Stern2e5f10e2008-04-30 15:37:19 -0400810
811 if (a == &dev_attr_manufacturer.attr) {
812 if (udev->manufacturer == NULL)
813 return 0;
814 } else if (a == &dev_attr_product.attr) {
815 if (udev->product == NULL)
816 return 0;
817 } else if (a == &dev_attr_serial.attr) {
818 if (udev->serial == NULL)
819 return 0;
820 }
821 return a->mode;
822}
823
824static struct attribute_group dev_string_attr_grp = {
825 .attrs = dev_string_attrs,
826 .is_visible = dev_string_attrs_are_visible,
827};
828
David Brownella4dbd672009-06-24 10:06:31 -0700829const struct attribute_group *usb_device_groups[] = {
Alan Stern2e5f10e2008-04-30 15:37:19 -0400830 &dev_attr_grp,
831 &dev_string_attr_grp,
832 NULL
833};
834
Alan Stern69d42a72007-07-12 17:06:23 -0400835/* Binary descriptors */
836
837static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -0700838read_descriptors(struct file *filp, struct kobject *kobj,
839 struct bin_attribute *attr,
Alan Stern69d42a72007-07-12 17:06:23 -0400840 char *buf, loff_t off, size_t count)
841{
H Hartley Sweetena864e3aa52009-04-15 21:34:40 -0400842 struct device *dev = container_of(kobj, struct device, kobj);
843 struct usb_device *udev = to_usb_device(dev);
Alan Stern69d42a72007-07-12 17:06:23 -0400844 size_t nleft = count;
845 size_t srclen, n;
Oliver Neukumb4a90d02016-01-21 15:18:48 +0100846 int cfgno;
Alan Stern217a9082008-05-20 16:40:42 -0400847 void *src;
Alan Stern69d42a72007-07-12 17:06:23 -0400848
Alan Stern217a9082008-05-20 16:40:42 -0400849 /* The binary attribute begins with the device descriptor.
850 * Following that are the raw descriptor entries for all the
851 * configurations (config plus subsidiary descriptors).
Alan Stern69d42a72007-07-12 17:06:23 -0400852 */
Alan Stern217a9082008-05-20 16:40:42 -0400853 for (cfgno = -1; cfgno < udev->descriptor.bNumConfigurations &&
854 nleft > 0; ++cfgno) {
855 if (cfgno < 0) {
856 src = &udev->descriptor;
857 srclen = sizeof(struct usb_device_descriptor);
858 } else {
859 src = udev->rawdescriptors[cfgno];
860 srclen = __le16_to_cpu(udev->config[cfgno].desc.
861 wTotalLength);
862 }
Alan Stern69d42a72007-07-12 17:06:23 -0400863 if (off < srclen) {
Alan Stern217a9082008-05-20 16:40:42 -0400864 n = min(nleft, srclen - (size_t) off);
865 memcpy(buf, src + off, n);
Alan Stern69d42a72007-07-12 17:06:23 -0400866 nleft -= n;
Alan Stern217a9082008-05-20 16:40:42 -0400867 buf += n;
868 off = 0;
869 } else {
870 off -= srclen;
Alan Stern69d42a72007-07-12 17:06:23 -0400871 }
872 }
Alan Stern69d42a72007-07-12 17:06:23 -0400873 return count - nleft;
874}
875
876static struct bin_attribute dev_bin_attr_descriptors = {
877 .attr = {.name = "descriptors", .mode = 0444},
878 .read = read_descriptors,
879 .size = 18 + 65535, /* dev descr + max-size raw descriptor */
880};
881
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700882int usb_create_sysfs_dev_files(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883{
884 struct device *dev = &udev->dev;
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700885 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
Alan Stern69d42a72007-07-12 17:06:23 -0400887 retval = device_create_bin_file(dev, &dev_bin_attr_descriptors);
888 if (retval)
889 goto error;
890
Alan Sternb41a60e2007-05-30 15:39:33 -0400891 retval = add_persist_attributes(dev);
892 if (retval)
893 goto error;
894
Alan Stern19c26232007-02-20 15:03:32 -0500895 retval = add_power_attributes(dev);
896 if (retval)
897 goto error;
Alan Stern3b23dd62008-12-05 14:10:34 -0500898 return retval;
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700899error:
Alan Sternaa084f32007-02-20 14:59:59 -0500900 usb_remove_sysfs_dev_files(udev);
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700901 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902}
903
Oliver Neukum92516442007-01-23 15:55:28 -0500904void usb_remove_sysfs_dev_files(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905{
906 struct device *dev = &udev->dev;
907
Alan Stern19c26232007-02-20 15:03:32 -0500908 remove_power_attributes(dev);
Alan Sternb41a60e2007-05-30 15:39:33 -0400909 remove_persist_attributes(dev);
Alan Stern69d42a72007-07-12 17:06:23 -0400910 device_remove_bin_file(dev, &dev_bin_attr_descriptors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911}
912
Rahul Bedarkar025d4432014-01-04 11:24:41 +0530913/* Interface Association Descriptor fields */
Craig W. Nadler165fe972007-06-15 23:14:35 -0400914#define usb_intf_assoc_attr(field, format_string) \
915static ssize_t \
Greg Kroah-Hartmand03f2542013-08-23 16:05:26 -0700916iad_##field##_show(struct device *dev, struct device_attribute *attr, \
Craig W. Nadler165fe972007-06-15 23:14:35 -0400917 char *buf) \
918{ \
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800919 struct usb_interface *intf = to_usb_interface(dev); \
Craig W. Nadler165fe972007-06-15 23:14:35 -0400920 \
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800921 return sprintf(buf, format_string, \
922 intf->intf_assoc->field); \
Craig W. Nadler165fe972007-06-15 23:14:35 -0400923} \
Greg Kroah-Hartmand03f2542013-08-23 16:05:26 -0700924static DEVICE_ATTR_RO(iad_##field)
Craig W. Nadler165fe972007-06-15 23:14:35 -0400925
Greg Kroah-Hartmand03f2542013-08-23 16:05:26 -0700926usb_intf_assoc_attr(bFirstInterface, "%02x\n");
927usb_intf_assoc_attr(bInterfaceCount, "%02d\n");
928usb_intf_assoc_attr(bFunctionClass, "%02x\n");
929usb_intf_assoc_attr(bFunctionSubClass, "%02x\n");
930usb_intf_assoc_attr(bFunctionProtocol, "%02x\n");
Craig W. Nadler165fe972007-06-15 23:14:35 -0400931
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932/* Interface fields */
933#define usb_intf_attr(field, format_string) \
934static ssize_t \
Greg Kroah-Hartmand03f2542013-08-23 16:05:26 -0700935field##_show(struct device *dev, struct device_attribute *attr, \
Alan Sternb724ae72005-10-24 15:36:00 -0400936 char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937{ \
Oliver Neukum92516442007-01-23 15:55:28 -0500938 struct usb_interface *intf = to_usb_interface(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 \
Oliver Neukum92516442007-01-23 15:55:28 -0500940 return sprintf(buf, format_string, \
Alan Sternb724ae72005-10-24 15:36:00 -0400941 intf->cur_altsetting->desc.field); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942} \
Greg Kroah-Hartmand03f2542013-08-23 16:05:26 -0700943static DEVICE_ATTR_RO(field)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
Greg Kroah-Hartmand03f2542013-08-23 16:05:26 -0700945usb_intf_attr(bInterfaceNumber, "%02x\n");
946usb_intf_attr(bAlternateSetting, "%2d\n");
947usb_intf_attr(bNumEndpoints, "%02x\n");
948usb_intf_attr(bInterfaceClass, "%02x\n");
949usb_intf_attr(bInterfaceSubClass, "%02x\n");
950usb_intf_attr(bInterfaceProtocol, "%02x\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
Greg Kroah-Hartmand03f2542013-08-23 16:05:26 -0700952static ssize_t interface_show(struct device *dev, struct device_attribute *attr,
953 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954{
955 struct usb_interface *intf;
Alan Stern2e5f10e2008-04-30 15:37:19 -0400956 char *string;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
Oliver Neukum92516442007-01-23 15:55:28 -0500958 intf = to_usb_interface(dev);
Alan Stern232275a2013-09-24 15:43:39 -0400959 string = ACCESS_ONCE(intf->cur_altsetting->string);
Alan Stern2e5f10e2008-04-30 15:37:19 -0400960 if (!string)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 return 0;
Alan Stern2e5f10e2008-04-30 15:37:19 -0400962 return sprintf(buf, "%s\n", string);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963}
Greg Kroah-Hartmand03f2542013-08-23 16:05:26 -0700964static DEVICE_ATTR_RO(interface);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
Greg Kroah-Hartmand03f2542013-08-23 16:05:26 -0700966static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
967 char *buf)
Greg KH360b52b2005-05-10 06:45:10 -0700968{
969 struct usb_interface *intf;
970 struct usb_device *udev;
Greg Kroah-Hartman75218032005-06-20 21:15:16 -0700971 struct usb_host_interface *alt;
Greg KH360b52b2005-05-10 06:45:10 -0700972
973 intf = to_usb_interface(dev);
974 udev = interface_to_usbdev(intf);
Alan Stern232275a2013-09-24 15:43:39 -0400975 alt = ACCESS_ONCE(intf->cur_altsetting);
Greg KH360b52b2005-05-10 06:45:10 -0700976
Greg Kroah-Hartman75218032005-06-20 21:15:16 -0700977 return sprintf(buf, "usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02X"
Bjørn Mork81df2d52012-05-18 21:27:43 +0200978 "ic%02Xisc%02Xip%02Xin%02X\n",
Greg Kroah-Hartman75218032005-06-20 21:15:16 -0700979 le16_to_cpu(udev->descriptor.idVendor),
980 le16_to_cpu(udev->descriptor.idProduct),
981 le16_to_cpu(udev->descriptor.bcdDevice),
982 udev->descriptor.bDeviceClass,
983 udev->descriptor.bDeviceSubClass,
984 udev->descriptor.bDeviceProtocol,
985 alt->desc.bInterfaceClass,
986 alt->desc.bInterfaceSubClass,
Bjørn Mork81df2d52012-05-18 21:27:43 +0200987 alt->desc.bInterfaceProtocol,
988 alt->desc.bInterfaceNumber);
Greg KH360b52b2005-05-10 06:45:10 -0700989}
Greg Kroah-Hartmand03f2542013-08-23 16:05:26 -0700990static DEVICE_ATTR_RO(modalias);
Greg KH360b52b2005-05-10 06:45:10 -0700991
Greg Kroah-Hartmand03f2542013-08-23 16:05:26 -0700992static ssize_t supports_autosuspend_show(struct device *dev,
993 struct device_attribute *attr,
994 char *buf)
Sarah Sharp49e7cc82008-10-06 14:45:46 -0700995{
Alan Stern232275a2013-09-24 15:43:39 -0400996 int s;
Sarah Sharp49e7cc82008-10-06 14:45:46 -0700997
Oliver Neukum7dd9cba2016-01-21 15:18:47 +0100998 s = device_lock_interruptible(dev);
999 if (s < 0)
1000 return -EINTR;
Sarah Sharp49e7cc82008-10-06 14:45:46 -07001001 /* Devices will be autosuspended even when an interface isn't claimed */
Alan Stern232275a2013-09-24 15:43:39 -04001002 s = (!dev->driver || to_usb_driver(dev->driver)->supports_autosuspend);
1003 device_unlock(dev);
Sarah Sharp49e7cc82008-10-06 14:45:46 -07001004
Alan Stern232275a2013-09-24 15:43:39 -04001005 return sprintf(buf, "%u\n", s);
Sarah Sharp49e7cc82008-10-06 14:45:46 -07001006}
Greg Kroah-Hartmand03f2542013-08-23 16:05:26 -07001007static DEVICE_ATTR_RO(supports_autosuspend);
Sarah Sharp49e7cc82008-10-06 14:45:46 -07001008
Stefan Koch310d2b42015-08-25 21:10:09 +02001009/*
1010 * interface_authorized_show - show authorization status of an USB interface
1011 * 1 is authorized, 0 is deauthorized
1012 */
1013static ssize_t interface_authorized_show(struct device *dev,
1014 struct device_attribute *attr, char *buf)
1015{
1016 struct usb_interface *intf = to_usb_interface(dev);
1017
1018 return sprintf(buf, "%u\n", intf->authorized);
1019}
1020
1021/*
1022 * interface_authorized_store - authorize or deauthorize an USB interface
1023 */
1024static ssize_t interface_authorized_store(struct device *dev,
1025 struct device_attribute *attr, const char *buf, size_t count)
1026{
1027 struct usb_interface *intf = to_usb_interface(dev);
1028 bool val;
1029
1030 if (strtobool(buf, &val) != 0)
1031 return -EINVAL;
1032
1033 if (val)
1034 usb_authorize_interface(intf);
1035 else
1036 usb_deauthorize_interface(intf);
1037
1038 return count;
1039}
1040static struct device_attribute dev_attr_interface_authorized =
1041 __ATTR(authorized, S_IRUGO | S_IWUSR,
1042 interface_authorized_show, interface_authorized_store);
1043
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044static struct attribute *intf_attrs[] = {
1045 &dev_attr_bInterfaceNumber.attr,
1046 &dev_attr_bAlternateSetting.attr,
1047 &dev_attr_bNumEndpoints.attr,
1048 &dev_attr_bInterfaceClass.attr,
1049 &dev_attr_bInterfaceSubClass.attr,
1050 &dev_attr_bInterfaceProtocol.attr,
Greg KH360b52b2005-05-10 06:45:10 -07001051 &dev_attr_modalias.attr,
Sarah Sharp49e7cc82008-10-06 14:45:46 -07001052 &dev_attr_supports_autosuspend.attr,
Stefan Koch310d2b42015-08-25 21:10:09 +02001053 &dev_attr_interface_authorized.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 NULL,
1055};
1056static struct attribute_group intf_attr_grp = {
1057 .attrs = intf_attrs,
1058};
1059
Alan Stern2e5f10e2008-04-30 15:37:19 -04001060static struct attribute *intf_assoc_attrs[] = {
1061 &dev_attr_iad_bFirstInterface.attr,
1062 &dev_attr_iad_bInterfaceCount.attr,
1063 &dev_attr_iad_bFunctionClass.attr,
1064 &dev_attr_iad_bFunctionSubClass.attr,
1065 &dev_attr_iad_bFunctionProtocol.attr,
1066 NULL,
1067};
1068
Al Viro587a1f12011-07-23 23:11:19 -04001069static umode_t intf_assoc_attrs_are_visible(struct kobject *kobj,
Alan Stern2e5f10e2008-04-30 15:37:19 -04001070 struct attribute *a, int n)
1071{
H Hartley Sweetena864e3aa52009-04-15 21:34:40 -04001072 struct device *dev = container_of(kobj, struct device, kobj);
1073 struct usb_interface *intf = to_usb_interface(dev);
Alan Stern2e5f10e2008-04-30 15:37:19 -04001074
1075 if (intf->intf_assoc == NULL)
1076 return 0;
1077 return a->mode;
1078}
1079
1080static struct attribute_group intf_assoc_attr_grp = {
1081 .attrs = intf_assoc_attrs,
1082 .is_visible = intf_assoc_attrs_are_visible,
1083};
1084
David Brownella4dbd672009-06-24 10:06:31 -07001085const struct attribute_group *usb_interface_groups[] = {
Alan Stern2e5f10e2008-04-30 15:37:19 -04001086 &intf_attr_grp,
1087 &intf_assoc_attr_grp,
1088 NULL
1089};
1090
Michal Nazarewicz643de622011-04-14 17:47:09 +02001091void usb_create_sysfs_intf_files(struct usb_interface *intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092{
Alan Stern4f62efe2005-10-24 16:24:14 -04001093 struct usb_device *udev = interface_to_usbdev(intf);
1094 struct usb_host_interface *alt = intf->cur_altsetting;
1095
Alan Stern352d0262008-10-29 15:16:58 -04001096 if (intf->sysfs_files_created || intf->unregistering)
Michal Nazarewicz643de622011-04-14 17:47:09 +02001097 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
Michal Nazarewicz643de622011-04-14 17:47:09 +02001099 if (!alt->string && !(udev->quirks & USB_QUIRK_CONFIG_INTF_STRINGS))
Alan Stern4f62efe2005-10-24 16:24:14 -04001100 alt->string = usb_cache_string(udev, alt->desc.iInterface);
Michal Nazarewicz643de622011-04-14 17:47:09 +02001101 if (alt->string && device_create_file(&intf->dev, &dev_attr_interface))
1102 ; /* We don't actually care if the function fails. */
Alan Stern7e615592007-11-06 11:43:42 -05001103 intf->sysfs_files_created = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104}
1105
Oliver Neukum92516442007-01-23 15:55:28 -05001106void usb_remove_sysfs_intf_files(struct usb_interface *intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107{
Alan Stern7e615592007-11-06 11:43:42 -05001108 if (!intf->sysfs_files_created)
1109 return;
Alan Stern92b0da12008-10-29 15:18:50 -04001110
Alan Stern92b0da12008-10-29 15:18:50 -04001111 device_remove_file(&intf->dev, &dev_attr_interface);
Alan Stern7e615592007-11-06 11:43:42 -05001112 intf->sysfs_files_created = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113}