sysfs.c 28 KB

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