使用Gadget 做usb鼠标键盘设备

感谢TI社区提供的好帮助啊!http://e2e.ti.com/support/arm/sitara_arm/f/791/p/571771/2103409?pi316653=2
虽然海思平台和TI平台不一样 ,但也大同小异。

具体的步骤如下:
转到内核目录:
//加载配置文件

>cp xxx .config
修改配置
make xxxx menuconfig
>-> Device Drivers -> USB support -> USB Gadget Support -> <M> USB Gadget Drivers -> <M> HID Gadget
进到:
>-> Device Drivers -> USB support > USB Gadget Support-> USB Peripheral Controller
> ->Device Drivers ->USB support > USB Gadget Support -> USB Peripheral Controller -> Hisilicon USB2. Device Controller SUPPORT
+ [*] Hisilicon USB2. Device auto switch

打开自动切换功能。
需要注意的是gadget子项的驱动依赖于udc模块和composite模块,而composite模块又依赖于udc模块,udc是硬件接口模块,直接操作USB寄存器等。composite为上层驱动提供操作UDC。

所以加载顺序必要是udc-hisi.ko 、libcomposite.ko 、g_hid.ko

加载发现出现问题,某些函数不能用,经过研究要打开

> File systems > Pseudo filesystems
+ {*} Userspace-driven configuration filesystem

即可,加载g_hid.ko后出现:

insmod: can't insert 'g_hid.ko': No such device

原因在于没有注册设备报告描述符!!!修改hid.c文件, 头文件:

```
+#include <linux/usb/g_hid.h>
```

添加键盘报告描述符

