misc.c 7.7 KB

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