file.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * drivers/usb/core/file.c
  3. *
  4. * (C) Copyright Linus Torvalds 1999
  5. * (C) Copyright Johannes Erdfelt 1999-2001
  6. * (C) Copyright Andreas Gal 1999
  7. * (C) Copyright Gregory P. Smith 1999
  8. * (C) Copyright Deti Fliegl 1999 (new USB architecture)
  9. * (C) Copyright Randy Dunlap 2000
  10. * (C) Copyright David Brownell 2000-2001 (kernel hotplug, usb_device_id,
  11. more docs, etc)
  12. * (C) Copyright Yggdrasil Computing, Inc. 2000
  13. * (usb_device_id matching changes by Adam J. Richter)
  14. * (C) Copyright Greg Kroah-Hartman 2002-2003
  15. *
  16. */
  17. #include <linux/module.h>
  18. #include <linux/errno.h>
  19. #include <linux/rwsem.h>
  20. #include <linux/slab.h>
  21. #include <linux/usb.h>
  22. #include "usb.h"
  23. #define MAX_USB_MINORS 256
  24. static const struct file_operations *usb_minors[MAX_USB_MINORS];
  25. static DECLARE_RWSEM(minor_rwsem);
  26. static int usb_open(struct inode * inode, struct file * file)
  27. {
  28. int minor = iminor(inode);
  29. const struct file_operations *c;
  30. int err = -ENODEV;
  31. const struct file_operations *old_fops, *new_fops = NULL;
  32. down_read(&minor_rwsem);
  33. c = usb_minors[minor];
  34. if (!c || !(new_fops = fops_get(c)))
  35. goto done;
  36. old_fops = file->f_op;
  37. file->f_op = new_fops;
  38. /* Curiouser and curiouser... NULL ->open() as "no device" ? */
  39. if (file->f_op->open)
  40. err = file->f_op->open(inode,file);
  41. if (err) {
  42. fops_put(file->f_op);
  43. file->f_op = fops_get(old_fops);
  44. }
  45. fops_put(old_fops);
  46. done:
  47. up_read(&minor_rwsem);
  48. return err;
  49. }
  50. static const struct file_operations usb_fops = {
  51. .owner = THIS_MODULE,
  52. .open = usb_open,
  53. .llseek = noop_llseek,
  54. };
  55. static struct usb_class {
  56. struct kref kref;
  57. struct class *class;
  58. } *usb_class;
  59. static char *usb_devnode(struct device *dev, umode_t *mode)
  60. {
  61. struct usb_class_driver *drv;
  62. drv = dev_get_drvdata(dev);
  63. if (!drv || !drv->devnode)
  64. return NULL;
  65. return drv->devnode(dev, mode);
  66. }
  67. static int init_usb_class(void)
  68. {
  69. int result = 0;
  70. if (usb_class != NULL) {
  71. kref_get(&usb_class->kref);
  72. goto exit;
  73. }
  74. usb_class = kmalloc(sizeof(*usb_class), GFP_KERNEL);
  75. if (!usb_class) {
  76. result = -ENOMEM;
  77. goto exit;
  78. }
  79. kref_init(&usb_class->kref);
  80. usb_class->class = class_create(THIS_MODULE, "usb");
  81. if (IS_ERR(usb_class->class)) {
  82. result = IS_ERR(usb_class->class);
  83. printk(KERN_ERR "class_create failed for usb devices\n");
  84. kfree(usb_class);
  85. usb_class = NULL;
  86. goto exit;
  87. }
  88. usb_class->class->devnode = usb_devnode;
  89. exit:
  90. return result;
  91. }
  92. static void release_usb_class(struct kref *kref)
  93. {
  94. /* Ok, we cheat as we know we only have one usb_class */
  95. class_destroy(usb_class->class);
  96. kfree(usb_class);
  97. usb_class = NULL;
  98. }
  99. static void destroy_usb_class(void)
  100. {
  101. if (usb_class)
  102. kref_put(&usb_class->kref, release_usb_class);
  103. }
  104. int usb_major_init(void)
  105. {
  106. int error;
  107. error = register_chrdev(USB_MAJOR, "usb", &usb_fops);
  108. if (error)
  109. printk(KERN_ERR "Unable to get major %d for usb devices\n",
  110. USB_MAJOR);
  111. return error;
  112. }
  113. void usb_major_cleanup(void)
  114. {
  115. unregister_chrdev(USB_MAJOR, "usb");
  116. }
  117. /**
  118. * usb_register_dev - register a USB device, and ask for a minor number
  119. * @intf: pointer to the usb_interface that is being registered
  120. * @class_driver: pointer to the usb_class_driver for this device
  121. *
  122. * This should be called by all USB drivers that use the USB major number.
  123. * If CONFIG_USB_DYNAMIC_MINORS is enabled, the minor number will be
  124. * dynamically allocated out of the list of available ones. If it is not
  125. * enabled, the minor number will be based on the next available free minor,
  126. * starting at the class_driver->minor_base.
  127. *
  128. * This function also creates a usb class device in the sysfs tree.
  129. *
  130. * usb_deregister_dev() must be called when the driver is done with
  131. * the minor numbers given out by this function.
  132. *
  133. * Returns -EINVAL if something bad happens with trying to register a
  134. * device, and 0 on success.
  135. */
  136. int usb_register_dev(struct usb_interface *intf,
  137. struct usb_class_driver *class_driver)
  138. {
  139. int retval;
  140. int minor_base = class_driver->minor_base;
  141. int minor;
  142. char name[20];
  143. char *temp;
  144. #ifdef CONFIG_USB_DYNAMIC_MINORS
  145. /*
  146. * We don't care what the device tries to start at, we want to start
  147. * at zero to pack the devices into the smallest available space with
  148. * no holes in the minor range.
  149. */
  150. minor_base = 0;
  151. #endif
  152. if (class_driver->fops == NULL)
  153. return -EINVAL;
  154. if (intf->minor >= 0)
  155. return -EADDRINUSE;
  156. retval = init_usb_class();
  157. if (retval)
  158. return retval;
  159. dev_dbg(&intf->dev, "looking for a minor, starting at %d", minor_base);
  160. down_write(&minor_rwsem);
  161. for (minor = minor_base; minor < MAX_USB_MINORS; ++minor) {
  162. if (usb_minors[minor])
  163. continue;
  164. usb_minors[minor] = class_driver->fops;
  165. intf->minor = minor;
  166. break;
  167. }
  168. if (intf->minor < 0) {
  169. up_write(&minor_rwsem);
  170. return -EXFULL;
  171. }
  172. /* create a usb class device for this usb interface */
  173. snprintf(name, sizeof(name), class_driver->name, minor - minor_base);
  174. temp = strrchr(name, '/');
  175. if (temp && (temp[1] != '\0'))
  176. ++temp;
  177. else
  178. temp = name;
  179. intf->usb_dev = device_create(usb_class->class, &intf->dev,
  180. MKDEV(USB_MAJOR, minor), class_driver,
  181. "%s", temp);
  182. if (IS_ERR(intf->usb_dev)) {
  183. usb_minors[minor] = NULL;
  184. intf->minor = -1;
  185. retval = PTR_ERR(intf->usb_dev);
  186. }
  187. up_write(&minor_rwsem);
  188. return retval;
  189. }
  190. EXPORT_SYMBOL_GPL(usb_register_dev);
  191. /**
  192. * usb_deregister_dev - deregister a USB device's dynamic minor.
  193. * @intf: pointer to the usb_interface that is being deregistered
  194. * @class_driver: pointer to the usb_class_driver for this device
  195. *
  196. * Used in conjunction with usb_register_dev(). This function is called
  197. * when the USB driver is finished with the minor numbers gotten from a
  198. * call to usb_register_dev() (usually when the device is disconnected
  199. * from the system.)
  200. *
  201. * This function also removes the usb class device from the sysfs tree.
  202. *
  203. * This should be called by all drivers that use the USB major number.
  204. */
  205. void usb_deregister_dev(struct usb_interface *intf,
  206. struct usb_class_driver *class_driver)
  207. {
  208. if (intf->minor == -1)
  209. return;
  210. dbg ("removing %d minor", intf->minor);
  211. device_destroy(usb_class->class, MKDEV(USB_MAJOR, intf->minor));
  212. down_write(&minor_rwsem);
  213. usb_minors[intf->minor] = NULL;
  214. up_write(&minor_rwsem);
  215. intf->usb_dev = NULL;
  216. intf->minor = -1;
  217. destroy_usb_class();
  218. }
  219. EXPORT_SYMBOL_GPL(usb_deregister_dev);