sysfs.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. #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 = udev = to_usb_device (dev);
  56. int config, value;
  57. if (sscanf (buf, "%u", &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. /* Descriptor fields */
  130. #define usb_descriptor_attr_le16(field, format_string) \
  131. static ssize_t \
  132. show_##field (struct device *dev, struct device_attribute *attr, \
  133. char *buf) \
  134. { \
  135. struct usb_device *udev; \
  136. \
  137. udev = to_usb_device (dev); \
  138. return sprintf (buf, format_string, \
  139. le16_to_cpu(udev->descriptor.field)); \
  140. } \
  141. static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
  142. usb_descriptor_attr_le16(idVendor, "%04x\n")
  143. usb_descriptor_attr_le16(idProduct, "%04x\n")
  144. usb_descriptor_attr_le16(bcdDevice, "%04x\n")
  145. #define usb_descriptor_attr(field, format_string) \
  146. static ssize_t \
  147. show_##field (struct device *dev, struct device_attribute *attr, \
  148. char *buf) \
  149. { \
  150. struct usb_device *udev; \
  151. \
  152. udev = to_usb_device (dev); \
  153. return sprintf (buf, format_string, udev->descriptor.field); \
  154. } \
  155. static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
  156. usb_descriptor_attr (bDeviceClass, "%02x\n")
  157. usb_descriptor_attr (bDeviceSubClass, "%02x\n")
  158. usb_descriptor_attr (bDeviceProtocol, "%02x\n")
  159. usb_descriptor_attr (bNumConfigurations, "%d\n")
  160. usb_descriptor_attr (bMaxPacketSize0, "%d\n")
  161. static struct attribute *dev_attrs[] = {
  162. /* current configuration's attributes */
  163. &dev_attr_bNumInterfaces.attr,
  164. &dev_attr_bConfigurationValue.attr,
  165. &dev_attr_bmAttributes.attr,
  166. &dev_attr_bMaxPower.attr,
  167. /* device attributes */
  168. &dev_attr_idVendor.attr,
  169. &dev_attr_idProduct.attr,
  170. &dev_attr_bcdDevice.attr,
  171. &dev_attr_bDeviceClass.attr,
  172. &dev_attr_bDeviceSubClass.attr,
  173. &dev_attr_bDeviceProtocol.attr,
  174. &dev_attr_bNumConfigurations.attr,
  175. &dev_attr_bMaxPacketSize0.attr,
  176. &dev_attr_speed.attr,
  177. &dev_attr_devnum.attr,
  178. &dev_attr_version.attr,
  179. &dev_attr_maxchild.attr,
  180. NULL,
  181. };
  182. static struct attribute_group dev_attr_grp = {
  183. .attrs = dev_attrs,
  184. };
  185. void usb_create_sysfs_dev_files (struct usb_device *udev)
  186. {
  187. struct device *dev = &udev->dev;
  188. sysfs_create_group(&dev->kobj, &dev_attr_grp);
  189. if (udev->manufacturer)
  190. device_create_file (dev, &dev_attr_manufacturer);
  191. if (udev->product)
  192. device_create_file (dev, &dev_attr_product);
  193. if (udev->serial)
  194. device_create_file (dev, &dev_attr_serial);
  195. device_create_file (dev, &dev_attr_configuration);
  196. usb_create_ep_files(dev, &udev->ep0, udev);
  197. }
  198. void usb_remove_sysfs_dev_files (struct usb_device *udev)
  199. {
  200. struct device *dev = &udev->dev;
  201. usb_remove_ep_files(&udev->ep0);
  202. sysfs_remove_group(&dev->kobj, &dev_attr_grp);
  203. if (udev->manufacturer)
  204. device_remove_file(dev, &dev_attr_manufacturer);
  205. if (udev->product)
  206. device_remove_file(dev, &dev_attr_product);
  207. if (udev->serial)
  208. device_remove_file(dev, &dev_attr_serial);
  209. device_remove_file (dev, &dev_attr_configuration);
  210. }
  211. /* Interface fields */
  212. #define usb_intf_attr(field, format_string) \
  213. static ssize_t \
  214. show_##field (struct device *dev, struct device_attribute *attr, \
  215. char *buf) \
  216. { \
  217. struct usb_interface *intf = to_usb_interface (dev); \
  218. \
  219. return sprintf (buf, format_string, \
  220. intf->cur_altsetting->desc.field); \
  221. } \
  222. static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
  223. usb_intf_attr (bInterfaceNumber, "%02x\n")
  224. usb_intf_attr (bAlternateSetting, "%2d\n")
  225. usb_intf_attr (bNumEndpoints, "%02x\n")
  226. usb_intf_attr (bInterfaceClass, "%02x\n")
  227. usb_intf_attr (bInterfaceSubClass, "%02x\n")
  228. usb_intf_attr (bInterfaceProtocol, "%02x\n")
  229. static ssize_t show_interface_string(struct device *dev,
  230. struct device_attribute *attr, char *buf)
  231. {
  232. struct usb_interface *intf;
  233. struct usb_device *udev;
  234. int len;
  235. intf = to_usb_interface (dev);
  236. udev = interface_to_usbdev (intf);
  237. len = snprintf(buf, 256, "%s", intf->cur_altsetting->string);
  238. if (len < 0)
  239. return 0;
  240. buf[len] = '\n';
  241. buf[len+1] = 0;
  242. return len+1;
  243. }
  244. static DEVICE_ATTR(interface, S_IRUGO, show_interface_string, NULL);
  245. static ssize_t show_modalias(struct device *dev,
  246. struct device_attribute *attr, char *buf)
  247. {
  248. struct usb_interface *intf;
  249. struct usb_device *udev;
  250. struct usb_host_interface *alt;
  251. intf = to_usb_interface(dev);
  252. udev = interface_to_usbdev(intf);
  253. alt = intf->cur_altsetting;
  254. return sprintf(buf, "usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02X"
  255. "ic%02Xisc%02Xip%02X\n",
  256. le16_to_cpu(udev->descriptor.idVendor),
  257. le16_to_cpu(udev->descriptor.idProduct),
  258. le16_to_cpu(udev->descriptor.bcdDevice),
  259. udev->descriptor.bDeviceClass,
  260. udev->descriptor.bDeviceSubClass,
  261. udev->descriptor.bDeviceProtocol,
  262. alt->desc.bInterfaceClass,
  263. alt->desc.bInterfaceSubClass,
  264. alt->desc.bInterfaceProtocol);
  265. }
  266. static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
  267. static struct attribute *intf_attrs[] = {
  268. &dev_attr_bInterfaceNumber.attr,
  269. &dev_attr_bAlternateSetting.attr,
  270. &dev_attr_bNumEndpoints.attr,
  271. &dev_attr_bInterfaceClass.attr,
  272. &dev_attr_bInterfaceSubClass.attr,
  273. &dev_attr_bInterfaceProtocol.attr,
  274. &dev_attr_modalias.attr,
  275. NULL,
  276. };
  277. static struct attribute_group intf_attr_grp = {
  278. .attrs = intf_attrs,
  279. };
  280. static inline void usb_create_intf_ep_files(struct usb_interface *intf,
  281. struct usb_device *udev)
  282. {
  283. struct usb_host_interface *iface_desc;
  284. int i;
  285. iface_desc = intf->cur_altsetting;
  286. for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i)
  287. usb_create_ep_files(&intf->dev, &iface_desc->endpoint[i],
  288. udev);
  289. }
  290. static inline void usb_remove_intf_ep_files(struct usb_interface *intf)
  291. {
  292. struct usb_host_interface *iface_desc;
  293. int i;
  294. iface_desc = intf->cur_altsetting;
  295. for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i)
  296. usb_remove_ep_files(&iface_desc->endpoint[i]);
  297. }
  298. void usb_create_sysfs_intf_files (struct usb_interface *intf)
  299. {
  300. struct usb_device *udev = interface_to_usbdev(intf);
  301. struct usb_host_interface *alt = intf->cur_altsetting;
  302. sysfs_create_group(&intf->dev.kobj, &intf_attr_grp);
  303. if (alt->string == NULL)
  304. alt->string = usb_cache_string(udev, alt->desc.iInterface);
  305. if (alt->string)
  306. device_create_file(&intf->dev, &dev_attr_interface);
  307. usb_create_intf_ep_files(intf, udev);
  308. }
  309. void usb_remove_sysfs_intf_files (struct usb_interface *intf)
  310. {
  311. usb_remove_intf_ep_files(intf);
  312. sysfs_remove_group(&intf->dev.kobj, &intf_attr_grp);
  313. if (intf->cur_altsetting->string)
  314. device_remove_file(&intf->dev, &dev_attr_interface);
  315. }