source: mainline/uspace/srv/devman/devman.c@ 38b3baf

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 38b3baf was 38b3baf, checked in by Jakub Jermar <jakub@…>, 15 years ago

Cstyle fixes of devman.

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