sysfs.c 15 KB

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