char_dev.c 9.1 KB

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