sysfs.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  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. static ssize_t
  146. show_urbnum(struct device *dev, struct device_attribute *attr, char *buf)
  147. {
  148. struct usb_device *udev;
  149. udev = to_usb_device(dev);
  150. return sprintf(buf, "%d\n", atomic_read(&udev->urbnum));
  151. }
  152. static DEVICE_ATTR(urbnum, S_IRUGO, show_urbnum, NULL);
  153. #if defined(CONFIG_USB_PERSIST) || defined(CONFIG_USB_SUSPEND)
  154. static const char power_group[] = "power";
  155. #endif
  156. #ifdef CONFIG_USB_PERSIST
  157. static ssize_t
  158. show_persist(struct device *dev, struct device_attribute *attr, char *buf)
  159. {
  160. struct usb_device *udev = to_usb_device(dev);
  161. return sprintf(buf, "%d\n", udev->persist_enabled);
  162. }
  163. static ssize_t
  164. set_persist(struct device *dev, struct device_attribute *attr,
  165. const char *buf, size_t count)
  166. {
  167. struct usb_device *udev = to_usb_device(dev);
  168. int value;
  169. /* Hubs are always enabled for USB_PERSIST */
  170. if (udev->descriptor.bDeviceClass == USB_CLASS_HUB)
  171. return -EPERM;
  172. if (sscanf(buf, "%d", &value) != 1)
  173. return -EINVAL;
  174. usb_pm_lock(udev);
  175. udev->persist_enabled = !!value;
  176. usb_pm_unlock(udev);
  177. return count;
  178. }
  179. static DEVICE_ATTR(persist, S_IRUGO | S_IWUSR, show_persist, set_persist);
  180. static int add_persist_attributes(struct device *dev)
  181. {
  182. int rc = 0;
  183. if (is_usb_device(dev)) {
  184. struct usb_device *udev = to_usb_device(dev);
  185. /* Hubs are automatically enabled for USB_PERSIST */
  186. if (udev->descriptor.bDeviceClass == USB_CLASS_HUB)
  187. udev->persist_enabled = 1;
  188. rc = sysfs_add_file_to_group(&dev->kobj,
  189. &dev_attr_persist.attr,
  190. power_group);
  191. }
  192. return rc;
  193. }
  194. static void remove_persist_attributes(struct device *dev)
  195. {
  196. sysfs_remove_file_from_group(&dev->kobj,
  197. &dev_attr_persist.attr,
  198. power_group);
  199. }
  200. #else
  201. #define add_persist_attributes(dev) 0
  202. #define remove_persist_attributes(dev) do {} while (0)
  203. #endif /* CONFIG_USB_PERSIST */
  204. #ifdef CONFIG_USB_SUSPEND
  205. static ssize_t
  206. show_autosuspend(struct device *dev, struct device_attribute *attr, char *buf)
  207. {
  208. struct usb_device *udev = to_usb_device(dev);
  209. return sprintf(buf, "%d\n", udev->autosuspend_delay / HZ);
  210. }
  211. static ssize_t
  212. set_autosuspend(struct device *dev, struct device_attribute *attr,
  213. const char *buf, size_t count)
  214. {
  215. struct usb_device *udev = to_usb_device(dev);
  216. int value;
  217. if (sscanf(buf, "%d", &value) != 1 || value >= INT_MAX/HZ ||
  218. value <= - INT_MAX/HZ)
  219. return -EINVAL;
  220. value *= HZ;
  221. udev->autosuspend_delay = value;
  222. if (value >= 0)
  223. usb_try_autosuspend_device(udev);
  224. else {
  225. if (usb_autoresume_device(udev) == 0)
  226. usb_autosuspend_device(udev);
  227. }
  228. return count;
  229. }
  230. static DEVICE_ATTR(autosuspend, S_IRUGO | S_IWUSR,
  231. show_autosuspend, set_autosuspend);
  232. static const char on_string[] = "on";
  233. static const char auto_string[] = "auto";
  234. static const char suspend_string[] = "suspend";
  235. static ssize_t
  236. show_level(struct device *dev, struct device_attribute *attr, char *buf)
  237. {
  238. struct usb_device *udev = to_usb_device(dev);
  239. const char *p = auto_string;
  240. if (udev->state == USB_STATE_SUSPENDED) {
  241. if (udev->autoresume_disabled)
  242. p = suspend_string;
  243. } else {
  244. if (udev->autosuspend_disabled)
  245. p = on_string;
  246. }
  247. return sprintf(buf, "%s\n", p);
  248. }
  249. static ssize_t
  250. set_level(struct device *dev, struct device_attribute *attr,
  251. const char *buf, size_t count)
  252. {
  253. struct usb_device *udev = to_usb_device(dev);
  254. int len = count;
  255. char *cp;
  256. int rc = 0;
  257. int old_autosuspend_disabled, old_autoresume_disabled;
  258. cp = memchr(buf, '\n', count);
  259. if (cp)
  260. len = cp - buf;
  261. usb_lock_device(udev);
  262. old_autosuspend_disabled = udev->autosuspend_disabled;
  263. old_autoresume_disabled = udev->autoresume_disabled;
  264. /* Setting the flags without calling usb_pm_lock is a subject to
  265. * races, but who cares...
  266. */
  267. if (len == sizeof on_string - 1 &&
  268. strncmp(buf, on_string, len) == 0) {
  269. udev->autosuspend_disabled = 1;
  270. udev->autoresume_disabled = 0;
  271. rc = usb_external_resume_device(udev);
  272. } else if (len == sizeof auto_string - 1 &&
  273. strncmp(buf, auto_string, len) == 0) {
  274. udev->autosuspend_disabled = 0;
  275. udev->autoresume_disabled = 0;
  276. rc = usb_external_resume_device(udev);
  277. } else if (len == sizeof suspend_string - 1 &&
  278. strncmp(buf, suspend_string, len) == 0) {
  279. udev->autosuspend_disabled = 0;
  280. udev->autoresume_disabled = 1;
  281. rc = usb_external_suspend_device(udev, PMSG_SUSPEND);
  282. } else
  283. rc = -EINVAL;
  284. if (rc) {
  285. udev->autosuspend_disabled = old_autosuspend_disabled;
  286. udev->autoresume_disabled = old_autoresume_disabled;
  287. }
  288. usb_unlock_device(udev);
  289. return (rc < 0 ? rc : count);
  290. }
  291. static DEVICE_ATTR(level, S_IRUGO | S_IWUSR, show_level, set_level);
  292. static int add_power_attributes(struct device *dev)
  293. {
  294. int rc = 0;
  295. if (is_usb_device(dev)) {
  296. rc = sysfs_add_file_to_group(&dev->kobj,
  297. &dev_attr_autosuspend.attr,
  298. power_group);
  299. if (rc == 0)
  300. rc = sysfs_add_file_to_group(&dev->kobj,
  301. &dev_attr_level.attr,
  302. power_group);
  303. }
  304. return rc;
  305. }
  306. static void remove_power_attributes(struct device *dev)
  307. {
  308. sysfs_remove_file_from_group(&dev->kobj,
  309. &dev_attr_level.attr,
  310. power_group);
  311. sysfs_remove_file_from_group(&dev->kobj,
  312. &dev_attr_autosuspend.attr,
  313. power_group);
  314. }
  315. #else
  316. #define add_power_attributes(dev) 0
  317. #define remove_power_attributes(dev) do {} while (0)
  318. #endif /* CONFIG_USB_SUSPEND */
  319. /* Descriptor fields */
  320. #define usb_descriptor_attr_le16(field, format_string) \
  321. static ssize_t \
  322. show_##field(struct device *dev, struct device_attribute *attr, \
  323. char *buf) \
  324. { \
  325. struct usb_device *udev; \
  326. \
  327. udev = to_usb_device(dev); \
  328. return sprintf(buf, format_string, \
  329. le16_to_cpu(udev->descriptor.field)); \
  330. } \
  331. static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
  332. usb_descriptor_attr_le16(idVendor, "%04x\n")
  333. usb_descriptor_attr_le16(idProduct, "%04x\n")
  334. usb_descriptor_attr_le16(bcdDevice, "%04x\n")
  335. #define usb_descriptor_attr(field, format_string) \
  336. static ssize_t \
  337. show_##field(struct device *dev, struct device_attribute *attr, \
  338. char *buf) \
  339. { \
  340. struct usb_device *udev; \
  341. \
  342. udev = to_usb_device(dev); \
  343. return sprintf(buf, format_string, udev->descriptor.field); \
  344. } \
  345. static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
  346. usb_descriptor_attr(bDeviceClass, "%02x\n")
  347. usb_descriptor_attr(bDeviceSubClass, "%02x\n")
  348. usb_descriptor_attr(bDeviceProtocol, "%02x\n")
  349. usb_descriptor_attr(bNumConfigurations, "%d\n")
  350. usb_descriptor_attr(bMaxPacketSize0, "%d\n")
  351. /* show if the device is authorized (1) or not (0) */
  352. static ssize_t usb_dev_authorized_show(struct device *dev,
  353. struct device_attribute *attr,
  354. char *buf)
  355. {
  356. struct usb_device *usb_dev = to_usb_device(dev);
  357. return snprintf(buf, PAGE_SIZE, "%u\n", usb_dev->authorized);
  358. }
  359. /*
  360. * Authorize a device to be used in the system
  361. *
  362. * Writing a 0 deauthorizes the device, writing a 1 authorizes it.
  363. */
  364. static ssize_t usb_dev_authorized_store(struct device *dev,
  365. struct device_attribute *attr,
  366. const char *buf, size_t size)
  367. {
  368. ssize_t result;
  369. struct usb_device *usb_dev = to_usb_device(dev);
  370. unsigned val;
  371. result = sscanf(buf, "%u\n", &val);
  372. if (result != 1)
  373. result = -EINVAL;
  374. else if (val == 0)
  375. result = usb_deauthorize_device(usb_dev);
  376. else
  377. result = usb_authorize_device(usb_dev);
  378. return result < 0? result : size;
  379. }
  380. static DEVICE_ATTR(authorized, 0644,
  381. usb_dev_authorized_show, usb_dev_authorized_store);
  382. static struct attribute *dev_attrs[] = {
  383. /* current configuration's attributes */
  384. &dev_attr_configuration.attr,
  385. &dev_attr_bNumInterfaces.attr,
  386. &dev_attr_bConfigurationValue.attr,
  387. &dev_attr_bmAttributes.attr,
  388. &dev_attr_bMaxPower.attr,
  389. &dev_attr_urbnum.attr,
  390. /* device attributes */
  391. &dev_attr_idVendor.attr,
  392. &dev_attr_idProduct.attr,
  393. &dev_attr_bcdDevice.attr,
  394. &dev_attr_bDeviceClass.attr,
  395. &dev_attr_bDeviceSubClass.attr,
  396. &dev_attr_bDeviceProtocol.attr,
  397. &dev_attr_bNumConfigurations.attr,
  398. &dev_attr_bMaxPacketSize0.attr,
  399. &dev_attr_speed.attr,
  400. &dev_attr_busnum.attr,
  401. &dev_attr_devnum.attr,
  402. &dev_attr_version.attr,
  403. &dev_attr_maxchild.attr,
  404. &dev_attr_quirks.attr,
  405. &dev_attr_authorized.attr,
  406. NULL,
  407. };
  408. static struct attribute_group dev_attr_grp = {
  409. .attrs = dev_attrs,
  410. };
  411. /* Binary descriptors */
  412. static ssize_t
  413. read_descriptors(struct kobject *kobj, struct bin_attribute *attr,
  414. char *buf, loff_t off, size_t count)
  415. {
  416. struct usb_device *udev = to_usb_device(
  417. container_of(kobj, struct device, kobj));
  418. size_t nleft = count;
  419. size_t srclen, n;
  420. usb_lock_device(udev);
  421. /* The binary attribute begins with the device descriptor */
  422. srclen = sizeof(struct usb_device_descriptor);
  423. if (off < srclen) {
  424. n = min_t(size_t, nleft, srclen - off);
  425. memcpy(buf, off + (char *) &udev->descriptor, n);
  426. nleft -= n;
  427. buf += n;
  428. off = 0;
  429. } else {
  430. off -= srclen;
  431. }
  432. /* Then follows the raw descriptor entry for the current
  433. * configuration (config plus subsidiary descriptors).
  434. */
  435. if (udev->actconfig) {
  436. int cfgno = udev->actconfig - udev->config;
  437. srclen = __le16_to_cpu(udev->actconfig->desc.wTotalLength);
  438. if (off < srclen) {
  439. n = min_t(size_t, nleft, srclen - off);
  440. memcpy(buf, off + udev->rawdescriptors[cfgno], n);
  441. nleft -= n;
  442. }
  443. }
  444. usb_unlock_device(udev);
  445. return count - nleft;
  446. }
  447. static struct bin_attribute dev_bin_attr_descriptors = {
  448. .attr = {.name = "descriptors", .mode = 0444},
  449. .read = read_descriptors,
  450. .size = 18 + 65535, /* dev descr + max-size raw descriptor */
  451. };
  452. int usb_create_sysfs_dev_files(struct usb_device *udev)
  453. {
  454. struct device *dev = &udev->dev;
  455. int retval;
  456. retval = sysfs_create_group(&dev->kobj, &dev_attr_grp);
  457. if (retval)
  458. return retval;
  459. retval = device_create_bin_file(dev, &dev_bin_attr_descriptors);
  460. if (retval)
  461. goto error;
  462. retval = add_persist_attributes(dev);
  463. if (retval)
  464. goto error;
  465. retval = add_power_attributes(dev);
  466. if (retval)
  467. goto error;
  468. if (udev->manufacturer) {
  469. retval = device_create_file(dev, &dev_attr_manufacturer);
  470. if (retval)
  471. goto error;
  472. }
  473. if (udev->product) {
  474. retval = device_create_file(dev, &dev_attr_product);
  475. if (retval)
  476. goto error;
  477. }
  478. if (udev->serial) {
  479. retval = device_create_file(dev, &dev_attr_serial);
  480. if (retval)
  481. goto error;
  482. }
  483. retval = usb_create_ep_files(dev, &udev->ep0, udev);
  484. if (retval)
  485. goto error;
  486. return 0;
  487. error:
  488. usb_remove_sysfs_dev_files(udev);
  489. return retval;
  490. }
  491. void usb_remove_sysfs_dev_files(struct usb_device *udev)
  492. {
  493. struct device *dev = &udev->dev;
  494. usb_remove_ep_files(&udev->ep0);
  495. device_remove_file(dev, &dev_attr_manufacturer);
  496. device_remove_file(dev, &dev_attr_product);
  497. device_remove_file(dev, &dev_attr_serial);
  498. remove_power_attributes(dev);
  499. remove_persist_attributes(dev);
  500. device_remove_bin_file(dev, &dev_bin_attr_descriptors);
  501. sysfs_remove_group(&dev->kobj, &dev_attr_grp);
  502. }
  503. /* Interface Accociation Descriptor fields */
  504. #define usb_intf_assoc_attr(field, format_string) \
  505. static ssize_t \
  506. show_iad_##field (struct device *dev, struct device_attribute *attr, \
  507. char *buf) \
  508. { \
  509. struct usb_interface *intf = to_usb_interface (dev); \
  510. \
  511. return sprintf (buf, format_string, \
  512. intf->intf_assoc->field); \
  513. } \
  514. static DEVICE_ATTR(iad_##field, S_IRUGO, show_iad_##field, NULL);
  515. usb_intf_assoc_attr (bFirstInterface, "%02x\n")
  516. usb_intf_assoc_attr (bInterfaceCount, "%02d\n")
  517. usb_intf_assoc_attr (bFunctionClass, "%02x\n")
  518. usb_intf_assoc_attr (bFunctionSubClass, "%02x\n")
  519. usb_intf_assoc_attr (bFunctionProtocol, "%02x\n")
  520. /* Interface fields */
  521. #define usb_intf_attr(field, format_string) \
  522. static ssize_t \
  523. show_##field(struct device *dev, struct device_attribute *attr, \
  524. char *buf) \
  525. { \
  526. struct usb_interface *intf = to_usb_interface(dev); \
  527. \
  528. return sprintf(buf, format_string, \
  529. intf->cur_altsetting->desc.field); \
  530. } \
  531. static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
  532. usb_intf_attr(bInterfaceNumber, "%02x\n")
  533. usb_intf_attr(bAlternateSetting, "%2d\n")
  534. usb_intf_attr(bNumEndpoints, "%02x\n")
  535. usb_intf_attr(bInterfaceClass, "%02x\n")
  536. usb_intf_attr(bInterfaceSubClass, "%02x\n")
  537. usb_intf_attr(bInterfaceProtocol, "%02x\n")
  538. static ssize_t show_interface_string(struct device *dev,
  539. struct device_attribute *attr, char *buf)
  540. {
  541. struct usb_interface *intf;
  542. struct usb_device *udev;
  543. int len;
  544. intf = to_usb_interface(dev);
  545. udev = interface_to_usbdev(intf);
  546. len = snprintf(buf, 256, "%s", intf->cur_altsetting->string);
  547. if (len < 0)
  548. return 0;
  549. buf[len] = '\n';
  550. buf[len+1] = 0;
  551. return len+1;
  552. }
  553. static DEVICE_ATTR(interface, S_IRUGO, show_interface_string, NULL);
  554. static ssize_t show_modalias(struct device *dev,
  555. struct device_attribute *attr, char *buf)
  556. {
  557. struct usb_interface *intf;
  558. struct usb_device *udev;
  559. struct usb_host_interface *alt;
  560. intf = to_usb_interface(dev);
  561. udev = interface_to_usbdev(intf);
  562. alt = intf->cur_altsetting;
  563. return sprintf(buf, "usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02X"
  564. "ic%02Xisc%02Xip%02X\n",
  565. le16_to_cpu(udev->descriptor.idVendor),
  566. le16_to_cpu(udev->descriptor.idProduct),
  567. le16_to_cpu(udev->descriptor.bcdDevice),
  568. udev->descriptor.bDeviceClass,
  569. udev->descriptor.bDeviceSubClass,
  570. udev->descriptor.bDeviceProtocol,
  571. alt->desc.bInterfaceClass,
  572. alt->desc.bInterfaceSubClass,
  573. alt->desc.bInterfaceProtocol);
  574. }
  575. static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
  576. static struct attribute *intf_assoc_attrs[] = {
  577. &dev_attr_iad_bFirstInterface.attr,
  578. &dev_attr_iad_bInterfaceCount.attr,
  579. &dev_attr_iad_bFunctionClass.attr,
  580. &dev_attr_iad_bFunctionSubClass.attr,
  581. &dev_attr_iad_bFunctionProtocol.attr,
  582. NULL,
  583. };
  584. static struct attribute_group intf_assoc_attr_grp = {
  585. .attrs = intf_assoc_attrs,
  586. };
  587. static struct attribute *intf_attrs[] = {
  588. &dev_attr_bInterfaceNumber.attr,
  589. &dev_attr_bAlternateSetting.attr,
  590. &dev_attr_bNumEndpoints.attr,
  591. &dev_attr_bInterfaceClass.attr,
  592. &dev_attr_bInterfaceSubClass.attr,
  593. &dev_attr_bInterfaceProtocol.attr,
  594. &dev_attr_modalias.attr,
  595. NULL,
  596. };
  597. static struct attribute_group intf_attr_grp = {
  598. .attrs = intf_attrs,
  599. };
  600. static inline void usb_create_intf_ep_files(struct usb_interface *intf,
  601. struct usb_device *udev)
  602. {
  603. struct usb_host_interface *iface_desc;
  604. int i;
  605. iface_desc = intf->cur_altsetting;
  606. for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i)
  607. usb_create_ep_files(&intf->dev, &iface_desc->endpoint[i],
  608. udev);
  609. }
  610. static inline void usb_remove_intf_ep_files(struct usb_interface *intf)
  611. {
  612. struct usb_host_interface *iface_desc;
  613. int i;
  614. iface_desc = intf->cur_altsetting;
  615. for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i)
  616. usb_remove_ep_files(&iface_desc->endpoint[i]);
  617. }
  618. int usb_create_sysfs_intf_files(struct usb_interface *intf)
  619. {
  620. struct device *dev = &intf->dev;
  621. struct usb_device *udev = interface_to_usbdev(intf);
  622. struct usb_host_interface *alt = intf->cur_altsetting;
  623. int retval;
  624. retval = sysfs_create_group(&dev->kobj, &intf_attr_grp);
  625. if (retval)
  626. return retval;
  627. if (alt->string == NULL)
  628. alt->string = usb_cache_string(udev, alt->desc.iInterface);
  629. if (alt->string)
  630. retval = device_create_file(dev, &dev_attr_interface);
  631. if (intf->intf_assoc)
  632. retval = sysfs_create_group(&dev->kobj, &intf_assoc_attr_grp);
  633. usb_create_intf_ep_files(intf, udev);
  634. return 0;
  635. }
  636. void usb_remove_sysfs_intf_files(struct usb_interface *intf)
  637. {
  638. struct device *dev = &intf->dev;
  639. usb_remove_intf_ep_files(intf);
  640. device_remove_file(dev, &dev_attr_interface);
  641. sysfs_remove_group(&dev->kobj, &intf_attr_grp);
  642. sysfs_remove_group(&intf->dev.kobj, &intf_assoc_attr_grp);
  643. }