char_dev.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. /*
  2. * linux/fs/char_dev.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. */
  6. #include <linux/config.h>
  7. #include <linux/init.h>
  8. #include <linux/fs.h>
  9. #include <linux/slab.h>
  10. #include <linux/string.h>
  11. #include <linux/major.h>
  12. #include <linux/errno.h>
  13. #include <linux/module.h>
  14. #include <linux/smp_lock.h>
  15. #include <linux/devfs_fs_kernel.h>
  16. #include <linux/kobject.h>
  17. #include <linux/kobj_map.h>
  18. #include <linux/cdev.h>
  19. #ifdef CONFIG_KMOD
  20. #include <linux/kmod.h>
  21. #endif
  22. static struct kobj_map *cdev_map;
  23. /* degrade to linked list for small systems */
  24. #define MAX_PROBE_HASH (CONFIG_BASE_SMALL ? 1 : 255)
  25. static DECLARE_MUTEX(chrdevs_lock);
  26. static struct char_device_struct {
  27. struct char_device_struct *next;
  28. unsigned int major;
  29. unsigned int baseminor;
  30. int minorct;
  31. const char *name;
  32. struct file_operations *fops;
  33. struct cdev *cdev; /* will die */
  34. } *chrdevs[MAX_PROBE_HASH];
  35. /* index in the above */
  36. static inline int major_to_index(int major)
  37. {
  38. return major % MAX_PROBE_HASH;
  39. }
  40. /* get char device names in somewhat random order */
  41. int get_chrdev_list(char *page)
  42. {
  43. struct char_device_struct *cd;
  44. int i, len;
  45. len = sprintf(page, "Character devices:\n");
  46. down(&chrdevs_lock);
  47. for (i = 0; i < ARRAY_SIZE(chrdevs) ; i++) {
  48. for (cd = chrdevs[i]; cd; cd = cd->next)
  49. len += sprintf(page+len, "%3d %s\n",
  50. cd->major, cd->name);
  51. }
  52. up(&chrdevs_lock);
  53. return len;
  54. }
  55. /*
  56. * Register a single major with a specified minor range.
  57. *
  58. * If major == 0 this functions will dynamically allocate a major and return
  59. * its number.
  60. *
  61. * If major > 0 this function will attempt to reserve the passed range of
  62. * minors and will return zero on success.
  63. *
  64. * Returns a -ve errno on failure.
  65. */
  66. static struct char_device_struct *
  67. __register_chrdev_region(unsigned int major, unsigned int baseminor,
  68. int minorct, const char *name)
  69. {
  70. struct char_device_struct *cd, **cp;
  71. int ret = 0;
  72. int i;
  73. cd = kmalloc(sizeof(struct char_device_struct), GFP_KERNEL);
  74. if (cd == NULL)
  75. return ERR_PTR(-ENOMEM);
  76. memset(cd, 0, sizeof(struct char_device_struct));
  77. down(&chrdevs_lock);
  78. /* temporary */
  79. if (major == 0) {
  80. for (i = ARRAY_SIZE(chrdevs)-1; i > 0; i--) {
  81. if (chrdevs[i] == NULL)
  82. break;
  83. }
  84. if (i == 0) {
  85. ret = -EBUSY;
  86. goto out;
  87. }
  88. major = i;
  89. ret = major;
  90. }
  91. cd->major = major;
  92. cd->baseminor = baseminor;
  93. cd->minorct = minorct;
  94. cd->name = name;
  95. i = major_to_index(major);
  96. for (cp = &chrdevs[i]; *cp; cp = &(*cp)->next)
  97. if ((*cp)->major > major ||
  98. ((*cp)->major == major && (*cp)->baseminor >= baseminor))
  99. break;
  100. if (*cp && (*cp)->major == major &&
  101. (*cp)->baseminor < baseminor + minorct) {
  102. ret = -EBUSY;
  103. goto out;
  104. }
  105. cd->next = *cp;
  106. *cp = cd;
  107. up(&chrdevs_lock);
  108. return cd;
  109. out:
  110. up(&chrdevs_lock);
  111. kfree(cd);
  112. return ERR_PTR(ret);
  113. }
  114. static struct char_device_struct *
  115. __unregister_chrdev_region(unsigned major, unsigned baseminor, int minorct)
  116. {
  117. struct char_device_struct *cd = NULL, **cp;
  118. int i = major_to_index(major);
  119. up(&chrdevs_lock);
  120. for (cp = &chrdevs[i]; *cp; cp = &(*cp)->next)
  121. if ((*cp)->major == major &&
  122. (*cp)->baseminor == baseminor &&
  123. (*cp)->minorct == minorct)
  124. break;
  125. if (*cp) {
  126. cd = *cp;
  127. *cp = cd->next;
  128. }
  129. up(&chrdevs_lock);
  130. return cd;
  131. }
  132. int register_chrdev_region(dev_t from, unsigned count, const char *name)
  133. {
  134. struct char_device_struct *cd;
  135. dev_t to = from + count;
  136. dev_t n, next;
  137. for (n = from; n < to; n = next) {
  138. next = MKDEV(MAJOR(n)+1, 0);
  139. if (next > to)
  140. next = to;
  141. cd = __register_chrdev_region(MAJOR(n), MINOR(n),
  142. next - n, name);
  143. if (IS_ERR(cd))
  144. goto fail;
  145. }
  146. return 0;
  147. fail:
  148. to = n;
  149. for (n = from; n < to; n = next) {
  150. next = MKDEV(MAJOR(n)+1, 0);
  151. kfree(__unregister_chrdev_region(MAJOR(n), MINOR(n), next - n));
  152. }
  153. return PTR_ERR(cd);
  154. }
  155. int alloc_chrdev_region(dev_t *dev, unsigned baseminor, unsigned count,
  156. const char *name)
  157. {
  158. struct char_device_struct *cd;
  159. cd = __register_chrdev_region(0, baseminor, count, name);
  160. if (IS_ERR(cd))
  161. return PTR_ERR(cd);
  162. *dev = MKDEV(cd->major, cd->baseminor);
  163. return 0;
  164. }
  165. int register_chrdev(unsigned int major, const char *name,
  166. struct file_operations *fops)
  167. {
  168. struct char_device_struct *cd;
  169. struct cdev *cdev;
  170. char *s;
  171. int err = -ENOMEM;
  172. cd = __register_chrdev_region(major, 0, 256, name);
  173. if (IS_ERR(cd))
  174. return PTR_ERR(cd);
  175. cdev = cdev_alloc();
  176. if (!cdev)
  177. goto out2;
  178. cdev->owner = fops->owner;
  179. cdev->ops = fops;
  180. kobject_set_name(&cdev->kobj, "%s", name);
  181. for (s = strchr(kobject_name(&cdev->kobj),'/'); s; s = strchr(s, '/'))
  182. *s = '!';
  183. err = cdev_add(cdev, MKDEV(cd->major, 0), 256);
  184. if (err)
  185. goto out;
  186. cd->cdev = cdev;
  187. return major ? 0 : cd->major;
  188. out:
  189. kobject_put(&cdev->kobj);
  190. out2:
  191. kfree(__unregister_chrdev_region(cd->major, 0, 256));
  192. return err;
  193. }
  194. void unregister_chrdev_region(dev_t from, unsigned count)
  195. {
  196. dev_t to = from + count;
  197. dev_t n, next;
  198. for (n = from; n < to; n = next) {
  199. next = MKDEV(MAJOR(n)+1, 0);
  200. if (next > to)
  201. next = to;
  202. kfree(__unregister_chrdev_region(MAJOR(n), MINOR(n), next - n));
  203. }
  204. }
  205. int unregister_chrdev(unsigned int major, const char *name)
  206. {
  207. struct char_device_struct *cd;
  208. cd = __unregister_chrdev_region(major, 0, 256);
  209. if (cd && cd->cdev)
  210. cdev_del(cd->cdev);
  211. kfree(cd);
  212. return 0;
  213. }
  214. static DEFINE_SPINLOCK(cdev_lock);
  215. static struct kobject *cdev_get(struct cdev *p)
  216. {
  217. struct module *owner = p->owner;
  218. struct kobject *kobj;
  219. if (owner && !try_module_get(owner))
  220. return NULL;
  221. kobj = kobject_get(&p->kobj);
  222. if (!kobj)
  223. module_put(owner);
  224. return kobj;
  225. }
  226. void cdev_put(struct cdev *p)
  227. {
  228. if (p) {
  229. kobject_put(&p->kobj);
  230. module_put(p->owner);
  231. }
  232. }
  233. /*
  234. * Called every time a character special file is opened
  235. */
  236. int chrdev_open(struct inode * inode, struct file * filp)
  237. {
  238. struct cdev *p;
  239. struct cdev *new = NULL;
  240. int ret = 0;
  241. spin_lock(&cdev_lock);
  242. p = inode->i_cdev;
  243. if (!p) {
  244. struct kobject *kobj;
  245. int idx;
  246. spin_unlock(&cdev_lock);
  247. kobj = kobj_lookup(cdev_map, inode->i_rdev, &idx);
  248. if (!kobj)
  249. return -ENXIO;
  250. new = container_of(kobj, struct cdev, kobj);
  251. spin_lock(&cdev_lock);
  252. p = inode->i_cdev;
  253. if (!p) {
  254. inode->i_cdev = p = new;
  255. inode->i_cindex = idx;
  256. list_add(&inode->i_devices, &p->list);
  257. new = NULL;
  258. } else if (!cdev_get(p))
  259. ret = -ENXIO;
  260. } else if (!cdev_get(p))
  261. ret = -ENXIO;
  262. spin_unlock(&cdev_lock);
  263. cdev_put(new);
  264. if (ret)
  265. return ret;
  266. filp->f_op = fops_get(p->ops);
  267. if (!filp->f_op) {
  268. cdev_put(p);
  269. return -ENXIO;
  270. }
  271. if (filp->f_op->open) {
  272. lock_kernel();
  273. ret = filp->f_op->open(inode,filp);
  274. unlock_kernel();
  275. }
  276. if (ret)
  277. cdev_put(p);
  278. return ret;
  279. }
  280. void cd_forget(struct inode *inode)
  281. {
  282. spin_lock(&cdev_lock);
  283. list_del_init(&inode->i_devices);
  284. inode->i_cdev = NULL;
  285. spin_unlock(&cdev_lock);
  286. }
  287. void cdev_purge(struct cdev *cdev)
  288. {
  289. spin_lock(&cdev_lock);
  290. while (!list_empty(&cdev->list)) {
  291. struct inode *inode;
  292. inode = container_of(cdev->list.next, struct inode, i_devices);
  293. list_del_init(&inode->i_devices);
  294. inode->i_cdev = NULL;
  295. }
  296. spin_unlock(&cdev_lock);
  297. }
  298. /*
  299. * Dummy default file-operations: the only thing this does
  300. * is contain the open that then fills in the correct operations
  301. * depending on the special file...
  302. */
  303. struct file_operations def_chr_fops = {
  304. .open = chrdev_open,
  305. };
  306. static struct kobject *exact_match(dev_t dev, int *part, void *data)
  307. {
  308. struct cdev *p = data;
  309. return &p->kobj;
  310. }
  311. static int exact_lock(dev_t dev, void *data)
  312. {
  313. struct cdev *p = data;
  314. return cdev_get(p) ? 0 : -1;
  315. }
  316. int cdev_add(struct cdev *p, dev_t dev, unsigned count)
  317. {
  318. p->dev = dev;
  319. p->count = count;
  320. return kobj_map(cdev_map, dev, count, NULL, exact_match, exact_lock, p);
  321. }
  322. static void cdev_unmap(dev_t dev, unsigned count)
  323. {
  324. kobj_unmap(cdev_map, dev, count);
  325. }
  326. void cdev_del(struct cdev *p)
  327. {
  328. cdev_unmap(p->dev, p->count);
  329. kobject_put(&p->kobj);
  330. }
  331. static void cdev_default_release(struct kobject *kobj)
  332. {
  333. struct cdev *p = container_of(kobj, struct cdev, kobj);
  334. cdev_purge(p);
  335. }
  336. static void cdev_dynamic_release(struct kobject *kobj)
  337. {
  338. struct cdev *p = container_of(kobj, struct cdev, kobj);
  339. cdev_purge(p);
  340. kfree(p);
  341. }
  342. static struct kobj_type ktype_cdev_default = {
  343. .release = cdev_default_release,
  344. };
  345. static struct kobj_type ktype_cdev_dynamic = {
  346. .release = cdev_dynamic_release,
  347. };
  348. struct cdev *cdev_alloc(void)
  349. {
  350. struct cdev *p = kmalloc(sizeof(struct cdev), GFP_KERNEL);
  351. if (p) {
  352. memset(p, 0, sizeof(struct cdev));
  353. p->kobj.ktype = &ktype_cdev_dynamic;
  354. INIT_LIST_HEAD(&p->list);
  355. kobject_init(&p->kobj);
  356. }
  357. return p;
  358. }
  359. void cdev_init(struct cdev *cdev, struct file_operations *fops)
  360. {
  361. memset(cdev, 0, sizeof *cdev);
  362. INIT_LIST_HEAD(&cdev->list);
  363. cdev->kobj.ktype = &ktype_cdev_default;
  364. kobject_init(&cdev->kobj);
  365. cdev->ops = fops;
  366. }
  367. static struct kobject *base_probe(dev_t dev, int *part, void *data)
  368. {
  369. if (request_module("char-major-%d-%d", MAJOR(dev), MINOR(dev)) > 0)
  370. /* Make old-style 2.4 aliases work */
  371. request_module("char-major-%d", MAJOR(dev));
  372. return NULL;
  373. }
  374. void __init chrdev_init(void)
  375. {
  376. cdev_map = kobj_map_init(base_probe, &chrdevs_lock);
  377. }
  378. /* Let modules do char dev stuff */
  379. EXPORT_SYMBOL(register_chrdev_region);
  380. EXPORT_SYMBOL(unregister_chrdev_region);
  381. EXPORT_SYMBOL(alloc_chrdev_region);
  382. EXPORT_SYMBOL(cdev_init);
  383. EXPORT_SYMBOL(cdev_alloc);
  384. EXPORT_SYMBOL(cdev_del);
  385. EXPORT_SYMBOL(cdev_add);
  386. EXPORT_SYMBOL(register_chrdev);
  387. EXPORT_SYMBOL(unregister_chrdev);