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

Last change on this file since a78b0a0 was 09ab0a9a, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Fix vertical spacing with new Ccheck revision.

  • Property mode set to 100644
File size: 5.4 KB
RevLine 
[252e30c]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
[160b75e]29/** @addtogroup libusbhid
[252e30c]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>
[faa44e58]42#include <usb/hid/hidparser.h>
[7d521e24]43#include <usb/dev/dp.h>
44#include <usb/dev/driver.h>
45#include <usb/dev/pipes.h>
[faa44e58]46#include <usb/hid/hid.h>
[252e30c]47#include <usb/descriptor.h>
[7d521e24]48#include <usb/dev/request.h>
[252e30c]49
[faa44e58]50#include <usb/hid/hidreport.h>
[252e30c]51
[5a6cc679]52static errno_t usb_hid_get_report_descriptor(usb_device_t *dev,
[252e30c]53 uint8_t **report_desc, size_t *size)
54{
55 assert(report_desc != NULL);
56 assert(size != NULL);
[a35b458]57
[252e30c]58 usb_dp_parser_t parser = {
59 .nesting = usb_dp_standard_descriptor_nesting
60 };
[a35b458]61
[252e30c]62 usb_dp_parser_data_t parser_data = {
[e2dfa86]63 .data = usb_device_descriptors(dev)->full_config,
64 .size = usb_device_descriptors(dev)->full_config_size,
[252e30c]65 .arg = NULL
66 };
[a35b458]67
[252e30c]68 /*
69 * First nested descriptor of the configuration descriptor.
70 */
[5b9d2a35]71 const uint8_t *d =
[e2dfa86]72 usb_dp_get_nested_descriptor(&parser, &parser_data,
[ae3a941]73 usb_device_descriptors(dev)->full_config);
[a35b458]74
[252e30c]75 /*
76 * Find the interface descriptor corresponding to our interface number.
77 */
78 int i = 0;
[8b68bdf]79 while (d != NULL && i < usb_device_get_iface_number(dev)) {
[5b9d2a35]80 d = usb_dp_get_sibling_descriptor(&parser, &parser_data,
[e2dfa86]81 usb_device_descriptors(dev)->full_config, d);
[15a9e63]82 ++i;
[252e30c]83 }
[a35b458]84
[252e30c]85 if (d == NULL) {
[a1732929]86 usb_log_error("The %d. interface descriptor not found!",
[8b68bdf]87 usb_device_get_iface_number(dev));
[252e30c]88 return ENOENT;
89 }
[a35b458]90
[252e30c]91 /*
92 * First nested descriptor of the interface descriptor.
93 */
[8a121b1]94 const uint8_t *iface_desc = d;
[252e30c]95 d = usb_dp_get_nested_descriptor(&parser, &parser_data, iface_desc);
[a35b458]96
[252e30c]97 /*
98 * Search through siblings until the HID descriptor is found.
99 */
100 while (d != NULL && *(d + 1) != USB_DESCTYPE_HID) {
[5b9d2a35]101 d = usb_dp_get_sibling_descriptor(&parser, &parser_data,
[252e30c]102 iface_desc, d);
103 }
[a35b458]104
[252e30c]105 if (d == NULL) {
[a1732929]106 usb_log_fatal("No HID descriptor found!");
[252e30c]107 return ENOENT;
108 }
[a35b458]109
[252e30c]110 if (*d != sizeof(usb_standard_hid_descriptor_t)) {
[4125b7d]111 usb_log_error("HID descriptor has wrong size (%u, expected %zu"
[252e30c]112 ")\n", *d, sizeof(usb_standard_hid_descriptor_t));
113 return EINVAL;
114 }
[a35b458]115
[5b9d2a35]116 usb_standard_hid_descriptor_t *hid_desc =
[252e30c]117 (usb_standard_hid_descriptor_t *)d;
[a35b458]118
[5b9d2a35]119 uint16_t length = uint16_usb2host(hid_desc->report_desc_info.length);
[252e30c]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 }
[a35b458]131
[a1732929]132 usb_log_debug("Getting Report descriptor, expected size: %u", length);
[a35b458]133
[252e30c]134 /*
135 * Get the descriptor from the device.
136 */
[5a6cc679]137 errno_t rc = usb_request_get_descriptor(usb_device_get_default_pipe(dev),
[252e30c]138 USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_INTERFACE,
[8b68bdf]139 USB_DESCTYPE_HID_REPORT, 0, usb_device_get_iface_number(dev),
[252e30c]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;
[4125b7d]151 usb_log_error("Report descriptor has wrong size (%zu, expected "
[252e30c]152 "%u)\n", actual_size, length);
153 return EINVAL;
154 }
[a35b458]155
[252e30c]156 *size = length;
[a35b458]157
[a1732929]158 usb_log_debug("Done.");
[a35b458]159
[252e30c]160 return EOK;
161}
162
[ae3a941]163errno_t usb_hid_process_report_descriptor(usb_device_t *dev,
[fa8d346]164 usb_hid_report_t *report, uint8_t **report_desc, size_t *report_size)
[252e30c]165{
[175ad13e]166 if (dev == NULL || report == NULL) {
[252e30c]167 usb_log_error("Failed to process Report descriptor: wrong "
168 "parameters given.\n");
169 return EINVAL;
170 }
[a35b458]171
[5a6cc679]172 errno_t rc = usb_hid_get_report_descriptor(dev, report_desc, report_size);
[a35b458]173
[252e30c]174 if (rc != EOK) {
[a1732929]175 usb_log_error("Problem with getting Report descriptor: %s.",
[252e30c]176 str_error(rc));
[fa8d346]177 if (*report_desc != NULL) {
178 free(*report_desc);
179 *report_desc = NULL;
[252e30c]180 }
181 return rc;
182 }
[a35b458]183
[fa8d346]184 assert(*report_desc != NULL);
[a35b458]185
[fa8d346]186 rc = usb_hid_parse_report_descriptor(report, *report_desc, *report_size);
[252e30c]187 if (rc != EOK) {
[a1732929]188 usb_log_error("Problem parsing Report descriptor: %s.",
[252e30c]189 str_error(rc));
[fa8d346]190 free(*report_desc);
191 *report_desc = NULL;
[252e30c]192 return rc;
193 }
[a35b458]194
[175ad13e]195 usb_hid_descriptor_print(report);
[a35b458]196
[252e30c]197 return EOK;
198}
199
200/**
201 * @}
202 */
Note: See TracBrowser for help on using the repository browser.