Changeset 8b1e15ac in mainline for uspace/lib/drv/generic/driver.c


Ignore:
Timestamp:
2011-02-11T22:26:36Z (13 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
68414f4a
Parents:
1b367b4
Message:

Finish splitting device node: devman client in C library, drv library. Update device drivers accordingly.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/drv/generic/driver.c

    r1b367b4 r8b1e15ac  
    5959
    6060/** Devices */
    61 LIST_INITIALIZE(devices);
    62 FIBRIL_MUTEX_INITIALIZE(devices_mutex);
     61LIST_INITIALIZE(functions);
     62FIBRIL_MUTEX_INITIALIZE(functions_mutex);
    6363
    6464/** Interrupts */
     
    209209}
    210210
    211 static void add_to_devices_list(device_t *dev)
    212 {
    213         fibril_mutex_lock(&devices_mutex);
    214         list_append(&dev->link, &devices);
    215         fibril_mutex_unlock(&devices_mutex);
    216 }
    217 
    218 static void remove_from_devices_list(device_t *dev)
    219 {
    220         fibril_mutex_lock(&devices_mutex);
    221         list_remove(&dev->link);
    222         fibril_mutex_unlock(&devices_mutex);
    223 }
    224 
    225 static device_t *driver_get_device(link_t *devices, devman_handle_t handle)
    226 {
    227         device_t *dev = NULL;
    228        
    229         fibril_mutex_lock(&devices_mutex);
    230         link_t *link = devices->next;
    231        
    232         while (link != devices) {
    233                 dev = list_get_instance(link, device_t, link);
    234                 if (dev->handle == handle) {
    235                         fibril_mutex_unlock(&devices_mutex);
    236                         return dev;
     211static void add_to_functions_list(function_t *fun)
     212{
     213        fibril_mutex_lock(&functions_mutex);
     214        list_append(&fun->link, &functions);
     215        fibril_mutex_unlock(&functions_mutex);
     216}
     217
     218static void remove_from_functions_list(function_t *fun)
     219{
     220        fibril_mutex_lock(&functions_mutex);
     221        list_remove(&fun->link);
     222        fibril_mutex_unlock(&functions_mutex);
     223}
     224
     225static function_t *driver_get_function(link_t *functions, devman_handle_t handle)
     226{
     227        function_t *fun = NULL;
     228        printf("driver_get_function handle=%" PRIun "\n", handle);
     229       
     230        fibril_mutex_lock(&functions_mutex);
     231        link_t *link = functions->next;
     232       
     233        while (link != functions) {
     234                fun = list_get_instance(link, function_t, link);
     235                printf(" - fun handle %" PRIun "\n", fun->handle);
     236                if (fun->handle == handle) {
     237                        fibril_mutex_unlock(&functions_mutex);
     238                        return fun;
    237239                }
     240               
    238241                link = link->next;
    239242        }
    240243       
    241         fibril_mutex_unlock(&devices_mutex);
     244        fibril_mutex_unlock(&functions_mutex);
    242245       
    243246        return NULL;
     
    250253       
    251254        devman_handle_t dev_handle = IPC_GET_ARG1(*icall);
    252         devman_handle_t parent_dev_handle = IPC_GET_ARG2(*icall);
     255        devman_handle_t parent_fun_handle = IPC_GET_ARG2(*icall);
    253256       
    254257        device_t *dev = create_device();
    255258        dev->handle = dev_handle;
    256        
     259
    257260        async_data_write_accept((void **) &dev_name, true, 0, 0, 0, 0);
    258261        dev->name = dev_name;
    259        
    260         add_to_devices_list(dev);
    261         dev->parent = driver_get_device(&devices, parent_dev_handle);
     262
     263        /*
     264         * Currently not used, parent fun handle is stored in context
     265         * of the connection to the parent device driver.
     266         */
     267        (void) parent_fun_handle;
    262268       
    263269        res = driver->driver_ops->add_device(dev);
     
    268274                printf("%s: failed to add a new device with handle = %" PRIun ".\n",
    269275                    driver->name, dev_handle);
    270                 remove_from_devices_list(dev);
    271276                delete_device(dev);
    272277        }
     
    311316         */
    312317        devman_handle_t handle = IPC_GET_ARG2(*icall);
    313         device_t *dev = driver_get_device(&devices, handle);
    314 
    315         if (dev == NULL) {
    316                 printf("%s: driver_connection_gen error - no device with handle"
     318        function_t *fun = driver_get_function(&functions, handle);
     319
     320        if (fun == NULL) {
     321                printf("%s: driver_connection_gen error - no function with handle"
    317322                    " %" PRIun " was found.\n", driver->name, handle);
    318323                async_answer_0(iid, ENOENT);
     
    327332       
    328333        int ret = EOK;
    329         /* open the device */
    330         if (dev->ops != NULL && dev->ops->open != NULL)
    331                 ret = (*dev->ops->open)(dev);
     334        /* Open device function */
     335        if (fun->ops != NULL && fun->ops->open != NULL)
     336                ret = (*fun->ops->open)(fun);
    332337       
    333338        async_answer_0(iid, ret);
     
    344349                switch  (method) {
    345350                case IPC_M_PHONE_HUNGUP:
    346                         /* close the device */
    347                         if (dev->ops != NULL && dev->ops->close != NULL)
    348                                 (*dev->ops->close)(dev);
     351                        /* Close device function */
     352                        if (fun->ops != NULL && fun->ops->close != NULL)
     353                                (*fun->ops->close)(fun);
    349354                        async_answer_0(callid, EOK);
    350355                        return;
     
    356361                        if (!is_valid_iface_idx(iface_idx)) {
    357362                                remote_handler_t *default_handler =
    358                                     device_get_default_handler(dev);
     363                                    function_get_default_handler(fun);
    359364                                if (default_handler != NULL) {
    360                                         (*default_handler)(dev, callid, &call);
     365                                        (*default_handler)(fun, callid, &call);
    361366                                        break;
    362367                                }
     368                               
    363369                                /*
    364                                  * This is not device's interface and the
     370                                 * Function has no such interface and
    365371                                 * default handler is not provided.
    366372                                 */
     
    372378                        }
    373379                       
    374                         /* calling one of the device's interfaces */
     380                        /* calling one of the function's interfaces */
    375381                       
    376382                        /* Get the interface ops structure. */
    377                         void *ops = device_get_ops(dev, iface_idx);
     383                        void *ops = function_get_ops(fun, iface_idx);
    378384                        if (ops == NULL) {
    379385                                printf("%s: driver_connection_gen error - ",
    380386                                    driver->name);
    381                                 printf("device with handle %" PRIun " has no interface "
     387                                printf("Function with handle %" PRIun " has no interface "
    382388                                    "with id %d.\n", handle, iface_idx);
    383389                                async_answer_0(callid, ENOTSUP);
     
    408414                         * receive parameters from the remote client and it will
    409415                         * pass it to the corresponding local interface method
    410                          * associated with the device by its driver.
     416                         * associated with the function by its driver.
    411417                         */
    412                         (*iface_method_ptr)(dev, ops, callid, &call);
     418                        (*iface_method_ptr)(fun, ops, callid, &call);
    413419                        break;
    414420                }
     
    425431        driver_connection_gen(iid, icall, false);
    426432}
    427 
    428433
    429434/** Function for handling connections to device driver. */
     
    456461device_t *create_device(void)
    457462{
    458         device_t *dev = malloc(sizeof(device_t));
    459 
    460         if (dev != NULL) {
    461                 memset(dev, 0, sizeof(device_t));
    462                 init_match_ids(&dev->match_ids);
    463         }
    464 
     463        device_t *dev;
     464
     465        dev = malloc(sizeof(device_t));
     466        if (dev == NULL)
     467                return NULL;
     468
     469        memset(dev, 0, sizeof(device_t));
    465470        return dev;
    466471}
    467472
     473/** Create new function structure.
     474 *
     475 * @return              The device structure.
     476 */
     477function_t *create_function(void)
     478{
     479        function_t *fun;
     480
     481        fun = malloc(sizeof(function_t));
     482        if (fun == NULL)
     483                return NULL;
     484
     485        memset(fun, 0, sizeof(device_t));
     486
     487        init_match_ids(&fun->match_ids);
     488        link_initialize(&fun->link);
     489
     490        return fun;
     491}
     492
    468493/** Delete device structure.
    469494 *
     
    472497void delete_device(device_t *dev)
    473498{
    474         clean_match_ids(&dev->match_ids);
    475         if (dev->name != NULL)
    476                 free(dev->name);
    477499        free(dev);
    478500}
    479501
    480 void *device_get_ops(device_t *dev, dev_inferface_idx_t idx)
     502/** Delete device structure.
     503 *
     504 * @param dev           The device structure.
     505 */
     506void delete_function(function_t *fun)
     507{
     508        clean_match_ids(&fun->match_ids);
     509        if (fun->name != NULL)
     510                free(fun->name);
     511        free(fun);
     512}
     513
     514void *function_get_ops(function_t *fun, dev_inferface_idx_t idx)
    481515{
    482516        assert(is_valid_iface_idx(idx));
    483         if (dev->ops == NULL)
     517        if (fun->ops == NULL)
    484518                return NULL;
    485         return dev->ops->interfaces[idx];
    486 }
    487 
    488 int child_device_register(device_t *child, device_t *parent)
    489 {
    490         assert(child->name != NULL);
     519        return fun->ops->interfaces[idx];
     520}
     521
     522int register_function(function_t *fun, device_t *dev)
     523{
     524        assert(fun->name != NULL);
    491525       
    492526        int res;
    493527       
    494         add_to_devices_list(child);
    495         res = devman_child_device_register(child->name, &child->match_ids,
    496             parent->handle, &child->handle);
     528        fun->dev = dev;
     529       
     530        add_to_functions_list(fun);
     531        res = devman_add_function(fun->name, fun->ftype, &fun->match_ids,
     532            dev->handle, &fun->handle);
    497533        if (res != EOK) {
    498                 remove_from_devices_list(child);
     534                remove_from_functions_list(fun);
    499535                return res;
    500536        }
     
    511547 * @return Error code.
    512548 */
    513 int child_device_register_wrapper(device_t *parent, const char *child_name,
    514     const char *child_match_id, int child_match_score)
    515 {
    516         device_t *child = NULL;
    517         match_id_t *match_id = NULL;
     549int register_function_wrapper(device_t *dev, const char *fun_name,
     550    const char *match_id, int match_score)
     551{
     552        function_t *fun = NULL;
     553        match_id_t *m_id = NULL;
    518554        int rc;
    519555       
    520         child = create_device();
    521         if (child == NULL) {
     556        fun = create_function();
     557        if (fun == NULL) {
    522558                rc = ENOMEM;
    523559                goto failure;
    524560        }
    525561       
    526         child->name = child_name;
    527        
    528         match_id = create_match_id();
    529         if (match_id == NULL) {
     562        fun->dev = dev;
     563        fun->name = fun_name;
     564        fun->ftype = fun_inner;
     565       
     566        m_id = create_match_id();
     567        if (m_id == NULL) {
    530568                rc = ENOMEM;
    531569                goto failure;
    532570        }
    533571       
    534         match_id->id = child_match_id;
    535         match_id->score = child_match_score;
    536         add_match_id(&child->match_ids, match_id);
    537        
    538         rc = child_device_register(child, parent);
     572        m_id->id = match_id;
     573        m_id->score = match_score;
     574        add_match_id(&fun->match_ids, m_id);
     575       
     576        rc = register_function(fun, dev);
    539577        if (rc != EOK)
    540578                goto failure;
     
    543581       
    544582failure:
    545         if (match_id != NULL) {
    546                 match_id->id = NULL;
    547                 delete_match_id(match_id);
    548         }
    549        
    550         if (child != NULL) {
    551                 child->name = NULL;
    552                 delete_device(child);
     583        if (m_id != NULL) {
     584                m_id->id = NULL;
     585                delete_match_id(m_id);
     586        }
     587       
     588        if (fun != NULL) {
     589                fun->name = NULL;
     590                delete_function(fun);
    553591        }
    554592       
     
    557595
    558596/** Get default handler for client requests */
    559 remote_handler_t *device_get_default_handler(device_t *dev)
    560 {
    561         if (dev->ops == NULL)
     597remote_handler_t *function_get_default_handler(function_t *fun)
     598{
     599        if (fun->ops == NULL)
    562600                return NULL;
    563         return dev->ops->default_handler;
    564 }
    565 
    566 int add_device_to_class(device_t *dev, const char *class_name)
    567 {
    568         return devman_add_device_to_class(dev->handle, class_name);
     601        return fun->ops->default_handler;
     602}
     603
     604int add_function_to_class(function_t *fun, const char *class_name)
     605{
     606        return devman_add_device_to_class(fun->handle, class_name);
    569607}
    570608
Note: See TracChangeset for help on using the changeset viewer.