gadget_hid.txt 11 KB

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