1 | /*
|
---|
2 | * Copyright (c) 2010 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 usb
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file
|
---|
33 | * @brief Virtual host controller driver.
|
---|
34 | */
|
---|
35 |
|
---|
36 | #include <devmap.h>
|
---|
37 | #include <ipc/ipc.h>
|
---|
38 | #include <async.h>
|
---|
39 | #include <unistd.h>
|
---|
40 | #include <stdlib.h>
|
---|
41 | #include <sysinfo.h>
|
---|
42 | #include <stdio.h>
|
---|
43 | #include <errno.h>
|
---|
44 | #include <str_error.h>
|
---|
45 |
|
---|
46 | #include <usb/hcd.h>
|
---|
47 | #include "vhcd.h"
|
---|
48 | #include "hc.h"
|
---|
49 | #include "devices.h"
|
---|
50 | #include "conn.h"
|
---|
51 |
|
---|
52 |
|
---|
53 | static void client_connection(ipc_callid_t iid, ipc_call_t *icall)
|
---|
54 | {
|
---|
55 | ipcarg_t phone_hash = icall->in_phone_hash;
|
---|
56 |
|
---|
57 | ipc_answer_0(iid, EOK);
|
---|
58 | printf("%s: new client connected (phone %#x).\n", NAME, phone_hash);
|
---|
59 |
|
---|
60 | while (true) {
|
---|
61 | ipc_callid_t callid;
|
---|
62 | ipc_call_t call;
|
---|
63 |
|
---|
64 | callid = async_get_call(&call);
|
---|
65 |
|
---|
66 | /*
|
---|
67 | * We can do nothing until we have the callback phone.
|
---|
68 | * Thus, we will wait for the callback and start processing
|
---|
69 | * after that.
|
---|
70 | */
|
---|
71 | int method = (int) IPC_GET_METHOD(call);
|
---|
72 |
|
---|
73 | if (method == IPC_M_PHONE_HUNGUP) {
|
---|
74 | ipc_answer_0(callid, EOK);
|
---|
75 | return;
|
---|
76 | }
|
---|
77 |
|
---|
78 | if (method == IPC_M_CONNECT_TO_ME) {
|
---|
79 | int kind = IPC_GET_ARG1(call);
|
---|
80 | int callback = IPC_GET_ARG5(call);
|
---|
81 |
|
---|
82 | /*
|
---|
83 | * Determine whether host connected to us
|
---|
84 | * or a device.
|
---|
85 | */
|
---|
86 | if (kind == 0) {
|
---|
87 | ipc_answer_0(callid, EOK);
|
---|
88 | connection_handler_host(phone_hash, callback);
|
---|
89 | return;
|
---|
90 | } else if (kind == 1) {
|
---|
91 | int device_id = IPC_GET_ARG2(call);
|
---|
92 | virtdev_connection_t *dev
|
---|
93 | = virtdev_recognise(device_id, callback);
|
---|
94 | if (!dev) {
|
---|
95 | ipc_answer_0(callid, EEXISTS);
|
---|
96 | ipc_hangup(callback);
|
---|
97 | return;
|
---|
98 | }
|
---|
99 | ipc_answer_0(callid, EOK);
|
---|
100 | connection_handler_device(phone_hash, dev);
|
---|
101 | virtdev_destroy_device(dev);
|
---|
102 | return;
|
---|
103 | } else {
|
---|
104 | ipc_answer_0(callid, EINVAL);
|
---|
105 | ipc_hangup(callback);
|
---|
106 | return;
|
---|
107 | }
|
---|
108 | }
|
---|
109 |
|
---|
110 | /*
|
---|
111 | * No other methods could be served now.
|
---|
112 | */
|
---|
113 | ipc_answer_0(callid, ENOTSUP);
|
---|
114 | }
|
---|
115 | }
|
---|
116 |
|
---|
117 | int main(int argc, char * argv[])
|
---|
118 | {
|
---|
119 | printf("%s: Virtual USB host controller driver.\n", NAME);
|
---|
120 |
|
---|
121 | int rc;
|
---|
122 |
|
---|
123 | rc = devmap_driver_register(NAME, client_connection);
|
---|
124 | if (rc != EOK) {
|
---|
125 | printf("%s: unable to register driver (%s).\n",
|
---|
126 | NAME, str_error(rc));
|
---|
127 | return 1;
|
---|
128 | }
|
---|
129 |
|
---|
130 | rc = devmap_device_register(DEVMAP_PATH, NULL);
|
---|
131 | if (rc != EOK) {
|
---|
132 | printf("%s: unable to register device %s (%s).\n",
|
---|
133 | NAME, DEVMAP_PATH, str_error(rc));
|
---|
134 | return 1;
|
---|
135 | }
|
---|
136 |
|
---|
137 | printf("%s: accepting connections.\n", NAME);
|
---|
138 | hc_manager();
|
---|
139 |
|
---|
140 | return 0;
|
---|
141 | }
|
---|
142 |
|
---|
143 |
|
---|
144 | /**
|
---|
145 | * @}
|
---|
146 | */
|
---|