4bea725a993c7c61917ce06162e6440712e5c2d4
[bertos.git] / bertos / drv / usbmouse.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 mouse device driver.
36  *
37  */
38
39 #include "usb_hid.h"
40 #include "usbmouse.h"
41
42 #include "cfg/cfg_usbmouse.h"
43
44 #define LOG_LEVEL  USB_MOUSE_LOG_LEVEL
45 #define LOG_FORMAT USB_MOUSE_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-mouse)
61  */
62 #define USB_HID_VENDOR_ID       USB_MOUSE_VENDOR_ID
63 #define USB_HID_PRODUCT_ID      USB_MOUSE_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_MOUSE_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_MOUSE,
111         .iInterface = 0,
112 };
113
114 static const uint8_t hid_report_descriptor[] =
115 {
116         0x05, 0x01, // Usage Page (Generic Desktop)
117         0x09, 0x02, // Usage (Mouse)
118         0xA1, 0x01, // Collection (Application)
119         0x09, 0x01, // Usage (Pointer)
120         0xA1, 0x00, // Collection (Physical)
121         0x05, 0x09, // Usage Page (Buttons)
122         0x19, 0x01, // Usage Minimum (1)
123         0x29, 0x03, // Usage Maximum (3)
124         0x15, 0x00, // Logical Minimum (0)
125         0x25, 0x01, // Logical Maximum (1)
126         0x95, 0x03, // Report Count (3)
127         0x75, 0x01, // Report Size (1)
128         0x81, 0x02, // Input (Data, Variable, Absolute)
129         0x95, 0x01, // Report Count (1)
130         0x75, 0x05, // Report Size (5)
131         0x81, 0x01, // Input (03=Logitech, 01=Hid spec:Constant)
132         0x05, 0x01, // Usage Page (Generic Desktop)
133         0x09, 0x30, // Usage (X)
134         0x09, 0x31, // Usage (Y)
135         0x09, 0x38, // Usage (Logitech:_)
136         0x15, 0x81, // Logical Minimum (-127)
137         0x25, 0x7F, // Logical Maximum (127)
138         0x75, 0x08, // Report Size (8)
139         0x95, 0x02, // Report Count (Logitech=3, Hid spec =2)
140         0x81, 0x06, // Input (Data, Variable, Relative)
141         0xC0,   // End Collection
142         0xC0    // End Collection
143 };
144
145 static const usb_HidDesc usb_hid_descriptor =
146 {
147         .bLength = sizeof(usb_hid_descriptor),
148         .bDescriptorType = HID_DT_HID,
149         .bcdHID = usb_cpu_to_le16((uint16_t)0x0110),
150         .bCountryCode = 0,
151         .bNumDescriptors = 1,
152         .bDescriptorHidType = HID_DT_REPORT,
153         .wDescriptorLength =
154                 usb_cpu_to_le16((uint16_t)sizeof(hid_report_descriptor)),
155 };
156
157 static const UsbEndpointDesc usb_hid_ep_descriptor =
158 {
159         .bLength = sizeof(usb_hid_ep_descriptor),
160         .bDescriptorType = USB_DT_ENDPOINT,
161         .bEndpointAddress = USB_HID_REPORT_EP,
162         .bmAttributes = USB_ENDPOINT_XFER_INT,
163         .wMaxPacketSize = usb_cpu_to_le16((uint16_t)4),
164         .bInterval = 10, /* resolution in ms */
165 };
166
167 static const UsbDescHeader *usb_hid_config[] =
168 {
169         (const UsbDescHeader *)&usb_hid_config_descriptor,
170         (const UsbDescHeader *)&usb_hid_interface_descriptor,
171         (const UsbDescHeader *)&usb_hid_descriptor,
172         (const UsbDescHeader *)&usb_hid_ep_descriptor,
173         NULL,
174 };
175
176 static const DEFINE_USB_STRING(language_str, "\x09\x04"); // Language ID: en_US
177 static const DEFINE_USB_STRING(manufacturer_str,
178                 USB_STRING("B", "e", "R", "T", "O", "S"));
179 static const DEFINE_USB_STRING(product_str,
180                 USB_STRING("U", "S", "B", " ", "M", "o", "u", "s", "e"));
181
182 static const UsbStringDesc *usb_hid_strings[] =
183 {
184         (const UsbStringDesc *)&language_str,
185         (const UsbStringDesc *)&manufacturer_str,
186         (const UsbStringDesc *)&product_str,
187         NULL,
188 };
189
190 typedef struct mouse_report
191 {
192         uint8_t buttons;
193         int8_t x;
194         int8_t y;
195 } PACKED mouse_report_t;
196
197 static mouse_report_t report;
198
199 static bool hid_mouse_configured;
200
201 static void usb_hid_event_cb(UsbCtrlRequest *ctrl)
202 {
203         uint16_t value = usb_le16_to_cpu(ctrl->wValue);
204         uint16_t index = usb_le16_to_cpu(ctrl->wIndex);
205         uint16_t length = usb_le16_to_cpu(ctrl->wLength);
206         uint8_t type = ctrl->mRequestType;
207         uint8_t request = ctrl->bRequest;
208
209         LOG_INFO("%s: s 0x%02x 0x%02x 0x%04x 0x%04x 0x%04x\n",
210                 __func__, type, request, value, index, length);
211         switch (ctrl->bRequest)
212         {
213         case USB_REQ_GET_DESCRIPTOR:
214                 switch (value >> 8)
215                 {
216                 case HID_DT_HID:
217                         LOG_INFO("%s: HID_DT_HID\n", __func__);
218                         usb_endpointWrite(USB_DIR_IN | 0,
219                                         &usb_hid_descriptor,
220                                         sizeof(usb_hid_descriptor));
221                         break;
222                 case HID_DT_REPORT:
223                         LOG_INFO("%s: HID_DT_REPORT\n", __func__);
224                         usb_endpointWrite(USB_DIR_IN | 0,
225                                         &hid_report_descriptor,
226                                         sizeof(hid_report_descriptor));
227                         hid_mouse_configured = true;
228                         break;
229                 }
230                 break;
231         case HID_REQ_GET_REPORT:
232                 LOG_INFO("%s: HID_REQ_GET_REPORT\n", __func__);
233                 break;
234         case HID_REQ_SET_REPORT:
235                 LOG_INFO("%s: HID_REQ_SET_REPORT\n", __func__);
236                 break;
237         case HID_REQ_GET_IDLE:
238                 LOG_INFO("%s: HID_REQ_GET_IDLE\n", __func__);
239                 break;
240         case HID_REQ_SET_IDLE:
241                 LOG_INFO("%s: HID_REQ_SET_IDLE\n", __func__);
242                 usb_endpointWrite(USB_DIR_IN | 0, NULL, 0);
243                 break;
244         case HID_REQ_GET_PROTOCOL:
245                 LOG_INFO("%s: HID_REQ_GET_PROTOCOL\n", __func__);
246                 break;
247         case HID_REQ_SET_PROTOCOL:
248                 LOG_INFO("%s: HID_REQ_SET_PROTOCOL\n", __func__);
249                 break;
250         default:
251                 LOG_ERR("%s: unknown request: 0x%02x\n",
252                         __func__, ctrl->bRequest);
253                 break;
254         }
255 }
256
257 /* Global usb-mouse descriptor that identifies the usb-mouse device */
258 static UsbDevice usb_mouse = {
259         .device = &usb_hid_device_descriptor,
260         .config = usb_hid_config,
261         .strings = usb_hid_strings,
262         .event_cb = usb_hid_event_cb,
263 };
264
265 /* Low-level usb-hid device initialization */
266 static int usb_mouse_hw_init(void)
267 {
268         if (usb_deviceRegister(&usb_mouse) < 0)
269                 return -1;
270         LOG_INFO("usb-hid: registered new USB mouse device\n");
271         return 0;
272 }
273
274 /* Send a mouse event */
275 void usbmouse_sendEvent(int8_t x, int8_t y, int8_t buttons)
276 {
277         report.x = x;
278         report.y = y;
279         report.buttons = buttons;
280         usb_endpointWrite(USB_HID_REPORT_EP, &report, sizeof(report));
281 }
282
283 /*
284  * Initialize a USB HID device.
285  *
286  * TODO: support more than one device at the same time.
287  */
288 int usbmouse_init(UNUSED_ARG(int, unit))
289 {
290 #if CONFIG_KERN
291         MOD_CHECK(proc);
292 #endif
293         usb_mouse_hw_init();
294         while (!hid_mouse_configured)
295                 cpu_relax();
296         return 0;
297 }