1 | /*
|
---|
2 | * Copyright (c) 2010 Lenka Trochtova
|
---|
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 | /**
|
---|
30 | * @defgroup devman Device manager.
|
---|
31 | * @brief HelenOS device manager.
|
---|
32 | * @{
|
---|
33 | */
|
---|
34 |
|
---|
35 | /** @file
|
---|
36 | */
|
---|
37 |
|
---|
38 | #include <assert.h>
|
---|
39 | #include <ipc/services.h>
|
---|
40 | #include <ipc/ns.h>
|
---|
41 | #include <async.h>
|
---|
42 | #include <stdio.h>
|
---|
43 | #include <errno.h>
|
---|
44 | #include <bool.h>
|
---|
45 | #include <fibril_synch.h>
|
---|
46 | #include <stdlib.h>
|
---|
47 | #include <string.h>
|
---|
48 | #include <dirent.h>
|
---|
49 | #include <fcntl.h>
|
---|
50 | #include <sys/stat.h>
|
---|
51 | #include <ctype.h>
|
---|
52 | #include <ipc/devman.h>
|
---|
53 |
|
---|
54 | #include "devman.h"
|
---|
55 |
|
---|
56 | #define DRIVER_DEFAULT_STORE "/srv/drivers"
|
---|
57 |
|
---|
58 | static driver_list_t drivers_list;
|
---|
59 | static dev_tree_t device_tree;
|
---|
60 |
|
---|
61 | /**
|
---|
62 | *
|
---|
63 | *
|
---|
64 | * Driver's mutex must be locked.
|
---|
65 | */
|
---|
66 | static void pass_devices_to_driver(driver_t *driver)
|
---|
67 | {
|
---|
68 |
|
---|
69 |
|
---|
70 |
|
---|
71 |
|
---|
72 | }
|
---|
73 |
|
---|
74 | static void init_running_driver(driver_t *driver)
|
---|
75 | {
|
---|
76 | fibril_mutex_lock(&driver->driver_mutex);
|
---|
77 |
|
---|
78 | // pass devices which have been already assigned to the driver to the driver
|
---|
79 | pass_devices_to_driver(driver);
|
---|
80 |
|
---|
81 | // change driver's state to running
|
---|
82 | driver->state = DRIVER_RUNNING;
|
---|
83 |
|
---|
84 | fibril_mutex_unlock(&driver->driver_mutex);
|
---|
85 | }
|
---|
86 |
|
---|
87 | /**
|
---|
88 | * Register running driver.
|
---|
89 | */
|
---|
90 | static driver_t * devman_driver_register(void)
|
---|
91 | {
|
---|
92 | printf(NAME ": devman_driver_register \n");
|
---|
93 |
|
---|
94 | ipc_call_t icall;
|
---|
95 | ipc_callid_t iid = async_get_call(&icall);
|
---|
96 | driver_t *driver = NULL;
|
---|
97 |
|
---|
98 | if (IPC_GET_METHOD(icall) != DEVMAN_DRIVER_REGISTER) {
|
---|
99 | ipc_answer_0(iid, EREFUSED);
|
---|
100 | return NULL;
|
---|
101 | }
|
---|
102 |
|
---|
103 | char drv_name[DEVMAN_NAME_MAXLEN];
|
---|
104 |
|
---|
105 | // Get driver name
|
---|
106 | int rc = async_string_receive(drv_name, DEVMAN_NAME_MAXLEN, NULL);
|
---|
107 | if (rc != EOK) {
|
---|
108 | ipc_answer_0(iid, rc);
|
---|
109 | return NULL;
|
---|
110 | }
|
---|
111 | printf(NAME ": the %s driver is trying to register by the service.\n", drv_name);
|
---|
112 |
|
---|
113 | // Find driver structure
|
---|
114 | driver = find_driver(&drivers_list, drv_name);
|
---|
115 | if (NULL == driver) {
|
---|
116 | printf(NAME ": no driver named %s was found.\n", drv_name);
|
---|
117 | ipc_answer_0(iid, ENOENT);
|
---|
118 | return NULL;
|
---|
119 | }
|
---|
120 | printf(NAME ": registering the running instance of the %s driver.\n", driver->name);
|
---|
121 |
|
---|
122 | // Create connection to the driver
|
---|
123 | printf(NAME ": creating connection to the %s driver.\n", driver->name);
|
---|
124 | ipc_call_t call;
|
---|
125 | ipc_callid_t callid = async_get_call(&call);
|
---|
126 | if (IPC_GET_METHOD(call) != IPC_M_CONNECT_TO_ME) {
|
---|
127 | ipc_answer_0(callid, ENOTSUP);
|
---|
128 | ipc_answer_0(iid, ENOTSUP);
|
---|
129 | return NULL;
|
---|
130 | }
|
---|
131 |
|
---|
132 | fibril_mutex_lock(&driver->driver_mutex);
|
---|
133 | assert(DRIVER_STARTING == driver->state);
|
---|
134 | driver->phone = IPC_GET_ARG5(call);
|
---|
135 | fibril_mutex_unlock(&driver->driver_mutex);
|
---|
136 |
|
---|
137 | printf(NAME ": the %s driver was successfully registered as running.\n", driver->name);
|
---|
138 |
|
---|
139 | ipc_answer_0(callid, EOK);
|
---|
140 |
|
---|
141 | ipc_answer_0(iid, EOK);
|
---|
142 |
|
---|
143 | return driver;
|
---|
144 | }
|
---|
145 |
|
---|
146 | /** Function for handling connections to device manager.
|
---|
147 | *
|
---|
148 | */
|
---|
149 | static void devman_connection_driver(ipc_callid_t iid, ipc_call_t *icall)
|
---|
150 | {
|
---|
151 | /* Accept the connection */
|
---|
152 | ipc_answer_0(iid, EOK);
|
---|
153 |
|
---|
154 | driver_t *driver = devman_driver_register();
|
---|
155 | if (driver == NULL)
|
---|
156 | return;
|
---|
157 |
|
---|
158 | init_running_driver(driver);
|
---|
159 |
|
---|
160 | ipc_callid_t callid;
|
---|
161 | ipc_call_t call;
|
---|
162 | bool cont = true;
|
---|
163 | while (cont) {
|
---|
164 | callid = async_get_call(&call);
|
---|
165 |
|
---|
166 | switch (IPC_GET_METHOD(call)) {
|
---|
167 | case IPC_M_PHONE_HUNGUP:
|
---|
168 | cont = false;
|
---|
169 | continue;
|
---|
170 | case DEVMAN_ADD_CHILD_DEVICE:
|
---|
171 | // TODO add new device node to the device tree
|
---|
172 | break;
|
---|
173 | default:
|
---|
174 | ipc_answer_0(callid, EINVAL);
|
---|
175 | break;
|
---|
176 | }
|
---|
177 | }
|
---|
178 | }
|
---|
179 |
|
---|
180 | /** Function for handling connections to device manager.
|
---|
181 | *
|
---|
182 | */
|
---|
183 | static void devman_connection(ipc_callid_t iid, ipc_call_t *icall)
|
---|
184 | {
|
---|
185 | // Select interface
|
---|
186 | switch ((ipcarg_t) (IPC_GET_ARG1(*icall))) {
|
---|
187 | case DEVMAN_DRIVER:
|
---|
188 | devman_connection_driver(iid, icall);
|
---|
189 | break;
|
---|
190 | /*case DEVMAN_CLIENT:
|
---|
191 | devmap_connection_client(iid, icall);
|
---|
192 | break;
|
---|
193 | case DEVMAN_CONNECT_TO_DEVICE:
|
---|
194 | // Connect client to selected device
|
---|
195 | devmap_forward(iid, icall);
|
---|
196 | break;*/
|
---|
197 | default:
|
---|
198 | /* No such interface */
|
---|
199 | ipc_answer_0(iid, ENOENT);
|
---|
200 | }
|
---|
201 | }
|
---|
202 |
|
---|
203 | /** Initialize device manager internal structures.
|
---|
204 | */
|
---|
205 | static bool devman_init()
|
---|
206 | {
|
---|
207 | printf(NAME ": devman_init - looking for available drivers. \n");
|
---|
208 |
|
---|
209 | // initialize list of available drivers
|
---|
210 | init_driver_list(&drivers_list);
|
---|
211 | if (0 == lookup_available_drivers(&drivers_list, DRIVER_DEFAULT_STORE)) {
|
---|
212 | printf(NAME " no drivers found.");
|
---|
213 | return false;
|
---|
214 | }
|
---|
215 | printf(NAME ": devman_init - list of drivers has been initialized. \n");
|
---|
216 |
|
---|
217 | // create root device node
|
---|
218 | if (!init_device_tree(&device_tree, &drivers_list)) {
|
---|
219 | printf(NAME " failed to initialize device tree.");
|
---|
220 | return false;
|
---|
221 | }
|
---|
222 |
|
---|
223 | return true;
|
---|
224 | }
|
---|
225 |
|
---|
226 | int main(int argc, char *argv[])
|
---|
227 | {
|
---|
228 | printf(NAME ": HelenOS Device Manager\n");
|
---|
229 |
|
---|
230 | if (!devman_init()) {
|
---|
231 | printf(NAME ": Error while initializing service\n");
|
---|
232 | return -1;
|
---|
233 | }
|
---|
234 |
|
---|
235 | // Set a handler of incomming connections
|
---|
236 | async_set_client_connection(devman_connection);
|
---|
237 |
|
---|
238 | // Register device manager at naming service
|
---|
239 | ipcarg_t phonead;
|
---|
240 | if (ipc_connect_to_me(PHONE_NS, SERVICE_DEVMAN, 0, 0, &phonead) != 0)
|
---|
241 | return -1;
|
---|
242 |
|
---|
243 | printf(NAME ": Accepting connections\n");
|
---|
244 | async_manager();
|
---|
245 |
|
---|
246 | // Never reached
|
---|
247 | return 0;
|
---|
248 | }
|
---|