char_dev.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  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. static struct kobj_map *cdev_map;
  23. static DEFINE_MUTEX(chrdevs_lock);
  24. #define CHRDEV_MAJOR_HASH_SIZE 255
  25. static struct char_device_struct {
  26. struct char_device_struct *next;
  27. unsigned int major;
  28. unsigned int baseminor;
  29. int minorct;
  30. char name[64];
  31. struct cdev *cdev; /* will die */
  32. } *chrdevs[CHRDEV_MAJOR_HASH_SIZE];
  33. /* index in the above */
  34. static inline int major_to_index(unsigned 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. mutex_lock(&chrdevs_lock);
  43. for (cd = chrdevs[major_to_index(offset)]; cd; cd = cd->next) {
  44. if (cd->major == offset)
  45. seq_printf(f, "%3d %s\n", cd->major, cd->name);
  46. }
  47. mutex_unlock(&chrdevs_lock);
  48. }
  49. #endif /* CONFIG_PROC_FS */
  50. static int find_dynamic_major(void)
  51. {
  52. int i;
  53. struct char_device_struct *cd;
  54. for (i = ARRAY_SIZE(chrdevs)-1; i > CHRDEV_MAJOR_DYN_END; i--) {
  55. if (chrdevs[i] == NULL)
  56. return i;
  57. }
  58. for (i = CHRDEV_MAJOR_DYN_EXT_START;
  59. i > CHRDEV_MAJOR_DYN_EXT_END; i--) {
  60. for (cd = chrdevs[major_to_index(i)]; cd; cd = cd->next)
  61. if (cd->major == i)
  62. break;
  63. if (cd == NULL || cd->major != i)
  64. return i;
  65. }
  66. return -EBUSY;
  67. }
  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. if (major == 0) {
  91. ret = find_dynamic_major();
  92. if (ret < 0) {
  93. pr_err("CHRDEV \"%s\" dynamic allocation region is full\n",
  94. name);
  95. goto out;
  96. }
  97. major = ret;
  98. }
  99. if (major >= CHRDEV_MAJOR_MAX) {
  100. pr_err("CHRDEV \"%s\" major requested (%d) is greater than the maximum (%d)\n",
  101. name, major, CHRDEV_MAJOR_MAX);
  102. ret = -EINVAL;
  103. goto out;
  104. }
  105. cd->major = major;
  106. cd->baseminor = baseminor;
  107. cd->minorct = minorct;
  108. strlcpy(cd->name, name, sizeof(cd->name));
  109. i = major_to_index(major);
  110. for (cp = &chrdevs[i]; *cp; cp = &(*cp)->next)
  111. if ((*cp)->major > major ||
  112. ((*cp)->major == major &&
  113. (((*cp)->baseminor >= baseminor) ||
  114. ((*cp)->baseminor + (*cp)->minorct > baseminor))))
  115. break;
  116. /* Check for overlapping minor ranges. */
  117. if (*cp && (*cp)->major == major) {
  118. int old_min = (*cp)->baseminor;
  119. int old_max = (*cp)->baseminor + (*cp)->minorct - 1;
  120. int new_min = baseminor;
  121. int new_max = baseminor + minorct - 1;
  122. /* New driver overlaps from the left. */
  123. if (new_max >= old_min && new_max <= old_max) {
  124. ret = -EBUSY;
  125. goto out;
  126. }
  127. /* New driver overlaps from the right. */
  128. if (new_min <= old_max && new_min >= old_min) {
  129. ret = -EBUSY;
  130. goto out;
  131. }
  132. }
  133. cd->next = *cp;
  134. *cp = cd;
  135. mutex_unlock(&chrdevs_lock);
  136. return cd;
  137. out:
  138. mutex_unlock(&chrdevs_lock);
  139. kfree(cd);
  140. return ERR_PTR(ret);
  141. }
  142. static struct char_device_struct *
  143. __unregister_chrdev_region(unsigned major, unsigned baseminor, int minorct)
  144. {
  145. struct char_device_struct *cd = NULL, **cp;
  146. int i = major_to_index(major);
  147. mutex_lock(&chrdevs_lock);
  148. for (cp = &chrdevs[i]; *cp; cp = &(*cp)->next)
  149. if ((*cp)->major == major &&
  150. (*cp)->baseminor == baseminor &&
  151. (*cp)->minorct == minorct)
  152. break;
  153. if (*cp) {
  154. cd = *cp;
  155. *cp = cd->next;
  156. }
  157. mutex_unlock(&chrdevs_lock);
  158. return cd;
  159. }
  160. /**
  161. * register_chrdev_region() - register a range of device numbers
  162. * @from: the first in the desired range of device numbers; must include
  163. * the major number.
  164. * @count: the number of consecutive device numbers required
  165. * @name: the name of the device or driver.
  166. *
  167. * Return value is zero on success, a negative error code on failure.
  168. */
  169. int register_chrdev_region(dev_t from, unsigned count, const char *name)
  170. {
  171. struct char_device_struct *cd;
  172. dev_t to = from + count;
  173. dev_t n, next;
  174. for (n = from; n < to; n = next) {
  175. next = MKDEV(MAJOR(n)+1, 0);
  176. if (next > to)
  177. next = to;
  178. cd = __register_chrdev_region(MAJOR(n), MINOR(n),
  179. next - n, name);
  180. if (IS_ERR(cd))
  181. goto fail;
  182. }
  183. return 0;
  184. fail:
  185. to = n;
  186. for (n = from; n < to; n = next) {
  187. next = MKDEV(MAJOR(n)+1, 0);
  188. kfree(__unregister_chrdev_region(MAJOR(n), MINOR(n), next - n));
  189. }
  190. return PTR_ERR(cd);
  191. }
  192. /**
  193. * alloc_chrdev_region() - register a range of char device numbers
  194. * @dev: output parameter for first assigned number
  195. * @baseminor: first of the requested range of minor numbers
  196. * @count: the number of minor numbers required
  197. * @name: the name of the associated device or driver
  198. *
  199. * Allocates a range of char device numbers. The major number will be
  200. * chosen dynamically, and returned (along with the first minor number)
  201. * in @dev. Returns zero or a negative error code.
  202. */
  203. int alloc_chrdev_region(dev_t *dev, unsigned baseminor, unsigned count,
  204. const char *name)
  205. {
  206. struct char_device_struct *cd;
  207. cd = __register_chrdev_region(0, baseminor, count, name);
  208. if (IS_ERR(cd))
  209. return PTR_ERR(cd);
  210. *dev = MKDEV(cd->major, cd->baseminor);
  211. return 0;
  212. }
  213. /**
  214. * __register_chrdev() - create and register a cdev occupying a range of minors
  215. * @major: major device number or 0 for dynamic allocation
  216. * @baseminor: first of the requested range of minor numbers
  217. * @count: the number of minor numbers required
  218. * @name: name of this range of devices
  219. * @fops: file operations associated with this devices
  220. *
  221. * If @major == 0 this functions will dynamically allocate a major and return
  222. * its number.
  223. *
  224. * If @major > 0 this function will attempt to reserve a device with the given
  225. * major number and will return zero on success.
  226. *
  227. * Returns a -ve errno on failure.
  228. *
  229. * The name of this device has nothing to do with the name of the device in
  230. * /dev. It only helps to keep track of the different owners of devices. If
  231. * your module name has only one type of devices it's ok to use e.g. the name
  232. * of the module here.
  233. */
  234. int __register_chrdev(unsigned int major, unsigned int baseminor,
  235. unsigned int count, const char *name,
  236. const struct file_operations *fops)
  237. {
  238. struct char_device_struct *cd;
  239. struct cdev *cdev;
  240. int err = -ENOMEM;
  241. cd = __register_chrdev_region(major, baseminor, count, name);
  242. if (IS_ERR(cd))
  243. return PTR_ERR(cd);
  244. cdev = cdev_alloc();
  245. if (!cdev)
  246. goto out2;
  247. cdev->owner = fops->owner;
  248. cdev->ops = fops;
  249. kobject_set_name(&cdev->kobj, "%s", name);
  250. err = cdev_add(cdev, MKDEV(cd->major, baseminor), count);
  251. if (err)
  252. goto out;
  253. cd->cdev = cdev;
  254. return major ? 0 : cd->major;
  255. out:
  256. kobject_put(&cdev->kobj);
  257. out2:
  258. kfree(__unregister_chrdev_region(cd->major, baseminor, count));
  259. return err;
  260. }
  261. /**
  262. * unregister_chrdev_region() - unregister a range of device numbers
  263. * @from: the first in the range of numbers to unregister
  264. * @count: the number of device numbers to unregister
  265. *
  266. * This function will unregister a range of @count device numbers,
  267. * starting with @from. The caller should normally be the one who
  268. * allocated those numbers in the first place...
  269. */
  270. void unregister_chrdev_region(dev_t from, unsigned count)
  271. {
  272. dev_t to = from + count;
  273. dev_t n, next;
  274. for (n = from; n < to; n = next) {
  275. next = MKDEV(MAJOR(n)+1, 0);
  276. if (next > to)
  277. next = to;
  278. kfree(__unregister_chrdev_region(MAJOR(n), MINOR(n), next - n));
  279. }
  280. }
  281. /**
  282. * __unregister_chrdev - unregister and destroy a cdev
  283. * @major: major device number
  284. * @baseminor: first of the range of minor numbers
  285. * @count: the number of minor numbers this cdev is occupying
  286. * @name: name of this range of devices
  287. *
  288. * Unregister and destroy the cdev occupying the region described by
  289. * @major, @baseminor and @count. This function undoes what
  290. * __register_chrdev() did.
  291. */
  292. void __unregister_chrdev(unsigned int major, unsigned int baseminor,
  293. unsigned int count, const char *name)
  294. {
  295. struct char_device_struct *cd;
  296. cd = __unregister_chrdev_region(major, baseminor, count);
  297. if (cd && cd->cdev)
  298. cdev_del(cd->cdev);
  299. kfree(cd);
  300. }
  301. static DEFINE_SPINLOCK(cdev_lock);
  302. static struct kobject *cdev_get(struct cdev *p)
  303. {
  304. struct module *owner = p->owner;
  305. struct kobject *kobj;
  306. if (owner && !try_module_get(owner))
  307. return NULL;
  308. kobj = kobject_get(&p->kobj);
  309. if (!kobj)
  310. module_put(owner);
  311. return kobj;
  312. }
  313. void cdev_put(struct cdev *p)
  314. {
  315. if (p) {
  316. struct module *owner = p->owner;
  317. kobject_put(&p->kobj);
  318. module_put(owner);
  319. }
  320. }
  321. /*
  322. * Called every time a character special file is opened
  323. */
  324. static int chrdev_open(struct inode *inode, struct file *filp)
  325. {
  326. const struct file_operations *fops;
  327. struct cdev *p;
  328. struct cdev *new = NULL;
  329. int ret = 0;
  330. spin_lock(&cdev_lock);
  331. p = inode->i_cdev;
  332. if (!p) {
  333. struct kobject *kobj;
  334. int idx;
  335. spin_unlock(&cdev_lock);
  336. kobj = kobj_lookup(cdev_map, inode->i_rdev, &idx);
  337. if (!kobj)
  338. return -ENXIO;
  339. new = container_of(kobj, struct cdev, kobj);
  340. spin_lock(&cdev_lock);
  341. /* Check i_cdev again in case somebody beat us to it while
  342. we dropped the lock. */
  343. p = inode->i_cdev;
  344. if (!p) {
  345. inode->i_cdev = p = new;
  346. list_add(&inode->i_devices, &p->list);
  347. new = NULL;
  348. } else if (!cdev_get(p))
  349. ret = -ENXIO;
  350. } else if (!cdev_get(p))
  351. ret = -ENXIO;
  352. spin_unlock(&cdev_lock);
  353. cdev_put(new);
  354. if (ret)
  355. return ret;
  356. ret = -ENXIO;
  357. fops = fops_get(p->ops);
  358. if (!fops)
  359. goto out_cdev_put;
  360. replace_fops(filp, fops);
  361. if (filp->f_op->open) {
  362. ret = filp->f_op->open(inode, filp);
  363. if (ret)
  364. goto out_cdev_put;
  365. }
  366. return 0;
  367. out_cdev_put:
  368. cdev_put(p);
  369. return ret;
  370. }
  371. void cd_forget(struct inode *inode)
  372. {
  373. spin_lock(&cdev_lock);
  374. list_del_init(&inode->i_devices);
  375. inode->i_cdev = NULL;
  376. inode->i_mapping = &inode->i_data;
  377. spin_unlock(&cdev_lock);
  378. }
  379. static void cdev_purge(struct cdev *cdev)
  380. {
  381. spin_lock(&cdev_lock);
  382. while (!list_empty(&cdev->list)) {
  383. struct inode *inode;
  384. inode = container_of(cdev->list.next, struct inode, i_devices);
  385. list_del_init(&inode->i_devices);
  386. inode->i_cdev = NULL;
  387. }
  388. spin_unlock(&cdev_lock);
  389. }
  390. /*
  391. * Dummy default file-operations: the only thing this does
  392. * is contain the open that then fills in the correct operations
  393. * depending on the special file...
  394. */
  395. const struct file_operations def_chr_fops = {
  396. .open = chrdev_open,
  397. .llseek = noop_llseek,
  398. };
  399. static struct kobject *exact_match(dev_t dev, int *part, void *data)
  400. {
  401. struct cdev *p = data;
  402. return &p->kobj;
  403. }
  404. static int exact_lock(dev_t dev, void *data)
  405. {
  406. struct cdev *p = data;
  407. return cdev_get(p) ? 0 : -1;
  408. }
  409. /**
  410. * cdev_add() - add a char device to the system
  411. * @p: the cdev structure for the device
  412. * @dev: the first device number for which this device is responsible
  413. * @count: the number of consecutive minor numbers corresponding to this
  414. * device
  415. *
  416. * cdev_add() adds the device represented by @p to the system, making it
  417. * live immediately. A negative error code is returned on failure.
  418. */
  419. int cdev_add(struct cdev *p, dev_t dev, unsigned count)
  420. {
  421. int error;
  422. p->dev = dev;
  423. p->count = count;
  424. error = kobj_map(cdev_map, dev, count, NULL,
  425. exact_match, exact_lock, p);
  426. if (error)
  427. return error;
  428. kobject_get(p->kobj.parent);
  429. return 0;
  430. }
  431. /**
  432. * cdev_set_parent() - set the parent kobject for a char device
  433. * @p: the cdev structure
  434. * @kobj: the kobject to take a reference to
  435. *
  436. * cdev_set_parent() sets a parent kobject which will be referenced
  437. * appropriately so the parent is not freed before the cdev. This
  438. * should be called before cdev_add.
  439. */
  440. void cdev_set_parent(struct cdev *p, struct kobject *kobj)
  441. {
  442. WARN_ON(!kobj->state_initialized);
  443. p->kobj.parent = kobj;
  444. }
  445. /**
  446. * cdev_device_add() - add a char device and it's corresponding
  447. * struct device, linkink
  448. * @dev: the device structure
  449. * @cdev: the cdev structure
  450. *
  451. * cdev_device_add() adds the char device represented by @cdev to the system,
  452. * just as cdev_add does. It then adds @dev to the system using device_add
  453. * The dev_t for the char device will be taken from the struct device which
  454. * needs to be initialized first. This helper function correctly takes a
  455. * reference to the parent device so the parent will not get released until
  456. * all references to the cdev are released.
  457. *
  458. * This helper uses dev->devt for the device number. If it is not set
  459. * it will not add the cdev and it will be equivalent to device_add.
  460. *
  461. * This function should be used whenever the struct cdev and the
  462. * struct device are members of the same structure whose lifetime is
  463. * managed by the struct device.
  464. *
  465. * NOTE: Callers must assume that userspace was able to open the cdev and
  466. * can call cdev fops callbacks at any time, even if this function fails.
  467. */
  468. int cdev_device_add(struct cdev *cdev, struct device *dev)
  469. {
  470. int rc = 0;
  471. if (dev->devt) {
  472. cdev_set_parent(cdev, &dev->kobj);
  473. rc = cdev_add(cdev, dev->devt, 1);
  474. if (rc)
  475. return rc;
  476. }
  477. rc = device_add(dev);
  478. if (rc)
  479. cdev_del(cdev);
  480. return rc;
  481. }
  482. /**
  483. * cdev_device_del() - inverse of cdev_device_add
  484. * @dev: the device structure
  485. * @cdev: the cdev structure
  486. *
  487. * cdev_device_del() is a helper function to call cdev_del and device_del.
  488. * It should be used whenever cdev_device_add is used.
  489. *
  490. * If dev->devt is not set it will not remove the cdev and will be equivalent
  491. * to device_del.
  492. *
  493. * NOTE: This guarantees that associated sysfs callbacks are not running
  494. * or runnable, however any cdevs already open will remain and their fops
  495. * will still be callable even after this function returns.
  496. */
  497. void cdev_device_del(struct cdev *cdev, struct device *dev)
  498. {
  499. device_del(dev);
  500. if (dev->devt)
  501. cdev_del(cdev);
  502. }
  503. static void cdev_unmap(dev_t dev, unsigned count)
  504. {
  505. kobj_unmap(cdev_map, dev, count);
  506. }
  507. /**
  508. * cdev_del() - remove a cdev from the system
  509. * @p: the cdev structure to be removed
  510. *
  511. * cdev_del() removes @p from the system, possibly freeing the structure
  512. * itself.
  513. *
  514. * NOTE: This guarantees that cdev device will no longer be able to be
  515. * opened, however any cdevs already open will remain and their fops will
  516. * still be callable even after cdev_del returns.
  517. */
  518. void cdev_del(struct cdev *p)
  519. {
  520. cdev_unmap(p->dev, p->count);
  521. kobject_put(&p->kobj);
  522. }
  523. static void cdev_default_release(struct kobject *kobj)
  524. {
  525. struct cdev *p = container_of(kobj, struct cdev, kobj);
  526. struct kobject *parent = kobj->parent;
  527. cdev_purge(p);
  528. kobject_put(parent);
  529. }
  530. static void cdev_dynamic_release(struct kobject *kobj)
  531. {
  532. struct cdev *p = container_of(kobj, struct cdev, kobj);
  533. struct kobject *parent = kobj->parent;
  534. cdev_purge(p);
  535. kfree(p);
  536. kobject_put(parent);
  537. }
  538. static struct kobj_type ktype_cdev_default = {
  539. .release = cdev_default_release,
  540. };
  541. static struct kobj_type ktype_cdev_dynamic = {
  542. .release = cdev_dynamic_release,
  543. };
  544. /**
  545. * cdev_alloc() - allocate a cdev structure
  546. *
  547. * Allocates and returns a cdev structure, or NULL on failure.
  548. */
  549. struct cdev *cdev_alloc(void)
  550. {
  551. struct cdev *p = kzalloc(sizeof(struct cdev), GFP_KERNEL);
  552. if (p) {
  553. INIT_LIST_HEAD(&p->list);
  554. kobject_init(&p->kobj, &ktype_cdev_dynamic);
  555. }
  556. return p;
  557. }
  558. /**
  559. * cdev_init() - initialize a cdev structure
  560. * @cdev: the structure to initialize
  561. * @fops: the file_operations for this device
  562. *
  563. * Initializes @cdev, remembering @fops, making it ready to add to the
  564. * system with cdev_add().
  565. */
  566. void cdev_init(struct cdev *cdev, const struct file_operations *fops)
  567. {
  568. memset(cdev, 0, sizeof *cdev);
  569. INIT_LIST_HEAD(&cdev->list);
  570. kobject_init(&cdev->kobj, &ktype_cdev_default);
  571. cdev->ops = fops;
  572. }
  573. static struct kobject *base_probe(dev_t dev, int *part, void *data)
  574. {
  575. if (request_module("char-major-%d-%d", MAJOR(dev), MINOR(dev)) > 0)
  576. /* Make old-style 2.4 aliases work */
  577. request_module("char-major-%d", MAJOR(dev));
  578. return NULL;
  579. }
  580. void __init chrdev_init(void)
  581. {
  582. cdev_map = kobj_map_init(base_probe, &chrdevs_lock);
  583. }
  584. /* Let modules do char dev stuff */
  585. EXPORT_SYMBOL(register_chrdev_region);
  586. EXPORT_SYMBOL(unregister_chrdev_region);
  587. EXPORT_SYMBOL(alloc_chrdev_region);
  588. EXPORT_SYMBOL(cdev_init);
  589. EXPORT_SYMBOL(cdev_alloc);
  590. EXPORT_SYMBOL(cdev_del);
  591. EXPORT_SYMBOL(cdev_add);
  592. EXPORT_SYMBOL(cdev_set_parent);
  593. EXPORT_SYMBOL(cdev_device_add);
  594. EXPORT_SYMBOL(cdev_device_del);
  595. EXPORT_SYMBOL(__register_chrdev);
  596. EXPORT_SYMBOL(__unregister_chrdev);