char_dev.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  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/kdev_t.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/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. #include <linux/tty.h>
  21. #include "internal.h"
  22. /*
  23. * capabilities for /dev/mem, /dev/kmem and similar directly mappable character
  24. * devices
  25. * - permits shared-mmap for read, write and/or exec
  26. * - does not permit private mmap in NOMMU mode (can't do COW)
  27. * - no readahead or I/O queue unplugging required
  28. */
  29. struct backing_dev_info directly_mappable_cdev_bdi = {
  30. .name = "char",
  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 cdev *cdev; /* will die */
  49. } *chrdevs[CHRDEV_MAJOR_HASH_SIZE];
  50. /* index in the above */
  51. static inline int major_to_index(int major)
  52. {
  53. return major % CHRDEV_MAJOR_HASH_SIZE;
  54. }
  55. #ifdef CONFIG_PROC_FS
  56. void chrdev_show(struct seq_file *f, off_t offset)
  57. {
  58. struct char_device_struct *cd;
  59. if (offset < CHRDEV_MAJOR_HASH_SIZE) {
  60. mutex_lock(&chrdevs_lock);
  61. for (cd = chrdevs[offset]; cd; cd = cd->next)
  62. seq_printf(f, "%3d %s\n", cd->major, cd->name);
  63. mutex_unlock(&chrdevs_lock);
  64. }
  65. }
  66. #endif /* CONFIG_PROC_FS */
  67. /*
  68. * Register a single major with a specified minor range.
  69. *
  70. * If major == 0 this functions will dynamically allocate a major and return
  71. * its number.
  72. *
  73. * If major > 0 this function will attempt to reserve the passed range of
  74. * minors and will return zero on success.
  75. *
  76. * Returns a -ve errno on failure.
  77. */
  78. static struct char_device_struct *
  79. __register_chrdev_region(unsigned int major, unsigned int baseminor,
  80. int minorct, const char *name)
  81. {
  82. struct char_device_struct *cd, **cp;
  83. int ret = 0;
  84. int i;
  85. cd = kzalloc(sizeof(struct char_device_struct), GFP_KERNEL);
  86. if (cd == NULL)
  87. return ERR_PTR(-ENOMEM);
  88. mutex_lock(&chrdevs_lock);
  89. /* temporary */
  90. if (major == 0) {
  91. for (i = ARRAY_SIZE(chrdevs)-1; i > 0; i--) {
  92. if (chrdevs[i] == NULL)
  93. break;
  94. }
  95. if (i == 0) {
  96. ret = -EBUSY;
  97. goto out;
  98. }
  99. major = i;
  100. ret = major;
  101. }
  102. cd->major = major;
  103. cd->baseminor = baseminor;
  104. cd->minorct = minorct;
  105. strlcpy(cd->name, name, sizeof(cd->name));
  106. i = major_to_index(major);
  107. for (cp = &chrdevs[i]; *cp; cp = &(*cp)->next)
  108. if ((*cp)->major > major ||
  109. ((*cp)->major == major &&
  110. (((*cp)->baseminor >= baseminor) ||
  111. ((*cp)->baseminor + (*cp)->minorct > baseminor))))
  112. break;
  113. /* Check for overlapping minor ranges. */
  114. if (*cp && (*cp)->major == major) {
  115. int old_min = (*cp)->baseminor;
  116. int old_max = (*cp)->baseminor + (*cp)->minorct - 1;
  117. int new_min = baseminor;
  118. int new_max = baseminor + minorct - 1;
  119. /* New driver overlaps from the left. */
  120. if (new_max >= old_min && new_max <= old_max) {
  121. ret = -EBUSY;
  122. goto out;
  123. }
  124. /* New driver overlaps from the right. */
  125. if (new_min <= old_max && new_min >= old_min) {
  126. ret = -EBUSY;
  127. goto out;
  128. }
  129. }
  130. cd->next = *cp;
  131. *cp = cd;
  132. mutex_unlock(&chrdevs_lock);
  133. return cd;
  134. out:
  135. mutex_unlock(&chrdevs_lock);
  136. kfree(cd);
  137. return ERR_PTR(ret);
  138. }
  139. static struct char_device_struct *
  140. __unregister_chrdev_region(unsigned major, unsigned baseminor, int minorct)
  141. {
  142. struct char_device_struct *cd = NULL, **cp;
  143. int i = major_to_index(major);
  144. mutex_lock(&chrdevs_lock);
  145. for (cp = &chrdevs[i]; *cp; cp = &(*cp)->next)
  146. if ((*cp)->major == major &&
  147. (*cp)->baseminor == baseminor &&
  148. (*cp)->minorct == minorct)
  149. break;
  150. if (*cp) {
  151. cd = *cp;
  152. *cp = cd->next;
  153. }
  154. mutex_unlock(&chrdevs_lock);
  155. return cd;
  156. }
  157. /**
  158. * register_chrdev_region() - register a range of device numbers
  159. * @from: the first in the desired range of device numbers; must include
  160. * the major number.
  161. * @count: the number of consecutive device numbers required
  162. * @name: the name of the device or driver.
  163. *
  164. * Return value is zero on success, a negative error code on failure.
  165. */
  166. int register_chrdev_region(dev_t from, unsigned count, const char *name)
  167. {
  168. struct char_device_struct *cd;
  169. dev_t to = from + count;
  170. dev_t n, next;
  171. for (n = from; n < to; n = next) {
  172. next = MKDEV(MAJOR(n)+1, 0);
  173. if (next > to)
  174. next = to;
  175. cd = __register_chrdev_region(MAJOR(n), MINOR(n),
  176. next - n, name);
  177. if (IS_ERR(cd))
  178. goto fail;
  179. }
  180. return 0;
  181. fail:
  182. to = n;
  183. for (n = from; n < to; n = next) {
  184. next = MKDEV(MAJOR(n)+1, 0);
  185. kfree(__unregister_chrdev_region(MAJOR(n), MINOR(n), next - n));
  186. }
  187. return PTR_ERR(cd);
  188. }
  189. /**
  190. * alloc_chrdev_region() - register a range of char device numbers
  191. * @dev: output parameter for first assigned number
  192. * @baseminor: first of the requested range of minor numbers
  193. * @count: the number of minor numbers required
  194. * @name: the name of the associated device or driver
  195. *
  196. * Allocates a range of char device numbers. The major number will be
  197. * chosen dynamically, and returned (along with the first minor number)
  198. * in @dev. Returns zero or a negative error code.
  199. */
  200. int alloc_chrdev_region(dev_t *dev, unsigned baseminor, unsigned count,
  201. const char *name)
  202. {
  203. struct char_device_struct *cd;
  204. cd = __register_chrdev_region(0, baseminor, count, name);
  205. if (IS_ERR(cd))
  206. return PTR_ERR(cd);
  207. *dev = MKDEV(cd->major, cd->baseminor);
  208. return 0;
  209. }
  210. /**
  211. * __register_chrdev() - create and register a cdev occupying a range of minors
  212. * @major: major device number or 0 for dynamic allocation
  213. * @baseminor: first of the requested range of minor numbers
  214. * @count: the number of minor numbers required
  215. * @name: name of this range of devices
  216. * @fops: file operations associated with this devices
  217. *
  218. * If @major == 0 this functions will dynamically allocate a major and return
  219. * its number.
  220. *
  221. * If @major > 0 this function will attempt to reserve a device with the given
  222. * major number and will return zero on success.
  223. *
  224. * Returns a -ve errno on failure.
  225. *
  226. * The name of this device has nothing to do with the name of the device in
  227. * /dev. It only helps to keep track of the different owners of devices. If
  228. * your module name has only one type of devices it's ok to use e.g. the name
  229. * of the module here.
  230. */
  231. int __register_chrdev(unsigned int major, unsigned int baseminor,
  232. unsigned int count, const char *name,
  233. const struct file_operations *fops)
  234. {
  235. struct char_device_struct *cd;
  236. struct cdev *cdev;
  237. int err = -ENOMEM;
  238. cd = __register_chrdev_region(major, baseminor, count, name);
  239. if (IS_ERR(cd))
  240. return PTR_ERR(cd);
  241. cdev = cdev_alloc();
  242. if (!cdev)
  243. goto out2;
  244. cdev->owner = fops->owner;
  245. cdev->ops = fops;
  246. kobject_set_name(&cdev->kobj, "%s", name);
  247. err = cdev_add(cdev, MKDEV(cd->major, baseminor), count);
  248. if (err)
  249. goto out;
  250. cd->cdev = cdev;
  251. return major ? 0 : cd->major;
  252. out:
  253. kobject_put(&cdev->kobj);
  254. out2:
  255. kfree(__unregister_chrdev_region(cd->major, baseminor, count));
  256. return err;
  257. }
  258. /**
  259. * unregister_chrdev_region() - return a range of device numbers
  260. * @from: the first in the range of numbers to unregister
  261. * @count: the number of device numbers to unregister
  262. *
  263. * This function will unregister a range of @count device numbers,
  264. * starting with @from. The caller should normally be the one who
  265. * allocated those numbers in the first place...
  266. */
  267. void unregister_chrdev_region(dev_t from, unsigned count)
  268. {
  269. dev_t to = from + count;
  270. dev_t n, next;
  271. for (n = from; n < to; n = next) {
  272. next = MKDEV(MAJOR(n)+1, 0);
  273. if (next > to)
  274. next = to;
  275. kfree(__unregister_chrdev_region(MAJOR(n), MINOR(n), next - n));
  276. }
  277. }
  278. /**
  279. * __unregister_chrdev - unregister and destroy a cdev
  280. * @major: major device number
  281. * @baseminor: first of the range of minor numbers
  282. * @count: the number of minor numbers this cdev is occupying
  283. * @name: name of this range of devices
  284. *
  285. * Unregister and destroy the cdev occupying the region described by
  286. * @major, @baseminor and @count. This function undoes what
  287. * __register_chrdev() did.
  288. */
  289. void __unregister_chrdev(unsigned int major, unsigned int baseminor,
  290. unsigned int count, const char *name)
  291. {
  292. struct char_device_struct *cd;
  293. cd = __unregister_chrdev_region(major, baseminor, count);
  294. if (cd && cd->cdev)
  295. cdev_del(cd->cdev);
  296. kfree(cd);
  297. }
  298. static DEFINE_SPINLOCK(cdev_lock);
  299. static struct kobject *cdev_get(struct cdev *p)
  300. {
  301. struct module *owner = p->owner;
  302. struct kobject *kobj;
  303. if (owner && !try_module_get(owner))
  304. return NULL;
  305. kobj = kobject_get(&p->kobj);
  306. if (!kobj)
  307. module_put(owner);
  308. return kobj;
  309. }
  310. void cdev_put(struct cdev *p)
  311. {
  312. if (p) {
  313. struct module *owner = p->owner;
  314. kobject_put(&p->kobj);
  315. module_put(owner);
  316. }
  317. }
  318. /*
  319. * Called every time a character special file is opened
  320. */
  321. static int chrdev_open(struct inode *inode, struct file *filp)
  322. {
  323. struct cdev *p;
  324. struct cdev *new = NULL;
  325. int ret = 0;
  326. spin_lock(&cdev_lock);
  327. p = inode->i_cdev;
  328. if (!p) {
  329. struct kobject *kobj;
  330. int idx;
  331. spin_unlock(&cdev_lock);
  332. kobj = kobj_lookup(cdev_map, inode->i_rdev, &idx);
  333. if (!kobj)
  334. return -ENXIO;
  335. new = container_of(kobj, struct cdev, kobj);
  336. spin_lock(&cdev_lock);
  337. /* Check i_cdev again in case somebody beat us to it while
  338. we dropped the lock. */
  339. p = inode->i_cdev;
  340. if (!p) {
  341. inode->i_cdev = p = new;
  342. list_add(&inode->i_devices, &p->list);
  343. new = NULL;
  344. } else if (!cdev_get(p))
  345. ret = -ENXIO;
  346. } else if (!cdev_get(p))
  347. ret = -ENXIO;
  348. spin_unlock(&cdev_lock);
  349. cdev_put(new);
  350. if (ret)
  351. return ret;
  352. ret = -ENXIO;
  353. filp->f_op = fops_get(p->ops);
  354. if (!filp->f_op)
  355. goto out_cdev_put;
  356. if (filp->f_op->open) {
  357. ret = filp->f_op->open(inode,filp);
  358. if (ret)
  359. goto out_cdev_put;
  360. }
  361. return 0;
  362. out_cdev_put:
  363. cdev_put(p);
  364. return ret;
  365. }
  366. int cdev_index(struct inode *inode)
  367. {
  368. int idx;
  369. struct kobject *kobj;
  370. kobj = kobj_lookup(cdev_map, inode->i_rdev, &idx);
  371. if (!kobj)
  372. return -1;
  373. kobject_put(kobj);
  374. return idx;
  375. }
  376. void cd_forget(struct inode *inode)
  377. {
  378. spin_lock(&cdev_lock);
  379. list_del_init(&inode->i_devices);
  380. inode->i_cdev = NULL;
  381. spin_unlock(&cdev_lock);
  382. }
  383. static void cdev_purge(struct cdev *cdev)
  384. {
  385. spin_lock(&cdev_lock);
  386. while (!list_empty(&cdev->list)) {
  387. struct inode *inode;
  388. inode = container_of(cdev->list.next, struct inode, i_devices);
  389. list_del_init(&inode->i_devices);
  390. inode->i_cdev = NULL;
  391. }
  392. spin_unlock(&cdev_lock);
  393. }
  394. /*
  395. * Dummy default file-operations: the only thing this does
  396. * is contain the open that then fills in the correct operations
  397. * depending on the special file...
  398. */
  399. const struct file_operations def_chr_fops = {
  400. .open = chrdev_open,
  401. };
  402. static struct kobject *exact_match(dev_t dev, int *part, void *data)
  403. {
  404. struct cdev *p = data;
  405. return &p->kobj;
  406. }
  407. static int exact_lock(dev_t dev, void *data)
  408. {
  409. struct cdev *p = data;
  410. return cdev_get(p) ? 0 : -1;
  411. }
  412. /**
  413. * cdev_add() - add a char device to the system
  414. * @p: the cdev structure for the device
  415. * @dev: the first device number for which this device is responsible
  416. * @count: the number of consecutive minor numbers corresponding to this
  417. * device
  418. *
  419. * cdev_add() adds the device represented by @p to the system, making it
  420. * live immediately. A negative error code is returned on failure.
  421. */
  422. int cdev_add(struct cdev *p, dev_t dev, unsigned count)
  423. {
  424. p->dev = dev;
  425. p->count = count;
  426. return kobj_map(cdev_map, dev, count, NULL, exact_match, exact_lock, p);
  427. }
  428. static void cdev_unmap(dev_t dev, unsigned count)
  429. {
  430. kobj_unmap(cdev_map, dev, count);
  431. }
  432. /**
  433. * cdev_del() - remove a cdev from the system
  434. * @p: the cdev structure to be removed
  435. *
  436. * cdev_del() removes @p from the system, possibly freeing the structure
  437. * itself.
  438. */
  439. void cdev_del(struct cdev *p)
  440. {
  441. cdev_unmap(p->dev, p->count);
  442. kobject_put(&p->kobj);
  443. }
  444. static void cdev_default_release(struct kobject *kobj)
  445. {
  446. struct cdev *p = container_of(kobj, struct cdev, kobj);
  447. cdev_purge(p);
  448. }
  449. static void cdev_dynamic_release(struct kobject *kobj)
  450. {
  451. struct cdev *p = container_of(kobj, struct cdev, kobj);
  452. cdev_purge(p);
  453. kfree(p);
  454. }
  455. static struct kobj_type ktype_cdev_default = {
  456. .release = cdev_default_release,
  457. };
  458. static struct kobj_type ktype_cdev_dynamic = {
  459. .release = cdev_dynamic_release,
  460. };
  461. /**
  462. * cdev_alloc() - allocate a cdev structure
  463. *
  464. * Allocates and returns a cdev structure, or NULL on failure.
  465. */
  466. struct cdev *cdev_alloc(void)
  467. {
  468. struct cdev *p = kzalloc(sizeof(struct cdev), GFP_KERNEL);
  469. if (p) {
  470. INIT_LIST_HEAD(&p->list);
  471. kobject_init(&p->kobj, &ktype_cdev_dynamic);
  472. }
  473. return p;
  474. }
  475. /**
  476. * cdev_init() - initialize a cdev structure
  477. * @cdev: the structure to initialize
  478. * @fops: the file_operations for this device
  479. *
  480. * Initializes @cdev, remembering @fops, making it ready to add to the
  481. * system with cdev_add().
  482. */
  483. void cdev_init(struct cdev *cdev, const struct file_operations *fops)
  484. {
  485. memset(cdev, 0, sizeof *cdev);
  486. INIT_LIST_HEAD(&cdev->list);
  487. kobject_init(&cdev->kobj, &ktype_cdev_default);
  488. cdev->ops = fops;
  489. }
  490. static struct kobject *base_probe(dev_t dev, int *part, void *data)
  491. {
  492. if (request_module("char-major-%d-%d", MAJOR(dev), MINOR(dev)) > 0)
  493. /* Make old-style 2.4 aliases work */
  494. request_module("char-major-%d", MAJOR(dev));
  495. return NULL;
  496. }
  497. void __init chrdev_init(void)
  498. {
  499. cdev_map = kobj_map_init(base_probe, &chrdevs_lock);
  500. bdi_init(&directly_mappable_cdev_bdi);
  501. }
  502. /* Let modules do char dev stuff */
  503. EXPORT_SYMBOL(register_chrdev_region);
  504. EXPORT_SYMBOL(unregister_chrdev_region);
  505. EXPORT_SYMBOL(alloc_chrdev_region);
  506. EXPORT_SYMBOL(cdev_init);
  507. EXPORT_SYMBOL(cdev_alloc);
  508. EXPORT_SYMBOL(cdev_del);
  509. EXPORT_SYMBOL(cdev_add);
  510. EXPORT_SYMBOL(cdev_index);
  511. EXPORT_SYMBOL(__register_chrdev);
  512. EXPORT_SYMBOL(__unregister_chrdev);
  513. EXPORT_SYMBOL(directly_mappable_cdev_bdi);