| 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 |
|
|---|
| 40 | #include <usb/hid/hid.h>
|
|---|
| 41 | #include <usb/debug.h>
|
|---|
| 42 | #include <usb/dev/request.h>
|
|---|
| 43 | #include <usb/dev/pipes.h>
|
|---|
| 44 |
|
|---|
| 45 | #include <usb/hid/request.h>
|
|---|
| 46 |
|
|---|
| 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.
|
|---|
| 57 | * @return Other value inherited from function usb_control_request_set().
|
|---|
| 58 | */
|
|---|
| 59 | errno_t usbhid_req_set_report(usb_pipe_t *ctrl_pipe, int iface_no,
|
|---|
| 60 | usb_hid_report_type_t type, uint8_t *buffer, size_t buf_size)
|
|---|
| 61 | {
|
|---|
| 62 | if (ctrl_pipe == NULL) {
|
|---|
| 63 | usb_log_warning("usbhid_req_set_report(): no pipe given.");
|
|---|
| 64 | return EINVAL;
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | if (iface_no < 0) {
|
|---|
| 68 | usb_log_warning("usbhid_req_set_report(): no interface given."
|
|---|
| 69 | "\n");
|
|---|
| 70 | return EINVAL;
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | /*
|
|---|
| 74 | * No need for checking other parameters, as they are checked in
|
|---|
| 75 | * the called function (usb_control_request_set()).
|
|---|
| 76 | */
|
|---|
| 77 |
|
|---|
| 78 | errno_t rc;
|
|---|
| 79 |
|
|---|
| 80 | uint16_t value = 0;
|
|---|
| 81 | value |= (type << 8);
|
|---|
| 82 |
|
|---|
| 83 | usb_log_debug("Sending Set Report request to the device.");
|
|---|
| 84 |
|
|---|
| 85 | rc = usb_control_request_set(ctrl_pipe,
|
|---|
| 86 | USB_REQUEST_TYPE_CLASS, USB_REQUEST_RECIPIENT_INTERFACE,
|
|---|
| 87 | USB_HIDREQ_SET_REPORT, value, iface_no, buffer, buf_size);
|
|---|
| 88 |
|
|---|
| 89 | if (rc != EOK) {
|
|---|
| 90 | usb_log_error("Error sending Set Report request to the "
|
|---|
| 91 | "device: %s.\n", str_error(rc));
|
|---|
| 92 | return rc;
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | return EOK;
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 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.
|
|---|
| 106 | * @return Other value inherited from function usb_control_request_set().
|
|---|
| 107 | */
|
|---|
| 108 | errno_t usbhid_req_set_protocol(usb_pipe_t *ctrl_pipe, int iface_no,
|
|---|
| 109 | usb_hid_protocol_t protocol)
|
|---|
| 110 | {
|
|---|
| 111 | if (ctrl_pipe == NULL) {
|
|---|
| 112 | usb_log_warning("usbhid_req_set_report(): no pipe given.");
|
|---|
| 113 | return EINVAL;
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | if (iface_no < 0) {
|
|---|
| 117 | usb_log_warning("usbhid_req_set_report(): no interface given."
|
|---|
| 118 | "\n");
|
|---|
| 119 | return EINVAL;
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | /*
|
|---|
| 123 | * No need for checking other parameters, as they are checked in
|
|---|
| 124 | * the called function (usb_control_request_set()).
|
|---|
| 125 | */
|
|---|
| 126 |
|
|---|
| 127 | errno_t rc;
|
|---|
| 128 |
|
|---|
| 129 | usb_log_debug("Sending Set Protocol request to the device ("
|
|---|
| 130 | "protocol: %d, iface: %d).\n", protocol, iface_no);
|
|---|
| 131 |
|
|---|
| 132 | rc = usb_control_request_set(ctrl_pipe,
|
|---|
| 133 | USB_REQUEST_TYPE_CLASS, USB_REQUEST_RECIPIENT_INTERFACE,
|
|---|
| 134 | USB_HIDREQ_SET_PROTOCOL, protocol, iface_no, NULL, 0);
|
|---|
| 135 |
|
|---|
| 136 | if (rc != EOK) {
|
|---|
| 137 | usb_log_warning("Error sending Set Protocol request to the "
|
|---|
| 138 | "device: %s.\n", str_error(rc));
|
|---|
| 139 | return rc;
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 | return EOK;
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 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.
|
|---|
| 154 | * @return Other value inherited from function usb_control_request_set().
|
|---|
| 155 | */
|
|---|
| 156 | errno_t usbhid_req_set_idle(usb_pipe_t *ctrl_pipe, int iface_no, uint8_t duration)
|
|---|
| 157 | {
|
|---|
| 158 | if (ctrl_pipe == NULL) {
|
|---|
| 159 | usb_log_warning("usbhid_req_set_report(): no pipe given.");
|
|---|
| 160 | return EINVAL;
|
|---|
| 161 | }
|
|---|
| 162 |
|
|---|
| 163 | if (iface_no < 0) {
|
|---|
| 164 | usb_log_warning("usbhid_req_set_report(): no interface given."
|
|---|
| 165 | "\n");
|
|---|
| 166 | return EINVAL;
|
|---|
| 167 | }
|
|---|
| 168 |
|
|---|
| 169 | /*
|
|---|
| 170 | * No need for checking other parameters, as they are checked in
|
|---|
| 171 | * the called function (usb_control_request_set()).
|
|---|
| 172 | */
|
|---|
| 173 |
|
|---|
| 174 | errno_t rc;
|
|---|
| 175 |
|
|---|
| 176 | usb_log_debug("Sending Set Idle request to the device ("
|
|---|
| 177 | "duration: %u, iface: %d).\n", duration, iface_no);
|
|---|
| 178 |
|
|---|
| 179 | uint16_t value = duration << 8;
|
|---|
| 180 |
|
|---|
| 181 | rc = usb_control_request_set(ctrl_pipe,
|
|---|
| 182 | USB_REQUEST_TYPE_CLASS, USB_REQUEST_RECIPIENT_INTERFACE,
|
|---|
| 183 | USB_HIDREQ_SET_IDLE, value, iface_no, NULL, 0);
|
|---|
| 184 |
|
|---|
| 185 | if (rc != EOK) {
|
|---|
| 186 | usb_log_warning("Device did not accept Set Idle request: "
|
|---|
| 187 | "%s.\n", str_error(rc));
|
|---|
| 188 | return rc;
|
|---|
| 189 | }
|
|---|
| 190 |
|
|---|
| 191 | return EOK;
|
|---|
| 192 | }
|
|---|
| 193 |
|
|---|
| 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).
|
|---|
| 201 | * @param[out] actual_size Actual size of report received from the device
|
|---|
| 202 | * (in bytes).
|
|---|
| 203 | *
|
|---|
| 204 | * @retval EOK if successful.
|
|---|
| 205 | * @retval EINVAL if no HID device is given.
|
|---|
| 206 | * @return Other value inherited from function usb_control_request_set().
|
|---|
| 207 | */
|
|---|
| 208 | errno_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,
|
|---|
| 210 | size_t *actual_size)
|
|---|
| 211 | {
|
|---|
| 212 | if (ctrl_pipe == NULL) {
|
|---|
| 213 | usb_log_warning("usbhid_req_set_report(): no pipe given.");
|
|---|
| 214 | return EINVAL;
|
|---|
| 215 | }
|
|---|
| 216 |
|
|---|
| 217 | if (iface_no < 0) {
|
|---|
| 218 | usb_log_warning("usbhid_req_set_report(): no interface given."
|
|---|
| 219 | "\n");
|
|---|
| 220 | return EINVAL;
|
|---|
| 221 | }
|
|---|
| 222 |
|
|---|
| 223 | /*
|
|---|
| 224 | * No need for checking other parameters, as they are checked in
|
|---|
| 225 | * the called function (usb_control_request_set()).
|
|---|
| 226 | */
|
|---|
| 227 |
|
|---|
| 228 | errno_t rc;
|
|---|
| 229 |
|
|---|
| 230 | uint16_t value = 0;
|
|---|
| 231 | value |= (type << 8);
|
|---|
| 232 |
|
|---|
| 233 | usb_log_debug("Sending Get Report request to the device.");
|
|---|
| 234 |
|
|---|
| 235 | rc = usb_control_request_get(ctrl_pipe,
|
|---|
| 236 | USB_REQUEST_TYPE_CLASS, USB_REQUEST_RECIPIENT_INTERFACE,
|
|---|
| 237 | USB_HIDREQ_GET_REPORT, value, iface_no, buffer, buf_size,
|
|---|
| 238 | actual_size);
|
|---|
| 239 |
|
|---|
| 240 | if (rc != EOK) {
|
|---|
| 241 | usb_log_warning("Error sending Get Report request to the device: "
|
|---|
| 242 | "%s.\n", str_error(rc));
|
|---|
| 243 | return rc;
|
|---|
| 244 | }
|
|---|
| 245 |
|
|---|
| 246 | return EOK;
|
|---|
| 247 | }
|
|---|
| 248 |
|
|---|
| 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.
|
|---|
| 257 | * @return Other value inherited from function usb_control_request_set().
|
|---|
| 258 | */
|
|---|
| 259 | errno_t usbhid_req_get_protocol(usb_pipe_t *ctrl_pipe, int iface_no,
|
|---|
| 260 | usb_hid_protocol_t *protocol)
|
|---|
| 261 | {
|
|---|
| 262 | if (ctrl_pipe == NULL) {
|
|---|
| 263 | usb_log_warning("usbhid_req_set_report(): no pipe given.");
|
|---|
| 264 | return EINVAL;
|
|---|
| 265 | }
|
|---|
| 266 |
|
|---|
| 267 | if (iface_no < 0) {
|
|---|
| 268 | usb_log_warning("usbhid_req_set_report(): no interface given."
|
|---|
| 269 | "\n");
|
|---|
| 270 | return EINVAL;
|
|---|
| 271 | }
|
|---|
| 272 |
|
|---|
| 273 | /*
|
|---|
| 274 | * No need for checking other parameters, as they are checked in
|
|---|
| 275 | * the called function (usb_control_request_set()).
|
|---|
| 276 | */
|
|---|
| 277 |
|
|---|
| 278 | errno_t rc;
|
|---|
| 279 |
|
|---|
| 280 | usb_log_debug("Sending Get Protocol request to the device ("
|
|---|
| 281 | "iface: %d).\n", iface_no);
|
|---|
| 282 |
|
|---|
| 283 | uint8_t buffer[1];
|
|---|
| 284 | size_t actual_size = 0;
|
|---|
| 285 |
|
|---|
| 286 | rc = usb_control_request_get(ctrl_pipe,
|
|---|
| 287 | USB_REQUEST_TYPE_CLASS, USB_REQUEST_RECIPIENT_INTERFACE,
|
|---|
| 288 | USB_HIDREQ_GET_PROTOCOL, 0, iface_no, buffer, 1, &actual_size);
|
|---|
| 289 |
|
|---|
| 290 | if (rc != EOK) {
|
|---|
| 291 | usb_log_warning("Error sending Get Protocol request to the "
|
|---|
| 292 | "device: %s.\n", str_error(rc));
|
|---|
| 293 | return rc;
|
|---|
| 294 | }
|
|---|
| 295 |
|
|---|
| 296 | if (actual_size != 1) {
|
|---|
| 297 | usb_log_warning("Wrong data size: %zu, expected: 1.",
|
|---|
| 298 | actual_size);
|
|---|
| 299 | return ELIMIT;
|
|---|
| 300 | }
|
|---|
| 301 |
|
|---|
| 302 | *protocol = buffer[0];
|
|---|
| 303 |
|
|---|
| 304 | return EOK;
|
|---|
| 305 | }
|
|---|
| 306 |
|
|---|
| 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.
|
|---|
| 316 | * @return Other value inherited from one of functions
|
|---|
| 317 | * usb_pipe_start_session(), usb_pipe_end_session(),
|
|---|
| 318 | * usb_control_request_set().
|
|---|
| 319 | */
|
|---|
| 320 | errno_t usbhid_req_get_idle(usb_pipe_t *ctrl_pipe, int iface_no,
|
|---|
| 321 | uint8_t *duration)
|
|---|
| 322 | {
|
|---|
| 323 | if (ctrl_pipe == NULL) {
|
|---|
| 324 | usb_log_warning("usbhid_req_set_report(): no pipe given.");
|
|---|
| 325 | return EINVAL;
|
|---|
| 326 | }
|
|---|
| 327 |
|
|---|
| 328 | if (iface_no < 0) {
|
|---|
| 329 | usb_log_warning("usbhid_req_set_report(): no interface given."
|
|---|
| 330 | "\n");
|
|---|
| 331 | return EINVAL;
|
|---|
| 332 | }
|
|---|
| 333 |
|
|---|
| 334 | /*
|
|---|
| 335 | * No need for checking other parameters, as they are checked in
|
|---|
| 336 | * the called function (usb_control_request_set()).
|
|---|
| 337 | */
|
|---|
| 338 |
|
|---|
| 339 | errno_t rc;
|
|---|
| 340 |
|
|---|
| 341 | usb_log_debug("Sending Get Idle request to the device ("
|
|---|
| 342 | "iface: %d).\n", iface_no);
|
|---|
| 343 |
|
|---|
| 344 | uint16_t value = 0;
|
|---|
| 345 | uint8_t buffer[1];
|
|---|
| 346 | size_t actual_size = 0;
|
|---|
| 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,
|
|---|
| 351 | &actual_size);
|
|---|
| 352 |
|
|---|
| 353 | if (rc != EOK) {
|
|---|
| 354 | usb_log_warning("Error sending Get Idle request to the device: "
|
|---|
| 355 | "%s.\n", str_error(rc));
|
|---|
| 356 | return rc;
|
|---|
| 357 | }
|
|---|
| 358 |
|
|---|
| 359 | if (actual_size != 1) {
|
|---|
| 360 | usb_log_warning("Wrong data size: %zu, expected: 1.",
|
|---|
| 361 | actual_size);
|
|---|
| 362 | return ELIMIT;
|
|---|
| 363 | }
|
|---|
| 364 |
|
|---|
| 365 | *duration = buffer[0];
|
|---|
| 366 |
|
|---|
| 367 | return EOK;
|
|---|
| 368 | }
|
|---|
| 369 |
|
|---|
| 370 | /**
|
|---|
| 371 | * @}
|
|---|
| 372 | */
|
|---|