[966acede] | 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 HID 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/devdrv.h>
|
---|
[5e07e2b5] | 44 | #include <usb/devpoll.h>
|
---|
[966acede] | 45 |
|
---|
| 46 | #include "usbhid.h"
|
---|
| 47 |
|
---|
| 48 | /*----------------------------------------------------------------------------*/
|
---|
| 49 |
|
---|
| 50 | #define NAME "usbhid"
|
---|
| 51 |
|
---|
| 52 | /**
|
---|
| 53 | * Function for adding a new device of type USB/HID/keyboard.
|
---|
| 54 | *
|
---|
| 55 | * This functions initializes required structures from the device's descriptors
|
---|
| 56 | * and starts new fibril for polling the keyboard for events and another one for
|
---|
| 57 | * handling auto-repeat of keys.
|
---|
| 58 | *
|
---|
| 59 | * During initialization, the keyboard is switched into boot protocol, the idle
|
---|
| 60 | * rate is set to 0 (infinity), resulting in the keyboard only reporting event
|
---|
| 61 | * when a key is pressed or released. Finally, the LED lights are turned on
|
---|
| 62 | * according to the default setup of lock keys.
|
---|
| 63 | *
|
---|
| 64 | * @note By default, the keyboards is initialized with Num Lock turned on and
|
---|
| 65 | * other locks turned off.
|
---|
| 66 | * @note Currently supports only boot-protocol keyboards.
|
---|
| 67 | *
|
---|
| 68 | * @param dev Device to add.
|
---|
| 69 | *
|
---|
| 70 | * @retval EOK if successful.
|
---|
| 71 | * @retval ENOMEM if there
|
---|
| 72 | * @return Other error code inherited from one of functions usb_kbd_init(),
|
---|
| 73 | * ddf_fun_bind() and ddf_fun_add_to_class().
|
---|
| 74 | */
|
---|
| 75 | static int usb_hid_try_add_device(usb_device_t *dev)
|
---|
| 76 | {
|
---|
[56f8a04] | 77 | assert(dev != NULL);
|
---|
| 78 |
|
---|
[966acede] | 79 | /*
|
---|
| 80 | * Initialize device (get and process descriptors, get address, etc.)
|
---|
| 81 | */
|
---|
| 82 | usb_log_debug("Initializing USB/HID device...\n");
|
---|
| 83 |
|
---|
[61257f4] | 84 | usb_hid_dev_t *hid_dev = usb_hid_new();
|
---|
| 85 | if (hid_dev == NULL) {
|
---|
| 86 | usb_log_error("Error while creating USB/HID device "
|
---|
| 87 | "structure.\n");
|
---|
| 88 | return ENOMEM;
|
---|
| 89 | }
|
---|
[966acede] | 90 |
|
---|
[61257f4] | 91 | int rc = usb_hid_init(hid_dev, dev);
|
---|
[966acede] | 92 |
|
---|
[61257f4] | 93 | if (rc != EOK) {
|
---|
| 94 | usb_log_error("Failed to initialize USB/HID device.\n");
|
---|
| 95 | usb_hid_free(&hid_dev);
|
---|
| 96 | return rc;
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | usb_log_debug("USB/HID device structure initialized.\n");
|
---|
[966acede] | 100 |
|
---|
[61257f4] | 101 | /* Create the function exposed under /dev/devices. */
|
---|
| 102 | ddf_fun_t *hid_fun = ddf_fun_create(dev->ddf_dev, fun_exposed,
|
---|
[60c0573] | 103 | usb_hid_get_function_name(hid_dev));
|
---|
[61257f4] | 104 | if (hid_fun == NULL) {
|
---|
| 105 | usb_log_error("Could not create DDF function node.\n");
|
---|
| 106 | usb_hid_free(&hid_dev);
|
---|
| 107 | return ENOMEM;
|
---|
| 108 | }
|
---|
[966acede] | 109 |
|
---|
| 110 | /*
|
---|
[61257f4] | 111 | * Store the initialized HID device and HID ops
|
---|
[966acede] | 112 | * to the DDF function.
|
---|
| 113 | */
|
---|
[61257f4] | 114 | hid_fun->ops = &hid_dev->ops;
|
---|
| 115 | hid_fun->driver_data = hid_dev; // TODO: maybe change to hid_dev->data
|
---|
[966acede] | 116 |
|
---|
[61257f4] | 117 | rc = ddf_fun_bind(hid_fun);
|
---|
[966acede] | 118 | if (rc != EOK) {
|
---|
| 119 | usb_log_error("Could not bind DDF function: %s.\n",
|
---|
| 120 | str_error(rc));
|
---|
| 121 | // TODO: Can / should I destroy the DDF function?
|
---|
| 122 | ddf_fun_destroy(hid_fun);
|
---|
[61257f4] | 123 | usb_hid_free(&hid_dev);
|
---|
[966acede] | 124 | return rc;
|
---|
| 125 | }
|
---|
| 126 |
|
---|
[60c0573] | 127 | rc = ddf_fun_add_to_class(hid_fun, usb_hid_get_class_name(hid_dev));
|
---|
[966acede] | 128 | if (rc != EOK) {
|
---|
| 129 | usb_log_error(
|
---|
| 130 | "Could not add DDF function to class 'hid': %s.\n",
|
---|
| 131 | str_error(rc));
|
---|
| 132 | // TODO: Can / should I destroy the DDF function?
|
---|
| 133 | ddf_fun_destroy(hid_fun);
|
---|
[61257f4] | 134 | usb_hid_free(&hid_dev);
|
---|
[966acede] | 135 | return rc;
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | /* Start automated polling function.
|
---|
| 139 | * This will create a separate fibril that will query the device
|
---|
| 140 | * for the data continuously
|
---|
| 141 | */
|
---|
| 142 | rc = usb_device_auto_poll(dev,
|
---|
| 143 | /* Index of the polling pipe. */
|
---|
[61257f4] | 144 | hid_dev->poll_pipe_index,
|
---|
[966acede] | 145 | /* Callback when data arrives. */
|
---|
[60c0573] | 146 | usb_hid_polling_callback,
|
---|
[966acede] | 147 | /* How much data to request. */
|
---|
[61257f4] | 148 | dev->pipes[hid_dev->poll_pipe_index].pipe->max_packet_size,
|
---|
[966acede] | 149 | /* Callback when the polling ends. */
|
---|
| 150 | usb_hid_polling_ended_callback,
|
---|
| 151 | /* Custom argument. */
|
---|
[61257f4] | 152 | hid_dev);
|
---|
[966acede] | 153 |
|
---|
| 154 |
|
---|
| 155 | if (rc != EOK) {
|
---|
| 156 | usb_log_error("Failed to start polling fibril for `%s'.\n",
|
---|
| 157 | dev->ddf_dev->name);
|
---|
| 158 | return rc;
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | /*
|
---|
| 162 | * Hurrah, device is initialized.
|
---|
| 163 | */
|
---|
| 164 | return EOK;
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 | /*----------------------------------------------------------------------------*/
|
---|
| 168 | /**
|
---|
| 169 | * Callback for passing a new device to the driver.
|
---|
| 170 | *
|
---|
| 171 | * @note Currently, only boot-protocol keyboards are supported by this driver.
|
---|
| 172 | *
|
---|
| 173 | * @param dev Structure representing the new device.
|
---|
| 174 | *
|
---|
| 175 | * @retval EOK if successful.
|
---|
| 176 | * @retval EREFUSED if the device is not supported.
|
---|
| 177 | */
|
---|
| 178 | static int usb_hid_add_device(usb_device_t *dev)
|
---|
| 179 | {
|
---|
| 180 | usb_log_debug("usb_hid_add_device()\n");
|
---|
| 181 |
|
---|
[8f8f34ad] | 182 | if (dev == NULL) {
|
---|
[56f8a04] | 183 | usb_log_warning("Wrong parameter given for add_device().\n");
|
---|
| 184 | return EINVAL;
|
---|
| 185 | }
|
---|
| 186 |
|
---|
[966acede] | 187 | if (dev->interface_no < 0) {
|
---|
| 188 | usb_log_warning("Device is not a supported HID device.\n");
|
---|
[61257f4] | 189 | usb_log_error("Failed to add HID device: endpoints not found."
|
---|
[966acede] | 190 | "\n");
|
---|
| 191 | return ENOTSUP;
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | int rc = usb_hid_try_add_device(dev);
|
---|
| 195 |
|
---|
| 196 | if (rc != EOK) {
|
---|
| 197 | usb_log_warning("Device is not a supported HID device.\n");
|
---|
| 198 | usb_log_error("Failed to add HID device: %s.\n",
|
---|
| 199 | str_error(rc));
|
---|
| 200 | return rc;
|
---|
| 201 | }
|
---|
| 202 |
|
---|
| 203 | usb_log_info("HID device `%s' ready to use.\n", dev->ddf_dev->name);
|
---|
| 204 |
|
---|
| 205 | return EOK;
|
---|
| 206 | }
|
---|
| 207 |
|
---|
| 208 | /*----------------------------------------------------------------------------*/
|
---|
| 209 |
|
---|
| 210 | /* Currently, the framework supports only device adding. Once the framework
|
---|
| 211 | * supports unplug, more callbacks will be added. */
|
---|
| 212 | static usb_driver_ops_t usb_hid_driver_ops = {
|
---|
| 213 | .add_device = usb_hid_add_device,
|
---|
| 214 | };
|
---|
| 215 |
|
---|
| 216 |
|
---|
| 217 | /* The driver itself. */
|
---|
| 218 | static usb_driver_t usb_hid_driver = {
|
---|
| 219 | .name = NAME,
|
---|
| 220 | .ops = &usb_hid_driver_ops,
|
---|
| 221 | .endpoints = usb_hid_endpoints
|
---|
| 222 | };
|
---|
| 223 |
|
---|
| 224 | /*----------------------------------------------------------------------------*/
|
---|
| 225 |
|
---|
| 226 | int main(int argc, char *argv[])
|
---|
| 227 | {
|
---|
| 228 | printf(NAME ": HelenOS USB HID driver.\n");
|
---|
| 229 |
|
---|
| 230 | usb_log_enable(USB_LOG_LEVEL_DEBUG, NAME);
|
---|
| 231 |
|
---|
| 232 | return usb_driver_main(&usb_hid_driver);
|
---|
| 233 | }
|
---|
| 234 |
|
---|
| 235 | /**
|
---|
| 236 | * @}
|
---|
| 237 | */
|
---|