source: mainline/uspace/lib/drv/generic/remote_usbhid.c@ 612af1a0

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 612af1a0 was 266fcd8, checked in by Lubos Slovak <lubos.slovak@…>, 14 years ago

Sending event number in get_event() interface + control in mkbd.

  • Property mode set to 100644
File size: 5.7 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
40#include "usbhid_iface.h"
41#include "ddf/driver.h"
42
43static void remote_usbhid_get_event_length(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
44static void remote_usbhid_get_event(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
45static void remote_usbhid_get_report_descriptor_length(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
46static void remote_usbhid_get_report_descriptor(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
47// static void remote_usbhid_(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
48
49/** Remote USB HID interface operations. */
50static remote_iface_func_ptr_t remote_usbhid_iface_ops [] = {
51 remote_usbhid_get_event_length,
52 remote_usbhid_get_event,
53 remote_usbhid_get_report_descriptor_length,
54 remote_usbhid_get_report_descriptor
55};
56
57/** Remote USB HID interface structure.
58 */
59remote_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
65//usbhc_iface_t *usb_iface = (usbhc_iface_t *) iface;
66
67
68void remote_usbhid_get_event_length(ddf_fun_t *fun, void *iface,
69 ipc_callid_t callid, ipc_call_t *call)
70{
71 printf("remote_usbhid_get_event_length()\n");
72
73 usbhid_iface_t *hid_iface = (usbhid_iface_t *) iface;
74
75 if (!hid_iface->get_event_length) {
76 printf("Get event length not set!\n");
77 async_answer_0(callid, ENOTSUP);
78 return;
79 }
80
81 size_t len = hid_iface->get_event_length(fun);
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// }
92}
93
94void 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 }
112// /* Check that length is even number. Truncate otherwise. */
113// if ((len % 2) == 1) {
114// len--;
115// }
116 if (len == 0) {
117 async_answer_0(data_callid, EINVAL);
118 async_answer_0(callid, EINVAL);
119 return;
120 }
121
122 int rc;
123
124 uint8_t *data = malloc(len);
125 if (data == NULL) {
126 async_answer_0(data_callid, ENOMEM);
127 async_answer_0(callid, ENOMEM);
128 return;
129 }
130
131 size_t act_length;
132 int event_nr;
133 rc = hid_iface->get_event(fun, data, len, &act_length, &event_nr, flags);
134 if (rc != EOK) {
135 free(data);
136 async_answer_0(data_callid, rc);
137 async_answer_0(callid, rc);
138 return;
139 }
140 if (act_length >= len) {
141 /* This shall not happen. */
142 // FIXME: how about an assert here?
143 act_length = len;
144 }
145
146 async_data_read_finalize(data_callid, data, act_length);
147
148 free(data);
149
150 async_answer_1(callid, EOK, event_nr);
151}
152
153void remote_usbhid_get_report_descriptor_length(ddf_fun_t *fun, void *iface,
154 ipc_callid_t callid, ipc_call_t *call)
155{
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);
165}
166
167void remote_usbhid_get_report_descriptor(ddf_fun_t *fun, void *iface,
168 ipc_callid_t callid, ipc_call_t *call)
169{
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);
214}
215
216
217
218/**
219 * @}
220 */
Note: See TracBrowser for help on using the repository browser.