source: mainline/uspace/lib/drv/generic/remote_usbhc.c@ 1b17e37

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

usb: Allow devices to request explicit address.

  • Property mode set to 100644
File size: 10.6 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
39#include "usbhc_iface.h"
40#include "ddf/driver.h"
41
42#define USB_MAX_PAYLOAD_SIZE 1020
43
44static void remote_usbhc_request_address(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
45static void remote_usbhc_bind_address(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
46static void remote_usbhc_find_by_address(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
47static void remote_usbhc_release_address(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
48static void remote_usbhc_register_endpoint(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
49static void remote_usbhc_unregister_endpoint(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
50static void remote_usbhc_read(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
51static void remote_usbhc_write(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
52//static void remote_usbhc(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
53
54/** Remote USB host controller interface operations. */
55static remote_iface_func_ptr_t remote_usbhc_iface_ops[] = {
56 [IPC_M_USBHC_REQUEST_ADDRESS] = remote_usbhc_request_address,
57 [IPC_M_USBHC_BIND_ADDRESS] = remote_usbhc_bind_address,
58 [IPC_M_USBHC_GET_HANDLE_BY_ADDRESS] = remote_usbhc_find_by_address,
59 [IPC_M_USBHC_RELEASE_ADDRESS] = remote_usbhc_release_address,
60
61 [IPC_M_USBHC_REGISTER_ENDPOINT] = remote_usbhc_register_endpoint,
62 [IPC_M_USBHC_UNREGISTER_ENDPOINT] = remote_usbhc_unregister_endpoint,
63
64 [IPC_M_USBHC_READ] = remote_usbhc_read,
65 [IPC_M_USBHC_WRITE] = remote_usbhc_write,
66};
67
68/** Remote USB host controller interface structure.
69 */
70remote_iface_t remote_usbhc_iface = {
71 .method_count = sizeof(remote_usbhc_iface_ops) /
72 sizeof(remote_usbhc_iface_ops[0]),
73 .methods = remote_usbhc_iface_ops
74};
75
76typedef struct {
77 ipc_callid_t caller;
78 ipc_callid_t data_caller;
79 void *buffer;
80 size_t size;
81} async_transaction_t;
82
83static void async_transaction_destroy(async_transaction_t *trans)
84{
85 if (trans == NULL) {
86 return;
87 }
88 if (trans->buffer != NULL) {
89 free(trans->buffer);
90 }
91
92 free(trans);
93}
94
95static async_transaction_t *async_transaction_create(ipc_callid_t caller)
96{
97 async_transaction_t *trans = malloc(sizeof(async_transaction_t));
98 if (trans == NULL) {
99 return NULL;
100 }
101
102 trans->caller = caller;
103 trans->data_caller = 0;
104 trans->buffer = NULL;
105 trans->size = 0;
106
107 return trans;
108}
109
110void remote_usbhc_request_address(ddf_fun_t *fun, void *iface,
111 ipc_callid_t callid, ipc_call_t *call)
112{
113 usbhc_iface_t *usb_iface = (usbhc_iface_t *) iface;
114
115 if (!usb_iface->request_address) {
116 async_answer_0(callid, ENOTSUP);
117 return;
118 }
119
120 usb_address_t address = DEV_IPC_GET_ARG1(*call);
121 const bool strict = DEV_IPC_GET_ARG2(*call);
122 const usb_speed_t speed = DEV_IPC_GET_ARG3(*call);
123
124 const int rc = usb_iface->request_address(fun, &address, strict, speed);
125 if (rc != EOK) {
126 async_answer_0(callid, rc);
127 } else {
128 async_answer_1(callid, EOK, (sysarg_t) address);
129 }
130}
131
132void remote_usbhc_bind_address(ddf_fun_t *fun, void *iface,
133 ipc_callid_t callid, ipc_call_t *call)
134{
135 usbhc_iface_t *usb_iface = (usbhc_iface_t *) iface;
136
137 if (!usb_iface->bind_address) {
138 async_answer_0(callid, ENOTSUP);
139 return;
140 }
141
142 usb_address_t address = (usb_address_t) DEV_IPC_GET_ARG1(*call);
143 devman_handle_t handle = (devman_handle_t) DEV_IPC_GET_ARG2(*call);
144
145 int rc = usb_iface->bind_address(fun, address, handle);
146
147 async_answer_0(callid, rc);
148}
149
150void remote_usbhc_find_by_address(ddf_fun_t *fun, void *iface,
151 ipc_callid_t callid, ipc_call_t *call)
152{
153 usbhc_iface_t *usb_iface = (usbhc_iface_t *) iface;
154
155 if (!usb_iface->find_by_address) {
156 async_answer_0(callid, ENOTSUP);
157 return;
158 }
159
160 usb_address_t address = (usb_address_t) DEV_IPC_GET_ARG1(*call);
161 devman_handle_t handle;
162 int rc = usb_iface->find_by_address(fun, address, &handle);
163
164 if (rc == EOK) {
165 async_answer_1(callid, EOK, handle);
166 } else {
167 async_answer_0(callid, rc);
168 }
169}
170
171void remote_usbhc_release_address(ddf_fun_t *fun, void *iface,
172 ipc_callid_t callid, ipc_call_t *call)
173{
174 usbhc_iface_t *usb_iface = (usbhc_iface_t *) iface;
175
176 if (!usb_iface->release_address) {
177 async_answer_0(callid, ENOTSUP);
178 return;
179 }
180
181 usb_address_t address = (usb_address_t) DEV_IPC_GET_ARG1(*call);
182
183 int rc = usb_iface->release_address(fun, address);
184
185 async_answer_0(callid, rc);
186}
187
188
189static void callback_out(ddf_fun_t *fun,
190 int outcome, void *arg)
191{
192 async_transaction_t *trans = (async_transaction_t *)arg;
193
194 async_answer_0(trans->caller, outcome);
195
196 async_transaction_destroy(trans);
197}
198
199static void callback_in(ddf_fun_t *fun,
200 int outcome, size_t actual_size, void *arg)
201{
202 async_transaction_t *trans = (async_transaction_t *)arg;
203
204 if (outcome != EOK) {
205 async_answer_0(trans->caller, outcome);
206 if (trans->data_caller) {
207 async_answer_0(trans->data_caller, EINTR);
208 }
209 async_transaction_destroy(trans);
210 return;
211 }
212
213 trans->size = actual_size;
214
215 if (trans->data_caller) {
216 async_data_read_finalize(trans->data_caller,
217 trans->buffer, actual_size);
218 }
219
220 async_answer_0(trans->caller, EOK);
221
222 async_transaction_destroy(trans);
223}
224
225void remote_usbhc_register_endpoint(ddf_fun_t *fun, void *iface,
226 ipc_callid_t callid, ipc_call_t *call)
227{
228 usbhc_iface_t *usb_iface = (usbhc_iface_t *) iface;
229
230 if (!usb_iface->register_endpoint) {
231 async_answer_0(callid, ENOTSUP);
232 return;
233 }
234
235#define _INIT_FROM_HIGH_DATA2(type, var, arg_no) \
236 type var = (type) DEV_IPC_GET_ARG##arg_no(*call) / (1 << 16)
237#define _INIT_FROM_LOW_DATA2(type, var, arg_no) \
238 type var = (type) DEV_IPC_GET_ARG##arg_no(*call) % (1 << 16)
239#define _INIT_FROM_HIGH_DATA3(type, var, arg_no) \
240 type var = (type) DEV_IPC_GET_ARG##arg_no(*call) / (1 << 16)
241#define _INIT_FROM_MIDDLE_DATA3(type, var, arg_no) \
242 type var = (type) (DEV_IPC_GET_ARG##arg_no(*call) / (1 << 8)) % (1 << 8)
243#define _INIT_FROM_LOW_DATA3(type, var, arg_no) \
244 type var = (type) DEV_IPC_GET_ARG##arg_no(*call) % (1 << 8)
245
246 const usb_target_t target = { .packed = DEV_IPC_GET_ARG1(*call) };
247
248 _INIT_FROM_HIGH_DATA3(usb_speed_t, speed, 2);
249 _INIT_FROM_MIDDLE_DATA3(usb_transfer_type_t, transfer_type, 2);
250 _INIT_FROM_LOW_DATA3(usb_direction_t, direction, 2);
251
252 _INIT_FROM_HIGH_DATA2(size_t, max_packet_size, 3);
253 _INIT_FROM_LOW_DATA2(unsigned int, interval, 3);
254
255#undef _INIT_FROM_HIGH_DATA2
256#undef _INIT_FROM_LOW_DATA2
257#undef _INIT_FROM_HIGH_DATA3
258#undef _INIT_FROM_MIDDLE_DATA3
259#undef _INIT_FROM_LOW_DATA3
260
261 int rc = usb_iface->register_endpoint(fun, target.address, speed,
262 target.endpoint, transfer_type, direction, max_packet_size, interval);
263
264 async_answer_0(callid, rc);
265}
266
267void remote_usbhc_unregister_endpoint(ddf_fun_t *fun, void *iface,
268 ipc_callid_t callid, ipc_call_t *call)
269{
270 usbhc_iface_t *usb_iface = (usbhc_iface_t *) iface;
271
272 if (!usb_iface->unregister_endpoint) {
273 async_answer_0(callid, ENOTSUP);
274 return;
275 }
276
277 usb_address_t address = (usb_address_t) DEV_IPC_GET_ARG1(*call);
278 usb_endpoint_t endpoint = (usb_endpoint_t) DEV_IPC_GET_ARG2(*call);
279 usb_direction_t direction = (usb_direction_t) DEV_IPC_GET_ARG3(*call);
280
281 int rc = usb_iface->unregister_endpoint(fun,
282 address, endpoint, direction);
283
284 async_answer_0(callid, rc);
285}
286
287void remote_usbhc_read(
288 ddf_fun_t *fun, void *iface, ipc_callid_t callid, ipc_call_t *call)
289{
290 assert(fun);
291 assert(iface);
292 assert(call);
293
294 const usbhc_iface_t *hc_iface = iface;
295
296 if (!hc_iface->read) {
297 async_answer_0(callid, ENOTSUP);
298 return;
299 }
300
301 const usb_target_t target = { .packed = DEV_IPC_GET_ARG1(*call) };
302 const uint64_t setup =
303 ((uint64_t)DEV_IPC_GET_ARG2(*call)) |
304 (((uint64_t)DEV_IPC_GET_ARG3(*call)) << 32);
305
306 async_transaction_t *trans = async_transaction_create(callid);
307 if (trans == NULL) {
308 async_answer_0(callid, ENOMEM);
309 return;
310 }
311
312 if (!async_data_read_receive(&trans->data_caller, &trans->size)) {
313 async_answer_0(callid, EPARTY);
314 return;
315 }
316
317 trans->buffer = malloc(trans->size);
318 if (trans->buffer == NULL) {
319 async_answer_0(trans->data_caller, ENOMEM);
320 async_answer_0(callid, ENOMEM);
321 async_transaction_destroy(trans);
322 }
323
324 const int rc = hc_iface->read(
325 fun, target, setup, trans->buffer, trans->size, callback_in, trans);
326
327 if (rc != EOK) {
328 async_answer_0(trans->data_caller, rc);
329 async_answer_0(callid, rc);
330 async_transaction_destroy(trans);
331 }
332}
333
334void remote_usbhc_write(
335 ddf_fun_t *fun, void *iface, ipc_callid_t callid, ipc_call_t *call)
336{
337 assert(fun);
338 assert(iface);
339 assert(call);
340
341 const usbhc_iface_t *hc_iface = iface;
342
343 if (!hc_iface->write) {
344 async_answer_0(callid, ENOTSUP);
345 return;
346 }
347
348 const usb_target_t target = { .packed = DEV_IPC_GET_ARG1(*call) };
349 const size_t data_buffer_len = DEV_IPC_GET_ARG2(*call);
350 const uint64_t setup =
351 ((uint64_t)DEV_IPC_GET_ARG3(*call)) |
352 (((uint64_t)DEV_IPC_GET_ARG4(*call)) << 32);
353
354 async_transaction_t *trans = async_transaction_create(callid);
355 if (trans == NULL) {
356 async_answer_0(callid, ENOMEM);
357 return;
358 }
359
360 if (data_buffer_len > 0) {
361 int rc = async_data_write_accept(&trans->buffer, false,
362 1, USB_MAX_PAYLOAD_SIZE,
363 0, &trans->size);
364
365 if (rc != EOK) {
366 async_answer_0(callid, rc);
367 async_transaction_destroy(trans);
368 return;
369 }
370 }
371
372 int rc = hc_iface->write(
373 fun, target, setup, trans->buffer, trans->size, callback_out, trans);
374
375 if (rc != EOK) {
376 async_answer_0(callid, rc);
377 async_transaction_destroy(trans);
378 }
379}
380
381
382/**
383 * @}
384 */
Note: See TracBrowser for help on using the repository browser.