linux_driver自动创建节点的解析
来源:互联网 发布:好看的青春爱情网络剧 编辑:程序博客网 时间:2024/06/05 18:46
自动创建节点的解析
在驱动用加入对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定义在头文件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 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 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");
下面www.linuxidc.com 以一个简单字符设备驱动来展示如何使用这几个函数
#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 file_operations hello_fops = {
.owner = THIS_MODULE
};
static int __init hello_init (void)
{
int result;
devno = MKDEV(HELLO_MAJOR, HELLO_MINOR);
if (HELLO_MAJOR)
result = register_chrdev_region(devno, 2, "memdev");
else
{
result = alloc_chrdev_region(&devno, 0, 2, "memdev");
HELLO_MAJOR = MAJOR(devno);
}
printk("MAJOR IS %d\n",HELLO_MAJOR);
my_class = class_create(THIS_MODULE,"hello_char_class"); //类名为hello_char_class
if(IS_ERR(my_class))
{
printk("Err: failed in creating class.\n");
return -1;
}
device_create(my_class,NULL,devno,NULL,"memdev"); //设备名为memdev
if (result<0)
{
printk (KERN_WARNING "hello: can't get major number %d\n", HELLO_MAJOR);
return result;
}
cdev_init(&cdev, &hello_fops);
cdev.owner = THIS_MODULE;
cdev_add(&cdev, devno, NUMBER_OF_DEVICES);
printk (KERN_INFO "Character driver Registered\n");
return 0;
}
static void __exit hello_exit (void)
{
cdev_del (&cdev);
device_destroy(my_class, devno); //delete device node under /dev//必须先删除设备,再删除class类
class_destroy(my_class); //delete class created by us
unregister_chrdev_region (devno,NUMBER_OF_DEVICES);
printk (KERN_INFO "char driver cleaned up\n");
}
module_init (hello_init);
module_exit (hello_exit);
MODULE_LICENSE ("GPL");
这样,模块加载后,就能在/dev目录下找到memdev这个设备节点了。
- linux_driver自动创建节点的解析
- 设备节点的自动创建
- 自动创建设备节点
- 自动创建设备节点
- 自动创建设备节点
- 自动创建设备节点
- 自动创建设备节点
- 自动创建设备文件节点
- 自动创建设备节点 device_create
- Linux自动创建设备节点
- 设备驱动-----自动创建节点
- 设备驱动--自动创建节点
- linux 自动创建设备节点
- 自动创建设备节点笔记
- linux自动创建设备节点
- 自动创建设备节点 device_create
- linux自动创建设备节点
- 字符驱动自动创建节点
- uva 1588Kickdown
- 在aws默认的linux的instance上如何迅速安装java环境
- 关于启动超时的问题
- Java之七:HashMap的实现原理
- 桑德菲杰快圣诞节
- linux_driver自动创建节点的解析
- air报错 "Context3D not available." fd as3项目默认不开启gpu加速的问题
- 电话给V及客户客户空间
- 就会给V交话费V机会
- 图像修复二:基于 Object Removal by Exemplar-Based Inpainting 的改进算法
- away3d学习笔记 转载
- UVA 1339 - Ancient Cipher
- genymotion常见问题解答
- 据阿斯顿孩子看病苜蓿草最下很满足对方,没好地方