前一篇对cdev结构体及初始化做了简单介绍

Linux内核有两个分配设备号的函数

 1 /**
 2  * register_chrdev_region() - register a range of device numbers
 3  * @from: the first in the desired range of device numbers; must include
 4  *        the major number.
 5  * @count: the number of consecutive device numbers required
 6  * @name: the name of the device or driver.
 7  *
 8  * Return value is zero on success, a negative error code on failure.
 9  */
10 int register_chrdev_region(dev_t from, unsigned count, const char *name)
11 {
12     struct char_device_struct *cd;
13     dev_t to = from + count;
14     dev_t n, next;
15 
16     for (n = from; n < to; n = next) {
17         next = MKDEV(MAJOR(n)+1, 0);
18         if (next > to)
19             next = to;
20         cd = __register_chrdev_region(MAJOR(n), MINOR(n),
21                    next - n, name);
22         if (IS_ERR(cd))
23             goto fail;
24     }
25     return 0;
26 fail:
27     to = n;
28     for (n = from; n < to; n = next) {
29         next = MKDEV(MAJOR(n)+1, 0);
30         kfree(__unregister_chrdev_region(MAJOR(n), MINOR(n), next - n));
31     }
32     return PTR_ERR(cd);
33 }
register_chrdev_region()

相关文章:

  • 2021-09-04
  • 2022-12-23
  • 2021-10-12
  • 2021-10-03
  • 2022-02-08
  • 2021-11-23
  • 2021-12-13
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-06-16
  • 2021-04-07
  • 2022-12-23
  • 2021-10-06
  • 2022-12-23
  • 2021-07-13
相关资源
相似解决方案