source: mainline/uspace/lib/usbhid/src/hidreport.c@ 7d521e24

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

libusbdev uses include usb/dev

  • Property mode set to 100644
File size: 5.4 KB
Line 
1/*
2 * Copyright (c) 2011 Lubos Slovak
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 libusb
30 * @{
31 */
32/**
33 * @file
34 * USB HID keyboard device structure and API.
35 */
36
37#include <assert.h>
38#include <errno.h>
39#include <str_error.h>
40
41#include <usb/debug.h>
42#include <usb/classes/hidparser.h>
43#include <usb/dev/dp.h>
44#include <usb/dev/driver.h>
45#include <usb/dev/pipes.h>
46#include <usb/classes/hid.h>
47#include <usb/descriptor.h>
48#include <usb/dev/request.h>
49
50#include <usb/classes/hidreport.h>
51
52static int usb_hid_get_report_descriptor(usb_device_t *dev,
53 uint8_t **report_desc, size_t *size)
54{
55 assert(report_desc != NULL);
56 assert(size != NULL);
57
58 usb_dp_parser_t parser = {
59 .nesting = usb_dp_standard_descriptor_nesting
60 };
61
62 usb_dp_parser_data_t parser_data = {
63 .data = dev->descriptors.configuration,
64 .size = dev->descriptors.configuration_size,
65 .arg = NULL
66 };
67
68 /*
69 * First nested descriptor of the configuration descriptor.
70 */
71 uint8_t *d =
72 usb_dp_get_nested_descriptor(&parser, &parser_data,
73 dev->descriptors.configuration);
74
75 /*
76 * Find the interface descriptor corresponding to our interface number.
77 */
78 int i = 0;
79 while (d != NULL && i < dev->interface_no) {
80 d = usb_dp_get_sibling_descriptor(&parser, &parser_data,
81 dev->descriptors.configuration, d);
82 ++i;
83 }
84
85 if (d == NULL) {
86 usb_log_error("The %d. interface descriptor not found!\n",
87 dev->interface_no);
88 return ENOENT;
89 }
90
91 /*
92 * First nested descriptor of the interface descriptor.
93 */
94 uint8_t *iface_desc = d;
95 d = usb_dp_get_nested_descriptor(&parser, &parser_data, iface_desc);
96
97 /*
98 * Search through siblings until the HID descriptor is found.
99 */
100 while (d != NULL && *(d + 1) != USB_DESCTYPE_HID) {
101 d = usb_dp_get_sibling_descriptor(&parser, &parser_data,
102 iface_desc, d);
103 }
104
105 if (d == NULL) {
106 usb_log_fatal("No HID descriptor found!\n");
107 return ENOENT;
108 }
109
110 if (*d != sizeof(usb_standard_hid_descriptor_t)) {
111 usb_log_error("HID descriptor has wrong size (%u, expected %zu"
112 ")\n", *d, sizeof(usb_standard_hid_descriptor_t));
113 return EINVAL;
114 }
115
116 usb_standard_hid_descriptor_t *hid_desc =
117 (usb_standard_hid_descriptor_t *)d;
118
119 uint16_t length = hid_desc->report_desc_info.length;
120 size_t actual_size = 0;
121
122 /*
123 * Allocate space for the report descriptor.
124 */
125 *report_desc = (uint8_t *)malloc(length);
126 if (*report_desc == NULL) {
127 usb_log_error("Failed to allocate space for Report descriptor."
128 "\n");
129 return ENOMEM;
130 }
131
132 usb_log_debug("Getting Report descriptor, expected size: %u\n", length);
133
134 /*
135 * Get the descriptor from the device.
136 */
137 int rc = usb_request_get_descriptor(&dev->ctrl_pipe,
138 USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_INTERFACE,
139 USB_DESCTYPE_HID_REPORT, 0, dev->interface_no,
140 *report_desc, length, &actual_size);
141
142 if (rc != EOK) {
143 free(*report_desc);
144 *report_desc = NULL;
145 return rc;
146 }
147
148 if (actual_size != length) {
149 free(*report_desc);
150 *report_desc = NULL;
151 usb_log_error("Report descriptor has wrong size (%zu, expected "
152 "%u)\n", actual_size, length);
153 return EINVAL;
154 }
155
156 *size = length;
157
158 usb_log_debug("Done.\n");
159
160 return EOK;
161}
162
163/*----------------------------------------------------------------------------*/
164
165int usb_hid_process_report_descriptor(usb_device_t *dev,
166 usb_hid_report_t *report)
167{
168 if (dev == NULL || report == NULL) {
169 usb_log_error("Failed to process Report descriptor: wrong "
170 "parameters given.\n");
171 return EINVAL;
172 }
173
174 uint8_t *report_desc = NULL;
175 size_t report_size;
176
177 int rc = usb_hid_get_report_descriptor(dev, &report_desc,
178 &report_size);
179
180 if (rc != EOK) {
181 usb_log_error("Problem with getting Report descriptor: %s.\n",
182 str_error(rc));
183 if (report_desc != NULL) {
184 free(report_desc);
185 }
186 return rc;
187 }
188
189 assert(report_desc != NULL);
190
191 rc = usb_hid_parse_report_descriptor(report, report_desc, report_size);
192 if (rc != EOK) {
193 usb_log_error("Problem parsing Report descriptor: %s.\n",
194 str_error(rc));
195 free(report_desc);
196 return rc;
197 }
198
199 usb_hid_descriptor_print(report);
200 free(report_desc);
201
202 return EOK;
203}
204
205/**
206 * @}
207 */
Note: See TracBrowser for help on using the repository browser.