当前位置: 技术问答>linux和unix
简答的字符驱动
来源: 互联网 发布时间:2017-01-05
本文导语: 我自己写了个简单的字符驱动,编译什么都没有错,模块加载也成功了,但自动创建的设备节点没有,部分代码如下: static void my_cdev_setup(struct my_cdev *dev, int index) { int err; dev_t devno = MKDEV(device_major, index); ...
我自己写了个简单的字符驱动,编译什么都没有错,模块加载也成功了,但自动创建的设备节点没有,部分代码如下:
static void my_cdev_setup(struct my_cdev *dev, int index)
{
int err;
dev_t devno = MKDEV(device_major, index);
cdev_init(&dev->cdev,&my_cdev_fops);
dev->cdev.owner = THIS_MODULE;
dev->cdev.ops = &my_cdev_fops;
err = cdev_add(&dev->cdev,devno,1);
if(err)
printk(KERN_NOTICE"Error %d adding LED %dn", err, index);
}
static int __init my_cdev_init(void)
{
int result;
dev_t devno;
printk(KERN_ALERT"init my char drivern");
if(device_major)
{
devno = MKDEV(DEVICE_MAJOR, 0);
result = register_chrdev_region(devno,1,"my_cdev_qiong");
}
else
{
result = alloc_chrdev_region(&devno,0,1,"my_cdev_qiong");
device_major = MAJOR(devno);
}
if(result cdev);
device_destroy(my_cdev_class, MKDEV(device_major, 0));
class_destroy(my_cdev_class);
unregister_chrdev_region(MKDEV(device_major, 0), 1);
kfree(my_cdevp);
}
为什么创建设备节点会没有,如果用命令的话是可以创建成功的,还想问下在卸载模块时老是说模块在被使用,但我只是加载了,什么都没做,求高人解答!!!
static void my_cdev_setup(struct my_cdev *dev, int index)
{
int err;
dev_t devno = MKDEV(device_major, index);
cdev_init(&dev->cdev,&my_cdev_fops);
dev->cdev.owner = THIS_MODULE;
dev->cdev.ops = &my_cdev_fops;
err = cdev_add(&dev->cdev,devno,1);
if(err)
printk(KERN_NOTICE"Error %d adding LED %dn", err, index);
}
static int __init my_cdev_init(void)
{
int result;
dev_t devno;
printk(KERN_ALERT"init my char drivern");
if(device_major)
{
devno = MKDEV(DEVICE_MAJOR, 0);
result = register_chrdev_region(devno,1,"my_cdev_qiong");
}
else
{
result = alloc_chrdev_region(&devno,0,1,"my_cdev_qiong");
device_major = MAJOR(devno);
}
if(result cdev);
device_destroy(my_cdev_class, MKDEV(device_major, 0));
class_destroy(my_cdev_class);
unregister_chrdev_region(MKDEV(device_major, 0), 1);
kfree(my_cdevp);
}
为什么创建设备节点会没有,如果用命令的话是可以创建成功的,还想问下在卸载模块时老是说模块在被使用,但我只是加载了,什么都没做,求高人解答!!!
|
没有,你试下就知道。少了那个NULL会oops crash
|
好像是和别人的节点号冲突了
|
改成自动获取节点吧,省事,不会出错。
给你个参考代码,我自己写的,代码肯定能够没有问题。
static int __init spi_init(void)
{
int result;
dev = MKDEV(spi_major, 0);
/**/
if (spi_major)
{
result = register_chrdev_region(dev, 1, spi_name);
}
else /**/
{
result = alloc_chrdev_region(&dev, 0, 1, spi_name);
spi_major = MAJOR(dev);
}
if (result
给你个参考代码,我自己写的,代码肯定能够没有问题。
static int __init spi_init(void)
{
int result;
dev = MKDEV(spi_major, 0);
/**/
if (spi_major)
{
result = register_chrdev_region(dev, 1, spi_name);
}
else /**/
{
result = alloc_chrdev_region(&dev, 0, 1, spi_name);
spi_major = MAJOR(dev);
}
if (result