gadget_hid.txt 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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. Configuration with configfs
  67. Instead of adding fake platform devices and drivers in order to pass
  68. some data to the kernel, if HID is a part of a gadget composed with
  69. configfs the hidg_func_descriptor.report_desc is passed to the kernel
  70. by writing the appropriate stream of bytes to a configfs attribute.
  71. Send and receive HID reports
  72. HID reports can be sent/received using read/write on the
  73. /dev/hidgX character devices. See below for an example program
  74. to do this.
  75. hid_gadget_test is a small interactive program to test the HID
  76. gadget driver. To use, point it at a hidg device and set the
  77. device type (keyboard / mouse / joystick) - E.G.:
  78. # hid_gadget_test /dev/hidg0 keyboard
  79. You are now in the prompt of hid_gadget_test. You can type any
  80. combination of options and values. Available options and
  81. values are listed at program start. In keyboard mode you can
  82. send up to six values.
  83. For example type: g i s t r --left-shift
  84. Hit return and the corresponding report will be sent by the
  85. HID gadget.
  86. Another interesting example is the caps lock test. Type
  87. --caps-lock and hit return. A report is then sent by the
  88. gadget and you should receive the host answer, corresponding
  89. to the caps lock LED status.
  90. --caps-lock
  91. recv report:2
  92. With this command:
  93. # hid_gadget_test /dev/hidg1 mouse
  94. You can test the mouse emulation. Values are two signed numbers.
  95. Sample code
  96. /* hid_gadget_test */
  97. #include <pthread.h>
  98. #include <string.h>
  99. #include <stdio.h>
  100. #include <ctype.h>
  101. #include <fcntl.h>
  102. #include <errno.h>
  103. #include <stdio.h>
  104. #include <stdlib.h>
  105. #include <unistd.h>
  106. #define BUF_LEN 512
  107. struct options {
  108. const char *opt;
  109. unsigned char val;
  110. };
  111. static struct options kmod[] = {
  112. {.opt = "--left-ctrl", .val = 0x01},
  113. {.opt = "--right-ctrl", .val = 0x10},
  114. {.opt = "--left-shift", .val = 0x02},
  115. {.opt = "--right-shift", .val = 0x20},
  116. {.opt = "--left-alt", .val = 0x04},
  117. {.opt = "--right-alt", .val = 0x40},
  118. {.opt = "--left-meta", .val = 0x08},
  119. {.opt = "--right-meta", .val = 0x80},
  120. {.opt = NULL}
  121. };
  122. static struct options kval[] = {
  123. {.opt = "--return", .val = 0x28},
  124. {.opt = "--esc", .val = 0x29},
  125. {.opt = "--bckspc", .val = 0x2a},
  126. {.opt = "--tab", .val = 0x2b},
  127. {.opt = "--spacebar", .val = 0x2c},
  128. {.opt = "--caps-lock", .val = 0x39},
  129. {.opt = "--f1", .val = 0x3a},
  130. {.opt = "--f2", .val = 0x3b},
  131. {.opt = "--f3", .val = 0x3c},
  132. {.opt = "--f4", .val = 0x3d},
  133. {.opt = "--f5", .val = 0x3e},
  134. {.opt = "--f6", .val = 0x3f},
  135. {.opt = "--f7", .val = 0x40},
  136. {.opt = "--f8", .val = 0x41},
  137. {.opt = "--f9", .val = 0x42},
  138. {.opt = "--f10", .val = 0x43},
  139. {.opt = "--f11", .val = 0x44},
  140. {.opt = "--f12", .val = 0x45},
  141. {.opt = "--insert", .val = 0x49},
  142. {.opt = "--home", .val = 0x4a},
  143. {.opt = "--pageup", .val = 0x4b},
  144. {.opt = "--del", .val = 0x4c},
  145. {.opt = "--end", .val = 0x4d},
  146. {.opt = "--pagedown", .val = 0x4e},
  147. {.opt = "--right", .val = 0x4f},
  148. {.opt = "--left", .val = 0x50},
  149. {.opt = "--down", .val = 0x51},
  150. {.opt = "--kp-enter", .val = 0x58},
  151. {.opt = "--up", .val = 0x52},
  152. {.opt = "--num-lock", .val = 0x53},
  153. {.opt = NULL}
  154. };
  155. int keyboard_fill_report(char report[8], char buf[BUF_LEN], int *hold)
  156. {
  157. char *tok = strtok(buf, " ");
  158. int key = 0;
  159. int i = 0;
  160. for (; tok != NULL; tok = strtok(NULL, " ")) {
  161. if (strcmp(tok, "--quit") == 0)
  162. return -1;
  163. if (strcmp(tok, "--hold") == 0) {
  164. *hold = 1;
  165. continue;
  166. }
  167. if (key < 6) {
  168. for (i = 0; kval[i].opt != NULL; i++)
  169. if (strcmp(tok, kval[i].opt) == 0) {
  170. report[2 + key++] = kval[i].val;
  171. break;
  172. }
  173. if (kval[i].opt != NULL)
  174. continue;
  175. }
  176. if (key < 6)
  177. if (islower(tok[0])) {
  178. report[2 + key++] = (tok[0] - ('a' - 0x04));
  179. continue;
  180. }
  181. for (i = 0; kmod[i].opt != NULL; i++)
  182. if (strcmp(tok, kmod[i].opt) == 0) {
  183. report[0] = report[0] | kmod[i].val;
  184. break;
  185. }
  186. if (kmod[i].opt != NULL)
  187. continue;
  188. if (key < 6)
  189. fprintf(stderr, "unknown option: %s\n", tok);
  190. }
  191. return 8;
  192. }
  193. static struct options mmod[] = {
  194. {.opt = "--b1", .val = 0x01},
  195. {.opt = "--b2", .val = 0x02},
  196. {.opt = "--b3", .val = 0x04},
  197. {.opt = NULL}
  198. };
  199. int mouse_fill_report(char report[8], char buf[BUF_LEN], int *hold)
  200. {
  201. char *tok = strtok(buf, " ");
  202. int mvt = 0;
  203. int i = 0;
  204. for (; tok != NULL; tok = strtok(NULL, " ")) {
  205. if (strcmp(tok, "--quit") == 0)
  206. return -1;
  207. if (strcmp(tok, "--hold") == 0) {
  208. *hold = 1;
  209. continue;
  210. }
  211. for (i = 0; mmod[i].opt != NULL; i++)
  212. if (strcmp(tok, mmod[i].opt) == 0) {
  213. report[0] = report[0] | mmod[i].val;
  214. break;
  215. }
  216. if (mmod[i].opt != NULL)
  217. continue;
  218. if (!(tok[0] == '-' && tok[1] == '-') && mvt < 2) {
  219. errno = 0;
  220. report[1 + mvt++] = (char)strtol(tok, NULL, 0);
  221. if (errno != 0) {
  222. fprintf(stderr, "Bad value:'%s'\n", tok);
  223. report[1 + mvt--] = 0;
  224. }
  225. continue;
  226. }
  227. fprintf(stderr, "unknown option: %s\n", tok);
  228. }
  229. return 3;
  230. }
  231. static struct options jmod[] = {
  232. {.opt = "--b1", .val = 0x10},
  233. {.opt = "--b2", .val = 0x20},
  234. {.opt = "--b3", .val = 0x40},
  235. {.opt = "--b4", .val = 0x80},
  236. {.opt = "--hat1", .val = 0x00},
  237. {.opt = "--hat2", .val = 0x01},
  238. {.opt = "--hat3", .val = 0x02},
  239. {.opt = "--hat4", .val = 0x03},
  240. {.opt = "--hatneutral", .val = 0x04},
  241. {.opt = NULL}
  242. };
  243. int joystick_fill_report(char report[8], char buf[BUF_LEN], int *hold)
  244. {
  245. char *tok = strtok(buf, " ");
  246. int mvt = 0;
  247. int i = 0;
  248. *hold = 1;
  249. /* set default hat position: neutral */
  250. report[3] = 0x04;
  251. for (; tok != NULL; tok = strtok(NULL, " ")) {
  252. if (strcmp(tok, "--quit") == 0)
  253. return -1;
  254. for (i = 0; jmod[i].opt != NULL; i++)
  255. if (strcmp(tok, jmod[i].opt) == 0) {
  256. report[3] = (report[3] & 0xF0) | jmod[i].val;
  257. break;
  258. }
  259. if (jmod[i].opt != NULL)
  260. continue;
  261. if (!(tok[0] == '-' && tok[1] == '-') && mvt < 3) {
  262. errno = 0;
  263. report[mvt++] = (char)strtol(tok, NULL, 0);
  264. if (errno != 0) {
  265. fprintf(stderr, "Bad value:'%s'\n", tok);
  266. report[mvt--] = 0;
  267. }
  268. continue;
  269. }
  270. fprintf(stderr, "unknown option: %s\n", tok);
  271. }
  272. return 4;
  273. }
  274. void print_options(char c)
  275. {
  276. int i = 0;
  277. if (c == 'k') {
  278. printf(" keyboard options:\n"
  279. " --hold\n");
  280. for (i = 0; kmod[i].opt != NULL; i++)
  281. printf("\t\t%s\n", kmod[i].opt);
  282. printf("\n keyboard values:\n"
  283. " [a-z] or\n");
  284. for (i = 0; kval[i].opt != NULL; i++)
  285. printf("\t\t%-8s%s", kval[i].opt, i % 2 ? "\n" : "");
  286. printf("\n");
  287. } else if (c == 'm') {
  288. printf(" mouse options:\n"
  289. " --hold\n");
  290. for (i = 0; mmod[i].opt != NULL; i++)
  291. printf("\t\t%s\n", mmod[i].opt);
  292. printf("\n mouse values:\n"
  293. " Two signed numbers\n"
  294. "--quit to close\n");
  295. } else {
  296. printf(" joystick options:\n");
  297. for (i = 0; jmod[i].opt != NULL; i++)
  298. printf("\t\t%s\n", jmod[i].opt);
  299. printf("\n joystick values:\n"
  300. " three signed numbers\n"
  301. "--quit to close\n");
  302. }
  303. }
  304. int main(int argc, const char *argv[])
  305. {
  306. const char *filename = NULL;
  307. int fd = 0;
  308. char buf[BUF_LEN];
  309. int cmd_len;
  310. char report[8];
  311. int to_send = 8;
  312. int hold = 0;
  313. fd_set rfds;
  314. int retval, i;
  315. if (argc < 3) {
  316. fprintf(stderr, "Usage: %s devname mouse|keyboard|joystick\n",
  317. argv[0]);
  318. return 1;
  319. }
  320. if (argv[2][0] != 'k' && argv[2][0] != 'm' && argv[2][0] != 'j')
  321. return 2;
  322. filename = argv[1];
  323. if ((fd = open(filename, O_RDWR, 0666)) == -1) {
  324. perror(filename);
  325. return 3;
  326. }
  327. print_options(argv[2][0]);
  328. while (42) {
  329. FD_ZERO(&rfds);
  330. FD_SET(STDIN_FILENO, &rfds);
  331. FD_SET(fd, &rfds);
  332. retval = select(fd + 1, &rfds, NULL, NULL, NULL);
  333. if (retval == -1 && errno == EINTR)
  334. continue;
  335. if (retval < 0) {
  336. perror("select()");
  337. return 4;
  338. }
  339. if (FD_ISSET(fd, &rfds)) {
  340. cmd_len = read(fd, buf, BUF_LEN - 1);
  341. printf("recv report:");
  342. for (i = 0; i < cmd_len; i++)
  343. printf(" %02x", buf[i]);
  344. printf("\n");
  345. }
  346. if (FD_ISSET(STDIN_FILENO, &rfds)) {
  347. memset(report, 0x0, sizeof(report));
  348. cmd_len = read(STDIN_FILENO, buf, BUF_LEN - 1);
  349. if (cmd_len == 0)
  350. break;
  351. buf[cmd_len - 1] = '\0';
  352. hold = 0;
  353. memset(report, 0x0, sizeof(report));
  354. if (argv[2][0] == 'k')
  355. to_send = keyboard_fill_report(report, buf, &hold);
  356. else if (argv[2][0] == 'm')
  357. to_send = mouse_fill_report(report, buf, &hold);
  358. else
  359. to_send = joystick_fill_report(report, buf, &hold);
  360. if (to_send == -1)
  361. break;
  362. if (write(fd, report, to_send) != to_send) {
  363. perror(filename);
  364. return 5;
  365. }
  366. if (!hold) {
  367. memset(report, 0x0, sizeof(report));
  368. if (write(fd, report, to_send) != to_send) {
  369. perror(filename);
  370. return 6;
  371. }
  372. }
  373. }
  374. }
  375. close(fd);
  376. return 0;
  377. }