4 * This file is part of BeRTOS.
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.
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.
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
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.
29 * Copyright 2010 Develer S.r.l. (http://www.develer.com/)
33 * \author Andrea Righi <arighi@develer.com>
35 * \brief Generic USB keyboard device driver.
39 #include "cfg/cfg_usbkbd.h"
41 #define LOG_LEVEL USB_KEYBOARD_LOG_LEVEL
42 #define LOG_FORMAT USB_KEYBOARD_LOG_FORMAT
45 #include <cfg/debug.h>
46 #include <cfg/macros.h>
47 #include <cfg/compiler.h>
48 #include <cfg/module.h>
50 #include <cpu/power.h> // cpu_relax()
54 #include "drv/usb_hid.h"
55 #include "drv/usbkbd.h"
58 * HID device configuration (usb-keyboard)
60 #define USB_HID_VENDOR_ID 0xffff /* custom */
61 #define USB_HID_PRODUCT_ID 0x0000
63 #define USB_HID_INTERFACES 1
64 #define USB_HID_ENDPOINTS 1
66 #define USB_STRING_MANUFACTURER 1
67 #define USB_STRING_PRODUCT 2
69 #define USB_HID_REPORT_EP (USB_DIR_IN | 1)
71 static UsbDeviceDesc usb_hid_device_descriptor =
73 .bLength = sizeof(usb_hid_device_descriptor),
74 .bDescriptorType = USB_DT_DEVICE,
79 .idVendor = USB_HID_VENDOR_ID,
80 .idProduct = USB_HID_PRODUCT_ID,
82 .iManufacturer = USB_STRING_MANUFACTURER,
83 .iProduct = USB_STRING_PRODUCT,
85 .bNumConfigurations = 1,
88 static const UsbConfigDesc usb_hid_config_descriptor =
90 .bLength = sizeof(usb_hid_config_descriptor),
91 .bDescriptorType = USB_DT_CONFIG,
92 .bNumInterfaces = USB_HID_INTERFACES,
93 .bConfigurationValue = 1,
95 .bmAttributes = USB_CONFIG_ATT_ONE,
96 .bMaxPower = 50, /* 100 mA */
99 static const UsbInterfaceDesc usb_hid_interface_descriptor =
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,
113 * Keyboard report descriptor
115 * Taken from the USB HID spec:
116 * - E.6 Report Descriptor (Keyboard), HID1_11.pdf, p.69
118 static const uint8_t hid_report_descriptor[] =
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
154 static const usb_HidDesc usb_hid_descriptor =
156 .bLength = sizeof(usb_hid_descriptor),
157 .bDescriptorType = HID_DT_HID,
158 .bcdHID = usb_cpu_to_le16((uint16_t)0x0110),
160 .bNumDescriptors = 1,
161 .bDescriptorHidType = HID_DT_REPORT,
163 usb_cpu_to_le16((uint16_t)sizeof(hid_report_descriptor)),
166 static const UsbEndpointDesc usb_hid_ep_descriptor =
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 */
176 static const UsbDescHeader *usb_hid_config[] =
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,
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"));
192 static const UsbStringDesc *usb_hid_strings[] =
194 (UsbStringDesc *)&language_str,
195 (UsbStringDesc *)&manufacturer_str,
196 (UsbStringDesc *)&product_str,
200 static uint8_t report[8];
202 static bool hid_keyboard_configured;
204 static void usb_hid_event_cb(UsbCtrlRequest *ctrl)
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;
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)
216 case USB_REQ_GET_DESCRIPTOR:
220 LOG_INFO("%s: HID_DT_HID\n", __func__);
221 usb_endpointWrite(USB_DIR_IN | 0,
223 sizeof(usb_hid_descriptor));
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;
233 LOG_INFO("%s: unknown HID request\n", __func__);
237 case HID_REQ_GET_REPORT:
238 LOG_INFO("%s: HID_REQ_GET_REPORT\n", __func__);
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);
244 case HID_REQ_GET_IDLE:
245 LOG_INFO("%s: HID_REQ_GET_IDLE\n", __func__);
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);
251 case HID_REQ_GET_PROTOCOL:
252 LOG_INFO("%s: HID_REQ_GET_PROTOCOL\n", __func__);
254 case HID_REQ_SET_PROTOCOL:
255 LOG_INFO("%s: HID_REQ_SET_PROTOCOL\n", __func__);
258 LOG_ERR("%s: unknown request: 0x%02x\n",
259 __func__, ctrl->bRequest);
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,
272 /* Low-level usb-hid device initialization */
273 static int usb_keyboard_hw_init(void)
275 if (usb_deviceRegister(&usb_keyboard) < 0)
277 LOG_INFO("usb-hid: registered new USB keyboard device\n");
281 /* Send a keyboard event */
282 void usbkbd_sendEvent(uint8_t mod, uint8_t code)
286 usb_endpointWrite(USB_HID_REPORT_EP, &report, sizeof(report));
290 * Initialize a USB HID keyboard device.
292 * TODO: support more than one device at the same time.
294 int usbkbd_init(UNUSED_ARG(int, unit))
299 usb_keyboard_hw_init();
300 while (!hid_keyboard_configured)