file.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * drivers/usb/core/file.c
  4. *
  5. * (C) Copyright Linus Torvalds 1999
  6. * (C) Copyright Johannes Erdfelt 1999-2001
  7. * (C) Copyright Andreas Gal 1999
  8. * (C) Copyright Gregory P. Smith 1999
  9. * (C) Copyright Deti Fliegl 1999 (new USB architecture)
  10. * (C) Copyright Randy Dunlap 2000
  11. * (C) Copyright David Brownell 2000-2001 (kernel hotplug, usb_device_id,
  12. * more docs, etc)
  13. * (C) Copyright Yggdrasil Computing, Inc. 2000
  14. * (usb_device_id matching changes by Adam J. Richter)
  15. * (C) Copyright Greg Kroah-Hartman 2002-2003
  16. *
  17. * Released under the GPLv2 only.
  18. */
  19. #include <linux/module.h>
  20. #include <linux/errno.h>
  21. #include <linux/rwsem.h>
  22. #include <linux/slab.h>
  23. #include <linux/string.h>
  24. #include <linux/usb.h>
  25. #include "usb.h"
  26. #define MAX_USB_MINORS 256
  27. static const struct file_operations *usb_minors[MAX_USB_MINORS];
  28. static DECLARE_RWSEM(minor_rwsem);
  29. static int usb_open(struct inode *inode, struct file *file)
  30. {
  31. int err = -ENODEV;
  32. const struct file_operations *new_fops;
  33. down_read(&minor_rwsem);
  34. new_fops = fops_get(usb_minors[iminor(inode)]);
  35. if (!new_fops)
  36. goto done;
  37. replace_fops(file, new_fops);
  38. /* Curiouser and curiouser... NULL ->open() as "no device" ? */
  39. if (file->f_op->open)
  40. err = file->f_op->open(inode, file);
  41. done:
  42. up_read(&minor_rwsem);
  43. return err;
  44. }
  45. static const struct file_operations usb_fops = {
  46. .owner = THIS_MODULE,
  47. .open = usb_open,
  48. .llseek = noop_llseek,
  49. };
  50. static char *usb_devnode(const struct device *dev, umode_t *mode)
  51. {
  52. struct usb_class_driver *drv;
  53. drv = dev_get_drvdata(dev);
  54. if (!drv || !drv->devnode)
  55. return NULL;
  56. return drv->devnode(dev, mode);
  57. }
  58. const struct class usbmisc_class = {
  59. .name = "usbmisc",
  60. .devnode = usb_devnode,
  61. };
  62. int usb_major_init(void)
  63. {
  64. int error;
  65. error = register_chrdev(USB_MAJOR, "usb", &usb_fops);
  66. if (error)
  67. printk(KERN_ERR "Unable to get major %d for usb devices\n",
  68. USB_MAJOR);
  69. return error;
  70. }
  71. void usb_major_cleanup(void)
  72. {
  73. unregister_chrdev(USB_MAJOR, "usb");
  74. }
  75. /**
  76. * usb_register_dev - register a USB device, and ask for a minor number
  77. * @intf: pointer to the usb_interface that is being registered
  78. * @class_driver: pointer to the usb_class_driver for this device
  79. *
  80. * This should be called by all USB drivers that use the USB major number.
  81. * If CONFIG_USB_DYNAMIC_MINORS is enabled, the minor number will be
  82. * dynamically allocated out of the list of available ones. If it is not
  83. * enabled, the minor number will be based on the next available free minor,
  84. * starting at the class_driver->minor_base.
  85. *
  86. * This function also creates a usb class device in the sysfs tree.
  87. *
  88. * usb_deregister_dev() must be called when the driver is done with
  89. * the minor numbers given out by this function.
  90. *
  91. * Return: -EINVAL if something bad happens with trying to register a
  92. * device, and 0 on success.
  93. */
  94. int usb_register_dev(struct usb_interface *intf,
  95. struct usb_class_driver *class_driver)
  96. {
  97. int retval = 0;
  98. int minor_base = class_driver->minor_base;
  99. int minor;
  100. char name[20];
  101. #ifdef CONFIG_USB_DYNAMIC_MINORS
  102. /*
  103. * We don't care what the device tries to start at, we want to start
  104. * at zero to pack the devices into the smallest available space with
  105. * no holes in the minor range.
  106. */
  107. minor_base = 0;
  108. #endif
  109. if (class_driver->fops == NULL)
  110. return -EINVAL;
  111. if (intf->minor >= 0)
  112. return -EADDRINUSE;
  113. dev_dbg(&intf->dev, "looking for a minor, starting at %d\n", minor_base);
  114. down_write(&minor_rwsem);
  115. for (minor = minor_base; minor < MAX_USB_MINORS; ++minor) {
  116. if (usb_minors[minor])
  117. continue;
  118. usb_minors[minor] = class_driver->fops;
  119. intf->minor = minor;
  120. break;
  121. }
  122. if (intf->minor < 0) {
  123. up_write(&minor_rwsem);
  124. return -EXFULL;
  125. }
  126. /* create a usb class device for this usb interface */
  127. snprintf(name, sizeof(name), class_driver->name, minor - minor_base);
  128. intf->usb_dev = device_create(&usbmisc_class, &intf->dev,
  129. MKDEV(USB_MAJOR, minor), class_driver,
  130. "%s", kbasename(name));
  131. if (IS_ERR(intf->usb_dev)) {
  132. usb_minors[minor] = NULL;
  133. intf->minor = -1;
  134. retval = PTR_ERR(intf->usb_dev);
  135. }
  136. up_write(&minor_rwsem);
  137. return retval;
  138. }
  139. EXPORT_SYMBOL_GPL(usb_register_dev);
  140. /**
  141. * usb_deregister_dev - deregister a USB device's dynamic minor.
  142. * @intf: pointer to the usb_interface that is being deregistered
  143. * @class_driver: pointer to the usb_class_driver for this device
  144. *
  145. * Used in conjunction with usb_register_dev(). This function is called
  146. * when the USB driver is finished with the minor numbers gotten from a
  147. * call to usb_register_dev() (usually when the device is disconnected
  148. * from the system.)
  149. *
  150. * This function also removes the usb class device from the sysfs tree.
  151. *
  152. * This should be called by all drivers that use the USB major number.
  153. */
  154. void usb_deregister_dev(struct usb_interface *intf,
  155. struct usb_class_driver *class_driver)
  156. {
  157. if (intf->minor == -1)
  158. return;
  159. dev_dbg(&intf->dev, "removing %d minor\n", intf->minor);
  160. device_destroy(&usbmisc_class, MKDEV(USB_MAJOR, intf->minor));
  161. down_write(&minor_rwsem);
  162. usb_minors[intf->minor] = NULL;
  163. up_write(&minor_rwsem);
  164. intf->usb_dev = NULL;
  165. intf->minor = -1;
  166. }
  167. EXPORT_SYMBOL_GPL(usb_deregister_dev);