sysfs.c 11 KB

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