模块描述

将30个字节的内存空间模仿成设备文件。每次读写不超过30个字节。

模块加载成功之后,需建立设备文件

mknod /dev/mydev c 231 0

模块代码

#include <linux/types.h>     //dev_t,MAJOR,MINOR,MKDEV
#include <linux/fs.h> //file_operations,struct file,struct indoe,register
//unregister_chrdev,register/alloc/unregister_chrdev_regino,
#include <linux/cdev.h> //cdev_alloc,cdev_init,cdev_add,cdev_del
#include <linux/kernel.h> //container_of
#include <linux/slab.h> //kfree,kmalloc
#include <linux/errno.h> //error code
#include <linux/module.h> //MODULE_LICENSE,...
#include <linux/platform_device.h> //struct platform_device,struct platform_driver
#include <asm/uaccess.h> //copy_from_user,copy_to_user #define MYMAJOR 231 //静态分配设备号 MODULE_LICENSE("GPL");
MODULE_AUTHOR("WGY");
MODULE_DESCRIPTION("MY_FIRST_MODULE"); unsigned int count=0; //记录打开设备的次数
dev_t devn; //设备号 struct my_cdev
{
char kerbuf[30]; //分配30个字节的内存空间
struct cdev cd; //如果这里实例化cd为指针,记得使用前为cd分配空间
}; struct my_cdev ex_cdev; //设备结构 int simple_open(struct inode * inode,struct file * filp)
{
if(count>=1) //设备已被打开
{
printk(KERN_ERR "Device is in use.");
return -EBUSY;
}
count++; //设备初次打开,加一
return 0;
} int simple_release(struct inode * inode,struct file * filp)
{
count--; //关闭设备,计数减一
return 0;
} ssize_t simple_read(struct file * filp,char __user *usrbuf,size_t count,loff_t * f_pos)
{
if(count>30) //用户要求读出字符总数超过30个字节
{
printk(KERN_WARNING "Over the max length of the kernel array");
return -1;
} //拷贝内核空间的数据到用户空间
if(copy_to_user(usrbuf,ex_cdev.kerbuf,count))
{
return -EFAULT;
} return count;
} ssize_t simple_write(struct file * filp,const char __user *usrbuf,size_t count,loff_t * f_pos)
{
if(count>30) //用户写入字符总数超过30个字节
{
printk(KERN_WARNING "over the max length of the kernel array");
return -1;
} //拷贝用户空间的数据到内核空间
if(copy_from_user(ex_cdev.kerbuf,usrbuf,count))
{
return -EFAULT;
} return 0;
} struct file_operations simple_ops=
{
.owner=THIS_MODULE,
.read=simple_read,
.write=simple_write,
.open=simple_open,
.release=simple_release,
}; int __init simple_init_module(void)
{
int ret,result; devn=MKDEV(MYMAJOR,0); //获取设备号 ret=register_chrdev_region(devn,1,"/dev/mydev"); //起始设备号,设备个数,设备名
if(ret<0)
{
printk(KERN_WARNING "failed to alloc a dev number");
return ret;
} //直接涉及内核的操作
cdev_init(&ex_cdev.cd,&simple_ops);
ex_cdev.cd.owner=THIS_MODULE;
result = cdev_add (&ex_cdev.cd, devn, 1);
if(result)
{
printk(KERN_NOTICE "Error %d adding DEMO\n", result);
}
printk("success ,good!"); return 0; } void __exit simple_cleanup_module(void)
{
cdev_del(&ex_cdev.cd);
unregister_chrdev_region(devn,1);
printk("simple_cleanup_module");
} module_init(simple_init_module);
module_exit(simple_cleanup_module);

应用

#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h> int main()
{
int fd,ret;
char buf[30]={'\0'}; fd=open("/dev/mydev",O_RDWR|O_NONBLOCK|O_NOCTTY);
if(fd<0)
{
printf("fail to open..");
exit(1);
} ret=write(fd,"hello",5); //向设备写入5个字符
if(ret<0)
{
printf("fail to write.. ");
exit(1);
} ret=read(fd,buf,5); //从设备读入5个字符
if(ret<0)
{
printf("fail to read..");
exit(1);
} printf("string=%s\n",buf); //打印从设备读到的字符
close(fd); //关闭打开的文件
return 0; }

MY_FIRSH_MODULE的更多相关文章

随机推荐

  1. xmanager远程桌面连接Linux

    远程桌面连接Linux,两种工具:vnc.xmanager xmanager使用参考如下,暂未整理: Xmanager连接Centos的远程桌面 | 大爱 http://lovesoo.org/xma ...

  2. php操作大文件

    看了http://hi.baidu.com/qiaoyuetian/item/76c51f0ce25030e4f45ba69e(php读取大文件详解),然后测试了里边的代码,发现一些错误, 总结,红色 ...

  3. AndroidManiFast 字段意义

    每个Activity都要在本文件中注册. <Activity>下的<Intent-filter>中. 两个字段的意思是: <action android:name=&qu ...

  4. Sql Server作业

    参考资料: http://jingyan.baidu.com/article/49ad8bce7287315834d8fab4.html

  5. 【转】常用的shell脚本

    from:http://blog.sina.com.cn/s/blog_4152a9f501013r6c.html 常用的shell脚本 (2012-10-10 22:09:07) 转载▼ 标签: 杂 ...

  6. .NET之委托

    有些.NET中的高级特性,比如:委托! 有一种怎么也搞不懂的赶脚... 博客读了好几篇,代码也动手写了,书中的一些介绍也看了, 各种搜索关于委托的,至今还处于"会用"的阶段. 该怎 ...

  7. SQL如何将A,B,C替换为'A','B','C'

    因为涉及到逗号,和单引号' 本来想一次转换成功, 但是最后貌似没有好的办法, 只有分两次完成了 select REPLACE(REPLACE('A,B,C',',','>,>'),'> ...

  8. javascript 2048游戏

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. C++中的单例模式(转)

    单例模式也称为单件模式.单子模式,可能是使用最广泛的设计模式.其意图是保证一个类仅有一个实例,并提供一个访问它的全局访问点,该实例被所有程序模块共享.有很多地方需要这样的功能模块,如系统的日志输出,G ...

  10. Redis学习笔记四:独立功能之发布与订阅

    客户端可以通过执行 subscribe 命令订阅一个或多个频道,每当有其他客户端向被订阅的频道发送消息时,频道所有的订阅者都会收到这条消息. 客户端还可以通过执行 psubscribe 命令订阅一个或 ...