source: mainline/uspace/app/vuhid/device.c@ 179f6f2

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

Fix bad pointer iteration

  • 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 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
60static 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
77static 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
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
111int 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 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 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 /* Allocation is okay, we can (actually have to now) overwrite the
275 * original pointer.
276 */
277 dev->descriptors->configuration->extra = extra_descriptors;
278 extra_descriptors += dev->descriptors->configuration->extra_count;
279
280 /* Initialize them. */
281#define _APPEND_DESCRIPTOR(descriptor) \
282 do { \
283 if ((descriptor) != NULL) { \
284 extra_descriptors->data = (uint8_t *) (descriptor); \
285 extra_descriptors->length = (descriptor)->length; \
286 extra_descriptors++; \
287 } \
288 } while (0)
289
290 _APPEND_DESCRIPTOR(descr_iface);
291 _APPEND_DESCRIPTOR(descr_hid);
292 _APPEND_DESCRIPTOR(descr_ep_in);
293 _APPEND_DESCRIPTOR(descr_ep_out);
294#undef _APPEND_DESCRIPTOR
295 dev->descriptors->configuration->extra_count += descr_count;
296
297 /*
298 * Final changes.
299 * Increase the necessary counters, make endpoint mapping.
300 *
301 */
302 if (iface->in_data_size > 0) {
303 hid_data->in_endpoints_mapping[hid_data->in_endpoint_first_free]
304 = iface;
305 dev->ops->data_in[hid_data->in_endpoint_first_free]
306 = on_data_from_device;
307 hid_data->in_endpoint_first_free++;
308 }
309 if (iface->out_data_size > 0) {
310 hid_data->out_endpoints_mapping[hid_data->out_endpoint_first_free]
311 = iface;
312 dev->ops->data_out[hid_data->out_endpoint_first_free]
313 = on_data_to_device;
314 hid_data->out_endpoint_first_free++;
315 }
316
317 hid_data->interface_mapping[
318 dev->descriptors->configuration->descriptor->interface_count]
319 = iface;
320
321 dev->descriptors->configuration->descriptor->interface_count++;
322 dev->descriptors->configuration->descriptor->total_length
323 += total_descr_size;
324
325 hid_data->iface_count++;
326 fibril_add_ready(life_fibril);
327
328 return EOK;
329
330error_leave:
331 if (descr_iface != NULL) {
332 free(descr_iface);
333 }
334 if (descr_hid != NULL) {
335 free(descr_hid);
336 }
337 if (descr_ep_in != NULL) {
338 free(descr_ep_in);
339 }
340 if (descr_ep_out != NULL) {
341 free(descr_ep_out);
342 }
343
344 return rc;
345}
346
347void wait_for_interfaces_death(usbvirt_device_t *dev)
348{
349 vuhid_data_t *hid_data = dev->device_data;
350
351 fibril_mutex_lock(&hid_data->iface_count_mutex);
352 while (hid_data->iface_died_count < hid_data->iface_count) {
353 fibril_condvar_wait(&hid_data->iface_count_cv,
354 &hid_data->iface_count_mutex);
355 }
356 fibril_mutex_unlock(&hid_data->iface_count_mutex);
357}
358
359/** @}
360 */
Note: See TracBrowser for help on using the repository browser.