Linux内核驱动自动创建设备节点文件

Linux下生成驱动设备节点文件的方法有3个:1、手动mknod;2、利用devfs;3、利用udev
在刚开始写Linux设备驱动程序的时候,很多时候都是利用mknod命令手动创建设备节点,实际上Linux内核为我们提供了一组函数,可以用来在模块加载的时候自动在/dev目录下创建相应设备节点,并在卸载模块时删除该节点。
在2.6.17以前,在/dev目录下生成设备文件很容易,
devfs_mk_bdev
devfs_mk_cdev
devfs_mk_symlink
devfs_mk_dir
devfs_remove
这几个是纯devfs的api,2.6.17以前可用,但后来devfs被 sysfs+udev的形式取代,同时期sysfs文件系统可以用的api:
class_device_create_file,在2.6.26以后也不行了,现在,使用的是device_create ,从2.6.18开始可用
struct device *device_create(struct class *class, struct device *parent,
dev_t devt, const char *fmt, ...)
从2.6.26起又多了一个参数drvdata: the data to be added to the device for callbacks
不会用可以给此参数赋NULL
struct device * device_create(struct class *class, struct device *parent,
dev_t devt, void *drvdata, const char *fmt, ...)
 
下面着重讲解第三种方法udev
在驱动用加入对udev的支持主要做的就是:在驱动初始化的代码里调用 class_create(...)为该设备创建一个class,再为每个设备调用 device_create(...)( 在2.6较早的内核中用class_device_create)创建对应的设备。
内核中定义的struct class结构体,顾名思义,一个struct class结构体类型变量对应一个类,内核同时提供了 class_create(…)函数,可以用它来创建一个类,这个类存放于sysfs下面,一旦创建好了这个类, 再调用 device_create(…)函数来在/dev目录下创建相应的设备节点。这样, 加载模块的时候,用户空间中的udev会自动响应 device_create(…)函数,去/sysfs下寻找对应的类从而创建设备节点。
struct class和class_create(…) 以及device_create(…)都包含在在/include/linux/device.h中,使用的时候一定要包含这个头文件,否则编译器会报错。
struct class定义在头文件include/linux/device.h中
class_create(…)在/drivers/base/class.c中实现
device_create(…)函数在/drivers/base/core.c中实现
class_destroy(...),device_destroy(...)也在/drivers/base/core.c中实现调用过程类似如下:
static struct class *spidev_class;
 
/*-------------------------------------------------------------------------*/
 
static int __devinit spidev_probe(struct spi_device *spi)
{
        ....
        
        dev = device_create(spidev_class, &spi->dev, spidev->devt,
                spidev, "spidev%d.%d",
                spi->master->bus_num, spi->chip_select);
        ...
}
 
static int __devexit spidev_remove(struct spi_device *spi)
{
    ......
    device_destroy(spidev_class, spidev->devt);
    .....
 
    return 0;
}
 
static struct spi_driver spidev_spi = {
    .driver = {
        .name =        "spidev",
        .owner =    THIS_MODULE,
    },
    .probe =    spidev_probe,
    .remove =    __devexit_p(spidev_remove),
 
};
 
/*-------------------------------------------------------------------------*/
 
static int __init spidev_init(void)
{
    ....
 
    spidev_class = class_create(THIS_MODULE, "spidev");
    if (IS_ERR(spidev_class)) {
        unregister_chrdev(SPIDEV_MAJOR, spidev_spi.driver.name);
        return PTR_ERR(spidev_class);
    }
    ....
}
module_init(spidev_init);
 
static void __exit spidev_exit(void)
{
    ......
    class_destroy(spidev_class);
    ......
}
module_exit(spidev_exit);
 
MODULE_DESCRIPTION("User mode SPI device interface");
MODULE_LICENSE("GPL");
 
 下面以一个简单字符设备驱动来展示如何使用这几个函数 
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/device.h>
 
int HELLO_MAJOR = 0;
int HELLO_MINOR = 0;
int NUMBER_OF_DEVICES = 2;
 
struct class *my_class;
//struct cdev cdev;
//dev_t devno;
 
struct hello_dev {
struct device *dev;
dev_t chrdev;
struct cdev cdev;
};

