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

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

USB drivers specifies full target in requests

  • Property mode set to 100644
File size: 3.0 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 <errno.h>
31
32#define BUFFER_SIZE 32
33
34/* Call this periodically to check keyboard status changes. */
35static void poll_keyboard(device_t *dev)
36{
37 int rc;
38 usb_handle_t handle;
39 char buffer[BUFFER_SIZE];
40 size_t actual_size;
41 usb_endpoint_t poll_endpoint = 1;
42
43 usb_address_t my_address = usb_drv_get_my_address(dev->parent_phone,
44 dev);
45 if (my_address < 0) {
46 return;
47 }
48
49 usb_target_t poll_target = {
50 .address = my_address,
51 .endpoint = poll_endpoint
52 };
53
54 rc = usb_drv_async_interrupt_in(dev->parent_phone, poll_target,
55 buffer, BUFFER_SIZE, &actual_size, &handle);
56 if (rc != EOK) {
57 return;
58 }
59
60 rc = usb_drv_async_wait_for(handle);
61 if (rc != EOK) {
62 return;
63 }
64
65 /*
66 * If the keyboard answered with NAK, it returned no data.
67 * This implies that no change happened since last query.
68 */
69 if (actual_size == 0) {
70 return;
71 }
72
73 /*
74 * Process pressed keys.
75 */
76}
77
78static int add_kbd_device(device_t *dev)
79{
80 /* For now, fail immediately. */
81 return ENOTSUP;
82
83 /*
84 * When everything is okay, connect to "our" HC.
85 */
86 int phone = usb_drv_hc_connect(dev, 0);
87 if (phone < 0) {
88 /*
89 * Connecting to HC failed, roll-back and announce
90 * failure.
91 */
92 return phone;
93 }
94
95 dev->parent_phone = phone;
96
97 /*
98 * Just for fun ;-).
99 */
100 poll_keyboard(dev);
101
102 /*
103 * Hurrah, device is initialized.
104 */
105 return EOK;
106}
107
108static driver_ops_t kbd_driver_ops = {
109 .add_device = add_kbd_device,
110};
111
112static driver_t kbd_driver = {
113 .name = "usbkbd",
114 .driver_ops = &kbd_driver_ops
115};
116
117int main(int argc, char *argv[])
118{
119 return driver_main(&kbd_driver);
120}
Note: See TracBrowser for help on using the repository browser.