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 }