char_dev.c 11 KB

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