sysfs.c 28 KB

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