source: mainline/uspace/srv/devman/devman.c@ 791f58c

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

Convert inline functions to regular.

  • Property mode set to 100644
File size: 28.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>
[084ff99]36#include <ipc/driver.h>
37#include <ipc/devman.h>
[a32defa]38#include <devmap.h>
[0358da0]39
[e2b9a993]40#include "devman.h"
[0358da0]41
[38b3baf]42/* hash table operations */
[957cfa58]43
44static hash_index_t devices_hash(unsigned long key[])
45{
46 return key[0] % DEVICE_BUCKETS;
47}
48
[58b833c]49static int devman_devices_compare(unsigned long key[], hash_count_t keys,
50 link_t *item)
[957cfa58]51{
52 node_t *dev = hash_table_get_instance(item, node_t, devman_link);
53 return (dev->handle == (device_handle_t) key[0]);
54}
55
[58b833c]56static int devmap_devices_compare(unsigned long key[], hash_count_t keys,
57 link_t *item)
[957cfa58]58{
59 node_t *dev = hash_table_get_instance(item, node_t, devmap_link);
60 return (dev->devmap_handle == (dev_handle_t) key[0]);
61}
62
63static void devices_remove_callback(link_t *item)
64{
65}
66
67static hash_table_operations_t devman_devices_ops = {
68 .hash = devices_hash,
69 .compare = devman_devices_compare,
70 .remove_callback = devices_remove_callback
71};
72
73static hash_table_operations_t devmap_devices_ops = {
74 .hash = devices_hash,
75 .compare = devmap_devices_compare,
76 .remove_callback = devices_remove_callback
77};
78
[791f58c]79/**
80 * Initialize the list of device driver's.
81 *
82 * @param drv_list the list of device driver's.
83 *
84 */
85void init_driver_list(driver_list_t *drv_list)
86{
87 assert(drv_list != NULL);
88
89 list_initialize(&drv_list->drivers);
90 fibril_mutex_initialize(&drv_list->drivers_mutex);
91}
92
[0c3666d]93/** Allocate and initialize a new driver structure.
[38b3baf]94 *
[58b833c]95 * @return Driver structure.
[0c3666d]96 */
[38b3baf]97driver_t *create_driver(void)
[58b833c]98{
[e4c4247]99 driver_t *res = malloc(sizeof(driver_t));
[38b3baf]100 if (res != NULL)
[08d9c4e6]101 init_driver(res);
[e4c4247]102 return res;
103}
104
[0c3666d]105/** Add a driver to the list of drivers.
[38b3baf]106 *
[58b833c]107 * @param drivers_list List of drivers.
108 * @param drv Driver structure.
[0c3666d]109 */
110void add_driver(driver_list_t *drivers_list, driver_t *drv)
111{
112 fibril_mutex_lock(&drivers_list->drivers_mutex);
113 list_prepend(&drv->drivers, &drivers_list->drivers);
114 fibril_mutex_unlock(&drivers_list->drivers_mutex);
[58b833c]115
[38b3baf]116 printf(NAME": the '%s' driver was added to the list of available "
117 "drivers.\n", drv->name);
[0c3666d]118}
119
[38b3baf]120/** Read match id at the specified position of a string and set the position in
121 * the string to the first character following the id.
122 *
123 * @param buf The position in the input string.
124 * @return The match id.
[0c3666d]125 */
[38b3baf]126char *read_match_id(char **buf)
[e4c4247]127{
128 char *res = NULL;
[e2b9a993]129 size_t len = get_nonspace_len(*buf);
[38b3baf]130
[e4c4247]131 if (len > 0) {
132 res = malloc(len + 1);
133 if (res != NULL) {
[38b3baf]134 str_ncpy(res, len + 1, *buf, len);
[e4c4247]135 *buf += len;
136 }
137 }
[38b3baf]138
[e4c4247]139 return res;
140}
141
[0c3666d]142/**
143 * Read match ids and associated match scores from a string.
[38b3baf]144 *
145 * Each match score in the string is followed by its match id.
146 * The match ids and match scores are separated by whitespaces.
147 * Neither match ids nor match scores can contain whitespaces.
148 *
149 * @param buf The string from which the match ids are read.
150 * @param ids The list of match ids into which the match ids and
151 * scores are added.
152 * @return True if at least one match id and associated match score
153 * was successfully read, false otherwise.
[0c3666d]154 */
[c47e1a8]155bool parse_match_ids(char *buf, match_id_list_t *ids)
[e4c4247]156{
157 int score = 0;
158 char *id = NULL;
159 int ids_read = 0;
160
161 while (true) {
[38b3baf]162 /* skip spaces */
163 if (!skip_spaces(&buf))
[e4c4247]164 break;
[38b3baf]165
166 /* read score */
[e4c4247]167 score = strtoul(buf, &buf, 10);
168
[38b3baf]169 /* skip spaces */
170 if (!skip_spaces(&buf))
[e4c4247]171 break;
172
[38b3baf]173 /* read id */
174 id = read_match_id(&buf);
175 if (NULL == id)
176 break;
[e4c4247]177
[38b3baf]178 /* create new match_id structure */
[e4c4247]179 match_id_t *mid = create_match_id();
180 mid->id = id;
181 mid->score = score;
182
[38b3baf]183 /* add it to the list */
[e4c4247]184 add_match_id(ids, mid);
185
[38b3baf]186 ids_read++;
187 }
[e4c4247]188
189 return ids_read > 0;
190}
191
[0c3666d]192/**
193 * Read match ids and associated match scores from a file.
[38b3baf]194 *
195 * Each match score in the file is followed by its match id.
196 * The match ids and match scores are separated by whitespaces.
197 * Neither match ids nor match scores can contain whitespaces.
198 *
199 * @param buf The path to the file from which the match ids are read.
200 * @param ids The list of match ids into which the match ids and
201 * scores are added.
202 * @return True if at least one match id and associated match score
203 * was successfully read, false otherwise.
[0c3666d]204 */
[38b3baf]205bool read_match_ids(const char *conf_path, match_id_list_t *ids)
206{
[08d9c4e6]207 printf(NAME ": read_match_ids conf_path = %s.\n", conf_path);
208
[38b3baf]209 bool suc = false;
[e4c4247]210 char *buf = NULL;
211 bool opened = false;
[38b3baf]212 int fd;
[c47e1a8]213 size_t len = 0;
[e4c4247]214
215 fd = open(conf_path, O_RDONLY);
216 if (fd < 0) {
217 printf(NAME ": unable to open %s\n", conf_path);
218 goto cleanup;
[38b3baf]219 }
220 opened = true;
[e4c4247]221
222 len = lseek(fd, 0, SEEK_END);
[38b3baf]223 lseek(fd, 0, SEEK_SET);
[e4c4247]224 if (len == 0) {
225 printf(NAME ": configuration file '%s' is empty.\n", conf_path);
[38b3baf]226 goto cleanup;
[e4c4247]227 }
228
229 buf = malloc(len + 1);
230 if (buf == NULL) {
[38b3baf]231 printf(NAME ": memory allocation failed when parsing file "
232 "'%s'.\n", conf_path);
[e4c4247]233 goto cleanup;
[58b833c]234 }
[e4c4247]235
[58b833c]236 if (read(fd, buf, len) <= 0) {
[e4c4247]237 printf(NAME ": unable to read file '%s'.\n", conf_path);
238 goto cleanup;
239 }
240 buf[len] = 0;
241
242 suc = parse_match_ids(buf, ids);
243
244cleanup:
245 free(buf);
246
[58b833c]247 if (opened)
[38b3baf]248 close(fd);
[e4c4247]249
250 return suc;
251}
252
[0c3666d]253/**
254 * Get information about a driver.
[38b3baf]255 *
256 * Each driver has its own directory in the base directory.
[0c3666d]257 * The name of the driver's directory is the same as the name of the driver.
[38b3baf]258 * The driver's directory contains driver's binary (named as the driver without
259 * extension) and the configuration file with match ids for device-to-driver
260 * matching (named as the driver with a special extension).
261 *
262 * This function searches for the driver's directory and containing
263 * configuration files. If all the files needed are found, they are parsed and
264 * the information about the driver is stored in the driver's structure.
265 *
266 * @param base_path The base directory, in which we look for driver's
267 * subdirectory.
268 * @param name The name of the driver.
269 * @param drv The driver structure to fill information in.
270 *
271 * @return True on success, false otherwise.
[0c3666d]272 */
[e2b9a993]273bool get_driver_info(const char *base_path, const char *name, driver_t *drv)
[e4c4247]274{
[38b3baf]275 printf(NAME ": get_driver_info base_path = %s, name = %s.\n",
276 base_path, name);
[08d9c4e6]277
[e4c4247]278 assert(base_path != NULL && name != NULL && drv != NULL);
279
280 bool suc = false;
[38b3baf]281 char *match_path = NULL;
[e4c4247]282 size_t name_size = 0;
283
[38b3baf]284 /* Read the list of match ids from the driver's configuration file. */
285 match_path = get_abs_path(base_path, name, MATCH_EXT);
[58b833c]286 if (match_path == NULL)
[e4c4247]287 goto cleanup;
288
[38b3baf]289 if (!read_match_ids(match_path, &drv->match_ids))
[e4c4247]290 goto cleanup;
291
[38b3baf]292 /* Allocate and fill driver's name. */
293 name_size = str_size(name) + 1;
[e4c4247]294 drv->name = malloc(name_size);
[58b833c]295 if (drv->name == NULL)
[e4c4247]296 goto cleanup;
297 str_cpy(drv->name, name_size, name);
298
[38b3baf]299 /* Initialize path with driver's binary. */
300 drv->binary_path = get_abs_path(base_path, name, "");
[58b833c]301 if (drv->binary_path == NULL)
[85e48a9]302 goto cleanup;
303
[38b3baf]304 /* Check whether the driver's binary exists. */
[85e48a9]305 struct stat s;
[58b833c]306 if (stat(drv->binary_path, &s) == ENOENT) { /* FIXME!! */
[85e48a9]307 printf(NAME ": driver not found at path %s.", drv->binary_path);
308 goto cleanup;
309 }
310
[e4c4247]311 suc = true;
312
313cleanup:
314 if (!suc) {
315 free(drv->binary_path);
316 free(drv->name);
[38b3baf]317 /* Set the driver structure to the default state. */
318 init_driver(drv);
[e4c4247]319 }
320
321 free(match_path);
322
323 return suc;
324}
325
326/** Lookup drivers in the directory.
[38b3baf]327 *
328 * @param drivers_list The list of available drivers.
329 * @param dir_path The path to the directory where we search for drivers.
330 * @return Number of drivers which were found.
331 */
[0c3666d]332int lookup_available_drivers(driver_list_t *drivers_list, const char *dir_path)
[e4c4247]333{
[d347b53]334 printf(NAME ": lookup_available_drivers, dir = %s \n", dir_path);
[08d9c4e6]335
[e4c4247]336 int drv_cnt = 0;
337 DIR *dir = NULL;
338 struct dirent *diren;
339
340 dir = opendir(dir_path);
[08d9c4e6]341
[e4c4247]342 if (dir != NULL) {
343 driver_t *drv = create_driver();
[38b3baf]344 while ((diren = readdir(dir))) {
[e4c4247]345 if (get_driver_info(dir_path, diren->d_name, drv)) {
[e2b9a993]346 add_driver(drivers_list, drv);
[08d9c4e6]347 drv_cnt++;
[e4c4247]348 drv = create_driver();
[38b3baf]349 }
[e4c4247]350 }
351 delete_driver(drv);
352 closedir(dir);
353 }
354
355 return drv_cnt;
356}
357
[084ff99]358/** Create root device node in the device tree.
[38b3baf]359 *
[58b833c]360 * @param tree The device tree.
361 * @return True on success, false otherwise.
[0c3666d]362 */
[084ff99]363bool create_root_node(dev_tree_t *tree)
[e4c4247]364{
[58b833c]365 node_t *node;
366
[e85920d]367 printf(NAME ": create_root_node\n");
[58b833c]368
369 node = create_dev_node();
370 if (node != NULL) {
[c47e1a8]371 insert_dev_node(tree, node, clone_string(""), NULL);
[85e48a9]372 match_id_t *id = create_match_id();
[c47e1a8]373 id->id = clone_string("root");
[85e48a9]374 id->score = 100;
375 add_match_id(&node->match_ids, id);
[084ff99]376 tree->root_node = node;
[85e48a9]377 }
[58b833c]378
[38b3baf]379 return node != NULL;
[85e48a9]380}
381
[38b3baf]382/** Lookup the best matching driver for the specified device in the list of
383 * drivers.
[0c3666d]384 *
[38b3baf]385 * A match between a device and a driver is found if one of the driver's match
386 * ids match one of the device's match ids. The score of the match is the
387 * product of the driver's and device's score associated with the matching id.
388 * The best matching driver for a device is the driver with the highest score
389 * of the match between the device and the driver.
390 *
391 * @param drivers_list The list of drivers, where we look for the driver
392 * suitable for handling the device.
393 * @param node The device node structure of the device.
394 * @return The best matching driver or NULL if no matching driver
395 * is found.
[0c3666d]396 */
[38b3baf]397driver_t *find_best_match_driver(driver_list_t *drivers_list, node_t *node)
[e4c4247]398{
[85e48a9]399 driver_t *best_drv = NULL, *drv = NULL;
400 int best_score = 0, score = 0;
401
[0c3666d]402 fibril_mutex_lock(&drivers_list->drivers_mutex);
[729fa2d6]403
[38b3baf]404 link_t *link = drivers_list->drivers.next;
[0c3666d]405 while (link != &drivers_list->drivers) {
[85e48a9]406 drv = list_get_instance(link, driver_t, drivers);
407 score = get_match_score(drv, node);
408 if (score > best_score) {
409 best_score = score;
410 best_drv = drv;
[58b833c]411 }
[e85920d]412 link = link->next;
[0c3666d]413 }
[729fa2d6]414
[0c3666d]415 fibril_mutex_unlock(&drivers_list->drivers_mutex);
[e4c4247]416
[38b3baf]417 return best_drv;
[85e48a9]418}
419
[38b3baf]420/** Assign a driver to a device.
421 *
422 * @param node The device's node in the device tree.
423 * @param drv The driver.
[0c3666d]424 */
[38b3baf]425void attach_driver(node_t *node, driver_t *drv)
[85e48a9]426{
[38b3baf]427 printf(NAME ": attach_driver %s to device %s\n",
428 drv->name, node->pathname);
[2480e19]429
[0c3666d]430 fibril_mutex_lock(&drv->driver_mutex);
431
[85e48a9]432 node->drv = drv;
433 list_append(&node->driver_devices, &drv->devices);
[0c3666d]434
435 fibril_mutex_unlock(&drv->driver_mutex);
[85e48a9]436}
437
[38b3baf]438/** Start a driver
439 *
[0c3666d]440 * The driver's mutex is assumed to be locked.
[38b3baf]441 *
442 * @param drv The driver's structure.
443 * @return True if the driver's task is successfully spawned, false
444 * otherwise.
[0c3666d]445 */
[e2b9a993]446bool start_driver(driver_t *drv)
[85e48a9]447{
[d347b53]448 printf(NAME ": start_driver '%s'\n", drv->name);
[e85920d]449
[c47e1a8]450 const char *argv[2];
[85e48a9]451
452 argv[0] = drv->name;
453 argv[1] = NULL;
454
[c47e1a8]455 int err;
[58b833c]456 if (task_spawn(drv->binary_path, argv, &err) == 0) {
[38b3baf]457 printf(NAME ": error spawning %s, errno = %d\n",
458 drv->name, err);
[85e48a9]459 return false;
460 }
461
[e85920d]462 drv->state = DRIVER_STARTING;
[85e48a9]463 return true;
464}
465
[bda60d9]466/** Find device driver in the list of device drivers.
[38b3baf]467 *
468 * @param drv_list The list of device drivers.
469 * @param drv_name The name of the device driver which is searched.
470 * @return The device driver of the specified name, if it is in the
471 * list, NULL otherwise.
[bda60d9]472 */
[38b3baf]473driver_t *find_driver(driver_list_t *drv_list, const char *drv_name)
474{
[729fa2d6]475 driver_t *res = NULL;
[58b833c]476 driver_t *drv = NULL;
477 link_t *link;
[729fa2d6]478
[38b3baf]479 fibril_mutex_lock(&drv_list->drivers_mutex);
[729fa2d6]480
[58b833c]481 link = drv_list->drivers.next;
482 while (link != &drv_list->drivers) {
[729fa2d6]483 drv = list_get_instance(link, driver_t, drivers);
[58b833c]484 if (str_cmp(drv->name, drv_name) == 0) {
[729fa2d6]485 res = drv;
486 break;
[58b833c]487 }
488
[729fa2d6]489 link = link->next;
[58b833c]490 }
[729fa2d6]491
492 fibril_mutex_unlock(&drv_list->drivers_mutex);
493
494 return res;
495}
496
[bda60d9]497/** Remember the driver's phone.
[38b3baf]498 *
499 * @param driver The driver.
500 * @param phone The phone to the driver.
[bda60d9]501 */
[c16cf62]502void set_driver_phone(driver_t *driver, ipcarg_t phone)
[38b3baf]503{
504 fibril_mutex_lock(&driver->driver_mutex);
[58b833c]505 assert(driver->state == DRIVER_STARTING);
[38b3baf]506 driver->phone = phone;
507 fibril_mutex_unlock(&driver->driver_mutex);
[c16cf62]508}
509
[38b3baf]510/** Notify driver about the devices to which it was assigned.
511 *
[c16cf62]512 * The driver's mutex must be locked.
[38b3baf]513 *
514 * @param driver The driver to which the devices are passed.
[c16cf62]515 */
[a32defa]516static void pass_devices_to_driver(driver_t *driver, dev_tree_t *tree)
[38b3baf]517{
[c16cf62]518 node_t *dev;
519 link_t *link;
[58b833c]520 int phone;
521
522 printf(NAME ": pass_devices_to_driver\n");
523
524 phone = ipc_connect_me_to(driver->phone, DRIVER_DEVMAN, 0, 0);
525 if (phone > 0) {
[084ff99]526
527 link = driver->devices.next;
528 while (link != &driver->devices) {
529 dev = list_get_instance(link, node_t, driver_devices);
[a32defa]530 add_device(phone, driver, dev, tree);
[084ff99]531 link = link->next;
532 }
533
534 ipc_hangup(phone);
535 }
[c16cf62]536}
537
[38b3baf]538/** Finish the initialization of a driver after it has succesfully started
[bda60d9]539 * and after it has registered itself by the device manager.
[38b3baf]540 *
541 * Pass devices formerly matched to the driver to the driver and remember the
542 * driver is running and fully functional now.
543 *
544 * @param driver The driver which registered itself as running by the
545 * device manager.
[c16cf62]546 */
[38b3baf]547void initialize_running_driver(driver_t *driver, dev_tree_t *tree)
548{
[d347b53]549 printf(NAME ": initialize_running_driver\n");
[c16cf62]550 fibril_mutex_lock(&driver->driver_mutex);
551
[38b3baf]552 /*
553 * Pass devices which have been already assigned to the driver to the
554 * driver.
555 */
556 pass_devices_to_driver(driver, tree);
[c16cf62]557
[38b3baf]558 /* Change driver's state to running. */
559 driver->state = DRIVER_RUNNING;
[c16cf62]560
561 fibril_mutex_unlock(&driver->driver_mutex);
562}
563
[791f58c]564/** Initialize device driver structure.
565 *
566 * @param drv The device driver structure.
567 */
568void init_driver(driver_t *drv)
569{
570 assert(drv != NULL);
571
572 memset(drv, 0, sizeof(driver_t));
573 list_initialize(&drv->match_ids.ids);
574 list_initialize(&drv->devices);
575 fibril_mutex_initialize(&drv->driver_mutex);
576}
577
578/** Device driver structure clean-up.
579 *
580 * @param drv The device driver structure.
581 */
582void clean_driver(driver_t *drv)
583{
584 assert(drv != NULL);
585
586 free_not_null(drv->name);
587 free_not_null(drv->binary_path);
588
589 clean_match_ids(&drv->match_ids);
590
591 init_driver(drv);
592}
593
594/** Delete device driver structure.
595 *
596 * @param drv The device driver structure.
597 */
598void delete_driver(driver_t *drv)
599{
600 assert(drv != NULL);
601
602 clean_driver(drv);
603 free(drv);
604}
[a32defa]605
[38b3baf]606/** Create devmap path and name for the device. */
[a32defa]607static void devmap_register_tree_device(node_t *node, dev_tree_t *tree)
608{
609 char *devmap_pathname = NULL;
610 char *devmap_name = NULL;
611
612 asprintf(&devmap_name, "%s", node->pathname);
[58b833c]613 if (devmap_name == NULL)
[a32defa]614 return;
615
616 replace_char(devmap_name, '/', DEVMAP_SEPARATOR);
617
[38b3baf]618 asprintf(&devmap_pathname, "%s/%s", DEVMAP_DEVICE_NAMESPACE,
619 devmap_name);
[58b833c]620 if (devmap_pathname == NULL) {
[a32defa]621 free(devmap_name);
622 return;
[38b3baf]623 }
[a32defa]624
625 devmap_device_register(devmap_pathname, &node->devmap_handle);
626
627 tree_add_devmap_device(tree, node);
628
629 free(devmap_name);
[38b3baf]630 free(devmap_pathname);
[a32defa]631}
632
633
[0c3666d]634/** Pass a device to running driver.
[38b3baf]635 *
636 * @param drv The driver's structure.
637 * @param node The device's node in the device tree.
[0c3666d]638 */
[a32defa]639void add_device(int phone, driver_t *drv, node_t *node, dev_tree_t *tree)
[85e48a9]640{
[e85920d]641 printf(NAME ": add_device\n");
[a78fa2a]642
643 ipcarg_t rc;
644 ipc_call_t answer;
645
[38b3baf]646 /* Send the device to the driver. */
647 aid_t req = async_send_1(phone, DRIVER_ADD_DEVICE, node->handle,
648 &answer);
[a78fa2a]649
[38b3baf]650 /* Send the device's name to the driver. */
651 rc = async_data_write_start(phone, node->name,
652 str_size(node->name) + 1);
653 if (rc != EOK) {
654 /* TODO handle error */
655 }
[a78fa2a]656
[38b3baf]657 /* Wait for answer from the driver. */
[a78fa2a]658 async_wait_for(req, &rc);
659 switch(rc) {
660 case EOK:
[df747b9c]661 node->state = DEVICE_USABLE;
[a32defa]662 devmap_register_tree_device(node, tree);
[df747b9c]663 break;
[a78fa2a]664 case ENOENT:
[df747b9c]665 node->state = DEVICE_NOT_PRESENT;
[a78fa2a]666 break;
[df747b9c]667 default:
[38b3baf]668 node->state = DEVICE_INVALID;
[084ff99]669 }
[e85920d]670
[5cd136ab]671 return;
[85e48a9]672}
673
[38b3baf]674/** Find suitable driver for a device and assign the driver to it.
675 *
676 * @param node The device node of the device in the device tree.
677 * @param drivers_list The list of available drivers.
678 * @return True if the suitable driver is found and
679 * successfully assigned to the device, false otherwise.
[0c3666d]680 */
[38b3baf]681bool assign_driver(node_t *node, driver_list_t *drivers_list, dev_tree_t *tree)
[85e48a9]682{
[38b3baf]683 /*
684 * Find the driver which is the most suitable for handling this device.
685 */
[e2b9a993]686 driver_t *drv = find_best_match_driver(drivers_list, node);
[58b833c]687 if (drv == NULL) {
[38b3baf]688 printf(NAME ": no driver found for device '%s'.\n",
689 node->pathname);
690 return false;
[85e48a9]691 }
692
[38b3baf]693 /* Attach the driver to the device. */
[85e48a9]694 attach_driver(node, drv);
695
[58b833c]696 if (drv->state == DRIVER_NOT_STARTED) {
[38b3baf]697 /* Start the driver. */
[85e48a9]698 start_driver(drv);
[38b3baf]699 }
[e85920d]700
[58b833c]701 if (drv->state == DRIVER_RUNNING) {
[38b3baf]702 /* Notify the driver about the new device. */
[084ff99]703 int phone = ipc_connect_me_to(drv->phone, DRIVER_DEVMAN, 0, 0);
704 if (phone > 0) {
[38b3baf]705 add_device(phone, drv, node, tree);
[084ff99]706 ipc_hangup(phone);
707 }
[85e48a9]708 }
709
710 return true;
711}
712
[38b3baf]713/** Initialize the device tree.
714 *
[0c3666d]715 * Create root device node of the tree and assign driver to it.
[38b3baf]716 *
717 * @param tree The device tree.
718 * @param drivers_list the list of available drivers.
719 * @return True on success, false otherwise.
[0c3666d]720 */
721bool init_device_tree(dev_tree_t *tree, driver_list_t *drivers_list)
[85e48a9]722{
[e85920d]723 printf(NAME ": init_device_tree.\n");
[0c3666d]724
[957cfa58]725 tree->current_handle = 0;
726
[38b3baf]727 hash_table_create(&tree->devman_devices, DEVICE_BUCKETS, 1,
728 &devman_devices_ops);
729 hash_table_create(&tree->devmap_devices, DEVICE_BUCKETS, 1,
730 &devmap_devices_ops);
[bda60d9]731
[957cfa58]732 fibril_rwlock_initialize(&tree->rwlock);
[084ff99]733
[38b3baf]734 /* Create root node and add it to the device tree. */
735 if (!create_root_node(tree))
[85e48a9]736 return false;
[e4c4247]737
[38b3baf]738 /* Find suitable driver and start it. */
[a32defa]739 return assign_driver(tree->root_node, drivers_list, tree);
[e4c4247]740}
741
[791f58c]742/* Device nodes */
743
744/** Create a new device node.
745 *
746 * @return A device node structure.
747 */
748node_t *create_dev_node(void)
749{
750 node_t *res = malloc(sizeof(node_t));
751
752 if (res != NULL) {
753 memset(res, 0, sizeof(node_t));
754 list_initialize(&res->children);
755 list_initialize(&res->match_ids.ids);
756 list_initialize(&res->classes);
757 }
758
759 return res;
760}
761
762/** Delete a device node.
763 *
764 * @param node The device node structure.
765 */
766void delete_dev_node(node_t *node)
767{
768 assert(list_empty(&node->children));
769 assert(node->parent == NULL);
770 assert(node->drv == NULL);
771
772 clean_match_ids(&node->match_ids);
773 free_not_null(node->name);
774 free_not_null(node->pathname);
775 free(node);
776}
777
778/** Find the device node structure of the device witch has the specified handle.
779 *
780 * Device tree's rwlock should be held at least for reading.
781 *
782 * @param tree The device tree where we look for the device node.
783 * @param handle The handle of the device.
784 * @return The device node.
785 */
786node_t *find_dev_node_no_lock(dev_tree_t *tree, device_handle_t handle)
787{
788 unsigned long key = handle;
789 link_t *link = hash_table_find(&tree->devman_devices, &key);
790 return hash_table_get_instance(link, node_t, devman_link);
791}
792
793/** Find the device node structure of the device witch has the specified handle.
794 *
795 * @param tree The device tree where we look for the device node.
796 * @param handle The handle of the device.
797 * @return The device node.
798 */
799node_t *find_dev_node(dev_tree_t *tree, device_handle_t handle)
800{
801 node_t *node = NULL;
802
803 fibril_rwlock_read_lock(&tree->rwlock);
804 node = find_dev_node_no_lock(tree, handle);
805 fibril_rwlock_read_unlock(&tree->rwlock);
806
807 return node;
808}
809
810
[bda60d9]811/** Create and set device's full path in device tree.
[38b3baf]812 *
813 * @param node The device's device node.
814 * @param parent The parent device node.
815 * @return True on success, false otherwise (insufficient
816 * resources etc.).
[bda60d9]817 */
818static bool set_dev_path(node_t *node, node_t *parent)
[38b3baf]819{
[58b833c]820 assert(node->name != NULL);
[bda60d9]821
[38b3baf]822 size_t pathsize = (str_size(node->name) + 1);
[58b833c]823 if (parent != NULL)
[38b3baf]824 pathsize += str_size(parent->pathname) + 1;
[bda60d9]825
[38b3baf]826 node->pathname = (char *) malloc(pathsize);
[58b833c]827 if (node->pathname == NULL) {
[bda60d9]828 printf(NAME ": failed to allocate device path.\n");
829 return false;
830 }
831
[58b833c]832 if (parent != NULL) {
[bda60d9]833 str_cpy(node->pathname, pathsize, parent->pathname);
834 str_append(node->pathname, pathsize, "/");
835 str_append(node->pathname, pathsize, node->name);
836 } else {
837 str_cpy(node->pathname, pathsize, node->name);
838 }
839
840 return true;
841}
842
843/** Insert new device into device tree.
[38b3baf]844 *
845 * The device tree's rwlock should be already held exclusively when calling this
846 * function.
847 *
848 * @param tree The device tree.
849 * @param node The newly added device node.
850 * @param dev_name The name of the newly added device.
851 * @param parent The parent device node.
[58b833c]852 *
[38b3baf]853 * @return True on success, false otherwise (insufficient resources
854 * etc.).
[bda60d9]855 */
[58b833c]856bool insert_dev_node(dev_tree_t *tree, node_t *node, char *dev_name,
857 node_t *parent)
[bda60d9]858{
[58b833c]859 assert(node != NULL);
860 assert(tree != NULL);
861 assert(dev_name != NULL);
[bda60d9]862
863 node->name = dev_name;
864 if (!set_dev_path(node, parent)) {
[957cfa58]865 fibril_rwlock_write_unlock(&tree->rwlock);
[38b3baf]866 return false;
[bda60d9]867 }
868
[38b3baf]869 /* Add the node to the handle-to-node map. */
[957cfa58]870 node->handle = ++tree->current_handle;
871 unsigned long key = node->handle;
872 hash_table_insert(&tree->devman_devices, &key, &node->devman_link);
[bda60d9]873
[38b3baf]874 /* Add the node to the list of its parent's children. */
[bda60d9]875 node->parent = parent;
[58b833c]876 if (parent != NULL)
[38b3baf]877 list_append(&node->sibling, &parent->children);
878
[bda60d9]879 return true;
880}
881
[38b3baf]882/** Find device node with a specified path in the device tree.
[5cd136ab]883 *
[38b3baf]884 * @param path The path of the device node in the device tree.
885 * @param tree The device tree.
886 * @return The device node if it is present in the tree, NULL
887 * otherwise.
[5cd136ab]888 */
[38b3baf]889node_t *find_dev_node_by_path(dev_tree_t *tree, char *path)
[5cd136ab]890{
[957cfa58]891 fibril_rwlock_read_lock(&tree->rwlock);
892
[5cd136ab]893 node_t *dev = tree->root_node;
[38b3baf]894 /*
895 * Relative path to the device from its parent (but with '/' at the
896 * beginning)
897 */
[5cd136ab]898 char *rel_path = path;
899 char *next_path_elem = NULL;
[58b833c]900 bool cont = (rel_path[0] == '/');
[5cd136ab]901
[58b833c]902 while (cont && dev != NULL) {
[38b3baf]903 next_path_elem = get_path_elem_end(rel_path + 1);
[58b833c]904 if (next_path_elem[0] == '/') {
[5cd136ab]905 cont = true;
906 next_path_elem[0] = 0;
907 } else {
908 cont = false;
909 }
910
[38b3baf]911 dev = find_node_child(dev, rel_path + 1);
[5cd136ab]912
913 if (cont) {
[38b3baf]914 /* Restore the original path. */
[5cd136ab]915 next_path_elem[0] = '/';
916 }
[38b3baf]917 rel_path = next_path_elem;
[5cd136ab]918 }
919
[957cfa58]920 fibril_rwlock_read_unlock(&tree->rwlock);
921
[5cd136ab]922 return dev;
923}
924
[38b3baf]925/** Find child device node with a specified name.
926 *
927 * Device tree rwlock should be held at least for reading.
928 *
929 * @param parent The parent device node.
930 * @param name The name of the child device node.
931 * @return The child device node.
[5cd136ab]932 */
933node_t *find_node_child(node_t *parent, const char *name)
934{
935 node_t *dev;
936 link_t *link;
[38b3baf]937
[5cd136ab]938 link = parent->children.next;
939
940 while (link != &parent->children) {
941 dev = list_get_instance(link, node_t, sibling);
942
[58b833c]943 if (str_cmp(name, dev->name) == 0)
[38b3baf]944 return dev;
[2480e19]945
946 link = link->next;
[38b3baf]947 }
948
[5cd136ab]949 return NULL;
950}
951
[791f58c]952/* Device classes */
953
954/** Create device class.
955 *
956 * @return Device class.
957 */
958dev_class_t *create_dev_class(void)
959{
960 dev_class_t *cl;
961
962 cl = (dev_class_t *) malloc(sizeof(dev_class_t));
963 if (cl != NULL) {
964 memset(cl, 0, sizeof(dev_class_t));
965 list_initialize(&cl->devices);
966 fibril_mutex_initialize(&cl->mutex);
967 }
968
969 return cl;
970}
971
972/** Create device class info.
973 *
974 * @return Device class info.
975 */
976dev_class_info_t *create_dev_class_info(void)
977{
978 dev_class_info_t *info;
979
980 info = (dev_class_info_t *) malloc(sizeof(dev_class_info_t));
981 if (info != NULL)
982 memset(info, 0, sizeof(dev_class_info_t));
983
984 return info;
985}
986
987size_t get_new_class_dev_idx(dev_class_t *cl)
988{
989 size_t dev_idx;
990
991 fibril_mutex_lock(&cl->mutex);
992 dev_idx = ++cl->curr_dev_idx;
993 fibril_mutex_unlock(&cl->mutex);
994
995 return dev_idx;
996}
997
998
[38b3baf]999/** Create unique device name within the class.
1000 *
1001 * @param cl The class.
1002 * @param base_dev_name Contains the base name for the device if it was
1003 * specified by the driver when it registered the device by
1004 * the class; NULL if driver specified no base name.
1005 * @return The unique name for the device within the class.
[d51ee2b]1006 */
[38b3baf]1007char *create_dev_name_for_class(dev_class_t *cl, const char *base_dev_name)
[d51ee2b]1008{
1009 char *dev_name;
1010 const char *base_name;
[38b3baf]1011
[58b833c]1012 if (base_dev_name != NULL)
[d51ee2b]1013 base_name = base_dev_name;
[38b3baf]1014 else
[d51ee2b]1015 base_name = cl->base_dev_name;
1016
1017 size_t idx = get_new_class_dev_idx(cl);
1018 asprintf(&dev_name, "%s%d", base_name, idx);
[38b3baf]1019
1020 return dev_name;
[d51ee2b]1021}
1022
1023/** Add the device to the class.
[38b3baf]1024 *
1025 * The device may be added to multiple classes and a class may contain multiple
1026 * devices. The class and the device are associated with each other by the
1027 * dev_class_info_t structure.
1028 *
1029 * @param dev The device.
1030 * @param class The class.
1031 * @param base_dev_name The base name of the device within the class if
1032 * specified by the driver, NULL otherwise.
1033 * @return dev_class_info_t structure which associates the device
1034 * with the class.
[d51ee2b]1035 */
[58b833c]1036dev_class_info_t *add_device_to_class(node_t *dev, dev_class_t *cl,
1037 const char *base_dev_name)
[38b3baf]1038{
[d51ee2b]1039 dev_class_info_t *info = create_dev_class_info();
[38b3baf]1040
[58b833c]1041 if (info != NULL) {
[692c40cb]1042 info->dev_class = cl;
1043 info->dev = dev;
1044
[38b3baf]1045 /* Add the device to the class. */
[692c40cb]1046 fibril_mutex_lock(&cl->mutex);
1047 list_append(&info->link, &cl->devices);
1048 fibril_mutex_unlock(&cl->mutex);
1049
[38b3baf]1050 /* Add the class to the device. */
[692c40cb]1051 list_append(&info->dev_classes, &dev->classes);
1052
[38b3baf]1053 /* Create unique name for the device within the class. */
1054 info->dev_name = create_dev_name_for_class(cl, base_dev_name);
[692c40cb]1055 }
[d51ee2b]1056
1057 return info;
1058}
1059
[38b3baf]1060dev_class_t *get_dev_class(class_list_t *class_list, char *class_name)
[692c40cb]1061{
1062 dev_class_t *cl;
[38b3baf]1063
1064 fibril_rwlock_write_lock(&class_list->rwlock);
[692c40cb]1065 cl = find_dev_class_no_lock(class_list, class_name);
[58b833c]1066 if (cl == NULL) {
[692c40cb]1067 cl = create_dev_class();
[58b833c]1068 if (cl != NULL) {
[38b3baf]1069 cl->name = class_name;
[692c40cb]1070 cl->base_dev_name = "";
1071 add_dev_class_no_lock(class_list, cl);
[38b3baf]1072 }
1073 }
[58b833c]1074
[ce89036b]1075 fibril_rwlock_write_unlock(&class_list->rwlock);
[692c40cb]1076 return cl;
1077}
1078
[58b833c]1079dev_class_t *find_dev_class_no_lock(class_list_t *class_list,
1080 const char *class_name)
[692c40cb]1081{
1082 dev_class_t *cl;
1083 link_t *link = class_list->classes.next;
[38b3baf]1084
[692c40cb]1085 while (link != &class_list->classes) {
1086 cl = list_get_instance(link, dev_class_t, link);
[58b833c]1087 if (str_cmp(cl->name, class_name) == 0)
[692c40cb]1088 return cl;
1089 }
1090
[38b3baf]1091 return NULL;
[692c40cb]1092}
1093
[791f58c]1094void add_dev_class_no_lock(class_list_t *class_list, dev_class_t *cl)
1095{
1096 list_append(&cl->link, &class_list->classes);
1097}
1098
[ce89036b]1099void init_class_list(class_list_t *class_list)
1100{
1101 list_initialize(&class_list->classes);
1102 fibril_rwlock_initialize(&class_list->rwlock);
[38b3baf]1103 hash_table_create(&class_list->devmap_devices, DEVICE_BUCKETS, 1,
1104 &devmap_devices_ops);
[ce89036b]1105}
1106
1107
[791f58c]1108/* Devmap devices */
[ce89036b]1109
1110node_t *find_devmap_tree_device(dev_tree_t *tree, dev_handle_t devmap_handle)
1111{
1112 node_t *dev = NULL;
1113 link_t *link;
[38b3baf]1114 unsigned long key = (unsigned long) devmap_handle;
[ce89036b]1115
1116 fibril_rwlock_read_lock(&tree->rwlock);
[38b3baf]1117 link = hash_table_find(&tree->devmap_devices, &key);
[58b833c]1118 if (link != NULL)
[ce89036b]1119 dev = hash_table_get_instance(link, node_t, devmap_link);
1120 fibril_rwlock_read_unlock(&tree->rwlock);
1121
1122 return dev;
1123}
1124
[58b833c]1125node_t *find_devmap_class_device(class_list_t *classes,
1126 dev_handle_t devmap_handle)
[ce89036b]1127{
1128 node_t *dev = NULL;
1129 dev_class_info_t *cli;
1130 link_t *link;
1131 unsigned long key = (unsigned long)devmap_handle;
1132
1133 fibril_rwlock_read_lock(&classes->rwlock);
[38b3baf]1134 link = hash_table_find(&classes->devmap_devices, &key);
[58b833c]1135 if (link != NULL) {
[38b3baf]1136 cli = hash_table_get_instance(link, dev_class_info_t,
1137 devmap_link);
[ce89036b]1138 dev = cli->dev;
1139 }
1140 fibril_rwlock_read_unlock(&classes->rwlock);
1141
[38b3baf]1142 return dev;
[ce89036b]1143}
1144
[791f58c]1145void class_add_devmap_device(class_list_t *class_list, dev_class_info_t *cli)
1146{
1147 unsigned long key = (unsigned long) cli->devmap_handle;
1148
1149 fibril_rwlock_write_lock(&class_list->rwlock);
1150 hash_table_insert(&class_list->devmap_devices, &key, &cli->devmap_link);
1151 fibril_rwlock_write_unlock(&class_list->rwlock);
1152}
1153
1154void tree_add_devmap_device(dev_tree_t *tree, node_t *node)
1155{
1156 unsigned long key = (unsigned long) node->devmap_handle;
1157 fibril_rwlock_write_lock(&tree->rwlock);
1158 hash_table_insert(&tree->devmap_devices, &key, &node->devmap_link);
1159 fibril_rwlock_write_unlock(&tree->rwlock);
1160}
1161
[c16cf62]1162/** @}
[58b833c]1163 */
Note: See TracBrowser for help on using the repository browser.