1、概述
Android 和PC系统本身是支持 usb hid输入设备的。不过由于业务的发展,需要采用高精度触摸框。重新设计框架,改变原来 串口+usb_hid的方式。采用俩路usb,一路usb_buik+一路usb_hid方式。具体架构如下图:
2、触摸驱动
该驱动是基于Android 8.0 内核 4.9版本上调试的。驱动主要从俩个方面去分析:1) USB 驱动框架 2)input 驱动
2.1 USB驱动框架
USB驱动基于Linux USB总线完成的。主要注册USB 设备和填充USB设备结构体相关信息。
static int __init usb_touch_init(void)
{
int retval = usb_register(&XX_driver); //注册usb driver到系统中
printk(KERN_ALERT "usb touch init\r\n");
if(retval)
printk(KERN_ALERT "usb touch init error %d\r\n",retval);
return retval;
}
static void __exit usb_touch_exit(void)
{
printk(KERN_ALERT "usb touch exit");
usb_deregister(&XX_driver);
}
module_init(usb_touch_init);
module_exit(usb_touch_exit);
Linux 内核驱动都是从init函数开始执行,相当于应用程序里面的main函数。在init函数中主要做了usb_register(),注册usb driver到
系统中。usb_register() 参数为struct usb_driver 结构。
static struct usb_driver XX_driver = {
.name = "XXXXXX", //usb driver name
.probe = XX_probe, //usb 匹配后执行probe
.disconnect = XX_disconnect, //usb 设备断开
.id_table = XX_id_table, // 用于usb 匹配信息
};
在usb_driver结构中重要的几个参数为 .probe 该函数用于usb 设备匹配正确后执行的。匹配规则后面会讲到 ,.disconnect该函数用于usb设备断开后执行的,主要做一些资源的释放。id_table 用于usb设备匹配信息,下面会讲。.name 用于usb driver name
static struct usb_device_id XX_id_table [] ={
{
.match_flags = USB_DEVICE_ID_MATCH_VENDOR,//匹配规则,根据vid pid 去匹配设备
.idVendor = XXXX,
.idProduct = XXXXX,
},
{
.match_flags = USB_DEVICE_ID_MATCH_VENDOR, //可以匹配多个usb设备
.idVendor = XXXXX,
.idProduct = XXXXX,
},
//{ USB_DEVICE(USB_TOUCH_VENDOR_ID, USB_TOUCH_PRODUCT_ID) },
{}
};
MODULE_DEVICE_TABLE (usb, XX_id_table );
usb _device_id 写明usb 设备与驱动匹配的规则,这里采用的 是根据vid pid去匹配。每个usb设备的pid vid都是唯一的,当然也可以根据class Subclass (设备描述符)去匹配usb 设备.同时一个usb 驱动可以匹配多个usb 设备。MODULE_DEVICE_TABLE 宏将你写好的匹配规则带入到系统中.
注意 usb_device_id XX_id_table 要与 usb_driver 中的.id_table 关联起来,系统才可以识别相关的usb 设备。
static int usb_touch_probe(struct usb_interface *intf,const s