Ignore:
File:
1 edited

Legend:

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

    rc4f7bf6 rf302586  
    4444#include <errno.h>
    4545#include <str_error.h>
    46 #include <stdbool.h>
     46#include <bool.h>
    4747#include <fibril_synch.h>
    4848#include <stdlib.h>
     
    7373        char *drv_name = NULL;
    7474
    75         log_msg(LOG_DEFAULT, LVL_DEBUG, "devman_driver_register");
     75        log_msg(LVL_DEBUG, "devman_driver_register");
    7676       
    7777        /* Get driver name. */
     
    8282        }
    8383
    84         log_msg(LOG_DEFAULT, LVL_DEBUG, "The `%s' driver is trying to register.",
     84        log_msg(LVL_DEBUG, "The `%s' driver is trying to register.",
    8585            drv_name);
    8686       
     
    8888        driver = find_driver(&drivers_list, drv_name);
    8989        if (driver == NULL) {
    90                 log_msg(LOG_DEFAULT, LVL_ERROR, "No driver named `%s' was found.", drv_name);
     90                log_msg(LVL_ERROR, "No driver named `%s' was found.", drv_name);
    9191                free(drv_name);
    9292                drv_name = NULL;
     
    102102        if (driver->sess) {
    103103                /* We already have a connection to the driver. */
    104                 log_msg(LOG_DEFAULT, LVL_ERROR, "Driver '%s' already started.\n",
     104                log_msg(LVL_ERROR, "Driver '%s' already started.\n",
    105105                    driver->name);
    106106                fibril_mutex_unlock(&driver->driver_mutex);
     
    112112        case DRIVER_NOT_STARTED:
    113113                /* Somebody started the driver manually. */
    114                 log_msg(LOG_DEFAULT, LVL_NOTE, "Driver '%s' started manually.\n",
     114                log_msg(LVL_NOTE, "Driver '%s' started manually.\n",
    115115                    driver->name);
    116116                driver->state = DRIVER_STARTING;
     
    125125       
    126126        /* Create connection to the driver. */
    127         log_msg(LOG_DEFAULT, LVL_DEBUG, "Creating connection to the `%s' driver.",
     127        log_msg(LVL_DEBUG, "Creating connection to the `%s' driver.",
    128128            driver->name);
    129129        driver->sess = async_callback_receive(EXCHANGE_PARALLEL);
     
    136136        async_sess_args_set(driver->sess, DRIVER_DEVMAN, 0, 0);
    137137       
    138         log_msg(LOG_DEFAULT, LVL_NOTE,
     138        log_msg(LVL_NOTE,
    139139            "The `%s' driver was successfully registered as running.",
    140140            driver->name);
     
    147147        fid_t fid = fibril_create(init_running_drv, driver);
    148148        if (fid == 0) {
    149                 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed to create initialization fibril " \
     149                log_msg(LVL_ERROR, "Failed to create initialization fibril " \
    150150                    "for driver `%s'.", driver->name);
    151151                fibril_mutex_unlock(&driver->driver_mutex);
     
    176176        callid = async_get_call(&call);
    177177        if (DEVMAN_ADD_MATCH_ID != IPC_GET_IMETHOD(call)) {
    178                 log_msg(LOG_DEFAULT, LVL_ERROR,
     178                log_msg(LVL_ERROR,
    179179                    "Invalid protocol when trying to receive match id.");
    180180                async_answer_0(callid, EINVAL);
     
    184184       
    185185        if (match_id == NULL) {
    186                 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed to allocate match id.");
     186                log_msg(LVL_ERROR, "Failed to allocate match id.");
    187187                async_answer_0(callid, ENOMEM);
    188188                return ENOMEM;
     
    198198        if (rc != EOK) {
    199199                delete_match_id(match_id);
    200                 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed to receive match id string: %s.",
     200                log_msg(LVL_ERROR, "Failed to receive match id string: %s.",
    201201                    str_error(rc));
    202202                return rc;
     
    205205        list_append(&match_id->link, &match_ids->ids);
    206206       
    207         log_msg(LOG_DEFAULT, LVL_DEBUG, "Received match id `%s', score %d.",
     207        log_msg(LVL_DEBUG, "Received match id `%s', score %d.",
    208208            match_id->id, match_id->score);
    209209        return rc;
     
    245245       
    246246        fibril_rwlock_write_lock(&device_tree.rwlock);
    247        
     247
    248248        if (fun->state == FUN_ON_LINE) {
    249249                fibril_rwlock_write_unlock(&device_tree.rwlock);
    250                 log_msg(LOG_DEFAULT, LVL_WARN, "Function %s is already on line.",
     250                log_msg(LVL_WARN, "Function %s is already on line.",
    251251                    fun->pathname);
    252252                return EOK;
     
    259259                        return ENOMEM;
    260260                }
    261                
     261
    262262                insert_dev_node(&device_tree, dev, fun);
    263263                dev_add_ref(dev);
    264264        }
    265265       
    266         log_msg(LOG_DEFAULT, LVL_DEBUG, "devman_add_function(fun=\"%s\")", fun->pathname);
     266        log_msg(LVL_DEBUG, "devman_add_function(fun=\"%s\")", fun->pathname);
    267267       
    268268        if (fun->ftype == fun_inner) {
     
    272272                /* Give one reference over to assign_driver_fibril(). */
    273273                dev_add_ref(dev);
    274                
    275274                /*
    276275                 * Try to find a suitable driver and assign it to the device.  We do
     
    282281                fid_t assign_fibril = fibril_create(assign_driver_fibril, dev);
    283282                if (assign_fibril == 0) {
    284                         log_msg(LOG_DEFAULT, LVL_ERROR, "Failed to create fibril for "
     283                        log_msg(LVL_ERROR, "Failed to create fibril for "
    285284                            "assigning driver.");
    286285                        /* XXX Cleanup */
     
    289288                }
    290289                fibril_add_ready(assign_fibril);
    291         } else
     290        } else {
    292291                loc_register_tree_function(fun, &device_tree);
     292        }
    293293       
    294294        fibril_rwlock_write_unlock(&device_tree.rwlock);
     
    305305        if (fun->state == FUN_OFF_LINE) {
    306306                fibril_rwlock_write_unlock(&device_tree.rwlock);
    307                 log_msg(LOG_DEFAULT, LVL_WARN, "Function %s is already off line.",
     307                log_msg(LVL_WARN, "Function %s is already off line.",
    308308                    fun->pathname);
    309309                return EOK;
     
    311311       
    312312        if (fun->ftype == fun_inner) {
    313                 log_msg(LOG_DEFAULT, LVL_DEBUG, "Offlining inner function %s.",
     313                log_msg(LVL_DEBUG, "Offlining inner function %s.",
    314314                    fun->pathname);
    315315               
     
    359359                if (rc != EOK) {
    360360                        fibril_rwlock_write_unlock(&device_tree.rwlock);
    361                         log_msg(LOG_DEFAULT, LVL_ERROR, "Failed unregistering tree service.");
     361                        log_msg(LVL_ERROR, "Failed unregistering tree service.");
    362362                        return EIO;
    363363                }
     
    391391        if (ftype != fun_inner && ftype != fun_exposed) {
    392392                /* Unknown function type */
    393                 log_msg(LOG_DEFAULT, LVL_ERROR,
     393                log_msg(LVL_ERROR,
    394394                    "Unknown function type %d provided by driver.",
    395395                    (int) ftype);
     
    419419       
    420420        /* Check that function with same name is not there already. */
    421         fun_node_t *tfun = find_fun_node_in_device(tree, pdev, fun_name);
    422         if (tfun) {
    423                 fun_del_ref(tfun);      /* drop the new unwanted reference */
     421        if (find_fun_node_in_device(tree, pdev, fun_name) != NULL) {
    424422                fibril_rwlock_write_unlock(&tree->rwlock);
    425423                dev_del_ref(pdev);
     
    432430       
    433431        fun_node_t *fun = create_fun_node();
    434         /* One reference for creation, one for us */
    435         fun_add_ref(fun);
    436432        fun_add_ref(fun);
    437433        fun->ftype = ftype;
    438        
    439         /*
    440          * We can lock the function here even when holding the tree because
    441          * we know it cannot be held by anyone else yet.
    442          */
    443         fun_busy_lock(fun);
    444434       
    445435        if (!insert_fun_node(&device_tree, fun, fun_name, pdev)) {
    446436                fibril_rwlock_write_unlock(&tree->rwlock);
    447437                dev_del_ref(pdev);
    448                 fun_busy_unlock(fun);
    449                 fun_del_ref(fun);
    450438                delete_fun_node(fun);
    451439                async_answer_0(callid, ENOMEM);
     
    460448        rc = online_function(fun);
    461449        if (rc != EOK) {
    462                 /* XXX Set some failed state? */
    463                 fun_busy_unlock(fun);
    464                 fun_del_ref(fun);
     450                /* XXX clean up */
    465451                async_answer_0(callid, rc);
    466452                return;
    467453        }
    468        
    469         fun_busy_unlock(fun);
    470         fun_del_ref(fun);
    471454       
    472455        /* Return device handle to parent's driver. */
     
    507490        if (rc == EOK) {
    508491                loc_service_add_to_cat(fun->service_id, cat_id);
    509                 log_msg(LOG_DEFAULT, LVL_NOTE, "Function `%s' added to category `%s'.",
     492                log_msg(LVL_NOTE, "Function `%s' added to category `%s'.",
    510493                    fun->pathname, cat_name);
    511494        } else {
    512                 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed adding function `%s' to category "
     495                log_msg(LVL_ERROR, "Failed adding function `%s' to category "
    513496                    "`%s'.", fun->pathname, cat_name);
    514497        }
     
    529512        int rc;
    530513       
    531         log_msg(LOG_DEFAULT, LVL_DEBUG, "devman_drv_fun_online()");
     514        log_msg(LVL_DEBUG, "devman_drv_fun_online()");
    532515       
    533516        fun = find_fun_node(&device_tree, IPC_GET_ARG1(*icall));
     
    536519                return;
    537520        }
    538        
    539         fun_busy_lock(fun);
    540521       
    541522        fibril_rwlock_read_lock(&device_tree.rwlock);
    542523        if (fun->dev == NULL || fun->dev->drv != drv) {
    543524                fibril_rwlock_read_unlock(&device_tree.rwlock);
    544                 fun_busy_unlock(fun);
    545525                fun_del_ref(fun);
    546526                async_answer_0(iid, ENOENT);
     
    551531        rc = online_function(fun);
    552532        if (rc != EOK) {
    553                 fun_busy_unlock(fun);
    554533                fun_del_ref(fun);
    555534                async_answer_0(iid, (sysarg_t) rc);
     
    557536        }
    558537       
    559         fun_busy_unlock(fun);
    560538        fun_del_ref(fun);
    561539       
     
    579557        }
    580558       
    581         fun_busy_lock(fun);
    582        
    583559        fibril_rwlock_write_lock(&device_tree.rwlock);
    584560        if (fun->dev == NULL || fun->dev->drv != drv) {
    585                 fun_busy_unlock(fun);
    586561                fun_del_ref(fun);
    587562                async_answer_0(iid, ENOENT);
     
    592567        rc = offline_function(fun);
    593568        if (rc != EOK) {
    594                 fun_busy_unlock(fun);
    595569                fun_del_ref(fun);
    596570                async_answer_0(iid, (sysarg_t) rc);
     
    598572        }
    599573       
    600         fun_busy_unlock(fun);
    601574        fun_del_ref(fun);
    602575        async_answer_0(iid, (sysarg_t) EOK);
     
    616589        }
    617590       
    618         fun_busy_lock(fun);
    619        
    620591        fibril_rwlock_write_lock(&tree->rwlock);
    621592       
    622         log_msg(LOG_DEFAULT, LVL_DEBUG, "devman_remove_function(fun='%s')", fun->pathname);
     593        log_msg(LVL_DEBUG, "devman_remove_function(fun='%s')", fun->pathname);
    623594       
    624595        /* Check function state */
    625596        if (fun->state == FUN_REMOVED) {
    626597                fibril_rwlock_write_unlock(&tree->rwlock);
    627                 fun_busy_unlock(fun);
    628                 fun_del_ref(fun);
    629598                async_answer_0(callid, ENOENT);
    630599                return;
     
    653622                        /* Verify that driver succeeded and removed all functions */
    654623                        if (gone_rc != EOK || !list_empty(&dev->functions)) {
    655                                 log_msg(LOG_DEFAULT, LVL_ERROR, "Driver did not remove "
     624                                log_msg(LVL_ERROR, "Driver did not remove "
    656625                                    "functions for device that is gone. "
    657626                                    "Device node is now defunct.");
     
    667636                                if (gone_rc == EOK)
    668637                                        gone_rc = ENOTSUP;
    669                                 fun_busy_unlock(fun);
    670                                 fun_del_ref(fun);
    671638                                async_answer_0(callid, gone_rc);
    672639                                return;
     
    692659                        rc = loc_service_unregister(fun->service_id);
    693660                        if (rc != EOK) {
    694                                 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed unregistering tree "
     661                                log_msg(LVL_ERROR, "Failed unregistering tree "
    695662                                    "service.");
    696663                                fibril_rwlock_write_unlock(&tree->rwlock);
    697                                 fun_busy_unlock(fun);
    698664                                fun_del_ref(fun);
    699665                                async_answer_0(callid, EIO);
     
    705671        remove_fun_node(&device_tree, fun);
    706672        fibril_rwlock_write_unlock(&tree->rwlock);
    707         fun_busy_unlock(fun);
    708673       
    709674        /* Delete ref added when inserting function into tree */
     
    712677        fun_del_ref(fun);
    713678       
    714         log_msg(LOG_DEFAULT, LVL_DEBUG, "devman_remove_function() succeeded.");
     679        log_msg(LVL_DEBUG, "devman_remove_function() succeeded.");
    715680        async_answer_0(callid, EOK);
    716681}
     
    726691       
    727692        initialize_running_driver(driver, &device_tree);
    728         log_msg(LOG_DEFAULT, LVL_DEBUG, "The `%s` driver was successfully initialized.",
     693        log_msg(LVL_DEBUG, "The `%s` driver was successfully initialized.",
    729694            driver->name);
    730695        return 0;
     
    735700{
    736701        client_t *client;
    737         driver_t *driver = NULL;
     702        driver_t *driver;
    738703       
    739704        /* Accept the connection. */
     
    742707        client = async_get_client_data();
    743708        if (client == NULL) {
    744                 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed to allocate client data.");
     709                log_msg(LVL_ERROR, "Failed to allocate client data.");
    745710                return;
    746711        }
     
    890855}
    891856
    892 /** Get function driver name. */
    893 static void devman_fun_get_driver_name(ipc_callid_t iid, ipc_call_t *icall)
    894 {
    895         devman_handle_t handle = IPC_GET_ARG1(*icall);
    896 
    897         fun_node_t *fun = find_fun_node(&device_tree, handle);
    898         if (fun == NULL) {
    899                 async_answer_0(iid, ENOMEM);
    900                 return;
    901         }
    902 
    903         ipc_callid_t data_callid;
    904         size_t data_len;
    905         if (!async_data_read_receive(&data_callid, &data_len)) {
    906                 async_answer_0(iid, EINVAL);
    907                 fun_del_ref(fun);
    908                 return;
    909         }
    910 
    911         void *buffer = malloc(data_len);
    912         if (buffer == NULL) {
    913                 async_answer_0(data_callid, ENOMEM);
    914                 async_answer_0(iid, ENOMEM);
    915                 fun_del_ref(fun);
    916                 return;
    917         }
    918 
    919         fibril_rwlock_read_lock(&device_tree.rwlock);
    920 
    921         /* Check function state */
    922         if (fun->state == FUN_REMOVED) {
    923                 fibril_rwlock_read_unlock(&device_tree.rwlock);
    924                 free(buffer);
    925 
    926                 async_answer_0(data_callid, ENOENT);
    927                 async_answer_0(iid, ENOENT);
    928                 fun_del_ref(fun);
    929                 return;
    930         }
    931 
    932         /* Check whether function has a driver */
    933         if (fun->child == NULL || fun->child->drv == NULL) {
    934                 fibril_rwlock_read_unlock(&device_tree.rwlock);
    935                 free(buffer);
    936 
    937                 async_answer_0(data_callid, EINVAL);
    938                 async_answer_0(iid, EINVAL);
    939                 fun_del_ref(fun);
    940                 return;
    941         }
    942 
    943         size_t sent_length = str_size(fun->child->drv->name);
    944         if (sent_length > data_len) {
    945                 sent_length = data_len;
    946         }
    947 
    948         async_data_read_finalize(data_callid, fun->child->drv->name,
    949             sent_length);
    950         async_answer_0(iid, EOK);
    951 
    952         fibril_rwlock_read_unlock(&device_tree.rwlock);
    953         fun_del_ref(fun);
    954         free(buffer);
    955 }
    956857
    957858/** Get device path. */
     
    11821083                        devman_fun_get_name(callid, &call);
    11831084                        break;
    1184                 case DEVMAN_FUN_GET_DRIVER_NAME:
    1185                         devman_fun_get_driver_name(callid, &call);
    1186                         break;
    11871085                case DEVMAN_FUN_GET_PATH:
    11881086                        devman_fun_get_path(callid, &call);
     
    12291127         */
    12301128        if (dev == NULL) {
    1231                 log_msg(LOG_DEFAULT, LVL_ERROR, "IPC forwarding failed - no device or "
     1129                log_msg(LVL_ERROR, "IPC forwarding failed - no device or "
    12321130                    "function with handle %" PRIun " was found.", handle);
    12331131                async_answer_0(iid, ENOENT);
     
    12361134
    12371135        if (fun == NULL && !drv_to_parent) {
    1238                 log_msg(LOG_DEFAULT, LVL_ERROR, NAME ": devman_forward error - cannot "
     1136                log_msg(LVL_ERROR, NAME ": devman_forward error - cannot "
    12391137                    "connect to handle %" PRIun ", refers to a device.",
    12401138                    handle);
     
    12511149                if (dev->pfun->dev != NULL)
    12521150                        driver = dev->pfun->dev->drv;
    1253                
    12541151                fwd_h = dev->pfun->handle;
    12551152        } else if (dev->state == DEVICE_USABLE) {
     
    12571154                driver = dev->drv;
    12581155                assert(driver != NULL);
    1259                
     1156
    12601157                fwd_h = handle;
    12611158        }
     
    12641161       
    12651162        if (driver == NULL) {
    1266                 log_msg(LOG_DEFAULT, LVL_ERROR, "IPC forwarding refused - " \
     1163                log_msg(LVL_ERROR, "IPC forwarding refused - " \
    12671164                    "the device %" PRIun " is not in usable state.", handle);
    12681165                async_answer_0(iid, ENOENT);
     
    12771174       
    12781175        if (!driver->sess) {
    1279                 log_msg(LOG_DEFAULT, LVL_ERROR,
     1176                log_msg(LVL_ERROR,
    12801177                    "Could not forward to driver `%s'.", driver->name);
    12811178                async_answer_0(iid, EINVAL);
     
    12841181
    12851182        if (fun != NULL) {
    1286                 log_msg(LOG_DEFAULT, LVL_DEBUG,
     1183                log_msg(LVL_DEBUG,
    12871184                    "Forwarding request for `%s' function to driver `%s'.",
    12881185                    fun->pathname, driver->name);
    12891186        } else {
    1290                 log_msg(LOG_DEFAULT, LVL_DEBUG,
     1187                log_msg(LVL_DEBUG,
    12911188                    "Forwarding request for `%s' device to driver `%s'.",
    12921189                    dev->pfun->pathname, driver->name);
     
    12961193        async_forward_fast(iid, exch, method, fwd_h, 0, IPC_FF_NONE);
    12971194        async_exchange_end(exch);
    1298        
     1195
    12991196cleanup:
    13001197        if (dev != NULL)
    13011198                dev_del_ref(dev);
    1302        
    13031199        if (fun != NULL)
    13041200                fun_del_ref(fun);
     
    13201216       
    13211217        if (fun == NULL || fun->dev == NULL || fun->dev->drv == NULL) {
    1322                 log_msg(LOG_DEFAULT, LVL_WARN, "devman_connection_loc(): function "
     1218                log_msg(LVL_WARN, "devman_connection_loc(): function "
    13231219                    "not found.\n");
    13241220                fibril_rwlock_read_unlock(&device_tree.rwlock);
     
    13381234        async_exchange_end(exch);
    13391235       
    1340         log_msg(LOG_DEFAULT, LVL_DEBUG,
     1236        log_msg(LVL_DEBUG,
    13411237            "Forwarding loc service request for `%s' function to driver `%s'.",
    13421238            fun->pathname, driver->name);
     
    13941290static bool devman_init(void)
    13951291{
    1396         log_msg(LOG_DEFAULT, LVL_DEBUG, "devman_init - looking for available drivers.");
     1292        log_msg(LVL_DEBUG, "devman_init - looking for available drivers.");
    13971293       
    13981294        /* Initialize list of available drivers. */
     
    14001296        if (lookup_available_drivers(&drivers_list,
    14011297            DRIVER_DEFAULT_STORE) == 0) {
    1402                 log_msg(LOG_DEFAULT, LVL_FATAL, "No drivers found.");
     1298                log_msg(LVL_FATAL, "No drivers found.");
    14031299                return false;
    14041300        }
    1405        
    1406         log_msg(LOG_DEFAULT, LVL_DEBUG, "devman_init - list of drivers has been initialized.");
    1407        
     1301
     1302        log_msg(LVL_DEBUG, "devman_init - list of drivers has been initialized.");
     1303
    14081304        /* Create root device node. */
    14091305        if (!init_device_tree(&device_tree, &drivers_list)) {
    1410                 log_msg(LOG_DEFAULT, LVL_FATAL, "Failed to initialize device tree.");
     1306                log_msg(LVL_FATAL, "Failed to initialize device tree.");
    14111307                return false;
    14121308        }
    1413        
     1309
    14141310        /*
    14151311         * Caution: As the device manager is not a real loc
     
    14261322int main(int argc, char *argv[])
    14271323{
    1428         printf("%s: HelenOS Device Manager\n", NAME);
    1429        
    1430         int rc = log_init(NAME);
    1431         if (rc != EOK) {
    1432                 printf("%s: Error initializing logging subsystem.\n", NAME);
    1433                 return rc;
     1324        printf(NAME ": HelenOS Device Manager\n");
     1325
     1326        if (log_init(NAME, LVL_WARN) != EOK) {
     1327                printf(NAME ": Error initializing logging subsystem.\n");
     1328                return -1;
    14341329        }
    14351330       
     
    14381333        async_set_client_data_destructor(devman_client_data_destroy);
    14391334        async_set_client_connection(devman_connection);
    1440        
     1335
    14411336        if (!devman_init()) {
    1442                 log_msg(LOG_DEFAULT, LVL_ERROR, "Error while initializing service.");
     1337                log_msg(LVL_ERROR, "Error while initializing service.");
    14431338                return -1;
    14441339        }
    1445        
     1340
    14461341        /* Register device manager at naming service. */
    1447         rc = service_register(SERVICE_DEVMAN);
    1448         if (rc != EOK) {
    1449                 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering as a service.");
    1450                 return rc;
    1451         }
    1452        
    1453         printf("%s: Accepting connections.\n", NAME);
     1342        if (service_register(SERVICE_DEVMAN) != EOK) {
     1343                log_msg(LVL_ERROR, "Failed registering as a service.");
     1344                return -1;
     1345        }
     1346
     1347        printf(NAME ": Accepting connections.\n");
    14541348        task_retval(0);
    14551349        async_manager();
    1456        
     1350
    14571351        /* Never reached. */
    14581352        return 0;
Note: See TracChangeset for help on using the changeset viewer.