sysfs.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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. /* endpoint stuff */
  21. struct endpoint_attribute {
  22. struct device_attribute dev_attr;
  23. struct usb_endpoint_descriptor *endpoint;
  24. struct usb_device *udev;
  25. };
  26. #define to_endpoint_attr(_dev_attr) \
  27. container_of(_dev_attr, struct endpoint_attribute, dev_attr)
  28. #define usb_ep_attr(field, format_string) \
  29. static ssize_t show_ep_##field(struct device *dev, struct device_attribute *attr, \
  30. char *buf) \
  31. { \
  32. struct endpoint_attribute *endpoint_attr = to_endpoint_attr(attr); \
  33. \
  34. return sprintf(buf, format_string, endpoint_attr->endpoint->field); \
  35. }
  36. usb_ep_attr(bLength, "%02x\n")
  37. usb_ep_attr(bDescriptorType, "%02x\n")
  38. usb_ep_attr(bEndpointAddress, "%02x\n")
  39. usb_ep_attr(bmAttributes, "%02x\n")
  40. usb_ep_attr(bInterval, "%02x\n")
  41. static ssize_t show_ep_wMaxPacketSize(struct device *dev,
  42. struct device_attribute *attr, char *buf)
  43. {
  44. struct endpoint_attribute *endpoint_attr = to_endpoint_attr(attr);
  45. return sprintf(buf, "%04x\n",
  46. le16_to_cpu(endpoint_attr->endpoint->wMaxPacketSize) & 0x07ff);
  47. }
  48. static ssize_t show_ep_type(struct device *dev, struct device_attribute *attr, char *buf)
  49. {
  50. struct endpoint_attribute *endpoint_attr = to_endpoint_attr(attr);
  51. char *type = "unknown";
  52. switch (endpoint_attr->endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
  53. case USB_ENDPOINT_XFER_CONTROL:
  54. type = "Control";
  55. break;
  56. case USB_ENDPOINT_XFER_ISOC:
  57. type = "Isoc";
  58. break;
  59. case USB_ENDPOINT_XFER_BULK:
  60. type = "Bulk";
  61. break;
  62. case USB_ENDPOINT_XFER_INT:
  63. type = "Interrupt";
  64. break;
  65. }
  66. return sprintf(buf, "%s\n", type);
  67. }
  68. static ssize_t show_ep_interval(struct device *dev, struct device_attribute *attr, char *buf)
  69. {
  70. struct endpoint_attribute *endpoint_attr = to_endpoint_attr(attr);
  71. struct usb_device *udev = endpoint_attr->udev;
  72. struct usb_endpoint_descriptor *endpoint = endpoint_attr->endpoint;
  73. char unit;
  74. unsigned interval = 0;
  75. unsigned in;
  76. in = (endpoint->bEndpointAddress & USB_DIR_IN);
  77. switch (endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
  78. case USB_ENDPOINT_XFER_CONTROL:
  79. if (udev->speed == USB_SPEED_HIGH) /* uframes per NAK */
  80. interval = endpoint->bInterval;
  81. break;
  82. case USB_ENDPOINT_XFER_ISOC:
  83. interval = 1 << (endpoint->bInterval - 1);
  84. break;
  85. case USB_ENDPOINT_XFER_BULK:
  86. if (udev->speed == USB_SPEED_HIGH && !in) /* uframes per NAK */
  87. interval = endpoint->bInterval;
  88. break;
  89. case USB_ENDPOINT_XFER_INT:
  90. if (udev->speed == USB_SPEED_HIGH) {
  91. interval = 1 << (endpoint->bInterval - 1);
  92. } else
  93. interval = endpoint->bInterval;
  94. break;
  95. }
  96. interval *= (udev->speed == USB_SPEED_HIGH) ? 125 : 1000;
  97. if (interval % 1000)
  98. unit = 'u';
  99. else {
  100. unit = 'm';
  101. interval /= 1000;
  102. }
  103. return sprintf(buf, "%d%cs\n", interval, unit);
  104. }
  105. static ssize_t show_ep_direction(struct device *dev, struct device_attribute *attr, char *buf)
  106. {
  107. struct endpoint_attribute *endpoint_attr = to_endpoint_attr(attr);
  108. char *direction;
  109. if ((endpoint_attr->endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
  110. USB_ENDPOINT_XFER_CONTROL)
  111. direction = "both";
  112. else if (endpoint_attr->endpoint->bEndpointAddress & USB_DIR_IN)
  113. direction = "in";
  114. else
  115. direction = "out";
  116. return sprintf(buf, "%s\n", direction);
  117. }
  118. static struct endpoint_attribute *create_ep_attr(struct usb_endpoint_descriptor *endpoint,
  119. struct usb_device *udev, char *name,
  120. ssize_t (*show)(struct device *dev, struct device_attribute *attr, char *buf))
  121. {
  122. struct endpoint_attribute *ep_attr;
  123. ep_attr = kzalloc(sizeof(*ep_attr), GFP_KERNEL);
  124. if (ep_attr) {
  125. ep_attr->endpoint = endpoint;
  126. ep_attr->udev = udev;
  127. ep_attr->dev_attr.attr.name = name;
  128. ep_attr->dev_attr.attr.mode = 0444;
  129. ep_attr->dev_attr.attr.owner = THIS_MODULE;
  130. ep_attr->dev_attr.show = show;
  131. }
  132. return ep_attr;
  133. }
  134. static void usb_create_ep_files(struct kobject *kobj, struct usb_host_endpoint *endpoint, struct usb_device *udev)
  135. {
  136. struct usb_endpoint_descriptor *ep;
  137. ep = &endpoint->desc;
  138. endpoint->attrs = kzalloc(sizeof(struct attribute *) * 10, GFP_KERNEL);
  139. endpoint->attrs[0] = &(create_ep_attr(ep, udev, "direction", show_ep_direction)->dev_attr.attr);
  140. endpoint->attrs[1] = &(create_ep_attr(ep, udev, "type", show_ep_type)->dev_attr.attr);
  141. endpoint->attrs[2] = &(create_ep_attr(ep, udev, "bLength", show_ep_bLength)->dev_attr.attr);
  142. endpoint->attrs[3] = &(create_ep_attr(ep, udev, "bDescriptorType", show_ep_bDescriptorType)->dev_attr.attr);
  143. endpoint->attrs[4] = &(create_ep_attr(ep, udev, "bEndpointAddress", show_ep_bEndpointAddress)->dev_attr.attr);
  144. endpoint->attrs[5] = &(create_ep_attr(ep, udev, "bmAttributes", show_ep_bmAttributes)->dev_attr.attr);
  145. endpoint->attrs[6] = &(create_ep_attr(ep, udev, "wMaxPacketSize", show_ep_wMaxPacketSize)->dev_attr.attr);
  146. endpoint->attrs[7] = &(create_ep_attr(ep, udev, "bInterval", show_ep_bInterval)->dev_attr.attr);
  147. endpoint->attrs[8] = &(create_ep_attr(ep, udev, "interval", show_ep_interval)->dev_attr.attr);
  148. endpoint->attrs[9] = NULL;
  149. endpoint->num_attrs = 9;
  150. endpoint->attr_group = kzalloc(sizeof(*endpoint->attr_group), GFP_KERNEL);
  151. endpoint->attr_name = kzalloc(10, GFP_KERNEL);
  152. sprintf(endpoint->attr_name, "ep_%02x", endpoint->desc.bEndpointAddress);
  153. endpoint->attr_group->attrs = endpoint->attrs;
  154. endpoint->attr_group->name = endpoint->attr_name;
  155. sysfs_create_group(kobj, endpoint->attr_group);
  156. }
  157. static void usb_remove_ep_files(struct kobject *kobj, struct usb_host_endpoint *endpoint)
  158. {
  159. int i;
  160. sysfs_remove_group(kobj, endpoint->attr_group);
  161. kfree(endpoint->attr_group);
  162. kfree(endpoint->attr_name);
  163. for (i = 0; i < endpoint->num_attrs; ++i)
  164. kfree(endpoint->attrs[i]);
  165. kfree(endpoint->attrs);
  166. }
  167. /* Active configuration fields */
  168. #define usb_actconfig_show(field, multiplier, format_string) \
  169. static ssize_t show_##field (struct device *dev, struct device_attribute *attr, char *buf) \
  170. { \
  171. struct usb_device *udev; \
  172. struct usb_host_config *actconfig; \
  173. \
  174. udev = to_usb_device (dev); \
  175. actconfig = udev->actconfig; \
  176. if (actconfig) \
  177. return sprintf (buf, format_string, \
  178. actconfig->desc.field * multiplier); \
  179. else \
  180. return 0; \
  181. } \
  182. #define usb_actconfig_attr(field, multiplier, format_string) \
  183. usb_actconfig_show(field, multiplier, format_string) \
  184. static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
  185. usb_actconfig_attr (bNumInterfaces, 1, "%2d\n")
  186. usb_actconfig_attr (bmAttributes, 1, "%2x\n")
  187. usb_actconfig_attr (bMaxPower, 2, "%3dmA\n")
  188. static ssize_t show_configuration_string(struct device *dev, struct device_attribute *attr, char *buf)
  189. {
  190. struct usb_device *udev;
  191. struct usb_host_config *actconfig;
  192. int len;
  193. udev = to_usb_device (dev);
  194. actconfig = udev->actconfig;
  195. if ((!actconfig) || (!actconfig->string))
  196. return 0;
  197. len = sprintf(buf, actconfig->string, PAGE_SIZE);
  198. if (len < 0)
  199. return 0;
  200. buf[len] = '\n';
  201. buf[len+1] = 0;
  202. return len+1;
  203. }
  204. static DEVICE_ATTR(configuration, S_IRUGO, show_configuration_string, NULL);
  205. /* configuration value is always present, and r/w */
  206. usb_actconfig_show(bConfigurationValue, 1, "%u\n");
  207. static ssize_t
  208. set_bConfigurationValue (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  209. {
  210. struct usb_device *udev = udev = to_usb_device (dev);
  211. int config, value;
  212. if (sscanf (buf, "%u", &config) != 1 || config > 255)
  213. return -EINVAL;
  214. usb_lock_device(udev);
  215. value = usb_set_configuration (udev, config);
  216. usb_unlock_device(udev);
  217. return (value < 0) ? value : count;
  218. }
  219. static DEVICE_ATTR(bConfigurationValue, S_IRUGO | S_IWUSR,
  220. show_bConfigurationValue, set_bConfigurationValue);
  221. /* String fields */
  222. #define usb_string_attr(name) \
  223. static ssize_t show_##name(struct device *dev, struct device_attribute *attr, char *buf) \
  224. { \
  225. struct usb_device *udev; \
  226. int len; \
  227. \
  228. udev = to_usb_device (dev); \
  229. len = snprintf(buf, 256, "%s", udev->name); \
  230. if (len < 0) \
  231. return 0; \
  232. buf[len] = '\n'; \
  233. buf[len+1] = 0; \
  234. return len+1; \
  235. } \
  236. static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL);
  237. usb_string_attr(product);
  238. usb_string_attr(manufacturer);
  239. usb_string_attr(serial);
  240. static ssize_t
  241. show_speed (struct device *dev, struct device_attribute *attr, char *buf)
  242. {
  243. struct usb_device *udev;
  244. char *speed;
  245. udev = to_usb_device (dev);
  246. switch (udev->speed) {
  247. case USB_SPEED_LOW:
  248. speed = "1.5";
  249. break;
  250. case USB_SPEED_UNKNOWN:
  251. case USB_SPEED_FULL:
  252. speed = "12";
  253. break;
  254. case USB_SPEED_HIGH:
  255. speed = "480";
  256. break;
  257. default:
  258. speed = "unknown";
  259. }
  260. return sprintf (buf, "%s\n", speed);
  261. }
  262. static DEVICE_ATTR(speed, S_IRUGO, show_speed, NULL);
  263. static ssize_t
  264. show_devnum (struct device *dev, struct device_attribute *attr, char *buf)
  265. {
  266. struct usb_device *udev;
  267. udev = to_usb_device (dev);
  268. return sprintf (buf, "%d\n", udev->devnum);
  269. }
  270. static DEVICE_ATTR(devnum, S_IRUGO, show_devnum, NULL);
  271. static ssize_t
  272. show_version (struct device *dev, struct device_attribute *attr, char *buf)
  273. {
  274. struct usb_device *udev;
  275. u16 bcdUSB;
  276. udev = to_usb_device(dev);
  277. bcdUSB = le16_to_cpu(udev->descriptor.bcdUSB);
  278. return sprintf(buf, "%2x.%02x\n", bcdUSB >> 8, bcdUSB & 0xff);
  279. }
  280. static DEVICE_ATTR(version, S_IRUGO, show_version, NULL);
  281. static ssize_t
  282. show_maxchild (struct device *dev, struct device_attribute *attr, char *buf)
  283. {
  284. struct usb_device *udev;
  285. udev = to_usb_device (dev);
  286. return sprintf (buf, "%d\n", udev->maxchild);
  287. }
  288. static DEVICE_ATTR(maxchild, S_IRUGO, show_maxchild, NULL);
  289. /* Descriptor fields */
  290. #define usb_descriptor_attr_le16(field, format_string) \
  291. static ssize_t \
  292. show_##field (struct device *dev, struct device_attribute *attr, char *buf) \
  293. { \
  294. struct usb_device *udev; \
  295. \
  296. udev = to_usb_device (dev); \
  297. return sprintf (buf, format_string, \
  298. le16_to_cpu(udev->descriptor.field)); \
  299. } \
  300. static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
  301. usb_descriptor_attr_le16(idVendor, "%04x\n")
  302. usb_descriptor_attr_le16(idProduct, "%04x\n")
  303. usb_descriptor_attr_le16(bcdDevice, "%04x\n")
  304. #define usb_descriptor_attr(field, format_string) \
  305. static ssize_t \
  306. show_##field (struct device *dev, struct device_attribute *attr, char *buf) \
  307. { \
  308. struct usb_device *udev; \
  309. \
  310. udev = to_usb_device (dev); \
  311. return sprintf (buf, format_string, udev->descriptor.field); \
  312. } \
  313. static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
  314. usb_descriptor_attr (bDeviceClass, "%02x\n")
  315. usb_descriptor_attr (bDeviceSubClass, "%02x\n")
  316. usb_descriptor_attr (bDeviceProtocol, "%02x\n")
  317. usb_descriptor_attr (bNumConfigurations, "%d\n")
  318. usb_descriptor_attr (bMaxPacketSize0, "%d\n")
  319. static struct attribute *dev_attrs[] = {
  320. /* current configuration's attributes */
  321. &dev_attr_bNumInterfaces.attr,
  322. &dev_attr_bConfigurationValue.attr,
  323. &dev_attr_bmAttributes.attr,
  324. &dev_attr_bMaxPower.attr,
  325. /* device attributes */
  326. &dev_attr_idVendor.attr,
  327. &dev_attr_idProduct.attr,
  328. &dev_attr_bcdDevice.attr,
  329. &dev_attr_bDeviceClass.attr,
  330. &dev_attr_bDeviceSubClass.attr,
  331. &dev_attr_bDeviceProtocol.attr,
  332. &dev_attr_bNumConfigurations.attr,
  333. &dev_attr_bMaxPacketSize0.attr,
  334. &dev_attr_speed.attr,
  335. &dev_attr_devnum.attr,
  336. &dev_attr_version.attr,
  337. &dev_attr_maxchild.attr,
  338. NULL,
  339. };
  340. static struct attribute_group dev_attr_grp = {
  341. .attrs = dev_attrs,
  342. };
  343. void usb_create_sysfs_dev_files (struct usb_device *udev)
  344. {
  345. struct device *dev = &udev->dev;
  346. sysfs_create_group(&dev->kobj, &dev_attr_grp);
  347. if (udev->manufacturer)
  348. device_create_file (dev, &dev_attr_manufacturer);
  349. if (udev->product)
  350. device_create_file (dev, &dev_attr_product);
  351. if (udev->serial)
  352. device_create_file (dev, &dev_attr_serial);
  353. device_create_file (dev, &dev_attr_configuration);
  354. usb_create_ep_files(&dev->kobj, &udev->ep0, udev);
  355. }
  356. void usb_remove_sysfs_dev_files (struct usb_device *udev)
  357. {
  358. struct device *dev = &udev->dev;
  359. usb_remove_ep_files(&dev->kobj, &udev->ep0);
  360. sysfs_remove_group(&dev->kobj, &dev_attr_grp);
  361. if (udev->descriptor.iManufacturer)
  362. device_remove_file(dev, &dev_attr_manufacturer);
  363. if (udev->descriptor.iProduct)
  364. device_remove_file(dev, &dev_attr_product);
  365. if (udev->descriptor.iSerialNumber)
  366. device_remove_file(dev, &dev_attr_serial);
  367. device_remove_file (dev, &dev_attr_configuration);
  368. }
  369. /* Interface fields */
  370. #define usb_intf_attr(field, format_string) \
  371. static ssize_t \
  372. show_##field (struct device *dev, struct device_attribute *attr, char *buf) \
  373. { \
  374. struct usb_interface *intf = to_usb_interface (dev); \
  375. \
  376. return sprintf (buf, format_string, intf->cur_altsetting->desc.field); \
  377. } \
  378. static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
  379. usb_intf_attr (bInterfaceNumber, "%02x\n")
  380. usb_intf_attr (bAlternateSetting, "%2d\n")
  381. usb_intf_attr (bNumEndpoints, "%02x\n")
  382. usb_intf_attr (bInterfaceClass, "%02x\n")
  383. usb_intf_attr (bInterfaceSubClass, "%02x\n")
  384. usb_intf_attr (bInterfaceProtocol, "%02x\n")
  385. static ssize_t show_interface_string(struct device *dev, struct device_attribute *attr, char *buf)
  386. {
  387. struct usb_interface *intf;
  388. struct usb_device *udev;
  389. int len;
  390. intf = to_usb_interface (dev);
  391. udev = interface_to_usbdev (intf);
  392. len = snprintf(buf, 256, "%s", intf->cur_altsetting->string);
  393. if (len < 0)
  394. return 0;
  395. buf[len] = '\n';
  396. buf[len+1] = 0;
  397. return len+1;
  398. }
  399. static DEVICE_ATTR(interface, S_IRUGO, show_interface_string, NULL);
  400. static ssize_t show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
  401. {
  402. struct usb_interface *intf;
  403. struct usb_device *udev;
  404. int len;
  405. intf = to_usb_interface(dev);
  406. udev = interface_to_usbdev(intf);
  407. len = sprintf(buf, "usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02Xic",
  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. buf += len;
  415. if (udev->descriptor.bDeviceClass == 0) {
  416. struct usb_host_interface *alt = intf->cur_altsetting;
  417. return len + sprintf(buf, "%02Xisc%02Xip%02X\n",
  418. alt->desc.bInterfaceClass,
  419. alt->desc.bInterfaceSubClass,
  420. alt->desc.bInterfaceProtocol);
  421. } else {
  422. return len + sprintf(buf, "*isc*ip*\n");
  423. }
  424. }
  425. static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
  426. static struct attribute *intf_attrs[] = {
  427. &dev_attr_bInterfaceNumber.attr,
  428. &dev_attr_bAlternateSetting.attr,
  429. &dev_attr_bNumEndpoints.attr,
  430. &dev_attr_bInterfaceClass.attr,
  431. &dev_attr_bInterfaceSubClass.attr,
  432. &dev_attr_bInterfaceProtocol.attr,
  433. &dev_attr_modalias.attr,
  434. NULL,
  435. };
  436. static struct attribute_group intf_attr_grp = {
  437. .attrs = intf_attrs,
  438. };
  439. static void usb_create_intf_ep_files(struct usb_interface *intf)
  440. {
  441. struct usb_host_interface *iface_desc;
  442. int i;
  443. iface_desc = intf->cur_altsetting;
  444. for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i)
  445. usb_create_ep_files(&intf->dev.kobj, &iface_desc->endpoint[i],
  446. interface_to_usbdev(intf));
  447. }
  448. static void usb_remove_intf_ep_files(struct usb_interface *intf)
  449. {
  450. struct usb_host_interface *iface_desc;
  451. int i;
  452. iface_desc = intf->cur_altsetting;
  453. for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i)
  454. usb_remove_ep_files(&intf->dev.kobj, &iface_desc->endpoint[i]);
  455. }
  456. void usb_create_sysfs_intf_files (struct usb_interface *intf)
  457. {
  458. sysfs_create_group(&intf->dev.kobj, &intf_attr_grp);
  459. if (intf->cur_altsetting->string)
  460. device_create_file(&intf->dev, &dev_attr_interface);
  461. usb_create_intf_ep_files(intf);
  462. }
  463. void usb_remove_sysfs_intf_files (struct usb_interface *intf)
  464. {
  465. usb_remove_intf_ep_files(intf);
  466. sysfs_remove_group(&intf->dev.kobj, &intf_attr_grp);
  467. if (intf->cur_altsetting->string)
  468. device_remove_file(&intf->dev, &dev_attr_interface);
  469. }