sysfs.c 31 KB

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