自动创建字符设备,不需mknod
自动创建设备文件
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的更多相关文章
- linux之misc及使用misc创建字符设备
1:linux字符设备及udev 1.1字符设备 字符设备就是:一个一个字节来进行访问的,不能对字符设备进行随机读写.简单字符设备创建实例如下: #include <linux/module.h ...
- LCD驱动分析(一)字符设备驱动框架分析
参考:S3C2440 LCD驱动(FrameBuffer)实例开发<一> S3C2440 LCD驱动(FrameBuffer)实例开发<二> LCD驱动也是字符设备驱动,也 ...
- platform型设备在/dev目录下自动创建设备节点的分析【转】
转自:http://blog.csdn.net/rockrockwu/article/details/7357648 系统启动过程中platform设备.驱动注册完毕,为什么在/dev目录下就自动创建 ...
- Linux 设备驱动之字符设备
参考转载博客:http://blog.chinaunix.net/uid-26833883-id-4369060.html https://www.cnblogs.com/xiaojiang1025/ ...
- fl2440字符设备led驱动
首先要明白字符设备驱动注册的基本流程 当我们调用insomd命令加载驱动后,驱动程序从module_init函数开始执行:硬件初始化 -> 申请主次设备号 -> 定义fops(file_o ...
- linux字符设备文件的打开操作
2.7 字符设备文件的打开操作(1) 作为例子,这里假定前面对应于/dev/demodev设备节点的驱动程序在自己的代码里实现了如下的struct file_operations对象fops: st ...
- Linux驱动实践:你知道【字符设备驱动程序】的两种写法吗?
作 者:道哥,10+年嵌入式开发老兵,专注于:C/C++.嵌入式.Linux. 关注下方公众号,回复[书籍],获取 Linux.嵌入式领域经典书籍:回复[PDF],获取所有原创文章( PDF 格式). ...
- LDD3 字符设备驱动简单分析
最近在看LDD3,理解了一下,为了加深自己的印象,自己梳理一下.我用的CentOS release 6.6 (Final)系统. 一.编写编译内核模块的Makefile 以下是我用的Makefile ...
- Linux 字符设备驱动模型
一.使用字符设备驱动程序 1. 编译/安装驱动 在Linux系统中,驱动程序通常采用内核模块的程序结构来进行编码.因此,编译/安装一个驱动程序,其实质就是编译/安装一个内核模块 2. 创建设备文件 通 ...
随机推荐
- day2 作业
1.判断下列逻辑语句的True,False. 1),1 > 1 or 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6 ...
- 网站搭建中,怎么区分ASP和PHP
1:空间支持上 ASP:程序要求比较低,空间只要支持ASP+access即可运行 PHP:配置要求比较高,空间需要支持PHP及数据库,而且程序和数据库是单独的,一般的 unix空间都是这种配置. 2: ...
- linux常用命令(CentOS)
1.目录切换命令 linux目录结构 ps:绿色标注为常用命令 cd xx 切换到该目录下的xx目录 cd ../ 切换到上一层目录 cd / 切换到系统根目录 cd ~ 切换到用户主目录 cd - ...
- Java数据持久层框架 MyBatis之背景知识一
对于MyBatis的学习而言,最好去MyBatis的官方文档:http://www.mybatis.org/mybatis-3/zh/index.html 对于语言的学习而言,马上上手去编程,多多练习 ...
- Mybatis (一)
1 DAO层框架 框架:是一种整体的解决方案. 1.1 JDBC的步骤 1.2 Hibernate执行的步骤 1.3 MyBaits 2 Mybatis简介 Mybatis是支持定制化SQL.存储过程 ...
- js内置函数大全及基本使用方法(一)
一,常规函数 alert函数:显示一个警告对话框,包括一个OK按钮. 语法:alert("hello world"); confirm函数:显示一个确认对话框,包括OK.Cance ...
- vue调试神器vue-devtools安装
vue-devtools安装 vue-devtools是一款用来调试Vue应用的Chrome插件,可极大提高开发者调试项目效率,接着我们说一下如何下载安装这个插件; 一. 从chrome商店直接下载安 ...
- Lambda表达式详解 (转)
前言 1.天真热,程序员活着不易,星期天,也要顶着火辣辣的太阳,总结这些东西. 2.夸夸lambda吧:简化了匿名委托的使用,让你让代码更加简洁,优雅.据说它是微软自C#1.0后新增的最重要的功能之一 ...
- bzoj 2208 [Jsoi2010]连通数
2208: [Jsoi2010]连通数 Time Limit: 20 Sec Memory Limit: 512 MB Description Input 输入数据第一行是图顶点的数量,一个正整数N ...
- debug时ClassNotFound可能出现的原因
自我总结,欢迎拍砖! 目的:总结ClassNotFound可能出现的原因,方便以后定位该类问题. 原因:当项目中加了新类,debug或run时,报classnotfound,说明没有找到该类的clas ...