注册驱动程序的几种方法:
(1)调用register_chrdev
if (register_chrdev(MAJOR_NUM, " gobalvar ", &gobalvar_fops))
{
//…注册失败
}
else
{
//…注册成功
}
(2)先调用register_chrdev_region,再调用cdev_add
dev_id = MKDEV(major, 0);
retval = register_chrdev_region(dev_id, 1,NAME);
cdev_init(&dev, &fops);
cdev_add(&dev, dev_id, 1);
(3)前两种方法使用在已知主设备号的情况下,这一种方法由内核自动分配主设备号。例子:
if (major) {
dev_id = MKDEV(major, 0);
retval = register_chrdev_region(dev_id, CS5535_GPIO_COUNT,
NAME);
} else {
retval = alloc_chrdev_region(&dev_id, 0, CS5535_GPIO_COUNT,
NAME);
major = MAJOR(dev_id);
}
if (retval) {
release_region(gpio_base, CS5535_GPIO_SIZE);
return -1;
}
printk(KERN_DEBUG NAME ": base=%#x mask=%#lx major=%d\n",
gpio_base, mask, major);
cdev_init(&cs5535_gpio_cdev, &cs5535_gpio_fops);
cdev_add(&cs5535_gpio_cdev, dev_id, CS5535_GPIO_COUNT);