file.c 6.0 KB

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