sysfs.c 21 KB

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