24608770c91fbad5ff168de67239254e5d9274a8
[bertos.git] / bertos / drv / usbkbd.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_usbkbd.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/usbkbd.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 UsbDeviceDesc 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 UsbConfigDesc 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 UsbInterfaceDesc 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_HidDesc usb_hid_descriptor =
155 {
156         .bLength = sizeof(usb_hid_descriptor),
157         .bDescriptorType = HID_DT_HID,
158         .bcdHID = usb_cpu_to_le16((uint16_t)0x0110),
159         .bCountryCode = 0,
160         .bNumDescriptors = 1,
161         .bDescriptorHidType = HID_DT_REPORT,
162         .wDescriptorLength =
163                 usb_cpu_to_le16((uint16_t)sizeof(hid_report_descriptor)),
164 };
165
166 static const UsbEndpointDesc usb_hid_ep_descriptor =
167 {
168         .bLength = sizeof(usb_hid_ep_descriptor),
169         .bDescriptorType = USB_DT_ENDPOINT,
170         .bEndpointAddress = USB_HID_REPORT_EP,
171         .bmAttributes = USB_ENDPOINT_XFER_INT,
172         .wMaxPacketSize = usb_cpu_to_le16((uint16_t)4),
173         .bInterval = 10, /* resolution in ms */
174 };
175
176 static const UsbDescHeader *usb_hid_config[] =
177 {
178         (const UsbDescHeader *)&usb_hid_config_descriptor,
179         (const UsbDescHeader *)&usb_hid_interface_descriptor,
180         (const UsbDescHeader *)&usb_hid_descriptor,
181         (const UsbDescHeader *)&usb_hid_ep_descriptor,
182         NULL,
183 };
184
185 static DEFINE_USB_STRING(language_str, "\x09\x04"); // Language ID: en_US
186 static DEFINE_USB_STRING(manufacturer_str,
187                 USB_STRING("B", "e", "R", "T", "O", "S"));
188 static DEFINE_USB_STRING(product_str,
189                 USB_STRING("U", "S", "B", " ",
190                                 "K", "e", "y", "b", "o", "a", "r", "d"));
191
192 static const UsbStringDesc *usb_hid_strings[] =
193 {
194         (UsbStringDesc *)&language_str,
195         (UsbStringDesc *)&manufacturer_str,
196         (UsbStringDesc *)&product_str,
197         NULL,
198 };
199
200 static uint8_t report[8];
201
202 static bool hid_keyboard_configured;
203
204 static void usb_hid_event_cb(UsbCtrlRequest *ctrl)
205 {
206         uint16_t value = usb_le16_to_cpu(ctrl->wValue);
207         uint16_t index = usb_le16_to_cpu(ctrl->wIndex);
208         uint16_t length = usb_le16_to_cpu(ctrl->wLength);
209         uint8_t type = ctrl->mRequestType;
210         uint8_t request = ctrl->bRequest;
211
212         LOG_INFO("%s: s 0x%02x 0x%02x 0x%04x 0x%04x 0x%04x\n",
213                 __func__, type, request, value, index, length);
214         switch (ctrl->bRequest)
215         {
216         case USB_REQ_GET_DESCRIPTOR:
217                 switch (value >> 8)
218                 {
219                 case HID_DT_HID:
220                         LOG_INFO("%s: HID_DT_HID\n", __func__);
221                         usb_endpointWrite(USB_DIR_IN | 0,
222                                         &usb_hid_descriptor,
223                                         sizeof(usb_hid_descriptor));
224                         break;
225                 case HID_DT_REPORT:
226                         LOG_INFO("%s: HID_DT_REPORT\n", __func__);
227                         usb_endpointWrite(USB_DIR_IN | 0,
228                                         &hid_report_descriptor,
229                                         sizeof(hid_report_descriptor));
230                         hid_keyboard_configured = true;
231                         break;
232                 default:
233                         LOG_INFO("%s: unknown HID request\n", __func__);
234                         break;
235                 }
236                 break;
237         case HID_REQ_GET_REPORT:
238                 LOG_INFO("%s: HID_REQ_GET_REPORT\n", __func__);
239                 break;
240         case HID_REQ_SET_REPORT:
241                 LOG_INFO("%s: HID_REQ_SET_REPORT\n", __func__);
242                 usb_endpointWrite(USB_DIR_IN | 0, NULL, 0);
243                 break;
244         case HID_REQ_GET_IDLE:
245                 LOG_INFO("%s: HID_REQ_GET_IDLE\n", __func__);
246                 break;
247         case HID_REQ_SET_IDLE:
248                 LOG_INFO("%s: HID_REQ_SET_IDLE\n", __func__);
249                 usb_endpointWrite(USB_DIR_IN | 0, NULL, 0);
250                 break;
251         case HID_REQ_GET_PROTOCOL:
252                 LOG_INFO("%s: HID_REQ_GET_PROTOCOL\n", __func__);
253                 break;
254         case HID_REQ_SET_PROTOCOL:
255                 LOG_INFO("%s: HID_REQ_SET_PROTOCOL\n", __func__);
256                 break;
257         default:
258                 LOG_ERR("%s: unknown request: 0x%02x\n",
259                         __func__, ctrl->bRequest);
260                 break;
261         }
262 }
263
264 /* Global usb-keyboard descriptor that identifies the usb-keyboard device */
265 static UsbDevice usb_keyboard = {
266         .device = &usb_hid_device_descriptor,
267         .config = usb_hid_config,
268         .strings = usb_hid_strings,
269         .event_cb = usb_hid_event_cb,
270 };
271
272 /* Low-level usb-hid device initialization */
273 static int usb_keyboard_hw_init(void)
274 {
275         if (usb_deviceRegister(&usb_keyboard) < 0)
276                 return -1;
277         LOG_INFO("usb-hid: registered new USB keyboard device\n");
278         return 0;
279 }
280
281 /* Send a keyboard event */
282 void usbkbd_sendEvent(uint8_t mod, uint8_t code)
283 {
284         report[0] = mod;
285         report[2] = code;
286         usb_endpointWrite(USB_HID_REPORT_EP, &report, sizeof(report));
287 }
288
289 /*
290  * Initialize a USB HID keyboard device.
291  *
292  * TODO: support more than one device at the same time.
293  */
294 int usbkbd_init(UNUSED_ARG(int, unit))
295 {
296 #if CONFIG_KERN
297         MOD_CHECK(proc);
298 #endif
299         usb_keyboard_hw_init();
300         while (!hid_keyboard_configured)
301                 cpu_relax();
302         return 0;
303 }