自动创建设备文件
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");

自动创建字符设备,不需mknod的更多相关文章

  1. linux之misc及使用misc创建字符设备

    1:linux字符设备及udev 1.1字符设备 字符设备就是:一个一个字节来进行访问的,不能对字符设备进行随机读写.简单字符设备创建实例如下: #include <linux/module.h ...

  2. LCD驱动分析(一)字符设备驱动框架分析

    参考:S3C2440 LCD驱动(FrameBuffer)实例开发<一>   S3C2440 LCD驱动(FrameBuffer)实例开发<二> LCD驱动也是字符设备驱动,也 ...

  3. platform型设备在/dev目录下自动创建设备节点的分析【转】

    转自:http://blog.csdn.net/rockrockwu/article/details/7357648 系统启动过程中platform设备.驱动注册完毕,为什么在/dev目录下就自动创建 ...

  4. Linux 设备驱动之字符设备

    参考转载博客:http://blog.chinaunix.net/uid-26833883-id-4369060.html https://www.cnblogs.com/xiaojiang1025/ ...

  5. fl2440字符设备led驱动

    首先要明白字符设备驱动注册的基本流程 当我们调用insomd命令加载驱动后,驱动程序从module_init函数开始执行:硬件初始化 -> 申请主次设备号 -> 定义fops(file_o ...

  6. linux字符设备文件的打开操作

    2.7  字符设备文件的打开操作(1) 作为例子,这里假定前面对应于/dev/demodev设备节点的驱动程序在自己的代码里实现了如下的struct file_operations对象fops: st ...

  7. Linux驱动实践:你知道【字符设备驱动程序】的两种写法吗?

    作 者:道哥,10+年嵌入式开发老兵,专注于:C/C++.嵌入式.Linux. 关注下方公众号,回复[书籍],获取 Linux.嵌入式领域经典书籍:回复[PDF],获取所有原创文章( PDF 格式). ...

  8. LDD3 字符设备驱动简单分析

    最近在看LDD3,理解了一下,为了加深自己的印象,自己梳理一下.我用的CentOS release 6.6 (Final)系统. 一.编写编译内核模块的Makefile 以下是我用的Makefile ...

  9. Linux 字符设备驱动模型

    一.使用字符设备驱动程序 1. 编译/安装驱动 在Linux系统中,驱动程序通常采用内核模块的程序结构来进行编码.因此,编译/安装一个驱动程序,其实质就是编译/安装一个内核模块 2. 创建设备文件 通 ...

随机推荐

  1. encodeURIComponent() 函数

    https://baike.baidu.com/item/encodeURIComponent() 函数/7418815?fr=aladdin encodeURIComponent() 函数[1] 作 ...

  2. beetl模板引擎使用笔记

    maven项目pom: <dependency> <groupId>com.ibeetl</groupId> <artifactId>beetl< ...

  3. js 原型规则与示例

    五大规则 1. 所有的引用类型( 数组 对象 函数 ) 都是 具有对象特性即自由拓展属性 (除了 "null")意外 2. 所有的引用类型(数组 对象 函数 ) 都有一个 prot ...

  4. Intellij-工程目录下隐藏不想显示的文件和文件夹

    File-->Editor-->File Types

  5. python3 第六章 - 条件判断

    Python条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块. 条件语句的执行过程,如下图: 条件语句,又称为if语句,它的完整语法如下: if 条件1: 语句块1 ...

  6. scrapy_简介页面和详情页面

    如何对提取的URL进行限定? 往上找id和class属性值,进行多次层级选择,进行内容限定 如何实现获取主页所有urls,然后交给scrapy下载后并解析详情页面,返回结果?(文章简介页面和文章详细页 ...

  7. ImmutableMap

    不可变集合,为什么使用它呢?线程安全\更有效的利用内存\可作为常量 ImmutableMap.<String, Object> builder().put("yananList& ...

  8. 嵌套for in循环组合cat方式文件中包含空格问题

    关于循环嵌套使用for循环的空格问题 原创不易,转载请注明 需求: 现有两个功文件,需要将文件拼接 [root@localhost ~]# cat name 111 222 223 333 444 5 ...

  9. 【转】globk和glorg中使用的apr文件

    gamit和globk分析设置先验坐标和速度的要求和规则是不同的,因为在不同的阶段和环境下需要不同.从相位数据和松弛约束坐标和它们的方差得到的精 密坐标的可靠估计是假设对先验值的调整很小,也就是说在调 ...

  10. Git版本管理的简介与安装[一]

    标签(linux): git 笔者Q:972581034 交流群:605799367.有任何疑问可与笔者或加群交流 git简介 很多人都知道,Linus在1991年创建了开源的Linux,从此,Lin ...