file.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 class *usb_class;
  56. int usb_major_init(void)
  57. {
  58. int error;
  59. error = register_chrdev(USB_MAJOR, "usb", &usb_fops);
  60. if (error) {
  61. err("unable to get major %d for usb devices", USB_MAJOR);
  62. goto out;
  63. }
  64. usb_class = class_create(THIS_MODULE, "usb");
  65. if (IS_ERR(usb_class)) {
  66. error = PTR_ERR(usb_class);
  67. err("class_create failed for usb devices");
  68. unregister_chrdev(USB_MAJOR, "usb");
  69. goto out;
  70. }
  71. out:
  72. return error;
  73. }
  74. void usb_major_cleanup(void)
  75. {
  76. class_destroy(usb_class);
  77. unregister_chrdev(USB_MAJOR, "usb");
  78. }
  79. /**
  80. * usb_register_dev - register a USB device, and ask for a minor number
  81. * @intf: pointer to the usb_interface that is being registered
  82. * @class_driver: pointer to the usb_class_driver for this device
  83. *
  84. * This should be called by all USB drivers that use the USB major number.
  85. * If CONFIG_USB_DYNAMIC_MINORS is enabled, the minor number will be
  86. * dynamically allocated out of the list of available ones. If it is not
  87. * enabled, the minor number will be based on the next available free minor,
  88. * starting at the class_driver->minor_base.
  89. *
  90. * This function also creates a usb class device in the sysfs tree.
  91. *
  92. * usb_deregister_dev() must be called when the driver is done with
  93. * the minor numbers given out by this function.
  94. *
  95. * Returns -EINVAL if something bad happens with trying to register a
  96. * device, and 0 on success.
  97. */
  98. int usb_register_dev(struct usb_interface *intf,
  99. struct usb_class_driver *class_driver)
  100. {
  101. int retval = -EINVAL;
  102. int minor_base = class_driver->minor_base;
  103. int minor = 0;
  104. char name[BUS_ID_SIZE];
  105. char *temp;
  106. #ifdef CONFIG_USB_DYNAMIC_MINORS
  107. /*
  108. * We don't care what the device tries to start at, we want to start
  109. * at zero to pack the devices into the smallest available space with
  110. * no holes in the minor range.
  111. */
  112. minor_base = 0;
  113. #endif
  114. intf->minor = -1;
  115. dbg ("looking for a minor, starting at %d", minor_base);
  116. if (class_driver->fops == NULL)
  117. goto exit;
  118. spin_lock (&minor_lock);
  119. for (minor = minor_base; minor < MAX_USB_MINORS; ++minor) {
  120. if (usb_minors[minor])
  121. continue;
  122. usb_minors[minor] = class_driver->fops;
  123. retval = 0;
  124. break;
  125. }
  126. spin_unlock (&minor_lock);
  127. if (retval)
  128. goto exit;
  129. intf->minor = minor;
  130. /* create a usb class device for this usb interface */
  131. snprintf(name, BUS_ID_SIZE, class_driver->name, minor - minor_base);
  132. temp = strrchr(name, '/');
  133. if (temp && (temp[1] != 0x00))
  134. ++temp;
  135. else
  136. temp = name;
  137. intf->class_dev = class_device_create(usb_class, NULL,
  138. MKDEV(USB_MAJOR, minor),
  139. &intf->dev, "%s", temp);
  140. if (IS_ERR(intf->class_dev)) {
  141. spin_lock (&minor_lock);
  142. usb_minors[intf->minor] = NULL;
  143. spin_unlock (&minor_lock);
  144. retval = PTR_ERR(intf->class_dev);
  145. }
  146. exit:
  147. return retval;
  148. }
  149. EXPORT_SYMBOL(usb_register_dev);
  150. /**
  151. * usb_deregister_dev - deregister a USB device's dynamic minor.
  152. * @intf: pointer to the usb_interface that is being deregistered
  153. * @class_driver: pointer to the usb_class_driver for this device
  154. *
  155. * Used in conjunction with usb_register_dev(). This function is called
  156. * when the USB driver is finished with the minor numbers gotten from a
  157. * call to usb_register_dev() (usually when the device is disconnected
  158. * from the system.)
  159. *
  160. * This function also removes the usb class device from the sysfs tree.
  161. *
  162. * This should be called by all drivers that use the USB major number.
  163. */
  164. void usb_deregister_dev(struct usb_interface *intf,
  165. struct usb_class_driver *class_driver)
  166. {
  167. int minor_base = class_driver->minor_base;
  168. char name[BUS_ID_SIZE];
  169. #ifdef CONFIG_USB_DYNAMIC_MINORS
  170. minor_base = 0;
  171. #endif
  172. if (intf->minor == -1)
  173. return;
  174. dbg ("removing %d minor", intf->minor);
  175. spin_lock (&minor_lock);
  176. usb_minors[intf->minor] = NULL;
  177. spin_unlock (&minor_lock);
  178. snprintf(name, BUS_ID_SIZE, class_driver->name, intf->minor - minor_base);
  179. class_device_destroy(usb_class, MKDEV(USB_MAJOR, intf->minor));
  180. intf->class_dev = NULL;
  181. intf->minor = -1;
  182. }
  183. EXPORT_SYMBOL(usb_deregister_dev);