file.c 6.4 KB

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