char_dev.c 17 KB

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