char_dev.c 14 KB

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