编写字符驱动程序 - 读取cmos中的时间
来源:互联网 发布:win10网络授权 编辑:程序博客网 时间:2024/06/05 02:47
要求:该驱动程序主设备文件名为:/dev/cmos , 主设备号位 211 ,次设备号为0
cmos.c
#include <linux/init.h>#include <linux/kernel.h>#include <linux/fs.h>#include <linux/cdev.h>#include <linux/types.h>#include <linux/slab.h> //kmalloc()#include <asm/uaccess.h> //copy_from_user#include <linux/pci.h>#include <linux/module.h>#include <asm/io.h> MODULE_LICENSE("GPL") ;#define device_name "cmos"#define cmos_major 211#define cmos_minor 0#define CMOS_READ(addr) ({outb(0x80|addr,0x70);inb(0x71);}) #define BCD_TO_BIN(val) ((val)=((val)&15)+((val)>>4)*10) static int cmos_open(struct inode *inode, struct file *file){ return 0;}static int cmos_release(struct inode *inode, struct file *file){ return 0;}static ssize_t cmos_read(struct file *file, char *buf,size_t count, loff_t *ppos){ char d[6]; //yy/mm/dd/hh/min/sec int i; printk("cmos driver:read\n"); d[0] = CMOS_READ(9); d[1] = CMOS_READ(8); d[2] = CMOS_READ(7); d[3] = CMOS_READ(4); d[4] = CMOS_READ(2); d[5] = CMOS_READ(0); for(i=0;i<6;i++) BCD_TO_BIN(d[i]); if(count > 6) count = 6; copy_to_user(buf,d,count); return count;}static struct file_operations cmos_fops = { owner:THIS_MODULE, /* Owner *///<linux/fs.h> open:cmos_open, /* Open method */ read:cmos_read, release:cmos_release, /* Release method */}; static int __init cmos_init(void){ printk("CMOS Driver Initialized.\n"); register_chrdev(cmos_major , device_name,&cmos_fops) ; return 0;}static void __exit cmos_exit(void){ printk("exit\n"); unregister_chrdev(cmos_major , device_name) ;}module_init(cmos_init) ;module_exit(cmos_exit) ;cmostest.c
#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <fcntl.h>char buf[6];int main(int argc,char **argv){int fd;int ret;if((fd=open("/dev/cmos",O_RDWR))<0){printf("cmos driver open fail\n");return -1;}ret=read(fd,buf,6);if(ret == -1){printf("read fail\n");return -1;}printf("CMOS tell me the time is:20%02d/%02d/%02d %02d:%02d:%02d\n",buf[0],buf[1],buf[2],buf[3],buf[4],buf[5]);close(fd);return 0;}Makefile:
ifneq ($(KERNELRELEASE),)obj-m := cmos.oelseKERNELDIR ?= /lib/modules/$(shell uname -r)/buildPWD := $(shell pwd)default:$(MAKE) -C $(KERNELDIR) M=$(PWD) modulesclean:rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.crm -rf Module.* modules.*endif
加载模块 并查看调试信息
查看创建的设备驱动
运行测试程序
卸载驱动模块,检查模块驱动设备是否没有了
0 0
- 编写字符驱动程序 - 读取cmos中的时间
- 从CMOS中读取时间
- 读取CMOS时间并显示
- 读取CMOS内存中的时钟
- 直接从CMOS读取系统时间
- 直接从CMOS读取系统时间
- 字符设备驱动程序编写基础
- 字符型驱动程序的编写
- 字符设备驱动程序的编写
- 王爽汇编 实验14读取CMOS RAM时间信息
- 16位汇编读取CMOS时间并显示
- 访问PC IO CMOS RAM(读取bios时间)
- Linux字符设备驱动程序的编写框架
- Linux字符设备驱动程序的编写框架
- Linux字符设备驱动程序的编写框架
- Linux字符设备驱动程序的编写框架
- Linux字符设备驱动程序的编写框架
- Linux字符设备驱动程序编写基本流程
- PHP解决抢购、秒杀、抢楼、抽奖等阻塞式高并发库存防控超量的思路方法
- 使用html添加链接
- 安卓应用App发布至各大平台汇总
- 最大子数组问题
- CString转char *
- 编写字符驱动程序 - 读取cmos中的时间
- WordPress 函数:add_action()(添加动作)
- CDOJ 1222--Sudoku【DFS && 数独】
- 自定义BadgeButton提醒数字
- 关于去掉Li标签前面的小圆点和距离/显示下划线
- 黑马程序员——网络概念及Socket编程示例
- 安卓 线程 和 线程池
- LeetCode -- Length of last word
- Linux-grep学习笔记