在RK平台上利用gadget去模拟一个HID触摸屏驱动_hid触摸屏

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
目的

在大屏触摸屏上点击一个位置通过RK3568模拟出来的触摸屏能够在usb连接的主机上也点击相对的位置。

实现HID调通的办法:

首先先把HID调通在kernel/drivers/usb/gadget/legacy/hid.c文件中修改添加一个HID键盘使得通过OTG的usb口连接主机后在主机上能够识别该OTG口为一个HID键盘
通过查询资料——https://blog.csdn.net/donghailin/article/details/106096566
添加相应代码后再编译烧录成功在开发板上找到hidg0文件随后通过
echo otg > /sys/devices/platform/fe8a0000.usb2-phy/otg_mode 命令把该usb口配置成otg模式可以先cat一下看看是不是otg模式我这里是host模式所以要改同时也要mask掉adba.service随后连接主机能够看到设备管理器中新添加了一个hid 键盘。已经可以找到hidg0文件接下来的操作都是通过它来实现

调试

参考kernel/Documentation/usb/gadget_hid.txt中的测试例程对HID键盘进行测试结果成功在主机上写入字母或功能键

/* hid_gadget_test */
/*其中第一个参数是hidg0文件位置第二个参数是m鼠标 k键盘*/
#include <pthread.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#define BUF_LEN 512

struct options {
	const char    *opt;
	unsigned char val;
};

static struct options kmod[] = {
	{.opt = "--left-ctrl",		.val = 0x01},
	{.opt = "--right-ctrl",		.val = 0x10},
	{.opt = "--left-shift",		.val = 0x02},
	{.opt = "--right-shift",	.val = 0x20},
	{.opt = "--left-alt",		.val = 0x04},
	{.opt = "--right-alt",		.val = 0x40},
	{.opt = "--left-meta",		.val = 0x08},
	{.opt = "--right-meta",		.val = 0x80},
	{.opt = NULL}
};

static struct options kval[] = {
	{.opt = "--return",	.val = 0x28},
	{.opt = "--esc",	.val = 0x29},
	{.opt = "--bckspc",	.val = 0x2a},
	{.opt = "--tab",	.val = 0x2b},
	{.opt = "--spacebar",	.val = 0x2c},
	{.opt = "--caps-lock",	.val = 0x39},
	{.opt = "--f1",		.val = 0x3a},
	{.opt = "--f2",		.val = 0x3b},
	{.opt = "--f3",		.val = 0x3c},
	{.opt = "--f4",		.val = 0x3d},
	{.opt = "--f5",		.val = 0x3e},
	{.opt = "--f6",		.val = 0x3f},
	{.opt = "--f7",		.val = 0x40},
	{.opt = "--f8",		.val = 0x41},
	{.opt = "--f9",		.val = 0x42},
	{.opt = "--f10",	.val = 0x43},
	{.opt = "--f11",	.val = 0x44},
	{.opt = "--f12",	.val = 0x45},
	{.opt = "--insert",	.val = 0x49},
	{.opt = "--home",	.val = 0x4a},
	{.opt = "--pageup",	.val = 0x4b},
	{.opt = "--del",	.val = 0x4c},
	{.opt = "--end",	.val = 0x4d},
	{.opt = "--pagedown",	.val = 0x4e},
	{.opt = "--right",	.val = 0x4f},
	{.opt = "--left",	.val = 0x50},
	{.opt = "--down",	.val = 0x51},
	{.opt = "--kp-enter",	.val = 0x58},
	{.opt = "--up",		.val = 0x52},
	{.opt = "--num-lock",	.val = 0x53},
	{.opt = NULL}
};

