misc.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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/config.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/slab.h>
  43. #include <linux/proc_fs.h>
  44. #include <linux/seq_file.h>
  45. #include <linux/devfs_fs_kernel.h>
  46. #include <linux/stat.h>
  47. #include <linux/init.h>
  48. #include <linux/device.h>
  49. #include <linux/tty.h>
  50. #include <linux/kmod.h>
  51. /*
  52. * Head entry for the doubly linked miscdevice list
  53. */
  54. static LIST_HEAD(misc_list);
  55. static DECLARE_MUTEX(misc_sem);
  56. /*
  57. * Assigned numbers, used for dynamic minors
  58. */
  59. #define DYNAMIC_MINORS 64 /* like dynamic majors */
  60. static unsigned char misc_minors[DYNAMIC_MINORS / 8];
  61. extern int pmu_device_init(void);
  62. #ifdef CONFIG_PROC_FS
  63. static void *misc_seq_start(struct seq_file *seq, loff_t *pos)
  64. {
  65. struct miscdevice *p;
  66. loff_t off = 0;
  67. down(&misc_sem);
  68. list_for_each_entry(p, &misc_list, list) {
  69. if (*pos == off++)
  70. return p;
  71. }
  72. return NULL;
  73. }
  74. static void *misc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  75. {
  76. struct list_head *n = ((struct miscdevice *)v)->list.next;
  77. ++*pos;
  78. return (n != &misc_list) ? list_entry(n, struct miscdevice, list)
  79. : NULL;
  80. }
  81. static void misc_seq_stop(struct seq_file *seq, void *v)
  82. {
  83. up(&misc_sem);
  84. }
  85. static int misc_seq_show(struct seq_file *seq, void *v)
  86. {
  87. const struct miscdevice *p = v;
  88. seq_printf(seq, "%3i %s\n", p->minor, p->name ? p->name : "");
  89. return 0;
  90. }
  91. static struct seq_operations misc_seq_ops = {
  92. .start = misc_seq_start,
  93. .next = misc_seq_next,
  94. .stop = misc_seq_stop,
  95. .show = misc_seq_show,
  96. };
  97. static int misc_seq_open(struct inode *inode, struct file *file)
  98. {
  99. return seq_open(file, &misc_seq_ops);
  100. }
  101. static struct file_operations misc_proc_fops = {
  102. .owner = THIS_MODULE,
  103. .open = misc_seq_open,
  104. .read = seq_read,
  105. .llseek = seq_lseek,
  106. .release = seq_release,
  107. };
  108. #endif
  109. static int misc_open(struct inode * inode, struct file * file)
  110. {
  111. int minor = iminor(inode);
  112. struct miscdevice *c;
  113. int err = -ENODEV;
  114. const struct file_operations *old_fops, *new_fops = NULL;
  115. down(&misc_sem);
  116. list_for_each_entry(c, &misc_list, list) {
  117. if (c->minor == minor) {
  118. new_fops = fops_get(c->fops);
  119. break;
  120. }
  121. }
  122. if (!new_fops) {
  123. up(&misc_sem);
  124. request_module("char-major-%d-%d", MISC_MAJOR, minor);
  125. down(&misc_sem);
  126. list_for_each_entry(c, &misc_list, list) {
  127. if (c->minor == minor) {
  128. new_fops = fops_get(c->fops);
  129. break;
  130. }
  131. }
  132. if (!new_fops)
  133. goto fail;
  134. }
  135. err = 0;
  136. old_fops = file->f_op;
  137. file->f_op = new_fops;
  138. if (file->f_op->open) {
  139. err=file->f_op->open(inode,file);
  140. if (err) {
  141. fops_put(file->f_op);
  142. file->f_op = fops_get(old_fops);
  143. }
  144. }
  145. fops_put(old_fops);
  146. fail:
  147. up(&misc_sem);
  148. return err;
  149. }
  150. /*
  151. * TODO for 2.7:
  152. * - add a struct kref to struct miscdevice and make all usages of
  153. * them dynamic.
  154. */
  155. static struct class *misc_class;
  156. static struct file_operations misc_fops = {
  157. .owner = THIS_MODULE,
  158. .open = misc_open,
  159. };
  160. /**
  161. * misc_register - register a miscellaneous device
  162. * @misc: device structure
  163. *
  164. * Register a miscellaneous device with the kernel. If the minor
  165. * number is set to %MISC_DYNAMIC_MINOR a minor number is assigned
  166. * and placed in the minor field of the structure. For other cases
  167. * the minor number requested is used.
  168. *
  169. * The structure passed is linked into the kernel and may not be
  170. * destroyed until it has been unregistered.
  171. *
  172. * A zero is returned on success and a negative errno code for
  173. * failure.
  174. */
  175. int misc_register(struct miscdevice * misc)
  176. {
  177. struct miscdevice *c;
  178. dev_t dev;
  179. int err;
  180. down(&misc_sem);
  181. list_for_each_entry(c, &misc_list, list) {
  182. if (c->minor == misc->minor) {
  183. up(&misc_sem);
  184. return -EBUSY;
  185. }
  186. }
  187. if (misc->minor == MISC_DYNAMIC_MINOR) {
  188. int i = DYNAMIC_MINORS;
  189. while (--i >= 0)
  190. if ( (misc_minors[i>>3] & (1 << (i&7))) == 0)
  191. break;
  192. if (i<0) {
  193. up(&misc_sem);
  194. return -EBUSY;
  195. }
  196. misc->minor = i;
  197. }
  198. if (misc->minor < DYNAMIC_MINORS)
  199. misc_minors[misc->minor >> 3] |= 1 << (misc->minor & 7);
  200. if (misc->devfs_name[0] == '\0') {
  201. snprintf(misc->devfs_name, sizeof(misc->devfs_name),
  202. "misc/%s", misc->name);
  203. }
  204. dev = MKDEV(MISC_MAJOR, misc->minor);
  205. misc->class = class_device_create(misc_class, NULL, dev, misc->dev,
  206. "%s", misc->name);
  207. if (IS_ERR(misc->class)) {
  208. err = PTR_ERR(misc->class);
  209. goto out;
  210. }
  211. err = devfs_mk_cdev(dev, S_IFCHR|S_IRUSR|S_IWUSR|S_IRGRP,
  212. misc->devfs_name);
  213. if (err) {
  214. class_device_destroy(misc_class, dev);
  215. goto out;
  216. }
  217. /*
  218. * Add it to the front, so that later devices can "override"
  219. * earlier defaults
  220. */
  221. list_add(&misc->list, &misc_list);
  222. out:
  223. up(&misc_sem);
  224. return err;
  225. }
  226. /**
  227. * misc_deregister - unregister a miscellaneous device
  228. * @misc: device to unregister
  229. *
  230. * Unregister a miscellaneous device that was previously
  231. * successfully registered with misc_register(). Success
  232. * is indicated by a zero return, a negative errno code
  233. * indicates an error.
  234. */
  235. int misc_deregister(struct miscdevice * misc)
  236. {
  237. int i = misc->minor;
  238. if (list_empty(&misc->list))
  239. return -EINVAL;
  240. down(&misc_sem);
  241. list_del(&misc->list);
  242. class_device_destroy(misc_class, MKDEV(MISC_MAJOR, misc->minor));
  243. devfs_remove(misc->devfs_name);
  244. if (i < DYNAMIC_MINORS && i>0) {
  245. misc_minors[i>>3] &= ~(1 << (misc->minor & 7));
  246. }
  247. up(&misc_sem);
  248. return 0;
  249. }
  250. EXPORT_SYMBOL(misc_register);
  251. EXPORT_SYMBOL(misc_deregister);
  252. static int __init misc_init(void)
  253. {
  254. #ifdef CONFIG_PROC_FS
  255. struct proc_dir_entry *ent;
  256. ent = create_proc_entry("misc", 0, NULL);
  257. if (ent)
  258. ent->proc_fops = &misc_proc_fops;
  259. #endif
  260. misc_class = class_create(THIS_MODULE, "misc");
  261. if (IS_ERR(misc_class))
  262. return PTR_ERR(misc_class);
  263. if (register_chrdev(MISC_MAJOR,"misc",&misc_fops)) {
  264. printk("unable to get major %d for misc devices\n",
  265. MISC_MAJOR);
  266. class_destroy(misc_class);
  267. return -EIO;
  268. }
  269. return 0;
  270. }
  271. subsys_initcall(misc_init);