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