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