char_dev.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  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/cleanup.h>
  13. #include <linux/major.h>
  14. #include <linux/errno.h>
  15. #include <linux/module.h>
  16. #include <linux/seq_file.h>
  17. #include <linux/kobject.h>
  18. #include <linux/kobj_map.h>
  19. #include <linux/cdev.h>
  20. #include <linux/mutex.h>
  21. #include <linux/backing-dev.h>
  22. #include <linux/tty.h>
  23. #include "internal.h"
  24. static struct kobj_map *cdev_map __ro_after_init;
  25. static DEFINE_MUTEX(chrdevs_lock);
  26. #define CHRDEV_MAJOR_HASH_SIZE 255
  27. static struct char_device_struct {
  28. struct char_device_struct *next;
  29. unsigned int major;
  30. unsigned int baseminor;
  31. int minorct;
  32. char name[64];
  33. struct cdev *cdev; /* will die */
  34. } *chrdevs[CHRDEV_MAJOR_HASH_SIZE];
  35. /* index in the above */
  36. static inline int major_to_index(unsigned major)
  37. {
  38. return major % CHRDEV_MAJOR_HASH_SIZE;
  39. }
  40. #ifdef CONFIG_PROC_FS
  41. void chrdev_show(struct seq_file *f, off_t offset)
  42. {
  43. struct char_device_struct *cd;
  44. mutex_lock(&chrdevs_lock);
  45. for (cd = chrdevs[major_to_index(offset)]; cd; cd = cd->next) {
  46. if (cd->major == offset)
  47. seq_printf(f, "%3d %s\n", cd->major, cd->name);
  48. }
  49. mutex_unlock(&chrdevs_lock);
  50. }
  51. #endif /* CONFIG_PROC_FS */
  52. static int find_dynamic_major(void)
  53. {
  54. int i;
  55. struct char_device_struct *cd;
  56. for (i = ARRAY_SIZE(chrdevs)-1; i >= CHRDEV_MAJOR_DYN_END; i--) {
  57. if (chrdevs[i] == NULL)
  58. return i;
  59. }
  60. for (i = CHRDEV_MAJOR_DYN_EXT_START;
  61. i >= CHRDEV_MAJOR_DYN_EXT_END; i--) {
  62. for (cd = chrdevs[major_to_index(i)]; cd; cd = cd->next)
  63. if (cd->major == i)
  64. break;
  65. if (cd == NULL)
  66. return i;
  67. }
  68. return -EBUSY;
  69. }
  70. /*
  71. * Register a single major with a specified minor range.
  72. *
  73. * If major == 0 this function will dynamically allocate an unused major.
  74. * If major > 0 this function will attempt to reserve the range of minors
  75. * with given major.
  76. *
  77. */
  78. static struct char_device_struct *
  79. __register_chrdev_region(unsigned int major, unsigned int baseminor,
  80. int minorct, const char *name)
  81. {
  82. struct char_device_struct *cd __free(kfree) = NULL;
  83. struct char_device_struct *curr, *prev = NULL;
  84. int ret;
  85. int i;
  86. if (major >= CHRDEV_MAJOR_MAX) {
  87. pr_err("CHRDEV \"%s\" major requested (%u) is greater than the maximum (%u)\n",
  88. name, major, CHRDEV_MAJOR_MAX-1);
  89. return ERR_PTR(-EINVAL);
  90. }
  91. if (minorct > MINORMASK + 1 - baseminor) {
  92. pr_err("CHRDEV \"%s\" minor range requested (%u-%u) is out of range of maximum range (%u-%u) for a single major\n",
  93. name, baseminor, baseminor + minorct - 1, 0, MINORMASK);
  94. return ERR_PTR(-EINVAL);
  95. }
  96. cd = kzalloc_obj(struct char_device_struct);
  97. if (cd == NULL)
  98. return ERR_PTR(-ENOMEM);
  99. guard(mutex)(&chrdevs_lock);
  100. if (major == 0) {
  101. ret = find_dynamic_major();
  102. if (ret < 0) {
  103. pr_err("CHRDEV \"%s\" dynamic allocation region is full\n",
  104. name);
  105. return ERR_PTR(ret);
  106. }
  107. major = ret;
  108. }
  109. ret = -EBUSY;
  110. i = major_to_index(major);
  111. for (curr = chrdevs[i]; curr; prev = curr, curr = curr->next) {
  112. if (curr->major < major)
  113. continue;
  114. if (curr->major > major)
  115. break;
  116. if (curr->baseminor + curr->minorct <= baseminor)
  117. continue;
  118. if (curr->baseminor >= baseminor + minorct)
  119. break;
  120. return ERR_PTR(ret);
  121. }
  122. cd->major = major;
  123. cd->baseminor = baseminor;
  124. cd->minorct = minorct;
  125. strscpy(cd->name, name, sizeof(cd->name));
  126. if (!prev) {
  127. cd->next = curr;
  128. chrdevs[i] = cd;
  129. } else {
  130. cd->next = prev->next;
  131. prev->next = cd;
  132. }
  133. return_ptr(cd);
  134. }
  135. static struct char_device_struct *
  136. __unregister_chrdev_region(unsigned major, unsigned baseminor, int minorct)
  137. {
  138. struct char_device_struct *cd = NULL, **cp;
  139. int i = major_to_index(major);
  140. mutex_lock(&chrdevs_lock);
  141. for (cp = &chrdevs[i]; *cp; cp = &(*cp)->next)
  142. if ((*cp)->major == major &&
  143. (*cp)->baseminor == baseminor &&
  144. (*cp)->minorct == minorct)
  145. break;
  146. if (*cp) {
  147. cd = *cp;
  148. *cp = cd->next;
  149. }
  150. mutex_unlock(&chrdevs_lock);
  151. return cd;
  152. }
  153. /**
  154. * register_chrdev_region() - register a range of device numbers
  155. * @from: the first in the desired range of device numbers; must include
  156. * the major number.
  157. * @count: the number of consecutive device numbers required
  158. * @name: the name of the device or driver.
  159. *
  160. * Return value is zero on success, a negative error code on failure.
  161. */
  162. int register_chrdev_region(dev_t from, unsigned count, const char *name)
  163. {
  164. struct char_device_struct *cd;
  165. dev_t to = from + count;
  166. dev_t n, next;
  167. for (n = from; n < to; n = next) {
  168. next = MKDEV(MAJOR(n)+1, 0);
  169. if (next > to)
  170. next = to;
  171. cd = __register_chrdev_region(MAJOR(n), MINOR(n),
  172. next - n, name);
  173. if (IS_ERR(cd))
  174. goto fail;
  175. }
  176. return 0;
  177. fail:
  178. to = n;
  179. for (n = from; n < to; n = next) {
  180. next = MKDEV(MAJOR(n)+1, 0);
  181. kfree(__unregister_chrdev_region(MAJOR(n), MINOR(n), next - n));
  182. }
  183. return PTR_ERR(cd);
  184. }
  185. /**
  186. * alloc_chrdev_region() - register a range of char device numbers
  187. * @dev: output parameter for first assigned number
  188. * @baseminor: first of the requested range of minor numbers
  189. * @count: the number of minor numbers required
  190. * @name: the name of the associated device or driver
  191. *
  192. * Allocates a range of char device numbers. The major number will be
  193. * chosen dynamically, and returned (along with the first minor number)
  194. * in @dev. Returns zero or a negative error code.
  195. */
  196. int alloc_chrdev_region(dev_t *dev, unsigned baseminor, unsigned count,
  197. const char *name)
  198. {
  199. struct char_device_struct *cd;
  200. cd = __register_chrdev_region(0, baseminor, count, name);
  201. if (IS_ERR(cd))
  202. return PTR_ERR(cd);
  203. *dev = MKDEV(cd->major, cd->baseminor);
  204. return 0;
  205. }
  206. /**
  207. * __register_chrdev() - create and register a cdev occupying a range of minors
  208. * @major: major device number or 0 for dynamic allocation
  209. * @baseminor: first of the requested range of minor numbers
  210. * @count: the number of minor numbers required
  211. * @name: name of this range of devices
  212. * @fops: file operations associated with this devices
  213. *
  214. * If @major == 0 this functions will dynamically allocate a major and return
  215. * its number.
  216. *
  217. * If @major > 0 this function will attempt to reserve a device with the given
  218. * major number and will return zero on success.
  219. *
  220. * Returns a -ve errno on failure.
  221. *
  222. * The name of this device has nothing to do with the name of the device in
  223. * /dev. It only helps to keep track of the different owners of devices. If
  224. * your module name has only one type of devices it's ok to use e.g. the name
  225. * of the module here.
  226. */
  227. int __register_chrdev(unsigned int major, unsigned int baseminor,
  228. unsigned int count, const char *name,
  229. const struct file_operations *fops)
  230. {
  231. struct char_device_struct *cd;
  232. struct cdev *cdev;
  233. int err = -ENOMEM;
  234. cd = __register_chrdev_region(major, baseminor, count, name);
  235. if (IS_ERR(cd))
  236. return PTR_ERR(cd);
  237. cdev = cdev_alloc();
  238. if (!cdev)
  239. goto out2;
  240. cdev->owner = fops->owner;
  241. cdev->ops = fops;
  242. kobject_set_name(&cdev->kobj, "%s", name);
  243. err = cdev_add(cdev, MKDEV(cd->major, baseminor), count);
  244. if (err)
  245. goto out;
  246. cd->cdev = cdev;
  247. return major ? 0 : cd->major;
  248. out:
  249. kobject_put(&cdev->kobj);
  250. out2:
  251. kfree(__unregister_chrdev_region(cd->major, baseminor, count));
  252. return err;
  253. }
  254. /**
  255. * unregister_chrdev_region() - unregister a range of device numbers
  256. * @from: the first in the range of numbers to unregister
  257. * @count: the number of device numbers to unregister
  258. *
  259. * This function will unregister a range of @count device numbers,
  260. * starting with @from. The caller should normally be the one who
  261. * allocated those numbers in the first place...
  262. */
  263. void unregister_chrdev_region(dev_t from, unsigned count)
  264. {
  265. dev_t to = from + count;
  266. dev_t n, next;
  267. for (n = from; n < to; n = next) {
  268. next = MKDEV(MAJOR(n)+1, 0);
  269. if (next > to)
  270. next = to;
  271. kfree(__unregister_chrdev_region(MAJOR(n), MINOR(n), next - n));
  272. }
  273. }
  274. /**
  275. * __unregister_chrdev - unregister and destroy a cdev
  276. * @major: major device number
  277. * @baseminor: first of the range of minor numbers
  278. * @count: the number of minor numbers this cdev is occupying
  279. * @name: name of this range of devices
  280. *
  281. * Unregister and destroy the cdev occupying the region described by
  282. * @major, @baseminor and @count. This function undoes what
  283. * __register_chrdev() did.
  284. */
  285. void __unregister_chrdev(unsigned int major, unsigned int baseminor,
  286. unsigned int count, const char *name)
  287. {
  288. struct char_device_struct *cd;
  289. cd = __unregister_chrdev_region(major, baseminor, count);
  290. if (cd && cd->cdev)
  291. cdev_del(cd->cdev);
  292. kfree(cd);
  293. }
  294. static __cacheline_aligned_in_smp DEFINE_SPINLOCK(cdev_lock);
  295. static struct kobject *cdev_get(struct cdev *p)
  296. {
  297. struct module *owner = p->owner;
  298. struct kobject *kobj;
  299. if (!try_module_get(owner))
  300. return NULL;
  301. kobj = kobject_get_unless_zero(&p->kobj);
  302. if (!kobj)
  303. module_put(owner);
  304. return kobj;
  305. }
  306. void cdev_put(struct cdev *p)
  307. {
  308. if (p) {
  309. struct module *owner = p->owner;
  310. kobject_put(&p->kobj);
  311. module_put(owner);
  312. }
  313. }
  314. /*
  315. * Called every time a character special file is opened
  316. */
  317. static int chrdev_open(struct inode *inode, struct file *filp)
  318. {
  319. const struct file_operations *fops;
  320. struct cdev *p;
  321. struct cdev *new = NULL;
  322. int ret = 0;
  323. spin_lock(&cdev_lock);
  324. p = inode->i_cdev;
  325. if (!p) {
  326. struct kobject *kobj;
  327. int idx;
  328. spin_unlock(&cdev_lock);
  329. kobj = kobj_lookup(cdev_map, inode->i_rdev, &idx);
  330. if (!kobj)
  331. return -ENXIO;
  332. new = container_of(kobj, struct cdev, kobj);
  333. spin_lock(&cdev_lock);
  334. /* Check i_cdev again in case somebody beat us to it while
  335. we dropped the lock. */
  336. p = inode->i_cdev;
  337. if (!p) {
  338. inode->i_cdev = p = new;
  339. list_add(&inode->i_devices, &p->list);
  340. new = NULL;
  341. } else if (!cdev_get(p))
  342. ret = -ENXIO;
  343. } else if (!cdev_get(p))
  344. ret = -ENXIO;
  345. spin_unlock(&cdev_lock);
  346. cdev_put(new);
  347. if (ret)
  348. return ret;
  349. ret = -ENXIO;
  350. fops = fops_get(p->ops);
  351. if (!fops)
  352. goto out_cdev_put;
  353. replace_fops(filp, fops);
  354. if (filp->f_op->open) {
  355. ret = filp->f_op->open(inode, filp);
  356. if (ret)
  357. goto out_cdev_put;
  358. }
  359. return 0;
  360. out_cdev_put:
  361. cdev_put(p);
  362. return ret;
  363. }
  364. void cd_forget(struct inode *inode)
  365. {
  366. spin_lock(&cdev_lock);
  367. list_del_init(&inode->i_devices);
  368. inode->i_cdev = NULL;
  369. inode->i_mapping = &inode->i_data;
  370. spin_unlock(&cdev_lock);
  371. }
  372. static void cdev_purge(struct cdev *cdev)
  373. {
  374. spin_lock(&cdev_lock);
  375. while (!list_empty(&cdev->list)) {
  376. struct inode *inode;
  377. inode = container_of(cdev->list.next, struct inode, i_devices);
  378. list_del_init(&inode->i_devices);
  379. inode->i_cdev = NULL;
  380. }
  381. spin_unlock(&cdev_lock);
  382. }
  383. /*
  384. * Dummy default file-operations: the only thing this does
  385. * is contain the open that then fills in the correct operations
  386. * depending on the special file...
  387. */
  388. const struct file_operations def_chr_fops = {
  389. .open = chrdev_open,
  390. .llseek = noop_llseek,
  391. };
  392. static struct kobject *exact_match(dev_t dev, int *part, void *data)
  393. {
  394. struct cdev *p = data;
  395. return &p->kobj;
  396. }
  397. static int exact_lock(dev_t dev, void *data)
  398. {
  399. struct cdev *p = data;
  400. return cdev_get(p) ? 0 : -1;
  401. }
  402. /**
  403. * cdev_add() - add a char device to the system
  404. * @p: the cdev structure for the device
  405. * @dev: the first device number for which this device is responsible
  406. * @count: the number of consecutive minor numbers corresponding to this
  407. * device
  408. *
  409. * cdev_add() adds the device represented by @p to the system, making it
  410. * live immediately. A negative error code is returned on failure.
  411. */
  412. int cdev_add(struct cdev *p, dev_t dev, unsigned count)
  413. {
  414. int error;
  415. p->dev = dev;
  416. p->count = count;
  417. if (WARN_ON(dev == WHITEOUT_DEV)) {
  418. error = -EBUSY;
  419. goto err;
  420. }
  421. error = kobj_map(cdev_map, dev, count, NULL,
  422. exact_match, exact_lock, p);
  423. if (error)
  424. goto err;
  425. kobject_get(p->kobj.parent);
  426. return 0;
  427. err:
  428. kfree_const(p->kobj.name);
  429. p->kobj.name = NULL;
  430. return error;
  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 && dev->devt)
  480. cdev_del(cdev);
  481. return rc;
  482. }
  483. /**
  484. * cdev_device_del() - inverse of cdev_device_add
  485. * @cdev: the cdev structure
  486. * @dev: the device 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_obj(struct cdev);
  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);