一个简单的字符设备驱动程序和测试程序
来源:互联网 发布:印度红色走廊知乎 编辑:程序博客网 时间:2024/05/31 19:28
------------------------字符设备驱动程序------------------------------
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/ioctl.h>
#include <linux/mm.h>
#include <asm/uaccess.h>
#include <linux/blkdev.h>
MODULE_DESCRIPTION(
"a test of char device driver"
);
MODULE_AUTHOR(
"Jerry"
);
MODULE_LICENSE(
"GPL"
);
#define CDRIVER_NAME "simple_chrdev"
int
CDRIVER_MAJOR=0;
int
CDRIVER_MINOR=0;
int
count=1;
static
struct
class
*simple_class;
loff_t simple_llseek(
struct
file *filp,loff_t off,
int
whence);
int
simple_ioctl(
struct
inode *inode,
struct
file *filp,unsigned
int
cmd,unsigned
long
arg);
int
simple_open(
struct
inode *inode,
struct
file *filp);
int
simple_release(
struct
inode *inode,
struct
file *filp);
static
ssize_t simple_read(
struct
file *filp,
char
*buffer,
size_t
count, loff_t *ppos)
{
int
ret;
int
data = 0xdc;
ret=copy_to_user(buffer, (
char
*)&data,
sizeof
(data));
if
(ret<0)
{
printk(
"ret =%d \n"
,ret);
return
ret;
}
return
sizeof
(data);
}
extern
struct
file_operations simple_fops;
struct
cdev *simple_cdev;
dev_t simple_dev;
static
struct
device *dev;
struct
file_operations simple_fops=
{
.owner=THIS_MODULE,
.llseek=simple_llseek,
.open=simple_open,
.release=simple_release,
.ioctl=simple_ioctl,
.read=simple_read,
};
/*open*/
int
simple_open(
struct
inode *inode,
struct
file *filp)
{
//do sth you want
return
0;
}
/*llseek*/
loff_t simple_llseek(
struct
file *filp,loff_t off,
int
whence)
{
//do sth you want
return
0;
}
/*ioctl*/
int
simple_ioctl(
struct
inode *inode,
struct
file *filp,unsigned
int
cmd,unsigned
long
arg)
{
//do sth you want
return
0;
}
int
simple_release(
struct
inode *inode,
struct
file *filp)
{
printk(
"simple device is released\n"
);
//module_put(THIS_MODULE);
return
0;
}
/* 本代码自动生成了/dev/simple */
static
int
skeleton_init_module(
void
)
{
int
ret;
printk( KERN_DEBUG
"Module skeleton init\n"
);
/*register major and minor*/
if
(CDRIVER_MAJOR!=0)
{
simple_dev=MKDEV(CDRIVER_MAJOR,CDRIVER_MINOR);
ret=register_chrdev_region(simple_dev,count,CDRIVER_NAME);
}
else
{
/*dynamic assign major*/
ret=alloc_chrdev_region(&simple_dev,CDRIVER_MINOR,count,CDRIVER_NAME);
CDRIVER_MAJOR=MAJOR(simple_dev);
}
if
(ret<0)
{
printk(KERN_ERR
"cannot get major %d \n"
,CDRIVER_MAJOR);
return
-1;
}
/*register character device driver*/
simple_cdev=cdev_alloc();
if
(simple_cdev!=NULL)
{
cdev_init(simple_cdev,&simple_fops);
simple_cdev->ops=&simple_fops;
simple_cdev->owner=THIS_MODULE;
if
(cdev_add(simple_cdev,simple_dev,count)!=0)
{
printk(KERN_NOTICE
"something wrong when adding simple_cdev!\n"
);
}
else
{
printk(
"success adding simple_cdev!\n"
);
}
}
else
{
printk(KERN_ERR
"register simple_dev error!\n"
);
return
-1;
}
simple_class = class_create(THIS_MODULE,
"simple"
);
if
(IS_ERR(simple_class))
{
printk( KERN_DEBUG
"class_create error\n"
);
return
-1;
}
printk(KERN_ERR
"devno is %d \n"
,simple_dev);
dev=device_create(simple_class, NULL, simple_dev,
"simple"
);
//2.6.26以上为
dev=device_create(simple_class, NULL, simple_dev,NULL,
"simple"
);
return
0;
}
static
void
skeleton_exit_module(
void
)
{
printk( KERN_DEBUG
"Module skeleton exit\n"
);
device_destroy(simple_class, simple_dev);
class_destroy(simple_class);
cdev_del(simple_cdev);
unregister_chrdev_region(simple_dev,count);
}
module_init(skeleton_init_module);
module_exit(skeleton_exit_module);
---------------测试程序----------------
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
int
main()
{
unsigned
char
buffer[100];
int
i;
int
fd;
char
key=0;
fd = open(
"/dev/simple"
, O_RDWR);
if
(fd == -1)
{
perror
(
"open device button errr!"
);
return
0;
}
read(fd,buffer,4);
for
(i=0;i <4;i++)
printf
(
"%d\n"
,buffer[i]);
pause();
close(fd);
return
0;
}
0 0
- 一个简单的字符设备驱动程序和测试程序
- 写一个简单的字符设备驱动程序
- 简单字符设备驱动程序 以及 相应测试程序
- 简单的字符设备驱动程序
- Linux字符设备驱动程序的一个简单示例
- Linux字符设备驱动程序的一个简单示例
- Linux字符设备驱动程序的一个简单示例
- Linux字符设备驱动程序的一个简单示例
- (5)一个简单字符设备驱动程序
- 一个简单的字符驱动程序
- 一个简单的字符驱动程序
- 简单字符设备驱动程序
- 简单字符设备驱动程序
- 简单字符设备驱动程序
- 简单字符设备驱动程序
- 一个最简单的测试在dev/下生成字符设备的程序
- linux简单的字符设备驱动程序
- 最简单的字符设备驱动程序
- request method
- 生产消费者模式
- JS中的Map简单实现
- make iframe responsive and fullscreen iframe(支持iframe自适应,并全屏iframe)
- JS&DOM相关介绍
- 一个简单的字符设备驱动程序和测试程序
- swift UIScrollView简单介绍
- 兄弟连专注IT教育 九周年再创辉煌
- http://my.oschina.net/u/1245614/blog/481573?p={{page}}
- [LeetCode16]3Sum Closed
- Pull解析
- 【Linux】使用__wrap_malloc查看内存使用
- 乐观锁和悲观锁
- 如何使div居中-简单实现