sysfs.c 28 KB

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