sysfs.c 22 KB

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