file.c 5.8 KB

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