misc.c 7.1 KB

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