char_dev.c 9.5 KB

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