file.c 6.1 KB

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