source: mainline/uspace/lib/drv/generic/remote_usb.c@ 5514cf7

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

libdrv: Remove old hc handle based functions

  • Property mode set to 100644
File size: 16.6 KB
Line 
1/*
2 * Copyright (c) 2010 Vojtech Horky
3 * Copyright (c) 2011 Jan Vesely
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/** @addtogroup libdrv
31 * @{
32 */
33/** @file
34 */
35
36#include <async.h>
37#include <macros.h>
38#include <errno.h>
39#include <devman.h>
40
41#include "usb_iface.h"
42#include "ddf/driver.h"
43
44
45usb_dev_session_t *usb_dev_connect(devman_handle_t handle)
46{
47 return devman_device_connect(EXCHANGE_PARALLEL, handle, IPC_FLAG_BLOCKING);
48}
49
50usb_dev_session_t *usb_dev_connect_to_self(ddf_dev_t *dev)
51{
52 // TODO All usb requests are atomic so this is safe,
53 // it will need to change once USING EXCHANGE PARALLEL is safe with
54 // devman_parent_device_connect
55 return devman_parent_device_connect(EXCHANGE_ATOMIC,
56 ddf_dev_get_handle(dev), IPC_FLAG_BLOCKING);
57}
58
59void usb_dev_disconnect(usb_dev_session_t *sess)
60{
61 if (sess)
62 async_hangup(sess);
63}
64
65typedef enum {
66 IPC_M_USB_GET_MY_INTERFACE,
67 IPC_M_USB_GET_DEVICE_HANDLE,
68 IPC_M_USB_RESERVE_DEFAULT_ADDRESS,
69 IPC_M_USB_RELEASE_DEFAULT_ADDRESS,
70 IPC_M_USB_DEVICE_ENUMERATE,
71 IPC_M_USB_DEVICE_REMOVE,
72 IPC_M_USB_REGISTER_ENDPOINT,
73 IPC_M_USB_UNREGISTER_ENDPOINT,
74 IPC_M_USB_READ,
75 IPC_M_USB_WRITE,
76} usb_iface_funcs_t;
77
78/** Tell interface number given device can use.
79 * @param[in] exch IPC communication exchange
80 * @param[in] handle Id of the device
81 * @param[out] usb_iface Assigned USB interface
82 * @return Error code.
83 */
84int usb_get_my_interface(async_exch_t *exch, int *usb_iface)
85{
86 if (!exch)
87 return EBADMEM;
88 sysarg_t iface_no;
89 const int ret = async_req_1_1(exch, DEV_IFACE_ID(USB_DEV_IFACE),
90 IPC_M_USB_GET_MY_INTERFACE, &iface_no);
91 if (ret == EOK && usb_iface)
92 *usb_iface = (int)iface_no;
93 return ret;
94}
95
96/** Tell devman handle of the usb device function.
97 * @param[in] exch IPC communication exchange
98 * @param[out] handle devman handle of the HC used by the target device.
99 * @return Error code.
100 */
101int usb_get_device_handle(async_exch_t *exch, devman_handle_t *handle)
102{
103 devman_handle_t h = 0;
104 const int ret = async_req_1_1(exch, DEV_IFACE_ID(USB_DEV_IFACE),
105 IPC_M_USB_GET_DEVICE_HANDLE, &h);
106 if (ret == EOK && handle)
107 *handle = (devman_handle_t)h;
108 return ret;
109}
110
111/** Reserve default USB address.
112 * @param[in] exch IPC communication exchange
113 * @param[in] speed Communication speed of the newly attached device
114 * @return Error code.
115 */
116int usb_reserve_default_address(async_exch_t *exch, usb_speed_t speed)
117{
118 if (!exch)
119 return EBADMEM;
120 return async_req_2_0(exch, DEV_IFACE_ID(USB_DEV_IFACE),
121 IPC_M_USB_RESERVE_DEFAULT_ADDRESS, speed);
122}
123
124/** Release default USB address.
125 * @param[in] exch IPC communication exchange
126 * @return Error code.
127 */
128int usb_release_default_address(async_exch_t *exch)
129{
130 if (!exch)
131 return EBADMEM;
132 return async_req_1_0(exch, DEV_IFACE_ID(USB_DEV_IFACE),
133 IPC_M_USB_RELEASE_DEFAULT_ADDRESS);
134}
135
136/** Trigger USB device enumeration
137 * @param[in] exch IPC communication exchange
138 * @param[out] handle Identifier of the newly added device (if successful)
139 * @return Error code.
140 */
141int usb_device_enumerate(async_exch_t *exch, usb_device_handle_t *handle)
142{
143 if (!exch || !handle)
144 return EBADMEM;
145 sysarg_t h;
146 const int ret = async_req_1_1(exch, DEV_IFACE_ID(USB_DEV_IFACE),
147 IPC_M_USB_DEVICE_ENUMERATE, &h);
148 if (ret == EOK)
149 *handle = (usb_device_handle_t)h;
150 return ret;
151}
152
153/** Trigger USB device enumeration
154 * @param[in] exch IPC communication exchange
155 * @param[in] handle Identifier of the device
156 * @return Error code.
157 */
158int usb_device_remove(async_exch_t *exch, usb_device_handle_t handle)
159{
160 if (!exch)
161 return EBADMEM;
162 return async_req_2_0(exch, DEV_IFACE_ID(USB_DEV_IFACE),
163 IPC_M_USB_DEVICE_REMOVE, handle);
164}
165
166int usb_register_endpoint(async_exch_t *exch, usb_endpoint_t endpoint,
167 usb_transfer_type_t type, usb_direction_t direction,
168 size_t mps, unsigned interval)
169{
170 if (!exch)
171 return EBADMEM;
172#define _PACK2(high, low) (((high & 0xffff) << 16) | (low & 0xffff))
173
174 return async_req_4_0(exch, DEV_IFACE_ID(USB_DEV_IFACE),
175 IPC_M_USB_REGISTER_ENDPOINT, endpoint,
176 _PACK2(type, direction), _PACK2(mps, interval));
177
178#undef _PACK2
179}
180
181int usb_unregister_endpoint(async_exch_t *exch, usb_endpoint_t endpoint,
182 usb_direction_t direction)
183{
184 if (!exch)
185 return EBADMEM;
186 return async_req_3_0(exch, DEV_IFACE_ID(USB_DEV_IFACE),
187 IPC_M_USB_UNREGISTER_ENDPOINT, endpoint, direction);
188}
189
190int usb_read(async_exch_t *exch, usb_endpoint_t endpoint, uint64_t setup,
191 void *data, size_t size, size_t *rec_size)
192{
193 if (!exch)
194 return EBADMEM;
195
196 if (size == 0 && setup == 0)
197 return EOK;
198
199 /* Make call identifying target USB device and type of transfer. */
200 aid_t opening_request = async_send_4(exch,
201 DEV_IFACE_ID(USB_DEV_IFACE), IPC_M_USB_READ, endpoint,
202 (setup & UINT32_MAX), (setup >> 32), NULL);
203
204 if (opening_request == 0) {
205 return ENOMEM;
206 }
207
208 /* Retrieve the data. */
209 ipc_call_t data_request_call;
210 aid_t data_request =
211 async_data_read(exch, data, size, &data_request_call);
212
213 if (data_request == 0) {
214 // FIXME: How to let the other side know that we want to abort?
215 async_forget(opening_request);
216 return ENOMEM;
217 }
218
219 /* Wait for the answer. */
220 sysarg_t data_request_rc;
221 sysarg_t opening_request_rc;
222 async_wait_for(data_request, &data_request_rc);
223 async_wait_for(opening_request, &opening_request_rc);
224
225 if (data_request_rc != EOK) {
226 /* Prefer the return code of the opening request. */
227 if (opening_request_rc != EOK) {
228 return (int) opening_request_rc;
229 } else {
230 return (int) data_request_rc;
231 }
232 }
233 if (opening_request_rc != EOK) {
234 return (int) opening_request_rc;
235 }
236
237 *rec_size = IPC_GET_ARG2(data_request_call);
238 return EOK;
239}
240
241int usb_write(async_exch_t *exch, usb_endpoint_t endpoint, uint64_t setup,
242 const void *data, size_t size)
243{
244 if (!exch)
245 return EBADMEM;
246
247 if (size == 0 && setup == 0)
248 return EOK;
249
250 aid_t opening_request = async_send_5(exch,
251 DEV_IFACE_ID(USB_DEV_IFACE), IPC_M_USB_WRITE, endpoint, size,
252 (setup & UINT32_MAX), (setup >> 32), NULL);
253
254 if (opening_request == 0) {
255 return ENOMEM;
256 }
257
258 /* Send the data if any. */
259 if (size > 0) {
260 const int ret = async_data_write_start(exch, data, size);
261 if (ret != EOK) {
262 async_forget(opening_request);
263 return ret;
264 }
265 }
266
267 /* Wait for the answer. */
268 sysarg_t opening_request_rc;
269 async_wait_for(opening_request, &opening_request_rc);
270
271 return (int) opening_request_rc;
272}
273
274static void remote_usb_get_my_interface(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
275static void remote_usb_get_device_handle(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
276static void remote_usb_reserve_default_address(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
277static void remote_usb_release_default_address(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
278static void remote_usb_device_enumerate(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
279static void remote_usb_device_remove(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
280static void remote_usb_register_endpoint(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
281static void remote_usb_unregister_endpoint(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
282static void remote_usb_read(ddf_fun_t *fun, void *iface, ipc_callid_t callid, ipc_call_t *call);
283static void remote_usb_write(ddf_fun_t *fun, void *iface, ipc_callid_t callid, ipc_call_t *call);
284
285/** Remote USB interface operations. */
286static remote_iface_func_ptr_t remote_usb_iface_ops [] = {
287 [IPC_M_USB_GET_MY_INTERFACE] = remote_usb_get_my_interface,
288 [IPC_M_USB_GET_DEVICE_HANDLE] = remote_usb_get_device_handle,
289 [IPC_M_USB_RESERVE_DEFAULT_ADDRESS] = remote_usb_reserve_default_address,
290 [IPC_M_USB_RELEASE_DEFAULT_ADDRESS] = remote_usb_release_default_address,
291 [IPC_M_USB_DEVICE_ENUMERATE] = remote_usb_device_enumerate,
292 [IPC_M_USB_DEVICE_REMOVE] = remote_usb_device_remove,
293 [IPC_M_USB_REGISTER_ENDPOINT] = remote_usb_register_endpoint,
294 [IPC_M_USB_UNREGISTER_ENDPOINT] = remote_usb_unregister_endpoint,
295 [IPC_M_USB_READ] = remote_usb_read,
296 [IPC_M_USB_WRITE] = remote_usb_write,
297};
298
299/** Remote USB interface structure.
300 */
301remote_iface_t remote_usb_iface = {
302 .method_count = ARRAY_SIZE(remote_usb_iface_ops),
303 .methods = remote_usb_iface_ops,
304};
305
306void remote_usb_get_my_interface(ddf_fun_t *fun, void *iface,
307 ipc_callid_t callid, ipc_call_t *call)
308{
309 const usb_iface_t *usb_iface = (usb_iface_t *) iface;
310
311 if (usb_iface->get_my_interface == NULL) {
312 async_answer_0(callid, ENOTSUP);
313 return;
314 }
315
316 int iface_no;
317 const int ret = usb_iface->get_my_interface(fun, &iface_no);
318 if (ret != EOK) {
319 async_answer_0(callid, ret);
320 } else {
321 async_answer_1(callid, EOK, iface_no);
322 }
323}
324
325void remote_usb_get_device_handle(ddf_fun_t *fun, void *iface,
326 ipc_callid_t callid, ipc_call_t *call)
327{
328 const usb_iface_t *usb_iface = (usb_iface_t *) iface;
329
330 if (usb_iface->get_device_handle == NULL) {
331 async_answer_0(callid, ENOTSUP);
332 return;
333 }
334
335 devman_handle_t handle;
336 const int ret = usb_iface->get_device_handle(fun, &handle);
337 if (ret != EOK) {
338 async_answer_0(callid, ret);
339 }
340
341 async_answer_1(callid, EOK, (sysarg_t) handle);
342}
343
344void remote_usb_reserve_default_address(ddf_fun_t *fun, void *iface,
345 ipc_callid_t callid, ipc_call_t *call)
346{
347 const usb_iface_t *usb_iface = (usb_iface_t *) iface;
348
349 if (usb_iface->reserve_default_address == NULL) {
350 async_answer_0(callid, ENOTSUP);
351 return;
352 }
353
354 usb_speed_t speed = DEV_IPC_GET_ARG1(*call);
355 const int ret = usb_iface->reserve_default_address(fun, speed);
356 async_answer_0(callid, ret);
357}
358
359void remote_usb_release_default_address(ddf_fun_t *fun, void *iface,
360 ipc_callid_t callid, ipc_call_t *call)
361{
362 const usb_iface_t *usb_iface = (usb_iface_t *) iface;
363
364 if (usb_iface->release_default_address == NULL) {
365 async_answer_0(callid, ENOTSUP);
366 return;
367 }
368
369 const int ret = usb_iface->release_default_address(fun);
370 async_answer_0(callid, ret);
371}
372
373static void remote_usb_device_enumerate(ddf_fun_t *fun, void *iface,
374 ipc_callid_t callid, ipc_call_t *call)
375{
376 const usb_iface_t *usb_iface = (usb_iface_t *) iface;
377
378 if (usb_iface->device_enumerate == NULL) {
379 async_answer_0(callid, ENOTSUP);
380 return;
381 }
382
383 usb_device_handle_t handle = 0;
384 const int ret = usb_iface->device_enumerate(fun, &handle);
385 if (ret != EOK) {
386 async_answer_0(callid, ret);
387 }
388
389 async_answer_1(callid, EOK, (sysarg_t) handle);
390}
391
392static void remote_usb_device_remove(ddf_fun_t *fun, void *iface,
393 ipc_callid_t callid, ipc_call_t *call)
394{
395 const usb_iface_t *usb_iface = (usb_iface_t *) iface;
396
397 if (usb_iface->device_remove == NULL) {
398 async_answer_0(callid, ENOTSUP);
399 return;
400 }
401
402 usb_device_handle_t handle = DEV_IPC_GET_ARG1(*call);
403 const int ret = usb_iface->device_remove(fun, handle);
404 async_answer_0(callid, ret);
405}
406
407static void remote_usb_register_endpoint(ddf_fun_t *fun, void *iface,
408 ipc_callid_t callid, ipc_call_t *call)
409{
410 usb_iface_t *usb_iface = (usb_iface_t *) iface;
411
412 if (!usb_iface->register_endpoint) {
413 async_answer_0(callid, ENOTSUP);
414 return;
415 }
416
417#define _INIT_FROM_HIGH_DATA2(type, var, arg_no) \
418 type var = (type) (DEV_IPC_GET_ARG##arg_no(*call) >> 16)
419#define _INIT_FROM_LOW_DATA2(type, var, arg_no) \
420 type var = (type) (DEV_IPC_GET_ARG##arg_no(*call) & 0xffff)
421
422 const usb_endpoint_t endpoint = DEV_IPC_GET_ARG1(*call);
423
424 _INIT_FROM_HIGH_DATA2(usb_transfer_type_t, transfer_type, 2);
425 _INIT_FROM_LOW_DATA2(usb_direction_t, direction, 2);
426
427 _INIT_FROM_HIGH_DATA2(size_t, max_packet_size, 3);
428 _INIT_FROM_LOW_DATA2(unsigned int, interval, 3);
429
430#undef _INIT_FROM_HIGH_DATA2
431#undef _INIT_FROM_LOW_DATA2
432
433 const int ret = usb_iface->register_endpoint(fun, endpoint,
434 transfer_type, direction, max_packet_size, interval);
435
436 async_answer_0(callid, ret);
437}
438
439static void remote_usb_unregister_endpoint(ddf_fun_t *fun, void *iface,
440 ipc_callid_t callid, ipc_call_t *call)
441{
442 usb_iface_t *usb_iface = (usb_iface_t *) iface;
443
444 if (!usb_iface->unregister_endpoint) {
445 async_answer_0(callid, ENOTSUP);
446 return;
447 }
448
449 usb_endpoint_t endpoint = (usb_endpoint_t) DEV_IPC_GET_ARG1(*call);
450 usb_direction_t direction = (usb_direction_t) DEV_IPC_GET_ARG2(*call);
451
452 int rc = usb_iface->unregister_endpoint(fun, endpoint, direction);
453
454 async_answer_0(callid, rc);
455}
456
457typedef struct {
458 ipc_callid_t caller;
459 ipc_callid_t data_caller;
460 void *buffer;
461} async_transaction_t;
462
463static void async_transaction_destroy(async_transaction_t *trans)
464{
465 if (trans == NULL) {
466 return;
467 }
468 if (trans->buffer != NULL) {
469 free(trans->buffer);
470 }
471
472 free(trans);
473}
474
475static async_transaction_t *async_transaction_create(ipc_callid_t caller)
476{
477 async_transaction_t *trans = malloc(sizeof(async_transaction_t));
478 if (trans == NULL) {
479 return NULL;
480 }
481
482 trans->caller = caller;
483 trans->data_caller = 0;
484 trans->buffer = NULL;
485
486 return trans;
487}
488
489static void callback_out(int outcome, void *arg)
490{
491 async_transaction_t *trans = arg;
492
493 async_answer_0(trans->caller, outcome);
494
495 async_transaction_destroy(trans);
496}
497
498static void callback_in(int outcome, size_t actual_size, void *arg)
499{
500 async_transaction_t *trans = (async_transaction_t *)arg;
501
502 if (outcome != EOK) {
503 async_answer_0(trans->caller, outcome);
504 if (trans->data_caller) {
505 async_answer_0(trans->data_caller, EINTR);
506 }
507 async_transaction_destroy(trans);
508 return;
509 }
510
511 if (trans->data_caller) {
512 async_data_read_finalize(trans->data_caller,
513 trans->buffer, actual_size);
514 }
515
516 async_answer_0(trans->caller, EOK);
517
518 async_transaction_destroy(trans);
519}
520
521void remote_usb_read(
522 ddf_fun_t *fun, void *iface, ipc_callid_t callid, ipc_call_t *call)
523{
524 assert(fun);
525 assert(iface);
526 assert(call);
527
528 const usb_iface_t *usb_iface = iface;
529
530 if (!usb_iface->read) {
531 async_answer_0(callid, ENOTSUP);
532 return;
533 }
534
535 const usb_endpoint_t ep = DEV_IPC_GET_ARG1(*call);
536 const uint64_t setup =
537 ((uint64_t)DEV_IPC_GET_ARG2(*call)) |
538 (((uint64_t)DEV_IPC_GET_ARG3(*call)) << 32);
539
540 async_transaction_t *trans = async_transaction_create(callid);
541 if (trans == NULL) {
542 async_answer_0(callid, ENOMEM);
543 return;
544 }
545
546 size_t size = 0;
547 if (!async_data_read_receive(&trans->data_caller, &size)) {
548 async_answer_0(callid, EPARTY);
549 return;
550 }
551
552 trans->buffer = malloc(size);
553 if (trans->buffer == NULL) {
554 async_answer_0(trans->data_caller, ENOMEM);
555 async_answer_0(callid, ENOMEM);
556 async_transaction_destroy(trans);
557 }
558
559 const int rc = usb_iface->read(
560 fun, ep, setup, trans->buffer, size, callback_in, trans);
561
562 if (rc != EOK) {
563 async_answer_0(trans->data_caller, rc);
564 async_answer_0(callid, rc);
565 async_transaction_destroy(trans);
566 }
567}
568
569void remote_usb_write(
570 ddf_fun_t *fun, void *iface, ipc_callid_t callid, ipc_call_t *call)
571{
572 assert(fun);
573 assert(iface);
574 assert(call);
575
576 const usb_iface_t *usb_iface = iface;
577
578 if (!usb_iface->write) {
579 async_answer_0(callid, ENOTSUP);
580 return;
581 }
582
583 const usb_endpoint_t ep = DEV_IPC_GET_ARG1(*call);
584 const size_t data_buffer_len = DEV_IPC_GET_ARG2(*call);
585 const uint64_t setup =
586 ((uint64_t)DEV_IPC_GET_ARG3(*call)) |
587 (((uint64_t)DEV_IPC_GET_ARG4(*call)) << 32);
588
589 async_transaction_t *trans = async_transaction_create(callid);
590 if (trans == NULL) {
591 async_answer_0(callid, ENOMEM);
592 return;
593 }
594
595 size_t size = 0;
596 if (data_buffer_len > 0) {
597 const int rc = async_data_write_accept(&trans->buffer, false,
598 1, data_buffer_len, 0, &size);
599
600 if (rc != EOK) {
601 async_answer_0(callid, rc);
602 async_transaction_destroy(trans);
603 return;
604 }
605 }
606
607 const int rc = usb_iface->write(
608 fun, ep, setup, trans->buffer, size, callback_out, trans);
609
610 if (rc != EOK) {
611 async_answer_0(callid, rc);
612 async_transaction_destroy(trans);
613 }
614}
615/**
616 * @}
617 */
Note: See TracBrowser for help on using the repository browser.