file.c 5.8 KB

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