source: mainline/uspace/lib/drv/generic/remote_usbhid.c@ 7f80313

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 7f80313 was 7f80313, checked in by Jan Vesely <jano.vesely@…>, 12 years ago

libdrv: Make interface structures constant.

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