sysfs.c 25 KB

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