char_dev.c 14 KB

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