Ignore:
File:
1 edited

Legend:

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

    ra996ae31 r77ad86c  
    7070FIBRIL_MUTEX_INITIALIZE(functions_mutex);
    7171
    72 /** Interrupts */
    73 static interrupt_context_list_t interrupt_contexts;
    74 
    75 static irq_cmd_t default_cmds[] = {
    76         {
    77                 .cmd = CMD_ACCEPT
    78         }
    79 };
    80 
    81 static irq_code_t default_pseudocode = {
    82         0,
    83         NULL,
    84         sizeof(default_cmds) / sizeof(irq_cmd_t),
    85         default_cmds
    86 };
    87 
    8872static ddf_dev_t *create_device(void);
    8973static void delete_device(ddf_dev_t *);
     
    9579static void *function_get_ops(ddf_fun_t *, dev_inferface_idx_t);
    9680
    97 static void driver_irq_handler(ipc_callid_t iid, ipc_call_t *icall)
    98 {
    99         int id = (int)IPC_GET_IMETHOD(*icall);
    100         interrupt_context_t *ctx;
    101        
    102         ctx = find_interrupt_context_by_id(&interrupt_contexts, id);
    103         if (ctx != NULL && ctx->handler != NULL)
    104                 (*ctx->handler)(ctx->dev, iid, icall);
    105 }
    106 
    107 interrupt_context_t *create_interrupt_context(void)
    108 {
    109         interrupt_context_t *ctx;
    110        
    111         ctx = (interrupt_context_t *) malloc(sizeof(interrupt_context_t));
    112         if (ctx != NULL)
    113                 memset(ctx, 0, sizeof(interrupt_context_t));
    114        
    115         return ctx;
    116 }
    117 
    118 void delete_interrupt_context(interrupt_context_t *ctx)
    119 {
    120         if (ctx != NULL)
    121                 free(ctx);
    122 }
    123 
    124 void init_interrupt_context_list(interrupt_context_list_t *list)
    125 {
    126         memset(list, 0, sizeof(interrupt_context_list_t));
    127         fibril_mutex_initialize(&list->mutex);
    128         list_initialize(&list->contexts);
    129 }
    130 
    131 void
    132 add_interrupt_context(interrupt_context_list_t *list, interrupt_context_t *ctx)
    133 {
    134         fibril_mutex_lock(&list->mutex);
    135         ctx->id = list->curr_id++;
    136         list_append(&ctx->link, &list->contexts);
    137         fibril_mutex_unlock(&list->mutex);
    138 }
    139 
    140 void remove_interrupt_context(interrupt_context_list_t *list,
    141     interrupt_context_t *ctx)
    142 {
    143         fibril_mutex_lock(&list->mutex);
    144         list_remove(&ctx->link);
    145         fibril_mutex_unlock(&list->mutex);
    146 }
    147 
    148 interrupt_context_t *
    149 find_interrupt_context_by_id(interrupt_context_list_t *list, int id)
    150 {
    151         interrupt_context_t *ctx;
    152        
    153         fibril_mutex_lock(&list->mutex);
    154        
    155         list_foreach(list->contexts, link) {
    156                 ctx = list_get_instance(link, interrupt_context_t, link);
    157                 if (ctx->id == id) {
    158                         fibril_mutex_unlock(&list->mutex);
    159                         return ctx;
    160                 }
    161         }
    162        
    163         fibril_mutex_unlock(&list->mutex);
    164         return NULL;
    165 }
    166 
    167 interrupt_context_t *
    168 find_interrupt_context(interrupt_context_list_t *list, ddf_dev_t *dev, int irq)
    169 {
    170         interrupt_context_t *ctx;
    171        
    172         fibril_mutex_lock(&list->mutex);
    173        
    174         list_foreach(list->contexts, link) {
    175                 ctx = list_get_instance(link, interrupt_context_t, link);
    176                 if (ctx->irq == irq && ctx->dev == dev) {
    177                         fibril_mutex_unlock(&list->mutex);
    178                         return ctx;
    179                 }
    180         }
    181        
    182         fibril_mutex_unlock(&list->mutex);
    183         return NULL;
    184 }
    185 
    186 
    187 int
    188 register_interrupt_handler(ddf_dev_t *dev, int irq, interrupt_handler_t *handler,
    189     irq_code_t *pseudocode)
    190 {
    191         interrupt_context_t *ctx = create_interrupt_context();
    192        
    193         ctx->dev = dev;
    194         ctx->irq = irq;
    195         ctx->handler = handler;
    196        
    197         add_interrupt_context(&interrupt_contexts, ctx);
    198        
    199         if (pseudocode == NULL)
    200                 pseudocode = &default_pseudocode;
    201        
    202         int res = irq_register(irq, dev->handle, ctx->id, pseudocode);
    203         if (res != EOK) {
    204                 remove_interrupt_context(&interrupt_contexts, ctx);
    205                 delete_interrupt_context(ctx);
    206         }
    207 
    208         return res;
    209 }
    210 
    211 int unregister_interrupt_handler(ddf_dev_t *dev, int irq)
    212 {
    213         interrupt_context_t *ctx = find_interrupt_context(&interrupt_contexts,
    214             dev, irq);
    215         int res = irq_unregister(irq, dev->handle);
    216        
    217         if (ctx != NULL) {
    218                 remove_interrupt_context(&interrupt_contexts, ctx);
    219                 delete_interrupt_context(ctx);
    220         }
    221        
    222         return res;
    223 }
    224 
    22581static void add_to_functions_list(ddf_fun_t *fun)
    22682{
     
    269125static void driver_dev_add(ipc_callid_t iid, ipc_call_t *icall)
    270126{
    271         char *dev_name = NULL;
    272         int res;
    273        
    274127        devman_handle_t dev_handle = IPC_GET_ARG1(*icall);
    275128        devman_handle_t parent_fun_handle = IPC_GET_ARG2(*icall);
    276129       
    277130        ddf_dev_t *dev = create_device();
    278 
     131       
    279132        /* Add one reference that will be dropped by driver_dev_remove() */
    280133        dev_add_ref(dev);
    281134        dev->handle = dev_handle;
    282 
     135       
     136        char *dev_name = NULL;
    283137        async_data_write_accept((void **) &dev_name, true, 0, 0, 0, 0);
    284138        dev->name = dev_name;
    285 
     139       
    286140        /*
    287141         * Currently not used, parent fun handle is stored in context
     
    290144        (void) parent_fun_handle;
    291145       
    292         res = driver->driver_ops->dev_add(dev);
     146        int res = driver->driver_ops->dev_add(dev);
    293147       
    294148        if (res != EOK) {
     
    305159}
    306160
    307 static void driver_dev_added(ipc_callid_t iid, ipc_call_t *icall)
    308 {
     161static void driver_dev_remove(ipc_callid_t iid, ipc_call_t *icall)
     162{
     163        devman_handle_t devh = IPC_GET_ARG1(*icall);
     164       
    309165        fibril_mutex_lock(&devices_mutex);
    310         ddf_dev_t *dev = driver_get_device(IPC_GET_ARG1(*icall));
    311         fibril_mutex_unlock(&devices_mutex);
    312        
    313         if (dev != NULL && driver->driver_ops->device_added != NULL)
    314                 driver->driver_ops->device_added(dev);
    315 }
    316 
    317 static void driver_dev_remove(ipc_callid_t iid, ipc_call_t *icall)
    318 {
    319         devman_handle_t devh;
    320         ddf_dev_t *dev;
    321         int rc;
    322        
    323         devh = IPC_GET_ARG1(*icall);
    324        
    325         fibril_mutex_lock(&devices_mutex);
    326         dev = driver_get_device(devh);
     166        ddf_dev_t *dev = driver_get_device(devh);
    327167        if (dev != NULL)
    328168                dev_add_ref(dev);
     
    333173                return;
    334174        }
     175       
     176        int rc;
    335177       
    336178        if (driver->driver_ops->dev_remove != NULL)
     
    347189static void driver_dev_gone(ipc_callid_t iid, ipc_call_t *icall)
    348190{
    349         devman_handle_t devh;
    350         ddf_dev_t *dev;
    351         int rc;
    352        
    353         devh = IPC_GET_ARG1(*icall);
     191        devman_handle_t devh = IPC_GET_ARG1(*icall);
    354192       
    355193        fibril_mutex_lock(&devices_mutex);
    356         dev = driver_get_device(devh);
     194        ddf_dev_t *dev = driver_get_device(devh);
    357195        if (dev != NULL)
    358196                dev_add_ref(dev);
     
    363201                return;
    364202        }
     203       
     204        int rc;
    365205       
    366206        if (driver->driver_ops->dev_gone != NULL)
     
    377217static void driver_fun_online(ipc_callid_t iid, ipc_call_t *icall)
    378218{
    379         devman_handle_t funh;
    380         ddf_fun_t *fun;
    381         int rc;
    382        
    383         funh = IPC_GET_ARG1(*icall);
     219        devman_handle_t funh = IPC_GET_ARG1(*icall);
    384220       
    385221        /*
     
    390226        fibril_mutex_lock(&functions_mutex);
    391227       
    392         fun = driver_get_function(funh);
     228        ddf_fun_t *fun = driver_get_function(funh);
    393229        if (fun != NULL)
    394230                fun_add_ref(fun);
     
    402238       
    403239        /* Call driver entry point */
     240        int rc;
     241       
    404242        if (driver->driver_ops->fun_online != NULL)
    405243                rc = driver->driver_ops->fun_online(fun);
     
    414252static void driver_fun_offline(ipc_callid_t iid, ipc_call_t *icall)
    415253{
    416         devman_handle_t funh;
    417         ddf_fun_t *fun;
    418         int rc;
    419        
    420         funh = IPC_GET_ARG1(*icall);
     254        devman_handle_t funh = IPC_GET_ARG1(*icall);
    421255       
    422256        /*
     
    427261        fibril_mutex_lock(&functions_mutex);
    428262       
    429         fun = driver_get_function(funh);
     263        ddf_fun_t *fun = driver_get_function(funh);
    430264        if (fun != NULL)
    431265                fun_add_ref(fun);
     
    439273       
    440274        /* Call driver entry point */
     275        int rc;
     276       
    441277        if (driver->driver_ops->fun_offline != NULL)
    442278                rc = driver->driver_ops->fun_offline(fun);
     
    462298                case DRIVER_DEV_ADD:
    463299                        driver_dev_add(callid, &call);
    464                         break;
    465                 case DRIVER_DEV_ADDED:
    466                         async_answer_0(callid, EOK);
    467                         driver_dev_added(callid, &call);
    468300                        break;
    469301                case DRIVER_DEV_REMOVE:
     
    753585
    754586/** Allocate driver-specific device data. */
    755 extern void *ddf_dev_data_alloc(ddf_dev_t *dev, size_t size)
    756 {
    757         void *data;
    758 
     587void *ddf_dev_data_alloc(ddf_dev_t *dev, size_t size)
     588{
    759589        assert(dev->driver_data == NULL);
    760 
    761         data = calloc(1, size);
     590       
     591        void *data = calloc(1, size);
    762592        if (data == NULL)
    763593                return NULL;
    764 
     594       
    765595        dev->driver_data = data;
    766596        return data;
     
    792622ddf_fun_t *ddf_fun_create(ddf_dev_t *dev, fun_type_t ftype, const char *name)
    793623{
    794         ddf_fun_t *fun;
    795 
    796         fun = create_function();
     624        ddf_fun_t *fun = create_function();
    797625        if (fun == NULL)
    798626                return NULL;
    799 
     627       
    800628        /* Add one reference that will be dropped by ddf_fun_destroy() */
    801629        fun->dev = dev;
    802630        fun_add_ref(fun);
    803 
     631       
    804632        fun->bound = false;
    805633        fun->ftype = ftype;
    806 
     634       
    807635        fun->name = str_dup(name);
    808636        if (fun->name == NULL) {
     
    810638                return NULL;
    811639        }
    812 
     640       
    813641        return fun;
    814642}
    815643
    816644/** Allocate driver-specific function data. */
    817 extern void *ddf_fun_data_alloc(ddf_fun_t *fun, size_t size)
    818 {
    819         void *data;
    820 
     645void *ddf_fun_data_alloc(ddf_fun_t *fun, size_t size)
     646{
    821647        assert(fun->bound == false);
    822648        assert(fun->driver_data == NULL);
    823 
    824         data = calloc(1, size);
     649       
     650        void *data = calloc(1, size);
    825651        if (data == NULL)
    826652                return NULL;
    827 
     653       
    828654        fun->driver_data = data;
    829655        return data;
     
    835661 * must not be bound.
    836662 *
    837  * @param fun           Function to destroy
     663 * @param fun Function to destroy
     664 *
    838665 */
    839666void ddf_fun_destroy(ddf_fun_t *fun)
    840667{
    841668        assert(fun->bound == false);
    842 
     669       
    843670        /*
    844671         * Drop the reference added by ddf_fun_create(). This will deallocate
     
    855682        if (fun->ops == NULL)
    856683                return NULL;
     684       
    857685        return fun->ops->interfaces[idx];
    858686}
     
    867695 * the same name.
    868696 *
    869  * @param fun           Function to bind
    870  * @return              EOK on success or negative error code
     697 * @param fun Function to bind
     698 *
     699 * @return EOK on success or negative error code
     700 *
    871701 */
    872702int ddf_fun_bind(ddf_fun_t *fun)
     
    875705        assert(fun->name != NULL);
    876706       
    877         int res;
    878        
    879707        add_to_functions_list(fun);
    880         res = devman_add_function(fun->name, fun->ftype, &fun->match_ids,
     708        int res = devman_add_function(fun->name, fun->ftype, &fun->match_ids,
    881709            fun->dev->handle, &fun->handle);
    882710        if (res != EOK) {
     
    894722 * the function invisible to the system.
    895723 *
    896  * @param fun           Function to unbind
    897  * @return              EOK on success or negative error code
     724 * @param fun Function to unbind
     725 *
     726 * @return EOK on success or negative error code
     727 *
    898728 */
    899729int ddf_fun_unbind(ddf_fun_t *fun)
    900730{
    901         int res;
    902        
    903731        assert(fun->bound == true);
    904732       
    905         res = devman_remove_function(fun->handle);
     733        int res = devman_remove_function(fun->handle);
    906734        if (res != EOK)
    907735                return res;
    908 
     736       
    909737        remove_from_functions_list(fun);
    910738       
     
    915743/** Online function.
    916744 *
    917  * @param fun           Function to online
    918  * @return              EOK on success or negative error code
     745 * @param fun Function to online
     746 *
     747 * @return EOK on success or negative error code
     748 *
    919749 */
    920750int ddf_fun_online(ddf_fun_t *fun)
    921751{
    922         int res;
    923        
    924752        assert(fun->bound == true);
    925753       
    926         res = devman_drv_fun_online(fun->handle);
     754        int res = devman_drv_fun_online(fun->handle);
    927755        if (res != EOK)
    928756                return res;
     
    933761/** Offline function.
    934762 *
    935  * @param fun           Function to offline
    936  * @return              EOK on success or negative error code
     763 * @param fun Function to offline
     764 *
     765 * @return EOK on success or negative error code
     766 *
    937767 */
    938768int ddf_fun_offline(ddf_fun_t *fun)
    939769{
    940         int res;
    941        
    942770        assert(fun->bound == true);
    943771       
    944         res = devman_drv_fun_offline(fun->handle);
     772        int res = devman_drv_fun_offline(fun->handle);
    945773        if (res != EOK)
    946774                return res;
     
    954782 * Cannot be called when the function node is bound.
    955783 *
    956  * @param fun                   Function
    957  * @param match_id_str          Match string
    958  * @param match_score           Match score
    959  * @return                      EOK on success, ENOMEM if out of memory.
     784 * @param fun          Function
     785 * @param match_id_str Match string
     786 * @param match_score  Match score
     787 *
     788 * @return EOK on success.
     789 * @return ENOMEM if out of memory.
     790 *
    960791 */
    961792int ddf_fun_add_match_id(ddf_fun_t *fun, const char *match_id_str,
    962793    int match_score)
    963794{
    964         match_id_t *match_id;
    965        
    966795        assert(fun->bound == false);
    967796        assert(fun->ftype == fun_inner);
    968797       
    969         match_id = create_match_id();
     798        match_id_t *match_id = create_match_id();
    970799        if (match_id == NULL)
    971800                return ENOMEM;
     
    989818 *
    990819 * Must only be called when the function is bound.
     820 *
    991821 */
    992822int ddf_fun_add_to_category(ddf_fun_t *fun, const char *cat_name)
     
    1000830int ddf_driver_main(driver_t *drv)
    1001831{
    1002         int rc;
    1003 
    1004832        /*
    1005833         * Remember the driver structure - driver_ops will be called by generic
     
    1008836        driver = drv;
    1009837       
    1010         /* Initialize the list of interrupt contexts. */
    1011         init_interrupt_context_list(&interrupt_contexts);
    1012        
    1013         /* Set generic interrupt handler. */
    1014         async_set_interrupt_received(driver_irq_handler);
     838        /* Initialize interrupt module */
     839        interrupt_init();
    1015840       
    1016841        /*
     
    1019844         */
    1020845        async_set_client_connection(driver_connection);
    1021         rc = devman_driver_register(driver->name);
     846        int rc = devman_driver_register(driver->name);
    1022847        if (rc != EOK) {
    1023848                printf("Error: Failed to register driver with device manager "
     
    1025850                    str_error(rc));
    1026851               
    1027                 return 1;
     852                return rc;
    1028853        }
    1029854       
     
    1031856        rc = task_retval(0);
    1032857        if (rc != EOK)
    1033                 return 1;
    1034 
     858                return rc;
     859       
    1035860        async_manager();
    1036861       
    1037862        /* Never reached. */
    1038         return 0;
     863        return EOK;
    1039864}
    1040865
Note: See TracChangeset for help on using the changeset viewer.