sysfs.c 15 KB

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