source: mainline/uspace/srv/devman/devman.c@ 1b367b4

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 1b367b4 was ba38f72c, checked in by Jiri Svoboda <jiri@…>, 14 years ago

Split device tree node into dev_node_t (device node) and fun_node_t (function node).

  • Property mode set to 100644
File size: 34.9 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
[ba38f72c]43fun_node_t *find_node_child(fun_node_t *parent, const char *name);
44
[38b3baf]45/* hash table operations */
[957cfa58]46
47static hash_index_t devices_hash(unsigned long key[])
48{
49 return key[0] % DEVICE_BUCKETS;
50}
51
[58b833c]52static int devman_devices_compare(unsigned long key[], hash_count_t keys,
53 link_t *item)
[957cfa58]54{
[ba38f72c]55 dev_node_t *dev = hash_table_get_instance(item, dev_node_t, devman_dev);
[0b5a4131]56 return (dev->handle == (devman_handle_t) key[0]);
[957cfa58]57}
58
[ba38f72c]59static int devman_functions_compare(unsigned long key[], hash_count_t keys,
[58b833c]60 link_t *item)
[957cfa58]61{
[ba38f72c]62 fun_node_t *fun = hash_table_get_instance(item, fun_node_t, devman_fun);
63 return (fun->handle == (devman_handle_t) key[0]);
64}
65
66static int devmap_functions_compare(unsigned long key[], hash_count_t keys,
67 link_t *item)
68{
69 fun_node_t *fun = hash_table_get_instance(item, fun_node_t, devmap_fun);
70 return (fun->devmap_handle == (devmap_handle_t) key[0]);
[957cfa58]71}
72
[3ca3430]73static int devmap_devices_class_compare(unsigned long key[], hash_count_t keys,
74 link_t *item)
75{
76 dev_class_info_t *class_info
77 = hash_table_get_instance(item, dev_class_info_t, devmap_link);
78 assert(class_info != NULL);
79
80 return (class_info->devmap_handle == (devmap_handle_t) key[0]);
81}
82
[957cfa58]83static void devices_remove_callback(link_t *item)
84{
85}
86
87static hash_table_operations_t devman_devices_ops = {
88 .hash = devices_hash,
89 .compare = devman_devices_compare,
90 .remove_callback = devices_remove_callback
91};
92
[ba38f72c]93static hash_table_operations_t devman_functions_ops = {
94 .hash = devices_hash,
95 .compare = devman_functions_compare,
96 .remove_callback = devices_remove_callback
97};
98
[957cfa58]99static hash_table_operations_t devmap_devices_ops = {
100 .hash = devices_hash,
[ba38f72c]101 .compare = devmap_functions_compare,
[957cfa58]102 .remove_callback = devices_remove_callback
103};
104
[3ca3430]105static hash_table_operations_t devmap_devices_class_ops = {
106 .hash = devices_hash,
107 .compare = devmap_devices_class_compare,
108 .remove_callback = devices_remove_callback
109};
110
[791f58c]111/**
112 * Initialize the list of device driver's.
113 *
114 * @param drv_list the list of device driver's.
115 *
116 */
117void init_driver_list(driver_list_t *drv_list)
118{
119 assert(drv_list != NULL);
120
121 list_initialize(&drv_list->drivers);
122 fibril_mutex_initialize(&drv_list->drivers_mutex);
123}
124
[0c3666d]125/** Allocate and initialize a new driver structure.
[38b3baf]126 *
[58b833c]127 * @return Driver structure.
[0c3666d]128 */
[38b3baf]129driver_t *create_driver(void)
[58b833c]130{
[e4c4247]131 driver_t *res = malloc(sizeof(driver_t));
[38b3baf]132 if (res != NULL)
[08d9c4e6]133 init_driver(res);
[e4c4247]134 return res;
135}
136
[0c3666d]137/** Add a driver to the list of drivers.
[38b3baf]138 *
[58b833c]139 * @param drivers_list List of drivers.
140 * @param drv Driver structure.
[0c3666d]141 */
142void add_driver(driver_list_t *drivers_list, driver_t *drv)
143{
144 fibril_mutex_lock(&drivers_list->drivers_mutex);
145 list_prepend(&drv->drivers, &drivers_list->drivers);
146 fibril_mutex_unlock(&drivers_list->drivers_mutex);
[58b833c]147
[38b3baf]148 printf(NAME": the '%s' driver was added to the list of available "
149 "drivers.\n", drv->name);
[0c3666d]150}
151
[38b3baf]152/** Read match id at the specified position of a string and set the position in
153 * the string to the first character following the id.
154 *
155 * @param buf The position in the input string.
156 * @return The match id.
[0c3666d]157 */
[38b3baf]158char *read_match_id(char **buf)
[e4c4247]159{
160 char *res = NULL;
[e2b9a993]161 size_t len = get_nonspace_len(*buf);
[38b3baf]162
[e4c4247]163 if (len > 0) {
164 res = malloc(len + 1);
165 if (res != NULL) {
[38b3baf]166 str_ncpy(res, len + 1, *buf, len);
[e4c4247]167 *buf += len;
168 }
169 }
[38b3baf]170
[e4c4247]171 return res;
172}
173
[0c3666d]174/**
175 * Read match ids and associated match scores from a string.
[38b3baf]176 *
177 * Each match score in the string is followed by its match id.
178 * The match ids and match scores are separated by whitespaces.
179 * Neither match ids nor match scores can contain whitespaces.
180 *
181 * @param buf The string from which the match ids are read.
182 * @param ids The list of match ids into which the match ids and
183 * scores are added.
184 * @return True if at least one match id and associated match score
185 * was successfully read, false otherwise.
[0c3666d]186 */
[c47e1a8]187bool parse_match_ids(char *buf, match_id_list_t *ids)
[e4c4247]188{
189 int score = 0;
190 char *id = NULL;
191 int ids_read = 0;
192
193 while (true) {
[38b3baf]194 /* skip spaces */
195 if (!skip_spaces(&buf))
[e4c4247]196 break;
[38b3baf]197
198 /* read score */
[e4c4247]199 score = strtoul(buf, &buf, 10);
200
[38b3baf]201 /* skip spaces */
202 if (!skip_spaces(&buf))
[e4c4247]203 break;
204
[38b3baf]205 /* read id */
206 id = read_match_id(&buf);
207 if (NULL == id)
208 break;
[e4c4247]209
[38b3baf]210 /* create new match_id structure */
[e4c4247]211 match_id_t *mid = create_match_id();
212 mid->id = id;
213 mid->score = score;
214
[38b3baf]215 /* add it to the list */
[e4c4247]216 add_match_id(ids, mid);
217
[38b3baf]218 ids_read++;
219 }
[e4c4247]220
221 return ids_read > 0;
222}
223
[0c3666d]224/**
225 * Read match ids and associated match scores from a file.
[38b3baf]226 *
227 * Each match score in the file is followed by its match id.
228 * The match ids and match scores are separated by whitespaces.
229 * Neither match ids nor match scores can contain whitespaces.
230 *
231 * @param buf The path to the file from which the match ids are read.
232 * @param ids The list of match ids into which the match ids and
233 * scores are added.
234 * @return True if at least one match id and associated match score
235 * was successfully read, false otherwise.
[0c3666d]236 */
[38b3baf]237bool read_match_ids(const char *conf_path, match_id_list_t *ids)
238{
[08d9c4e6]239 printf(NAME ": read_match_ids conf_path = %s.\n", conf_path);
240
[38b3baf]241 bool suc = false;
[e4c4247]242 char *buf = NULL;
243 bool opened = false;
[38b3baf]244 int fd;
[c47e1a8]245 size_t len = 0;
[e4c4247]246
247 fd = open(conf_path, O_RDONLY);
248 if (fd < 0) {
249 printf(NAME ": unable to open %s\n", conf_path);
250 goto cleanup;
[38b3baf]251 }
252 opened = true;
[e4c4247]253
254 len = lseek(fd, 0, SEEK_END);
[38b3baf]255 lseek(fd, 0, SEEK_SET);
[e4c4247]256 if (len == 0) {
257 printf(NAME ": configuration file '%s' is empty.\n", conf_path);
[38b3baf]258 goto cleanup;
[e4c4247]259 }
260
261 buf = malloc(len + 1);
262 if (buf == NULL) {
[38b3baf]263 printf(NAME ": memory allocation failed when parsing file "
264 "'%s'.\n", conf_path);
[e4c4247]265 goto cleanup;
[58b833c]266 }
[e4c4247]267
[58b833c]268 if (read(fd, buf, len) <= 0) {
[e4c4247]269 printf(NAME ": unable to read file '%s'.\n", conf_path);
270 goto cleanup;
271 }
272 buf[len] = 0;
273
274 suc = parse_match_ids(buf, ids);
275
276cleanup:
277 free(buf);
278
[58b833c]279 if (opened)
[38b3baf]280 close(fd);
[e4c4247]281
282 return suc;
283}
284
[0c3666d]285/**
286 * Get information about a driver.
[38b3baf]287 *
288 * Each driver has its own directory in the base directory.
[0c3666d]289 * The name of the driver's directory is the same as the name of the driver.
[38b3baf]290 * The driver's directory contains driver's binary (named as the driver without
291 * extension) and the configuration file with match ids for device-to-driver
292 * matching (named as the driver with a special extension).
293 *
294 * This function searches for the driver's directory and containing
295 * configuration files. If all the files needed are found, they are parsed and
296 * the information about the driver is stored in the driver's structure.
297 *
298 * @param base_path The base directory, in which we look for driver's
299 * subdirectory.
300 * @param name The name of the driver.
301 * @param drv The driver structure to fill information in.
302 *
303 * @return True on success, false otherwise.
[0c3666d]304 */
[e2b9a993]305bool get_driver_info(const char *base_path, const char *name, driver_t *drv)
[e4c4247]306{
[38b3baf]307 printf(NAME ": get_driver_info base_path = %s, name = %s.\n",
308 base_path, name);
[08d9c4e6]309
[e4c4247]310 assert(base_path != NULL && name != NULL && drv != NULL);
311
312 bool suc = false;
[38b3baf]313 char *match_path = NULL;
[e4c4247]314 size_t name_size = 0;
315
[38b3baf]316 /* Read the list of match ids from the driver's configuration file. */
317 match_path = get_abs_path(base_path, name, MATCH_EXT);
[58b833c]318 if (match_path == NULL)
[e4c4247]319 goto cleanup;
320
[38b3baf]321 if (!read_match_ids(match_path, &drv->match_ids))
[e4c4247]322 goto cleanup;
323
[38b3baf]324 /* Allocate and fill driver's name. */
325 name_size = str_size(name) + 1;
[e4c4247]326 drv->name = malloc(name_size);
[58b833c]327 if (drv->name == NULL)
[e4c4247]328 goto cleanup;
329 str_cpy(drv->name, name_size, name);
330
[38b3baf]331 /* Initialize path with driver's binary. */
332 drv->binary_path = get_abs_path(base_path, name, "");
[58b833c]333 if (drv->binary_path == NULL)
[85e48a9]334 goto cleanup;
335
[38b3baf]336 /* Check whether the driver's binary exists. */
[85e48a9]337 struct stat s;
[58b833c]338 if (stat(drv->binary_path, &s) == ENOENT) { /* FIXME!! */
[85e48a9]339 printf(NAME ": driver not found at path %s.", drv->binary_path);
340 goto cleanup;
341 }
342
[e4c4247]343 suc = true;
344
345cleanup:
346 if (!suc) {
347 free(drv->binary_path);
348 free(drv->name);
[38b3baf]349 /* Set the driver structure to the default state. */
350 init_driver(drv);
[e4c4247]351 }
352
353 free(match_path);
354
355 return suc;
356}
357
358/** Lookup drivers in the directory.
[38b3baf]359 *
360 * @param drivers_list The list of available drivers.
361 * @param dir_path The path to the directory where we search for drivers.
362 * @return Number of drivers which were found.
363 */
[0c3666d]364int lookup_available_drivers(driver_list_t *drivers_list, const char *dir_path)
[e4c4247]365{
[d347b53]366 printf(NAME ": lookup_available_drivers, dir = %s \n", dir_path);
[08d9c4e6]367
[e4c4247]368 int drv_cnt = 0;
369 DIR *dir = NULL;
370 struct dirent *diren;
371
372 dir = opendir(dir_path);
[08d9c4e6]373
[e4c4247]374 if (dir != NULL) {
375 driver_t *drv = create_driver();
[38b3baf]376 while ((diren = readdir(dir))) {
[e4c4247]377 if (get_driver_info(dir_path, diren->d_name, drv)) {
[e2b9a993]378 add_driver(drivers_list, drv);
[08d9c4e6]379 drv_cnt++;
[e4c4247]380 drv = create_driver();
[38b3baf]381 }
[e4c4247]382 }
383 delete_driver(drv);
384 closedir(dir);
385 }
386
387 return drv_cnt;
388}
389
[ba38f72c]390/** Create root device and function node in the device tree.
[38b3baf]391 *
[58b833c]392 * @param tree The device tree.
393 * @return True on success, false otherwise.
[0c3666d]394 */
[ba38f72c]395bool create_root_nodes(dev_tree_t *tree)
[e4c4247]396{
[ba38f72c]397 fun_node_t *fun;
398 dev_node_t *dev;
399
400 printf(NAME ": create_root_nodes\n");
401
[01b87dc5]402 fibril_rwlock_write_lock(&tree->rwlock);
[ba38f72c]403
404 /*
405 * Create root function. This is a pseudo function to which
406 * the root device node is attached. It allows us to match
407 * the root device driver in a standard manner, i.e. against
408 * the parent function.
409 */
410
411 fun = create_fun_node();
412 if (fun == NULL) {
413 fibril_rwlock_write_unlock(&tree->rwlock);
414 return false;
[85e48a9]415 }
[ba38f72c]416
417 insert_fun_node(tree, fun, clone_string(""), NULL);
418 match_id_t *id = create_match_id();
419 id->id = clone_string("root");
420 id->score = 100;
421 add_match_id(&fun->match_ids, id);
422 tree->root_node = fun;
423
424 /*
425 * Create root device node.
426 */
427 dev = create_dev_node();
428 if (dev == NULL) {
429 fibril_rwlock_write_unlock(&tree->rwlock);
430 return false;
431 }
432
433 insert_dev_node(tree, dev, fun);
434
[01b87dc5]435 fibril_rwlock_write_unlock(&tree->rwlock);
[ba38f72c]436
437 return dev != NULL;
[85e48a9]438}
439
[38b3baf]440/** Lookup the best matching driver for the specified device in the list of
441 * drivers.
[0c3666d]442 *
[38b3baf]443 * A match between a device and a driver is found if one of the driver's match
444 * ids match one of the device's match ids. The score of the match is the
445 * product of the driver's and device's score associated with the matching id.
446 * The best matching driver for a device is the driver with the highest score
447 * of the match between the device and the driver.
448 *
449 * @param drivers_list The list of drivers, where we look for the driver
450 * suitable for handling the device.
451 * @param node The device node structure of the device.
452 * @return The best matching driver or NULL if no matching driver
453 * is found.
[0c3666d]454 */
[ba38f72c]455driver_t *find_best_match_driver(driver_list_t *drivers_list, dev_node_t *node)
[e4c4247]456{
[85e48a9]457 driver_t *best_drv = NULL, *drv = NULL;
458 int best_score = 0, score = 0;
459
[0c3666d]460 fibril_mutex_lock(&drivers_list->drivers_mutex);
[729fa2d6]461
[38b3baf]462 link_t *link = drivers_list->drivers.next;
[0c3666d]463 while (link != &drivers_list->drivers) {
[85e48a9]464 drv = list_get_instance(link, driver_t, drivers);
465 score = get_match_score(drv, node);
466 if (score > best_score) {
467 best_score = score;
468 best_drv = drv;
[58b833c]469 }
[e85920d]470 link = link->next;
[0c3666d]471 }
[729fa2d6]472
[0c3666d]473 fibril_mutex_unlock(&drivers_list->drivers_mutex);
[e4c4247]474
[38b3baf]475 return best_drv;
[85e48a9]476}
477
[38b3baf]478/** Assign a driver to a device.
479 *
480 * @param node The device's node in the device tree.
481 * @param drv The driver.
[0c3666d]482 */
[ba38f72c]483void attach_driver(dev_node_t *dev, driver_t *drv)
[85e48a9]484{
[38b3baf]485 printf(NAME ": attach_driver %s to device %s\n",
[ba38f72c]486 drv->name, dev->pfun->pathname);
[2480e19]487
[0c3666d]488 fibril_mutex_lock(&drv->driver_mutex);
489
[ba38f72c]490 dev->drv = drv;
491 list_append(&dev->driver_devices, &drv->devices);
[0c3666d]492
493 fibril_mutex_unlock(&drv->driver_mutex);
[85e48a9]494}
495
[38b3baf]496/** Start a driver
497 *
498 * @param drv The driver's structure.
499 * @return True if the driver's task is successfully spawned, false
500 * otherwise.
[0c3666d]501 */
[e2b9a993]502bool start_driver(driver_t *drv)
[85e48a9]503{
[0485135]504 int rc;
505
[01b87dc5]506 assert(fibril_mutex_is_locked(&drv->driver_mutex));
507
[d347b53]508 printf(NAME ": start_driver '%s'\n", drv->name);
[e85920d]509
[0485135]510 rc = task_spawnl(NULL, drv->binary_path, drv->binary_path, NULL);
511 if (rc != EOK) {
512 printf(NAME ": error spawning %s (%s)\n",
513 drv->name, str_error(rc));
[85e48a9]514 return false;
515 }
516
[e85920d]517 drv->state = DRIVER_STARTING;
[85e48a9]518 return true;
519}
520
[bda60d9]521/** Find device driver in the list of device drivers.
[38b3baf]522 *
523 * @param drv_list The list of device drivers.
524 * @param drv_name The name of the device driver which is searched.
525 * @return The device driver of the specified name, if it is in the
526 * list, NULL otherwise.
[bda60d9]527 */
[38b3baf]528driver_t *find_driver(driver_list_t *drv_list, const char *drv_name)
529{
[729fa2d6]530 driver_t *res = NULL;
[58b833c]531 driver_t *drv = NULL;
532 link_t *link;
[729fa2d6]533
[38b3baf]534 fibril_mutex_lock(&drv_list->drivers_mutex);
[729fa2d6]535
[58b833c]536 link = drv_list->drivers.next;
537 while (link != &drv_list->drivers) {
[729fa2d6]538 drv = list_get_instance(link, driver_t, drivers);
[58b833c]539 if (str_cmp(drv->name, drv_name) == 0) {
[729fa2d6]540 res = drv;
541 break;
[58b833c]542 }
543
[729fa2d6]544 link = link->next;
[58b833c]545 }
[729fa2d6]546
547 fibril_mutex_unlock(&drv_list->drivers_mutex);
548
549 return res;
550}
551
[bda60d9]552/** Remember the driver's phone.
[38b3baf]553 *
554 * @param driver The driver.
555 * @param phone The phone to the driver.
[bda60d9]556 */
[96b02eb9]557void set_driver_phone(driver_t *driver, sysarg_t phone)
[38b3baf]558{
559 fibril_mutex_lock(&driver->driver_mutex);
[58b833c]560 assert(driver->state == DRIVER_STARTING);
[38b3baf]561 driver->phone = phone;
562 fibril_mutex_unlock(&driver->driver_mutex);
[c16cf62]563}
564
[38b3baf]565/** Notify driver about the devices to which it was assigned.
566 *
567 * @param driver The driver to which the devices are passed.
[c16cf62]568 */
[a32defa]569static void pass_devices_to_driver(driver_t *driver, dev_tree_t *tree)
[38b3baf]570{
[ba38f72c]571 dev_node_t *dev;
[c16cf62]572 link_t *link;
[58b833c]573 int phone;
574
[703d19c]575 printf(NAME ": pass_devices_to_driver(`%s')\n", driver->name);
[58b833c]576
[5bee897]577 fibril_mutex_lock(&driver->driver_mutex);
578
[398c4d7]579 phone = async_connect_me_to(driver->phone, DRIVER_DEVMAN, 0, 0);
[5bee897]580
581 if (phone < 0) {
582 fibril_mutex_unlock(&driver->driver_mutex);
583 return;
584 }
585
586 /*
587 * Go through devices list as long as there is some device
588 * that has not been passed to the driver.
589 */
590 link = driver->devices.next;
591 while (link != &driver->devices) {
[ba38f72c]592 dev = list_get_instance(link, dev_node_t, driver_devices);
[5bee897]593 if (dev->passed_to_driver) {
[084ff99]594 link = link->next;
[5bee897]595 continue;
[084ff99]596 }
[5bee897]597
[2edcb63]598 /*
599 * We remove the device from the list to allow safe adding
600 * of new devices (no one will touch our item this way).
601 */
602 list_remove(link);
603
[5bee897]604 /*
605 * Unlock to avoid deadlock when adding device
606 * handled by itself.
607 */
608 fibril_mutex_unlock(&driver->driver_mutex);
609
610 add_device(phone, driver, dev, tree);
611
612 /*
613 * Lock again as we will work with driver's
614 * structure.
615 */
616 fibril_mutex_lock(&driver->driver_mutex);
617
[2edcb63]618 /*
619 * Insert the device back.
620 * The order is not relevant here so no harm is done
621 * (actually, the order would be preserved in most cases).
622 */
623 list_append(link, &driver->devices);
624
[5bee897]625 /*
626 * Restart the cycle to go through all devices again.
627 */
628 link = driver->devices.next;
[084ff99]629 }
[5bee897]630
[ffa2c8ef]631 async_hangup(phone);
[5bee897]632
633 /*
634 * Once we passed all devices to the driver, we need to mark the
635 * driver as running.
636 * It is vital to do it here and inside critical section.
637 *
638 * If we would change the state earlier, other devices added to
639 * the driver would be added to the device list and started
640 * immediately and possibly started here as well.
641 */
[398c4d7]642 printf(NAME ": driver %s goes into running state.\n", driver->name);
[5bee897]643 driver->state = DRIVER_RUNNING;
644
645 fibril_mutex_unlock(&driver->driver_mutex);
[c16cf62]646}
647
[38b3baf]648/** Finish the initialization of a driver after it has succesfully started
[bda60d9]649 * and after it has registered itself by the device manager.
[38b3baf]650 *
651 * Pass devices formerly matched to the driver to the driver and remember the
652 * driver is running and fully functional now.
653 *
654 * @param driver The driver which registered itself as running by the
655 * device manager.
[c16cf62]656 */
[38b3baf]657void initialize_running_driver(driver_t *driver, dev_tree_t *tree)
658{
[703d19c]659 printf(NAME ": initialize_running_driver (`%s')\n", driver->name);
[c16cf62]660
[38b3baf]661 /*
662 * Pass devices which have been already assigned to the driver to the
663 * driver.
664 */
665 pass_devices_to_driver(driver, tree);
[c16cf62]666}
667
[791f58c]668/** Initialize device driver structure.
669 *
670 * @param drv The device driver structure.
671 */
672void init_driver(driver_t *drv)
673{
674 assert(drv != NULL);
675
676 memset(drv, 0, sizeof(driver_t));
677 list_initialize(&drv->match_ids.ids);
678 list_initialize(&drv->devices);
679 fibril_mutex_initialize(&drv->driver_mutex);
680}
681
682/** Device driver structure clean-up.
683 *
684 * @param drv The device driver structure.
685 */
686void clean_driver(driver_t *drv)
687{
688 assert(drv != NULL);
689
690 free_not_null(drv->name);
691 free_not_null(drv->binary_path);
692
693 clean_match_ids(&drv->match_ids);
694
695 init_driver(drv);
696}
697
698/** Delete device driver structure.
699 *
700 * @param drv The device driver structure.
701 */
702void delete_driver(driver_t *drv)
703{
704 assert(drv != NULL);
705
706 clean_driver(drv);
707 free(drv);
708}
[a32defa]709
[ba38f72c]710/** Create devmap path and name for the function. */
711static void devmap_register_tree_function(fun_node_t *fun, dev_tree_t *tree)
[a32defa]712{
713 char *devmap_pathname = NULL;
714 char *devmap_name = NULL;
715
[ba38f72c]716 asprintf(&devmap_name, "%s", fun->pathname);
[58b833c]717 if (devmap_name == NULL)
[a32defa]718 return;
719
720 replace_char(devmap_name, '/', DEVMAP_SEPARATOR);
721
[38b3baf]722 asprintf(&devmap_pathname, "%s/%s", DEVMAP_DEVICE_NAMESPACE,
723 devmap_name);
[58b833c]724 if (devmap_pathname == NULL) {
[a32defa]725 free(devmap_name);
726 return;
[38b3baf]727 }
[a32defa]728
[47a7174f]729 devmap_device_register_with_iface(devmap_pathname,
[ba38f72c]730 &fun->devmap_handle, DEVMAN_CONNECT_FROM_DEVMAP);
[a32defa]731
[ba38f72c]732 tree_add_devmap_function(tree, fun);
[a32defa]733
734 free(devmap_name);
[38b3baf]735 free(devmap_pathname);
[a32defa]736}
737
[0c3666d]738/** Pass a device to running driver.
[38b3baf]739 *
740 * @param drv The driver's structure.
741 * @param node The device's node in the device tree.
[0c3666d]742 */
[ba38f72c]743void add_device(int phone, driver_t *drv, dev_node_t *dev, dev_tree_t *tree)
[85e48a9]744{
[5bee897]745 /*
746 * We do not expect to have driver's mutex locked as we do not
747 * access any structures that would affect driver_t.
748 */
749 printf(NAME ": add_device (driver `%s', device `%s')\n", drv->name,
[ba38f72c]750 dev->pfun->name);
[a78fa2a]751
[96b02eb9]752 sysarg_t rc;
[a78fa2a]753 ipc_call_t answer;
754
[38b3baf]755 /* Send the device to the driver. */
[0d6915f]756 devman_handle_t parent_handle;
[ba38f72c]757 if (dev->pfun) {
758 parent_handle = dev->pfun->handle;
[0d6915f]759 } else {
760 parent_handle = 0;
761 }
[5bee897]762
[ba38f72c]763 aid_t req = async_send_2(phone, DRIVER_ADD_DEVICE, dev->handle,
[0d6915f]764 parent_handle, &answer);
[a78fa2a]765
[38b3baf]766 /* Send the device's name to the driver. */
[ba38f72c]767 rc = async_data_write_start(phone, dev->pfun->name,
768 str_size(dev->pfun->name) + 1);
[38b3baf]769 if (rc != EOK) {
770 /* TODO handle error */
771 }
[398c4d7]772
[38b3baf]773 /* Wait for answer from the driver. */
[a78fa2a]774 async_wait_for(req, &rc);
[5bee897]775
[a78fa2a]776 switch(rc) {
777 case EOK:
[ba38f72c]778 dev->state = DEVICE_USABLE;
779 // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
780 if (0) devmap_register_tree_function(NULL, tree);
781 // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
[df747b9c]782 break;
[a78fa2a]783 case ENOENT:
[ba38f72c]784 dev->state = DEVICE_NOT_PRESENT;
[a78fa2a]785 break;
[df747b9c]786 default:
[ba38f72c]787 dev->state = DEVICE_INVALID;
[084ff99]788 }
[e85920d]789
[ba38f72c]790 dev->passed_to_driver = true;
[5bee897]791
[5cd136ab]792 return;
[85e48a9]793}
794
[38b3baf]795/** Find suitable driver for a device and assign the driver to it.
796 *
797 * @param node The device node of the device in the device tree.
798 * @param drivers_list The list of available drivers.
799 * @return True if the suitable driver is found and
800 * successfully assigned to the device, false otherwise.
[0c3666d]801 */
[ba38f72c]802bool assign_driver(dev_node_t *dev, driver_list_t *drivers_list,
803 dev_tree_t *tree)
[85e48a9]804{
[ba38f72c]805 assert(dev != NULL);
806 assert(drivers_list != NULL);
807 assert(tree != NULL);
808
[38b3baf]809 /*
810 * Find the driver which is the most suitable for handling this device.
811 */
[ba38f72c]812 driver_t *drv = find_best_match_driver(drivers_list, dev);
[58b833c]813 if (drv == NULL) {
[38b3baf]814 printf(NAME ": no driver found for device '%s'.\n",
[ba38f72c]815 dev->pfun->pathname);
[38b3baf]816 return false;
[85e48a9]817 }
818
[38b3baf]819 /* Attach the driver to the device. */
[ba38f72c]820 attach_driver(dev, drv);
[85e48a9]821
[398c4d7]822 fibril_mutex_lock(&drv->driver_mutex);
[58b833c]823 if (drv->state == DRIVER_NOT_STARTED) {
[38b3baf]824 /* Start the driver. */
[85e48a9]825 start_driver(drv);
[38b3baf]826 }
[398c4d7]827 bool is_running = drv->state == DRIVER_RUNNING;
828 fibril_mutex_unlock(&drv->driver_mutex);
829
830 if (is_running) {
[38b3baf]831 /* Notify the driver about the new device. */
[398c4d7]832 int phone = async_connect_me_to(drv->phone, DRIVER_DEVMAN, 0, 0);
[fa581b3]833 if (phone >= 0) {
[ba38f72c]834 add_device(phone, drv, dev, tree);
[ffa2c8ef]835 async_hangup(phone);
[084ff99]836 }
[85e48a9]837 }
838
839 return true;
840}
841
[38b3baf]842/** Initialize the device tree.
843 *
[0c3666d]844 * Create root device node of the tree and assign driver to it.
[38b3baf]845 *
846 * @param tree The device tree.
847 * @param drivers_list the list of available drivers.
848 * @return True on success, false otherwise.
[0c3666d]849 */
850bool init_device_tree(dev_tree_t *tree, driver_list_t *drivers_list)
[85e48a9]851{
[e85920d]852 printf(NAME ": init_device_tree.\n");
[0c3666d]853
[957cfa58]854 tree->current_handle = 0;
855
[38b3baf]856 hash_table_create(&tree->devman_devices, DEVICE_BUCKETS, 1,
857 &devman_devices_ops);
[ba38f72c]858 hash_table_create(&tree->devman_functions, DEVICE_BUCKETS, 1,
859 &devman_functions_ops);
860 hash_table_create(&tree->devmap_functions, DEVICE_BUCKETS, 1,
[38b3baf]861 &devmap_devices_ops);
[bda60d9]862
[957cfa58]863 fibril_rwlock_initialize(&tree->rwlock);
[084ff99]864
[ba38f72c]865 /* Create root function and root device and add them to the device tree. */
866 if (!create_root_nodes(tree))
[85e48a9]867 return false;
[e4c4247]868
[38b3baf]869 /* Find suitable driver and start it. */
[ba38f72c]870 return assign_driver(tree->root_node->child, drivers_list, tree);
[e4c4247]871}
872
[791f58c]873/* Device nodes */
874
875/** Create a new device node.
876 *
877 * @return A device node structure.
878 */
[ba38f72c]879dev_node_t *create_dev_node(void)
[791f58c]880{
[ba38f72c]881 dev_node_t *res = malloc(sizeof(dev_node_t));
[791f58c]882
883 if (res != NULL) {
[ba38f72c]884 memset(res, 0, sizeof(dev_node_t));
885 list_initialize(&res->functions);
886 link_initialize(&res->driver_devices);
887 link_initialize(&res->devman_dev);
[791f58c]888 }
889
890 return res;
891}
892
893/** Delete a device node.
894 *
895 * @param node The device node structure.
896 */
[ba38f72c]897void delete_dev_node(dev_node_t *dev)
[791f58c]898{
[ba38f72c]899 assert(list_empty(&dev->functions));
900 assert(dev->pfun == NULL);
901 assert(dev->drv == NULL);
902
903 free(dev);
[791f58c]904}
905
906/** Find the device node structure of the device witch has the specified handle.
907 *
908 * @param tree The device tree where we look for the device node.
909 * @param handle The handle of the device.
910 * @return The device node.
911 */
[ba38f72c]912dev_node_t *find_dev_node_no_lock(dev_tree_t *tree, devman_handle_t handle)
[791f58c]913{
914 unsigned long key = handle;
[01b87dc5]915 link_t *link;
916
917 assert(fibril_rwlock_is_locked(&tree->rwlock));
918
919 link = hash_table_find(&tree->devman_devices, &key);
[ba38f72c]920 return hash_table_get_instance(link, dev_node_t, devman_dev);
[791f58c]921}
922
923/** Find the device node structure of the device witch has the specified handle.
924 *
925 * @param tree The device tree where we look for the device node.
926 * @param handle The handle of the device.
927 * @return The device node.
928 */
[ba38f72c]929dev_node_t *find_dev_node(dev_tree_t *tree, devman_handle_t handle)
[791f58c]930{
[ba38f72c]931 dev_node_t *dev = NULL;
[791f58c]932
933 fibril_rwlock_read_lock(&tree->rwlock);
[ba38f72c]934 dev = find_dev_node_no_lock(tree, handle);
[791f58c]935 fibril_rwlock_read_unlock(&tree->rwlock);
936
[ba38f72c]937 return dev;
938}
939
940/* Function nodes */
941
942/** Create a new function node.
943 *
944 * @return A function node structure.
945 */
946fun_node_t *create_fun_node(void)
947{
948 fun_node_t *res = malloc(sizeof(fun_node_t));
949
950 if (res != NULL) {
951 memset(res, 0, sizeof(fun_node_t));
952 link_initialize(&res->dev_functions);
953 list_initialize(&res->match_ids.ids);
954 list_initialize(&res->classes);
955 link_initialize(&res->devman_fun);
956 link_initialize(&res->devmap_fun);
957 }
958
959 return res;
960}
961
962/** Delete a function node.
963 *
964 * @param fun The device node structure.
965 */
966void delete_fun_node(fun_node_t *fun)
967{
968 assert(fun->dev == NULL);
969 assert(fun->child == NULL);
970
971 clean_match_ids(&fun->match_ids);
972 free_not_null(fun->name);
973 free_not_null(fun->pathname);
974 free(fun);
975}
976
977/** Find the function node with the specified handle.
978 *
979 * @param tree The device tree where we look for the device node.
980 * @param handle The handle of the function.
981 * @return The function node.
982 */
983fun_node_t *find_fun_node_no_lock(dev_tree_t *tree, devman_handle_t handle)
984{
985 unsigned long key = handle;
986 link_t *link;
987
988 assert(fibril_rwlock_is_locked(&tree->rwlock));
989
990 link = hash_table_find(&tree->devman_functions, &key);
991 if (link == NULL)
992 return NULL;
993
994 return hash_table_get_instance(link, fun_node_t, devman_fun);
[791f58c]995}
996
[ba38f72c]997/** Find the function node with the specified handle.
998 *
999 * @param tree The device tree where we look for the device node.
1000 * @param handle The handle of the function.
1001 * @return The function node.
1002 */
1003fun_node_t *find_fun_node(dev_tree_t *tree, devman_handle_t handle)
1004{
1005 fun_node_t *fun = NULL;
1006
1007 fibril_rwlock_read_lock(&tree->rwlock);
1008 fun = find_fun_node_no_lock(tree, handle);
1009 fibril_rwlock_read_unlock(&tree->rwlock);
1010
1011 return fun;
1012}
[791f58c]1013
[bda60d9]1014/** Create and set device's full path in device tree.
[38b3baf]1015 *
1016 * @param node The device's device node.
1017 * @param parent The parent device node.
1018 * @return True on success, false otherwise (insufficient
1019 * resources etc.).
[bda60d9]1020 */
[ba38f72c]1021static bool set_fun_path(fun_node_t *fun, fun_node_t *parent)
[38b3baf]1022{
[ba38f72c]1023 assert(fun->name != NULL);
[bda60d9]1024
[ba38f72c]1025 size_t pathsize = (str_size(fun->name) + 1);
[58b833c]1026 if (parent != NULL)
[38b3baf]1027 pathsize += str_size(parent->pathname) + 1;
[bda60d9]1028
[ba38f72c]1029 fun->pathname = (char *) malloc(pathsize);
1030 if (fun->pathname == NULL) {
[bda60d9]1031 printf(NAME ": failed to allocate device path.\n");
1032 return false;
1033 }
1034
[58b833c]1035 if (parent != NULL) {
[ba38f72c]1036 str_cpy(fun->pathname, pathsize, parent->pathname);
1037 str_append(fun->pathname, pathsize, "/");
1038 str_append(fun->pathname, pathsize, fun->name);
[bda60d9]1039 } else {
[ba38f72c]1040 str_cpy(fun->pathname, pathsize, fun->name);
[bda60d9]1041 }
1042
1043 return true;
1044}
1045
1046/** Insert new device into device tree.
[38b3baf]1047 *
1048 * @param tree The device tree.
1049 * @param node The newly added device node.
1050 * @param dev_name The name of the newly added device.
1051 * @param parent The parent device node.
[58b833c]1052 *
[38b3baf]1053 * @return True on success, false otherwise (insufficient resources
1054 * etc.).
[bda60d9]1055 */
[ba38f72c]1056bool insert_dev_node(dev_tree_t *tree, dev_node_t *dev, fun_node_t *pfun)
1057{
1058 assert(dev != NULL);
1059 assert(tree != NULL);
1060 assert(fibril_rwlock_is_write_locked(&tree->rwlock));
1061
1062 /* Add the node to the handle-to-node map. */
1063 dev->handle = ++tree->current_handle;
1064 unsigned long key = dev->handle;
1065 hash_table_insert(&tree->devman_devices, &key, &dev->devman_dev);
1066
1067 /* Add the node to the list of its parent's children. */
1068 printf("insert_dev_node: dev=%p, dev->pfun := %p\n", dev, pfun);
1069 dev->pfun = pfun;
1070 pfun->child = dev;
1071
1072 return true;
1073}
1074
1075/** Insert new function into device tree.
1076 *
1077 * @param tree The device tree.
1078 * @param node The newly added function node.
1079 * @param dev_name The name of the newly added function.
1080 * @param parent Owning device node.
1081 *
1082 * @return True on success, false otherwise (insufficient resources
1083 * etc.).
1084 */
1085bool insert_fun_node(dev_tree_t *tree, fun_node_t *fun, char *fun_name,
1086 dev_node_t *dev)
[bda60d9]1087{
[ba38f72c]1088 fun_node_t *pfun;
1089
1090 assert(fun != NULL);
[58b833c]1091 assert(tree != NULL);
[ba38f72c]1092 assert(fun_name != NULL);
[01b87dc5]1093 assert(fibril_rwlock_is_write_locked(&tree->rwlock));
[bda60d9]1094
[ba38f72c]1095 /*
1096 * The root function is a special case, it does not belong to any
1097 * device so for the root function dev == NULL.
1098 */
1099 pfun = (dev != NULL) ? dev->pfun : NULL;
1100
1101 fun->name = fun_name;
1102 if (!set_fun_path(fun, pfun)) {
[38b3baf]1103 return false;
[bda60d9]1104 }
1105
[38b3baf]1106 /* Add the node to the handle-to-node map. */
[ba38f72c]1107 fun->handle = ++tree->current_handle;
1108 unsigned long key = fun->handle;
1109 hash_table_insert(&tree->devman_functions, &key, &fun->devman_fun);
[bda60d9]1110
[38b3baf]1111 /* Add the node to the list of its parent's children. */
[ba38f72c]1112 fun->dev = dev;
1113 if (dev != NULL)
1114 list_append(&fun->dev_functions, &dev->functions);
[38b3baf]1115
[bda60d9]1116 return true;
1117}
1118
[ba38f72c]1119/** Find function node with a specified path in the device tree.
[5cd136ab]1120 *
[ba38f72c]1121 * @param path The path of the function node in the device tree.
[38b3baf]1122 * @param tree The device tree.
[ba38f72c]1123 * @return The function node if it is present in the tree, NULL
[38b3baf]1124 * otherwise.
[5cd136ab]1125 */
[ba38f72c]1126fun_node_t *find_fun_node_by_path(dev_tree_t *tree, char *path)
[5cd136ab]1127{
[957cfa58]1128 fibril_rwlock_read_lock(&tree->rwlock);
1129
[ba38f72c]1130 fun_node_t *fun = tree->root_node;
[38b3baf]1131 /*
[ba38f72c]1132 * Relative path to the function from its parent (but with '/' at the
[38b3baf]1133 * beginning)
1134 */
[5cd136ab]1135 char *rel_path = path;
1136 char *next_path_elem = NULL;
[58b833c]1137 bool cont = (rel_path[0] == '/');
[5cd136ab]1138
[ba38f72c]1139 while (cont && fun != NULL) {
[38b3baf]1140 next_path_elem = get_path_elem_end(rel_path + 1);
[58b833c]1141 if (next_path_elem[0] == '/') {
[5cd136ab]1142 cont = true;
1143 next_path_elem[0] = 0;
1144 } else {
1145 cont = false;
1146 }
1147
[ba38f72c]1148 fun = find_node_child(fun, rel_path + 1);
[5cd136ab]1149
1150 if (cont) {
[38b3baf]1151 /* Restore the original path. */
[5cd136ab]1152 next_path_elem[0] = '/';
1153 }
[38b3baf]1154 rel_path = next_path_elem;
[5cd136ab]1155 }
1156
[957cfa58]1157 fibril_rwlock_read_unlock(&tree->rwlock);
1158
[ba38f72c]1159 return fun;
[5cd136ab]1160}
1161
[ba38f72c]1162/** Find child function node with a specified name.
[38b3baf]1163 *
1164 * Device tree rwlock should be held at least for reading.
1165 *
[ba38f72c]1166 * @param parent The parent function node.
1167 * @param name The name of the child function.
1168 * @return The child function node.
[5cd136ab]1169 */
[ba38f72c]1170fun_node_t *find_node_child(fun_node_t *pfun, const char *name)
[5cd136ab]1171{
[ba38f72c]1172 fun_node_t *fun;
[5cd136ab]1173 link_t *link;
[38b3baf]1174
[ba38f72c]1175 link = pfun->child->functions.next;
[5cd136ab]1176
[ba38f72c]1177 while (link != &pfun->child->functions) {
1178 fun = list_get_instance(link, fun_node_t, dev_functions);
[5cd136ab]1179
[ba38f72c]1180 if (str_cmp(name, fun->name) == 0)
1181 return fun;
[2480e19]1182
1183 link = link->next;
[38b3baf]1184 }
1185
[5cd136ab]1186 return NULL;
1187}
1188
[791f58c]1189/* Device classes */
1190
1191/** Create device class.
1192 *
1193 * @return Device class.
1194 */
1195dev_class_t *create_dev_class(void)
1196{
1197 dev_class_t *cl;
1198
1199 cl = (dev_class_t *) malloc(sizeof(dev_class_t));
1200 if (cl != NULL) {
1201 memset(cl, 0, sizeof(dev_class_t));
1202 list_initialize(&cl->devices);
1203 fibril_mutex_initialize(&cl->mutex);
1204 }
1205
1206 return cl;
1207}
1208
1209/** Create device class info.
1210 *
1211 * @return Device class info.
1212 */
1213dev_class_info_t *create_dev_class_info(void)
1214{
1215 dev_class_info_t *info;
1216
1217 info = (dev_class_info_t *) malloc(sizeof(dev_class_info_t));
[3ca3430]1218 if (info != NULL) {
[791f58c]1219 memset(info, 0, sizeof(dev_class_info_t));
[3ca3430]1220 list_initialize(&info->dev_classes);
1221 list_initialize(&info->devmap_link);
1222 list_initialize(&info->link);
1223 }
[791f58c]1224
1225 return info;
1226}
1227
1228size_t get_new_class_dev_idx(dev_class_t *cl)
1229{
1230 size_t dev_idx;
1231
1232 fibril_mutex_lock(&cl->mutex);
1233 dev_idx = ++cl->curr_dev_idx;
1234 fibril_mutex_unlock(&cl->mutex);
1235
1236 return dev_idx;
1237}
1238
1239
[38b3baf]1240/** Create unique device name within the class.
1241 *
1242 * @param cl The class.
1243 * @param base_dev_name Contains the base name for the device if it was
1244 * specified by the driver when it registered the device by
1245 * the class; NULL if driver specified no base name.
1246 * @return The unique name for the device within the class.
[d51ee2b]1247 */
[38b3baf]1248char *create_dev_name_for_class(dev_class_t *cl, const char *base_dev_name)
[d51ee2b]1249{
1250 char *dev_name;
1251 const char *base_name;
[38b3baf]1252
[58b833c]1253 if (base_dev_name != NULL)
[d51ee2b]1254 base_name = base_dev_name;
[38b3baf]1255 else
[d51ee2b]1256 base_name = cl->base_dev_name;
1257
1258 size_t idx = get_new_class_dev_idx(cl);
[7e752b2]1259 asprintf(&dev_name, "%s%zu", base_name, idx);
[38b3baf]1260
1261 return dev_name;
[d51ee2b]1262}
1263
[ba38f72c]1264/** Add the device function to the class.
[38b3baf]1265 *
1266 * The device may be added to multiple classes and a class may contain multiple
1267 * devices. The class and the device are associated with each other by the
1268 * dev_class_info_t structure.
1269 *
1270 * @param dev The device.
1271 * @param class The class.
1272 * @param base_dev_name The base name of the device within the class if
1273 * specified by the driver, NULL otherwise.
1274 * @return dev_class_info_t structure which associates the device
1275 * with the class.
[d51ee2b]1276 */
[ba38f72c]1277dev_class_info_t *add_function_to_class(fun_node_t *fun, dev_class_t *cl,
[58b833c]1278 const char *base_dev_name)
[38b3baf]1279{
[ba38f72c]1280 dev_class_info_t *info;
1281
1282 assert(fun != NULL);
1283 assert(cl != NULL);
1284
1285 info = create_dev_class_info();
1286
[38b3baf]1287
[58b833c]1288 if (info != NULL) {
[692c40cb]1289 info->dev_class = cl;
[ba38f72c]1290 info->fun = fun;
[692c40cb]1291
[38b3baf]1292 /* Add the device to the class. */
[692c40cb]1293 fibril_mutex_lock(&cl->mutex);
1294 list_append(&info->link, &cl->devices);
1295 fibril_mutex_unlock(&cl->mutex);
1296
[38b3baf]1297 /* Add the class to the device. */
[ba38f72c]1298 list_append(&info->dev_classes, &fun->classes);
[692c40cb]1299
[38b3baf]1300 /* Create unique name for the device within the class. */
1301 info->dev_name = create_dev_name_for_class(cl, base_dev_name);
[692c40cb]1302 }
[d51ee2b]1303
1304 return info;
1305}
1306
[38b3baf]1307dev_class_t *get_dev_class(class_list_t *class_list, char *class_name)
[692c40cb]1308{
1309 dev_class_t *cl;
[38b3baf]1310
1311 fibril_rwlock_write_lock(&class_list->rwlock);
[692c40cb]1312 cl = find_dev_class_no_lock(class_list, class_name);
[58b833c]1313 if (cl == NULL) {
[692c40cb]1314 cl = create_dev_class();
[58b833c]1315 if (cl != NULL) {
[38b3baf]1316 cl->name = class_name;
[692c40cb]1317 cl->base_dev_name = "";
1318 add_dev_class_no_lock(class_list, cl);
[38b3baf]1319 }
1320 }
[58b833c]1321
[ce89036b]1322 fibril_rwlock_write_unlock(&class_list->rwlock);
[692c40cb]1323 return cl;
1324}
1325
[58b833c]1326dev_class_t *find_dev_class_no_lock(class_list_t *class_list,
1327 const char *class_name)
[692c40cb]1328{
1329 dev_class_t *cl;
1330 link_t *link = class_list->classes.next;
[38b3baf]1331
[692c40cb]1332 while (link != &class_list->classes) {
1333 cl = list_get_instance(link, dev_class_t, link);
[2edcb63]1334 if (str_cmp(cl->name, class_name) == 0) {
[692c40cb]1335 return cl;
[2edcb63]1336 }
1337 link = link->next;
[692c40cb]1338 }
1339
[38b3baf]1340 return NULL;
[692c40cb]1341}
1342
[791f58c]1343void add_dev_class_no_lock(class_list_t *class_list, dev_class_t *cl)
1344{
1345 list_append(&cl->link, &class_list->classes);
1346}
1347
[ce89036b]1348void init_class_list(class_list_t *class_list)
1349{
1350 list_initialize(&class_list->classes);
1351 fibril_rwlock_initialize(&class_list->rwlock);
[ba38f72c]1352 hash_table_create(&class_list->devmap_functions, DEVICE_BUCKETS, 1,
[3ca3430]1353 &devmap_devices_class_ops);
[ce89036b]1354}
1355
1356
[791f58c]1357/* Devmap devices */
[ce89036b]1358
[ba38f72c]1359fun_node_t *find_devmap_tree_function(dev_tree_t *tree, devmap_handle_t devmap_handle)
[ce89036b]1360{
[ba38f72c]1361 fun_node_t *fun = NULL;
[ce89036b]1362 link_t *link;
[38b3baf]1363 unsigned long key = (unsigned long) devmap_handle;
[ce89036b]1364
1365 fibril_rwlock_read_lock(&tree->rwlock);
[ba38f72c]1366 link = hash_table_find(&tree->devmap_functions, &key);
[58b833c]1367 if (link != NULL)
[ba38f72c]1368 fun = hash_table_get_instance(link, fun_node_t, devmap_fun);
[ce89036b]1369 fibril_rwlock_read_unlock(&tree->rwlock);
1370
[ba38f72c]1371 return fun;
[ce89036b]1372}
1373
[ba38f72c]1374fun_node_t *find_devmap_class_function(class_list_t *classes,
[991f645]1375 devmap_handle_t devmap_handle)
[ce89036b]1376{
[ba38f72c]1377 fun_node_t *fun = NULL;
[ce89036b]1378 dev_class_info_t *cli;
1379 link_t *link;
1380 unsigned long key = (unsigned long)devmap_handle;
1381
1382 fibril_rwlock_read_lock(&classes->rwlock);
[ba38f72c]1383 link = hash_table_find(&classes->devmap_functions, &key);
[58b833c]1384 if (link != NULL) {
[38b3baf]1385 cli = hash_table_get_instance(link, dev_class_info_t,
1386 devmap_link);
[ba38f72c]1387 fun = cli->fun;
[ce89036b]1388 }
1389 fibril_rwlock_read_unlock(&classes->rwlock);
1390
[ba38f72c]1391 return fun;
[ce89036b]1392}
1393
[ba38f72c]1394void class_add_devmap_function(class_list_t *class_list, dev_class_info_t *cli)
[791f58c]1395{
1396 unsigned long key = (unsigned long) cli->devmap_handle;
1397
1398 fibril_rwlock_write_lock(&class_list->rwlock);
[ba38f72c]1399 hash_table_insert(&class_list->devmap_functions, &key, &cli->devmap_link);
[791f58c]1400 fibril_rwlock_write_unlock(&class_list->rwlock);
[3ca3430]1401
[ba38f72c]1402 assert(find_devmap_class_function(class_list, cli->devmap_handle) != NULL);
[791f58c]1403}
1404
[ba38f72c]1405void tree_add_devmap_function(dev_tree_t *tree, fun_node_t *fun)
[791f58c]1406{
[ba38f72c]1407 unsigned long key = (unsigned long) fun->devmap_handle;
[791f58c]1408 fibril_rwlock_write_lock(&tree->rwlock);
[ba38f72c]1409 hash_table_insert(&tree->devmap_functions, &key, &fun->devmap_fun);
[791f58c]1410 fibril_rwlock_write_unlock(&tree->rwlock);
1411}
1412
[c16cf62]1413/** @}
[58b833c]1414 */
Note: See TracBrowser for help on using the repository browser.