sysfs.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  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 "usb.h"
  15. /* Active configuration fields */
  16. #define usb_actconfig_show(field, multiplier, format_string) \
  17. static ssize_t show_##field(struct device *dev, \
  18. struct device_attribute *attr, char *buf) \
  19. { \
  20. struct usb_device *udev; \
  21. struct usb_host_config *actconfig; \
  22. \
  23. udev = to_usb_device(dev); \
  24. actconfig = udev->actconfig; \
  25. if (actconfig) \
  26. return sprintf(buf, format_string, \
  27. actconfig->desc.field * multiplier); \
  28. else \
  29. return 0; \
  30. } \
  31. #define usb_actconfig_attr(field, multiplier, format_string) \
  32. usb_actconfig_show(field, multiplier, format_string) \
  33. static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
  34. usb_actconfig_attr(bNumInterfaces, 1, "%2d\n")
  35. usb_actconfig_attr(bmAttributes, 1, "%2x\n")
  36. usb_actconfig_attr(bMaxPower, 2, "%3dmA\n")
  37. static ssize_t show_configuration_string(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) || (!actconfig->string))
  45. return 0;
  46. return sprintf(buf, "%s\n", actconfig->string);
  47. }
  48. static DEVICE_ATTR(configuration, S_IRUGO, show_configuration_string, NULL);
  49. /* configuration value is always present, and r/w */
  50. usb_actconfig_show(bConfigurationValue, 1, "%u\n");
  51. static ssize_t
  52. set_bConfigurationValue(struct device *dev, struct device_attribute *attr,
  53. const char *buf, size_t count)
  54. {
  55. struct usb_device *udev = to_usb_device(dev);
  56. int config, value;
  57. if (sscanf(buf, "%d", &config) != 1 || config < -1 || config > 255)
  58. return -EINVAL;
  59. usb_lock_device(udev);
  60. value = usb_set_configuration(udev, config);
  61. usb_unlock_device(udev);
  62. return (value < 0) ? value : count;
  63. }
  64. static DEVICE_ATTR(bConfigurationValue, S_IRUGO | S_IWUSR,
  65. show_bConfigurationValue, set_bConfigurationValue);
  66. /* String fields */
  67. #define usb_string_attr(name) \
  68. static ssize_t show_##name(struct device *dev, \
  69. struct device_attribute *attr, char *buf) \
  70. { \
  71. struct usb_device *udev; \
  72. \
  73. udev = to_usb_device(dev); \
  74. return sprintf(buf, "%s\n", udev->name); \
  75. } \
  76. static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL);
  77. usb_string_attr(product);
  78. usb_string_attr(manufacturer);
  79. usb_string_attr(serial);
  80. static ssize_t
  81. show_speed(struct device *dev, struct device_attribute *attr, char *buf)
  82. {
  83. struct usb_device *udev;
  84. char *speed;
  85. udev = to_usb_device(dev);
  86. switch (udev->speed) {
  87. case USB_SPEED_LOW:
  88. speed = "1.5";
  89. break;
  90. case USB_SPEED_UNKNOWN:
  91. case USB_SPEED_FULL:
  92. speed = "12";
  93. break;
  94. case USB_SPEED_HIGH:
  95. speed = "480";
  96. break;
  97. default:
  98. speed = "unknown";
  99. }
  100. return sprintf(buf, "%s\n", speed);
  101. }
  102. static DEVICE_ATTR(speed, S_IRUGO, show_speed, NULL);
  103. static ssize_t
  104. show_busnum(struct device *dev, struct device_attribute *attr, char *buf)
  105. {
  106. struct usb_device *udev;
  107. udev = to_usb_device(dev);
  108. return sprintf(buf, "%d\n", udev->bus->busnum);
  109. }
  110. static DEVICE_ATTR(busnum, S_IRUGO, show_busnum, NULL);
  111. static ssize_t
  112. show_devnum(struct device *dev, struct device_attribute *attr, char *buf)
  113. {
  114. struct usb_device *udev;
  115. udev = to_usb_device(dev);
  116. return sprintf(buf, "%d\n", udev->devnum);
  117. }
  118. static DEVICE_ATTR(devnum, S_IRUGO, show_devnum, NULL);
  119. static ssize_t
  120. show_version(struct device *dev, struct device_attribute *attr, char *buf)
  121. {
  122. struct usb_device *udev;
  123. u16 bcdUSB;
  124. udev = to_usb_device(dev);
  125. bcdUSB = le16_to_cpu(udev->descriptor.bcdUSB);
  126. return sprintf(buf, "%2x.%02x\n", bcdUSB >> 8, bcdUSB & 0xff);
  127. }
  128. static DEVICE_ATTR(version, S_IRUGO, show_version, NULL);
  129. static ssize_t
  130. show_maxchild(struct device *dev, struct device_attribute *attr, char *buf)
  131. {
  132. struct usb_device *udev;
  133. udev = to_usb_device(dev);
  134. return sprintf(buf, "%d\n", udev->maxchild);
  135. }
  136. static DEVICE_ATTR(maxchild, S_IRUGO, show_maxchild, NULL);
  137. static ssize_t
  138. show_quirks(struct device *dev, struct device_attribute *attr, char *buf)
  139. {
  140. struct usb_device *udev;
  141. udev = to_usb_device(dev);
  142. return sprintf(buf, "0x%x\n", udev->quirks);
  143. }
  144. static DEVICE_ATTR(quirks, S_IRUGO, show_quirks, NULL);
  145. #if defined(CONFIG_USB_PERSIST) || defined(CONFIG_USB_SUSPEND)
  146. static const char power_group[] = "power";
  147. #endif
  148. #ifdef CONFIG_USB_PERSIST
  149. static ssize_t
  150. show_persist(struct device *dev, struct device_attribute *attr, char *buf)
  151. {
  152. struct usb_device *udev = to_usb_device(dev);
  153. return sprintf(buf, "%d\n", udev->persist_enabled);
  154. }
  155. static ssize_t
  156. set_persist(struct device *dev, struct device_attribute *attr,
  157. const char *buf, size_t count)
  158. {
  159. struct usb_device *udev = to_usb_device(dev);
  160. int value;
  161. /* Hubs are always enabled for USB_PERSIST */
  162. if (udev->descriptor.bDeviceClass == USB_CLASS_HUB)
  163. return -EPERM;
  164. if (sscanf(buf, "%d", &value) != 1)
  165. return -EINVAL;
  166. usb_pm_lock(udev);
  167. udev->persist_enabled = !!value;
  168. usb_pm_unlock(udev);
  169. return count;
  170. }
  171. static DEVICE_ATTR(persist, S_IRUGO | S_IWUSR, show_persist, set_persist);
  172. static int add_persist_attributes(struct device *dev)
  173. {
  174. int rc = 0;
  175. if (is_usb_device(dev)) {
  176. struct usb_device *udev = to_usb_device(dev);
  177. /* Hubs are automatically enabled for USB_PERSIST */
  178. if (udev->descriptor.bDeviceClass == USB_CLASS_HUB)
  179. udev->persist_enabled = 1;
  180. rc = sysfs_add_file_to_group(&dev->kobj,
  181. &dev_attr_persist.attr,
  182. power_group);
  183. }
  184. return rc;
  185. }
  186. static void remove_persist_attributes(struct device *dev)
  187. {
  188. sysfs_remove_file_from_group(&dev->kobj,
  189. &dev_attr_persist.attr,
  190. power_group);
  191. }
  192. #else
  193. #define add_persist_attributes(dev) 0
  194. #define remove_persist_attributes(dev) do {} while (0)
  195. #endif /* CONFIG_USB_PERSIST */
  196. #ifdef CONFIG_USB_SUSPEND
  197. static ssize_t
  198. show_autosuspend(struct device *dev, struct device_attribute *attr, char *buf)
  199. {
  200. struct usb_device *udev = to_usb_device(dev);
  201. return sprintf(buf, "%d\n", udev->autosuspend_delay / HZ);
  202. }
  203. static ssize_t
  204. set_autosuspend(struct device *dev, struct device_attribute *attr,
  205. const char *buf, size_t count)
  206. {
  207. struct usb_device *udev = to_usb_device(dev);
  208. int value;
  209. if (sscanf(buf, "%d", &value) != 1 || value >= INT_MAX/HZ ||
  210. value <= - INT_MAX/HZ)
  211. return -EINVAL;
  212. value *= HZ;
  213. udev->autosuspend_delay = value;
  214. if (value >= 0)
  215. usb_try_autosuspend_device(udev);
  216. else {
  217. if (usb_autoresume_device(udev) == 0)
  218. usb_autosuspend_device(udev);
  219. }
  220. return count;
  221. }
  222. static DEVICE_ATTR(autosuspend, S_IRUGO | S_IWUSR,
  223. show_autosuspend, set_autosuspend);
  224. static const char on_string[] = "on";
  225. static const char auto_string[] = "auto";
  226. static const char suspend_string[] = "suspend";
  227. static ssize_t
  228. show_level(struct device *dev, struct device_attribute *attr, char *buf)
  229. {
  230. struct usb_device *udev = to_usb_device(dev);
  231. const char *p = auto_string;
  232. if (udev->state == USB_STATE_SUSPENDED) {
  233. if (udev->autoresume_disabled)
  234. p = suspend_string;
  235. } else {
  236. if (udev->autosuspend_disabled)
  237. p = on_string;
  238. }
  239. return sprintf(buf, "%s\n", p);
  240. }
  241. static ssize_t
  242. set_level(struct device *dev, struct device_attribute *attr,
  243. const char *buf, size_t count)
  244. {
  245. struct usb_device *udev = to_usb_device(dev);
  246. int len = count;
  247. char *cp;
  248. int rc = 0;
  249. int old_autosuspend_disabled, old_autoresume_disabled;
  250. cp = memchr(buf, '\n', count);
  251. if (cp)
  252. len = cp - buf;
  253. usb_lock_device(udev);
  254. old_autosuspend_disabled = udev->autosuspend_disabled;
  255. old_autoresume_disabled = udev->autoresume_disabled;
  256. /* Setting the flags without calling usb_pm_lock is a subject to
  257. * races, but who cares...
  258. */
  259. if (len == sizeof on_string - 1 &&
  260. strncmp(buf, on_string, len) == 0) {
  261. udev->autosuspend_disabled = 1;
  262. udev->autoresume_disabled = 0;
  263. rc = usb_external_resume_device(udev);
  264. } else if (len == sizeof auto_string - 1 &&
  265. strncmp(buf, auto_string, len) == 0) {
  266. udev->autosuspend_disabled = 0;
  267. udev->autoresume_disabled = 0;
  268. rc = usb_external_resume_device(udev);
  269. } else if (len == sizeof suspend_string - 1 &&
  270. strncmp(buf, suspend_string, len) == 0) {
  271. udev->autosuspend_disabled = 0;
  272. udev->autoresume_disabled = 1;
  273. rc = usb_external_suspend_device(udev, PMSG_SUSPEND);
  274. } else
  275. rc = -EINVAL;
  276. if (rc) {
  277. udev->autosuspend_disabled = old_autosuspend_disabled;
  278. udev->autoresume_disabled = old_autoresume_disabled;
  279. }
  280. usb_unlock_device(udev);
  281. return (rc < 0 ? rc : count);
  282. }
  283. static DEVICE_ATTR(level, S_IRUGO | S_IWUSR, show_level, set_level);
  284. static int add_power_attributes(struct device *dev)
  285. {
  286. int rc = 0;
  287. if (is_usb_device(dev)) {
  288. rc = sysfs_add_file_to_group(&dev->kobj,
  289. &dev_attr_autosuspend.attr,
  290. power_group);
  291. if (rc == 0)
  292. rc = sysfs_add_file_to_group(&dev->kobj,
  293. &dev_attr_level.attr,
  294. power_group);
  295. }
  296. return rc;
  297. }
  298. static void remove_power_attributes(struct device *dev)
  299. {
  300. sysfs_remove_file_from_group(&dev->kobj,
  301. &dev_attr_level.attr,
  302. power_group);
  303. sysfs_remove_file_from_group(&dev->kobj,
  304. &dev_attr_autosuspend.attr,
  305. power_group);
  306. }
  307. #else
  308. #define add_power_attributes(dev) 0
  309. #define remove_power_attributes(dev) do {} while (0)
  310. #endif /* CONFIG_USB_SUSPEND */
  311. /* Descriptor fields */
  312. #define usb_descriptor_attr_le16(field, format_string) \
  313. static ssize_t \
  314. show_##field(struct device *dev, struct device_attribute *attr, \
  315. char *buf) \
  316. { \
  317. struct usb_device *udev; \
  318. \
  319. udev = to_usb_device(dev); \
  320. return sprintf(buf, format_string, \
  321. le16_to_cpu(udev->descriptor.field)); \
  322. } \
  323. static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
  324. usb_descriptor_attr_le16(idVendor, "%04x\n")
  325. usb_descriptor_attr_le16(idProduct, "%04x\n")
  326. usb_descriptor_attr_le16(bcdDevice, "%04x\n")
  327. #define usb_descriptor_attr(field, format_string) \
  328. static ssize_t \
  329. show_##field(struct device *dev, struct device_attribute *attr, \
  330. char *buf) \
  331. { \
  332. struct usb_device *udev; \
  333. \
  334. udev = to_usb_device(dev); \
  335. return sprintf(buf, format_string, udev->descriptor.field); \
  336. } \
  337. static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
  338. usb_descriptor_attr(bDeviceClass, "%02x\n")
  339. usb_descriptor_attr(bDeviceSubClass, "%02x\n")
  340. usb_descriptor_attr(bDeviceProtocol, "%02x\n")
  341. usb_descriptor_attr(bNumConfigurations, "%d\n")
  342. usb_descriptor_attr(bMaxPacketSize0, "%d\n")
  343. static struct attribute *dev_attrs[] = {
  344. /* current configuration's attributes */
  345. &dev_attr_configuration.attr,
  346. &dev_attr_bNumInterfaces.attr,
  347. &dev_attr_bConfigurationValue.attr,
  348. &dev_attr_bmAttributes.attr,
  349. &dev_attr_bMaxPower.attr,
  350. /* device attributes */
  351. &dev_attr_idVendor.attr,
  352. &dev_attr_idProduct.attr,
  353. &dev_attr_bcdDevice.attr,
  354. &dev_attr_bDeviceClass.attr,
  355. &dev_attr_bDeviceSubClass.attr,
  356. &dev_attr_bDeviceProtocol.attr,
  357. &dev_attr_bNumConfigurations.attr,
  358. &dev_attr_bMaxPacketSize0.attr,
  359. &dev_attr_speed.attr,
  360. &dev_attr_busnum.attr,
  361. &dev_attr_devnum.attr,
  362. &dev_attr_version.attr,
  363. &dev_attr_maxchild.attr,
  364. &dev_attr_quirks.attr,
  365. NULL,
  366. };
  367. static struct attribute_group dev_attr_grp = {
  368. .attrs = dev_attrs,
  369. };
  370. /* Binary descriptors */
  371. static ssize_t
  372. read_descriptors(struct kobject *kobj, struct bin_attribute *attr,
  373. char *buf, loff_t off, size_t count)
  374. {
  375. struct usb_device *udev = to_usb_device(
  376. container_of(kobj, struct device, kobj));
  377. size_t nleft = count;
  378. size_t srclen, n;
  379. usb_lock_device(udev);
  380. /* The binary attribute begins with the device descriptor */
  381. srclen = sizeof(struct usb_device_descriptor);
  382. if (off < srclen) {
  383. n = min_t(size_t, nleft, srclen - off);
  384. memcpy(buf, off + (char *) &udev->descriptor, n);
  385. nleft -= n;
  386. buf += n;
  387. off = 0;
  388. } else {
  389. off -= srclen;
  390. }
  391. /* Then follows the raw descriptor entry for the current
  392. * configuration (config plus subsidiary descriptors).
  393. */
  394. if (udev->actconfig) {
  395. int cfgno = udev->actconfig - udev->config;
  396. srclen = __le16_to_cpu(udev->actconfig->desc.wTotalLength);
  397. if (off < srclen) {
  398. n = min_t(size_t, nleft, srclen - off);
  399. memcpy(buf, off + udev->rawdescriptors[cfgno], n);
  400. nleft -= n;
  401. }
  402. }
  403. usb_unlock_device(udev);
  404. return count - nleft;
  405. }
  406. static struct bin_attribute dev_bin_attr_descriptors = {
  407. .attr = {.name = "descriptors", .mode = 0444},
  408. .read = read_descriptors,
  409. .size = 18 + 65535, /* dev descr + max-size raw descriptor */
  410. };
  411. int usb_create_sysfs_dev_files(struct usb_device *udev)
  412. {
  413. struct device *dev = &udev->dev;
  414. int retval;
  415. retval = sysfs_create_group(&dev->kobj, &dev_attr_grp);
  416. if (retval)
  417. return retval;
  418. retval = device_create_bin_file(dev, &dev_bin_attr_descriptors);
  419. if (retval)
  420. goto error;
  421. retval = add_persist_attributes(dev);
  422. if (retval)
  423. goto error;
  424. retval = add_power_attributes(dev);
  425. if (retval)
  426. goto error;
  427. if (udev->manufacturer) {
  428. retval = device_create_file(dev, &dev_attr_manufacturer);
  429. if (retval)
  430. goto error;
  431. }
  432. if (udev->product) {
  433. retval = device_create_file(dev, &dev_attr_product);
  434. if (retval)
  435. goto error;
  436. }
  437. if (udev->serial) {
  438. retval = device_create_file(dev, &dev_attr_serial);
  439. if (retval)
  440. goto error;
  441. }
  442. retval = usb_create_ep_files(dev, &udev->ep0, udev);
  443. if (retval)
  444. goto error;
  445. return 0;
  446. error:
  447. usb_remove_sysfs_dev_files(udev);
  448. return retval;
  449. }
  450. void usb_remove_sysfs_dev_files(struct usb_device *udev)
  451. {
  452. struct device *dev = &udev->dev;
  453. usb_remove_ep_files(&udev->ep0);
  454. device_remove_file(dev, &dev_attr_manufacturer);
  455. device_remove_file(dev, &dev_attr_product);
  456. device_remove_file(dev, &dev_attr_serial);
  457. remove_power_attributes(dev);
  458. remove_persist_attributes(dev);
  459. device_remove_bin_file(dev, &dev_bin_attr_descriptors);
  460. sysfs_remove_group(&dev->kobj, &dev_attr_grp);
  461. }
  462. /* Interface Accociation Descriptor fields */
  463. #define usb_intf_assoc_attr(field, format_string) \
  464. static ssize_t \
  465. show_iad_##field (struct device *dev, struct device_attribute *attr, \
  466. char *buf) \
  467. { \
  468. struct usb_interface *intf = to_usb_interface (dev); \
  469. \
  470. return sprintf (buf, format_string, \
  471. intf->intf_assoc->field); \
  472. } \
  473. static DEVICE_ATTR(iad_##field, S_IRUGO, show_iad_##field, NULL);
  474. usb_intf_assoc_attr (bFirstInterface, "%02x\n")
  475. usb_intf_assoc_attr (bInterfaceCount, "%02d\n")
  476. usb_intf_assoc_attr (bFunctionClass, "%02x\n")
  477. usb_intf_assoc_attr (bFunctionSubClass, "%02x\n")
  478. usb_intf_assoc_attr (bFunctionProtocol, "%02x\n")
  479. /* Interface fields */
  480. #define usb_intf_attr(field, format_string) \
  481. static ssize_t \
  482. show_##field(struct device *dev, struct device_attribute *attr, \
  483. char *buf) \
  484. { \
  485. struct usb_interface *intf = to_usb_interface(dev); \
  486. \
  487. return sprintf(buf, format_string, \
  488. intf->cur_altsetting->desc.field); \
  489. } \
  490. static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
  491. usb_intf_attr(bInterfaceNumber, "%02x\n")
  492. usb_intf_attr(bAlternateSetting, "%2d\n")
  493. usb_intf_attr(bNumEndpoints, "%02x\n")
  494. usb_intf_attr(bInterfaceClass, "%02x\n")
  495. usb_intf_attr(bInterfaceSubClass, "%02x\n")
  496. usb_intf_attr(bInterfaceProtocol, "%02x\n")
  497. static ssize_t show_interface_string(struct device *dev,
  498. struct device_attribute *attr, char *buf)
  499. {
  500. struct usb_interface *intf;
  501. struct usb_device *udev;
  502. int len;
  503. intf = to_usb_interface(dev);
  504. udev = interface_to_usbdev(intf);
  505. len = snprintf(buf, 256, "%s", intf->cur_altsetting->string);
  506. if (len < 0)
  507. return 0;
  508. buf[len] = '\n';
  509. buf[len+1] = 0;
  510. return len+1;
  511. }
  512. static DEVICE_ATTR(interface, S_IRUGO, show_interface_string, NULL);
  513. static ssize_t show_modalias(struct device *dev,
  514. struct device_attribute *attr, char *buf)
  515. {
  516. struct usb_interface *intf;
  517. struct usb_device *udev;
  518. struct usb_host_interface *alt;
  519. intf = to_usb_interface(dev);
  520. udev = interface_to_usbdev(intf);
  521. alt = intf->cur_altsetting;
  522. return sprintf(buf, "usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02X"
  523. "ic%02Xisc%02Xip%02X\n",
  524. le16_to_cpu(udev->descriptor.idVendor),
  525. le16_to_cpu(udev->descriptor.idProduct),
  526. le16_to_cpu(udev->descriptor.bcdDevice),
  527. udev->descriptor.bDeviceClass,
  528. udev->descriptor.bDeviceSubClass,
  529. udev->descriptor.bDeviceProtocol,
  530. alt->desc.bInterfaceClass,
  531. alt->desc.bInterfaceSubClass,
  532. alt->desc.bInterfaceProtocol);
  533. }
  534. static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
  535. static struct attribute *intf_assoc_attrs[] = {
  536. &dev_attr_iad_bFirstInterface.attr,
  537. &dev_attr_iad_bInterfaceCount.attr,
  538. &dev_attr_iad_bFunctionClass.attr,
  539. &dev_attr_iad_bFunctionSubClass.attr,
  540. &dev_attr_iad_bFunctionProtocol.attr,
  541. NULL,
  542. };
  543. static struct attribute_group intf_assoc_attr_grp = {
  544. .attrs = intf_assoc_attrs,
  545. };
  546. static struct attribute *intf_attrs[] = {
  547. &dev_attr_bInterfaceNumber.attr,
  548. &dev_attr_bAlternateSetting.attr,
  549. &dev_attr_bNumEndpoints.attr,
  550. &dev_attr_bInterfaceClass.attr,
  551. &dev_attr_bInterfaceSubClass.attr,
  552. &dev_attr_bInterfaceProtocol.attr,
  553. &dev_attr_modalias.attr,
  554. NULL,
  555. };
  556. static struct attribute_group intf_attr_grp = {
  557. .attrs = intf_attrs,
  558. };
  559. static inline void usb_create_intf_ep_files(struct usb_interface *intf,
  560. struct usb_device *udev)
  561. {
  562. struct usb_host_interface *iface_desc;
  563. int i;
  564. iface_desc = intf->cur_altsetting;
  565. for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i)
  566. usb_create_ep_files(&intf->dev, &iface_desc->endpoint[i],
  567. udev);
  568. }
  569. static inline void usb_remove_intf_ep_files(struct usb_interface *intf)
  570. {
  571. struct usb_host_interface *iface_desc;
  572. int i;
  573. iface_desc = intf->cur_altsetting;
  574. for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i)
  575. usb_remove_ep_files(&iface_desc->endpoint[i]);
  576. }
  577. int usb_create_sysfs_intf_files(struct usb_interface *intf)
  578. {
  579. struct device *dev = &intf->dev;
  580. struct usb_device *udev = interface_to_usbdev(intf);
  581. struct usb_host_interface *alt = intf->cur_altsetting;
  582. int retval;
  583. retval = sysfs_create_group(&dev->kobj, &intf_attr_grp);
  584. if (retval)
  585. return retval;
  586. if (alt->string == NULL)
  587. alt->string = usb_cache_string(udev, alt->desc.iInterface);
  588. if (alt->string)
  589. retval = device_create_file(dev, &dev_attr_interface);
  590. if (intf->intf_assoc)
  591. retval = sysfs_create_group(&dev->kobj, &intf_assoc_attr_grp);
  592. usb_create_intf_ep_files(intf, udev);
  593. return 0;
  594. }
  595. void usb_remove_sysfs_intf_files(struct usb_interface *intf)
  596. {
  597. struct device *dev = &intf->dev;
  598. usb_remove_intf_ep_files(intf);
  599. device_remove_file(dev, &dev_attr_interface);
  600. sysfs_remove_group(&dev->kobj, &intf_attr_grp);
  601. sysfs_remove_group(&intf->dev.kobj, &intf_assoc_attr_grp);
  602. }