source: mainline/uspace/drv/usbkbd/main.c@ 45c4f5a

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 45c4f5a was ba54451, checked in by Vojtech Horky <vojtechhorky@…>, 15 years ago

HID driver put inside Doxygen group

  • Property mode set to 100644
File size: 9.9 KB
Line 
1/*
2 * Copyright (c) 2010 Vojtech Horky
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28/** @addtogroup drvusbhid
29 * @{
30 */
31#include <usb/usbdrv.h>
32#include <driver.h>
33#include <ipc/driver.h>
34#include <ipc/kbd.h>
35#include <io/keycode.h>
36#include <io/console.h>
37#include <errno.h>
38#include <fibril.h>
39#include <usb/classes/hid.h>
40#include <usb/classes/hidparser.h>
41#include <usb/devreq.h>
42#include <usb/descriptor.h>
43#include "descparser.h"
44#include "descdump.h"
45
46#define BUFFER_SIZE 32
47#define NAME "usbkbd"
48
49#define GUESSED_POLL_ENDPOINT 1
50
51static void default_connection_handler(device_t *, ipc_callid_t, ipc_call_t *);
52static device_ops_t keyboard_ops = {
53 .default_handler = default_connection_handler
54};
55
56static int console_callback_phone = -1;
57
58/** Default handler for IPC methods not handled by DDF.
59 *
60 * @param dev Device handling the call.
61 * @param icallid Call id.
62 * @param icall Call data.
63 */
64void default_connection_handler(device_t *dev,
65 ipc_callid_t icallid, ipc_call_t *icall)
66{
67 sysarg_t method = IPC_GET_IMETHOD(*icall);
68
69 if (method == IPC_M_CONNECT_TO_ME) {
70 int callback = IPC_GET_ARG5(*icall);
71
72 if (console_callback_phone != -1) {
73 ipc_answer_0(icallid, ELIMIT);
74 return;
75 }
76
77 console_callback_phone = callback;
78 ipc_answer_0(icallid, EOK);
79 return;
80 }
81
82 ipc_answer_0(icallid, EINVAL);
83}
84
85#if 0
86static void send_key(int key, int type, wchar_t c) {
87 async_msg_4(console_callback_phone, KBD_EVENT, type, key,
88 KM_NUM_LOCK, c);
89}
90#endif
91
92/*
93 * Callbacks for parser
94 */
95static void usbkbd_process_keycodes(const uint8_t *key_codes, size_t count,
96 uint8_t modifiers, void *arg)
97{
98 printf("Got keys: ");
99 unsigned i;
100 for (i = 0; i < count; ++i) {
101 printf("%d ", key_codes[i]);
102 }
103 printf("\n");
104}
105
106/*
107 * Kbd functions
108 */
109static int usbkbd_get_report_descriptor(usb_hid_dev_kbd_t *kbd_dev)
110{
111 // iterate over all configurations and interfaces
112 // TODO: more configurations!!
113 unsigned i;
114 for (i = 0; i < kbd_dev->conf->config_descriptor.interface_count; ++i) {
115 // TODO: endianness
116 uint16_t length =
117 kbd_dev->conf->interfaces[i].hid_desc.report_desc_info.length;
118 size_t actual_size = 0;
119
120 // allocate space for the report descriptor
121 kbd_dev->conf->interfaces[i].report_desc = (uint8_t *)malloc(length);
122
123 // get the descriptor from the device
124 int rc = usb_drv_req_get_descriptor(kbd_dev->device->parent_phone,
125 kbd_dev->address, USB_REQUEST_TYPE_CLASS, USB_DESCTYPE_HID_REPORT,
126 0, i, kbd_dev->conf->interfaces[i].report_desc, length,
127 &actual_size);
128
129 if (rc != EOK) {
130 return rc;
131 }
132
133 assert(actual_size == length);
134
135 //dump_hid_class_descriptor(0, USB_DESCTYPE_HID_REPORT,
136 // kbd_dev->conf->interfaces[i].report_desc, length);
137 }
138
139 return EOK;
140}
141
142static int usbkbd_process_descriptors(usb_hid_dev_kbd_t *kbd_dev)
143{
144 // get the first configuration descriptor (TODO: parse also other!)
145 usb_standard_configuration_descriptor_t config_desc;
146
147 int rc = usb_drv_req_get_bare_configuration_descriptor(
148 kbd_dev->device->parent_phone, kbd_dev->address, 0, &config_desc);
149
150 if (rc != EOK) {
151 return rc;
152 }
153
154 // prepare space for all underlying descriptors
155 uint8_t *descriptors = (uint8_t *)malloc(config_desc.total_length);
156 if (descriptors == NULL) {
157 return ENOMEM;
158 }
159
160 size_t transferred = 0;
161 // get full configuration descriptor
162 rc = usb_drv_req_get_full_configuration_descriptor(
163 kbd_dev->device->parent_phone, kbd_dev->address, 0, descriptors,
164 config_desc.total_length, &transferred);
165
166 if (rc != EOK) {
167 return rc;
168 }
169 if (transferred != config_desc.total_length) {
170 return ELIMIT;
171 }
172
173 kbd_dev->conf = (usb_hid_configuration_t *)calloc(1,
174 sizeof(usb_hid_configuration_t));
175 if (kbd_dev->conf == NULL) {
176 free(descriptors);
177 return ENOMEM;
178 }
179
180 rc = usbkbd_parse_descriptors(descriptors, transferred, kbd_dev->conf);
181 free(descriptors);
182 if (rc != EOK) {
183 printf("Problem with parsing standard descriptors.\n");
184 return rc;
185 }
186
187 // get and report descriptors
188 rc = usbkbd_get_report_descriptor(kbd_dev);
189 if (rc != EOK) {
190 printf("Problem with parsing HID REPORT descriptor.\n");
191 return rc;
192 }
193
194 //usbkbd_print_config(kbd_dev->conf);
195
196 /*
197 * TODO:
198 * 1) select one configuration (lets say the first)
199 * 2) how many interfaces?? how to select one??
200 * ("The default setting for an interface is always alternate setting zero.")
201 * 3) find endpoint which is IN and INTERRUPT (parse), save its number
202 * as the endpoint for polling
203 */
204
205 return EOK;
206}
207
208static usb_hid_dev_kbd_t *usbkbd_init_device(device_t *dev)
209{
210 usb_hid_dev_kbd_t *kbd_dev = (usb_hid_dev_kbd_t *)calloc(1,
211 sizeof(usb_hid_dev_kbd_t));
212
213 if (kbd_dev == NULL) {
214 fprintf(stderr, NAME ": No memory!\n");
215 return NULL;
216 }
217
218 kbd_dev->device = dev;
219
220 // get phone to my HC and save it as my parent's phone
221 // TODO: maybe not a good idea if DDF will use parent_phone
222 int rc = kbd_dev->device->parent_phone = usb_drv_hc_connect_auto(dev, 0);
223 if (rc < 0) {
224 printf("Problem setting phone to HC.\n");
225 free(kbd_dev);
226 return NULL;
227 }
228
229 rc = kbd_dev->address = usb_drv_get_my_address(dev->parent_phone, dev);
230 if (rc < 0) {
231 printf("Problem getting address of the device.\n");
232 free(kbd_dev);
233 return NULL;
234 }
235
236 // doesn't matter now that we have no address
237// if (kbd_dev->address < 0) {
238// fprintf(stderr, NAME ": No device address!\n");
239// free(kbd_dev);
240// return NULL;
241// }
242
243 // default endpoint
244 kbd_dev->poll_endpoint = GUESSED_POLL_ENDPOINT;
245
246 /*
247 * will need all descriptors:
248 * 1) choose one configuration from configuration descriptors
249 * (set it to the device)
250 * 2) set endpoints from endpoint descriptors
251 */
252
253 // TODO: get descriptors, parse descriptors and save endpoints
254 usbkbd_process_descriptors(kbd_dev);
255
256 return kbd_dev;
257}
258
259static void usbkbd_process_interrupt_in(usb_hid_dev_kbd_t *kbd_dev,
260 uint8_t *buffer, size_t actual_size)
261{
262 usb_hid_report_in_callbacks_t *callbacks =
263 (usb_hid_report_in_callbacks_t *)malloc(
264 sizeof(usb_hid_report_in_callbacks_t));
265 callbacks->keyboard = usbkbd_process_keycodes;
266
267 //usb_hid_parse_report(kbd_dev->parser, buffer, actual_size, callbacks,
268 // NULL);
269 printf("Calling usb_hid_boot_keyboard_input_report()...\n)");
270 usb_hid_boot_keyboard_input_report(buffer, actual_size, callbacks, NULL);
271}
272
273static void usbkbd_poll_keyboard(usb_hid_dev_kbd_t *kbd_dev)
274{
275 return;
276
277 int rc;
278 usb_handle_t handle;
279 uint8_t buffer[BUFFER_SIZE];
280 size_t actual_size;
281 //usb_endpoint_t poll_endpoint = 1;
282
283// usb_address_t my_address = usb_drv_get_my_address(dev->parent_phone,
284// dev);
285// if (my_address < 0) {
286// return;
287// }
288
289 usb_target_t poll_target = {
290 .address = kbd_dev->address,
291 .endpoint = kbd_dev->poll_endpoint
292 };
293
294 while (true) {
295 async_usleep(1000 * 1000);
296 rc = usb_drv_async_interrupt_in(kbd_dev->device->parent_phone,
297 poll_target, buffer, BUFFER_SIZE, &actual_size, &handle);
298
299 if (rc != EOK) {
300 continue;
301 }
302
303 rc = usb_drv_async_wait_for(handle);
304 if (rc != EOK) {
305 continue;
306 }
307
308 /*
309 * If the keyboard answered with NAK, it returned no data.
310 * This implies that no change happened since last query.
311 */
312 if (actual_size == 0) {
313 continue;
314 }
315
316 /*
317 * TODO: Process pressed keys.
318 */
319 usbkbd_process_interrupt_in(kbd_dev, buffer, actual_size);
320 }
321
322 // not reached
323 assert(0);
324}
325
326static int usbkbd_fibril_device(void *arg)
327{
328 printf("!!! USB device fibril\n");
329
330 if (arg == NULL) {
331 printf("No device!\n");
332 return -1;
333 }
334
335 device_t *dev = (device_t *)arg;
336
337 // initialize device (get and process descriptors, get address, etc.)
338 usb_hid_dev_kbd_t *kbd_dev = usbkbd_init_device(dev);
339
340 usbkbd_poll_keyboard(kbd_dev);
341
342 return EOK;
343}
344
345static int usbkbd_add_device(device_t *dev)
346{
347 /* For now, fail immediately. */
348 //return ENOTSUP;
349
350 /*
351 * When everything is okay, connect to "our" HC.
352 *
353 * Not supported yet, skip..
354 */
355// int phone = usb_drv_hc_connect_auto(dev, 0);
356// if (phone < 0) {
357// /*
358// * Connecting to HC failed, roll-back and announce
359// * failure.
360// */
361// return phone;
362// }
363
364// dev->parent_phone = phone;
365
366 /*
367 * Create new fibril for handling this keyboard
368 */
369 fid_t fid = fibril_create(usbkbd_fibril_device, dev);
370 if (fid == 0) {
371 printf("%s: failed to start fibril for HID device\n", NAME);
372 return ENOMEM;
373 }
374 fibril_add_ready(fid);
375
376 dev->ops = &keyboard_ops;
377
378 add_device_to_class(dev, "keyboard");
379
380 /*
381 * Hurrah, device is initialized.
382 */
383 return EOK;
384}
385
386static driver_ops_t kbd_driver_ops = {
387 .add_device = usbkbd_add_device,
388};
389
390static driver_t kbd_driver = {
391 .name = NAME,
392 .driver_ops = &kbd_driver_ops
393};
394
395int main(int argc, char *argv[])
396{
397 return driver_main(&kbd_driver);
398}
399
400/**
401 * @}
402 */
Note: See TracBrowser for help on using the repository browser.