[57c7050] | 1 | /*
|
---|
| 2 | * Copyright (c) 2011 Vojtech Horky
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup usbvirthid
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | *
|
---|
| 34 | */
|
---|
| 35 | #include "../virthid.h"
|
---|
| 36 | #include <errno.h>
|
---|
| 37 | #include <usb/debug.h>
|
---|
[faa44e58] | 38 | #include <usb/hid/hid.h>
|
---|
| 39 | #include <usb/hid/usages/core.h>
|
---|
[57c7050] | 40 |
|
---|
[42d47f8] | 41 | #include "../report.h"
|
---|
[57c7050] | 42 |
|
---|
| 43 | uint8_t report_descriptor[] = {
|
---|
| 44 | STD_USAGE_PAGE(USB_HIDUT_PAGE_GENERIC_DESKTOP),
|
---|
| 45 | USAGE1(USB_HIDUT_USAGE_GENERIC_DESKTOP_KEYBOARD),
|
---|
| 46 | START_COLLECTION(COLLECTION_APPLICATION),
|
---|
| 47 | STD_USAGE_PAGE(USB_HIDUT_PAGE_KEYBOARD),
|
---|
| 48 | USAGE_MINIMUM1(224),
|
---|
| 49 | USAGE_MAXIMUM1(231),
|
---|
| 50 | LOGICAL_MINIMUM1(0),
|
---|
| 51 | LOGICAL_MAXIMUM1(1),
|
---|
| 52 | REPORT_SIZE1(1),
|
---|
| 53 | REPORT_COUNT1(8),
|
---|
| 54 | /* Modifiers */
|
---|
| 55 | INPUT(IOF_DATA | IOF_VARIABLE | IOF_ABSOLUTE),
|
---|
| 56 | REPORT_COUNT1(1),
|
---|
| 57 | REPORT_SIZE1(8),
|
---|
| 58 | /* Reserved */
|
---|
| 59 | INPUT(IOF_CONSTANT),
|
---|
| 60 | REPORT_COUNT1(5),
|
---|
| 61 | REPORT_SIZE1(1),
|
---|
| 62 | STD_USAGE_PAGE(USB_HIDUT_PAGE_LED),
|
---|
| 63 | USAGE_MINIMUM1(1),
|
---|
| 64 | USAGE_MAXIMUM1(5),
|
---|
| 65 | /* LED states */
|
---|
| 66 | OUTPUT(IOF_DATA | IOF_VARIABLE | IOF_ABSOLUTE),
|
---|
| 67 | REPORT_COUNT1(1),
|
---|
| 68 | REPORT_SIZE1(3),
|
---|
| 69 | /* LED states padding */
|
---|
| 70 | OUTPUT(IOF_CONSTANT),
|
---|
| 71 | REPORT_COUNT1(6),
|
---|
| 72 | REPORT_SIZE1(8),
|
---|
| 73 | LOGICAL_MINIMUM1(0),
|
---|
| 74 | LOGICAL_MAXIMUM1(101),
|
---|
| 75 | STD_USAGE_PAGE(USB_HIDUT_PAGE_KEYBOARD),
|
---|
| 76 | USAGE_MINIMUM1(0),
|
---|
| 77 | USAGE_MAXIMUM1(101),
|
---|
| 78 | /* Key array */
|
---|
| 79 | INPUT(IOF_DATA | IOF_ARRAY),
|
---|
| 80 | END_COLLECTION()
|
---|
| 81 | };
|
---|
| 82 |
|
---|
| 83 | #define INPUT_SIZE 8
|
---|
| 84 |
|
---|
| 85 | static uint8_t in_data[] = {
|
---|
| 86 | 0, 0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
---|
| 87 | 0, 0, 0x39, 0x00, 0x00, 0x00, 0x00, 0x00, // Caps Lock
|
---|
| 88 | 0, 0, 0x53, 0x00, 0x00, 0x00, 0x00, 0x00, // Num Lock
|
---|
| 89 | 0, 0, 0x39, 0x00, 0x00, 0x00, 0x00, 0x00, // Caps Lock
|
---|
| 90 | 1 << 2, 0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
---|
| 91 | 1 << 2, 0, 0x3F, 0x00, 0x00, 0x00, 0x00, 0x00,
|
---|
| 92 | 1 << 2, 0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
---|
| 93 | 0, 0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
|
---|
| 94 | };
|
---|
| 95 | static size_t in_data_count = sizeof(in_data)/INPUT_SIZE;
|
---|
| 96 | // FIXME - locking
|
---|
| 97 | static size_t in_data_position = 0;
|
---|
| 98 |
|
---|
| 99 | static int on_data_in(vuhid_interface_t *iface,
|
---|
| 100 | void *buffer, size_t buffer_size, size_t *act_buffer_size)
|
---|
| 101 | {
|
---|
| 102 | static size_t last_pos = (size_t) -1;
|
---|
| 103 | size_t pos = in_data_position;
|
---|
| 104 | if (pos >= in_data_count) {
|
---|
| 105 | return EBADCHECKSUM;
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | if (last_pos == pos) {
|
---|
| 109 | return ENAK;
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | if (buffer_size > INPUT_SIZE) {
|
---|
| 113 | buffer_size = INPUT_SIZE;
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | if (act_buffer_size != NULL) {
|
---|
| 117 | *act_buffer_size = buffer_size;
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | memcpy(buffer, in_data + pos * INPUT_SIZE, buffer_size);
|
---|
| 121 | last_pos = pos;
|
---|
| 122 |
|
---|
| 123 | return EOK;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | static int on_data_out(vuhid_interface_t *iface,
|
---|
| 127 | void *buffer, size_t buffer_size)
|
---|
| 128 | {
|
---|
| 129 | if (buffer_size == 0) {
|
---|
| 130 | return EEMPTY;
|
---|
| 131 | }
|
---|
| 132 | uint8_t leds = ((uint8_t *) buffer)[0];
|
---|
| 133 | #define _GET_LED(index, signature) \
|
---|
| 134 | (((leds) & (1 << index)) ? (signature) : '-')
|
---|
| 135 | usb_log_info("%s: LEDs = %c%c%c%c%c\n",
|
---|
| 136 | iface->name,
|
---|
| 137 | _GET_LED(0, '0'), _GET_LED(1, 'A'), _GET_LED(2, 's'),
|
---|
| 138 | _GET_LED(3, 'c'), _GET_LED(4, 'k'));
|
---|
| 139 | #undef _GET_LED
|
---|
| 140 | return EOK;
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 |
|
---|
| 144 | static void live(vuhid_interface_t *iface)
|
---|
| 145 | {
|
---|
| 146 | async_usleep(1000 * 1000 * 5);
|
---|
| 147 | usb_log_debug("Boot keyboard comes to life...\n");
|
---|
| 148 | while (in_data_position < in_data_count) {
|
---|
| 149 | async_usleep(1000 * 500);
|
---|
| 150 | in_data_position++;
|
---|
| 151 | }
|
---|
| 152 | usb_log_debug("Boot keyboard died.\n");
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 |
|
---|
| 156 | vuhid_interface_t vuhid_interface_bootkbd = {
|
---|
| 157 | .id = "boot",
|
---|
| 158 | .name = "boot keyboard",
|
---|
| 159 | .usb_subclass = USB_HID_SUBCLASS_BOOT,
|
---|
| 160 | .usb_protocol = USB_HID_PROTOCOL_KEYBOARD,
|
---|
| 161 |
|
---|
| 162 | .report_descriptor = report_descriptor,
|
---|
| 163 | .report_descriptor_size = sizeof(report_descriptor),
|
---|
| 164 |
|
---|
| 165 | .in_data_size = INPUT_SIZE,
|
---|
| 166 | .on_data_in = on_data_in,
|
---|
| 167 |
|
---|
| 168 | .out_data_size = 1,
|
---|
| 169 | .on_data_out = on_data_out,
|
---|
| 170 |
|
---|
[9a884ed] | 171 | .live = live,
|
---|
| 172 |
|
---|
| 173 | .vuhid_data = NULL
|
---|
[57c7050] | 174 | };
|
---|
| 175 |
|
---|
| 176 | /**
|
---|
| 177 | * @}
|
---|
| 178 | */
|
---|