[虚拟化/云][全栈demo] 为qemu增加一个PCI的watchdog外设(七)

时间:2022-05-20 19:15:15

目标:

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