sysfs.c 14 KB

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