[b371844] | 1 | /*
|
---|
[6cb58e6] | 2 | * Copyright (c) 2011 Vojtech Horky
|
---|
[b371844] | 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 |
|
---|
[bd8c753d] | 29 | /** @addtogroup drvusbvhc
|
---|
[b371844] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
[6cb58e6] | 33 | * Connection handling of calls from virtual device (implementation).
|
---|
[b371844] | 34 | */
|
---|
| 35 |
|
---|
| 36 | #include <assert.h>
|
---|
| 37 | #include <errno.h>
|
---|
[6cb58e6] | 38 | #include <ddf/driver.h>
|
---|
[00b6c73] | 39 | #include <usbvirt/ipc.h>
|
---|
[79ae36dd] | 40 | #include <async.h>
|
---|
[b371844] | 41 | #include "conn.h"
|
---|
[ca07cd3] | 42 |
|
---|
[6cb58e6] | 43 | static fibril_local uintptr_t plugged_device_handle = 0;
|
---|
[00b6c73] | 44 | #define PLUGGED_DEVICE_NAME_MAXLEN 256
|
---|
| 45 | static fibril_local char plugged_device_name[PLUGGED_DEVICE_NAME_MAXLEN + 1] = "<unknown>";
|
---|
| 46 |
|
---|
| 47 | /** Receive device name.
|
---|
| 48 | *
|
---|
| 49 | * @warning Errors are silently ignored.
|
---|
| 50 | *
|
---|
[79ae36dd] | 51 | * @param sess Session to the virtual device.
|
---|
| 52 | *
|
---|
[00b6c73] | 53 | */
|
---|
[79ae36dd] | 54 | static void receive_device_name(async_sess_t *sess)
|
---|
[00b6c73] | 55 | {
|
---|
[79ae36dd] | 56 | async_exch_t *exch = async_exchange_begin(sess);
|
---|
| 57 |
|
---|
| 58 | aid_t opening_request = async_send_0(exch, IPC_M_USBVIRT_GET_NAME, NULL);
|
---|
[00b6c73] | 59 | if (opening_request == 0) {
|
---|
[79ae36dd] | 60 | async_exchange_end(exch);
|
---|
[00b6c73] | 61 | return;
|
---|
| 62 | }
|
---|
[79ae36dd] | 63 |
|
---|
[00b6c73] | 64 | ipc_call_t data_request_call;
|
---|
[79ae36dd] | 65 | aid_t data_request = async_data_read(exch, plugged_device_name,
|
---|
| 66 | PLUGGED_DEVICE_NAME_MAXLEN, &data_request_call);
|
---|
| 67 |
|
---|
| 68 | async_exchange_end(exch);
|
---|
| 69 |
|
---|
[00b6c73] | 70 | if (data_request == 0) {
|
---|
| 71 | async_wait_for(opening_request, NULL);
|
---|
| 72 | return;
|
---|
| 73 | }
|
---|
[79ae36dd] | 74 |
|
---|
[00b6c73] | 75 | sysarg_t data_request_rc;
|
---|
| 76 | sysarg_t opening_request_rc;
|
---|
| 77 | async_wait_for(data_request, &data_request_rc);
|
---|
| 78 | async_wait_for(opening_request, &opening_request_rc);
|
---|
[79ae36dd] | 79 |
|
---|
| 80 | if ((data_request_rc != EOK) || (opening_request_rc != EOK))
|
---|
[00b6c73] | 81 | return;
|
---|
[79ae36dd] | 82 |
|
---|
[00b6c73] | 83 | size_t len = IPC_GET_ARG2(data_request_call);
|
---|
| 84 | plugged_device_name[len] = 0;
|
---|
| 85 | }
|
---|
[ca07cd3] | 86 |
|
---|
[e27595b] | 87 | /** Default handler for IPC methods not handled by DDF.
|
---|
[355f7c2] | 88 | *
|
---|
[eb1a2f4] | 89 | * @param fun Device handling the call.
|
---|
[e27595b] | 90 | * @param icallid Call id.
|
---|
| 91 | * @param icall Call data.
|
---|
[b371844] | 92 | */
|
---|
[8869f7b] | 93 | void default_connection_handler(ddf_fun_t *fun, ipc_callid_t icallid,
|
---|
| 94 | ipc_call_t *icall)
|
---|
[b371844] | 95 | {
|
---|
[6cb58e6] | 96 | vhc_data_t *vhc = fun->dev->driver_data;
|
---|
[8869f7b] | 97 |
|
---|
| 98 | async_sess_t *callback =
|
---|
| 99 | async_callback_receive_start(EXCHANGE_SERIALIZE, icall);
|
---|
| 100 |
|
---|
| 101 | if (callback) {
|
---|
| 102 | int rc = vhc_virtdev_plug(vhc, callback, &plugged_device_handle);
|
---|
[6cb58e6] | 103 | if (rc != EOK) {
|
---|
| 104 | async_answer_0(icallid, rc);
|
---|
[17aca1c] | 105 | async_hangup(callback);
|
---|
[e27595b] | 106 | return;
|
---|
[b371844] | 107 | }
|
---|
[8869f7b] | 108 |
|
---|
[6cb58e6] | 109 | async_answer_0(icallid, EOK);
|
---|
[8869f7b] | 110 |
|
---|
[00b6c73] | 111 | receive_device_name(callback);
|
---|
[8869f7b] | 112 |
|
---|
[00b6c73] | 113 | usb_log_info("New virtual device `%s' (id: %" PRIxn ").\n",
|
---|
| 114 | plugged_device_name, plugged_device_handle);
|
---|
[8869f7b] | 115 | } else
|
---|
| 116 | async_answer_0(icallid, EINVAL);
|
---|
[b371844] | 117 | }
|
---|
| 118 |
|
---|
[6cb58e6] | 119 | /** Callback when client disconnects.
|
---|
| 120 | *
|
---|
| 121 | * Used to unplug virtual USB device.
|
---|
[6967c14] | 122 | *
|
---|
[6cb58e6] | 123 | * @param fun
|
---|
[6967c14] | 124 | */
|
---|
[eb1a2f4] | 125 | void on_client_close(ddf_fun_t *fun)
|
---|
[6967c14] | 126 | {
|
---|
[6cb58e6] | 127 | vhc_data_t *vhc = fun->dev->driver_data;
|
---|
[6967c14] | 128 |
|
---|
[6cb58e6] | 129 | if (plugged_device_handle != 0) {
|
---|
[00b6c73] | 130 | usb_log_info("Virtual device `%s' disconnected (id: %" PRIxn ").\n",
|
---|
| 131 | plugged_device_name, plugged_device_handle);
|
---|
[6cb58e6] | 132 | vhc_virtdev_unplug(vhc, plugged_device_handle);
|
---|
| 133 | }
|
---|
[6967c14] | 134 | }
|
---|
| 135 |
|
---|
[e27595b] | 136 |
|
---|
[b371844] | 137 | /**
|
---|
| 138 | * @}
|
---|
| 139 | */
|
---|