char_dev.c 9.1 KB

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