sysfs.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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/usb.h>
  13. #include "usb.h"
  14. /* Active configuration fields */
  15. #define usb_actconfig_show(field, multiplier, format_string) \
  16. static ssize_t show_##field(struct device *dev, \
  17. struct device_attribute *attr, char *buf) \
  18. { \
  19. struct usb_device *udev; \
  20. struct usb_host_config *actconfig; \
  21. \
  22. udev = to_usb_device(dev); \
  23. actconfig = udev->actconfig; \
  24. if (actconfig) \
  25. return sprintf(buf, format_string, \
  26. actconfig->desc.field * multiplier); \
  27. else \
  28. return 0; \
  29. } \
  30. #define usb_actconfig_attr(field, multiplier, format_string) \
  31. usb_actconfig_show(field, multiplier, format_string) \
  32. static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
  33. usb_actconfig_attr(bNumInterfaces, 1, "%2d\n")
  34. usb_actconfig_attr(bmAttributes, 1, "%2x\n")
  35. usb_actconfig_attr(bMaxPower, 2, "%3dmA\n")
  36. static ssize_t show_configuration_string(struct device *dev,
  37. struct device_attribute *attr, char *buf)
  38. {
  39. struct usb_device *udev;
  40. struct usb_host_config *actconfig;
  41. udev = to_usb_device(dev);
  42. actconfig = udev->actconfig;
  43. if ((!actconfig) || (!actconfig->string))
  44. return 0;
  45. return sprintf(buf, "%s\n", actconfig->string);
  46. }
  47. static DEVICE_ATTR(configuration, S_IRUGO, show_configuration_string, NULL);
  48. /* configuration value is always present, and r/w */
  49. usb_actconfig_show(bConfigurationValue, 1, "%u\n");
  50. static ssize_t
  51. set_bConfigurationValue(struct device *dev, struct device_attribute *attr,
  52. const char *buf, size_t count)
  53. {
  54. struct usb_device *udev = to_usb_device(dev);
  55. int config, value;
  56. if (sscanf(buf, "%d", &config) != 1 || config < -1 || config > 255)
  57. return -EINVAL;
  58. usb_lock_device(udev);
  59. value = usb_set_configuration(udev, config);
  60. usb_unlock_device(udev);
  61. return (value < 0) ? value : count;
  62. }
  63. static DEVICE_ATTR(bConfigurationValue, S_IRUGO | S_IWUSR,
  64. show_bConfigurationValue, set_bConfigurationValue);
  65. /* String fields */
  66. #define usb_string_attr(name) \
  67. static ssize_t show_##name(struct device *dev, \
  68. struct device_attribute *attr, char *buf) \
  69. { \
  70. struct usb_device *udev; \
  71. \
  72. udev = to_usb_device(dev); \
  73. return sprintf(buf, "%s\n", udev->name); \
  74. } \
  75. static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL);
  76. usb_string_attr(product);
  77. usb_string_attr(manufacturer);
  78. usb_string_attr(serial);
  79. static ssize_t
  80. show_speed(struct device *dev, struct device_attribute *attr, char *buf)
  81. {
  82. struct usb_device *udev;
  83. char *speed;
  84. udev = to_usb_device(dev);
  85. switch (udev->speed) {
  86. case USB_SPEED_LOW:
  87. speed = "1.5";
  88. break;
  89. case USB_SPEED_UNKNOWN:
  90. case USB_SPEED_FULL:
  91. speed = "12";
  92. break;
  93. case USB_SPEED_HIGH:
  94. speed = "480";
  95. break;
  96. default:
  97. speed = "unknown";
  98. }
  99. return sprintf(buf, "%s\n", speed);
  100. }
  101. static DEVICE_ATTR(speed, S_IRUGO, show_speed, NULL);
  102. static ssize_t
  103. show_devnum(struct device *dev, struct device_attribute *attr, char *buf)
  104. {
  105. struct usb_device *udev;
  106. udev = to_usb_device(dev);
  107. return sprintf(buf, "%d\n", udev->devnum);
  108. }
  109. static DEVICE_ATTR(devnum, S_IRUGO, show_devnum, NULL);
  110. static ssize_t
  111. show_version(struct device *dev, struct device_attribute *attr, char *buf)
  112. {
  113. struct usb_device *udev;
  114. u16 bcdUSB;
  115. udev = to_usb_device(dev);
  116. bcdUSB = le16_to_cpu(udev->descriptor.bcdUSB);
  117. return sprintf(buf, "%2x.%02x\n", bcdUSB >> 8, bcdUSB & 0xff);
  118. }
  119. static DEVICE_ATTR(version, S_IRUGO, show_version, NULL);
  120. static ssize_t
  121. show_maxchild(struct device *dev, struct device_attribute *attr, char *buf)
  122. {
  123. struct usb_device *udev;
  124. udev = to_usb_device(dev);
  125. return sprintf(buf, "%d\n", udev->maxchild);
  126. }
  127. static DEVICE_ATTR(maxchild, S_IRUGO, show_maxchild, NULL);
  128. static ssize_t
  129. show_quirks(struct device *dev, struct device_attribute *attr, char *buf)
  130. {
  131. struct usb_device *udev;
  132. udev = to_usb_device(dev);
  133. return sprintf(buf, "0x%x\n", udev->quirks);
  134. }
  135. static DEVICE_ATTR(quirks, S_IRUGO, show_quirks, NULL);
  136. #ifdef CONFIG_USB_SUSPEND
  137. static ssize_t
  138. show_autosuspend(struct device *dev, struct device_attribute *attr, char *buf)
  139. {
  140. struct usb_device *udev = to_usb_device(dev);
  141. return sprintf(buf, "%d\n", udev->autosuspend_delay / HZ);
  142. }
  143. static ssize_t
  144. set_autosuspend(struct device *dev, struct device_attribute *attr,
  145. const char *buf, size_t count)
  146. {
  147. struct usb_device *udev = to_usb_device(dev);
  148. int value;
  149. if (sscanf(buf, "%d", &value) != 1 || value >= INT_MAX/HZ ||
  150. value <= - INT_MAX/HZ)
  151. return -EINVAL;
  152. value *= HZ;
  153. udev->autosuspend_delay = value;
  154. if (value >= 0)
  155. usb_try_autosuspend_device(udev);
  156. else {
  157. usb_lock_device(udev);
  158. usb_external_resume_device(udev);
  159. usb_unlock_device(udev);
  160. }
  161. return count;
  162. }
  163. static DEVICE_ATTR(autosuspend, S_IRUGO | S_IWUSR,
  164. show_autosuspend, set_autosuspend);
  165. static char power_group[] = "power";
  166. static int add_power_attributes(struct device *dev)
  167. {
  168. int rc = 0;
  169. if (is_usb_device(dev))
  170. rc = sysfs_add_file_to_group(&dev->kobj,
  171. &dev_attr_autosuspend.attr,
  172. power_group);
  173. return rc;
  174. }
  175. static void remove_power_attributes(struct device *dev)
  176. {
  177. sysfs_remove_file_from_group(&dev->kobj,
  178. &dev_attr_autosuspend.attr,
  179. power_group);
  180. }
  181. #else
  182. #define add_power_attributes(dev) 0
  183. #define remove_power_attributes(dev) do {} while (0)
  184. #endif /* CONFIG_USB_SUSPEND */
  185. /* Descriptor fields */
  186. #define usb_descriptor_attr_le16(field, format_string) \
  187. static ssize_t \
  188. show_##field(struct device *dev, struct device_attribute *attr, \
  189. char *buf) \
  190. { \
  191. struct usb_device *udev; \
  192. \
  193. udev = to_usb_device(dev); \
  194. return sprintf(buf, format_string, \
  195. le16_to_cpu(udev->descriptor.field)); \
  196. } \
  197. static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
  198. usb_descriptor_attr_le16(idVendor, "%04x\n")
  199. usb_descriptor_attr_le16(idProduct, "%04x\n")
  200. usb_descriptor_attr_le16(bcdDevice, "%04x\n")
  201. #define usb_descriptor_attr(field, format_string) \
  202. static ssize_t \
  203. show_##field(struct device *dev, struct device_attribute *attr, \
  204. char *buf) \
  205. { \
  206. struct usb_device *udev; \
  207. \
  208. udev = to_usb_device(dev); \
  209. return sprintf(buf, format_string, udev->descriptor.field); \
  210. } \
  211. static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
  212. usb_descriptor_attr(bDeviceClass, "%02x\n")
  213. usb_descriptor_attr(bDeviceSubClass, "%02x\n")
  214. usb_descriptor_attr(bDeviceProtocol, "%02x\n")
  215. usb_descriptor_attr(bNumConfigurations, "%d\n")
  216. usb_descriptor_attr(bMaxPacketSize0, "%d\n")
  217. static struct attribute *dev_attrs[] = {
  218. /* current configuration's attributes */
  219. &dev_attr_configuration.attr,
  220. &dev_attr_bNumInterfaces.attr,
  221. &dev_attr_bConfigurationValue.attr,
  222. &dev_attr_bmAttributes.attr,
  223. &dev_attr_bMaxPower.attr,
  224. /* device attributes */
  225. &dev_attr_idVendor.attr,
  226. &dev_attr_idProduct.attr,
  227. &dev_attr_bcdDevice.attr,
  228. &dev_attr_bDeviceClass.attr,
  229. &dev_attr_bDeviceSubClass.attr,
  230. &dev_attr_bDeviceProtocol.attr,
  231. &dev_attr_bNumConfigurations.attr,
  232. &dev_attr_bMaxPacketSize0.attr,
  233. &dev_attr_speed.attr,
  234. &dev_attr_devnum.attr,
  235. &dev_attr_version.attr,
  236. &dev_attr_maxchild.attr,
  237. &dev_attr_quirks.attr,
  238. NULL,
  239. };
  240. static struct attribute_group dev_attr_grp = {
  241. .attrs = dev_attrs,
  242. };
  243. int usb_create_sysfs_dev_files(struct usb_device *udev)
  244. {
  245. struct device *dev = &udev->dev;
  246. int retval;
  247. retval = sysfs_create_group(&dev->kobj, &dev_attr_grp);
  248. if (retval)
  249. return retval;
  250. retval = add_power_attributes(dev);
  251. if (retval)
  252. goto error;
  253. if (udev->manufacturer) {
  254. retval = device_create_file(dev, &dev_attr_manufacturer);
  255. if (retval)
  256. goto error;
  257. }
  258. if (udev->product) {
  259. retval = device_create_file(dev, &dev_attr_product);
  260. if (retval)
  261. goto error;
  262. }
  263. if (udev->serial) {
  264. retval = device_create_file(dev, &dev_attr_serial);
  265. if (retval)
  266. goto error;
  267. }
  268. retval = usb_create_ep_files(dev, &udev->ep0, udev);
  269. if (retval)
  270. goto error;
  271. return 0;
  272. error:
  273. usb_remove_sysfs_dev_files(udev);
  274. return retval;
  275. }
  276. void usb_remove_sysfs_dev_files(struct usb_device *udev)
  277. {
  278. struct device *dev = &udev->dev;
  279. usb_remove_ep_files(&udev->ep0);
  280. device_remove_file(dev, &dev_attr_manufacturer);
  281. device_remove_file(dev, &dev_attr_product);
  282. device_remove_file(dev, &dev_attr_serial);
  283. remove_power_attributes(dev);
  284. sysfs_remove_group(&dev->kobj, &dev_attr_grp);
  285. }
  286. /* Interface fields */
  287. #define usb_intf_attr(field, format_string) \
  288. static ssize_t \
  289. show_##field(struct device *dev, struct device_attribute *attr, \
  290. char *buf) \
  291. { \
  292. struct usb_interface *intf = to_usb_interface(dev); \
  293. \
  294. return sprintf(buf, format_string, \
  295. intf->cur_altsetting->desc.field); \
  296. } \
  297. static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
  298. usb_intf_attr(bInterfaceNumber, "%02x\n")
  299. usb_intf_attr(bAlternateSetting, "%2d\n")
  300. usb_intf_attr(bNumEndpoints, "%02x\n")
  301. usb_intf_attr(bInterfaceClass, "%02x\n")
  302. usb_intf_attr(bInterfaceSubClass, "%02x\n")
  303. usb_intf_attr(bInterfaceProtocol, "%02x\n")
  304. static ssize_t show_interface_string(struct device *dev,
  305. struct device_attribute *attr, char *buf)
  306. {
  307. struct usb_interface *intf;
  308. struct usb_device *udev;
  309. int len;
  310. intf = to_usb_interface(dev);
  311. udev = interface_to_usbdev(intf);
  312. len = snprintf(buf, 256, "%s", intf->cur_altsetting->string);
  313. if (len < 0)
  314. return 0;
  315. buf[len] = '\n';
  316. buf[len+1] = 0;
  317. return len+1;
  318. }
  319. static DEVICE_ATTR(interface, S_IRUGO, show_interface_string, NULL);
  320. static ssize_t show_modalias(struct device *dev,
  321. struct device_attribute *attr, char *buf)
  322. {
  323. struct usb_interface *intf;
  324. struct usb_device *udev;
  325. struct usb_host_interface *alt;
  326. intf = to_usb_interface(dev);
  327. udev = interface_to_usbdev(intf);
  328. alt = intf->cur_altsetting;
  329. return sprintf(buf, "usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02X"
  330. "ic%02Xisc%02Xip%02X\n",
  331. le16_to_cpu(udev->descriptor.idVendor),
  332. le16_to_cpu(udev->descriptor.idProduct),
  333. le16_to_cpu(udev->descriptor.bcdDevice),
  334. udev->descriptor.bDeviceClass,
  335. udev->descriptor.bDeviceSubClass,
  336. udev->descriptor.bDeviceProtocol,
  337. alt->desc.bInterfaceClass,
  338. alt->desc.bInterfaceSubClass,
  339. alt->desc.bInterfaceProtocol);
  340. }
  341. static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
  342. static struct attribute *intf_attrs[] = {
  343. &dev_attr_bInterfaceNumber.attr,
  344. &dev_attr_bAlternateSetting.attr,
  345. &dev_attr_bNumEndpoints.attr,
  346. &dev_attr_bInterfaceClass.attr,
  347. &dev_attr_bInterfaceSubClass.attr,
  348. &dev_attr_bInterfaceProtocol.attr,
  349. &dev_attr_modalias.attr,
  350. NULL,
  351. };
  352. static struct attribute_group intf_attr_grp = {
  353. .attrs = intf_attrs,
  354. };
  355. static inline void usb_create_intf_ep_files(struct usb_interface *intf,
  356. struct usb_device *udev)
  357. {
  358. struct usb_host_interface *iface_desc;
  359. int i;
  360. iface_desc = intf->cur_altsetting;
  361. for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i)
  362. usb_create_ep_files(&intf->dev, &iface_desc->endpoint[i],
  363. udev);
  364. }
  365. static inline void usb_remove_intf_ep_files(struct usb_interface *intf)
  366. {
  367. struct usb_host_interface *iface_desc;
  368. int i;
  369. iface_desc = intf->cur_altsetting;
  370. for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i)
  371. usb_remove_ep_files(&iface_desc->endpoint[i]);
  372. }
  373. int usb_create_sysfs_intf_files(struct usb_interface *intf)
  374. {
  375. struct device *dev = &intf->dev;
  376. struct usb_device *udev = interface_to_usbdev(intf);
  377. struct usb_host_interface *alt = intf->cur_altsetting;
  378. int retval;
  379. retval = sysfs_create_group(&dev->kobj, &intf_attr_grp);
  380. if (retval)
  381. return retval;
  382. if (alt->string == NULL)
  383. alt->string = usb_cache_string(udev, alt->desc.iInterface);
  384. if (alt->string)
  385. retval = device_create_file(dev, &dev_attr_interface);
  386. usb_create_intf_ep_files(intf, udev);
  387. return 0;
  388. }
  389. void usb_remove_sysfs_intf_files(struct usb_interface *intf)
  390. {
  391. struct device *dev = &intf->dev;
  392. usb_remove_intf_ep_files(intf);
  393. device_remove_file(dev, &dev_attr_interface);
  394. sysfs_remove_group(&dev->kobj, &intf_attr_grp);
  395. }