int keyboard_fill_report(char report[8], char buf[BUF_LEN], int *hold)
{
	char *tok = strtok(buf, " ");
	int key = 0;
	int i = 0;

	for (; tok != NULL; tok = strtok(NULL, " ")) {

		if (strcmp(tok, "--quit") == 0)
			return -1;

		if (strcmp(tok, "--hold") == 0) {
			*hold = 1;
			continue;
		}

		if (key < 6) {
			for (i = 0; kval[i].opt != NULL; i++)
				if (strcmp(tok, kval[i].opt) == 0) {
					report[2 + key++] = kval[i].val;
					break;
				}
			if (kval[i].opt != NULL)
				continue;
		}

		if (key < 6)
			if (islower(tok[0])) {
				report[2 + key++] = (tok[0] - ('a' - 0x04));
				continue;
			}

		for (i = 0; kmod[i].opt != NULL; i++)
			if (strcmp(tok, kmod[i].opt) == 0) {
				report[0] = report[0] | kmod[i].val;
				break;
			}
		if (kmod[i].opt != NULL)
			continue;

		if (key < 6)
			fprintf(stderr, "unknown option: %s\n", tok);
	}
	return 8;
}

static struct options mmod[] = {
	{.opt = "--b1", .val = 0x01},
	{.opt = "--b2", .val = 0x02},
	{.opt = "--b3", .val = 0x04},
	{.opt = NULL}
};

int mouse_fill_report(char report[8], char buf[BUF_LEN], int *hold)
{
	char *tok = strtok(buf, " ");
	int mvt = 0;
	int i = 0;
	for (; tok != NULL; tok = strtok(NULL, " ")) {

		if (strcmp(tok, "--quit") == 0)
			return -1;

		if (strcmp(tok, "--hold") == 0) {
			*hold = 1;
			continue;
		}

		for (i = 0; mmod[i].opt != NULL; i++)
			if (strcmp(tok, mmod[i].opt) == 0) {
				report[0] = report[0] | mmod[i].val;
				break;
			}
		if (mmod[i].opt != NULL)
			continue;

		if (!(tok[0] == '-' && tok[1] == '-') && mvt < 2) {
			errno = 0;
			report[1 + mvt++] = (char)strtol(tok, NULL, 0);
			if (errno != 0) {
				fprintf(stderr, "Bad value:'%s'\n", tok);
				report[1 + mvt--] = 0;
			}
			continue;
		}

		fprintf(stderr, "unknown option: %s\n", tok);
	}
	return 3;
}

static struct options jmod[] = {
	{.opt = "--b1",		.val = 0x10},
	{.opt = "--b2",		.val = 0x20},
	{.opt = "--b3",		.val = 0x40},
	{.opt = "--b4",		.val = 0x80},
	{.opt = "--hat1",	.val = 0x00},
	{.opt = "--hat2",	.val = 0x01},
	{.opt = "--hat3",	.val = 0x02},
	{.opt = "--hat4",	.val = 0x03},
	{.opt = "--hatneutral",	.val = 0x04},
	{.opt = NULL}
};

int joystick_fill_report(char report[8], char buf[BUF_LEN], int *hold)
{
	char *tok = strtok(buf, " ");
	int mvt = 0;
	int i = 0;

	*hold = 1;

	/* set default hat position: neutral */
	report[3] = 0x04;

	for (; tok != NULL; tok = strtok(NULL, " ")) {

		if (strcmp(tok, "--quit") == 0)
			return -1;

		for (i = 0; jmod[i].opt != NULL; i++)
			if (strcmp(tok, jmod[i].opt) == 0) {
				report[3] = (report[3] & 0xF0) | jmod[i].val;
				break;
			}
		if (jmod[i].opt != NULL)
			continue;

		if (!(tok[0] == '-' && tok[1] == '-') && mvt < 3) {
			errno = 0;
			report[mvt++] = (char)strtol(tok, NULL, 0);
			if (errno != 0) {
				fprintf(stderr, "Bad value:'%s'\n", tok);
				report[mvt--] = 0;
			}
			continue;
		}

		fprintf(stderr, "unknown option: %s\n", tok);
	}
	return 4;
}

