1 | /*
|
---|
2 | * Copyright (c) 2010 Vojtech Horky
|
---|
3 | * Copyright (c) 2011 Lubos Slovak
|
---|
4 | * Copyright (c) 2018 Petr Manek, Ondrej Hlavaty
|
---|
5 | * All rights reserved.
|
---|
6 | *
|
---|
7 | * Redistribution and use in source and binary forms, with or without
|
---|
8 | * modification, are permitted provided that the following conditions
|
---|
9 | * are met:
|
---|
10 | *
|
---|
11 | * - Redistributions of source code must retain the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer.
|
---|
13 | * - Redistributions in binary form must reproduce the above copyright
|
---|
14 | * notice, this list of conditions and the following disclaimer in the
|
---|
15 | * documentation and/or other materials provided with the distribution.
|
---|
16 | * - The name of the author may not be used to endorse or promote products
|
---|
17 | * derived from this software without specific prior written permission.
|
---|
18 | *
|
---|
19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
20 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
21 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
22 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
23 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
24 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
28 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
29 | */
|
---|
30 |
|
---|
31 | /** @addtogroup drvusbhid
|
---|
32 | * @{
|
---|
33 | */
|
---|
34 | /**
|
---|
35 | * @file
|
---|
36 | * Main routines of USB HID driver.
|
---|
37 | */
|
---|
38 |
|
---|
39 | #include <ddf/driver.h>
|
---|
40 | #include <usb/debug.h>
|
---|
41 | #include <errno.h>
|
---|
42 | #include <str_error.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 errno_t usb_hid_device_add(usb_device_t *dev)
|
---|
60 | {
|
---|
61 | usb_log_debug("%s", __FUNCTION__);
|
---|
62 |
|
---|
63 | if (dev == NULL) {
|
---|
64 | usb_log_error("Wrong parameter given for add_device().");
|
---|
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.");
|
---|
77 | return ENOMEM;
|
---|
78 | }
|
---|
79 |
|
---|
80 | errno_t rc = usb_hid_init(hid_dev, dev);
|
---|
81 | if (rc != EOK) {
|
---|
82 | usb_log_error("Failed to initialize USB/HID device.");
|
---|
83 | usb_hid_deinit(hid_dev);
|
---|
84 | return rc;
|
---|
85 | }
|
---|
86 |
|
---|
87 | usb_log_debug("USB/HID device structure initialized.");
|
---|
88 |
|
---|
89 | /*
|
---|
90 | * Start automated polling function.
|
---|
91 | * This will create a separate fibril that will query the device
|
---|
92 | * for the data continuously.
|
---|
93 | */
|
---|
94 | rc = usb_polling_start(&hid_dev->polling);
|
---|
95 |
|
---|
96 | if (rc != EOK) {
|
---|
97 | usb_log_error("Failed to start polling fibril for `%s'.",
|
---|
98 | usb_device_get_name(dev));
|
---|
99 | usb_hid_deinit(hid_dev);
|
---|
100 | return rc;
|
---|
101 | }
|
---|
102 | hid_dev->running = true;
|
---|
103 |
|
---|
104 | usb_log_info("HID device `%s' ready.", usb_device_get_name(dev));
|
---|
105 |
|
---|
106 | return EOK;
|
---|
107 | }
|
---|
108 |
|
---|
109 | static errno_t join_and_clean(usb_device_t *dev)
|
---|
110 | {
|
---|
111 | assert(dev);
|
---|
112 | usb_hid_dev_t *hid_dev = usb_device_data_get(dev);
|
---|
113 | assert(hid_dev);
|
---|
114 |
|
---|
115 | /* Join polling fibril (ignoring error code). */
|
---|
116 | usb_polling_join(&hid_dev->polling);
|
---|
117 |
|
---|
118 | /* Clean up. */
|
---|
119 | usb_hid_deinit(hid_dev);
|
---|
120 | usb_log_info("%s destruction complete.", usb_device_get_name(dev));
|
---|
121 |
|
---|
122 | return EOK;
|
---|
123 | }
|
---|
124 |
|
---|
125 | /**
|
---|
126 | * Callback for a device about to be removed from the driver.
|
---|
127 | *
|
---|
128 | * @param dev Structure representing the device.
|
---|
129 | * @return Error code.
|
---|
130 | */
|
---|
131 | static errno_t usb_hid_device_remove(usb_device_t *dev)
|
---|
132 | {
|
---|
133 | assert(dev);
|
---|
134 | usb_hid_dev_t *hid_dev = usb_device_data_get(dev);
|
---|
135 | assert(hid_dev);
|
---|
136 |
|
---|
137 | usb_log_info("Device %s removed.", usb_device_get_name(dev));
|
---|
138 | return join_and_clean(dev);
|
---|
139 | }
|
---|
140 |
|
---|
141 | /**
|
---|
142 | * Callback for removing a device from the driver.
|
---|
143 | *
|
---|
144 | * @param dev Structure representing the device.
|
---|
145 | * @return Error code.
|
---|
146 | */
|
---|
147 | static errno_t usb_hid_device_gone(usb_device_t *dev)
|
---|
148 | {
|
---|
149 | assert(dev);
|
---|
150 | usb_hid_dev_t *hid_dev = usb_device_data_get(dev);
|
---|
151 | assert(hid_dev);
|
---|
152 |
|
---|
153 | usb_log_info("Device %s gone.", usb_device_get_name(dev));
|
---|
154 | return join_and_clean(dev);
|
---|
155 | }
|
---|
156 |
|
---|
157 | /** USB generic driver callbacks */
|
---|
158 | static const usb_driver_ops_t usb_hid_driver_ops = {
|
---|
159 | .device_add = usb_hid_device_add,
|
---|
160 | .device_remove = usb_hid_device_remove,
|
---|
161 | .device_gone = usb_hid_device_gone,
|
---|
162 | };
|
---|
163 |
|
---|
164 | /** The driver itself. */
|
---|
165 | static const usb_driver_t usb_hid_driver = {
|
---|
166 | .name = NAME,
|
---|
167 | .ops = &usb_hid_driver_ops,
|
---|
168 | .endpoints = usb_hid_endpoints
|
---|
169 | };
|
---|
170 |
|
---|
171 | int main(int argc, char *argv[])
|
---|
172 | {
|
---|
173 | printf(NAME ": HelenOS USB HID driver.\n");
|
---|
174 |
|
---|
175 | log_init(NAME);
|
---|
176 |
|
---|
177 | return usb_driver_main(&usb_hid_driver);
|
---|
178 | }
|
---|
179 | /**
|
---|
180 | * @}
|
---|
181 | */
|
---|