char_dev.c 9.2 KB

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