sysfs.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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/config.h>
  12. #include <linux/kernel.h>
  13. #ifdef CONFIG_USB_DEBUG
  14. #define DEBUG
  15. #else
  16. #undef DEBUG
  17. #endif
  18. #include <linux/usb.h>
  19. #include "usb.h"
  20. /* Active configuration fields */
  21. #define usb_actconfig_show(field, multiplier, format_string) \
  22. static ssize_t show_##field (struct device *dev, struct device_attribute *attr, char *buf) \
  23. { \
  24. struct usb_device *udev; \
  25. struct usb_host_config *actconfig; \
  26. \
  27. udev = to_usb_device (dev); \
  28. actconfig = udev->actconfig; \
  29. if (actconfig) \
  30. return sprintf (buf, format_string, \
  31. actconfig->desc.field * multiplier); \
  32. else \
  33. return 0; \
  34. } \
  35. #define usb_actconfig_attr(field, multiplier, format_string) \
  36. usb_actconfig_show(field, multiplier, format_string) \
  37. static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
  38. usb_actconfig_attr (bNumInterfaces, 1, "%2d\n")
  39. usb_actconfig_attr (bmAttributes, 1, "%2x\n")
  40. usb_actconfig_attr (bMaxPower, 2, "%3dmA\n")
  41. static ssize_t show_configuration_string(struct device *dev, struct device_attribute *attr, char *buf)
  42. {
  43. struct usb_device *udev;
  44. struct usb_host_config *actconfig;
  45. int len;
  46. udev = to_usb_device (dev);
  47. actconfig = udev->actconfig;
  48. if ((!actconfig) || (!actconfig->string))
  49. return 0;
  50. len = sprintf(buf, actconfig->string, PAGE_SIZE);
  51. if (len < 0)
  52. return 0;
  53. buf[len] = '\n';
  54. buf[len+1] = 0;
  55. return len+1;
  56. }
  57. static DEVICE_ATTR(configuration, S_IRUGO, show_configuration_string, NULL);
  58. /* configuration value is always present, and r/w */
  59. usb_actconfig_show(bConfigurationValue, 1, "%u\n");
  60. static ssize_t
  61. set_bConfigurationValue (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  62. {
  63. struct usb_device *udev = udev = to_usb_device (dev);
  64. int config, value;
  65. if (sscanf (buf, "%u", &config) != 1 || config > 255)
  66. return -EINVAL;
  67. usb_lock_device(udev);
  68. value = usb_set_configuration (udev, config);
  69. usb_unlock_device(udev);
  70. return (value < 0) ? value : count;
  71. }
  72. static DEVICE_ATTR(bConfigurationValue, S_IRUGO | S_IWUSR,
  73. show_bConfigurationValue, set_bConfigurationValue);
  74. /* String fields */
  75. #define usb_string_attr(name) \
  76. static ssize_t show_##name(struct device *dev, struct device_attribute *attr, char *buf) \
  77. { \
  78. struct usb_device *udev; \
  79. int len; \
  80. \
  81. udev = to_usb_device (dev); \
  82. len = snprintf(buf, 256, "%s", udev->name); \
  83. if (len < 0) \
  84. return 0; \
  85. buf[len] = '\n'; \
  86. buf[len+1] = 0; \
  87. return len+1; \
  88. } \
  89. static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL);
  90. usb_string_attr(product);
  91. usb_string_attr(manufacturer);
  92. usb_string_attr(serial);
  93. static ssize_t
  94. show_speed (struct device *dev, struct device_attribute *attr, char *buf)
  95. {
  96. struct usb_device *udev;
  97. char *speed;
  98. udev = to_usb_device (dev);
  99. switch (udev->speed) {
  100. case USB_SPEED_LOW:
  101. speed = "1.5";
  102. break;
  103. case USB_SPEED_UNKNOWN:
  104. case USB_SPEED_FULL:
  105. speed = "12";
  106. break;
  107. case USB_SPEED_HIGH:
  108. speed = "480";
  109. break;
  110. default:
  111. speed = "unknown";
  112. }
  113. return sprintf (buf, "%s\n", speed);
  114. }
  115. static DEVICE_ATTR(speed, S_IRUGO, show_speed, NULL);
  116. static ssize_t
  117. show_devnum (struct device *dev, struct device_attribute *attr, char *buf)
  118. {
  119. struct usb_device *udev;
  120. udev = to_usb_device (dev);
  121. return sprintf (buf, "%d\n", udev->devnum);
  122. }
  123. static DEVICE_ATTR(devnum, S_IRUGO, show_devnum, NULL);
  124. static ssize_t
  125. show_version (struct device *dev, struct device_attribute *attr, char *buf)
  126. {
  127. struct usb_device *udev;
  128. u16 bcdUSB;
  129. udev = to_usb_device(dev);
  130. bcdUSB = le16_to_cpu(udev->descriptor.bcdUSB);
  131. return sprintf(buf, "%2x.%02x\n", bcdUSB >> 8, bcdUSB & 0xff);
  132. }
  133. static DEVICE_ATTR(version, S_IRUGO, show_version, NULL);
  134. static ssize_t
  135. show_maxchild (struct device *dev, struct device_attribute *attr, char *buf)
  136. {
  137. struct usb_device *udev;
  138. udev = to_usb_device (dev);
  139. return sprintf (buf, "%d\n", udev->maxchild);
  140. }
  141. static DEVICE_ATTR(maxchild, S_IRUGO, show_maxchild, NULL);
  142. /* Descriptor fields */
  143. #define usb_descriptor_attr_le16(field, format_string) \
  144. static ssize_t \
  145. show_##field (struct device *dev, struct device_attribute *attr, char *buf) \
  146. { \
  147. struct usb_device *udev; \
  148. \
  149. udev = to_usb_device (dev); \
  150. return sprintf (buf, format_string, \
  151. le16_to_cpu(udev->descriptor.field)); \
  152. } \
  153. static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
  154. usb_descriptor_attr_le16(idVendor, "%04x\n")
  155. usb_descriptor_attr_le16(idProduct, "%04x\n")
  156. usb_descriptor_attr_le16(bcdDevice, "%04x\n")
  157. #define usb_descriptor_attr(field, format_string) \
  158. static ssize_t \
  159. show_##field (struct device *dev, struct device_attribute *attr, char *buf) \
  160. { \
  161. struct usb_device *udev; \
  162. \
  163. udev = to_usb_device (dev); \
  164. return sprintf (buf, format_string, udev->descriptor.field); \
  165. } \
  166. static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
  167. usb_descriptor_attr (bDeviceClass, "%02x\n")
  168. usb_descriptor_attr (bDeviceSubClass, "%02x\n")
  169. usb_descriptor_attr (bDeviceProtocol, "%02x\n")
  170. usb_descriptor_attr (bNumConfigurations, "%d\n")
  171. usb_descriptor_attr (bMaxPacketSize0, "%d\n")
  172. static struct attribute *dev_attrs[] = {
  173. /* current configuration's attributes */
  174. &dev_attr_bNumInterfaces.attr,
  175. &dev_attr_bConfigurationValue.attr,
  176. &dev_attr_bmAttributes.attr,
  177. &dev_attr_bMaxPower.attr,
  178. /* device attributes */
  179. &dev_attr_idVendor.attr,
  180. &dev_attr_idProduct.attr,
  181. &dev_attr_bcdDevice.attr,
  182. &dev_attr_bDeviceClass.attr,
  183. &dev_attr_bDeviceSubClass.attr,
  184. &dev_attr_bDeviceProtocol.attr,
  185. &dev_attr_bNumConfigurations.attr,
  186. &dev_attr_bMaxPacketSize0.attr,
  187. &dev_attr_speed.attr,
  188. &dev_attr_devnum.attr,
  189. &dev_attr_version.attr,
  190. &dev_attr_maxchild.attr,
  191. NULL,
  192. };
  193. static struct attribute_group dev_attr_grp = {
  194. .attrs = dev_attrs,
  195. };
  196. void usb_create_sysfs_dev_files (struct usb_device *udev)
  197. {
  198. struct device *dev = &udev->dev;
  199. sysfs_create_group(&dev->kobj, &dev_attr_grp);
  200. if (udev->manufacturer)
  201. device_create_file (dev, &dev_attr_manufacturer);
  202. if (udev->product)
  203. device_create_file (dev, &dev_attr_product);
  204. if (udev->serial)
  205. device_create_file (dev, &dev_attr_serial);
  206. device_create_file (dev, &dev_attr_configuration);
  207. }
  208. void usb_remove_sysfs_dev_files (struct usb_device *udev)
  209. {
  210. struct device *dev = &udev->dev;
  211. sysfs_remove_group(&dev->kobj, &dev_attr_grp);
  212. if (udev->descriptor.iManufacturer)
  213. device_remove_file(dev, &dev_attr_manufacturer);
  214. if (udev->descriptor.iProduct)
  215. device_remove_file(dev, &dev_attr_product);
  216. if (udev->descriptor.iSerialNumber)
  217. device_remove_file(dev, &dev_attr_serial);
  218. device_remove_file (dev, &dev_attr_configuration);
  219. }
  220. /* Interface fields */
  221. #define usb_intf_attr(field, format_string) \
  222. static ssize_t \
  223. show_##field (struct device *dev, struct device_attribute *attr, char *buf) \
  224. { \
  225. struct usb_interface *intf = to_usb_interface (dev); \
  226. \
  227. return sprintf (buf, format_string, intf->cur_altsetting->desc.field); \
  228. } \
  229. static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
  230. usb_intf_attr (bInterfaceNumber, "%02x\n")
  231. usb_intf_attr (bAlternateSetting, "%2d\n")
  232. usb_intf_attr (bNumEndpoints, "%02x\n")
  233. usb_intf_attr (bInterfaceClass, "%02x\n")
  234. usb_intf_attr (bInterfaceSubClass, "%02x\n")
  235. usb_intf_attr (bInterfaceProtocol, "%02x\n")
  236. static ssize_t show_interface_string(struct device *dev, struct device_attribute *attr, char *buf)
  237. {
  238. struct usb_interface *intf;
  239. struct usb_device *udev;
  240. int len;
  241. intf = to_usb_interface (dev);
  242. udev = interface_to_usbdev (intf);
  243. len = snprintf(buf, 256, "%s", intf->cur_altsetting->string);
  244. if (len < 0)
  245. return 0;
  246. buf[len] = '\n';
  247. buf[len+1] = 0;
  248. return len+1;
  249. }
  250. static DEVICE_ATTR(interface, S_IRUGO, show_interface_string, NULL);
  251. static ssize_t show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
  252. {
  253. struct usb_interface *intf;
  254. struct usb_device *udev;
  255. int len;
  256. intf = to_usb_interface(dev);
  257. udev = interface_to_usbdev(intf);
  258. len = sprintf(buf, "usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02Xic",
  259. le16_to_cpu(udev->descriptor.idVendor),
  260. le16_to_cpu(udev->descriptor.idProduct),
  261. le16_to_cpu(udev->descriptor.bcdDevice),
  262. udev->descriptor.bDeviceClass,
  263. udev->descriptor.bDeviceSubClass,
  264. udev->descriptor.bDeviceProtocol);
  265. buf += len;
  266. if (udev->descriptor.bDeviceClass == 0) {
  267. struct usb_host_interface *alt = intf->cur_altsetting;
  268. return len + sprintf(buf, "%02Xisc%02Xip%02X\n",
  269. alt->desc.bInterfaceClass,
  270. alt->desc.bInterfaceSubClass,
  271. alt->desc.bInterfaceProtocol);
  272. } else {
  273. return len + sprintf(buf, "*isc*ip*\n");
  274. }
  275. }
  276. static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
  277. static struct attribute *intf_attrs[] = {
  278. &dev_attr_bInterfaceNumber.attr,
  279. &dev_attr_bAlternateSetting.attr,
  280. &dev_attr_bNumEndpoints.attr,
  281. &dev_attr_bInterfaceClass.attr,
  282. &dev_attr_bInterfaceSubClass.attr,
  283. &dev_attr_bInterfaceProtocol.attr,
  284. &dev_attr_modalias.attr,
  285. NULL,
  286. };
  287. static struct attribute_group intf_attr_grp = {
  288. .attrs = intf_attrs,
  289. };
  290. void usb_create_sysfs_intf_files (struct usb_interface *intf)
  291. {
  292. sysfs_create_group(&intf->dev.kobj, &intf_attr_grp);
  293. if (intf->cur_altsetting->string)
  294. device_create_file(&intf->dev, &dev_attr_interface);
  295. }
  296. void usb_remove_sysfs_intf_files (struct usb_interface *intf)
  297. {
  298. sysfs_remove_group(&intf->dev.kobj, &intf_attr_grp);
  299. if (intf->cur_altsetting->string)
  300. device_remove_file(&intf->dev, &dev_attr_interface);
  301. }