+static struct hidg_func_descriptor keyboard_report_data =
+{
+ .subclass = , /* No subclass */
+ .protocol = , /* Keyboard */
+ .report_length = ,
+ .report_desc_length = ,
+ .report_desc = {
+ 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
+ 0x09, 0x06, /* USAGE (Keyboard) */
+ 0xa1, 0x01, /* COLLECTION (Application) */
+ 0x05, 0x07, /* USAGE_PAGE (Keyboard) */
+ 0x19, 0xe0, /* USAGE_MINIMUM (Keyboard LeftControl) */
+ 0x29, 0xe7, /* USAGE_MAXIMUM (Keyboard Right GUI) */
+ 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
+ 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
+ 0x75, 0x01, /* REPORT_SIZE (1) */
+ 0x95, 0x08, /* REPORT_COUNT (8) */
+ 0x81, 0x02, /* INPUT (Data,Var,Abs) */
+ 0x95, 0x01, /* REPORT_COUNT (1) */
+ 0x75, 0x08, /* REPORT_SIZE (8) */
+ 0x81, 0x03, /* INPUT (Cnst,Var,Abs) */
+ 0x95, 0x05, /* REPORT_COUNT (5) */
+ 0x75, 0x01, /* REPORT_SIZE (1) */
+ 0x05, 0x08, /* USAGE_PAGE (LEDs) */
+ 0x19, 0x01, /* USAGE_MINIMUM (Num Lock) */
+ 0x29, 0x05, /* USAGE_MAXIMUM (Kana) */
+ 0x91, 0x02, /* OUTPUT (Data,Var,Abs) */
+ 0x95, 0x01, /* REPORT_COUNT (1) */
+ 0x75, 0x03, /* REPORT_SIZE (3) */
+ 0x91, 0x03, /* OUTPUT (Cnst,Var,Abs) */
+ 0x95, 0x06, /* REPORT_COUNT (6) */
+ 0x75, 0x08, /* REPORT_SIZE (8) */
+ 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
+ 0x25, 0x65, /* LOGICAL_MAXIMUM (101) */
+ 0x05, 0x07, /* USAGE_PAGE (Keyboard) */
+ 0x19, 0x00, /* USAGE_MINIMUM (Reserved) */
+ 0x29, 0x65, /* USAGE_MAXIMUM (Keyboard Application) */
+ 0x81, 0x00, /* INPUT (Data,Ary,Abs) */
+ 0xc0 /* END_COLLECTION */
+};

定义键盘设备:

+static struct platform_device keyboard_device = {
+ .name = "hidg",
+ .id = ,
+ .num_resources = ,
+ .resource = ,
+ .dev.platform_data = &keyboard_report_data,
+};

在static int __init hidg_init(void)函数开始处注册键盘设备

+status = platform_device_register(&keyboard_device);
+ if (status < ) {
+ printk("____ reg failed\n");
+ platform_device_unregister(&keyboard_device);
+ return status;
+ }

在驱动卸载处释放键盘设备

platform_driver_unregister(&hidg_plat_driver);
+platform_device_unregister(&keyboard_device);
usb_composite_unregister(&hidg_driver);

添加后即可识别为键盘设别如下图所示:

单个键盘和鼠标没有任何问题,然而同时设置鼠标和键盘加载后出现如下错误:

------------[ cut here ]------------
WARNING: CPU: PID: at fs/sysfs/dir.c: sysfs_warn_dup+0x60/0x70()
sysfs: cannot create duplicate filename ‘/devices/platform/hidg.’
Modules linked in: g_hid(+) libcomposite udc_hisi [last unloaded: g_hid]
CPU: PID: Comm: insmod Tainted: G W 3.18. #
[] (unwind_backtrace) from [] (show_stack+0x10/0x14)
[] (show_stack) from [] (dump_stack+0x88/0x98)
[] (dump_stack) from [] (warn_slowpath_common+0x6c/0x88)
[] (warn_slowpath_common) from [] (warn_slowpath_fmt+0x30/0x40)
[] (warn_slowpath_fmt) from [] (sysfs_warn_dup+0x60/0x70)
[] (sysfs_warn_dup) from [] (sysfs_create_dir_ns+0x88/0x98)
[] (sysfs_create_dir_ns) from [] (kobject_add_internal+0xb0/0x300)
[] (kobject_add_internal) from [] (kobject_add+0x48/0x94)
[] (kobject_add) from [] (device_add+0xd8/0x53c)
[] (device_add) from [] (platform_device_add+0xb4/0x21c)
[] (platform_device_add) from [] (hidg_init+0x38/0xc8 [g_hid])
[] (hidg_init [g_hid]) from [] (do_one_initcall+0x8c/0x1c8)
[] (do_one_initcall) from [] (load_module+0x1740/0x1e88)
[] (load_module) from [] (SyS_init_module+0xd8/0xec)
[] (SyS_init_module) from [] (ret_fast_syscall+0x0/0x38)
—[ end trace c7b2846a724052eb ]—

原因如下位置:

static struct platform_device mouse_device = {
.name = “hidg”,
.id = ,
.num_resources = ,
.resource = ,
.dev.platform_data = &mouse_report_desc,
};

id为0和keyboard的id也是0相同了冲突了!! 经过这样子做,鼠标键盘在dev对应的是hidg0(键盘id 0) hidg1(鼠标id 1),只要打开这两个设备,往这两个设备发送键盘和鼠标报告就可以了

经过如此的配置只要按照顺序加载udc-hisi.ko、libcomposite.ko、以及g_hid.ko即可使用gadget鼠标键盘设备了,卸载这些驱动后,host又可以正常使用。

使用Gadget 做usb鼠标键盘设备的更多相关文章

  1. Qt移植对USB鼠标键盘、触摸屏的支持

    .USB键盘 经过一番搜索,发现对Qt键盘的支持主要关系到两个方面: 1. 键盘类型确定: 4.7以前的Qt版本,如果是PS2圆孔键盘,Qt编译时需加上选项:-qt-kbd-vr41xx(未测试):如 ...

  2. USB鼠标键盘数据格式以及按键键值

    鼠标发送给PC的数据每次4个字节 BYTE1 BYTE2 BYTE3 BYTE4 定义分别是: BYTE1 --        |--bit7:   1   表示   Y   坐标的变化量超出-256 ...

  3. 解决ubuntu16.04 USB鼠标键盘使用卡顿

    小米游戏本,键盘突然M建不能用,去了售后换了个键盘. 小米售后真的不想吐槽……入坑 键盘鼠标卡顿由于挂起导致失灵,每次需要激活所以卡顿 解决办法如下: 编辑如下文件配置 sudo vim /etc/l ...

  4. STM32 USB 鼠标+键盘 串口控制

    *MOS0101000000# 鼠标左键按下 *MOS0102000000# 鼠标右键按下 *MOS0103000000# 鼠标中键按下 *MOS0100000000# 鼠标抬起 *MOS01000a ...

  5. DM816X 实现 USB HID Gadget 鼠标键盘功能【转】

    转自:https://my.oschina.net/renyongke/blog/410695 开发环境: 平台: DM8168 内核 :linux 2.6.32 RDK:DVRRDK_04.00.0 ...

  6. 玩转X-CTR100 l USB功能-HOST鼠标键盘

    更多塔克创新资讯欢迎登陆[塔克社区 www.xtark.cn ][塔克博客 www.cnblogs.com/xtark/ ] X-CTR100控制器具有MicroUSB接口,支持SLAVE和HOST两 ...

  7. usb的hid鼠标键盘报告描述符(五)

    title: usb的hid鼠标键盘报告描述符 tags: linux date: 2018/12/20/ 18:05:08 toc: true --- usb的hid鼠标键盘报告描述符 https: ...

  8. 学习Linux下s3c2440的USB鼠标驱动笔记

    1.ARM-Linux下USB驱动程序开发1.1.1.linux下USB配置:*********(MassStorage:存储设备)********************************** ...

  9. Linux usb子系统(一) _写一个usb鼠标驱动

    USB总线是一种典型的热插拔的总线标准,由于其优异的性能几乎成为了当下大小设备中的标配. USB的驱动可以分为3类:SoC的USB控制器的驱动,主机端USB设备的驱动,设备上的USB Gadget驱动 ...

随机推荐

  1. 【原创】大数据基础之ElasticSearch(4)es数据导入过程

    1 准备analyzer 内置analyzer 参考:https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis- ...

  2. vue.js过滤器

    import Vue from 'vue' import { ENV } from '@/config/conf' const dateFormat = (str) => { var date ...

  3. C语言经典题目

    回顾一下吧: 一. 有1.2.3.4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少? 解析:可填在百位.十位.个位的数字都是1.2.3.4.组成所有的排列后再去 掉不满足条件的排列. 常规 ...

  4. 《剑指offer》整数中1出现的次数

    本题来自<剑指offer> 反转链表 题目: 思路: C++ Code: Python Code: 总结:

  5. delete web server(nginx+apache)

    #!/bin/bash conf_dir1="/usr/local/nginx/conf/vhost.d" conf_dir2="/usr/local/apache2/c ...

  6. Linux文件系统的目录结构详解

    Linux文件系统的目录结构详解   一.前 言 文章对Linux下所有目录一一说明,对比较重要的目录加以重点解说,以帮助初学者熟练掌握Linux的目录结构. 二.目 录 1.什么是文件系统 2.文件 ...

  7. python 基于机器学习识别验证码

    1.背景    验证码自动识别在模拟登陆上使用的较为广泛,一直有耳闻好多人在使用机器学习来识别验证码,最近因为刚好接触这方面的知识,所以特定研究了一番.发现网上已有很多基于machine learni ...

  8. post 数据

    可参照:http://www.voidcn.com/blog/Vindra/article/p-4917667.html 一.get请求 curl "http://www.baidu.com ...

  9. Angular路由——子路由

    一.子路由语法: 二.实例 在商品详情页面,除了显示商品id信息,还显示了商品描述,和销售员的信息. 通过子路由实现商品描述组件和销售员信息组件展示在商品详情组件内部. 1.新建2个组件修改其内容 n ...

  10. Service的启动过程

    --摘自<Android进阶解密> 第一步:ContextImpl到ActivityManagerService的调用过程 第二步:ActivityThread启动Service 1)Pr ...