Fix flash start address.
[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 "usb_hid.h"
40 #include "usbkbd.h"
41
42 #include "cfg/cfg_usbkbd.h"
43
44 #define LOG_LEVEL  USB_KEYBOARD_LOG_LEVEL
45 #define LOG_FORMAT USB_KEYBOARD_LOG_FORMAT
46
47 #include <cfg/log.h>
48 #include <cfg/debug.h>
49 #include <cfg/macros.h>
50 #include <cfg/compiler.h>
51 #include <cfg/module.h>
52
53 #include <cpu/power.h> // cpu_relax()
54
55 #include <drv/usb.h>
56 #include <drv/usb_endpoint.h>
57
58
59 /*
60  * HID device configuration (usb-keyboard)
61  */
62 #define USB_HID_VENDOR_ID       USB_KEYBOARD_VENDOR_ID
63 #define USB_HID_PRODUCT_ID      USB_KEYBOARD_PRODUCT_ID
64
65 #define USB_HID_INTERFACES      1
66 #define USB_HID_ENDPOINTS       1
67
68 #define USB_STRING_MANUFACTURER 1
69 #define USB_STRING_PRODUCT      2
70
71 #define USB_HID_REPORT_EP       (USB_DIR_IN | USB_KBD_EP_REPORT)
72
73 static UsbDeviceDesc usb_hid_device_descriptor =
74 {
75         .bLength = sizeof(usb_hid_device_descriptor),
76         .bDescriptorType = USB_DT_DEVICE,
77         .bcdUSB = 0x100,
78         .bDeviceClass = 0,
79         .bDeviceSubClass = 0,
80         .bDeviceProtocol = 0,
81         .idVendor = USB_HID_VENDOR_ID,
82         .idProduct = USB_HID_PRODUCT_ID,
83         .bcdDevice = 0,
84         .iManufacturer = USB_STRING_MANUFACTURER,
85         .iProduct = USB_STRING_PRODUCT,
86         .iSerialNumber = 0,
87         .bNumConfigurations = 1,
88 };
89
90 static const UsbConfigDesc usb_hid_config_descriptor =
91 {
92         .bLength = sizeof(usb_hid_config_descriptor),
93         .bDescriptorType = USB_DT_CONFIG,
94         .bNumInterfaces = USB_HID_INTERFACES,
95         .bConfigurationValue = 1,
96         .iConfiguration = 0,
97         .bmAttributes = USB_CONFIG_ATT_ONE,
98         .bMaxPower = 50, /* 100 mA */
99 };
100
101 static const UsbInterfaceDesc usb_hid_interface_descriptor =
102 {
103         .bLength = sizeof(usb_hid_interface_descriptor),
104         .bDescriptorType = USB_DT_INTERFACE,
105         .bInterfaceNumber = 0,
106         .bAlternateSetting = 0,
107         .bNumEndpoints = USB_HID_ENDPOINTS,
108         .bInterfaceClass = USB_CLASS_HID,
109         .bInterfaceSubClass = USB_INTERFACE_SUBCLASS_BOOT,
110         .bInterfaceProtocol = USB_INTERFACE_PROTOCOL_KEYBOARD,
111         .iInterface = 0,
112 };
113
114 /*
115  * Keyboard report descriptor
116  *
117  * Taken from the USB HID spec:
118  *  - E.6 Report Descriptor (Keyboard), HID1_11.pdf, p.69
119  */
120 static const uint8_t hid_report_descriptor[] =
121 {
122         0x05, 0x01, // Usage Page (Generic Desktop)
123         0x09, 0x06, // Usage (Keyboard)
124         0xA1, 0x01, // Collection (Application)
125         0x05, 0x07, // Usage Page (Key Codes)
126         0x19, 0xE0, // Usage Minimum (224)
127         0x29, 0xE7, // Usage Maximum (231)
128         0x15, 0x00, // Logical Minimum (0)
129         0x25, 0x01, // Logical Maximum (1)
130         0x75, 0x01, // Report Size (1)
131         0x95, 0x08, // Report Count (8)
132         0x81, 0x02, // Input (Data, Variable, Absolute)
133         0x95, 0x01, // Report Count (1)
134         0x75, 0x08, // Report Size (8)
135         0x81, 0x01, // Input (Constant)
136         0x95, 0x05, // Report Count (5)
137         0x75, 0x01, // Report Size (1)
138         0x05, 0x08, // Usage Page (Page# for LEDs)
139         0x19, 0x01, // Usage Minimum (1)
140         0x29, 0x05, // Usage Maximum (5)
141         0x91, 0x02, // Output (Data, Variable, Absolute)
142         0x95, 0x01, // Report Count (1)
143         0x75, 0x03, // Report Size (3)
144         0x91, 0x01, // Output (Constant)
145         0x95, 0x06, // Report Count (6)
146         0x75, 0x08, // Report Size (8)
147         0x15, 0x00, // Logical Minimum (0)
148         0x25, 0x65, // Logical Maximum(101)
149         0x05, 0x07, // Usage Page (Key Codes)
150         0x19, 0x00, // Usage Minimum (0)
151         0x29, 0x65, // Usage Maximum (101)
152         0x81, 0x00, // Input (Data, Array)
153         0xC0, // End Collection
154 };
155
156 static const usb_HidDesc usb_hid_descriptor =
157 {
158         .bLength = sizeof(usb_hid_descriptor),
159         .bDescriptorType = HID_DT_HID,
160         .bcdHID = usb_cpu_to_le16((uint16_t)0x0110),
161         .bCountryCode = 0,
162         .bNumDescriptors = 1,
163         .bDescriptorHidType = HID_DT_REPORT,
164         .wDescriptorLength =
165                 usb_cpu_to_le16((uint16_t)sizeof(hid_report_descriptor)),
166 };
167
168 static const UsbEndpointDesc usb_hid_ep_descriptor =
169 {
170         .bLength = sizeof(usb_hid_ep_descriptor),
171         .bDescriptorType = USB_DT_ENDPOINT,
172         .bEndpointAddress = USB_HID_REPORT_EP,
173         .bmAttributes = USB_ENDPOINT_XFER_INT,
174         .wMaxPacketSize = usb_cpu_to_le16((uint16_t)4),
175         .bInterval = 10, /* resolution in ms */
176 };
177
178 static const UsbDescHeader *usb_hid_config[] =
179 {
180         (const UsbDescHeader *)&usb_hid_config_descriptor,
181         (const UsbDescHeader *)&usb_hid_interface_descriptor,
182         (const UsbDescHeader *)&usb_hid_descriptor,
183         (const UsbDescHeader *)&usb_hid_ep_descriptor,
184         NULL,
185 };
186
187 static const DEFINE_USB_STRING(language_str, "\x09\x04"); // Language ID: en_US
188 static const DEFINE_USB_STRING(manufacturer_str,
189                 USB_STRING("B", "e", "R", "T", "O", "S"));
190 static const DEFINE_USB_STRING(product_str,
191                 USB_STRING("U", "S", "B", " ",
192                                 "K", "e", "y", "b", "o", "a", "r", "d"));
193
194 static const UsbStringDesc *usb_hid_strings[] =
195 {
196         (const UsbStringDesc *)&language_str,
197         (const UsbStringDesc *)&manufacturer_str,
198         (const UsbStringDesc *)&product_str,
199         NULL,
200 };
201
202 static uint8_t report[8];
203
204 static bool hid_keyboard_configured;
205
206 static void usb_hid_event_cb(UsbCtrlRequest *ctrl)
207 {
208         uint16_t value = usb_le16_to_cpu(ctrl->wValue);
209         uint16_t index = usb_le16_to_cpu(ctrl->wIndex);
210         uint16_t length = usb_le16_to_cpu(ctrl->wLength);
211         uint8_t type = ctrl->mRequestType;
212         uint8_t request = ctrl->bRequest;
213
214         LOG_INFO("%s: s 0x%02x 0x%02x 0x%04x 0x%04x 0x%04x\n",
215                 __func__, type, request, value, index, length);
216         switch (ctrl->bRequest)
217         {
218         case USB_REQ_GET_DESCRIPTOR:
219                 switch (value >> 8)
220                 {
221                 case HID_DT_HID:
222                         LOG_INFO("%s: HID_DT_HID\n", __func__);
223                         usb_endpointWrite(USB_DIR_IN | 0,
224                                         &usb_hid_descriptor,
225                                         sizeof(usb_hid_descriptor));
226                         break;
227                 case HID_DT_REPORT:
228                         LOG_INFO("%s: HID_DT_REPORT\n", __func__);
229                         usb_endpointWrite(USB_DIR_IN | 0,
230                                         &hid_report_descriptor,
231                                         sizeof(hid_report_descriptor));
232                         hid_keyboard_configured = true;
233                         break;
234                 default:
235                         LOG_INFO("%s: unknown HID request\n", __func__);
236                         break;
237                 }
238                 break;
239         case HID_REQ_GET_REPORT:
240                 LOG_INFO("%s: HID_REQ_GET_REPORT\n", __func__);
241                 break;
242         case HID_REQ_SET_REPORT:
243                 LOG_INFO("%s: HID_REQ_SET_REPORT\n", __func__);
244                 usb_endpointWrite(USB_DIR_IN | 0, NULL, 0);
245                 break;
246         case HID_REQ_GET_IDLE:
247                 LOG_INFO("%s: HID_REQ_GET_IDLE\n", __func__);
248                 break;
249         case HID_REQ_SET_IDLE:
250                 LOG_INFO("%s: HID_REQ_SET_IDLE\n", __func__);
251                 usb_endpointWrite(USB_DIR_IN | 0, NULL, 0);
252                 break;
253         case HID_REQ_GET_PROTOCOL:
254                 LOG_INFO("%s: HID_REQ_GET_PROTOCOL\n", __func__);
255                 break;
256         case HID_REQ_SET_PROTOCOL:
257                 LOG_INFO("%s: HID_REQ_SET_PROTOCOL\n", __func__);
258                 break;
259         default:
260                 LOG_ERR("%s: unknown request: 0x%02x\n",
261                         __func__, ctrl->bRequest);
262                 break;
263         }
264 }
265
266 /* Global usb-keyboard descriptor that identifies the usb-keyboard device */
267 static UsbDevice usb_keyboard = {
268         .device = &usb_hid_device_descriptor,
269         .config = usb_hid_config,
270         .strings = usb_hid_strings,
271         .event_cb = usb_hid_event_cb,
272 };
273
274 /* Low-level usb-hid device initialization */
275 static int usb_keyboard_hw_init(void)
276 {
277         if (usb_deviceRegister(&usb_keyboard) < 0)
278                 return -1;
279         LOG_INFO("usb-hid: registered new USB keyboard device\n");
280         return 0;
281 }
282
283 /* Send a keyboard event */
284 void usbkbd_sendEvent(uint8_t mod, uint8_t code)
285 {
286         report[0] = mod;
287         report[2] = code;
288         usb_endpointWrite(USB_HID_REPORT_EP, &report, sizeof(report));
289 }
290
291 /*
292  * Initialize a USB HID keyboard device.
293  *
294  * TODO: support more than one device at the same time.
295  */
296 int usbkbd_init(UNUSED_ARG(int, unit))
297 {
298 #if CONFIG_KERN
299         MOD_CHECK(proc);
300 #endif
301         usb_keyboard_hw_init();
302         while (!hid_keyboard_configured)
303                 cpu_relax();
304         return 0;
305 }