Changeset 92574f4 in mainline for uspace/lib/drv/generic/driver.c


Ignore:
Timestamp:
2011-02-24T12:03:27Z (13 years ago)
Author:
Lubos Slovak <lubos.slovak@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
e7b7ebd5
Parents:
4837092 (diff), a80849c (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:

Merged development (changes in DDF, etc.).

Conflicts in uspace/drv/usbkbd/main.c

File:
1 edited

Legend:

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

    r4837092 r92574f4  
    11/*
    22 * Copyright (c) 2010 Lenka Trochtova
     3 * Copyright (c) 2011 Jiri Svoboda
    34 * All rights reserved.
    45 *
     
    4950#include <errno.h>
    5051#include <inttypes.h>
     52#include <devman.h>
    5153
    5254#include <ipc/driver.h>
    5355
    5456#include "dev_iface.h"
    55 #include "driver.h"
     57#include "ddf/driver.h"
     58#include "ddf/interrupt.h"
    5659
    5760/** Driver structure */
     
    5962
    6063/** Devices */
    61 LIST_INITIALIZE(devices);
    62 FIBRIL_MUTEX_INITIALIZE(devices_mutex);
     64LIST_INITIALIZE(functions);
     65FIBRIL_MUTEX_INITIALIZE(functions_mutex);
    6366
    6467/** Interrupts */
     
    7679};
    7780
     81static ddf_dev_t *create_device(void);
     82static void delete_device(ddf_dev_t *);
     83static remote_handler_t *function_get_default_handler(ddf_fun_t *);
     84static void *function_get_ops(ddf_fun_t *, dev_inferface_idx_t);
    7885
    7986static void driver_irq_handler(ipc_callid_t iid, ipc_call_t *icall)
     
    150157
    151158interrupt_context_t *
    152 find_interrupt_context(interrupt_context_list_t *list, device_t *dev, int irq)
     159find_interrupt_context(interrupt_context_list_t *list, ddf_dev_t *dev, int irq)
    153160{
    154161        fibril_mutex_lock(&list->mutex);
     
    172179
    173180int
    174 register_interrupt_handler(device_t *dev, int irq, interrupt_handler_t *handler,
     181register_interrupt_handler(ddf_dev_t *dev, int irq, interrupt_handler_t *handler,
    175182    irq_code_t *pseudocode)
    176183{
     
    186193                pseudocode = &default_pseudocode;
    187194       
    188         int res = ipc_register_irq(irq, dev->handle, ctx->id, pseudocode);
     195        int res = register_irq(irq, dev->handle, ctx->id, pseudocode);
    189196        if (res != EOK) {
    190197                remove_interrupt_context(&interrupt_contexts, ctx);
     
    195202}
    196203
    197 int unregister_interrupt_handler(device_t *dev, int irq)
     204int unregister_interrupt_handler(ddf_dev_t *dev, int irq)
    198205{
    199206        interrupt_context_t *ctx = find_interrupt_context(&interrupt_contexts,
    200207            dev, irq);
    201         int res = ipc_unregister_irq(irq, dev->handle);
     208        int res = unregister_irq(irq, dev->handle);
    202209       
    203210        if (ctx != NULL) {
     
    209216}
    210217
    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;
     218static void add_to_functions_list(ddf_fun_t *fun)
     219{
     220        fibril_mutex_lock(&functions_mutex);
     221        list_append(&fun->link, &functions);
     222        fibril_mutex_unlock(&functions_mutex);
     223}
     224
     225static void remove_from_functions_list(ddf_fun_t *fun)
     226{
     227        fibril_mutex_lock(&functions_mutex);
     228        list_remove(&fun->link);
     229        fibril_mutex_unlock(&functions_mutex);
     230}
     231
     232static ddf_fun_t *driver_get_function(link_t *functions, devman_handle_t handle)
     233{
     234        ddf_fun_t *fun = NULL;
     235       
     236        fibril_mutex_lock(&functions_mutex);
     237        link_t *link = functions->next;
     238       
     239        while (link != functions) {
     240                fun = list_get_instance(link, ddf_fun_t, link);
     241                if (fun->handle == handle) {
     242                        fibril_mutex_unlock(&functions_mutex);
     243                        return fun;
    237244                }
     245               
    238246                link = link->next;
    239247        }
    240248       
    241         fibril_mutex_unlock(&devices_mutex);
     249        fibril_mutex_unlock(&functions_mutex);
    242250       
    243251        return NULL;
     
    250258       
    251259        devman_handle_t dev_handle = IPC_GET_ARG1(*icall);
    252         devman_handle_t parent_dev_handle = IPC_GET_ARG2(*icall);
    253        
    254         device_t *dev = create_device();
     260        devman_handle_t parent_fun_handle = IPC_GET_ARG2(*icall);
     261       
     262        ddf_dev_t *dev = create_device();
    255263        dev->handle = dev_handle;
    256        
     264
    257265        async_data_write_accept((void **) &dev_name, true, 0, 0, 0, 0);
    258266        dev->name = dev_name;
    259        
    260         add_to_devices_list(dev);
    261         dev->parent = driver_get_device(&devices, parent_dev_handle);
     267
     268        /*
     269         * Currently not used, parent fun handle is stored in context
     270         * of the connection to the parent device driver.
     271         */
     272        (void) parent_fun_handle;
    262273       
    263274        res = driver->driver_ops->add_device(dev);
     
    268279                printf("%s: failed to add a new device with handle = %" PRIun ".\n",
    269280                    driver->name, dev_handle);
    270                 remove_from_devices_list(dev);
    271281                delete_device(dev);
    272282        }
    273283       
    274         ipc_answer_0(iid, res);
     284        async_answer_0(iid, res);
    275285}
    276286
     
    278288{
    279289        /* Accept connection */
    280         ipc_answer_0(iid, EOK);
     290        async_answer_0(iid, EOK);
    281291       
    282292        bool cont = true;
     
    293303                        break;
    294304                default:
    295                         ipc_answer_0(callid, ENOENT);
     305                        async_answer_0(callid, ENOENT);
    296306                }
    297307        }
     
    311321         */
    312322        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"
     323        ddf_fun_t *fun = driver_get_function(&functions, handle);
     324
     325        if (fun == NULL) {
     326                printf("%s: driver_connection_gen error - no function with handle"
    317327                    " %" PRIun " was found.\n", driver->name, handle);
    318                 ipc_answer_0(iid, ENOENT);
     328                async_answer_0(iid, ENOENT);
    319329                return;
    320330        }
     
    327337       
    328338        int ret = EOK;
    329         /* open the device */
    330         if (dev->ops != NULL && dev->ops->open != NULL)
    331                 ret = (*dev->ops->open)(dev);
    332        
    333         ipc_answer_0(iid, ret);
     339        /* Open device function */
     340        if (fun->ops != NULL && fun->ops->open != NULL)
     341                ret = (*fun->ops->open)(fun);
     342       
     343        async_answer_0(iid, ret);
    334344        if (ret != EOK)
    335345                return;
     
    344354                switch  (method) {
    345355                case IPC_M_PHONE_HUNGUP:
    346                         /* close the device */
    347                         if (dev->ops != NULL && dev->ops->close != NULL)
    348                                 (*dev->ops->close)(dev);
    349                         ipc_answer_0(callid, EOK);
     356                        /* Close device function */
     357                        if (fun->ops != NULL && fun->ops->close != NULL)
     358                                (*fun->ops->close)(fun);
     359                        async_answer_0(callid, EOK);
    350360                        return;
    351361                default:
     
    356366                        if (!is_valid_iface_idx(iface_idx)) {
    357367                                remote_handler_t *default_handler =
    358                                     device_get_default_handler(dev);
     368                                    function_get_default_handler(fun);
    359369                                if (default_handler != NULL) {
    360                                         (*default_handler)(dev, callid, &call);
     370                                        (*default_handler)(fun, callid, &call);
    361371                                        break;
    362372                                }
     373                               
    363374                                /*
    364                                  * This is not device's interface and the
     375                                 * Function has no such interface and
    365376                                 * default handler is not provided.
    366377                                 */
     
    368379                                    "invalid interface id %d.",
    369380                                    driver->name, iface_idx);
    370                                 ipc_answer_0(callid, ENOTSUP);
     381                                async_answer_0(callid, ENOTSUP);
    371382                                break;
    372383                        }
    373384                       
    374                         /* calling one of the device's interfaces */
     385                        /* calling one of the function's interfaces */
    375386                       
    376387                        /* Get the interface ops structure. */
    377                         void *ops = device_get_ops(dev, iface_idx);
     388                        void *ops = function_get_ops(fun, iface_idx);
    378389                        if (ops == NULL) {
    379390                                printf("%s: driver_connection_gen error - ",
    380391                                    driver->name);
    381                                 printf("device with handle %" PRIun " has no interface "
     392                                printf("Function with handle %" PRIun " has no interface "
    382393                                    "with id %d.\n", handle, iface_idx);
    383                                 ipc_answer_0(callid, ENOTSUP);
     394                                async_answer_0(callid, ENOTSUP);
    384395                                break;
    385396                        }
     
    400411                                printf("%s: driver_connection_gen error - "
    401412                                    "invalid interface method.", driver->name);
    402                                 ipc_answer_0(callid, ENOTSUP);
     413                                async_answer_0(callid, ENOTSUP);
    403414                                break;
    404415                        }
     
    408419                         * receive parameters from the remote client and it will
    409420                         * pass it to the corresponding local interface method
    410                          * associated with the device by its driver.
     421                         * associated with the function by its driver.
    411422                         */
    412                         (*iface_method_ptr)(dev, ops, callid, &call);
     423                        (*iface_method_ptr)(fun, ops, callid, &call);
    413424                        break;
    414425                }
     
    425436        driver_connection_gen(iid, icall, false);
    426437}
    427 
    428438
    429439/** Function for handling connections to device driver. */
     
    446456        default:
    447457                /* No such interface */
    448                 ipc_answer_0(iid, ENOENT);
     458                async_answer_0(iid, ENOENT);
    449459        }
    450460}
     
    454464 * @return              The device structure.
    455465 */
    456 device_t *create_device(void)
    457 {
    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 
     466static ddf_dev_t *create_device(void)
     467{
     468        ddf_dev_t *dev;
     469
     470        dev = malloc(sizeof(ddf_dev_t));
     471        if (dev == NULL)
     472                return NULL;
     473
     474        memset(dev, 0, sizeof(ddf_dev_t));
    465475        return dev;
    466476}
    467477
     478/** Create new function structure.
     479 *
     480 * @return              The device structure.
     481 */
     482static ddf_fun_t *create_function(void)
     483{
     484        ddf_fun_t *fun;
     485
     486        fun = calloc(1, sizeof(ddf_fun_t));
     487        if (fun == NULL)
     488                return NULL;
     489
     490        init_match_ids(&fun->match_ids);
     491        link_initialize(&fun->link);
     492
     493        return fun;
     494}
     495
    468496/** Delete device structure.
    469497 *
    470498 * @param dev           The device structure.
    471499 */
    472 void delete_device(device_t *dev)
    473 {
    474         clean_match_ids(&dev->match_ids);
    475         if (dev->name != NULL)
    476                 free(dev->name);
     500static void delete_device(ddf_dev_t *dev)
     501{
    477502        free(dev);
    478503}
    479504
    480 void *device_get_ops(device_t *dev, dev_inferface_idx_t idx)
     505/** Delete device structure.
     506 *
     507 * @param dev           The device structure.
     508 */
     509static void delete_function(ddf_fun_t *fun)
     510{
     511        clean_match_ids(&fun->match_ids);
     512        if (fun->name != NULL)
     513                free(fun->name);
     514        free(fun);
     515}
     516
     517/** Create a DDF function node.
     518 *
     519 * Create a DDF function (in memory). Both child devices and external clients
     520 * communicate with a device via its functions.
     521 *
     522 * The created function node is fully formed, but only exists in the memory
     523 * of the client task. In order to be visible to the system, the function
     524 * must be bound using ddf_fun_bind().
     525 *
     526 * This function should only fail if there is not enough free memory.
     527 * Specifically, this function succeeds even if @a dev already has
     528 * a (bound) function with the same name.
     529 *
     530 * Type: A function of type fun_inner indicates that DDF should attempt
     531 * to attach child devices to the function. fun_exposed means that
     532 * the function should be exported to external clients (applications).
     533 *
     534 * @param dev           Device to which we are adding function
     535 * @param ftype         Type of function (fun_inner or fun_exposed)
     536 * @param name          Name of function
     537 *
     538 * @return              New function or @c NULL if memory is not available
     539 */
     540ddf_fun_t *ddf_fun_create(ddf_dev_t *dev, fun_type_t ftype, const char *name)
     541{
     542        ddf_fun_t *fun;
     543
     544        fun = create_function();
     545        if (fun == NULL)
     546                return NULL;
     547
     548        fun->bound = false;
     549        fun->dev = dev;
     550        fun->ftype = ftype;
     551
     552        fun->name = str_dup(name);
     553        if (fun->name == NULL) {
     554                delete_function(fun);
     555                return NULL;
     556        }
     557
     558        return fun;
     559}
     560
     561/** Destroy DDF function node.
     562 *
     563 * Destroy a function previously created with ddf_fun_create(). The function
     564 * must not be bound.
     565 *
     566 * @param fun           Function to destroy
     567 */
     568void ddf_fun_destroy(ddf_fun_t *fun)
     569{
     570        assert(fun->bound == false);
     571        delete_function(fun);
     572}
     573
     574static void *function_get_ops(ddf_fun_t *fun, dev_inferface_idx_t idx)
    481575{
    482576        assert(is_valid_iface_idx(idx));
    483         if (dev->ops == NULL)
     577        if (fun->ops == NULL)
    484578                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);
     579        return fun->ops->interfaces[idx];
     580}
     581
     582/** Bind a function node.
     583 *
     584 * Bind the specified function to the system. This effectively makes
     585 * the function visible to the system (uploads it to the server).
     586 *
     587 * This function can fail for several reasons. Specifically,
     588 * it will fail if the device already has a bound function of
     589 * the same name.
     590 *
     591 * @param fun           Function to bind
     592 * @return              EOK on success or negative error code
     593 */
     594int ddf_fun_bind(ddf_fun_t *fun)
     595{
     596        assert(fun->name != NULL);
    491597       
    492598        int res;
    493599       
    494         add_to_devices_list(child);
    495         res = devman_child_device_register(child->name, &child->match_ids,
    496             parent->handle, &child->handle);
     600        add_to_functions_list(fun);
     601        res = devman_add_function(fun->name, fun->ftype, &fun->match_ids,
     602            fun->dev->handle, &fun->handle);
    497603        if (res != EOK) {
    498                 remove_from_devices_list(child);
     604                remove_from_functions_list(fun);
    499605                return res;
    500606        }
    501607       
     608        fun->bound = true;
    502609        return res;
    503610}
    504611
    505 /** Wrapper for child_device_register for devices with single match id.
    506  *
    507  * @param parent Parent device.
    508  * @param child_name Child device name.
    509  * @param child_match_id Child device match id.
    510  * @param child_match_score Child device match score.
    511  * @return Error code.
    512  */
    513 int child_device_register_wrapper(device_t *parent, const char *child_name,
    514     const char *child_match_id, int child_match_score,
    515     devman_handle_t *child_handle)
    516 {
    517         device_t *child = NULL;
    518         match_id_t *match_id = NULL;
    519         int rc;
    520        
    521         child = create_device();
    522         if (child == NULL) {
    523                 rc = ENOMEM;
    524                 goto failure;
    525         }
    526        
    527         child->name = child_name;
     612/** Add single match ID to inner function.
     613 *
     614 * Construct and add a single match ID to the specified function.
     615 * Cannot be called when the function node is bound.
     616 *
     617 * @param fun                   Function
     618 * @param match_id_str          Match string
     619 * @param match_score           Match score
     620 * @return                      EOK on success, ENOMEM if out of memory.
     621 */
     622int ddf_fun_add_match_id(ddf_fun_t *fun, const char *match_id_str,
     623    int match_score)
     624{
     625        match_id_t *match_id;
     626       
     627        assert(fun->bound == false);
     628        assert(fun->ftype == fun_inner);
    528629       
    529630        match_id = create_match_id();
    530         if (match_id == NULL) {
    531                 rc = ENOMEM;
    532                 goto failure;
    533         }
    534        
    535         match_id->id = child_match_id;
    536         match_id->score = child_match_score;
    537         add_match_id(&child->match_ids, match_id);
    538        
    539         rc = child_device_register(child, parent);
    540         if (rc != EOK)
    541                 goto failure;
    542 
    543         if (child_handle != NULL) {
    544                 *child_handle = child->handle;
    545         }
    546 
     631        if (match_id == NULL)
     632                return ENOMEM;
     633       
     634        match_id->id = match_id_str;
     635        match_id->score = 90;
     636       
     637        add_match_id(&fun->match_ids, match_id);
    547638        return EOK;
    548        
    549 failure:
    550         if (match_id != NULL) {
    551                 match_id->id = NULL;
    552                 delete_match_id(match_id);
    553         }
    554        
    555         if (child != NULL) {
    556                 child->name = NULL;
    557                 delete_device(child);
    558         }
    559        
    560         return rc;
    561639}
    562640
    563641/** Get default handler for client requests */
    564 remote_handler_t *device_get_default_handler(device_t *dev)
    565 {
    566         if (dev->ops == NULL)
     642static remote_handler_t *function_get_default_handler(ddf_fun_t *fun)
     643{
     644        if (fun->ops == NULL)
    567645                return NULL;
    568         return dev->ops->default_handler;
    569 }
    570 
    571 int add_device_to_class(device_t *dev, const char *class_name)
    572 {
    573         return devman_add_device_to_class(dev->handle, class_name);
    574 }
    575 
    576 int driver_main(driver_t *drv)
     646        return fun->ops->default_handler;
     647}
     648
     649/** Add exposed function to class.
     650 *
     651 * Must only be called when the function is bound.
     652 */
     653int ddf_fun_add_to_class(ddf_fun_t *fun, const char *class_name)
     654{
     655        assert(fun->bound == true);
     656        assert(fun->ftype == fun_exposed);
     657       
     658        return devman_add_device_to_class(fun->handle, class_name);
     659}
     660
     661int ddf_driver_main(driver_t *drv)
    577662{
    578663        /*
Note: See TracChangeset for help on using the changeset viewer.