sysfs.c 22 KB

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