[c7137738] | 1 | /*
|
---|
| 2 | * Copyright (c) 2010 Vojtech Horky
|
---|
[1c13dac] | 3 | * Copyright (c) 2011 Lubos Slovak
|
---|
[c7137738] | 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 | */
|
---|
[1c13dac] | 29 |
|
---|
[ba54451] | 30 | /** @addtogroup drvusbhid
|
---|
| 31 | * @{
|
---|
| 32 | */
|
---|
[1c13dac] | 33 | /**
|
---|
| 34 | * @file
|
---|
[966acede] | 35 | * Main routines of USB KBD driver.
|
---|
[1c13dac] | 36 | */
|
---|
| 37 |
|
---|
[eb1a2f4] | 38 | #include <ddf/driver.h>
|
---|
[b43bcf1] | 39 | #include <usb/debug.h>
|
---|
[2391aaf] | 40 | #include <errno.h>
|
---|
[fbefd0e] | 41 | #include <str_error.h>
|
---|
[882f8b1] | 42 |
|
---|
[f8e4cb6] | 43 | #include <usb/devdrv.h>
|
---|
[5e07e2b5] | 44 | #include <usb/devpoll.h>
|
---|
[f8e4cb6] | 45 |
|
---|
[2391aaf] | 46 | #include "kbddev.h"
|
---|
[f8e4cb6] | 47 | #include "kbdrepeat.h"
|
---|
[45019865] | 48 |
|
---|
[2391aaf] | 49 | /*----------------------------------------------------------------------------*/
|
---|
[45019865] | 50 |
|
---|
[476b71ff] | 51 | #define NAME "usbkbd"
|
---|
[45019865] | 52 |
|
---|
[f8e4cb6] | 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
|
---|
[252e30c] | 73 | * @return Other error code inherited from one of functions usb_kbd_init(),
|
---|
[f8e4cb6] | 74 | * ddf_fun_bind() and ddf_fun_add_to_class().
|
---|
| 75 | *
|
---|
[252e30c] | 76 | * @sa usb_kbd_fibril(), usb_kbd_repeat_fibril()
|
---|
[f8e4cb6] | 77 | */
|
---|
[966acede] | 78 | static int usb_kbd_try_add_device(usb_device_t *dev)
|
---|
[f8e4cb6] | 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 |
|
---|
[252e30c] | 93 | usb_kbd_t *kbd_dev = usb_kbd_new();
|
---|
[f8e4cb6] | 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 |
|
---|
[252e30c] | 101 | int rc = usb_kbd_init(kbd_dev, dev);
|
---|
[f8e4cb6] | 102 |
|
---|
| 103 | if (rc != EOK) {
|
---|
| 104 | usb_log_error("Failed to initialize USB/HID KBD device.\n");
|
---|
| 105 | ddf_fun_destroy(kbd_fun);
|
---|
[252e30c] | 106 | usb_kbd_free(&kbd_dev);
|
---|
[f8e4cb6] | 107 | return rc;
|
---|
[b83edb93] | 108 | }
|
---|
[f8e4cb6] | 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);
|
---|
[252e30c] | 125 | usb_kbd_free(&kbd_dev);
|
---|
[f8e4cb6] | 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);
|
---|
[252e30c] | 136 | usb_kbd_free(&kbd_dev);
|
---|
[f8e4cb6] | 137 | return rc;
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | /*
|
---|
| 141 | * Create new fibril for handling this keyboard
|
---|
| 142 | */
|
---|
[252e30c] | 143 | //fid_t fid = fibril_create(usb_kbd_fibril, kbd_dev);
|
---|
[f8e4cb6] | 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. */
|
---|
[252e30c] | 151 | USB_KBD_POLL_EP_NO,
|
---|
[f8e4cb6] | 152 | /* Callback when data arrives. */
|
---|
[252e30c] | 153 | usb_kbd_polling_callback,
|
---|
[f8e4cb6] | 154 | /* How much data to request. */
|
---|
[252e30c] | 155 | dev->pipes[USB_KBD_POLL_EP_NO].pipe->max_packet_size,
|
---|
[f8e4cb6] | 156 | /* Callback when the polling ends. */
|
---|
[252e30c] | 157 | usb_kbd_polling_ended_callback,
|
---|
[f8e4cb6] | 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 | */
|
---|
[252e30c] | 172 | fid_t fid = fibril_create(usb_kbd_repeat_fibril, kbd_dev);
|
---|
[f8e4cb6] | 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 |
|
---|
[2391aaf] | 187 | /*----------------------------------------------------------------------------*/
|
---|
[48d2765] | 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 | */
|
---|
[966acede] | 198 | static int usb_kbd_add_device(usb_device_t *dev)
|
---|
[243cb86] | 199 | {
|
---|
[966acede] | 200 | usb_log_debug("usb_kbd_add_device()\n");
|
---|
[1c6c4092] | 201 |
|
---|
[f8e4cb6] | 202 | if (dev->interface_no < 0) {
|
---|
| 203 | usb_log_warning("Device is not a supported keyboard.\n");
|
---|
[b83edb93] | 204 | usb_log_error("Failed to add USB KBD device: endpoint not "
|
---|
| 205 | "found.\n");
|
---|
[f8e4cb6] | 206 | return ENOTSUP;
|
---|
| 207 | }
|
---|
| 208 |
|
---|
[966acede] | 209 | int rc = usb_kbd_try_add_device(dev);
|
---|
[dafab9e0] | 210 |
|
---|
| 211 | if (rc != EOK) {
|
---|
[fbefd0e] | 212 | usb_log_warning("Device is not a supported keyboard.\n");
|
---|
[966acede] | 213 | usb_log_error("Failed to add KBD device: %s.\n",
|
---|
[fbefd0e] | 214 | str_error(rc));
|
---|
| 215 | return rc;
|
---|
[b43bcf1] | 216 | }
|
---|
[882f8b1] | 217 |
|
---|
[f8e4cb6] | 218 | usb_log_info("Keyboard `%s' ready to use.\n", dev->ddf_dev->name);
|
---|
[fbefd0e] | 219 |
|
---|
[45019865] | 220 | return EOK;
|
---|
[243cb86] | 221 | }
|
---|
[91db50ac] | 222 |
|
---|
[2391aaf] | 223 | /*----------------------------------------------------------------------------*/
|
---|
[c7137738] | 224 |
|
---|
[f8e4cb6] | 225 | /* Currently, the framework supports only device adding. Once the framework
|
---|
| 226 | * supports unplug, more callbacks will be added. */
|
---|
[966acede] | 227 | static usb_driver_ops_t usb_kbd_driver_ops = {
|
---|
| 228 | .add_device = usb_kbd_add_device,
|
---|
[c7137738] | 229 | };
|
---|
| 230 |
|
---|
[2391aaf] | 231 |
|
---|
[f8e4cb6] | 232 | /* The driver itself. */
|
---|
[966acede] | 233 | static usb_driver_t usb_kbd_driver = {
|
---|
[f8e4cb6] | 234 | .name = NAME,
|
---|
[966acede] | 235 | .ops = &usb_kbd_driver_ops,
|
---|
[252e30c] | 236 | .endpoints = usb_kbd_endpoints
|
---|
[c7137738] | 237 | };
|
---|
| 238 |
|
---|
[2391aaf] | 239 | /*----------------------------------------------------------------------------*/
|
---|
| 240 |
|
---|
[c7137738] | 241 | int main(int argc, char *argv[])
|
---|
| 242 | {
|
---|
[966acede] | 243 | printf(NAME ": HelenOS USB KBD driver.\n");
|
---|
[fbefd0e] | 244 |
|
---|
[f8e4cb6] | 245 | usb_log_enable(USB_LOG_LEVEL_DEBUG, NAME);
|
---|
[fbefd0e] | 246 |
|
---|
[966acede] | 247 | return usb_driver_main(&usb_kbd_driver);
|
---|
[c7137738] | 248 | }
|
---|
[ba54451] | 249 |
|
---|
| 250 | /**
|
---|
| 251 | * @}
|
---|
| 252 | */
|
---|