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