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 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;
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 */
200 bool is_negative
= 0;
205 is_negative
= (int8_t)value
< 0;
208 is_negative
= (int16_t)value
< 0;
212 is_negative
= (int32_t)value
< 0;
222 **dest
= parameter
| (mark_size
? size_value
: 0);
227 **dest
= value
& 0xff;
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
);
242 int usb_hid_set_first_interface(int interface
)
244 usb_interface
= interface
;
246 return interface
+ 1;
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' };
259 for (int line
= 0; i
< size
; line
++)
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];
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)(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
);
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");
596 currently_sending
= false;
599 /* called by usb_core_init() */
600 void usb_hid_init(void)
604 for (int i
= 0; i
< HID_NUM_BUFFERS
; i
++)
605 send_buffer_len
[i
] = 0;
611 currently_sending
= false;
614 void usb_hid_disconnect(void)
616 logf("hid: disconnect");
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
)
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)));
648 if ((req
->wValue
>> 8) != REPORT_TYPE_OUTPUT
)
650 logf("Unsopported report type");
653 if ((req
->wValue
& 0xff) != REPORT_ID_KEYBOARD
)
655 logf("Wrong report id");
658 if (req
->wIndex
!= (uint16_t)usb_interface
)
660 logf("Wrong interface");
663 length
= req
->wLength
;
664 if (length
!= SET_REPORT_BUF_LEN
)
666 logf("Wrong length");
670 memset(buf
, 0, length
);
671 usb_drv_recv(EP_CONTROL
, buf
, length
);
675 logf("Num Lock enabled");
677 logf("Caps Lock enabled");
679 logf("Scroll Lock enabled");
681 logf("Compose enabled");
683 logf("Kana enabled");
686 /* Defining other LEDs and setting them from the USB host (OS) can be used
687 * to send messages to the DAP */
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;
701 logf("hid: type %d %s", type
, (type
== USB_DT_HID
) ? "hid" :
702 ((type
== USB_DT_REPORT
) ? "report" : ""));
706 descriptor_hid_get(&dest
);
709 len
= descriptor_report_get(report_descriptor
);
710 memcpy(dest
, &report_descriptor
, len
);
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
);
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
))
734 case USB_HID_SET_IDLE
:
735 usb_drv_send(EP_CONTROL
, NULL
, 0); /* ack */
741 case USB_TYPE_VENDOR
:
742 logf("hid: unsup. ven. req");
748 static void usb_hid_try_send_drv(void)
751 int length
= send_buffer_len
[cur_buf_send
];
756 if (currently_sending
)
758 logf("HID: Already sending");
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;
766 send_buffer_len
[cur_buf_send
] = 0;
769 static void usb_hid_queue(unsigned char *data
, int length
)
771 if (!active
|| length
<= 0)
774 /* Buffer overflow - item still in use */
775 if (send_buffer_len
[cur_buf_prepare
])
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
)
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
];
804 logf("Unsupported usage_page");
808 logf("Sending cmd 0x%x", id
);
809 length
= report
->buf_set(&buf
[1], id
) + 1;
810 logf("length %u", length
);
816 buf_dump(buf
, length
, "key press");
817 usb_hid_queue(buf
, length
);
819 if (report
->is_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();