sysfs.c 14 KB

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