source: mainline/uspace/lib/drv/generic/remote_usbhc.c@ 365e29e2

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

usb: Use new target packing scheme for IPC.

  • Property mode set to 100644
File size: 13.4 KB
RevLine 
[91db50ac]1/*
[9753220]2 * Copyright (c) 2010-2011 Vojtech Horky
[91db50ac]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>
[eb1a2f4]37#include <assert.h>
[91db50ac]38
[cb59f787]39#include "usbhc_iface.h"
[eb1a2f4]40#include "ddf/driver.h"
[91db50ac]41
[1b22bd4]42#define USB_MAX_PAYLOAD_SIZE 1020
43
[eb1a2f4]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 *);
[eb2f7dd]46static void remote_usbhc_find_by_address(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
[eb1a2f4]47static void remote_usbhc_release_address(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
[b7d8fd9]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 *);
[1e647c7d]50static void remote_usbhc_control_write(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
51static void remote_usbhc_control_read(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
[ffe3fe1]52static void remote_usbhc_data_read(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
53static void remote_usbhc_data_write(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
[eb1a2f4]54//static void remote_usbhc(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
[91db50ac]55
[6edd494]56/** Remote USB host controller interface operations. */
[ffe3fe1]57static remote_iface_func_ptr_t remote_usbhc_iface_ops[] = {
58 [IPC_M_USBHC_REQUEST_ADDRESS] = remote_usbhc_request_address,
59 [IPC_M_USBHC_BIND_ADDRESS] = remote_usbhc_bind_address,
60 [IPC_M_USBHC_GET_HANDLE_BY_ADDRESS] = remote_usbhc_find_by_address,
61 [IPC_M_USBHC_RELEASE_ADDRESS] = remote_usbhc_release_address,
[6f04905]62
[1e647c7d]63 [IPC_M_USBHC_REGISTER_ENDPOINT] = remote_usbhc_register_endpoint,
64 [IPC_M_USBHC_UNREGISTER_ENDPOINT] = remote_usbhc_unregister_endpoint,
[0a46c41e]65
[ffe3fe1]66 [IPC_M_USBHC_CONTROL_WRITE] = remote_usbhc_control_write,
67 [IPC_M_USBHC_CONTROL_READ] = remote_usbhc_control_read,
[b7d8fd9]68
[ffe3fe1]69 [IPC_M_USBHC_DATA_READ] = remote_usbhc_data_read,
70 [IPC_M_USBHC_DATA_WRITE] = remote_usbhc_data_write,
[91db50ac]71};
72
[6edd494]73/** Remote USB host controller interface structure.
[91db50ac]74 */
[cb59f787]75remote_iface_t remote_usbhc_iface = {
76 .method_count = sizeof(remote_usbhc_iface_ops) /
77 sizeof(remote_usbhc_iface_ops[0]),
78 .methods = remote_usbhc_iface_ops
[91db50ac]79};
80
[1b22bd4]81typedef struct {
82 ipc_callid_t caller;
[0a6fa9f]83 ipc_callid_t data_caller;
[1b22bd4]84 void *buffer;
85 size_t size;
86} async_transaction_t;
[91db50ac]87
[93f8da1]88static void async_transaction_destroy(async_transaction_t *trans)
89{
90 if (trans == NULL) {
91 return;
92 }
93 if (trans->buffer != NULL) {
94 free(trans->buffer);
95 }
96
97 free(trans);
98}
99
100static async_transaction_t *async_transaction_create(ipc_callid_t caller)
101{
102 async_transaction_t *trans = malloc(sizeof(async_transaction_t));
103 if (trans == NULL) {
104 return NULL;
105 }
106
107 trans->caller = caller;
[0a6fa9f]108 trans->data_caller = 0;
[93f8da1]109 trans->buffer = NULL;
110 trans->size = 0;
111
112 return trans;
113}
114
[eb1a2f4]115void remote_usbhc_request_address(ddf_fun_t *fun, void *iface,
[6f04905]116 ipc_callid_t callid, ipc_call_t *call)
117{
118 usbhc_iface_t *usb_iface = (usbhc_iface_t *) iface;
119
120 if (!usb_iface->request_address) {
[17aca1c]121 async_answer_0(callid, ENOTSUP);
[6f04905]122 return;
123 }
[ffe3fe1]124
[fa48ebe]125 usb_speed_t speed = DEV_IPC_GET_ARG1(*call);
[6f04905]126
127 usb_address_t address;
[eb1a2f4]128 int rc = usb_iface->request_address(fun, speed, &address);
[6f04905]129 if (rc != EOK) {
[17aca1c]130 async_answer_0(callid, rc);
[6f04905]131 } else {
[17aca1c]132 async_answer_1(callid, EOK, (sysarg_t) address);
[6f04905]133 }
134}
135
[eb1a2f4]136void remote_usbhc_bind_address(ddf_fun_t *fun, void *iface,
[4689d40]137 ipc_callid_t callid, ipc_call_t *call)
138{
139 usbhc_iface_t *usb_iface = (usbhc_iface_t *) iface;
140
141 if (!usb_iface->bind_address) {
[17aca1c]142 async_answer_0(callid, ENOTSUP);
[4689d40]143 return;
144 }
145
[eac610e]146 usb_address_t address = (usb_address_t) DEV_IPC_GET_ARG1(*call);
147 devman_handle_t handle = (devman_handle_t) DEV_IPC_GET_ARG2(*call);
[4689d40]148
[eb1a2f4]149 int rc = usb_iface->bind_address(fun, address, handle);
[4689d40]150
[17aca1c]151 async_answer_0(callid, rc);
[4689d40]152}
153
[eb2f7dd]154void remote_usbhc_find_by_address(ddf_fun_t *fun, void *iface,
155 ipc_callid_t callid, ipc_call_t *call)
156{
157 usbhc_iface_t *usb_iface = (usbhc_iface_t *) iface;
158
159 if (!usb_iface->find_by_address) {
160 async_answer_0(callid, ENOTSUP);
161 return;
162 }
163
164 usb_address_t address = (usb_address_t) DEV_IPC_GET_ARG1(*call);
165 devman_handle_t handle;
166 int rc = usb_iface->find_by_address(fun, address, &handle);
167
168 if (rc == EOK) {
169 async_answer_1(callid, EOK, handle);
170 } else {
171 async_answer_0(callid, rc);
172 }
173}
174
[eb1a2f4]175void remote_usbhc_release_address(ddf_fun_t *fun, void *iface,
[6f04905]176 ipc_callid_t callid, ipc_call_t *call)
177{
178 usbhc_iface_t *usb_iface = (usbhc_iface_t *) iface;
179
180 if (!usb_iface->release_address) {
[17aca1c]181 async_answer_0(callid, ENOTSUP);
[6f04905]182 return;
183 }
184
[eac610e]185 usb_address_t address = (usb_address_t) DEV_IPC_GET_ARG1(*call);
[6f04905]186
[eb1a2f4]187 int rc = usb_iface->release_address(fun, address);
[6f04905]188
[17aca1c]189 async_answer_0(callid, rc);
[6f04905]190}
191
[1b22bd4]192
[eb1a2f4]193static void callback_out(ddf_fun_t *fun,
[daec5e04]194 int outcome, void *arg)
[1b22bd4]195{
196 async_transaction_t *trans = (async_transaction_t *)arg;
197
[17aca1c]198 async_answer_0(trans->caller, outcome);
[1b22bd4]199
[93f8da1]200 async_transaction_destroy(trans);
[1b22bd4]201}
202
[eb1a2f4]203static void callback_in(ddf_fun_t *fun,
[daec5e04]204 int outcome, size_t actual_size, void *arg)
[1b22bd4]205{
206 async_transaction_t *trans = (async_transaction_t *)arg;
207
[daec5e04]208 if (outcome != EOK) {
[17aca1c]209 async_answer_0(trans->caller, outcome);
[5842493]210 if (trans->data_caller) {
211 async_answer_0(trans->data_caller, EINTR);
212 }
[93f8da1]213 async_transaction_destroy(trans);
214 return;
215 }
[1b22bd4]216
217 trans->size = actual_size;
[0a6fa9f]218
219 if (trans->data_caller) {
220 async_data_read_finalize(trans->data_caller,
221 trans->buffer, actual_size);
222 }
223
[daec5e04]224 async_answer_0(trans->caller, EOK);
[1e64b250]225
226 async_transaction_destroy(trans);
[91db50ac]227}
228
[eb1a2f4]229void remote_usbhc_control_write(ddf_fun_t *fun, void *iface,
[9753220]230ipc_callid_t callid, ipc_call_t *call)
231{
232 usbhc_iface_t *usb_iface = (usbhc_iface_t *) iface;
233 assert(usb_iface != NULL);
234
[3a8370c]235 if (!usb_iface->write) {
[17aca1c]236 async_answer_0(callid, ENOTSUP);
[9753220]237 return;
238 }
239
[365e29e2]240 const usb_target_t target = { .packed = DEV_IPC_GET_ARG1(*call) };
241 size_t data_buffer_len = DEV_IPC_GET_ARG2(*call);
[9753220]242
243 int rc;
244
245 void *setup_packet = NULL;
246 void *data_buffer = NULL;
247 size_t setup_packet_len = 0;
248
249 rc = async_data_write_accept(&setup_packet, false,
250 1, USB_MAX_PAYLOAD_SIZE, 0, &setup_packet_len);
251 if (rc != EOK) {
[17aca1c]252 async_answer_0(callid, rc);
[9753220]253 return;
254 }
[3937bda]255
256 if (data_buffer_len > 0) {
257 rc = async_data_write_accept(&data_buffer, false,
258 1, USB_MAX_PAYLOAD_SIZE, 0, &data_buffer_len);
259 if (rc != EOK) {
260 async_answer_0(callid, rc);
261 free(setup_packet);
262 return;
263 }
[9753220]264 }
265
[93f8da1]266 async_transaction_t *trans = async_transaction_create(callid);
267 if (trans == NULL) {
[17aca1c]268 async_answer_0(callid, ENOMEM);
[93f8da1]269 free(setup_packet);
270 free(data_buffer);
271 return;
272 }
[9753220]273 trans->buffer = data_buffer;
274 trans->size = data_buffer_len;
275
[3a8370c]276 assert(setup_packet_len == 8);
277 uint64_t setup_buffer;
278 memcpy(&setup_buffer, setup_packet, 8);
279 free(setup_packet);
280 rc = usb_iface->write(fun, target, setup_buffer, trans->buffer,
281 trans->size, callback_out, trans);
[9753220]282
283 if (rc != EOK) {
[17aca1c]284 async_answer_0(callid, rc);
[93f8da1]285 async_transaction_destroy(trans);
[9753220]286 }
287}
288
289
[eb1a2f4]290void remote_usbhc_control_read(ddf_fun_t *fun, void *iface,
[9753220]291ipc_callid_t callid, ipc_call_t *call)
292{
293 usbhc_iface_t *usb_iface = (usbhc_iface_t *) iface;
294 assert(usb_iface != NULL);
295
[3a8370c]296 if (!usb_iface->read) {
[17aca1c]297 async_answer_0(callid, ENOTSUP);
[9753220]298 return;
299 }
300
[365e29e2]301 const usb_target_t target = { .packed = DEV_IPC_GET_ARG1(*call) };
[9753220]302
303 int rc;
304
305 void *setup_packet = NULL;
306 size_t setup_packet_len = 0;
[3937bda]307 size_t data_len = 0;
[9753220]308
309 rc = async_data_write_accept(&setup_packet, false,
310 1, USB_MAX_PAYLOAD_SIZE, 0, &setup_packet_len);
311 if (rc != EOK) {
[17aca1c]312 async_answer_0(callid, rc);
[9753220]313 return;
314 }
315
[0a6fa9f]316 ipc_callid_t data_callid;
317 if (!async_data_read_receive(&data_callid, &data_len)) {
318 async_answer_0(callid, EPARTY);
319 free(setup_packet);
320 return;
321 }
322
[93f8da1]323 async_transaction_t *trans = async_transaction_create(callid);
324 if (trans == NULL) {
[6985b4e]325 async_answer_0(data_callid, ENOMEM);
[17aca1c]326 async_answer_0(callid, ENOMEM);
[93f8da1]327 free(setup_packet);
328 return;
329 }
[0a6fa9f]330 trans->data_caller = data_callid;
[9753220]331 trans->size = data_len;
[93f8da1]332 trans->buffer = malloc(data_len);
333 if (trans->buffer == NULL) {
[6985b4e]334 async_answer_0(data_callid, ENOMEM);
[17aca1c]335 async_answer_0(callid, ENOMEM);
[93f8da1]336 async_transaction_destroy(trans);
337 return;
338 }
[9753220]339
[3a8370c]340 assert(setup_packet_len == 8);
341 uint64_t setup_buffer;
342 memcpy(&setup_buffer, setup_packet, 8);
343 free(setup_packet);
344 rc = usb_iface->read(fun, target, setup_buffer, trans->buffer,
345 trans->size, callback_in, trans);
[9753220]346 if (rc != EOK) {
[6985b4e]347 async_answer_0(data_callid, rc);
[17aca1c]348 async_answer_0(callid, rc);
[93f8da1]349 async_transaction_destroy(trans);
[9753220]350 }
351}
352
[a3dfb2e]353
[b7d8fd9]354void remote_usbhc_register_endpoint(ddf_fun_t *fun, void *iface,
355 ipc_callid_t callid, ipc_call_t *call)
356{
357 usbhc_iface_t *usb_iface = (usbhc_iface_t *) iface;
358
359 if (!usb_iface->register_endpoint) {
360 async_answer_0(callid, ENOTSUP);
361 return;
362 }
363
[1998bcd]364#define _INIT_FROM_HIGH_DATA2(type, var, arg_no) \
365 type var = (type) DEV_IPC_GET_ARG##arg_no(*call) / (1 << 16)
366#define _INIT_FROM_LOW_DATA2(type, var, arg_no) \
367 type var = (type) DEV_IPC_GET_ARG##arg_no(*call) % (1 << 16)
368#define _INIT_FROM_HIGH_DATA3(type, var, arg_no) \
369 type var = (type) DEV_IPC_GET_ARG##arg_no(*call) / (1 << 16)
370#define _INIT_FROM_MIDDLE_DATA3(type, var, arg_no) \
371 type var = (type) (DEV_IPC_GET_ARG##arg_no(*call) / (1 << 8)) % (1 << 8)
372#define _INIT_FROM_LOW_DATA3(type, var, arg_no) \
373 type var = (type) DEV_IPC_GET_ARG##arg_no(*call) % (1 << 8)
374
[365e29e2]375 const usb_target_t target = { .packed = DEV_IPC_GET_ARG1(*call) };
[1998bcd]376
377 _INIT_FROM_HIGH_DATA3(usb_speed_t, speed, 2);
378 _INIT_FROM_MIDDLE_DATA3(usb_transfer_type_t, transfer_type, 2);
379 _INIT_FROM_LOW_DATA3(usb_direction_t, direction, 2);
380
381 _INIT_FROM_HIGH_DATA2(size_t, max_packet_size, 3);
382 _INIT_FROM_LOW_DATA2(unsigned int, interval, 3);
383
384#undef _INIT_FROM_HIGH_DATA2
385#undef _INIT_FROM_LOW_DATA2
386#undef _INIT_FROM_HIGH_DATA3
387#undef _INIT_FROM_MIDDLE_DATA3
388#undef _INIT_FROM_LOW_DATA3
389
[365e29e2]390 int rc = usb_iface->register_endpoint(fun, target.address,
391 speed, target.endpoint,
[b7d8fd9]392 transfer_type, direction, max_packet_size, interval);
393
394 async_answer_0(callid, rc);
395}
396
397
398void remote_usbhc_unregister_endpoint(ddf_fun_t *fun, void *iface,
399 ipc_callid_t callid, ipc_call_t *call)
400{
401 usbhc_iface_t *usb_iface = (usbhc_iface_t *) iface;
402
403 if (!usb_iface->unregister_endpoint) {
404 async_answer_0(callid, ENOTSUP);
405 return;
406 }
407
408 usb_address_t address = (usb_address_t) DEV_IPC_GET_ARG1(*call);
409 usb_endpoint_t endpoint = (usb_endpoint_t) DEV_IPC_GET_ARG2(*call);
410 usb_direction_t direction = (usb_direction_t) DEV_IPC_GET_ARG3(*call);
411
412 int rc = usb_iface->unregister_endpoint(fun,
413 address, endpoint, direction);
414
415 async_answer_0(callid, rc);
416}
417
[ffe3fe1]418static void remote_usbhc_data_read(
419 ddf_fun_t *fun, void *iface, ipc_callid_t callid, ipc_call_t *call)
420{
421 assert(fun);
422 assert(iface);
423 assert(call);
424
425 const usbhc_iface_t *hc_iface = iface;
426
427 if (!hc_iface->read) {
428 async_answer_0(callid, ENOTSUP);
429 return;
430 }
431
[365e29e2]432 const usb_target_t target = { .packed = DEV_IPC_GET_ARG1(*call) };
[ffe3fe1]433
434 async_transaction_t *trans = async_transaction_create(callid);
435 if (trans == NULL) {
436 async_answer_0(callid, ENOMEM);
437 return;
438 }
439
440 if (!async_data_read_receive(&trans->data_caller, &trans->size)) {
441 async_answer_0(callid, EPARTY);
442 return;
443 }
444
445 trans->buffer = malloc(trans->size);
446 if (trans->buffer == NULL) {
447 async_answer_0(trans->data_caller, ENOMEM);
448 async_answer_0(callid, ENOMEM);
449 async_transaction_destroy(trans);
450 }
451
452 const int rc = hc_iface->read(
453 fun, target, 0, trans->buffer, trans->size, callback_in, trans);
454
455 if (rc != EOK) {
456 async_answer_0(trans->data_caller, rc);
457 async_answer_0(callid, rc);
458 async_transaction_destroy(trans);
459 }
460}
461
462static void remote_usbhc_data_write(
463 ddf_fun_t *fun, void *iface, ipc_callid_t callid, ipc_call_t *call)
464{
465 assert(fun);
466 assert(iface);
467 assert(call);
468
469 const usbhc_iface_t *hc_iface = iface;
470
471 if (!hc_iface->write) {
472 async_answer_0(callid, ENOTSUP);
473 return;
474 }
475
[365e29e2]476 const usb_target_t target = { .packed = DEV_IPC_GET_ARG1(*call) };
[ffe3fe1]477
478 async_transaction_t *trans = async_transaction_create(callid);
479 if (trans == NULL) {
480 async_answer_0(callid, ENOMEM);
481 return;
482 }
483
484 int rc = async_data_write_accept(&trans->buffer, false,
485 1, USB_MAX_PAYLOAD_SIZE,
486 0, &trans->size);
487
488 if (rc != EOK) {
489 async_answer_0(callid, rc);
490 async_transaction_destroy(trans);
491 return;
492 }
493
494 rc = hc_iface->write(
495 fun, target, 0, trans->buffer, trans->size, callback_out, trans);
496
497 if (rc != EOK) {
498 async_answer_0(callid, rc);
499 async_transaction_destroy(trans);
500 }
501}
502
[1b22bd4]503
[91db50ac]504/**
505 * @}
506 */
Note: See TracBrowser for help on using the repository browser.