当前位置: 首页 > java >正文

linux驱动 - 5: simple usb device驱动

参考第2节, 准备好编译环境并实现hello.ko:

linux驱动 - 2: helloworld.ko_linux 驱动开发 hello world ko-CSDN博客

下面在hello模块的基础上, 添加代码, 实现一个usb设备驱动的最小骨架.

#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/usb.h>
#define TAG "hello-usb: "static int hello_usb_probe(struct usb_interface *intf, const struct usb_device_id *id)
{printk(TAG "probe()\n");return 0;
}static void hello_usb_disconnect(struct usb_interface *intf)
{printk(TAG "disconnect()\n");
}static struct usb_device_id hello_usb_id_table [] = {{.match_flags= USB_DEVICE_ID_MATCH_VENDOR|USB_DEVICE_ID_MATCH_INT_CLASS|USB_DEVICE_ID_MATCH_INT_PROTOCOL,.idVendor = 0x046d,.bInterfaceClass = 0x03,.bInterfaceProtocol = 0x02,},{ }	/* Terminating entry */
};struct usb_driver hello_usb_driver = {.name		= "hello-usb",.probe		= hello_usb_probe,.disconnect	= hello_usb_disconnect,.id_table	= hello_usb_id_table,
};static int hello_init(void)
{usb_register(&hello_usb_driver);printk("hello.ko: hello :) \n");return 0;
}static void hello_exit(void)
{usb_deregister(&hello_usb_driver);printk("hello.ko: byebye! \n");
}module_init(hello_init);
module_exit(hello_exit);MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("a hello module");
  1. usb设备驱动用usb_register()注册, 只有一个参数, 即数据结构struct usb_driver.
  2. 数据结构struct usb_driver中比较关键的是3个:
    id(结构体指针), 指向的结构体用来告诉内核哪些vid/pid/class...是该驱动需要接管的设备.
    probe(函数指针), 指向的函数会在设备插入时调用一次, 一般用来创建并提交URB.
    disconnect(函数指针), 指向的函数会在设备拔除时调用一次, 一般用来取消并销毁URB.

如上代码中, id匹配列表表示匹配vid==046d(罗技)/class=03(HID设备)/protocol=02(鼠标).
probe()/disconnect()只添加了打印, 暂未加入处理代码.

make编译即可生成hello.ko.

直接在虚拟机中插入你的罗技鼠标(如果是其他品牌, 可以换成别的vid即可).

发图, 使用virtualbox时的操作. vmware类似.

# rmmod usbhid    (很重要!!! 必须要先卸载掉标准驱动, 否则鼠标会被标准驱动捕获)
# insmod hello.ko
# dmesg -c
[ 2348.440455] usbcore: deregistering interface driver usbhid
[ 2356.464285] hello-usb: probe()
[ 2356.464320] usbcore: registered new interface driver hello-usb
[ 2356.464321] hello.ko: hello :) 

很重要!!! 必须要先卸载掉标准驱动, 否则鼠标会被标准驱动捕获.
如果是自己做的设备端, 只需要将class定义为0xff(厂家自定义), 就不会跟HID有什么冲突和关联了, 也不需要这一步操作了.

http://www.xdnf.cn/news/10534.html

相关文章:

  • 小白的进阶之路系列之十----人工智能从初步到精通pytorch综合运用的讲解第三部分
  • CppCon 2014 学习:Exception-Safe Coding
  • [免费]微信小程序网上花店系统(SpringBoot后端+Vue管理端)【论文+源码+SQL脚本】
  • Cesium快速入门到精通系列教程四:加载渲染GEOJSON数据
  • BA-SAM: 用于 Segment Anything 模型的可扩展偏置模式注意力掩码
  • vue-13(延迟加载路由)
  • Oracle的Hint
  • 2025/6月最新Cursor(0.50.5版本)一键自动更换邮箱无限续杯教程
  • Spring 5 响应式编程:构建高性能全栈应用的关键
  • 数据库系统概论(十二)SQL 基于派生表的查询 超详细讲解(附带例题表格对比带你一步步掌握)
  • MySQL-多表关系、多表查询
  • Qt OpenGL 相机实现
  • 机器学习算法:逻辑回归
  • 操作系统复习
  • 方法重写与方法重载详解
  • CSS之动画(奔跑的熊、两面反转盒子、3D导航栏、旋转木马)
  • 谷歌CEO皮查伊眼中的“下一代平台“与未来图景
  • 基于FPGA的VGA显示文字和动态数字基础例程,进而动态显示数据,类似温湿度等
  • Pyomo中线性规划接口的使用
  • 为什么ping显示connect:network is unreachable,如何排查网络不通问题?
  • LearnOpenGL-笔记-其十三
  • py爬虫的话,selenium是不是能完全取代requests?
  • NodeJS全栈WEB3面试题——P2智能合约与 Solidity
  • 单调栈(打卡)
  • 【C++/Linux】TinyWebServer前置知识之IP协议详解
  • 【iOS】YYModel源码解析
  • 告别printf!嵌入式系统高效日志记录方案
  • 如何评估 RAG 的分块Chunking策略
  • 【沉浸式求职学习day52】【初识Mybaits】
  • 风控研发大数据学习路线