1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
27 #include "usb_class_driver.h"
28 /*#define LOGF_ENABLE*/
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) */
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
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)
102 #define HID_BUF_INC(i) do { (i) = ((i) + 1) % HID_NUM_BUFFERS; } while (0)
103 #define PACK_VAL(dest, val) *(dest)++ = (val) & 0xff
107 REPORT_ID_KEYBOARD
= 1,
109 #ifdef HAVE_USB_HID_MOUSE
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,
124 .bInterfaceClass
= USB_CLASS_HID
,
125 .bInterfaceSubClass
= SUBCLASS_BOOT_INTERFACE
,
126 .bInterfaceProtocol
= PROTOCOL_CODE_KEYBOARD
,
130 struct usb_hid_descriptor
{
132 uint8_t bDescriptorType
;
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
,
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
,
165 /* Write the id into the buffer in the appropriate location, and returns the
167 uint8_t (*buf_set
)(unsigned char *buf
, int id
);
168 bool is_key_released
;
171 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 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;
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 */
199 bool is_negative
= 0;
204 is_negative
= (int8_t)value
< 0;
207 is_negative
= (int16_t)value
< 0;
211 is_negative
= (int32_t)value
< 0;
221 **dest
= parameter
| (mark_size
? size_value
: 0);
226 **dest
= value
& 0xff;
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
);
241 int usb_hid_set_first_interface(int interface
)
243 usb_interface
= interface
;
245 return interface
+ 1;
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
)
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
++)
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];
272 /* Prefix must be of len BUF_DUMP_PREFIX_SIZE */
273 logf("%03d: %s %s", i0
, buf_dump_ar
[line
], msg
);
278 #define buf_dump(...)
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
);
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
;
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
314 #define MOUSE_BUTTON_LEFT 0x1
315 #define MOUSE_BUTTON_RIGHT 0x2
316 //#define MOUSE_BUTTON_MIDDLE 0x4
317 #define MOUSE_BUTTON 0
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 */
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
);
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
);
357 case HID_MOUSE_BUTTON_LEFT_REL
:
358 case HID_MOUSE_BUTTON_RIGHT_REL
:
359 /* Keep buf empty, to mark mouse button release */
365 /* Handle mouse accelarated movement */
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
:
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
:
395 /* TODO: Find a better mouse accellaration algorithm */
396 step
*= 1 + MOUSE_ACCEL_FACTOR(count
);
404 /* Move/scroll mouse */
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
);
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
);
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
);
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
);
439 case HID_MOUSE_SCROLL_UP
:
440 MOUSE_DO(MOUSE_WHEEL
, MOUSE_WHEEL_STEP
);
442 case HID_MOUSE_SCROLL_DOWN
:
443 MOUSE_DO(MOUSE_WHEEL
, -MOUSE_WHEEL_STEP
);
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
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)((uint32_t)report
- (uint32_t)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
);
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");
600 /* called by usb_core_init() */
601 void usb_hid_init(void)
607 for (i
= 0; i
< HID_NUM_BUFFERS
; i
++)
608 send_buffer_len
[i
] = 0;
616 void usb_hid_disconnect(void)
618 logf("hid: disconnect");
622 /* called by usb_core_transfer_complete() */
623 void usb_hid_transfer_complete(int ep
, int dir
, int status
, int length
)
639 send_buffer_len
[cur_buf_send
] = 0;
640 HID_BUF_INC(cur_buf_send
);
641 usb_hid_try_send_drv();
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)));
659 if ((req
->wValue
>> 8) != REPORT_TYPE_OUTPUT
)
661 logf("Unsopported report type");
665 if ((req
->wValue
& 0xff) != REPORT_ID_KEYBOARD
)
667 logf("Wrong report id");
671 if (req
->wIndex
!= (uint16_t)usb_interface
)
673 logf("Wrong interface");
677 length
= req
->wLength
;
678 if (length
!= SET_REPORT_BUF_LEN
)
680 logf("Wrong length");
685 memset(buf
, 0, length
);
686 usb_drv_recv(EP_CONTROL
, buf
, length
);
690 logf("Num Lock enabled");
692 logf("Caps Lock enabled");
694 logf("Scroll Lock enabled");
696 logf("Compose enabled");
698 logf("Kana enabled");
701 /* Defining other LEDs and setting them from the USB host (OS) can be used
702 * to send messages to the DAP */
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 */
723 logf("hid: type hid");
724 descriptor_hid_get(&dest
);
731 logf("hid: type report");
733 len
= descriptor_report_get(report_descriptor
);
734 memcpy(dest
, &report_descriptor
, len
);
739 logf("hid: unsup. std. req");
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
);
753 switch (req
->bRequest
)
755 case USB_HID_SET_IDLE
:
756 logf("hid: set idle");
757 usb_drv_send(EP_CONTROL
, NULL
, 0); /* ack */
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 */
769 logf("%d: unsup. cls. req", req
->bRequest
);
775 case USB_TYPE_VENDOR
:
776 logf("hid: unsup. ven. req");
782 static void usb_hid_try_send_drv(void)
785 int length
= send_buffer_len
[cur_buf_send
];
790 rc
= usb_drv_send_nonblocking(ep_in
, send_buffer
[cur_buf_send
], length
);
793 send_buffer_len
[cur_buf_send
] = 0;
798 static void usb_hid_queue(unsigned char *data
, int length
)
800 if (!active
|| length
<= 0)
803 /* Buffer overflow - item still in use */
804 if (send_buffer_len
[cur_buf_prepare
])
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
];
833 logf("Unsupported usage_page");
837 logf("Sending cmd 0x%x", id
);
839 length
= report
->buf_set(&buf
[1], id
) + 1;
840 logf("length %u", length
);
846 buf_dump(buf
, length
, "key press");
847 usb_hid_queue(buf
, length
);
849 if (report
->is_key_released
)
852 memset(buf
, 0, length
);
855 buf_dump(buf
, length
, "key release");
856 usb_hid_queue(buf
, length
);
859 usb_hid_try_send_drv();