Changeset 694ca93f in mainline for uspace/srv/devman/main.c


Ignore:
Timestamp:
2011-05-01T19:34:26Z (13 years ago)
Author:
Martin Sucha <sucha14@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
0e26444
Parents:
1ff896e (diff), 042fbe0 (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/main.c

    r1ff896e r694ca93f  
    4343#include <stdio.h>
    4444#include <errno.h>
     45#include <str_error.h>
    4546#include <bool.h>
    4647#include <fibril_synch.h>
     
    5152#include <sys/stat.h>
    5253#include <ctype.h>
     54#include <io/log.h>
    5355#include <ipc/devman.h>
    5456#include <ipc/driver.h>
     
    7173        driver_t *driver = NULL;
    7274
    73         printf(NAME ": devman_driver_register \n");
     75        log_msg(LVL_DEBUG, "devman_driver_register");
    7476       
    7577        iid = async_get_call(&icall);
     
    8890        }
    8991
    90         printf(NAME ": the %s driver is trying to register by the service.\n",
     92        log_msg(LVL_DEBUG, "The `%s' driver is trying to register.",
    9193            drv_name);
    9294       
    9395        /* Find driver structure. */
    9496        driver = find_driver(&drivers_list, drv_name);
    95        
    9697        if (driver == NULL) {
    97                 printf(NAME ": no driver named %s was found.\n", drv_name);
     98                log_msg(LVL_ERROR, "No driver named `%s' was found.", drv_name);
    9899                free(drv_name);
    99100                drv_name = NULL;
     
    105106        drv_name = NULL;
    106107       
     108        fibril_mutex_lock(&driver->driver_mutex);
     109       
     110        if (driver->phone >= 0) {
     111                /* We already have a connection to the driver. */
     112                log_msg(LVL_ERROR, "Driver '%s' already started.\n",
     113                    driver->name);
     114                fibril_mutex_unlock(&driver->driver_mutex);
     115                async_answer_0(iid, EEXISTS);
     116                return NULL;
     117        }
     118       
     119        switch (driver->state) {
     120        case DRIVER_NOT_STARTED:
     121                /* Somebody started the driver manually. */
     122                log_msg(LVL_NOTE, "Driver '%s' started manually.\n",
     123                    driver->name);
     124                driver->state = DRIVER_STARTING;
     125                break;
     126        case DRIVER_STARTING:
     127                /* The expected case */
     128                break;
     129        case DRIVER_RUNNING:
     130                /* Should not happen since we do not have a connected phone */
     131                assert(false);
     132        }
     133       
    107134        /* Create connection to the driver. */
    108         printf(NAME ":  creating connection to the %s driver.\n", driver->name);
     135        log_msg(LVL_DEBUG, "Creating connection to the `%s' driver.",
     136            driver->name);
    109137        ipc_call_t call;
    110138        ipc_callid_t callid = async_get_call(&call);
    111139        if (IPC_GET_IMETHOD(call) != IPC_M_CONNECT_TO_ME) {
     140                fibril_mutex_unlock(&driver->driver_mutex);
    112141                async_answer_0(callid, ENOTSUP);
    113142                async_answer_0(iid, ENOTSUP);
     
    116145       
    117146        /* Remember driver's phone. */
    118         set_driver_phone(driver, IPC_GET_ARG5(call));
    119        
    120         printf(NAME ": the %s driver was successfully registered as running.\n",
     147        driver->phone = IPC_GET_ARG5(call);
     148       
     149        fibril_mutex_unlock(&driver->driver_mutex);
     150       
     151        log_msg(LVL_NOTE,
     152            "The `%s' driver was successfully registered as running.",
    121153            driver->name);
    122154       
     
    142174        callid = async_get_call(&call);
    143175        if (DEVMAN_ADD_MATCH_ID != IPC_GET_IMETHOD(call)) {
    144                 printf(NAME ": ERROR: devman_receive_match_id - invalid "
    145                     "protocol.\n");
     176                log_msg(LVL_ERROR,
     177                    "Invalid protocol when trying to receive match id.");
    146178                async_answer_0(callid, EINVAL);
    147179                delete_match_id(match_id);
     
    150182       
    151183        if (match_id == NULL) {
    152                 printf(NAME ": ERROR: devman_receive_match_id - failed to "
    153                     "allocate match id.\n");
     184                log_msg(LVL_ERROR, "Failed to allocate match id.");
    154185                async_answer_0(callid, ENOMEM);
    155186                return ENOMEM;
     
    165196        if (rc != EOK) {
    166197                delete_match_id(match_id);
    167                 printf(NAME ": devman_receive_match_id - failed to receive "
    168                     "match id string.\n");
     198                log_msg(LVL_ERROR, "Failed to receive match id string: %s.",
     199                    str_error(rc));
    169200                return rc;
    170201        }
     
    172203        list_append(&match_id->link, &match_ids->ids);
    173204       
    174         printf(NAME ": received match id '%s', score = %d \n",
     205        log_msg(LVL_DEBUG, "Received match id `%s', score %d.",
    175206            match_id->id, match_id->score);
    176207        return rc;
     
    228259        if (ftype != fun_inner && ftype != fun_exposed) {
    229260                /* Unknown function type */
    230                 printf(NAME ": Error, unknown function type provided by driver!\n");
     261                log_msg(LVL_ERROR,
     262                    "Unknown function type %d provided by driver.",
     263                    (int) ftype);
    231264
    232265                fibril_rwlock_write_unlock(&tree->rwlock);
     
    243276        }
    244277       
     278        /* Check that function with same name is not there already. */
     279        if (find_fun_node_in_device(pdev, fun_name) != NULL) {
     280                fibril_rwlock_write_unlock(&tree->rwlock);
     281                async_answer_0(callid, EEXISTS);
     282                printf(NAME ": Warning, driver tried to register `%s' twice.\n",
     283                    fun_name);
     284                free(fun_name);
     285                return;
     286        }
     287
    245288        fun_node_t *fun = create_fun_node();
    246289        if (!insert_fun_node(&device_tree, fun, fun_name, pdev)) {
     
    265308        fibril_rwlock_write_unlock(&tree->rwlock);
    266309       
    267         printf(NAME ": devman_add_function %s\n", fun->pathname);
     310        log_msg(LVL_DEBUG, "devman_add_function(fun=\"%s\")", fun->pathname);
    268311       
    269312        devman_receive_match_ids(match_count, &fun->match_ids);
     
    347390        devmap_register_class_dev(class_info);
    348391       
    349         printf(NAME ": function'%s' added to class '%s', class name '%s' was "
    350             "asigned to it\n", fun->pathname, class_name, class_info->dev_name);
     392        log_msg(LVL_NOTE, "Function `%s' added to class `%s' as `%s'.",
     393            fun->pathname, class_name, class_info->dev_name);
    351394
    352395        async_answer_0(callid, EOK);
     
    363406       
    364407        initialize_running_driver(driver, &device_tree);
    365         printf(NAME ": the %s driver was successfully initialized. \n",
     408        log_msg(LVL_DEBUG, "The `%s` driver was successfully initialized.",
    366409            driver->name);
    367410        return 0;
     
    385428        fid_t fid = fibril_create(init_running_drv, driver);
    386429        if (fid == 0) {
    387                 printf(NAME ": Error creating fibril for the initialization of "
    388                     "the newly registered running driver.\n");
     430                log_msg(LVL_ERROR, "Failed to create initialization fibril " \
     431                    "for driver `%s'.", driver->name);
    389432                return;
    390433        }
     
    438481}
    439482
     483/** Find handle for the device instance identified by device class name. */
     484static void devman_function_get_handle_by_class(ipc_callid_t iid,
     485    ipc_call_t *icall)
     486{
     487        char *classname;
     488        char *devname;
     489
     490        int rc = async_data_write_accept((void **) &classname, true, 0, 0, 0, 0);
     491        if (rc != EOK) {
     492                async_answer_0(iid, rc);
     493                return;
     494        }
     495        rc = async_data_write_accept((void **) &devname, true, 0, 0, 0, 0);
     496        if (rc != EOK) {
     497                free(classname);
     498                async_answer_0(iid, rc);
     499                return;
     500        }
     501
     502
     503        fun_node_t *fun = find_fun_node_by_class(&class_list,
     504            classname, devname);
     505
     506        free(classname);
     507        free(devname);
     508
     509        if (fun == NULL) {
     510                async_answer_0(iid, ENOENT);
     511                return;
     512        }
     513
     514        async_answer_1(iid, EOK, fun->handle);
     515}
     516
    440517
    441518/** Function for handling connections from a client to the device manager. */
     
    457534                        devman_function_get_handle(callid, &call);
    458535                        break;
     536                case DEVMAN_DEVICE_GET_HANDLE_BY_CLASS:
     537                        devman_function_get_handle_by_class(callid, &call);
     538                        break;
    459539                default:
    460540                        async_answer_0(callid, ENOENT);
     
    477557                dev = fun->dev;
    478558
    479         if (fun == NULL && dev == NULL) {
    480                 printf(NAME ": devman_forward error - no device or function with "
    481                     "handle %" PRIun " was found.\n", handle);
     559        /*
     560         * For a valid function to connect to we need a device. The root
     561         * function, for example, has no device and cannot be connected to.
     562         * This means @c dev needs to be valid regardless whether we are
     563         * connecting to a device or to a function.
     564         */
     565        if (dev == NULL) {
     566                log_msg(LVL_ERROR, "IPC forwarding failed - no device or "
     567                    "function with handle %" PRIun " was found.", handle);
    482568                async_answer_0(iid, ENOENT);
    483569                return;
     
    485571
    486572        if (fun == NULL && !drv_to_parent) {
    487                 printf(NAME ": devman_forward error - cannot connect to "
    488                     "handle %" PRIun ", refers to a device.\n", handle);
     573                log_msg(LVL_ERROR, NAME ": devman_forward error - cannot "
     574                    "connect to handle %" PRIun ", refers to a device.",
     575                    handle);
    489576                async_answer_0(iid, ENOENT);
    490577                return;
     
    507594       
    508595        if (driver == NULL) {
    509                 printf(NAME ": devman_forward error - the device is not in %" PRIun
    510                     " usable state.\n", handle);
     596                log_msg(LVL_ERROR, "IPC forwarding refused - " \
     597                    "the device %" PRIun " is not in usable state.", handle);
    511598                async_answer_0(iid, ENOENT);
    512599                return;
     
    519606                method = DRIVER_CLIENT;
    520607       
    521         if (driver->phone <= 0) {
    522                 printf(NAME ": devman_forward: cound not forward to driver %s ",
    523                     driver->name);
    524                 printf("the driver's phone is %" PRIun ").\n", driver->phone);
     608        if (driver->phone < 0) {
     609                log_msg(LVL_ERROR,
     610                    "Could not forward to driver `%s' (phone is %d).",
     611                    driver->name, (int) driver->phone);
    525612                async_answer_0(iid, EINVAL);
    526613                return;
     
    528615
    529616        if (fun != NULL) {
    530                 printf(NAME ": devman_forward: forward connection to function %s to "
    531                     "driver %s.\n", fun->pathname, driver->name);
     617                log_msg(LVL_DEBUG,
     618                    "Forwarding request for `%s' function to driver `%s'.",
     619                    fun->pathname, driver->name);
    532620        } else {
    533                 printf(NAME ": devman_forward: forward connection to device %s to "
    534                     "driver %s.\n", dev->pfun->pathname, driver->name);
     621                log_msg(LVL_DEBUG,
     622                    "Forwarding request for `%s' device to driver `%s'.",
     623                    dev->pfun->pathname, driver->name);
    535624        }
    536625
     
    557646        dev = fun->dev;
    558647       
    559         if (dev->state != DEVICE_USABLE || dev->drv->phone <= 0) {
     648        if (dev->state != DEVICE_USABLE || dev->drv->phone < 0) {
    560649                async_answer_0(iid, EINVAL);
    561650                return;
     
    564653        async_forward_fast(iid, dev->drv->phone, DRIVER_CLIENT, fun->handle, 0,
    565654            IPC_FF_NONE);
    566         printf(NAME ": devman_connection_devmapper: forwarded connection to "
    567             "device %s to driver %s.\n", fun->pathname, dev->drv->name);
     655        log_msg(LVL_DEBUG,
     656            "Forwarding devmapper request for `%s' function to driver `%s'.",
     657            fun->pathname, dev->drv->name);
    568658}
    569659
     
    600690static bool devman_init(void)
    601691{
    602         printf(NAME ": devman_init - looking for available drivers.\n");
     692        log_msg(LVL_DEBUG, "devman_init - looking for available drivers.");
    603693       
    604694        /* Initialize list of available drivers. */
     
    606696        if (lookup_available_drivers(&drivers_list,
    607697            DRIVER_DEFAULT_STORE) == 0) {
    608                 printf(NAME " no drivers found.");
     698                log_msg(LVL_FATAL, "No drivers found.");
    609699                return false;
    610700        }
    611701
    612         printf(NAME ": devman_init  - list of drivers has been initialized.\n");
     702        log_msg(LVL_DEBUG, "devman_init - list of drivers has been initialized.");
    613703
    614704        /* Create root device node. */
    615705        if (!init_device_tree(&device_tree, &drivers_list)) {
    616                 printf(NAME " failed to initialize device tree.");
     706                log_msg(LVL_FATAL, "Failed to initialize device tree.");
    617707                return false;
    618708        }
     
    635725        printf(NAME ": HelenOS Device Manager\n");
    636726
     727        if (log_init(NAME, LVL_ERROR) != EOK) {
     728                printf(NAME ": Error initializing logging subsystem.\n");
     729                return -1;
     730        }
     731
    637732        if (!devman_init()) {
    638                 printf(NAME ": Error while initializing service\n");
     733                log_msg(LVL_ERROR, "Error while initializing service.");
    639734                return -1;
    640735        }
     
    644739
    645740        /* Register device manager at naming service. */
    646         if (service_register(SERVICE_DEVMAN) != EOK)
     741        if (service_register(SERVICE_DEVMAN) != EOK) {
     742                log_msg(LVL_ERROR, "Failed registering as a service.");
    647743                return -1;
    648 
    649         printf(NAME ": Accepting connections\n");
     744        }
     745
     746        printf(NAME ": Accepting connections.\n");
    650747        async_manager();
    651748
Note: See TracChangeset for help on using the changeset viewer.