source: mainline/uspace/srv/devman/devman.c@ 58b833c

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

More cstyle in devman.

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