source: mainline/uspace/lib/drv/generic/remote_usbdiag.c@ ee820ff

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since ee820ff was 298e010, checked in by Petr Manek <petr.manek@…>, 8 years ago

drv: refactor usbdiag interface to errno_t

  • Property mode set to 100644
File size: 6.3 KB
RevLine 
[7faf0f0]1/*
2 * Copyright (c) 2017 Petr Manek
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 * USB diagnostic device remote interface.
34 */
35
36#include <async.h>
37#include <assert.h>
38#include <macros.h>
39#include <errno.h>
40#include <devman.h>
41
42#include "usbdiag_iface.h"
43
44typedef enum {
[8393c73b]45 IPC_M_USBDIAG_TEST_IN,
46 IPC_M_USBDIAG_TEST_OUT,
[7faf0f0]47} usb_iface_funcs_t;
48
49async_sess_t *usbdiag_connect(devman_handle_t handle)
50{
51 return devman_device_connect(handle, IPC_FLAG_BLOCKING);
52}
53
54void usbdiag_disconnect(async_sess_t *sess)
55{
56 if (sess)
57 async_hangup(sess);
58}
59
[298e010]60errno_t usbdiag_test_in(async_exch_t *exch, const usbdiag_test_params_t *params, usbdiag_test_results_t *results)
[7faf0f0]61{
62 if (!exch)
63 return EBADMEM;
64
[8393c73b]65 ipc_call_t answer;
66 aid_t req = async_send_1(exch, DEV_IFACE_ID(USBDIAG_DEV_IFACE), IPC_M_USBDIAG_TEST_IN, &answer);
[b10a434]67
[298e010]68 errno_t rc = async_data_write_start(exch, params, sizeof(usbdiag_test_params_t));
[8393c73b]69 if (rc != EOK) {
70 async_exchange_end(exch);
71 async_forget(req);
72 return rc;
73 }
[24c8bf8]74
[8393c73b]75 rc = async_data_read_start(exch, results, sizeof(usbdiag_test_results_t));
76 if (rc != EOK) {
77 async_exchange_end(exch);
78 async_forget(req);
79 return rc;
80 }
[24c8bf8]81
[8393c73b]82 async_exchange_end(exch);
[24c8bf8]83
[64ce0c1]84 errno_t retval;
[8393c73b]85 async_wait_for(req, &retval);
[24c8bf8]86
[298e010]87 return (errno_t) retval;
[24c8bf8]88}
89
[298e010]90errno_t usbdiag_test_out(async_exch_t *exch, const usbdiag_test_params_t *params, usbdiag_test_results_t *results)
[24c8bf8]91{
92 if (!exch)
93 return EBADMEM;
94
[8393c73b]95 ipc_call_t answer;
96 aid_t req = async_send_1(exch, DEV_IFACE_ID(USBDIAG_DEV_IFACE), IPC_M_USBDIAG_TEST_OUT, &answer);
[24c8bf8]97
[298e010]98 errno_t rc = async_data_write_start(exch, params, sizeof(usbdiag_test_params_t));
[8393c73b]99 if (rc != EOK) {
100 async_exchange_end(exch);
101 async_forget(req);
102 return rc;
103 }
[24c8bf8]104
[8393c73b]105 rc = async_data_read_start(exch, results, sizeof(usbdiag_test_results_t));
106 if (rc != EOK) {
107 async_exchange_end(exch);
108 async_forget(req);
109 return rc;
110 }
[24c8bf8]111
[8393c73b]112 async_exchange_end(exch);
[24c8bf8]113
[64ce0c1]114 errno_t retval;
[8393c73b]115 async_wait_for(req, &retval);
[24c8bf8]116
[298e010]117 return (errno_t) retval;
[24c8bf8]118}
119
[8393c73b]120static void remote_usbdiag_test_in(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
121static void remote_usbdiag_test_out(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
[24c8bf8]122
[7faf0f0]123/** Remote USB diagnostic interface operations. */
124static const remote_iface_func_ptr_t remote_usbdiag_iface_ops [] = {
[8393c73b]125 [IPC_M_USBDIAG_TEST_IN] = remote_usbdiag_test_in,
126 [IPC_M_USBDIAG_TEST_OUT] = remote_usbdiag_test_out
[7faf0f0]127};
128
129/** Remote USB diagnostic interface structure. */
130const remote_iface_t remote_usbdiag_iface = {
131 .method_count = ARRAY_SIZE(remote_usbdiag_iface_ops),
132 .methods = remote_usbdiag_iface_ops,
133};
134
[8393c73b]135void remote_usbdiag_test_in(ddf_fun_t *fun, void *iface, ipc_callid_t callid, ipc_call_t *call)
[e9d600c2]136{
137 const usbdiag_iface_t *diag_iface = (usbdiag_iface_t *) iface;
138
[8393c73b]139 size_t size;
140 ipc_callid_t data_callid;
141 if (!async_data_write_receive(&data_callid, &size)) {
142 async_answer_0(data_callid, EINVAL);
143 async_answer_0(callid, EINVAL);
[e9d600c2]144 return;
145 }
146
[8393c73b]147 if (size != sizeof(usbdiag_test_params_t)) {
148 async_answer_0(data_callid, EINVAL);
149 async_answer_0(callid, EINVAL);
[7faf0f0]150 return;
151 }
152
[8393c73b]153 usbdiag_test_params_t params;
154 if (async_data_write_finalize(data_callid, &params, size) != EOK) {
155 async_answer_0(callid, EINVAL);
[fd312d5]156 return;
[7faf0f0]157 }
[fd312d5]158
[8393c73b]159 usbdiag_test_results_t results;
[298e010]160 const errno_t ret = !diag_iface->test_in ? ENOTSUP : diag_iface->test_in(fun, &params, &results);
[b10a434]161
162 if (ret != EOK) {
163 async_answer_0(callid, ret);
[e9d600c2]164 return;
165 }
166
[8393c73b]167 if (!async_data_read_receive(&data_callid, &size)) {
168 async_answer_0(data_callid, EINVAL);
169 async_answer_0(callid, EINVAL);
[ff16da5f]170 return;
171 }
172
[8393c73b]173 if (size != sizeof(usbdiag_test_results_t)) {
174 async_answer_0(data_callid, EINVAL);
175 async_answer_0(callid, EINVAL);
[ff16da5f]176 return;
177 }
[8393c73b]178
179 if (async_data_read_finalize(data_callid, &results, size) != EOK) {
180 async_answer_0(callid, EINVAL);
[24c8bf8]181 return;
182 }
183
[8393c73b]184 async_answer_0(callid, ret);
[24c8bf8]185}
186
[8393c73b]187void remote_usbdiag_test_out(ddf_fun_t *fun, void *iface, ipc_callid_t callid, ipc_call_t *call)
[24c8bf8]188{
189 const usbdiag_iface_t *diag_iface = (usbdiag_iface_t *) iface;
190
[8393c73b]191 size_t size;
192 ipc_callid_t data_callid;
193 if (!async_data_write_receive(&data_callid, &size)) {
194 async_answer_0(data_callid, EINVAL);
195 async_answer_0(callid, EINVAL);
[24c8bf8]196 return;
197 }
198
[8393c73b]199 if (size != sizeof(usbdiag_test_params_t)) {
200 async_answer_0(data_callid, EINVAL);
201 async_answer_0(callid, EINVAL);
[24c8bf8]202 return;
203 }
204
[8393c73b]205 usbdiag_test_params_t params;
206 if (async_data_write_finalize(data_callid, &params, size) != EOK) {
207 async_answer_0(callid, EINVAL);
[24c8bf8]208 return;
209 }
210
[8393c73b]211 usbdiag_test_results_t results;
[298e010]212 const errno_t ret = !diag_iface->test_out ? ENOTSUP : diag_iface->test_out(fun, &params, &results);
[24c8bf8]213
214 if (ret != EOK) {
215 async_answer_0(callid, ret);
[8393c73b]216 return;
[24c8bf8]217 }
218
[8393c73b]219 if (!async_data_read_receive(&data_callid, &size)) {
220 async_answer_0(data_callid, EINVAL);
221 async_answer_0(callid, EINVAL);
[24c8bf8]222 return;
223 }
224
[8393c73b]225 if (size != sizeof(usbdiag_test_results_t)) {
226 async_answer_0(data_callid, EINVAL);
227 async_answer_0(callid, EINVAL);
228 return;
[24c8bf8]229 }
[8393c73b]230
231 if (async_data_read_finalize(data_callid, &results, size) != EOK) {
232 async_answer_0(callid, EINVAL);
[24c8bf8]233 return;
234 }
235
[8393c73b]236 async_answer_0(callid, ret);
[24c8bf8]237}
238
[7faf0f0]239/**
240 * @}
241 */
Note: See TracBrowser for help on using the repository browser.