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 | #include <fibril_synch.h>
|
---|
43 |
|
---|
44 | #include <usb/dev/driver.h>
|
---|
45 | #include <usb/dev/poll.h>
|
---|
46 |
|
---|
47 | #include "usbhid.h"
|
---|
48 |
|
---|
49 | #define NAME "usbhid"
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * Callback for passing a new device to the driver.
|
---|
53 | *
|
---|
54 | * @note Currently, only boot-protocol keyboards are supported by this driver.
|
---|
55 | *
|
---|
56 | * @param dev Structure representing the new device.
|
---|
57 | * @return Error code.
|
---|
58 | */
|
---|
59 | static int usb_hid_device_add(usb_device_t *dev)
|
---|
60 | {
|
---|
61 | usb_log_debug("%s\n", __FUNCTION__);
|
---|
62 |
|
---|
63 | if (dev == NULL) {
|
---|
64 | usb_log_error("Wrong parameter given for add_device().\n");
|
---|
65 | return EINVAL;
|
---|
66 | }
|
---|
67 |
|
---|
68 | if (usb_device_get_iface_number(dev) < 0) {
|
---|
69 | usb_log_error("Failed to add HID device: endpoints not found."
|
---|
70 | "\n");
|
---|
71 | return ENOTSUP;
|
---|
72 | }
|
---|
73 | usb_hid_dev_t *hid_dev =
|
---|
74 | usb_device_data_alloc(dev, sizeof(usb_hid_dev_t));
|
---|
75 | if (hid_dev == NULL) {
|
---|
76 | usb_log_error("Failed to create USB/HID device structure.\n");
|
---|
77 | return ENOMEM;
|
---|
78 | }
|
---|
79 |
|
---|
80 | int rc = usb_hid_init(hid_dev, dev);
|
---|
81 | if (rc != EOK) {
|
---|
82 | usb_log_error("Failed to initialize USB/HID device.\n");
|
---|
83 | usb_hid_deinit(hid_dev);
|
---|
84 | return rc;
|
---|
85 | }
|
---|
86 |
|
---|
87 | usb_log_debug("USB/HID device structure initialized.\n");
|
---|
88 |
|
---|
89 | /* Start automated polling function.
|
---|
90 | * This will create a separate fibril that will query the device
|
---|
91 | * for the data continuously. */
|
---|
92 | rc = usb_device_auto_poll_desc(dev,
|
---|
93 | /* Index of the polling pipe. */
|
---|
94 | hid_dev->poll_pipe_mapping->description,
|
---|
95 | /* Callback when data arrives. */
|
---|
96 | usb_hid_polling_callback,
|
---|
97 | /* How much data to request. */
|
---|
98 | hid_dev->poll_pipe_mapping->pipe.desc.max_transfer_size,
|
---|
99 | /* Delay */
|
---|
100 | -1,
|
---|
101 | /* Callback when the polling fails. */
|
---|
102 | usb_hid_polling_error_callback,
|
---|
103 | /* Callback when the polling ends. */
|
---|
104 | usb_hid_polling_ended_callback,
|
---|
105 | /* Custom argument. */
|
---|
106 | hid_dev);
|
---|
107 |
|
---|
108 | if (rc != EOK) {
|
---|
109 | usb_log_error("Failed to start polling fibril for `%s'.\n",
|
---|
110 | usb_device_get_name(dev));
|
---|
111 | usb_hid_deinit(hid_dev);
|
---|
112 | return rc;
|
---|
113 | }
|
---|
114 | hid_dev->running = true;
|
---|
115 |
|
---|
116 | usb_log_info("HID device `%s' ready.\n", usb_device_get_name(dev));
|
---|
117 |
|
---|
118 | return EOK;
|
---|
119 | }
|
---|
120 |
|
---|
121 | /**
|
---|
122 | * Callback for a device about to be removed from the driver.
|
---|
123 | *
|
---|
124 | * @param dev Structure representing the device.
|
---|
125 | * @return Error code.
|
---|
126 | */
|
---|
127 | static int usb_hid_device_remove(usb_device_t *dev)
|
---|
128 | {
|
---|
129 | assert(dev);
|
---|
130 | usb_hid_dev_t *hid_dev = usb_device_data_get(dev);
|
---|
131 | assert(hid_dev);
|
---|
132 |
|
---|
133 | usb_log_info("%s will be removed, setting remove flag.\n", usb_device_get_name(dev));
|
---|
134 | usb_hid_prepare_deinit(hid_dev);
|
---|
135 |
|
---|
136 | return EOK;
|
---|
137 | }
|
---|
138 |
|
---|
139 | /**
|
---|
140 | * Callback for when a device has just been from the driver.
|
---|
141 | *
|
---|
142 | * @param dev Structure representing the device.
|
---|
143 | * @return Error code.
|
---|
144 | */
|
---|
145 | static int usb_hid_device_removed(usb_device_t *dev)
|
---|
146 | {
|
---|
147 | assert(dev);
|
---|
148 | usb_hid_dev_t *hid_dev = usb_device_data_get(dev);
|
---|
149 | assert(hid_dev);
|
---|
150 |
|
---|
151 | usb_log_info("%s endpoints unregistered, joining polling fibril.\n", usb_device_get_name(dev));
|
---|
152 |
|
---|
153 | /* Join polling fibril. */
|
---|
154 | fibril_mutex_lock(&hid_dev->guard);
|
---|
155 | while (hid_dev->running)
|
---|
156 | fibril_condvar_wait(&hid_dev->poll_end, &hid_dev->guard);
|
---|
157 | fibril_mutex_unlock(&hid_dev->guard);
|
---|
158 |
|
---|
159 | /* Clean up. */
|
---|
160 | usb_hid_deinit(hid_dev);
|
---|
161 | usb_log_info("%s destruction complete.\n", usb_device_get_name(dev));
|
---|
162 |
|
---|
163 | return EOK;
|
---|
164 | }
|
---|
165 |
|
---|
166 | /**
|
---|
167 | * Callback for removing a device from the driver.
|
---|
168 | *
|
---|
169 | * @param dev Structure representing the device.
|
---|
170 | * @return Error code.
|
---|
171 | */
|
---|
172 | static int usb_hid_device_gone(usb_device_t *dev)
|
---|
173 | {
|
---|
174 | assert(dev);
|
---|
175 | usb_hid_dev_t *hid_dev = usb_device_data_get(dev);
|
---|
176 | assert(hid_dev);
|
---|
177 |
|
---|
178 | usb_log_debug2("Device %s gone.\n", usb_device_get_name(dev));
|
---|
179 | usb_hid_prepare_deinit(hid_dev);
|
---|
180 |
|
---|
181 | unsigned tries = 100;
|
---|
182 | /* Wait for fail. */
|
---|
183 | while (hid_dev->running && tries--) {
|
---|
184 | async_usleep(100000);
|
---|
185 | }
|
---|
186 | if (hid_dev->running) {
|
---|
187 | usb_log_error("Can't remove hid, still running.\n");
|
---|
188 | return EBUSY;
|
---|
189 | }
|
---|
190 |
|
---|
191 | usb_hid_deinit(hid_dev);
|
---|
192 | usb_log_debug2("%s destruction complete.\n", usb_device_get_name(dev));
|
---|
193 |
|
---|
194 | return EOK;
|
---|
195 | }
|
---|
196 |
|
---|
197 | /** USB generic driver callbacks */
|
---|
198 | static const usb_driver_ops_t usb_hid_driver_ops = {
|
---|
199 | .device_add = usb_hid_device_add,
|
---|
200 | .device_remove = usb_hid_device_remove,
|
---|
201 | .device_removed = usb_hid_device_removed,
|
---|
202 | .device_gone = usb_hid_device_gone,
|
---|
203 | };
|
---|
204 |
|
---|
205 | /** The driver itself. */
|
---|
206 | static const usb_driver_t usb_hid_driver = {
|
---|
207 | .name = NAME,
|
---|
208 | .ops = &usb_hid_driver_ops,
|
---|
209 | .endpoints = usb_hid_endpoints
|
---|
210 | };
|
---|
211 |
|
---|
212 | int main(int argc, char *argv[])
|
---|
213 | {
|
---|
214 | printf(NAME ": HelenOS USB HID driver.\n");
|
---|
215 |
|
---|
216 | log_init(NAME);
|
---|
217 |
|
---|
218 | return usb_driver_main(&usb_hid_driver);
|
---|
219 | }
|
---|
220 | /**
|
---|
221 | * @}
|
---|
222 | */
|
---|