source: mainline/uspace/srv/devman/devman.c@ ab3a851

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since ab3a851 was ab3a851, checked in by Vojtech Horky <vojtechhorky@…>, 15 years ago

Merge mainline changes

Local changes: fixed printf related warnings

  • Property mode set to 100644
File size: 28.6 KB
RevLine 
[0358da0]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
[e2b9a993]29/** @addtogroup devman
[0358da0]30 * @{
31 */
32
33#include <errno.h>
[e4c4247]34#include <fcntl.h>
[85e48a9]35#include <sys/stat.h>
[084ff99]36#include <ipc/driver.h>
37#include <ipc/devman.h>
[a32defa]38#include <devmap.h>
[0485135]39#include <str_error.h>
[0358da0]40
[e2b9a993]41#include "devman.h"
[0358da0]42
[38b3baf]43/* hash table operations */
[957cfa58]44
45static hash_index_t devices_hash(unsigned long key[])
46{
47 return key[0] % DEVICE_BUCKETS;
48}
49
[58b833c]50static int devman_devices_compare(unsigned long key[], hash_count_t keys,
51 link_t *item)
[957cfa58]52{
53 node_t *dev = hash_table_get_instance(item, node_t, devman_link);
[0b5a4131]54 return (dev->handle == (devman_handle_t) key[0]);
[957cfa58]55}
56
[58b833c]57static int devmap_devices_compare(unsigned long key[], hash_count_t keys,
58 link_t *item)
[957cfa58]59{
60 node_t *dev = hash_table_get_instance(item, node_t, devmap_link);
[991f645]61 return (dev->devmap_handle == (devmap_handle_t) key[0]);
[957cfa58]62}
63
64static void devices_remove_callback(link_t *item)
65{
66}
67
68static hash_table_operations_t devman_devices_ops = {
69 .hash = devices_hash,
70 .compare = devman_devices_compare,
71 .remove_callback = devices_remove_callback
72};
73
74static hash_table_operations_t devmap_devices_ops = {
75 .hash = devices_hash,
76 .compare = devmap_devices_compare,
77 .remove_callback = devices_remove_callback
78};
79
[791f58c]80/**
81 * Initialize the list of device driver's.
82 *
83 * @param drv_list the list of device driver's.
84 *
85 */
86void init_driver_list(driver_list_t *drv_list)
87{
88 assert(drv_list != NULL);
89
90 list_initialize(&drv_list->drivers);
91 fibril_mutex_initialize(&drv_list->drivers_mutex);
92}
93
[0c3666d]94/** Allocate and initialize a new driver structure.
[38b3baf]95 *
[58b833c]96 * @return Driver structure.
[0c3666d]97 */
[38b3baf]98driver_t *create_driver(void)
[58b833c]99{
[e4c4247]100 driver_t *res = malloc(sizeof(driver_t));
[38b3baf]101 if (res != NULL)
[08d9c4e6]102 init_driver(res);
[e4c4247]103 return res;
104}
105
[0c3666d]106/** Add a driver to the list of drivers.
[38b3baf]107 *
[58b833c]108 * @param drivers_list List of drivers.
109 * @param drv Driver structure.
[0c3666d]110 */
111void add_driver(driver_list_t *drivers_list, driver_t *drv)
112{
113 fibril_mutex_lock(&drivers_list->drivers_mutex);
114 list_prepend(&drv->drivers, &drivers_list->drivers);
115 fibril_mutex_unlock(&drivers_list->drivers_mutex);
[58b833c]116
[38b3baf]117 printf(NAME": the '%s' driver was added to the list of available "
118 "drivers.\n", drv->name);
[0c3666d]119}
120
[38b3baf]121/** Read match id at the specified position of a string and set the position in
122 * the string to the first character following the id.
123 *
124 * @param buf The position in the input string.
125 * @return The match id.
[0c3666d]126 */
[38b3baf]127char *read_match_id(char **buf)
[e4c4247]128{
129 char *res = NULL;
[e2b9a993]130 size_t len = get_nonspace_len(*buf);
[38b3baf]131
[e4c4247]132 if (len > 0) {
133 res = malloc(len + 1);
134 if (res != NULL) {
[38b3baf]135 str_ncpy(res, len + 1, *buf, len);
[e4c4247]136 *buf += len;
137 }
138 }
[38b3baf]139
[e4c4247]140 return res;
141}
142
[0c3666d]143/**
144 * Read match ids and associated match scores from a string.
[38b3baf]145 *
146 * Each match score in the string is followed by its match id.
147 * The match ids and match scores are separated by whitespaces.
148 * Neither match ids nor match scores can contain whitespaces.
149 *
150 * @param buf The string from which the match ids are read.
151 * @param ids The list of match ids into which the match ids and
152 * scores are added.
153 * @return True if at least one match id and associated match score
154 * was successfully read, false otherwise.
[0c3666d]155 */
[c47e1a8]156bool parse_match_ids(char *buf, match_id_list_t *ids)
[e4c4247]157{
158 int score = 0;
159 char *id = NULL;
160 int ids_read = 0;
161
162 while (true) {
[38b3baf]163 /* skip spaces */
164 if (!skip_spaces(&buf))
[e4c4247]165 break;
[38b3baf]166
167 /* read score */
[e4c4247]168 score = strtoul(buf, &buf, 10);
169
[38b3baf]170 /* skip spaces */
171 if (!skip_spaces(&buf))
[e4c4247]172 break;
173
[38b3baf]174 /* read id */
175 id = read_match_id(&buf);
176 if (NULL == id)
177 break;
[e4c4247]178
[38b3baf]179 /* create new match_id structure */
[e4c4247]180 match_id_t *mid = create_match_id();
181 mid->id = id;
182 mid->score = score;
183
[38b3baf]184 /* add it to the list */
[e4c4247]185 add_match_id(ids, mid);
186
[38b3baf]187 ids_read++;
188 }
[e4c4247]189
190 return ids_read > 0;
191}
192
[0c3666d]193/**
194 * Read match ids and associated match scores from a file.
[38b3baf]195 *
196 * Each match score in the file is followed by its match id.
197 * The match ids and match scores are separated by whitespaces.
198 * Neither match ids nor match scores can contain whitespaces.
199 *
200 * @param buf The path to the file from which the match ids are read.
201 * @param ids The list of match ids into which the match ids and
202 * scores are added.
203 * @return True if at least one match id and associated match score
204 * was successfully read, false otherwise.
[0c3666d]205 */
[38b3baf]206bool read_match_ids(const char *conf_path, match_id_list_t *ids)
207{
[08d9c4e6]208 printf(NAME ": read_match_ids conf_path = %s.\n", conf_path);
209
[38b3baf]210 bool suc = false;
[e4c4247]211 char *buf = NULL;
212 bool opened = false;
[38b3baf]213 int fd;
[c47e1a8]214 size_t len = 0;
[e4c4247]215
216 fd = open(conf_path, O_RDONLY);
217 if (fd < 0) {
218 printf(NAME ": unable to open %s\n", conf_path);
219 goto cleanup;
[38b3baf]220 }
221 opened = true;
[e4c4247]222
223 len = lseek(fd, 0, SEEK_END);
[38b3baf]224 lseek(fd, 0, SEEK_SET);
[e4c4247]225 if (len == 0) {
226 printf(NAME ": configuration file '%s' is empty.\n", conf_path);
[38b3baf]227 goto cleanup;
[e4c4247]228 }
229
230 buf = malloc(len + 1);
231 if (buf == NULL) {
[38b3baf]232 printf(NAME ": memory allocation failed when parsing file "
233 "'%s'.\n", conf_path);
[e4c4247]234 goto cleanup;
[58b833c]235 }
[e4c4247]236
[58b833c]237 if (read(fd, buf, len) <= 0) {
[e4c4247]238 printf(NAME ": unable to read file '%s'.\n", conf_path);
239 goto cleanup;
240 }
241 buf[len] = 0;
242
243 suc = parse_match_ids(buf, ids);
244
245cleanup:
246 free(buf);
247
[58b833c]248 if (opened)
[38b3baf]249 close(fd);
[e4c4247]250
251 return suc;
252}
253
[0c3666d]254/**
255 * Get information about a driver.
[38b3baf]256 *
257 * Each driver has its own directory in the base directory.
[0c3666d]258 * The name of the driver's directory is the same as the name of the driver.
[38b3baf]259 * The driver's directory contains driver's binary (named as the driver without
260 * extension) and the configuration file with match ids for device-to-driver
261 * matching (named as the driver with a special extension).
262 *
263 * This function searches for the driver's directory and containing
264 * configuration files. If all the files needed are found, they are parsed and
265 * the information about the driver is stored in the driver's structure.
266 *
267 * @param base_path The base directory, in which we look for driver's
268 * subdirectory.
269 * @param name The name of the driver.
270 * @param drv The driver structure to fill information in.
271 *
272 * @return True on success, false otherwise.
[0c3666d]273 */
[e2b9a993]274bool get_driver_info(const char *base_path, const char *name, driver_t *drv)
[e4c4247]275{
[38b3baf]276 printf(NAME ": get_driver_info base_path = %s, name = %s.\n",
277 base_path, name);
[08d9c4e6]278
[e4c4247]279 assert(base_path != NULL && name != NULL && drv != NULL);
280
281 bool suc = false;
[38b3baf]282 char *match_path = NULL;
[e4c4247]283 size_t name_size = 0;
284
[38b3baf]285 /* Read the list of match ids from the driver's configuration file. */
286 match_path = get_abs_path(base_path, name, MATCH_EXT);
[58b833c]287 if (match_path == NULL)
[e4c4247]288 goto cleanup;
289
[38b3baf]290 if (!read_match_ids(match_path, &drv->match_ids))
[e4c4247]291 goto cleanup;
292
[38b3baf]293 /* Allocate and fill driver's name. */
294 name_size = str_size(name) + 1;
[e4c4247]295 drv->name = malloc(name_size);
[58b833c]296 if (drv->name == NULL)
[e4c4247]297 goto cleanup;
298 str_cpy(drv->name, name_size, name);
299
[38b3baf]300 /* Initialize path with driver's binary. */
301 drv->binary_path = get_abs_path(base_path, name, "");
[58b833c]302 if (drv->binary_path == NULL)
[85e48a9]303 goto cleanup;
304
[38b3baf]305 /* Check whether the driver's binary exists. */
[85e48a9]306 struct stat s;
[58b833c]307 if (stat(drv->binary_path, &s) == ENOENT) { /* FIXME!! */
[85e48a9]308 printf(NAME ": driver not found at path %s.", drv->binary_path);
309 goto cleanup;
310 }
311
[e4c4247]312 suc = true;
313
314cleanup:
315 if (!suc) {
316 free(drv->binary_path);
317 free(drv->name);
[38b3baf]318 /* Set the driver structure to the default state. */
319 init_driver(drv);
[e4c4247]320 }
321
322 free(match_path);
323
324 return suc;
325}
326
327/** Lookup drivers in the directory.
[38b3baf]328 *
329 * @param drivers_list The list of available drivers.
330 * @param dir_path The path to the directory where we search for drivers.
331 * @return Number of drivers which were found.
332 */
[0c3666d]333int lookup_available_drivers(driver_list_t *drivers_list, const char *dir_path)
[e4c4247]334{
[d347b53]335 printf(NAME ": lookup_available_drivers, dir = %s \n", dir_path);
[08d9c4e6]336
[e4c4247]337 int drv_cnt = 0;
338 DIR *dir = NULL;
339 struct dirent *diren;
340
341 dir = opendir(dir_path);
[08d9c4e6]342
[e4c4247]343 if (dir != NULL) {
344 driver_t *drv = create_driver();
[38b3baf]345 while ((diren = readdir(dir))) {
[e4c4247]346 if (get_driver_info(dir_path, diren->d_name, drv)) {
[e2b9a993]347 add_driver(drivers_list, drv);
[08d9c4e6]348 drv_cnt++;
[e4c4247]349 drv = create_driver();
[38b3baf]350 }
[e4c4247]351 }
352 delete_driver(drv);
353 closedir(dir);
354 }
355
356 return drv_cnt;
357}
358
[084ff99]359/** Create root device node in the device tree.
[38b3baf]360 *
[58b833c]361 * @param tree The device tree.
362 * @return True on success, false otherwise.
[0c3666d]363 */
[084ff99]364bool create_root_node(dev_tree_t *tree)
[e4c4247]365{
[58b833c]366 node_t *node;
367
[e85920d]368 printf(NAME ": create_root_node\n");
[58b833c]369
370 node = create_dev_node();
371 if (node != NULL) {
[c47e1a8]372 insert_dev_node(tree, node, clone_string(""), NULL);
[85e48a9]373 match_id_t *id = create_match_id();
[c47e1a8]374 id->id = clone_string("root");
[85e48a9]375 id->score = 100;
376 add_match_id(&node->match_ids, id);
[084ff99]377 tree->root_node = node;
[85e48a9]378 }
[58b833c]379
[38b3baf]380 return node != NULL;
[85e48a9]381}
382
[38b3baf]383/** Lookup the best matching driver for the specified device in the list of
384 * drivers.
[0c3666d]385 *
[38b3baf]386 * A match between a device and a driver is found if one of the driver's match
387 * ids match one of the device's match ids. The score of the match is the
388 * product of the driver's and device's score associated with the matching id.
389 * The best matching driver for a device is the driver with the highest score
390 * of the match between the device and the driver.
391 *
392 * @param drivers_list The list of drivers, where we look for the driver
393 * suitable for handling the device.
394 * @param node The device node structure of the device.
395 * @return The best matching driver or NULL if no matching driver
396 * is found.
[0c3666d]397 */
[38b3baf]398driver_t *find_best_match_driver(driver_list_t *drivers_list, node_t *node)
[e4c4247]399{
[85e48a9]400 driver_t *best_drv = NULL, *drv = NULL;
401 int best_score = 0, score = 0;
402
[0c3666d]403 fibril_mutex_lock(&drivers_list->drivers_mutex);
[729fa2d6]404
[38b3baf]405 link_t *link = drivers_list->drivers.next;
[0c3666d]406 while (link != &drivers_list->drivers) {
[85e48a9]407 drv = list_get_instance(link, driver_t, drivers);
408 score = get_match_score(drv, node);
409 if (score > best_score) {
410 best_score = score;
411 best_drv = drv;
[58b833c]412 }
[e85920d]413 link = link->next;
[0c3666d]414 }
[729fa2d6]415
[0c3666d]416 fibril_mutex_unlock(&drivers_list->drivers_mutex);
[e4c4247]417
[38b3baf]418 return best_drv;
[85e48a9]419}
420
[38b3baf]421/** Assign a driver to a device.
422 *
423 * @param node The device's node in the device tree.
424 * @param drv The driver.
[0c3666d]425 */
[38b3baf]426void attach_driver(node_t *node, driver_t *drv)
[85e48a9]427{
[38b3baf]428 printf(NAME ": attach_driver %s to device %s\n",
429 drv->name, node->pathname);
[2480e19]430
[0c3666d]431 fibril_mutex_lock(&drv->driver_mutex);
432
[85e48a9]433 node->drv = drv;
434 list_append(&node->driver_devices, &drv->devices);
[0c3666d]435
436 fibril_mutex_unlock(&drv->driver_mutex);
[85e48a9]437}
438
[38b3baf]439/** Start a driver
440 *
[0c3666d]441 * The driver's mutex is assumed to be locked.
[38b3baf]442 *
443 * @param drv The driver's structure.
444 * @return True if the driver's task is successfully spawned, false
445 * otherwise.
[0c3666d]446 */
[e2b9a993]447bool start_driver(driver_t *drv)
[85e48a9]448{
[0485135]449 int rc;
450
[d347b53]451 printf(NAME ": start_driver '%s'\n", drv->name);
[e85920d]452
[0485135]453 rc = task_spawnl(NULL, drv->binary_path, drv->binary_path, NULL);
454 if (rc != EOK) {
455 printf(NAME ": error spawning %s (%s)\n",
456 drv->name, str_error(rc));
[85e48a9]457 return false;
458 }
459
[e85920d]460 drv->state = DRIVER_STARTING;
[85e48a9]461 return true;
462}
463
[bda60d9]464/** Find device driver in the list of device drivers.
[38b3baf]465 *
466 * @param drv_list The list of device drivers.
467 * @param drv_name The name of the device driver which is searched.
468 * @return The device driver of the specified name, if it is in the
469 * list, NULL otherwise.
[bda60d9]470 */
[38b3baf]471driver_t *find_driver(driver_list_t *drv_list, const char *drv_name)
472{
[729fa2d6]473 driver_t *res = NULL;
[58b833c]474 driver_t *drv = NULL;
475 link_t *link;
[729fa2d6]476
[38b3baf]477 fibril_mutex_lock(&drv_list->drivers_mutex);
[729fa2d6]478
[58b833c]479 link = drv_list->drivers.next;
480 while (link != &drv_list->drivers) {
[729fa2d6]481 drv = list_get_instance(link, driver_t, drivers);
[58b833c]482 if (str_cmp(drv->name, drv_name) == 0) {
[729fa2d6]483 res = drv;
484 break;
[58b833c]485 }
486
[729fa2d6]487 link = link->next;
[58b833c]488 }
[729fa2d6]489
490 fibril_mutex_unlock(&drv_list->drivers_mutex);
491
492 return res;
493}
494
[bda60d9]495/** Remember the driver's phone.
[38b3baf]496 *
497 * @param driver The driver.
498 * @param phone The phone to the driver.
[bda60d9]499 */
[c16cf62]500void set_driver_phone(driver_t *driver, ipcarg_t phone)
[38b3baf]501{
502 fibril_mutex_lock(&driver->driver_mutex);
[58b833c]503 assert(driver->state == DRIVER_STARTING);
[38b3baf]504 driver->phone = phone;
505 fibril_mutex_unlock(&driver->driver_mutex);
[c16cf62]506}
507
[38b3baf]508/** Notify driver about the devices to which it was assigned.
509 *
[c16cf62]510 * The driver's mutex must be locked.
[38b3baf]511 *
512 * @param driver The driver to which the devices are passed.
[c16cf62]513 */
[a32defa]514static void pass_devices_to_driver(driver_t *driver, dev_tree_t *tree)
[38b3baf]515{
[c16cf62]516 node_t *dev;
517 link_t *link;
[58b833c]518 int phone;
519
520 printf(NAME ": pass_devices_to_driver\n");
521
522 phone = ipc_connect_me_to(driver->phone, DRIVER_DEVMAN, 0, 0);
523 if (phone > 0) {
[084ff99]524
525 link = driver->devices.next;
526 while (link != &driver->devices) {
527 dev = list_get_instance(link, node_t, driver_devices);
[a32defa]528 add_device(phone, driver, dev, tree);
[084ff99]529 link = link->next;
530 }
531
532 ipc_hangup(phone);
533 }
[c16cf62]534}
535
[38b3baf]536/** Finish the initialization of a driver after it has succesfully started
[bda60d9]537 * and after it has registered itself by the device manager.
[38b3baf]538 *
539 * Pass devices formerly matched to the driver to the driver and remember the
540 * driver is running and fully functional now.
541 *
542 * @param driver The driver which registered itself as running by the
543 * device manager.
[c16cf62]544 */
[38b3baf]545void initialize_running_driver(driver_t *driver, dev_tree_t *tree)
546{
[d347b53]547 printf(NAME ": initialize_running_driver\n");
[c16cf62]548 fibril_mutex_lock(&driver->driver_mutex);
549
[38b3baf]550 /*
551 * Pass devices which have been already assigned to the driver to the
552 * driver.
553 */
554 pass_devices_to_driver(driver, tree);
[c16cf62]555
[38b3baf]556 /* Change driver's state to running. */
557 driver->state = DRIVER_RUNNING;
[c16cf62]558
559 fibril_mutex_unlock(&driver->driver_mutex);
560}
561
[791f58c]562/** Initialize device driver structure.
563 *
564 * @param drv The device driver structure.
565 */
566void init_driver(driver_t *drv)
567{
568 assert(drv != NULL);
569
570 memset(drv, 0, sizeof(driver_t));
571 list_initialize(&drv->match_ids.ids);
572 list_initialize(&drv->devices);
573 fibril_mutex_initialize(&drv->driver_mutex);
574}
575
576/** Device driver structure clean-up.
577 *
578 * @param drv The device driver structure.
579 */
580void clean_driver(driver_t *drv)
581{
582 assert(drv != NULL);
583
584 free_not_null(drv->name);
585 free_not_null(drv->binary_path);
586
587 clean_match_ids(&drv->match_ids);
588
589 init_driver(drv);
590}
591
592/** Delete device driver structure.
593 *
594 * @param drv The device driver structure.
595 */
596void delete_driver(driver_t *drv)
597{
598 assert(drv != NULL);
599
600 clean_driver(drv);
601 free(drv);
602}
[a32defa]603
[38b3baf]604/** Create devmap path and name for the device. */
[a32defa]605static void devmap_register_tree_device(node_t *node, dev_tree_t *tree)
606{
607 char *devmap_pathname = NULL;
608 char *devmap_name = NULL;
609
610 asprintf(&devmap_name, "%s", node->pathname);
[58b833c]611 if (devmap_name == NULL)
[a32defa]612 return;
613
614 replace_char(devmap_name, '/', DEVMAP_SEPARATOR);
615
[38b3baf]616 asprintf(&devmap_pathname, "%s/%s", DEVMAP_DEVICE_NAMESPACE,
617 devmap_name);
[58b833c]618 if (devmap_pathname == NULL) {
[a32defa]619 free(devmap_name);
620 return;
[38b3baf]621 }
[a32defa]622
623 devmap_device_register(devmap_pathname, &node->devmap_handle);
624
625 tree_add_devmap_device(tree, node);
626
627 free(devmap_name);
[38b3baf]628 free(devmap_pathname);
[a32defa]629}
630
631
[0c3666d]632/** Pass a device to running driver.
[38b3baf]633 *
634 * @param drv The driver's structure.
635 * @param node The device's node in the device tree.
[0c3666d]636 */
[a32defa]637void add_device(int phone, driver_t *drv, node_t *node, dev_tree_t *tree)
[85e48a9]638{
[e85920d]639 printf(NAME ": add_device\n");
[a78fa2a]640
641 ipcarg_t rc;
642 ipc_call_t answer;
643
[38b3baf]644 /* Send the device to the driver. */
[0d6915f]645 devman_handle_t parent_handle;
646 if (node->parent) {
647 parent_handle = node->parent->handle;
648 } else {
649 parent_handle = 0;
650 }
651 aid_t req = async_send_2(phone, DRIVER_ADD_DEVICE, node->handle,
652 parent_handle, &answer);
[a78fa2a]653
[38b3baf]654 /* Send the device's name to the driver. */
655 rc = async_data_write_start(phone, node->name,
656 str_size(node->name) + 1);
657 if (rc != EOK) {
658 /* TODO handle error */
659 }
[a78fa2a]660
[38b3baf]661 /* Wait for answer from the driver. */
[a78fa2a]662 async_wait_for(req, &rc);
663 switch(rc) {
664 case EOK:
[df747b9c]665 node->state = DEVICE_USABLE;
[a32defa]666 devmap_register_tree_device(node, tree);
[df747b9c]667 break;
[a78fa2a]668 case ENOENT:
[df747b9c]669 node->state = DEVICE_NOT_PRESENT;
[a78fa2a]670 break;
[df747b9c]671 default:
[38b3baf]672 node->state = DEVICE_INVALID;
[084ff99]673 }
[e85920d]674
[5cd136ab]675 return;
[85e48a9]676}
677
[38b3baf]678/** Find suitable driver for a device and assign the driver to it.
679 *
680 * @param node The device node of the device in the device tree.
681 * @param drivers_list The list of available drivers.
682 * @return True if the suitable driver is found and
683 * successfully assigned to the device, false otherwise.
[0c3666d]684 */
[38b3baf]685bool assign_driver(node_t *node, driver_list_t *drivers_list, dev_tree_t *tree)
[85e48a9]686{
[38b3baf]687 /*
688 * Find the driver which is the most suitable for handling this device.
689 */
[e2b9a993]690 driver_t *drv = find_best_match_driver(drivers_list, node);
[58b833c]691 if (drv == NULL) {
[38b3baf]692 printf(NAME ": no driver found for device '%s'.\n",
693 node->pathname);
694 return false;
[85e48a9]695 }
696
[38b3baf]697 /* Attach the driver to the device. */
[85e48a9]698 attach_driver(node, drv);
699
[58b833c]700 if (drv->state == DRIVER_NOT_STARTED) {
[38b3baf]701 /* Start the driver. */
[85e48a9]702 start_driver(drv);
[38b3baf]703 }
[e85920d]704
[58b833c]705 if (drv->state == DRIVER_RUNNING) {
[38b3baf]706 /* Notify the driver about the new device. */
[084ff99]707 int phone = ipc_connect_me_to(drv->phone, DRIVER_DEVMAN, 0, 0);
708 if (phone > 0) {
[38b3baf]709 add_device(phone, drv, node, tree);
[084ff99]710 ipc_hangup(phone);
711 }
[85e48a9]712 }
713
714 return true;
715}
716
[38b3baf]717/** Initialize the device tree.
718 *
[0c3666d]719 * Create root device node of the tree and assign driver to it.
[38b3baf]720 *
721 * @param tree The device tree.
722 * @param drivers_list the list of available drivers.
723 * @return True on success, false otherwise.
[0c3666d]724 */
725bool init_device_tree(dev_tree_t *tree, driver_list_t *drivers_list)
[85e48a9]726{
[e85920d]727 printf(NAME ": init_device_tree.\n");
[0c3666d]728
[957cfa58]729 tree->current_handle = 0;
730
[38b3baf]731 hash_table_create(&tree->devman_devices, DEVICE_BUCKETS, 1,
732 &devman_devices_ops);
733 hash_table_create(&tree->devmap_devices, DEVICE_BUCKETS, 1,
734 &devmap_devices_ops);
[bda60d9]735
[957cfa58]736 fibril_rwlock_initialize(&tree->rwlock);
[084ff99]737
[38b3baf]738 /* Create root node and add it to the device tree. */
739 if (!create_root_node(tree))
[85e48a9]740 return false;
[e4c4247]741
[38b3baf]742 /* Find suitable driver and start it. */
[a32defa]743 return assign_driver(tree->root_node, drivers_list, tree);
[e4c4247]744}
745
[791f58c]746/* Device nodes */
747
748/** Create a new device node.
749 *
750 * @return A device node structure.
751 */
752node_t *create_dev_node(void)
753{
754 node_t *res = malloc(sizeof(node_t));
755
756 if (res != NULL) {
757 memset(res, 0, sizeof(node_t));
758 list_initialize(&res->children);
759 list_initialize(&res->match_ids.ids);
760 list_initialize(&res->classes);
761 }
762
763 return res;
764}
765
766/** Delete a device node.
767 *
768 * @param node The device node structure.
769 */
770void delete_dev_node(node_t *node)
771{
772 assert(list_empty(&node->children));
773 assert(node->parent == NULL);
774 assert(node->drv == NULL);
775
776 clean_match_ids(&node->match_ids);
777 free_not_null(node->name);
778 free_not_null(node->pathname);
779 free(node);
780}
781
782/** Find the device node structure of the device witch has the specified handle.
783 *
784 * Device tree's rwlock should be held at least for reading.
785 *
786 * @param tree The device tree where we look for the device node.
787 * @param handle The handle of the device.
788 * @return The device node.
789 */
[0b5a4131]790node_t *find_dev_node_no_lock(dev_tree_t *tree, devman_handle_t handle)
[791f58c]791{
792 unsigned long key = handle;
793 link_t *link = hash_table_find(&tree->devman_devices, &key);
794 return hash_table_get_instance(link, node_t, devman_link);
795}
796
797/** Find the device node structure of the device witch has the specified handle.
798 *
799 * @param tree The device tree where we look for the device node.
800 * @param handle The handle of the device.
801 * @return The device node.
802 */
[0b5a4131]803node_t *find_dev_node(dev_tree_t *tree, devman_handle_t handle)
[791f58c]804{
805 node_t *node = NULL;
806
807 fibril_rwlock_read_lock(&tree->rwlock);
808 node = find_dev_node_no_lock(tree, handle);
809 fibril_rwlock_read_unlock(&tree->rwlock);
810
811 return node;
812}
813
814
[bda60d9]815/** Create and set device's full path in device tree.
[38b3baf]816 *
817 * @param node The device's device node.
818 * @param parent The parent device node.
819 * @return True on success, false otherwise (insufficient
820 * resources etc.).
[bda60d9]821 */
822static bool set_dev_path(node_t *node, node_t *parent)
[38b3baf]823{
[58b833c]824 assert(node->name != NULL);
[bda60d9]825
[38b3baf]826 size_t pathsize = (str_size(node->name) + 1);
[58b833c]827 if (parent != NULL)
[38b3baf]828 pathsize += str_size(parent->pathname) + 1;
[bda60d9]829
[38b3baf]830 node->pathname = (char *) malloc(pathsize);
[58b833c]831 if (node->pathname == NULL) {
[bda60d9]832 printf(NAME ": failed to allocate device path.\n");
833 return false;
834 }
835
[58b833c]836 if (parent != NULL) {
[bda60d9]837 str_cpy(node->pathname, pathsize, parent->pathname);
838 str_append(node->pathname, pathsize, "/");
839 str_append(node->pathname, pathsize, node->name);
840 } else {
841 str_cpy(node->pathname, pathsize, node->name);
842 }
843
844 return true;
845}
846
847/** Insert new device into device tree.
[38b3baf]848 *
849 * The device tree's rwlock should be already held exclusively when calling this
850 * function.
851 *
852 * @param tree The device tree.
853 * @param node The newly added device node.
854 * @param dev_name The name of the newly added device.
855 * @param parent The parent device node.
[58b833c]856 *
[38b3baf]857 * @return True on success, false otherwise (insufficient resources
858 * etc.).
[bda60d9]859 */
[58b833c]860bool insert_dev_node(dev_tree_t *tree, node_t *node, char *dev_name,
861 node_t *parent)
[bda60d9]862{
[58b833c]863 assert(node != NULL);
864 assert(tree != NULL);
865 assert(dev_name != NULL);
[bda60d9]866
867 node->name = dev_name;
868 if (!set_dev_path(node, parent)) {
[957cfa58]869 fibril_rwlock_write_unlock(&tree->rwlock);
[38b3baf]870 return false;
[bda60d9]871 }
872
[38b3baf]873 /* Add the node to the handle-to-node map. */
[957cfa58]874 node->handle = ++tree->current_handle;
875 unsigned long key = node->handle;
876 hash_table_insert(&tree->devman_devices, &key, &node->devman_link);
[bda60d9]877
[38b3baf]878 /* Add the node to the list of its parent's children. */
[bda60d9]879 node->parent = parent;
[58b833c]880 if (parent != NULL)
[38b3baf]881 list_append(&node->sibling, &parent->children);
882
[bda60d9]883 return true;
884}
885
[38b3baf]886/** Find device node with a specified path in the device tree.
[5cd136ab]887 *
[38b3baf]888 * @param path The path of the device node in the device tree.
889 * @param tree The device tree.
890 * @return The device node if it is present in the tree, NULL
891 * otherwise.
[5cd136ab]892 */
[38b3baf]893node_t *find_dev_node_by_path(dev_tree_t *tree, char *path)
[5cd136ab]894{
[957cfa58]895 fibril_rwlock_read_lock(&tree->rwlock);
896
[5cd136ab]897 node_t *dev = tree->root_node;
[38b3baf]898 /*
899 * Relative path to the device from its parent (but with '/' at the
900 * beginning)
901 */
[5cd136ab]902 char *rel_path = path;
903 char *next_path_elem = NULL;
[58b833c]904 bool cont = (rel_path[0] == '/');
[5cd136ab]905
[58b833c]906 while (cont && dev != NULL) {
[38b3baf]907 next_path_elem = get_path_elem_end(rel_path + 1);
[58b833c]908 if (next_path_elem[0] == '/') {
[5cd136ab]909 cont = true;
910 next_path_elem[0] = 0;
911 } else {
912 cont = false;
913 }
914
[38b3baf]915 dev = find_node_child(dev, rel_path + 1);
[5cd136ab]916
917 if (cont) {
[38b3baf]918 /* Restore the original path. */
[5cd136ab]919 next_path_elem[0] = '/';
920 }
[38b3baf]921 rel_path = next_path_elem;
[5cd136ab]922 }
923
[957cfa58]924 fibril_rwlock_read_unlock(&tree->rwlock);
925
[5cd136ab]926 return dev;
927}
928
[38b3baf]929/** Find child device node with a specified name.
930 *
931 * Device tree rwlock should be held at least for reading.
932 *
933 * @param parent The parent device node.
934 * @param name The name of the child device node.
935 * @return The child device node.
[5cd136ab]936 */
937node_t *find_node_child(node_t *parent, const char *name)
938{
939 node_t *dev;
940 link_t *link;
[38b3baf]941
[5cd136ab]942 link = parent->children.next;
943
944 while (link != &parent->children) {
945 dev = list_get_instance(link, node_t, sibling);
946
[58b833c]947 if (str_cmp(name, dev->name) == 0)
[38b3baf]948 return dev;
[2480e19]949
950 link = link->next;
[38b3baf]951 }
952
[5cd136ab]953 return NULL;
954}
955
[791f58c]956/* Device classes */
957
958/** Create device class.
959 *
960 * @return Device class.
961 */
962dev_class_t *create_dev_class(void)
963{
964 dev_class_t *cl;
965
966 cl = (dev_class_t *) malloc(sizeof(dev_class_t));
967 if (cl != NULL) {
968 memset(cl, 0, sizeof(dev_class_t));
969 list_initialize(&cl->devices);
970 fibril_mutex_initialize(&cl->mutex);
971 }
972
973 return cl;
974}
975
976/** Create device class info.
977 *
978 * @return Device class info.
979 */
980dev_class_info_t *create_dev_class_info(void)
981{
982 dev_class_info_t *info;
983
984 info = (dev_class_info_t *) malloc(sizeof(dev_class_info_t));
985 if (info != NULL)
986 memset(info, 0, sizeof(dev_class_info_t));
987
988 return info;
989}
990
991size_t get_new_class_dev_idx(dev_class_t *cl)
992{
993 size_t dev_idx;
994
995 fibril_mutex_lock(&cl->mutex);
996 dev_idx = ++cl->curr_dev_idx;
997 fibril_mutex_unlock(&cl->mutex);
998
999 return dev_idx;
1000}
1001
1002
[38b3baf]1003/** Create unique device name within the class.
1004 *
1005 * @param cl The class.
1006 * @param base_dev_name Contains the base name for the device if it was
1007 * specified by the driver when it registered the device by
1008 * the class; NULL if driver specified no base name.
1009 * @return The unique name for the device within the class.
[d51ee2b]1010 */
[38b3baf]1011char *create_dev_name_for_class(dev_class_t *cl, const char *base_dev_name)
[d51ee2b]1012{
1013 char *dev_name;
1014 const char *base_name;
[38b3baf]1015
[58b833c]1016 if (base_dev_name != NULL)
[d51ee2b]1017 base_name = base_dev_name;
[38b3baf]1018 else
[d51ee2b]1019 base_name = cl->base_dev_name;
1020
1021 size_t idx = get_new_class_dev_idx(cl);
[7e752b2]1022 asprintf(&dev_name, "%s%zu", base_name, idx);
[38b3baf]1023
1024 return dev_name;
[d51ee2b]1025}
1026
1027/** Add the device to the class.
[38b3baf]1028 *
1029 * The device may be added to multiple classes and a class may contain multiple
1030 * devices. The class and the device are associated with each other by the
1031 * dev_class_info_t structure.
1032 *
1033 * @param dev The device.
1034 * @param class The class.
1035 * @param base_dev_name The base name of the device within the class if
1036 * specified by the driver, NULL otherwise.
1037 * @return dev_class_info_t structure which associates the device
1038 * with the class.
[d51ee2b]1039 */
[58b833c]1040dev_class_info_t *add_device_to_class(node_t *dev, dev_class_t *cl,
1041 const char *base_dev_name)
[38b3baf]1042{
[d51ee2b]1043 dev_class_info_t *info = create_dev_class_info();
[38b3baf]1044
[58b833c]1045 if (info != NULL) {
[692c40cb]1046 info->dev_class = cl;
1047 info->dev = dev;
1048
[38b3baf]1049 /* Add the device to the class. */
[692c40cb]1050 fibril_mutex_lock(&cl->mutex);
1051 list_append(&info->link, &cl->devices);
1052 fibril_mutex_unlock(&cl->mutex);
1053
[38b3baf]1054 /* Add the class to the device. */
[692c40cb]1055 list_append(&info->dev_classes, &dev->classes);
1056
[38b3baf]1057 /* Create unique name for the device within the class. */
1058 info->dev_name = create_dev_name_for_class(cl, base_dev_name);
[692c40cb]1059 }
[d51ee2b]1060
1061 return info;
1062}
1063
[38b3baf]1064dev_class_t *get_dev_class(class_list_t *class_list, char *class_name)
[692c40cb]1065{
1066 dev_class_t *cl;
[38b3baf]1067
1068 fibril_rwlock_write_lock(&class_list->rwlock);
[692c40cb]1069 cl = find_dev_class_no_lock(class_list, class_name);
[58b833c]1070 if (cl == NULL) {
[692c40cb]1071 cl = create_dev_class();
[58b833c]1072 if (cl != NULL) {
[38b3baf]1073 cl->name = class_name;
[692c40cb]1074 cl->base_dev_name = "";
1075 add_dev_class_no_lock(class_list, cl);
[38b3baf]1076 }
1077 }
[58b833c]1078
[ce89036b]1079 fibril_rwlock_write_unlock(&class_list->rwlock);
[692c40cb]1080 return cl;
1081}
1082
[58b833c]1083dev_class_t *find_dev_class_no_lock(class_list_t *class_list,
1084 const char *class_name)
[692c40cb]1085{
1086 dev_class_t *cl;
1087 link_t *link = class_list->classes.next;
[38b3baf]1088
[692c40cb]1089 while (link != &class_list->classes) {
1090 cl = list_get_instance(link, dev_class_t, link);
[58b833c]1091 if (str_cmp(cl->name, class_name) == 0)
[692c40cb]1092 return cl;
1093 }
1094
[38b3baf]1095 return NULL;
[692c40cb]1096}
1097
[791f58c]1098void add_dev_class_no_lock(class_list_t *class_list, dev_class_t *cl)
1099{
1100 list_append(&cl->link, &class_list->classes);
1101}
1102
[ce89036b]1103void init_class_list(class_list_t *class_list)
1104{
1105 list_initialize(&class_list->classes);
1106 fibril_rwlock_initialize(&class_list->rwlock);
[38b3baf]1107 hash_table_create(&class_list->devmap_devices, DEVICE_BUCKETS, 1,
1108 &devmap_devices_ops);
[ce89036b]1109}
1110
1111
[791f58c]1112/* Devmap devices */
[ce89036b]1113
[991f645]1114node_t *find_devmap_tree_device(dev_tree_t *tree, devmap_handle_t devmap_handle)
[ce89036b]1115{
1116 node_t *dev = NULL;
1117 link_t *link;
[38b3baf]1118 unsigned long key = (unsigned long) devmap_handle;
[ce89036b]1119
1120 fibril_rwlock_read_lock(&tree->rwlock);
[38b3baf]1121 link = hash_table_find(&tree->devmap_devices, &key);
[58b833c]1122 if (link != NULL)
[ce89036b]1123 dev = hash_table_get_instance(link, node_t, devmap_link);
1124 fibril_rwlock_read_unlock(&tree->rwlock);
1125
1126 return dev;
1127}
1128
[58b833c]1129node_t *find_devmap_class_device(class_list_t *classes,
[991f645]1130 devmap_handle_t devmap_handle)
[ce89036b]1131{
1132 node_t *dev = NULL;
1133 dev_class_info_t *cli;
1134 link_t *link;
1135 unsigned long key = (unsigned long)devmap_handle;
1136
1137 fibril_rwlock_read_lock(&classes->rwlock);
[38b3baf]1138 link = hash_table_find(&classes->devmap_devices, &key);
[58b833c]1139 if (link != NULL) {
[38b3baf]1140 cli = hash_table_get_instance(link, dev_class_info_t,
1141 devmap_link);
[ce89036b]1142 dev = cli->dev;
1143 }
1144 fibril_rwlock_read_unlock(&classes->rwlock);
1145
[38b3baf]1146 return dev;
[ce89036b]1147}
1148
[791f58c]1149void class_add_devmap_device(class_list_t *class_list, dev_class_info_t *cli)
1150{
1151 unsigned long key = (unsigned long) cli->devmap_handle;
1152
1153 fibril_rwlock_write_lock(&class_list->rwlock);
1154 hash_table_insert(&class_list->devmap_devices, &key, &cli->devmap_link);
1155 fibril_rwlock_write_unlock(&class_list->rwlock);
1156}
1157
1158void tree_add_devmap_device(dev_tree_t *tree, node_t *node)
1159{
1160 unsigned long key = (unsigned long) node->devmap_handle;
1161 fibril_rwlock_write_lock(&tree->rwlock);
1162 hash_table_insert(&tree->devmap_devices, &key, &node->devmap_link);
1163 fibril_rwlock_write_unlock(&tree->rwlock);
1164}
1165
[c16cf62]1166/** @}
[58b833c]1167 */
Note: See TracBrowser for help on using the repository browser.