模块描述

将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. Webpack 之 Loader 的使用

    安装 loaders 如果loader在npm里,可以这样安装: $ npm install xxx-loader --save 或者 $ npm install xxx-loader --save- ...

  2. thinkphp连接数据库

    版本:3.1.1 连接数据库的具体位置 thinkphp/Config/convention.php,默认修改数据库在这里就可以了 但是为了方便,把数据库配置写到Index/Conf/config.p ...

  3. JQuery.getJSON 从aspx页面返回JSON数据

    public partial class WebForm2 : System.Web.UI.Page { protected void Page_Load(object sender, EventAr ...

  4. verilog阻塞与非阻塞的初步理解(二)

    将阻塞模块改为下述代码: module blocking(clk,a,b,c); :] a; input clk; :] b,c; :] b,c; always @(posedge clk) begi ...

  5. 由Collections.unmodifiableList引发的重构

    原文  http://www.cnblogs.com/persist-confident/p/4516741.html 今天阅读源码的时候,无意中看到了Collections.unmodifiable ...

  6. Linux 下 netbeans 字体抗锯齿正解

    转自:http://leenjewel.blog.163.com/blog/static/601937922010124444051/ 说来这个不难,主要是我看网上有的写的不是很明确,甚至有的写的根本 ...

  7. yaf扩展

  8. redis php扩展安装下载

    php的redis 扩展下载地址 Windows :http://windows.php.net/downloads/pecl/releases/redis/2.2.7/ 下载对应版本,一般有两个 n ...

  9. 关于轻松安装LNMP和LAMP的编译环境

    http://lnmp.org/install.html 系统需求: CentOS/RHEL/Fedora/Debian/Ubuntu/Raspbian Linux系统 需要2GB以上硬盘剩余空间 1 ...

  10. jQuery.imgLazyLoad图片懒加载组件

    一.前言 当一个页面中请求的图片过多,而且图片太大,页面访问的速度是非常慢的,对用户的体验非常不友好:使用图片懒加载,可以减轻服务器的压力,增加页面的访问量,这里主要是总结一下我自己写的图片懒加载组件 ...