file.c 6.2 KB

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