Commit 71adf118946957839a13aa4d1094183e05c6c094
Committed by
Greg Kroah-Hartman
1 parent
e49bbce133
Exists in
master
and in
4 other branches
USB: gadget: add HID gadget driver
g_hid is a USB gadget driver implementing the Human Interface Device class specification. The driver handles basic HID protocol handling in the kernel, and allows userspace to read/write HID reports trough /dev/hidgX character devices. Signed-off-by: Fabien Chouteau <fabien.chouteau@barco.com> Signed-off-by: Peter Korsgaard <peter.korsgaard@barco.com> Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
Showing 6 changed files with 1451 additions and 0 deletions Side-by-side Diff
Documentation/usb/gadget_hid.txt
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 | + | |
11 | + For more details about HID, see the developer page on | |
12 | + http://www.usb.org/developers/hidpage/ | |
13 | + | |
14 | +Configuration | |
15 | + | |
16 | + g_hid is a platform driver, so to use it you need to add | |
17 | + struct platform_device(s) to your platform code defining the | |
18 | + HID function descriptors you want to use - E.G. something | |
19 | + like: | |
20 | + | |
21 | +#include <linux/platform_device.h> | |
22 | +#include <linux/usb/g_hid.h> | |
23 | + | |
24 | +/* hid descriptor for a keyboard */ | |
25 | +static struct hidg_func_descriptor my_hid_data = { | |
26 | + .subclass = 0, /* No subclass */ | |
27 | + .protocol = 1, /* Keyboard */ | |
28 | + .report_length = 8, | |
29 | + .report_desc_length = 63, | |
30 | + .report_desc = { | |
31 | + 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */ | |
32 | + 0x09, 0x06, /* USAGE (Keyboard) */ | |
33 | + 0xa1, 0x01, /* COLLECTION (Application) */ | |
34 | + 0x05, 0x07, /* USAGE_PAGE (Keyboard) */ | |
35 | + 0x19, 0xe0, /* USAGE_MINIMUM (Keyboard LeftControl) */ | |
36 | + 0x29, 0xe7, /* USAGE_MAXIMUM (Keyboard Right GUI) */ | |
37 | + 0x15, 0x00, /* LOGICAL_MINIMUM (0) */ | |
38 | + 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */ | |
39 | + 0x75, 0x01, /* REPORT_SIZE (1) */ | |
40 | + 0x95, 0x08, /* REPORT_COUNT (8) */ | |
41 | + 0x81, 0x02, /* INPUT (Data,Var,Abs) */ | |
42 | + 0x95, 0x01, /* REPORT_COUNT (1) */ | |
43 | + 0x75, 0x08, /* REPORT_SIZE (8) */ | |
44 | + 0x81, 0x03, /* INPUT (Cnst,Var,Abs) */ | |
45 | + 0x95, 0x05, /* REPORT_COUNT (5) */ | |
46 | + 0x75, 0x01, /* REPORT_SIZE (1) */ | |
47 | + 0x05, 0x08, /* USAGE_PAGE (LEDs) */ | |
48 | + 0x19, 0x01, /* USAGE_MINIMUM (Num Lock) */ | |
49 | + 0x29, 0x05, /* USAGE_MAXIMUM (Kana) */ | |
50 | + 0x91, 0x02, /* OUTPUT (Data,Var,Abs) */ | |
51 | + 0x95, 0x01, /* REPORT_COUNT (1) */ | |
52 | + 0x75, 0x03, /* REPORT_SIZE (3) */ | |
53 | + 0x91, 0x03, /* OUTPUT (Cnst,Var,Abs) */ | |
54 | + 0x95, 0x06, /* REPORT_COUNT (6) */ | |
55 | + 0x75, 0x08, /* REPORT_SIZE (8) */ | |
56 | + 0x15, 0x00, /* LOGICAL_MINIMUM (0) */ | |
57 | + 0x25, 0x65, /* LOGICAL_MAXIMUM (101) */ | |
58 | + 0x05, 0x07, /* USAGE_PAGE (Keyboard) */ | |
59 | + 0x19, 0x00, /* USAGE_MINIMUM (Reserved) */ | |
60 | + 0x29, 0x65, /* USAGE_MAXIMUM (Keyboard Application) */ | |
61 | + 0x81, 0x00, /* INPUT (Data,Ary,Abs) */ | |
62 | + 0xc0 /* END_COLLECTION */ | |
63 | + } | |
64 | +}; | |
65 | + | |
66 | +static struct platform_device my_hid = { | |
67 | + .name = "hidg", | |
68 | + .id = 0, | |
69 | + .num_resources = 0, | |
70 | + .resource = 0, | |
71 | + .dev.platform_data = &my_hid_data, | |
72 | +}; | |
73 | + | |
74 | + You can add as many HID functions as you want, only limited by | |
75 | + the amount of interrupt endpoints your gadget driver supports. | |
76 | + | |
77 | +Send and receive HID reports | |
78 | + | |
79 | + HID reports can be sent/received using read/write on the | |
80 | + /dev/hidgX character devices. See below for an example program | |
81 | + to do this. | |
82 | + | |
83 | + hid_gadget_test is a small interactive program to test the HID | |
84 | + gadget driver. To use, point it at a hidg device and set the | |
85 | + device type (keyboard / mouse / joystick) - E.G.: | |
86 | + | |
87 | + # hid_gadget_test /dev/hidg0 keyboard | |
88 | + | |
89 | + You are now in the prompt of hid_gadget_test. You can type any | |
90 | + combination of options and values. Available options and | |
91 | + values are listed at program start. In keyboard mode you can | |
92 | + send up to six values. | |
93 | + | |
94 | + For example type: g i s t r --left-shift | |
95 | + | |
96 | + Hit return and the corresponding report will be sent by the | |
97 | + HID gadget. | |
98 | + | |
99 | + Another interesting example is the caps lock test. Type | |
100 | + -โcaps-lock and hit return. A report is then sent by the | |
101 | + gadget and you should receive the host answer, corresponding | |
102 | + to the caps lock LED status. | |
103 | + | |
104 | + --caps-lock | |
105 | + recv report:2 | |
106 | + | |
107 | + With this command: | |
108 | + | |
109 | + # hid_gadget_test /dev/hidg1 mouse | |
110 | + | |
111 | + You can test the mouse emulation. Values are two signed numbers. | |
112 | + | |
113 | + | |
114 | +Sample code | |
115 | + | |
116 | +/* hid_gadget_test */ | |
117 | + | |
118 | +#include <pthread.h> | |
119 | +#include <string.h> | |
120 | +#include <stdio.h> | |
121 | +#include <ctype.h> | |
122 | +#include <fcntl.h> | |
123 | +#include <errno.h> | |
124 | +#include <stdio.h> | |
125 | +#include <stdlib.h> | |
126 | +#include <unistd.h> | |
127 | + | |
128 | +#define BUF_LEN 512 | |
129 | + | |
130 | +struct options { | |
131 | + const char *opt; | |
132 | + unsigned char val; | |
133 | +}; | |
134 | + | |
135 | +static struct options kmod[] = { | |
136 | + {.opt = "--left-ctrl", .val = 0x01}, | |
137 | + {.opt = "--right-ctrl", .val = 0x10}, | |
138 | + {.opt = "--left-shift", .val = 0x02}, | |
139 | + {.opt = "--right-shift", .val = 0x20}, | |
140 | + {.opt = "--left-alt", .val = 0x04}, | |
141 | + {.opt = "--right-alt", .val = 0x40}, | |
142 | + {.opt = "--left-meta", .val = 0x08}, | |
143 | + {.opt = "--right-meta", .val = 0x80}, | |
144 | + {.opt = NULL} | |
145 | +}; | |
146 | + | |
147 | +static struct options kval[] = { | |
148 | + {.opt = "--return", .val = 0x28}, | |
149 | + {.opt = "--esc", .val = 0x29}, | |
150 | + {.opt = "--bckspc", .val = 0x2a}, | |
151 | + {.opt = "--tab", .val = 0x2b}, | |
152 | + {.opt = "--spacebar", .val = 0x2c}, | |
153 | + {.opt = "--caps-lock", .val = 0x39}, | |
154 | + {.opt = "--f1", .val = 0x3a}, | |
155 | + {.opt = "--f2", .val = 0x3b}, | |
156 | + {.opt = "--f3", .val = 0x3c}, | |
157 | + {.opt = "--f4", .val = 0x3d}, | |
158 | + {.opt = "--f5", .val = 0x3e}, | |
159 | + {.opt = "--f6", .val = 0x3f}, | |
160 | + {.opt = "--f7", .val = 0x40}, | |
161 | + {.opt = "--f8", .val = 0x41}, | |
162 | + {.opt = "--f9", .val = 0x42}, | |
163 | + {.opt = "--f10", .val = 0x43}, | |
164 | + {.opt = "--f11", .val = 0x44}, | |
165 | + {.opt = "--f12", .val = 0x45}, | |
166 | + {.opt = "--insert", .val = 0x49}, | |
167 | + {.opt = "--home", .val = 0x4a}, | |
168 | + {.opt = "--pageup", .val = 0x4b}, | |
169 | + {.opt = "--del", .val = 0x4c}, | |
170 | + {.opt = "--end", .val = 0x4d}, | |
171 | + {.opt = "--pagedown", .val = 0x4e}, | |
172 | + {.opt = "--right", .val = 0x4f}, | |
173 | + {.opt = "--left", .val = 0x50}, | |
174 | + {.opt = "--down", .val = 0x51}, | |
175 | + {.opt = "--kp-enter", .val = 0x58}, | |
176 | + {.opt = "--up", .val = 0x52}, | |
177 | + {.opt = "--num-lock", .val = 0x53}, | |
178 | + {.opt = NULL} | |
179 | +}; | |
180 | + | |
181 | +int keyboard_fill_report(char report[8], char buf[BUF_LEN], int *hold) | |
182 | +{ | |
183 | + char *tok = strtok(buf, " "); | |
184 | + int key = 0; | |
185 | + int i = 0; | |
186 | + | |
187 | + for (; tok != NULL; tok = strtok(NULL, " ")) { | |
188 | + | |
189 | + if (strcmp(tok, "--quit") == 0) | |
190 | + return -1; | |
191 | + | |
192 | + if (strcmp(tok, "--hold") == 0) { | |
193 | + *hold = 1; | |
194 | + continue; | |
195 | + } | |
196 | + | |
197 | + if (key < 6) { | |
198 | + for (i = 0; kval[i].opt != NULL; i++) | |
199 | + if (strcmp(tok, kval[i].opt) == 0) { | |
200 | + report[2 + key++] = kval[i].val; | |
201 | + break; | |
202 | + } | |
203 | + if (kval[i].opt != NULL) | |
204 | + continue; | |
205 | + } | |
206 | + | |
207 | + if (key < 6) | |
208 | + if (islower(tok[0])) { | |
209 | + report[2 + key++] = (tok[0] - ('a' - 0x04)); | |
210 | + continue; | |
211 | + } | |
212 | + | |
213 | + for (i = 0; kmod[i].opt != NULL; i++) | |
214 | + if (strcmp(tok, kmod[i].opt) == 0) { | |
215 | + report[0] = report[0] | kmod[i].val; | |
216 | + break; | |
217 | + } | |
218 | + if (kmod[i].opt != NULL) | |
219 | + continue; | |
220 | + | |
221 | + if (key < 6) | |
222 | + fprintf(stderr, "unknown option: %s\n", tok); | |
223 | + } | |
224 | + return 8; | |
225 | +} | |
226 | + | |
227 | +static struct options mmod[] = { | |
228 | + {.opt = "--b1", .val = 0x01}, | |
229 | + {.opt = "--b2", .val = 0x02}, | |
230 | + {.opt = "--b3", .val = 0x04}, | |
231 | + {.opt = NULL} | |
232 | +}; | |
233 | + | |
234 | +int mouse_fill_report(char report[8], char buf[BUF_LEN], int *hold) | |
235 | +{ | |
236 | + char *tok = strtok(buf, " "); | |
237 | + int mvt = 0; | |
238 | + int i = 0; | |
239 | + for (; tok != NULL; tok = strtok(NULL, " ")) { | |
240 | + | |
241 | + if (strcmp(tok, "--quit") == 0) | |
242 | + return -1; | |
243 | + | |
244 | + if (strcmp(tok, "--hold") == 0) { | |
245 | + *hold = 1; | |
246 | + continue; | |
247 | + } | |
248 | + | |
249 | + for (i = 0; mmod[i].opt != NULL; i++) | |
250 | + if (strcmp(tok, mmod[i].opt) == 0) { | |
251 | + report[0] = report[0] | mmod[i].val; | |
252 | + break; | |
253 | + } | |
254 | + if (mmod[i].opt != NULL) | |
255 | + continue; | |
256 | + | |
257 | + if (!(tok[0] == '-' && tok[1] == '-') && mvt < 2) { | |
258 | + errno = 0; | |
259 | + report[1 + mvt++] = (char)strtol(tok, NULL, 0); | |
260 | + if (errno != 0) { | |
261 | + fprintf(stderr, "Bad value:'%s'\n", tok); | |
262 | + report[1 + mvt--] = 0; | |
263 | + } | |
264 | + continue; | |
265 | + } | |
266 | + | |
267 | + fprintf(stderr, "unknown option: %s\n", tok); | |
268 | + } | |
269 | + return 3; | |
270 | +} | |
271 | + | |
272 | +static struct options jmod[] = { | |
273 | + {.opt = "--b1", .val = 0x10}, | |
274 | + {.opt = "--b2", .val = 0x20}, | |
275 | + {.opt = "--b3", .val = 0x40}, | |
276 | + {.opt = "--b4", .val = 0x80}, | |
277 | + {.opt = "--hat1", .val = 0x00}, | |
278 | + {.opt = "--hat2", .val = 0x01}, | |
279 | + {.opt = "--hat3", .val = 0x02}, | |
280 | + {.opt = "--hat4", .val = 0x03}, | |
281 | + {.opt = "--hatneutral", .val = 0x04}, | |
282 | + {.opt = NULL} | |
283 | +}; | |
284 | + | |
285 | +int joystick_fill_report(char report[8], char buf[BUF_LEN], int *hold) | |
286 | +{ | |
287 | + char *tok = strtok(buf, " "); | |
288 | + int mvt = 0; | |
289 | + int i = 0; | |
290 | + | |
291 | + *hold = 1; | |
292 | + | |
293 | + /* set default hat position: neutral */ | |
294 | + report[3] = 0x04; | |
295 | + | |
296 | + for (; tok != NULL; tok = strtok(NULL, " ")) { | |
297 | + | |
298 | + if (strcmp(tok, "--quit") == 0) | |
299 | + return -1; | |
300 | + | |
301 | + for (i = 0; jmod[i].opt != NULL; i++) | |
302 | + if (strcmp(tok, jmod[i].opt) == 0) { | |
303 | + report[3] = (report[3] & 0xF0) | jmod[i].val; | |
304 | + break; | |
305 | + } | |
306 | + if (jmod[i].opt != NULL) | |
307 | + continue; | |
308 | + | |
309 | + if (!(tok[0] == '-' && tok[1] == '-') && mvt < 3) { | |
310 | + errno = 0; | |
311 | + report[mvt++] = (char)strtol(tok, NULL, 0); | |
312 | + if (errno != 0) { | |
313 | + fprintf(stderr, "Bad value:'%s'\n", tok); | |
314 | + report[mvt--] = 0; | |
315 | + } | |
316 | + continue; | |
317 | + } | |
318 | + | |
319 | + fprintf(stderr, "unknown option: %s\n", tok); | |
320 | + } | |
321 | + return 4; | |
322 | +} | |
323 | + | |
324 | +void print_options(char c) | |
325 | +{ | |
326 | + int i = 0; | |
327 | + | |
328 | + if (c == 'k') { | |
329 | + printf(" keyboard options:\n" | |
330 | + " --hold\n"); | |
331 | + for (i = 0; kmod[i].opt != NULL; i++) | |
332 | + printf("\t\t%s\n", kmod[i].opt); | |
333 | + printf("\n keyboard values:\n" | |
334 | + " [a-z] or\n"); | |
335 | + for (i = 0; kval[i].opt != NULL; i++) | |
336 | + printf("\t\t%-8s%s", kval[i].opt, i % 2 ? "\n" : ""); | |
337 | + printf("\n"); | |
338 | + } else if (c == 'm') { | |
339 | + printf(" mouse options:\n" | |
340 | + " --hold\n"); | |
341 | + for (i = 0; mmod[i].opt != NULL; i++) | |
342 | + printf("\t\t%s\n", mmod[i].opt); | |
343 | + printf("\n mouse values:\n" | |
344 | + " Two signed numbers\n" | |
345 | + "--quit to close\n"); | |
346 | + } else { | |
347 | + printf(" joystick options:\n"); | |
348 | + for (i = 0; jmod[i].opt != NULL; i++) | |
349 | + printf("\t\t%s\n", jmod[i].opt); | |
350 | + printf("\n joystick values:\n" | |
351 | + " three signed numbers\n" | |
352 | + "--quit to close\n"); | |
353 | + } | |
354 | +} | |
355 | + | |
356 | +int main(int argc, const char *argv[]) | |
357 | +{ | |
358 | + const char *filename = NULL; | |
359 | + int fd = 0; | |
360 | + char buf[BUF_LEN]; | |
361 | + int cmd_len; | |
362 | + char report[8]; | |
363 | + int to_send = 8; | |
364 | + int hold = 0; | |
365 | + fd_set rfds; | |
366 | + int retval, i; | |
367 | + | |
368 | + if (argc < 3) { | |
369 | + fprintf(stderr, "Usage: %s devname mouse|keyboard|joystick\n", | |
370 | + argv[0]); | |
371 | + return 1; | |
372 | + } | |
373 | + | |
374 | + if (argv[2][0] != 'k' && argv[2][0] != 'm' && argv[2][0] != 'j') | |
375 | + return 2; | |
376 | + | |
377 | + filename = argv[1]; | |
378 | + | |
379 | + if ((fd = open(filename, O_RDWR, 0666)) == -1) { | |
380 | + perror(filename); | |
381 | + return 3; | |
382 | + } | |
383 | + | |
384 | + print_options(argv[2][0]); | |
385 | + | |
386 | + while (42) { | |
387 | + | |
388 | + FD_ZERO(&rfds); | |
389 | + FD_SET(STDIN_FILENO, &rfds); | |
390 | + FD_SET(fd, &rfds); | |
391 | + | |
392 | + retval = select(fd + 1, &rfds, NULL, NULL, NULL); | |
393 | + if (retval == -1 && errno == EINTR) | |
394 | + continue; | |
395 | + if (retval < 0) { | |
396 | + perror("select()"); | |
397 | + return 4; | |
398 | + } | |
399 | + | |
400 | + if (FD_ISSET(fd, &rfds)) { | |
401 | + cmd_len = read(fd, buf, BUF_LEN - 1); | |
402 | + printf("recv report:"); | |
403 | + for (i = 0; i < cmd_len; i++) | |
404 | + printf(" %02x", buf[i]); | |
405 | + printf("\n"); | |
406 | + } | |
407 | + | |
408 | + if (FD_ISSET(STDIN_FILENO, &rfds)) { | |
409 | + memset(report, 0x0, sizeof(report)); | |
410 | + cmd_len = read(STDIN_FILENO, buf, BUF_LEN - 1); | |
411 | + | |
412 | + if (cmd_len == 0) | |
413 | + break; | |
414 | + | |
415 | + buf[cmd_len - 1] = '\0'; | |
416 | + hold = 0; | |
417 | + | |
418 | + memset(report, 0x0, sizeof(report)); | |
419 | + if (argv[2][0] == 'k') | |
420 | + to_send = keyboard_fill_report(report, buf, &hold); | |
421 | + else if (argv[2][0] == 'm') | |
422 | + to_send = mouse_fill_report(report, buf, &hold); | |
423 | + else | |
424 | + to_send = joystick_fill_report(report, buf, &hold); | |
425 | + | |
426 | + if (to_send == -1) | |
427 | + break; | |
428 | + | |
429 | + if (write(fd, report, to_send) != to_send) { | |
430 | + perror(filename); | |
431 | + return 5; | |
432 | + } | |
433 | + if (!hold) { | |
434 | + memset(report, 0x0, sizeof(report)); | |
435 | + if (write(fd, report, to_send) != to_send) { | |
436 | + perror(filename); | |
437 | + return 6; | |
438 | + } | |
439 | + } | |
440 | + } | |
441 | + } | |
442 | + | |
443 | + close(fd); | |
444 | + return 0; | |
445 | +} |
drivers/usb/gadget/Kconfig
... | ... | @@ -863,6 +863,17 @@ |
863 | 863 | |
864 | 864 | If unsure, say "y". |
865 | 865 | |
866 | +config USB_G_HID | |
867 | + tristate "HID Gadget" | |
868 | + help | |
869 | + The HID gadget driver provides generic emulation of USB | |
870 | + Human Interface Devices (HID). | |
871 | + | |
872 | + For more information, see Documentation/usb/gadget_hid.txt which | |
873 | + includes sample code for accessing the device files. | |
874 | + | |
875 | + Say "y" to link the driver statically, or "m" to build a | |
876 | + dynamically linked module called "g_hid". | |
866 | 877 | |
867 | 878 | # put drivers that need isochronous transfer support (for audio |
868 | 879 | # or video class gadget drivers), or specific hardware, here. |
drivers/usb/gadget/Makefile
... | ... | @@ -43,6 +43,7 @@ |
43 | 43 | g_printer-objs := printer.o |
44 | 44 | g_cdc-objs := cdc2.o |
45 | 45 | g_multi-objs := multi.o |
46 | +g_hid-objs := hid.o | |
46 | 47 | g_nokia-objs := nokia.o |
47 | 48 | |
48 | 49 | obj-$(CONFIG_USB_ZERO) += g_zero.o |
... | ... | @@ -55,6 +56,7 @@ |
55 | 56 | obj-$(CONFIG_USB_G_PRINTER) += g_printer.o |
56 | 57 | obj-$(CONFIG_USB_MIDI_GADGET) += g_midi.o |
57 | 58 | obj-$(CONFIG_USB_CDC_COMPOSITE) += g_cdc.o |
59 | +obj-$(CONFIG_USB_G_HID) += g_hid.o | |
58 | 60 | obj-$(CONFIG_USB_G_MULTI) += g_multi.o |
59 | 61 | obj-$(CONFIG_USB_G_NOKIA) += g_nokia.o |
drivers/usb/gadget/f_hid.c
1 | +/* | |
2 | + * f_hid.c -- USB HID function driver | |
3 | + * | |
4 | + * Copyright (C) 2010 Fabien Chouteau <fabien.chouteau@barco.com> | |
5 | + * | |
6 | + * This program is free software; you can redistribute it and/or modify | |
7 | + * it under the terms of the GNU General Public License as published by | |
8 | + * the Free Software Foundation; either version 2 of the License, or | |
9 | + * (at your option) any later version. | |
10 | + * | |
11 | + * This program is distributed in the hope that it will be useful, | |
12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | + * GNU General Public License for more details. | |
15 | + * | |
16 | + * You should have received a copy of the GNU General Public License | |
17 | + * along with this program; if not, write to the Free Software | |
18 | + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
19 | + */ | |
20 | + | |
21 | +#include <linux/kernel.h> | |
22 | +#include <linux/utsname.h> | |
23 | +#include <linux/module.h> | |
24 | +#include <linux/hid.h> | |
25 | +#include <linux/cdev.h> | |
26 | +#include <linux/mutex.h> | |
27 | +#include <linux/poll.h> | |
28 | +#include <linux/smp_lock.h> | |
29 | +#include <linux/uaccess.h> | |
30 | +#include <linux/wait.h> | |
31 | +#include <linux/usb/g_hid.h> | |
32 | + | |
33 | +static int major, minors; | |
34 | +static struct class *hidg_class; | |
35 | + | |
36 | +/*-------------------------------------------------------------------------*/ | |
37 | +/* HID gadget struct */ | |
38 | + | |
39 | +struct f_hidg { | |
40 | + /* configuration */ | |
41 | + unsigned char bInterfaceSubClass; | |
42 | + unsigned char bInterfaceProtocol; | |
43 | + unsigned short report_desc_length; | |
44 | + char *report_desc; | |
45 | + unsigned short report_length; | |
46 | + | |
47 | + /* recv report */ | |
48 | + char *set_report_buff; | |
49 | + unsigned short set_report_length; | |
50 | + spinlock_t spinlock; | |
51 | + wait_queue_head_t read_queue; | |
52 | + | |
53 | + /* send report */ | |
54 | + struct mutex lock; | |
55 | + bool write_pending; | |
56 | + wait_queue_head_t write_queue; | |
57 | + struct usb_request *req; | |
58 | + | |
59 | + int minor; | |
60 | + struct cdev cdev; | |
61 | + struct usb_function func; | |
62 | + struct usb_ep *in_ep; | |
63 | + struct usb_endpoint_descriptor *fs_in_ep_desc; | |
64 | + struct usb_endpoint_descriptor *hs_in_ep_desc; | |
65 | +}; | |
66 | + | |
67 | +static inline struct f_hidg *func_to_hidg(struct usb_function *f) | |
68 | +{ | |
69 | + return container_of(f, struct f_hidg, func); | |
70 | +} | |
71 | + | |
72 | +/*-------------------------------------------------------------------------*/ | |
73 | +/* Static descriptors */ | |
74 | + | |
75 | +static struct usb_interface_descriptor hidg_interface_desc = { | |
76 | + .bLength = sizeof hidg_interface_desc, | |
77 | + .bDescriptorType = USB_DT_INTERFACE, | |
78 | + /* .bInterfaceNumber = DYNAMIC */ | |
79 | + .bAlternateSetting = 0, | |
80 | + .bNumEndpoints = 1, | |
81 | + .bInterfaceClass = USB_CLASS_HID, | |
82 | + /* .bInterfaceSubClass = DYNAMIC */ | |
83 | + /* .bInterfaceProtocol = DYNAMIC */ | |
84 | + /* .iInterface = DYNAMIC */ | |
85 | +}; | |
86 | + | |
87 | +static struct hid_descriptor hidg_desc = { | |
88 | + .bLength = sizeof hidg_desc, | |
89 | + .bDescriptorType = HID_DT_HID, | |
90 | + .bcdHID = 0x0101, | |
91 | + .bCountryCode = 0x00, | |
92 | + .bNumDescriptors = 0x1, | |
93 | + /*.desc[0].bDescriptorType = DYNAMIC */ | |
94 | + /*.desc[0].wDescriptorLenght = DYNAMIC */ | |
95 | +}; | |
96 | + | |
97 | +/* High-Speed Support */ | |
98 | + | |
99 | +static struct usb_endpoint_descriptor hidg_hs_in_ep_desc = { | |
100 | + .bLength = USB_DT_ENDPOINT_SIZE, | |
101 | + .bDescriptorType = USB_DT_ENDPOINT, | |
102 | + .bEndpointAddress = USB_DIR_IN, | |
103 | + .bmAttributes = USB_ENDPOINT_XFER_INT, | |
104 | + /*.wMaxPacketSize = DYNAMIC */ | |
105 | + .bInterval = 4, /* FIXME: Add this field in the | |
106 | + * HID gadget configuration? | |
107 | + * (struct hidg_func_descriptor) | |
108 | + */ | |
109 | +}; | |
110 | + | |
111 | +static struct usb_descriptor_header *hidg_hs_descriptors[] = { | |
112 | + (struct usb_descriptor_header *)&hidg_interface_desc, | |
113 | + (struct usb_descriptor_header *)&hidg_desc, | |
114 | + (struct usb_descriptor_header *)&hidg_hs_in_ep_desc, | |
115 | + NULL, | |
116 | +}; | |
117 | + | |
118 | +/* Full-Speed Support */ | |
119 | + | |
120 | +static struct usb_endpoint_descriptor hidg_fs_in_ep_desc = { | |
121 | + .bLength = USB_DT_ENDPOINT_SIZE, | |
122 | + .bDescriptorType = USB_DT_ENDPOINT, | |
123 | + .bEndpointAddress = USB_DIR_IN, | |
124 | + .bmAttributes = USB_ENDPOINT_XFER_INT, | |
125 | + /*.wMaxPacketSize = DYNAMIC */ | |
126 | + .bInterval = 10, /* FIXME: Add this field in the | |
127 | + * HID gadget configuration? | |
128 | + * (struct hidg_func_descriptor) | |
129 | + */ | |
130 | +}; | |
131 | + | |
132 | +static struct usb_descriptor_header *hidg_fs_descriptors[] = { | |
133 | + (struct usb_descriptor_header *)&hidg_interface_desc, | |
134 | + (struct usb_descriptor_header *)&hidg_desc, | |
135 | + (struct usb_descriptor_header *)&hidg_fs_in_ep_desc, | |
136 | + NULL, | |
137 | +}; | |
138 | + | |
139 | +/*-------------------------------------------------------------------------*/ | |
140 | +/* Char Device */ | |
141 | + | |
142 | +static ssize_t f_hidg_read(struct file *file, char __user *buffer, | |
143 | + size_t count, loff_t *ptr) | |
144 | +{ | |
145 | + struct f_hidg *hidg = (struct f_hidg *)file->private_data; | |
146 | + char *tmp_buff = NULL; | |
147 | + unsigned long flags; | |
148 | + | |
149 | + if (!count) | |
150 | + return 0; | |
151 | + | |
152 | + if (!access_ok(VERIFY_WRITE, buffer, count)) | |
153 | + return -EFAULT; | |
154 | + | |
155 | + spin_lock_irqsave(&hidg->spinlock, flags); | |
156 | + | |
157 | +#define READ_COND (hidg->set_report_buff != NULL) | |
158 | + | |
159 | + while (!READ_COND) { | |
160 | + spin_unlock_irqrestore(&hidg->spinlock, flags); | |
161 | + if (file->f_flags & O_NONBLOCK) | |
162 | + return -EAGAIN; | |
163 | + | |
164 | + if (wait_event_interruptible(hidg->read_queue, READ_COND)) | |
165 | + return -ERESTARTSYS; | |
166 | + | |
167 | + spin_lock_irqsave(&hidg->spinlock, flags); | |
168 | + } | |
169 | + | |
170 | + | |
171 | + count = min_t(unsigned, count, hidg->set_report_length); | |
172 | + tmp_buff = hidg->set_report_buff; | |
173 | + hidg->set_report_buff = NULL; | |
174 | + | |
175 | + spin_unlock_irqrestore(&hidg->spinlock, flags); | |
176 | + | |
177 | + if (tmp_buff != NULL) { | |
178 | + /* copy to user outside spinlock */ | |
179 | + count -= copy_to_user(buffer, tmp_buff, count); | |
180 | + kfree(tmp_buff); | |
181 | + } else | |
182 | + count = -ENOMEM; | |
183 | + | |
184 | + return count; | |
185 | +} | |
186 | + | |
187 | +static void f_hidg_req_complete(struct usb_ep *ep, struct usb_request *req) | |
188 | +{ | |
189 | + struct f_hidg *hidg = (struct f_hidg *)ep->driver_data; | |
190 | + | |
191 | + if (req->status != 0) { | |
192 | + ERROR(hidg->func.config->cdev, | |
193 | + "End Point Request ERROR: %d\n", req->status); | |
194 | + } | |
195 | + | |
196 | + hidg->write_pending = 0; | |
197 | + wake_up(&hidg->write_queue); | |
198 | +} | |
199 | + | |
200 | +static ssize_t f_hidg_write(struct file *file, const char __user *buffer, | |
201 | + size_t count, loff_t *offp) | |
202 | +{ | |
203 | + struct f_hidg *hidg = (struct f_hidg *)file->private_data; | |
204 | + ssize_t status = -ENOMEM; | |
205 | + | |
206 | + if (!access_ok(VERIFY_READ, buffer, count)) | |
207 | + return -EFAULT; | |
208 | + | |
209 | + mutex_lock(&hidg->lock); | |
210 | + | |
211 | +#define WRITE_COND (!hidg->write_pending) | |
212 | + | |
213 | + /* write queue */ | |
214 | + while (!WRITE_COND) { | |
215 | + mutex_unlock(&hidg->lock); | |
216 | + if (file->f_flags & O_NONBLOCK) | |
217 | + return -EAGAIN; | |
218 | + | |
219 | + if (wait_event_interruptible_exclusive( | |
220 | + hidg->write_queue, WRITE_COND)) | |
221 | + return -ERESTARTSYS; | |
222 | + | |
223 | + mutex_lock(&hidg->lock); | |
224 | + } | |
225 | + | |
226 | + count = min_t(unsigned, count, hidg->report_length); | |
227 | + status = copy_from_user(hidg->req->buf, buffer, count); | |
228 | + | |
229 | + if (status != 0) { | |
230 | + ERROR(hidg->func.config->cdev, | |
231 | + "copy_from_user error\n"); | |
232 | + mutex_unlock(&hidg->lock); | |
233 | + return -EINVAL; | |
234 | + } | |
235 | + | |
236 | + hidg->req->status = 0; | |
237 | + hidg->req->zero = 0; | |
238 | + hidg->req->length = count; | |
239 | + hidg->req->complete = f_hidg_req_complete; | |
240 | + hidg->req->context = hidg; | |
241 | + hidg->write_pending = 1; | |
242 | + | |
243 | + status = usb_ep_queue(hidg->in_ep, hidg->req, GFP_ATOMIC); | |
244 | + if (status < 0) { | |
245 | + ERROR(hidg->func.config->cdev, | |
246 | + "usb_ep_queue error on int endpoint %zd\n", status); | |
247 | + hidg->write_pending = 0; | |
248 | + wake_up(&hidg->write_queue); | |
249 | + } else { | |
250 | + status = count; | |
251 | + } | |
252 | + | |
253 | + mutex_unlock(&hidg->lock); | |
254 | + | |
255 | + return status; | |
256 | +} | |
257 | + | |
258 | +static unsigned int f_hidg_poll(struct file *file, poll_table *wait) | |
259 | +{ | |
260 | + struct f_hidg *hidg = (struct f_hidg *)file->private_data; | |
261 | + unsigned int ret = 0; | |
262 | + | |
263 | + poll_wait(file, &hidg->read_queue, wait); | |
264 | + poll_wait(file, &hidg->write_queue, wait); | |
265 | + | |
266 | + if (WRITE_COND) | |
267 | + ret |= POLLOUT | POLLWRNORM; | |
268 | + | |
269 | + if (READ_COND) | |
270 | + ret |= POLLIN | POLLRDNORM; | |
271 | + | |
272 | + return ret; | |
273 | +} | |
274 | + | |
275 | +#undef WRITE_COND | |
276 | +#undef READ_COND | |
277 | + | |
278 | +static int f_hidg_release(struct inode *inode, struct file *fd) | |
279 | +{ | |
280 | + fd->private_data = NULL; | |
281 | + return 0; | |
282 | +} | |
283 | + | |
284 | +static int f_hidg_open(struct inode *inode, struct file *fd) | |
285 | +{ | |
286 | + struct f_hidg *hidg = | |
287 | + container_of(inode->i_cdev, struct f_hidg, cdev); | |
288 | + | |
289 | + fd->private_data = hidg; | |
290 | + | |
291 | + return 0; | |
292 | +} | |
293 | + | |
294 | +/*-------------------------------------------------------------------------*/ | |
295 | +/* usb_function */ | |
296 | + | |
297 | +static void hidg_set_report_complete(struct usb_ep *ep, struct usb_request *req) | |
298 | +{ | |
299 | + struct f_hidg *hidg = (struct f_hidg *)req->context; | |
300 | + | |
301 | + if (req->status != 0 || req->buf == NULL || req->actual == 0) { | |
302 | + ERROR(hidg->func.config->cdev, "%s FAILED\n", __func__); | |
303 | + return; | |
304 | + } | |
305 | + | |
306 | + spin_lock(&hidg->spinlock); | |
307 | + | |
308 | + hidg->set_report_buff = krealloc(hidg->set_report_buff, | |
309 | + req->actual, GFP_ATOMIC); | |
310 | + | |
311 | + if (hidg->set_report_buff == NULL) { | |
312 | + spin_unlock(&hidg->spinlock); | |
313 | + return; | |
314 | + } | |
315 | + hidg->set_report_length = req->actual; | |
316 | + memcpy(hidg->set_report_buff, req->buf, req->actual); | |
317 | + | |
318 | + spin_unlock(&hidg->spinlock); | |
319 | + | |
320 | + wake_up(&hidg->read_queue); | |
321 | + | |
322 | + return; | |
323 | +} | |
324 | + | |
325 | +static int hidg_setup(struct usb_function *f, | |
326 | + const struct usb_ctrlrequest *ctrl) | |
327 | +{ | |
328 | + struct f_hidg *hidg = func_to_hidg(f); | |
329 | + struct usb_composite_dev *cdev = f->config->cdev; | |
330 | + struct usb_request *req = cdev->req; | |
331 | + int status = 0; | |
332 | + __u16 value, length; | |
333 | + | |
334 | + value = __le16_to_cpu(ctrl->wValue); | |
335 | + length = __le16_to_cpu(ctrl->wLength); | |
336 | + | |
337 | + VDBG(cdev, "hid_setup crtl_request : bRequestType:0x%x bRequest:0x%x " | |
338 | + "Value:0x%x\n", ctrl->bRequestType, ctrl->bRequest, value); | |
339 | + | |
340 | + switch ((ctrl->bRequestType << 8) | ctrl->bRequest) { | |
341 | + case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8 | |
342 | + | HID_REQ_GET_REPORT): | |
343 | + VDBG(cdev, "get_report\n"); | |
344 | + | |
345 | + /* send an empty report */ | |
346 | + length = min_t(unsigned, length, hidg->report_length); | |
347 | + memset(req->buf, 0x0, length); | |
348 | + | |
349 | + goto respond; | |
350 | + break; | |
351 | + | |
352 | + case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8 | |
353 | + | HID_REQ_GET_PROTOCOL): | |
354 | + VDBG(cdev, "get_protocol\n"); | |
355 | + goto stall; | |
356 | + break; | |
357 | + | |
358 | + case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8 | |
359 | + | HID_REQ_SET_REPORT): | |
360 | + VDBG(cdev, "set_report | wLenght=%d\n", ctrl->wLength); | |
361 | + req->context = hidg; | |
362 | + req->complete = hidg_set_report_complete; | |
363 | + goto respond; | |
364 | + break; | |
365 | + | |
366 | + case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8 | |
367 | + | HID_REQ_SET_PROTOCOL): | |
368 | + VDBG(cdev, "set_protocol\n"); | |
369 | + goto stall; | |
370 | + break; | |
371 | + | |
372 | + case ((USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_INTERFACE) << 8 | |
373 | + | USB_REQ_GET_DESCRIPTOR): | |
374 | + switch (value >> 8) { | |
375 | + case HID_DT_REPORT: | |
376 | + VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: REPORT\n"); | |
377 | + length = min_t(unsigned short, length, | |
378 | + hidg->report_desc_length); | |
379 | + memcpy(req->buf, hidg->report_desc, length); | |
380 | + goto respond; | |
381 | + break; | |
382 | + | |
383 | + default: | |
384 | + VDBG(cdev, "Unknown decriptor request 0x%x\n", | |
385 | + value >> 8); | |
386 | + goto stall; | |
387 | + break; | |
388 | + } | |
389 | + break; | |
390 | + | |
391 | + default: | |
392 | + VDBG(cdev, "Unknown request 0x%x\n", | |
393 | + ctrl->bRequest); | |
394 | + goto stall; | |
395 | + break; | |
396 | + } | |
397 | + | |
398 | +stall: | |
399 | + return -EOPNOTSUPP; | |
400 | + | |
401 | +respond: | |
402 | + req->zero = 0; | |
403 | + req->length = length; | |
404 | + status = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC); | |
405 | + if (status < 0) | |
406 | + ERROR(cdev, "usb_ep_queue error on ep0 %d\n", value); | |
407 | + return status; | |
408 | +} | |
409 | + | |
410 | +static void hidg_disable(struct usb_function *f) | |
411 | +{ | |
412 | + struct f_hidg *hidg = func_to_hidg(f); | |
413 | + | |
414 | + usb_ep_disable(hidg->in_ep); | |
415 | + hidg->in_ep->driver_data = NULL; | |
416 | + | |
417 | + return; | |
418 | +} | |
419 | + | |
420 | +static int hidg_set_alt(struct usb_function *f, unsigned intf, unsigned alt) | |
421 | +{ | |
422 | + struct usb_composite_dev *cdev = f->config->cdev; | |
423 | + struct f_hidg *hidg = func_to_hidg(f); | |
424 | + const struct usb_endpoint_descriptor *ep_desc; | |
425 | + int status = 0; | |
426 | + | |
427 | + VDBG(cdev, "hidg_set_alt intf:%d alt:%d\n", intf, alt); | |
428 | + | |
429 | + if (hidg->in_ep != NULL) { | |
430 | + /* restart endpoint */ | |
431 | + if (hidg->in_ep->driver_data != NULL) | |
432 | + usb_ep_disable(hidg->in_ep); | |
433 | + | |
434 | + ep_desc = ep_choose(f->config->cdev->gadget, | |
435 | + hidg->hs_in_ep_desc, hidg->fs_in_ep_desc); | |
436 | + status = usb_ep_enable(hidg->in_ep, ep_desc); | |
437 | + if (status < 0) { | |
438 | + ERROR(cdev, "Enable endpoint FAILED!\n"); | |
439 | + goto fail; | |
440 | + } | |
441 | + hidg->in_ep->driver_data = hidg; | |
442 | + } | |
443 | +fail: | |
444 | + return status; | |
445 | +} | |
446 | + | |
447 | +const struct file_operations f_hidg_fops = { | |
448 | + .owner = THIS_MODULE, | |
449 | + .open = f_hidg_open, | |
450 | + .release = f_hidg_release, | |
451 | + .write = f_hidg_write, | |
452 | + .read = f_hidg_read, | |
453 | + .poll = f_hidg_poll, | |
454 | +}; | |
455 | + | |
456 | +static int __init hidg_bind(struct usb_configuration *c, struct usb_function *f) | |
457 | +{ | |
458 | + struct usb_ep *ep; | |
459 | + struct f_hidg *hidg = func_to_hidg(f); | |
460 | + int status; | |
461 | + dev_t dev; | |
462 | + | |
463 | + /* allocate instance-specific interface IDs, and patch descriptors */ | |
464 | + status = usb_interface_id(c, f); | |
465 | + if (status < 0) | |
466 | + goto fail; | |
467 | + hidg_interface_desc.bInterfaceNumber = status; | |
468 | + | |
469 | + | |
470 | + /* allocate instance-specific endpoints */ | |
471 | + status = -ENODEV; | |
472 | + ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_in_ep_desc); | |
473 | + if (!ep) | |
474 | + goto fail; | |
475 | + ep->driver_data = c->cdev; /* claim */ | |
476 | + hidg->in_ep = ep; | |
477 | + | |
478 | + /* preallocate request and buffer */ | |
479 | + status = -ENOMEM; | |
480 | + hidg->req = usb_ep_alloc_request(hidg->in_ep, GFP_KERNEL); | |
481 | + if (!hidg->req) | |
482 | + goto fail; | |
483 | + | |
484 | + | |
485 | + hidg->req->buf = kmalloc(hidg->report_length, GFP_KERNEL); | |
486 | + if (!hidg->req->buf) | |
487 | + goto fail; | |
488 | + | |
489 | + /* set descriptor dynamic values */ | |
490 | + hidg_interface_desc.bInterfaceSubClass = hidg->bInterfaceSubClass; | |
491 | + hidg_interface_desc.bInterfaceProtocol = hidg->bInterfaceProtocol; | |
492 | + hidg_hs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length); | |
493 | + hidg_fs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length); | |
494 | + hidg_desc.desc[0].bDescriptorType = HID_DT_REPORT; | |
495 | + hidg_desc.desc[0].wDescriptorLength = | |
496 | + cpu_to_le16(hidg->report_desc_length); | |
497 | + | |
498 | + hidg->set_report_buff = NULL; | |
499 | + | |
500 | + /* copy descriptors */ | |
501 | + f->descriptors = usb_copy_descriptors(hidg_fs_descriptors); | |
502 | + if (!f->descriptors) | |
503 | + goto fail; | |
504 | + | |
505 | + hidg->fs_in_ep_desc = usb_find_endpoint(hidg_fs_descriptors, | |
506 | + f->descriptors, | |
507 | + &hidg_fs_in_ep_desc); | |
508 | + | |
509 | + if (gadget_is_dualspeed(c->cdev->gadget)) { | |
510 | + hidg_hs_in_ep_desc.bEndpointAddress = | |
511 | + hidg_fs_in_ep_desc.bEndpointAddress; | |
512 | + f->hs_descriptors = usb_copy_descriptors(hidg_hs_descriptors); | |
513 | + if (!f->hs_descriptors) | |
514 | + goto fail; | |
515 | + hidg->hs_in_ep_desc = usb_find_endpoint(hidg_hs_descriptors, | |
516 | + f->hs_descriptors, | |
517 | + &hidg_hs_in_ep_desc); | |
518 | + } else { | |
519 | + hidg->hs_in_ep_desc = NULL; | |
520 | + } | |
521 | + | |
522 | + mutex_init(&hidg->lock); | |
523 | + spin_lock_init(&hidg->spinlock); | |
524 | + init_waitqueue_head(&hidg->write_queue); | |
525 | + init_waitqueue_head(&hidg->read_queue); | |
526 | + | |
527 | + /* create char device */ | |
528 | + cdev_init(&hidg->cdev, &f_hidg_fops); | |
529 | + dev = MKDEV(major, hidg->minor); | |
530 | + status = cdev_add(&hidg->cdev, dev, 1); | |
531 | + if (status) | |
532 | + goto fail; | |
533 | + | |
534 | + device_create(hidg_class, NULL, dev, NULL, "%s%d", "hidg", hidg->minor); | |
535 | + | |
536 | + return 0; | |
537 | + | |
538 | +fail: | |
539 | + ERROR(f->config->cdev, "hidg_bind FAILED\n"); | |
540 | + if (hidg->req != NULL) { | |
541 | + kfree(hidg->req->buf); | |
542 | + if (hidg->in_ep != NULL) | |
543 | + usb_ep_free_request(hidg->in_ep, hidg->req); | |
544 | + } | |
545 | + | |
546 | + usb_free_descriptors(f->hs_descriptors); | |
547 | + usb_free_descriptors(f->descriptors); | |
548 | + | |
549 | + return status; | |
550 | +} | |
551 | + | |
552 | +static void hidg_unbind(struct usb_configuration *c, struct usb_function *f) | |
553 | +{ | |
554 | + struct f_hidg *hidg = func_to_hidg(f); | |
555 | + | |
556 | + device_destroy(hidg_class, MKDEV(major, hidg->minor)); | |
557 | + cdev_del(&hidg->cdev); | |
558 | + | |
559 | + /* disable/free request and end point */ | |
560 | + usb_ep_disable(hidg->in_ep); | |
561 | + usb_ep_dequeue(hidg->in_ep, hidg->req); | |
562 | + kfree(hidg->req->buf); | |
563 | + usb_ep_free_request(hidg->in_ep, hidg->req); | |
564 | + | |
565 | + /* free descriptors copies */ | |
566 | + usb_free_descriptors(f->hs_descriptors); | |
567 | + usb_free_descriptors(f->descriptors); | |
568 | + | |
569 | + kfree(hidg->report_desc); | |
570 | + kfree(hidg->set_report_buff); | |
571 | + kfree(hidg); | |
572 | +} | |
573 | + | |
574 | +/*-------------------------------------------------------------------------*/ | |
575 | +/* Strings */ | |
576 | + | |
577 | +#define CT_FUNC_HID_IDX 0 | |
578 | + | |
579 | +static struct usb_string ct_func_string_defs[] = { | |
580 | + [CT_FUNC_HID_IDX].s = "HID Interface", | |
581 | + {}, /* end of list */ | |
582 | +}; | |
583 | + | |
584 | +static struct usb_gadget_strings ct_func_string_table = { | |
585 | + .language = 0x0409, /* en-US */ | |
586 | + .strings = ct_func_string_defs, | |
587 | +}; | |
588 | + | |
589 | +static struct usb_gadget_strings *ct_func_strings[] = { | |
590 | + &ct_func_string_table, | |
591 | + NULL, | |
592 | +}; | |
593 | + | |
594 | +/*-------------------------------------------------------------------------*/ | |
595 | +/* usb_configuration */ | |
596 | + | |
597 | +int __init hidg_bind_config(struct usb_configuration *c, | |
598 | + struct hidg_func_descriptor *fdesc, int index) | |
599 | +{ | |
600 | + struct f_hidg *hidg; | |
601 | + int status; | |
602 | + | |
603 | + if (index >= minors) | |
604 | + return -ENOENT; | |
605 | + | |
606 | + /* maybe allocate device-global string IDs, and patch descriptors */ | |
607 | + if (ct_func_string_defs[CT_FUNC_HID_IDX].id == 0) { | |
608 | + status = usb_string_id(c->cdev); | |
609 | + if (status < 0) | |
610 | + return status; | |
611 | + ct_func_string_defs[CT_FUNC_HID_IDX].id = status; | |
612 | + hidg_interface_desc.iInterface = status; | |
613 | + } | |
614 | + | |
615 | + /* allocate and initialize one new instance */ | |
616 | + hidg = kzalloc(sizeof *hidg, GFP_KERNEL); | |
617 | + if (!hidg) | |
618 | + return -ENOMEM; | |
619 | + | |
620 | + hidg->minor = index; | |
621 | + hidg->bInterfaceSubClass = fdesc->subclass; | |
622 | + hidg->bInterfaceProtocol = fdesc->protocol; | |
623 | + hidg->report_length = fdesc->report_length; | |
624 | + hidg->report_desc_length = fdesc->report_desc_length; | |
625 | + hidg->report_desc = kmemdup(fdesc->report_desc, | |
626 | + fdesc->report_desc_length, | |
627 | + GFP_KERNEL); | |
628 | + if (!hidg->report_desc) { | |
629 | + kfree(hidg); | |
630 | + return -ENOMEM; | |
631 | + } | |
632 | + | |
633 | + hidg->func.name = "hid"; | |
634 | + hidg->func.strings = ct_func_strings; | |
635 | + hidg->func.bind = hidg_bind; | |
636 | + hidg->func.unbind = hidg_unbind; | |
637 | + hidg->func.set_alt = hidg_set_alt; | |
638 | + hidg->func.disable = hidg_disable; | |
639 | + hidg->func.setup = hidg_setup; | |
640 | + | |
641 | + status = usb_add_function(c, &hidg->func); | |
642 | + if (status) | |
643 | + kfree(hidg); | |
644 | + | |
645 | + return status; | |
646 | +} | |
647 | + | |
648 | +int __init ghid_setup(struct usb_gadget *g, int count) | |
649 | +{ | |
650 | + int status; | |
651 | + dev_t dev; | |
652 | + | |
653 | + hidg_class = class_create(THIS_MODULE, "hidg"); | |
654 | + | |
655 | + status = alloc_chrdev_region(&dev, 0, count, "hidg"); | |
656 | + if (!status) { | |
657 | + major = MAJOR(dev); | |
658 | + minors = count; | |
659 | + } | |
660 | + | |
661 | + return status; | |
662 | +} | |
663 | + | |
664 | +void ghid_cleanup(void) | |
665 | +{ | |
666 | + if (major) { | |
667 | + unregister_chrdev_region(MKDEV(major, 0), minors); | |
668 | + major = minors = 0; | |
669 | + } | |
670 | + | |
671 | + class_destroy(hidg_class); | |
672 | + hidg_class = NULL; | |
673 | +} |
drivers/usb/gadget/hid.c
1 | +/* | |
2 | + * hid.c -- HID Composite driver | |
3 | + * | |
4 | + * Based on multi.c | |
5 | + * | |
6 | + * Copyright (C) 2010 Fabien Chouteau <fabien.chouteau@barco.com> | |
7 | + * | |
8 | + * This program is free software; you can redistribute it and/or modify | |
9 | + * it under the terms of the GNU General Public License as published by | |
10 | + * the Free Software Foundation; either version 2 of the License, or | |
11 | + * (at your option) any later version. | |
12 | + * | |
13 | + * This program is distributed in the hope that it will be useful, | |
14 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | + * GNU General Public License for more details. | |
17 | + * | |
18 | + * You should have received a copy of the GNU General Public License | |
19 | + * along with this program; if not, write to the Free Software | |
20 | + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
21 | + */ | |
22 | + | |
23 | + | |
24 | +#include <linux/kernel.h> | |
25 | +#include <linux/platform_device.h> | |
26 | +#include <linux/list.h> | |
27 | + | |
28 | +#define DRIVER_DESC "HID Gadget" | |
29 | +#define DRIVER_VERSION "2010/03/16" | |
30 | + | |
31 | +/*-------------------------------------------------------------------------*/ | |
32 | + | |
33 | +#define HIDG_VENDOR_NUM 0x0525 /* XXX NetChip */ | |
34 | +#define HIDG_PRODUCT_NUM 0xa4ac /* Linux-USB HID gadget */ | |
35 | + | |
36 | +/*-------------------------------------------------------------------------*/ | |
37 | + | |
38 | +/* | |
39 | + * kbuild is not very cooperative with respect to linking separately | |
40 | + * compiled library objects into one module. So for now we won't use | |
41 | + * separate compilation ... ensuring init/exit sections work to shrink | |
42 | + * the runtime footprint, and giving us at least some parts of what | |
43 | + * a "gcc --combine ... part1.c part2.c part3.c ... " build would. | |
44 | + */ | |
45 | + | |
46 | +#include "composite.c" | |
47 | +#include "usbstring.c" | |
48 | +#include "config.c" | |
49 | +#include "epautoconf.c" | |
50 | + | |
51 | +#include "f_hid.c" | |
52 | + | |
53 | + | |
54 | +struct hidg_func_node { | |
55 | + struct list_head node; | |
56 | + struct hidg_func_descriptor *func; | |
57 | +}; | |
58 | + | |
59 | +static LIST_HEAD(hidg_func_list); | |
60 | + | |
61 | +/*-------------------------------------------------------------------------*/ | |
62 | + | |
63 | +static struct usb_device_descriptor device_desc = { | |
64 | + .bLength = sizeof device_desc, | |
65 | + .bDescriptorType = USB_DT_DEVICE, | |
66 | + | |
67 | + .bcdUSB = cpu_to_le16(0x0200), | |
68 | + | |
69 | + /* .bDeviceClass = USB_CLASS_COMM, */ | |
70 | + /* .bDeviceSubClass = 0, */ | |
71 | + /* .bDeviceProtocol = 0, */ | |
72 | + .bDeviceClass = 0xEF, | |
73 | + .bDeviceSubClass = 2, | |
74 | + .bDeviceProtocol = 1, | |
75 | + /* .bMaxPacketSize0 = f(hardware) */ | |
76 | + | |
77 | + /* Vendor and product id can be overridden by module parameters. */ | |
78 | + .idVendor = cpu_to_le16(HIDG_VENDOR_NUM), | |
79 | + .idProduct = cpu_to_le16(HIDG_PRODUCT_NUM), | |
80 | + /* .bcdDevice = f(hardware) */ | |
81 | + /* .iManufacturer = DYNAMIC */ | |
82 | + /* .iProduct = DYNAMIC */ | |
83 | + /* NO SERIAL NUMBER */ | |
84 | + .bNumConfigurations = 1, | |
85 | +}; | |
86 | + | |
87 | +static struct usb_otg_descriptor otg_descriptor = { | |
88 | + .bLength = sizeof otg_descriptor, | |
89 | + .bDescriptorType = USB_DT_OTG, | |
90 | + | |
91 | + /* REVISIT SRP-only hardware is possible, although | |
92 | + * it would not be called "OTG" ... | |
93 | + */ | |
94 | + .bmAttributes = USB_OTG_SRP | USB_OTG_HNP, | |
95 | +}; | |
96 | + | |
97 | +static const struct usb_descriptor_header *otg_desc[] = { | |
98 | + (struct usb_descriptor_header *) &otg_descriptor, | |
99 | + NULL, | |
100 | +}; | |
101 | + | |
102 | + | |
103 | +/* string IDs are assigned dynamically */ | |
104 | + | |
105 | +#define STRING_MANUFACTURER_IDX 0 | |
106 | +#define STRING_PRODUCT_IDX 1 | |
107 | + | |
108 | +static char manufacturer[50]; | |
109 | + | |
110 | +static struct usb_string strings_dev[] = { | |
111 | + [STRING_MANUFACTURER_IDX].s = manufacturer, | |
112 | + [STRING_PRODUCT_IDX].s = DRIVER_DESC, | |
113 | + { } /* end of list */ | |
114 | +}; | |
115 | + | |
116 | +static struct usb_gadget_strings stringtab_dev = { | |
117 | + .language = 0x0409, /* en-us */ | |
118 | + .strings = strings_dev, | |
119 | +}; | |
120 | + | |
121 | +static struct usb_gadget_strings *dev_strings[] = { | |
122 | + &stringtab_dev, | |
123 | + NULL, | |
124 | +}; | |
125 | + | |
126 | + | |
127 | + | |
128 | +/****************************** Configurations ******************************/ | |
129 | + | |
130 | +static int __init do_config(struct usb_configuration *c) | |
131 | +{ | |
132 | + struct hidg_func_node *e; | |
133 | + int func = 0, status = 0; | |
134 | + | |
135 | + if (gadget_is_otg(c->cdev->gadget)) { | |
136 | + c->descriptors = otg_desc; | |
137 | + c->bmAttributes |= USB_CONFIG_ATT_WAKEUP; | |
138 | + } | |
139 | + | |
140 | + list_for_each_entry(e, &hidg_func_list, node) { | |
141 | + status = hidg_bind_config(c, e->func, func++); | |
142 | + if (status) | |
143 | + break; | |
144 | + } | |
145 | + | |
146 | + return status; | |
147 | +} | |
148 | + | |
149 | +static struct usb_configuration config_driver = { | |
150 | + .label = "HID Gadget", | |
151 | + .bind = do_config, | |
152 | + .bConfigurationValue = 1, | |
153 | + /* .iConfiguration = DYNAMIC */ | |
154 | + .bmAttributes = USB_CONFIG_ATT_SELFPOWER, | |
155 | +}; | |
156 | + | |
157 | +/****************************** Gadget Bind ******************************/ | |
158 | + | |
159 | +static int __init hid_bind(struct usb_composite_dev *cdev) | |
160 | +{ | |
161 | + struct usb_gadget *gadget = cdev->gadget; | |
162 | + struct list_head *tmp; | |
163 | + int status, gcnum, funcs = 0; | |
164 | + | |
165 | + list_for_each(tmp, &hidg_func_list) | |
166 | + funcs++; | |
167 | + | |
168 | + if (!funcs) | |
169 | + return -ENODEV; | |
170 | + | |
171 | + /* set up HID */ | |
172 | + status = ghid_setup(cdev->gadget, funcs); | |
173 | + if (status < 0) | |
174 | + return status; | |
175 | + | |
176 | + gcnum = usb_gadget_controller_number(gadget); | |
177 | + if (gcnum >= 0) | |
178 | + device_desc.bcdDevice = cpu_to_le16(0x0300 | gcnum); | |
179 | + else | |
180 | + device_desc.bcdDevice = cpu_to_le16(0x0300 | 0x0099); | |
181 | + | |
182 | + | |
183 | + /* Allocate string descriptor numbers ... note that string | |
184 | + * contents can be overridden by the composite_dev glue. | |
185 | + */ | |
186 | + | |
187 | + /* device descriptor strings: manufacturer, product */ | |
188 | + snprintf(manufacturer, sizeof manufacturer, "%s %s with %s", | |
189 | + init_utsname()->sysname, init_utsname()->release, | |
190 | + gadget->name); | |
191 | + status = usb_string_id(cdev); | |
192 | + if (status < 0) | |
193 | + return status; | |
194 | + strings_dev[STRING_MANUFACTURER_IDX].id = status; | |
195 | + device_desc.iManufacturer = status; | |
196 | + | |
197 | + status = usb_string_id(cdev); | |
198 | + if (status < 0) | |
199 | + return status; | |
200 | + strings_dev[STRING_PRODUCT_IDX].id = status; | |
201 | + device_desc.iProduct = status; | |
202 | + | |
203 | + /* register our configuration */ | |
204 | + status = usb_add_config(cdev, &config_driver); | |
205 | + if (status < 0) | |
206 | + return status; | |
207 | + | |
208 | + dev_info(&gadget->dev, DRIVER_DESC ", version: " DRIVER_VERSION "\n"); | |
209 | + | |
210 | + return 0; | |
211 | +} | |
212 | + | |
213 | +static int __exit hid_unbind(struct usb_composite_dev *cdev) | |
214 | +{ | |
215 | + ghid_cleanup(); | |
216 | + return 0; | |
217 | +} | |
218 | + | |
219 | +static int __init hidg_plat_driver_probe(struct platform_device *pdev) | |
220 | +{ | |
221 | + struct hidg_func_descriptor *func = pdev->dev.platform_data; | |
222 | + struct hidg_func_node *entry; | |
223 | + | |
224 | + if (!func) { | |
225 | + dev_err(&pdev->dev, "Platform data missing\n"); | |
226 | + return -ENODEV; | |
227 | + } | |
228 | + | |
229 | + entry = kzalloc(sizeof(*entry), GFP_KERNEL); | |
230 | + if (!entry) | |
231 | + return -ENOMEM; | |
232 | + | |
233 | + entry->func = func; | |
234 | + list_add_tail(&entry->node, &hidg_func_list); | |
235 | + | |
236 | + return 0; | |
237 | +} | |
238 | + | |
239 | +static int __devexit hidg_plat_driver_remove(struct platform_device *pdev) | |
240 | +{ | |
241 | + struct hidg_func_node *e, *n; | |
242 | + | |
243 | + list_for_each_entry_safe(e, n, &hidg_func_list, node) { | |
244 | + list_del(&e->node); | |
245 | + kfree(e); | |
246 | + } | |
247 | + | |
248 | + return 0; | |
249 | +} | |
250 | + | |
251 | + | |
252 | +/****************************** Some noise ******************************/ | |
253 | + | |
254 | + | |
255 | +static struct usb_composite_driver hidg_driver = { | |
256 | + .name = "g_hid", | |
257 | + .dev = &device_desc, | |
258 | + .strings = dev_strings, | |
259 | + .bind = hid_bind, | |
260 | + .unbind = __exit_p(hid_unbind), | |
261 | +}; | |
262 | + | |
263 | +static struct platform_driver hidg_plat_driver = { | |
264 | + .remove = __devexit_p(hidg_plat_driver_remove), | |
265 | + .driver = { | |
266 | + .owner = THIS_MODULE, | |
267 | + .name = "hidg", | |
268 | + }, | |
269 | +}; | |
270 | + | |
271 | + | |
272 | +MODULE_DESCRIPTION(DRIVER_DESC); | |
273 | +MODULE_AUTHOR("Fabien Chouteau, Peter Korsgaard"); | |
274 | +MODULE_LICENSE("GPL"); | |
275 | + | |
276 | +static int __init hidg_init(void) | |
277 | +{ | |
278 | + platform_driver_probe(&hidg_plat_driver, hidg_plat_driver_probe); | |
279 | + return usb_composite_register(&hidg_driver); | |
280 | +} | |
281 | +module_init(hidg_init); | |
282 | + | |
283 | +static void __exit hidg_cleanup(void) | |
284 | +{ | |
285 | + platform_driver_unregister(&hidg_plat_driver); | |
286 | + usb_composite_unregister(&hidg_driver); | |
287 | +} | |
288 | +module_exit(hidg_cleanup); |
include/linux/usb/g_hid.h
1 | +/* | |
2 | + * g_hid.h -- Header file for USB HID gadget driver | |
3 | + * | |
4 | + * Copyright (C) 2010 Fabien Chouteau <fabien.chouteau@barco.com> | |
5 | + * | |
6 | + * This program is free software; you can redistribute it and/or modify | |
7 | + * it under the terms of the GNU General Public License as published by | |
8 | + * the Free Software Foundation; either version 2 of the License, or | |
9 | + * (at your option) any later version. | |
10 | + * | |
11 | + * This program is distributed in the hope that it will be useful, | |
12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | + * GNU General Public License for more details. | |
15 | + * | |
16 | + * You should have received a copy of the GNU General Public License | |
17 | + * along with this program; if not, write to the Free Software | |
18 | + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
19 | + */ | |
20 | + | |
21 | +#ifndef __LINUX_USB_G_HID_H | |
22 | +#define __LINUX_USB_G_HID_H | |
23 | + | |
24 | +struct hidg_func_descriptor { | |
25 | + unsigned char subclass; | |
26 | + unsigned char protocol; | |
27 | + unsigned short report_length; | |
28 | + unsigned short report_desc_length; | |
29 | + unsigned char report_desc[]; | |
30 | +}; | |
31 | + | |
32 | +#endif /* __LINUX_USB_G_HID_H */ |