char_dev.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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. /**
  160. * register_chrdev() - Register a major number for character devices.
  161. * @major: major device number or 0 for dynamic allocation
  162. * @name: name of this range of devices
  163. * @fops: file operations associated with this devices
  164. *
  165. * If @major == 0 this functions will dynamically allocate a major and return
  166. * its number.
  167. *
  168. * If @major > 0 this function will attempt to reserve a device with the given
  169. * major number and will return zero on success.
  170. *
  171. * Returns a -ve errno on failure.
  172. *
  173. * The name of this device has nothing to do with the name of the device in
  174. * /dev. It only helps to keep track of the different owners of devices. If
  175. * your module name has only one type of devices it's ok to use e.g. the name
  176. * of the module here.
  177. *
  178. * This function registers a range of 256 minor numbers. The first minor number
  179. * is 0.
  180. */
  181. int register_chrdev(unsigned int major, const char *name,
  182. const struct file_operations *fops)
  183. {
  184. struct char_device_struct *cd;
  185. struct cdev *cdev;
  186. char *s;
  187. int err = -ENOMEM;
  188. cd = __register_chrdev_region(major, 0, 256, name);
  189. if (IS_ERR(cd))
  190. return PTR_ERR(cd);
  191. cdev = cdev_alloc();
  192. if (!cdev)
  193. goto out2;
  194. cdev->owner = fops->owner;
  195. cdev->ops = fops;
  196. kobject_set_name(&cdev->kobj, "%s", name);
  197. for (s = strchr(kobject_name(&cdev->kobj),'/'); s; s = strchr(s, '/'))
  198. *s = '!';
  199. err = cdev_add(cdev, MKDEV(cd->major, 0), 256);
  200. if (err)
  201. goto out;
  202. cd->cdev = cdev;
  203. return major ? 0 : cd->major;
  204. out:
  205. kobject_put(&cdev->kobj);
  206. out2:
  207. kfree(__unregister_chrdev_region(cd->major, 0, 256));
  208. return err;
  209. }
  210. void unregister_chrdev_region(dev_t from, unsigned count)
  211. {
  212. dev_t to = from + count;
  213. dev_t n, next;
  214. for (n = from; n < to; n = next) {
  215. next = MKDEV(MAJOR(n)+1, 0);
  216. if (next > to)
  217. next = to;
  218. kfree(__unregister_chrdev_region(MAJOR(n), MINOR(n), next - n));
  219. }
  220. }
  221. int unregister_chrdev(unsigned int major, const char *name)
  222. {
  223. struct char_device_struct *cd;
  224. cd = __unregister_chrdev_region(major, 0, 256);
  225. if (cd && cd->cdev)
  226. cdev_del(cd->cdev);
  227. kfree(cd);
  228. return 0;
  229. }
  230. static DEFINE_SPINLOCK(cdev_lock);
  231. static struct kobject *cdev_get(struct cdev *p)
  232. {
  233. struct module *owner = p->owner;
  234. struct kobject *kobj;
  235. if (owner && !try_module_get(owner))
  236. return NULL;
  237. kobj = kobject_get(&p->kobj);
  238. if (!kobj)
  239. module_put(owner);
  240. return kobj;
  241. }
  242. void cdev_put(struct cdev *p)
  243. {
  244. if (p) {
  245. struct module *owner = p->owner;
  246. kobject_put(&p->kobj);
  247. module_put(owner);
  248. }
  249. }
  250. /*
  251. * Called every time a character special file is opened
  252. */
  253. int chrdev_open(struct inode * inode, struct file * filp)
  254. {
  255. struct cdev *p;
  256. struct cdev *new = NULL;
  257. int ret = 0;
  258. spin_lock(&cdev_lock);
  259. p = inode->i_cdev;
  260. if (!p) {
  261. struct kobject *kobj;
  262. int idx;
  263. spin_unlock(&cdev_lock);
  264. kobj = kobj_lookup(cdev_map, inode->i_rdev, &idx);
  265. if (!kobj)
  266. return -ENXIO;
  267. new = container_of(kobj, struct cdev, kobj);
  268. spin_lock(&cdev_lock);
  269. p = inode->i_cdev;
  270. if (!p) {
  271. inode->i_cdev = p = new;
  272. inode->i_cindex = idx;
  273. list_add(&inode->i_devices, &p->list);
  274. new = NULL;
  275. } else if (!cdev_get(p))
  276. ret = -ENXIO;
  277. } else if (!cdev_get(p))
  278. ret = -ENXIO;
  279. spin_unlock(&cdev_lock);
  280. cdev_put(new);
  281. if (ret)
  282. return ret;
  283. filp->f_op = fops_get(p->ops);
  284. if (!filp->f_op) {
  285. cdev_put(p);
  286. return -ENXIO;
  287. }
  288. if (filp->f_op->open) {
  289. lock_kernel();
  290. ret = filp->f_op->open(inode,filp);
  291. unlock_kernel();
  292. }
  293. if (ret)
  294. cdev_put(p);
  295. return ret;
  296. }
  297. void cd_forget(struct inode *inode)
  298. {
  299. spin_lock(&cdev_lock);
  300. list_del_init(&inode->i_devices);
  301. inode->i_cdev = NULL;
  302. spin_unlock(&cdev_lock);
  303. }
  304. static void cdev_purge(struct cdev *cdev)
  305. {
  306. spin_lock(&cdev_lock);
  307. while (!list_empty(&cdev->list)) {
  308. struct inode *inode;
  309. inode = container_of(cdev->list.next, struct inode, i_devices);
  310. list_del_init(&inode->i_devices);
  311. inode->i_cdev = NULL;
  312. }
  313. spin_unlock(&cdev_lock);
  314. }
  315. /*
  316. * Dummy default file-operations: the only thing this does
  317. * is contain the open that then fills in the correct operations
  318. * depending on the special file...
  319. */
  320. const struct file_operations def_chr_fops = {
  321. .open = chrdev_open,
  322. };
  323. static struct kobject *exact_match(dev_t dev, int *part, void *data)
  324. {
  325. struct cdev *p = data;
  326. return &p->kobj;
  327. }
  328. static int exact_lock(dev_t dev, void *data)
  329. {
  330. struct cdev *p = data;
  331. return cdev_get(p) ? 0 : -1;
  332. }
  333. int cdev_add(struct cdev *p, dev_t dev, unsigned count)
  334. {
  335. p->dev = dev;
  336. p->count = count;
  337. return kobj_map(cdev_map, dev, count, NULL, exact_match, exact_lock, p);
  338. }
  339. static void cdev_unmap(dev_t dev, unsigned count)
  340. {
  341. kobj_unmap(cdev_map, dev, count);
  342. }
  343. void cdev_del(struct cdev *p)
  344. {
  345. cdev_unmap(p->dev, p->count);
  346. kobject_put(&p->kobj);
  347. }
  348. static void cdev_default_release(struct kobject *kobj)
  349. {
  350. struct cdev *p = container_of(kobj, struct cdev, kobj);
  351. cdev_purge(p);
  352. }
  353. static void cdev_dynamic_release(struct kobject *kobj)
  354. {
  355. struct cdev *p = container_of(kobj, struct cdev, kobj);
  356. cdev_purge(p);
  357. kfree(p);
  358. }
  359. static struct kobj_type ktype_cdev_default = {
  360. .release = cdev_default_release,
  361. };
  362. static struct kobj_type ktype_cdev_dynamic = {
  363. .release = cdev_dynamic_release,
  364. };
  365. struct cdev *cdev_alloc(void)
  366. {
  367. struct cdev *p = kzalloc(sizeof(struct cdev), GFP_KERNEL);
  368. if (p) {
  369. p->kobj.ktype = &ktype_cdev_dynamic;
  370. INIT_LIST_HEAD(&p->list);
  371. kobject_init(&p->kobj);
  372. }
  373. return p;
  374. }
  375. void cdev_init(struct cdev *cdev, const struct file_operations *fops)
  376. {
  377. memset(cdev, 0, sizeof *cdev);
  378. INIT_LIST_HEAD(&cdev->list);
  379. cdev->kobj.ktype = &ktype_cdev_default;
  380. kobject_init(&cdev->kobj);
  381. cdev->ops = fops;
  382. }
  383. static struct kobject *base_probe(dev_t dev, int *part, void *data)
  384. {
  385. if (request_module("char-major-%d-%d", MAJOR(dev), MINOR(dev)) > 0)
  386. /* Make old-style 2.4 aliases work */
  387. request_module("char-major-%d", MAJOR(dev));
  388. return NULL;
  389. }
  390. void __init chrdev_init(void)
  391. {
  392. cdev_map = kobj_map_init(base_probe, &chrdevs_lock);
  393. }
  394. /* Let modules do char dev stuff */
  395. EXPORT_SYMBOL(register_chrdev_region);
  396. EXPORT_SYMBOL(unregister_chrdev_region);
  397. EXPORT_SYMBOL(alloc_chrdev_region);
  398. EXPORT_SYMBOL(cdev_init);
  399. EXPORT_SYMBOL(cdev_alloc);
  400. EXPORT_SYMBOL(cdev_del);
  401. EXPORT_SYMBOL(cdev_add);
  402. EXPORT_SYMBOL(register_chrdev);
  403. EXPORT_SYMBOL(unregister_chrdev);