source: mainline/uspace/srv/devman/devman.c@ 3b5d1535

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

Merge mainline changes (DDF refactoring)

This merge includes DDF refactoring that brought multifunctional devices
(i.e. ddf_dev_t and ddf_fun_t). Please, see ticket #295 at HelenOS
upstream Trac.

The conflicts themselves were easy to solve (merely several renamings).

Changes to USB subsystem:

  • drivers uses ddf_dev_t and ddf_fun_t
  • different signatures of many library functions
  • several hacks around communication with parent device (now the communication is clearer and somehow what we have now is hack about other hacks)
    • will repair and clean later
  • maybe added some extra debugging messages (the diff has about 240K, and I admit I have no energy to double check that)

WARNING:

  • the diff is VERY long, recommended is viewing partial diffs of the merge (i.e. merges in mainline branch that lead to the parent one)
  • merging with your branches might involve huge renamings, sorry, no other way is possible

BUGS:

  • hub driver will not work (no function created)

GOOD NEWS:

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