Changeset 38b3baf in mainline for uspace/srv/devman/main.c


Ignore:
Timestamp:
2010-10-23T07:16:14Z (14 years ago)
Author:
Jakub Jermar <jakub@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
7e66a5ec
Parents:
032e0bb
Message:

Cstyle fixes of devman.

File:
1 edited

Legend:

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

    r032e0bb r38b3baf  
    6363static class_list_t class_list;
    6464
    65 /**
    66  * Register running driver.
    67  */
    68 static driver_t * devman_driver_register(void)
    69 {       
     65/** Register running driver. */
     66static driver_t *devman_driver_register(void)
     67{
    7068        printf(NAME ": devman_driver_register \n");
    7169       
     
    8179        char *drv_name = NULL;
    8280       
    83         // Get driver name
    84         int rc = async_data_write_accept((void **)&drv_name, true, 0, 0, 0, 0);
     81        /* Get driver name. */
     82        int rc = async_data_write_accept((void **) &drv_name, true, 0, 0, 0, 0);
    8583        if (rc != EOK) {
    8684                ipc_answer_0(iid, rc);
    8785                return NULL;
    8886        }
    89         printf(NAME ": the %s driver is trying to register by the service.\n", drv_name);
    90        
    91         // Find driver structure
     87        printf(NAME ": the %s driver is trying to register by the service.\n",
     88            drv_name);
     89       
     90        /* Find driver structure. */
    9291        driver = find_driver(&drivers_list, drv_name);
    9392       
     
    103102        drv_name = NULL;
    104103       
    105         // Create connection to the driver
    106         printf(NAME ":  creating connection to the %s driver.\n", driver->name);       
     104        /* Create connection to the driver. */
     105        printf(NAME ":  creating connection to the %s driver.\n", driver->name);
    107106        ipc_call_t call;
    108         ipc_callid_t callid = async_get_call(&call);           
     107        ipc_callid_t callid = async_get_call(&call);
    109108        if (IPC_GET_METHOD(call) != IPC_M_CONNECT_TO_ME) {
    110109                ipc_answer_0(callid, ENOTSUP);
     
    113112        }
    114113       
    115         // remember driver's phone
     114        /* Remember driver's phone. */
    116115        set_driver_phone(driver, IPC_GET_ARG5(call));
    117116       
    118         printf(NAME ":  the %s driver was successfully registered as running.\n", driver->name);
    119        
    120         ipc_answer_0(callid, EOK);     
    121        
     117        printf(NAME ": the %s driver was successfully registered as running.\n",
     118            driver->name);
     119       
     120        ipc_answer_0(callid, EOK);
    122121        ipc_answer_0(iid, EOK);
    123122       
     
    125124}
    126125
    127 /**
    128  * Receive device match ID from the device's parent driver and add it to the list of devices match ids.
    129  *
    130  * @param match_ids the list of the device's match ids.
    131  *
    132  * @return 0 on success, negative error code otherwise.
    133  */
    134 static int devman_receive_match_id(match_id_list_t *match_ids) {
    135        
     126/** Receive device match ID from the device's parent driver and add it to the
     127 * list of devices match ids.
     128 *
     129 * @param match_ids     The list of the device's match ids.
     130 * @return              Zero on success, negative error code otherwise.
     131 */
     132static int devman_receive_match_id(match_id_list_t *match_ids)
     133{
    136134        match_id_t *match_id = create_match_id();
    137135        ipc_callid_t callid;
     
    141139        callid = async_get_call(&call);
    142140        if (DEVMAN_ADD_MATCH_ID != IPC_GET_METHOD(call)) {
    143                 printf(NAME ": ERROR: devman_receive_match_id - invalid protocol.\n");
     141                printf(NAME ": ERROR: devman_receive_match_id - invalid "
     142                    "protocol.\n");
    144143                ipc_answer_0(callid, EINVAL);
    145144                delete_match_id(match_id);
     
    148147       
    149148        if (NULL == match_id) {
    150                 printf(NAME ": ERROR: devman_receive_match_id - failed to allocate match id.\n");
     149                printf(NAME ": ERROR: devman_receive_match_id - failed to "
     150                    "allocate match id.\n");
    151151                ipc_answer_0(callid, ENOMEM);
    152152                return ENOMEM;
     
    158158       
    159159        char *match_id_str;
    160         rc = async_data_write_accept((void **)&match_id_str, true, 0, 0, 0, 0);
     160        rc = async_data_write_accept((void **) &match_id_str, true, 0, 0, 0, 0);
    161161        match_id->id = match_id_str;
    162162        if (EOK != rc) {
    163163                delete_match_id(match_id);
    164                 printf(NAME ": devman_receive_match_id - failed to receive match id string.\n");
     164                printf(NAME ": devman_receive_match_id - failed to receive "
     165                    "match id string.\n");
    165166                return rc;
    166167        }
     
    168169        list_append(&match_id->link, &match_ids->ids);
    169170       
    170         printf(NAME ": received match id '%s', score = %d \n", match_id->id, match_id->score);
     171        printf(NAME ": received match id '%s', score = %d \n",
     172            match_id->id, match_id->score);
    171173        return rc;
    172174}
    173175
    174 /**
    175  * Receive device match IDs from the device's parent driver
    176  * and add them to the list of devices match ids.
    177  *
    178  * @param match_count the number of device's match ids to be received.
    179  * @param match_ids the list of the device's match ids.
    180  *
    181  * @return 0 on success, negative error code otherwise.
    182  */
    183 static int devman_receive_match_ids(ipcarg_t match_count, match_id_list_t *match_ids)
    184 {       
     176/** Receive device match IDs from the device's parent driver and add them to the
     177 * list of devices match ids.
     178 *
     179 * @param match_count   The number of device's match ids to be received.
     180 * @param match_ids     The list of the device's match ids.
     181 * @return              Zero on success, negative error code otherwise.
     182 */
     183static int
     184devman_receive_match_ids(ipcarg_t match_count, match_id_list_t *match_ids)
     185{
    185186        int ret = EOK;
    186187        size_t i;
     188       
    187189        for (i = 0; i < match_count; i++) {
    188                 if (EOK != (ret = devman_receive_match_id(match_ids))) {
     190                if (EOK != (ret = devman_receive_match_id(match_ids)))
    189191                        return ret;
    190                 }
    191192        }
    192193        return ret;
    193194}
    194195
    195 /** Handle child device registration. 
    196  * 
     196/** Handle child device registration.
     197 *
    197198 * Child devices are registered by their parent's device driver.
    198199 */
    199200static void devman_add_child(ipc_callid_t callid, ipc_call_t *call)
    200201{
    201         //printf(NAME ": devman_add_child\n");
    202        
    203202        device_handle_t parent_handle = IPC_GET_ARG1(*call);
    204203        ipcarg_t match_count = IPC_GET_ARG2(*call);
     
    215214       
    216215        char *dev_name = NULL;
    217         int rc = async_data_write_accept((void **)&dev_name, true, 0, 0, 0, 0); 
     216        int rc = async_data_write_accept((void **)&dev_name, true, 0, 0, 0, 0);
    218217        if (EOK != rc) {
    219218                fibril_rwlock_write_unlock(&tree->rwlock);
     
    221220                return;
    222221        }
    223         //printf(NAME ": newly added child device's name is '%s'.\n", dev_name);
    224222       
    225223        node_t *node = create_dev_node();
     
    236234        devman_receive_match_ids(match_count, &node->match_ids);
    237235       
    238         // return device handle to parent's driver
     236        /* Return device handle to parent's driver. */
    239237        ipc_answer_1(callid, EOK, node->handle);
    240238       
    241         // try to find suitable driver and assign it to the device
    242         assign_driver(node, &drivers_list, &device_tree);       
     239        /* Try to find suitable driver and assign it to the device. */
     240        assign_driver(node, &drivers_list, &device_tree);
    243241}
    244242
    245243static void devmap_register_class_dev(dev_class_info_t *cli)
    246244{
    247         // create devmap path and name for the device
     245        /* Create devmap path and name for the device. */
    248246        char *devmap_pathname = NULL;
    249         asprintf(&devmap_pathname, "%s/%s%c%s", DEVMAP_CLASS_NAMESPACE, cli->dev_class->name, DEVMAP_SEPARATOR, cli->dev_name);
    250         if (NULL == devmap_pathname) {
    251                 return;
    252         }
    253        
    254         // register the device by the device mapper and remember its devmap handle
    255         devmap_device_register(devmap_pathname, &cli->devmap_handle);   
    256        
    257         // add device to the hash map of class devices registered by device mapper
     247        asprintf(&devmap_pathname, "%s/%s%c%s", DEVMAP_CLASS_NAMESPACE,
     248            cli->dev_class->name, DEVMAP_SEPARATOR, cli->dev_name);
     249        if (NULL == devmap_pathname)
     250                return;
     251       
     252        /*
     253         * Register the device by the device mapper and remember its devmap
     254         * handle.
     255         */
     256        devmap_device_register(devmap_pathname, &cli->devmap_handle);
     257       
     258        /*
     259         * Add device to the hash map of class devices registered by device
     260         * mapper.
     261         */
    258262        class_add_devmap_device(&class_list, cli);
    259263       
    260         free(devmap_pathname); 
     264        free(devmap_pathname);
    261265}
    262266
     
    265269        device_handle_t handle = IPC_GET_ARG1(*call);
    266270       
    267         // Get class name
     271        /* Get class name. */
    268272        char *class_name;
    269         int rc = async_data_write_accept((void **)&class_name, true, 0, 0, 0, 0);
     273        int rc = async_data_write_accept((void **) &class_name, true,
     274            0, 0, 0, 0);
    270275        if (rc != EOK) {
    271276                ipc_answer_0(callid, rc);
     
    280285       
    281286        dev_class_t *cl = get_dev_class(&class_list, class_name);
    282                
    283287        dev_class_info_t *class_info = add_device_to_class(dev, cl, NULL);
    284288       
    285         // register the device's class alias by devmapper
     289        /* Register the device's class alias by devmapper. */
    286290        devmap_register_class_dev(class_info);
    287291       
    288         printf(NAME ": device '%s' added to class '%s', class name '%s' was asigned to it\n", dev->pathname, class_name, class_info->dev_name);
    289        
    290         ipc_answer_0(callid, EOK);     
    291 }
    292 
    293 /**
    294  * Initialize driver which has registered itself as running and ready.
    295  * 
    296  * The initialization is done in a separate fibril to avoid deadlocks
    297  * (if the driver needed to be served by devman during the driver's initialization).
     292        printf(NAME ": device '%s' added to class '%s', class name '%s' was "
     293            "asigned to it\n", dev->pathname, class_name, class_info->dev_name);
     294       
     295        ipc_answer_0(callid, EOK);
     296}
     297
     298/** Initialize driver which has registered itself as running and ready.
     299 *
     300 * The initialization is done in a separate fibril to avoid deadlocks (if the
     301 * driver needed to be served by devman during the driver's initialization).
    298302 */
    299303static int init_running_drv(void *drv)
    300304{
    301         driver_t *driver = (driver_t *)drv;
    302         initialize_running_driver(driver, &device_tree);       
    303         printf(NAME ": the %s driver was successfully initialized. \n", driver->name);
     305        driver_t *driver = (driver_t *) drv;
     306       
     307        initialize_running_driver(driver, &device_tree);
     308        printf(NAME ": the %s driver was successfully initialized. \n",
     309            driver->name);
    304310        return 0;
    305311}
    306312
    307 /** Function for handling connections from a driver to the device manager.
    308  */
     313/** Function for handling connections from a driver to the device manager. */
    309314static void devman_connection_driver(ipc_callid_t iid, ipc_call_t *icall)
    310 {       
    311         /* Accept the connection */
     315{
     316        /* Accept the connection. */
    312317        ipc_answer_0(iid, EOK);
    313318       
     
    316321                return;
    317322       
    318         // Initialize the driver as running (e.g. pass assigned devices to it) in a separate fibril;
    319         // the separate fibril is used to enable the driver
    320         // to use devman service during the driver's initialization.
     323        /*
     324         * Initialize the driver as running (e.g. pass assigned devices to it)
     325         * in a separate fibril; the separate fibril is used to enable the
     326         * driver to use devman service during the driver's initialization.
     327         */
    321328        fid_t fid = fibril_create(init_running_drv, driver);
    322329        if (fid == 0) {
    323                 printf(NAME ": Error creating fibril for the initialization of the newly registered running driver.\n");
     330                printf(NAME ": Error creating fibril for the initialization of "
     331                    "the newly registered running driver.\n");
    324332                return;
    325333        }
    326334        fibril_add_ready(fid);
    327        
    328         /*thread_id_t tid;
    329         if (0 != thread_create(init_running_drv, driver, "init_running_drv", &tid)) {
    330                 printf(NAME ": failed to start the initialization of the newly registered running driver.\n");
    331         }*/
    332335       
    333336        ipc_callid_t callid;
     
    354357}
    355358
    356 /** Find handle for the device instance identified by the device's path in the device tree.
    357  */
     359/** Find handle for the device instance identified by the device's path in the
     360 * device tree. */
    358361static void devman_device_get_handle(ipc_callid_t iid, ipc_call_t *icall)
    359362{
    360         char *pathname;
    361         int rc = async_data_write_accept((void **)&pathname, true, 0, 0, 0, 0);
     363        char *pathname;
     364       
     365        int rc = async_data_write_accept((void **) &pathname, true, 0, 0, 0, 0);
    362366        if (rc != EOK) {
    363367                ipc_answer_0(iid, rc);
     
    365369        }
    366370       
    367         node_t * dev = find_dev_node_by_path(&device_tree, pathname); 
     371        node_t * dev = find_dev_node_by_path(&device_tree, pathname);
    368372       
    369373        free(pathname);
     
    378382
    379383
    380 /** Function for handling connections from a client to the device manager.
    381  */
     384/** Function for handling connections from a client to the device manager. */
    382385static void devman_connection_client(ipc_callid_t iid, ipc_call_t *icall)
    383386{
    384         /* Accept connection */
     387        /* Accept connection. */
    385388        ipc_answer_0(iid, EOK);
    386389       
     
    404407}
    405408
    406 static void devman_forward(ipc_callid_t iid, ipc_call_t *icall, bool drv_to_parent) {   
    407        
     409static void
     410devman_forward(ipc_callid_t iid, ipc_call_t *icall, bool drv_to_parent)
     411{
    408412        device_handle_t handle = IPC_GET_ARG2(*icall);
    409         // printf(NAME ": devman_forward - trying to forward connection to device with handle %x.\n", handle);
    410413       
    411414        node_t *dev = find_dev_node(&device_tree, handle);
    412415        if (NULL == dev) {
    413                 printf(NAME ": devman_forward error - no device with handle %x was found.\n", handle);
     416                printf(NAME ": devman_forward error - no device with handle %x "
     417                    "was found.\n", handle);
    414418                ipc_answer_0(iid, ENOENT);
    415419                return;
     
    419423       
    420424        if (drv_to_parent) {
    421                 if (NULL != dev->parent) {
    422                         driver = dev->parent->drv;             
    423                 }
     425                if (NULL != dev->parent)
     426                        driver = dev->parent->drv;
    424427        } else if (DEVICE_USABLE == dev->state) {
    425                 driver = dev->drv;             
     428                driver = dev->drv;
    426429                assert(NULL != driver);
    427430        }
    428431       
    429         if (NULL == driver) {   
    430                 printf(NAME ": devman_forward error - the device is not in usable state.\n", handle);
     432        if (NULL == driver) {
     433                printf(NAME ": devman_forward error - the device is not in "
     434                    "usable state.\n", handle);
    431435                ipc_answer_0(iid, ENOENT);
    432                 return; 
    433         }
    434        
    435         int method;     
    436         if (drv_to_parent) {
     436                return;
     437        }
     438       
     439        int method;
     440        if (drv_to_parent)
    437441                method = DRIVER_DRIVER;
    438         } else {
     442        else
    439443                method = DRIVER_CLIENT;
    440         }
    441444       
    442445        if (driver->phone <= 0) {
    443                 printf(NAME ": devman_forward: cound not forward to driver %s ", driver->name);
     446                printf(NAME ": devman_forward: cound not forward to driver %s ",
     447                    driver->name);
    444448                printf("the driver's phone is %x).\n", driver->phone);
    445449                ipc_answer_0(iid, EINVAL);
    446450                return;
    447451        }
    448         printf(NAME ": devman_forward: forward connection to device %s to driver %s.\n",
    449                 dev->pathname, driver->name);
    450         ipc_forward_fast(iid, driver->phone, method, dev->handle, 0, IPC_FF_NONE);     
    451 }
    452 
    453 /** Function for handling connections from a client forwarded by the device mapper to the device manager.
    454  */
     452        printf(NAME ": devman_forward: forward connection to device %s to "
     453            "driver %s.\n", dev->pathname, driver->name);
     454        ipc_forward_fast(iid, driver->phone, method, dev->handle, 0, IPC_FF_NONE);
     455}
     456
     457/** Function for handling connections from a client forwarded by the device
     458 * mapper to the device manager. */
    455459static void devman_connection_devmapper(ipc_callid_t iid, ipc_call_t *icall)
    456460{
    457461        dev_handle_t devmap_handle = IPC_GET_METHOD(*icall);
     462       
    458463        node_t *dev = find_devmap_tree_device(&device_tree, devmap_handle);
    459         if (NULL == dev) {
     464        if (NULL == dev)
    460465                dev = find_devmap_class_device(&class_list, devmap_handle);
    461         }
    462466       
    463467        if (NULL == dev || NULL == dev->drv) {
     
    471475        }
    472476       
    473         printf(NAME ": devman_connection_devmapper: forward connection to device %s to driver %s.\n",
    474                 dev->pathname, dev->drv->name);
    475         ipc_forward_fast(iid, dev->drv->phone, DRIVER_CLIENT, dev->handle, 0, IPC_FF_NONE);     
    476 }
    477 
    478 /** Function for handling connections to device manager.
    479  *
    480  */
     477        printf(NAME ": devman_connection_devmapper: forward connection to "
     478            "device %s to driver %s.\n", dev->pathname, dev->drv->name);
     479        ipc_forward_fast(iid, dev->drv->phone, DRIVER_CLIENT, dev->handle, 0,
     480            IPC_FF_NONE);
     481}
     482
     483/** Function for handling connections to device manager. */
    481484static void devman_connection(ipc_callid_t iid, ipc_call_t *icall)
    482 {       
    483         // Silly hack to enable the device manager to register as a driver by the device mapper.
    484         // If the ipc method is not IPC_M_CONNECT_ME_TO, this is not the forwarded connection from naming service,
    485         // so it must be a connection from the devmapper which thinks this is a devmapper-style driver.
    486         // So pretend this is a devmapper-style driver.
    487         // (This does not work for device with handle == IPC_M_CONNECT_ME_TO,
    488         // because devmapper passes device handle to the driver as an ipc method.)
    489         if (IPC_M_CONNECT_ME_TO != IPC_GET_METHOD(*icall)) {
     485{
     486        /*
     487         * Silly hack to enable the device manager to register as a driver by
     488         * the device mapper. If the ipc method is not IPC_M_CONNECT_ME_TO, this
     489         * is not the forwarded connection from naming service, so it must be a
     490         * connection from the devmapper which thinks this is a devmapper-style
     491         * driver. So pretend this is a devmapper-style driver. (This does not
     492         * work for device with handle == IPC_M_CONNECT_ME_TO, because devmapper
     493         * passes device handle to the driver as an ipc method.)
     494         */
     495        if (IPC_M_CONNECT_ME_TO != IPC_GET_METHOD(*icall))
    490496                devman_connection_devmapper(iid, icall);
    491         }
    492 
    493         // ipc method is IPC_M_CONNECT_ME_TO, so this is forwarded connection from naming service
    494         // by which we registered as device manager, so be device manager
    495        
    496         // Select interface
     497
     498        /*
     499         * ipc method is IPC_M_CONNECT_ME_TO, so this is forwarded connection
     500         * from naming service by which we registered as device manager, so be
     501         * device manager.
     502         */
     503       
     504        /* Select interface. */
    497505        switch ((ipcarg_t) (IPC_GET_ARG1(*icall))) {
    498506        case DEVMAN_DRIVER:
     
    503511                break;
    504512        case DEVMAN_CONNECT_TO_DEVICE:
    505                 // Connect client to selected device
     513                /* Connect client to selected device. */
    506514                devman_forward(iid, icall, false);
    507515                break;
    508516        case DEVMAN_CONNECT_TO_PARENTS_DEVICE:
    509                 // Connect client to selected device
     517                /* Connect client to selected device. */
    510518                devman_forward(iid, icall, true);
    511                 break;         
     519                break;
    512520        default:
    513521                /* No such interface */
     
    516524}
    517525
    518 /** Initialize device manager internal structures.
    519  */
    520 static bool devman_init()
    521 {
    522         printf(NAME ": devman_init - looking for available drivers. \n");       
    523        
    524         // initialize list of available drivers
     526/** Initialize device manager internal structures. */
     527static bool devman_init(void)
     528{
     529        printf(NAME ": devman_init - looking for available drivers.\n");
     530       
     531        /* Initialize list of available drivers. */
    525532        init_driver_list(&drivers_list);
    526         if (0 == lookup_available_drivers(&drivers_list, DRIVER_DEFAULT_STORE)) {
     533        if (0 == lookup_available_drivers(&drivers_list,
     534            DRIVER_DEFAULT_STORE)) {
    527535                printf(NAME " no drivers found.");
    528536                return false;
    529537        }
    530         printf(NAME ": devman_init  - list of drivers has been initialized. \n");
    531 
    532         // create root device node
     538        printf(NAME ": devman_init  - list of drivers has been initialized.\n");
     539
     540        /* Create root device node. */
    533541        if (!init_device_tree(&device_tree, &drivers_list)) {
    534542                printf(NAME " failed to initialize device tree.");
    535                 return false;           
     543                return false;
    536544        }
    537545
    538546        init_class_list(&class_list);
    539547       
    540         // !!! devman_connection ... as the device manager is not a real devmap driver
    541         // (it uses a completely different ipc protocol than an ordinary devmap driver)
    542         // forwarding a connection from client to the devman by devmapper would not work
    543         devmap_driver_register(NAME, devman_connection);       
     548        /*
     549         * !!! devman_connection ... as the device manager is not a real devmap
     550         * driver (it uses a completely different ipc protocol than an ordinary
     551         * devmap driver) forwarding a connection from client to the devman by
     552         * devmapper would not work.
     553         */
     554        devmap_driver_register(NAME, devman_connection);
    544555       
    545556        return true;
     
    555566        }
    556567       
    557         // Set a handler of incomming connections
     568        /* Set a handler of incomming connections. */
    558569        async_set_client_connection(devman_connection);
    559570
    560         // Register device manager at naming service
     571        /* Register device manager at naming service. */
    561572        ipcarg_t phonead;
    562573        if (ipc_connect_to_me(PHONE_NS, SERVICE_DEVMAN, 0, 0, &phonead) != 0)
     
    566577        async_manager();
    567578
    568         // Never reached
     579        /* Never reached. */
    569580        return 0;
    570581}
Note: See TracChangeset for help on using the changeset viewer.