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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 1d1bb0f was 79ae36dd, checked in by Martin Decky <martin@…>, 14 years ago

new async framework with integrated exchange tracking

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