《精通Linux设备驱动程序开发》vms.c 测试

本文介绍了一个简单的Linux虚拟鼠标驱动程序实现。该驱动通过sysfs接口接收坐标输入,并将其报告为相对移动事件。程序注册了一个平台设备,创建了用于读取坐标的sysfs节点,并分配了输入设备数据结构。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <linux/fs.h>
#include <asm/uaccess.h>
#include <linux/pci.h>
#include <linux/input.h>
#include <linux/platform_device.h>

struct input_dev *vms_input_dev;
static struct platform_device *vms_dev;
static ssize_t write_vms(struct device *dev,struct device_atrribute *attr, const char *buffer, size_t count)
{
    int x, y;
    sscanf(buffer, "%d%d", &x, &y);
    
    /* Report relative coordinates via the event interface 相对坐标*/
    input_report_rel(vms_input_dev, REL_X, x);
    input_report_rel(vms_input_dev, REL_Y, y);
    input_sync(vms_input_dev);

    return count;
}

//Attach the sysfs write method
DEVICE_ATTR(coordinates, 0664, NULL, write_vms);

//Attribute Descriptor
static struct attribute *vms_attrs[] = {
    &dev_attr_coordinates.attr,
    NULL
};

//Attribute group
static struct attribute_group vms_attr_group = {
    .attrs = vms_attrs,
};

//Driver Initialization
int __init vms_init(void)
{
    /* Register a platform device */
    vms_dev = platform_device_register_simple("vms", -1, NULL, 0);
    if(IS_ERR(vms_dev))
    {
        printk("vms_init: error\n");
        return PTR_ERR(vms_dev);
    }

    /* Create a sysfs node to read simulated coordinates 模拟坐标*/
    sysfs_create_group(&vms_dev->dev.kobj, &vms_attr_group);

    /* Allocate an input device data structure */
    vms_input_dev = input_allocate_device();
    if(!vms_input_dev)
    {
        printk("Bad input_allocate_device()\n");
        return -ENOMEM;
    }

    /* Announce that the virtual mouse will generate relative coordinates 
     * 宣布,虚拟鼠标将产生相对坐标
     */
     set_bit(EV_REL, vms_input_dev->evbit);
     set_bit(REL_X, vms_input_dev->relbit);
     set_bit(REL_Y, vms_input_dev->relbit);

     /* Register with the input subsystem */
     input_register_device(vms_input_dev);
     printk("Virtual Mouse Driver Initialized.\n");
     return 0;
}

//Driver Exit
void vms_cleanup(void)
{
    /* Unregister from the input subsystem */
    input_unregister_device(vms_input_dev);

    /* Clean sysfs node */
    sysfs_remove_group(&vms_dev->dev.kobj, &vms_attr_group);

    /* Unregister driver */
    platform_device_unregister(vms_dev);
    printk("Unregister input device...................\n");

    return;
}
    
module_init(vms_init);
module_exit(vms_cleanup);
MODULE_LICENSE("Dual BSD/GPL");

 

 

 

 

 


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值