source: mainline/uspace/app/vuhid/device.c@ cc5908e

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since cc5908e was 9a884ed, checked in by Vojtech Horky <vojtechhorky@…>, 14 years ago

Virtual HID terminates with last interface

Also it is not possible to turn on the same interface twice.

  • Property mode set to 100644
File size: 10.3 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 <str.h>
39#include <assert.h>
40#include <usb/classes/classes.h>
41
42static int on_data_from_device(usbvirt_device_t *dev,
43 usb_endpoint_t ep, usb_transfer_type_t tr_type,
44 void *data, size_t data_size, size_t *actual_size)
45{
46 vuhid_data_t *vuhid = dev->device_data;
47 vuhid_interface_t *iface = vuhid->in_endpoints_mapping[ep];
48 if (iface == NULL) {
49 return ESTALL;
50 }
51
52 if (iface->on_data_in == NULL) {
53 return EBADCHECKSUM;
54 }
55
56 return iface->on_data_in(iface, data, data_size, actual_size);
57}
58
59static int on_data_to_device(usbvirt_device_t *dev,
60 usb_endpoint_t ep, usb_transfer_type_t tr_type,
61 void *data, size_t data_size)
62{
63 vuhid_data_t *vuhid = dev->device_data;
64 vuhid_interface_t *iface = vuhid->out_endpoints_mapping[ep];
65 if (iface == NULL) {
66 return ESTALL;
67 }
68
69 if (iface->on_data_out == NULL) {
70 return EBADCHECKSUM;
71 }
72
73 return iface->on_data_out(iface, data, data_size);
74}
75
76static int interface_life_fibril(void *arg)
77{
78 vuhid_interface_t *iface = arg;
79 vuhid_data_t *hid_data = iface->vuhid_data;
80
81 if (iface->live != NULL) {
82 iface->live(iface);
83 }
84
85 fibril_mutex_lock(&hid_data->iface_count_mutex);
86 hid_data->iface_died_count++;
87 fibril_condvar_broadcast(&hid_data->iface_count_cv);
88 fibril_mutex_unlock(&hid_data->iface_count_mutex);
89
90 return EOK;
91}
92
93
94static vuhid_interface_t *find_interface_by_id(vuhid_interface_t **ifaces,
95 const char *id)
96{
97 vuhid_interface_t *iface = ifaces[0];
98 while (iface != NULL) {
99 if (str_cmp(iface->id, id) == 0) {
100 return iface;
101 }
102 iface++;
103 }
104
105 return iface;
106}
107
108int add_interface_by_id(vuhid_interface_t **interfaces, const char *id,
109 usbvirt_device_t *dev)
110{
111 vuhid_interface_t *iface = find_interface_by_id(interfaces, id);
112 if (iface == NULL) {
113 return ENOENT;
114 }
115
116 if ((iface->in_data_size == 0) && (iface->out_data_size == 0)) {
117 return EEMPTY;
118 }
119
120 // FIXME - we shall set vuhid_data to NULL in the main() rather
121 // than to depend on individual interfaces
122 /* Already used interface. */
123 if (iface->vuhid_data != NULL) {
124 return EEXISTS;
125 }
126
127 vuhid_data_t *hid_data = dev->device_data;
128
129 /* Check that we have not run out of available endpoints. */
130 if ((iface->in_data_size > 0)
131 && (hid_data->in_endpoint_first_free >= VUHID_ENDPOINT_MAX)) {
132 return ELIMIT;
133 }
134 if ((iface->out_data_size > 0)
135 && (hid_data->out_endpoint_first_free >= VUHID_ENDPOINT_MAX)) {
136 return ELIMIT;
137 }
138
139 if (dev->descriptors->configuration->descriptor->interface_count >=
140 VUHID_INTERFACE_MAX) {
141 return ELIMIT;
142 }
143
144 /*
145 * How may descriptors we would add?
146 */
147 /* Start with interface descriptor. */
148 size_t new_descriptor_count = 1;
149 /* Having in/out data size positive means we would need endpoint. */
150 if (iface->in_data_size > 0) {
151 new_descriptor_count++;
152 }
153 if (iface->out_data_size > 0) {
154 new_descriptor_count++;
155 }
156 /* Having report descriptor means adding the HID descriptor. */
157 if (iface->report_descriptor != NULL) {
158 new_descriptor_count++;
159 }
160
161 /* From now on, in case of errors, we goto to error_leave */
162 int rc;
163
164 /*
165 * Prepare new descriptors.
166 */
167 printf("preparing descriptors...\n");
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 = dev->descriptors->configuration->extra;
258 extra_descriptors = realloc(extra_descriptors,
259 sizeof(usbvirt_device_configuration_extras_t)
260 * (dev->descriptors->configuration->extra_count + descr_count));
261 if (extra_descriptors == NULL) {
262 rc = ENOMEM;
263 goto error_leave;
264 }
265
266 /* Launch the "life" fibril. */
267 iface->vuhid_data = hid_data;
268 fid_t life_fibril = fibril_create(interface_life_fibril, iface);
269 if (life_fibril == 0) {
270 rc = ENOMEM;
271 goto error_leave;
272 }
273
274 printf("adding extra descriptors...\n");
275 /* Allocation is okay, we can (actually have to now) overwrite the
276 * original pointer.
277 */
278 dev->descriptors->configuration->extra = extra_descriptors;
279 extra_descriptors += dev->descriptors->configuration->extra_count;
280
281 /* Initialize them. */
282#define _APPEND_DESCRIPTOR(descriptor) \
283 do { \
284 if ((descriptor) != NULL) { \
285 extra_descriptors->data = (uint8_t *) (descriptor); \
286 extra_descriptors->length = (descriptor)->length; \
287 extra_descriptors++; \
288 } \
289 } while (0)
290
291 _APPEND_DESCRIPTOR(descr_iface);
292 _APPEND_DESCRIPTOR(descr_hid);
293 _APPEND_DESCRIPTOR(descr_ep_in);
294 _APPEND_DESCRIPTOR(descr_ep_out);
295#undef _APPEND_DESCRIPTOR
296 dev->descriptors->configuration->extra_count += descr_count;
297
298 /*
299 * Final changes.
300 * Increase the necessary counters, make endpoint mapping.
301 *
302 */
303 if (iface->in_data_size > 0) {
304 hid_data->in_endpoints_mapping[hid_data->in_endpoint_first_free]
305 = iface;
306 dev->ops->data_in[hid_data->in_endpoint_first_free]
307 = on_data_from_device;
308 hid_data->in_endpoint_first_free++;
309 }
310 if (iface->out_data_size > 0) {
311 hid_data->out_endpoints_mapping[hid_data->out_endpoint_first_free]
312 = iface;
313 dev->ops->data_out[hid_data->out_endpoint_first_free]
314 = on_data_to_device;
315 hid_data->out_endpoint_first_free++;
316 }
317
318 hid_data->interface_mapping[
319 dev->descriptors->configuration->descriptor->interface_count]
320 = iface;
321
322 dev->descriptors->configuration->descriptor->interface_count++;
323 dev->descriptors->configuration->descriptor->total_length
324 += total_descr_size;
325
326 hid_data->iface_count++;
327 fibril_add_ready(life_fibril);
328
329 return EOK;
330
331error_leave:
332 if (descr_iface != NULL) {
333 free(descr_iface);
334 }
335 if (descr_hid != NULL) {
336 free(descr_hid);
337 }
338 if (descr_ep_in != NULL) {
339 free(descr_ep_in);
340 }
341 if (descr_ep_out != NULL) {
342 free(descr_ep_out);
343 }
344
345 return rc;
346}
347
348void wait_for_interfaces_death(usbvirt_device_t *dev)
349{
350 vuhid_data_t *hid_data = dev->device_data;
351
352 fibril_mutex_lock(&hid_data->iface_count_mutex);
353 while (hid_data->iface_died_count < hid_data->iface_count) {
354 fibril_condvar_wait(&hid_data->iface_count_cv,
355 &hid_data->iface_count_mutex);
356 }
357 fibril_mutex_unlock(&hid_data->iface_count_mutex);
358}
359
360/** @}
361 */
Note: See TracBrowser for help on using the repository browser.