sysfs.c 22 KB

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