source: mainline/uspace/lib/usbhid/src/hidpath.c@ 79ae36dd

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 79ae36dd was dcb7d7cd, checked in by Matej Klonfar <maklf@…>, 14 years ago

subdrivers matching without specified report_id

  • Property mode set to 100644
File size: 11.7 KB
Line 
1/*
2 * Copyright (c) 2011 Matej Klonfar
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 libusbhid
30 * @{
31 */
32/** @file
33 * HID report descriptor and report data parser implementation.
34 */
35#include <usb/hid/hidparser.h>
36#include <errno.h>
37#include <stdio.h>
38#include <malloc.h>
39#include <mem.h>
40#include <usb/debug.h>
41#include <assert.h>
42
43/*---------------------------------------------------------------------------*/
44/**
45 * Compares two usages if they are same or not or one of the usages is not
46 * set.
47 *
48 * @param usage1
49 * @param usage2
50 * @return boolean
51 */
52#define USB_HID_SAME_USAGE(usage1, usage2) \
53 ((usage1 == usage2) || (usage1 == 0) || (usage2 == 0))
54
55/**
56 * Compares two usage pages if they are same or not or one of them is not set.
57 *
58 * @param page1
59 * @param page2
60 * @return boolean
61 */
62#define USB_HID_SAME_USAGE_PAGE(page1, page2) \
63 ((page1 == page2) || (page1 == 0) || (page2 == 0))
64
65/*---------------------------------------------------------------------------*/
66/**
67 * Appends one item (couple of usage_path and usage) into the usage path
68 * structure
69 *
70 * @param usage_path Usage path structure
71 * @param usage_page Usage page constant
72 * @param usage Usage constant
73 * @return Error code
74 */
75int usb_hid_report_path_append_item(usb_hid_report_path_t *usage_path,
76 int32_t usage_page, int32_t usage)
77{
78 usb_hid_report_usage_path_t *item;
79
80 if(!(item=malloc(sizeof(usb_hid_report_usage_path_t)))) {
81 return ENOMEM;
82 }
83 list_initialize(&item->link);
84
85 item->usage = usage;
86 item->usage_page = usage_page;
87 item->flags = 0;
88
89 list_append (&item->link, &usage_path->head);
90 usage_path->depth++;
91 return EOK;
92}
93
94/*---------------------------------------------------------------------------*/
95/**
96 * Removes last item from the usage path structure
97 * @param usage_path
98 * @return void
99 */
100void usb_hid_report_remove_last_item(usb_hid_report_path_t *usage_path)
101{
102 usb_hid_report_usage_path_t *item;
103
104 if(!list_empty(&usage_path->head)){
105 item = list_get_instance(usage_path->head.prev,
106 usb_hid_report_usage_path_t, link);
107 list_remove(usage_path->head.prev);
108 usage_path->depth--;
109 free(item);
110 }
111}
112
113/*---------------------------------------------------------------------------*/
114/**
115 * Nulls last item of the usage path structure.
116 *
117 * @param usage_path
118 * @return void
119 */
120void usb_hid_report_null_last_item(usb_hid_report_path_t *usage_path)
121{
122 usb_hid_report_usage_path_t *item;
123
124 if(!list_empty(&usage_path->head)){
125 item = list_get_instance(usage_path->head.prev,
126 usb_hid_report_usage_path_t, link);
127
128 memset(item, 0, sizeof(usb_hid_report_usage_path_t));
129 }
130}
131
132/*---------------------------------------------------------------------------*/
133/**
134 * Modifies last item of usage path structure by given usage page or usage
135 *
136 * @param usage_path Opaque usage path structure
137 * @param tag Class of currently processed tag (Usage page tag falls into Global
138 * class but Usage tag into the Local)
139 * @param data Value of the processed tag
140 * @return void
141 */
142void usb_hid_report_set_last_item(usb_hid_report_path_t *usage_path,
143 int32_t tag, int32_t data)
144{
145 usb_hid_report_usage_path_t *item;
146
147 if(!list_empty(&usage_path->head)){
148 item = list_get_instance(usage_path->head.prev,
149 usb_hid_report_usage_path_t, link);
150
151 switch(tag) {
152 case USB_HID_TAG_CLASS_GLOBAL:
153 item->usage_page = data;
154 break;
155 case USB_HID_TAG_CLASS_LOCAL:
156 item->usage = data;
157 break;
158 }
159 }
160
161}
162
163/*---------------------------------------------------------------------------*/
164/**
165 *
166 *
167 *
168 *
169 */
170void usb_hid_print_usage_path(usb_hid_report_path_t *path)
171{
172 usb_log_debug("USAGE_PATH FOR RId(%d):\n", path->report_id);
173 usb_log_debug("\tLENGTH: %d\n", path->depth);
174
175 link_t *item = path->head.next;
176 usb_hid_report_usage_path_t *path_item;
177 while(item != &path->head) {
178
179 path_item = list_get_instance(item, usb_hid_report_usage_path_t,
180 link);
181
182 usb_log_debug("\tUSAGE_PAGE: %X\n", path_item->usage_page);
183 usb_log_debug("\tUSAGE: %X\n", path_item->usage);
184 usb_log_debug("\tFLAGS: %d\n", path_item->flags);
185
186 item = item->next;
187 }
188}
189
190/*---------------------------------------------------------------------------*/
191/**
192 * Compares two usage paths structures
193 *
194 *
195 * @param report_path usage path structure to compare with @path
196 * @param path usage patrh structure to compare
197 * @param flags Flags determining the mode of comparison
198 * @return EOK if both paths are identical, non zero number otherwise
199 */
200int usb_hid_report_compare_usage_path(usb_hid_report_path_t *report_path,
201 usb_hid_report_path_t *path,
202 int flags)
203{
204 usb_hid_report_usage_path_t *report_item;
205 usb_hid_report_usage_path_t *path_item;
206
207 link_t *report_link;
208 link_t *path_link;
209
210 int only_page;
211
212 if(report_path->report_id != path->report_id) {
213 if(path->report_id != 0) {
214 return 1;
215 }
216 }
217
218 // Empty path match all others
219 if(path->depth == 0){
220 return EOK;
221 }
222
223
224 if((only_page = flags & USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY) != 0){
225 flags -= USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY;
226 }
227
228 switch(flags){
229 /* path is somewhere in report_path */
230 case USB_HID_PATH_COMPARE_ANYWHERE:
231 if(path->depth != 1){
232 return 1;
233 }
234
235 report_link = report_path->head.next;
236 path_link = path->head.next;
237 path_item = list_get_instance(path_link,
238 usb_hid_report_usage_path_t, link);
239
240 while(report_link != &report_path->head) {
241 report_item = list_get_instance(report_link,
242 usb_hid_report_usage_path_t, link);
243
244 if(USB_HID_SAME_USAGE_PAGE(report_item->usage_page,
245 path_item->usage_page)){
246
247 if(only_page == 0){
248 if(USB_HID_SAME_USAGE(
249 report_item->usage,
250 path_item->usage)) {
251
252 return EOK;
253 }
254 }
255 else {
256 return EOK;
257 }
258 }
259
260 report_link = report_link->next;
261 }
262
263 return 1;
264 break;
265
266 /* the paths must be identical */
267 case USB_HID_PATH_COMPARE_STRICT:
268 if(report_path->depth != path->depth){
269 return 1;
270 }
271
272 /* path is prefix of the report_path */
273 case USB_HID_PATH_COMPARE_BEGIN:
274
275 report_link = report_path->head.next;
276 path_link = path->head.next;
277
278 while((report_link != &report_path->head) &&
279 (path_link != &path->head)) {
280
281 report_item = list_get_instance(report_link,
282 usb_hid_report_usage_path_t, link);
283
284 path_item = list_get_instance(path_link,
285 usb_hid_report_usage_path_t, link);
286
287 if(!USB_HID_SAME_USAGE_PAGE(report_item->usage_page,
288 path_item->usage_page) || ((only_page == 0) &&
289 !USB_HID_SAME_USAGE(report_item->usage,
290 path_item->usage))) {
291
292 return 1;
293 }
294 else {
295 report_link = report_link->next;
296 path_link = path_link->next;
297 }
298
299 }
300
301 if((((flags & USB_HID_PATH_COMPARE_BEGIN) != 0) &&
302 (path_link == &path->head)) ||
303 ((report_link == &report_path->head) &&
304 (path_link == &path->head))) {
305
306 return EOK;
307 }
308 else {
309 return 1;
310 }
311 break;
312
313 /* path is suffix of report_path */
314 case USB_HID_PATH_COMPARE_END:
315
316 report_link = report_path->head.prev;
317 path_link = path->head.prev;
318
319 if(list_empty(&path->head)){
320 return EOK;
321 }
322
323 while((report_link != &report_path->head) &&
324 (path_link != &path->head)) {
325
326 report_item = list_get_instance(report_link,
327 usb_hid_report_usage_path_t, link);
328
329 path_item = list_get_instance(path_link,
330 usb_hid_report_usage_path_t, link);
331
332 if(!USB_HID_SAME_USAGE_PAGE(report_item->usage_page,
333 path_item->usage_page) || ((only_page == 0) &&
334 !USB_HID_SAME_USAGE(report_item->usage,
335 path_item->usage))) {
336
337 return 1;
338 } else {
339 report_link = report_link->prev;
340 path_link = path_link->prev;
341 }
342
343 }
344
345 if(path_link == &path->head) {
346 return EOK;
347 }
348 else {
349 return 1;
350 }
351
352 break;
353
354 default:
355 return EINVAL;
356 }
357}
358
359/*---------------------------------------------------------------------------*/
360/**
361 * Allocates and initializes new usage path structure.
362 *
363 * @return Initialized usage path structure
364 */
365usb_hid_report_path_t *usb_hid_report_path(void)
366{
367 usb_hid_report_path_t *path;
368 path = malloc(sizeof(usb_hid_report_path_t));
369 if(path == NULL){
370 return NULL;
371 }
372 else {
373 path->depth = 0;
374 path->report_id = 0;
375 list_initialize(&path->link);
376 list_initialize(&path->head);
377 return path;
378 }
379}
380
381/*---------------------------------------------------------------------------*/
382/**
383 * Releases given usage path structure.
384 *
385 * @param path usage path structure to release
386 * @return void
387 */
388void usb_hid_report_path_free(usb_hid_report_path_t *path)
389{
390 while(!list_empty(&path->head)){
391 usb_hid_report_remove_last_item(path);
392 }
393
394 list_remove(&path->link);
395 free(path);
396}
397
398/*---------------------------------------------------------------------------*/
399/**
400 * Clone content of given usage path to the new one
401 *
402 * @param usage_path Usage path structure to clone
403 * @return New copy of given usage path structure
404 */
405usb_hid_report_path_t *usb_hid_report_path_clone(
406 usb_hid_report_path_t *usage_path)
407{
408 link_t *path_link;
409 usb_hid_report_usage_path_t *path_item;
410 usb_hid_report_usage_path_t *new_path_item;
411 usb_hid_report_path_t *new_usage_path = usb_hid_report_path ();
412
413 if(new_usage_path == NULL){
414 return NULL;
415 }
416
417 new_usage_path->report_id = usage_path->report_id;
418
419 if(list_empty(&usage_path->head)){
420 return new_usage_path;
421 }
422
423 path_link = usage_path->head.next;
424 while(path_link != &usage_path->head) {
425 path_item = list_get_instance(path_link,
426 usb_hid_report_usage_path_t, link);
427
428 new_path_item = malloc(sizeof(usb_hid_report_usage_path_t));
429 if(new_path_item == NULL) {
430 return NULL;
431 }
432
433 list_initialize (&new_path_item->link);
434 new_path_item->usage_page = path_item->usage_page;
435 new_path_item->usage = path_item->usage;
436 new_path_item->flags = path_item->flags;
437
438 list_append(&new_path_item->link, &new_usage_path->head);
439 new_usage_path->depth++;
440
441 path_link = path_link->next;
442 }
443
444 return new_usage_path;
445}
446
447/*---------------------------------------------------------------------------*/
448/**
449 * Sets report id in usage path structure
450 *
451 * @param path Usage path structure
452 * @param report_id Report id to set
453 * @return Error code
454 */
455int usb_hid_report_path_set_report_id(usb_hid_report_path_t *path,
456 uint8_t report_id)
457{
458 if(path == NULL){
459 return EINVAL;
460 }
461
462 path->report_id = report_id;
463 return EOK;
464}
465
466/**
467 * @}
468 */
Note: See TracBrowser for help on using the repository browser.