sysfs.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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_devnum(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->devnum);
  109. }
  110. static DEVICE_ATTR(devnum, S_IRUGO, show_devnum, NULL);
  111. static ssize_t
  112. show_version(struct device *dev, struct device_attribute *attr, char *buf)
  113. {
  114. struct usb_device *udev;
  115. u16 bcdUSB;
  116. udev = to_usb_device(dev);
  117. bcdUSB = le16_to_cpu(udev->descriptor.bcdUSB);
  118. return sprintf(buf, "%2x.%02x\n", bcdUSB >> 8, bcdUSB & 0xff);
  119. }
  120. static DEVICE_ATTR(version, S_IRUGO, show_version, NULL);
  121. static ssize_t
  122. show_maxchild(struct device *dev, struct device_attribute *attr, char *buf)
  123. {
  124. struct usb_device *udev;
  125. udev = to_usb_device(dev);
  126. return sprintf(buf, "%d\n", udev->maxchild);
  127. }
  128. static DEVICE_ATTR(maxchild, S_IRUGO, show_maxchild, NULL);
  129. static ssize_t
  130. show_quirks(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, "0x%x\n", udev->quirks);
  135. }
  136. static DEVICE_ATTR(quirks, S_IRUGO, show_quirks, NULL);
  137. #ifdef CONFIG_USB_SUSPEND
  138. static ssize_t
  139. show_autosuspend(struct device *dev, struct device_attribute *attr, char *buf)
  140. {
  141. struct usb_device *udev = to_usb_device(dev);
  142. return sprintf(buf, "%d\n", udev->autosuspend_delay / HZ);
  143. }
  144. static ssize_t
  145. set_autosuspend(struct device *dev, struct device_attribute *attr,
  146. const char *buf, size_t count)
  147. {
  148. struct usb_device *udev = to_usb_device(dev);
  149. int value;
  150. if (sscanf(buf, "%d", &value) != 1 || value >= INT_MAX/HZ ||
  151. value <= - INT_MAX/HZ)
  152. return -EINVAL;
  153. value *= HZ;
  154. udev->autosuspend_delay = value;
  155. if (value >= 0)
  156. usb_try_autosuspend_device(udev);
  157. else {
  158. if (usb_autoresume_device(udev) == 0)
  159. usb_autosuspend_device(udev);
  160. }
  161. return count;
  162. }
  163. static DEVICE_ATTR(autosuspend, S_IRUGO | S_IWUSR,
  164. show_autosuspend, set_autosuspend);
  165. static const char on_string[] = "on";
  166. static const char auto_string[] = "auto";
  167. static const char suspend_string[] = "suspend";
  168. static ssize_t
  169. show_level(struct device *dev, struct device_attribute *attr, char *buf)
  170. {
  171. struct usb_device *udev = to_usb_device(dev);
  172. const char *p = auto_string;
  173. if (udev->state == USB_STATE_SUSPENDED) {
  174. if (udev->autoresume_disabled)
  175. p = suspend_string;
  176. } else {
  177. if (udev->autosuspend_disabled)
  178. p = on_string;
  179. }
  180. return sprintf(buf, "%s\n", p);
  181. }
  182. static ssize_t
  183. set_level(struct device *dev, struct device_attribute *attr,
  184. const char *buf, size_t count)
  185. {
  186. struct usb_device *udev = to_usb_device(dev);
  187. int len = count;
  188. char *cp;
  189. int rc = 0;
  190. cp = memchr(buf, '\n', count);
  191. if (cp)
  192. len = cp - buf;
  193. usb_lock_device(udev);
  194. /* Setting the flags without calling usb_pm_lock is a subject to
  195. * races, but who cares...
  196. */
  197. if (len == sizeof on_string - 1 &&
  198. strncmp(buf, on_string, len) == 0) {
  199. udev->autosuspend_disabled = 1;
  200. udev->autoresume_disabled = 0;
  201. rc = usb_external_resume_device(udev);
  202. } else if (len == sizeof auto_string - 1 &&
  203. strncmp(buf, auto_string, len) == 0) {
  204. udev->autosuspend_disabled = 0;
  205. udev->autoresume_disabled = 0;
  206. rc = usb_external_resume_device(udev);
  207. } else if (len == sizeof suspend_string - 1 &&
  208. strncmp(buf, suspend_string, len) == 0) {
  209. udev->autosuspend_disabled = 0;
  210. udev->autoresume_disabled = 1;
  211. rc = usb_external_suspend_device(udev, PMSG_SUSPEND);
  212. } else
  213. rc = -EINVAL;
  214. usb_unlock_device(udev);
  215. return (rc < 0 ? rc : count);
  216. }
  217. static DEVICE_ATTR(level, S_IRUGO | S_IWUSR, show_level, set_level);
  218. static char power_group[] = "power";
  219. static int add_power_attributes(struct device *dev)
  220. {
  221. int rc = 0;
  222. if (is_usb_device(dev)) {
  223. rc = sysfs_add_file_to_group(&dev->kobj,
  224. &dev_attr_autosuspend.attr,
  225. power_group);
  226. if (rc == 0)
  227. rc = sysfs_add_file_to_group(&dev->kobj,
  228. &dev_attr_level.attr,
  229. power_group);
  230. }
  231. return rc;
  232. }
  233. static void remove_power_attributes(struct device *dev)
  234. {
  235. sysfs_remove_file_from_group(&dev->kobj,
  236. &dev_attr_level.attr,
  237. power_group);
  238. sysfs_remove_file_from_group(&dev->kobj,
  239. &dev_attr_autosuspend.attr,
  240. power_group);
  241. }
  242. #else
  243. #define add_power_attributes(dev) 0
  244. #define remove_power_attributes(dev) do {} while (0)
  245. #endif /* CONFIG_USB_SUSPEND */
  246. /* Descriptor fields */
  247. #define usb_descriptor_attr_le16(field, format_string) \
  248. static ssize_t \
  249. show_##field(struct device *dev, struct device_attribute *attr, \
  250. char *buf) \
  251. { \
  252. struct usb_device *udev; \
  253. \
  254. udev = to_usb_device(dev); \
  255. return sprintf(buf, format_string, \
  256. le16_to_cpu(udev->descriptor.field)); \
  257. } \
  258. static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
  259. usb_descriptor_attr_le16(idVendor, "%04x\n")
  260. usb_descriptor_attr_le16(idProduct, "%04x\n")
  261. usb_descriptor_attr_le16(bcdDevice, "%04x\n")
  262. #define usb_descriptor_attr(field, format_string) \
  263. static ssize_t \
  264. show_##field(struct device *dev, struct device_attribute *attr, \
  265. char *buf) \
  266. { \
  267. struct usb_device *udev; \
  268. \
  269. udev = to_usb_device(dev); \
  270. return sprintf(buf, format_string, udev->descriptor.field); \
  271. } \
  272. static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
  273. usb_descriptor_attr(bDeviceClass, "%02x\n")
  274. usb_descriptor_attr(bDeviceSubClass, "%02x\n")
  275. usb_descriptor_attr(bDeviceProtocol, "%02x\n")
  276. usb_descriptor_attr(bNumConfigurations, "%d\n")
  277. usb_descriptor_attr(bMaxPacketSize0, "%d\n")
  278. static struct attribute *dev_attrs[] = {
  279. /* current configuration's attributes */
  280. &dev_attr_configuration.attr,
  281. &dev_attr_bNumInterfaces.attr,
  282. &dev_attr_bConfigurationValue.attr,
  283. &dev_attr_bmAttributes.attr,
  284. &dev_attr_bMaxPower.attr,
  285. /* device attributes */
  286. &dev_attr_idVendor.attr,
  287. &dev_attr_idProduct.attr,
  288. &dev_attr_bcdDevice.attr,
  289. &dev_attr_bDeviceClass.attr,
  290. &dev_attr_bDeviceSubClass.attr,
  291. &dev_attr_bDeviceProtocol.attr,
  292. &dev_attr_bNumConfigurations.attr,
  293. &dev_attr_bMaxPacketSize0.attr,
  294. &dev_attr_speed.attr,
  295. &dev_attr_devnum.attr,
  296. &dev_attr_version.attr,
  297. &dev_attr_maxchild.attr,
  298. &dev_attr_quirks.attr,
  299. NULL,
  300. };
  301. static struct attribute_group dev_attr_grp = {
  302. .attrs = dev_attrs,
  303. };
  304. int usb_create_sysfs_dev_files(struct usb_device *udev)
  305. {
  306. struct device *dev = &udev->dev;
  307. int retval;
  308. retval = sysfs_create_group(&dev->kobj, &dev_attr_grp);
  309. if (retval)
  310. return retval;
  311. retval = add_power_attributes(dev);
  312. if (retval)
  313. goto error;
  314. if (udev->manufacturer) {
  315. retval = device_create_file(dev, &dev_attr_manufacturer);
  316. if (retval)
  317. goto error;
  318. }
  319. if (udev->product) {
  320. retval = device_create_file(dev, &dev_attr_product);
  321. if (retval)
  322. goto error;
  323. }
  324. if (udev->serial) {
  325. retval = device_create_file(dev, &dev_attr_serial);
  326. if (retval)
  327. goto error;
  328. }
  329. retval = usb_create_ep_files(dev, &udev->ep0, udev);
  330. if (retval)
  331. goto error;
  332. return 0;
  333. error:
  334. usb_remove_sysfs_dev_files(udev);
  335. return retval;
  336. }
  337. void usb_remove_sysfs_dev_files(struct usb_device *udev)
  338. {
  339. struct device *dev = &udev->dev;
  340. usb_remove_ep_files(&udev->ep0);
  341. device_remove_file(dev, &dev_attr_manufacturer);
  342. device_remove_file(dev, &dev_attr_product);
  343. device_remove_file(dev, &dev_attr_serial);
  344. remove_power_attributes(dev);
  345. sysfs_remove_group(&dev->kobj, &dev_attr_grp);
  346. }
  347. /* Interface fields */
  348. #define usb_intf_attr(field, format_string) \
  349. static ssize_t \
  350. show_##field(struct device *dev, struct device_attribute *attr, \
  351. char *buf) \
  352. { \
  353. struct usb_interface *intf = to_usb_interface(dev); \
  354. \
  355. return sprintf(buf, format_string, \
  356. intf->cur_altsetting->desc.field); \
  357. } \
  358. static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
  359. usb_intf_attr(bInterfaceNumber, "%02x\n")
  360. usb_intf_attr(bAlternateSetting, "%2d\n")
  361. usb_intf_attr(bNumEndpoints, "%02x\n")
  362. usb_intf_attr(bInterfaceClass, "%02x\n")
  363. usb_intf_attr(bInterfaceSubClass, "%02x\n")
  364. usb_intf_attr(bInterfaceProtocol, "%02x\n")
  365. static ssize_t show_interface_string(struct device *dev,
  366. struct device_attribute *attr, char *buf)
  367. {
  368. struct usb_interface *intf;
  369. struct usb_device *udev;
  370. int len;
  371. intf = to_usb_interface(dev);
  372. udev = interface_to_usbdev(intf);
  373. len = snprintf(buf, 256, "%s", intf->cur_altsetting->string);
  374. if (len < 0)
  375. return 0;
  376. buf[len] = '\n';
  377. buf[len+1] = 0;
  378. return len+1;
  379. }
  380. static DEVICE_ATTR(interface, S_IRUGO, show_interface_string, NULL);
  381. static ssize_t show_modalias(struct device *dev,
  382. struct device_attribute *attr, char *buf)
  383. {
  384. struct usb_interface *intf;
  385. struct usb_device *udev;
  386. struct usb_host_interface *alt;
  387. intf = to_usb_interface(dev);
  388. udev = interface_to_usbdev(intf);
  389. alt = intf->cur_altsetting;
  390. return sprintf(buf, "usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02X"
  391. "ic%02Xisc%02Xip%02X\n",
  392. le16_to_cpu(udev->descriptor.idVendor),
  393. le16_to_cpu(udev->descriptor.idProduct),
  394. le16_to_cpu(udev->descriptor.bcdDevice),
  395. udev->descriptor.bDeviceClass,
  396. udev->descriptor.bDeviceSubClass,
  397. udev->descriptor.bDeviceProtocol,
  398. alt->desc.bInterfaceClass,
  399. alt->desc.bInterfaceSubClass,
  400. alt->desc.bInterfaceProtocol);
  401. }
  402. static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
  403. static struct attribute *intf_attrs[] = {
  404. &dev_attr_bInterfaceNumber.attr,
  405. &dev_attr_bAlternateSetting.attr,
  406. &dev_attr_bNumEndpoints.attr,
  407. &dev_attr_bInterfaceClass.attr,
  408. &dev_attr_bInterfaceSubClass.attr,
  409. &dev_attr_bInterfaceProtocol.attr,
  410. &dev_attr_modalias.attr,
  411. NULL,
  412. };
  413. static struct attribute_group intf_attr_grp = {
  414. .attrs = intf_attrs,
  415. };
  416. static inline void usb_create_intf_ep_files(struct usb_interface *intf,
  417. struct usb_device *udev)
  418. {
  419. struct usb_host_interface *iface_desc;
  420. int i;
  421. iface_desc = intf->cur_altsetting;
  422. for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i)
  423. usb_create_ep_files(&intf->dev, &iface_desc->endpoint[i],
  424. udev);
  425. }
  426. static inline void usb_remove_intf_ep_files(struct usb_interface *intf)
  427. {
  428. struct usb_host_interface *iface_desc;
  429. int i;
  430. iface_desc = intf->cur_altsetting;
  431. for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i)
  432. usb_remove_ep_files(&iface_desc->endpoint[i]);
  433. }
  434. int usb_create_sysfs_intf_files(struct usb_interface *intf)
  435. {
  436. struct device *dev = &intf->dev;
  437. struct usb_device *udev = interface_to_usbdev(intf);
  438. struct usb_host_interface *alt = intf->cur_altsetting;
  439. int retval;
  440. retval = sysfs_create_group(&dev->kobj, &intf_attr_grp);
  441. if (retval)
  442. return retval;
  443. if (alt->string == NULL)
  444. alt->string = usb_cache_string(udev, alt->desc.iInterface);
  445. if (alt->string)
  446. retval = device_create_file(dev, &dev_attr_interface);
  447. usb_create_intf_ep_files(intf, udev);
  448. return 0;
  449. }
  450. void usb_remove_sysfs_intf_files(struct usb_interface *intf)
  451. {
  452. struct device *dev = &intf->dev;
  453. usb_remove_intf_ep_files(intf);
  454. device_remove_file(dev, &dev_attr_interface);
  455. sysfs_remove_group(&dev->kobj, &intf_attr_grp);
  456. }