char_dev.c 17 KB

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