sysfs.c 27 KB

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