void print_options(char c)
{
	int i = 0;

	if (c == 'k') {
		printf("	keyboard options:\n"
		       "		--hold\n");
		for (i = 0; kmod[i].opt != NULL; i++)
			printf("\t\t%s\n", kmod[i].opt);
		printf("\n	keyboard values:\n"
		       "		[a-z] or\n");
		for (i = 0; kval[i].opt != NULL; i++)
			printf("\t\t%-8s%s", kval[i].opt, i % 2 ? "\n" : "");
		printf("\n");
	} else if (c == 'm') {
		printf("	mouse options:\n"
		       "		--hold\n");
		for (i = 0; mmod[i].opt != NULL; i++)
			printf("\t\t%s\n", mmod[i].opt);
		printf("\n	mouse values:\n"
		       "		Two signed numbers\n"
		       "--quit to close\n");
	} else {
		printf("	joystick options:\n");
		for (i = 0; jmod[i].opt != NULL; i++)
			printf("\t\t%s\n", jmod[i].opt);
		printf("\n	joystick values:\n"
		       "		three signed numbers\n"
		       "--quit to close\n");
	}
}

int main(int argc, const char *argv[])
{
	const char *filename = NULL;
	int fd = 0;
	char buf[BUF_LEN];
	int cmd_len;
	char report[8];
	int to_send = 8;
	int hold = 0;
	fd_set rfds;
	int retval, i;

	if (argc < 3) {
		fprintf(stderr, "Usage: %s devname mouse|keyboard|joystick\n",
			argv[0]);
		return 1;
	}

	if (argv[2][0] != 'k' && argv[2][0] != 'm' && argv[2][0] != 'j')
	  return 2;

	filename = argv[1];

	if ((fd = open(filename, O_RDWR, 0666)) == -1) {
		perror(filename);
		return 3;
	}

	print_options(argv[2][0]);

	while (42) {

		FD_ZERO(&rfds);
		FD_SET(STDIN_FILENO, &rfds);
		FD_SET(fd, &rfds);

		retval = select(fd + 1, &rfds, NULL, NULL, NULL);
		if (retval == -1 && errno == EINTR)
			continue;
		if (retval < 0) {
			perror("select()");
			return 4;
		}

		if (FD_ISSET(fd, &rfds)) {
			cmd_len = read(fd, buf, BUF_LEN - 1);
			printf("recv report:");
			for (i = 0; i < cmd_len; i++)
				printf(" %02x", buf[i]);
			printf("\n");
		}

		if (FD_ISSET(STDIN_FILENO, &rfds)) {
			memset(report, 0x0, sizeof(report));
			cmd_len = read(STDIN_FILENO, buf, BUF_LEN - 1);

			if (cmd_len == 0)
				break;

			buf[cmd_len - 1] = '\0';
			hold = 0;

			memset(report, 0x0, sizeof(report));
			if (argv[2][0] == 'k')
				to_send = keyboard_fill_report(report, buf, &hold);
			else if (argv[2][0] == 'm')
				to_send = mouse_fill_report(report, buf, &hold);
			else
				to_send = joystick_fill_report(report, buf, &hold);

			if (to_send == -1)
				break;

			if (write(fd, report, to_send) != to_send) {
				perror(filename);
				return 5;
			}
			if (!hold) {
				memset(report, 0x0, sizeof(report));
				if (write(fd, report, to_send) != to_send) {
					perror(filename);
					return 6;
				}
			}
		}
	}

	close(fd);
	return 0;
}

实现触摸点击办法

通过查询在https://www.cnblogs.com/barfoot/p/13587190.html上有教学如何把一个鼠标模拟成触摸点击。
一般鼠标和触摸点击对比传统鼠标报告的位置是相对位置也就是上次报告和今次报告对比的位置而实现触摸点击我们需要报告的是一个绝对位置根据教学中对HID报告描述符的配置以及结合《圈圈教你玩USB第二版》对于HID报告描述符的详细说明整理了一份能报告绝对坐标的HID鼠标的报告描述符文档

