char_dev.c 9.2 KB

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