source: mainline/uspace/lib/usbhid/src/hidreq.c

Last change on this file 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: 9.8 KB
RevLine 
[2391aaf]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 drvusbhid
30 * @{
31 */
32/** @file
33 * HID class-specific requests.
34 */
35
36#include <stdint.h>
37#include <errno.h>
38#include <str_error.h>
39
[faa44e58]40#include <usb/hid/hid.h>
[2391aaf]41#include <usb/debug.h>
[7d521e24]42#include <usb/dev/request.h>
43#include <usb/dev/pipes.h>
[2391aaf]44
[faa44e58]45#include <usb/hid/request.h>
[2391aaf]46
[2b12f06]47/**
48 * Send Set Report request to the HID device.
49 *
50 * @param hid_dev HID device to send the request to.
51 * @param type Type of the report.
52 * @param buffer Report data.
53 * @param buf_size Report data size (in bytes).
54 *
55 * @retval EOK if successful.
56 * @retval EINVAL if no HID device is given.
[8fd4ba0]57 * @return Other value inherited from function usb_control_request_set().
[2b12f06]58 */
[5a6cc679]59errno_t usbhid_req_set_report(usb_pipe_t *ctrl_pipe, int iface_no,
[2391aaf]60 usb_hid_report_type_t type, uint8_t *buffer, size_t buf_size)
61{
[f8e4cb6]62 if (ctrl_pipe == NULL) {
[a1732929]63 usb_log_warning("usbhid_req_set_report(): no pipe given.");
[f8e4cb6]64 return EINVAL;
65 }
[ae3a941]66
[f8e4cb6]67 if (iface_no < 0) {
68 usb_log_warning("usbhid_req_set_report(): no interface given."
69 "\n");
[2391aaf]70 return EINVAL;
71 }
[ae3a941]72
[2391aaf]73 /*
74 * No need for checking other parameters, as they are checked in
75 * the called function (usb_control_request_set()).
76 */
[ae3a941]77
[5a6cc679]78 errno_t rc;
[ae3a941]79
[35f0899]80 uint16_t value = 0;
81 value |= (type << 8);
[2391aaf]82
[a1732929]83 usb_log_debug("Sending Set Report request to the device.");
[ae3a941]84
[2155955]85 rc = usb_control_request_set(ctrl_pipe,
86 USB_REQUEST_TYPE_CLASS, USB_REQUEST_RECIPIENT_INTERFACE,
[f8e4cb6]87 USB_HIDREQ_SET_REPORT, value, iface_no, buffer, buf_size);
[2391aaf]88
89 if (rc != EOK) {
[2155955]90 usb_log_error("Error sending Set Report request to the "
[0a7627b]91 "device: %s.\n", str_error(rc));
[2391aaf]92 return rc;
93 }
[ae3a941]94
[2391aaf]95 return EOK;
96}
97
[2b12f06]98/**
99 * Send Set Protocol request to the HID device.
100 *
101 * @param hid_dev HID device to send the request to.
102 * @param protocol Protocol to set.
103 *
104 * @retval EOK if successful.
105 * @retval EINVAL if no HID device is given.
[8fd4ba0]106 * @return Other value inherited from function usb_control_request_set().
[2b12f06]107 */
[5a6cc679]108errno_t usbhid_req_set_protocol(usb_pipe_t *ctrl_pipe, int iface_no,
[f8e4cb6]109 usb_hid_protocol_t protocol)
[2391aaf]110{
[f8e4cb6]111 if (ctrl_pipe == NULL) {
[a1732929]112 usb_log_warning("usbhid_req_set_report(): no pipe given.");
[f8e4cb6]113 return EINVAL;
114 }
[ae3a941]115
[f8e4cb6]116 if (iface_no < 0) {
117 usb_log_warning("usbhid_req_set_report(): no interface given."
118 "\n");
[2391aaf]119 return EINVAL;
120 }
[ae3a941]121
[2391aaf]122 /*
123 * No need for checking other parameters, as they are checked in
124 * the called function (usb_control_request_set()).
125 */
[ae3a941]126
[5a6cc679]127 errno_t rc;
[2391aaf]128
[0a7627b]129 usb_log_debug("Sending Set Protocol request to the device ("
[f8e4cb6]130 "protocol: %d, iface: %d).\n", protocol, iface_no);
[ae3a941]131
132 rc = usb_control_request_set(ctrl_pipe,
133 USB_REQUEST_TYPE_CLASS, USB_REQUEST_RECIPIENT_INTERFACE,
[f8e4cb6]134 USB_HIDREQ_SET_PROTOCOL, protocol, iface_no, NULL, 0);
[2391aaf]135
136 if (rc != EOK) {
[0a7627b]137 usb_log_warning("Error sending Set Protocol request to the "
138 "device: %s.\n", str_error(rc));
[2391aaf]139 return rc;
140 }
[ae3a941]141
[2391aaf]142 return EOK;
143}
144
[2b12f06]145/**
146 * Send Set Idle request to the HID device.
147 *
148 * @param hid_dev HID device to send the request to.
149 * @param duration Duration value (is multiplicated by 4 by the device to
150 * get real duration in miliseconds).
151 *
152 * @retval EOK if successful.
153 * @retval EINVAL if no HID device is given.
[8fd4ba0]154 * @return Other value inherited from function usb_control_request_set().
[2b12f06]155 */
[5a6cc679]156errno_t usbhid_req_set_idle(usb_pipe_t *ctrl_pipe, int iface_no, uint8_t duration)
[dde8ca4]157{
[f8e4cb6]158 if (ctrl_pipe == NULL) {
[a1732929]159 usb_log_warning("usbhid_req_set_report(): no pipe given.");
[f8e4cb6]160 return EINVAL;
161 }
[ae3a941]162
[f8e4cb6]163 if (iface_no < 0) {
164 usb_log_warning("usbhid_req_set_report(): no interface given."
165 "\n");
[dde8ca4]166 return EINVAL;
167 }
[ae3a941]168
[dde8ca4]169 /*
170 * No need for checking other parameters, as they are checked in
171 * the called function (usb_control_request_set()).
172 */
[ae3a941]173
[5a6cc679]174 errno_t rc;
[dde8ca4]175
[0a7627b]176 usb_log_debug("Sending Set Idle request to the device ("
[f8e4cb6]177 "duration: %u, iface: %d).\n", duration, iface_no);
[ae3a941]178
[dde8ca4]179 uint16_t value = duration << 8;
[ae3a941]180
[4bfe063]181 rc = usb_control_request_set(ctrl_pipe,
182 USB_REQUEST_TYPE_CLASS, USB_REQUEST_RECIPIENT_INTERFACE,
[f8e4cb6]183 USB_HIDREQ_SET_IDLE, value, iface_no, NULL, 0);
[dde8ca4]184
185 if (rc != EOK) {
[4bfe063]186 usb_log_warning("Device did not accept Set Idle request: "
[dde8ca4]187 "%s.\n", str_error(rc));
188 return rc;
189 }
[ae3a941]190
[dde8ca4]191 return EOK;
192}
193
[2b12f06]194/**
195 * Send Get Report request to the HID device.
196 *
197 * @param[in] hid_dev HID device to send the request to.
198 * @param[in] type Type of the report.
199 * @param[in][out] buffer Buffer for the report data.
200 * @param[in] buf_size Size of the buffer (in bytes).
[ae3a941]201 * @param[out] actual_size Actual size of report received from the device
[2b12f06]202 * (in bytes).
203 *
204 * @retval EOK if successful.
205 * @retval EINVAL if no HID device is given.
[8fd4ba0]206 * @return Other value inherited from function usb_control_request_set().
[2b12f06]207 */
[ae3a941]208errno_t usbhid_req_get_report(usb_pipe_t *ctrl_pipe, int iface_no,
209 usb_hid_report_type_t type, uint8_t *buffer, size_t buf_size,
[f8e4cb6]210 size_t *actual_size)
[35f0899]211{
[f8e4cb6]212 if (ctrl_pipe == NULL) {
[a1732929]213 usb_log_warning("usbhid_req_set_report(): no pipe given.");
[f8e4cb6]214 return EINVAL;
215 }
[ae3a941]216
[f8e4cb6]217 if (iface_no < 0) {
218 usb_log_warning("usbhid_req_set_report(): no interface given."
219 "\n");
[35f0899]220 return EINVAL;
221 }
[ae3a941]222
[35f0899]223 /*
224 * No need for checking other parameters, as they are checked in
225 * the called function (usb_control_request_set()).
226 */
[ae3a941]227
[5a6cc679]228 errno_t rc;
[35f0899]229
230 uint16_t value = 0;
231 value |= (type << 8);
[ae3a941]232
[a1732929]233 usb_log_debug("Sending Get Report request to the device.");
[ae3a941]234
235 rc = usb_control_request_get(ctrl_pipe,
236 USB_REQUEST_TYPE_CLASS, USB_REQUEST_RECIPIENT_INTERFACE,
[f8e4cb6]237 USB_HIDREQ_GET_REPORT, value, iface_no, buffer, buf_size,
[35f0899]238 actual_size);
239
240 if (rc != EOK) {
[0a7627b]241 usb_log_warning("Error sending Get Report request to the device: "
[35f0899]242 "%s.\n", str_error(rc));
243 return rc;
244 }
[ae3a941]245
[35f0899]246 return EOK;
247}
248
[2b12f06]249/**
250 * Send Get Protocol request to the HID device.
251 *
252 * @param[in] hid_dev HID device to send the request to.
253 * @param[out] protocol Current protocol of the device.
254 *
255 * @retval EOK if successful.
256 * @retval EINVAL if no HID device is given.
[8fd4ba0]257 * @return Other value inherited from function usb_control_request_set().
[2b12f06]258 */
[ae3a941]259errno_t usbhid_req_get_protocol(usb_pipe_t *ctrl_pipe, int iface_no,
[f8e4cb6]260 usb_hid_protocol_t *protocol)
[35f0899]261{
[f8e4cb6]262 if (ctrl_pipe == NULL) {
[a1732929]263 usb_log_warning("usbhid_req_set_report(): no pipe given.");
[f8e4cb6]264 return EINVAL;
265 }
[ae3a941]266
[f8e4cb6]267 if (iface_no < 0) {
268 usb_log_warning("usbhid_req_set_report(): no interface given."
269 "\n");
[35f0899]270 return EINVAL;
271 }
[ae3a941]272
[35f0899]273 /*
274 * No need for checking other parameters, as they are checked in
275 * the called function (usb_control_request_set()).
276 */
[ae3a941]277
278 errno_t rc;
[35f0899]279
[0a7627b]280 usb_log_debug("Sending Get Protocol request to the device ("
[f8e4cb6]281 "iface: %d).\n", iface_no);
[ae3a941]282
[35f0899]283 uint8_t buffer[1];
284 size_t actual_size = 0;
[ae3a941]285
286 rc = usb_control_request_get(ctrl_pipe,
287 USB_REQUEST_TYPE_CLASS, USB_REQUEST_RECIPIENT_INTERFACE,
[f8e4cb6]288 USB_HIDREQ_GET_PROTOCOL, 0, iface_no, buffer, 1, &actual_size);
[35f0899]289
290 if (rc != EOK) {
[0a7627b]291 usb_log_warning("Error sending Get Protocol request to the "
292 "device: %s.\n", str_error(rc));
[35f0899]293 return rc;
294 }
[ae3a941]295
[35f0899]296 if (actual_size != 1) {
[a1732929]297 usb_log_warning("Wrong data size: %zu, expected: 1.",
[ae3a941]298 actual_size);
[35f0899]299 return ELIMIT;
300 }
[ae3a941]301
[35f0899]302 *protocol = buffer[0];
[ae3a941]303
[35f0899]304 return EOK;
305}
306
[2b12f06]307/**
308 * Send Get Idle request to the HID device.
309 *
310 * @param[in] hid_dev HID device to send the request to.
311 * @param[out] duration Duration value (multiplicate by 4 to get real duration
312 * in miliseconds).
313 *
314 * @retval EOK if successful.
315 * @retval EINVAL if no HID device is given.
[ae3a941]316 * @return Other value inherited from one of functions
[3954a63b]317 * usb_pipe_start_session(), usb_pipe_end_session(),
[2b12f06]318 * usb_control_request_set().
319 */
[ae3a941]320errno_t usbhid_req_get_idle(usb_pipe_t *ctrl_pipe, int iface_no,
321 uint8_t *duration)
[35f0899]322{
[f8e4cb6]323 if (ctrl_pipe == NULL) {
[a1732929]324 usb_log_warning("usbhid_req_set_report(): no pipe given.");
[f8e4cb6]325 return EINVAL;
326 }
[ae3a941]327
[f8e4cb6]328 if (iface_no < 0) {
329 usb_log_warning("usbhid_req_set_report(): no interface given."
330 "\n");
[35f0899]331 return EINVAL;
332 }
[ae3a941]333
[35f0899]334 /*
335 * No need for checking other parameters, as they are checked in
336 * the called function (usb_control_request_set()).
337 */
[ae3a941]338
[5a6cc679]339 errno_t rc;
[35f0899]340
[0a7627b]341 usb_log_debug("Sending Get Idle request to the device ("
[f8e4cb6]342 "iface: %d).\n", iface_no);
[ae3a941]343
[35f0899]344 uint16_t value = 0;
345 uint8_t buffer[1];
346 size_t actual_size = 0;
[ae3a941]347
348 rc = usb_control_request_get(ctrl_pipe,
349 USB_REQUEST_TYPE_CLASS, USB_REQUEST_RECIPIENT_INTERFACE,
350 USB_HIDREQ_GET_IDLE, value, iface_no, buffer, 1,
[35f0899]351 &actual_size);
352
353 if (rc != EOK) {
[0a7627b]354 usb_log_warning("Error sending Get Idle request to the device: "
[35f0899]355 "%s.\n", str_error(rc));
356 return rc;
357 }
[ae3a941]358
[35f0899]359 if (actual_size != 1) {
[a1732929]360 usb_log_warning("Wrong data size: %zu, expected: 1.",
[ae3a941]361 actual_size);
[35f0899]362 return ELIMIT;
363 }
[ae3a941]364
[35f0899]365 *duration = buffer[0];
[ae3a941]366
[35f0899]367 return EOK;
368}
369
[2391aaf]370/**
371 * @}
372 */
Note: See TracBrowser for help on using the repository browser.