source: mainline/uspace/srv/devman/devman.c@ b72efe8

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

Separate list_t typedef from link_t (user-space part).

  • list_t represents lists
  • Use list_first(), list_last(), list_empty() where appropriate
  • Use list_foreach() where possible
  • assert_link_not_used()
  • usb_hid_report_path_free() shall not unlink the path, caller must do it
  • Property mode set to 100644
File size: 36.4 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
[b72efe8]468 list_foreach(drivers_list->drivers, link) {
[85e48a9]469 drv = list_get_instance(link, driver_t, drivers);
470 score = get_match_score(drv, node);
471 if (score > best_score) {
472 best_score = score;
473 best_drv = drv;
[58b833c]474 }
[0c3666d]475 }
[729fa2d6]476
[0c3666d]477 fibril_mutex_unlock(&drivers_list->drivers_mutex);
[e4c4247]478
[38b3baf]479 return best_drv;
[85e48a9]480}
481
[38b3baf]482/** Assign a driver to a device.
483 *
484 * @param node The device's node in the device tree.
485 * @param drv The driver.
[0c3666d]486 */
[ba38f72c]487void attach_driver(dev_node_t *dev, driver_t *drv)
[85e48a9]488{
[ebcb05a]489 log_msg(LVL_DEBUG, "attach_driver(dev=\"%s\",drv=\"%s\")",
[9b415c9]490 dev->pfun->pathname, drv->name);
[2480e19]491
[0c3666d]492 fibril_mutex_lock(&drv->driver_mutex);
493
[ba38f72c]494 dev->drv = drv;
495 list_append(&dev->driver_devices, &drv->devices);
[0c3666d]496
497 fibril_mutex_unlock(&drv->driver_mutex);
[85e48a9]498}
499
[38b3baf]500/** Start a driver
501 *
502 * @param drv The driver's structure.
503 * @return True if the driver's task is successfully spawned, false
504 * otherwise.
[0c3666d]505 */
[e2b9a993]506bool start_driver(driver_t *drv)
[85e48a9]507{
[0485135]508 int rc;
509
[01b87dc5]510 assert(fibril_mutex_is_locked(&drv->driver_mutex));
511
[ebcb05a]512 log_msg(LVL_DEBUG, "start_driver(drv=\"%s\")", drv->name);
[e85920d]513
[0485135]514 rc = task_spawnl(NULL, drv->binary_path, drv->binary_path, NULL);
515 if (rc != EOK) {
[ebcb05a]516 log_msg(LVL_ERROR, "Spawning driver `%s' (%s) failed: %s.",
[9b415c9]517 drv->name, drv->binary_path, str_error(rc));
[85e48a9]518 return false;
519 }
520
[e85920d]521 drv->state = DRIVER_STARTING;
[85e48a9]522 return true;
523}
524
[bda60d9]525/** Find device driver in the list of device drivers.
[38b3baf]526 *
527 * @param drv_list The list of device drivers.
528 * @param drv_name The name of the device driver which is searched.
529 * @return The device driver of the specified name, if it is in the
530 * list, NULL otherwise.
[bda60d9]531 */
[38b3baf]532driver_t *find_driver(driver_list_t *drv_list, const char *drv_name)
533{
[729fa2d6]534 driver_t *res = NULL;
[58b833c]535 driver_t *drv = NULL;
[729fa2d6]536
[38b3baf]537 fibril_mutex_lock(&drv_list->drivers_mutex);
[729fa2d6]538
[b72efe8]539 list_foreach(drv_list->drivers, link) {
[729fa2d6]540 drv = list_get_instance(link, driver_t, drivers);
[58b833c]541 if (str_cmp(drv->name, drv_name) == 0) {
[729fa2d6]542 res = drv;
543 break;
[58b833c]544 }
545 }
[729fa2d6]546
547 fibril_mutex_unlock(&drv_list->drivers_mutex);
548
549 return res;
550}
551
[38b3baf]552/** Notify driver about the devices to which it was assigned.
553 *
554 * @param driver The driver to which the devices are passed.
[c16cf62]555 */
[a32defa]556static void pass_devices_to_driver(driver_t *driver, dev_tree_t *tree)
[38b3baf]557{
[ba38f72c]558 dev_node_t *dev;
[c16cf62]559 link_t *link;
[58b833c]560
[ebcb05a]561 log_msg(LVL_DEBUG, "pass_devices_to_driver(driver=\"%s\")",
[9b415c9]562 driver->name);
[58b833c]563
[5bee897]564 fibril_mutex_lock(&driver->driver_mutex);
[79ae36dd]565
566 async_exch_t *exch = async_exchange_begin(driver->sess);
567 async_sess_t *sess = async_connect_me_to(EXCHANGE_SERIALIZE, exch,
568 DRIVER_DEVMAN, 0, 0);
569 async_exchange_end(exch);
[5bee897]570
[79ae36dd]571 if (!sess) {
[5bee897]572 fibril_mutex_unlock(&driver->driver_mutex);
573 return;
574 }
575
576 /*
577 * Go through devices list as long as there is some device
578 * that has not been passed to the driver.
579 */
[b72efe8]580 link = driver->devices.head.next;
581 while (link != &driver->devices.head) {
[ba38f72c]582 dev = list_get_instance(link, dev_node_t, driver_devices);
[5bee897]583 if (dev->passed_to_driver) {
[084ff99]584 link = link->next;
[5bee897]585 continue;
[084ff99]586 }
[5bee897]587
[2edcb63]588 /*
589 * We remove the device from the list to allow safe adding
590 * of new devices (no one will touch our item this way).
591 */
592 list_remove(link);
593
[5bee897]594 /*
595 * Unlock to avoid deadlock when adding device
596 * handled by itself.
597 */
598 fibril_mutex_unlock(&driver->driver_mutex);
599
[79ae36dd]600 add_device(sess, driver, dev, tree);
[5bee897]601
602 /*
603 * Lock again as we will work with driver's
604 * structure.
605 */
606 fibril_mutex_lock(&driver->driver_mutex);
607
[2edcb63]608 /*
609 * Insert the device back.
610 * The order is not relevant here so no harm is done
611 * (actually, the order would be preserved in most cases).
612 */
613 list_append(link, &driver->devices);
614
[5bee897]615 /*
616 * Restart the cycle to go through all devices again.
617 */
[b72efe8]618 link = driver->devices.head.next;
[084ff99]619 }
[5bee897]620
[79ae36dd]621 async_hangup(sess);
[5bee897]622
623 /*
624 * Once we passed all devices to the driver, we need to mark the
625 * driver as running.
626 * It is vital to do it here and inside critical section.
627 *
628 * If we would change the state earlier, other devices added to
629 * the driver would be added to the device list and started
630 * immediately and possibly started here as well.
631 */
[ebcb05a]632 log_msg(LVL_DEBUG, "Driver `%s' enters running state.", driver->name);
[5bee897]633 driver->state = DRIVER_RUNNING;
634
635 fibril_mutex_unlock(&driver->driver_mutex);
[c16cf62]636}
637
[38b3baf]638/** Finish the initialization of a driver after it has succesfully started
[bda60d9]639 * and after it has registered itself by the device manager.
[38b3baf]640 *
641 * Pass devices formerly matched to the driver to the driver and remember the
642 * driver is running and fully functional now.
643 *
644 * @param driver The driver which registered itself as running by the
645 * device manager.
[c16cf62]646 */
[38b3baf]647void initialize_running_driver(driver_t *driver, dev_tree_t *tree)
648{
[ebcb05a]649 log_msg(LVL_DEBUG, "initialize_running_driver(driver=\"%s\")",
[9b415c9]650 driver->name);
[c16cf62]651
[38b3baf]652 /*
653 * Pass devices which have been already assigned to the driver to the
654 * driver.
655 */
656 pass_devices_to_driver(driver, tree);
[c16cf62]657}
658
[791f58c]659/** Initialize device driver structure.
660 *
661 * @param drv The device driver structure.
662 */
663void init_driver(driver_t *drv)
664{
665 assert(drv != NULL);
666
667 memset(drv, 0, sizeof(driver_t));
668 list_initialize(&drv->match_ids.ids);
669 list_initialize(&drv->devices);
670 fibril_mutex_initialize(&drv->driver_mutex);
[79ae36dd]671 drv->sess = NULL;
[791f58c]672}
673
674/** Device driver structure clean-up.
675 *
676 * @param drv The device driver structure.
677 */
678void clean_driver(driver_t *drv)
679{
680 assert(drv != NULL);
681
682 free_not_null(drv->name);
683 free_not_null(drv->binary_path);
684
685 clean_match_ids(&drv->match_ids);
686
687 init_driver(drv);
688}
689
690/** Delete device driver structure.
691 *
692 * @param drv The device driver structure.
693 */
694void delete_driver(driver_t *drv)
695{
696 assert(drv != NULL);
697
698 clean_driver(drv);
699 free(drv);
700}
[a32defa]701
[ba38f72c]702/** Create devmap path and name for the function. */
[8b1e15ac]703void devmap_register_tree_function(fun_node_t *fun, dev_tree_t *tree)
[a32defa]704{
705 char *devmap_pathname = NULL;
706 char *devmap_name = NULL;
707
[ba38f72c]708 asprintf(&devmap_name, "%s", fun->pathname);
[58b833c]709 if (devmap_name == NULL)
[a32defa]710 return;
711
712 replace_char(devmap_name, '/', DEVMAP_SEPARATOR);
713
[38b3baf]714 asprintf(&devmap_pathname, "%s/%s", DEVMAP_DEVICE_NAMESPACE,
715 devmap_name);
[58b833c]716 if (devmap_pathname == NULL) {
[a32defa]717 free(devmap_name);
718 return;
[38b3baf]719 }
[a32defa]720
[47a7174f]721 devmap_device_register_with_iface(devmap_pathname,
[ba38f72c]722 &fun->devmap_handle, DEVMAN_CONNECT_FROM_DEVMAP);
[a32defa]723
[ba38f72c]724 tree_add_devmap_function(tree, fun);
[a32defa]725
726 free(devmap_name);
[38b3baf]727 free(devmap_pathname);
[a32defa]728}
729
[0c3666d]730/** Pass a device to running driver.
[38b3baf]731 *
732 * @param drv The driver's structure.
733 * @param node The device's node in the device tree.
[0c3666d]734 */
[79ae36dd]735void add_device(async_sess_t *sess, driver_t *drv, dev_node_t *dev,
736 dev_tree_t *tree)
[85e48a9]737{
[5bee897]738 /*
739 * We do not expect to have driver's mutex locked as we do not
740 * access any structures that would affect driver_t.
741 */
[ebcb05a]742 log_msg(LVL_DEBUG, "add_device(drv=\"%s\", dev=\"%s\")",
[9b415c9]743 drv->name, dev->pfun->name);
[a78fa2a]744
[38b3baf]745 /* Send the device to the driver. */
[0d6915f]746 devman_handle_t parent_handle;
[ba38f72c]747 if (dev->pfun) {
748 parent_handle = dev->pfun->handle;
[0d6915f]749 } else {
750 parent_handle = 0;
751 }
[79ae36dd]752
753 async_exch_t *exch = async_exchange_begin(sess);
754
755 ipc_call_t answer;
756 aid_t req = async_send_2(exch, DRIVER_ADD_DEVICE, dev->handle,
[0d6915f]757 parent_handle, &answer);
[a78fa2a]758
[79ae36dd]759 /* Send the device name to the driver. */
760 sysarg_t rc = async_data_write_start(exch, dev->pfun->name,
[ba38f72c]761 str_size(dev->pfun->name) + 1);
[79ae36dd]762
763 async_exchange_end(exch);
764
[38b3baf]765 if (rc != EOK) {
766 /* TODO handle error */
767 }
[398c4d7]768
[38b3baf]769 /* Wait for answer from the driver. */
[a78fa2a]770 async_wait_for(req, &rc);
[5bee897]771
[a78fa2a]772 switch(rc) {
773 case EOK:
[ba38f72c]774 dev->state = DEVICE_USABLE;
[df747b9c]775 break;
[a78fa2a]776 case ENOENT:
[ba38f72c]777 dev->state = DEVICE_NOT_PRESENT;
[a78fa2a]778 break;
[df747b9c]779 default:
[ba38f72c]780 dev->state = DEVICE_INVALID;
[084ff99]781 }
[e85920d]782
[ba38f72c]783 dev->passed_to_driver = true;
[5bee897]784
[5cd136ab]785 return;
[85e48a9]786}
787
[38b3baf]788/** Find suitable driver for a device and assign the driver to it.
789 *
790 * @param node The device node of the device in the device tree.
791 * @param drivers_list The list of available drivers.
792 * @return True if the suitable driver is found and
793 * successfully assigned to the device, false otherwise.
[0c3666d]794 */
[ba38f72c]795bool assign_driver(dev_node_t *dev, driver_list_t *drivers_list,
796 dev_tree_t *tree)
[85e48a9]797{
[ba38f72c]798 assert(dev != NULL);
799 assert(drivers_list != NULL);
800 assert(tree != NULL);
801
[38b3baf]802 /*
803 * Find the driver which is the most suitable for handling this device.
804 */
[ba38f72c]805 driver_t *drv = find_best_match_driver(drivers_list, dev);
[58b833c]806 if (drv == NULL) {
[ebcb05a]807 log_msg(LVL_ERROR, "No driver found for device `%s'.",
[ba38f72c]808 dev->pfun->pathname);
[38b3baf]809 return false;
[85e48a9]810 }
811
[38b3baf]812 /* Attach the driver to the device. */
[ba38f72c]813 attach_driver(dev, drv);
[85e48a9]814
[398c4d7]815 fibril_mutex_lock(&drv->driver_mutex);
[58b833c]816 if (drv->state == DRIVER_NOT_STARTED) {
[38b3baf]817 /* Start the driver. */
[85e48a9]818 start_driver(drv);
[38b3baf]819 }
[398c4d7]820 bool is_running = drv->state == DRIVER_RUNNING;
821 fibril_mutex_unlock(&drv->driver_mutex);
822
823 if (is_running) {
[38b3baf]824 /* Notify the driver about the new device. */
[79ae36dd]825 async_exch_t *exch = async_exchange_begin(drv->sess);
826 async_sess_t *sess = async_connect_me_to(EXCHANGE_SERIALIZE, exch,
827 DRIVER_DEVMAN, 0, 0);
828 async_exchange_end(exch);
829
830 if (sess) {
831 add_device(sess, drv, dev, tree);
832 async_hangup(sess);
[084ff99]833 }
[85e48a9]834 }
835
836 return true;
837}
838
[38b3baf]839/** Initialize the device tree.
840 *
[0c3666d]841 * Create root device node of the tree and assign driver to it.
[38b3baf]842 *
843 * @param tree The device tree.
844 * @param drivers_list the list of available drivers.
845 * @return True on success, false otherwise.
[0c3666d]846 */
847bool init_device_tree(dev_tree_t *tree, driver_list_t *drivers_list)
[85e48a9]848{
[ebcb05a]849 log_msg(LVL_DEBUG, "init_device_tree()");
[0c3666d]850
[957cfa58]851 tree->current_handle = 0;
852
[38b3baf]853 hash_table_create(&tree->devman_devices, DEVICE_BUCKETS, 1,
854 &devman_devices_ops);
[ba38f72c]855 hash_table_create(&tree->devman_functions, DEVICE_BUCKETS, 1,
856 &devman_functions_ops);
857 hash_table_create(&tree->devmap_functions, DEVICE_BUCKETS, 1,
[38b3baf]858 &devmap_devices_ops);
[bda60d9]859
[957cfa58]860 fibril_rwlock_initialize(&tree->rwlock);
[084ff99]861
[ba38f72c]862 /* Create root function and root device and add them to the device tree. */
863 if (!create_root_nodes(tree))
[85e48a9]864 return false;
[e4c4247]865
[38b3baf]866 /* Find suitable driver and start it. */
[ba38f72c]867 return assign_driver(tree->root_node->child, drivers_list, tree);
[e4c4247]868}
869
[791f58c]870/* Device nodes */
871
872/** Create a new device node.
873 *
874 * @return A device node structure.
875 */
[ba38f72c]876dev_node_t *create_dev_node(void)
[791f58c]877{
[ba38f72c]878 dev_node_t *res = malloc(sizeof(dev_node_t));
[791f58c]879
880 if (res != NULL) {
[ba38f72c]881 memset(res, 0, sizeof(dev_node_t));
882 list_initialize(&res->functions);
883 link_initialize(&res->driver_devices);
884 link_initialize(&res->devman_dev);
[791f58c]885 }
886
887 return res;
888}
889
890/** Delete a device node.
891 *
892 * @param node The device node structure.
893 */
[ba38f72c]894void delete_dev_node(dev_node_t *dev)
[791f58c]895{
[ba38f72c]896 assert(list_empty(&dev->functions));
897 assert(dev->pfun == NULL);
898 assert(dev->drv == NULL);
899
900 free(dev);
[791f58c]901}
902
903/** Find the device node structure of the device witch has the specified handle.
904 *
905 * @param tree The device tree where we look for the device node.
906 * @param handle The handle of the device.
907 * @return The device node.
908 */
[ba38f72c]909dev_node_t *find_dev_node_no_lock(dev_tree_t *tree, devman_handle_t handle)
[791f58c]910{
911 unsigned long key = handle;
[01b87dc5]912 link_t *link;
913
914 assert(fibril_rwlock_is_locked(&tree->rwlock));
915
916 link = hash_table_find(&tree->devman_devices, &key);
[ba38f72c]917 return hash_table_get_instance(link, dev_node_t, devman_dev);
[791f58c]918}
919
920/** Find the device node structure of the device witch has the specified handle.
921 *
922 * @param tree The device tree where we look for the device node.
923 * @param handle The handle of the device.
924 * @return The device node.
925 */
[ba38f72c]926dev_node_t *find_dev_node(dev_tree_t *tree, devman_handle_t handle)
[791f58c]927{
[ba38f72c]928 dev_node_t *dev = NULL;
[791f58c]929
930 fibril_rwlock_read_lock(&tree->rwlock);
[ba38f72c]931 dev = find_dev_node_no_lock(tree, handle);
[791f58c]932 fibril_rwlock_read_unlock(&tree->rwlock);
933
[ba38f72c]934 return dev;
935}
936
937/* Function nodes */
938
939/** Create a new function node.
940 *
941 * @return A function node structure.
942 */
943fun_node_t *create_fun_node(void)
944{
945 fun_node_t *res = malloc(sizeof(fun_node_t));
946
947 if (res != NULL) {
948 memset(res, 0, sizeof(fun_node_t));
949 link_initialize(&res->dev_functions);
950 list_initialize(&res->match_ids.ids);
951 list_initialize(&res->classes);
952 link_initialize(&res->devman_fun);
953 link_initialize(&res->devmap_fun);
954 }
955
956 return res;
957}
958
959/** Delete a function node.
960 *
961 * @param fun The device node structure.
962 */
963void delete_fun_node(fun_node_t *fun)
964{
965 assert(fun->dev == NULL);
966 assert(fun->child == NULL);
967
968 clean_match_ids(&fun->match_ids);
969 free_not_null(fun->name);
970 free_not_null(fun->pathname);
971 free(fun);
972}
973
974/** Find the function node with the specified handle.
975 *
976 * @param tree The device tree where we look for the device node.
977 * @param handle The handle of the function.
978 * @return The function node.
979 */
980fun_node_t *find_fun_node_no_lock(dev_tree_t *tree, devman_handle_t handle)
981{
982 unsigned long key = handle;
983 link_t *link;
984
985 assert(fibril_rwlock_is_locked(&tree->rwlock));
986
987 link = hash_table_find(&tree->devman_functions, &key);
988 if (link == NULL)
989 return NULL;
990
991 return hash_table_get_instance(link, fun_node_t, devman_fun);
[791f58c]992}
993
[ba38f72c]994/** Find the function node with the specified handle.
995 *
996 * @param tree The device tree where we look for the device node.
997 * @param handle The handle of the function.
998 * @return The function node.
999 */
1000fun_node_t *find_fun_node(dev_tree_t *tree, devman_handle_t handle)
1001{
1002 fun_node_t *fun = NULL;
1003
1004 fibril_rwlock_read_lock(&tree->rwlock);
1005 fun = find_fun_node_no_lock(tree, handle);
1006 fibril_rwlock_read_unlock(&tree->rwlock);
1007
1008 return fun;
1009}
[791f58c]1010
[bda60d9]1011/** Create and set device's full path in device tree.
[38b3baf]1012 *
1013 * @param node The device's device node.
1014 * @param parent The parent device node.
1015 * @return True on success, false otherwise (insufficient
1016 * resources etc.).
[bda60d9]1017 */
[ba38f72c]1018static bool set_fun_path(fun_node_t *fun, fun_node_t *parent)
[38b3baf]1019{
[ba38f72c]1020 assert(fun->name != NULL);
[bda60d9]1021
[ba38f72c]1022 size_t pathsize = (str_size(fun->name) + 1);
[58b833c]1023 if (parent != NULL)
[38b3baf]1024 pathsize += str_size(parent->pathname) + 1;
[bda60d9]1025
[ba38f72c]1026 fun->pathname = (char *) malloc(pathsize);
1027 if (fun->pathname == NULL) {
[ebcb05a]1028 log_msg(LVL_ERROR, "Failed to allocate device path.");
[bda60d9]1029 return false;
1030 }
1031
[58b833c]1032 if (parent != NULL) {
[ba38f72c]1033 str_cpy(fun->pathname, pathsize, parent->pathname);
1034 str_append(fun->pathname, pathsize, "/");
1035 str_append(fun->pathname, pathsize, fun->name);
[bda60d9]1036 } else {
[ba38f72c]1037 str_cpy(fun->pathname, pathsize, fun->name);
[bda60d9]1038 }
1039
1040 return true;
1041}
1042
1043/** Insert new device into device tree.
[38b3baf]1044 *
1045 * @param tree The device tree.
1046 * @param node The newly added device node.
1047 * @param dev_name The name of the newly added device.
1048 * @param parent The parent device node.
[58b833c]1049 *
[38b3baf]1050 * @return True on success, false otherwise (insufficient resources
1051 * etc.).
[bda60d9]1052 */
[ba38f72c]1053bool insert_dev_node(dev_tree_t *tree, dev_node_t *dev, fun_node_t *pfun)
1054{
1055 assert(dev != NULL);
1056 assert(tree != NULL);
1057 assert(fibril_rwlock_is_write_locked(&tree->rwlock));
1058
[ebcb05a]1059 log_msg(LVL_DEBUG, "insert_dev_node(dev=%p, pfun=%p [\"%s\"])",
[9b415c9]1060 dev, pfun, pfun->pathname);
1061
[ba38f72c]1062 /* Add the node to the handle-to-node map. */
1063 dev->handle = ++tree->current_handle;
1064 unsigned long key = dev->handle;
1065 hash_table_insert(&tree->devman_devices, &key, &dev->devman_dev);
1066
1067 /* Add the node to the list of its parent's children. */
1068 dev->pfun = pfun;
1069 pfun->child = dev;
1070
1071 return true;
1072}
1073
1074/** Insert new function into device tree.
1075 *
1076 * @param tree The device tree.
1077 * @param node The newly added function node.
1078 * @param dev_name The name of the newly added function.
1079 * @param parent Owning device node.
1080 *
1081 * @return True on success, false otherwise (insufficient resources
1082 * etc.).
1083 */
1084bool insert_fun_node(dev_tree_t *tree, fun_node_t *fun, char *fun_name,
1085 dev_node_t *dev)
[bda60d9]1086{
[ba38f72c]1087 fun_node_t *pfun;
1088
1089 assert(fun != NULL);
[58b833c]1090 assert(tree != NULL);
[ba38f72c]1091 assert(fun_name != NULL);
[01b87dc5]1092 assert(fibril_rwlock_is_write_locked(&tree->rwlock));
[bda60d9]1093
[ba38f72c]1094 /*
1095 * The root function is a special case, it does not belong to any
1096 * device so for the root function dev == NULL.
1097 */
1098 pfun = (dev != NULL) ? dev->pfun : NULL;
1099
1100 fun->name = fun_name;
1101 if (!set_fun_path(fun, pfun)) {
[38b3baf]1102 return false;
[bda60d9]1103 }
1104
[38b3baf]1105 /* Add the node to the handle-to-node map. */
[ba38f72c]1106 fun->handle = ++tree->current_handle;
1107 unsigned long key = fun->handle;
1108 hash_table_insert(&tree->devman_functions, &key, &fun->devman_fun);
[bda60d9]1109
[38b3baf]1110 /* Add the node to the list of its parent's children. */
[ba38f72c]1111 fun->dev = dev;
1112 if (dev != NULL)
1113 list_append(&fun->dev_functions, &dev->functions);
[38b3baf]1114
[bda60d9]1115 return true;
1116}
1117
[ba38f72c]1118/** Find function node with a specified path in the device tree.
[5cd136ab]1119 *
[ba38f72c]1120 * @param path The path of the function node in the device tree.
[38b3baf]1121 * @param tree The device tree.
[ba38f72c]1122 * @return The function node if it is present in the tree, NULL
[38b3baf]1123 * otherwise.
[5cd136ab]1124 */
[ba38f72c]1125fun_node_t *find_fun_node_by_path(dev_tree_t *tree, char *path)
[5cd136ab]1126{
[df147c7]1127 assert(path != NULL);
1128
1129 bool is_absolute = path[0] == '/';
1130 if (!is_absolute) {
1131 return NULL;
1132 }
1133
[957cfa58]1134 fibril_rwlock_read_lock(&tree->rwlock);
1135
[ba38f72c]1136 fun_node_t *fun = tree->root_node;
[38b3baf]1137 /*
[ba38f72c]1138 * Relative path to the function from its parent (but with '/' at the
[38b3baf]1139 * beginning)
1140 */
[5cd136ab]1141 char *rel_path = path;
1142 char *next_path_elem = NULL;
[df147c7]1143 bool cont = true;
[5cd136ab]1144
[ba38f72c]1145 while (cont && fun != NULL) {
[38b3baf]1146 next_path_elem = get_path_elem_end(rel_path + 1);
[58b833c]1147 if (next_path_elem[0] == '/') {
[5cd136ab]1148 cont = true;
1149 next_path_elem[0] = 0;
1150 } else {
1151 cont = false;
1152 }
1153
[ba38f72c]1154 fun = find_node_child(fun, rel_path + 1);
[5cd136ab]1155
1156 if (cont) {
[38b3baf]1157 /* Restore the original path. */
[5cd136ab]1158 next_path_elem[0] = '/';
1159 }
[38b3baf]1160 rel_path = next_path_elem;
[5cd136ab]1161 }
1162
[957cfa58]1163 fibril_rwlock_read_unlock(&tree->rwlock);
1164
[ba38f72c]1165 return fun;
[5cd136ab]1166}
1167
[0876062]1168/** Find function with a specified name belonging to given device.
[38b3baf]1169 *
1170 * Device tree rwlock should be held at least for reading.
1171 *
[0876062]1172 * @param dev Device the function belongs to.
1173 * @param name Function name (not path).
1174 * @return Function node.
1175 * @retval NULL No function with given name.
[5cd136ab]1176 */
[0876062]1177fun_node_t *find_fun_node_in_device(dev_node_t *dev, const char *name)
[5cd136ab]1178{
[0876062]1179 assert(dev != NULL);
1180 assert(name != NULL);
1181
[ba38f72c]1182 fun_node_t *fun;
[0876062]1183
[b72efe8]1184 list_foreach(dev->functions, link) {
[ba38f72c]1185 fun = list_get_instance(link, fun_node_t, dev_functions);
[0876062]1186
[ba38f72c]1187 if (str_cmp(name, fun->name) == 0)
1188 return fun;
[38b3baf]1189 }
[0876062]1190
[5cd136ab]1191 return NULL;
1192}
1193
[fc8c2b6]1194/** Find function node by its class name and index. */
1195fun_node_t *find_fun_node_by_class(class_list_t *class_list,
1196 const char *class_name, const char *dev_name)
1197{
1198 assert(class_list != NULL);
1199 assert(class_name != NULL);
1200 assert(dev_name != NULL);
1201
1202 fibril_rwlock_read_lock(&class_list->rwlock);
1203
1204 dev_class_t *cl = find_dev_class_no_lock(class_list, class_name);
1205 if (cl == NULL) {
1206 fibril_rwlock_read_unlock(&class_list->rwlock);
1207 return NULL;
1208 }
1209
1210 dev_class_info_t *dev = find_dev_in_class(cl, dev_name);
1211 if (dev == NULL) {
1212 fibril_rwlock_read_unlock(&class_list->rwlock);
1213 return NULL;
1214 }
1215
1216 fun_node_t *fun = dev->fun;
1217
1218 fibril_rwlock_read_unlock(&class_list->rwlock);
1219
1220 return fun;
1221}
1222
1223
[0876062]1224/** Find child function node with a specified name.
1225 *
1226 * Device tree rwlock should be held at least for reading.
1227 *
1228 * @param parent The parent function node.
1229 * @param name The name of the child function.
1230 * @return The child function node.
1231 */
1232fun_node_t *find_node_child(fun_node_t *pfun, const char *name)
1233{
1234 return find_fun_node_in_device(pfun->child, name);
1235}
1236
[791f58c]1237/* Device classes */
1238
1239/** Create device class.
1240 *
1241 * @return Device class.
1242 */
1243dev_class_t *create_dev_class(void)
1244{
1245 dev_class_t *cl;
1246
1247 cl = (dev_class_t *) malloc(sizeof(dev_class_t));
1248 if (cl != NULL) {
1249 memset(cl, 0, sizeof(dev_class_t));
1250 list_initialize(&cl->devices);
1251 fibril_mutex_initialize(&cl->mutex);
1252 }
1253
1254 return cl;
1255}
1256
1257/** Create device class info.
1258 *
1259 * @return Device class info.
1260 */
1261dev_class_info_t *create_dev_class_info(void)
1262{
1263 dev_class_info_t *info;
1264
1265 info = (dev_class_info_t *) malloc(sizeof(dev_class_info_t));
[3ca3430]1266 if (info != NULL) {
[791f58c]1267 memset(info, 0, sizeof(dev_class_info_t));
[ca2a18e]1268 link_initialize(&info->dev_classes);
1269 link_initialize(&info->devmap_link);
1270 link_initialize(&info->link);
[3ca3430]1271 }
[791f58c]1272
1273 return info;
1274}
1275
1276size_t get_new_class_dev_idx(dev_class_t *cl)
1277{
1278 size_t dev_idx;
1279
1280 fibril_mutex_lock(&cl->mutex);
1281 dev_idx = ++cl->curr_dev_idx;
1282 fibril_mutex_unlock(&cl->mutex);
1283
1284 return dev_idx;
1285}
1286
1287
[38b3baf]1288/** Create unique device name within the class.
1289 *
1290 * @param cl The class.
1291 * @param base_dev_name Contains the base name for the device if it was
1292 * specified by the driver when it registered the device by
1293 * the class; NULL if driver specified no base name.
1294 * @return The unique name for the device within the class.
[d51ee2b]1295 */
[38b3baf]1296char *create_dev_name_for_class(dev_class_t *cl, const char *base_dev_name)
[d51ee2b]1297{
1298 char *dev_name;
1299 const char *base_name;
[38b3baf]1300
[58b833c]1301 if (base_dev_name != NULL)
[d51ee2b]1302 base_name = base_dev_name;
[38b3baf]1303 else
[d51ee2b]1304 base_name = cl->base_dev_name;
1305
1306 size_t idx = get_new_class_dev_idx(cl);
[7e752b2]1307 asprintf(&dev_name, "%s%zu", base_name, idx);
[38b3baf]1308
1309 return dev_name;
[d51ee2b]1310}
1311
[ba38f72c]1312/** Add the device function to the class.
[38b3baf]1313 *
1314 * The device may be added to multiple classes and a class may contain multiple
1315 * devices. The class and the device are associated with each other by the
1316 * dev_class_info_t structure.
1317 *
1318 * @param dev The device.
1319 * @param class The class.
1320 * @param base_dev_name The base name of the device within the class if
1321 * specified by the driver, NULL otherwise.
1322 * @return dev_class_info_t structure which associates the device
1323 * with the class.
[d51ee2b]1324 */
[ba38f72c]1325dev_class_info_t *add_function_to_class(fun_node_t *fun, dev_class_t *cl,
[58b833c]1326 const char *base_dev_name)
[38b3baf]1327{
[ba38f72c]1328 dev_class_info_t *info;
1329
1330 assert(fun != NULL);
1331 assert(cl != NULL);
1332
1333 info = create_dev_class_info();
1334
[38b3baf]1335
[58b833c]1336 if (info != NULL) {
[692c40cb]1337 info->dev_class = cl;
[ba38f72c]1338 info->fun = fun;
[692c40cb]1339
[38b3baf]1340 /* Add the device to the class. */
[692c40cb]1341 fibril_mutex_lock(&cl->mutex);
1342 list_append(&info->link, &cl->devices);
1343 fibril_mutex_unlock(&cl->mutex);
1344
[38b3baf]1345 /* Add the class to the device. */
[ba38f72c]1346 list_append(&info->dev_classes, &fun->classes);
[692c40cb]1347
[38b3baf]1348 /* Create unique name for the device within the class. */
1349 info->dev_name = create_dev_name_for_class(cl, base_dev_name);
[692c40cb]1350 }
[d51ee2b]1351
1352 return info;
1353}
1354
[38b3baf]1355dev_class_t *get_dev_class(class_list_t *class_list, char *class_name)
[692c40cb]1356{
1357 dev_class_t *cl;
[38b3baf]1358
1359 fibril_rwlock_write_lock(&class_list->rwlock);
[692c40cb]1360 cl = find_dev_class_no_lock(class_list, class_name);
[58b833c]1361 if (cl == NULL) {
[692c40cb]1362 cl = create_dev_class();
[58b833c]1363 if (cl != NULL) {
[38b3baf]1364 cl->name = class_name;
[692c40cb]1365 cl->base_dev_name = "";
1366 add_dev_class_no_lock(class_list, cl);
[38b3baf]1367 }
1368 }
[58b833c]1369
[ce89036b]1370 fibril_rwlock_write_unlock(&class_list->rwlock);
[692c40cb]1371 return cl;
1372}
1373
[58b833c]1374dev_class_t *find_dev_class_no_lock(class_list_t *class_list,
1375 const char *class_name)
[692c40cb]1376{
1377 dev_class_t *cl;
[38b3baf]1378
[b72efe8]1379 list_foreach(class_list->classes, link) {
[692c40cb]1380 cl = list_get_instance(link, dev_class_t, link);
[2edcb63]1381 if (str_cmp(cl->name, class_name) == 0) {
[692c40cb]1382 return cl;
[2edcb63]1383 }
[692c40cb]1384 }
1385
[38b3baf]1386 return NULL;
[692c40cb]1387}
1388
[791f58c]1389void add_dev_class_no_lock(class_list_t *class_list, dev_class_t *cl)
1390{
1391 list_append(&cl->link, &class_list->classes);
1392}
1393
[fc8c2b6]1394dev_class_info_t *find_dev_in_class(dev_class_t *dev_class, const char *dev_name)
1395{
1396 assert(dev_class != NULL);
1397 assert(dev_name != NULL);
1398
[b72efe8]1399 list_foreach(dev_class->devices, link) {
[fc8c2b6]1400 dev_class_info_t *dev = list_get_instance(link,
1401 dev_class_info_t, link);
1402
1403 if (str_cmp(dev->dev_name, dev_name) == 0) {
1404 return dev;
1405 }
1406 }
1407
1408 return NULL;
1409}
1410
[ce89036b]1411void init_class_list(class_list_t *class_list)
1412{
1413 list_initialize(&class_list->classes);
1414 fibril_rwlock_initialize(&class_list->rwlock);
[ba38f72c]1415 hash_table_create(&class_list->devmap_functions, DEVICE_BUCKETS, 1,
[3ca3430]1416 &devmap_devices_class_ops);
[ce89036b]1417}
1418
1419
[791f58c]1420/* Devmap devices */
[ce89036b]1421
[ba38f72c]1422fun_node_t *find_devmap_tree_function(dev_tree_t *tree, devmap_handle_t devmap_handle)
[ce89036b]1423{
[ba38f72c]1424 fun_node_t *fun = NULL;
[ce89036b]1425 link_t *link;
[38b3baf]1426 unsigned long key = (unsigned long) devmap_handle;
[ce89036b]1427
1428 fibril_rwlock_read_lock(&tree->rwlock);
[ba38f72c]1429 link = hash_table_find(&tree->devmap_functions, &key);
[58b833c]1430 if (link != NULL)
[ba38f72c]1431 fun = hash_table_get_instance(link, fun_node_t, devmap_fun);
[ce89036b]1432 fibril_rwlock_read_unlock(&tree->rwlock);
1433
[ba38f72c]1434 return fun;
[ce89036b]1435}
1436
[ba38f72c]1437fun_node_t *find_devmap_class_function(class_list_t *classes,
[991f645]1438 devmap_handle_t devmap_handle)
[ce89036b]1439{
[ba38f72c]1440 fun_node_t *fun = NULL;
[ce89036b]1441 dev_class_info_t *cli;
1442 link_t *link;
1443 unsigned long key = (unsigned long)devmap_handle;
1444
1445 fibril_rwlock_read_lock(&classes->rwlock);
[ba38f72c]1446 link = hash_table_find(&classes->devmap_functions, &key);
[58b833c]1447 if (link != NULL) {
[38b3baf]1448 cli = hash_table_get_instance(link, dev_class_info_t,
1449 devmap_link);
[ba38f72c]1450 fun = cli->fun;
[ce89036b]1451 }
1452 fibril_rwlock_read_unlock(&classes->rwlock);
1453
[ba38f72c]1454 return fun;
[ce89036b]1455}
1456
[ba38f72c]1457void class_add_devmap_function(class_list_t *class_list, dev_class_info_t *cli)
[791f58c]1458{
1459 unsigned long key = (unsigned long) cli->devmap_handle;
1460
1461 fibril_rwlock_write_lock(&class_list->rwlock);
[ba38f72c]1462 hash_table_insert(&class_list->devmap_functions, &key, &cli->devmap_link);
[791f58c]1463 fibril_rwlock_write_unlock(&class_list->rwlock);
[3ca3430]1464
[ba38f72c]1465 assert(find_devmap_class_function(class_list, cli->devmap_handle) != NULL);
[791f58c]1466}
1467
[ba38f72c]1468void tree_add_devmap_function(dev_tree_t *tree, fun_node_t *fun)
[791f58c]1469{
[ba38f72c]1470 unsigned long key = (unsigned long) fun->devmap_handle;
[791f58c]1471 fibril_rwlock_write_lock(&tree->rwlock);
[ba38f72c]1472 hash_table_insert(&tree->devmap_functions, &key, &fun->devmap_fun);
[791f58c]1473 fibril_rwlock_write_unlock(&tree->rwlock);
1474}
1475
[c16cf62]1476/** @}
[58b833c]1477 */
Note: See TracBrowser for help on using the repository browser.