source: mainline/uspace/drv/usbkbd/main.c@ 7d521e24

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

libusbdev uses include usb/dev

  • Property mode set to 100644
File size: 7.1 KB
Line 
1/*
2 * Copyright (c) 2010 Vojtech Horky
3 * Copyright (c) 2011 Lubos Slovak
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/** @addtogroup drvusbhid
31 * @{
32 */
33/**
34 * @file
35 * Main routines of USB KBD driver.
36 */
37
38#include <ddf/driver.h>
39#include <usb/debug.h>
40#include <errno.h>
41#include <str_error.h>
42
43#include <usb/dev/driver.h>
44#include <usb/dev/poll.h>
45
46#include "kbddev.h"
47#include "kbdrepeat.h"
48
49/*----------------------------------------------------------------------------*/
50
51#define NAME "usbkbd"
52
53/**
54 * Function for adding a new device of type USB/HID/keyboard.
55 *
56 * This functions initializes required structures from the device's descriptors
57 * and starts new fibril for polling the keyboard for events and another one for
58 * handling auto-repeat of keys.
59 *
60 * During initialization, the keyboard is switched into boot protocol, the idle
61 * rate is set to 0 (infinity), resulting in the keyboard only reporting event
62 * when a key is pressed or released. Finally, the LED lights are turned on
63 * according to the default setup of lock keys.
64 *
65 * @note By default, the keyboards is initialized with Num Lock turned on and
66 * other locks turned off.
67 * @note Currently supports only boot-protocol keyboards.
68 *
69 * @param dev Device to add.
70 *
71 * @retval EOK if successful.
72 * @retval ENOMEM if there
73 * @return Other error code inherited from one of functions usb_kbd_init(),
74 * ddf_fun_bind() and ddf_fun_add_to_class().
75 *
76 * @sa usb_kbd_fibril(), usb_kbd_repeat_fibril()
77 */
78static int usb_kbd_try_add_device(usb_device_t *dev)
79{
80 /* Create the function exposed under /dev/devices. */
81 ddf_fun_t *kbd_fun = ddf_fun_create(dev->ddf_dev, fun_exposed,
82 "keyboard");
83 if (kbd_fun == NULL) {
84 usb_log_error("Could not create DDF function node.\n");
85 return ENOMEM;
86 }
87
88 /*
89 * Initialize device (get and process descriptors, get address, etc.)
90 */
91 usb_log_debug("Initializing USB/HID KBD device...\n");
92
93 usb_kbd_t *kbd_dev = usb_kbd_new();
94 if (kbd_dev == NULL) {
95 usb_log_error("Error while creating USB/HID KBD device "
96 "structure.\n");
97 ddf_fun_destroy(kbd_fun);
98 return ENOMEM; // TODO: some other code??
99 }
100
101 int rc = usb_kbd_init(kbd_dev, dev);
102
103 if (rc != EOK) {
104 usb_log_error("Failed to initialize USB/HID KBD device.\n");
105 ddf_fun_destroy(kbd_fun);
106 usb_kbd_free(&kbd_dev);
107 return rc;
108 }
109
110 usb_log_debug("USB/HID KBD device structure initialized.\n");
111
112 /*
113 * Store the initialized keyboard device and keyboard ops
114 * to the DDF function.
115 */
116 kbd_fun->driver_data = kbd_dev;
117 kbd_fun->ops = &keyboard_ops;
118
119 rc = ddf_fun_bind(kbd_fun);
120 if (rc != EOK) {
121 usb_log_error("Could not bind DDF function: %s.\n",
122 str_error(rc));
123 // TODO: Can / should I destroy the DDF function?
124 ddf_fun_destroy(kbd_fun);
125 usb_kbd_free(&kbd_dev);
126 return rc;
127 }
128
129 rc = ddf_fun_add_to_class(kbd_fun, "keyboard");
130 if (rc != EOK) {
131 usb_log_error(
132 "Could not add DDF function to class 'keyboard': %s.\n",
133 str_error(rc));
134 // TODO: Can / should I destroy the DDF function?
135 ddf_fun_destroy(kbd_fun);
136 usb_kbd_free(&kbd_dev);
137 return rc;
138 }
139
140 /*
141 * Create new fibril for handling this keyboard
142 */
143 //fid_t fid = fibril_create(usb_kbd_fibril, kbd_dev);
144
145 /* Start automated polling function.
146 * This will create a separate fibril that will query the device
147 * for the data continuously
148 */
149 rc = usb_device_auto_poll(dev,
150 /* Index of the polling pipe. */
151 USB_KBD_POLL_EP_NO,
152 /* Callback when data arrives. */
153 usb_kbd_polling_callback,
154 /* How much data to request. */
155 dev->pipes[USB_KBD_POLL_EP_NO].pipe->max_packet_size,
156 /* Callback when the polling ends. */
157 usb_kbd_polling_ended_callback,
158 /* Custom argument. */
159 kbd_dev);
160
161
162 if (rc != EOK) {
163 usb_log_error("Failed to start polling fibril for `%s'.\n",
164 dev->ddf_dev->name);
165 return rc;
166 }
167 //fibril_add_ready(fid);
168
169 /*
170 * Create new fibril for auto-repeat
171 */
172 fid_t fid = fibril_create(usb_kbd_repeat_fibril, kbd_dev);
173 if (fid == 0) {
174 usb_log_error("Failed to start fibril for KBD auto-repeat");
175 return ENOMEM;
176 }
177 fibril_add_ready(fid);
178
179 (void)keyboard_ops;
180
181 /*
182 * Hurrah, device is initialized.
183 */
184 return EOK;
185}
186
187/*----------------------------------------------------------------------------*/
188/**
189 * Callback for passing a new device to the driver.
190 *
191 * @note Currently, only boot-protocol keyboards are supported by this driver.
192 *
193 * @param dev Structure representing the new device.
194 *
195 * @retval EOK if successful.
196 * @retval EREFUSED if the device is not supported.
197 */
198static int usb_kbd_add_device(usb_device_t *dev)
199{
200 usb_log_debug("usb_kbd_add_device()\n");
201
202 if (dev->interface_no < 0) {
203 usb_log_warning("Device is not a supported keyboard.\n");
204 usb_log_error("Failed to add USB KBD device: endpoint not "
205 "found.\n");
206 return ENOTSUP;
207 }
208
209 int rc = usb_kbd_try_add_device(dev);
210
211 if (rc != EOK) {
212 usb_log_warning("Device is not a supported keyboard.\n");
213 usb_log_error("Failed to add KBD device: %s.\n",
214 str_error(rc));
215 return rc;
216 }
217
218 usb_log_info("Keyboard `%s' ready to use.\n", dev->ddf_dev->name);
219
220 return EOK;
221}
222
223/*----------------------------------------------------------------------------*/
224
225/* Currently, the framework supports only device adding. Once the framework
226 * supports unplug, more callbacks will be added. */
227static usb_driver_ops_t usb_kbd_driver_ops = {
228 .add_device = usb_kbd_add_device,
229};
230
231
232/* The driver itself. */
233static usb_driver_t usb_kbd_driver = {
234 .name = NAME,
235 .ops = &usb_kbd_driver_ops,
236 .endpoints = usb_kbd_endpoints
237};
238
239/*----------------------------------------------------------------------------*/
240
241int main(int argc, char *argv[])
242{
243 printf(NAME ": HelenOS USB KBD driver.\n");
244
245 usb_log_enable(USB_LOG_LEVEL_DEBUG, NAME);
246
247 return usb_driver_main(&usb_kbd_driver);
248}
249
250/**
251 * @}
252 */
Note: See TracBrowser for help on using the repository browser.