1 | /*
|
---|
2 | * Copyright (c) 2011 Vojtech Horky
|
---|
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 usbvirthid
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /**
|
---|
33 | * @file
|
---|
34 | * Virtual USB HID device.
|
---|
35 | */
|
---|
36 | #include "virthid.h"
|
---|
37 | #include <errno.h>
|
---|
38 | #include <stdio.h>
|
---|
39 | #include <str.h>
|
---|
40 | #include <assert.h>
|
---|
41 | #include <usb/classes/classes.h>
|
---|
42 |
|
---|
43 | static int on_data_from_device(usbvirt_device_t *dev,
|
---|
44 | usb_endpoint_t ep, usb_transfer_type_t tr_type,
|
---|
45 | void *data, size_t data_size, size_t *actual_size)
|
---|
46 | {
|
---|
47 | vuhid_data_t *vuhid = dev->device_data;
|
---|
48 | vuhid_interface_t *iface = vuhid->in_endpoints_mapping[ep];
|
---|
49 | if (iface == NULL) {
|
---|
50 | return ESTALL;
|
---|
51 | }
|
---|
52 |
|
---|
53 | if (iface->on_data_in == NULL) {
|
---|
54 | return EBADCHECKSUM;
|
---|
55 | }
|
---|
56 |
|
---|
57 | return iface->on_data_in(iface, data, data_size, actual_size);
|
---|
58 | }
|
---|
59 |
|
---|
60 | static int on_data_to_device(usbvirt_device_t *dev,
|
---|
61 | usb_endpoint_t ep, usb_transfer_type_t tr_type,
|
---|
62 | void *data, size_t data_size)
|
---|
63 | {
|
---|
64 | vuhid_data_t *vuhid = dev->device_data;
|
---|
65 | vuhid_interface_t *iface = vuhid->out_endpoints_mapping[ep];
|
---|
66 | if (iface == NULL) {
|
---|
67 | return ESTALL;
|
---|
68 | }
|
---|
69 |
|
---|
70 | if (iface->on_data_out == NULL) {
|
---|
71 | return EBADCHECKSUM;
|
---|
72 | }
|
---|
73 |
|
---|
74 | return iface->on_data_out(iface, data, data_size);
|
---|
75 | }
|
---|
76 |
|
---|
77 | static int interface_life_fibril(void *arg)
|
---|
78 | {
|
---|
79 | vuhid_interface_t *iface = arg;
|
---|
80 | vuhid_data_t *hid_data = iface->vuhid_data;
|
---|
81 |
|
---|
82 | if (iface->live != NULL) {
|
---|
83 | iface->live(iface);
|
---|
84 | }
|
---|
85 |
|
---|
86 | fibril_mutex_lock(&hid_data->iface_count_mutex);
|
---|
87 | hid_data->iface_died_count++;
|
---|
88 | fibril_condvar_broadcast(&hid_data->iface_count_cv);
|
---|
89 | fibril_mutex_unlock(&hid_data->iface_count_mutex);
|
---|
90 |
|
---|
91 | return EOK;
|
---|
92 | }
|
---|
93 |
|
---|
94 |
|
---|
95 | static vuhid_interface_t *find_interface_by_id(vuhid_interface_t **ifaces,
|
---|
96 | const char *id)
|
---|
97 | {
|
---|
98 | vuhid_interface_t *iface = ifaces[0];
|
---|
99 | while (iface != NULL) {
|
---|
100 | if (str_cmp(iface->id, id) == 0) {
|
---|
101 | return iface;
|
---|
102 | }
|
---|
103 | iface++;
|
---|
104 | }
|
---|
105 |
|
---|
106 | return iface;
|
---|
107 | }
|
---|
108 |
|
---|
109 | int add_interface_by_id(vuhid_interface_t **interfaces, const char *id,
|
---|
110 | usbvirt_device_t *dev)
|
---|
111 | {
|
---|
112 | vuhid_interface_t *iface = find_interface_by_id(interfaces, id);
|
---|
113 | if (iface == NULL) {
|
---|
114 | return ENOENT;
|
---|
115 | }
|
---|
116 |
|
---|
117 | if ((iface->in_data_size == 0) && (iface->out_data_size == 0)) {
|
---|
118 | return EEMPTY;
|
---|
119 | }
|
---|
120 |
|
---|
121 | // FIXME - we shall set vuhid_data to NULL in the main() rather
|
---|
122 | // than to depend on individual interfaces
|
---|
123 | /* Already used interface. */
|
---|
124 | if (iface->vuhid_data != NULL) {
|
---|
125 | return EEXISTS;
|
---|
126 | }
|
---|
127 |
|
---|
128 | vuhid_data_t *hid_data = dev->device_data;
|
---|
129 |
|
---|
130 | /* Check that we have not run out of available endpoints. */
|
---|
131 | if ((iface->in_data_size > 0)
|
---|
132 | && (hid_data->in_endpoint_first_free >= VUHID_ENDPOINT_MAX)) {
|
---|
133 | return ELIMIT;
|
---|
134 | }
|
---|
135 | if ((iface->out_data_size > 0)
|
---|
136 | && (hid_data->out_endpoint_first_free >= VUHID_ENDPOINT_MAX)) {
|
---|
137 | return ELIMIT;
|
---|
138 | }
|
---|
139 |
|
---|
140 | if (dev->descriptors->configuration->descriptor->interface_count >=
|
---|
141 | VUHID_INTERFACE_MAX) {
|
---|
142 | return ELIMIT;
|
---|
143 | }
|
---|
144 |
|
---|
145 | /*
|
---|
146 | * How may descriptors we would add?
|
---|
147 | */
|
---|
148 | /* Start with interface descriptor. */
|
---|
149 | size_t new_descriptor_count = 1;
|
---|
150 | /* Having in/out data size positive means we would need endpoint. */
|
---|
151 | if (iface->in_data_size > 0) {
|
---|
152 | new_descriptor_count++;
|
---|
153 | }
|
---|
154 | if (iface->out_data_size > 0) {
|
---|
155 | new_descriptor_count++;
|
---|
156 | }
|
---|
157 | /* Having report descriptor means adding the HID descriptor. */
|
---|
158 | if (iface->report_descriptor != NULL) {
|
---|
159 | new_descriptor_count++;
|
---|
160 | }
|
---|
161 |
|
---|
162 | /* From now on, in case of errors, we goto to error_leave */
|
---|
163 | int rc;
|
---|
164 |
|
---|
165 | /*
|
---|
166 | * Prepare new descriptors.
|
---|
167 | */
|
---|
168 | printf("preparing descriptors...\n");
|
---|
169 | size_t descr_count = 0;
|
---|
170 | size_t total_descr_size = 0;
|
---|
171 | usb_standard_interface_descriptor_t *descr_iface = NULL;
|
---|
172 | hid_descriptor_t *descr_hid = NULL;
|
---|
173 | usb_standard_endpoint_descriptor_t *descr_ep_in = NULL;
|
---|
174 | usb_standard_endpoint_descriptor_t *descr_ep_out = NULL;
|
---|
175 |
|
---|
176 | size_t ep_count = 0;
|
---|
177 | if (iface->in_data_size > 0) {
|
---|
178 | ep_count++;
|
---|
179 | }
|
---|
180 | if (iface->out_data_size > 0) {
|
---|
181 | ep_count++;
|
---|
182 | }
|
---|
183 | assert(ep_count > 0);
|
---|
184 |
|
---|
185 | /* Interface would be needed always. */
|
---|
186 | descr_iface = malloc(sizeof(usb_standard_interface_descriptor_t));
|
---|
187 | if (descr_iface == NULL) {
|
---|
188 | rc = ENOMEM;
|
---|
189 | goto error_leave;
|
---|
190 | }
|
---|
191 | descr_count++;
|
---|
192 | total_descr_size += sizeof(usb_standard_interface_descriptor_t);
|
---|
193 | descr_iface->length = sizeof(usb_standard_interface_descriptor_t);
|
---|
194 | descr_iface->descriptor_type = USB_DESCTYPE_INTERFACE;
|
---|
195 | descr_iface->interface_number
|
---|
196 | = dev->descriptors->configuration->descriptor->interface_count;
|
---|
197 | descr_iface->alternate_setting = 0;
|
---|
198 | descr_iface->endpoint_count = ep_count;
|
---|
199 | descr_iface->interface_class = USB_CLASS_HID;
|
---|
200 | descr_iface->interface_subclass = iface->usb_subclass;
|
---|
201 | descr_iface->interface_protocol = iface->usb_protocol;
|
---|
202 | descr_iface->str_interface = 0;
|
---|
203 |
|
---|
204 | /* Create the HID descriptor. */
|
---|
205 | if (iface->report_descriptor != NULL) {
|
---|
206 | descr_hid = malloc(sizeof(hid_descriptor_t));
|
---|
207 | if (descr_hid == NULL) {
|
---|
208 | rc = ENOMEM;
|
---|
209 | goto error_leave;
|
---|
210 | }
|
---|
211 | descr_count++;
|
---|
212 | total_descr_size += sizeof(hid_descriptor_t);
|
---|
213 | descr_hid->length = sizeof(hid_descriptor_t);
|
---|
214 | descr_hid->type = USB_DESCTYPE_HID;
|
---|
215 | descr_hid->hid_spec_release = 0x101;
|
---|
216 | descr_hid->country_code = 0;
|
---|
217 | descr_hid->descriptor_count = 1;
|
---|
218 | descr_hid->descriptor1_type = USB_DESCTYPE_HID_REPORT;
|
---|
219 | descr_hid->descriptor1_length = iface->report_descriptor_size;
|
---|
220 | }
|
---|
221 |
|
---|
222 | /* Prepare the endpoint descriptors. */
|
---|
223 | if (iface->in_data_size > 0) {
|
---|
224 | descr_ep_in = malloc(sizeof(usb_standard_endpoint_descriptor_t));
|
---|
225 | if (descr_ep_in == NULL) {
|
---|
226 | rc = ENOMEM;
|
---|
227 | goto error_leave;
|
---|
228 | }
|
---|
229 | descr_count++;
|
---|
230 | total_descr_size += sizeof(usb_standard_endpoint_descriptor_t);
|
---|
231 | descr_ep_in->length = sizeof(usb_standard_endpoint_descriptor_t);
|
---|
232 | descr_ep_in->descriptor_type = USB_DESCTYPE_ENDPOINT;
|
---|
233 | descr_ep_in->endpoint_address
|
---|
234 | = 0x80 | hid_data->in_endpoint_first_free;
|
---|
235 | descr_ep_in->attributes = 3;
|
---|
236 | descr_ep_in->max_packet_size = iface->in_data_size;
|
---|
237 | descr_ep_in->poll_interval = 10;
|
---|
238 | }
|
---|
239 | if (iface->out_data_size > 0) {
|
---|
240 | descr_ep_out = malloc(sizeof(usb_standard_endpoint_descriptor_t));
|
---|
241 | if (descr_ep_out == NULL) {
|
---|
242 | rc = ENOMEM;
|
---|
243 | goto error_leave;
|
---|
244 | }
|
---|
245 | descr_count++;
|
---|
246 | total_descr_size += sizeof(usb_standard_endpoint_descriptor_t);
|
---|
247 | descr_ep_out->length = sizeof(usb_standard_endpoint_descriptor_t);
|
---|
248 | descr_ep_out->descriptor_type = USB_DESCTYPE_ENDPOINT;
|
---|
249 | descr_ep_out->endpoint_address
|
---|
250 | = 0x00 | hid_data->out_endpoint_first_free;
|
---|
251 | descr_ep_out->attributes = 3;
|
---|
252 | descr_ep_out->max_packet_size = iface->out_data_size;
|
---|
253 | descr_ep_out->poll_interval = 10;
|
---|
254 | }
|
---|
255 |
|
---|
256 | /* Extend existing extra descriptors with these ones. */
|
---|
257 | usbvirt_device_configuration_extras_t *extra_descriptors
|
---|
258 | = dev->descriptors->configuration->extra;
|
---|
259 | extra_descriptors = realloc(extra_descriptors,
|
---|
260 | sizeof(usbvirt_device_configuration_extras_t)
|
---|
261 | * (dev->descriptors->configuration->extra_count + descr_count));
|
---|
262 | if (extra_descriptors == NULL) {
|
---|
263 | rc = ENOMEM;
|
---|
264 | goto error_leave;
|
---|
265 | }
|
---|
266 |
|
---|
267 | /* Launch the "life" fibril. */
|
---|
268 | iface->vuhid_data = hid_data;
|
---|
269 | fid_t life_fibril = fibril_create(interface_life_fibril, iface);
|
---|
270 | if (life_fibril == 0) {
|
---|
271 | rc = ENOMEM;
|
---|
272 | goto error_leave;
|
---|
273 | }
|
---|
274 |
|
---|
275 | printf("adding extra descriptors...\n");
|
---|
276 | /* Allocation is okay, we can (actually have to now) overwrite the
|
---|
277 | * original pointer.
|
---|
278 | */
|
---|
279 | dev->descriptors->configuration->extra = extra_descriptors;
|
---|
280 | extra_descriptors += dev->descriptors->configuration->extra_count;
|
---|
281 |
|
---|
282 | /* Initialize them. */
|
---|
283 | #define _APPEND_DESCRIPTOR(descriptor) \
|
---|
284 | do { \
|
---|
285 | if ((descriptor) != NULL) { \
|
---|
286 | extra_descriptors->data = (uint8_t *) (descriptor); \
|
---|
287 | extra_descriptors->length = (descriptor)->length; \
|
---|
288 | extra_descriptors++; \
|
---|
289 | } \
|
---|
290 | } while (0)
|
---|
291 |
|
---|
292 | _APPEND_DESCRIPTOR(descr_iface);
|
---|
293 | _APPEND_DESCRIPTOR(descr_hid);
|
---|
294 | _APPEND_DESCRIPTOR(descr_ep_in);
|
---|
295 | _APPEND_DESCRIPTOR(descr_ep_out);
|
---|
296 | #undef _APPEND_DESCRIPTOR
|
---|
297 | dev->descriptors->configuration->extra_count += descr_count;
|
---|
298 |
|
---|
299 | /*
|
---|
300 | * Final changes.
|
---|
301 | * Increase the necessary counters, make endpoint mapping.
|
---|
302 | *
|
---|
303 | */
|
---|
304 | if (iface->in_data_size > 0) {
|
---|
305 | hid_data->in_endpoints_mapping[hid_data->in_endpoint_first_free]
|
---|
306 | = iface;
|
---|
307 | dev->ops->data_in[hid_data->in_endpoint_first_free]
|
---|
308 | = on_data_from_device;
|
---|
309 | hid_data->in_endpoint_first_free++;
|
---|
310 | }
|
---|
311 | if (iface->out_data_size > 0) {
|
---|
312 | hid_data->out_endpoints_mapping[hid_data->out_endpoint_first_free]
|
---|
313 | = iface;
|
---|
314 | dev->ops->data_out[hid_data->out_endpoint_first_free]
|
---|
315 | = on_data_to_device;
|
---|
316 | hid_data->out_endpoint_first_free++;
|
---|
317 | }
|
---|
318 |
|
---|
319 | hid_data->interface_mapping[
|
---|
320 | dev->descriptors->configuration->descriptor->interface_count]
|
---|
321 | = iface;
|
---|
322 |
|
---|
323 | dev->descriptors->configuration->descriptor->interface_count++;
|
---|
324 | dev->descriptors->configuration->descriptor->total_length
|
---|
325 | += total_descr_size;
|
---|
326 |
|
---|
327 | hid_data->iface_count++;
|
---|
328 | fibril_add_ready(life_fibril);
|
---|
329 |
|
---|
330 | return EOK;
|
---|
331 |
|
---|
332 | error_leave:
|
---|
333 | if (descr_iface != NULL) {
|
---|
334 | free(descr_iface);
|
---|
335 | }
|
---|
336 | if (descr_hid != NULL) {
|
---|
337 | free(descr_hid);
|
---|
338 | }
|
---|
339 | if (descr_ep_in != NULL) {
|
---|
340 | free(descr_ep_in);
|
---|
341 | }
|
---|
342 | if (descr_ep_out != NULL) {
|
---|
343 | free(descr_ep_out);
|
---|
344 | }
|
---|
345 |
|
---|
346 | return rc;
|
---|
347 | }
|
---|
348 |
|
---|
349 | void wait_for_interfaces_death(usbvirt_device_t *dev)
|
---|
350 | {
|
---|
351 | vuhid_data_t *hid_data = dev->device_data;
|
---|
352 |
|
---|
353 | fibril_mutex_lock(&hid_data->iface_count_mutex);
|
---|
354 | while (hid_data->iface_died_count < hid_data->iface_count) {
|
---|
355 | fibril_condvar_wait(&hid_data->iface_count_cv,
|
---|
356 | &hid_data->iface_count_mutex);
|
---|
357 | }
|
---|
358 | fibril_mutex_unlock(&hid_data->iface_count_mutex);
|
---|
359 | }
|
---|
360 |
|
---|
361 | /** @}
|
---|
362 | */
|
---|