usb-keyboard: acknowledge HID_REQ_SET_REPORT to the host
[bertos.git] / bertos / drv / usb_keyboard.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2010 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \author Andrea Righi <arighi@develer.com>
34  *
35  * \brief Generic USB keyboard device driver.
36  *
37  */
38
39 #include "cfg/cfg_usb_keyboard.h"
40
41 #define LOG_LEVEL  USB_KEYBOARD_LOG_LEVEL
42 #define LOG_FORMAT USB_KEYBOARD_LOG_FORMAT
43
44 #include <cfg/log.h>
45 #include <cfg/debug.h>
46 #include <cfg/macros.h>
47 #include <cfg/compiler.h>
48 #include <cfg/module.h>
49
50 #include <cpu/power.h> // cpu_relax()
51
52 #include <drv/usb.h>
53
54 #include "drv/usb_hid.h"
55 #include "drv/usb_keyboard.h"
56
57 /*
58  * HID device configuration (usb-keyboard)
59  */
60 #define USB_HID_VENDOR_ID       0xffff /* custom */
61 #define USB_HID_PRODUCT_ID      0x0000
62
63 #define USB_HID_INTERFACES      1
64 #define USB_HID_ENDPOINTS       1
65
66 #define USB_STRING_MANUFACTURER 1
67 #define USB_STRING_PRODUCT      2
68
69 #define USB_HID_REPORT_EP       (USB_DIR_IN | 1)
70
71 static usb_device_descriptor_t usb_hid_device_descriptor =
72 {
73         .bLength = sizeof(usb_hid_device_descriptor),
74         .bDescriptorType = USB_DT_DEVICE,
75         .bcdUSB = 0x100,
76         .bDeviceClass = 0,
77         .bDeviceSubClass = 0,
78         .bDeviceProtocol = 0,
79         .idVendor = USB_HID_VENDOR_ID,
80         .idProduct = USB_HID_PRODUCT_ID,
81         .bcdDevice = 0,
82         .iManufacturer = USB_STRING_MANUFACTURER,
83         .iProduct = USB_STRING_PRODUCT,
84         .iSerialNumber = 0,
85         .bNumConfigurations = 1,
86 };
87
88 static const usb_config_descriptor_t usb_hid_config_descriptor =
89 {
90         .bLength = sizeof(usb_hid_config_descriptor),
91         .bDescriptorType = USB_DT_CONFIG,
92         .bNumInterfaces = USB_HID_INTERFACES,
93         .bConfigurationValue = 1,
94         .iConfiguration = 0,
95         .bmAttributes = USB_CONFIG_ATT_ONE,
96         .bMaxPower = 50, /* 100 mA */
97 };
98
99 static const usb_interface_descriptor_t usb_hid_interface_descriptor =
100 {
101         .bLength = sizeof(usb_hid_interface_descriptor),
102         .bDescriptorType = USB_DT_INTERFACE,
103         .bInterfaceNumber = 0,
104         .bAlternateSetting = 0,
105         .bNumEndpoints = USB_HID_ENDPOINTS,
106         .bInterfaceClass = USB_CLASS_HID,
107         .bInterfaceSubClass = USB_INTERFACE_SUBCLASS_BOOT,
108         .bInterfaceProtocol = USB_INTERFACE_PROTOCOL_KEYBOARD,
109         .iInterface = 0,
110 };
111
112 /*
113  * Keyboard report descriptor
114  *
115  * Taken from the USB HID spec:
116  *  - E.6 Report Descriptor (Keyboard), HID1_11.pdf, p.69
117  */
118 static const uint8_t hid_report_descriptor[] =
119 {
120         0x05, 0x01, // Usage Page (Generic Desktop)
121         0x09, 0x06, // Usage (Keyboard)
122         0xA1, 0x01, // Collection (Application)
123         0x05, 0x07, // Usage Page (Key Codes)
124         0x19, 0xE0, // Usage Minimum (224)
125         0x29, 0xE7, // Usage Maximum (231)
126         0x15, 0x00, // Logical Minimum (0)
127         0x25, 0x01, // Logical Maximum (1)
128         0x75, 0x01, // Report Size (1)
129         0x95, 0x08, // Report Count (8)
130         0x81, 0x02, // Input (Data, Variable, Absolute)
131         0x95, 0x01, // Report Count (1)
132         0x75, 0x08, // Report Size (8)
133         0x81, 0x01, // Input (Constant)
134         0x95, 0x05, // Report Count (5)
135         0x75, 0x01, // Report Size (1)
136         0x05, 0x08, // Usage Page (Page# for LEDs)
137         0x19, 0x01, // Usage Minimum (1)
138         0x29, 0x05, // Usage Maximum (5)
139         0x91, 0x02, // Output (Data, Variable, Absolute)
140         0x95, 0x01, // Report Count (1)
141         0x75, 0x03, // Report Size (3)
142         0x91, 0x01, // Output (Constant)
143         0x95, 0x06, // Report Count (6)
144         0x75, 0x08, // Report Size (8)
145         0x15, 0x00, // Logical Minimum (0)
146         0x25, 0x65, // Logical Maximum(101)
147         0x05, 0x07, // Usage Page (Key Codes)
148         0x19, 0x00, // Usage Minimum (0)
149         0x29, 0x65, // Usage Maximum (101)
150         0x81, 0x00, // Input (Data, Array)
151         0xC0, // End Collection
152 };
153
154 static const usb_hid_descriptor_t usb_hid_descriptor =
155 {
156         .bLength = sizeof(usb_hid_descriptor),
157         .bDescriptorType = HID_DT_HID,
158         .bcdHID = usb_cpu_to_le16(0x0110),
159         .bCountryCode = 0,
160         .bNumDescriptors = 1,
161         .bDescriptorHidType = HID_DT_REPORT,
162         .wDescriptorLength = usb_cpu_to_le16(sizeof(hid_report_descriptor)),
163 };
164
165 static const usb_endpoint_descriptor_t usb_hid_ep_descriptor =
166 {
167         .bLength = sizeof(usb_hid_ep_descriptor),
168         .bDescriptorType = USB_DT_ENDPOINT,
169         .bEndpointAddress = USB_HID_REPORT_EP,
170         .bmAttributes = USB_ENDPOINT_XFER_INT,
171         .wMaxPacketSize = usb_cpu_to_le16(4),
172         .bInterval = 10, /* resolution in ms */
173 };
174
175 static const usb_descriptor_header_t *usb_hid_config[] =
176 {
177         (const usb_descriptor_header_t *)&usb_hid_config_descriptor,
178         (const usb_descriptor_header_t *)&usb_hid_interface_descriptor,
179         (const usb_descriptor_header_t *)&usb_hid_descriptor,
180         (const usb_descriptor_header_t *)&usb_hid_ep_descriptor,
181         NULL,
182 };
183
184 static DEFINE_USB_STRING(language_str, "\x09\x04"); // Language ID: en_US
185 static DEFINE_USB_STRING(manufacturer_str,
186                 USB_STRING("B", "e", "R", "T", "O", "S"));
187 static DEFINE_USB_STRING(product_str,
188                 USB_STRING("U", "S", "B", " ",
189                                 "K", "e", "y", "b", "o", "a", "r", "d"));
190
191 static const usb_string_descriptor_t *usb_hid_strings[] =
192 {
193         (usb_string_descriptor_t *)&language_str,
194         (usb_string_descriptor_t *)&manufacturer_str,
195         (usb_string_descriptor_t *)&product_str,
196         NULL,
197 };
198
199 static uint8_t report[8];
200
201 static bool hid_keyboard_configured;
202
203 static void usb_hid_event_cb(usb_ctrlrequest_t *ctrl)
204 {
205         uint16_t value = usb_le16_to_cpu(ctrl->wValue);
206         uint16_t index = usb_le16_to_cpu(ctrl->wIndex);
207         uint16_t length = usb_le16_to_cpu(ctrl->wLength);
208         uint8_t type = ctrl->mRequestType;
209         uint8_t request = ctrl->bRequest;
210
211         LOG_INFO("%s: s 0x%02x 0x%02x 0x%04x 0x%04x 0x%04x\n",
212                 __func__, type, request, value, index, length);
213         switch (ctrl->bRequest)
214         {
215         case USB_REQ_GET_DESCRIPTOR:
216                 switch (value >> 8)
217                 {
218                 case HID_DT_HID:
219                         LOG_INFO("%s: HID_DT_HID\n", __func__);
220                         usb_ep_write(USB_DIR_IN | 0,
221                                         &usb_hid_descriptor,
222                                         sizeof(usb_hid_descriptor));
223                         break;
224                 case HID_DT_REPORT:
225                         LOG_INFO("%s: HID_DT_REPORT\n", __func__);
226                         usb_ep_write(USB_DIR_IN | 0,
227                                         &hid_report_descriptor,
228                                         sizeof(hid_report_descriptor));
229                         hid_keyboard_configured = true;
230                         break;
231                 default:
232                         LOG_INFO("%s: unknown HID request\n", __func__);
233                         break;
234                 }
235                 break;
236         case HID_REQ_GET_REPORT:
237                 LOG_INFO("%s: HID_REQ_GET_REPORT\n", __func__);
238                 break;
239         case HID_REQ_SET_REPORT:
240                 LOG_INFO("%s: HID_REQ_SET_REPORT\n", __func__);
241                 usb_ep_write(USB_DIR_IN | 0, NULL, 0);
242                 break;
243         case HID_REQ_GET_IDLE:
244                 LOG_INFO("%s: HID_REQ_GET_IDLE\n", __func__);
245                 break;
246         case HID_REQ_SET_IDLE:
247                 LOG_INFO("%s: HID_REQ_SET_IDLE\n", __func__);
248                 usb_ep_write(USB_DIR_IN | 0, NULL, 0);
249                 break;
250         case HID_REQ_GET_PROTOCOL:
251                 LOG_INFO("%s: HID_REQ_GET_PROTOCOL\n", __func__);
252                 break;
253         case HID_REQ_SET_PROTOCOL:
254                 LOG_INFO("%s: HID_REQ_SET_PROTOCOL\n", __func__);
255                 break;
256         default:
257                 LOG_ERR("%s: unknown request: 0x%02x\n",
258                         __func__, ctrl->bRequest);
259                 break;
260         }
261 }
262
263 /* Global usb-keyboard descriptor that identifies the usb-keyboard device */
264 static struct usb_device usb_keyboard = {
265         .device = &usb_hid_device_descriptor,
266         .config = usb_hid_config,
267         .strings = usb_hid_strings,
268         .event_cb = usb_hid_event_cb,
269 };
270
271 /* Low-level usb-hid device initialization */
272 static int usb_keyboard_hw_init(void)
273 {
274         if (usb_device_register(&usb_keyboard) < 0)
275                 return -1;
276         LOG_INFO("usb-hid: registered new USB keyboard device\n");
277         return 0;
278 }
279
280 /* Send a keyboard event */
281 void usb_keyboard_send_event(uint8_t mod, uint8_t code)
282 {
283         report[0] = mod;
284         report[2] = code;
285         usb_ep_write(USB_HID_REPORT_EP, &report, sizeof(report));
286 }
287
288 /*
289  * Initialize a USB HID keyboard device.
290  *
291  * TODO: support more than one device at the same time.
292  */
293 int usb_keyboard_init(UNUSED_ARG(int, unit))
294 {
295 #if CONFIG_KERN
296         MOD_CHECK(proc);
297 #endif
298         usb_keyboard_hw_init();
299         while (!hid_keyboard_configured)
300                 cpu_relax();
301         return 0;
302 }