sysfs.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153
  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. ret = usb_set_usb2_hardware_lpm(udev, value);
  448. }
  449. usb_unlock_device(udev);
  450. if (!ret)
  451. return count;
  452. return ret;
  453. }
  454. static DEVICE_ATTR_RW(usb2_hardware_lpm);
  455. static ssize_t usb2_lpm_l1_timeout_show(struct device *dev,
  456. struct device_attribute *attr,
  457. char *buf)
  458. {
  459. struct usb_device *udev = to_usb_device(dev);
  460. return sprintf(buf, "%d\n", udev->l1_params.timeout);
  461. }
  462. static ssize_t usb2_lpm_l1_timeout_store(struct device *dev,
  463. struct device_attribute *attr,
  464. const char *buf, size_t count)
  465. {
  466. struct usb_device *udev = to_usb_device(dev);
  467. u16 timeout;
  468. if (kstrtou16(buf, 0, &timeout))
  469. return -EINVAL;
  470. udev->l1_params.timeout = timeout;
  471. return count;
  472. }
  473. static DEVICE_ATTR_RW(usb2_lpm_l1_timeout);
  474. static ssize_t usb2_lpm_besl_show(struct device *dev,
  475. struct device_attribute *attr, char *buf)
  476. {
  477. struct usb_device *udev = to_usb_device(dev);
  478. return sprintf(buf, "%d\n", udev->l1_params.besl);
  479. }
  480. static ssize_t usb2_lpm_besl_store(struct device *dev,
  481. struct device_attribute *attr,
  482. const char *buf, size_t count)
  483. {
  484. struct usb_device *udev = to_usb_device(dev);
  485. u8 besl;
  486. if (kstrtou8(buf, 0, &besl) || besl > 15)
  487. return -EINVAL;
  488. udev->l1_params.besl = besl;
  489. return count;
  490. }
  491. static DEVICE_ATTR_RW(usb2_lpm_besl);
  492. static ssize_t usb3_hardware_lpm_u1_show(struct device *dev,
  493. struct device_attribute *attr, char *buf)
  494. {
  495. struct usb_device *udev = to_usb_device(dev);
  496. const char *p;
  497. int rc;
  498. rc = usb_lock_device_interruptible(udev);
  499. if (rc < 0)
  500. return -EINTR;
  501. if (udev->usb3_lpm_u1_enabled)
  502. p = "enabled";
  503. else
  504. p = "disabled";
  505. usb_unlock_device(udev);
  506. return sprintf(buf, "%s\n", p);
  507. }
  508. static DEVICE_ATTR_RO(usb3_hardware_lpm_u1);
  509. static ssize_t usb3_hardware_lpm_u2_show(struct device *dev,
  510. struct device_attribute *attr, char *buf)
  511. {
  512. struct usb_device *udev = to_usb_device(dev);
  513. const char *p;
  514. int rc;
  515. rc = usb_lock_device_interruptible(udev);
  516. if (rc < 0)
  517. return -EINTR;
  518. if (udev->usb3_lpm_u2_enabled)
  519. p = "enabled";
  520. else
  521. p = "disabled";
  522. usb_unlock_device(udev);
  523. return sprintf(buf, "%s\n", p);
  524. }
  525. static DEVICE_ATTR_RO(usb3_hardware_lpm_u2);
  526. static struct attribute *usb2_hardware_lpm_attr[] = {
  527. &dev_attr_usb2_hardware_lpm.attr,
  528. &dev_attr_usb2_lpm_l1_timeout.attr,
  529. &dev_attr_usb2_lpm_besl.attr,
  530. NULL,
  531. };
  532. static struct attribute_group usb2_hardware_lpm_attr_group = {
  533. .name = power_group_name,
  534. .attrs = usb2_hardware_lpm_attr,
  535. };
  536. static struct attribute *usb3_hardware_lpm_attr[] = {
  537. &dev_attr_usb3_hardware_lpm_u1.attr,
  538. &dev_attr_usb3_hardware_lpm_u2.attr,
  539. NULL,
  540. };
  541. static struct attribute_group usb3_hardware_lpm_attr_group = {
  542. .name = power_group_name,
  543. .attrs = usb3_hardware_lpm_attr,
  544. };
  545. static struct attribute *power_attrs[] = {
  546. &dev_attr_autosuspend.attr,
  547. &dev_attr_level.attr,
  548. &dev_attr_connected_duration.attr,
  549. &dev_attr_active_duration.attr,
  550. NULL,
  551. };
  552. static struct attribute_group power_attr_group = {
  553. .name = power_group_name,
  554. .attrs = power_attrs,
  555. };
  556. static int add_power_attributes(struct device *dev)
  557. {
  558. int rc = 0;
  559. if (is_usb_device(dev)) {
  560. struct usb_device *udev = to_usb_device(dev);
  561. rc = sysfs_merge_group(&dev->kobj, &power_attr_group);
  562. if (udev->usb2_hw_lpm_capable == 1)
  563. rc = sysfs_merge_group(&dev->kobj,
  564. &usb2_hardware_lpm_attr_group);
  565. if ((udev->speed == USB_SPEED_SUPER ||
  566. udev->speed == USB_SPEED_SUPER_PLUS) &&
  567. udev->lpm_capable == 1)
  568. rc = sysfs_merge_group(&dev->kobj,
  569. &usb3_hardware_lpm_attr_group);
  570. }
  571. return rc;
  572. }
  573. static void remove_power_attributes(struct device *dev)
  574. {
  575. sysfs_unmerge_group(&dev->kobj, &usb2_hardware_lpm_attr_group);
  576. sysfs_unmerge_group(&dev->kobj, &power_attr_group);
  577. }
  578. #else
  579. #define add_persist_attributes(dev) 0
  580. #define remove_persist_attributes(dev) do {} while (0)
  581. #define add_power_attributes(dev) 0
  582. #define remove_power_attributes(dev) do {} while (0)
  583. #endif /* CONFIG_PM */
  584. /* Descriptor fields */
  585. #define usb_descriptor_attr_le16(field, format_string) \
  586. static ssize_t \
  587. field##_show(struct device *dev, struct device_attribute *attr, \
  588. char *buf) \
  589. { \
  590. struct usb_device *udev; \
  591. \
  592. udev = to_usb_device(dev); \
  593. return sprintf(buf, format_string, \
  594. le16_to_cpu(udev->descriptor.field)); \
  595. } \
  596. static DEVICE_ATTR_RO(field)
  597. usb_descriptor_attr_le16(idVendor, "%04x\n");
  598. usb_descriptor_attr_le16(idProduct, "%04x\n");
  599. usb_descriptor_attr_le16(bcdDevice, "%04x\n");
  600. #define usb_descriptor_attr(field, format_string) \
  601. static ssize_t \
  602. field##_show(struct device *dev, struct device_attribute *attr, \
  603. char *buf) \
  604. { \
  605. struct usb_device *udev; \
  606. \
  607. udev = to_usb_device(dev); \
  608. return sprintf(buf, format_string, udev->descriptor.field); \
  609. } \
  610. static DEVICE_ATTR_RO(field)
  611. usb_descriptor_attr(bDeviceClass, "%02x\n");
  612. usb_descriptor_attr(bDeviceSubClass, "%02x\n");
  613. usb_descriptor_attr(bDeviceProtocol, "%02x\n");
  614. usb_descriptor_attr(bNumConfigurations, "%d\n");
  615. usb_descriptor_attr(bMaxPacketSize0, "%d\n");
  616. /* show if the device is authorized (1) or not (0) */
  617. static ssize_t authorized_show(struct device *dev,
  618. struct device_attribute *attr, char *buf)
  619. {
  620. struct usb_device *usb_dev = to_usb_device(dev);
  621. return snprintf(buf, PAGE_SIZE, "%u\n", usb_dev->authorized);
  622. }
  623. /*
  624. * Authorize a device to be used in the system
  625. *
  626. * Writing a 0 deauthorizes the device, writing a 1 authorizes it.
  627. */
  628. static ssize_t authorized_store(struct device *dev,
  629. struct device_attribute *attr, const char *buf,
  630. size_t size)
  631. {
  632. ssize_t result;
  633. struct usb_device *usb_dev = to_usb_device(dev);
  634. unsigned val;
  635. result = sscanf(buf, "%u\n", &val);
  636. if (result != 1)
  637. result = -EINVAL;
  638. else if (val == 0)
  639. result = usb_deauthorize_device(usb_dev);
  640. else
  641. result = usb_authorize_device(usb_dev);
  642. return result < 0 ? result : size;
  643. }
  644. static DEVICE_ATTR_IGNORE_LOCKDEP(authorized, S_IRUGO | S_IWUSR,
  645. authorized_show, authorized_store);
  646. /* "Safely remove a device" */
  647. static ssize_t remove_store(struct device *dev, struct device_attribute *attr,
  648. const char *buf, size_t count)
  649. {
  650. struct usb_device *udev = to_usb_device(dev);
  651. int rc = 0;
  652. usb_lock_device(udev);
  653. if (udev->state != USB_STATE_NOTATTACHED) {
  654. /* To avoid races, first unconfigure and then remove */
  655. usb_set_configuration(udev, -1);
  656. rc = usb_remove_device(udev);
  657. }
  658. if (rc == 0)
  659. rc = count;
  660. usb_unlock_device(udev);
  661. return rc;
  662. }
  663. static DEVICE_ATTR_IGNORE_LOCKDEP(remove, S_IWUSR, NULL, remove_store);
  664. static struct attribute *dev_attrs[] = {
  665. /* current configuration's attributes */
  666. &dev_attr_configuration.attr,
  667. &dev_attr_bNumInterfaces.attr,
  668. &dev_attr_bConfigurationValue.attr,
  669. &dev_attr_bmAttributes.attr,
  670. &dev_attr_bMaxPower.attr,
  671. /* device attributes */
  672. &dev_attr_urbnum.attr,
  673. &dev_attr_idVendor.attr,
  674. &dev_attr_idProduct.attr,
  675. &dev_attr_bcdDevice.attr,
  676. &dev_attr_bDeviceClass.attr,
  677. &dev_attr_bDeviceSubClass.attr,
  678. &dev_attr_bDeviceProtocol.attr,
  679. &dev_attr_bNumConfigurations.attr,
  680. &dev_attr_bMaxPacketSize0.attr,
  681. &dev_attr_speed.attr,
  682. &dev_attr_rx_lanes.attr,
  683. &dev_attr_tx_lanes.attr,
  684. &dev_attr_busnum.attr,
  685. &dev_attr_devnum.attr,
  686. &dev_attr_devpath.attr,
  687. &dev_attr_version.attr,
  688. &dev_attr_maxchild.attr,
  689. &dev_attr_quirks.attr,
  690. &dev_attr_avoid_reset_quirk.attr,
  691. &dev_attr_authorized.attr,
  692. &dev_attr_remove.attr,
  693. &dev_attr_removable.attr,
  694. &dev_attr_ltm_capable.attr,
  695. #ifdef CONFIG_OF
  696. &dev_attr_devspec.attr,
  697. #endif
  698. NULL,
  699. };
  700. static struct attribute_group dev_attr_grp = {
  701. .attrs = dev_attrs,
  702. };
  703. /* When modifying this list, be sure to modify dev_string_attrs_are_visible()
  704. * accordingly.
  705. */
  706. static struct attribute *dev_string_attrs[] = {
  707. &dev_attr_manufacturer.attr,
  708. &dev_attr_product.attr,
  709. &dev_attr_serial.attr,
  710. NULL
  711. };
  712. static umode_t dev_string_attrs_are_visible(struct kobject *kobj,
  713. struct attribute *a, int n)
  714. {
  715. struct device *dev = container_of(kobj, struct device, kobj);
  716. struct usb_device *udev = to_usb_device(dev);
  717. if (a == &dev_attr_manufacturer.attr) {
  718. if (udev->manufacturer == NULL)
  719. return 0;
  720. } else if (a == &dev_attr_product.attr) {
  721. if (udev->product == NULL)
  722. return 0;
  723. } else if (a == &dev_attr_serial.attr) {
  724. if (udev->serial == NULL)
  725. return 0;
  726. }
  727. return a->mode;
  728. }
  729. static struct attribute_group dev_string_attr_grp = {
  730. .attrs = dev_string_attrs,
  731. .is_visible = dev_string_attrs_are_visible,
  732. };
  733. const struct attribute_group *usb_device_groups[] = {
  734. &dev_attr_grp,
  735. &dev_string_attr_grp,
  736. NULL
  737. };
  738. /* Binary descriptors */
  739. static ssize_t
  740. read_descriptors(struct file *filp, struct kobject *kobj,
  741. struct bin_attribute *attr,
  742. char *buf, loff_t off, size_t count)
  743. {
  744. struct device *dev = container_of(kobj, struct device, kobj);
  745. struct usb_device *udev = to_usb_device(dev);
  746. size_t nleft = count;
  747. size_t srclen, n;
  748. int cfgno;
  749. void *src;
  750. /* The binary attribute begins with the device descriptor.
  751. * Following that are the raw descriptor entries for all the
  752. * configurations (config plus subsidiary descriptors).
  753. */
  754. for (cfgno = -1; cfgno < udev->descriptor.bNumConfigurations &&
  755. nleft > 0; ++cfgno) {
  756. if (cfgno < 0) {
  757. src = &udev->descriptor;
  758. srclen = sizeof(struct usb_device_descriptor);
  759. } else {
  760. src = udev->rawdescriptors[cfgno];
  761. srclen = __le16_to_cpu(udev->config[cfgno].desc.
  762. wTotalLength);
  763. }
  764. if (off < srclen) {
  765. n = min(nleft, srclen - (size_t) off);
  766. memcpy(buf, src + off, n);
  767. nleft -= n;
  768. buf += n;
  769. off = 0;
  770. } else {
  771. off -= srclen;
  772. }
  773. }
  774. return count - nleft;
  775. }
  776. static struct bin_attribute dev_bin_attr_descriptors = {
  777. .attr = {.name = "descriptors", .mode = 0444},
  778. .read = read_descriptors,
  779. .size = 18 + 65535, /* dev descr + max-size raw descriptor */
  780. };
  781. int usb_create_sysfs_dev_files(struct usb_device *udev)
  782. {
  783. struct device *dev = &udev->dev;
  784. int retval;
  785. retval = device_create_bin_file(dev, &dev_bin_attr_descriptors);
  786. if (retval)
  787. goto error;
  788. retval = add_persist_attributes(dev);
  789. if (retval)
  790. goto error;
  791. retval = add_power_attributes(dev);
  792. if (retval)
  793. goto error;
  794. return retval;
  795. error:
  796. usb_remove_sysfs_dev_files(udev);
  797. return retval;
  798. }
  799. void usb_remove_sysfs_dev_files(struct usb_device *udev)
  800. {
  801. struct device *dev = &udev->dev;
  802. remove_power_attributes(dev);
  803. remove_persist_attributes(dev);
  804. device_remove_bin_file(dev, &dev_bin_attr_descriptors);
  805. }
  806. /* Interface Association Descriptor fields */
  807. #define usb_intf_assoc_attr(field, format_string) \
  808. static ssize_t \
  809. iad_##field##_show(struct device *dev, struct device_attribute *attr, \
  810. char *buf) \
  811. { \
  812. struct usb_interface *intf = to_usb_interface(dev); \
  813. \
  814. return sprintf(buf, format_string, \
  815. intf->intf_assoc->field); \
  816. } \
  817. static DEVICE_ATTR_RO(iad_##field)
  818. usb_intf_assoc_attr(bFirstInterface, "%02x\n");
  819. usb_intf_assoc_attr(bInterfaceCount, "%02d\n");
  820. usb_intf_assoc_attr(bFunctionClass, "%02x\n");
  821. usb_intf_assoc_attr(bFunctionSubClass, "%02x\n");
  822. usb_intf_assoc_attr(bFunctionProtocol, "%02x\n");
  823. /* Interface fields */
  824. #define usb_intf_attr(field, format_string) \
  825. static ssize_t \
  826. field##_show(struct device *dev, struct device_attribute *attr, \
  827. char *buf) \
  828. { \
  829. struct usb_interface *intf = to_usb_interface(dev); \
  830. \
  831. return sprintf(buf, format_string, \
  832. intf->cur_altsetting->desc.field); \
  833. } \
  834. static DEVICE_ATTR_RO(field)
  835. usb_intf_attr(bInterfaceNumber, "%02x\n");
  836. usb_intf_attr(bAlternateSetting, "%2d\n");
  837. usb_intf_attr(bNumEndpoints, "%02x\n");
  838. usb_intf_attr(bInterfaceClass, "%02x\n");
  839. usb_intf_attr(bInterfaceSubClass, "%02x\n");
  840. usb_intf_attr(bInterfaceProtocol, "%02x\n");
  841. static ssize_t interface_show(struct device *dev, struct device_attribute *attr,
  842. char *buf)
  843. {
  844. struct usb_interface *intf;
  845. char *string;
  846. intf = to_usb_interface(dev);
  847. string = READ_ONCE(intf->cur_altsetting->string);
  848. if (!string)
  849. return 0;
  850. return sprintf(buf, "%s\n", string);
  851. }
  852. static DEVICE_ATTR_RO(interface);
  853. static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
  854. char *buf)
  855. {
  856. struct usb_interface *intf;
  857. struct usb_device *udev;
  858. struct usb_host_interface *alt;
  859. intf = to_usb_interface(dev);
  860. udev = interface_to_usbdev(intf);
  861. alt = READ_ONCE(intf->cur_altsetting);
  862. return sprintf(buf, "usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02X"
  863. "ic%02Xisc%02Xip%02Xin%02X\n",
  864. le16_to_cpu(udev->descriptor.idVendor),
  865. le16_to_cpu(udev->descriptor.idProduct),
  866. le16_to_cpu(udev->descriptor.bcdDevice),
  867. udev->descriptor.bDeviceClass,
  868. udev->descriptor.bDeviceSubClass,
  869. udev->descriptor.bDeviceProtocol,
  870. alt->desc.bInterfaceClass,
  871. alt->desc.bInterfaceSubClass,
  872. alt->desc.bInterfaceProtocol,
  873. alt->desc.bInterfaceNumber);
  874. }
  875. static DEVICE_ATTR_RO(modalias);
  876. static ssize_t supports_autosuspend_show(struct device *dev,
  877. struct device_attribute *attr,
  878. char *buf)
  879. {
  880. int s;
  881. s = device_lock_interruptible(dev);
  882. if (s < 0)
  883. return -EINTR;
  884. /* Devices will be autosuspended even when an interface isn't claimed */
  885. s = (!dev->driver || to_usb_driver(dev->driver)->supports_autosuspend);
  886. device_unlock(dev);
  887. return sprintf(buf, "%u\n", s);
  888. }
  889. static DEVICE_ATTR_RO(supports_autosuspend);
  890. /*
  891. * interface_authorized_show - show authorization status of an USB interface
  892. * 1 is authorized, 0 is deauthorized
  893. */
  894. static ssize_t interface_authorized_show(struct device *dev,
  895. struct device_attribute *attr, char *buf)
  896. {
  897. struct usb_interface *intf = to_usb_interface(dev);
  898. return sprintf(buf, "%u\n", intf->authorized);
  899. }
  900. /*
  901. * interface_authorized_store - authorize or deauthorize an USB interface
  902. */
  903. static ssize_t interface_authorized_store(struct device *dev,
  904. struct device_attribute *attr, const char *buf, size_t count)
  905. {
  906. struct usb_interface *intf = to_usb_interface(dev);
  907. bool val;
  908. if (strtobool(buf, &val) != 0)
  909. return -EINVAL;
  910. if (val)
  911. usb_authorize_interface(intf);
  912. else
  913. usb_deauthorize_interface(intf);
  914. return count;
  915. }
  916. static struct device_attribute dev_attr_interface_authorized =
  917. __ATTR(authorized, S_IRUGO | S_IWUSR,
  918. interface_authorized_show, interface_authorized_store);
  919. static struct attribute *intf_attrs[] = {
  920. &dev_attr_bInterfaceNumber.attr,
  921. &dev_attr_bAlternateSetting.attr,
  922. &dev_attr_bNumEndpoints.attr,
  923. &dev_attr_bInterfaceClass.attr,
  924. &dev_attr_bInterfaceSubClass.attr,
  925. &dev_attr_bInterfaceProtocol.attr,
  926. &dev_attr_modalias.attr,
  927. &dev_attr_supports_autosuspend.attr,
  928. &dev_attr_interface_authorized.attr,
  929. NULL,
  930. };
  931. static struct attribute_group intf_attr_grp = {
  932. .attrs = intf_attrs,
  933. };
  934. static struct attribute *intf_assoc_attrs[] = {
  935. &dev_attr_iad_bFirstInterface.attr,
  936. &dev_attr_iad_bInterfaceCount.attr,
  937. &dev_attr_iad_bFunctionClass.attr,
  938. &dev_attr_iad_bFunctionSubClass.attr,
  939. &dev_attr_iad_bFunctionProtocol.attr,
  940. NULL,
  941. };
  942. static umode_t intf_assoc_attrs_are_visible(struct kobject *kobj,
  943. struct attribute *a, int n)
  944. {
  945. struct device *dev = container_of(kobj, struct device, kobj);
  946. struct usb_interface *intf = to_usb_interface(dev);
  947. if (intf->intf_assoc == NULL)
  948. return 0;
  949. return a->mode;
  950. }
  951. static struct attribute_group intf_assoc_attr_grp = {
  952. .attrs = intf_assoc_attrs,
  953. .is_visible = intf_assoc_attrs_are_visible,
  954. };
  955. const struct attribute_group *usb_interface_groups[] = {
  956. &intf_attr_grp,
  957. &intf_assoc_attr_grp,
  958. NULL
  959. };
  960. void usb_create_sysfs_intf_files(struct usb_interface *intf)
  961. {
  962. struct usb_device *udev = interface_to_usbdev(intf);
  963. struct usb_host_interface *alt = intf->cur_altsetting;
  964. if (intf->sysfs_files_created || intf->unregistering)
  965. return;
  966. if (!alt->string && !(udev->quirks & USB_QUIRK_CONFIG_INTF_STRINGS))
  967. alt->string = usb_cache_string(udev, alt->desc.iInterface);
  968. if (alt->string && device_create_file(&intf->dev, &dev_attr_interface))
  969. ; /* We don't actually care if the function fails. */
  970. intf->sysfs_files_created = 1;
  971. }
  972. void usb_remove_sysfs_intf_files(struct usb_interface *intf)
  973. {
  974. if (!intf->sysfs_files_created)
  975. return;
  976. device_remove_file(&intf->dev, &dev_attr_interface);
  977. intf->sysfs_files_created = 0;
  978. }