Ignore:
File:
1 edited

Legend:

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

    r7e752b2 rca2a18e  
    4646#include <str.h>
    4747#include <ipc/devmap.h>
     48#include <assert.h>
    4849
    4950#define NAME          "devmap"
     
    6162        link_t devices;
    6263        /** Phone asociated with this driver */
    63         ipcarg_t phone;
     64        sysarg_t phone;
    6465        /** Device driver name */
    6566        char *name;
     
    99100        /** Device driver handling this device */
    100101        devmap_driver_t *driver;
     102        /** Use this interface when forwarding to driver. */
     103        sysarg_t forward_interface;
    101104} devmap_device_t;
    102105
     
    206209}
    207210
    208 /** Find namespace with given name.
    209  *
    210  * The devices_list_mutex should be already held when
    211  * calling this function.
    212  *
    213  */
     211/** Find namespace with given name. */
    214212static devmap_namespace_t *devmap_namespace_find_name(const char *name)
    215213{
    216214        link_t *item;
     215       
     216        assert(fibril_mutex_is_locked(&devices_list_mutex));
     217       
    217218        for (item = namespaces_list.next; item != &namespaces_list; item = item->next) {
    218219                devmap_namespace_t *namespace =
     
    227228/** Find namespace with given handle.
    228229 *
    229  * The devices_list_mutex should be already held when
    230  * calling this function.
    231  *
    232230 * @todo: use hash table
    233231 *
     
    236234{
    237235        link_t *item;
     236       
     237        assert(fibril_mutex_is_locked(&devices_list_mutex));
     238       
    238239        for (item = namespaces_list.next; item != &namespaces_list; item = item->next) {
    239240                devmap_namespace_t *namespace =
     
    246247}
    247248
    248 /** Find device with given name.
    249  *
    250  * The devices_list_mutex should be already held when
    251  * calling this function.
    252  *
    253  */
     249/** Find device with given name. */
    254250static devmap_device_t *devmap_device_find_name(const char *ns_name,
    255251    const char *name)
    256252{
    257253        link_t *item;
     254       
     255        assert(fibril_mutex_is_locked(&devices_list_mutex));
     256       
    258257        for (item = devices_list.next; item != &devices_list; item = item->next) {
    259258                devmap_device_t *device =
     
    269268/** Find device with given handle.
    270269 *
    271  * The devices_list_mutex should be already held when
    272  * calling this function.
    273  *
    274270 * @todo: use hash table
    275271 *
     
    278274{
    279275        link_t *item;
     276       
     277        assert(fibril_mutex_is_locked(&devices_list_mutex));
     278       
    280279        for (item = devices_list.next; item != &devices_list; item = item->next) {
    281280                devmap_device_t *device =
     
    288287}
    289288
    290 /** Create a namespace (if not already present)
    291  *
    292  * The devices_list_mutex should be already held when
    293  * calling this function.
    294  *
    295  */
     289/** Create a namespace (if not already present). */
    296290static devmap_namespace_t *devmap_namespace_create(const char *ns_name)
    297291{
    298         devmap_namespace_t *namespace = devmap_namespace_find_name(ns_name);
     292        devmap_namespace_t *namespace;
     293       
     294        assert(fibril_mutex_is_locked(&devices_list_mutex));
     295       
     296        namespace = devmap_namespace_find_name(ns_name);
    299297        if (namespace != NULL)
    300298                return namespace;
     
    321319}
    322320
    323 /** Destroy a namespace (if it is no longer needed)
    324  *
    325  * The devices_list_mutex should be already held when
    326  * calling this function.
    327  *
    328  */
     321/** Destroy a namespace (if it is no longer needed). */
    329322static void devmap_namespace_destroy(devmap_namespace_t *namespace)
    330323{
     324        assert(fibril_mutex_is_locked(&devices_list_mutex));
     325
    331326        if (namespace->refcnt == 0) {
    332327                list_remove(&(namespace->namespaces));
     
    337332}
    338333
    339 /** Increase namespace reference count by including device
    340  *
    341  * The devices_list_mutex should be already held when
    342  * calling this function.
    343  *
    344  */
     334/** Increase namespace reference count by including device. */
    345335static void devmap_namespace_addref(devmap_namespace_t *namespace,
    346336    devmap_device_t *device)
    347337{
     338        assert(fibril_mutex_is_locked(&devices_list_mutex));
     339
    348340        device->namespace = namespace;
    349341        namespace->refcnt++;
    350342}
    351343
    352 /** Decrease namespace reference count
    353  *
    354  * The devices_list_mutex should be already held when
    355  * calling this function.
    356  *
    357  */
     344/** Decrease namespace reference count. */
    358345static void devmap_namespace_delref(devmap_namespace_t *namespace)
    359346{
     347        assert(fibril_mutex_is_locked(&devices_list_mutex));
     348
    360349        namespace->refcnt--;
    361350        devmap_namespace_destroy(namespace);
    362351}
    363352
    364 /** Unregister device and free it
    365  *
    366  * The devices_list_mutex should be already held when
    367  * calling this function.
    368  *
    369  */
     353/** Unregister device and free it. */
    370354static void devmap_device_unregister_core(devmap_device_t *device)
    371355{
     356        assert(fibril_mutex_is_locked(&devices_list_mutex));
     357
    372358        devmap_namespace_delref(device->namespace);
    373359        list_remove(&(device->devices));
     
    387373        ipc_callid_t iid = async_get_call(&icall);
    388374       
    389         if (IPC_GET_METHOD(icall) != DEVMAP_DRIVER_REGISTER) {
    390                 ipc_answer_0(iid, EREFUSED);
     375        if (IPC_GET_IMETHOD(icall) != DEVMAP_DRIVER_REGISTER) {
     376                async_answer_0(iid, EREFUSED);
    391377                return NULL;
    392378        }
     
    395381            (devmap_driver_t *) malloc(sizeof(devmap_driver_t));
    396382        if (driver == NULL) {
    397                 ipc_answer_0(iid, ENOMEM);
     383                async_answer_0(iid, ENOMEM);
    398384                return NULL;
    399385        }
     
    406392        if (rc != EOK) {
    407393                free(driver);
    408                 ipc_answer_0(iid, rc);
     394                async_answer_0(iid, rc);
    409395                return NULL;
    410396        }
     
    416402        ipc_callid_t callid = async_get_call(&call);
    417403       
    418         if (IPC_GET_METHOD(call) != IPC_M_CONNECT_TO_ME) {
     404        if (IPC_GET_IMETHOD(call) != IPC_M_CONNECT_TO_ME) {
    419405                free(driver->name);
    420406                free(driver);
    421                 ipc_answer_0(callid, ENOTSUP);
    422                 ipc_answer_0(iid, ENOTSUP);
     407                async_answer_0(callid, ENOTSUP);
     408                async_answer_0(iid, ENOTSUP);
    423409                return NULL;
    424410        }
    425411       
    426412        driver->phone = IPC_GET_ARG5(call);
    427         ipc_answer_0(callid, EOK);
     413        async_answer_0(callid, EOK);
    428414       
    429415        /*
     
    437423         */
    438424        list_initialize(&driver->devices);
    439         list_initialize(&(driver->drivers));
     425
     426        link_initialize(&driver->drivers);
    440427       
    441428        fibril_mutex_lock(&drivers_list_mutex);
     
    452439        fibril_mutex_unlock(&drivers_list_mutex);
    453440       
    454         ipc_answer_0(iid, EOK);
     441        async_answer_0(iid, EOK);
    455442       
    456443        return driver;
     
    470457       
    471458        if (driver->phone != 0)
    472                 ipc_hangup(driver->phone);
     459                async_hangup(driver->phone);
    473460       
    474461        /* Remove it from list of drivers */
     
    505492{
    506493        if (driver == NULL) {
    507                 ipc_answer_0(iid, EREFUSED);
     494                async_answer_0(iid, EREFUSED);
    508495                return;
    509496        }
     
    513500            (devmap_device_t *) malloc(sizeof(devmap_device_t));
    514501        if (device == NULL) {
    515                 ipc_answer_0(iid, ENOMEM);
    516                 return;
    517         }
    518        
     502                async_answer_0(iid, ENOMEM);
     503                return;
     504        }
     505       
     506        /* Set the interface, if any. */
     507        device->forward_interface = IPC_GET_ARG1(*icall);
     508
    519509        /* Get fqdn */
    520510        char *fqdn;
     
    523513        if (rc != EOK) {
    524514                free(device);
    525                 ipc_answer_0(iid, rc);
     515                async_answer_0(iid, rc);
    526516                return;
    527517        }
     
    531521                free(fqdn);
    532522                free(device);
    533                 ipc_answer_0(iid, EINVAL);
     523                async_answer_0(iid, EINVAL);
    534524                return;
    535525        }
     
    545535                free(device->name);
    546536                free(device);
    547                 ipc_answer_0(iid, ENOMEM);
    548                 return;
    549         }
    550        
    551         list_initialize(&(device->devices));
    552         list_initialize(&(device->driver_devices));
     537                async_answer_0(iid, ENOMEM);
     538                return;
     539        }
     540       
     541        link_initialize(&device->devices);
     542        link_initialize(&device->driver_devices);
    553543       
    554544        /* Check that device is not already registered */
     
    560550                free(device->name);
    561551                free(device);
    562                 ipc_answer_0(iid, EEXISTS);
     552                async_answer_0(iid, EEXISTS);
    563553                return;
    564554        }
     
    566556        /* Get unique device handle */
    567557        device->handle = devmap_create_handle();
    568        
     558
    569559        devmap_namespace_addref(namespace, device);
    570560        device->driver = driver;
     
    582572        fibril_mutex_unlock(&devices_list_mutex);
    583573       
    584         ipc_answer_1(iid, EOK, device->handle);
     574        async_answer_1(iid, EOK, device->handle);
    585575}
    586576
     
    613603        if ((dev == NULL) || (dev->driver == NULL) || (dev->driver->phone == 0)) {
    614604                fibril_mutex_unlock(&devices_list_mutex);
    615                 ipc_answer_0(callid, ENOENT);
    616                 return;
    617         }
    618        
    619         ipc_forward_fast(callid, dev->driver->phone, dev->handle,
    620             IPC_GET_ARG3(*call), 0, IPC_FF_NONE);
     605                async_answer_0(callid, ENOENT);
     606                return;
     607        }
     608       
     609        if (dev->forward_interface == 0) {
     610                async_forward_fast(callid, dev->driver->phone,
     611                    dev->handle, 0, 0,
     612                    IPC_FF_NONE);
     613        } else {
     614                async_forward_fast(callid, dev->driver->phone,
     615                    dev->forward_interface, dev->handle, 0,
     616                    IPC_FF_NONE);
     617        }
    621618       
    622619        fibril_mutex_unlock(&devices_list_mutex);
     
    637634            DEVMAP_NAME_MAXLEN, 0, NULL);
    638635        if (rc != EOK) {
    639                 ipc_answer_0(iid, rc);
     636                async_answer_0(iid, rc);
    640637                return;
    641638        }
     
    645642        if (!devmap_fqdn_split(fqdn, &ns_name, &name)) {
    646643                free(fqdn);
    647                 ipc_answer_0(iid, EINVAL);
     644                async_answer_0(iid, EINVAL);
    648645                return;
    649646        }
     
    672669                }
    673670               
    674                 ipc_answer_0(iid, ENOENT);
     671                async_answer_0(iid, ENOENT);
    675672                free(ns_name);
    676673                free(name);
     
    679676        }
    680677       
    681         ipc_answer_1(iid, EOK, dev->handle);
     678        async_answer_1(iid, EOK, dev->handle);
    682679       
    683680        fibril_mutex_unlock(&devices_list_mutex);
     
    700697            DEVMAP_NAME_MAXLEN, 0, NULL);
    701698        if (rc != EOK) {
    702                 ipc_answer_0(iid, rc);
     699                async_answer_0(iid, rc);
    703700                return;
    704701        }
     
    725722                }
    726723               
    727                 ipc_answer_0(iid, ENOENT);
     724                async_answer_0(iid, ENOENT);
    728725                free(name);
    729726                fibril_mutex_unlock(&devices_list_mutex);
     
    731728        }
    732729       
    733         ipc_answer_1(iid, EOK, namespace->handle);
     730        async_answer_1(iid, EOK, namespace->handle);
    734731       
    735732        fibril_mutex_unlock(&devices_list_mutex);
     
    747744                    devmap_device_find_handle(IPC_GET_ARG1(*icall));
    748745                if (dev == NULL)
    749                         ipc_answer_1(iid, EOK, DEV_HANDLE_NONE);
     746                        async_answer_1(iid, EOK, DEV_HANDLE_NONE);
    750747                else
    751                         ipc_answer_1(iid, EOK, DEV_HANDLE_DEVICE);
     748                        async_answer_1(iid, EOK, DEV_HANDLE_DEVICE);
    752749        } else
    753                 ipc_answer_1(iid, EOK, DEV_HANDLE_NAMESPACE);
     750                async_answer_1(iid, EOK, DEV_HANDLE_NAMESPACE);
    754751       
    755752        fibril_mutex_unlock(&devices_list_mutex);
     
    759756{
    760757        fibril_mutex_lock(&devices_list_mutex);
    761         ipc_answer_1(iid, EOK, list_count(&namespaces_list));
     758        async_answer_1(iid, EOK, list_count(&namespaces_list));
    762759        fibril_mutex_unlock(&devices_list_mutex);
    763760}
     
    770767            devmap_namespace_find_handle(IPC_GET_ARG1(*icall));
    771768        if (namespace == NULL)
    772                 ipc_answer_0(iid, EEXISTS);
     769                async_answer_0(iid, EEXISTS);
    773770        else
    774                 ipc_answer_1(iid, EOK, namespace->refcnt);
     771                async_answer_1(iid, EOK, namespace->refcnt);
    775772       
    776773        fibril_mutex_unlock(&devices_list_mutex);
     
    782779        size_t size;
    783780        if (!async_data_read_receive(&callid, &size)) {
    784                 ipc_answer_0(callid, EREFUSED);
    785                 ipc_answer_0(iid, EREFUSED);
     781                async_answer_0(callid, EREFUSED);
     782                async_answer_0(iid, EREFUSED);
    786783                return;
    787784        }
    788785       
    789786        if ((size % sizeof(dev_desc_t)) != 0) {
    790                 ipc_answer_0(callid, EINVAL);
    791                 ipc_answer_0(iid, EINVAL);
     787                async_answer_0(callid, EINVAL);
     788                async_answer_0(iid, EINVAL);
    792789                return;
    793790        }
     
    798795        if (count != list_count(&namespaces_list)) {
    799796                fibril_mutex_unlock(&devices_list_mutex);
    800                 ipc_answer_0(callid, EOVERFLOW);
    801                 ipc_answer_0(iid, EOVERFLOW);
     797                async_answer_0(callid, EOVERFLOW);
     798                async_answer_0(iid, EOVERFLOW);
    802799                return;
    803800        }
     
    806803        if (desc == NULL) {
    807804                fibril_mutex_unlock(&devices_list_mutex);
    808                 ipc_answer_0(callid, ENOMEM);
    809                 ipc_answer_0(iid, ENOMEM);
     805                async_answer_0(callid, ENOMEM);
     806                async_answer_0(iid, ENOMEM);
    810807                return;
    811808        }
     
    823820        }
    824821       
    825         ipcarg_t retval = async_data_read_finalize(callid, desc, size);
     822        sysarg_t retval = async_data_read_finalize(callid, desc, size);
    826823       
    827824        free(desc);
    828825        fibril_mutex_unlock(&devices_list_mutex);
    829826       
    830         ipc_answer_0(iid, retval);
     827        async_answer_0(iid, retval);
    831828}
    832829
     
    839836        size_t size;
    840837        if (!async_data_read_receive(&callid, &size)) {
    841                 ipc_answer_0(callid, EREFUSED);
    842                 ipc_answer_0(iid, EREFUSED);
     838                async_answer_0(callid, EREFUSED);
     839                async_answer_0(iid, EREFUSED);
    843840                return;
    844841        }
    845842       
    846843        if ((size % sizeof(dev_desc_t)) != 0) {
    847                 ipc_answer_0(callid, EINVAL);
    848                 ipc_answer_0(iid, EINVAL);
     844                async_answer_0(callid, EINVAL);
     845                async_answer_0(iid, EINVAL);
    849846                return;
    850847        }
     
    856853        if (namespace == NULL) {
    857854                fibril_mutex_unlock(&devices_list_mutex);
    858                 ipc_answer_0(callid, ENOENT);
    859                 ipc_answer_0(iid, ENOENT);
     855                async_answer_0(callid, ENOENT);
     856                async_answer_0(iid, ENOENT);
    860857                return;
    861858        }
     
    864861        if (count != namespace->refcnt) {
    865862                fibril_mutex_unlock(&devices_list_mutex);
    866                 ipc_answer_0(callid, EOVERFLOW);
    867                 ipc_answer_0(iid, EOVERFLOW);
     863                async_answer_0(callid, EOVERFLOW);
     864                async_answer_0(iid, EOVERFLOW);
    868865                return;
    869866        }
     
    872869        if (desc == NULL) {
    873870                fibril_mutex_unlock(&devices_list_mutex);
    874                 ipc_answer_0(callid, ENOMEM);
    875                 ipc_answer_0(iid, EREFUSED);
     871                async_answer_0(callid, ENOMEM);
     872                async_answer_0(iid, EREFUSED);
    876873                return;
    877874        }
     
    890887        }
    891888       
    892         ipcarg_t retval = async_data_read_finalize(callid, desc, size);
     889        sysarg_t retval = async_data_read_finalize(callid, desc, size);
    893890       
    894891        free(desc);
    895892        fibril_mutex_unlock(&devices_list_mutex);
    896893       
    897         ipc_answer_0(iid, retval);
     894        async_answer_0(iid, retval);
    898895}
    899896
     
    914911        if (!fnd) {
    915912                fibril_mutex_unlock(&null_devices_mutex);
    916                 ipc_answer_0(iid, ENOMEM);
     913                async_answer_0(iid, ENOMEM);
    917914                return;
    918915        }
     
    924921        if (dev_name == NULL) {
    925922                fibril_mutex_unlock(&null_devices_mutex);
    926                 ipc_answer_0(iid, ENOMEM);
     923                async_answer_0(iid, ENOMEM);
    927924                return;
    928925        }
     
    932929        if (device == NULL) {
    933930                fibril_mutex_unlock(&null_devices_mutex);
    934                 ipc_answer_0(iid, ENOMEM);
     931                async_answer_0(iid, ENOMEM);
    935932                return;
    936933        }
     
    942939                fibril_mutex_lock(&devices_list_mutex);
    943940                fibril_mutex_unlock(&null_devices_mutex);
    944                 ipc_answer_0(iid, ENOMEM);
    945                 return;
    946         }
    947        
    948         list_initialize(&(device->devices));
    949         list_initialize(&(device->driver_devices));
     941                async_answer_0(iid, ENOMEM);
     942                return;
     943        }
     944       
     945        link_initialize(&device->devices);
     946        link_initialize(&device->driver_devices);
    950947       
    951948        /* Get unique device handle */
     
    964961        fibril_mutex_unlock(&null_devices_mutex);
    965962       
    966         ipc_answer_1(iid, EOK, (ipcarg_t) i);
     963        async_answer_1(iid, EOK, (sysarg_t) i);
    967964}
    968965
    969966static void devmap_null_destroy(ipc_callid_t iid, ipc_call_t *icall)
    970967{
    971         ipcarg_t i = IPC_GET_ARG1(*icall);
     968        sysarg_t i = IPC_GET_ARG1(*icall);
    972969        if (i >= NULL_DEVICES) {
    973                 ipc_answer_0(iid, ELIMIT);
     970                async_answer_0(iid, ELIMIT);
    974971                return;
    975972        }
     
    979976        if (null_devices[i] == NULL) {
    980977                fibril_mutex_unlock(&null_devices_mutex);
    981                 ipc_answer_0(iid, ENOENT);
     978                async_answer_0(iid, ENOENT);
    982979                return;
    983980        }
     
    990987       
    991988        fibril_mutex_unlock(&null_devices_mutex);
    992         ipc_answer_0(iid, EOK);
     989        async_answer_0(iid, EOK);
    993990}
    994991
     
    10161013{
    10171014        /* Accept connection */
    1018         ipc_answer_0(iid, EOK);
     1015        async_answer_0(iid, EOK);
    10191016       
    10201017        devmap_driver_t *driver = devmap_driver_register();
     
    10271024                ipc_callid_t callid = async_get_call(&call);
    10281025               
    1029                 switch (IPC_GET_METHOD(call)) {
     1026                switch (IPC_GET_IMETHOD(call)) {
    10301027                case IPC_M_PHONE_HUNGUP:
    10311028                        cont = false;
     
    10331030                case DEVMAP_DRIVER_UNREGISTER:
    10341031                        if (NULL == driver)
    1035                                 ipc_answer_0(callid, ENOENT);
     1032                                async_answer_0(callid, ENOENT);
    10361033                        else
    1037                                 ipc_answer_0(callid, EOK);
     1034                                async_answer_0(callid, EOK);
    10381035                        break;
    10391036                case DEVMAP_DEVICE_REGISTER:
     
    10521049                        break;
    10531050                default:
    1054                         ipc_answer_0(callid, ENOENT);
     1051                        async_answer_0(callid, ENOENT);
    10551052                }
    10561053        }
     
    10711068{
    10721069        /* Accept connection */
    1073         ipc_answer_0(iid, EOK);
     1070        async_answer_0(iid, EOK);
    10741071       
    10751072        bool cont = true;
     
    10781075                ipc_callid_t callid = async_get_call(&call);
    10791076               
    1080                 switch (IPC_GET_METHOD(call)) {
     1077                switch (IPC_GET_IMETHOD(call)) {
    10811078                case IPC_M_PHONE_HUNGUP:
    10821079                        cont = false;
     
    11101107                        break;
    11111108                default:
    1112                         ipc_answer_0(callid, ENOENT);
     1109                        async_answer_0(callid, ENOENT);
    11131110                }
    11141111        }
     
    11211118{
    11221119        /* Select interface */
    1123         switch ((ipcarg_t) (IPC_GET_ARG1(*icall))) {
     1120        switch ((sysarg_t) (IPC_GET_ARG1(*icall))) {
    11241121        case DEVMAP_DRIVER:
    11251122                devmap_connection_driver(iid, icall);
     
    11341131        default:
    11351132                /* No such interface */
    1136                 ipc_answer_0(iid, ENOENT);
     1133                async_answer_0(iid, ENOENT);
    11371134        }
    11381135}
     
    11541151       
    11551152        /* Register device mapper at naming service */
    1156         ipcarg_t phonead;
    1157         if (ipc_connect_to_me(PHONE_NS, SERVICE_DEVMAP, 0, 0, &phonead) != 0)
     1153        if (service_register(SERVICE_DEVMAP) != EOK)
    11581154                return -1;
    11591155       
Note: See TracChangeset for help on using the changeset viewer.