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
)
257 static const char v
[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
258 'A', 'B', 'C', 'D', 'E', 'F' };
260 for (i
= 0, line
= 0; i
< size
; line
++)
263 char *b
= buf_dump_ar
[line
];
265 for (j
= 0; j
< BUF_DUMP_ITEMS_IN_LINE
&& i
< size
; j
++, i
++)
267 *b
++ = v
[buf
[i
] >> 4];
268 *b
++ = v
[buf
[i
] & 0xF];
273 /* Prefix must be of len BUF_DUMP_PREFIX_SIZE */
274 logf("%03d: %s %s", i0
, buf_dump_ar
[line
], msg
);
279 #define buf_dump(...)
282 #define BUF_LEN_KEYBOARD 7
283 static uint8_t buf_set_keyboard(unsigned char *buf
, int id
)
285 memset(buf
, 0, BUF_LEN_KEYBOARD
);
288 /* Each key is a word in id (up to 4 keys supported) */
289 while ((key
= id
& 0xff))
291 /* Each modifier key is a bit in the first byte */
292 if (HID_KEYBOARD_LEFT_CONTROL
<= key
&& key
<= HID_KEYBOARD_RIGHT_GUI
)
293 buf
[0] |= (1 << (key
- HID_KEYBOARD_LEFT_CONTROL
));
294 else /* Any other key should be set in a separate byte */
295 buf
[i
++] = (uint8_t)key
;
300 return BUF_LEN_KEYBOARD
;
303 #define BUF_LEN_CONSUMER 4
304 static uint8_t buf_set_consumer(unsigned char *buf
, int id
)
306 memset(buf
, 0, BUF_LEN_CONSUMER
);
307 buf
[0] = (uint8_t)id
;
309 return BUF_LEN_CONSUMER
;
312 #ifdef HAVE_USB_HID_MOUSE
313 #define MOUSE_WHEEL_STEP 1
315 #define MOUSE_BUTTON_LEFT 0x1
316 #define MOUSE_BUTTON_RIGHT 0x2
317 //#define MOUSE_BUTTON_MIDDLE 0x4
318 #define MOUSE_BUTTON 0
321 #define MOUSE_WHEEL 3
322 #define BUF_LEN_MOUSE 4
323 #define MOUSE_ACCEL_FACTOR(count) ((count) / 2)
324 #define MOUSE_DO(item, step) (buf[(item)] = ((uint8_t)(step)))
326 static uint8_t buf_set_mouse(unsigned char *buf
, int id
)
328 static int count
= 0;
329 int step
= MOUSE_STEP
;
331 memset(buf
, 0, BUF_LEN_MOUSE
);
333 /* Set proper button */
336 case HID_MOUSE_BUTTON_LEFT
:
337 case HID_MOUSE_LDRAG_UP
:
338 case HID_MOUSE_LDRAG_UP_REP
:
339 case HID_MOUSE_LDRAG_DOWN
:
340 case HID_MOUSE_LDRAG_DOWN_REP
:
341 case HID_MOUSE_LDRAG_LEFT
:
342 case HID_MOUSE_LDRAG_LEFT_REP
:
343 case HID_MOUSE_LDRAG_RIGHT
:
344 case HID_MOUSE_LDRAG_RIGHT_REP
:
345 MOUSE_DO(MOUSE_BUTTON
, MOUSE_BUTTON_LEFT
);
347 case HID_MOUSE_BUTTON_RIGHT
:
348 case HID_MOUSE_RDRAG_UP
:
349 case HID_MOUSE_RDRAG_UP_REP
:
350 case HID_MOUSE_RDRAG_DOWN
:
351 case HID_MOUSE_RDRAG_DOWN_REP
:
352 case HID_MOUSE_RDRAG_LEFT
:
353 case HID_MOUSE_RDRAG_LEFT_REP
:
354 case HID_MOUSE_RDRAG_RIGHT
:
355 case HID_MOUSE_RDRAG_RIGHT_REP
:
356 MOUSE_DO(MOUSE_BUTTON
, MOUSE_BUTTON_RIGHT
);
358 case HID_MOUSE_BUTTON_LEFT_REL
:
359 case HID_MOUSE_BUTTON_RIGHT_REL
:
360 /* Keep buf empty, to mark mouse button release */
366 /* Handle mouse accelarated movement */
372 case HID_MOUSE_RIGHT
:
373 case HID_MOUSE_LDRAG_UP
:
374 case HID_MOUSE_LDRAG_DOWN
:
375 case HID_MOUSE_LDRAG_LEFT
:
376 case HID_MOUSE_LDRAG_RIGHT
:
377 case HID_MOUSE_RDRAG_UP
:
378 case HID_MOUSE_RDRAG_DOWN
:
379 case HID_MOUSE_RDRAG_LEFT
:
380 case HID_MOUSE_RDRAG_RIGHT
:
383 case HID_MOUSE_UP_REP
:
384 case HID_MOUSE_DOWN_REP
:
385 case HID_MOUSE_LEFT_REP
:
386 case HID_MOUSE_RIGHT_REP
:
387 case HID_MOUSE_LDRAG_UP_REP
:
388 case HID_MOUSE_LDRAG_DOWN_REP
:
389 case HID_MOUSE_LDRAG_LEFT_REP
:
390 case HID_MOUSE_LDRAG_RIGHT_REP
:
391 case HID_MOUSE_RDRAG_UP_REP
:
392 case HID_MOUSE_RDRAG_DOWN_REP
:
393 case HID_MOUSE_RDRAG_LEFT_REP
:
394 case HID_MOUSE_RDRAG_RIGHT_REP
:
396 /* TODO: Find a better mouse accellaration algorithm */
397 step
*= 1 + MOUSE_ACCEL_FACTOR(count
);
405 /* Move/scroll mouse */
409 case HID_MOUSE_UP_REP
:
410 case HID_MOUSE_LDRAG_UP
:
411 case HID_MOUSE_LDRAG_UP_REP
:
412 case HID_MOUSE_RDRAG_UP
:
413 case HID_MOUSE_RDRAG_UP_REP
:
414 MOUSE_DO(MOUSE_Y
, -step
);
417 case HID_MOUSE_DOWN_REP
:
418 case HID_MOUSE_LDRAG_DOWN
:
419 case HID_MOUSE_LDRAG_DOWN_REP
:
420 case HID_MOUSE_RDRAG_DOWN
:
421 case HID_MOUSE_RDRAG_DOWN_REP
:
422 MOUSE_DO(MOUSE_Y
, step
);
425 case HID_MOUSE_LEFT_REP
:
426 case HID_MOUSE_LDRAG_LEFT
:
427 case HID_MOUSE_LDRAG_LEFT_REP
:
428 case HID_MOUSE_RDRAG_LEFT
:
429 case HID_MOUSE_RDRAG_LEFT_REP
:
430 MOUSE_DO(MOUSE_X
, -step
);
432 case HID_MOUSE_RIGHT
:
433 case HID_MOUSE_RIGHT_REP
:
434 case HID_MOUSE_LDRAG_RIGHT
:
435 case HID_MOUSE_LDRAG_RIGHT_REP
:
436 case HID_MOUSE_RDRAG_RIGHT
:
437 case HID_MOUSE_RDRAG_RIGHT_REP
:
438 MOUSE_DO(MOUSE_X
, step
);
440 case HID_MOUSE_SCROLL_UP
:
441 MOUSE_DO(MOUSE_WHEEL
, MOUSE_WHEEL_STEP
);
443 case HID_MOUSE_SCROLL_DOWN
:
444 MOUSE_DO(MOUSE_WHEEL
, -MOUSE_WHEEL_STEP
);
450 return BUF_LEN_MOUSE
;
452 #endif /* HAVE_USB_HID_MOUSE */
454 static size_t descriptor_report_get(unsigned char *dest
)
456 unsigned char *report
= dest
;
457 usb_hid_report_t
*usb_hid_report
;
459 /* Keyboard control */
460 usb_hid_report
= &usb_hid_reports
[REPORT_ID_KEYBOARD
];
461 usb_hid_report
->usage_page
= HID_USAGE_PAGE_KEYBOARD_KEYPAD
;
462 usb_hid_report
->buf_set
= buf_set_keyboard
;
463 usb_hid_report
->is_key_released
= 1;
465 pack_parameter(&report
, 0, 1, USAGE_PAGE
,
466 HID_USAGE_PAGE_GENERIC_DESKTOP_CONTROLS
);
467 pack_parameter(&report
, 0, 0, CONSUMER_USAGE
, HID_GENERIC_DESKTOP_KEYBOARD
);
468 pack_parameter(&report
, 0, 1, COLLECTION
, COLLECTION_APPLICATION
);
469 pack_parameter(&report
, 0, 1, REPORT_ID
, REPORT_ID_KEYBOARD
);
470 pack_parameter(&report
, 0, 1, USAGE_PAGE
, HID_GENERIC_DESKTOP_KEYPAD
);
471 pack_parameter(&report
, 0, 1, USAGE_MINIMUM
, HID_KEYBOARD_LEFT_CONTROL
);
472 pack_parameter(&report
, 0, 1, USAGE_MAXIMUM
, HID_KEYBOARD_RIGHT_GUI
);
473 pack_parameter(&report
, 1, 1, LOGICAL_MINIMUM
, 0);
474 pack_parameter(&report
, 1, 1, LOGICAL_MAXIMUM
, 1);
475 pack_parameter(&report
, 0, 1, REPORT_SIZE
, 1);
476 pack_parameter(&report
, 0, 1, REPORT_COUNT
, 8);
477 pack_parameter(&report
, 0, 1, INPUT
, MAIN_ITEM_VARIABLE
);
478 pack_parameter(&report
, 0, 1, REPORT_SIZE
, 1);
479 pack_parameter(&report
, 0, 1, REPORT_COUNT
, 5);
480 pack_parameter(&report
, 0, 1, USAGE_PAGE
, HID_USAGE_PAGE_LEDS
);
481 pack_parameter(&report
, 0, 1, USAGE_MINIMUM
, HID_LED_NUM_LOCK
);
482 pack_parameter(&report
, 0, 1, USAGE_MAXIMUM
, HID_LED_KANA
);
483 pack_parameter(&report
, 0, 1, OUTPUT
, MAIN_ITEM_VARIABLE
);
484 pack_parameter(&report
, 0, 1, REPORT_SIZE
, 3);
485 pack_parameter(&report
, 0, 1, REPORT_COUNT
, 1);
486 pack_parameter(&report
, 0, 1, OUTPUT
, MAIN_ITEM_CONSTANT
);
487 pack_parameter(&report
, 0, 1, REPORT_SIZE
, 8);
488 pack_parameter(&report
, 0, 1, REPORT_COUNT
, 6);
489 pack_parameter(&report
, 1, 1, LOGICAL_MINIMUM
, 0);
490 pack_parameter(&report
, 1, 1, LOGICAL_MAXIMUM
, HID_KEYBOARD_EX_SEL
);
491 pack_parameter(&report
, 0, 1, USAGE_PAGE
, HID_USAGE_PAGE_KEYBOARD_KEYPAD
);
492 pack_parameter(&report
, 0, 1, USAGE_MINIMUM
, 0);
493 pack_parameter(&report
, 0, 1, USAGE_MAXIMUM
, HID_KEYBOARD_EX_SEL
);
494 pack_parameter(&report
, 0, 1, INPUT
, 0);
495 PACK_VAL(report
, END_COLLECTION
);
497 /* Consumer usage controls - play/pause, stop, etc. */
498 usb_hid_report
= &usb_hid_reports
[REPORT_ID_CONSUMER
];
499 usb_hid_report
->usage_page
= HID_USAGE_PAGE_CONSUMER
;
500 usb_hid_report
->buf_set
= buf_set_consumer
;
501 usb_hid_report
->is_key_released
= 1;
503 pack_parameter(&report
, 0, 1, USAGE_PAGE
, HID_USAGE_PAGE_CONSUMER
);
504 pack_parameter(&report
, 0, 0, CONSUMER_USAGE
,
505 HID_CONSUMER_USAGE_CONSUMER_CONTROL
);
506 pack_parameter(&report
, 0, 1, COLLECTION
, COLLECTION_APPLICATION
);
507 pack_parameter(&report
, 0, 1, REPORT_ID
, REPORT_ID_CONSUMER
);
508 pack_parameter(&report
, 0, 1, REPORT_SIZE
, 16);
509 pack_parameter(&report
, 0, 1, REPORT_COUNT
, 2);
510 pack_parameter(&report
, 1, 1, LOGICAL_MINIMUM
, 1);
511 pack_parameter(&report
, 1, 1, LOGICAL_MAXIMUM
, 652);
512 pack_parameter(&report
, 0, 1, USAGE_MINIMUM
,
513 HID_CONSUMER_USAGE_CONSUMER_CONTROL
);
514 pack_parameter(&report
, 0, 1, USAGE_MAXIMUM
, HID_CONSUMER_USAGE_AC_SEND
);
515 pack_parameter(&report
, 0, 1, INPUT
, MAIN_ITEM_NO_PREFERRED
|
516 MAIN_ITEM_NULL_STATE
);
517 PACK_VAL(report
, END_COLLECTION
);
519 #ifdef HAVE_USB_HID_MOUSE
521 usb_hid_report
= &usb_hid_reports
[REPORT_ID_MOUSE
];
522 usb_hid_report
->usage_page
= HID_USAGE_PAGE_GENERIC_DESKTOP_CONTROLS
;
523 usb_hid_report
->buf_set
= buf_set_mouse
;
524 usb_hid_report
->is_key_released
= 0;
526 pack_parameter(&report
, 0, 1, USAGE_PAGE
,
527 HID_USAGE_PAGE_GENERIC_DESKTOP_CONTROLS
);
528 pack_parameter(&report
, 0, 0, CONSUMER_USAGE
, HID_GENERIC_DESKTOP_MOUSE
);
529 pack_parameter(&report
, 0, 1, COLLECTION
, COLLECTION_APPLICATION
);
530 pack_parameter(&report
, 0, 1, REPORT_ID
, REPORT_ID_MOUSE
);
531 pack_parameter(&report
, 0, 0, CONSUMER_USAGE
, HID_GENERIC_DESKTOP_POINTER
);
532 pack_parameter(&report
, 0, 1, COLLECTION
, COLLECTION_PHYSICAL
);
533 pack_parameter(&report
, 0, 1, USAGE_PAGE
, HID_USAGE_PAGE_BUTTON
);
534 pack_parameter(&report
, 0, 1, USAGE_MINIMUM
, 1);
535 pack_parameter(&report
, 0, 1, USAGE_MAXIMUM
, 8);
536 pack_parameter(&report
, 1, 1, LOGICAL_MINIMUM
, 0);
537 pack_parameter(&report
, 1, 1, LOGICAL_MAXIMUM
, 1);
538 pack_parameter(&report
, 0, 1, REPORT_SIZE
, 1);
539 pack_parameter(&report
, 0, 1, REPORT_COUNT
, 8);
540 pack_parameter(&report
, 0, 1, INPUT
, MAIN_ITEM_VARIABLE
);
541 pack_parameter(&report
, 0, 1, USAGE_PAGE
,
542 HID_USAGE_PAGE_GENERIC_DESKTOP_CONTROLS
);
543 pack_parameter(&report
, 0, 0, CONSUMER_USAGE
, HID_GENERIC_DESKTOP_X
);
544 pack_parameter(&report
, 0, 0, CONSUMER_USAGE
, HID_GENERIC_DESKTOP_Y
);
545 pack_parameter(&report
, 0, 0, CONSUMER_USAGE
, HID_GENERIC_DESKTOP_WHEEL
);
546 pack_parameter(&report
, 0, 1, LOGICAL_MINIMUM
, -127 & 0xFF);
547 pack_parameter(&report
, 0, 1, LOGICAL_MAXIMUM
, 127);
548 pack_parameter(&report
, 0, 1, REPORT_SIZE
, 8);
549 pack_parameter(&report
, 0, 1, REPORT_COUNT
, 3);
550 pack_parameter(&report
, 0, 1, INPUT
, MAIN_ITEM_VARIABLE
| MAIN_ITEM_RELATIVE
);
551 PACK_VAL(report
, END_COLLECTION
);
552 PACK_VAL(report
, END_COLLECTION
);
553 #endif /* HAVE_USB_HID_MOUSE */
555 return (size_t)(report
- dest
);
558 static void descriptor_hid_get(unsigned char **dest
)
560 hid_descriptor
.wDescriptorLength0
=
561 (uint16_t)descriptor_report_get(report_descriptor
);
563 logf("hid: desc len %u", hid_descriptor
.wDescriptorLength0
);
564 buf_dump(report_descriptor
, hid_descriptor
.wDescriptorLength0
, "desc");
566 PACK_DATA(*dest
, hid_descriptor
);
569 int usb_hid_get_config_descriptor(unsigned char *dest
, int max_packet_size
)
571 unsigned char *orig_dest
= dest
;
573 logf("hid: config desc.");
575 /* Ignore given max_packet_size */
576 (void)max_packet_size
;
578 /* Interface descriptor */
579 interface_descriptor
.bInterfaceNumber
= usb_interface
;
580 PACK_DATA(dest
, interface_descriptor
);
583 descriptor_hid_get(&dest
);
585 /* Endpoint descriptor */
586 endpoint_descriptor
.wMaxPacketSize
= 8;
587 endpoint_descriptor
.bInterval
= 8;
588 endpoint_descriptor
.bEndpointAddress
= ep_in
;
589 PACK_DATA(dest
, endpoint_descriptor
);
591 return (int)(dest
- orig_dest
);
594 void usb_hid_init_connection(void)
596 logf("hid: init connection");
599 currently_sending
= false;
602 /* called by usb_core_init() */
603 void usb_hid_init(void)
609 for (i
= 0; i
< HID_NUM_BUFFERS
; i
++)
610 send_buffer_len
[i
] = 0;
616 currently_sending
= false;
619 void usb_hid_disconnect(void)
621 logf("hid: disconnect");
623 currently_sending
= false;
626 /* called by usb_core_transfer_complete() */
627 void usb_hid_transfer_complete(int ep
, int dir
, int status
, int length
)
634 logf("HID: transfer complete: %d %d %d %d",ep
,dir
,status
,length
);
644 send_buffer_len
[cur_buf_send
] = 0;
645 HID_BUF_INC(cur_buf_send
);
646 currently_sending
= false;
647 usb_hid_try_send_drv();
653 /* The DAP is registered as a keyboard with several LEDs, therefore the OS sends
654 * LED report to notify the DAP whether Num Lock / Caps Lock etc. are enabled.
655 * In order to allow sending info to the DAP, the Set Report mechanism can be
656 * used by defining vendor specific output reports and send them from the host
657 * to the DAP using the host's custom driver */
658 static int usb_hid_set_report(struct usb_ctrlrequest
*req
)
660 static unsigned char buf
[SET_REPORT_BUF_LEN
] USB_DEVBSS_ATTR
661 __attribute__((aligned(32)));
665 if ((req
->wValue
>> 8) != REPORT_TYPE_OUTPUT
)
667 logf("Unsopported report type");
671 if ((req
->wValue
& 0xff) != REPORT_ID_KEYBOARD
)
673 logf("Wrong report id");
677 if (req
->wIndex
!= (uint16_t)usb_interface
)
679 logf("Wrong interface");
683 length
= req
->wLength
;
684 if (length
!= SET_REPORT_BUF_LEN
)
686 logf("Wrong length");
691 memset(buf
, 0, length
);
692 usb_drv_recv(EP_CONTROL
, buf
, length
);
696 logf("Num Lock enabled");
698 logf("Caps Lock enabled");
700 logf("Scroll Lock enabled");
702 logf("Compose enabled");
704 logf("Kana enabled");
707 /* Defining other LEDs and setting them from the USB host (OS) can be used
708 * to send messages to the DAP */
714 /* called by usb_core_control_request() */
715 bool usb_hid_control_request(struct usb_ctrlrequest
*req
, unsigned char *dest
)
717 bool handled
= false;
719 switch (req
->bRequestType
& USB_TYPE_MASK
)
721 case USB_TYPE_STANDARD
:
723 unsigned char *orig_dest
= dest
;
725 switch (req
->wValue
>>8) /* type */
729 logf("hid: type hid");
730 descriptor_hid_get(&dest
);
737 logf("hid: type report");
739 len
= descriptor_report_get(report_descriptor
);
740 memcpy(dest
, &report_descriptor
, len
);
745 logf("hid: unsup. std. req");
748 if (dest
!= orig_dest
)
750 usb_drv_recv(EP_CONTROL
, NULL
, 0); /* ack */
751 usb_drv_send(EP_CONTROL
, orig_dest
, dest
- orig_dest
);
759 switch (req
->bRequest
)
761 case USB_HID_SET_IDLE
:
762 logf("hid: set idle");
763 usb_drv_send(EP_CONTROL
, NULL
, 0); /* ack */
766 case USB_HID_SET_REPORT
:
767 logf("hid: set report");
768 if (!usb_hid_set_report(req
))
770 usb_drv_send(EP_CONTROL
, NULL
, 0); /* ack */
775 logf("%d: unsup. cls. req", req
->bRequest
);
781 case USB_TYPE_VENDOR
:
782 logf("hid: unsup. ven. req");
788 static void usb_hid_try_send_drv(void)
791 int length
= send_buffer_len
[cur_buf_send
];
796 if (currently_sending
)
798 logf("HID: Already sending");
802 logf("HID: Sending %d bytes",length
);
803 rc
= usb_drv_send_nonblocking(ep_in
, send_buffer
[cur_buf_send
], length
);
804 currently_sending
= true;
807 send_buffer_len
[cur_buf_send
] = 0;
812 static void usb_hid_queue(unsigned char *data
, int length
)
814 if (!active
|| length
<= 0)
817 /* Buffer overflow - item still in use */
818 if (send_buffer_len
[cur_buf_prepare
])
821 /* Prepare buffer for sending */
822 if (length
> HID_BUF_SIZE_MSG
)
823 length
= HID_BUF_SIZE_MSG
;
824 memcpy(send_buffer
[cur_buf_prepare
], data
, length
);
825 send_buffer_len
[cur_buf_prepare
] = length
;
827 HID_BUF_INC(cur_buf_prepare
);
830 void usb_hid_send(usage_page_t usage_page
, int id
)
832 uint8_t report_id
, length
;
833 static unsigned char buf
[HID_BUF_SIZE_CMD
] USB_DEVBSS_ATTR
834 __attribute__((aligned(32)));
835 usb_hid_report_t
*report
= NULL
;
837 for (report_id
= 1; report_id
< REPORT_ID_COUNT
; report_id
++)
839 if (usb_hid_reports
[report_id
].usage_page
== usage_page
)
841 report
= &usb_hid_reports
[report_id
];
847 logf("Unsupported usage_page");
851 logf("Sending cmd 0x%x", id
);
853 length
= report
->buf_set(&buf
[1], id
) + 1;
854 logf("length %u", length
);
860 buf_dump(buf
, length
, "key press");
861 usb_hid_queue(buf
, length
);
863 if (report
->is_key_released
)
866 memset(buf
, 0, length
);
869 buf_dump(buf
, length
, "key release");
870 usb_hid_queue(buf
, length
);
873 usb_hid_try_send_drv();