1 | /*
|
---|
2 | * Copyright (c) 2010 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 <ipc/ipc.h>
|
---|
36 | #include <async.h>
|
---|
37 | #include <errno.h>
|
---|
38 |
|
---|
39 | #include "usb_iface.h"
|
---|
40 | #include "driver.h"
|
---|
41 |
|
---|
42 | #define USB_MAX_PAYLOAD_SIZE 1020
|
---|
43 |
|
---|
44 | static void remote_usb_get_buffer(device_t *, void *, ipc_callid_t, ipc_call_t *);
|
---|
45 | static void remote_usb_interrupt_out(device_t *, void *, ipc_callid_t, ipc_call_t *);
|
---|
46 | static void remote_usb_interrupt_in(device_t *, void *, ipc_callid_t, ipc_call_t *);
|
---|
47 | //static void remote_usb(device_t *, void *, ipc_callid_t, ipc_call_t *);
|
---|
48 |
|
---|
49 | /** Remote USB interface operations. */
|
---|
50 | static remote_iface_func_ptr_t remote_usb_iface_ops [] = {
|
---|
51 | &remote_usb_get_buffer,
|
---|
52 | &remote_usb_interrupt_out,
|
---|
53 | &remote_usb_interrupt_in
|
---|
54 | };
|
---|
55 |
|
---|
56 | /** Remote USB interface structure.
|
---|
57 | */
|
---|
58 | remote_iface_t remote_usb_iface = {
|
---|
59 | .method_count = sizeof(remote_usb_iface_ops) /
|
---|
60 | sizeof(remote_usb_iface_ops[0]),
|
---|
61 | .methods = remote_usb_iface_ops
|
---|
62 | };
|
---|
63 |
|
---|
64 | typedef struct {
|
---|
65 | ipc_callid_t caller;
|
---|
66 | void *buffer;
|
---|
67 | size_t size;
|
---|
68 | } async_transaction_t;
|
---|
69 |
|
---|
70 |
|
---|
71 | void remote_usb_get_buffer(device_t *device, void *iface,
|
---|
72 | ipc_callid_t callid, ipc_call_t *call)
|
---|
73 | {
|
---|
74 | ipcarg_t buffer_hash = IPC_GET_ARG1(*call);
|
---|
75 | async_transaction_t * trans = (async_transaction_t *)buffer_hash;
|
---|
76 | if (trans == NULL) {
|
---|
77 | ipc_answer_0(callid, ENOENT);
|
---|
78 | return;
|
---|
79 | }
|
---|
80 | if (trans->buffer == NULL) {
|
---|
81 | ipc_answer_0(callid, EINVAL);
|
---|
82 | free(trans);
|
---|
83 | return;
|
---|
84 | }
|
---|
85 |
|
---|
86 | ipc_callid_t cid;
|
---|
87 | size_t accepted_size;
|
---|
88 | if (!async_data_read_receive(&cid, &accepted_size)) {
|
---|
89 | ipc_answer_0(callid, EINVAL);
|
---|
90 | return;
|
---|
91 | }
|
---|
92 |
|
---|
93 | if (accepted_size > trans->size) {
|
---|
94 | accepted_size = trans->size;
|
---|
95 | }
|
---|
96 | async_data_read_finalize(callid, trans->buffer, accepted_size);
|
---|
97 |
|
---|
98 | ipc_answer_1(callid, EOK, accepted_size);
|
---|
99 |
|
---|
100 | free(trans->buffer);
|
---|
101 | free(trans);
|
---|
102 | }
|
---|
103 |
|
---|
104 |
|
---|
105 | static void callback_out(device_t *device,
|
---|
106 | usb_transaction_outcome_t outcome, void *arg)
|
---|
107 | {
|
---|
108 | async_transaction_t *trans = (async_transaction_t *)arg;
|
---|
109 |
|
---|
110 | // FIXME - answer according to outcome
|
---|
111 | ipc_answer_0(trans->caller, EOK);
|
---|
112 |
|
---|
113 | free(trans);
|
---|
114 | }
|
---|
115 |
|
---|
116 | static void callback_in(device_t *device,
|
---|
117 | usb_transaction_outcome_t outcome, size_t actual_size, void *arg)
|
---|
118 | {
|
---|
119 | async_transaction_t *trans = (async_transaction_t *)arg;
|
---|
120 |
|
---|
121 | // FIXME - answer according to outcome
|
---|
122 | ipc_answer_1(trans->caller, EOK, (ipcarg_t)trans);
|
---|
123 |
|
---|
124 | trans->size = actual_size;
|
---|
125 | }
|
---|
126 |
|
---|
127 | void remote_usb_interrupt_out(device_t *device, void *iface,
|
---|
128 | ipc_callid_t callid, ipc_call_t *call)
|
---|
129 | {
|
---|
130 | usb_iface_t *usb_iface = (usb_iface_t *) iface;
|
---|
131 |
|
---|
132 | size_t expected_len = IPC_GET_ARG3(*call);
|
---|
133 | usb_target_t target = {
|
---|
134 | .address = IPC_GET_ARG1(*call),
|
---|
135 | .endpoint = IPC_GET_ARG2(*call)
|
---|
136 | };
|
---|
137 |
|
---|
138 | size_t len = 0;
|
---|
139 | void *buffer = NULL;
|
---|
140 | if (expected_len > 0) {
|
---|
141 | int rc = async_data_write_accept(&buffer, false,
|
---|
142 | 1, USB_MAX_PAYLOAD_SIZE,
|
---|
143 | 0, &len);
|
---|
144 |
|
---|
145 | if (rc != EOK) {
|
---|
146 | ipc_answer_0(callid, rc);
|
---|
147 | return;
|
---|
148 | }
|
---|
149 | }
|
---|
150 |
|
---|
151 | if (!usb_iface->interrupt_out) {
|
---|
152 | ipc_answer_0(callid, ENOTSUP);
|
---|
153 | return;
|
---|
154 | }
|
---|
155 |
|
---|
156 | async_transaction_t *trans = malloc(sizeof(async_transaction_t));
|
---|
157 | trans->caller = callid;
|
---|
158 | trans->buffer = NULL;
|
---|
159 | trans->size = 0;
|
---|
160 |
|
---|
161 | int rc = usb_iface->interrupt_out(device, target, buffer, len,
|
---|
162 | callback_out, trans);
|
---|
163 |
|
---|
164 | if (rc != EOK) {
|
---|
165 | ipc_answer_0(callid, rc);
|
---|
166 | free(trans);
|
---|
167 | }
|
---|
168 | }
|
---|
169 |
|
---|
170 | void remote_usb_interrupt_in(device_t *device, void *iface,
|
---|
171 | ipc_callid_t callid, ipc_call_t *call)
|
---|
172 | {
|
---|
173 | usb_iface_t *usb_iface = (usb_iface_t *) iface;
|
---|
174 |
|
---|
175 | size_t len = IPC_GET_ARG3(*call);
|
---|
176 | usb_target_t target = {
|
---|
177 | .address = IPC_GET_ARG1(*call),
|
---|
178 | .endpoint = IPC_GET_ARG2(*call)
|
---|
179 | };
|
---|
180 |
|
---|
181 | if (!usb_iface->interrupt_in) {
|
---|
182 | ipc_answer_0(callid, ENOTSUP);
|
---|
183 | return;
|
---|
184 | }
|
---|
185 |
|
---|
186 | async_transaction_t *trans = malloc(sizeof(async_transaction_t));
|
---|
187 | trans->caller = callid;
|
---|
188 | trans->buffer = malloc(len);
|
---|
189 | trans->size = len;
|
---|
190 |
|
---|
191 | int rc = usb_iface->interrupt_in(device, target, trans->buffer, len,
|
---|
192 | callback_in, trans);
|
---|
193 |
|
---|
194 | if (rc != EOK) {
|
---|
195 | ipc_answer_0(callid, rc);
|
---|
196 | free(trans->buffer);
|
---|
197 | free(trans);
|
---|
198 | }
|
---|
199 | }
|
---|
200 |
|
---|
201 |
|
---|
202 | /**
|
---|
203 | * @}
|
---|
204 | */
|
---|