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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since e186eb5 was fa581b3, checked in by Jakub Jermar <jakub@…>, 14 years ago

Fix range of valid phone numbers.

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