static void second_setup_cdev(struct second_dev *dev, int index)
{
int err, devno = MKDEV(second_major, index);
cdev_init(&dev->cdev, &second_fops);
dev->cdev.owner = THIS_MODULE;
dev->cdev.ops = &second_fops;
err = cdev_add(&dev->cdev, devno, 1);
if (err)
printk(KERN_NOTICE "Error %d adding LED%d", err, index);
}
/*设备驱动模块加载函数*/
int second_init(void)
{
int ret;
dev_t devno = MKDEV(second_major, 0);
/* 申请设备号*/
if (second_major)
ret = register_chrdev_region(devno, 1, "second");
else /* 动态申请设备号 */
{
ret = alloc_chrdev_region(&devno, 0, 1, "second");
second_major = MAJOR(devno);
}
if (ret < 0)
return ret;
/* 动态申请设备结构体的内存*/
second_devp = kmalloc(sizeof(struct second_dev), GFP_KERNEL);
if (!second_devp) /*申请失败*/
{
ret = - ENOMEM;
goto fail_malloc;
}
memset(second_devp, 0, sizeof(struct second_dev));
second_setup_cdev(second_devp, 0);
return 0;
fail_malloc: unregister_chrdev_region(devno, 1);
}