Ignore:
File:
1 edited

Legend:

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

    rc7bbf029 r609243f4  
    3030 * @{
    3131 */
     32/** @file Device Manager
     33 *
     34 * Locking order:
     35 *   (1) driver_t.driver_mutex
     36 *   (2) dev_tree_t.rwlock
     37 *
     38 * Synchronization:
     39 *    - device_tree.rwlock protects:
     40 *        - tree root, complete tree topology
     41 *        - complete contents of device and function nodes
     42 *    - dev_node_t.refcnt, fun_node_t.refcnt prevent nodes from
     43 *      being deallocated
     44 *    - find_xxx() functions increase reference count of returned object
     45 *    - find_xxx_no_lock() do not increase reference count
     46 *
     47 * TODO
     48 *    - Track all steady and transient device/function states
     49 *    - Check states, wait for steady state on certain operations
     50 */
    3251
    3352#include <errno.h>
     
    3756#include <ipc/driver.h>
    3857#include <ipc/devman.h>
    39 #include <devmap.h>
     58#include <loc.h>
    4059#include <str_error.h>
    4160#include <stdio.h>
     
    4362#include "devman.h"
    4463
    45 fun_node_t *find_node_child(fun_node_t *parent, const char *name);
     64static fun_node_t *find_node_child(dev_tree_t *, fun_node_t *, const char *);
    4665
    4766/* hash table operations */
     
    6685}
    6786
    68 static int devmap_functions_compare(unsigned long key[], hash_count_t keys,
     87static int loc_functions_compare(unsigned long key[], hash_count_t keys,
    6988    link_t *item)
    7089{
    71         fun_node_t *fun = hash_table_get_instance(item, fun_node_t, devmap_fun);
    72         return (fun->devmap_handle == (devmap_handle_t) key[0]);
    73 }
    74 
    75 static int devmap_devices_class_compare(unsigned long key[], hash_count_t keys,
    76     link_t *item)
    77 {
    78         dev_class_info_t *class_info
    79             = hash_table_get_instance(item, dev_class_info_t, devmap_link);
    80         assert(class_info != NULL);
    81 
    82         return (class_info->devmap_handle == (devmap_handle_t) key[0]);
     90        fun_node_t *fun = hash_table_get_instance(item, fun_node_t, loc_fun);
     91        return (fun->service_id == (service_id_t) key[0]);
    8392}
    8493
     
    99108};
    100109
    101 static hash_table_operations_t devmap_devices_ops = {
     110static hash_table_operations_t loc_devices_ops = {
    102111        .hash = devices_hash,
    103         .compare = devmap_functions_compare,
    104         .remove_callback = devices_remove_callback
    105 };
    106 
    107 static hash_table_operations_t devmap_devices_class_ops = {
    108         .hash = devices_hash,
    109         .compare = devmap_devices_class_compare,
     112        .compare = loc_functions_compare,
    110113        .remove_callback = devices_remove_callback
    111114};
     
    270273        }
    271274       
    272         ssize_t read_bytes = safe_read(fd, buf, len);
     275        ssize_t read_bytes = read_all(fd, buf, len);
    273276        if (read_bytes <= 0) {
    274                 log_msg(LVL_ERROR, "Unable to read file '%s'.", conf_path);
     277                log_msg(LVL_ERROR, "Unable to read file '%s' (%zd).", conf_path,
     278                    read_bytes);
    275279                goto cleanup;
    276280        }
     
    421425        }
    422426       
    423         insert_fun_node(tree, fun, clone_string(""), NULL);
     427        fun_add_ref(fun);
     428        insert_fun_node(tree, fun, str_dup(""), NULL);
     429       
    424430        match_id_t *id = create_match_id();
    425         id->id = clone_string("root");
     431        id->id = str_dup("root");
    426432        id->score = 100;
    427433        add_match_id(&fun->match_ids, id);
     
    437443        }
    438444       
     445        dev_add_ref(dev);
    439446        insert_dev_node(tree, dev, fun);
    440447       
     
    466473        fibril_mutex_lock(&drivers_list->drivers_mutex);
    467474       
    468         link_t *link = drivers_list->drivers.next;
    469         while (link != &drivers_list->drivers) {
     475        list_foreach(drivers_list->drivers, link) {
    470476                drv = list_get_instance(link, driver_t, drivers);
    471477                score = get_match_score(drv, node);
     
    474480                        best_drv = drv;
    475481                }
    476                 link = link->next;
    477482        }
    478483       
     
    484489/** Assign a driver to a device.
    485490 *
     491 * @param tree          Device tree
    486492 * @param node          The device's node in the device tree.
    487493 * @param drv           The driver.
    488494 */
    489 void attach_driver(dev_node_t *dev, driver_t *drv)
     495void attach_driver(dev_tree_t *tree, dev_node_t *dev, driver_t *drv)
    490496{
    491497        log_msg(LVL_DEBUG, "attach_driver(dev=\"%s\",drv=\"%s\")",
     
    493499       
    494500        fibril_mutex_lock(&drv->driver_mutex);
     501        fibril_rwlock_write_lock(&tree->rwlock);
    495502       
    496503        dev->drv = drv;
    497504        list_append(&dev->driver_devices, &drv->devices);
    498505       
     506        fibril_rwlock_write_unlock(&tree->rwlock);
     507        fibril_mutex_unlock(&drv->driver_mutex);
     508}
     509
     510/** Detach driver from device.
     511 *
     512 * @param tree          Device tree
     513 * @param node          The device's node in the device tree.
     514 * @param drv           The driver.
     515 */
     516void detach_driver(dev_tree_t *tree, dev_node_t *dev)
     517{
     518        driver_t *drv = dev->drv;
     519       
     520        assert(drv != NULL);
     521       
     522        log_msg(LVL_DEBUG, "detach_driver(dev=\"%s\",drv=\"%s\")",
     523            dev->pfun->pathname, drv->name);
     524       
     525        fibril_mutex_lock(&drv->driver_mutex);
     526        fibril_rwlock_write_lock(&tree->rwlock);
     527       
     528        dev->drv = NULL;
     529        list_remove(&dev->driver_devices);
     530       
     531        fibril_rwlock_write_unlock(&tree->rwlock);
    499532        fibril_mutex_unlock(&drv->driver_mutex);
    500533}
     
    536569        driver_t *res = NULL;
    537570        driver_t *drv = NULL;
    538         link_t *link;
    539571       
    540572        fibril_mutex_lock(&drv_list->drivers_mutex);
    541573       
    542         link = drv_list->drivers.next;
    543         while (link != &drv_list->drivers) {
     574        list_foreach(drv_list->drivers, link) {
    544575                drv = list_get_instance(link, driver_t, drivers);
    545576                if (str_cmp(drv->name, drv_name) == 0) {
     
    547578                        break;
    548579                }
    549 
    550                 link = link->next;
    551580        }
    552581       
     
    564593        dev_node_t *dev;
    565594        link_t *link;
    566         int phone;
    567595
    568596        log_msg(LVL_DEBUG, "pass_devices_to_driver(driver=\"%s\")",
     
    570598
    571599        fibril_mutex_lock(&driver->driver_mutex);
    572 
    573         phone = async_connect_me_to(driver->phone, DRIVER_DEVMAN, 0, 0);
    574 
    575         if (phone < 0) {
    576                 fibril_mutex_unlock(&driver->driver_mutex);
    577                 return;
    578         }
    579600
    580601        /*
     
    582603         * that has not been passed to the driver.
    583604         */
    584         link = driver->devices.next;
    585         while (link != &driver->devices) {
     605        link = driver->devices.head.next;
     606        while (link != &driver->devices.head) {
    586607                dev = list_get_instance(link, dev_node_t, driver_devices);
     608                fibril_rwlock_write_lock(&tree->rwlock);
     609               
    587610                if (dev->passed_to_driver) {
     611                        fibril_rwlock_write_unlock(&tree->rwlock);
    588612                        link = link->next;
    589613                        continue;
    590614                }
    591615
    592                 /*
    593                  * We remove the device from the list to allow safe adding
    594                  * of new devices (no one will touch our item this way).
    595                  */
    596                 list_remove(link);
     616                log_msg(LVL_DEBUG, "pass_devices_to_driver: dev->refcnt=%d\n",
     617                    (int)atomic_get(&dev->refcnt));
     618                dev_add_ref(dev);
    597619
    598620                /*
     
    601623                 */
    602624                fibril_mutex_unlock(&driver->driver_mutex);
    603 
    604                 add_device(phone, driver, dev, tree);
     625                fibril_rwlock_write_unlock(&tree->rwlock);
     626
     627                add_device(driver, dev, tree);
     628
     629                dev_del_ref(dev);
    605630
    606631                /*
     
    611636
    612637                /*
    613                  * Insert the device back.
    614                  * The order is not relevant here so no harm is done
    615                  * (actually, the order would be preserved in most cases).
    616                  */
    617                 list_append(link, &driver->devices);
    618 
    619                 /*
    620638                 * Restart the cycle to go through all devices again.
    621639                 */
    622                 link = driver->devices.next;
    623         }
    624 
    625         async_hangup(phone);
     640                link = driver->devices.head.next;
     641        }
    626642
    627643        /*
     
    673689        list_initialize(&drv->devices);
    674690        fibril_mutex_initialize(&drv->driver_mutex);
    675         drv->phone = -1;
     691        drv->sess = NULL;
    676692}
    677693
     
    704720}
    705721
    706 /** Create devmap path and name for the function. */
    707 void devmap_register_tree_function(fun_node_t *fun, dev_tree_t *tree)
    708 {
    709         char *devmap_pathname = NULL;
    710         char *devmap_name = NULL;
    711        
    712         asprintf(&devmap_name, "%s", fun->pathname);
    713         if (devmap_name == NULL)
     722/** Create loc path and name for the function. */
     723void loc_register_tree_function(fun_node_t *fun, dev_tree_t *tree)
     724{
     725        char *loc_pathname = NULL;
     726        char *loc_name = NULL;
     727       
     728        assert(fibril_rwlock_is_locked(&tree->rwlock));
     729       
     730        asprintf(&loc_name, "%s", fun->pathname);
     731        if (loc_name == NULL)
    714732                return;
    715733       
    716         replace_char(devmap_name, '/', DEVMAP_SEPARATOR);
    717        
    718         asprintf(&devmap_pathname, "%s/%s", DEVMAP_DEVICE_NAMESPACE,
    719             devmap_name);
    720         if (devmap_pathname == NULL) {
    721                 free(devmap_name);
     734        replace_char(loc_name, '/', LOC_SEPARATOR);
     735       
     736        asprintf(&loc_pathname, "%s/%s", LOC_DEVICE_NAMESPACE,
     737            loc_name);
     738        if (loc_pathname == NULL) {
     739                free(loc_name);
    722740                return;
    723741        }
    724742       
    725         devmap_device_register_with_iface(devmap_pathname,
    726             &fun->devmap_handle, DEVMAN_CONNECT_FROM_DEVMAP);
    727        
    728         tree_add_devmap_function(tree, fun);
    729        
    730         free(devmap_name);
    731         free(devmap_pathname);
     743        loc_service_register_with_iface(loc_pathname,
     744            &fun->service_id, DEVMAN_CONNECT_FROM_LOC);
     745       
     746        tree_add_loc_function(tree, fun);
     747       
     748        free(loc_name);
     749        free(loc_pathname);
    732750}
    733751
     
    737755 * @param node          The device's node in the device tree.
    738756 */
    739 void add_device(int phone, driver_t *drv, dev_node_t *dev, dev_tree_t *tree)
     757void add_device(driver_t *drv, dev_node_t *dev, dev_tree_t *tree)
    740758{
    741759        /*
     
    746764            drv->name, dev->pfun->name);
    747765       
    748         sysarg_t rc;
    749         ipc_call_t answer;
    750        
    751766        /* Send the device to the driver. */
    752767        devman_handle_t parent_handle;
     
    756771                parent_handle = 0;
    757772        }
    758 
    759         aid_t req = async_send_2(phone, DRIVER_ADD_DEVICE, dev->handle,
     773       
     774        async_exch_t *exch = async_exchange_begin(drv->sess);
     775       
     776        ipc_call_t answer;
     777        aid_t req = async_send_2(exch, DRIVER_DEV_ADD, dev->handle,
    760778            parent_handle, &answer);
    761779       
    762         /* Send the device's name to the driver. */
    763         rc = async_data_write_start(phone, dev->pfun->name,
     780        /* Send the device name to the driver. */
     781        sysarg_t rc = async_data_write_start(exch, dev->pfun->name,
    764782            str_size(dev->pfun->name) + 1);
     783       
     784        async_exchange_end(exch);
     785       
    765786        if (rc != EOK) {
    766787                /* TODO handle error */
     
    773794        case EOK:
    774795                dev->state = DEVICE_USABLE;
     796                exch = async_exchange_begin(drv->sess);
     797                async_msg_1(exch, DRIVER_DEV_ADDED, dev->handle);
     798                async_exchange_end(exch);
    775799                break;
    776800        case ENOENT:
     
    811835       
    812836        /* Attach the driver to the device. */
    813         attach_driver(dev, drv);
     837        attach_driver(tree, dev, drv);
    814838       
    815839        fibril_mutex_lock(&drv->driver_mutex);
     
    821845        fibril_mutex_unlock(&drv->driver_mutex);
    822846
    823         if (is_running) {
    824                 /* Notify the driver about the new device. */
    825                 int phone = async_connect_me_to(drv->phone, DRIVER_DEVMAN, 0, 0);
    826                 if (phone >= 0) {
    827                         add_device(phone, drv, dev, tree);
    828                         async_hangup(phone);
    829                 }
    830         }
    831        
     847        /* Notify the driver about the new device. */
     848        if (is_running)
     849                add_device(drv, dev, tree);
     850       
     851        fibril_mutex_lock(&drv->driver_mutex);
     852        fibril_mutex_unlock(&drv->driver_mutex);
     853
     854        fibril_rwlock_write_lock(&tree->rwlock);
     855        if (dev->pfun != NULL) {
     856                dev->pfun->state = FUN_ON_LINE;
     857        }
     858        fibril_rwlock_write_unlock(&tree->rwlock);
    832859        return true;
     860}
     861
     862int driver_dev_remove(dev_tree_t *tree, dev_node_t *dev)
     863{
     864        async_exch_t *exch;
     865        sysarg_t retval;
     866        driver_t *drv;
     867        devman_handle_t handle;
     868       
     869        assert(dev != NULL);
     870       
     871        log_msg(LVL_DEBUG, "driver_dev_remove(%p)", dev);
     872       
     873        fibril_rwlock_read_lock(&tree->rwlock);
     874        drv = dev->drv;
     875        handle = dev->handle;
     876        fibril_rwlock_read_unlock(&tree->rwlock);
     877       
     878        exch = async_exchange_begin(drv->sess);
     879        retval = async_req_1_0(exch, DRIVER_DEV_REMOVE, handle);
     880        async_exchange_end(exch);
     881       
     882        return retval;
     883}
     884
     885int driver_dev_gone(dev_tree_t *tree, dev_node_t *dev)
     886{
     887        async_exch_t *exch;
     888        sysarg_t retval;
     889        driver_t *drv;
     890        devman_handle_t handle;
     891       
     892        assert(dev != NULL);
     893       
     894        log_msg(LVL_DEBUG, "driver_dev_gone(%p)", dev);
     895       
     896        fibril_rwlock_read_lock(&tree->rwlock);
     897        drv = dev->drv;
     898        handle = dev->handle;
     899        fibril_rwlock_read_unlock(&tree->rwlock);
     900       
     901        exch = async_exchange_begin(drv->sess);
     902        retval = async_req_1_0(exch, DRIVER_DEV_GONE, handle);
     903        async_exchange_end(exch);
     904       
     905        return retval;
     906}
     907
     908int driver_fun_online(dev_tree_t *tree, fun_node_t *fun)
     909{
     910        async_exch_t *exch;
     911        sysarg_t retval;
     912        driver_t *drv;
     913        devman_handle_t handle;
     914       
     915        log_msg(LVL_DEBUG, "driver_fun_online(%p)", fun);
     916
     917        fibril_rwlock_read_lock(&tree->rwlock);
     918       
     919        if (fun->dev == NULL) {
     920                /* XXX root function? */
     921                fibril_rwlock_read_unlock(&tree->rwlock);
     922                return EINVAL;
     923        }
     924       
     925        drv = fun->dev->drv;
     926        handle = fun->handle;
     927        fibril_rwlock_read_unlock(&tree->rwlock);
     928       
     929        exch = async_exchange_begin(drv->sess);
     930        retval = async_req_1_0(exch, DRIVER_FUN_ONLINE, handle);
     931        loc_exchange_end(exch);
     932       
     933        return retval;
     934}
     935
     936int driver_fun_offline(dev_tree_t *tree, fun_node_t *fun)
     937{
     938        async_exch_t *exch;
     939        sysarg_t retval;
     940        driver_t *drv;
     941        devman_handle_t handle;
     942       
     943        log_msg(LVL_DEBUG, "driver_fun_offline(%p)", fun);
     944
     945        fibril_rwlock_read_lock(&tree->rwlock);
     946        if (fun->dev == NULL) {
     947                /* XXX root function? */
     948                fibril_rwlock_read_unlock(&tree->rwlock);
     949                return EINVAL;
     950        }
     951       
     952        drv = fun->dev->drv;
     953        handle = fun->handle;
     954        fibril_rwlock_read_unlock(&tree->rwlock);
     955       
     956        exch = async_exchange_begin(drv->sess);
     957        retval = async_req_1_0(exch, DRIVER_FUN_OFFLINE, handle);
     958        loc_exchange_end(exch);
     959       
     960        return retval;
     961
    833962}
    834963
     
    851980        hash_table_create(&tree->devman_functions, DEVICE_BUCKETS, 1,
    852981            &devman_functions_ops);
    853         hash_table_create(&tree->devmap_functions, DEVICE_BUCKETS, 1,
    854             &devmap_devices_ops);
     982        hash_table_create(&tree->loc_functions, DEVICE_BUCKETS, 1,
     983            &loc_devices_ops);
    855984       
    856985        fibril_rwlock_initialize(&tree->rwlock);
     
    859988        if (!create_root_nodes(tree))
    860989                return false;
    861 
     990   
    862991        /* Find suitable driver and start it. */
    863         return assign_driver(tree->root_node->child, drivers_list, tree);
     992        dev_node_t *rdev = tree->root_node->child;
     993        dev_add_ref(rdev);
     994        int rc = assign_driver(rdev, drivers_list, tree);
     995        dev_del_ref(rdev);
     996       
     997        return rc;
    864998}
    865999
     
    8721006dev_node_t *create_dev_node(void)
    8731007{
    874         dev_node_t *res = malloc(sizeof(dev_node_t));
    875        
    876         if (res != NULL) {
    877                 memset(res, 0, sizeof(dev_node_t));
    878                 list_initialize(&res->functions);
    879                 link_initialize(&res->driver_devices);
    880                 link_initialize(&res->devman_dev);
    881         }
    882        
    883         return res;
     1008        dev_node_t *dev;
     1009       
     1010        dev = calloc(1, sizeof(dev_node_t));
     1011        if (dev == NULL)
     1012                return NULL;
     1013       
     1014        atomic_set(&dev->refcnt, 0);
     1015        list_initialize(&dev->functions);
     1016        link_initialize(&dev->driver_devices);
     1017        link_initialize(&dev->devman_dev);
     1018       
     1019        return dev;
    8841020}
    8851021
     
    8971033}
    8981034
     1035/** Increase device node reference count.
     1036 *
     1037 * @param dev   Device node
     1038 */
     1039void dev_add_ref(dev_node_t *dev)
     1040{
     1041        atomic_inc(&dev->refcnt);
     1042}
     1043
     1044/** Decrease device node reference count.
     1045 *
     1046 * When the count drops to zero the device node is freed.
     1047 *
     1048 * @param dev   Device node
     1049 */
     1050void dev_del_ref(dev_node_t *dev)
     1051{
     1052        if (atomic_predec(&dev->refcnt) == 0)
     1053                delete_dev_node(dev);
     1054}
     1055
     1056
    8991057/** Find the device node structure of the device witch has the specified handle.
    9001058 *
     
    9111069       
    9121070        link = hash_table_find(&tree->devman_devices, &key);
     1071        if (link == NULL)
     1072                return NULL;
     1073       
    9131074        return hash_table_get_instance(link, dev_node_t, devman_dev);
    9141075}
     
    9261087        fibril_rwlock_read_lock(&tree->rwlock);
    9271088        dev = find_dev_node_no_lock(tree, handle);
     1089        if (dev != NULL)
     1090                dev_add_ref(dev);
     1091       
    9281092        fibril_rwlock_read_unlock(&tree->rwlock);
    9291093       
     
    9311095}
    9321096
     1097/** Get list of device functions. */
     1098int dev_get_functions(dev_tree_t *tree, dev_node_t *dev,
     1099    devman_handle_t *hdl_buf, size_t buf_size, size_t *act_size)
     1100{
     1101        size_t act_cnt;
     1102        size_t buf_cnt;
     1103
     1104        assert(fibril_rwlock_is_locked(&tree->rwlock));
     1105
     1106        buf_cnt = buf_size / sizeof(devman_handle_t);
     1107
     1108        act_cnt = list_count(&dev->functions);
     1109        *act_size = act_cnt * sizeof(devman_handle_t);
     1110
     1111        if (buf_size % sizeof(devman_handle_t) != 0)
     1112                return EINVAL;
     1113
     1114        size_t pos = 0;
     1115        list_foreach(dev->functions, item) {
     1116                fun_node_t *fun =
     1117                    list_get_instance(item, fun_node_t, dev_functions);
     1118
     1119                if (pos < buf_cnt) {
     1120                        hdl_buf[pos] = fun->handle;
     1121                }
     1122
     1123                pos++;
     1124        }
     1125
     1126        return EOK;
     1127}
     1128
     1129
    9331130/* Function nodes */
    9341131
     
    9391136fun_node_t *create_fun_node(void)
    9401137{
    941         fun_node_t *res = malloc(sizeof(fun_node_t));
    942        
    943         if (res != NULL) {
    944                 memset(res, 0, sizeof(fun_node_t));
    945                 link_initialize(&res->dev_functions);
    946                 list_initialize(&res->match_ids.ids);
    947                 list_initialize(&res->classes);
    948                 link_initialize(&res->devman_fun);
    949                 link_initialize(&res->devmap_fun);
    950         }
    951        
    952         return res;
     1138        fun_node_t *fun;
     1139
     1140        fun = calloc(1, sizeof(fun_node_t));
     1141        if (fun == NULL)
     1142                return NULL;
     1143       
     1144        fun->state = FUN_INIT;
     1145        atomic_set(&fun->refcnt, 0);
     1146        link_initialize(&fun->dev_functions);
     1147        list_initialize(&fun->match_ids.ids);
     1148        link_initialize(&fun->devman_fun);
     1149        link_initialize(&fun->loc_fun);
     1150       
     1151        return fun;
    9531152}
    9541153
     
    9681167}
    9691168
     1169/** Increase function node reference count.
     1170 *
     1171 * @param fun   Function node
     1172 */
     1173void fun_add_ref(fun_node_t *fun)
     1174{
     1175        atomic_inc(&fun->refcnt);
     1176}
     1177
     1178/** Decrease function node reference count.
     1179 *
     1180 * When the count drops to zero the function node is freed.
     1181 *
     1182 * @param fun   Function node
     1183 */
     1184void fun_del_ref(fun_node_t *fun)
     1185{
     1186        if (atomic_predec(&fun->refcnt) == 0)
     1187                delete_fun_node(fun);
     1188}
     1189
    9701190/** Find the function node with the specified handle.
    9711191 *
     
    9781198        unsigned long key = handle;
    9791199        link_t *link;
     1200        fun_node_t *fun;
    9801201       
    9811202        assert(fibril_rwlock_is_locked(&tree->rwlock));
     
    9851206                return NULL;
    9861207       
    987         return hash_table_get_instance(link, fun_node_t, devman_fun);
     1208        fun = hash_table_get_instance(link, fun_node_t, devman_fun);
     1209       
     1210        return fun;
    9881211}
    9891212
     
    9991222       
    10001223        fibril_rwlock_read_lock(&tree->rwlock);
     1224       
    10011225        fun = find_fun_node_no_lock(tree, handle);
     1226        if (fun != NULL)
     1227                fun_add_ref(fun);
     1228       
    10021229        fibril_rwlock_read_unlock(&tree->rwlock);
    10031230       
     
    10071234/** Create and set device's full path in device tree.
    10081235 *
     1236 * @param tree          Device tree
    10091237 * @param node          The device's device node.
    10101238 * @param parent        The parent device node.
     
    10121240 *                      resources etc.).
    10131241 */
    1014 static bool set_fun_path(fun_node_t *fun, fun_node_t *parent)
    1015 {
     1242static bool set_fun_path(dev_tree_t *tree, fun_node_t *fun, fun_node_t *parent)
     1243{
     1244        assert(fibril_rwlock_is_write_locked(&tree->rwlock));
    10161245        assert(fun->name != NULL);
    10171246       
     
    10401269 *
    10411270 * @param tree          The device tree.
    1042  * @param node          The newly added device node.
    1043  * @param dev_name      The name of the newly added device.
    1044  * @param parent        The parent device node.
     1271 * @param dev           The newly added device node.
     1272 * @param pfun          The parent function node.
    10451273 *
    10461274 * @return              True on success, false otherwise (insufficient resources
     
    10491277bool insert_dev_node(dev_tree_t *tree, dev_node_t *dev, fun_node_t *pfun)
    10501278{
    1051         assert(dev != NULL);
    1052         assert(tree != NULL);
    10531279        assert(fibril_rwlock_is_write_locked(&tree->rwlock));
    10541280       
     
    10681294}
    10691295
     1296/** Remove device from device tree.
     1297 *
     1298 * @param tree          Device tree
     1299 * @param dev           Device node
     1300 */
     1301void remove_dev_node(dev_tree_t *tree, dev_node_t *dev)
     1302{
     1303        assert(fibril_rwlock_is_write_locked(&tree->rwlock));
     1304       
     1305        log_msg(LVL_DEBUG, "remove_dev_node(dev=%p)", dev);
     1306       
     1307        /* Remove node from the handle-to-node map. */
     1308        unsigned long key = dev->handle;
     1309        hash_table_remove(&tree->devman_devices, &key, 1);
     1310       
     1311        /* Unlink from parent function. */
     1312        dev->pfun->child = NULL;
     1313        dev->pfun = NULL;
     1314       
     1315        dev->state = DEVICE_REMOVED;
     1316}
     1317
     1318
    10701319/** Insert new function into device tree.
    10711320 *
    10721321 * @param tree          The device tree.
    1073  * @param node          The newly added function node.
    1074  * @param dev_name      The name of the newly added function.
    1075  * @param parent        Owning device node.
     1322 * @param fun           The newly added function node.
     1323 * @param fun_name      The name of the newly added function.
     1324 * @param dev           Owning device node.
    10761325 *
    10771326 * @return              True on success, false otherwise (insufficient resources
     
    10831332        fun_node_t *pfun;
    10841333       
    1085         assert(fun != NULL);
    1086         assert(tree != NULL);
    10871334        assert(fun_name != NULL);
    10881335        assert(fibril_rwlock_is_write_locked(&tree->rwlock));
     
    10951342       
    10961343        fun->name = fun_name;
    1097         if (!set_fun_path(fun, pfun)) {
     1344        if (!set_fun_path(tree, fun, pfun)) {
    10981345                return false;
    10991346        }
     
    11101357       
    11111358        return true;
     1359}
     1360
     1361/** Remove function from device tree.
     1362 *
     1363 * @param tree          Device tree
     1364 * @param node          Function node to remove
     1365 */
     1366void remove_fun_node(dev_tree_t *tree, fun_node_t *fun)
     1367{
     1368        assert(fibril_rwlock_is_write_locked(&tree->rwlock));
     1369       
     1370        /* Remove the node from the handle-to-node map. */
     1371        unsigned long key = fun->handle;
     1372        hash_table_remove(&tree->devman_functions, &key, 1);
     1373       
     1374        /* Remove the node from the list of its parent's children. */
     1375        if (fun->dev != NULL)
     1376                list_remove(&fun->dev_functions);
     1377       
     1378        fun->dev = NULL;
     1379        fun->state = FUN_REMOVED;
    11121380}
    11131381
     
    11311399       
    11321400        fun_node_t *fun = tree->root_node;
     1401        fun_add_ref(fun);
    11331402        /*
    11341403         * Relative path to the function from its parent (but with '/' at the
     
    11371406        char *rel_path = path;
    11381407        char *next_path_elem = NULL;
    1139         bool cont = true;
     1408        bool cont = (rel_path[1] != '\0');
    11401409       
    11411410        while (cont && fun != NULL) {
     
    11481417                }
    11491418               
    1150                 fun = find_node_child(fun, rel_path + 1);
     1419                fun_node_t *cfun = find_node_child(tree, fun, rel_path + 1);
     1420                fun_del_ref(fun);
     1421                fun = cfun;
    11511422               
    11521423                if (cont) {
     
    11661437 * Device tree rwlock should be held at least for reading.
    11671438 *
     1439 * @param tree Device tree
    11681440 * @param dev Device the function belongs to.
    11691441 * @param name Function name (not path).
     
    11711443 * @retval NULL No function with given name.
    11721444 */
    1173 fun_node_t *find_fun_node_in_device(dev_node_t *dev, const char *name)
    1174 {
    1175         assert(dev != NULL);
     1445fun_node_t *find_fun_node_in_device(dev_tree_t *tree, dev_node_t *dev,
     1446    const char *name)
     1447{
    11761448        assert(name != NULL);
     1449        assert(fibril_rwlock_is_locked(&tree->rwlock));
    11771450
    11781451        fun_node_t *fun;
    1179         link_t *link;
    1180 
    1181         for (link = dev->functions.next;
    1182             link != &dev->functions;
    1183             link = link->next) {
     1452
     1453        list_foreach(dev->functions, link) {
    11841454                fun = list_get_instance(link, fun_node_t, dev_functions);
    11851455
    1186                 if (str_cmp(name, fun->name) == 0)
     1456                if (str_cmp(name, fun->name) == 0) {
     1457                        fun_add_ref(fun);
    11871458                        return fun;
     1459                }
    11881460        }
    11891461
     
    11911463}
    11921464
    1193 /** Find function node by its class name and index. */
    1194 fun_node_t *find_fun_node_by_class(class_list_t *class_list,
    1195     const char *class_name, const char *dev_name)
    1196 {
    1197         assert(class_list != NULL);
    1198         assert(class_name != NULL);
    1199         assert(dev_name != NULL);
    1200 
    1201         fibril_rwlock_read_lock(&class_list->rwlock);
    1202 
    1203         dev_class_t *cl = find_dev_class_no_lock(class_list, class_name);
    1204         if (cl == NULL) {
    1205                 fibril_rwlock_read_unlock(&class_list->rwlock);
    1206                 return NULL;
    1207         }
    1208 
    1209         dev_class_info_t *dev = find_dev_in_class(cl, dev_name);
    1210         if (dev == NULL) {
    1211                 fibril_rwlock_read_unlock(&class_list->rwlock);
    1212                 return NULL;
    1213         }
    1214 
    1215         fun_node_t *fun = dev->fun;
    1216 
    1217         fibril_rwlock_read_unlock(&class_list->rwlock);
    1218 
    1219         return fun;
    1220 }
    1221 
    1222 
    12231465/** Find child function node with a specified name.
    12241466 *
    12251467 * Device tree rwlock should be held at least for reading.
    12261468 *
     1469 * @param tree          Device tree
    12271470 * @param parent        The parent function node.
    12281471 * @param name          The name of the child function.
    12291472 * @return              The child function node.
    12301473 */
    1231 fun_node_t *find_node_child(fun_node_t *pfun, const char *name)
    1232 {
    1233         return find_fun_node_in_device(pfun->child, name);
    1234 }
    1235 
    1236 /* Device classes */
    1237 
    1238 /** Create device class.
    1239  *
    1240  * @return      Device class.
    1241  */
    1242 dev_class_t *create_dev_class(void)
    1243 {
    1244         dev_class_t *cl;
    1245        
    1246         cl = (dev_class_t *) malloc(sizeof(dev_class_t));
    1247         if (cl != NULL) {
    1248                 memset(cl, 0, sizeof(dev_class_t));
    1249                 list_initialize(&cl->devices);
    1250                 fibril_mutex_initialize(&cl->mutex);
    1251         }
    1252        
    1253         return cl;
    1254 }
    1255 
    1256 /** Create device class info.
    1257  *
    1258  * @return              Device class info.
    1259  */
    1260 dev_class_info_t *create_dev_class_info(void)
    1261 {
    1262         dev_class_info_t *info;
    1263        
    1264         info = (dev_class_info_t *) malloc(sizeof(dev_class_info_t));
    1265         if (info != NULL) {
    1266                 memset(info, 0, sizeof(dev_class_info_t));
    1267                 link_initialize(&info->dev_classes);
    1268                 link_initialize(&info->devmap_link);
    1269                 link_initialize(&info->link);
    1270         }
    1271        
    1272         return info;
    1273 }
    1274 
    1275 size_t get_new_class_dev_idx(dev_class_t *cl)
    1276 {
    1277         size_t dev_idx;
    1278        
    1279         fibril_mutex_lock(&cl->mutex);
    1280         dev_idx = ++cl->curr_dev_idx;
    1281         fibril_mutex_unlock(&cl->mutex);
    1282        
    1283         return dev_idx;
    1284 }
    1285 
    1286 
    1287 /** Create unique device name within the class.
    1288  *
    1289  * @param cl            The class.
    1290  * @param base_dev_name Contains the base name for the device if it was
    1291  *                      specified by the driver when it registered the device by
    1292  *                      the class; NULL if driver specified no base name.
    1293  * @return              The unique name for the device within the class.
    1294  */
    1295 char *create_dev_name_for_class(dev_class_t *cl, const char *base_dev_name)
    1296 {
    1297         char *dev_name;
    1298         const char *base_name;
    1299        
    1300         if (base_dev_name != NULL)
    1301                 base_name = base_dev_name;
    1302         else
    1303                 base_name = cl->base_dev_name;
    1304        
    1305         size_t idx = get_new_class_dev_idx(cl);
    1306         asprintf(&dev_name, "%s%zu", base_name, idx);
    1307        
    1308         return dev_name;
    1309 }
    1310 
    1311 /** Add the device function to the class.
    1312  *
    1313  * The device may be added to multiple classes and a class may contain multiple
    1314  * devices. The class and the device are associated with each other by the
    1315  * dev_class_info_t structure.
    1316  *
    1317  * @param dev           The device.
    1318  * @param class         The class.
    1319  * @param base_dev_name The base name of the device within the class if
    1320  *                      specified by the driver, NULL otherwise.
    1321  * @return              dev_class_info_t structure which associates the device
    1322  *                      with the class.
    1323  */
    1324 dev_class_info_t *add_function_to_class(fun_node_t *fun, dev_class_t *cl,
    1325     const char *base_dev_name)
    1326 {
    1327         dev_class_info_t *info;
    1328 
    1329         assert(fun != NULL);
    1330         assert(cl != NULL);
    1331 
    1332         info = create_dev_class_info();
    1333 
    1334        
    1335         if (info != NULL) {
    1336                 info->dev_class = cl;
    1337                 info->fun = fun;
    1338                
    1339                 /* Add the device to the class. */
    1340                 fibril_mutex_lock(&cl->mutex);
    1341                 list_append(&info->link, &cl->devices);
    1342                 fibril_mutex_unlock(&cl->mutex);
    1343                
    1344                 /* Add the class to the device. */
    1345                 list_append(&info->dev_classes, &fun->classes);
    1346                
    1347                 /* Create unique name for the device within the class. */
    1348                 info->dev_name = create_dev_name_for_class(cl, base_dev_name);
    1349         }
    1350        
    1351         return info;
    1352 }
    1353 
    1354 dev_class_t *get_dev_class(class_list_t *class_list, char *class_name)
    1355 {
    1356         dev_class_t *cl;
    1357        
    1358         fibril_rwlock_write_lock(&class_list->rwlock);
    1359         cl = find_dev_class_no_lock(class_list, class_name);
    1360         if (cl == NULL) {
    1361                 cl = create_dev_class();
    1362                 if (cl != NULL) {
    1363                         cl->name = class_name;
    1364                         cl->base_dev_name = "";
    1365                         add_dev_class_no_lock(class_list, cl);
    1366                 }
    1367         }
    1368 
    1369         fibril_rwlock_write_unlock(&class_list->rwlock);
    1370         return cl;
    1371 }
    1372 
    1373 dev_class_t *find_dev_class_no_lock(class_list_t *class_list,
    1374     const char *class_name)
    1375 {
    1376         dev_class_t *cl;
    1377         link_t *link = class_list->classes.next;
    1378        
    1379         while (link != &class_list->classes) {
    1380                 cl = list_get_instance(link, dev_class_t, link);
    1381                 if (str_cmp(cl->name, class_name) == 0) {
    1382                         return cl;
    1383                 }
    1384                 link = link->next;
    1385         }
    1386        
    1387         return NULL;
    1388 }
    1389 
    1390 void add_dev_class_no_lock(class_list_t *class_list, dev_class_t *cl)
    1391 {
    1392         list_append(&cl->link, &class_list->classes);
    1393 }
    1394 
    1395 dev_class_info_t *find_dev_in_class(dev_class_t *dev_class, const char *dev_name)
    1396 {
    1397         assert(dev_class != NULL);
    1398         assert(dev_name != NULL);
    1399 
    1400         link_t *link;
    1401         for (link = dev_class->devices.next;
    1402             link != &dev_class->devices;
    1403             link = link->next) {
    1404                 dev_class_info_t *dev = list_get_instance(link,
    1405                     dev_class_info_t, link);
    1406 
    1407                 if (str_cmp(dev->dev_name, dev_name) == 0) {
    1408                         return dev;
    1409                 }
    1410         }
    1411 
    1412         return NULL;
    1413 }
    1414 
    1415 void init_class_list(class_list_t *class_list)
    1416 {
    1417         list_initialize(&class_list->classes);
    1418         fibril_rwlock_initialize(&class_list->rwlock);
    1419         hash_table_create(&class_list->devmap_functions, DEVICE_BUCKETS, 1,
    1420             &devmap_devices_class_ops);
    1421 }
    1422 
    1423 
    1424 /* Devmap devices */
    1425 
    1426 fun_node_t *find_devmap_tree_function(dev_tree_t *tree, devmap_handle_t devmap_handle)
     1474static fun_node_t *find_node_child(dev_tree_t *tree, fun_node_t *pfun,
     1475    const char *name)
     1476{
     1477        return find_fun_node_in_device(tree, pfun->child, name);
     1478}
     1479
     1480/* loc devices */
     1481
     1482fun_node_t *find_loc_tree_function(dev_tree_t *tree, service_id_t service_id)
    14271483{
    14281484        fun_node_t *fun = NULL;
    14291485        link_t *link;
    1430         unsigned long key = (unsigned long) devmap_handle;
     1486        unsigned long key = (unsigned long) service_id;
    14311487       
    14321488        fibril_rwlock_read_lock(&tree->rwlock);
    1433         link = hash_table_find(&tree->devmap_functions, &key);
    1434         if (link != NULL)
    1435                 fun = hash_table_get_instance(link, fun_node_t, devmap_fun);
     1489        link = hash_table_find(&tree->loc_functions, &key);
     1490        if (link != NULL) {
     1491                fun = hash_table_get_instance(link, fun_node_t, loc_fun);
     1492                fun_add_ref(fun);
     1493        }
    14361494        fibril_rwlock_read_unlock(&tree->rwlock);
    14371495       
     
    14391497}
    14401498
    1441 fun_node_t *find_devmap_class_function(class_list_t *classes,
    1442     devmap_handle_t devmap_handle)
    1443 {
    1444         fun_node_t *fun = NULL;
    1445         dev_class_info_t *cli;
    1446         link_t *link;
    1447         unsigned long key = (unsigned long)devmap_handle;
    1448        
    1449         fibril_rwlock_read_lock(&classes->rwlock);
    1450         link = hash_table_find(&classes->devmap_functions, &key);
    1451         if (link != NULL) {
    1452                 cli = hash_table_get_instance(link, dev_class_info_t,
    1453                     devmap_link);
    1454                 fun = cli->fun;
    1455         }
    1456         fibril_rwlock_read_unlock(&classes->rwlock);
    1457        
    1458         return fun;
    1459 }
    1460 
    1461 void class_add_devmap_function(class_list_t *class_list, dev_class_info_t *cli)
    1462 {
    1463         unsigned long key = (unsigned long) cli->devmap_handle;
    1464        
    1465         fibril_rwlock_write_lock(&class_list->rwlock);
    1466         hash_table_insert(&class_list->devmap_functions, &key, &cli->devmap_link);
    1467         fibril_rwlock_write_unlock(&class_list->rwlock);
    1468 
    1469         assert(find_devmap_class_function(class_list, cli->devmap_handle) != NULL);
    1470 }
    1471 
    1472 void tree_add_devmap_function(dev_tree_t *tree, fun_node_t *fun)
    1473 {
    1474         unsigned long key = (unsigned long) fun->devmap_handle;
    1475         fibril_rwlock_write_lock(&tree->rwlock);
    1476         hash_table_insert(&tree->devmap_functions, &key, &fun->devmap_fun);
    1477         fibril_rwlock_write_unlock(&tree->rwlock);
     1499void tree_add_loc_function(dev_tree_t *tree, fun_node_t *fun)
     1500{
     1501        assert(fibril_rwlock_is_write_locked(&tree->rwlock));
     1502       
     1503        unsigned long key = (unsigned long) fun->service_id;
     1504        hash_table_insert(&tree->loc_functions, &key, &fun->loc_fun);
    14781505}
    14791506
Note: See TracChangeset for help on using the changeset viewer.