sysfs.c 24 KB

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