hid_common.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /* Copyright (c) 2022-2024 Red Hat */
  3. #include "../kselftest_harness.h"
  4. #include <fcntl.h>
  5. #include <fnmatch.h>
  6. #include <dirent.h>
  7. #include <poll.h>
  8. #include <pthread.h>
  9. #include <stdbool.h>
  10. #include <linux/hidraw.h>
  11. #include <linux/uhid.h>
  12. #define SHOW_UHID_DEBUG 0
  13. #define min(a, b) \
  14. ({ __typeof__(a) _a = (a); \
  15. __typeof__(b) _b = (b); \
  16. _a < _b ? _a : _b; })
  17. static unsigned char rdesc[] = {
  18. 0x06, 0x00, 0xff, /* Usage Page (Vendor Defined Page 1) */
  19. 0x09, 0x21, /* Usage (Vendor Usage 0x21) */
  20. 0xa1, 0x01, /* COLLECTION (Application) */
  21. 0x09, 0x01, /* Usage (Vendor Usage 0x01) */
  22. 0xa1, 0x00, /* COLLECTION (Physical) */
  23. 0x85, 0x02, /* REPORT_ID (2) */
  24. 0x19, 0x01, /* USAGE_MINIMUM (1) */
  25. 0x29, 0x08, /* USAGE_MAXIMUM (3) */
  26. 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
  27. 0x25, 0xff, /* LOGICAL_MAXIMUM (255) */
  28. 0x95, 0x08, /* REPORT_COUNT (8) */
  29. 0x75, 0x08, /* REPORT_SIZE (8) */
  30. 0x81, 0x02, /* INPUT (Data,Var,Abs) */
  31. 0xc0, /* END_COLLECTION */
  32. 0x09, 0x01, /* Usage (Vendor Usage 0x01) */
  33. 0xa1, 0x00, /* COLLECTION (Physical) */
  34. 0x85, 0x01, /* REPORT_ID (1) */
  35. 0x06, 0x00, 0xff, /* Usage Page (Vendor Defined Page 1) */
  36. 0x19, 0x01, /* USAGE_MINIMUM (1) */
  37. 0x29, 0x03, /* USAGE_MAXIMUM (3) */
  38. 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
  39. 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
  40. 0x95, 0x03, /* REPORT_COUNT (3) */
  41. 0x75, 0x01, /* REPORT_SIZE (1) */
  42. 0x81, 0x02, /* INPUT (Data,Var,Abs) */
  43. 0x95, 0x01, /* REPORT_COUNT (1) */
  44. 0x75, 0x05, /* REPORT_SIZE (5) */
  45. 0x81, 0x01, /* INPUT (Cnst,Var,Abs) */
  46. 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
  47. 0x09, 0x30, /* USAGE (X) */
  48. 0x09, 0x31, /* USAGE (Y) */
  49. 0x15, 0x81, /* LOGICAL_MINIMUM (-127) */
  50. 0x25, 0x7f, /* LOGICAL_MAXIMUM (127) */
  51. 0x75, 0x10, /* REPORT_SIZE (16) */
  52. 0x95, 0x02, /* REPORT_COUNT (2) */
  53. 0x81, 0x06, /* INPUT (Data,Var,Rel) */
  54. 0x06, 0x00, 0xff, /* Usage Page (Vendor Defined Page 1) */
  55. 0x19, 0x01, /* USAGE_MINIMUM (1) */
  56. 0x29, 0x03, /* USAGE_MAXIMUM (3) */
  57. 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
  58. 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
  59. 0x95, 0x03, /* REPORT_COUNT (3) */
  60. 0x75, 0x01, /* REPORT_SIZE (1) */
  61. 0x91, 0x02, /* Output (Data,Var,Abs) */
  62. 0x95, 0x01, /* REPORT_COUNT (1) */
  63. 0x75, 0x05, /* REPORT_SIZE (5) */
  64. 0x91, 0x01, /* Output (Cnst,Var,Abs) */
  65. 0x06, 0x00, 0xff, /* Usage Page (Vendor Defined Page 1) */
  66. 0x19, 0x06, /* USAGE_MINIMUM (6) */
  67. 0x29, 0x08, /* USAGE_MAXIMUM (8) */
  68. 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
  69. 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
  70. 0x95, 0x03, /* REPORT_COUNT (3) */
  71. 0x75, 0x01, /* REPORT_SIZE (1) */
  72. 0xb1, 0x02, /* Feature (Data,Var,Abs) */
  73. 0x95, 0x01, /* REPORT_COUNT (1) */
  74. 0x75, 0x05, /* REPORT_SIZE (5) */
  75. 0x91, 0x01, /* Output (Cnst,Var,Abs) */
  76. 0xc0, /* END_COLLECTION */
  77. 0xc0, /* END_COLLECTION */
  78. };
  79. static __u8 feature_data[] = { 1, 2 };
  80. #define ASSERT_OK(data) ASSERT_FALSE(data)
  81. #define ASSERT_OK_PTR(ptr) ASSERT_NE(NULL, ptr)
  82. #define UHID_LOG(fmt, ...) do { \
  83. if (SHOW_UHID_DEBUG) \
  84. TH_LOG(fmt, ##__VA_ARGS__); \
  85. } while (0)
  86. static pthread_mutex_t uhid_started_mtx = PTHREAD_MUTEX_INITIALIZER;
  87. static pthread_cond_t uhid_started = PTHREAD_COND_INITIALIZER;
  88. static pthread_mutex_t uhid_output_mtx = PTHREAD_MUTEX_INITIALIZER;
  89. static pthread_cond_t uhid_output_cond = PTHREAD_COND_INITIALIZER;
  90. static unsigned char output_report[10];
  91. /* no need to protect uhid_stopped, only one thread accesses it */
  92. static bool uhid_stopped;
  93. static int uhid_write(struct __test_metadata *_metadata, int fd, const struct uhid_event *ev)
  94. {
  95. ssize_t ret;
  96. ret = write(fd, ev, sizeof(*ev));
  97. if (ret < 0) {
  98. TH_LOG("Cannot write to uhid: %m");
  99. return -errno;
  100. } else if (ret != sizeof(*ev)) {
  101. TH_LOG("Wrong size written to uhid: %zd != %zu",
  102. ret, sizeof(ev));
  103. return -EFAULT;
  104. } else {
  105. return 0;
  106. }
  107. }
  108. static int uhid_create(struct __test_metadata *_metadata, int fd, int rand_nb)
  109. {
  110. struct uhid_event ev;
  111. char buf[25];
  112. sprintf(buf, "test-uhid-device-%d", rand_nb);
  113. memset(&ev, 0, sizeof(ev));
  114. ev.type = UHID_CREATE;
  115. strcpy((char *)ev.u.create.name, buf);
  116. ev.u.create.rd_data = rdesc;
  117. ev.u.create.rd_size = sizeof(rdesc);
  118. ev.u.create.bus = BUS_USB;
  119. ev.u.create.vendor = 0x0001;
  120. ev.u.create.product = 0x0a37;
  121. ev.u.create.version = 0;
  122. ev.u.create.country = 0;
  123. sprintf(buf, "%d", rand_nb);
  124. strcpy((char *)ev.u.create.phys, buf);
  125. return uhid_write(_metadata, fd, &ev);
  126. }
  127. static void uhid_destroy(struct __test_metadata *_metadata, int fd)
  128. {
  129. struct uhid_event ev;
  130. memset(&ev, 0, sizeof(ev));
  131. ev.type = UHID_DESTROY;
  132. uhid_write(_metadata, fd, &ev);
  133. }
  134. static int uhid_event(struct __test_metadata *_metadata, int fd)
  135. {
  136. struct uhid_event ev, answer;
  137. ssize_t ret;
  138. memset(&ev, 0, sizeof(ev));
  139. ret = read(fd, &ev, sizeof(ev));
  140. if (ret == 0) {
  141. UHID_LOG("Read HUP on uhid-cdev");
  142. return -EFAULT;
  143. } else if (ret < 0) {
  144. UHID_LOG("Cannot read uhid-cdev: %m");
  145. return -errno;
  146. } else if (ret != sizeof(ev)) {
  147. UHID_LOG("Invalid size read from uhid-dev: %zd != %zu",
  148. ret, sizeof(ev));
  149. return -EFAULT;
  150. }
  151. switch (ev.type) {
  152. case UHID_START:
  153. pthread_mutex_lock(&uhid_started_mtx);
  154. pthread_cond_signal(&uhid_started);
  155. pthread_mutex_unlock(&uhid_started_mtx);
  156. UHID_LOG("UHID_START from uhid-dev");
  157. break;
  158. case UHID_STOP:
  159. uhid_stopped = true;
  160. UHID_LOG("UHID_STOP from uhid-dev");
  161. break;
  162. case UHID_OPEN:
  163. UHID_LOG("UHID_OPEN from uhid-dev");
  164. break;
  165. case UHID_CLOSE:
  166. UHID_LOG("UHID_CLOSE from uhid-dev");
  167. break;
  168. case UHID_OUTPUT:
  169. UHID_LOG("UHID_OUTPUT from uhid-dev");
  170. pthread_mutex_lock(&uhid_output_mtx);
  171. memcpy(output_report,
  172. ev.u.output.data,
  173. min(ev.u.output.size, sizeof(output_report)));
  174. pthread_cond_signal(&uhid_output_cond);
  175. pthread_mutex_unlock(&uhid_output_mtx);
  176. break;
  177. case UHID_GET_REPORT:
  178. UHID_LOG("UHID_GET_REPORT from uhid-dev");
  179. answer.type = UHID_GET_REPORT_REPLY;
  180. answer.u.get_report_reply.id = ev.u.get_report.id;
  181. answer.u.get_report_reply.err = ev.u.get_report.rnum == 1 ? 0 : -EIO;
  182. answer.u.get_report_reply.size = sizeof(feature_data);
  183. memcpy(answer.u.get_report_reply.data, feature_data, sizeof(feature_data));
  184. uhid_write(_metadata, fd, &answer);
  185. break;
  186. case UHID_SET_REPORT:
  187. UHID_LOG("UHID_SET_REPORT from uhid-dev");
  188. break;
  189. default:
  190. TH_LOG("Invalid event from uhid-dev: %u", ev.type);
  191. }
  192. return 0;
  193. }
  194. struct uhid_thread_args {
  195. int fd;
  196. struct __test_metadata *_metadata;
  197. };
  198. static void *uhid_read_events_thread(void *arg)
  199. {
  200. struct uhid_thread_args *args = (struct uhid_thread_args *)arg;
  201. struct __test_metadata *_metadata = args->_metadata;
  202. struct pollfd pfds[1];
  203. int fd = args->fd;
  204. int ret = 0;
  205. pfds[0].fd = fd;
  206. pfds[0].events = POLLIN;
  207. uhid_stopped = false;
  208. while (!uhid_stopped) {
  209. ret = poll(pfds, 1, 100);
  210. if (ret < 0) {
  211. TH_LOG("Cannot poll for fds: %m");
  212. break;
  213. }
  214. if (pfds[0].revents & POLLIN) {
  215. ret = uhid_event(_metadata, fd);
  216. if (ret)
  217. break;
  218. }
  219. }
  220. return (void *)(long)ret;
  221. }
  222. static int uhid_start_listener(struct __test_metadata *_metadata, pthread_t *tid, int uhid_fd)
  223. {
  224. struct uhid_thread_args args = {
  225. .fd = uhid_fd,
  226. ._metadata = _metadata,
  227. };
  228. int err;
  229. pthread_mutex_lock(&uhid_started_mtx);
  230. err = pthread_create(tid, NULL, uhid_read_events_thread, (void *)&args);
  231. ASSERT_EQ(0, err) {
  232. TH_LOG("Could not start the uhid thread: %d", err);
  233. pthread_mutex_unlock(&uhid_started_mtx);
  234. close(uhid_fd);
  235. return -EIO;
  236. }
  237. pthread_cond_wait(&uhid_started, &uhid_started_mtx);
  238. pthread_mutex_unlock(&uhid_started_mtx);
  239. return 0;
  240. }
  241. static int uhid_send_event(struct __test_metadata *_metadata, int fd, __u8 *buf, size_t size)
  242. {
  243. struct uhid_event ev;
  244. if (size > sizeof(ev.u.input.data))
  245. return -E2BIG;
  246. memset(&ev, 0, sizeof(ev));
  247. ev.type = UHID_INPUT2;
  248. ev.u.input2.size = size;
  249. memcpy(ev.u.input2.data, buf, size);
  250. return uhid_write(_metadata, fd, &ev);
  251. }
  252. static int setup_uhid(struct __test_metadata *_metadata, int rand_nb)
  253. {
  254. int fd;
  255. const char *path = "/dev/uhid";
  256. int ret;
  257. fd = open(path, O_RDWR | O_CLOEXEC);
  258. ASSERT_GE(fd, 0) TH_LOG("open uhid-cdev failed; %d", fd);
  259. ret = uhid_create(_metadata, fd, rand_nb);
  260. ASSERT_EQ(0, ret) {
  261. TH_LOG("create uhid device failed: %d", ret);
  262. close(fd);
  263. }
  264. return fd;
  265. }
  266. static bool match_sysfs_device(int dev_id, const char *workdir, struct dirent *dir)
  267. {
  268. const char *target = "0003:0001:0A37.*";
  269. char phys[512];
  270. char uevent[1024];
  271. char temp[512];
  272. int fd, nread;
  273. bool found = false;
  274. if (fnmatch(target, dir->d_name, 0))
  275. return false;
  276. /* we found the correct VID/PID, now check for phys */
  277. sprintf(uevent, "%s/%s/uevent", workdir, dir->d_name);
  278. fd = open(uevent, O_RDONLY | O_NONBLOCK);
  279. if (fd < 0)
  280. return false;
  281. sprintf(phys, "PHYS=%d", dev_id);
  282. nread = read(fd, temp, ARRAY_SIZE(temp));
  283. if (nread > 0 && (strstr(temp, phys)) != NULL)
  284. found = true;
  285. close(fd);
  286. return found;
  287. }
  288. static int get_hid_id(int dev_id)
  289. {
  290. const char *workdir = "/sys/devices/virtual/misc/uhid";
  291. const char *str_id;
  292. DIR *d;
  293. struct dirent *dir;
  294. int found = -1, attempts = 3;
  295. /* it would be nice to be able to use nftw, but the no_alu32 target doesn't support it */
  296. while (found < 0 && attempts > 0) {
  297. attempts--;
  298. d = opendir(workdir);
  299. if (d) {
  300. while ((dir = readdir(d)) != NULL) {
  301. if (!match_sysfs_device(dev_id, workdir, dir))
  302. continue;
  303. str_id = dir->d_name + sizeof("0003:0001:0A37.");
  304. found = (int)strtol(str_id, NULL, 16);
  305. break;
  306. }
  307. closedir(d);
  308. }
  309. if (found < 0)
  310. usleep(100000);
  311. }
  312. return found;
  313. }
  314. static int get_hidraw(int dev_id)
  315. {
  316. const char *workdir = "/sys/devices/virtual/misc/uhid";
  317. char sysfs[1024];
  318. DIR *d, *subd;
  319. struct dirent *dir, *subdir;
  320. int i, found = -1;
  321. /* retry 5 times in case the system is loaded */
  322. for (i = 5; i > 0; i--) {
  323. usleep(10);
  324. d = opendir(workdir);
  325. if (!d)
  326. continue;
  327. while ((dir = readdir(d)) != NULL) {
  328. if (!match_sysfs_device(dev_id, workdir, dir))
  329. continue;
  330. sprintf(sysfs, "%s/%s/hidraw", workdir, dir->d_name);
  331. subd = opendir(sysfs);
  332. if (!subd)
  333. continue;
  334. while ((subdir = readdir(subd)) != NULL) {
  335. if (fnmatch("hidraw*", subdir->d_name, 0))
  336. continue;
  337. found = atoi(subdir->d_name + strlen("hidraw"));
  338. }
  339. closedir(subd);
  340. if (found > 0)
  341. break;
  342. }
  343. closedir(d);
  344. }
  345. return found;
  346. }
  347. static int open_hidraw(int dev_id)
  348. {
  349. int hidraw_number;
  350. char hidraw_path[64] = { 0 };
  351. hidraw_number = get_hidraw(dev_id);
  352. if (hidraw_number < 0)
  353. return hidraw_number;
  354. /* open hidraw node to check the other side of the pipe */
  355. sprintf(hidraw_path, "/dev/hidraw%d", hidraw_number);
  356. return open(hidraw_path, O_RDWR | O_NONBLOCK);
  357. }