source: mainline/uspace/app/vuhid/device.c@ 3fafe5e0

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 3fafe5e0 was 3bacee1, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Make ccheck-fix again and commit more good files.

  • Property mode set to 100644
File size: 10.1 KB
Line 
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
43static errno_t 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
60static errno_t on_data_to_device(usbvirt_device_t *dev,
61 usb_endpoint_t ep, usb_transfer_type_t tr_type,
62 const 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
77static errno_t 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
95static vuhid_interface_t *find_interface_by_id(vuhid_interface_t **ifaces,
96 const char *id)
97{
98 if ((ifaces == NULL) || (id == NULL)) {
99 return NULL;
100 }
101 while (*ifaces != NULL) {
102 if (str_cmp((*ifaces)->id, id) == 0) {
103 return *ifaces;
104 }
105 ifaces++;
106 }
107
108 return NULL;
109}
110
111errno_t add_interface_by_id(vuhid_interface_t **interfaces, const char *id,
112 usbvirt_device_t *dev)
113{
114 vuhid_interface_t *iface = find_interface_by_id(interfaces, id);
115 if (iface == NULL) {
116 return ENOENT;
117 }
118
119 if ((iface->in_data_size == 0) && (iface->out_data_size == 0)) {
120 return EEMPTY;
121 }
122
123 /* Already used interface. */
124 if (iface->vuhid_data != NULL) {
125 return EEXIST;
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 errno_t rc;
164
165 /*
166 * Prepare new descriptors.
167 */
168 size_t descr_count = 0;
169 size_t total_descr_size = 0;
170 usb_standard_interface_descriptor_t *descr_iface = NULL;
171 hid_descriptor_t *descr_hid = NULL;
172 usb_standard_endpoint_descriptor_t *descr_ep_in = NULL;
173 usb_standard_endpoint_descriptor_t *descr_ep_out = NULL;
174
175 size_t ep_count = 0;
176 if (iface->in_data_size > 0) {
177 ep_count++;
178 }
179 if (iface->out_data_size > 0) {
180 ep_count++;
181 }
182 assert(ep_count > 0);
183
184 /* Interface would be needed always. */
185 descr_iface = malloc(sizeof(usb_standard_interface_descriptor_t));
186 if (descr_iface == NULL) {
187 rc = ENOMEM;
188 goto error_leave;
189 }
190 descr_count++;
191 total_descr_size += sizeof(usb_standard_interface_descriptor_t);
192 descr_iface->length = sizeof(usb_standard_interface_descriptor_t);
193 descr_iface->descriptor_type = USB_DESCTYPE_INTERFACE;
194 descr_iface->interface_number =
195 dev->descriptors->configuration->descriptor->interface_count;
196 descr_iface->alternate_setting = 0;
197 descr_iface->endpoint_count = ep_count;
198 descr_iface->interface_class = USB_CLASS_HID;
199 descr_iface->interface_subclass = iface->usb_subclass;
200 descr_iface->interface_protocol = iface->usb_protocol;
201 descr_iface->str_interface = 0;
202
203 /* Create the HID descriptor. */
204 if (iface->report_descriptor != NULL) {
205 descr_hid = malloc(sizeof(hid_descriptor_t));
206 if (descr_hid == NULL) {
207 rc = ENOMEM;
208 goto error_leave;
209 }
210 descr_count++;
211 total_descr_size += sizeof(hid_descriptor_t);
212 descr_hid->length = sizeof(hid_descriptor_t);
213 descr_hid->type = USB_DESCTYPE_HID;
214 descr_hid->hid_spec_release = 0x101;
215 descr_hid->country_code = 0;
216 descr_hid->descriptor_count = 1;
217 descr_hid->descriptor1_type = USB_DESCTYPE_HID_REPORT;
218 descr_hid->descriptor1_length = iface->report_descriptor_size;
219 }
220
221 /* Prepare the endpoint descriptors. */
222 if (iface->in_data_size > 0) {
223 descr_ep_in = malloc(sizeof(usb_standard_endpoint_descriptor_t));
224 if (descr_ep_in == NULL) {
225 rc = ENOMEM;
226 goto error_leave;
227 }
228 descr_count++;
229 total_descr_size += sizeof(usb_standard_endpoint_descriptor_t);
230 descr_ep_in->length = sizeof(usb_standard_endpoint_descriptor_t);
231 descr_ep_in->descriptor_type = USB_DESCTYPE_ENDPOINT;
232 descr_ep_in->endpoint_address =
233 0x80 | hid_data->in_endpoint_first_free;
234 descr_ep_in->attributes = 3;
235 descr_ep_in->max_packet_size = iface->in_data_size;
236 descr_ep_in->poll_interval = 10;
237 }
238 if (iface->out_data_size > 0) {
239 descr_ep_out = malloc(sizeof(usb_standard_endpoint_descriptor_t));
240 if (descr_ep_out == NULL) {
241 rc = ENOMEM;
242 goto error_leave;
243 }
244 descr_count++;
245 total_descr_size += sizeof(usb_standard_endpoint_descriptor_t);
246 descr_ep_out->length = sizeof(usb_standard_endpoint_descriptor_t);
247 descr_ep_out->descriptor_type = USB_DESCTYPE_ENDPOINT;
248 descr_ep_out->endpoint_address =
249 0x00 | hid_data->out_endpoint_first_free;
250 descr_ep_out->attributes = 3;
251 descr_ep_out->max_packet_size = iface->out_data_size;
252 descr_ep_out->poll_interval = 10;
253 }
254
255 /* Extend existing extra descriptors with these ones. */
256 usbvirt_device_configuration_extras_t *extra_descriptors;
257 extra_descriptors = realloc(dev->descriptors->configuration->extra,
258 sizeof(usbvirt_device_configuration_extras_t) *
259 (dev->descriptors->configuration->extra_count + descr_count));
260 if (extra_descriptors == NULL) {
261 rc = ENOMEM;
262 goto error_leave;
263 }
264
265 /* Launch the "life" fibril. */
266 iface->vuhid_data = hid_data;
267 fid_t life_fibril = fibril_create(interface_life_fibril, iface);
268 if (life_fibril == 0) {
269 rc = ENOMEM;
270 goto error_leave;
271 }
272
273 /* Allocation is okay, we can (actually have to now) overwrite the
274 * original pointer.
275 */
276 dev->descriptors->configuration->extra = extra_descriptors;
277 extra_descriptors += dev->descriptors->configuration->extra_count;
278
279 /* Initialize them. */
280#define _APPEND_DESCRIPTOR(descriptor) \
281 do { \
282 if ((descriptor) != NULL) { \
283 extra_descriptors->data = (uint8_t *) (descriptor); \
284 extra_descriptors->length = (descriptor)->length; \
285 extra_descriptors++; \
286 } \
287 } while (0)
288
289 _APPEND_DESCRIPTOR(descr_iface);
290 _APPEND_DESCRIPTOR(descr_hid);
291 _APPEND_DESCRIPTOR(descr_ep_in);
292 _APPEND_DESCRIPTOR(descr_ep_out);
293#undef _APPEND_DESCRIPTOR
294 dev->descriptors->configuration->extra_count += descr_count;
295
296 /*
297 * Final changes.
298 * Increase the necessary counters, make endpoint mapping.
299 *
300 */
301 if (iface->in_data_size > 0) {
302 hid_data->in_endpoints_mapping[hid_data->in_endpoint_first_free] =
303 iface;
304 dev->ops->data_in[hid_data->in_endpoint_first_free] =
305 on_data_from_device;
306 hid_data->in_endpoint_first_free++;
307 }
308 if (iface->out_data_size > 0) {
309 hid_data->out_endpoints_mapping[hid_data->out_endpoint_first_free] =
310 iface;
311 dev->ops->data_out[hid_data->out_endpoint_first_free] =
312 on_data_to_device;
313 hid_data->out_endpoint_first_free++;
314 }
315
316 hid_data->interface_mapping[dev->descriptors->configuration->descriptor->interface_count] =
317 iface;
318
319 dev->descriptors->configuration->descriptor->interface_count++;
320 dev->descriptors->configuration->descriptor->total_length +=
321 total_descr_size;
322
323 hid_data->iface_count++;
324 fibril_add_ready(life_fibril);
325
326 return EOK;
327
328error_leave:
329 if (descr_iface != NULL) {
330 free(descr_iface);
331 }
332 if (descr_hid != NULL) {
333 free(descr_hid);
334 }
335 if (descr_ep_in != NULL) {
336 free(descr_ep_in);
337 }
338 if (descr_ep_out != NULL) {
339 free(descr_ep_out);
340 }
341
342 return rc;
343}
344
345void wait_for_interfaces_death(usbvirt_device_t *dev)
346{
347 vuhid_data_t *hid_data = dev->device_data;
348
349 fibril_mutex_lock(&hid_data->iface_count_mutex);
350 while (hid_data->iface_died_count < hid_data->iface_count) {
351 fibril_condvar_wait(&hid_data->iface_count_cv,
352 &hid_data->iface_count_mutex);
353 }
354 fibril_mutex_unlock(&hid_data->iface_count_mutex);
355}
356
357/** @}
358 */
Note: See TracBrowser for help on using the repository browser.