endpoint.c 8.1 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 struct attribute_group *ep_dev_groups[] = {
  144. &ep_dev_attr_grp,
  145. NULL
  146. };
  147. static int usb_endpoint_major_init(void)
  148. {
  149. dev_t dev;
  150. int error;
  151. error = alloc_chrdev_region(&dev, 0, MAX_ENDPOINT_MINORS,
  152. "usb_endpoint");
  153. if (error) {
  154. printk(KERN_ERR "Unable to get a dynamic major for "
  155. "usb endpoints.\n");
  156. return error;
  157. }
  158. usb_endpoint_major = MAJOR(dev);
  159. return error;
  160. }
  161. static void usb_endpoint_major_cleanup(void)
  162. {
  163. unregister_chrdev_region(MKDEV(usb_endpoint_major, 0),
  164. MAX_ENDPOINT_MINORS);
  165. }
  166. static int endpoint_get_minor(struct ep_device *ep_dev)
  167. {
  168. static DEFINE_MUTEX(minor_lock);
  169. int retval = -ENOMEM;
  170. int id;
  171. mutex_lock(&minor_lock);
  172. if (idr_pre_get(&endpoint_idr, GFP_KERNEL) == 0)
  173. goto exit;
  174. retval = idr_get_new(&endpoint_idr, ep_dev, &id);
  175. if (retval < 0) {
  176. if (retval == -EAGAIN)
  177. retval = -ENOMEM;
  178. goto exit;
  179. }
  180. ep_dev->minor = id & MAX_ID_MASK;
  181. exit:
  182. mutex_unlock(&minor_lock);
  183. return retval;
  184. }
  185. static void endpoint_free_minor(struct ep_device *ep_dev)
  186. {
  187. idr_remove(&endpoint_idr, ep_dev->minor);
  188. }
  189. static struct endpoint_class {
  190. struct kref kref;
  191. struct class *class;
  192. } *ep_class;
  193. static int init_endpoint_class(void)
  194. {
  195. int result = 0;
  196. if (ep_class != NULL) {
  197. kref_get(&ep_class->kref);
  198. goto exit;
  199. }
  200. ep_class = kmalloc(sizeof(*ep_class), GFP_KERNEL);
  201. if (!ep_class) {
  202. result = -ENOMEM;
  203. goto exit;
  204. }
  205. kref_init(&ep_class->kref);
  206. ep_class->class = class_create(THIS_MODULE, "usb_endpoint");
  207. if (IS_ERR(ep_class->class)) {
  208. result = PTR_ERR(ep_class->class);
  209. goto class_create_error;
  210. }
  211. result = usb_endpoint_major_init();
  212. if (result)
  213. goto endpoint_major_error;
  214. goto exit;
  215. endpoint_major_error:
  216. class_destroy(ep_class->class);
  217. class_create_error:
  218. kfree(ep_class);
  219. ep_class = NULL;
  220. exit:
  221. return result;
  222. }
  223. static void release_endpoint_class(struct kref *kref)
  224. {
  225. /* Ok, we cheat as we know we only have one ep_class */
  226. class_destroy(ep_class->class);
  227. kfree(ep_class);
  228. ep_class = NULL;
  229. usb_endpoint_major_cleanup();
  230. }
  231. static void destroy_endpoint_class(void)
  232. {
  233. if (ep_class)
  234. kref_put(&ep_class->kref, release_endpoint_class);
  235. }
  236. static void ep_device_release(struct device *dev)
  237. {
  238. struct ep_device *ep_dev = to_ep_device(dev);
  239. endpoint_free_minor(ep_dev);
  240. kfree(ep_dev);
  241. }
  242. int usb_create_ep_devs(struct device *parent,
  243. struct usb_host_endpoint *endpoint,
  244. struct usb_device *udev)
  245. {
  246. char name[8];
  247. struct ep_device *ep_dev;
  248. int retval;
  249. retval = init_endpoint_class();
  250. if (retval)
  251. goto exit;
  252. ep_dev = kzalloc(sizeof(*ep_dev), GFP_KERNEL);
  253. if (!ep_dev) {
  254. retval = -ENOMEM;
  255. goto error_alloc;
  256. }
  257. retval = endpoint_get_minor(ep_dev);
  258. if (retval) {
  259. dev_err(parent, "can not allocate minor number for %s\n",
  260. dev_name(&ep_dev->dev));
  261. goto error_register;
  262. }
  263. ep_dev->desc = &endpoint->desc;
  264. ep_dev->udev = udev;
  265. ep_dev->dev.groups = ep_dev_groups;
  266. ep_dev->dev.devt = MKDEV(usb_endpoint_major, ep_dev->minor);
  267. ep_dev->dev.class = ep_class->class;
  268. ep_dev->dev.parent = parent;
  269. ep_dev->dev.release = ep_device_release;
  270. dev_set_name(&ep_dev->dev, "usbdev%d.%d_ep%02x",
  271. udev->bus->busnum, udev->devnum,
  272. endpoint->desc.bEndpointAddress);
  273. retval = device_register(&ep_dev->dev);
  274. if (retval)
  275. goto error_chrdev;
  276. /* create the symlink to the old-style "ep_XX" directory */
  277. sprintf(name, "ep_%02x", endpoint->desc.bEndpointAddress);
  278. retval = sysfs_create_link(&parent->kobj, &ep_dev->dev.kobj, name);
  279. if (retval)
  280. goto error_link;
  281. endpoint->ep_dev = ep_dev;
  282. return retval;
  283. error_link:
  284. device_unregister(&ep_dev->dev);
  285. destroy_endpoint_class();
  286. return retval;
  287. error_chrdev:
  288. endpoint_free_minor(ep_dev);
  289. error_register:
  290. kfree(ep_dev);
  291. error_alloc:
  292. destroy_endpoint_class();
  293. exit:
  294. return retval;
  295. }
  296. void usb_remove_ep_devs(struct usb_host_endpoint *endpoint)
  297. {
  298. struct ep_device *ep_dev = endpoint->ep_dev;
  299. if (ep_dev) {
  300. char name[8];
  301. sprintf(name, "ep_%02x", endpoint->desc.bEndpointAddress);
  302. sysfs_remove_link(&ep_dev->dev.parent->kobj, name);
  303. device_unregister(&ep_dev->dev);
  304. endpoint->ep_dev = NULL;
  305. destroy_endpoint_class();
  306. }
  307. }