static struct hidg_func_descriptor fdesc_hid= {
	subclass           = 0, /* No subclass */
	protocol           = 0, /* Mouse */
	report_length      = 5,
	report_desc_length = 56,
	//其中第一个字节表示定义控制的特性第二个或第二和第三个表示它的值
	.report_desc        = {
	        0x05, 0x01,  //Usage Page(Generic Desktop Controls)    全局的条目表示选择用途页为普通桌面
	        0x09, 0x02,  //Usage (Mouse)    应用的集合用途为鼠标
	        0xa1, 0x01,  //Collection (Application)    主条目开集合0x01表示该集合为应用集合由前面定义为普通桌面鼠标
	        0x09, 0x01,  //Usage (pointer)    局部条目用途为指针集合
	        0xa1, 0x00,  //Collection (Physical)    主条目开集合0x00表示该集合为物理集合
	        0x05, 0x09,  //Usage Page (Button)    全局条目选择用途页为按键
	        0x19, 0x01,  //Usage Minimum(1)    局部条目说明用途的最小值为1.实际是鼠标左键
	        0x29, 0x05,  //Usage Maximum(5)    局部条目说明用途的最大值为5.实际上是鼠标中键
	        0x15, 0x00,  //Logical Minimum(1)    全局条目说明发送的数据逻辑值最小值发送的数据域的值为0
	        0x25, 0x01,  //Logical Maximum(1)    全局条目说明发送的数据逻辑值最大值为1
	        0x95, 0x05,  //Report Count(5)    全局条目说明数据域的数量为5
	        0x75, 0x01,  //Report Size(1)     全局条目说明数据域的长度为1位
	        0x81, 0x02,  //Input(Data,Variable,Absolute,BitField)
	
	/*            ↑↑↑↑↑↑主条目↑↑↑↑↑↑
	        说明有5个长度为1位的数据域由前边的全局条目定义
	        用来作为输入属性位Data、Var、Abs、BitField
	        Data表示数据的大小
	        Var表示这些数据域是独立的变量即每个数据域表示一个意思
	        Abs表示绝对值
	        这样定义后第一个数据域bit0表示左键是否按下
	        第二个数据域bit1表示右键是否按下
	        第三个数据域bit2表示中键是否按下
	        剩下的2个bit可能是鼠标的两个侧键猜测。
	*/
	        0x95, 0x01,  //Report Count(1)    凑字数的这里一共3个bit和前面的5个bit凑够一个字节
	        0x75, 0x03,  //Report Size(3)     凑字数
	        0x81, 0x01,  //Input(Constant,Array,Absolute,BitField)    凑字数
	        0x05, 0x01,  //Usage Page(Generic Desktop Controls)    全局条目选择用途页为普通桌面
	        0x09, 0x30,  //Usage(x)    局部条目为x轴
	        0x09, 0x31,  //Usage(y)    局部条目为y轴
	        0x15, 0x00,  //     LOGICAL_MINIMUM (0)    全局条目说明发送的X数据逻辑值最小值为0
	        0x26, 0xff, 0x7f, //     LOGICAL_MAXIMUM (32767)    全局条目说明发送的数据逻辑值最大值0x26为绝对值0x25为相对值为32767
	        0x35, 0x00,    //Physical Minimum (0)    全局条目说明发送的Y数据逻辑值最小值
	        0x46, 0xff, 0x7f, //Physical Maximum(32767)    全局条目说明发送的Y数据逻辑值最大值
	        0x75, 0x10,  //Report Size(16)    全局条目说明数据域的长度为16
	        0x95, 0x02,  //Report Count(2)    全局条目说明数据域的个数为2
	        0x81, 0x02,  //Input(Data,Variable,ABS)
	/*        ↑↑↑↑↑↑主条目↑↑↑↑↑↑
	        说明了2个16位的数据域是输入用的属性为DataVarAbsAbs表示了绝对量。
	*/
	        0xc0,  //End Collection    因为开了两个集合应用集合和物理集合所以要关闭两次
	        0xc0  //End Collection
	}
};
/*
    不难发现在写入hidg0文件的5个字节对应的就是这里的数据域其中1个字节表示的是按键按下情况而且有3个bit是多余的
    还有4个字节分别代表2个字节长度的x轴绝对坐标和y轴绝对坐标
*/

