major = register_chrdev(0, "button_drv", &button_ops); button_class = class_create(THIS_MODULE, "button_drv"); button_class_device = class_device_create(button_class, NULL, MKDEV(major, 0), NULL, "by_button");现在的驱动
alloc_chrdev_region(&dev_id, 0, 2, "bym_drv"); //分配设备号范围 major = MAJOR(dev_id); //取得分配到的设备号的主设备号 cdev_init(cdev, &drv_ops); cdev_add(cdev, dev_id, 2); //添加cdev设备 drv_class = class_create(THIS_MODULE, "bym_drv"); //创建类 class_device_create(drv_class, 0, MKDEV(major, 0), 0, "bym_test_drv0"); //在类上面创建设备节点
int register_chrdev(unsigned int major, const char *name, const struct file_operations *fops) cd = __register_chrdev_region(major, 0, 256, name); if (major == 0) { //分配主设备号 for (i = ARRAY_SIZE(chrdevs)-1; i > 0; i--) { if (chrdevs[i] == NULL) break; } err = cdev_add(cdev, MKDEV(cd->major, 0), 256); //将设备注册
alloc_chrdev_region(&dev_id, 0, 2, "bym_drv"); //分配设备号范围,人为指定为两个次设备号对应一个驱动 __register_chrdev_region(0, 0, 2, "bym_drv"); //注册相应的设备范围region(区域/范围) major = MAJOR(dev_id); //取得分配到的设备号的主设备号 /* 如果已经有了主设备号 */ dev_id = MKDEV(major, 0); register_chrdev_region(dev_id, 2, "bym_drv"); for (n = dev_id; n < dev_id + 2; n = next) { //查询注册 next = MKDEV(MAJOR(n)+1, 0); if (next > dev_id + 2) next = dev_id + 2; cd = __register_chrdev_region(MAJOR(n), MINOR(n), next - n, name); if (IS_ERR(cd)) goto fail; } /* 以上是有了主设备号之后 */ cdev_init(cdev, &drv_ops); cdev_add(cdev, dev_id, 2); //添加cdev设备
如果开始没有主设备号的话,要先分配设备号,alloc_chrdev_region(&dev_id, 0, 2, "bym_drv");//分配设备号范围,人为指定为两个次设备号对应一个驱动,得到主设备号major = MAJOR(dev_id);//取得分配到的设备号的主设备号
如果有了主设备号,直接注册次设备号
dev_id = MKDEV(major, 0);cdev_init(cdev, &drv_ops); cdev_add(cdev, dev_id, 2); //添加cdev设备
drv_class = class_create(THIS_MODULE, "bym_drv"); //创建类 class_device_create(drv_class, 0, MKDEV(major, 0), 0, "bym_test_drv0"); class_device_create(drv_class, 0, MKDEV(major, 1), 0, "bym_test_drv1");