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