Fix wrong pointer arithmetic in the PDbox aiff header writing code
[maemo-rb.git] / firmware / usbstack / usb_hid.c
blobed0816395ae67dbf5c82339561f712cc2277becc
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 bool currently_sending = false;
184 static int ep_in;
185 static int usb_interface;
187 static void usb_hid_try_send_drv(void);
189 static void pack_parameter(unsigned char **dest, bool is_signed, bool mark_size,
190 uint8_t parameter, uint32_t value)
192 uint8_t size_value = 1; /* # of bytes */
193 uint32_t v = value;
195 while (v >>= 8)
196 size_value++;
198 if (is_signed)
200 bool is_negative = 0;
202 switch (size_value)
204 case 1:
205 is_negative = (int8_t)value < 0;
206 break;
207 case 2:
208 is_negative = (int16_t)value < 0;
209 break;
210 case 3:
211 case 4:
212 is_negative = (int32_t)value < 0;
213 break;
214 default:
215 break;
218 if (is_negative)
219 size_value++;
222 **dest = parameter | (mark_size ? size_value : 0);
223 (*dest)++;
225 while (size_value--)
227 **dest = value & 0xff;
228 (*dest)++;
229 value >>= 8;
233 int usb_hid_request_endpoints(struct usb_class_driver *drv)
235 ep_in = usb_core_request_endpoint(USB_ENDPOINT_XFER_INT, USB_DIR_IN, drv);
236 if (ep_in < 0)
237 return -1;
239 return 0;
242 int usb_hid_set_first_interface(int interface)
244 usb_interface = interface;
246 return interface + 1;
249 #ifdef LOGF_ENABLE
250 static unsigned char buf_dump_ar[BUF_DUMP_NUM_LINES][BUF_DUMP_LINE_SIZE + 1]
251 USB_DEVBSS_ATTR __attribute__((aligned(32)));
253 void buf_dump(unsigned char *buf, size_t size, char *msg)
255 static const char v[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
256 'A', 'B', 'C', 'D', 'E', 'F' };
258 size_t i = 0;
259 for (int line = 0; i < size; line++)
261 int i0 = i;
262 char *b = buf_dump_ar[line];
264 for (int 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= descriptor_report_get(report_descriptor);
561 logf("hid: desc len %u", hid_descriptor.wDescriptorLength0);
562 buf_dump(report_descriptor, hid_descriptor.wDescriptorLength0, "desc");
564 PACK_DATA(dest, hid_descriptor);
567 int usb_hid_get_config_descriptor(unsigned char *dest, int max_packet_size)
569 (void)max_packet_size;
571 unsigned char *orig_dest = dest;
573 logf("hid: config desc.");
575 /* Interface descriptor */
576 interface_descriptor.bInterfaceNumber = usb_interface;
577 PACK_DATA(&dest, interface_descriptor);
579 /* HID descriptor */
580 descriptor_hid_get(&dest);
582 /* Endpoint descriptor */
583 endpoint_descriptor.wMaxPacketSize = 8;
584 endpoint_descriptor.bInterval = 8;
585 endpoint_descriptor.bEndpointAddress = ep_in;
586 PACK_DATA(&dest, endpoint_descriptor);
588 return (int)(dest - orig_dest);
591 void usb_hid_init_connection(void)
593 logf("hid: init connection");
595 active = true;
596 currently_sending = false;
599 /* called by usb_core_init() */
600 void usb_hid_init(void)
602 logf("hid: init");
604 for (int i = 0; i < HID_NUM_BUFFERS; i++)
605 send_buffer_len[i] = 0;
607 cur_buf_prepare = 0;
608 cur_buf_send = 0;
610 active = true;
611 currently_sending = false;
614 void usb_hid_disconnect(void)
616 logf("hid: disconnect");
617 active = false;
618 currently_sending = false;
621 /* called by usb_core_transfer_complete() */
622 void usb_hid_transfer_complete(int ep, int dir, int status, int length)
624 (void)ep;
625 (void)length;
627 logf("HID: transfer complete: %d %d %d %d",ep,dir,status,length);
628 if (dir == USB_DIR_IN && !status)
630 send_buffer_len[cur_buf_send] = 0;
631 HID_BUF_INC(cur_buf_send);
632 currently_sending = false;
633 usb_hid_try_send_drv();
637 /* The DAP is registered as a keyboard with several LEDs, therefore the OS sends
638 * LED report to notify the DAP whether Num Lock / Caps Lock etc. are enabled.
639 * In order to allow sending info to the DAP, the Set Report mechanism can be
640 * used by defining vendor specific output reports and send them from the host
641 * to the DAP using the host's custom driver */
642 static int usb_hid_set_report(struct usb_ctrlrequest *req)
644 static unsigned char buf[SET_REPORT_BUF_LEN] USB_DEVBSS_ATTR
645 __attribute__((aligned(32)));
646 int length;
648 if ((req->wValue >> 8) != REPORT_TYPE_OUTPUT)
650 logf("Unsopported report type");
651 return 1;
653 if ((req->wValue & 0xff) != REPORT_ID_KEYBOARD)
655 logf("Wrong report id");
656 return 2;
658 if (req->wIndex != (uint16_t)usb_interface)
660 logf("Wrong interface");
661 return 3;
663 length = req->wLength;
664 if (length != SET_REPORT_BUF_LEN)
666 logf("Wrong length");
667 return 4;
670 memset(buf, 0, length);
671 usb_drv_recv(EP_CONTROL, buf, length);
673 #ifdef LOGF_ENABLE
674 if (buf[1] & 0x01)
675 logf("Num Lock enabled");
676 if (buf[1] & 0x02)
677 logf("Caps Lock enabled");
678 if (buf[1] & 0x04)
679 logf("Scroll Lock enabled");
680 if (buf[1] & 0x08)
681 logf("Compose enabled");
682 if (buf[1] & 0x10)
683 logf("Kana enabled");
684 #endif
686 /* Defining other LEDs and setting them from the USB host (OS) can be used
687 * to send messages to the DAP */
688 return 0;
691 /* called by usb_core_control_request() */
692 bool usb_hid_control_request(struct usb_ctrlrequest *req, unsigned char *dest)
694 switch (req->bRequestType & USB_TYPE_MASK)
696 case USB_TYPE_STANDARD:
698 unsigned char *orig_dest = dest;
699 uint8_t type = req->wValue >> 8;
700 size_t len;
701 logf("hid: type %d %s", type, (type == USB_DT_HID) ? "hid" :
702 ((type == USB_DT_REPORT) ? "report" : ""));
703 switch (type)
705 case USB_DT_HID:
706 descriptor_hid_get(&dest);
707 break;
708 case USB_DT_REPORT:
709 len = descriptor_report_get(report_descriptor);
710 memcpy(dest, &report_descriptor, len);
711 dest += len;
712 break;
715 if (dest != orig_dest)
717 usb_drv_recv(EP_CONTROL, NULL, 0); /* ack */
718 usb_drv_send(EP_CONTROL, orig_dest, dest - orig_dest);
719 return true;
721 break;
724 case USB_TYPE_CLASS:
726 logf("req %d %s", req->bRequest,
727 (req->bRequest == USB_HID_SET_IDLE) ? "set idle" :
728 ((req->bRequest == USB_HID_SET_REPORT) ? "set report" : ""));
729 switch (req->bRequest)
731 case USB_HID_SET_REPORT:
732 if (usb_hid_set_report(req))
733 break;
734 case USB_HID_SET_IDLE:
735 usb_drv_send(EP_CONTROL, NULL, 0); /* ack */
736 return true;
738 break;
741 case USB_TYPE_VENDOR:
742 logf("hid: unsup. ven. req");
743 break;
745 return false;
748 static void usb_hid_try_send_drv(void)
750 int rc;
751 int length = send_buffer_len[cur_buf_send];
753 if (!length)
754 return;
756 if (currently_sending)
758 logf("HID: Already sending");
759 return;
762 logf("HID: Sending %d bytes",length);
763 rc = usb_drv_send_nonblocking(ep_in, send_buffer[cur_buf_send], length);
764 currently_sending = true;
765 if (rc)
766 send_buffer_len[cur_buf_send] = 0;
769 static void usb_hid_queue(unsigned char *data, int length)
771 if (!active || length <= 0)
772 return;
774 /* Buffer overflow - item still in use */
775 if (send_buffer_len[cur_buf_prepare])
776 return;
778 /* Prepare buffer for sending */
779 if (length > HID_BUF_SIZE_MSG)
780 length = HID_BUF_SIZE_MSG;
781 memcpy(send_buffer[cur_buf_prepare], data, length);
782 send_buffer_len[cur_buf_prepare] = length;
784 HID_BUF_INC(cur_buf_prepare);
787 void usb_hid_send(usage_page_t usage_page, int id)
789 uint8_t length;
790 static unsigned char buf[HID_BUF_SIZE_CMD] USB_DEVBSS_ATTR
791 __attribute__((aligned(32)));
792 usb_hid_report_t *report = NULL;
794 for (uint8_t report_id = 1; report_id < REPORT_ID_COUNT; report_id++)
795 if (usb_hid_reports[report_id].usage_page == usage_page)
797 report = &usb_hid_reports[report_id];
798 buf[0] = report_id;
799 break;
802 if (!report)
804 logf("Unsupported usage_page");
805 return;
808 logf("Sending cmd 0x%x", id);
809 length = report->buf_set(&buf[1], id) + 1;
810 logf("length %u", length);
812 if (!length)
813 return;
815 /* Key pressed */
816 buf_dump(buf, length, "key press");
817 usb_hid_queue(buf, length);
819 if (report->is_key_released)
821 /* Key released */
822 memset(&buf[1], 0, length-1);
824 buf_dump(buf, length, "key release");
825 usb_hid_queue(buf, length);
828 usb_hid_try_send_drv();