hid_common.h 12 KB

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