1 | /*
|
---|
2 | * Copyright (c) 2011 Lubos Slovak
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup drvusbhid
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /**
|
---|
33 | * @file
|
---|
34 | * USB HID driver API.
|
---|
35 | */
|
---|
36 |
|
---|
37 | #include <usb/debug.h>
|
---|
38 | #include <usb/classes/classes.h>
|
---|
39 | #include <errno.h>
|
---|
40 | #include <str_error.h>
|
---|
41 | #include <stdbool.h>
|
---|
42 |
|
---|
43 | #include <usbhid_iface.h>
|
---|
44 |
|
---|
45 | #include "hiddev.h"
|
---|
46 | #include "usbhid.h"
|
---|
47 |
|
---|
48 | const usb_endpoint_description_t usb_hid_generic_poll_endpoint_description = {
|
---|
49 | .transfer_type = USB_TRANSFER_INTERRUPT,
|
---|
50 | .direction = USB_DIRECTION_IN,
|
---|
51 | .interface_class = USB_CLASS_HID,
|
---|
52 | .interface_subclass = -1,
|
---|
53 | .interface_protocol = -1,
|
---|
54 | .flags = 0
|
---|
55 | };
|
---|
56 |
|
---|
57 | const char *HID_GENERIC_FUN_NAME = "hid";
|
---|
58 | const char *HID_GENERIC_CATEGORY = "hid";
|
---|
59 |
|
---|
60 |
|
---|
61 | static size_t usb_generic_hid_get_event_length(ddf_fun_t *fun);
|
---|
62 | static int usb_generic_hid_get_event(ddf_fun_t *fun, uint8_t *buffer,
|
---|
63 | size_t size, size_t *act_size, int *event_nr, unsigned int flags);
|
---|
64 | static int usb_generic_hid_client_connected(ddf_fun_t *fun);
|
---|
65 | static size_t usb_generic_get_report_descriptor_length(ddf_fun_t *fun);
|
---|
66 | static int usb_generic_get_report_descriptor(ddf_fun_t *fun, uint8_t *desc,
|
---|
67 | size_t size, size_t *actual_size);
|
---|
68 |
|
---|
69 | static usbhid_iface_t usb_generic_iface = {
|
---|
70 | .get_event = usb_generic_hid_get_event,
|
---|
71 | .get_event_length = usb_generic_hid_get_event_length,
|
---|
72 | .get_report_descriptor_length = usb_generic_get_report_descriptor_length,
|
---|
73 | .get_report_descriptor = usb_generic_get_report_descriptor
|
---|
74 | };
|
---|
75 |
|
---|
76 | static ddf_dev_ops_t usb_generic_hid_ops = {
|
---|
77 | .interfaces[USBHID_DEV_IFACE] = &usb_generic_iface,
|
---|
78 | .open = usb_generic_hid_client_connected
|
---|
79 | };
|
---|
80 |
|
---|
81 | /** Return hid_dev_t * for generic HID function node.
|
---|
82 | *
|
---|
83 | * For the generic HID subdriver the 'hid' function has usb_hid_gen_fun_t
|
---|
84 | * as soft state. Through that we can get to the usb_hid_dev_t.
|
---|
85 | */
|
---|
86 | static usb_hid_dev_t *fun_hid_dev(ddf_fun_t *fun)
|
---|
87 | {
|
---|
88 | return ((usb_hid_gen_fun_t *)ddf_fun_data_get(fun))->hid_dev;
|
---|
89 | }
|
---|
90 |
|
---|
91 | static size_t usb_generic_hid_get_event_length(ddf_fun_t *fun)
|
---|
92 | {
|
---|
93 | usb_log_debug2("Generic HID: Get event length (fun: %p, "
|
---|
94 | "fun->driver_data: %p.\n", fun, ddf_fun_data_get(fun));
|
---|
95 |
|
---|
96 | const usb_hid_dev_t *hid_dev = fun_hid_dev(fun);
|
---|
97 |
|
---|
98 | usb_log_debug2("hid_dev: %p, Max input report size (%zu).\n",
|
---|
99 | hid_dev, hid_dev->max_input_report_size);
|
---|
100 |
|
---|
101 | return hid_dev->max_input_report_size;
|
---|
102 | }
|
---|
103 |
|
---|
104 | static int usb_generic_hid_get_event(ddf_fun_t *fun, uint8_t *buffer,
|
---|
105 | size_t size, size_t *act_size, int *event_nr, unsigned int flags)
|
---|
106 | {
|
---|
107 | usb_log_debug2("Generic HID: Get event.\n");
|
---|
108 |
|
---|
109 | if (buffer == NULL || act_size == NULL || event_nr == NULL) {
|
---|
110 | usb_log_debug("No function");
|
---|
111 | return EINVAL;
|
---|
112 | }
|
---|
113 |
|
---|
114 | const usb_hid_dev_t *hid_dev = fun_hid_dev(fun);
|
---|
115 |
|
---|
116 | if (hid_dev->input_report_size > size) {
|
---|
117 | usb_log_debug("input_report_size > size (%zu, %zu)\n",
|
---|
118 | hid_dev->input_report_size, size);
|
---|
119 | return EINVAL; // TODO: other error code
|
---|
120 | }
|
---|
121 |
|
---|
122 | /*! @todo This should probably be somehow atomic. */
|
---|
123 | memcpy(buffer, hid_dev->input_report,
|
---|
124 | hid_dev->input_report_size);
|
---|
125 | *act_size = hid_dev->input_report_size;
|
---|
126 | *event_nr = usb_hid_report_number(hid_dev);
|
---|
127 |
|
---|
128 | usb_log_debug2("OK\n");
|
---|
129 |
|
---|
130 | return EOK;
|
---|
131 | }
|
---|
132 |
|
---|
133 | static size_t usb_generic_get_report_descriptor_length(ddf_fun_t *fun)
|
---|
134 | {
|
---|
135 | usb_log_debug("Generic HID: Get report descriptor length.\n");
|
---|
136 |
|
---|
137 | const usb_hid_dev_t *hid_dev = fun_hid_dev(fun);
|
---|
138 |
|
---|
139 | usb_log_debug2("hid_dev->report_desc_size = %zu\n",
|
---|
140 | hid_dev->report_desc_size);
|
---|
141 |
|
---|
142 | return hid_dev->report_desc_size;
|
---|
143 | }
|
---|
144 |
|
---|
145 | static int usb_generic_get_report_descriptor(ddf_fun_t *fun, uint8_t *desc,
|
---|
146 | size_t size, size_t *actual_size)
|
---|
147 | {
|
---|
148 | usb_log_debug2("Generic HID: Get report descriptor.\n");
|
---|
149 |
|
---|
150 | const usb_hid_dev_t *hid_dev = fun_hid_dev(fun);
|
---|
151 |
|
---|
152 | if (hid_dev->report_desc_size > size) {
|
---|
153 | return EINVAL;
|
---|
154 | }
|
---|
155 |
|
---|
156 | memcpy(desc, hid_dev->report_desc, hid_dev->report_desc_size);
|
---|
157 | *actual_size = hid_dev->report_desc_size;
|
---|
158 |
|
---|
159 | return EOK;
|
---|
160 | }
|
---|
161 |
|
---|
162 | static int usb_generic_hid_client_connected(ddf_fun_t *fun)
|
---|
163 | {
|
---|
164 | usb_log_debug("Generic HID: Client connected.\n");
|
---|
165 | return EOK;
|
---|
166 | }
|
---|
167 |
|
---|
168 | void usb_generic_hid_deinit(usb_hid_dev_t *hid_dev, void *data)
|
---|
169 | {
|
---|
170 | ddf_fun_t *fun = data;
|
---|
171 | if (fun == NULL)
|
---|
172 | return;
|
---|
173 |
|
---|
174 | if (ddf_fun_unbind(fun) != EOK) {
|
---|
175 | usb_log_error("Failed to unbind generic hid fun.\n");
|
---|
176 | return;
|
---|
177 | }
|
---|
178 | usb_log_debug2("%s unbound.\n", ddf_fun_get_name(fun));
|
---|
179 | ddf_fun_destroy(fun);
|
---|
180 | }
|
---|
181 |
|
---|
182 | int usb_generic_hid_init(usb_hid_dev_t *hid_dev, void **data)
|
---|
183 | {
|
---|
184 | usb_hid_gen_fun_t *hid_fun;
|
---|
185 |
|
---|
186 | if (hid_dev == NULL) {
|
---|
187 | return EINVAL;
|
---|
188 | }
|
---|
189 |
|
---|
190 | /* Create the exposed function. */
|
---|
191 | usb_log_debug("Creating DDF function %s...\n", HID_GENERIC_FUN_NAME);
|
---|
192 | ddf_fun_t *fun = usb_device_ddf_fun_create(hid_dev->usb_dev,
|
---|
193 | fun_exposed, HID_GENERIC_FUN_NAME);
|
---|
194 | if (fun == NULL) {
|
---|
195 | usb_log_error("Could not create DDF function node.\n");
|
---|
196 | return ENOMEM;
|
---|
197 | }
|
---|
198 |
|
---|
199 | /* Create softstate */
|
---|
200 | hid_fun = ddf_fun_data_alloc(fun, sizeof(usb_hid_gen_fun_t));
|
---|
201 | hid_fun->hid_dev = hid_dev;
|
---|
202 | ddf_fun_set_ops(fun, &usb_generic_hid_ops);
|
---|
203 |
|
---|
204 | int rc = ddf_fun_bind(fun);
|
---|
205 | if (rc != EOK) {
|
---|
206 | usb_log_error("Could not bind DDF function: %s.\n",
|
---|
207 | str_error(rc));
|
---|
208 | ddf_fun_destroy(fun);
|
---|
209 | return rc;
|
---|
210 | }
|
---|
211 |
|
---|
212 | usb_log_debug("HID function created. Handle: %" PRIun "\n",
|
---|
213 | ddf_fun_get_handle(fun));
|
---|
214 | *data = fun;
|
---|
215 |
|
---|
216 | return EOK;
|
---|
217 | }
|
---|
218 |
|
---|
219 | bool usb_generic_hid_polling_callback(usb_hid_dev_t *hid_dev, void *data)
|
---|
220 | {
|
---|
221 | /* Continue polling until the device is about to be removed. */
|
---|
222 | return !hid_dev->will_deinit;
|
---|
223 | }
|
---|
224 |
|
---|
225 | /**
|
---|
226 | * @}
|
---|
227 | */
|
---|