[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 |
|
---|
[7d521e24] | 43 | #include <usb/dev/driver.h>
|
---|
| 44 | #include <usb/dev/poll.h>
|
---|
[966acede] | 45 |
|
---|
| 46 | #include "usbhid.h"
|
---|
| 47 |
|
---|
| 48 | #define NAME "usbhid"
|
---|
| 49 |
|
---|
| 50 | /**
|
---|
[555499da] | 51 | * Callback for passing a new device to the driver.
|
---|
[966acede] | 52 | *
|
---|
[555499da] | 53 | * @note Currently, only boot-protocol keyboards are supported by this driver.
|
---|
[966acede] | 54 | *
|
---|
[555499da] | 55 | * @param dev Structure representing the new device.
|
---|
[fbe148ee] | 56 | * @return Error code.
|
---|
[966acede] | 57 | */
|
---|
[555499da] | 58 | static int usb_hid_device_add(usb_device_t *dev)
|
---|
[966acede] | 59 | {
|
---|
[555499da] | 60 | usb_log_debug("%s\n", __FUNCTION__);
|
---|
[cc29622] | 61 |
|
---|
[555499da] | 62 | if (dev == NULL) {
|
---|
| 63 | usb_log_error("Wrong parameter given for add_device().\n");
|
---|
| 64 | return EINVAL;
|
---|
| 65 | }
|
---|
[cc29622] | 66 |
|
---|
[555499da] | 67 | if (dev->interface_no < 0) {
|
---|
| 68 | usb_log_error("Failed to add HID device: endpoints not found."
|
---|
| 69 | "\n");
|
---|
| 70 | return ENOTSUP;
|
---|
| 71 | }
|
---|
[065064e6] | 72 | usb_hid_dev_t *hid_dev =
|
---|
| 73 | usb_device_data_alloc(dev, sizeof(usb_hid_dev_t));
|
---|
[61257f4] | 74 | if (hid_dev == NULL) {
|
---|
[90df90c] | 75 | usb_log_error("Failed to create USB/HID device structure.\n");
|
---|
[61257f4] | 76 | return ENOMEM;
|
---|
| 77 | }
|
---|
[cc29622] | 78 |
|
---|
[61257f4] | 79 | int rc = usb_hid_init(hid_dev, dev);
|
---|
| 80 | if (rc != EOK) {
|
---|
| 81 | usb_log_error("Failed to initialize USB/HID device.\n");
|
---|
[a8c4e871] | 82 | usb_hid_deinit(hid_dev);
|
---|
[61257f4] | 83 | return rc;
|
---|
[98604cc] | 84 | }
|
---|
[cc29622] | 85 |
|
---|
[61257f4] | 86 | usb_log_debug("USB/HID device structure initialized.\n");
|
---|
[cc29622] | 87 |
|
---|
[966acede] | 88 | /* Start automated polling function.
|
---|
| 89 | * This will create a separate fibril that will query the device
|
---|
[90df90c] | 90 | * for the data continuously. */
|
---|
[966acede] | 91 | rc = usb_device_auto_poll(dev,
|
---|
| 92 | /* Index of the polling pipe. */
|
---|
[61257f4] | 93 | hid_dev->poll_pipe_index,
|
---|
[966acede] | 94 | /* Callback when data arrives. */
|
---|
[60c0573] | 95 | usb_hid_polling_callback,
|
---|
[966acede] | 96 | /* How much data to request. */
|
---|
[b77931d] | 97 | dev->pipes[hid_dev->poll_pipe_index].pipe.max_packet_size,
|
---|
[966acede] | 98 | /* Callback when the polling ends. */
|
---|
| 99 | usb_hid_polling_ended_callback,
|
---|
| 100 | /* Custom argument. */
|
---|
[61257f4] | 101 | hid_dev);
|
---|
[cc29622] | 102 |
|
---|
[966acede] | 103 | if (rc != EOK) {
|
---|
| 104 | usb_log_error("Failed to start polling fibril for `%s'.\n",
|
---|
[56fd7cf] | 105 | ddf_dev_get_name(dev->ddf_dev));
|
---|
[a8c4e871] | 106 | usb_hid_deinit(hid_dev);
|
---|
[966acede] | 107 | return rc;
|
---|
| 108 | }
|
---|
[68dbe3e] | 109 | hid_dev->running = true;
|
---|
[966acede] | 110 |
|
---|
[56fd7cf] | 111 | usb_log_info("HID device `%s' ready to use.\n",
|
---|
| 112 | ddf_dev_get_name(dev->ddf_dev));
|
---|
[966acede] | 113 |
|
---|
| 114 | return EOK;
|
---|
| 115 | }
|
---|
[76fbd9a] | 116 |
|
---|
[98604cc] | 117 | /**
|
---|
[fbe148ee] | 118 | * Callback for a device about to be removed from the driver.
|
---|
[98604cc] | 119 | *
|
---|
| 120 | * @param dev Structure representing the device.
|
---|
[fbe148ee] | 121 | * @return Error code.
|
---|
| 122 | */
|
---|
| 123 | static int usb_hid_device_rem(usb_device_t *dev)
|
---|
| 124 | {
|
---|
[90df90c] | 125 | // TODO: Stop device polling
|
---|
| 126 | // TODO: Call deinit (stops autorepeat too)
|
---|
[dcc10b8d] | 127 | return ENOTSUP;
|
---|
[fbe148ee] | 128 | }
|
---|
[76fbd9a] | 129 |
|
---|
[fbe148ee] | 130 | /**
|
---|
| 131 | * Callback for removing a device from the driver.
|
---|
[98604cc] | 132 | *
|
---|
[fbe148ee] | 133 | * @param dev Structure representing the device.
|
---|
| 134 | * @return Error code.
|
---|
[98604cc] | 135 | */
|
---|
| 136 | static int usb_hid_device_gone(usb_device_t *dev)
|
---|
| 137 | {
|
---|
[f750345] | 138 | assert(dev);
|
---|
| 139 | assert(dev->driver_data);
|
---|
[98604cc] | 140 | usb_hid_dev_t *hid_dev = dev->driver_data;
|
---|
[f750345] | 141 | unsigned tries = 100;
|
---|
| 142 | /* Wait for fail. */
|
---|
| 143 | while (hid_dev->running && tries--) {
|
---|
[68dbe3e] | 144 | async_usleep(100000);
|
---|
[f750345] | 145 | }
|
---|
| 146 | if (hid_dev->running) {
|
---|
| 147 | usb_log_error("Can't remove hid, still running.\n");
|
---|
| 148 | return EBUSY;
|
---|
[68dbe3e] | 149 | }
|
---|
| 150 |
|
---|
[a8c4e871] | 151 | usb_hid_deinit(hid_dev);
|
---|
[56fd7cf] | 152 | usb_log_debug2("%s destruction complete.\n", ddf_dev_get_name(dev->ddf_dev));
|
---|
[98604cc] | 153 | return EOK;
|
---|
| 154 | }
|
---|
[76fbd9a] | 155 |
|
---|
[98604cc] | 156 | /** USB generic driver callbacks */
|
---|
[fbe148ee] | 157 | static const usb_driver_ops_t usb_hid_driver_ops = {
|
---|
[98604cc] | 158 | .device_add = usb_hid_device_add,
|
---|
[fbe148ee] | 159 | .device_rem = usb_hid_device_rem,
|
---|
[98604cc] | 160 | .device_gone = usb_hid_device_gone,
|
---|
[966acede] | 161 | };
|
---|
[76fbd9a] | 162 |
|
---|
[98604cc] | 163 | /** The driver itself. */
|
---|
[fbe148ee] | 164 | static const usb_driver_t usb_hid_driver = {
|
---|
[966acede] | 165 | .name = NAME,
|
---|
| 166 | .ops = &usb_hid_driver_ops,
|
---|
| 167 | .endpoints = usb_hid_endpoints
|
---|
| 168 | };
|
---|
[76fbd9a] | 169 |
|
---|
[966acede] | 170 | int main(int argc, char *argv[])
|
---|
| 171 | {
|
---|
| 172 | printf(NAME ": HelenOS USB HID driver.\n");
|
---|
| 173 |
|
---|
[920d0fc] | 174 | log_init(NAME);
|
---|
[966acede] | 175 |
|
---|
| 176 | return usb_driver_main(&usb_hid_driver);
|
---|
| 177 | }
|
---|
| 178 | /**
|
---|
| 179 | * @}
|
---|
| 180 | */
|
---|