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