source: mainline/uspace/drv/usbkbd/main.c@ b2a6fcfe

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since b2a6fcfe was 6986418, checked in by Lubos Slovak <lubos.slovak@…>, 15 years ago

Descriptor parsing modified (fixed)

  • change usbkbd_parse_descriptors() not to parse class-specific descriptors as these are retrieved differently
  • dump functions moved to descdump.h/c
  • getting report descriptor in usbkbd/main.c
  • changed descriptor structures (hid.h) to reflect, that there is only one report descriptor
  • Property mode set to 100644
File size: 9.1 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#include <usb/usbdrv.h>
29#include <driver.h>
30#include <ipc/driver.h>
31#include <ipc/kbd.h>
32#include <io/keycode.h>
33#include <io/console.h>
34#include <errno.h>
35#include <fibril.h>
36#include <usb/classes/hid.h>
37#include <usb/classes/hidparser.h>
38#include <usb/devreq.h>
39#include <usb/descriptor.h>
40#include "descparser.h"
41
42#define BUFFER_SIZE 32
43#define NAME "usbkbd"
44
45#define GUESSED_POLL_ENDPOINT 1
46
47static void default_connection_handler(device_t *, ipc_callid_t, ipc_call_t *);
48static device_ops_t keyboard_ops = {
49 .default_handler = default_connection_handler
50};
51
52static int console_callback_phone = -1;
53
54/** Default handler for IPC methods not handled by DDF.
55 *
56 * @param dev Device handling the call.
57 * @param icallid Call id.
58 * @param icall Call data.
59 */
60void default_connection_handler(device_t *dev,
61 ipc_callid_t icallid, ipc_call_t *icall)
62{
63 sysarg_t method = IPC_GET_IMETHOD(*icall);
64
65 if (method == IPC_M_CONNECT_TO_ME) {
66 int callback = IPC_GET_ARG5(*icall);
67
68 if (console_callback_phone != -1) {
69 ipc_answer_0(icallid, ELIMIT);
70 return;
71 }
72
73 console_callback_phone = callback;
74 ipc_answer_0(icallid, EOK);
75 return;
76 }
77
78 ipc_answer_0(icallid, EINVAL);
79}
80
81static void send_key(int key, int type, wchar_t c) {
82 async_msg_4(console_callback_phone, KBD_EVENT, type, key,
83 KM_NUM_LOCK, c);
84}
85
86static void send_alnum(int key, wchar_t c) {
87 printf(NAME ": sending key '%lc' to console\n", (wint_t) c);
88 send_key(key, KEY_PRESS, c);
89 send_key(key, KEY_RELEASE, c);
90}
91
92/*
93 * Callbacks for parser
94 */
95static void usbkbd_process_keycodes(const uint16_t *key_codes, size_t count,
96 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 uint8_t type =
116 kbd_dev->conf->interfaces[i].hid_desc.report_desc_info.type;
117 // TODO: endianness
118 uint16_t length =
119 kbd_dev->conf->interfaces[i].hid_desc.report_desc_info.length;
120
121 // allocate space for the report descriptor
122 kbd_dev->conf->interfaces[i].report_desc = (uint8_t *)malloc(length);
123 // get the descriptor from the device
124
125 }
126}
127
128static int usbkbd_process_descriptors(usb_hid_dev_kbd_t *kbd_dev)
129{
130 // get the first configuration descriptor (TODO: parse also other!)
131 usb_standard_configuration_descriptor_t config_desc;
132
133 int rc = usb_drv_req_get_bare_configuration_descriptor(
134 kbd_dev->device->parent_phone, kbd_dev->address, 0, &config_desc);
135
136 if (rc != EOK) {
137 return rc;
138 }
139
140 // prepare space for all underlying descriptors
141 uint8_t *descriptors = (uint8_t *)malloc(config_desc.total_length);
142 if (descriptors == NULL) {
143 return ENOMEM;
144 }
145
146 size_t transferred = 0;
147 // get full configuration descriptor
148 rc = usb_drv_req_get_full_configuration_descriptor(
149 kbd_dev->device->parent_phone, kbd_dev->address, 0, descriptors,
150 config_desc.total_length, &transferred);
151
152 if (rc != EOK) {
153 return rc;
154 }
155 if (transferred != config_desc.total_length) {
156 return ELIMIT;
157 }
158
159 kbd_dev->conf = (usb_hid_configuration_t *)calloc(1,
160 sizeof(usb_hid_configuration_t));
161 if (kbd_dev->conf == NULL) {
162 free(descriptors);
163 return ENOMEM;
164 }
165
166 rc = usbkbd_parse_descriptors(descriptors, transferred, kbd_dev->conf);
167 free(descriptors);
168
169 // get and report descriptors
170 rc = usbkbd_get_report_descriptor(kbd_dev);
171
172 usbkbd_print_config(kbd_dev->conf);
173
174 return rc;
175}
176
177static usb_hid_dev_kbd_t *usbkbd_init_device(device_t *dev)
178{
179 usb_hid_dev_kbd_t *kbd_dev = (usb_hid_dev_kbd_t *)calloc(1,
180 sizeof(usb_hid_dev_kbd_t));
181
182 if (kbd_dev == NULL) {
183 fprintf(stderr, NAME ": No memory!\n");
184 return NULL;
185 }
186
187 kbd_dev->device = dev;
188
189 // get phone to my HC and save it as my parent's phone
190 // TODO: maybe not a good idea if DDF will use parent_phone
191 kbd_dev->device->parent_phone = usb_drv_hc_connect_auto(dev, 0);
192
193 kbd_dev->address = usb_drv_get_my_address(dev->parent_phone,
194 dev);
195
196 // doesn't matter now that we have no address
197// if (kbd_dev->address < 0) {
198// fprintf(stderr, NAME ": No device address!\n");
199// free(kbd_dev);
200// return NULL;
201// }
202
203 // default endpoint
204 kbd_dev->poll_endpoint = GUESSED_POLL_ENDPOINT;
205
206 /*
207 * will need all descriptors:
208 * 1) choose one configuration from configuration descriptors
209 * (set it to the device)
210 * 2) set endpoints from endpoint descriptors
211 */
212
213 // TODO: get descriptors, parse descriptors and save endpoints
214 usbkbd_process_descriptors(kbd_dev);
215
216 return kbd_dev;
217}
218
219static void usbkbd_process_interrupt_in(usb_hid_dev_kbd_t *kbd_dev,
220 uint8_t *buffer, size_t actual_size)
221{
222 /*
223 * here, the parser will be called, probably with some callbacks
224 * now only take last 6 bytes and process, i.e. send to kbd
225 */
226
227 usb_hid_report_in_callbacks_t *callbacks =
228 (usb_hid_report_in_callbacks_t *)malloc(
229 sizeof(usb_hid_report_in_callbacks_t));
230 callbacks->keyboard = usbkbd_process_keycodes;
231
232 if (console_callback_phone != -1) {
233 static size_t counter = 0;
234 counter++;
235 if (counter > 3) {
236 counter = 0;
237 send_alnum(KC_A, L'a');
238 }
239 }
240
241 usb_hid_parse_report(kbd_dev->parser, buffer, actual_size, callbacks,
242 NULL);
243}
244
245static void usbkbd_poll_keyboard(usb_hid_dev_kbd_t *kbd_dev)
246{
247 return;
248
249 int rc;
250 usb_handle_t handle;
251 uint8_t buffer[BUFFER_SIZE];
252 size_t actual_size;
253 //usb_endpoint_t poll_endpoint = 1;
254
255// usb_address_t my_address = usb_drv_get_my_address(dev->parent_phone,
256// dev);
257// if (my_address < 0) {
258// return;
259// }
260
261 usb_target_t poll_target = {
262 .address = kbd_dev->address,
263 .endpoint = kbd_dev->poll_endpoint
264 };
265
266 while (true) {
267 async_usleep(1000 * 1000);
268 rc = usb_drv_async_interrupt_in(kbd_dev->device->parent_phone,
269 poll_target, buffer, BUFFER_SIZE, &actual_size, &handle);
270
271 if (rc != EOK) {
272 continue;
273 }
274
275 rc = usb_drv_async_wait_for(handle);
276 if (rc != EOK) {
277 continue;
278 }
279
280 /*
281 * If the keyboard answered with NAK, it returned no data.
282 * This implies that no change happened since last query.
283 */
284 if (actual_size == 0) {
285 continue;
286 }
287
288 /*
289 * TODO: Process pressed keys.
290 */
291 usbkbd_process_interrupt_in(kbd_dev, buffer, actual_size);
292 }
293
294 // not reached
295 assert(0);
296}
297
298static int usbkbd_fibril_device(void *arg)
299{
300 printf("!!! USB device fibril\n");
301
302 if (arg == NULL) {
303 printf("No device!\n");
304 return -1;
305 }
306
307 device_t *dev = (device_t *)arg;
308
309 // initialize device (get and process descriptors, get address, etc.)
310 usb_hid_dev_kbd_t *kbd_dev = usbkbd_init_device(dev);
311
312 usbkbd_poll_keyboard(kbd_dev);
313
314 return EOK;
315}
316
317static int usbkbd_add_device(device_t *dev)
318{
319 /* For now, fail immediately. */
320 //return ENOTSUP;
321
322 /*
323 * When everything is okay, connect to "our" HC.
324 *
325 * Not supported yet, skip..
326 */
327// int phone = usb_drv_hc_connect_auto(dev, 0);
328// if (phone < 0) {
329// /*
330// * Connecting to HC failed, roll-back and announce
331// * failure.
332// */
333// return phone;
334// }
335
336// dev->parent_phone = phone;
337
338 /*
339 * Create new fibril for handling this keyboard
340 */
341 fid_t fid = fibril_create(usbkbd_fibril_device, dev);
342 if (fid == 0) {
343 printf("%s: failed to start fibril for HID device\n", NAME);
344 return ENOMEM;
345 }
346 fibril_add_ready(fid);
347
348 dev->ops = &keyboard_ops;
349
350 add_device_to_class(dev, "keyboard");
351
352 /*
353 * Hurrah, device is initialized.
354 */
355 return EOK;
356}
357
358static driver_ops_t kbd_driver_ops = {
359 .add_device = usbkbd_add_device,
360};
361
362static driver_t kbd_driver = {
363 .name = NAME,
364 .driver_ops = &kbd_driver_ops
365};
366
367int main(int argc, char *argv[])
368{
369 return driver_main(&kbd_driver);
370}
Note: See TracBrowser for help on using the repository browser.