自动创建设备文件
1.自动创建设备文件的流程
字符设备驱动模块 --》创建一个设备驱动class--->创建属于class的device--->调用mdev工具(自动完成)--> 生成设备文件
mdev工具会根据/sys下的class找到相对应的device,然后根据device创建设备文件
class /sys/class
device /sys/device
1.1创建class /注销class
struct class * class_create(struct module *owner,const char *name);
参数:
struct module *owner---> class属于哪些一个module --->THIS_MODULE
const char *name --->自定义的类名.
返回值:
NULL -->失败
void class_destroy(struct class * cls)
例:
struct class *led_class=class_create(THIS_MODULE,"led_class");
class_destroy(led_class)
1.2创建device /注销
struct device *device_create(struct class *class, struct device *parent,
dev_t devt, void *drvdata, const char *fmt, ...)
参数:
struct class *class --->当前device属于那个class
struct device *parent --->device的parent,一般为NULL
dev_t devt --->设备号
void *drvdata --->设备的私有数据,一般为NULL
const char *fmt ---》 device的名字,也就是设备文件名
返回值:
struct device *
NULL -->失败
void device_destroy(struct class * class,dev_t devt);
struct device *dp=device_create(led_class,NULL,dev_no,NULL,"led_drv") ---> /dev/led_drv (字符设备)
if(dp==NULL)
{
}
#include<linux/module.h>
#include<linux/kernel.h>
#include<linux/cdev.h>
#include<linux/fs.h>
#include<linux/kdev_t.h>
#include<linux/types.h>
#include<linux/uaccess.h>
#include<linux/string.h>
#include<linux/ioport.h>
#include<asm/io.h>
#include<linux/init.h>
#include<linux/device.h>
#define GPJ2CON_PA 0xe0200280
#define GPJ2DAT_PA 0xe0200280
unsigned int *GPJ2CON_VA;
unsigned int *GPJ2DAT_VA;
struct cdev chrdev;
struct resource *res;
struct class *cla;
struct device *dev;
unsigned int TestMajor=;
unsigned int TestMinor=;
dev_t dev_no;
int ret; int testopen(struct inode *inode,struct file *file)
{
unsigned int a = ioread32(GPJ2CON_VA);
a |= 0x1111;
iowrite32(a,GPJ2CON_VA);
printk("cdev init\n");
return ; }
ssize_t testwrite(struct file *file, const char __user *usr, size_t len, loff_t *off)
{
unsigned int a; copy_from_user(&a,usr,len);
iowrite32(a,GPJ2DAT_VA); }
ssize_t testread(struct file *file, char __user *usr, size_t len, loff_t *off)
{
unsigned int a = ioread32(GPJ2DAT_VA);
copy_to_user(usr,&a,len); }
int testrelease(struct inode *inode, struct file *file)
{
printk("close\n");
return ; } struct file_operations fops=
{
.owner=THIS_MODULE,
.open = testopen,
.write = testwrite,
.read = testread,
.release = testrelease,
};
static int __init test_init(void)
{
dev_no = MKDEV(TestMajor,TestMinor);
if(dev_no>)
{
ret = register_chrdev_region(dev_no,,"chrdev_test");
}
else
{
alloc_chrdev_region(&dev_no,,,"chrdev_test");
}
if(ret<)
{
return ret;
}
cdev_init(&chrdev,&fops);
chrdev.owner=THIS_MODULE;
cdev_add(&chrdev,dev_no,);
res = request_mem_region(GPJ2CON_PA,,"gpj2_led");
GPJ2CON_VA = ioremap(GPJ2CON_PA,);
GPJ2DAT_VA = GPJ2CON_VA + ;
cla = class_create(THIS_MODULE,"led_class");
if(cla == NULL)
{
printk("class_creat() error!\n");
}
dev = device_create(cla,NULL,dev_no,NULL,"chrdev_test");
if(dev == NULL)
{
printk("device_creat() error!\n");
} return ;
} static int __exit test_exit(void)
{
unregister_chrdev_region(dev_no,);
cdev_del(&chrdev);
iounmap(GPJ2CON_VA);
release_mem_region(GPJ2CON_PA,);
device_destroy(cla,dev_no);
class_destroy(cla);
return ;
} module_init(test_init);
module_exit(test_exit); MODULE_AUTHOR("FENG");
MODULE_DESCRIPTION("the first module of char drivers");
MODULE_LICENSE("GPL");
MODULE_VERSION("V1.0");