sysfs.c 23 KB

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