file.c 6.2 KB

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