[27b85d9] | 1 | /*
|
---|
| 2 | * Copyright (c) 2010-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 libdrv
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | */
|
---|
| 34 |
|
---|
| 35 | #include <async.h>
|
---|
| 36 | #include <errno.h>
|
---|
| 37 | #include <assert.h>
|
---|
[4e78236] | 38 | #include <stdio.h>
|
---|
[27b85d9] | 39 |
|
---|
| 40 | #include "usbhid_iface.h"
|
---|
| 41 | #include "ddf/driver.h"
|
---|
| 42 |
|
---|
| 43 | static void remote_usbhid_get_event_length(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
|
---|
| 44 | static void remote_usbhid_get_event(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
|
---|
[d7c72db] | 45 | static void remote_usbhid_get_report_descriptor_length(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
|
---|
| 46 | static void remote_usbhid_get_report_descriptor(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
|
---|
[27b85d9] | 47 | // static void remote_usbhid_(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
|
---|
| 48 |
|
---|
| 49 | /** Remote USB HID interface operations. */
|
---|
| 50 | static remote_iface_func_ptr_t remote_usbhid_iface_ops [] = {
|
---|
| 51 | remote_usbhid_get_event_length,
|
---|
[d7c72db] | 52 | remote_usbhid_get_event,
|
---|
| 53 | remote_usbhid_get_report_descriptor_length,
|
---|
| 54 | remote_usbhid_get_report_descriptor
|
---|
[27b85d9] | 55 | };
|
---|
| 56 |
|
---|
| 57 | /** Remote USB HID interface structure.
|
---|
| 58 | */
|
---|
| 59 | remote_iface_t remote_usbhid_iface = {
|
---|
| 60 | .method_count = sizeof(remote_usbhid_iface_ops) /
|
---|
| 61 | sizeof(remote_usbhid_iface_ops[0]),
|
---|
| 62 | .methods = remote_usbhid_iface_ops
|
---|
| 63 | };
|
---|
| 64 |
|
---|
[4e78236] | 65 | //usbhc_iface_t *usb_iface = (usbhc_iface_t *) iface;
|
---|
[27b85d9] | 66 |
|
---|
| 67 |
|
---|
| 68 | void remote_usbhid_get_event_length(ddf_fun_t *fun, void *iface,
|
---|
| 69 | ipc_callid_t callid, ipc_call_t *call)
|
---|
| 70 | {
|
---|
[e7079cf] | 71 | printf("remote_usbhid_get_event_length()\n");
|
---|
| 72 |
|
---|
[27b85d9] | 73 | usbhid_iface_t *hid_iface = (usbhid_iface_t *) iface;
|
---|
| 74 |
|
---|
| 75 | if (!hid_iface->get_event_length) {
|
---|
[e7079cf] | 76 | printf("Get event length not set!\n");
|
---|
[27b85d9] | 77 | async_answer_0(callid, ENOTSUP);
|
---|
| 78 | return;
|
---|
| 79 | }
|
---|
| 80 |
|
---|
[e765ccb] | 81 | size_t len = hid_iface->get_event_length(fun);
|
---|
[4e78236] | 82 | // if (len == 0) {
|
---|
| 83 | // len = EEMPTY;
|
---|
| 84 | // }
|
---|
| 85 | async_answer_1(callid, EOK, len);
|
---|
| 86 |
|
---|
| 87 | // if (len < 0) {
|
---|
| 88 | // async_answer_0(callid, len);
|
---|
| 89 | // } else {
|
---|
| 90 | // async_answer_1(callid, EOK, len);
|
---|
| 91 | // }
|
---|
[27b85d9] | 92 | }
|
---|
| 93 |
|
---|
| 94 | void remote_usbhid_get_event(ddf_fun_t *fun, void *iface,
|
---|
| 95 | ipc_callid_t callid, ipc_call_t *call)
|
---|
| 96 | {
|
---|
| 97 | usbhid_iface_t *hid_iface = (usbhid_iface_t *) iface;
|
---|
| 98 |
|
---|
| 99 | if (!hid_iface->get_event) {
|
---|
| 100 | async_answer_0(callid, ENOTSUP);
|
---|
| 101 | return;
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | unsigned int flags = DEV_IPC_GET_ARG1(*call);
|
---|
| 105 |
|
---|
| 106 | size_t len;
|
---|
| 107 | ipc_callid_t data_callid;
|
---|
| 108 | if (!async_data_read_receive(&data_callid, &len)) {
|
---|
| 109 | async_answer_0(callid, EPARTY);
|
---|
| 110 | return;
|
---|
| 111 | }
|
---|
[e765ccb] | 112 | // /* Check that length is even number. Truncate otherwise. */
|
---|
| 113 | // if ((len % 2) == 1) {
|
---|
| 114 | // len--;
|
---|
| 115 | // }
|
---|
[27b85d9] | 116 | if (len == 0) {
|
---|
| 117 | async_answer_0(data_callid, EINVAL);
|
---|
| 118 | async_answer_0(callid, EINVAL);
|
---|
[9dddb3d] | 119 | return;
|
---|
[27b85d9] | 120 | }
|
---|
| 121 |
|
---|
| 122 | int rc;
|
---|
| 123 |
|
---|
[d7c72db] | 124 | uint8_t *data = malloc(len);
|
---|
[e765ccb] | 125 | if (data == NULL) {
|
---|
[27b85d9] | 126 | async_answer_0(data_callid, ENOMEM);
|
---|
| 127 | async_answer_0(callid, ENOMEM);
|
---|
[9dddb3d] | 128 | return;
|
---|
[27b85d9] | 129 | }
|
---|
| 130 |
|
---|
[e765ccb] | 131 | size_t act_length;
|
---|
[266fcd8] | 132 | int event_nr;
|
---|
| 133 | rc = hid_iface->get_event(fun, data, len, &act_length, &event_nr, flags);
|
---|
[27b85d9] | 134 | if (rc != EOK) {
|
---|
[e765ccb] | 135 | free(data);
|
---|
[27b85d9] | 136 | async_answer_0(data_callid, rc);
|
---|
| 137 | async_answer_0(callid, rc);
|
---|
[9dddb3d] | 138 | return;
|
---|
[27b85d9] | 139 | }
|
---|
[e765ccb] | 140 | if (act_length >= len) {
|
---|
[27b85d9] | 141 | /* This shall not happen. */
|
---|
| 142 | // FIXME: how about an assert here?
|
---|
[e765ccb] | 143 | act_length = len;
|
---|
[27b85d9] | 144 | }
|
---|
| 145 |
|
---|
[e765ccb] | 146 | async_data_read_finalize(data_callid, data, act_length);
|
---|
[27b85d9] | 147 |
|
---|
[e765ccb] | 148 | free(data);
|
---|
[27b85d9] | 149 |
|
---|
[266fcd8] | 150 | async_answer_1(callid, EOK, event_nr);
|
---|
[27b85d9] | 151 | }
|
---|
| 152 |
|
---|
[9dddb3d] | 153 | void remote_usbhid_get_report_descriptor_length(ddf_fun_t *fun, void *iface,
|
---|
[d7c72db] | 154 | ipc_callid_t callid, ipc_call_t *call)
|
---|
| 155 | {
|
---|
[9dddb3d] | 156 | usbhid_iface_t *hid_iface = (usbhid_iface_t *) iface;
|
---|
| 157 |
|
---|
| 158 | if (!hid_iface->get_report_descriptor_length) {
|
---|
| 159 | async_answer_0(callid, ENOTSUP);
|
---|
| 160 | return;
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | size_t len = hid_iface->get_report_descriptor_length(fun);
|
---|
| 164 | async_answer_1(callid, EOK, (sysarg_t) len);
|
---|
[d7c72db] | 165 | }
|
---|
| 166 |
|
---|
[9dddb3d] | 167 | void remote_usbhid_get_report_descriptor(ddf_fun_t *fun, void *iface,
|
---|
[d7c72db] | 168 | ipc_callid_t callid, ipc_call_t *call)
|
---|
| 169 | {
|
---|
[9dddb3d] | 170 | usbhid_iface_t *hid_iface = (usbhid_iface_t *) iface;
|
---|
| 171 |
|
---|
| 172 | if (!hid_iface->get_report_descriptor) {
|
---|
| 173 | async_answer_0(callid, ENOTSUP);
|
---|
| 174 | return;
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 | size_t len;
|
---|
| 178 | ipc_callid_t data_callid;
|
---|
| 179 | if (!async_data_read_receive(&data_callid, &len)) {
|
---|
| 180 | async_answer_0(callid, EINVAL);
|
---|
| 181 | return;
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 | if (len == 0) {
|
---|
| 185 | async_answer_0(data_callid, EINVAL);
|
---|
| 186 | async_answer_0(callid, EINVAL);
|
---|
| 187 | return;
|
---|
| 188 | }
|
---|
| 189 |
|
---|
| 190 | uint8_t *descriptor = malloc(len);
|
---|
| 191 | if (descriptor == NULL) {
|
---|
| 192 | async_answer_0(data_callid, ENOMEM);
|
---|
| 193 | async_answer_0(callid, ENOMEM);
|
---|
| 194 | return;
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 | size_t act_len = 0;
|
---|
| 198 | int rc = hid_iface->get_report_descriptor(fun, descriptor, len,
|
---|
| 199 | &act_len);
|
---|
| 200 | if (act_len > len) {
|
---|
| 201 | rc = ELIMIT;
|
---|
| 202 | }
|
---|
| 203 | if (rc != EOK) {
|
---|
| 204 | free(descriptor);
|
---|
| 205 | async_answer_0(data_callid, rc);
|
---|
| 206 | async_answer_0(callid, rc);
|
---|
| 207 | return;
|
---|
| 208 | }
|
---|
| 209 |
|
---|
| 210 | async_data_read_finalize(data_callid, descriptor, act_len);
|
---|
| 211 | async_answer_0(callid, EOK);
|
---|
| 212 |
|
---|
| 213 | free(descriptor);
|
---|
[d7c72db] | 214 | }
|
---|
| 215 |
|
---|
[9dddb3d] | 216 |
|
---|
| 217 |
|
---|
[27b85d9] | 218 | /**
|
---|
| 219 | * @}
|
---|
| 220 | */
|
---|