static struct hello_dev *my_hello_dev = NULL;

struct file_operations hello_fops = {
 .owner = THIS_MODULE
};
 
static int __init hello_init (void)
{
int err = 0;
struct device *dev;

my_hello_dev = kzalloc(sizeof(struct hello_dev), GFP_KERNEL);
if (NULL == my_hello_dev) {
printk("%s kzalloc failed!\n",__func__);
return -ENOMEM;
}

devno = MKDEV(HELLO_MAJOR, HELLO_MINOR);
if (HELLO_MAJOR)
err= register_chrdev_region(my_hello_dev->chrdev, 2, "memdev");
else
{
err = alloc_chrdev_region(&my_hello_dev->chrdev, 0, 2, "memdev");
HELLO_MAJOR = MAJOR(devno);
}  
if (err) {
printk("%s alloc_chrdev_region failed!\n",__func__);
goto alloc_chrdev_err;
}
printk("MAJOR IS %d\n",HELLO_MAJOR);

cdev_init(&(my_hello_dev->cdev), &hello_fops);
my_hello_dev->cdev.owner = THIS_MODULE;
err = cdev_add(&(my_hello_dev->cdev), my_hello_dev->chrdev, 1);
if (err) {
printk("%s cdev_add failed!\n",__func__);
goto cdev_add_err;
}
printk (KERN_INFO "Character driver Registered\n");

my_class = class_create(THIS_MODULE,"hello_char_class");  //类名为hello_char_class
if(IS_ERR(my_class)) 
{
err = PTR_ERR(my_class);
printk("%s class_create failed!\n",__func__);
goto class_err;
}

dev = device_create(my_class,NULL,my_hello_dev->chrdev,NULL,"memdev%d",0);      //设备名为memdev
if (IS_ERR(dev)) {
err = PTR_ERR(dev);
gyro_err("%s device_create failed!\n",__func__);
goto device_err;
}

printk("hello module initialization\n");
return 0;
 
device_err:
device_destroy(my_class, my_hello_dev->chrdev);
class_err:
cdev_del(my_hello_dev->chrdev);
cdev_add_err:
unregister_chrdev_region(my_hello_dev->chrdev, 1);
alloc_chrdev_err:
kfree(my_hello_dev);
return err;
}
 
static void __exit hello_exit (void)
{
cdev_del (&(my_hello_dev->cdev));
unregister_chrdev_region (my_hello_dev->chrdev,1);
device_destroy(my_class, devno);         //delete device node under /dev//必须先删除设备,再删除class类
class_destroy(my_class);                 //delete class created by us
printk (KERN_INFO "char driver cleaned up\n");
}
 
module_init (hello_init);
module_exit (hello_exit);
 
MODULE_LICENSE ("GPL");
这样,模块加载后,就能在/dev目录下找到memdev这个设备节点了。
 例2:内核中的drivers/i2c/i2c-dev.c
在i2cdev_attach_adapter中调用device_create(i2c_dev_class, &adap->dev,
         MKDEV(I2C_MAJOR, adap->nr), NULL,
         "i2c-%d", adap->nr);
这样在dev目录就产生i2c-0  或i2c-1节点
 
