sysfs.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130
  1. /*
  2. * drivers/usb/core/sysfs.c
  3. *
  4. * (C) Copyright 2002 David Brownell
  5. * (C) Copyright 2002,2004 Greg Kroah-Hartman
  6. * (C) Copyright 2002,2004 IBM Corp.
  7. *
  8. * All of the sysfs file attributes for usb devices and interfaces.
  9. *
  10. * Released under the GPLv2 only.
  11. * SPDX-License-Identifier: GPL-2.0
  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, "%s\n", of_node_full_name(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->lpm_capable == 1)
  551. rc = sysfs_merge_group(&dev->kobj,
  552. &usb3_hardware_lpm_attr_group);
  553. }
  554. return rc;
  555. }
  556. static void remove_power_attributes(struct device *dev)
  557. {
  558. sysfs_unmerge_group(&dev->kobj, &usb2_hardware_lpm_attr_group);
  559. sysfs_unmerge_group(&dev->kobj, &power_attr_group);
  560. }
  561. #else
  562. #define add_persist_attributes(dev) 0
  563. #define remove_persist_attributes(dev) do {} while (0)
  564. #define add_power_attributes(dev) 0
  565. #define remove_power_attributes(dev) do {} while (0)
  566. #endif /* CONFIG_PM */
  567. /* Descriptor fields */
  568. #define usb_descriptor_attr_le16(field, format_string) \
  569. static ssize_t \
  570. field##_show(struct device *dev, struct device_attribute *attr, \
  571. char *buf) \
  572. { \
  573. struct usb_device *udev; \
  574. \
  575. udev = to_usb_device(dev); \
  576. return sprintf(buf, format_string, \
  577. le16_to_cpu(udev->descriptor.field)); \
  578. } \
  579. static DEVICE_ATTR_RO(field)
  580. usb_descriptor_attr_le16(idVendor, "%04x\n");
  581. usb_descriptor_attr_le16(idProduct, "%04x\n");
  582. usb_descriptor_attr_le16(bcdDevice, "%04x\n");
  583. #define usb_descriptor_attr(field, format_string) \
  584. static ssize_t \
  585. field##_show(struct device *dev, struct device_attribute *attr, \
  586. char *buf) \
  587. { \
  588. struct usb_device *udev; \
  589. \
  590. udev = to_usb_device(dev); \
  591. return sprintf(buf, format_string, udev->descriptor.field); \
  592. } \
  593. static DEVICE_ATTR_RO(field)
  594. usb_descriptor_attr(bDeviceClass, "%02x\n");
  595. usb_descriptor_attr(bDeviceSubClass, "%02x\n");
  596. usb_descriptor_attr(bDeviceProtocol, "%02x\n");
  597. usb_descriptor_attr(bNumConfigurations, "%d\n");
  598. usb_descriptor_attr(bMaxPacketSize0, "%d\n");
  599. /* show if the device is authorized (1) or not (0) */
  600. static ssize_t authorized_show(struct device *dev,
  601. struct device_attribute *attr, char *buf)
  602. {
  603. struct usb_device *usb_dev = to_usb_device(dev);
  604. return snprintf(buf, PAGE_SIZE, "%u\n", usb_dev->authorized);
  605. }
  606. /*
  607. * Authorize a device to be used in the system
  608. *
  609. * Writing a 0 deauthorizes the device, writing a 1 authorizes it.
  610. */
  611. static ssize_t authorized_store(struct device *dev,
  612. struct device_attribute *attr, const char *buf,
  613. size_t size)
  614. {
  615. ssize_t result;
  616. struct usb_device *usb_dev = to_usb_device(dev);
  617. unsigned val;
  618. result = sscanf(buf, "%u\n", &val);
  619. if (result != 1)
  620. result = -EINVAL;
  621. else if (val == 0)
  622. result = usb_deauthorize_device(usb_dev);
  623. else
  624. result = usb_authorize_device(usb_dev);
  625. return result < 0 ? result : size;
  626. }
  627. static DEVICE_ATTR_IGNORE_LOCKDEP(authorized, S_IRUGO | S_IWUSR,
  628. authorized_show, authorized_store);
  629. /* "Safely remove a device" */
  630. static ssize_t remove_store(struct device *dev, struct device_attribute *attr,
  631. const char *buf, size_t count)
  632. {
  633. struct usb_device *udev = to_usb_device(dev);
  634. int rc = 0;
  635. usb_lock_device(udev);
  636. if (udev->state != USB_STATE_NOTATTACHED) {
  637. /* To avoid races, first unconfigure and then remove */
  638. usb_set_configuration(udev, -1);
  639. rc = usb_remove_device(udev);
  640. }
  641. if (rc == 0)
  642. rc = count;
  643. usb_unlock_device(udev);
  644. return rc;
  645. }
  646. static DEVICE_ATTR_IGNORE_LOCKDEP(remove, S_IWUSR, NULL, remove_store);
  647. static struct attribute *dev_attrs[] = {
  648. /* current configuration's attributes */
  649. &dev_attr_configuration.attr,
  650. &dev_attr_bNumInterfaces.attr,
  651. &dev_attr_bConfigurationValue.attr,
  652. &dev_attr_bmAttributes.attr,
  653. &dev_attr_bMaxPower.attr,
  654. /* device attributes */
  655. &dev_attr_urbnum.attr,
  656. &dev_attr_idVendor.attr,
  657. &dev_attr_idProduct.attr,
  658. &dev_attr_bcdDevice.attr,
  659. &dev_attr_bDeviceClass.attr,
  660. &dev_attr_bDeviceSubClass.attr,
  661. &dev_attr_bDeviceProtocol.attr,
  662. &dev_attr_bNumConfigurations.attr,
  663. &dev_attr_bMaxPacketSize0.attr,
  664. &dev_attr_speed.attr,
  665. &dev_attr_busnum.attr,
  666. &dev_attr_devnum.attr,
  667. &dev_attr_devpath.attr,
  668. &dev_attr_version.attr,
  669. &dev_attr_maxchild.attr,
  670. &dev_attr_quirks.attr,
  671. &dev_attr_avoid_reset_quirk.attr,
  672. &dev_attr_authorized.attr,
  673. &dev_attr_remove.attr,
  674. &dev_attr_removable.attr,
  675. &dev_attr_ltm_capable.attr,
  676. #ifdef CONFIG_OF
  677. &dev_attr_devspec.attr,
  678. #endif
  679. NULL,
  680. };
  681. static struct attribute_group dev_attr_grp = {
  682. .attrs = dev_attrs,
  683. };
  684. /* When modifying this list, be sure to modify dev_string_attrs_are_visible()
  685. * accordingly.
  686. */
  687. static struct attribute *dev_string_attrs[] = {
  688. &dev_attr_manufacturer.attr,
  689. &dev_attr_product.attr,
  690. &dev_attr_serial.attr,
  691. NULL
  692. };
  693. static umode_t dev_string_attrs_are_visible(struct kobject *kobj,
  694. struct attribute *a, int n)
  695. {
  696. struct device *dev = container_of(kobj, struct device, kobj);
  697. struct usb_device *udev = to_usb_device(dev);
  698. if (a == &dev_attr_manufacturer.attr) {
  699. if (udev->manufacturer == NULL)
  700. return 0;
  701. } else if (a == &dev_attr_product.attr) {
  702. if (udev->product == NULL)
  703. return 0;
  704. } else if (a == &dev_attr_serial.attr) {
  705. if (udev->serial == NULL)
  706. return 0;
  707. }
  708. return a->mode;
  709. }
  710. static struct attribute_group dev_string_attr_grp = {
  711. .attrs = dev_string_attrs,
  712. .is_visible = dev_string_attrs_are_visible,
  713. };
  714. const struct attribute_group *usb_device_groups[] = {
  715. &dev_attr_grp,
  716. &dev_string_attr_grp,
  717. NULL
  718. };
  719. /* Binary descriptors */
  720. static ssize_t
  721. read_descriptors(struct file *filp, struct kobject *kobj,
  722. struct bin_attribute *attr,
  723. char *buf, loff_t off, size_t count)
  724. {
  725. struct device *dev = container_of(kobj, struct device, kobj);
  726. struct usb_device *udev = to_usb_device(dev);
  727. size_t nleft = count;
  728. size_t srclen, n;
  729. int cfgno;
  730. void *src;
  731. /* The binary attribute begins with the device descriptor.
  732. * Following that are the raw descriptor entries for all the
  733. * configurations (config plus subsidiary descriptors).
  734. */
  735. for (cfgno = -1; cfgno < udev->descriptor.bNumConfigurations &&
  736. nleft > 0; ++cfgno) {
  737. if (cfgno < 0) {
  738. src = &udev->descriptor;
  739. srclen = sizeof(struct usb_device_descriptor);
  740. } else {
  741. src = udev->rawdescriptors[cfgno];
  742. srclen = __le16_to_cpu(udev->config[cfgno].desc.
  743. wTotalLength);
  744. }
  745. if (off < srclen) {
  746. n = min(nleft, srclen - (size_t) off);
  747. memcpy(buf, src + off, n);
  748. nleft -= n;
  749. buf += n;
  750. off = 0;
  751. } else {
  752. off -= srclen;
  753. }
  754. }
  755. return count - nleft;
  756. }
  757. static struct bin_attribute dev_bin_attr_descriptors = {
  758. .attr = {.name = "descriptors", .mode = 0444},
  759. .read = read_descriptors,
  760. .size = 18 + 65535, /* dev descr + max-size raw descriptor */
  761. };
  762. int usb_create_sysfs_dev_files(struct usb_device *udev)
  763. {
  764. struct device *dev = &udev->dev;
  765. int retval;
  766. retval = device_create_bin_file(dev, &dev_bin_attr_descriptors);
  767. if (retval)
  768. goto error;
  769. retval = add_persist_attributes(dev);
  770. if (retval)
  771. goto error;
  772. retval = add_power_attributes(dev);
  773. if (retval)
  774. goto error;
  775. return retval;
  776. error:
  777. usb_remove_sysfs_dev_files(udev);
  778. return retval;
  779. }
  780. void usb_remove_sysfs_dev_files(struct usb_device *udev)
  781. {
  782. struct device *dev = &udev->dev;
  783. remove_power_attributes(dev);
  784. remove_persist_attributes(dev);
  785. device_remove_bin_file(dev, &dev_bin_attr_descriptors);
  786. }
  787. /* Interface Association Descriptor fields */
  788. #define usb_intf_assoc_attr(field, format_string) \
  789. static ssize_t \
  790. iad_##field##_show(struct device *dev, struct device_attribute *attr, \
  791. char *buf) \
  792. { \
  793. struct usb_interface *intf = to_usb_interface(dev); \
  794. \
  795. return sprintf(buf, format_string, \
  796. intf->intf_assoc->field); \
  797. } \
  798. static DEVICE_ATTR_RO(iad_##field)
  799. usb_intf_assoc_attr(bFirstInterface, "%02x\n");
  800. usb_intf_assoc_attr(bInterfaceCount, "%02d\n");
  801. usb_intf_assoc_attr(bFunctionClass, "%02x\n");
  802. usb_intf_assoc_attr(bFunctionSubClass, "%02x\n");
  803. usb_intf_assoc_attr(bFunctionProtocol, "%02x\n");
  804. /* Interface fields */
  805. #define usb_intf_attr(field, format_string) \
  806. static ssize_t \
  807. field##_show(struct device *dev, struct device_attribute *attr, \
  808. char *buf) \
  809. { \
  810. struct usb_interface *intf = to_usb_interface(dev); \
  811. \
  812. return sprintf(buf, format_string, \
  813. intf->cur_altsetting->desc.field); \
  814. } \
  815. static DEVICE_ATTR_RO(field)
  816. usb_intf_attr(bInterfaceNumber, "%02x\n");
  817. usb_intf_attr(bAlternateSetting, "%2d\n");
  818. usb_intf_attr(bNumEndpoints, "%02x\n");
  819. usb_intf_attr(bInterfaceClass, "%02x\n");
  820. usb_intf_attr(bInterfaceSubClass, "%02x\n");
  821. usb_intf_attr(bInterfaceProtocol, "%02x\n");
  822. static ssize_t interface_show(struct device *dev, struct device_attribute *attr,
  823. char *buf)
  824. {
  825. struct usb_interface *intf;
  826. char *string;
  827. intf = to_usb_interface(dev);
  828. string = ACCESS_ONCE(intf->cur_altsetting->string);
  829. if (!string)
  830. return 0;
  831. return sprintf(buf, "%s\n", string);
  832. }
  833. static DEVICE_ATTR_RO(interface);
  834. static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
  835. char *buf)
  836. {
  837. struct usb_interface *intf;
  838. struct usb_device *udev;
  839. struct usb_host_interface *alt;
  840. intf = to_usb_interface(dev);
  841. udev = interface_to_usbdev(intf);
  842. alt = ACCESS_ONCE(intf->cur_altsetting);
  843. return sprintf(buf, "usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02X"
  844. "ic%02Xisc%02Xip%02Xin%02X\n",
  845. le16_to_cpu(udev->descriptor.idVendor),
  846. le16_to_cpu(udev->descriptor.idProduct),
  847. le16_to_cpu(udev->descriptor.bcdDevice),
  848. udev->descriptor.bDeviceClass,
  849. udev->descriptor.bDeviceSubClass,
  850. udev->descriptor.bDeviceProtocol,
  851. alt->desc.bInterfaceClass,
  852. alt->desc.bInterfaceSubClass,
  853. alt->desc.bInterfaceProtocol,
  854. alt->desc.bInterfaceNumber);
  855. }
  856. static DEVICE_ATTR_RO(modalias);
  857. static ssize_t supports_autosuspend_show(struct device *dev,
  858. struct device_attribute *attr,
  859. char *buf)
  860. {
  861. int s;
  862. s = device_lock_interruptible(dev);
  863. if (s < 0)
  864. return -EINTR;
  865. /* Devices will be autosuspended even when an interface isn't claimed */
  866. s = (!dev->driver || to_usb_driver(dev->driver)->supports_autosuspend);
  867. device_unlock(dev);
  868. return sprintf(buf, "%u\n", s);
  869. }
  870. static DEVICE_ATTR_RO(supports_autosuspend);
  871. /*
  872. * interface_authorized_show - show authorization status of an USB interface
  873. * 1 is authorized, 0 is deauthorized
  874. */
  875. static ssize_t interface_authorized_show(struct device *dev,
  876. struct device_attribute *attr, char *buf)
  877. {
  878. struct usb_interface *intf = to_usb_interface(dev);
  879. return sprintf(buf, "%u\n", intf->authorized);
  880. }
  881. /*
  882. * interface_authorized_store - authorize or deauthorize an USB interface
  883. */
  884. static ssize_t interface_authorized_store(struct device *dev,
  885. struct device_attribute *attr, const char *buf, size_t count)
  886. {
  887. struct usb_interface *intf = to_usb_interface(dev);
  888. bool val;
  889. if (strtobool(buf, &val) != 0)
  890. return -EINVAL;
  891. if (val)
  892. usb_authorize_interface(intf);
  893. else
  894. usb_deauthorize_interface(intf);
  895. return count;
  896. }
  897. static struct device_attribute dev_attr_interface_authorized =
  898. __ATTR(authorized, S_IRUGO | S_IWUSR,
  899. interface_authorized_show, interface_authorized_store);
  900. static struct attribute *intf_attrs[] = {
  901. &dev_attr_bInterfaceNumber.attr,
  902. &dev_attr_bAlternateSetting.attr,
  903. &dev_attr_bNumEndpoints.attr,
  904. &dev_attr_bInterfaceClass.attr,
  905. &dev_attr_bInterfaceSubClass.attr,
  906. &dev_attr_bInterfaceProtocol.attr,
  907. &dev_attr_modalias.attr,
  908. &dev_attr_supports_autosuspend.attr,
  909. &dev_attr_interface_authorized.attr,
  910. NULL,
  911. };
  912. static struct attribute_group intf_attr_grp = {
  913. .attrs = intf_attrs,
  914. };
  915. static struct attribute *intf_assoc_attrs[] = {
  916. &dev_attr_iad_bFirstInterface.attr,
  917. &dev_attr_iad_bInterfaceCount.attr,
  918. &dev_attr_iad_bFunctionClass.attr,
  919. &dev_attr_iad_bFunctionSubClass.attr,
  920. &dev_attr_iad_bFunctionProtocol.attr,
  921. NULL,
  922. };
  923. static umode_t intf_assoc_attrs_are_visible(struct kobject *kobj,
  924. struct attribute *a, int n)
  925. {
  926. struct device *dev = container_of(kobj, struct device, kobj);
  927. struct usb_interface *intf = to_usb_interface(dev);
  928. if (intf->intf_assoc == NULL)
  929. return 0;
  930. return a->mode;
  931. }
  932. static struct attribute_group intf_assoc_attr_grp = {
  933. .attrs = intf_assoc_attrs,
  934. .is_visible = intf_assoc_attrs_are_visible,
  935. };
  936. const struct attribute_group *usb_interface_groups[] = {
  937. &intf_attr_grp,
  938. &intf_assoc_attr_grp,
  939. NULL
  940. };
  941. void usb_create_sysfs_intf_files(struct usb_interface *intf)
  942. {
  943. struct usb_device *udev = interface_to_usbdev(intf);
  944. struct usb_host_interface *alt = intf->cur_altsetting;
  945. if (intf->sysfs_files_created || intf->unregistering)
  946. return;
  947. if (!alt->string && !(udev->quirks & USB_QUIRK_CONFIG_INTF_STRINGS))
  948. alt->string = usb_cache_string(udev, alt->desc.iInterface);
  949. if (alt->string && device_create_file(&intf->dev, &dev_attr_interface))
  950. ; /* We don't actually care if the function fails. */
  951. intf->sysfs_files_created = 1;
  952. }
  953. void usb_remove_sysfs_intf_files(struct usb_interface *intf)
  954. {
  955. if (!intf->sysfs_files_created)
  956. return;
  957. device_remove_file(&intf->dev, &dev_attr_interface);
  958. intf->sysfs_files_created = 0;
  959. }