Fix a compilation warning (simulator build on 64-bit host).
[kugel-rb.git] / firmware / usbstack / usb_hid.c
blob4c76ff6db6300895254bdbdf64bea0a9efb2775c
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2009 by Tomer Shalev
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #include "string.h"
22 #include "system.h"
23 #include "usb_core.h"
24 #include "usb_drv.h"
25 #include "kernel.h"
26 #include "usb_hid.h"
27 #include "usb_class_driver.h"
28 /*#define LOGF_ENABLE*/
29 #include "logf.h"
31 /* Documents avaiable here: http://www.usb.org/developers/devclass_docs/ */
33 #define HID_VER 0x0110 /* 1.1 */
34 /* Subclass Codes (HID1_11.pdf, page 18) */
35 #define SUBCLASS_NONE 0
36 #define SUBCLASS_BOOT_INTERFACE 1
37 /* Protocol Codes (HID1_11.pdf, page 19) */
38 #define PROTOCOL_CODE_NONE 0
39 #define PROTOCOL_CODE_KEYBOARD 1
40 #define PROTOCOL_CODE_MOUSE 2
41 /* HID main items (HID1_11.pdf, page 38) */
42 #define INPUT 0x80
43 #define OUTPUT 0x90
44 #define COLLECTION 0xA0
45 #define COLLECTION_PHYSICAL 0x00
46 #define COLLECTION_APPLICATION 0x01
47 #define END_COLLECTION 0xC0
48 /* Parts (HID1_11.pdf, page 40) */
49 #define MAIN_ITEM_CONSTANT BIT_N(0) /* 0x01 */
50 #define MAIN_ITEM_VARIABLE BIT_N(1) /* 0x02 */
51 #define MAIN_ITEM_RELATIVE BIT_N(2) /* 0x04 */
52 #define MAIN_ITEM_WRAP BIT_N(3) /* 0x08 */
53 #define MAIN_ITEM_NONLINEAR BIT_N(4) /* 0x10 */
54 #define MAIN_ITEM_NO_PREFERRED BIT_N(5) /* 0x20 */
55 #define MAIN_ITEM_NULL_STATE BIT_N(6) /* 0x40 */
56 #define MAIN_ITEM_VOLATILE BIT_N(7) /* 0x80 */
57 #define MAIN_ITEM_BUFFERED_BYTES BIT_N(8) /* 0x0100 */
58 /* HID global items (HID1_11.pdf, page 45) */
59 #define USAGE_PAGE 0x04
60 #define LOGICAL_MINIMUM 0x14
61 #define LOGICAL_MAXIMUM 0x24
62 #define REPORT_SIZE 0x74
63 #define REPORT_ID 0x84
64 #define REPORT_COUNT 0x94
65 /* HID local items (HID1_11.pdf, page 50) */
66 #define USAGE_MINIMUM 0x18
67 #define USAGE_MAXIMUM 0x28
68 /* Types of class descriptors (HID1_11.pdf, page 59) */
69 #define USB_DT_HID 0x21
70 #define USB_DT_REPORT 0x22
72 #define CONSUMER_USAGE 0x09
74 /* HID-only class specific requests (HID1_11.pdf, page 61) */
75 #define USB_HID_GET_REPORT 0x01
76 #define USB_HID_GET_IDLE 0x02
77 #define USB_HID_GET_PROTOCOL 0x03
78 #define USB_HID_SET_REPORT 0x09
79 #define USB_HID_SET_IDLE 0x0A
80 #define USB_HID_SET_PROTOCOL 0x0B
82 /* Get_Report and Set_Report Report Type field (HID1_11.pdf, page 61) */
83 #define REPORT_TYPE_INPUT 0x01
84 #define REPORT_TYPE_OUTPUT 0x02
85 #define REPORT_TYPE_FEATURE 0x03
87 #define HID_BUF_SIZE_MSG 16
88 #define HID_BUF_SIZE_CMD 16
89 #define HID_BUF_SIZE_REPORT 160
90 #define HID_NUM_BUFFERS 5
91 #define SET_REPORT_BUF_LEN 2
93 #ifdef LOGF_ENABLE
95 #define BUF_DUMP_BUF_LEN HID_BUF_SIZE_REPORT
96 #define BUF_DUMP_PREFIX_SIZE 5
97 #define BUF_DUMP_ITEMS_IN_LINE 8
98 #define BUF_DUMP_LINE_SIZE (BUF_DUMP_ITEMS_IN_LINE * 3)
99 #define BUF_DUMP_NUM_LINES (BUF_DUMP_BUF_LEN / BUF_DUMP_ITEMS_IN_LINE)
100 #endif
102 #define HID_BUF_INC(i) do { (i) = ((i) + 1) % HID_NUM_BUFFERS; } while (0)
103 #define PACK_VAL(dest, val) *(dest)++ = (val) & 0xff
105 typedef enum
107 REPORT_ID_KEYBOARD = 1,
108 REPORT_ID_CONSUMER,
109 #ifdef HAVE_USB_HID_MOUSE
110 REPORT_ID_MOUSE,
111 #endif
112 REPORT_ID_COUNT,
113 } report_id_t;
115 /* hid interface */
116 static struct usb_interface_descriptor __attribute__((aligned(2)))
117 interface_descriptor =
119 .bLength = sizeof(struct usb_interface_descriptor),
120 .bDescriptorType = USB_DT_INTERFACE,
121 .bInterfaceNumber = 0,
122 .bAlternateSetting = 0,
123 .bNumEndpoints = 1,
124 .bInterfaceClass = USB_CLASS_HID,
125 .bInterfaceSubClass = SUBCLASS_BOOT_INTERFACE,
126 .bInterfaceProtocol = PROTOCOL_CODE_KEYBOARD,
127 .iInterface = 0
130 struct usb_hid_descriptor {
131 uint8_t bLength;
132 uint8_t bDescriptorType;
133 uint16_t wBcdHID;
134 uint8_t bCountryCode;
135 uint8_t bNumDescriptors;
136 uint8_t bDescriptorType0;
137 uint16_t wDescriptorLength0;
138 } __attribute__ ((packed));
140 static struct usb_hid_descriptor __attribute__((aligned(2))) hid_descriptor =
142 .bLength = sizeof(struct usb_hid_descriptor),
143 .bDescriptorType = USB_DT_HID,
144 .wBcdHID = HID_VER,
145 .bCountryCode = 0,
146 .bNumDescriptors = 1,
147 .bDescriptorType0 = USB_DT_REPORT,
148 .wDescriptorLength0 = 0
151 static struct usb_endpoint_descriptor __attribute__((aligned(2)))
152 endpoint_descriptor =
154 .bLength = sizeof(struct usb_endpoint_descriptor),
155 .bDescriptorType = USB_DT_ENDPOINT,
156 .bEndpointAddress = 0,
157 .bmAttributes = USB_ENDPOINT_XFER_INT,
158 .wMaxPacketSize = 0,
159 .bInterval = 0
162 typedef struct
164 uint8_t usage_page;
165 /* Write the id into the buffer in the appropriate location, and returns the
166 * buffer length */
167 uint8_t (*buf_set)(unsigned char *buf, int id);
168 bool is_key_released;
169 } usb_hid_report_t;
171 static usb_hid_report_t usb_hid_reports[REPORT_ID_COUNT];
173 static unsigned char report_descriptor[HID_BUF_SIZE_REPORT]
174 USB_DEVBSS_ATTR __attribute__((aligned(32)));
176 static unsigned char send_buffer[HID_NUM_BUFFERS][HID_BUF_SIZE_MSG]
177 USB_DEVBSS_ATTR __attribute__((aligned(32)));
178 static size_t send_buffer_len[HID_NUM_BUFFERS];
179 static int cur_buf_prepare;
180 static int cur_buf_send;
182 static bool active = false;
183 static int ep_in;
184 static int usb_interface;
186 static void usb_hid_try_send_drv(void);
188 static void pack_parameter(unsigned char **dest, bool is_signed, bool mark_size,
189 uint8_t parameter, uint32_t value)
191 uint8_t size_value = 1; /* # of bytes */
192 uint32_t v = value;
194 while (v >>= 8)
195 size_value++;
197 if (is_signed)
199 bool is_negative = 0;
201 switch (size_value)
203 case 1:
204 is_negative = (int8_t)value < 0;
205 break;
206 case 2:
207 is_negative = (int16_t)value < 0;
208 break;
209 case 3:
210 case 4:
211 is_negative = (int32_t)value < 0;
212 break;
213 default:
214 break;
217 if (is_negative)
218 size_value++;
221 **dest = parameter | (mark_size ? size_value : 0);
222 (*dest)++;
224 while (size_value--)
226 **dest = value & 0xff;
227 (*dest)++;
228 value >>= 8;
232 int usb_hid_request_endpoints(struct usb_class_driver *drv)
234 ep_in = usb_core_request_endpoint(USB_ENDPOINT_XFER_INT, USB_DIR_IN, drv);
235 if (ep_in < 0)
236 return -1;
238 return 0;
241 int usb_hid_set_first_interface(int interface)
243 usb_interface = interface;
245 return interface + 1;
248 #ifdef LOGF_ENABLE
249 static unsigned char buf_dump_ar[BUF_DUMP_NUM_LINES][BUF_DUMP_LINE_SIZE + 1]
250 USB_DEVBSS_ATTR __attribute__((aligned(32)));
252 void buf_dump(unsigned char *buf, size_t size, char *msg)
254 size_t i;
255 int line;
256 static const char v[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
257 'A', 'B', 'C', 'D', 'E', 'F' };
259 for (i = 0, line = 0; i < size; line++)
261 int j, i0 = i;
262 char *b = buf_dump_ar[line];
264 for (j = 0; j < BUF_DUMP_ITEMS_IN_LINE && i < size; j++, i++)
266 *b++ = v[buf[i] >> 4];
267 *b++ = v[buf[i] & 0xF];
268 *b++ = ' ';
270 *b = 0;
272 /* Prefix must be of len BUF_DUMP_PREFIX_SIZE */
273 logf("%03d: %s %s", i0, buf_dump_ar[line], msg);
276 #else
277 #undef buf_dump
278 #define buf_dump(...)
279 #endif
281 #define BUF_LEN_KEYBOARD 7
282 static uint8_t buf_set_keyboard(unsigned char *buf, int id)
284 memset(buf, 0, BUF_LEN_KEYBOARD);
285 int key, i = 1;
287 /* Each key is a word in id (up to 4 keys supported) */
288 while ((key = id & 0xff))
290 /* Each modifier key is a bit in the first byte */
291 if (HID_KEYBOARD_LEFT_CONTROL <= key && key <= HID_KEYBOARD_RIGHT_GUI)
292 buf[0] |= (1 << (key - HID_KEYBOARD_LEFT_CONTROL));
293 else /* Any other key should be set in a separate byte */
294 buf[i++] = (uint8_t)key;
296 id >>= 8;
299 return BUF_LEN_KEYBOARD;
302 #define BUF_LEN_CONSUMER 4
303 static uint8_t buf_set_consumer(unsigned char *buf, int id)
305 memset(buf, 0, BUF_LEN_CONSUMER);
306 buf[0] = (uint8_t)id;
308 return BUF_LEN_CONSUMER;
311 #ifdef HAVE_USB_HID_MOUSE
312 #define MOUSE_WHEEL_STEP 1
313 #define MOUSE_STEP 8
314 #define MOUSE_BUTTON_LEFT 0x1
315 #define MOUSE_BUTTON_RIGHT 0x2
316 //#define MOUSE_BUTTON_MIDDLE 0x4
317 #define MOUSE_BUTTON 0
318 #define MOUSE_X 1
319 #define MOUSE_Y 2
320 #define MOUSE_WHEEL 3
321 #define BUF_LEN_MOUSE 4
322 #define MOUSE_ACCEL_FACTOR(count) ((count) / 2)
323 #define MOUSE_DO(item, step) (buf[(item)] = ((uint8_t)(step)))
325 static uint8_t buf_set_mouse(unsigned char *buf, int id)
327 static int count = 0;
328 int step = MOUSE_STEP;
330 memset(buf, 0, BUF_LEN_MOUSE);
332 /* Set proper button */
333 switch (id)
335 case HID_MOUSE_BUTTON_LEFT:
336 case HID_MOUSE_LDRAG_UP:
337 case HID_MOUSE_LDRAG_UP_REP:
338 case HID_MOUSE_LDRAG_DOWN:
339 case HID_MOUSE_LDRAG_DOWN_REP:
340 case HID_MOUSE_LDRAG_LEFT:
341 case HID_MOUSE_LDRAG_LEFT_REP:
342 case HID_MOUSE_LDRAG_RIGHT:
343 case HID_MOUSE_LDRAG_RIGHT_REP:
344 MOUSE_DO(MOUSE_BUTTON, MOUSE_BUTTON_LEFT);
345 break;
346 case HID_MOUSE_BUTTON_RIGHT:
347 case HID_MOUSE_RDRAG_UP:
348 case HID_MOUSE_RDRAG_UP_REP:
349 case HID_MOUSE_RDRAG_DOWN:
350 case HID_MOUSE_RDRAG_DOWN_REP:
351 case HID_MOUSE_RDRAG_LEFT:
352 case HID_MOUSE_RDRAG_LEFT_REP:
353 case HID_MOUSE_RDRAG_RIGHT:
354 case HID_MOUSE_RDRAG_RIGHT_REP:
355 MOUSE_DO(MOUSE_BUTTON, MOUSE_BUTTON_RIGHT);
356 break;
357 case HID_MOUSE_BUTTON_LEFT_REL:
358 case HID_MOUSE_BUTTON_RIGHT_REL:
359 /* Keep buf empty, to mark mouse button release */
360 break;
361 default:
362 break;
365 /* Handle mouse accelarated movement */
366 switch (id)
368 case HID_MOUSE_UP:
369 case HID_MOUSE_DOWN:
370 case HID_MOUSE_LEFT:
371 case HID_MOUSE_RIGHT:
372 case HID_MOUSE_LDRAG_UP:
373 case HID_MOUSE_LDRAG_DOWN:
374 case HID_MOUSE_LDRAG_LEFT:
375 case HID_MOUSE_LDRAG_RIGHT:
376 case HID_MOUSE_RDRAG_UP:
377 case HID_MOUSE_RDRAG_DOWN:
378 case HID_MOUSE_RDRAG_LEFT:
379 case HID_MOUSE_RDRAG_RIGHT:
380 count = 0;
381 break;
382 case HID_MOUSE_UP_REP:
383 case HID_MOUSE_DOWN_REP:
384 case HID_MOUSE_LEFT_REP:
385 case HID_MOUSE_RIGHT_REP:
386 case HID_MOUSE_LDRAG_UP_REP:
387 case HID_MOUSE_LDRAG_DOWN_REP:
388 case HID_MOUSE_LDRAG_LEFT_REP:
389 case HID_MOUSE_LDRAG_RIGHT_REP:
390 case HID_MOUSE_RDRAG_UP_REP:
391 case HID_MOUSE_RDRAG_DOWN_REP:
392 case HID_MOUSE_RDRAG_LEFT_REP:
393 case HID_MOUSE_RDRAG_RIGHT_REP:
394 count++;
395 /* TODO: Find a better mouse accellaration algorithm */
396 step *= 1 + MOUSE_ACCEL_FACTOR(count);
397 if (step > 255)
398 step = 255;
399 break;
400 default:
401 break;
404 /* Move/scroll mouse */
405 switch (id)
407 case HID_MOUSE_UP:
408 case HID_MOUSE_UP_REP:
409 case HID_MOUSE_LDRAG_UP:
410 case HID_MOUSE_LDRAG_UP_REP:
411 case HID_MOUSE_RDRAG_UP:
412 case HID_MOUSE_RDRAG_UP_REP:
413 MOUSE_DO(MOUSE_Y, -step);
414 break;
415 case HID_MOUSE_DOWN:
416 case HID_MOUSE_DOWN_REP:
417 case HID_MOUSE_LDRAG_DOWN:
418 case HID_MOUSE_LDRAG_DOWN_REP:
419 case HID_MOUSE_RDRAG_DOWN:
420 case HID_MOUSE_RDRAG_DOWN_REP:
421 MOUSE_DO(MOUSE_Y, step);
422 break;
423 case HID_MOUSE_LEFT:
424 case HID_MOUSE_LEFT_REP:
425 case HID_MOUSE_LDRAG_LEFT:
426 case HID_MOUSE_LDRAG_LEFT_REP:
427 case HID_MOUSE_RDRAG_LEFT:
428 case HID_MOUSE_RDRAG_LEFT_REP:
429 MOUSE_DO(MOUSE_X, -step);
430 break;
431 case HID_MOUSE_RIGHT:
432 case HID_MOUSE_RIGHT_REP:
433 case HID_MOUSE_LDRAG_RIGHT:
434 case HID_MOUSE_LDRAG_RIGHT_REP:
435 case HID_MOUSE_RDRAG_RIGHT:
436 case HID_MOUSE_RDRAG_RIGHT_REP:
437 MOUSE_DO(MOUSE_X, step);
438 break;
439 case HID_MOUSE_SCROLL_UP:
440 MOUSE_DO(MOUSE_WHEEL, MOUSE_WHEEL_STEP);
441 break;
442 case HID_MOUSE_SCROLL_DOWN:
443 MOUSE_DO(MOUSE_WHEEL, -MOUSE_WHEEL_STEP);
444 break;
445 default:
446 break;
449 return BUF_LEN_MOUSE;
451 #endif /* HAVE_USB_HID_MOUSE */
453 static size_t descriptor_report_get(unsigned char *dest)
455 unsigned char *report = dest;
456 usb_hid_report_t *usb_hid_report;
458 /* Keyboard control */
459 usb_hid_report = &usb_hid_reports[REPORT_ID_KEYBOARD];
460 usb_hid_report->usage_page = HID_USAGE_PAGE_KEYBOARD_KEYPAD;
461 usb_hid_report->buf_set = buf_set_keyboard;
462 usb_hid_report->is_key_released = 1;
464 pack_parameter(&report, 0, 1, USAGE_PAGE,
465 HID_USAGE_PAGE_GENERIC_DESKTOP_CONTROLS);
466 pack_parameter(&report, 0, 0, CONSUMER_USAGE, HID_GENERIC_DESKTOP_KEYBOARD);
467 pack_parameter(&report, 0, 1, COLLECTION, COLLECTION_APPLICATION);
468 pack_parameter(&report, 0, 1, REPORT_ID, REPORT_ID_KEYBOARD);
469 pack_parameter(&report, 0, 1, USAGE_PAGE, HID_GENERIC_DESKTOP_KEYPAD);
470 pack_parameter(&report, 0, 1, USAGE_MINIMUM, HID_KEYBOARD_LEFT_CONTROL);
471 pack_parameter(&report, 0, 1, USAGE_MAXIMUM, HID_KEYBOARD_RIGHT_GUI);
472 pack_parameter(&report, 1, 1, LOGICAL_MINIMUM, 0);
473 pack_parameter(&report, 1, 1, LOGICAL_MAXIMUM, 1);
474 pack_parameter(&report, 0, 1, REPORT_SIZE, 1);
475 pack_parameter(&report, 0, 1, REPORT_COUNT, 8);
476 pack_parameter(&report, 0, 1, INPUT, MAIN_ITEM_VARIABLE);
477 pack_parameter(&report, 0, 1, REPORT_SIZE, 1);
478 pack_parameter(&report, 0, 1, REPORT_COUNT, 5);
479 pack_parameter(&report, 0, 1, USAGE_PAGE, HID_USAGE_PAGE_LEDS);
480 pack_parameter(&report, 0, 1, USAGE_MINIMUM, HID_LED_NUM_LOCK);
481 pack_parameter(&report, 0, 1, USAGE_MAXIMUM, HID_LED_KANA);
482 pack_parameter(&report, 0, 1, OUTPUT, MAIN_ITEM_VARIABLE);
483 pack_parameter(&report, 0, 1, REPORT_SIZE, 3);
484 pack_parameter(&report, 0, 1, REPORT_COUNT, 1);
485 pack_parameter(&report, 0, 1, OUTPUT, MAIN_ITEM_CONSTANT);
486 pack_parameter(&report, 0, 1, REPORT_SIZE, 8);
487 pack_parameter(&report, 0, 1, REPORT_COUNT, 6);
488 pack_parameter(&report, 1, 1, LOGICAL_MINIMUM, 0);
489 pack_parameter(&report, 1, 1, LOGICAL_MAXIMUM, HID_KEYBOARD_EX_SEL);
490 pack_parameter(&report, 0, 1, USAGE_PAGE, HID_USAGE_PAGE_KEYBOARD_KEYPAD);
491 pack_parameter(&report, 0, 1, USAGE_MINIMUM, 0);
492 pack_parameter(&report, 0, 1, USAGE_MAXIMUM, HID_KEYBOARD_EX_SEL);
493 pack_parameter(&report, 0, 1, INPUT, 0);
494 PACK_VAL(report, END_COLLECTION);
496 /* Consumer usage controls - play/pause, stop, etc. */
497 usb_hid_report = &usb_hid_reports[REPORT_ID_CONSUMER];
498 usb_hid_report->usage_page = HID_USAGE_PAGE_CONSUMER;
499 usb_hid_report->buf_set = buf_set_consumer;
500 usb_hid_report->is_key_released = 1;
502 pack_parameter(&report, 0, 1, USAGE_PAGE, HID_USAGE_PAGE_CONSUMER);
503 pack_parameter(&report, 0, 0, CONSUMER_USAGE,
504 HID_CONSUMER_USAGE_CONSUMER_CONTROL);
505 pack_parameter(&report, 0, 1, COLLECTION, COLLECTION_APPLICATION);
506 pack_parameter(&report, 0, 1, REPORT_ID, REPORT_ID_CONSUMER);
507 pack_parameter(&report, 0, 1, REPORT_SIZE, 16);
508 pack_parameter(&report, 0, 1, REPORT_COUNT, 2);
509 pack_parameter(&report, 1, 1, LOGICAL_MINIMUM, 1);
510 pack_parameter(&report, 1, 1, LOGICAL_MAXIMUM, 652);
511 pack_parameter(&report, 0, 1, USAGE_MINIMUM,
512 HID_CONSUMER_USAGE_CONSUMER_CONTROL);
513 pack_parameter(&report, 0, 1, USAGE_MAXIMUM, HID_CONSUMER_USAGE_AC_SEND);
514 pack_parameter(&report, 0, 1, INPUT, MAIN_ITEM_NO_PREFERRED |
515 MAIN_ITEM_NULL_STATE);
516 PACK_VAL(report, END_COLLECTION);
518 #ifdef HAVE_USB_HID_MOUSE
519 /* Mouse control */
520 usb_hid_report = &usb_hid_reports[REPORT_ID_MOUSE];
521 usb_hid_report->usage_page = HID_USAGE_PAGE_GENERIC_DESKTOP_CONTROLS;
522 usb_hid_report->buf_set = buf_set_mouse;
523 usb_hid_report->is_key_released = 0;
525 pack_parameter(&report, 0, 1, USAGE_PAGE,
526 HID_USAGE_PAGE_GENERIC_DESKTOP_CONTROLS);
527 pack_parameter(&report, 0, 0, CONSUMER_USAGE, HID_GENERIC_DESKTOP_MOUSE);
528 pack_parameter(&report, 0, 1, COLLECTION, COLLECTION_APPLICATION);
529 pack_parameter(&report, 0, 1, REPORT_ID, REPORT_ID_MOUSE);
530 pack_parameter(&report, 0, 0, CONSUMER_USAGE, HID_GENERIC_DESKTOP_POINTER);
531 pack_parameter(&report, 0, 1, COLLECTION, COLLECTION_PHYSICAL);
532 pack_parameter(&report, 0, 1, USAGE_PAGE, HID_USAGE_PAGE_BUTTON);
533 pack_parameter(&report, 0, 1, USAGE_MINIMUM, 1);
534 pack_parameter(&report, 0, 1, USAGE_MAXIMUM, 8);
535 pack_parameter(&report, 1, 1, LOGICAL_MINIMUM, 0);
536 pack_parameter(&report, 1, 1, LOGICAL_MAXIMUM, 1);
537 pack_parameter(&report, 0, 1, REPORT_SIZE, 1);
538 pack_parameter(&report, 0, 1, REPORT_COUNT, 8);
539 pack_parameter(&report, 0, 1, INPUT, MAIN_ITEM_VARIABLE);
540 pack_parameter(&report, 0, 1, USAGE_PAGE,
541 HID_USAGE_PAGE_GENERIC_DESKTOP_CONTROLS);
542 pack_parameter(&report, 0, 0, CONSUMER_USAGE, HID_GENERIC_DESKTOP_X);
543 pack_parameter(&report, 0, 0, CONSUMER_USAGE, HID_GENERIC_DESKTOP_Y);
544 pack_parameter(&report, 0, 0, CONSUMER_USAGE, HID_GENERIC_DESKTOP_WHEEL);
545 pack_parameter(&report, 0, 1, LOGICAL_MINIMUM, -127 & 0xFF);
546 pack_parameter(&report, 0, 1, LOGICAL_MAXIMUM, 127);
547 pack_parameter(&report, 0, 1, REPORT_SIZE, 8);
548 pack_parameter(&report, 0, 1, REPORT_COUNT, 3);
549 pack_parameter(&report, 0, 1, INPUT, MAIN_ITEM_VARIABLE | MAIN_ITEM_RELATIVE);
550 PACK_VAL(report, END_COLLECTION);
551 PACK_VAL(report, END_COLLECTION);
552 #endif /* HAVE_USB_HID_MOUSE */
554 return (size_t)(report - dest);
557 static void descriptor_hid_get(unsigned char **dest)
559 hid_descriptor.wDescriptorLength0 =
560 (uint16_t)descriptor_report_get(report_descriptor);
562 logf("hid: desc len %u", hid_descriptor.wDescriptorLength0);
563 buf_dump(report_descriptor, hid_descriptor.wDescriptorLength0, "desc");
565 PACK_DATA(*dest, hid_descriptor);
568 int usb_hid_get_config_descriptor(unsigned char *dest, int max_packet_size)
570 unsigned char *orig_dest = dest;
572 logf("hid: config desc.");
574 /* Ignore given max_packet_size */
575 (void)max_packet_size;
577 /* Interface descriptor */
578 interface_descriptor.bInterfaceNumber = usb_interface;
579 PACK_DATA(dest, interface_descriptor);
581 /* HID descriptor */
582 descriptor_hid_get(&dest);
584 /* Endpoint descriptor */
585 endpoint_descriptor.wMaxPacketSize = 8;
586 endpoint_descriptor.bInterval = 8;
587 endpoint_descriptor.bEndpointAddress = ep_in;
588 PACK_DATA(dest, endpoint_descriptor);
590 return (int)(dest - orig_dest);
593 void usb_hid_init_connection(void)
595 logf("hid: init connection");
597 active = true;
600 /* called by usb_core_init() */
601 void usb_hid_init(void)
603 int i;
605 logf("hid: init");
607 for (i = 0; i < HID_NUM_BUFFERS; i++)
608 send_buffer_len[i] = 0;
610 cur_buf_prepare = 0;
611 cur_buf_send = 0;
613 active = true;
616 void usb_hid_disconnect(void)
618 logf("hid: disconnect");
619 active = false;
622 /* called by usb_core_transfer_complete() */
623 void usb_hid_transfer_complete(int ep, int dir, int status, int length)
625 (void)ep;
626 (void)dir;
627 (void)status;
628 (void)length;
630 switch (dir)
632 case USB_DIR_OUT:
633 break;
634 case USB_DIR_IN:
636 if (status)
637 break;
639 send_buffer_len[cur_buf_send] = 0;
640 HID_BUF_INC(cur_buf_send);
641 usb_hid_try_send_drv();
642 break;
647 /* The DAP is registered as a keyboard with several LEDs, therefore the OS sends
648 * LED report to notify the DAP whether Num Lock / Caps Lock etc. are enabled.
649 * In order to allow sending info to the DAP, the Set Report mechanism can be
650 * used by defining vendor specific output reports and send them from the host
651 * to the DAP using the host's custom driver */
652 static int usb_hid_set_report(struct usb_ctrlrequest *req)
654 static unsigned char buf[SET_REPORT_BUF_LEN] USB_DEVBSS_ATTR
655 __attribute__((aligned(32)));
656 int length;
657 int rc = 0;
659 if ((req->wValue >> 8) != REPORT_TYPE_OUTPUT)
661 logf("Unsopported report type");
662 rc = 1;
663 goto Exit;
665 if ((req->wValue & 0xff) != REPORT_ID_KEYBOARD)
667 logf("Wrong report id");
668 rc = 2;
669 goto Exit;
671 if (req->wIndex != (uint16_t)usb_interface)
673 logf("Wrong interface");
674 rc = 3;
675 goto Exit;
677 length = req->wLength;
678 if (length != SET_REPORT_BUF_LEN)
680 logf("Wrong length");
681 rc = 4;
682 goto Exit;
685 memset(buf, 0, length);
686 usb_drv_recv(EP_CONTROL, buf, length);
688 #ifdef LOGF_ENABLE
689 if (buf[1] & 0x01)
690 logf("Num Lock enabled");
691 if (buf[1] & 0x02)
692 logf("Caps Lock enabled");
693 if (buf[1] & 0x04)
694 logf("Scroll Lock enabled");
695 if (buf[1] & 0x08)
696 logf("Compose enabled");
697 if (buf[1] & 0x10)
698 logf("Kana enabled");
699 #endif
701 /* Defining other LEDs and setting them from the USB host (OS) can be used
702 * to send messages to the DAP */
704 Exit:
705 return rc;
708 /* called by usb_core_control_request() */
709 bool usb_hid_control_request(struct usb_ctrlrequest *req, unsigned char *dest)
711 bool handled = false;
713 switch (req->bRequestType & USB_TYPE_MASK)
715 case USB_TYPE_STANDARD:
717 unsigned char *orig_dest = dest;
719 switch (req->wValue>>8) /* type */
721 case USB_DT_HID:
723 logf("hid: type hid");
724 descriptor_hid_get(&dest);
725 break;
727 case USB_DT_REPORT:
729 int len;
731 logf("hid: type report");
733 len = descriptor_report_get(report_descriptor);
734 memcpy(dest, &report_descriptor, len);
735 dest += len;
736 break;
738 default:
739 logf("hid: unsup. std. req");
740 break;
742 if (dest != orig_dest)
744 usb_drv_recv(EP_CONTROL, NULL, 0); /* ack */
745 usb_drv_send(EP_CONTROL, orig_dest, dest - orig_dest);
746 handled = true;
748 break;
751 case USB_TYPE_CLASS:
753 switch (req->bRequest)
755 case USB_HID_SET_IDLE:
756 logf("hid: set idle");
757 usb_drv_send(EP_CONTROL, NULL, 0); /* ack */
758 handled = true;
759 break;
760 case USB_HID_SET_REPORT:
761 logf("hid: set report");
762 if (!usb_hid_set_report(req))
764 usb_drv_send(EP_CONTROL, NULL, 0); /* ack */
765 handled = true;
767 break;
768 default:
769 logf("%d: unsup. cls. req", req->bRequest);
770 break;
772 break;
775 case USB_TYPE_VENDOR:
776 logf("hid: unsup. ven. req");
777 break;
779 return handled;
782 static void usb_hid_try_send_drv(void)
784 int rc;
785 int length = send_buffer_len[cur_buf_send];
787 if (!length)
788 return;
790 rc = usb_drv_send_nonblocking(ep_in, send_buffer[cur_buf_send], length);
791 if (rc)
793 send_buffer_len[cur_buf_send] = 0;
794 return;
798 static void usb_hid_queue(unsigned char *data, int length)
800 if (!active || length <= 0)
801 return;
803 /* Buffer overflow - item still in use */
804 if (send_buffer_len[cur_buf_prepare])
805 return;
807 /* Prepare buffer for sending */
808 if (length > HID_BUF_SIZE_MSG)
809 length = HID_BUF_SIZE_MSG;
810 memcpy(send_buffer[cur_buf_prepare], data, length);
811 send_buffer_len[cur_buf_prepare] = length;
813 HID_BUF_INC(cur_buf_prepare);
816 void usb_hid_send(usage_page_t usage_page, int id)
818 uint8_t report_id, length;
819 static unsigned char buf[HID_BUF_SIZE_CMD] USB_DEVBSS_ATTR
820 __attribute__((aligned(32)));
821 usb_hid_report_t *report = NULL;
823 for (report_id = 1; report_id < REPORT_ID_COUNT; report_id++)
825 if (usb_hid_reports[report_id].usage_page == usage_page)
827 report = &usb_hid_reports[report_id];
828 break;
831 if (!report)
833 logf("Unsupported usage_page");
834 return;
837 logf("Sending cmd 0x%x", id);
838 buf[0] = report_id;
839 length = report->buf_set(&buf[1], id) + 1;
840 logf("length %u", length);
842 if (!length)
843 return;
845 /* Key pressed */
846 buf_dump(buf, length, "key press");
847 usb_hid_queue(buf, length);
849 if (report->is_key_released)
851 /* Key released */
852 memset(buf, 0, length);
853 buf[0] = report_id;
855 buf_dump(buf, length, "key release");
856 usb_hid_queue(buf, length);
859 usb_hid_try_send_drv();