[虚拟化/云][全栈demo] 为qemu增加一个PCI的watchdog外设(七)
目标:
1. 完成最终的设备驱动,增加具体的watchdog设备操作的代码。
测试代码:
代码最终实现见cwd_demo.c
代码只实现了read与write. 没有实现ioctl.
因此,我们可以通过shell指令直接操作我们的watchdog.
read函数,只读取watchdog的0x01 和0x02寄存器。
write函数无论写入多少个字节,驱动实际只写第一个字节。
1. 编译
$ make
2. 装载驱动
$ sudo insmod cwd_demo.ko
3.查看设备
$ sudo ls /dev/cdw_demo -l
crw------- 1 root root 10, 171 6月 30 18:38 /dev/cdw_demo
生成一个主设备号为10, 次设备号为171的设备。
4. 读取设备信息
$ sudo cat /dev/cdw_demo
B
5. 操作设备
此操作需要使用root用户
# sudo echo 'a' > /dev/cdw_demo #激活watchdog
# sudo echo 't' > /dev/cdw_demo # 喂狗
# sudo echo 'd' > /dev/cdw_demo # 停止停止设备
6. 卸载驱动 $ sudo rmmod cwd_demo
7. 查看log记录
$ tail -f -n 30 /var/log/syslog
8. 使用python操作
$ su
# python >>> f = open("/dev/cwd_demo", "w+") #打开
>>> f.write("a"); f.flush() #激活watchdog >>> f.write("t"); f.flush() # 喂狗
>>> f.write("d"); f.flush() # 停止watchdog
>>> f.readlines(); f.seek(0, 0) # 读外设的寄存器
['B\x00\n']
>>> f.close() #关闭外设
>>>
代码:
cwd_demo.c
#include <linux/init.h> //初始换函数
#include <linux/kernel.h> //内核头文件
#include <linux/module.h> //模块的头文件
#include <linux/pci.h>
#include <linux/miscdevice.h>
#include <linux/types.h>
#include <linux/fs.h>
#include <linux/mm.h>
#include <linux/watchdog.h>
#include <linux/ioport.h>
#include <linux/uaccess.h>
#include <linux/io.h> #define CWD_MODULE_NAME "cstl watchdog" /* We only use 1 card for cwd_demo */
static int cards_found;
static struct pci_dev *cwd_pci; MODULE_LICENSE("GPL");
MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR); #define CWD_VERSION "0.1"
#define PCI_VENDOR_ID_REDHAT 0x1af4
#define PCI_DEVICE_ID_CWD 0x0101 /* Memory mapped registers */
#define CWD_EXPECT_CLOSE_REG (io + 0x00) /* make no sense, read is 0x42*/
#define CWD_ACTIVATE_REG (io + 0x01)
#define CWD_TRIGER_REG (io + 0x02) /* internal variables */
static void __iomem *BASEADDR;
static resource_size_t io; /*
* Kernel Interfaces
*/ static ssize_t cwd_write(struct file *file, const char __user *data,
size_t len, loff_t *ppos)
{
/* See if we got the magic character 'V' and reload the timer */
char c;
char cwd_expect_close = inb(CWD_EXPECT_CLOSE_REG);
if (cwd_expect_close != 0x42){
printk(KERN_ERR "failed to request the magic character, %d\n", cwd_expect_close);
return -EFAULT;
}
printk(KERN_ALERT "Hello, I'm cwd_demo %d\n", cwd_expect_close);
/* only support one character one time write. ignore len */
if (get_user(c, data + ))
return -EFAULT;
printk(KERN_ALERT "Hello, cwd_demo is writing %d\n", c);
if (c == 'a') { //
printk(KERN_ALERT "cwd_demo activates watchdog\n");
outb(0x03, CWD_ACTIVATE_REG);
}
if (c == 'd') {//
printk(KERN_ALERT "cwd_demo deactivates watchdog\n");
outb(0x00, CWD_ACTIVATE_REG);
}
if (c == 't') {//
printk(KERN_ALERT "cwd_demo feeds watchdog\n");
outb(0x32, CWD_TRIGER_REG);
}
return len;
} static ssize_t cwd_read(struct file *file, char __user *buffer,
size_t count, loff_t *ppos)
{
char data[];
int retval = ;
if (*ppos >= )
goto out;
else if (*ppos + count > )
count = - *ppos;
printk(KERN_ALERT "in read, ppos is %d, count is %d\n", *ppos, count);
data[] = inb(CWD_EXPECT_CLOSE_REG);
if (data[] != 0x42){
printk(KERN_ERR "failed to request the magic character, 0x%x\n", data[]);
return -EFAULT;
}
printk(KERN_ALERT "Hello, I'm cwd_demo 0x%x\n", data[]);
data[] = inb(CWD_ACTIVATE_REG);
printk(KERN_ALERT "Hello, this is the second char 0x%x\n", data[]);
data[] = ;
if (copy_to_user(buffer, data, count)){
printk(KERN_ALERT "in read, copy to read failed\n");
retval = -EFAULT;
goto out;
}
*ppos += count;
retval = count;
out:
return retval;
} static loff_t cwd_llseek(struct file *file, loff_t offset, int whence)
{
file->f_pos = ;
return file->f_pos;
}
static int cwd_open(struct inode *inode, struct file *file)
{
// return nonseekable_open(inode, file);
return ;
} static int cwd_release(struct inode *inode, struct file *file)
{
/* Shut off the timer. */
char activate = 0x1;
outb(0x00, CWD_ACTIVATE_REG);
activate = inb(CWD_ACTIVATE_REG);
if (activate != 0x00){
printk(KERN_CRIT
"Unexpected close, not stopping watchdog!\n");
}
return ;
} static const struct file_operations cwd_fops = {
.owner = THIS_MODULE,
.llseek = cwd_llseek,
.write = cwd_write,
.read = cwd_read,
// .unlocked_ioctl = cwd_ioctl,
.open = cwd_open,
.release = cwd_release,
}; static struct miscdevice cwd_miscdev = {
// .minor = WATCHDOG_MINOR,
.minor = ,
.name = "cwd_demo",
.fops = &cwd_fops,
}; /*
* Data for PCI driver interface
*/
static DEFINE_PCI_DEVICE_TABLE(cwd_pci_tbl) = {
{ PCI_DEVICE(PCI_VENDOR_ID_REDHAT, PCI_DEVICE_ID_CWD), },
{ , }, /* End of list */
};
MODULE_DEVICE_TABLE(pci, cwd_pci_tbl); static unsigned char cwd_getdevice(struct pci_dev *pdev)
{
unsigned int addr = ;
if (pci_enable_device(pdev)) {
printk(KERN_ERR "failed to enable device\n");
goto err_devput;
} if (pci_resource_start(pdev, ) == 0x0000) {
printk(KERN_ERR "No I/O-Address for card detected\n");
goto err_disable;
} if (pci_request_region(pdev, , CWD_MODULE_NAME)) {
printk(KERN_ERR "failed to request region\n");
goto err_disable;
} // BASEADDR = pci_ioremap_bar(pdev, 0);
// BASEADDR = ioremap(pci_resource_start(pdev, 0), pci_resource_len(pdev, 0));
// if (BASEADDR == NULL) {
// /* Something's wrong here, BASEADDR has to be set */
// printk(KERN_ERR "failed to get BASEADDR\n");
// goto err_release;
// } /* here we are testing it is a io space or mem space */
// pci_write_config_dword(pdev, 0x10, 0xc090);
pci_read_config_dword(pdev, 0x10, &addr); io = pci_resource_start(pdev, );
printk(KERN_ERR "base addr 0 is 0x%x \n", inb(io));
printk(KERN_ERR "success to get BASEADDR: 0x%x\n", addr); /* Done */
cwd_pci = pdev;
return ; err_release:
pci_release_region(pdev, );
err_disable:
pci_disable_device(pdev);
err_devput:
return ;
} static int cwd_probe(struct pci_dev *pdev,
const struct pci_device_id *ent)
{
int ret;
static int major, minor;
cards_found++;
if (cards_found == )
printk(KERN_INFO "Cstl WatchDog Timer Driver v%s\n",
CWD_VERSION); if (cards_found > ) {
printk(KERN_ERR "Cstl driver only supports 1 device\n");
return -ENODEV;
} /* Check whether or not the hardware watchdog is there */
if (!cwd_getdevice(pdev) || cwd_pci == NULL)
return -ENODEV;
/* Register the watchdog so that userspace has access to it */
ret = misc_register(&cwd_miscdev);
// major = MAJOR(cwd_miscdev);
// minor = MINOR(cwd_miscdev);
// printk(KERN_ERR "register miscdev on major=%d minor=%d\n",
// MAJOR(cwd_miscdev), MINOR(cwd_miscdev));
printk(KERN_ERR "register miscdev on minor=%d\n", WATCHDOG_MINOR);
if (ret != ) {
printk(KERN_ERR
"cannot register miscdev on minor=%d (err=%d)\n",
WATCHDOG_MINOR, ret);
goto err_unmap;
}
printk(KERN_INFO
"initialized cstl watchdog (0x%x).", (unsigned int)io);
return ;
err_unmap:
iounmap(BASEADDR);
pci_release_region(cwd_pci, );
pci_disable_device(cwd_pci);
cwd_pci = NULL;
return ret; } static int cwd_timer_stop(void)
{
/* Returns 0 if the timer was disabled, non-zero otherwise */
return ;
} static void cwd_remove(struct pci_dev *pdev)
{
/* Stop the timer before we leave */
cwd_timer_stop(); /* Deregister */
misc_deregister(&cwd_miscdev);
// iounmap(BASEADDR);
pci_release_region(cwd_pci, );
pci_disable_device(cwd_pci);
cwd_pci = NULL;
} static void cwd_shutdown(struct pci_dev *pdev)
{
cwd_timer_stop();
} static struct pci_driver cwd_driver = {
.name = CWD_MODULE_NAME,
.id_table = cwd_pci_tbl,
.probe = cwd_probe,
.remove = cwd_remove,
.shutdown = cwd_shutdown,
}; static int __init cwd_demo_start(void)
{
printk(KERN_ALERT "Loading cwd_demo module...\n");
printk(KERN_ALERT "Hello, I'm cwd_demo\n");
return pci_register_driver(&cwd_driver);
} static void __exit cwd_demo_end(void)
{
pci_unregister_driver(&cwd_driver);
printk(KERN_ALERT "cwd demo Module Unloaded, Goodbye!\n"); } module_init(cwd_demo_start);
module_exit(cwd_demo_end);
Makefile
ifeq ($(KERNELRELEASE),)
KVERSION = $(shell uname -r)
all:
make -C /lib/modules/$(KVERSION)/build M=$(shell pwd) modules
echo $(shell pwd)
clean:
make -C /lib/modules/$(KVERSION)/build M=$(shell pwd) clean
else
obj-m :=cwd_demo.o
endif
[虚拟化/云][全栈demo] 为qemu增加一个PCI的watchdog外设(七)的更多相关文章
- [虚拟化/云][全栈demo] 为qemu增加一个PCI的watchdog外设(九)
目的 1. 使用verilog/vhdl设计一个PCI的watchdog设备. 2. 通过systemverilog 写testbench. 很久之前研究过AC97的verilog代码.但是很久没用v ...
- [虚拟化/云][全栈demo] 为qemu增加一个PCI的watchdog外设(四)
通过前面的操作,我们已经可以创建一个带有我们自己的PCI的watchdog外设qemu 虚拟机了. 目的: 1. 了解我们的外设情况. 2. 为在guest中开发我们自己的linux PCI驱动程序做 ...
- [虚拟化/云][全栈demo] 为qemu增加一个PCI的watchdog外设(一)
目的: 结合现在比较流行的技术,通过一个demo 展示一个全栈式设计的各种技能. 一个全栈式的工程师,应该能设计通过verilog/VHDL做logical设计.能写内核驱动,能架站. 要熟悉veri ...
- [虚拟化/云][全栈demo] 为qemu增加一个PCI的watchdog外设(六)
目的: 1. 为我们自己的watchdog写一个驱动 步骤: 通过之前的介绍,我们很容易猜想到写我们基于PCI的watchdog驱动,可以分2个步骤. 1. 探测加载PCI设备 这部分代码跟我们的设备 ...
- [虚拟化/云][全栈demo] 为qemu增加一个PCI的watchdog外设(二)
这篇文章的理解,需要一些专业知识了. 我们可以创建模拟自己的外设吗? 我们已经知道什么是qemu了,我们可以通过qmeu的提供的外设,DIY一个计算机了. 但是我们可能还不满足,我们可以自己制造一个外 ...
- [虚拟化/云][全栈demo] 为qemu增加一个PCI的watchdog外设(八)
目的: 1. 通过网页读取watchdog的信息 2. 通过网页设置watchdog 准备工作: 1. 选择一个web框架,选用 cherrypy $ sudo apt-get install pyt ...
- [虚拟化/云][全栈demo] 为qemu增加一个PCI的watchdog外设(五)
目的: 1. 了解PCI的基本知识,为完成watchdog的设备做准备. 准备知识: 简单的说,PCI 设备分3个空间. 配置空间,IO空间,内存地址空间. PCI设备厂家决定了外设是使用IO空间还是 ...
- [虚拟化/云][全栈demo] 为qemu增加一个PCI的watchdog外设(三)
我们已经设计了一个基于qemu的watchdog了.下一步工作就是创建一个含有我们的watchdog的虚拟计算机器了. 准备工作: 1. 使用virt-manager或者virsh创建一个虚拟机器. ...
- 从零开始的全栈工程师——利用CSS3画一个正方体 ( css3 )
transform属性 CSS3的变形(transform)属性让元素在一个坐标系统中变形.transform属性的基本语法如下: transform:none | <transform-fun ...
随机推荐
- Javascript 多物体运动——逐行分析代码,让你轻松了解运动的原理
我们先来看下之前的运动的代码,是否支持多物体运动,会出现怎么样的问题. <style type="text/css"> div { width: 100px; heig ...
- bootstrap简章
系统整理一遍bootstrap 的东西 1/ 设置页面为H5文档类型 <!DOCTYPE html> <html lang="zh-CN"> ... &l ...
- jquery ajax提交及请求
jQuery.ajax({ url: dataURL, success: function(results) { var parsedJson = jQuery.parseJSON(results); ...
- html5介绍 之亮点特性
html5 兴起- 乔帮助在2010年发布的:关于对flash的思考,提到有了h5放弃 flash 1 富图形,富媒体 2 本地存储 cookie 3 LBS 基于 ...
- C++ dynamic_cast实现原理
dynamic_cast是一个操作符,其用法不再赘述.查看汇编码可以发现实际调用的是这个函数__RTDynamicCast,其内部实现如下: rtti.h: #pragma once extern & ...
- rsyslog 读日志文件 ,当rsyslog 中断时,也会丢数据
rsyslog 日志服务器: [root@dr-mysql01 winfae_log]# grep scan0819 wj-proxy01-catalina.out.2016-08-19 [root@ ...
- [11-3] Gradient Boosting regression
main idea:用adaboost类似的方法,选出g,然后选出步长 Gredient Boosting for regression: h控制方向,eta控制步长,需要对h的大小进行限制 对(x, ...
- Aix 光盘软件包安装
1, mount -rv cdrfs /dev/cd0 /mnt (Aix 光盘挂载)2,fuser -kxcu /cdrom && unmount /mnt && u ...
- php解决与处理网站高并发 大流量访问的方法
方法/步骤 首先,确认服务器硬件是否足够支持当前的流量 普通的P4服务器一般最多能支持每天10万独立IP,如果访问量比这个还要大, 那么必须首先配置一台更高性能的专用服务器才能解决问题 ,否则怎么 ...
- C#获取单个字符的拼音声母
public class ConvertToPinYing { /// <summary> /// 汉字转拼音缩写 /// < ...