sysfs.c 24 KB

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