gadget_hid.rst 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. ===========================
  2. Linux USB HID gadget driver
  3. ===========================
  4. Introduction
  5. ============
  6. The HID Gadget driver provides emulation of USB Human Interface
  7. Devices (HID). The basic HID handling is done in the kernel,
  8. and HID reports can be sent/received through I/O on the
  9. /dev/hidgX character devices.
  10. For more details about HID, see the developer page on
  11. https://www.usb.org/developers/hidpage/
  12. Configuration
  13. =============
  14. g_hid is a platform driver, so to use it you need to add
  15. struct platform_device(s) to your platform code defining the
  16. HID function descriptors you want to use - E.G. something
  17. like::
  18. #include <linux/platform_device.h>
  19. #include <linux/usb/g_hid.h>
  20. /* hid descriptor for a keyboard */
  21. static struct hidg_func_descriptor my_hid_data = {
  22. .subclass = 0, /* No subclass */
  23. .protocol = 1, /* Keyboard */
  24. .report_length = 8,
  25. .report_desc_length = 63,
  26. .report_desc = {
  27. 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
  28. 0x09, 0x06, /* USAGE (Keyboard) */
  29. 0xa1, 0x01, /* COLLECTION (Application) */
  30. 0x05, 0x07, /* USAGE_PAGE (Keyboard) */
  31. 0x19, 0xe0, /* USAGE_MINIMUM (Keyboard LeftControl) */
  32. 0x29, 0xe7, /* USAGE_MAXIMUM (Keyboard Right GUI) */
  33. 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
  34. 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
  35. 0x75, 0x01, /* REPORT_SIZE (1) */
  36. 0x95, 0x08, /* REPORT_COUNT (8) */
  37. 0x81, 0x02, /* INPUT (Data,Var,Abs) */
  38. 0x95, 0x01, /* REPORT_COUNT (1) */
  39. 0x75, 0x08, /* REPORT_SIZE (8) */
  40. 0x81, 0x03, /* INPUT (Cnst,Var,Abs) */
  41. 0x95, 0x05, /* REPORT_COUNT (5) */
  42. 0x75, 0x01, /* REPORT_SIZE (1) */
  43. 0x05, 0x08, /* USAGE_PAGE (LEDs) */
  44. 0x19, 0x01, /* USAGE_MINIMUM (Num Lock) */
  45. 0x29, 0x05, /* USAGE_MAXIMUM (Kana) */
  46. 0x91, 0x02, /* OUTPUT (Data,Var,Abs) */
  47. 0x95, 0x01, /* REPORT_COUNT (1) */
  48. 0x75, 0x03, /* REPORT_SIZE (3) */
  49. 0x91, 0x03, /* OUTPUT (Cnst,Var,Abs) */
  50. 0x95, 0x06, /* REPORT_COUNT (6) */
  51. 0x75, 0x08, /* REPORT_SIZE (8) */
  52. 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
  53. 0x25, 0x65, /* LOGICAL_MAXIMUM (101) */
  54. 0x05, 0x07, /* USAGE_PAGE (Keyboard) */
  55. 0x19, 0x00, /* USAGE_MINIMUM (Reserved) */
  56. 0x29, 0x65, /* USAGE_MAXIMUM (Keyboard Application) */
  57. 0x81, 0x00, /* INPUT (Data,Ary,Abs) */
  58. 0xc0 /* END_COLLECTION */
  59. }
  60. };
  61. static struct platform_device my_hid = {
  62. .name = "hidg",
  63. .id = 0,
  64. .num_resources = 0,
  65. .resource = 0,
  66. .dev.platform_data = &my_hid_data,
  67. };
  68. You can add as many HID functions as you want, only limited by
  69. the amount of interrupt endpoints your gadget driver supports.
  70. Configuration with configfs
  71. ===========================
  72. Instead of adding fake platform devices and drivers in order to pass
  73. some data to the kernel, if HID is a part of a gadget composed with
  74. configfs the hidg_func_descriptor.report_desc is passed to the kernel
  75. by writing the appropriate stream of bytes to a configfs attribute.
  76. Send and receive HID reports
  77. ============================
  78. HID reports can be sent/received using read/write on the
  79. /dev/hidgX character devices. See below for an example program
  80. to do this.
  81. hid_gadget_test is a small interactive program to test the HID
  82. gadget driver. To use, point it at a hidg device and set the
  83. device type (keyboard / mouse / joystick) - E.G.::
  84. # hid_gadget_test /dev/hidg0 keyboard
  85. You are now in the prompt of hid_gadget_test. You can type any
  86. combination of options and values. Available options and
  87. values are listed at program start. In keyboard mode you can
  88. send up to six values.
  89. For example type: g i s t r --left-shift
  90. Hit return and the corresponding report will be sent by the
  91. HID gadget.
  92. Another interesting example is the caps lock test. Type
  93. --caps-lock and hit return. A report is then sent by the
  94. gadget and you should receive the host answer, corresponding
  95. to the caps lock LED status::
  96. --caps-lock
  97. recv report:2
  98. With this command::
  99. # hid_gadget_test /dev/hidg1 mouse
  100. You can test the mouse emulation. Values are two signed numbers.
  101. Sample code::
  102. /* hid_gadget_test */
  103. #include <pthread.h>
  104. #include <string.h>
  105. #include <stdio.h>
  106. #include <ctype.h>
  107. #include <fcntl.h>
  108. #include <errno.h>
  109. #include <stdio.h>
  110. #include <stdlib.h>
  111. #include <unistd.h>
  112. #define BUF_LEN 512
  113. struct options {
  114. const char *opt;
  115. unsigned char val;
  116. };
  117. static struct options kmod[] = {
  118. {.opt = "--left-ctrl", .val = 0x01},
  119. {.opt = "--right-ctrl", .val = 0x10},
  120. {.opt = "--left-shift", .val = 0x02},
  121. {.opt = "--right-shift", .val = 0x20},
  122. {.opt = "--left-alt", .val = 0x04},
  123. {.opt = "--right-alt", .val = 0x40},
  124. {.opt = "--left-meta", .val = 0x08},
  125. {.opt = "--right-meta", .val = 0x80},
  126. {.opt = NULL}
  127. };
  128. static struct options kval[] = {
  129. {.opt = "--return", .val = 0x28},
  130. {.opt = "--esc", .val = 0x29},
  131. {.opt = "--bckspc", .val = 0x2a},
  132. {.opt = "--tab", .val = 0x2b},
  133. {.opt = "--spacebar", .val = 0x2c},
  134. {.opt = "--caps-lock", .val = 0x39},
  135. {.opt = "--f1", .val = 0x3a},
  136. {.opt = "--f2", .val = 0x3b},
  137. {.opt = "--f3", .val = 0x3c},
  138. {.opt = "--f4", .val = 0x3d},
  139. {.opt = "--f5", .val = 0x3e},
  140. {.opt = "--f6", .val = 0x3f},
  141. {.opt = "--f7", .val = 0x40},
  142. {.opt = "--f8", .val = 0x41},
  143. {.opt = "--f9", .val = 0x42},
  144. {.opt = "--f10", .val = 0x43},
  145. {.opt = "--f11", .val = 0x44},
  146. {.opt = "--f12", .val = 0x45},
  147. {.opt = "--insert", .val = 0x49},
  148. {.opt = "--home", .val = 0x4a},
  149. {.opt = "--pageup", .val = 0x4b},
  150. {.opt = "--del", .val = 0x4c},
  151. {.opt = "--end", .val = 0x4d},
  152. {.opt = "--pagedown", .val = 0x4e},
  153. {.opt = "--right", .val = 0x4f},
  154. {.opt = "--left", .val = 0x50},
  155. {.opt = "--down", .val = 0x51},
  156. {.opt = "--kp-enter", .val = 0x58},
  157. {.opt = "--up", .val = 0x52},
  158. {.opt = "--num-lock", .val = 0x53},
  159. {.opt = NULL}
  160. };
  161. int keyboard_fill_report(char report[8], char buf[BUF_LEN], int *hold)
  162. {
  163. char *tok = strtok(buf, " ");
  164. int key = 0;
  165. int i = 0;
  166. for (; tok != NULL; tok = strtok(NULL, " ")) {
  167. if (strcmp(tok, "--quit") == 0)
  168. return -1;
  169. if (strcmp(tok, "--hold") == 0) {
  170. *hold = 1;
  171. continue;
  172. }
  173. if (key < 6) {
  174. for (i = 0; kval[i].opt != NULL; i++)
  175. if (strcmp(tok, kval[i].opt) == 0) {
  176. report[2 + key++] = kval[i].val;
  177. break;
  178. }
  179. if (kval[i].opt != NULL)
  180. continue;
  181. }
  182. if (key < 6)
  183. if (islower(tok[0])) {
  184. report[2 + key++] = (tok[0] - ('a' - 0x04));
  185. continue;
  186. }
  187. for (i = 0; kmod[i].opt != NULL; i++)
  188. if (strcmp(tok, kmod[i].opt) == 0) {
  189. report[0] = report[0] | kmod[i].val;
  190. break;
  191. }
  192. if (kmod[i].opt != NULL)
  193. continue;
  194. if (key < 6)
  195. fprintf(stderr, "unknown option: %s\n", tok);
  196. }
  197. return 8;
  198. }
  199. static struct options mmod[] = {
  200. {.opt = "--b1", .val = 0x01},
  201. {.opt = "--b2", .val = 0x02},
  202. {.opt = "--b3", .val = 0x04},
  203. {.opt = NULL}
  204. };
  205. int mouse_fill_report(char report[8], char buf[BUF_LEN], int *hold)
  206. {
  207. char *tok = strtok(buf, " ");
  208. int mvt = 0;
  209. int i = 0;
  210. for (; tok != NULL; tok = strtok(NULL, " ")) {
  211. if (strcmp(tok, "--quit") == 0)
  212. return -1;
  213. if (strcmp(tok, "--hold") == 0) {
  214. *hold = 1;
  215. continue;
  216. }
  217. for (i = 0; mmod[i].opt != NULL; i++)
  218. if (strcmp(tok, mmod[i].opt) == 0) {
  219. report[0] = report[0] | mmod[i].val;
  220. break;
  221. }
  222. if (mmod[i].opt != NULL)
  223. continue;
  224. if (!(tok[0] == '-' && tok[1] == '-') && mvt < 2) {
  225. errno = 0;
  226. report[1 + mvt++] = (char)strtol(tok, NULL, 0);
  227. if (errno != 0) {
  228. fprintf(stderr, "Bad value:'%s'\n", tok);
  229. report[1 + mvt--] = 0;
  230. }
  231. continue;
  232. }
  233. fprintf(stderr, "unknown option: %s\n", tok);
  234. }
  235. return 3;
  236. }
  237. static struct options jmod[] = {
  238. {.opt = "--b1", .val = 0x10},
  239. {.opt = "--b2", .val = 0x20},
  240. {.opt = "--b3", .val = 0x40},
  241. {.opt = "--b4", .val = 0x80},
  242. {.opt = "--hat1", .val = 0x00},
  243. {.opt = "--hat2", .val = 0x01},
  244. {.opt = "--hat3", .val = 0x02},
  245. {.opt = "--hat4", .val = 0x03},
  246. {.opt = "--hatneutral", .val = 0x04},
  247. {.opt = NULL}
  248. };
  249. int joystick_fill_report(char report[8], char buf[BUF_LEN], int *hold)
  250. {
  251. char *tok = strtok(buf, " ");
  252. int mvt = 0;
  253. int i = 0;
  254. *hold = 1;
  255. /* set default hat position: neutral */
  256. report[3] = 0x04;
  257. for (; tok != NULL; tok = strtok(NULL, " ")) {
  258. if (strcmp(tok, "--quit") == 0)
  259. return -1;
  260. for (i = 0; jmod[i].opt != NULL; i++)
  261. if (strcmp(tok, jmod[i].opt) == 0) {
  262. report[3] = (report[3] & 0xF0) | jmod[i].val;
  263. break;
  264. }
  265. if (jmod[i].opt != NULL)
  266. continue;
  267. if (!(tok[0] == '-' && tok[1] == '-') && mvt < 3) {
  268. errno = 0;
  269. report[mvt++] = (char)strtol(tok, NULL, 0);
  270. if (errno != 0) {
  271. fprintf(stderr, "Bad value:'%s'\n", tok);
  272. report[mvt--] = 0;
  273. }
  274. continue;
  275. }
  276. fprintf(stderr, "unknown option: %s\n", tok);
  277. }
  278. return 4;
  279. }
  280. void print_options(char c)
  281. {
  282. int i = 0;
  283. if (c == 'k') {
  284. printf(" keyboard options:\n"
  285. " --hold\n");
  286. for (i = 0; kmod[i].opt != NULL; i++)
  287. printf("\t\t%s\n", kmod[i].opt);
  288. printf("\n keyboard values:\n"
  289. " [a-z] or\n");
  290. for (i = 0; kval[i].opt != NULL; i++)
  291. printf("\t\t%-8s%s", kval[i].opt, i % 2 ? "\n" : "");
  292. printf("\n");
  293. } else if (c == 'm') {
  294. printf(" mouse options:\n"
  295. " --hold\n");
  296. for (i = 0; mmod[i].opt != NULL; i++)
  297. printf("\t\t%s\n", mmod[i].opt);
  298. printf("\n mouse values:\n"
  299. " Two signed numbers\n"
  300. "--quit to close\n");
  301. } else {
  302. printf(" joystick options:\n");
  303. for (i = 0; jmod[i].opt != NULL; i++)
  304. printf("\t\t%s\n", jmod[i].opt);
  305. printf("\n joystick values:\n"
  306. " three signed numbers\n"
  307. "--quit to close\n");
  308. }
  309. }
  310. int main(int argc, const char *argv[])
  311. {
  312. const char *filename = NULL;
  313. int fd = 0;
  314. char buf[BUF_LEN];
  315. int cmd_len;
  316. char report[8];
  317. int to_send = 8;
  318. int hold = 0;
  319. fd_set rfds;
  320. int retval, i;
  321. if (argc < 3) {
  322. fprintf(stderr, "Usage: %s devname mouse|keyboard|joystick\n",
  323. argv[0]);
  324. return 1;
  325. }
  326. if (argv[2][0] != 'k' && argv[2][0] != 'm' && argv[2][0] != 'j')
  327. return 2;
  328. filename = argv[1];
  329. if ((fd = open(filename, O_RDWR, 0666)) == -1) {
  330. perror(filename);
  331. return 3;
  332. }
  333. print_options(argv[2][0]);
  334. while (42) {
  335. FD_ZERO(&rfds);
  336. FD_SET(STDIN_FILENO, &rfds);
  337. FD_SET(fd, &rfds);
  338. retval = select(fd + 1, &rfds, NULL, NULL, NULL);
  339. if (retval == -1 && errno == EINTR)
  340. continue;
  341. if (retval < 0) {
  342. perror("select()");
  343. return 4;
  344. }
  345. if (FD_ISSET(fd, &rfds)) {
  346. cmd_len = read(fd, buf, BUF_LEN - 1);
  347. printf("recv report:");
  348. for (i = 0; i < cmd_len; i++)
  349. printf(" %02x", buf[i]);
  350. printf("\n");
  351. }
  352. if (FD_ISSET(STDIN_FILENO, &rfds)) {
  353. memset(report, 0x0, sizeof(report));
  354. cmd_len = read(STDIN_FILENO, buf, BUF_LEN - 1);
  355. if (cmd_len == 0)
  356. break;
  357. buf[cmd_len - 1] = '\0';
  358. hold = 0;
  359. memset(report, 0x0, sizeof(report));
  360. if (argv[2][0] == 'k')
  361. to_send = keyboard_fill_report(report, buf, &hold);
  362. else if (argv[2][0] == 'm')
  363. to_send = mouse_fill_report(report, buf, &hold);
  364. else
  365. to_send = joystick_fill_report(report, buf, &hold);
  366. if (to_send == -1)
  367. break;
  368. if (write(fd, report, to_send) != to_send) {
  369. perror(filename);
  370. return 5;
  371. }
  372. if (!hold) {
  373. memset(report, 0x0, sizeof(report));
  374. if (write(fd, report, to_send) != to_send) {
  375. perror(filename);
  376. return 6;
  377. }
  378. }
  379. }
  380. }
  381. close(fd);
  382. return 0;
  383. }