misc.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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.
  58. */
  59. static DEFINE_IDA(misc_minors_ida);
  60. static int misc_minor_alloc(int minor)
  61. {
  62. int ret = 0;
  63. if (minor == MISC_DYNAMIC_MINOR) {
  64. /* allocate free id */
  65. ret = ida_alloc_range(&misc_minors_ida, MISC_DYNAMIC_MINOR + 1,
  66. MINORMASK, GFP_KERNEL);
  67. } else {
  68. ret = ida_alloc_range(&misc_minors_ida, minor, minor, GFP_KERNEL);
  69. }
  70. return ret;
  71. }
  72. static void misc_minor_free(int minor)
  73. {
  74. ida_free(&misc_minors_ida, minor);
  75. }
  76. #ifdef CONFIG_PROC_FS
  77. static void *misc_seq_start(struct seq_file *seq, loff_t *pos)
  78. {
  79. mutex_lock(&misc_mtx);
  80. return seq_list_start(&misc_list, *pos);
  81. }
  82. static void *misc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  83. {
  84. return seq_list_next(v, &misc_list, pos);
  85. }
  86. static void misc_seq_stop(struct seq_file *seq, void *v)
  87. {
  88. mutex_unlock(&misc_mtx);
  89. }
  90. static int misc_seq_show(struct seq_file *seq, void *v)
  91. {
  92. const struct miscdevice *p = list_entry(v, struct miscdevice, list);
  93. seq_printf(seq, "%3i %s\n", p->minor, p->name ? p->name : "");
  94. return 0;
  95. }
  96. static const struct seq_operations misc_seq_ops = {
  97. .start = misc_seq_start,
  98. .next = misc_seq_next,
  99. .stop = misc_seq_stop,
  100. .show = misc_seq_show,
  101. };
  102. #endif
  103. static int misc_open(struct inode *inode, struct file *file)
  104. {
  105. int minor = iminor(inode);
  106. struct miscdevice *c = NULL, *iter;
  107. int err = -ENODEV;
  108. const struct file_operations *new_fops = NULL;
  109. mutex_lock(&misc_mtx);
  110. list_for_each_entry(iter, &misc_list, list) {
  111. if (iter->minor != minor)
  112. continue;
  113. c = iter;
  114. new_fops = fops_get(iter->fops);
  115. break;
  116. }
  117. /* Only request module for fixed minor code */
  118. if (!new_fops && minor < MISC_DYNAMIC_MINOR) {
  119. mutex_unlock(&misc_mtx);
  120. request_module("char-major-%d-%d", MISC_MAJOR, minor);
  121. mutex_lock(&misc_mtx);
  122. list_for_each_entry(iter, &misc_list, list) {
  123. if (iter->minor != minor)
  124. continue;
  125. c = iter;
  126. new_fops = fops_get(iter->fops);
  127. break;
  128. }
  129. }
  130. if (!new_fops)
  131. goto fail;
  132. /*
  133. * Place the miscdevice in the file's
  134. * private_data so it can be used by the
  135. * file operations, including f_op->open below
  136. */
  137. file->private_data = c;
  138. err = 0;
  139. replace_fops(file, new_fops);
  140. if (file->f_op->open)
  141. err = file->f_op->open(inode, file);
  142. fail:
  143. mutex_unlock(&misc_mtx);
  144. return err;
  145. }
  146. static char *misc_devnode(const struct device *dev, umode_t *mode)
  147. {
  148. const struct miscdevice *c = dev_get_drvdata(dev);
  149. if (mode && c->mode)
  150. *mode = c->mode;
  151. if (c->nodename)
  152. return kstrdup(c->nodename, GFP_KERNEL);
  153. return NULL;
  154. }
  155. static const struct class misc_class = {
  156. .name = "misc",
  157. .devnode = misc_devnode,
  158. };
  159. static const struct file_operations misc_fops = {
  160. .owner = THIS_MODULE,
  161. .open = misc_open,
  162. .llseek = noop_llseek,
  163. };
  164. /**
  165. * misc_register - register a miscellaneous device
  166. * @misc: device structure
  167. *
  168. * Register a miscellaneous device with the kernel. If the minor
  169. * number is set to %MISC_DYNAMIC_MINOR a minor number is assigned
  170. * and placed in the minor field of the structure. For other cases
  171. * the minor number requested is used.
  172. *
  173. * The structure passed is linked into the kernel and may not be
  174. * destroyed until it has been unregistered. By default, an open()
  175. * syscall to the device sets file->private_data to point to the
  176. * structure. Drivers don't need open in fops for this.
  177. *
  178. * A zero is returned on success and a negative errno code for
  179. * failure.
  180. */
  181. int misc_register(struct miscdevice *misc)
  182. {
  183. dev_t dev;
  184. int err = 0;
  185. bool is_dynamic = (misc->minor == MISC_DYNAMIC_MINOR);
  186. if (misc->minor > MISC_DYNAMIC_MINOR) {
  187. pr_err("Invalid fixed minor %d for miscdevice '%s'\n",
  188. misc->minor, misc->name);
  189. return -EINVAL;
  190. }
  191. INIT_LIST_HEAD(&misc->list);
  192. mutex_lock(&misc_mtx);
  193. if (is_dynamic) {
  194. int i = misc_minor_alloc(misc->minor);
  195. if (i < 0) {
  196. err = -EBUSY;
  197. goto out;
  198. }
  199. misc->minor = i;
  200. } else {
  201. struct miscdevice *c;
  202. int i;
  203. list_for_each_entry(c, &misc_list, list) {
  204. if (c->minor == misc->minor) {
  205. err = -EBUSY;
  206. goto out;
  207. }
  208. }
  209. i = misc_minor_alloc(misc->minor);
  210. if (i < 0) {
  211. err = -EBUSY;
  212. goto out;
  213. }
  214. }
  215. dev = MKDEV(MISC_MAJOR, misc->minor);
  216. misc->this_device =
  217. device_create_with_groups(&misc_class, misc->parent, dev,
  218. misc, misc->groups, "%s", misc->name);
  219. if (IS_ERR(misc->this_device)) {
  220. misc_minor_free(misc->minor);
  221. if (is_dynamic) {
  222. misc->minor = MISC_DYNAMIC_MINOR;
  223. }
  224. err = PTR_ERR(misc->this_device);
  225. goto out;
  226. }
  227. /*
  228. * Add it to the front, so that later devices can "override"
  229. * earlier defaults
  230. */
  231. list_add(&misc->list, &misc_list);
  232. out:
  233. mutex_unlock(&misc_mtx);
  234. return err;
  235. }
  236. EXPORT_SYMBOL(misc_register);
  237. /**
  238. * misc_deregister - unregister a miscellaneous device
  239. * @misc: device to unregister
  240. *
  241. * Unregister a miscellaneous device that was previously
  242. * successfully registered with misc_register().
  243. */
  244. void misc_deregister(struct miscdevice *misc)
  245. {
  246. mutex_lock(&misc_mtx);
  247. list_del_init(&misc->list);
  248. device_destroy(&misc_class, MKDEV(MISC_MAJOR, misc->minor));
  249. misc_minor_free(misc->minor);
  250. if (misc->minor > MISC_DYNAMIC_MINOR)
  251. misc->minor = MISC_DYNAMIC_MINOR;
  252. mutex_unlock(&misc_mtx);
  253. }
  254. EXPORT_SYMBOL(misc_deregister);
  255. static int __init misc_init(void)
  256. {
  257. int err;
  258. struct proc_dir_entry *misc_proc_file;
  259. misc_proc_file = proc_create_seq("misc", 0, NULL, &misc_seq_ops);
  260. err = class_register(&misc_class);
  261. if (err)
  262. goto fail_remove;
  263. err = __register_chrdev(MISC_MAJOR, 0, MINORMASK + 1, "misc", &misc_fops);
  264. if (err < 0)
  265. goto fail_printk;
  266. return 0;
  267. fail_printk:
  268. pr_err("unable to get major %d for misc devices\n", MISC_MAJOR);
  269. class_unregister(&misc_class);
  270. fail_remove:
  271. if (misc_proc_file)
  272. remove_proc_entry("misc", NULL);
  273. return err;
  274. }
  275. subsys_initcall(misc_init);