FS#8961 - Anti-Aliased Fonts.
[kugel-rb.git] / firmware / usbstack / usb_hid.c
blob4eb25841bb5b772b6d00aee0fa976e9fce86c8de
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 #define CONCAT(low, high) ((high << 8) | low)
32 #define PACK_VAL1(dest, val) *(dest)++ = (val) & 0xff
33 #define PACK_VAL2(dest, val) PACK_VAL1((dest), (val)); \
34 PACK_VAL1((dest), (val >> 8))
36 /* Documents avaiable here: http://www.usb.org/developers/devclass_docs/ */
38 #define HID_VER 0x0110 /* 1.1 */
39 /* Subclass Codes (HID1_11.pdf, page 18) */
40 #define SUBCLASS_NONE 0
41 #define SUBCLASS_BOOT_INTERFACE 1
42 /* Protocol Codes (HID1_11.pdf, page 19) */
43 #define PROTOCOL_CODE_NONE 0
44 #define PROTOCOL_CODE_KEYBOARD 1
45 #define PROTOCOL_CODE_MOUSE 2
46 /* HID main items (HID1_11.pdf, page 38) */
47 #define INPUT 0x80
48 #define OUTPUT 0x90
49 #define COLLECTION 0xA0
50 #define COLLECTION_APPLICATION 0x01
51 #define END_COLLECTION 0xC0
52 /* Parts (HID1_11.pdf, page 40) */
53 #define MAIN_ITEM_CONSTANT BIT_N(0) /* 0x01 */
54 #define MAIN_ITEM_VARIABLE BIT_N(1) /* 0x02 */
55 #define MAIN_ITEM_RELATIVE BIT_N(2) /* 0x04 */
56 #define MAIN_ITEM_WRAP BIT_N(3) /* 0x08 */
57 #define MAIN_ITEM_NONLINEAR BIT_N(4) /* 0x10 */
58 #define MAIN_ITEM_NO_PREFERRED BIT_N(5) /* 0x20 */
59 #define MAIN_ITEM_NULL_STATE BIT_N(6) /* 0x40 */
60 #define MAIN_ITEM_VOLATILE BIT_N(7) /* 0x80 */
61 #define MAIN_ITEM_BUFFERED_BYTES BIT_N(8) /* 0x0100 */
62 /* HID global items (HID1_11.pdf, page 45) */
63 #define USAGE_PAGE 0x04
64 #define LOGICAL_MINIMUM 0x14
65 #define LOGICAL_MAXIMUM 0x24
66 #define REPORT_SIZE 0x74
67 #define REPORT_ID 0x84
68 #define REPORT_COUNT 0x94
69 /* HID local items (HID1_11.pdf, page 50) */
70 #define USAGE_MINIMUM 0x18
71 #define USAGE_MAXIMUM 0x28
72 /* Types of class descriptors (HID1_11.pdf, page 59) */
73 #define USB_DT_HID 0x21
74 #define USB_DT_REPORT 0x22
76 #define CONSUMER_USAGE 0x09
78 /* HID-only class specific requests (HID1_11.pdf, page 61) */
79 #define USB_HID_GET_REPORT 0x01
80 #define USB_HID_GET_IDLE 0x02
81 #define USB_HID_GET_PROTOCOL 0x03
82 #define USB_HID_SET_REPORT 0x09
83 #define USB_HID_SET_IDLE 0x0A
84 #define USB_HID_SET_PROTOCOL 0x0B
86 /* Get_Report and Set_Report Report Type field (HID1_11.pdf, page 61) */
87 #define REPORT_TYPE_INPUT 0x01
88 #define REPORT_TYPE_OUTPUT 0x02
89 #define REPORT_TYPE_FEATURE 0x03
91 #define HID_BUF_SIZE_MSG 16
92 #define HID_BUF_SIZE_CMD 16
93 #define HID_BUF_SIZE_REPORT 96
94 #define HID_NUM_BUFFERS 5
95 #define SET_REPORT_BUF_LEN 2
97 #ifdef LOGF_ENABLE
98 #define BUF_DUMP_BUF_LEN HID_BUF_SIZE_REPORT
99 #define BUF_DUMP_PREFIX_SIZE 5
100 #define BUF_DUMP_LINE_SIZE (MAX_LOGF_ENTRY - BUF_DUMP_PREFIX_SIZE)
101 #define BUF_DUMP_ITEMS_IN_LINE (BUF_DUMP_LINE_SIZE / 3)
102 #define BUF_DUMP_NUM_LINES (BUF_DUMP_BUF_LEN / (BUF_DUMP_LINE_SIZE / 3))
103 #endif
105 #define HID_BUF_INC(i) do { (i) = ((i) + 1) % HID_NUM_BUFFERS; } while (0)
107 typedef enum
109 REPORT_ID_KEYBOARD = 1,
110 REPORT_ID_CONSUMER,
111 REPORT_ID_COUNT,
112 } report_id_t;
114 /* hid interface */
115 static struct usb_interface_descriptor __attribute__((aligned(2)))
116 interface_descriptor =
118 .bLength = sizeof(struct usb_interface_descriptor),
119 .bDescriptorType = USB_DT_INTERFACE,
120 .bInterfaceNumber = 0,
121 .bAlternateSetting = 0,
122 .bNumEndpoints = 1,
123 .bInterfaceClass = USB_CLASS_HID,
124 .bInterfaceSubClass = SUBCLASS_BOOT_INTERFACE,
125 .bInterfaceProtocol = PROTOCOL_CODE_KEYBOARD,
126 .iInterface = 0
129 struct usb_hid_descriptor {
130 uint8_t bLength;
131 uint8_t bDescriptorType;
132 uint16_t wBcdHID;
133 uint8_t bCountryCode;
134 uint8_t bNumDescriptors;
135 uint8_t bDescriptorType0;
136 uint16_t wDescriptorLength0;
137 } __attribute__ ((packed));
139 static struct usb_hid_descriptor __attribute__((aligned(2))) hid_descriptor =
141 .bLength = sizeof(struct usb_hid_descriptor),
142 .bDescriptorType = USB_DT_HID,
143 .wBcdHID = HID_VER,
144 .bCountryCode = 0,
145 .bNumDescriptors = 1,
146 .bDescriptorType0 = USB_DT_REPORT,
147 .wDescriptorLength0 = 0
150 static struct usb_endpoint_descriptor __attribute__((aligned(2)))
151 endpoint_descriptor =
153 .bLength = sizeof(struct usb_endpoint_descriptor),
154 .bDescriptorType = USB_DT_ENDPOINT,
155 .bEndpointAddress = 0,
156 .bmAttributes = USB_ENDPOINT_XFER_INT,
157 .wMaxPacketSize = 0,
158 .bInterval = 0
161 typedef struct
163 uint8_t usage_page;
164 /* Write the id into the buffer in the appropriate location, and returns the
165 * buffer length */
166 uint8_t (*buf_set)(unsigned char *buf, int id);
167 } usb_hid_report_t;
169 usb_hid_report_t usb_hid_reports[REPORT_ID_COUNT];
171 static unsigned char report_descriptor[HID_BUF_SIZE_REPORT]
172 USB_DEVBSS_ATTR __attribute__((aligned(32)));
174 static unsigned char send_buffer[HID_NUM_BUFFERS][HID_BUF_SIZE_MSG]
175 USB_DEVBSS_ATTR __attribute__((aligned(32)));
176 size_t send_buffer_len[HID_NUM_BUFFERS];
177 static int cur_buf_prepare;
178 static int cur_buf_send;
180 static bool active = false;
181 static int ep_in;
182 static int usb_interface;
184 static void usb_hid_try_send_drv(void);
186 static void pack_parameter(unsigned char **dest, bool is_signed,
187 uint8_t parameter, uint32_t value)
189 uint8_t size_value = 1; /* # of bytes */
190 uint32_t v = value;
192 while (v >>= 8)
193 size_value++;
195 if (is_signed)
197 bool is_negative = 0;
199 switch (size_value)
201 case 1:
202 is_negative = (int8_t)value < 0;
203 break;
204 case 2:
205 is_negative = (int16_t)value < 0;
206 break;
207 case 3:
208 case 4:
209 is_negative = (int32_t)value < 0;
210 break;
211 default:
212 break;
215 if (is_negative)
216 size_value++;
219 **dest = parameter | size_value;
220 (*dest)++;
222 while (size_value--)
224 **dest = value & 0xff;
225 (*dest)++;
226 value >>= 8;
230 int usb_hid_request_endpoints(struct usb_class_driver *drv)
232 ep_in = usb_core_request_endpoint(USB_ENDPOINT_XFER_INT, USB_DIR_IN, drv);
233 if (ep_in < 0)
234 return -1;
236 return 0;
239 int usb_hid_set_first_interface(int interface)
241 usb_interface = interface;
243 return interface + 1;
246 #ifdef LOGF_ENABLE
247 static unsigned char
248 buf_dump_ar[BUF_DUMP_NUM_LINES][BUF_DUMP_LINE_SIZE + 1]
249 USB_DEVBSS_ATTR __attribute__((aligned(32)));
251 void buf_dump(unsigned char *buf, size_t size)
253 size_t i;
254 int line;
255 static const char v[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
256 'A', 'B', 'C', 'D', 'E', 'F' };
258 for (i = 0, line = 0; i < size; line++)
260 int j, i0 = i;
261 char *b = buf_dump_ar[line];
263 for (j = 0; j < BUF_DUMP_ITEMS_IN_LINE && i < size; j++, i++)
265 *b++ = v[buf[i] >> 4];
266 *b++ = v[buf[i] & 0xF];
267 *b++ = ' ';
269 *b = 0;
271 /* Prefix must be of len BUF_DUMP_PREFIX_SIZE */
272 logf("%03d: %s", i0, buf_dump_ar[line]);
275 #else
276 #undef buf_dump
277 #define buf_dump(...)
278 #endif
280 static uint8_t buf_set_keyboard(unsigned char *buf, int id)
282 memset(buf, 0, 7);
284 if (HID_KEYBOARD_LEFT_CONTROL <= id && id <= HID_KEYBOARD_RIGHT_GUI)
285 buf[0] = (1 << (id - HID_KEYBOARD_LEFT_CONTROL));
286 else
287 buf[1] = (uint8_t)id;
289 return 7;
292 static uint8_t buf_set_consumer(unsigned char *buf, int id)
294 memset(buf, 0, 4);
295 buf[0] = (uint8_t)id;
297 return 4;
300 static size_t descriptor_report_get(unsigned char *dest)
302 unsigned char *report = dest;
303 usb_hid_report_t *usb_hid_report;
305 /* Keyboard control */
306 usb_hid_report = &usb_hid_reports[REPORT_ID_KEYBOARD];
307 usb_hid_report->usage_page = HID_USAGE_PAGE_KEYBOARD_KEYPAD;
308 usb_hid_report->buf_set = buf_set_keyboard;
310 pack_parameter(&report, 0, USAGE_PAGE,
311 HID_USAGE_PAGE_GENERIC_DESKTOP_CONTROLS);
312 PACK_VAL2(report, CONCAT(CONSUMER_USAGE, HID_GENERIC_DESKTOP_KEYBOARD));
313 pack_parameter(&report, 0, COLLECTION, COLLECTION_APPLICATION);
314 pack_parameter(&report, 0, REPORT_ID, REPORT_ID_KEYBOARD);
315 pack_parameter(&report, 0, USAGE_PAGE, HID_GENERIC_DESKTOP_KEYPAD);
316 pack_parameter(&report, 0, USAGE_MINIMUM, HID_KEYBOARD_LEFT_CONTROL);
317 pack_parameter(&report, 0, USAGE_MAXIMUM, HID_KEYBOARD_RIGHT_GUI);
318 pack_parameter(&report, 1, LOGICAL_MINIMUM, 0);
319 pack_parameter(&report, 1, LOGICAL_MAXIMUM, 1);
320 pack_parameter(&report, 0, REPORT_SIZE, 1);
321 pack_parameter(&report, 0, REPORT_COUNT, 8);
322 pack_parameter(&report, 0, INPUT, MAIN_ITEM_VARIABLE);
323 pack_parameter(&report, 0, REPORT_SIZE, 1);
324 pack_parameter(&report, 0, REPORT_COUNT, 5);
325 pack_parameter(&report, 0, USAGE_PAGE, HID_USAGE_PAGE_LEDS);
326 pack_parameter(&report, 0, USAGE_MINIMUM, HID_LED_NUM_LOCK);
327 pack_parameter(&report, 0, USAGE_MAXIMUM, HID_LED_KANA);
328 pack_parameter(&report, 0, OUTPUT, MAIN_ITEM_VARIABLE);
329 pack_parameter(&report, 0, REPORT_SIZE, 3);
330 pack_parameter(&report, 0, REPORT_COUNT, 1);
331 pack_parameter(&report, 0, OUTPUT, MAIN_ITEM_CONSTANT);
332 pack_parameter(&report, 0, REPORT_SIZE, 8);
333 pack_parameter(&report, 0, REPORT_COUNT, 6);
334 pack_parameter(&report, 1, LOGICAL_MINIMUM, 0);
335 pack_parameter(&report, 1, LOGICAL_MAXIMUM, HID_KEYBOARD_EX_SEL);
336 pack_parameter(&report, 0, USAGE_PAGE, HID_USAGE_PAGE_KEYBOARD_KEYPAD);
337 pack_parameter(&report, 0, USAGE_MINIMUM, 0);
338 pack_parameter(&report, 0, USAGE_MAXIMUM, HID_KEYBOARD_EX_SEL);
339 pack_parameter(&report, 0, INPUT, 0);
340 PACK_VAL1(report, END_COLLECTION);
342 /* Consumer usage controls - play/pause, stop, etc. */
343 usb_hid_report = &usb_hid_reports[REPORT_ID_CONSUMER];
344 usb_hid_report->usage_page = HID_USAGE_PAGE_CONSUMER;
345 usb_hid_report->buf_set = buf_set_consumer;
347 pack_parameter(&report, 0, USAGE_PAGE, HID_USAGE_PAGE_CONSUMER);
348 PACK_VAL2(report, CONCAT(CONSUMER_USAGE,
349 HID_CONSUMER_USAGE_CONSUMER_CONTROL));
350 pack_parameter(&report, 0, COLLECTION, COLLECTION_APPLICATION);
351 pack_parameter(&report, 0, REPORT_ID, REPORT_ID_CONSUMER);
352 pack_parameter(&report, 0, REPORT_SIZE, 16);
353 pack_parameter(&report, 0, REPORT_COUNT, 2);
354 pack_parameter(&report, 1, LOGICAL_MINIMUM, 1);
355 pack_parameter(&report, 1, LOGICAL_MAXIMUM, 652);
356 pack_parameter(&report, 0, USAGE_MINIMUM,
357 HID_CONSUMER_USAGE_CONSUMER_CONTROL);
358 pack_parameter(&report, 0, USAGE_MAXIMUM, HID_CONSUMER_USAGE_AC_SEND);
359 pack_parameter(&report, 0, INPUT, MAIN_ITEM_NO_PREFERRED |
360 MAIN_ITEM_NULL_STATE);
361 PACK_VAL1(report, END_COLLECTION);
363 return (size_t)((uint32_t)report - (uint32_t)dest);
366 static void descriptor_hid_get(unsigned char **dest)
368 hid_descriptor.wDescriptorLength0 =
369 (uint16_t)descriptor_report_get(report_descriptor);
371 PACK_DATA(*dest, hid_descriptor);
374 int usb_hid_get_config_descriptor(unsigned char *dest, int max_packet_size)
376 unsigned char *orig_dest = dest;
378 logf("hid: config desc.");
380 /* Ignore given max_packet_size */
381 (void)max_packet_size;
383 /* Interface descriptor */
384 interface_descriptor.bInterfaceNumber = usb_interface;
385 PACK_DATA(dest, interface_descriptor);
387 /* HID descriptor */
388 descriptor_hid_get(&dest);
390 /* Endpoint descriptor */
391 endpoint_descriptor.wMaxPacketSize = 8;
392 endpoint_descriptor.bInterval = 8;
393 endpoint_descriptor.bEndpointAddress = ep_in;
394 PACK_DATA(dest, endpoint_descriptor);
396 return (int)(dest - orig_dest);
399 void usb_hid_init_connection(void)
401 logf("hid: init connection");
403 active = true;
406 /* called by usb_core_init() */
407 void usb_hid_init(void)
409 int i;
411 logf("hid: init");
413 for (i = 0; i < HID_NUM_BUFFERS; i++)
414 send_buffer_len[i] = 0;
416 cur_buf_prepare = 0;
417 cur_buf_send = 0;
419 active = true;
422 void usb_hid_disconnect(void)
424 logf("hid: disconnect");
425 active = false;
428 /* called by usb_core_transfer_complete() */
429 void usb_hid_transfer_complete(int ep, int dir, int status, int length)
431 (void)ep;
432 (void)dir;
433 (void)status;
434 (void)length;
436 switch (dir)
438 case USB_DIR_OUT:
439 break;
440 case USB_DIR_IN:
442 if (status)
443 break;
445 send_buffer_len[cur_buf_send] = 0;
446 HID_BUF_INC(cur_buf_send);
447 usb_hid_try_send_drv();
448 break;
453 /* The DAP is registered as a keyboard with several LEDs, therefore the OS sends
454 * LED report to notify the DAP whether Num Lock / Caps Lock etc. are enabled.
455 * In order to allow sending info to the DAP, the Set Report mechanism can be
456 * used by defining vendor specific output reports and send them from the host
457 * to the DAP using the host's custom driver */
458 static int usb_hid_set_report(struct usb_ctrlrequest *req)
460 static unsigned char buf[SET_REPORT_BUF_LEN]
461 USB_DEVBSS_ATTR __attribute__((aligned(32)));
462 int length;
463 int rc = 0;
465 if ((req->wValue >> 8) != REPORT_TYPE_OUTPUT)
467 logf("Unsopported report type");
468 rc = 1;
469 goto Exit;
471 if ((req->wValue & 0xff) != REPORT_ID_KEYBOARD)
473 logf("Wrong report id");
474 rc = 2;
475 goto Exit;
477 if (req->wIndex != (uint16_t)usb_interface)
479 logf("Wrong interface");
480 rc = 3;
481 goto Exit;
483 length = req->wLength;
484 if (length != SET_REPORT_BUF_LEN)
486 logf("Wrong length");
487 rc = 4;
488 goto Exit;
491 memset(buf, 0, length);
492 usb_drv_recv(EP_CONTROL, buf, length);
494 #ifdef LOGF_ENABLE
495 if (buf[1] & 0x01)
496 logf("Num Lock enabled");
497 if (buf[1] & 0x02)
498 logf("Caps Lock enabled");
499 if (buf[1] & 0x04)
500 logf("Scroll Lock enabled");
501 if (buf[1] & 0x08)
502 logf("Compose enabled");
503 if (buf[1] & 0x10)
504 logf("Kana enabled");
505 #endif
507 /* Defining other LEDs and setting them from the USB host (OS) can be used
508 * to send messages to the DAP */
510 Exit:
511 return rc;
514 /* called by usb_core_control_request() */
515 bool usb_hid_control_request(struct usb_ctrlrequest *req, unsigned char *dest)
517 bool handled = false;
519 switch (req->bRequestType & USB_TYPE_MASK)
521 case USB_TYPE_STANDARD:
523 unsigned char *orig_dest = dest;
525 switch (req->wValue>>8) /* type */
527 case USB_DT_HID:
529 logf("hid: type hid");
530 descriptor_hid_get(&dest);
531 break;
533 case USB_DT_REPORT:
535 int len;
537 logf("hid: type report");
539 len = descriptor_report_get(report_descriptor);
540 memcpy(dest, &report_descriptor, len);
541 dest += len;
542 break;
544 default:
545 logf("hid: unsup. std. req");
546 break;
548 if (dest != orig_dest &&
549 !usb_drv_send(EP_CONTROL, orig_dest, dest - orig_dest))
551 usb_core_ack_control(req);
552 handled = true;
554 break;
557 case USB_TYPE_CLASS:
559 switch (req->bRequest)
561 case USB_HID_SET_IDLE:
562 logf("hid: set idle");
563 usb_core_ack_control(req);
564 handled = true;
565 break;
566 case USB_HID_SET_REPORT:
567 logf("hid: set report");
568 if (!usb_hid_set_report(req))
570 usb_core_ack_control(req);
571 handled = true;
573 break;
574 default:
575 logf("%d: unsup. cls. req", req->bRequest);
576 break;
578 break;
581 case USB_TYPE_VENDOR:
582 logf("hid: unsup. ven. req");
583 break;
585 return handled;
588 static void usb_hid_try_send_drv(void)
590 int rc;
591 int length = send_buffer_len[cur_buf_send];
593 if (!length)
594 return;
596 rc = usb_drv_send_nonblocking(ep_in, send_buffer[cur_buf_send], length);
597 if (rc)
599 send_buffer_len[cur_buf_send] = 0;
600 return;
604 static void usb_hid_queue(unsigned char *data, int length)
606 if (!active || length <= 0)
607 return;
609 /* Buffer overflow - item still in use */
610 if (send_buffer_len[cur_buf_prepare])
611 return;
613 /* Prepare buffer for sending */
614 if (length > HID_BUF_SIZE_MSG)
615 length = HID_BUF_SIZE_MSG;
616 memcpy(send_buffer[cur_buf_prepare], data, length);
617 send_buffer_len[cur_buf_prepare] = length;
619 HID_BUF_INC(cur_buf_prepare);
622 void usb_hid_send(usage_page_t usage_page, int id)
624 uint8_t report_id, length;
625 static unsigned char buf[HID_BUF_SIZE_CMD] USB_DEVBSS_ATTR
626 __attribute__((aligned(32)));
627 usb_hid_report_t *report = NULL;
629 for (report_id = 1; report_id < REPORT_ID_COUNT; report_id++)
631 if (usb_hid_reports[report_id].usage_page == usage_page)
633 report = &usb_hid_reports[report_id];
634 break;
637 if (!report)
639 logf("Unsupported usage_page");
640 return;
643 logf("Sending cmd 0x%x", id);
644 buf[0] = report_id;
645 length = report->buf_set(&buf[1], id) + 1;
646 logf("length %u", length);
648 /* Key pressed */
649 buf_dump(buf, length);
650 usb_hid_queue(buf, length);
652 /* Key released */
653 memset(buf, 0, length);
654 buf[0] = report_id;
656 buf_dump(buf, length);
657 usb_hid_queue(buf, length);
659 usb_hid_try_send_drv();