misc.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*
  2. * linux/drivers/char/misc.c
  3. *
  4. * Generic misc open routine by Johan Myreen
  5. *
  6. * Based on code from Linus
  7. *
  8. * Teemu Rantanen's Microsoft Busmouse support and Derrick Cole's
  9. * changes incorporated into 0.97pl4
  10. * by Peter Cervasio (pete%q106fm.uucp@wupost.wustl.edu) (08SEP92)
  11. * See busmouse.c for particulars.
  12. *
  13. * Made things a lot mode modular - easy to compile in just one or two
  14. * of the misc drivers, as they are now completely independent. Linus.
  15. *
  16. * Support for loadable modules. 8-Sep-95 Philip Blundell <pjb27@cam.ac.uk>
  17. *
  18. * Fixed a failing symbol register to free the device registration
  19. * Alan Cox <alan@lxorguk.ukuu.org.uk> 21-Jan-96
  20. *
  21. * Dynamic minors and /proc/mice by Alessandro Rubini. 26-Mar-96
  22. *
  23. * Renamed to misc and miscdevice to be more accurate. Alan Cox 26-Mar-96
  24. *
  25. * Handling of mouse minor numbers for kerneld:
  26. * Idea by Jacques Gelinas <jack@solucorp.qc.ca>,
  27. * adapted by Bjorn Ekwall <bj0rn@blox.se>
  28. * corrected by Alan Cox <alan@lxorguk.ukuu.org.uk>
  29. *
  30. * Changes for kmod (from kerneld):
  31. * Cyrus Durgin <cider@speakeasy.org>
  32. *
  33. * Added devfs support. Richard Gooch <rgooch@atnf.csiro.au> 10-Jan-1998
  34. */
  35. #include <linux/module.h>
  36. #include <linux/fs.h>
  37. #include <linux/errno.h>
  38. #include <linux/miscdevice.h>
  39. #include <linux/kernel.h>
  40. #include <linux/major.h>
  41. #include <linux/mutex.h>
  42. #include <linux/proc_fs.h>
  43. #include <linux/seq_file.h>
  44. #include <linux/stat.h>
  45. #include <linux/init.h>
  46. #include <linux/device.h>
  47. #include <linux/tty.h>
  48. #include <linux/kmod.h>
  49. #include <linux/gfp.h>
  50. /*
  51. * Head entry for the doubly linked miscdevice list
  52. */
  53. static LIST_HEAD(misc_list);
  54. static DEFINE_MUTEX(misc_mtx);
  55. /*
  56. * Assigned numbers, used for dynamic minors
  57. */
  58. #define DYNAMIC_MINORS 64 /* like dynamic majors */
  59. static DECLARE_BITMAP(misc_minors, DYNAMIC_MINORS);
  60. #ifdef CONFIG_PROC_FS
  61. static void *misc_seq_start(struct seq_file *seq, loff_t *pos)
  62. {
  63. mutex_lock(&misc_mtx);
  64. return seq_list_start(&misc_list, *pos);
  65. }
  66. static void *misc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  67. {
  68. return seq_list_next(v, &misc_list, pos);
  69. }
  70. static void misc_seq_stop(struct seq_file *seq, void *v)
  71. {
  72. mutex_unlock(&misc_mtx);
  73. }
  74. static int misc_seq_show(struct seq_file *seq, void *v)
  75. {
  76. const struct miscdevice *p = list_entry(v, struct miscdevice, list);
  77. seq_printf(seq, "%3i %s\n", p->minor, p->name ? p->name : "");
  78. return 0;
  79. }
  80. static const struct seq_operations misc_seq_ops = {
  81. .start = misc_seq_start,
  82. .next = misc_seq_next,
  83. .stop = misc_seq_stop,
  84. .show = misc_seq_show,
  85. };
  86. static int misc_seq_open(struct inode *inode, struct file *file)
  87. {
  88. return seq_open(file, &misc_seq_ops);
  89. }
  90. static const struct file_operations misc_proc_fops = {
  91. .owner = THIS_MODULE,
  92. .open = misc_seq_open,
  93. .read = seq_read,
  94. .llseek = seq_lseek,
  95. .release = seq_release,
  96. };
  97. #endif
  98. static int misc_open(struct inode * inode, struct file * file)
  99. {
  100. int minor = iminor(inode);
  101. struct miscdevice *c;
  102. int err = -ENODEV;
  103. const struct file_operations *new_fops = NULL;
  104. mutex_lock(&misc_mtx);
  105. list_for_each_entry(c, &misc_list, list) {
  106. if (c->minor == minor) {
  107. new_fops = fops_get(c->fops);
  108. break;
  109. }
  110. }
  111. if (!new_fops) {
  112. mutex_unlock(&misc_mtx);
  113. request_module("char-major-%d-%d", MISC_MAJOR, minor);
  114. mutex_lock(&misc_mtx);
  115. list_for_each_entry(c, &misc_list, list) {
  116. if (c->minor == minor) {
  117. new_fops = fops_get(c->fops);
  118. break;
  119. }
  120. }
  121. if (!new_fops)
  122. goto fail;
  123. }
  124. /*
  125. * Place the miscdevice in the file's
  126. * private_data so it can be used by the
  127. * file operations, including f_op->open below
  128. */
  129. file->private_data = c;
  130. err = 0;
  131. replace_fops(file, new_fops);
  132. if (file->f_op->open)
  133. err = file->f_op->open(inode,file);
  134. fail:
  135. mutex_unlock(&misc_mtx);
  136. return err;
  137. }
  138. static struct class *misc_class;
  139. static const struct file_operations misc_fops = {
  140. .owner = THIS_MODULE,
  141. .open = misc_open,
  142. .llseek = noop_llseek,
  143. };
  144. /**
  145. * misc_register - register a miscellaneous device
  146. * @misc: device structure
  147. *
  148. * Register a miscellaneous device with the kernel. If the minor
  149. * number is set to %MISC_DYNAMIC_MINOR a minor number is assigned
  150. * and placed in the minor field of the structure. For other cases
  151. * the minor number requested is used.
  152. *
  153. * The structure passed is linked into the kernel and may not be
  154. * destroyed until it has been unregistered. By default, an open()
  155. * syscall to the device sets file->private_data to point to the
  156. * structure. Drivers don't need open in fops for this.
  157. *
  158. * A zero is returned on success and a negative errno code for
  159. * failure.
  160. */
  161. int misc_register(struct miscdevice * misc)
  162. {
  163. dev_t dev;
  164. int err = 0;
  165. bool is_dynamic = (misc->minor == MISC_DYNAMIC_MINOR);
  166. INIT_LIST_HEAD(&misc->list);
  167. mutex_lock(&misc_mtx);
  168. if (is_dynamic) {
  169. int i = find_first_zero_bit(misc_minors, DYNAMIC_MINORS);
  170. if (i >= DYNAMIC_MINORS) {
  171. err = -EBUSY;
  172. goto out;
  173. }
  174. misc->minor = DYNAMIC_MINORS - i - 1;
  175. set_bit(i, misc_minors);
  176. } else {
  177. struct miscdevice *c;
  178. list_for_each_entry(c, &misc_list, list) {
  179. if (c->minor == misc->minor) {
  180. err = -EBUSY;
  181. goto out;
  182. }
  183. }
  184. }
  185. dev = MKDEV(MISC_MAJOR, misc->minor);
  186. misc->this_device =
  187. device_create_with_groups(misc_class, misc->parent, dev,
  188. misc, misc->groups, "%s", misc->name);
  189. if (IS_ERR(misc->this_device)) {
  190. if (is_dynamic) {
  191. int i = DYNAMIC_MINORS - misc->minor - 1;
  192. if (i < DYNAMIC_MINORS && i >= 0)
  193. clear_bit(i, misc_minors);
  194. misc->minor = MISC_DYNAMIC_MINOR;
  195. }
  196. err = PTR_ERR(misc->this_device);
  197. goto out;
  198. }
  199. /*
  200. * Add it to the front, so that later devices can "override"
  201. * earlier defaults
  202. */
  203. list_add(&misc->list, &misc_list);
  204. out:
  205. mutex_unlock(&misc_mtx);
  206. return err;
  207. }
  208. /**
  209. * misc_deregister - unregister a miscellaneous device
  210. * @misc: device to unregister
  211. *
  212. * Unregister a miscellaneous device that was previously
  213. * successfully registered with misc_register().
  214. */
  215. void misc_deregister(struct miscdevice *misc)
  216. {
  217. int i = DYNAMIC_MINORS - misc->minor - 1;
  218. if (WARN_ON(list_empty(&misc->list)))
  219. return;
  220. mutex_lock(&misc_mtx);
  221. list_del(&misc->list);
  222. device_destroy(misc_class, MKDEV(MISC_MAJOR, misc->minor));
  223. if (i < DYNAMIC_MINORS && i >= 0)
  224. clear_bit(i, misc_minors);
  225. mutex_unlock(&misc_mtx);
  226. }
  227. EXPORT_SYMBOL(misc_register);
  228. EXPORT_SYMBOL(misc_deregister);
  229. static char *misc_devnode(struct device *dev, umode_t *mode)
  230. {
  231. struct miscdevice *c = dev_get_drvdata(dev);
  232. if (mode && c->mode)
  233. *mode = c->mode;
  234. if (c->nodename)
  235. return kstrdup(c->nodename, GFP_KERNEL);
  236. return NULL;
  237. }
  238. static int __init misc_init(void)
  239. {
  240. int err;
  241. struct proc_dir_entry *ret;
  242. ret = proc_create("misc", 0, NULL, &misc_proc_fops);
  243. misc_class = class_create(THIS_MODULE, "misc");
  244. err = PTR_ERR(misc_class);
  245. if (IS_ERR(misc_class))
  246. goto fail_remove;
  247. err = -EIO;
  248. if (register_chrdev(MISC_MAJOR,"misc",&misc_fops))
  249. goto fail_printk;
  250. misc_class->devnode = misc_devnode;
  251. return 0;
  252. fail_printk:
  253. printk("unable to get major %d for misc devices\n", MISC_MAJOR);
  254. class_destroy(misc_class);
  255. fail_remove:
  256. if (ret)
  257. remove_proc_entry("misc", NULL);
  258. return err;
  259. }
  260. subsys_initcall(misc_init);