sysfs.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  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. #include <linux/kernel.h>
  12. #include <linux/string.h>
  13. #include <linux/usb.h>
  14. #include "usb.h"
  15. /* Active configuration fields */
  16. #define usb_actconfig_show(field, multiplier, format_string) \
  17. static ssize_t show_##field(struct device *dev, \
  18. struct device_attribute *attr, char *buf) \
  19. { \
  20. struct usb_device *udev; \
  21. struct usb_host_config *actconfig; \
  22. \
  23. udev = to_usb_device(dev); \
  24. actconfig = udev->actconfig; \
  25. if (actconfig) \
  26. return sprintf(buf, format_string, \
  27. actconfig->desc.field * multiplier); \
  28. else \
  29. return 0; \
  30. } \
  31. #define usb_actconfig_attr(field, multiplier, format_string) \
  32. usb_actconfig_show(field, multiplier, format_string) \
  33. static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
  34. usb_actconfig_attr(bNumInterfaces, 1, "%2d\n")
  35. usb_actconfig_attr(bmAttributes, 1, "%2x\n")
  36. usb_actconfig_attr(bMaxPower, 2, "%3dmA\n")
  37. static ssize_t show_configuration_string(struct device *dev,
  38. struct device_attribute *attr, char *buf)
  39. {
  40. struct usb_device *udev;
  41. struct usb_host_config *actconfig;
  42. udev = to_usb_device(dev);
  43. actconfig = udev->actconfig;
  44. if ((!actconfig) || (!actconfig->string))
  45. return 0;
  46. return sprintf(buf, "%s\n", actconfig->string);
  47. }
  48. static DEVICE_ATTR(configuration, S_IRUGO, show_configuration_string, NULL);
  49. /* configuration value is always present, and r/w */
  50. usb_actconfig_show(bConfigurationValue, 1, "%u\n");
  51. static ssize_t
  52. set_bConfigurationValue(struct device *dev, struct device_attribute *attr,
  53. const char *buf, size_t count)
  54. {
  55. struct usb_device *udev = to_usb_device(dev);
  56. int config, value;
  57. if (sscanf(buf, "%d", &config) != 1 || config < -1 || config > 255)
  58. return -EINVAL;
  59. usb_lock_device(udev);
  60. value = usb_set_configuration(udev, config);
  61. usb_unlock_device(udev);
  62. return (value < 0) ? value : count;
  63. }
  64. static DEVICE_ATTR(bConfigurationValue, S_IRUGO | S_IWUSR,
  65. show_bConfigurationValue, set_bConfigurationValue);
  66. /* String fields */
  67. #define usb_string_attr(name) \
  68. static ssize_t show_##name(struct device *dev, \
  69. struct device_attribute *attr, char *buf) \
  70. { \
  71. struct usb_device *udev; \
  72. \
  73. udev = to_usb_device(dev); \
  74. return sprintf(buf, "%s\n", udev->name); \
  75. } \
  76. static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL);
  77. usb_string_attr(product);
  78. usb_string_attr(manufacturer);
  79. usb_string_attr(serial);
  80. static ssize_t
  81. show_speed(struct device *dev, struct device_attribute *attr, char *buf)
  82. {
  83. struct usb_device *udev;
  84. char *speed;
  85. udev = to_usb_device(dev);
  86. switch (udev->speed) {
  87. case USB_SPEED_LOW:
  88. speed = "1.5";
  89. break;
  90. case USB_SPEED_UNKNOWN:
  91. case USB_SPEED_FULL:
  92. speed = "12";
  93. break;
  94. case USB_SPEED_HIGH:
  95. speed = "480";
  96. break;
  97. default:
  98. speed = "unknown";
  99. }
  100. return sprintf(buf, "%s\n", speed);
  101. }
  102. static DEVICE_ATTR(speed, S_IRUGO, show_speed, NULL);
  103. static ssize_t
  104. show_busnum(struct device *dev, struct device_attribute *attr, char *buf)
  105. {
  106. struct usb_device *udev;
  107. udev = to_usb_device(dev);
  108. return sprintf(buf, "%d\n", udev->bus->busnum);
  109. }
  110. static DEVICE_ATTR(busnum, S_IRUGO, show_busnum, NULL);
  111. static ssize_t
  112. show_devnum(struct device *dev, struct device_attribute *attr, char *buf)
  113. {
  114. struct usb_device *udev;
  115. udev = to_usb_device(dev);
  116. return sprintf(buf, "%d\n", udev->devnum);
  117. }
  118. static DEVICE_ATTR(devnum, S_IRUGO, show_devnum, NULL);
  119. static ssize_t
  120. show_version(struct device *dev, struct device_attribute *attr, char *buf)
  121. {
  122. struct usb_device *udev;
  123. u16 bcdUSB;
  124. udev = to_usb_device(dev);
  125. bcdUSB = le16_to_cpu(udev->descriptor.bcdUSB);
  126. return sprintf(buf, "%2x.%02x\n", bcdUSB >> 8, bcdUSB & 0xff);
  127. }
  128. static DEVICE_ATTR(version, S_IRUGO, show_version, NULL);
  129. static ssize_t
  130. show_maxchild(struct device *dev, struct device_attribute *attr, char *buf)
  131. {
  132. struct usb_device *udev;
  133. udev = to_usb_device(dev);
  134. return sprintf(buf, "%d\n", udev->maxchild);
  135. }
  136. static DEVICE_ATTR(maxchild, S_IRUGO, show_maxchild, NULL);
  137. static ssize_t
  138. show_quirks(struct device *dev, struct device_attribute *attr, char *buf)
  139. {
  140. struct usb_device *udev;
  141. udev = to_usb_device(dev);
  142. return sprintf(buf, "0x%x\n", udev->quirks);
  143. }
  144. static DEVICE_ATTR(quirks, S_IRUGO, show_quirks, NULL);
  145. static ssize_t
  146. show_urbnum(struct device *dev, struct device_attribute *attr, char *buf)
  147. {
  148. struct usb_device *udev;
  149. udev = to_usb_device(dev);
  150. return sprintf(buf, "%d\n", atomic_read(&udev->urbnum));
  151. }
  152. static DEVICE_ATTR(urbnum, S_IRUGO, show_urbnum, NULL);
  153. #ifdef CONFIG_PM
  154. static const char power_group[] = "power";
  155. static ssize_t
  156. show_persist(struct device *dev, struct device_attribute *attr, char *buf)
  157. {
  158. struct usb_device *udev = to_usb_device(dev);
  159. return sprintf(buf, "%d\n", udev->persist_enabled);
  160. }
  161. static ssize_t
  162. set_persist(struct device *dev, struct device_attribute *attr,
  163. const char *buf, size_t count)
  164. {
  165. struct usb_device *udev = to_usb_device(dev);
  166. int value;
  167. /* Hubs are always enabled for USB_PERSIST */
  168. if (udev->descriptor.bDeviceClass == USB_CLASS_HUB)
  169. return -EPERM;
  170. if (sscanf(buf, "%d", &value) != 1)
  171. return -EINVAL;
  172. usb_pm_lock(udev);
  173. udev->persist_enabled = !!value;
  174. usb_pm_unlock(udev);
  175. return count;
  176. }
  177. static DEVICE_ATTR(persist, S_IRUGO | S_IWUSR, show_persist, set_persist);
  178. static int add_persist_attributes(struct device *dev)
  179. {
  180. int rc = 0;
  181. if (is_usb_device(dev)) {
  182. struct usb_device *udev = to_usb_device(dev);
  183. /* Hubs are automatically enabled for USB_PERSIST,
  184. * no point in creating the attribute file.
  185. */
  186. if (udev->descriptor.bDeviceClass != USB_CLASS_HUB)
  187. rc = sysfs_add_file_to_group(&dev->kobj,
  188. &dev_attr_persist.attr,
  189. power_group);
  190. }
  191. return rc;
  192. }
  193. static void remove_persist_attributes(struct device *dev)
  194. {
  195. sysfs_remove_file_from_group(&dev->kobj,
  196. &dev_attr_persist.attr,
  197. power_group);
  198. }
  199. #else
  200. #define add_persist_attributes(dev) 0
  201. #define remove_persist_attributes(dev) do {} while (0)
  202. #endif /* CONFIG_PM */
  203. #ifdef CONFIG_USB_SUSPEND
  204. static ssize_t
  205. show_connected_duration(struct device *dev, struct device_attribute *attr,
  206. char *buf)
  207. {
  208. struct usb_device *udev = to_usb_device(dev);
  209. return sprintf(buf, "%u\n",
  210. jiffies_to_msecs(jiffies - udev->connect_time));
  211. }
  212. static DEVICE_ATTR(connected_duration, S_IRUGO, show_connected_duration, NULL);
  213. /*
  214. * If the device is resumed, the last time the device was suspended has
  215. * been pre-subtracted from active_duration. We add the current time to
  216. * get the duration that the device was actually active.
  217. *
  218. * If the device is suspended, the active_duration is up-to-date.
  219. */
  220. static ssize_t
  221. show_active_duration(struct device *dev, struct device_attribute *attr,
  222. char *buf)
  223. {
  224. struct usb_device *udev = to_usb_device(dev);
  225. int duration;
  226. if (udev->state != USB_STATE_SUSPENDED)
  227. duration = jiffies_to_msecs(jiffies + udev->active_duration);
  228. else
  229. duration = jiffies_to_msecs(udev->active_duration);
  230. return sprintf(buf, "%u\n", duration);
  231. }
  232. static DEVICE_ATTR(active_duration, S_IRUGO, show_active_duration, NULL);
  233. static ssize_t
  234. show_autosuspend(struct device *dev, struct device_attribute *attr, char *buf)
  235. {
  236. struct usb_device *udev = to_usb_device(dev);
  237. return sprintf(buf, "%d\n", udev->autosuspend_delay / HZ);
  238. }
  239. static ssize_t
  240. set_autosuspend(struct device *dev, struct device_attribute *attr,
  241. const char *buf, size_t count)
  242. {
  243. struct usb_device *udev = to_usb_device(dev);
  244. int value;
  245. if (sscanf(buf, "%d", &value) != 1 || value >= INT_MAX/HZ ||
  246. value <= - INT_MAX/HZ)
  247. return -EINVAL;
  248. value *= HZ;
  249. udev->autosuspend_delay = value;
  250. if (value >= 0)
  251. usb_try_autosuspend_device(udev);
  252. else {
  253. if (usb_autoresume_device(udev) == 0)
  254. usb_autosuspend_device(udev);
  255. }
  256. return count;
  257. }
  258. static DEVICE_ATTR(autosuspend, S_IRUGO | S_IWUSR,
  259. show_autosuspend, set_autosuspend);
  260. static const char on_string[] = "on";
  261. static const char auto_string[] = "auto";
  262. static const char suspend_string[] = "suspend";
  263. static ssize_t
  264. show_level(struct device *dev, struct device_attribute *attr, char *buf)
  265. {
  266. struct usb_device *udev = to_usb_device(dev);
  267. const char *p = auto_string;
  268. if (udev->state == USB_STATE_SUSPENDED) {
  269. if (udev->autoresume_disabled)
  270. p = suspend_string;
  271. } else {
  272. if (udev->autosuspend_disabled)
  273. p = on_string;
  274. }
  275. return sprintf(buf, "%s\n", p);
  276. }
  277. static ssize_t
  278. set_level(struct device *dev, struct device_attribute *attr,
  279. const char *buf, size_t count)
  280. {
  281. struct usb_device *udev = to_usb_device(dev);
  282. int len = count;
  283. char *cp;
  284. int rc = 0;
  285. int old_autosuspend_disabled, old_autoresume_disabled;
  286. cp = memchr(buf, '\n', count);
  287. if (cp)
  288. len = cp - buf;
  289. usb_lock_device(udev);
  290. old_autosuspend_disabled = udev->autosuspend_disabled;
  291. old_autoresume_disabled = udev->autoresume_disabled;
  292. /* Setting the flags without calling usb_pm_lock is a subject to
  293. * races, but who cares...
  294. */
  295. if (len == sizeof on_string - 1 &&
  296. strncmp(buf, on_string, len) == 0) {
  297. udev->autosuspend_disabled = 1;
  298. udev->autoresume_disabled = 0;
  299. rc = usb_external_resume_device(udev, PMSG_USER_RESUME);
  300. } else if (len == sizeof auto_string - 1 &&
  301. strncmp(buf, auto_string, len) == 0) {
  302. udev->autosuspend_disabled = 0;
  303. udev->autoresume_disabled = 0;
  304. rc = usb_external_resume_device(udev, PMSG_USER_RESUME);
  305. } else if (len == sizeof suspend_string - 1 &&
  306. strncmp(buf, suspend_string, len) == 0) {
  307. udev->autosuspend_disabled = 0;
  308. udev->autoresume_disabled = 1;
  309. rc = usb_external_suspend_device(udev, PMSG_USER_SUSPEND);
  310. } else
  311. rc = -EINVAL;
  312. if (rc) {
  313. udev->autosuspend_disabled = old_autosuspend_disabled;
  314. udev->autoresume_disabled = old_autoresume_disabled;
  315. }
  316. usb_unlock_device(udev);
  317. return (rc < 0 ? rc : count);
  318. }
  319. static DEVICE_ATTR(level, S_IRUGO | S_IWUSR, show_level, set_level);
  320. static int add_power_attributes(struct device *dev)
  321. {
  322. int rc = 0;
  323. if (is_usb_device(dev)) {
  324. rc = sysfs_add_file_to_group(&dev->kobj,
  325. &dev_attr_autosuspend.attr,
  326. power_group);
  327. if (rc == 0)
  328. rc = sysfs_add_file_to_group(&dev->kobj,
  329. &dev_attr_level.attr,
  330. power_group);
  331. if (rc == 0)
  332. rc = sysfs_add_file_to_group(&dev->kobj,
  333. &dev_attr_connected_duration.attr,
  334. power_group);
  335. if (rc == 0)
  336. rc = sysfs_add_file_to_group(&dev->kobj,
  337. &dev_attr_active_duration.attr,
  338. power_group);
  339. }
  340. return rc;
  341. }
  342. static void remove_power_attributes(struct device *dev)
  343. {
  344. sysfs_remove_file_from_group(&dev->kobj,
  345. &dev_attr_active_duration.attr,
  346. power_group);
  347. sysfs_remove_file_from_group(&dev->kobj,
  348. &dev_attr_connected_duration.attr,
  349. power_group);
  350. sysfs_remove_file_from_group(&dev->kobj,
  351. &dev_attr_level.attr,
  352. power_group);
  353. sysfs_remove_file_from_group(&dev->kobj,
  354. &dev_attr_autosuspend.attr,
  355. power_group);
  356. }
  357. #else
  358. #define add_power_attributes(dev) 0
  359. #define remove_power_attributes(dev) do {} while (0)
  360. #endif /* CONFIG_USB_SUSPEND */
  361. /* Descriptor fields */
  362. #define usb_descriptor_attr_le16(field, format_string) \
  363. static ssize_t \
  364. show_##field(struct device *dev, struct device_attribute *attr, \
  365. char *buf) \
  366. { \
  367. struct usb_device *udev; \
  368. \
  369. udev = to_usb_device(dev); \
  370. return sprintf(buf, format_string, \
  371. le16_to_cpu(udev->descriptor.field)); \
  372. } \
  373. static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
  374. usb_descriptor_attr_le16(idVendor, "%04x\n")
  375. usb_descriptor_attr_le16(idProduct, "%04x\n")
  376. usb_descriptor_attr_le16(bcdDevice, "%04x\n")
  377. #define usb_descriptor_attr(field, format_string) \
  378. static ssize_t \
  379. show_##field(struct device *dev, struct device_attribute *attr, \
  380. char *buf) \
  381. { \
  382. struct usb_device *udev; \
  383. \
  384. udev = to_usb_device(dev); \
  385. return sprintf(buf, format_string, udev->descriptor.field); \
  386. } \
  387. static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
  388. usb_descriptor_attr(bDeviceClass, "%02x\n")
  389. usb_descriptor_attr(bDeviceSubClass, "%02x\n")
  390. usb_descriptor_attr(bDeviceProtocol, "%02x\n")
  391. usb_descriptor_attr(bNumConfigurations, "%d\n")
  392. usb_descriptor_attr(bMaxPacketSize0, "%d\n")
  393. /* show if the device is authorized (1) or not (0) */
  394. static ssize_t usb_dev_authorized_show(struct device *dev,
  395. struct device_attribute *attr,
  396. char *buf)
  397. {
  398. struct usb_device *usb_dev = to_usb_device(dev);
  399. return snprintf(buf, PAGE_SIZE, "%u\n", usb_dev->authorized);
  400. }
  401. /*
  402. * Authorize a device to be used in the system
  403. *
  404. * Writing a 0 deauthorizes the device, writing a 1 authorizes it.
  405. */
  406. static ssize_t usb_dev_authorized_store(struct device *dev,
  407. struct device_attribute *attr,
  408. const char *buf, size_t size)
  409. {
  410. ssize_t result;
  411. struct usb_device *usb_dev = to_usb_device(dev);
  412. unsigned val;
  413. result = sscanf(buf, "%u\n", &val);
  414. if (result != 1)
  415. result = -EINVAL;
  416. else if (val == 0)
  417. result = usb_deauthorize_device(usb_dev);
  418. else
  419. result = usb_authorize_device(usb_dev);
  420. return result < 0? result : size;
  421. }
  422. static DEVICE_ATTR(authorized, 0644,
  423. usb_dev_authorized_show, usb_dev_authorized_store);
  424. static struct attribute *dev_attrs[] = {
  425. /* current configuration's attributes */
  426. &dev_attr_configuration.attr,
  427. &dev_attr_bNumInterfaces.attr,
  428. &dev_attr_bConfigurationValue.attr,
  429. &dev_attr_bmAttributes.attr,
  430. &dev_attr_bMaxPower.attr,
  431. &dev_attr_urbnum.attr,
  432. /* device attributes */
  433. &dev_attr_idVendor.attr,
  434. &dev_attr_idProduct.attr,
  435. &dev_attr_bcdDevice.attr,
  436. &dev_attr_bDeviceClass.attr,
  437. &dev_attr_bDeviceSubClass.attr,
  438. &dev_attr_bDeviceProtocol.attr,
  439. &dev_attr_bNumConfigurations.attr,
  440. &dev_attr_bMaxPacketSize0.attr,
  441. &dev_attr_speed.attr,
  442. &dev_attr_busnum.attr,
  443. &dev_attr_devnum.attr,
  444. &dev_attr_version.attr,
  445. &dev_attr_maxchild.attr,
  446. &dev_attr_quirks.attr,
  447. &dev_attr_authorized.attr,
  448. NULL,
  449. };
  450. static struct attribute_group dev_attr_grp = {
  451. .attrs = dev_attrs,
  452. };
  453. /* When modifying this list, be sure to modify dev_string_attrs_are_visible()
  454. * accordingly.
  455. */
  456. static struct attribute *dev_string_attrs[] = {
  457. &dev_attr_manufacturer.attr,
  458. &dev_attr_product.attr,
  459. &dev_attr_serial.attr,
  460. NULL
  461. };
  462. static mode_t dev_string_attrs_are_visible(struct kobject *kobj,
  463. struct attribute *a, int n)
  464. {
  465. struct usb_device *udev = to_usb_device(
  466. container_of(kobj, struct device, kobj));
  467. if (a == &dev_attr_manufacturer.attr) {
  468. if (udev->manufacturer == NULL)
  469. return 0;
  470. } else if (a == &dev_attr_product.attr) {
  471. if (udev->product == NULL)
  472. return 0;
  473. } else if (a == &dev_attr_serial.attr) {
  474. if (udev->serial == NULL)
  475. return 0;
  476. }
  477. return a->mode;
  478. }
  479. static struct attribute_group dev_string_attr_grp = {
  480. .attrs = dev_string_attrs,
  481. .is_visible = dev_string_attrs_are_visible,
  482. };
  483. struct attribute_group *usb_device_groups[] = {
  484. &dev_attr_grp,
  485. &dev_string_attr_grp,
  486. NULL
  487. };
  488. /* Binary descriptors */
  489. static ssize_t
  490. read_descriptors(struct kobject *kobj, struct bin_attribute *attr,
  491. char *buf, loff_t off, size_t count)
  492. {
  493. struct usb_device *udev = to_usb_device(
  494. container_of(kobj, struct device, kobj));
  495. size_t nleft = count;
  496. size_t srclen, n;
  497. int cfgno;
  498. void *src;
  499. /* The binary attribute begins with the device descriptor.
  500. * Following that are the raw descriptor entries for all the
  501. * configurations (config plus subsidiary descriptors).
  502. */
  503. for (cfgno = -1; cfgno < udev->descriptor.bNumConfigurations &&
  504. nleft > 0; ++cfgno) {
  505. if (cfgno < 0) {
  506. src = &udev->descriptor;
  507. srclen = sizeof(struct usb_device_descriptor);
  508. } else {
  509. src = udev->rawdescriptors[cfgno];
  510. srclen = __le16_to_cpu(udev->config[cfgno].desc.
  511. wTotalLength);
  512. }
  513. if (off < srclen) {
  514. n = min(nleft, srclen - (size_t) off);
  515. memcpy(buf, src + off, n);
  516. nleft -= n;
  517. buf += n;
  518. off = 0;
  519. } else {
  520. off -= srclen;
  521. }
  522. }
  523. return count - nleft;
  524. }
  525. static struct bin_attribute dev_bin_attr_descriptors = {
  526. .attr = {.name = "descriptors", .mode = 0444},
  527. .read = read_descriptors,
  528. .size = 18 + 65535, /* dev descr + max-size raw descriptor */
  529. };
  530. int usb_create_sysfs_dev_files(struct usb_device *udev)
  531. {
  532. struct device *dev = &udev->dev;
  533. int retval;
  534. retval = device_create_bin_file(dev, &dev_bin_attr_descriptors);
  535. if (retval)
  536. goto error;
  537. retval = add_persist_attributes(dev);
  538. if (retval)
  539. goto error;
  540. retval = add_power_attributes(dev);
  541. if (retval)
  542. goto error;
  543. return retval;
  544. error:
  545. usb_remove_sysfs_dev_files(udev);
  546. return retval;
  547. }
  548. void usb_remove_sysfs_dev_files(struct usb_device *udev)
  549. {
  550. struct device *dev = &udev->dev;
  551. remove_power_attributes(dev);
  552. remove_persist_attributes(dev);
  553. device_remove_bin_file(dev, &dev_bin_attr_descriptors);
  554. }
  555. /* Interface Accociation Descriptor fields */
  556. #define usb_intf_assoc_attr(field, format_string) \
  557. static ssize_t \
  558. show_iad_##field(struct device *dev, struct device_attribute *attr, \
  559. char *buf) \
  560. { \
  561. struct usb_interface *intf = to_usb_interface(dev); \
  562. \
  563. return sprintf(buf, format_string, \
  564. intf->intf_assoc->field); \
  565. } \
  566. static DEVICE_ATTR(iad_##field, S_IRUGO, show_iad_##field, NULL);
  567. usb_intf_assoc_attr(bFirstInterface, "%02x\n")
  568. usb_intf_assoc_attr(bInterfaceCount, "%02d\n")
  569. usb_intf_assoc_attr(bFunctionClass, "%02x\n")
  570. usb_intf_assoc_attr(bFunctionSubClass, "%02x\n")
  571. usb_intf_assoc_attr(bFunctionProtocol, "%02x\n")
  572. /* Interface fields */
  573. #define usb_intf_attr(field, format_string) \
  574. static ssize_t \
  575. show_##field(struct device *dev, struct device_attribute *attr, \
  576. char *buf) \
  577. { \
  578. struct usb_interface *intf = to_usb_interface(dev); \
  579. \
  580. return sprintf(buf, format_string, \
  581. intf->cur_altsetting->desc.field); \
  582. } \
  583. static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
  584. usb_intf_attr(bInterfaceNumber, "%02x\n")
  585. usb_intf_attr(bAlternateSetting, "%2d\n")
  586. usb_intf_attr(bNumEndpoints, "%02x\n")
  587. usb_intf_attr(bInterfaceClass, "%02x\n")
  588. usb_intf_attr(bInterfaceSubClass, "%02x\n")
  589. usb_intf_attr(bInterfaceProtocol, "%02x\n")
  590. static ssize_t show_interface_string(struct device *dev,
  591. struct device_attribute *attr, char *buf)
  592. {
  593. struct usb_interface *intf;
  594. char *string;
  595. intf = to_usb_interface(dev);
  596. string = intf->cur_altsetting->string;
  597. barrier(); /* The altsetting might change! */
  598. if (!string)
  599. return 0;
  600. return sprintf(buf, "%s\n", string);
  601. }
  602. static DEVICE_ATTR(interface, S_IRUGO, show_interface_string, NULL);
  603. static ssize_t show_modalias(struct device *dev,
  604. struct device_attribute *attr, char *buf)
  605. {
  606. struct usb_interface *intf;
  607. struct usb_device *udev;
  608. struct usb_host_interface *alt;
  609. intf = to_usb_interface(dev);
  610. udev = interface_to_usbdev(intf);
  611. alt = intf->cur_altsetting;
  612. return sprintf(buf, "usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02X"
  613. "ic%02Xisc%02Xip%02X\n",
  614. le16_to_cpu(udev->descriptor.idVendor),
  615. le16_to_cpu(udev->descriptor.idProduct),
  616. le16_to_cpu(udev->descriptor.bcdDevice),
  617. udev->descriptor.bDeviceClass,
  618. udev->descriptor.bDeviceSubClass,
  619. udev->descriptor.bDeviceProtocol,
  620. alt->desc.bInterfaceClass,
  621. alt->desc.bInterfaceSubClass,
  622. alt->desc.bInterfaceProtocol);
  623. }
  624. static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
  625. static ssize_t show_supports_autosuspend(struct device *dev,
  626. struct device_attribute *attr, char *buf)
  627. {
  628. struct usb_interface *intf;
  629. struct usb_device *udev;
  630. int ret;
  631. intf = to_usb_interface(dev);
  632. udev = interface_to_usbdev(intf);
  633. usb_lock_device(udev);
  634. /* Devices will be autosuspended even when an interface isn't claimed */
  635. if (!intf->dev.driver ||
  636. to_usb_driver(intf->dev.driver)->supports_autosuspend)
  637. ret = sprintf(buf, "%u\n", 1);
  638. else
  639. ret = sprintf(buf, "%u\n", 0);
  640. usb_unlock_device(udev);
  641. return ret;
  642. }
  643. static DEVICE_ATTR(supports_autosuspend, S_IRUGO, show_supports_autosuspend, NULL);
  644. static struct attribute *intf_attrs[] = {
  645. &dev_attr_bInterfaceNumber.attr,
  646. &dev_attr_bAlternateSetting.attr,
  647. &dev_attr_bNumEndpoints.attr,
  648. &dev_attr_bInterfaceClass.attr,
  649. &dev_attr_bInterfaceSubClass.attr,
  650. &dev_attr_bInterfaceProtocol.attr,
  651. &dev_attr_modalias.attr,
  652. &dev_attr_supports_autosuspend.attr,
  653. NULL,
  654. };
  655. static struct attribute_group intf_attr_grp = {
  656. .attrs = intf_attrs,
  657. };
  658. static struct attribute *intf_assoc_attrs[] = {
  659. &dev_attr_iad_bFirstInterface.attr,
  660. &dev_attr_iad_bInterfaceCount.attr,
  661. &dev_attr_iad_bFunctionClass.attr,
  662. &dev_attr_iad_bFunctionSubClass.attr,
  663. &dev_attr_iad_bFunctionProtocol.attr,
  664. NULL,
  665. };
  666. static mode_t intf_assoc_attrs_are_visible(struct kobject *kobj,
  667. struct attribute *a, int n)
  668. {
  669. struct usb_interface *intf = to_usb_interface(
  670. container_of(kobj, struct device, kobj));
  671. if (intf->intf_assoc == NULL)
  672. return 0;
  673. return a->mode;
  674. }
  675. static struct attribute_group intf_assoc_attr_grp = {
  676. .attrs = intf_assoc_attrs,
  677. .is_visible = intf_assoc_attrs_are_visible,
  678. };
  679. struct attribute_group *usb_interface_groups[] = {
  680. &intf_attr_grp,
  681. &intf_assoc_attr_grp,
  682. NULL
  683. };
  684. int usb_create_sysfs_intf_files(struct usb_interface *intf)
  685. {
  686. struct usb_device *udev = interface_to_usbdev(intf);
  687. struct usb_host_interface *alt = intf->cur_altsetting;
  688. int retval;
  689. if (intf->sysfs_files_created || intf->unregistering)
  690. return 0;
  691. if (alt->string == NULL)
  692. alt->string = usb_cache_string(udev, alt->desc.iInterface);
  693. if (alt->string)
  694. retval = device_create_file(&intf->dev, &dev_attr_interface);
  695. intf->sysfs_files_created = 1;
  696. return 0;
  697. }
  698. void usb_remove_sysfs_intf_files(struct usb_interface *intf)
  699. {
  700. if (!intf->sysfs_files_created)
  701. return;
  702. device_remove_file(&intf->dev, &dev_attr_interface);
  703. intf->sysfs_files_created = 0;
  704. }