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