Changeset 8b655705 in mainline for uspace/srv/devman/devman.c


Ignore:
Timestamp:
2011-04-15T19:38:07Z (13 years ago)
Author:
Jakub Jermar <jakub@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
9dd730d1
Parents:
6b9e85b (diff), b2fb47f (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge mainline changes.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/srv/devman/devman.c

    r6b9e85b r8b655705  
    3434#include <fcntl.h>
    3535#include <sys/stat.h>
     36#include <io/log.h>
    3637#include <ipc/driver.h>
    3738#include <ipc/devman.h>
     
    4142#include "devman.h"
    4243
     44fun_node_t *find_node_child(fun_node_t *parent, const char *name);
     45
    4346/* hash table operations */
    4447
     
    5154    link_t *item)
    5255{
    53         node_t *dev = hash_table_get_instance(item, node_t, devman_link);
     56        dev_node_t *dev = hash_table_get_instance(item, dev_node_t, devman_dev);
    5457        return (dev->handle == (devman_handle_t) key[0]);
    5558}
    5659
    57 static int devmap_devices_compare(unsigned long key[], hash_count_t keys,
     60static int devman_functions_compare(unsigned long key[], hash_count_t keys,
    5861    link_t *item)
    5962{
    60         node_t *dev = hash_table_get_instance(item, node_t, devmap_link);
    61         return (dev->devmap_handle == (devmap_handle_t) key[0]);
     63        fun_node_t *fun = hash_table_get_instance(item, fun_node_t, devman_fun);
     64        return (fun->handle == (devman_handle_t) key[0]);
     65}
     66
     67static int devmap_functions_compare(unsigned long key[], hash_count_t keys,
     68    link_t *item)
     69{
     70        fun_node_t *fun = hash_table_get_instance(item, fun_node_t, devmap_fun);
     71        return (fun->devmap_handle == (devmap_handle_t) key[0]);
    6272}
    6373
     
    8292};
    8393
     94static hash_table_operations_t devman_functions_ops = {
     95        .hash = devices_hash,
     96        .compare = devman_functions_compare,
     97        .remove_callback = devices_remove_callback
     98};
     99
    84100static hash_table_operations_t devmap_devices_ops = {
    85101        .hash = devices_hash,
    86         .compare = devmap_devices_compare,
     102        .compare = devmap_functions_compare,
    87103        .remove_callback = devices_remove_callback
    88104};
     
    131147        fibril_mutex_unlock(&drivers_list->drivers_mutex);
    132148
    133         printf(NAME": the '%s' driver was added to the list of available "
    134             "drivers.\n", drv->name);
     149        log_msg(LVL_NOTE, "Driver `%s' was added to the list of available "
     150            "drivers.", drv->name);
    135151}
    136152
     
    222238bool read_match_ids(const char *conf_path, match_id_list_t *ids)
    223239{
    224         printf(NAME ": read_match_ids conf_path = %s.\n", conf_path);
     240        log_msg(LVL_DEBUG, "read_match_ids(conf_path=\"%s\")", conf_path);
    225241       
    226242        bool suc = false;
     
    232248        fd = open(conf_path, O_RDONLY);
    233249        if (fd < 0) {
    234                 printf(NAME ": unable to open %s\n", conf_path);
     250                log_msg(LVL_ERROR, "Unable to open `%s' for reading: %s.",
     251                    conf_path, str_error(fd));
    235252                goto cleanup;
    236253        }
     
    240257        lseek(fd, 0, SEEK_SET);
    241258        if (len == 0) {
    242                 printf(NAME ": configuration file '%s' is empty.\n", conf_path);
     259                log_msg(LVL_ERROR, "Configuration file '%s' is empty.",
     260                    conf_path);
    243261                goto cleanup;
    244262        }
     
    246264        buf = malloc(len + 1);
    247265        if (buf == NULL) {
    248                 printf(NAME ": memory allocation failed when parsing file "
    249                     "'%s'.\n", conf_path);
     266                log_msg(LVL_ERROR, "Memory allocation failed when parsing file "
     267                    "'%s'.", conf_path);
    250268                goto cleanup;
    251269        }
    252270       
    253         if (read(fd, buf, len) <= 0) {
    254                 printf(NAME ": unable to read file '%s'.\n", conf_path);
     271        ssize_t read_bytes = safe_read(fd, buf, len);
     272        if (read_bytes <= 0) {
     273                log_msg(LVL_ERROR, "Unable to read file '%s'.", conf_path);
    255274                goto cleanup;
    256275        }
    257         buf[len] = 0;
     276        buf[read_bytes] = 0;
    258277       
    259278        suc = parse_match_ids(buf, ids);
     
    290309bool get_driver_info(const char *base_path, const char *name, driver_t *drv)
    291310{
    292         printf(NAME ": get_driver_info base_path = %s, name = %s.\n",
     311        log_msg(LVL_DEBUG, "get_driver_info(base_path=\"%s\", name=\"%s\")",
    293312            base_path, name);
    294313       
     
    322341        struct stat s;
    323342        if (stat(drv->binary_path, &s) == ENOENT) { /* FIXME!! */
    324                 printf(NAME ": driver not found at path %s.", drv->binary_path);
     343                log_msg(LVL_ERROR, "Driver not found at path `%s'.",
     344                    drv->binary_path);
    325345                goto cleanup;
    326346        }
     
    349369int lookup_available_drivers(driver_list_t *drivers_list, const char *dir_path)
    350370{
    351         printf(NAME ": lookup_available_drivers, dir = %s \n", dir_path);
     371        log_msg(LVL_DEBUG, "lookup_available_drivers(dir=\"%s\")", dir_path);
    352372       
    353373        int drv_cnt = 0;
     
    373393}
    374394
    375 /** Create root device node in the device tree.
     395/** Create root device and function node in the device tree.
    376396 *
    377397 * @param tree  The device tree.
    378398 * @return      True on success, false otherwise.
    379399 */
    380 bool create_root_node(dev_tree_t *tree)
    381 {
    382         node_t *node;
    383 
    384         printf(NAME ": create_root_node\n");
    385 
     400bool create_root_nodes(dev_tree_t *tree)
     401{
     402        fun_node_t *fun;
     403        dev_node_t *dev;
     404       
     405        log_msg(LVL_DEBUG, "create_root_nodes()");
     406       
    386407        fibril_rwlock_write_lock(&tree->rwlock);
    387         node = create_dev_node();
    388         if (node != NULL) {
    389                 insert_dev_node(tree, node, clone_string(""), NULL);
    390                 match_id_t *id = create_match_id();
    391                 id->id = clone_string("root");
    392                 id->score = 100;
    393                 add_match_id(&node->match_ids, id);
    394                 tree->root_node = node;
    395         }
     408       
     409        /*
     410         * Create root function. This is a pseudo function to which
     411         * the root device node is attached. It allows us to match
     412         * the root device driver in a standard manner, i.e. against
     413         * the parent function.
     414         */
     415       
     416        fun = create_fun_node();
     417        if (fun == NULL) {
     418                fibril_rwlock_write_unlock(&tree->rwlock);
     419                return false;
     420        }
     421       
     422        insert_fun_node(tree, fun, clone_string(""), NULL);
     423        match_id_t *id = create_match_id();
     424        id->id = clone_string("root");
     425        id->score = 100;
     426        add_match_id(&fun->match_ids, id);
     427        tree->root_node = fun;
     428       
     429        /*
     430         * Create root device node.
     431         */
     432        dev = create_dev_node();
     433        if (dev == NULL) {
     434                fibril_rwlock_write_unlock(&tree->rwlock);
     435                return false;
     436        }
     437       
     438        insert_dev_node(tree, dev, fun);
     439       
    396440        fibril_rwlock_write_unlock(&tree->rwlock);
    397 
    398         return node != NULL;
     441       
     442        return dev != NULL;
    399443}
    400444
     
    414458 *                      is found.
    415459 */
    416 driver_t *find_best_match_driver(driver_list_t *drivers_list, node_t *node)
     460driver_t *find_best_match_driver(driver_list_t *drivers_list, dev_node_t *node)
    417461{
    418462        driver_t *best_drv = NULL, *drv = NULL;
     
    442486 * @param drv           The driver.
    443487 */
    444 void attach_driver(node_t *node, driver_t *drv)
    445 {
    446         printf(NAME ": attach_driver %s to device %s\n",
    447             drv->name, node->pathname);
     488void attach_driver(dev_node_t *dev, driver_t *drv)
     489{
     490        log_msg(LVL_DEBUG, "attach_driver(dev=\"%s\",drv=\"%s\")",
     491            dev->pfun->pathname, drv->name);
    448492       
    449493        fibril_mutex_lock(&drv->driver_mutex);
    450494       
    451         node->drv = drv;
    452         list_append(&node->driver_devices, &drv->devices);
     495        dev->drv = drv;
     496        list_append(&dev->driver_devices, &drv->devices);
    453497       
    454498        fibril_mutex_unlock(&drv->driver_mutex);
     
    467511        assert(fibril_mutex_is_locked(&drv->driver_mutex));
    468512       
    469         printf(NAME ": start_driver '%s'\n", drv->name);
     513        log_msg(LVL_DEBUG, "start_driver(drv=\"%s\")", drv->name);
    470514       
    471515        rc = task_spawnl(NULL, drv->binary_path, drv->binary_path, NULL);
    472516        if (rc != EOK) {
    473                 printf(NAME ": error spawning %s (%s)\n",
    474                     drv->name, str_error(rc));
     517                log_msg(LVL_ERROR, "Spawning driver `%s' (%s) failed: %s.",
     518                    drv->name, drv->binary_path, str_error(rc));
    475519                return false;
    476520        }
     
    511555}
    512556
    513 /** Remember the driver's phone.
    514  *
    515  * @param driver        The driver.
    516  * @param phone         The phone to the driver.
    517  */
    518 void set_driver_phone(driver_t *driver, sysarg_t phone)
    519 {
    520         fibril_mutex_lock(&driver->driver_mutex);
    521         assert(driver->state == DRIVER_STARTING);
    522         driver->phone = phone;
    523         fibril_mutex_unlock(&driver->driver_mutex);
    524 }
    525 
    526557/** Notify driver about the devices to which it was assigned.
    527558 *
     
    530561static void pass_devices_to_driver(driver_t *driver, dev_tree_t *tree)
    531562{
    532         node_t *dev;
     563        dev_node_t *dev;
    533564        link_t *link;
    534565        int phone;
    535566
    536         printf(NAME ": pass_devices_to_driver(`%s')\n", driver->name);
     567        log_msg(LVL_DEBUG, "pass_devices_to_driver(driver=\"%s\")",
     568            driver->name);
    537569
    538570        fibril_mutex_lock(&driver->driver_mutex);
     
    551583        link = driver->devices.next;
    552584        while (link != &driver->devices) {
    553                 dev = list_get_instance(link, node_t, driver_devices);
     585                dev = list_get_instance(link, dev_node_t, driver_devices);
    554586                if (dev->passed_to_driver) {
    555587                        link = link->next;
     
    590622        }
    591623
    592         ipc_hangup(phone);
     624        async_hangup(phone);
    593625
    594626        /*
     
    601633         * immediately and possibly started here as well.
    602634         */
    603         printf(NAME ": driver %s goes into running state.\n", driver->name);
     635        log_msg(LVL_DEBUG, "Driver `%s' enters running state.", driver->name);
    604636        driver->state = DRIVER_RUNNING;
    605637
     
    618650void initialize_running_driver(driver_t *driver, dev_tree_t *tree)
    619651{
    620         printf(NAME ": initialize_running_driver (`%s')\n", driver->name);
     652        log_msg(LVL_DEBUG, "initialize_running_driver(driver=\"%s\")",
     653            driver->name);
    621654       
    622655        /*
     
    639672        list_initialize(&drv->devices);
    640673        fibril_mutex_initialize(&drv->driver_mutex);
     674        drv->phone = -1;
    641675}
    642676
     
    669703}
    670704
    671 /** Create devmap path and name for the device. */
    672 static void devmap_register_tree_device(node_t *node, dev_tree_t *tree)
     705/** Create devmap path and name for the function. */
     706void devmap_register_tree_function(fun_node_t *fun, dev_tree_t *tree)
    673707{
    674708        char *devmap_pathname = NULL;
    675709        char *devmap_name = NULL;
    676710       
    677         asprintf(&devmap_name, "%s", node->pathname);
     711        asprintf(&devmap_name, "%s", fun->pathname);
    678712        if (devmap_name == NULL)
    679713                return;
     
    689723       
    690724        devmap_device_register_with_iface(devmap_pathname,
    691             &node->devmap_handle, DEVMAN_CONNECT_FROM_DEVMAP);
    692        
    693         tree_add_devmap_device(tree, node);
     725            &fun->devmap_handle, DEVMAN_CONNECT_FROM_DEVMAP);
     726       
     727        tree_add_devmap_function(tree, fun);
    694728       
    695729        free(devmap_name);
     
    702736 * @param node          The device's node in the device tree.
    703737 */
    704 void add_device(int phone, driver_t *drv, node_t *node, dev_tree_t *tree)
     738void add_device(int phone, driver_t *drv, dev_node_t *dev, dev_tree_t *tree)
    705739{
    706740        /*
     
    708742         * access any structures that would affect driver_t.
    709743         */
    710         printf(NAME ": add_device (driver `%s', device `%s')\n", drv->name,
    711             node->name);
     744        log_msg(LVL_DEBUG, "add_device(drv=\"%s\", dev=\"%s\")",
     745            drv->name, dev->pfun->name);
    712746       
    713747        sysarg_t rc;
     
    716750        /* Send the device to the driver. */
    717751        devman_handle_t parent_handle;
    718         if (node->parent) {
    719                 parent_handle = node->parent->handle;
     752        if (dev->pfun) {
     753                parent_handle = dev->pfun->handle;
    720754        } else {
    721755                parent_handle = 0;
    722756        }
    723757
    724         aid_t req = async_send_2(phone, DRIVER_ADD_DEVICE, node->handle,
     758        aid_t req = async_send_2(phone, DRIVER_ADD_DEVICE, dev->handle,
    725759            parent_handle, &answer);
    726760       
    727761        /* Send the device's name to the driver. */
    728         rc = async_data_write_start(phone, node->name,
    729             str_size(node->name) + 1);
     762        rc = async_data_write_start(phone, dev->pfun->name,
     763            str_size(dev->pfun->name) + 1);
    730764        if (rc != EOK) {
    731765                /* TODO handle error */
     
    737771        switch(rc) {
    738772        case EOK:
    739                 node->state = DEVICE_USABLE;
    740                 devmap_register_tree_device(node, tree);
     773                dev->state = DEVICE_USABLE;
    741774                break;
    742775        case ENOENT:
    743                 node->state = DEVICE_NOT_PRESENT;
     776                dev->state = DEVICE_NOT_PRESENT;
    744777                break;
    745778        default:
    746                 node->state = DEVICE_INVALID;
    747         }
    748        
    749         node->passed_to_driver = true;
     779                dev->state = DEVICE_INVALID;
     780        }
     781       
     782        dev->passed_to_driver = true;
    750783
    751784        return;
     
    759792 *                      successfully assigned to the device, false otherwise.
    760793 */
    761 bool assign_driver(node_t *node, driver_list_t *drivers_list, dev_tree_t *tree)
    762 {
     794bool assign_driver(dev_node_t *dev, driver_list_t *drivers_list,
     795    dev_tree_t *tree)
     796{
     797        assert(dev != NULL);
     798        assert(drivers_list != NULL);
     799        assert(tree != NULL);
     800       
    763801        /*
    764802         * Find the driver which is the most suitable for handling this device.
    765803         */
    766         driver_t *drv = find_best_match_driver(drivers_list, node);
     804        driver_t *drv = find_best_match_driver(drivers_list, dev);
    767805        if (drv == NULL) {
    768                 printf(NAME ": no driver found for device '%s'.\n",
    769                     node->pathname);
     806                log_msg(LVL_ERROR, "No driver found for device `%s'.",
     807                    dev->pfun->pathname);
    770808                return false;
    771809        }
    772810       
    773811        /* Attach the driver to the device. */
    774         attach_driver(node, drv);
     812        attach_driver(dev, drv);
    775813       
    776814        fibril_mutex_lock(&drv->driver_mutex);
     
    786824                int phone = async_connect_me_to(drv->phone, DRIVER_DEVMAN, 0, 0);
    787825                if (phone >= 0) {
    788                         add_device(phone, drv, node, tree);
    789                         ipc_hangup(phone);
     826                        add_device(phone, drv, dev, tree);
     827                        async_hangup(phone);
    790828                }
    791829        }
     
    804842bool init_device_tree(dev_tree_t *tree, driver_list_t *drivers_list)
    805843{
    806         printf(NAME ": init_device_tree.\n");
     844        log_msg(LVL_DEBUG, "init_device_tree()");
    807845       
    808846        tree->current_handle = 0;
     
    810848        hash_table_create(&tree->devman_devices, DEVICE_BUCKETS, 1,
    811849            &devman_devices_ops);
    812         hash_table_create(&tree->devmap_devices, DEVICE_BUCKETS, 1,
     850        hash_table_create(&tree->devman_functions, DEVICE_BUCKETS, 1,
     851            &devman_functions_ops);
     852        hash_table_create(&tree->devmap_functions, DEVICE_BUCKETS, 1,
    813853            &devmap_devices_ops);
    814854       
    815855        fibril_rwlock_initialize(&tree->rwlock);
    816856       
    817         /* Create root node and add it to the device tree. */
    818         if (!create_root_node(tree))
     857        /* Create root function and root device and add them to the device tree. */
     858        if (!create_root_nodes(tree))
    819859                return false;
    820860
    821861        /* Find suitable driver and start it. */
    822         return assign_driver(tree->root_node, drivers_list, tree);
     862        return assign_driver(tree->root_node->child, drivers_list, tree);
    823863}
    824864
     
    829869 * @return              A device node structure.
    830870 */
    831 node_t *create_dev_node(void)
    832 {
    833         node_t *res = malloc(sizeof(node_t));
     871dev_node_t *create_dev_node(void)
     872{
     873        dev_node_t *res = malloc(sizeof(dev_node_t));
    834874       
    835875        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);
     876                memset(res, 0, sizeof(dev_node_t));
     877                list_initialize(&res->functions);
     878                link_initialize(&res->driver_devices);
     879                link_initialize(&res->devman_dev);
    840880        }
    841881       
     
    847887 * @param node          The device node structure.
    848888 */
    849 void 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);
     889void delete_dev_node(dev_node_t *dev)
     890{
     891        assert(list_empty(&dev->functions));
     892        assert(dev->pfun == NULL);
     893        assert(dev->drv == NULL);
     894       
     895        free(dev);
    859896}
    860897
     
    865902 * @return              The device node.
    866903 */
    867 node_t *find_dev_node_no_lock(dev_tree_t *tree, devman_handle_t handle)
     904dev_node_t *find_dev_node_no_lock(dev_tree_t *tree, devman_handle_t handle)
    868905{
    869906        unsigned long key = handle;
     
    873910       
    874911        link = hash_table_find(&tree->devman_devices, &key);
    875         return hash_table_get_instance(link, node_t, devman_link);
     912        return hash_table_get_instance(link, dev_node_t, devman_dev);
    876913}
    877914
     
    882919 * @return              The device node.
    883920 */
    884 node_t *find_dev_node(dev_tree_t *tree, devman_handle_t handle)
    885 {
    886         node_t *node = NULL;
     921dev_node_t *find_dev_node(dev_tree_t *tree, devman_handle_t handle)
     922{
     923        dev_node_t *dev = NULL;
    887924       
    888925        fibril_rwlock_read_lock(&tree->rwlock);
    889         node = find_dev_node_no_lock(tree, handle);
     926        dev = find_dev_node_no_lock(tree, handle);
    890927        fibril_rwlock_read_unlock(&tree->rwlock);
    891928       
    892         return node;
    893 }
    894 
     929        return dev;
     930}
     931
     932/* Function nodes */
     933
     934/** Create a new function node.
     935 *
     936 * @return              A function node structure.
     937 */
     938fun_node_t *create_fun_node(void)
     939{
     940        fun_node_t *res = malloc(sizeof(fun_node_t));
     941       
     942        if (res != NULL) {
     943                memset(res, 0, sizeof(fun_node_t));
     944                link_initialize(&res->dev_functions);
     945                list_initialize(&res->match_ids.ids);
     946                list_initialize(&res->classes);
     947                link_initialize(&res->devman_fun);
     948                link_initialize(&res->devmap_fun);
     949        }
     950       
     951        return res;
     952}
     953
     954/** Delete a function node.
     955 *
     956 * @param fun           The device node structure.
     957 */
     958void delete_fun_node(fun_node_t *fun)
     959{
     960        assert(fun->dev == NULL);
     961        assert(fun->child == NULL);
     962       
     963        clean_match_ids(&fun->match_ids);
     964        free_not_null(fun->name);
     965        free_not_null(fun->pathname);
     966        free(fun);
     967}
     968
     969/** Find the function node with the specified handle.
     970 *
     971 * @param tree          The device tree where we look for the device node.
     972 * @param handle        The handle of the function.
     973 * @return              The function node.
     974 */
     975fun_node_t *find_fun_node_no_lock(dev_tree_t *tree, devman_handle_t handle)
     976{
     977        unsigned long key = handle;
     978        link_t *link;
     979       
     980        assert(fibril_rwlock_is_locked(&tree->rwlock));
     981       
     982        link = hash_table_find(&tree->devman_functions, &key);
     983        if (link == NULL)
     984                return NULL;
     985       
     986        return hash_table_get_instance(link, fun_node_t, devman_fun);
     987}
     988
     989/** Find the function node with the specified handle.
     990 *
     991 * @param tree          The device tree where we look for the device node.
     992 * @param handle        The handle of the function.
     993 * @return              The function node.
     994 */
     995fun_node_t *find_fun_node(dev_tree_t *tree, devman_handle_t handle)
     996{
     997        fun_node_t *fun = NULL;
     998       
     999        fibril_rwlock_read_lock(&tree->rwlock);
     1000        fun = find_fun_node_no_lock(tree, handle);
     1001        fibril_rwlock_read_unlock(&tree->rwlock);
     1002       
     1003        return fun;
     1004}
    8951005
    8961006/** Create and set device's full path in device tree.
     
    9011011 *                      resources etc.).
    9021012 */
    903 static bool set_dev_path(node_t *node, node_t *parent)
    904 {
    905         assert(node->name != NULL);
    906        
    907         size_t pathsize = (str_size(node->name) + 1);
     1013static bool set_fun_path(fun_node_t *fun, fun_node_t *parent)
     1014{
     1015        assert(fun->name != NULL);
     1016       
     1017        size_t pathsize = (str_size(fun->name) + 1);
    9081018        if (parent != NULL)
    9091019                pathsize += str_size(parent->pathname) + 1;
    9101020       
    911         node->pathname = (char *) malloc(pathsize);
    912         if (node->pathname == NULL) {
    913                 printf(NAME ": failed to allocate device path.\n");
     1021        fun->pathname = (char *) malloc(pathsize);
     1022        if (fun->pathname == NULL) {
     1023                log_msg(LVL_ERROR, "Failed to allocate device path.");
    9141024                return false;
    9151025        }
    9161026       
    9171027        if (parent != NULL) {
    918                 str_cpy(node->pathname, pathsize, parent->pathname);
    919                 str_append(node->pathname, pathsize, "/");
    920                 str_append(node->pathname, pathsize, node->name);
     1028                str_cpy(fun->pathname, pathsize, parent->pathname);
     1029                str_append(fun->pathname, pathsize, "/");
     1030                str_append(fun->pathname, pathsize, fun->name);
    9211031        } else {
    922                 str_cpy(node->pathname, pathsize, node->name);
     1032                str_cpy(fun->pathname, pathsize, fun->name);
    9231033        }
    9241034       
     
    9361046 *                      etc.).
    9371047 */
    938 bool insert_dev_node(dev_tree_t *tree, node_t *node, char *dev_name,
    939     node_t *parent)
    940 {
    941         assert(node != NULL);
     1048bool insert_dev_node(dev_tree_t *tree, dev_node_t *dev, fun_node_t *pfun)
     1049{
     1050        assert(dev != NULL);
    9421051        assert(tree != NULL);
    943         assert(dev_name != NULL);
    9441052        assert(fibril_rwlock_is_write_locked(&tree->rwlock));
    9451053       
    946         node->name = dev_name;
    947         if (!set_dev_path(node, parent)) {
     1054        log_msg(LVL_DEBUG, "insert_dev_node(dev=%p, pfun=%p [\"%s\"])",
     1055            dev, pfun, pfun->pathname);
     1056
     1057        /* Add the node to the handle-to-node map. */
     1058        dev->handle = ++tree->current_handle;
     1059        unsigned long key = dev->handle;
     1060        hash_table_insert(&tree->devman_devices, &key, &dev->devman_dev);
     1061
     1062        /* Add the node to the list of its parent's children. */
     1063        dev->pfun = pfun;
     1064        pfun->child = dev;
     1065       
     1066        return true;
     1067}
     1068
     1069/** Insert new function into device tree.
     1070 *
     1071 * @param tree          The device tree.
     1072 * @param node          The newly added function node.
     1073 * @param dev_name      The name of the newly added function.
     1074 * @param parent        Owning device node.
     1075 *
     1076 * @return              True on success, false otherwise (insufficient resources
     1077 *                      etc.).
     1078 */
     1079bool insert_fun_node(dev_tree_t *tree, fun_node_t *fun, char *fun_name,
     1080    dev_node_t *dev)
     1081{
     1082        fun_node_t *pfun;
     1083       
     1084        assert(fun != NULL);
     1085        assert(tree != NULL);
     1086        assert(fun_name != NULL);
     1087        assert(fibril_rwlock_is_write_locked(&tree->rwlock));
     1088       
     1089        /*
     1090         * The root function is a special case, it does not belong to any
     1091         * device so for the root function dev == NULL.
     1092         */
     1093        pfun = (dev != NULL) ? dev->pfun : NULL;
     1094       
     1095        fun->name = fun_name;
     1096        if (!set_fun_path(fun, pfun)) {
    9481097                return false;
    9491098        }
    9501099       
    9511100        /* Add the node to the handle-to-node map. */
    952         node->handle = ++tree->current_handle;
    953         unsigned long key = node->handle;
    954         hash_table_insert(&tree->devman_devices, &key, &node->devman_link);
     1101        fun->handle = ++tree->current_handle;
     1102        unsigned long key = fun->handle;
     1103        hash_table_insert(&tree->devman_functions, &key, &fun->devman_fun);
    9551104
    9561105        /* Add the node to the list of its parent's children. */
    957         node->parent = parent;
    958         if (parent != NULL)
    959                 list_append(&node->sibling, &parent->children);
     1106        fun->dev = dev;
     1107        if (dev != NULL)
     1108                list_append(&fun->dev_functions, &dev->functions);
    9601109       
    9611110        return true;
    9621111}
    9631112
    964 /** Find device node with a specified path in the device tree.
     1113/** Find function node with a specified path in the device tree.
    9651114 *
    966  * @param path          The path of the device node in the device tree.
     1115 * @param path          The path of the function node in the device tree.
    9671116 * @param tree          The device tree.
    968  * @return              The device node if it is present in the tree, NULL
     1117 * @return              The function node if it is present in the tree, NULL
    9691118 *                      otherwise.
    9701119 */
    971 node_t *find_dev_node_by_path(dev_tree_t *tree, char *path)
    972 {
     1120fun_node_t *find_fun_node_by_path(dev_tree_t *tree, char *path)
     1121{
     1122        assert(path != NULL);
     1123
     1124        bool is_absolute = path[0] == '/';
     1125        if (!is_absolute) {
     1126                return NULL;
     1127        }
     1128
    9731129        fibril_rwlock_read_lock(&tree->rwlock);
    9741130       
    975         node_t *dev = tree->root_node;
     1131        fun_node_t *fun = tree->root_node;
    9761132        /*
    977          * Relative path to the device from its parent (but with '/' at the
     1133         * Relative path to the function from its parent (but with '/' at the
    9781134         * beginning)
    9791135         */
    9801136        char *rel_path = path;
    9811137        char *next_path_elem = NULL;
    982         bool cont = (rel_path[0] == '/');
    983        
    984         while (cont && dev != NULL) {
     1138        bool cont = true;
     1139       
     1140        while (cont && fun != NULL) {
    9851141                next_path_elem  = get_path_elem_end(rel_path + 1);
    9861142                if (next_path_elem[0] == '/') {
     
    9911147                }
    9921148               
    993                 dev = find_node_child(dev, rel_path + 1);
     1149                fun = find_node_child(fun, rel_path + 1);
    9941150               
    9951151                if (cont) {
     
    10021158        fibril_rwlock_read_unlock(&tree->rwlock);
    10031159       
    1004         return dev;
    1005 }
    1006 
    1007 /** Find child device node with a specified name.
     1160        return fun;
     1161}
     1162
     1163/** Find function with a specified name belonging to given device.
    10081164 *
    10091165 * Device tree rwlock should be held at least for reading.
    10101166 *
    1011  * @param parent        The parent device node.
    1012  * @param name          The name of the child device node.
    1013  * @return              The child device node.
    1014  */
    1015 node_t *find_node_child(node_t *parent, const char *name)
    1016 {
    1017         node_t *dev;
     1167 * @param dev Device the function belongs to.
     1168 * @param name Function name (not path).
     1169 * @return Function node.
     1170 * @retval NULL No function with given name.
     1171 */
     1172fun_node_t *find_fun_node_in_device(dev_node_t *dev, const char *name)
     1173{
     1174        assert(dev != NULL);
     1175        assert(name != NULL);
     1176
     1177        fun_node_t *fun;
    10181178        link_t *link;
    1019        
    1020         link = parent->children.next;
    1021        
    1022         while (link != &parent->children) {
    1023                 dev = list_get_instance(link, node_t, sibling);
    1024                
    1025                 if (str_cmp(name, dev->name) == 0)
    1026                         return dev;
    1027                
    1028                 link = link->next;
    1029         }
    1030        
     1179
     1180        for (link = dev->functions.next;
     1181            link != &dev->functions;
     1182            link = link->next) {
     1183                fun = list_get_instance(link, fun_node_t, dev_functions);
     1184
     1185                if (str_cmp(name, fun->name) == 0)
     1186                        return fun;
     1187        }
     1188
    10311189        return NULL;
     1190}
     1191
     1192/** Find function node by its class name and index. */
     1193fun_node_t *find_fun_node_by_class(class_list_t *class_list,
     1194    const char *class_name, const char *dev_name)
     1195{
     1196        assert(class_list != NULL);
     1197        assert(class_name != NULL);
     1198        assert(dev_name != NULL);
     1199
     1200        fibril_rwlock_read_lock(&class_list->rwlock);
     1201
     1202        dev_class_t *cl = find_dev_class_no_lock(class_list, class_name);
     1203        if (cl == NULL) {
     1204                fibril_rwlock_read_unlock(&class_list->rwlock);
     1205                return NULL;
     1206        }
     1207
     1208        dev_class_info_t *dev = find_dev_in_class(cl, dev_name);
     1209        if (dev == NULL) {
     1210                fibril_rwlock_read_unlock(&class_list->rwlock);
     1211                return NULL;
     1212        }
     1213
     1214        fun_node_t *fun = dev->fun;
     1215
     1216        fibril_rwlock_read_unlock(&class_list->rwlock);
     1217
     1218        return fun;
     1219}
     1220
     1221
     1222/** Find child function node with a specified name.
     1223 *
     1224 * Device tree rwlock should be held at least for reading.
     1225 *
     1226 * @param parent        The parent function node.
     1227 * @param name          The name of the child function.
     1228 * @return              The child function node.
     1229 */
     1230fun_node_t *find_node_child(fun_node_t *pfun, const char *name)
     1231{
     1232        return find_fun_node_in_device(pfun->child, name);
    10321233}
    10331234
     
    10631264        if (info != NULL) {
    10641265                memset(info, 0, sizeof(dev_class_info_t));
    1065                 list_initialize(&info->dev_classes);
    1066                 list_initialize(&info->devmap_link);
    1067                 list_initialize(&info->link);
     1266                link_initialize(&info->dev_classes);
     1267                link_initialize(&info->devmap_link);
     1268                link_initialize(&info->link);
    10681269        }
    10691270       
     
    11071308}
    11081309
    1109 /** Add the device to the class.
     1310/** Add the device function to the class.
    11101311 *
    11111312 * The device may be added to multiple classes and a class may contain multiple
     
    11201321 *                      with the class.
    11211322 */
    1122 dev_class_info_t *add_device_to_class(node_t *dev, dev_class_t *cl,
     1323dev_class_info_t *add_function_to_class(fun_node_t *fun, dev_class_t *cl,
    11231324    const char *base_dev_name)
    11241325{
    1125         dev_class_info_t *info = create_dev_class_info();
     1326        dev_class_info_t *info;
     1327
     1328        assert(fun != NULL);
     1329        assert(cl != NULL);
     1330
     1331        info = create_dev_class_info();
     1332
    11261333       
    11271334        if (info != NULL) {
    11281335                info->dev_class = cl;
    1129                 info->dev = dev;
     1336                info->fun = fun;
    11301337               
    11311338                /* Add the device to the class. */
     
    11351342               
    11361343                /* Add the class to the device. */
    1137                 list_append(&info->dev_classes, &dev->classes);
     1344                list_append(&info->dev_classes, &fun->classes);
    11381345               
    11391346                /* Create unique name for the device within the class. */
     
    11851392}
    11861393
     1394dev_class_info_t *find_dev_in_class(dev_class_t *dev_class, const char *dev_name)
     1395{
     1396        assert(dev_class != NULL);
     1397        assert(dev_name != NULL);
     1398
     1399        link_t *link;
     1400        for (link = dev_class->devices.next;
     1401            link != &dev_class->devices;
     1402            link = link->next) {
     1403                dev_class_info_t *dev = list_get_instance(link,
     1404                    dev_class_info_t, link);
     1405
     1406                if (str_cmp(dev->dev_name, dev_name) == 0) {
     1407                        return dev;
     1408                }
     1409        }
     1410
     1411        return NULL;
     1412}
     1413
    11871414void init_class_list(class_list_t *class_list)
    11881415{
    11891416        list_initialize(&class_list->classes);
    11901417        fibril_rwlock_initialize(&class_list->rwlock);
    1191         hash_table_create(&class_list->devmap_devices, DEVICE_BUCKETS, 1,
     1418        hash_table_create(&class_list->devmap_functions, DEVICE_BUCKETS, 1,
    11921419            &devmap_devices_class_ops);
    11931420}
     
    11961423/* Devmap devices */
    11971424
    1198 node_t *find_devmap_tree_device(dev_tree_t *tree, devmap_handle_t devmap_handle)
    1199 {
    1200         node_t *dev = NULL;
     1425fun_node_t *find_devmap_tree_function(dev_tree_t *tree, devmap_handle_t devmap_handle)
     1426{
     1427        fun_node_t *fun = NULL;
    12011428        link_t *link;
    12021429        unsigned long key = (unsigned long) devmap_handle;
    12031430       
    12041431        fibril_rwlock_read_lock(&tree->rwlock);
    1205         link = hash_table_find(&tree->devmap_devices, &key);
     1432        link = hash_table_find(&tree->devmap_functions, &key);
    12061433        if (link != NULL)
    1207                 dev = hash_table_get_instance(link, node_t, devmap_link);
     1434                fun = hash_table_get_instance(link, fun_node_t, devmap_fun);
    12081435        fibril_rwlock_read_unlock(&tree->rwlock);
    12091436       
    1210         return dev;
    1211 }
    1212 
    1213 node_t *find_devmap_class_device(class_list_t *classes,
     1437        return fun;
     1438}
     1439
     1440fun_node_t *find_devmap_class_function(class_list_t *classes,
    12141441    devmap_handle_t devmap_handle)
    12151442{
    1216         node_t *dev = NULL;
     1443        fun_node_t *fun = NULL;
    12171444        dev_class_info_t *cli;
    12181445        link_t *link;
     
    12201447       
    12211448        fibril_rwlock_read_lock(&classes->rwlock);
    1222         link = hash_table_find(&classes->devmap_devices, &key);
     1449        link = hash_table_find(&classes->devmap_functions, &key);
    12231450        if (link != NULL) {
    12241451                cli = hash_table_get_instance(link, dev_class_info_t,
    12251452                    devmap_link);
    1226                 dev = cli->dev;
     1453                fun = cli->fun;
    12271454        }
    12281455        fibril_rwlock_read_unlock(&classes->rwlock);
    12291456       
    1230         return dev;
    1231 }
    1232 
    1233 void class_add_devmap_device(class_list_t *class_list, dev_class_info_t *cli)
     1457        return fun;
     1458}
     1459
     1460void class_add_devmap_function(class_list_t *class_list, dev_class_info_t *cli)
    12341461{
    12351462        unsigned long key = (unsigned long) cli->devmap_handle;
    12361463       
    12371464        fibril_rwlock_write_lock(&class_list->rwlock);
    1238         hash_table_insert(&class_list->devmap_devices, &key, &cli->devmap_link);
     1465        hash_table_insert(&class_list->devmap_functions, &key, &cli->devmap_link);
    12391466        fibril_rwlock_write_unlock(&class_list->rwlock);
    12401467
    1241         assert(find_devmap_class_device(class_list, cli->devmap_handle) != NULL);
    1242 }
    1243 
    1244 void tree_add_devmap_device(dev_tree_t *tree, node_t *node)
    1245 {
    1246         unsigned long key = (unsigned long) node->devmap_handle;
     1468        assert(find_devmap_class_function(class_list, cli->devmap_handle) != NULL);
     1469}
     1470
     1471void tree_add_devmap_function(dev_tree_t *tree, fun_node_t *fun)
     1472{
     1473        unsigned long key = (unsigned long) fun->devmap_handle;
    12471474        fibril_rwlock_write_lock(&tree->rwlock);
    1248         hash_table_insert(&tree->devmap_devices, &key, &node->devmap_link);
     1475        hash_table_insert(&tree->devmap_functions, &key, &fun->devmap_fun);
    12491476        fibril_rwlock_write_unlock(&tree->rwlock);
    12501477}
Note: See TracChangeset for help on using the changeset viewer.