接下来就是udev应用,udev是应用层的东西,udev需要内核sysfs和tmpfs的支持,sysfs为udev提供设备入口和uevent通道,tmpfs为udev设备文件提供存放空间
udev的源码可以在去相关网站下载,然后就是对其在运行环境下的移植,指定交叉编译环境,修改Makefile下的CROSS_COMPILE,如为mipsel-linux-,DESTDIR=xxx,或直接make CROSS_COMPILE=mipsel-linux-,DESTDIR=xxx 并install
把主要生成的udevd、udevstart拷贝rootfs下的/sbin/目录内,udev的配置文件udev.conf和rules.d下的rules文件拷贝到rootfs下的/etc/目录内
并在rootfs/etc/init.d/rcS中添加以下几行:
echo “Starting udevd...”
/sbin/udevd --daemon
/sbin/udevstart
(原rcS内容如下:
# mount filesystems
/bin/mount -t proc /proc /proc
/bin/mount -t sysfs sysfs /sys
/bin/mount -t tmpfs tmpfs /dev
# create necessary devices
/bin/mknod /dev/null c 1 3
/bin/mkdir /dev/pts
/bin/mount -t devpts devpts /dev/pts
/bin/mknod /dev/audio c 14 4
/bin/mknod /dev/ts c 10 16
这样当系统启动后,udevd和udevstart就会解析配置文件,并自动在/dev下创建设备节点文件
 
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要利用内核模块创建一个设备文件节点,可以按照以下步骤进行: 1. 编写一个内核模块,包括设备驱动程序和设备文件节点创建代码。 2. 在设备驱动程序中实现设备的初始化、读写函数等操作。 3. 在设备驱动程序中使用`register_chrdev()`函数注册字符设备驱动程序,并获得主设备号。 4. 在设备驱动程序中实现`cdev_init()`函数和`cdev_add()`函数,注册字符设备。 5. 在设备驱动程序中实现`class_create()`函数和`device_create()`函数,创建设备类和设备文件节点。 6. 在设备驱动程序中实现设备文件节点的读写函数。 7. 在内核模块的初始化函数中调用上述函数,完成设备文件节点创建和注册。 以下是一个示例代码,可以用于创建一个名为`mydevice`的设备文件节点: ```c #include <linux/init.h> #include <linux/module.h> #include <linux/fs.h> #include <linux/cdev.h> #include <linux/device.h> MODULE_LICENSE("Dual BSD/GPL"); static dev_t devno; static struct cdev mycdev; static struct class *myclass; static struct device *mydevice; static int myopen(struct inode *inode, struct file *file) { printk(KERN_INFO "mydevice opened\n"); return 0; } static int myrelease(struct inode *inode, struct file *file) { printk(KERN_INFO "mydevice closed\n"); return 0; } static ssize_t myread(struct file *file, char __user *buf, size_t count, loff_t *f_pos) { printk(KERN_INFO "mydevice read\n"); return 0; } static ssize_t mywrite(struct file *file, const char __user *buf, size_t count, loff_t *f_pos) { printk(KERN_INFO "mydevice write\n"); return count; } static struct file_operations fops = { .owner = THIS_MODULE, .open = myopen, .release = myrelease, .read = myread, .write = mywrite, }; static int __init mymodule_init(void) { int ret; devno = MKDEV(202, 0); ret = register_chrdev_region(devno, 1, "mydevice"); if (ret < 0) { printk(KERN_ERR "register_chrdev_region failed\n"); return ret; } cdev_init(&mycdev, &fops); mycdev.owner = THIS_MODULE; ret = cdev_add(&mycdev, devno, 1); if (ret < 0) { unregister_chrdev_region(devno, 1); printk(KERN_ERR "cdev_add failed\n"); return ret; } myclass = class_create(THIS_MODULE, "myclass"); if (IS_ERR(myclass)) { cdev_del(&mycdev); unregister_chrdev_region(devno, 1); printk(KERN_ERR "class_create failed\n"); return PTR_ERR(myclass); } mydevice = device_create(myclass, NULL, devno, NULL, "mydevice"); if (IS_ERR(mydevice)) { class_destroy(myclass); cdev_del(&mycdev); unregister_chrdev_region(devno, 1); printk(KERN_ERR "device_create failed\n"); return PTR_ERR(mydevice); } printk(KERN_INFO "mydevice module loaded\n"); return 0; } static void __exit mymodule_exit(void) { device_destroy(myclass, devno); class_destroy(myclass); cdev_del(&mycdev); unregister_chrdev_region(devno, 1); printk(KERN_INFO "mydevice module unloaded\n"); } module_init(mymodule_init); module_exit(mymodule_exit); ``` 在该示例代码中,`myopen()`、`myrelease()`、`myread()`和`mywrite()`函数分别实现了设备文件节点的打开、关闭、读和写操作。`register_chrdev_region()`函数用于注册字符设备号,`cdev_init()`和`cdev_add()`函数用于注册字符设备,`class_create()`和`device_create()`函数用于创建设备类和设备文件节点。在模块初始化函数中,调用了上述函数,完成了设备文件节点创建和注册。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值