god@god-virtual-machine:/proc/bus/input$ cat /proc/devices Character devices: 1 mem 4 /dev/vc/0 4 tty 4 ttyS 5 /dev/tty 5 /dev/console 5 /dev/ptmx 5 ttyprintk 6 lp 7 vcs 10 misc 13 input ...... 252 ndctl 253 tpm 254 gpiochip Block devices: 7 loop 8 sd ...... 135 sd 253 device-mapper 254 mdp 259 blkext
由上可知,设备分为字符设备和块设备,数字表示主设备号
以下介绍的,无论是设备号,还是设备注册,都是指字符设备。
以下函数功能,包含了设备号的申请、字符设备的申请、字符设备添加到内核
static inline int register_chrdev(unsigned int major, const char *name, const struct file_operations *fops) { return __register_chrdev(major, 0, 256, name, fops); }
以下函数,只包含设备号的申请,from是起始设备号,由12位主设备号 + 20子设备号组成32bit值,count是申请设备号的数量
int register_chrdev_region(dev_t from, unsigned count, const char *name)
以下函数,只包含设备号的申请,register_chrdev() 和 register_chrdev_region() 最终都是调用此函数进行设备号的申请
static struct char_device_struct *__register_chrdev_region(unsigned int major, unsigned int baseminor, int minorct, const char *name)
register_chrdev |
设备号的申请,字符设备的申请、字符设备添加到内核 |
register_chrdev_region |
设备号的申请(函数名具有迷惑性,没有字符设备添加到内核功能) |
__register_chrdev_region |
设备号的申请(函数名具有迷惑性,没有字符设备添加到内核功能) |
static struct char_device_struct * __register_chrdev_region(unsigned int major, unsigned int baseminor, int minorct, const char *name) { struct char_device_struct *cd, *curr, *prev = NULL; int ret; int i; if (major >= CHRDEV_MAJOR_MAX) { pr_err("CHRDEV \"%s\" major requested (%u) is greater than the maximum (%u)\n", name, major, CHRDEV_MAJOR_MAX-1); return ERR_PTR(-EINVAL); } if (minorct > MINORMASK + 1 - baseminor) { pr_err("CHRDEV \"%s\" minor range requested (%u-%u) is out of range of maximum range (%u-%u) for a single major\n", name, baseminor, baseminor + minorct - 1, 0, MINORMASK); return ERR_PTR(-EINVAL); } cd = kzalloc(sizeof(struct char_device_struct), GFP_KERNEL); if (cd == NULL) return ERR_PTR(-ENOMEM); mutex_lock(&chrdevs_lock); if (major == 0) { ret = find_dynamic_major(); if (ret < 0) { pr_err("CHRDEV \"%s\" dynamic allocation region is full\n", name); goto out; } major = ret; } ret = -EBUSY; i = major_to_index(major); for (curr = chrdevs[i]; curr; prev = curr, curr = curr->next) { if (curr->major < major) continue; if (curr->major > major) break; if (curr->baseminor + curr->minorct <= baseminor) continue; if (curr->baseminor >= baseminor + minorct) break; goto out; } cd->major = major; cd->baseminor = baseminor; cd->minorct = minorct; strlcpy(cd->name, name, sizeof(cd->name)); if (!prev) { cd->next = curr; chrdevs[i] = cd; } else { cd->next = prev->next; prev->next = cd; } mutex_unlock(&chrdevs_lock); return cd; out: mutex_unlock(&chrdevs_lock); kfree(cd); return ERR_PTR(ret); }
由此函数可知,主设备号虽然由12位,但是主设备号的范围只支持 0~511,子设备号则占满20bit
标签:字符,major,curr,name,int,申请,cd,注册,设备 From: https://www.cnblogs.com/god-of-death/p/17228557.html