使用内核定时器的second字符设备驱动及测试代码
驱动:
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/types.h>
#include <linux/errno.h> #include <linux/cdev.h>//cdev
#include <linux/device.h>//udev
#include <linux/sched.h>//schedule
#include <linux/uaccess.h>//copy_to_user
#include <linux/slab.h>//kmalloc() #define DEVICE_NAME "second_drv"
static int second_major=;/*主设备号*/
struct class *second_class; /*second设备结构体*/
static struct second_dev
{
struct cdev cdev;/*cdev 结构体*/
atomic_t counter;/*一共经历了多少时间;定义原子变量*/
struct timer_list s_timer;/*定义一个定时器*/
};
struct second_dev *second_devp;/*定义设备结构体指针*/ /*定时器处理函数*/
static void second_timer_handler(unsigned long arg)
{
mod_timer(&second_devp->s_timer,jiffies+HZ);/*再次调度定时器*/
atomic_inc(&second_devp->counter);/*counter 加1*/
printk(KERN_NOTICE"current jiffies is %d!!\n",jiffies);
} static int second_open(struct inode *inode, struct file *filp)
{
//printk("in the second_open!!\n");
filp->private_data=second_devp;/*将设备结构体指针赋值给文件私有数据指针*/ /*初始化定时器*/
init_timer(&second_devp->s_timer);
second_devp->s_timer.function=&second_timer_handler;
second_devp->s_timer.expires=jiffies+HZ;
add_timer(&second_devp->s_timer);/*添加定时器*/ atomic_set(&second_devp->counter,);/*计数清零*/ return ;
} static int second_release(struct inode *inode,struct file *filp)
{
del_timer(&second_devp->s_timer);/*删除定时器*/
return ;
} static ssize_t second_read(struct file *filp,char __user *buf,ssize_t count,loff_t *ppos)
{
//printk("in the second_read!!\n");
int counter;
counter=atomic_read(&second_devp->counter);/*获取count值*/ if(copy_to_user(buf,&counter,count))
return -EFAULT;
else
return sizeof(unsigned int);
} static ssize_t second_write(struct file *filp,const char __user *buf,ssize_t count,loff_t *ppos)
{
return ;
} /*文件操作结构体*/
static const struct file_operations second_fops={
.owner = THIS_MODULE,
.open =second_open,
.release =second_release,
.read =second_read,
.write =second_write,
}; /*初始化并注册cdev*/
static void second_setup_cdev(struct second_dev *dev,int index)
{
int err;
dev_t devno=MKDEV(second_major,index);
cdev_init(&dev->cdev,&second_fops);/*初始化cdev成员*/
dev->cdev.owner=THIS_MODULE;
err=cdev_add(&dev->cdev,devno,);/*向系统注册字符设备*/
if(err)
printk(KERN_NOTICE"error=%d",err);
} /*设备驱动模块加载函数*/
static int __init second_init(void)
{
/*申请设备号*/
int ret;
dev_t devno=MKDEV(second_major,);/*获得设备号*/
if(second_major)
ret=register_chrdev_region(devno,,DEVICE_NAME);
else
{
/*动态申请设备号*/
ret=alloc_chrdev_region(&devno,,,DEVICE_NAME);
second_major=MAJOR(devno);
}
if(ret)
{
printk("request chrdev failed!!\n");
return ret;
} /*动态申请设备结构体内存*/
second_devp=kmalloc(sizeof(struct second_dev),GFP_KERNEL);
if(!second_devp)
{
ret= -ENOMEM;
goto fail_kmalloc;
}
memset(second_devp,,sizeof(struct second_dev));/*申请到的内存空间清零*/ /*注册字符设备*/
second_setup_cdev(second_devp,); /*用udev机制自动创建设备文件结点*/
second_class=class_create(THIS_MODULE,"second_class");/*在sys/class下添加second_class这个类*/
if (IS_ERR(second_class))
{
printk(KERN_ERR "class_create() failed for second_class\n");
goto fail_class_create;
}
device_create(second_class,NULL,devno,NULL,DEVICE_NAME);/*创建设备/dev/$DEVICE_NAME*/ printk("init second_drv success!!,major=%d!!\n",second_major);
return ;
fail_class_create:
cdev_del(&second_devp->cdev);/*注销cdev*/
kfree(second_devp);/*释放设备结构体内存*/
fail_kmalloc:
unregister_chrdev_region(devno,);/*释放设备号*/ return ret;
} /*设备驱动模块卸载函数*/
static void __exit second_exit(void)
{
device_destroy(second_class,MKDEV(second_major,));/*注销设备*/
class_destroy(second_class);/*注销类*/
cdev_del(&second_devp->cdev);/*注销cdev*/
kfree(second_devp);/*释放设备结构体内存*/
unregister_chrdev_region(MKDEV(second_major,),);/*释放设备号*/
} MODULE_AUTHOR("mhb@SEU");
MODULE_LICENSE("GPL"); module_init(second_init);
module_exit(second_exit);
测试代码:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <fcntl.h> main(int argc,char *argv[])
{
int fd;
int counter=;
int counter_old=; if ((fd=open("/dev/second_drv",O_RDONLY )) < )
{
printf("Open Device failed.\r\n");
exit();
}
else
while()
{
//printf("in the while(1)!\n");
read(fd,&counter,sizeof(unsigned int));
if(counter!=counter_old)
{
printf("seconds after open /dev/second_drv :%d\n",counter);
counter_old=counter;
}
} }
使用内核定时器的second字符设备驱动及测试代码的更多相关文章
- 【驱动】linux设备驱动·字符设备驱动开发
Preface 前面对linux设备驱动的相应知识点进行了总结,现在进入实践阶段! <linux设备驱动入门篇>:http://infohacker.blog.51cto.com/6751 ...
- Linux内核分析(五)----字符设备驱动实现
原文:Linux内核分析(五)----字符设备驱动实现 Linux内核分析(五) 昨天我们对linux内核的子系统进行简单的认识,今天我们正式进入驱动的开发,我们今后的学习为了避免大家没有硬件的缺陷, ...
- 3.字符设备驱动------Poll机制
1.poll情景描述 以之前的按键驱动为例进行说明,用阻塞的方式打开按键驱动文件/dev/buttons,应用程序使用read()函数来读取按键的键值. ) { read(fd, &key_v ...
- 鸿蒙内核源码分析(字符设备篇) | 字节为单位读写的设备 | 百篇博客分析OpenHarmony源码 | v67.01
百篇博客系列篇.本篇为: v67.xx 鸿蒙内核源码分析(字符设备篇) | 字节为单位读写的设备 | 51.c.h.o 文件系统相关篇为: v62.xx 鸿蒙内核源码分析(文件概念篇) | 为什么说一 ...
- Linux字符设备驱动框架
字符设备是Linux三大设备之一(另外两种是块设备,网络设备),字符设备就是字节流形式通讯的I/O设备,绝大部分设备都是字符设备,常见的字符设备包括鼠标.键盘.显示器.串口等等,当我们执行ls -l ...
- LDD3 字符设备驱动简单分析
最近在看LDD3,理解了一下,为了加深自己的印象,自己梳理一下.我用的CentOS release 6.6 (Final)系统. 一.编写编译内核模块的Makefile 以下是我用的Makefile ...
- 嵌入式Linux驱动学习之路(十)字符设备驱动-my_led
首先贴上代码: 字符设备驱动代码: /** *file name: led.c */#include <linux/sched.h> #include <linux/signal.h ...
- Linux应用程序访问字符设备驱动详细过程【转】
本文转载自:http://blog.csdn.net/coding__madman/article/details/51346532 下面先通过一个编写好的内核驱动模块来体验以下字符设备驱动 可以暂时 ...
- register_chrdev_region/alloc_chrdev_region和cdev注册字符设备驱动
内核提供了三个函数来注册一组字符设备编号,这三个函数分别是 register_chrdev_region().alloc_chrdev_region() 和 register_chrdev(). (1 ...
随机推荐
- java基础复习之对于String对象,能够使用“=”赋值,也能够使用newkeyword赋值,两种方式有什么差别?
String类型是实际工作中经经常使用到的类型,从数据类型上划分,String是一个引用类型,是API中定义的一个类.所以String类型的对象能够用new创建,比如String name=new S ...
- C++ stringstream介绍,使用方法与例子
From: http://www.usidcbbs.com/read-htm-tid-1898.html C++引入了ostringstream.istringstream.stringstream这 ...
- C#_LINQ(LINQ to Entities)
LINQ to Entities 是 LINQ 中最吸引人的部分.它让你可以使用标准的 C# 对象与数据库的结构和数据打交道.使用 LINQ to Entities 时,LINQ 查询在后台转换为 S ...
- C#_IComparable实例 - 对象ID进行排序
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Comp ...
- ASP.NET MVC 4 Ajax上传文件
这两天一直纠结着表单的问题.想在一个表单里实现三个功能: 输入查询条件,点击查询: 导出查询数据: 上传文件: 方法有很多,乱花渐欲迷人眼,尝试了很多,无果.大致说的是,给不同按钮写js代码,在js代 ...
- eclipse安装插件的方法,以python为例子
一 转载自:http://www.cnblogs.com/linzhenjie/articles/2639113.html 1.基本需求 1.Eclipse 集成开发环境下载 http://115.c ...
- servlet 容器,工作原理,优缺点
转自http://blog.sina.com.cn/s/blog_b5a157500101ld71.html servlet:是以java技术为基础,应用于服务器端的程序组件,本质就是java代码,用 ...
- PS基础
1.仿制图章工具:[小 ]大 建立一个新的图层,可以进行图片某个部分的复制,完全复制之后,还可以调整大小(ctrl+t), 颜色(ctrl+u打开色相饱和度的菜单)等. 2.修复画笔工具:与仿制 ...
- 深入理解计算机系统第二版习题解答CSAPP 2.19
在2.17的基础上完成下表: x 十六进制 T2U(x) -8 0x8 -3 0xD -2 0xE -1 0xF 0 0x0 5 0x5
- Matlab中plot、fplot、ezplot的使用方法和区别
函数plot 是绘制二维图形的最基本函数,它是针对向量或矩阵的列来绘制曲线的.也就是说,使用plot 函数之前,必须首先定义好曲线上每一点的x 及y 坐标; 常用格式为: (1)plot(x) 当x ...