source: mainline/uspace/srv/devman/devman.c@ 0c968a17

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

Merge DDF and drivers refactoring work. Major points:

  • Split driver.h into ddf/driver.h and ddf/interrupt.h
  • Rename entities in ddf/driver.h (now they start with 'ddf_')
  • Refactor driver.h API (ddf_fun_create/destroy/bind, for example)
  • Refactor drivers to use soft-state structures for context
  • Property mode set to 100644
File size: 34.8 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. */
[8b1e15ac]711void 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;
[df747b9c]779 break;
[a78fa2a]780 case ENOENT:
[ba38f72c]781 dev->state = DEVICE_NOT_PRESENT;
[a78fa2a]782 break;
[df747b9c]783 default:
[ba38f72c]784 dev->state = DEVICE_INVALID;
[084ff99]785 }
[e85920d]786
[ba38f72c]787 dev->passed_to_driver = true;
[5bee897]788
[5cd136ab]789 return;
[85e48a9]790}
791
[38b3baf]792/** Find suitable driver for a device and assign the driver to it.
793 *
794 * @param node The device node of the device in the device tree.
795 * @param drivers_list The list of available drivers.
796 * @return True if the suitable driver is found and
797 * successfully assigned to the device, false otherwise.
[0c3666d]798 */
[ba38f72c]799bool assign_driver(dev_node_t *dev, driver_list_t *drivers_list,
800 dev_tree_t *tree)
[85e48a9]801{
[ba38f72c]802 assert(dev != NULL);
803 assert(drivers_list != NULL);
804 assert(tree != NULL);
805
[38b3baf]806 /*
807 * Find the driver which is the most suitable for handling this device.
808 */
[ba38f72c]809 driver_t *drv = find_best_match_driver(drivers_list, dev);
[58b833c]810 if (drv == NULL) {
[38b3baf]811 printf(NAME ": no driver found for device '%s'.\n",
[ba38f72c]812 dev->pfun->pathname);
[38b3baf]813 return false;
[85e48a9]814 }
815
[38b3baf]816 /* Attach the driver to the device. */
[ba38f72c]817 attach_driver(dev, drv);
[85e48a9]818
[398c4d7]819 fibril_mutex_lock(&drv->driver_mutex);
[58b833c]820 if (drv->state == DRIVER_NOT_STARTED) {
[38b3baf]821 /* Start the driver. */
[85e48a9]822 start_driver(drv);
[38b3baf]823 }
[398c4d7]824 bool is_running = drv->state == DRIVER_RUNNING;
825 fibril_mutex_unlock(&drv->driver_mutex);
826
827 if (is_running) {
[38b3baf]828 /* Notify the driver about the new device. */
[398c4d7]829 int phone = async_connect_me_to(drv->phone, DRIVER_DEVMAN, 0, 0);
[fa581b3]830 if (phone >= 0) {
[ba38f72c]831 add_device(phone, drv, dev, tree);
[ffa2c8ef]832 async_hangup(phone);
[084ff99]833 }
[85e48a9]834 }
835
836 return true;
837}
838
[38b3baf]839/** Initialize the device tree.
840 *
[0c3666d]841 * Create root device node of the tree and assign driver to it.
[38b3baf]842 *
843 * @param tree The device tree.
844 * @param drivers_list the list of available drivers.
845 * @return True on success, false otherwise.
[0c3666d]846 */
847bool init_device_tree(dev_tree_t *tree, driver_list_t *drivers_list)
[85e48a9]848{
[e85920d]849 printf(NAME ": init_device_tree.\n");
[0c3666d]850
[957cfa58]851 tree->current_handle = 0;
852
[38b3baf]853 hash_table_create(&tree->devman_devices, DEVICE_BUCKETS, 1,
854 &devman_devices_ops);
[ba38f72c]855 hash_table_create(&tree->devman_functions, DEVICE_BUCKETS, 1,
856 &devman_functions_ops);
857 hash_table_create(&tree->devmap_functions, DEVICE_BUCKETS, 1,
[38b3baf]858 &devmap_devices_ops);
[bda60d9]859
[957cfa58]860 fibril_rwlock_initialize(&tree->rwlock);
[084ff99]861
[ba38f72c]862 /* Create root function and root device and add them to the device tree. */
863 if (!create_root_nodes(tree))
[85e48a9]864 return false;
[e4c4247]865
[38b3baf]866 /* Find suitable driver and start it. */
[ba38f72c]867 return assign_driver(tree->root_node->child, drivers_list, tree);
[e4c4247]868}
869
[791f58c]870/* Device nodes */
871
872/** Create a new device node.
873 *
874 * @return A device node structure.
875 */
[ba38f72c]876dev_node_t *create_dev_node(void)
[791f58c]877{
[ba38f72c]878 dev_node_t *res = malloc(sizeof(dev_node_t));
[791f58c]879
880 if (res != NULL) {
[ba38f72c]881 memset(res, 0, sizeof(dev_node_t));
882 list_initialize(&res->functions);
883 link_initialize(&res->driver_devices);
884 link_initialize(&res->devman_dev);
[791f58c]885 }
886
887 return res;
888}
889
890/** Delete a device node.
891 *
892 * @param node The device node structure.
893 */
[ba38f72c]894void delete_dev_node(dev_node_t *dev)
[791f58c]895{
[ba38f72c]896 assert(list_empty(&dev->functions));
897 assert(dev->pfun == NULL);
898 assert(dev->drv == NULL);
899
900 free(dev);
[791f58c]901}
902
903/** Find the device node structure of the device witch has the specified handle.
904 *
905 * @param tree The device tree where we look for the device node.
906 * @param handle The handle of the device.
907 * @return The device node.
908 */
[ba38f72c]909dev_node_t *find_dev_node_no_lock(dev_tree_t *tree, devman_handle_t handle)
[791f58c]910{
911 unsigned long key = handle;
[01b87dc5]912 link_t *link;
913
914 assert(fibril_rwlock_is_locked(&tree->rwlock));
915
916 link = hash_table_find(&tree->devman_devices, &key);
[ba38f72c]917 return hash_table_get_instance(link, dev_node_t, devman_dev);
[791f58c]918}
919
920/** Find the device node structure of the device witch has the specified handle.
921 *
922 * @param tree The device tree where we look for the device node.
923 * @param handle The handle of the device.
924 * @return The device node.
925 */
[ba38f72c]926dev_node_t *find_dev_node(dev_tree_t *tree, devman_handle_t handle)
[791f58c]927{
[ba38f72c]928 dev_node_t *dev = NULL;
[791f58c]929
930 fibril_rwlock_read_lock(&tree->rwlock);
[ba38f72c]931 dev = find_dev_node_no_lock(tree, handle);
[791f58c]932 fibril_rwlock_read_unlock(&tree->rwlock);
933
[ba38f72c]934 return dev;
935}
936
937/* Function nodes */
938
939/** Create a new function node.
940 *
941 * @return A function node structure.
942 */
943fun_node_t *create_fun_node(void)
944{
945 fun_node_t *res = malloc(sizeof(fun_node_t));
946
947 if (res != NULL) {
948 memset(res, 0, sizeof(fun_node_t));
949 link_initialize(&res->dev_functions);
950 list_initialize(&res->match_ids.ids);
951 list_initialize(&res->classes);
952 link_initialize(&res->devman_fun);
953 link_initialize(&res->devmap_fun);
954 }
955
956 return res;
957}
958
959/** Delete a function node.
960 *
961 * @param fun The device node structure.
962 */
963void delete_fun_node(fun_node_t *fun)
964{
965 assert(fun->dev == NULL);
966 assert(fun->child == NULL);
967
968 clean_match_ids(&fun->match_ids);
969 free_not_null(fun->name);
970 free_not_null(fun->pathname);
971 free(fun);
972}
973
974/** Find the function node with the specified handle.
975 *
976 * @param tree The device tree where we look for the device node.
977 * @param handle The handle of the function.
978 * @return The function node.
979 */
980fun_node_t *find_fun_node_no_lock(dev_tree_t *tree, devman_handle_t handle)
981{
982 unsigned long key = handle;
983 link_t *link;
984
985 assert(fibril_rwlock_is_locked(&tree->rwlock));
986
987 link = hash_table_find(&tree->devman_functions, &key);
988 if (link == NULL)
989 return NULL;
990
991 return hash_table_get_instance(link, fun_node_t, devman_fun);
[791f58c]992}
993
[ba38f72c]994/** Find the function node with the specified handle.
995 *
996 * @param tree The device tree where we look for the device node.
997 * @param handle The handle of the function.
998 * @return The function node.
999 */
1000fun_node_t *find_fun_node(dev_tree_t *tree, devman_handle_t handle)
1001{
1002 fun_node_t *fun = NULL;
1003
1004 fibril_rwlock_read_lock(&tree->rwlock);
1005 fun = find_fun_node_no_lock(tree, handle);
1006 fibril_rwlock_read_unlock(&tree->rwlock);
1007
1008 return fun;
1009}
[791f58c]1010
[bda60d9]1011/** Create and set device's full path in device tree.
[38b3baf]1012 *
1013 * @param node The device's device node.
1014 * @param parent The parent device node.
1015 * @return True on success, false otherwise (insufficient
1016 * resources etc.).
[bda60d9]1017 */
[ba38f72c]1018static bool set_fun_path(fun_node_t *fun, fun_node_t *parent)
[38b3baf]1019{
[ba38f72c]1020 assert(fun->name != NULL);
[bda60d9]1021
[ba38f72c]1022 size_t pathsize = (str_size(fun->name) + 1);
[58b833c]1023 if (parent != NULL)
[38b3baf]1024 pathsize += str_size(parent->pathname) + 1;
[bda60d9]1025
[ba38f72c]1026 fun->pathname = (char *) malloc(pathsize);
1027 if (fun->pathname == NULL) {
[bda60d9]1028 printf(NAME ": failed to allocate device path.\n");
1029 return false;
1030 }
1031
[58b833c]1032 if (parent != NULL) {
[ba38f72c]1033 str_cpy(fun->pathname, pathsize, parent->pathname);
1034 str_append(fun->pathname, pathsize, "/");
1035 str_append(fun->pathname, pathsize, fun->name);
[bda60d9]1036 } else {
[ba38f72c]1037 str_cpy(fun->pathname, pathsize, fun->name);
[bda60d9]1038 }
1039
1040 return true;
1041}
1042
1043/** Insert new device into device tree.
[38b3baf]1044 *
1045 * @param tree The device tree.
1046 * @param node The newly added device node.
1047 * @param dev_name The name of the newly added device.
1048 * @param parent The parent device node.
[58b833c]1049 *
[38b3baf]1050 * @return True on success, false otherwise (insufficient resources
1051 * etc.).
[bda60d9]1052 */
[ba38f72c]1053bool insert_dev_node(dev_tree_t *tree, dev_node_t *dev, fun_node_t *pfun)
1054{
1055 assert(dev != NULL);
1056 assert(tree != NULL);
1057 assert(fibril_rwlock_is_write_locked(&tree->rwlock));
1058
1059 /* Add the node to the handle-to-node map. */
1060 dev->handle = ++tree->current_handle;
1061 unsigned long key = dev->handle;
1062 hash_table_insert(&tree->devman_devices, &key, &dev->devman_dev);
1063
1064 /* Add the node to the list of its parent's children. */
1065 printf("insert_dev_node: dev=%p, dev->pfun := %p\n", dev, pfun);
1066 dev->pfun = pfun;
1067 pfun->child = dev;
1068
1069 return true;
1070}
1071
1072/** Insert new function into device tree.
1073 *
1074 * @param tree The device tree.
1075 * @param node The newly added function node.
1076 * @param dev_name The name of the newly added function.
1077 * @param parent Owning device node.
1078 *
1079 * @return True on success, false otherwise (insufficient resources
1080 * etc.).
1081 */
1082bool insert_fun_node(dev_tree_t *tree, fun_node_t *fun, char *fun_name,
1083 dev_node_t *dev)
[bda60d9]1084{
[ba38f72c]1085 fun_node_t *pfun;
1086
1087 assert(fun != NULL);
[58b833c]1088 assert(tree != NULL);
[ba38f72c]1089 assert(fun_name != NULL);
[01b87dc5]1090 assert(fibril_rwlock_is_write_locked(&tree->rwlock));
[bda60d9]1091
[ba38f72c]1092 /*
1093 * The root function is a special case, it does not belong to any
1094 * device so for the root function dev == NULL.
1095 */
1096 pfun = (dev != NULL) ? dev->pfun : NULL;
1097
1098 fun->name = fun_name;
1099 if (!set_fun_path(fun, pfun)) {
[38b3baf]1100 return false;
[bda60d9]1101 }
1102
[38b3baf]1103 /* Add the node to the handle-to-node map. */
[ba38f72c]1104 fun->handle = ++tree->current_handle;
1105 unsigned long key = fun->handle;
1106 hash_table_insert(&tree->devman_functions, &key, &fun->devman_fun);
[bda60d9]1107
[38b3baf]1108 /* Add the node to the list of its parent's children. */
[ba38f72c]1109 fun->dev = dev;
1110 if (dev != NULL)
1111 list_append(&fun->dev_functions, &dev->functions);
[38b3baf]1112
[bda60d9]1113 return true;
1114}
1115
[ba38f72c]1116/** Find function node with a specified path in the device tree.
[5cd136ab]1117 *
[ba38f72c]1118 * @param path The path of the function node in the device tree.
[38b3baf]1119 * @param tree The device tree.
[ba38f72c]1120 * @return The function node if it is present in the tree, NULL
[38b3baf]1121 * otherwise.
[5cd136ab]1122 */
[ba38f72c]1123fun_node_t *find_fun_node_by_path(dev_tree_t *tree, char *path)
[5cd136ab]1124{
[957cfa58]1125 fibril_rwlock_read_lock(&tree->rwlock);
1126
[ba38f72c]1127 fun_node_t *fun = tree->root_node;
[38b3baf]1128 /*
[ba38f72c]1129 * Relative path to the function from its parent (but with '/' at the
[38b3baf]1130 * beginning)
1131 */
[5cd136ab]1132 char *rel_path = path;
1133 char *next_path_elem = NULL;
[58b833c]1134 bool cont = (rel_path[0] == '/');
[5cd136ab]1135
[ba38f72c]1136 while (cont && fun != NULL) {
[38b3baf]1137 next_path_elem = get_path_elem_end(rel_path + 1);
[58b833c]1138 if (next_path_elem[0] == '/') {
[5cd136ab]1139 cont = true;
1140 next_path_elem[0] = 0;
1141 } else {
1142 cont = false;
1143 }
1144
[ba38f72c]1145 fun = find_node_child(fun, rel_path + 1);
[5cd136ab]1146
1147 if (cont) {
[38b3baf]1148 /* Restore the original path. */
[5cd136ab]1149 next_path_elem[0] = '/';
1150 }
[38b3baf]1151 rel_path = next_path_elem;
[5cd136ab]1152 }
1153
[957cfa58]1154 fibril_rwlock_read_unlock(&tree->rwlock);
1155
[ba38f72c]1156 return fun;
[5cd136ab]1157}
1158
[ba38f72c]1159/** Find child function node with a specified name.
[38b3baf]1160 *
1161 * Device tree rwlock should be held at least for reading.
1162 *
[ba38f72c]1163 * @param parent The parent function node.
1164 * @param name The name of the child function.
1165 * @return The child function node.
[5cd136ab]1166 */
[ba38f72c]1167fun_node_t *find_node_child(fun_node_t *pfun, const char *name)
[5cd136ab]1168{
[ba38f72c]1169 fun_node_t *fun;
[5cd136ab]1170 link_t *link;
[38b3baf]1171
[ba38f72c]1172 link = pfun->child->functions.next;
[5cd136ab]1173
[ba38f72c]1174 while (link != &pfun->child->functions) {
1175 fun = list_get_instance(link, fun_node_t, dev_functions);
[5cd136ab]1176
[ba38f72c]1177 if (str_cmp(name, fun->name) == 0)
1178 return fun;
[2480e19]1179
1180 link = link->next;
[38b3baf]1181 }
1182
[5cd136ab]1183 return NULL;
1184}
1185
[791f58c]1186/* Device classes */
1187
1188/** Create device class.
1189 *
1190 * @return Device class.
1191 */
1192dev_class_t *create_dev_class(void)
1193{
1194 dev_class_t *cl;
1195
1196 cl = (dev_class_t *) malloc(sizeof(dev_class_t));
1197 if (cl != NULL) {
1198 memset(cl, 0, sizeof(dev_class_t));
1199 list_initialize(&cl->devices);
1200 fibril_mutex_initialize(&cl->mutex);
1201 }
1202
1203 return cl;
1204}
1205
1206/** Create device class info.
1207 *
1208 * @return Device class info.
1209 */
1210dev_class_info_t *create_dev_class_info(void)
1211{
1212 dev_class_info_t *info;
1213
1214 info = (dev_class_info_t *) malloc(sizeof(dev_class_info_t));
[3ca3430]1215 if (info != NULL) {
[791f58c]1216 memset(info, 0, sizeof(dev_class_info_t));
[ca2a18e]1217 link_initialize(&info->dev_classes);
1218 link_initialize(&info->devmap_link);
1219 link_initialize(&info->link);
[3ca3430]1220 }
[791f58c]1221
1222 return info;
1223}
1224
1225size_t get_new_class_dev_idx(dev_class_t *cl)
1226{
1227 size_t dev_idx;
1228
1229 fibril_mutex_lock(&cl->mutex);
1230 dev_idx = ++cl->curr_dev_idx;
1231 fibril_mutex_unlock(&cl->mutex);
1232
1233 return dev_idx;
1234}
1235
1236
[38b3baf]1237/** Create unique device name within the class.
1238 *
1239 * @param cl The class.
1240 * @param base_dev_name Contains the base name for the device if it was
1241 * specified by the driver when it registered the device by
1242 * the class; NULL if driver specified no base name.
1243 * @return The unique name for the device within the class.
[d51ee2b]1244 */
[38b3baf]1245char *create_dev_name_for_class(dev_class_t *cl, const char *base_dev_name)
[d51ee2b]1246{
1247 char *dev_name;
1248 const char *base_name;
[38b3baf]1249
[58b833c]1250 if (base_dev_name != NULL)
[d51ee2b]1251 base_name = base_dev_name;
[38b3baf]1252 else
[d51ee2b]1253 base_name = cl->base_dev_name;
1254
1255 size_t idx = get_new_class_dev_idx(cl);
[7e752b2]1256 asprintf(&dev_name, "%s%zu", base_name, idx);
[38b3baf]1257
1258 return dev_name;
[d51ee2b]1259}
1260
[ba38f72c]1261/** Add the device function to the class.
[38b3baf]1262 *
1263 * The device may be added to multiple classes and a class may contain multiple
1264 * devices. The class and the device are associated with each other by the
1265 * dev_class_info_t structure.
1266 *
1267 * @param dev The device.
1268 * @param class The class.
1269 * @param base_dev_name The base name of the device within the class if
1270 * specified by the driver, NULL otherwise.
1271 * @return dev_class_info_t structure which associates the device
1272 * with the class.
[d51ee2b]1273 */
[ba38f72c]1274dev_class_info_t *add_function_to_class(fun_node_t *fun, dev_class_t *cl,
[58b833c]1275 const char *base_dev_name)
[38b3baf]1276{
[ba38f72c]1277 dev_class_info_t *info;
1278
1279 assert(fun != NULL);
1280 assert(cl != NULL);
1281
1282 info = create_dev_class_info();
1283
[38b3baf]1284
[58b833c]1285 if (info != NULL) {
[692c40cb]1286 info->dev_class = cl;
[ba38f72c]1287 info->fun = fun;
[692c40cb]1288
[38b3baf]1289 /* Add the device to the class. */
[692c40cb]1290 fibril_mutex_lock(&cl->mutex);
1291 list_append(&info->link, &cl->devices);
1292 fibril_mutex_unlock(&cl->mutex);
1293
[38b3baf]1294 /* Add the class to the device. */
[ba38f72c]1295 list_append(&info->dev_classes, &fun->classes);
[692c40cb]1296
[38b3baf]1297 /* Create unique name for the device within the class. */
1298 info->dev_name = create_dev_name_for_class(cl, base_dev_name);
[692c40cb]1299 }
[d51ee2b]1300
1301 return info;
1302}
1303
[38b3baf]1304dev_class_t *get_dev_class(class_list_t *class_list, char *class_name)
[692c40cb]1305{
1306 dev_class_t *cl;
[38b3baf]1307
1308 fibril_rwlock_write_lock(&class_list->rwlock);
[692c40cb]1309 cl = find_dev_class_no_lock(class_list, class_name);
[58b833c]1310 if (cl == NULL) {
[692c40cb]1311 cl = create_dev_class();
[58b833c]1312 if (cl != NULL) {
[38b3baf]1313 cl->name = class_name;
[692c40cb]1314 cl->base_dev_name = "";
1315 add_dev_class_no_lock(class_list, cl);
[38b3baf]1316 }
1317 }
[58b833c]1318
[ce89036b]1319 fibril_rwlock_write_unlock(&class_list->rwlock);
[692c40cb]1320 return cl;
1321}
1322
[58b833c]1323dev_class_t *find_dev_class_no_lock(class_list_t *class_list,
1324 const char *class_name)
[692c40cb]1325{
1326 dev_class_t *cl;
1327 link_t *link = class_list->classes.next;
[38b3baf]1328
[692c40cb]1329 while (link != &class_list->classes) {
1330 cl = list_get_instance(link, dev_class_t, link);
[2edcb63]1331 if (str_cmp(cl->name, class_name) == 0) {
[692c40cb]1332 return cl;
[2edcb63]1333 }
1334 link = link->next;
[692c40cb]1335 }
1336
[38b3baf]1337 return NULL;
[692c40cb]1338}
1339
[791f58c]1340void add_dev_class_no_lock(class_list_t *class_list, dev_class_t *cl)
1341{
1342 list_append(&cl->link, &class_list->classes);
1343}
1344
[ce89036b]1345void init_class_list(class_list_t *class_list)
1346{
1347 list_initialize(&class_list->classes);
1348 fibril_rwlock_initialize(&class_list->rwlock);
[ba38f72c]1349 hash_table_create(&class_list->devmap_functions, DEVICE_BUCKETS, 1,
[3ca3430]1350 &devmap_devices_class_ops);
[ce89036b]1351}
1352
1353
[791f58c]1354/* Devmap devices */
[ce89036b]1355
[ba38f72c]1356fun_node_t *find_devmap_tree_function(dev_tree_t *tree, devmap_handle_t devmap_handle)
[ce89036b]1357{
[ba38f72c]1358 fun_node_t *fun = NULL;
[ce89036b]1359 link_t *link;
[38b3baf]1360 unsigned long key = (unsigned long) devmap_handle;
[ce89036b]1361
1362 fibril_rwlock_read_lock(&tree->rwlock);
[ba38f72c]1363 link = hash_table_find(&tree->devmap_functions, &key);
[58b833c]1364 if (link != NULL)
[ba38f72c]1365 fun = hash_table_get_instance(link, fun_node_t, devmap_fun);
[ce89036b]1366 fibril_rwlock_read_unlock(&tree->rwlock);
1367
[ba38f72c]1368 return fun;
[ce89036b]1369}
1370
[ba38f72c]1371fun_node_t *find_devmap_class_function(class_list_t *classes,
[991f645]1372 devmap_handle_t devmap_handle)
[ce89036b]1373{
[ba38f72c]1374 fun_node_t *fun = NULL;
[ce89036b]1375 dev_class_info_t *cli;
1376 link_t *link;
1377 unsigned long key = (unsigned long)devmap_handle;
1378
1379 fibril_rwlock_read_lock(&classes->rwlock);
[ba38f72c]1380 link = hash_table_find(&classes->devmap_functions, &key);
[58b833c]1381 if (link != NULL) {
[38b3baf]1382 cli = hash_table_get_instance(link, dev_class_info_t,
1383 devmap_link);
[ba38f72c]1384 fun = cli->fun;
[ce89036b]1385 }
1386 fibril_rwlock_read_unlock(&classes->rwlock);
1387
[ba38f72c]1388 return fun;
[ce89036b]1389}
1390
[ba38f72c]1391void class_add_devmap_function(class_list_t *class_list, dev_class_info_t *cli)
[791f58c]1392{
1393 unsigned long key = (unsigned long) cli->devmap_handle;
1394
1395 fibril_rwlock_write_lock(&class_list->rwlock);
[ba38f72c]1396 hash_table_insert(&class_list->devmap_functions, &key, &cli->devmap_link);
[791f58c]1397 fibril_rwlock_write_unlock(&class_list->rwlock);
[3ca3430]1398
[ba38f72c]1399 assert(find_devmap_class_function(class_list, cli->devmap_handle) != NULL);
[791f58c]1400}
1401
[ba38f72c]1402void tree_add_devmap_function(dev_tree_t *tree, fun_node_t *fun)
[791f58c]1403{
[ba38f72c]1404 unsigned long key = (unsigned long) fun->devmap_handle;
[791f58c]1405 fibril_rwlock_write_lock(&tree->rwlock);
[ba38f72c]1406 hash_table_insert(&tree->devmap_functions, &key, &fun->devmap_fun);
[791f58c]1407 fibril_rwlock_write_unlock(&tree->rwlock);
1408}
1409
[c16cf62]1410/** @}
[58b833c]1411 */
Note: See TracBrowser for help on using the repository browser.