里面有对HID鼠标的报告描述符每条配置的详细说明。
利用这个报告描述符以及my_hid_data的各参数替换重复上面HID键盘的调试步骤并且利用教学里的测试程序去测试结果能够成功在主机上模拟一个绝对位置的鼠标能够完成位移和点击左键、右键、中键的操作

实现精确位置的办法

上面的只是实现了位移和点击的操作但位移量没有控制通过对报告描述符的详细说明文档可以发现X轴坐标和Y轴坐标均是配置成0到0x7fff32767给入x和y的值(0,0) (16300,16300) (32766,32766)后指针分别移动到左上角正中间右下角由此可以推断这个输入值是一个比例关系且通过更换分辨率、输入值后只要是同一个xy值就在相同的位置。
至此可以建立一个转换坐标的函数通过对于当前显示器的分辨率例如19201080以及需要移动到屏幕上的坐标例如中间是960540能够返回一个0到0x7fff之间的坐标值利用该坐标值可以移动指针到屏幕上对应的坐标

#include <pthread.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

/*
 * 函数功能移动鼠标指针
 *
 *
 * */
void MoveTo(int x,int y,int mFd)
{
        char Buf[5] = {0,0,0,0,0};
        Buf[0] = 0x00;
        Buf[1] = x & 0xFF;
        Buf[2] = (x>>8) & 0xFF;
        Buf[3] = y & 0xFF;
        Buf[4] = (y>>8) & 0xFF;
        if(write(mFd, Buf, 5) != 5){
                printf("write fd error\n");
                return;
        }
        usleep(50000);
}


/*
 * 函数功能点击后移动
 *
 *
 * */
void LineTo(int x,int y,int mFd)
{
        char Buf[5]={0,0,0,0,0};
        Buf[0] = 0x01;  //左键按下
        Buf[1] = x & 0xFF;
        Buf[2] = (x>>8) & 0xFF;
        Buf[3] = y & 0xFF;
        Buf[4] = (y>>8) & 0xFF;
        if(write(mFd,Buf,5) != 5){
                printf("write fd error\n");
                return;
                }
        usleep(50000);
}

typedef struct {
        int x;
        int y;
}location;

/*
 * 函数功能转换坐标
 *
 *
 * */
void trans_location(int origin_x,int origin_y,int image_x,int image_y,location* l)
{
        l->x = (0x7FFF*origin_x + image_x/2) / image_x;
        l->y = (0x7FFF*origin_y + image_y/2) / image_y;
}


int main(int argc, const char *argv[])
{
        int fd;
        location* l;
        int origin_x = atoi(argv[2]);
        int origin_y = atoi(argv[3]);
        int image_x = atoi(argv[4]);
        int image_y = atoi(argv[5]);
        const char *filename = argv[1];

        trans_location(origin_x,origin_y,image_x,image_y,l);
        int x = l->x;
        int y = l->y;

        if(argc != 6){
                printf("Please enter  hid file path / origin_x / origin_y / image_x / image_y /!\n");
                return -1;
        }

        if((fd = open(filename, O_RDWR, 0666)) == -1) {
                perror(filename);
                return 3;
        }
        MoveTo(x,y,fd);
        usleep(55000);
        MoveTo(x+300,y+300,fd);
        MoveTo(x+400,y+400,fd);
        MoveTo(x+500,y+500,fd);
        MoveTo(x+600,y+600,fd);
        return 0;
}```

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6