sysfs.c 25 KB

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