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

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

Logging functions should append newline automatically. Since one has no
choice but to end log message with a newline, there is no need to do it
manually in every invocation.

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