source: mainline/uspace/lib/usbhid/src/hidpath.c@ 41811af

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

subdrivers matching without specified report_id

  • Property mode set to 100644
File size: 11.7 KB
RevLine 
[9d05599]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
[160b75e]29/** @addtogroup libusbhid
[9d05599]30 * @{
31 */
32/** @file
33 * HID report descriptor and report data parser implementation.
34 */
[faa44e58]35#include <usb/hid/hidparser.h>
[9d05599]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
[5499a8b]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))
[9d05599]54
[5499a8b]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))
[3a6e423]64
[5499a8b]65/*---------------------------------------------------------------------------*/
[9d05599]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
[5499a8b]94/*---------------------------------------------------------------------------*/
[9d05599]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
[5499a8b]113/*---------------------------------------------------------------------------*/
[9d05599]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)){
[5499a8b]125 item = list_get_instance(usage_path->head.prev,
126 usb_hid_report_usage_path_t, link);
127
[9d05599]128 memset(item, 0, sizeof(usb_hid_report_usage_path_t));
129 }
130}
131
[5499a8b]132/*---------------------------------------------------------------------------*/
[9d05599]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
[5499a8b]163/*---------------------------------------------------------------------------*/
164/**
165 *
166 *
167 *
168 *
169 */
[9d05599]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
[5499a8b]179 path_item = list_get_instance(item, usb_hid_report_usage_path_t,
180 link);
181
[9d05599]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
[5499a8b]186 item = item->next;
[9d05599]187 }
188}
189
[5499a8b]190/*---------------------------------------------------------------------------*/
[9d05599]191/**
192 * Compares two usage paths structures
193 *
194 *
[5f7b75a]195 * @param report_path usage path structure to compare with @path
[9d05599]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) {
[dcb7d7cd]213 if(path->report_id != 0) {
214 return 1;
215 }
[9d05599]216 }
217
[5f7b75a]218 // Empty path match all others
[9d05599]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){
[f3b39b4]229 /* path is somewhere in report_path */
230 case USB_HID_PATH_COMPARE_ANYWHERE:
231 if(path->depth != 1){
232 return 1;
233 }
[5f7b75a]234
[f3b39b4]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);
[5f7b75a]239
[f3b39b4]240 while(report_link != &report_path->head) {
241 report_item = list_get_instance(report_link,
242 usb_hid_report_usage_path_t, link);
[5499a8b]243
[f3b39b4]244 if(USB_HID_SAME_USAGE_PAGE(report_item->usage_page,
245 path_item->usage_page)){
[5499a8b]246
[f3b39b4]247 if(only_page == 0){
248 if(USB_HID_SAME_USAGE(
249 report_item->usage,
250 path_item->usage)) {
[5499a8b]251
[5f7b75a]252 return EOK;
253 }
254 }
[f3b39b4]255 else {
256 return EOK;
257 }
[5f7b75a]258 }
259
[f3b39b4]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){
[5f7b75a]269 return 1;
[f3b39b4]270 }
[5f7b75a]271
[f3b39b4]272 /* path is prefix of the report_path */
273 case USB_HID_PATH_COMPARE_BEGIN:
[5f7b75a]274
[f3b39b4]275 report_link = report_path->head.next;
276 path_link = path->head.next;
[9d05599]277
[f3b39b4]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 }
[9d05599]298
299 }
300
[f3b39b4]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;
[9d05599]312
[f3b39b4]313 /* path is suffix of report_path */
314 case USB_HID_PATH_COMPARE_END:
[9d05599]315
[f3b39b4]316 report_link = report_path->head.prev;
317 path_link = path->head.prev;
[9d05599]318
[f3b39b4]319 if(list_empty(&path->head)){
320 return EOK;
321 }
[9d05599]322
[f3b39b4]323 while((report_link != &report_path->head) &&
324 (path_link != &path->head)) {
[9d05599]325
[f3b39b4]326 report_item = list_get_instance(report_link,
327 usb_hid_report_usage_path_t, link);
[5499a8b]328
[f3b39b4]329 path_item = list_get_instance(path_link,
330 usb_hid_report_usage_path_t, link);
[3a6e423]331
[f3b39b4]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))) {
[5499a8b]336
[9d05599]337 return 1;
[f3b39b4]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 }
[9d05599]351
[f3b39b4]352 break;
[9d05599]353
[f3b39b4]354 default:
355 return EINVAL;
[9d05599]356 }
357}
358
[5499a8b]359/*---------------------------------------------------------------------------*/
[9d05599]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
[5499a8b]381/*---------------------------------------------------------------------------*/
[9d05599]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
[5499a8b]398/*---------------------------------------------------------------------------*/
[9d05599]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 */
[5499a8b]405usb_hid_report_path_t *usb_hid_report_path_clone(
406 usb_hid_report_path_t *usage_path)
[9d05599]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) {
[f3b39b4]425 path_item = list_get_instance(path_link,
426 usb_hid_report_usage_path_t, link);
427
[9d05599]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
[5499a8b]447/*---------------------------------------------------------------------------*/
[9d05599]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 */
[5499a8b]455int usb_hid_report_path_set_report_id(usb_hid_report_path_t *path,
456 uint8_t report_id)
[9d05599]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.