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