Ignore:
File:
1 edited

Legend:

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

    r8b1e15ac rc7bbf029  
    3434#include <fcntl.h>
    3535#include <sys/stat.h>
     36#include <io/log.h>
    3637#include <ipc/driver.h>
    3738#include <ipc/devman.h>
    3839#include <devmap.h>
    3940#include <str_error.h>
     41#include <stdio.h>
    4042
    4143#include "devman.h"
     
    146148        fibril_mutex_unlock(&drivers_list->drivers_mutex);
    147149
    148         printf(NAME": the '%s' driver was added to the list of available "
    149             "drivers.\n", drv->name);
     150        log_msg(LVL_NOTE, "Driver `%s' was added to the list of available "
     151            "drivers.", drv->name);
    150152}
    151153
     
    237239bool read_match_ids(const char *conf_path, match_id_list_t *ids)
    238240{
    239         printf(NAME ": read_match_ids conf_path = %s.\n", conf_path);
     241        log_msg(LVL_DEBUG, "read_match_ids(conf_path=\"%s\")", conf_path);
    240242       
    241243        bool suc = false;
     
    247249        fd = open(conf_path, O_RDONLY);
    248250        if (fd < 0) {
    249                 printf(NAME ": unable to open %s\n", conf_path);
     251                log_msg(LVL_ERROR, "Unable to open `%s' for reading: %s.",
     252                    conf_path, str_error(fd));
    250253                goto cleanup;
    251254        }
     
    255258        lseek(fd, 0, SEEK_SET);
    256259        if (len == 0) {
    257                 printf(NAME ": configuration file '%s' is empty.\n", conf_path);
     260                log_msg(LVL_ERROR, "Configuration file '%s' is empty.",
     261                    conf_path);
    258262                goto cleanup;
    259263        }
     
    261265        buf = malloc(len + 1);
    262266        if (buf == NULL) {
    263                 printf(NAME ": memory allocation failed when parsing file "
    264                     "'%s'.\n", conf_path);
     267                log_msg(LVL_ERROR, "Memory allocation failed when parsing file "
     268                    "'%s'.", conf_path);
    265269                goto cleanup;
    266270        }
    267271       
    268         if (read(fd, buf, len) <= 0) {
    269                 printf(NAME ": unable to read file '%s'.\n", conf_path);
     272        ssize_t read_bytes = safe_read(fd, buf, len);
     273        if (read_bytes <= 0) {
     274                log_msg(LVL_ERROR, "Unable to read file '%s'.", conf_path);
    270275                goto cleanup;
    271276        }
    272         buf[len] = 0;
     277        buf[read_bytes] = 0;
    273278       
    274279        suc = parse_match_ids(buf, ids);
     
    305310bool get_driver_info(const char *base_path, const char *name, driver_t *drv)
    306311{
    307         printf(NAME ": get_driver_info base_path = %s, name = %s.\n",
     312        log_msg(LVL_DEBUG, "get_driver_info(base_path=\"%s\", name=\"%s\")",
    308313            base_path, name);
    309314       
     
    337342        struct stat s;
    338343        if (stat(drv->binary_path, &s) == ENOENT) { /* FIXME!! */
    339                 printf(NAME ": driver not found at path %s.", drv->binary_path);
     344                log_msg(LVL_ERROR, "Driver not found at path `%s'.",
     345                    drv->binary_path);
    340346                goto cleanup;
    341347        }
     
    364370int lookup_available_drivers(driver_list_t *drivers_list, const char *dir_path)
    365371{
    366         printf(NAME ": lookup_available_drivers, dir = %s \n", dir_path);
     372        log_msg(LVL_DEBUG, "lookup_available_drivers(dir=\"%s\")", dir_path);
    367373       
    368374        int drv_cnt = 0;
     
    398404        dev_node_t *dev;
    399405       
    400         printf(NAME ": create_root_nodes\n");
     406        log_msg(LVL_DEBUG, "create_root_nodes()");
    401407       
    402408        fibril_rwlock_write_lock(&tree->rwlock);
     
    483489void attach_driver(dev_node_t *dev, driver_t *drv)
    484490{
    485         printf(NAME ": attach_driver %s to device %s\n",
    486             drv->name, dev->pfun->pathname);
     491        log_msg(LVL_DEBUG, "attach_driver(dev=\"%s\",drv=\"%s\")",
     492            dev->pfun->pathname, drv->name);
    487493       
    488494        fibril_mutex_lock(&drv->driver_mutex);
     
    506512        assert(fibril_mutex_is_locked(&drv->driver_mutex));
    507513       
    508         printf(NAME ": start_driver '%s'\n", drv->name);
     514        log_msg(LVL_DEBUG, "start_driver(drv=\"%s\")", drv->name);
    509515       
    510516        rc = task_spawnl(NULL, drv->binary_path, drv->binary_path, NULL);
    511517        if (rc != EOK) {
    512                 printf(NAME ": error spawning %s (%s)\n",
    513                     drv->name, str_error(rc));
     518                log_msg(LVL_ERROR, "Spawning driver `%s' (%s) failed: %s.",
     519                    drv->name, drv->binary_path, str_error(rc));
    514520                return false;
    515521        }
     
    550556}
    551557
    552 /** Remember the driver's phone.
    553  *
    554  * @param driver        The driver.
    555  * @param phone         The phone to the driver.
    556  */
    557 void set_driver_phone(driver_t *driver, sysarg_t phone)
    558 {
    559         fibril_mutex_lock(&driver->driver_mutex);
    560         assert(driver->state == DRIVER_STARTING);
    561         driver->phone = phone;
    562         fibril_mutex_unlock(&driver->driver_mutex);
    563 }
    564 
    565558/** Notify driver about the devices to which it was assigned.
    566559 *
     
    573566        int phone;
    574567
    575         printf(NAME ": pass_devices_to_driver(`%s')\n", driver->name);
     568        log_msg(LVL_DEBUG, "pass_devices_to_driver(driver=\"%s\")",
     569            driver->name);
    576570
    577571        fibril_mutex_lock(&driver->driver_mutex);
     
    640634         * immediately and possibly started here as well.
    641635         */
    642         printf(NAME ": driver %s goes into running state.\n", driver->name);
     636        log_msg(LVL_DEBUG, "Driver `%s' enters running state.", driver->name);
    643637        driver->state = DRIVER_RUNNING;
    644638
     
    657651void initialize_running_driver(driver_t *driver, dev_tree_t *tree)
    658652{
    659         printf(NAME ": initialize_running_driver (`%s')\n", driver->name);
     653        log_msg(LVL_DEBUG, "initialize_running_driver(driver=\"%s\")",
     654            driver->name);
    660655       
    661656        /*
     
    678673        list_initialize(&drv->devices);
    679674        fibril_mutex_initialize(&drv->driver_mutex);
     675        drv->phone = -1;
    680676}
    681677
     
    747743         * access any structures that would affect driver_t.
    748744         */
    749         printf(NAME ": add_device (driver `%s', device `%s')\n", drv->name,
    750             dev->pfun->name);
     745        log_msg(LVL_DEBUG, "add_device(drv=\"%s\", dev=\"%s\")",
     746            drv->name, dev->pfun->name);
    751747       
    752748        sysarg_t rc;
     
    809805        driver_t *drv = find_best_match_driver(drivers_list, dev);
    810806        if (drv == NULL) {
    811                 printf(NAME ": no driver found for device '%s'.\n",
     807                log_msg(LVL_ERROR, "No driver found for device `%s'.",
    812808                    dev->pfun->pathname);
    813809                return false;
     
    847843bool init_device_tree(dev_tree_t *tree, driver_list_t *drivers_list)
    848844{
    849         printf(NAME ": init_device_tree.\n");
     845        log_msg(LVL_DEBUG, "init_device_tree()");
    850846       
    851847        tree->current_handle = 0;
     
    10261022        fun->pathname = (char *) malloc(pathsize);
    10271023        if (fun->pathname == NULL) {
    1028                 printf(NAME ": failed to allocate device path.\n");
     1024                log_msg(LVL_ERROR, "Failed to allocate device path.");
    10291025                return false;
    10301026        }
     
    10571053        assert(fibril_rwlock_is_write_locked(&tree->rwlock));
    10581054       
     1055        log_msg(LVL_DEBUG, "insert_dev_node(dev=%p, pfun=%p [\"%s\"])",
     1056            dev, pfun, pfun->pathname);
     1057
    10591058        /* Add the node to the handle-to-node map. */
    10601059        dev->handle = ++tree->current_handle;
     
    10631062
    10641063        /* Add the node to the list of its parent's children. */
    1065         printf("insert_dev_node: dev=%p, dev->pfun := %p\n", dev, pfun);
    10661064        dev->pfun = pfun;
    10671065        pfun->child = dev;
     
    11231121fun_node_t *find_fun_node_by_path(dev_tree_t *tree, char *path)
    11241122{
     1123        assert(path != NULL);
     1124
     1125        bool is_absolute = path[0] == '/';
     1126        if (!is_absolute) {
     1127                return NULL;
     1128        }
     1129
    11251130        fibril_rwlock_read_lock(&tree->rwlock);
    11261131       
     
    11321137        char *rel_path = path;
    11331138        char *next_path_elem = NULL;
    1134         bool cont = (rel_path[0] == '/');
     1139        bool cont = true;
    11351140       
    11361141        while (cont && fun != NULL) {
     
    11571162}
    11581163
     1164/** Find function with a specified name belonging to given device.
     1165 *
     1166 * Device tree rwlock should be held at least for reading.
     1167 *
     1168 * @param dev Device the function belongs to.
     1169 * @param name Function name (not path).
     1170 * @return Function node.
     1171 * @retval NULL No function with given name.
     1172 */
     1173fun_node_t *find_fun_node_in_device(dev_node_t *dev, const char *name)
     1174{
     1175        assert(dev != NULL);
     1176        assert(name != NULL);
     1177
     1178        fun_node_t *fun;
     1179        link_t *link;
     1180
     1181        for (link = dev->functions.next;
     1182            link != &dev->functions;
     1183            link = link->next) {
     1184                fun = list_get_instance(link, fun_node_t, dev_functions);
     1185
     1186                if (str_cmp(name, fun->name) == 0)
     1187                        return fun;
     1188        }
     1189
     1190        return NULL;
     1191}
     1192
     1193/** Find function node by its class name and index. */
     1194fun_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
    11591223/** Find child function node with a specified name.
    11601224 *
     
    11671231fun_node_t *find_node_child(fun_node_t *pfun, const char *name)
    11681232{
    1169         fun_node_t *fun;
    1170         link_t *link;
    1171        
    1172         link = pfun->child->functions.next;
    1173        
    1174         while (link != &pfun->child->functions) {
    1175                 fun = list_get_instance(link, fun_node_t, dev_functions);
    1176                
    1177                 if (str_cmp(name, fun->name) == 0)
    1178                         return fun;
    1179                
    1180                 link = link->next;
    1181         }
    1182        
    1183         return NULL;
     1233        return find_fun_node_in_device(pfun->child, name);
    11841234}
    11851235
     
    12151265        if (info != NULL) {
    12161266                memset(info, 0, sizeof(dev_class_info_t));
    1217                 list_initialize(&info->dev_classes);
    1218                 list_initialize(&info->devmap_link);
    1219                 list_initialize(&info->link);
     1267                link_initialize(&info->dev_classes);
     1268                link_initialize(&info->devmap_link);
     1269                link_initialize(&info->link);
    12201270        }
    12211271       
     
    13431393}
    13441394
     1395dev_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
    13451415void init_class_list(class_list_t *class_list)
    13461416{
Note: See TracChangeset for help on using the changeset viewer.