/drivers/char/misc.c

https://bitbucket.org/evzijst/gittest · C · 331 lines · 217 code · 41 blank · 73 comment · 30 complexity · c594e3ac7bc3dd43ee99d2cf9777fe6a MD5 · raw file

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