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 | #include <ipc/driver.h>
|
---|
54 | #include <thread.h>
|
---|
55 |
|
---|
56 | #include "devman.h"
|
---|
57 |
|
---|
58 | #define DRIVER_DEFAULT_STORE "/srv/drivers"
|
---|
59 |
|
---|
60 | static driver_list_t drivers_list;
|
---|
61 | static dev_tree_t device_tree;
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * Register running driver.
|
---|
65 | */
|
---|
66 | static driver_t * devman_driver_register(void)
|
---|
67 | {
|
---|
68 | printf(NAME ": devman_driver_register \n");
|
---|
69 |
|
---|
70 | ipc_call_t icall;
|
---|
71 | ipc_callid_t iid = async_get_call(&icall);
|
---|
72 | driver_t *driver = NULL;
|
---|
73 |
|
---|
74 | if (IPC_GET_METHOD(icall) != DEVMAN_DRIVER_REGISTER) {
|
---|
75 | ipc_answer_0(iid, EREFUSED);
|
---|
76 | return NULL;
|
---|
77 | }
|
---|
78 |
|
---|
79 | char *drv_name = NULL;
|
---|
80 |
|
---|
81 | // Get driver name
|
---|
82 | int rc = async_string_receive(&drv_name, DEVMAN_NAME_MAXLEN, NULL);
|
---|
83 | if (rc != EOK) {
|
---|
84 | ipc_answer_0(iid, rc);
|
---|
85 | return NULL;
|
---|
86 | }
|
---|
87 | printf(NAME ": the %s driver is trying to register by the service.\n", drv_name);
|
---|
88 |
|
---|
89 | // Find driver structure
|
---|
90 | driver = find_driver(&drivers_list, drv_name);
|
---|
91 |
|
---|
92 | free(drv_name);
|
---|
93 | drv_name = NULL;
|
---|
94 |
|
---|
95 | if (NULL == driver) {
|
---|
96 | printf(NAME ": no driver named %s was found.\n", drv_name);
|
---|
97 | ipc_answer_0(iid, ENOENT);
|
---|
98 | return NULL;
|
---|
99 | }
|
---|
100 |
|
---|
101 | // Create connection to the driver
|
---|
102 | printf(NAME ": creating connection to the %s driver.\n", driver->name);
|
---|
103 | ipc_call_t call;
|
---|
104 | ipc_callid_t callid = async_get_call(&call);
|
---|
105 | if (IPC_GET_METHOD(call) != IPC_M_CONNECT_TO_ME) {
|
---|
106 | ipc_answer_0(callid, ENOTSUP);
|
---|
107 | ipc_answer_0(iid, ENOTSUP);
|
---|
108 | return NULL;
|
---|
109 | }
|
---|
110 |
|
---|
111 | // remember driver's phone
|
---|
112 | set_driver_phone(driver, IPC_GET_ARG5(call));
|
---|
113 |
|
---|
114 | printf(NAME ": the %s driver was successfully registered as running.\n", driver->name);
|
---|
115 |
|
---|
116 | ipc_answer_0(callid, EOK);
|
---|
117 |
|
---|
118 | ipc_answer_0(iid, EOK);
|
---|
119 |
|
---|
120 | return driver;
|
---|
121 | }
|
---|
122 |
|
---|
123 | /**
|
---|
124 | * Receive device match ID from the device's parent driver and add it to the list of devices match ids.
|
---|
125 | *
|
---|
126 | * @param match_ids the list of the device's match ids.
|
---|
127 | *
|
---|
128 | * @return 0 on success, negative error code otherwise.
|
---|
129 | */
|
---|
130 | static int devman_receive_match_id(match_id_list_t *match_ids) {
|
---|
131 |
|
---|
132 | match_id_t *match_id = create_match_id();
|
---|
133 | ipc_callid_t callid;
|
---|
134 | ipc_call_t call;
|
---|
135 | int rc = 0;
|
---|
136 |
|
---|
137 | callid = async_get_call(&call);
|
---|
138 | if (DEVMAN_ADD_MATCH_ID != IPC_GET_METHOD(call)) {
|
---|
139 | printf(NAME ": ERROR: devman_receive_match_id - invalid protocol.\n");
|
---|
140 | ipc_answer_0(callid, EINVAL);
|
---|
141 | delete_match_id(match_id);
|
---|
142 | return EINVAL;
|
---|
143 | }
|
---|
144 |
|
---|
145 | if (NULL == match_id) {
|
---|
146 | printf(NAME ": ERROR: devman_receive_match_id - failed to allocate match id.\n");
|
---|
147 | ipc_answer_0(callid, ENOMEM);
|
---|
148 | return ENOMEM;
|
---|
149 | }
|
---|
150 |
|
---|
151 | ipc_answer_0(callid, EOK);
|
---|
152 |
|
---|
153 | match_id->score = IPC_GET_ARG1(call);
|
---|
154 |
|
---|
155 | rc = async_string_receive(&match_id->id, DEVMAN_NAME_MAXLEN, NULL);
|
---|
156 | if (EOK != rc) {
|
---|
157 | delete_match_id(match_id);
|
---|
158 | printf(NAME ": devman_receive_match_id - failed to receive match id string.\n");
|
---|
159 | return rc;
|
---|
160 | }
|
---|
161 |
|
---|
162 | list_append(&match_id->link, &match_ids->ids);
|
---|
163 |
|
---|
164 | printf(NAME ": received match id '%s', score = %d \n", match_id->id, match_id->score);
|
---|
165 | return rc;
|
---|
166 | }
|
---|
167 |
|
---|
168 | /**
|
---|
169 | * Receive device match IDs from the device's parent driver
|
---|
170 | * and add them to the list of devices match ids.
|
---|
171 | *
|
---|
172 | * @param match_count the number of device's match ids to be received.
|
---|
173 | * @param match_ids the list of the device's match ids.
|
---|
174 | *
|
---|
175 | * @return 0 on success, negative error code otherwise.
|
---|
176 | */
|
---|
177 | static int devman_receive_match_ids(ipcarg_t match_count, match_id_list_t *match_ids)
|
---|
178 | {
|
---|
179 | int ret = EOK;
|
---|
180 | size_t i;
|
---|
181 | for (i = 0; i < match_count; i++) {
|
---|
182 | if (EOK != (ret = devman_receive_match_id(match_ids))) {
|
---|
183 | return ret;
|
---|
184 | }
|
---|
185 | }
|
---|
186 | return ret;
|
---|
187 | }
|
---|
188 |
|
---|
189 | /** Handle child device registration.
|
---|
190 | *
|
---|
191 | * Child devices are registered by their parent's device driver.
|
---|
192 | */
|
---|
193 | static void devman_add_child(ipc_callid_t callid, ipc_call_t *call)
|
---|
194 | {
|
---|
195 | // printf(NAME ": devman_add_child\n");
|
---|
196 |
|
---|
197 | device_handle_t parent_handle = IPC_GET_ARG1(*call);
|
---|
198 | ipcarg_t match_count = IPC_GET_ARG2(*call);
|
---|
199 |
|
---|
200 | node_t *parent = find_dev_node(&device_tree, parent_handle);
|
---|
201 |
|
---|
202 | if (NULL == parent) {
|
---|
203 | ipc_answer_0(callid, ENOENT);
|
---|
204 | return;
|
---|
205 | }
|
---|
206 |
|
---|
207 | char *dev_name = NULL;
|
---|
208 | int rc = async_string_receive(&dev_name, DEVMAN_NAME_MAXLEN, NULL);
|
---|
209 | if (EOK != rc) {
|
---|
210 | ipc_answer_0(callid, rc);
|
---|
211 | return;
|
---|
212 | }
|
---|
213 | // printf(NAME ": newly added child device's name is '%s'.\n", dev_name);
|
---|
214 |
|
---|
215 | node_t *node = create_dev_node();
|
---|
216 | if (!insert_dev_node(&device_tree, node, dev_name, parent)) {
|
---|
217 | delete_dev_node(node);
|
---|
218 | ipc_answer_0(callid, ENOMEM);
|
---|
219 | return;
|
---|
220 | }
|
---|
221 |
|
---|
222 | printf(NAME ": devman_add_child %s\n", node->pathname);
|
---|
223 |
|
---|
224 | devman_receive_match_ids(match_count, &node->match_ids);
|
---|
225 |
|
---|
226 | // return device handle to parent's driver
|
---|
227 | ipc_answer_1(callid, EOK, node->handle);
|
---|
228 |
|
---|
229 | // try to find suitable driver and assign it to the device
|
---|
230 | assign_driver(node, &drivers_list);
|
---|
231 | }
|
---|
232 |
|
---|
233 | /**
|
---|
234 | * Initialize driver which has registered itself as running and ready.
|
---|
235 | *
|
---|
236 | * The initialization is done in a separate fibril to avoid deadlocks
|
---|
237 | * (if the driver needed to be served by devman during the driver's initialization).
|
---|
238 | */
|
---|
239 | static int init_running_drv(void *drv)
|
---|
240 | {
|
---|
241 | driver_t *driver = (driver_t *)drv;
|
---|
242 | initialize_running_driver(driver);
|
---|
243 | printf(NAME ": the %s driver was successfully initialized. \n", driver->name);
|
---|
244 | return 0;
|
---|
245 | }
|
---|
246 |
|
---|
247 | /** Function for handling connections from a driver to the device manager.
|
---|
248 | */
|
---|
249 | static void devman_connection_driver(ipc_callid_t iid, ipc_call_t *icall)
|
---|
250 | {
|
---|
251 | /* Accept the connection */
|
---|
252 | ipc_answer_0(iid, EOK);
|
---|
253 |
|
---|
254 | driver_t *driver = devman_driver_register();
|
---|
255 | if (NULL == driver)
|
---|
256 | return;
|
---|
257 |
|
---|
258 | // Initialize the driver as running (e.g. pass assigned devices to it) in a separate fibril;
|
---|
259 | // the separate fibril is used to enable the driver
|
---|
260 | // to use devman service during the driver's initialization.
|
---|
261 | fid_t fid = fibril_create(init_running_drv, driver);
|
---|
262 | if (fid == 0) {
|
---|
263 | printf(NAME ": Error creating fibril for the initialization of the newly registered running driver.\n");
|
---|
264 | return;
|
---|
265 | }
|
---|
266 | fibril_add_ready(fid);
|
---|
267 |
|
---|
268 | /*thread_id_t tid;
|
---|
269 | if (0 != thread_create(init_running_drv, driver, "init_running_drv", &tid)) {
|
---|
270 | printf(NAME ": failed to start the initialization of the newly registered running driver.\n");
|
---|
271 | }*/
|
---|
272 |
|
---|
273 | ipc_callid_t callid;
|
---|
274 | ipc_call_t call;
|
---|
275 | bool cont = true;
|
---|
276 | while (cont) {
|
---|
277 | callid = async_get_call(&call);
|
---|
278 |
|
---|
279 | switch (IPC_GET_METHOD(call)) {
|
---|
280 | case IPC_M_PHONE_HUNGUP:
|
---|
281 | cont = false;
|
---|
282 | continue;
|
---|
283 | case DEVMAN_ADD_CHILD_DEVICE:
|
---|
284 | devman_add_child(callid, &call);
|
---|
285 | break;
|
---|
286 | default:
|
---|
287 | ipc_answer_0(callid, EINVAL);
|
---|
288 | break;
|
---|
289 | }
|
---|
290 | }
|
---|
291 | }
|
---|
292 |
|
---|
293 | /** Find handle for the device instance identified by the device's path in the device tree.
|
---|
294 | */
|
---|
295 | static void devman_device_get_handle(ipc_callid_t iid, ipc_call_t *icall)
|
---|
296 | {
|
---|
297 | char *pathname;
|
---|
298 |
|
---|
299 | /* Get fqdn */
|
---|
300 | int rc = async_string_receive(&pathname, 0, NULL);
|
---|
301 | if (rc != EOK) {
|
---|
302 | ipc_answer_0(iid, rc);
|
---|
303 | return;
|
---|
304 | }
|
---|
305 |
|
---|
306 | node_t * dev = find_dev_node_by_path(&device_tree, pathname);
|
---|
307 |
|
---|
308 | free(pathname);
|
---|
309 |
|
---|
310 | if (NULL == dev) {
|
---|
311 | ipc_answer_0(iid, ENOENT);
|
---|
312 | return;
|
---|
313 | }
|
---|
314 |
|
---|
315 | ipc_answer_1(iid, EOK, dev->handle);
|
---|
316 | }
|
---|
317 |
|
---|
318 |
|
---|
319 | /** Function for handling connections from a client to the device manager.
|
---|
320 | */
|
---|
321 | static void devman_connection_client(ipc_callid_t iid, ipc_call_t *icall)
|
---|
322 | {
|
---|
323 | /* Accept connection */
|
---|
324 | ipc_answer_0(iid, EOK);
|
---|
325 |
|
---|
326 | bool cont = true;
|
---|
327 | while (cont) {
|
---|
328 | ipc_call_t call;
|
---|
329 | ipc_callid_t callid = async_get_call(&call);
|
---|
330 |
|
---|
331 | switch (IPC_GET_METHOD(call)) {
|
---|
332 | case IPC_M_PHONE_HUNGUP:
|
---|
333 | cont = false;
|
---|
334 | continue;
|
---|
335 | case DEVMAN_DEVICE_GET_HANDLE:
|
---|
336 | devman_device_get_handle(callid, &call);
|
---|
337 | break;
|
---|
338 | default:
|
---|
339 | if (!(callid & IPC_CALLID_NOTIFICATION))
|
---|
340 | ipc_answer_0(callid, ENOENT);
|
---|
341 | }
|
---|
342 | }
|
---|
343 | }
|
---|
344 |
|
---|
345 | static void devman_forward(ipc_callid_t iid, ipc_call_t *icall, bool drv_to_parent) {
|
---|
346 |
|
---|
347 | device_handle_t handle = IPC_GET_ARG2(*icall);
|
---|
348 | // printf(NAME ": devman_forward - trying to forward connection to device with handle %x.\n", handle);
|
---|
349 |
|
---|
350 | node_t *dev = find_dev_node(&device_tree, handle);
|
---|
351 | if (NULL == dev) {
|
---|
352 | printf(NAME ": devman_forward error - no device with handle %x was found.\n", handle);
|
---|
353 | ipc_answer_0(iid, ENOENT);
|
---|
354 | return;
|
---|
355 | }
|
---|
356 |
|
---|
357 | driver_t *driver = NULL;
|
---|
358 |
|
---|
359 | if (drv_to_parent) {
|
---|
360 | if (NULL != dev->parent) {
|
---|
361 | driver = dev->parent->drv;
|
---|
362 | }
|
---|
363 | } else if (DEVICE_USABLE == dev->state) {
|
---|
364 | driver = dev->drv;
|
---|
365 | assert(NULL != driver);
|
---|
366 | }
|
---|
367 |
|
---|
368 | if (NULL == driver) {
|
---|
369 | printf(NAME ": devman_forward error - the device is not in usable state.\n", handle);
|
---|
370 | ipc_answer_0(iid, ENOENT);
|
---|
371 | return;
|
---|
372 | }
|
---|
373 |
|
---|
374 | int method;
|
---|
375 | if (drv_to_parent) {
|
---|
376 | method = DRIVER_DRIVER;
|
---|
377 | } else {
|
---|
378 | method = DRIVER_CLIENT;
|
---|
379 | }
|
---|
380 |
|
---|
381 | if (driver->phone <= 0) {
|
---|
382 | printf(NAME ": devman_forward: cound not forward to driver %s ", driver->name);
|
---|
383 | printf("the driver's phone is %x).\n", driver->phone);
|
---|
384 | return;
|
---|
385 | }
|
---|
386 | printf(NAME ": devman_forward: forward connection to device %s to driver %s.\n", dev->pathname, driver->name);
|
---|
387 | ipc_forward_fast(iid, driver->phone, method, dev->handle, 0, IPC_FF_NONE);
|
---|
388 | }
|
---|
389 |
|
---|
390 | /** Function for handling connections to device manager.
|
---|
391 | *
|
---|
392 | */
|
---|
393 | static void devman_connection(ipc_callid_t iid, ipc_call_t *icall)
|
---|
394 | {
|
---|
395 | // Select interface
|
---|
396 | switch ((ipcarg_t) (IPC_GET_ARG1(*icall))) {
|
---|
397 | case DEVMAN_DRIVER:
|
---|
398 | devman_connection_driver(iid, icall);
|
---|
399 | break;
|
---|
400 | case DEVMAN_CLIENT:
|
---|
401 | devman_connection_client(iid, icall);
|
---|
402 | break;
|
---|
403 | case DEVMAN_CONNECT_TO_DEVICE:
|
---|
404 | // Connect client to selected device
|
---|
405 | devman_forward(iid, icall, false);
|
---|
406 | break;
|
---|
407 | case DEVMAN_CONNECT_TO_PARENTS_DEVICE:
|
---|
408 | // Connect client to selected device
|
---|
409 | devman_forward(iid, icall, true);
|
---|
410 | break;
|
---|
411 | default:
|
---|
412 | /* No such interface */
|
---|
413 | ipc_answer_0(iid, ENOENT);
|
---|
414 | }
|
---|
415 | }
|
---|
416 |
|
---|
417 | /** Initialize device manager internal structures.
|
---|
418 | */
|
---|
419 | static bool devman_init()
|
---|
420 | {
|
---|
421 | printf(NAME ": devman_init - looking for available drivers. \n");
|
---|
422 |
|
---|
423 | // initialize list of available drivers
|
---|
424 | init_driver_list(&drivers_list);
|
---|
425 | if (0 == lookup_available_drivers(&drivers_list, DRIVER_DEFAULT_STORE)) {
|
---|
426 | printf(NAME " no drivers found.");
|
---|
427 | return false;
|
---|
428 | }
|
---|
429 | printf(NAME ": devman_init - list of drivers has been initialized. \n");
|
---|
430 |
|
---|
431 | // create root device node
|
---|
432 | if (!init_device_tree(&device_tree, &drivers_list)) {
|
---|
433 | printf(NAME " failed to initialize device tree.");
|
---|
434 | return false;
|
---|
435 | }
|
---|
436 |
|
---|
437 | return true;
|
---|
438 | }
|
---|
439 |
|
---|
440 | int main(int argc, char *argv[])
|
---|
441 | {
|
---|
442 | printf(NAME ": HelenOS Device Manager\n");
|
---|
443 |
|
---|
444 | if (!devman_init()) {
|
---|
445 | printf(NAME ": Error while initializing service\n");
|
---|
446 | return -1;
|
---|
447 | }
|
---|
448 |
|
---|
449 | // Set a handler of incomming connections
|
---|
450 | async_set_client_connection(devman_connection);
|
---|
451 |
|
---|
452 | // Register device manager at naming service
|
---|
453 | ipcarg_t phonead;
|
---|
454 | if (ipc_connect_to_me(PHONE_NS, SERVICE_DEVMAN, 0, 0, &phonead) != 0)
|
---|
455 | return -1;
|
---|
456 |
|
---|
457 | printf(NAME ": Accepting connections\n");
|
---|
458 | async_manager();
|
---|
459 |
|
---|
460 | // Never reached
|
---|
461 | return 0;
|
---|
462 | }
|
---|
463 |
|
---|
464 | /** @}
|
---|
465 | */ |
---|