endpoint.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /*
  2. * drivers/usb/core/endpoint.c
  3. *
  4. * (C) Copyright 2002,2004,2006 Greg Kroah-Hartman
  5. * (C) Copyright 2002,2004 IBM Corp.
  6. * (C) Copyright 2006 Novell Inc.
  7. *
  8. * Endpoint sysfs stuff
  9. *
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/spinlock.h>
  13. #include <linux/idr.h>
  14. #include <linux/usb.h>
  15. #include "usb.h"
  16. #define MAX_ENDPOINT_MINORS (64*128*32)
  17. static int usb_endpoint_major;
  18. static DEFINE_IDR(endpoint_idr);
  19. struct ep_device {
  20. struct usb_endpoint_descriptor *desc;
  21. struct usb_device *udev;
  22. struct device dev;
  23. int minor;
  24. };
  25. #define to_ep_device(_dev) \
  26. container_of(_dev, struct ep_device, dev)
  27. struct ep_attribute {
  28. struct attribute attr;
  29. ssize_t (*show)(struct usb_device *,
  30. struct usb_endpoint_descriptor *, char *);
  31. };
  32. #define to_ep_attribute(_attr) \
  33. container_of(_attr, struct ep_attribute, attr)
  34. #define usb_ep_attr(field, format_string) \
  35. static ssize_t show_ep_##field(struct device *dev, \
  36. struct device_attribute *attr, \
  37. char *buf) \
  38. { \
  39. struct ep_device *ep = to_ep_device(dev); \
  40. return sprintf(buf, format_string, ep->desc->field); \
  41. } \
  42. static DEVICE_ATTR(field, S_IRUGO, show_ep_##field, NULL);
  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 device *dev,
  48. struct device_attribute *attr, char *buf)
  49. {
  50. struct ep_device *ep = to_ep_device(dev);
  51. return sprintf(buf, "%04x\n",
  52. le16_to_cpu(ep->desc->wMaxPacketSize) & 0x07ff);
  53. }
  54. static DEVICE_ATTR(wMaxPacketSize, S_IRUGO, show_ep_wMaxPacketSize, NULL);
  55. static ssize_t show_ep_type(struct device *dev, struct device_attribute *attr,
  56. char *buf)
  57. {
  58. struct ep_device *ep = to_ep_device(dev);
  59. char *type = "unknown";
  60. switch (ep->desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
  61. case USB_ENDPOINT_XFER_CONTROL:
  62. type = "Control";
  63. break;
  64. case USB_ENDPOINT_XFER_ISOC:
  65. type = "Isoc";
  66. break;
  67. case USB_ENDPOINT_XFER_BULK:
  68. type = "Bulk";
  69. break;
  70. case USB_ENDPOINT_XFER_INT:
  71. type = "Interrupt";
  72. break;
  73. }
  74. return sprintf(buf, "%s\n", type);
  75. }
  76. static DEVICE_ATTR(type, S_IRUGO, show_ep_type, NULL);
  77. static ssize_t show_ep_interval(struct device *dev,
  78. struct device_attribute *attr, char *buf)
  79. {
  80. struct ep_device *ep = to_ep_device(dev);
  81. char unit;
  82. unsigned interval = 0;
  83. unsigned in;
  84. in = (ep->desc->bEndpointAddress & USB_DIR_IN);
  85. switch (ep->desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
  86. case USB_ENDPOINT_XFER_CONTROL:
  87. if (ep->udev->speed == USB_SPEED_HIGH) /* uframes per NAK */
  88. interval = ep->desc->bInterval;
  89. break;
  90. case USB_ENDPOINT_XFER_ISOC:
  91. interval = 1 << (ep->desc->bInterval - 1);
  92. break;
  93. case USB_ENDPOINT_XFER_BULK:
  94. if (ep->udev->speed == USB_SPEED_HIGH && !in) /* uframes per NAK */
  95. interval = ep->desc->bInterval;
  96. break;
  97. case USB_ENDPOINT_XFER_INT:
  98. if (ep->udev->speed == USB_SPEED_HIGH)
  99. interval = 1 << (ep->desc->bInterval - 1);
  100. else
  101. interval = ep->desc->bInterval;
  102. break;
  103. }
  104. interval *= (ep->udev->speed == USB_SPEED_HIGH) ? 125 : 1000;
  105. if (interval % 1000)
  106. unit = 'u';
  107. else {
  108. unit = 'm';
  109. interval /= 1000;
  110. }
  111. return sprintf(buf, "%d%cs\n", interval, unit);
  112. }
  113. static DEVICE_ATTR(interval, S_IRUGO, show_ep_interval, NULL);
  114. static ssize_t show_ep_direction(struct device *dev,
  115. struct device_attribute *attr, char *buf)
  116. {
  117. struct ep_device *ep = to_ep_device(dev);
  118. char *direction;
  119. if ((ep->desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
  120. USB_ENDPOINT_XFER_CONTROL)
  121. direction = "both";
  122. else if (ep->desc->bEndpointAddress & USB_DIR_IN)
  123. direction = "in";
  124. else
  125. direction = "out";
  126. return sprintf(buf, "%s\n", direction);
  127. }
  128. static DEVICE_ATTR(direction, S_IRUGO, show_ep_direction, NULL);
  129. static struct attribute *ep_dev_attrs[] = {
  130. &dev_attr_bLength.attr,
  131. &dev_attr_bEndpointAddress.attr,
  132. &dev_attr_bmAttributes.attr,
  133. &dev_attr_bInterval.attr,
  134. &dev_attr_wMaxPacketSize.attr,
  135. &dev_attr_interval.attr,
  136. &dev_attr_type.attr,
  137. &dev_attr_direction.attr,
  138. NULL,
  139. };
  140. static struct attribute_group ep_dev_attr_grp = {
  141. .attrs = ep_dev_attrs,
  142. };
  143. static int usb_endpoint_major_init(void)
  144. {
  145. dev_t dev;
  146. int error;
  147. error = alloc_chrdev_region(&dev, 0, MAX_ENDPOINT_MINORS,
  148. "usb_endpoint");
  149. if (error) {
  150. err("unable to get a dynamic major for usb endpoints");
  151. return error;
  152. }
  153. usb_endpoint_major = MAJOR(dev);
  154. return error;
  155. }
  156. static void usb_endpoint_major_cleanup(void)
  157. {
  158. unregister_chrdev_region(MKDEV(usb_endpoint_major, 0),
  159. MAX_ENDPOINT_MINORS);
  160. }
  161. static int endpoint_get_minor(struct ep_device *ep_dev)
  162. {
  163. static DEFINE_MUTEX(minor_lock);
  164. int retval = -ENOMEM;
  165. int id;
  166. mutex_lock(&minor_lock);
  167. if (idr_pre_get(&endpoint_idr, GFP_KERNEL) == 0)
  168. goto exit;
  169. retval = idr_get_new(&endpoint_idr, ep_dev, &id);
  170. if (retval < 0) {
  171. if (retval == -EAGAIN)
  172. retval = -ENOMEM;
  173. goto exit;
  174. }
  175. ep_dev->minor = id & MAX_ID_MASK;
  176. exit:
  177. mutex_unlock(&minor_lock);
  178. return retval;
  179. }
  180. static void endpoint_free_minor(struct ep_device *ep_dev)
  181. {
  182. idr_remove(&endpoint_idr, ep_dev->minor);
  183. }
  184. static struct endpoint_class {
  185. struct kref kref;
  186. struct class *class;
  187. } *ep_class;
  188. static int init_endpoint_class(void)
  189. {
  190. int result = 0;
  191. if (ep_class != NULL) {
  192. kref_get(&ep_class->kref);
  193. goto exit;
  194. }
  195. ep_class = kmalloc(sizeof(*ep_class), GFP_KERNEL);
  196. if (!ep_class) {
  197. result = -ENOMEM;
  198. goto exit;
  199. }
  200. kref_init(&ep_class->kref);
  201. ep_class->class = class_create(THIS_MODULE, "usb_endpoint");
  202. if (IS_ERR(ep_class->class)) {
  203. result = PTR_ERR(ep_class->class);
  204. goto class_create_error;
  205. }
  206. result = usb_endpoint_major_init();
  207. if (result)
  208. goto endpoint_major_error;
  209. goto exit;
  210. endpoint_major_error:
  211. class_destroy(ep_class->class);
  212. class_create_error:
  213. kfree(ep_class);
  214. ep_class = NULL;
  215. exit:
  216. return result;
  217. }
  218. static void release_endpoint_class(struct kref *kref)
  219. {
  220. /* Ok, we cheat as we know we only have one ep_class */
  221. class_destroy(ep_class->class);
  222. kfree(ep_class);
  223. ep_class = NULL;
  224. usb_endpoint_major_cleanup();
  225. }
  226. static void destroy_endpoint_class(void)
  227. {
  228. if (ep_class)
  229. kref_put(&ep_class->kref, release_endpoint_class);
  230. }
  231. static void ep_device_release(struct device *dev)
  232. {
  233. struct ep_device *ep_dev = to_ep_device(dev);
  234. endpoint_free_minor(ep_dev);
  235. kfree(ep_dev);
  236. }
  237. int usb_create_ep_files(struct device *parent,
  238. struct usb_host_endpoint *endpoint,
  239. struct usb_device *udev)
  240. {
  241. char name[8];
  242. struct ep_device *ep_dev;
  243. int retval;
  244. retval = init_endpoint_class();
  245. if (retval)
  246. goto exit;
  247. ep_dev = kzalloc(sizeof(*ep_dev), GFP_KERNEL);
  248. if (!ep_dev) {
  249. retval = -ENOMEM;
  250. goto error_alloc;
  251. }
  252. retval = endpoint_get_minor(ep_dev);
  253. if (retval) {
  254. dev_err(parent, "can not allocate minor number for %s\n",
  255. ep_dev->dev.bus_id);
  256. goto error_register;
  257. }
  258. ep_dev->desc = &endpoint->desc;
  259. ep_dev->udev = udev;
  260. ep_dev->dev.devt = MKDEV(usb_endpoint_major, ep_dev->minor);
  261. ep_dev->dev.class = ep_class->class;
  262. ep_dev->dev.parent = parent;
  263. ep_dev->dev.release = ep_device_release;
  264. snprintf(ep_dev->dev.bus_id, BUS_ID_SIZE, "usbdev%d.%d_ep%02x",
  265. udev->bus->busnum, udev->devnum,
  266. endpoint->desc.bEndpointAddress);
  267. retval = device_register(&ep_dev->dev);
  268. if (retval)
  269. goto error_chrdev;
  270. retval = sysfs_create_group(&ep_dev->dev.kobj, &ep_dev_attr_grp);
  271. if (retval)
  272. goto error_group;
  273. /* create the symlink to the old-style "ep_XX" directory */
  274. sprintf(name, "ep_%02x", endpoint->desc.bEndpointAddress);
  275. retval = sysfs_create_link(&parent->kobj, &ep_dev->dev.kobj, name);
  276. if (retval)
  277. goto error_link;
  278. endpoint->ep_dev = ep_dev;
  279. return retval;
  280. error_link:
  281. sysfs_remove_group(&ep_dev->dev.kobj, &ep_dev_attr_grp);
  282. error_group:
  283. device_unregister(&ep_dev->dev);
  284. destroy_endpoint_class();
  285. return retval;
  286. error_chrdev:
  287. endpoint_free_minor(ep_dev);
  288. error_register:
  289. kfree(ep_dev);
  290. error_alloc:
  291. destroy_endpoint_class();
  292. exit:
  293. return retval;
  294. }
  295. void usb_remove_ep_files(struct usb_host_endpoint *endpoint)
  296. {
  297. struct ep_device *ep_dev = endpoint->ep_dev;
  298. if (ep_dev) {
  299. char name[8];
  300. sprintf(name, "ep_%02x", endpoint->desc.bEndpointAddress);
  301. sysfs_remove_link(&ep_dev->dev.parent->kobj, name);
  302. sysfs_remove_group(&ep_dev->dev.kobj, &ep_dev_attr_grp);
  303. device_unregister(&ep_dev->dev);
  304. endpoint->ep_dev = NULL;
  305. destroy_endpoint_class();
  306. }
  307. }