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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since e0a4686 was ae3a941, checked in by Ondřej Hlavatý <aearsis@…>, 7 years ago

usb: cstyle

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