1、内核配置
cd kernel
make menuconfig
配置如下:
Device Drivers
USB support
USB Gadget Support
USB Gadget Drivers
HID Gadget
2、修改kernel/drivers/u***/gadget/legacy/hid.c
修改如下:
diff --git a/kernel/drivers/u***/gadget/legacy/hid.c b/kernel/drivers/u***/gadget/legacy/hid.c
old mode 100644
new mode 100755
index 7e5d2c484..addffae56
--- a/kernel/drivers/u***/gadget/legacy/hid.c
+++ b/kernel/drivers/u***/gadget/legacy/hid.c
@@ -31,6 +31,56 @@
/*-------------------------------------------------------------------------*/
+/*--------------------------add by guchunqi begin--------------------------*/
+static struct hidg_func_descriptor my_hid_data =
+{
+ .subclass = 0, /* No subclass */
+ .protocol = 0, /* Keyboard */
+ .report_length = 8,
+ .report_desc_length = 52,
+ .report_desc =
+ {
+ 0x06, 0xA0, 0xFF, //用法页(FFA0h, vendor defined)
+ 0x09, 0x01, //用法(vendor defined)
+ 0xA1, 0x01, //集合(Application)
+ 0x09, 0x02, //用法(vendor defined)
+ 0xA1, 0x00, //集合(Physical)
+ 0x06, 0xA1, 0xFF, //用法页(vendor defined)
+//输入报告
+ 0x09, 0x03, //用法(vendor defined)
+ 0x09, 0x04, //用法(vendor defined)
+ 0x15, 0x80, //逻辑最小值(0x80 or -128)
+ 0x25, 0x7F, //逻辑最大值(0x7F or 127)
+ 0x35, 0x00, //物理最小值(0)
+ 0x45, 0xFF, //物理最大值(255)
+ 0x75, 0x08, //报告长度Report size (8位)
+ 0x95, 0x40, //报告数值(64 fields)
+ 0x81, 0x02, //输入(data, variable, absolute)
+//输出报告
+ 0x09, 0x05, //用法(vendor defined)
+ 0x09, 0x06, //用法(vendor defined)
+ 0x15, 0x80, //逻辑最小值(0x80 or -128)
+ 0x25, 0x7F, //逻辑最大值(0x7F or 127)
+ 0x35, 0x00, //物理最小值(0)
+ 0x45, 0xFF, //物理最大值(255)
+ 0x75, 0x08, //报告长度(8位)
+ 0x95, 0x40, //报告数值(64 fields)
+ 0x91, 0x02, //输出(data, variable, absolute)
+ 0xC0, //集合结束(Physical)
+ 0xC0 //集合结束(Application)
+ }
+};
+
+static struct platform_device my_hid =
+{
+ .name = "hidg",
+ .id = 0,
+ .num_resources = 0,
+ .resource = 0,
+ .dev.platform_data = &my_hid_data,
+};
+/*--------------------------add by guchunqi end----------------------------*/
+
struct hidg_func_node {
struct u***_function_instance *fi;
struct u***_function *f;
@@ -280,6 +330,15 @@ static int __init hidg_init(void)
{
int status;
+/*--------------------add by guchunqi begin------------------------*/
+ printk("********************enter hid**********************rn");
+ status = platform_device_register(&my_hid);
+ if (status < 0)
+ {
+ return status;
+ }
+/*--------------------add by guchunqi end--------------------------*/
+
status = platform_driver_probe(&hidg_plat_driver,
hidg_plat_driver_probe);
if (status < 0)
@@ -297,5 +356,11 @@ static void __exit hidg_cleanup(void)
{
u***_composite_unregister(&hidg_driver);
platform_driver_unregister(&hidg_plat_driver);
+
+/*--------------------add by guchunqi begin------------------------*/
+ platform_device_unregister(&my_hid);
+ printk("********************out hid************************rn");
+/*--------------------add by guchunqi end--------------------------*/
+
}
module_exit(hidg_cleanup);
修改完成后重新编译kernel将在kernel/drivers/u***/gadget生成对应的ko文件
3、拷贝ko到开发板
为了避免modprobe加载驱动的时候,相关依赖的ko找不到,可以把kernel/drivers/u***/gadget下的所有ko拷贝到开发板/lib/modules/4.4.194下面(当然,大神可以忽略)。
注意:4.4.194这个文件可能没有,没有的话自己手动创建(不一定是4.4.194,使用uname -r确认)
4、加载HID驱动
执行一下命令即可完成驱动加载:
depmod
modprobe libcomposite.ko
modprobe u***_f_hid.ko
modprobe g_hid.ko
加载成功会有以下打印:
[root@rk1808:/lib/modules/4.4.194]# modprobe libcomposite.ko
[root@rk1808:/lib/modules/4.4.194]# modprobe u***_f_hid.ko
[root@rk1808:/lib/modules/4.4.194]# modprobe g_hid.ko
[ 77.820449] *******************************enter hid*************************************
[ 77.822072] g_hid gadget: HID Gadget, version: 2010/03/16
[ 77.822114] g_hid gadget: g_hid ready
[root@rk1808:/lib/modules/4.4.194]# [ 81.956406] g_hid gadget: high-speed config #1: HID Gadget
[ 281.935604] rockchip-dwc3 u***: USB unconnected
[ 300.223501] phy phy-fe010000.syscon:u***2-phy@100.2: charger = USB_SDP_CHARGER
[ 300.224768] rockchip-dwc3 u***: USB peripheral connected
[ 309.200718] g_hid gadget: high-speed config #1: HID Gadget
加载成功后将开发板连接到windows PC在设备管理器中可以看到HID设备:
加载成功后将开发板连接到linux PC或者开发板使用lsu***可以看到HID设备:
lsu***
Bus 004 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 003 Device 004: ID 0e0f:0002 VMware, Inc. Virtual USB Hub
Bus 003 Device 003: ID 0e0f:0002 VMware, Inc. Virtual USB Hub
Bus 003 Device 042: ID 0525:a4ac Netchip Technology, Inc.
Bus 003 Device 002: ID 0e0f:0003 VMware, Inc. Virtual Mouse
Bus 003 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
上面的“Bus 003 Device 042: ID 0525:a4ac Netchip Technology, Inc.”就是插入的开发板
可以使用lsu*** -v查看详细的描述符信息:
lsu*** -v
Bus 003 Device 042: ID 0525:a4ac Netchip Technology, Inc.
Couldn't open device, some information will be missing
Device Descriptor:
bLength 18
bDescriptorType 1
bcdUSB 2.10
bDeviceClass 0 (Defined at Interface level)
bDeviceSubClass 0
bDeviceProtocol 0
bMaxPacketSize0 64
idVendor 0x0525 Netchip Technology, Inc.
idProduct 0xa4ac
bcdDevice 4.04
iManufacturer 1
iProduct 2
iSerial 0
bNumConfigurations 1
Configuration Descriptor:
bLength 9
bDescriptorType 2
wTotalLength 41
bNumInterfaces 1
bConfigurationValue 1
iConfiguration 0
bmAttributes 0xc0
Self Powered
MaxPower 500mA
Interface Descriptor:
bLength 9
bDescriptorType 4
bInterfaceNumber 0
bAlternateSetting 0
bNumEndpoints 2
bInterfaceClass 3 Human Interface Device
bInterfaceSubClass 0 No Subclass
bInterfaceProtocol 0 None
iInterface 4
HID Device Descriptor:
bLength 9
bDescriptorType 33
bcdHID 1.01
bCountryCode 0 Not supported
bNumDescriptors 1
bDescriptorType 34 Report
wDescriptorLength 63
Report Descriptors:
** UNAVAILABLE **
Endpoint Descriptor:
bLength 7
bDescriptorType 5
bEndpointAddress 0x81 EP 1 IN
bmAttributes 3
Transfer Type Interrupt
Synch Type None
Usage Type Data
wMaxPacketSize 0x0008 1x 8 bytes
bInterval 4
Endpoint Descriptor:
bLength 7
bDescriptorType 5
bEndpointAddress 0x01 EP 1 OUT
bmAttributes 3
Transfer Type Interrupt
Synch Type None
Usage Type Data
wMaxPacketSize 0x0008 1x 8 bytes
bInterval 4
上面的“bInterfaceClass 3 Human Interface Device”表明这是一个HID设备。
原作者:旧年不在666
|