source: mainline/uspace/srv/devman/main.c@ 957cfa58

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 957cfa58 was 957cfa58, checked in by Lenka Trochtova <trochtova.lenka@…>, 15 years ago

devman - use hash table to lookup device according to its handle.

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