设备号的获取与字符设备的注册

int major;

static struct class *buttons_class;
struct cdev *buttons_cdev;// = cdev_alloc();
//buttons_cdev->ops = &buttons_fops;

dev_t buttons_dev;

static int __init buttons_init(void)

{
int err;

alloc_chrdev_region(&buttons_dev, 0, 1, "buttons");

buttons_cdev = cdev_alloc();

cdev_init(buttons_cdev, &buttons_fops);

buttons_cdev->owner = THIS_MODULE;
buttons_cdev->ops = &buttons_fops;

err = cdev_add(buttons_cdev, buttons_dev, 1);

// major = register_chrdev(0, "buttons", &buttons_fops);

buttons_class = class_create(THIS_MODULE, "buttons");

device_create(buttons_class, NULL, buttons_dev, NULL, "buttons");

return 0;
}


static void __exit buttons_exit(void)
{
device_destroy(buttons_class, buttons_dev);

class_destroy(buttons_class);

unregister_chrdev_region(buttons_dev, 1);

cdev_del(buttons_cdev);

// unregister_chrdev(major, "buttons");
return ;
}

posted on 2016-11-28 16:10  asulove  阅读(352)  评论(0编辑  收藏  举报

导航