[27b85d9] | 1 | /*
|
---|
| 2 | * Copyright (c) 2011 Vojtech Horky
|
---|
| 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
|
---|
[27b85d9] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | * Client functions for accessing USB HID interface (implementation).
|
---|
| 34 | */
|
---|
[77ad86c] | 35 |
|
---|
[27b85d9] | 36 | #include <dev_iface.h>
|
---|
| 37 | #include <usbhid_iface.h>
|
---|
[faa44e58] | 38 | #include <usb/hid/iface.h>
|
---|
[27b85d9] | 39 | #include <errno.h>
|
---|
| 40 | #include <str_error.h>
|
---|
| 41 | #include <async.h>
|
---|
| 42 | #include <assert.h>
|
---|
| 43 |
|
---|
| 44 | /** Ask for event array length.
|
---|
| 45 | *
|
---|
[79ae36dd] | 46 | * @param dev_sess Session to DDF device providing USB HID interface.
|
---|
| 47 | *
|
---|
[27b85d9] | 48 | * @return Number of usages returned or negative error code.
|
---|
[79ae36dd] | 49 | *
|
---|
[27b85d9] | 50 | */
|
---|
[79ae36dd] | 51 | int usbhid_dev_get_event_length(async_sess_t *dev_sess, size_t *size)
|
---|
[27b85d9] | 52 | {
|
---|
[79ae36dd] | 53 | if (!dev_sess)
|
---|
[27b85d9] | 54 | return EINVAL;
|
---|
[79ae36dd] | 55 |
|
---|
| 56 | async_exch_t *exch = async_exchange_begin(dev_sess);
|
---|
| 57 |
|
---|
[27b85d9] | 58 | sysarg_t len;
|
---|
[79ae36dd] | 59 | int rc = async_req_1_1(exch, DEV_IFACE_ID(USBHID_DEV_IFACE),
|
---|
[27b85d9] | 60 | IPC_M_USBHID_GET_EVENT_LENGTH, &len);
|
---|
[79ae36dd] | 61 |
|
---|
| 62 | async_exchange_end(exch);
|
---|
| 63 |
|
---|
[27b85d9] | 64 | if (rc == EOK) {
|
---|
[79ae36dd] | 65 | if (size != NULL)
|
---|
[9dddb3d] | 66 | *size = (size_t) len;
|
---|
[27b85d9] | 67 | }
|
---|
[e765ccb] | 68 |
|
---|
| 69 | return rc;
|
---|
[27b85d9] | 70 | }
|
---|
| 71 |
|
---|
| 72 | /** Request for next event from HID device.
|
---|
| 73 | *
|
---|
[79ae36dd] | 74 | * @param[in] dev_sess Session to DDF device providing USB HID interface.
|
---|
[27b85d9] | 75 | * @param[out] usage_pages Where to store usage pages.
|
---|
[79ae36dd] | 76 | * @param[out] usages Where to store usages (actual data).
|
---|
| 77 | * @param[in] usage_count Length of @p usage_pages and @p usages buffer
|
---|
| 78 | * (in items, not bytes).
|
---|
[27b85d9] | 79 | * @param[out] actual_usage_count Number of usages actually returned by the
|
---|
[79ae36dd] | 80 | * device driver.
|
---|
| 81 | * @param[in] flags Flags (see USBHID_IFACE_FLAG_*).
|
---|
| 82 | *
|
---|
[27b85d9] | 83 | * @return Error code.
|
---|
[79ae36dd] | 84 | *
|
---|
[27b85d9] | 85 | */
|
---|
[79ae36dd] | 86 | int usbhid_dev_get_event(async_sess_t *dev_sess, uint8_t *buf,
|
---|
[266fcd8] | 87 | size_t size, size_t *actual_size, int *event_nr, unsigned int flags)
|
---|
[27b85d9] | 88 | {
|
---|
[79ae36dd] | 89 | if (!dev_sess)
|
---|
[27b85d9] | 90 | return EINVAL;
|
---|
[79ae36dd] | 91 |
|
---|
[d87561c] | 92 | if (buf == NULL)
|
---|
[27b85d9] | 93 | return ENOMEM;
|
---|
[79ae36dd] | 94 |
|
---|
| 95 | if (size == 0)
|
---|
[27b85d9] | 96 | return EINVAL;
|
---|
[4e78236] | 97 |
|
---|
[e765ccb] | 98 | size_t buffer_size = size;
|
---|
[d7c72db] | 99 | uint8_t *buffer = malloc(buffer_size);
|
---|
[79ae36dd] | 100 | if (buffer == NULL)
|
---|
[27b85d9] | 101 | return ENOMEM;
|
---|
[79ae36dd] | 102 |
|
---|
| 103 | async_exch_t *exch = async_exchange_begin(dev_sess);
|
---|
| 104 |
|
---|
[266fcd8] | 105 | ipc_call_t opening_request_call;
|
---|
[79ae36dd] | 106 | aid_t opening_request = async_send_2(exch,
|
---|
[27b85d9] | 107 | DEV_IFACE_ID(USBHID_DEV_IFACE), IPC_M_USBHID_GET_EVENT,
|
---|
[266fcd8] | 108 | flags, &opening_request_call);
|
---|
[79ae36dd] | 109 |
|
---|
[27b85d9] | 110 | if (opening_request == 0) {
|
---|
[79ae36dd] | 111 | async_exchange_end(exch);
|
---|
[27b85d9] | 112 | free(buffer);
|
---|
| 113 | return ENOMEM;
|
---|
| 114 | }
|
---|
[79ae36dd] | 115 |
|
---|
[27b85d9] | 116 | ipc_call_t data_request_call;
|
---|
[79ae36dd] | 117 | aid_t data_request = async_data_read(exch, buffer, buffer_size,
|
---|
[27b85d9] | 118 | &data_request_call);
|
---|
[79ae36dd] | 119 |
|
---|
| 120 | async_exchange_end(exch);
|
---|
| 121 |
|
---|
[27b85d9] | 122 | if (data_request == 0) {
|
---|
[50b581d] | 123 | async_forget(opening_request);
|
---|
[27b85d9] | 124 | free(buffer);
|
---|
| 125 | return ENOMEM;
|
---|
| 126 | }
|
---|
[79ae36dd] | 127 |
|
---|
[27b85d9] | 128 | sysarg_t data_request_rc;
|
---|
| 129 | sysarg_t opening_request_rc;
|
---|
| 130 | async_wait_for(data_request, &data_request_rc);
|
---|
| 131 | async_wait_for(opening_request, &opening_request_rc);
|
---|
[79ae36dd] | 132 |
|
---|
[27b85d9] | 133 | if (data_request_rc != EOK) {
|
---|
| 134 | /* Prefer return code of the opening request. */
|
---|
[79ae36dd] | 135 | if (opening_request_rc != EOK)
|
---|
[27b85d9] | 136 | return (int) opening_request_rc;
|
---|
[79ae36dd] | 137 | else
|
---|
[27b85d9] | 138 | return (int) data_request_rc;
|
---|
| 139 | }
|
---|
[79ae36dd] | 140 |
|
---|
| 141 | if (opening_request_rc != EOK)
|
---|
[27b85d9] | 142 | return (int) opening_request_rc;
|
---|
[79ae36dd] | 143 |
|
---|
[e765ccb] | 144 | size_t act_size = IPC_GET_ARG2(data_request_call);
|
---|
[79ae36dd] | 145 |
|
---|
[27b85d9] | 146 | /* Copy the individual items. */
|
---|
[e765ccb] | 147 | memcpy(buf, buffer, act_size);
|
---|
[79ae36dd] | 148 |
|
---|
| 149 | if (actual_size != NULL)
|
---|
[e765ccb] | 150 | *actual_size = act_size;
|
---|
[266fcd8] | 151 |
|
---|
[79ae36dd] | 152 | if (event_nr != NULL)
|
---|
[266fcd8] | 153 | *event_nr = IPC_GET_ARG1(opening_request_call);
|
---|
[79ae36dd] | 154 |
|
---|
[27b85d9] | 155 | return EOK;
|
---|
| 156 | }
|
---|
| 157 |
|
---|
[79ae36dd] | 158 | int usbhid_dev_get_report_descriptor_length(async_sess_t *dev_sess,
|
---|
| 159 | size_t *size)
|
---|
[d7c72db] | 160 | {
|
---|
[79ae36dd] | 161 | if (!dev_sess)
|
---|
[9dddb3d] | 162 | return EINVAL;
|
---|
[79ae36dd] | 163 |
|
---|
| 164 | async_exch_t *exch = async_exchange_begin(dev_sess);
|
---|
| 165 |
|
---|
[9dddb3d] | 166 | sysarg_t arg_size;
|
---|
[79ae36dd] | 167 | int rc = async_req_1_1(exch, DEV_IFACE_ID(USBHID_DEV_IFACE),
|
---|
[9dddb3d] | 168 | IPC_M_USBHID_GET_REPORT_DESCRIPTOR_LENGTH, &arg_size);
|
---|
[79ae36dd] | 169 |
|
---|
| 170 | async_exchange_end(exch);
|
---|
| 171 |
|
---|
[9dddb3d] | 172 | if (rc == EOK) {
|
---|
[79ae36dd] | 173 | if (size != NULL)
|
---|
[9dddb3d] | 174 | *size = (size_t) arg_size;
|
---|
| 175 | }
|
---|
[79ae36dd] | 176 |
|
---|
[9dddb3d] | 177 | return rc;
|
---|
[d7c72db] | 178 | }
|
---|
| 179 |
|
---|
[79ae36dd] | 180 | int usbhid_dev_get_report_descriptor(async_sess_t *dev_sess, uint8_t *buf,
|
---|
| 181 | size_t size, size_t *actual_size)
|
---|
[d7c72db] | 182 | {
|
---|
[79ae36dd] | 183 | if (!dev_sess)
|
---|
[9dddb3d] | 184 | return EINVAL;
|
---|
[79ae36dd] | 185 |
|
---|
[d87561c] | 186 | if (buf == NULL)
|
---|
[9dddb3d] | 187 | return ENOMEM;
|
---|
[79ae36dd] | 188 |
|
---|
| 189 | if (size == 0)
|
---|
[9dddb3d] | 190 | return EINVAL;
|
---|
[79ae36dd] | 191 |
|
---|
| 192 | async_exch_t *exch = async_exchange_begin(dev_sess);
|
---|
| 193 |
|
---|
| 194 | aid_t opening_request = async_send_1(exch,
|
---|
[9dddb3d] | 195 | DEV_IFACE_ID(USBHID_DEV_IFACE), IPC_M_USBHID_GET_REPORT_DESCRIPTOR,
|
---|
| 196 | NULL);
|
---|
| 197 | if (opening_request == 0) {
|
---|
[79ae36dd] | 198 | async_exchange_end(exch);
|
---|
[9dddb3d] | 199 | return ENOMEM;
|
---|
| 200 | }
|
---|
[79ae36dd] | 201 |
|
---|
[9dddb3d] | 202 | ipc_call_t data_request_call;
|
---|
[79ae36dd] | 203 | aid_t data_request = async_data_read(exch, buf, size,
|
---|
[9dddb3d] | 204 | &data_request_call);
|
---|
[79ae36dd] | 205 |
|
---|
| 206 | async_exchange_end(exch);
|
---|
| 207 |
|
---|
[9dddb3d] | 208 | if (data_request == 0) {
|
---|
[50b581d] | 209 | async_forget(opening_request);
|
---|
[9dddb3d] | 210 | return ENOMEM;
|
---|
| 211 | }
|
---|
[79ae36dd] | 212 |
|
---|
[9dddb3d] | 213 | sysarg_t data_request_rc;
|
---|
| 214 | sysarg_t opening_request_rc;
|
---|
| 215 | async_wait_for(data_request, &data_request_rc);
|
---|
| 216 | async_wait_for(opening_request, &opening_request_rc);
|
---|
[79ae36dd] | 217 |
|
---|
[9dddb3d] | 218 | if (data_request_rc != EOK) {
|
---|
| 219 | /* Prefer return code of the opening request. */
|
---|
[79ae36dd] | 220 | if (opening_request_rc != EOK)
|
---|
[9dddb3d] | 221 | return (int) opening_request_rc;
|
---|
[79ae36dd] | 222 | else
|
---|
[9dddb3d] | 223 | return (int) data_request_rc;
|
---|
| 224 | }
|
---|
[79ae36dd] | 225 |
|
---|
| 226 | if (opening_request_rc != EOK)
|
---|
[9dddb3d] | 227 | return (int) opening_request_rc;
|
---|
[79ae36dd] | 228 |
|
---|
[9dddb3d] | 229 | size_t act_size = IPC_GET_ARG2(data_request_call);
|
---|
[79ae36dd] | 230 |
|
---|
| 231 | if (actual_size != NULL)
|
---|
[9dddb3d] | 232 | *actual_size = act_size;
|
---|
[79ae36dd] | 233 |
|
---|
[9dddb3d] | 234 | return EOK;
|
---|
[d7c72db] | 235 | }
|
---|
| 236 |
|
---|
[27b85d9] | 237 | /**
|
---|
| 238 | * @}
|
---|
| 239 | */
|
---|