misc.c 6.8 KB

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