Changeset 6610565b in mainline for uspace/lib/drv


Ignore:
Timestamp:
2011-01-10T16:20:56Z (15 years ago)
Author:
Vojtech Horky <vojtechhorky@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
1ffa73b, 8048c648, 8426912a, 977fcea, f401312
Parents:
a97ea0f (diff), 4a5c6c1 (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:

Merge mainline changes

Location:
uspace/lib/drv
Files:
5 edited
6 moved

Legend:

Unmodified
Added
Removed
  • uspace/lib/drv/Makefile

    ra97ea0f r6610565b  
    3535        generic/driver.c \
    3636        generic/dev_iface.c \
    37         generic/remote_res.c \
     37        generic/remote_char_dev.c \
     38        generic/remote_hw_res.c \
    3839        generic/remote_usb.c \
    39         generic/remote_usbhc.c \
    40         generic/remote_char.c
     40        generic/remote_usbhc.c
    4141
    4242include $(USPACE_PREFIX)/Makefile.common
  • uspace/lib/drv/generic/dev_iface.c

    ra97ea0f r6610565b  
    3636 */
    3737
     38#include <assert.h>
     39
    3840#include "dev_iface.h"
    39 #include "remote_res.h"
    40 #include "remote_char.h"
     41#include "remote_hw_res.h"
     42#include "remote_char_dev.h"
    4143#include "remote_usb.h"
    4244#include "remote_usbhc.h"
     
    4446static iface_dipatch_table_t remote_ifaces = {
    4547        .ifaces = {
    46                 &remote_res_iface,
    47                 &remote_char_iface,
     48                &remote_hw_res_iface,
     49                &remote_char_dev_iface,
    4850                &remote_usb_iface,
    4951                &remote_usbhc_iface
     
    5153};
    5254
    53 remote_iface_t* get_remote_iface(int idx)
    54 {       
     55remote_iface_t *get_remote_iface(int idx)
     56{
    5557        assert(is_valid_iface_idx(idx));
    5658        return remote_ifaces.ifaces[idx];
     
    6365                return NULL;
    6466        }
     67
    6568        return rem_iface->methods[iface_method_idx];
     69}
     70
     71bool is_valid_iface_idx(int idx)
     72{
     73        return (0 <= idx) && (idx < DEV_IFACE_MAX);
    6674}
    6775
  • uspace/lib/drv/generic/driver.c

    ra97ea0f r6610565b  
    5252#include <ipc/driver.h>
    5353
     54#include "dev_iface.h"
    5455#include "driver.h"
    5556
    56 /* driver structure */
    57 
     57/** Driver structure */
    5858static driver_t *driver;
    5959
    60 /* devices */
    61 
     60/** Devices */
    6261LIST_INITIALIZE(devices);
    6362FIBRIL_MUTEX_INITIALIZE(devices_mutex);
    6463
    65 /* interrupts */
    66 
     64/** Interrupts */
    6765static interrupt_context_list_t interrupt_contexts;
    6866
     
    8583       
    8684        ctx = find_interrupt_context_by_id(&interrupt_contexts, id);
    87         if (NULL != ctx && NULL != ctx->handler)
     85        if (ctx != NULL && ctx->handler != NULL)
    8886                (*ctx->handler)(ctx->dev, iid, icall);
    8987}
     88
     89interrupt_context_t *create_interrupt_context(void)
     90{
     91        interrupt_context_t *ctx;
     92       
     93        ctx = (interrupt_context_t *) malloc(sizeof(interrupt_context_t));
     94        if (ctx != NULL)
     95                memset(ctx, 0, sizeof(interrupt_context_t));
     96       
     97        return ctx;
     98}
     99
     100void delete_interrupt_context(interrupt_context_t *ctx)
     101{
     102        if (ctx != NULL)
     103                free(ctx);
     104}
     105
     106void init_interrupt_context_list(interrupt_context_list_t *list)
     107{
     108        memset(list, 0, sizeof(interrupt_context_list_t));
     109        fibril_mutex_initialize(&list->mutex);
     110        list_initialize(&list->contexts);
     111}
     112
     113void
     114add_interrupt_context(interrupt_context_list_t *list, interrupt_context_t *ctx)
     115{
     116        fibril_mutex_lock(&list->mutex);
     117        ctx->id = list->curr_id++;
     118        list_append(&ctx->link, &list->contexts);
     119        fibril_mutex_unlock(&list->mutex);
     120}
     121
     122void remove_interrupt_context(interrupt_context_list_t *list,
     123    interrupt_context_t *ctx)
     124{
     125        fibril_mutex_lock(&list->mutex);
     126        list_remove(&ctx->link);
     127        fibril_mutex_unlock(&list->mutex);
     128}
     129
     130interrupt_context_t *
     131find_interrupt_context_by_id(interrupt_context_list_t *list, int id)
     132{
     133        fibril_mutex_lock(&list->mutex);
     134       
     135        link_t *link = list->contexts.next;
     136        interrupt_context_t *ctx;
     137       
     138        while (link != &list->contexts) {
     139                ctx = list_get_instance(link, interrupt_context_t, link);
     140                if (ctx->id == id) {
     141                        fibril_mutex_unlock(&list->mutex);
     142                        return ctx;
     143                }
     144                link = link->next;
     145        }
     146       
     147        fibril_mutex_unlock(&list->mutex);
     148        return NULL;
     149}
     150
     151interrupt_context_t *
     152find_interrupt_context(interrupt_context_list_t *list, device_t *dev, int irq)
     153{
     154        fibril_mutex_lock(&list->mutex);
     155       
     156        link_t *link = list->contexts.next;
     157        interrupt_context_t *ctx;
     158       
     159        while (link != &list->contexts) {
     160                ctx = list_get_instance(link, interrupt_context_t, link);
     161                if (ctx->irq == irq && ctx->dev == dev) {
     162                        fibril_mutex_unlock(&list->mutex);
     163                        return ctx;
     164                }
     165                link = link->next;
     166        }
     167       
     168        fibril_mutex_unlock(&list->mutex);
     169        return NULL;
     170}
     171
    90172
    91173int
     
    101183        add_interrupt_context(&interrupt_contexts, ctx);
    102184       
    103         if (NULL == pseudocode)
     185        if (pseudocode == NULL)
    104186                pseudocode = &default_pseudocode;
    105187       
    106188        int res = ipc_register_irq(irq, dev->handle, ctx->id, pseudocode);
    107         if (0 != res) {
     189        if (res != EOK) {
    108190                remove_interrupt_context(&interrupt_contexts, ctx);
    109191                delete_interrupt_context(ctx);
     
    118200            dev, irq);
    119201        int res = ipc_unregister_irq(irq, dev->handle);
    120 
    121         if (NULL != ctx) {
     202       
     203        if (ctx != NULL) {
    122204                remove_interrupt_context(&interrupt_contexts, ctx);
    123205                delete_interrupt_context(ctx);
    124206        }
     207       
    125208        return res;
    126209}
     
    140223}
    141224
    142 static device_t * driver_get_device(link_t *devices, devman_handle_t handle)
     225static device_t *driver_get_device(link_t *devices, devman_handle_t handle)
    143226{
    144227        device_t *dev = NULL;
     
    146229        fibril_mutex_lock(&devices_mutex);
    147230        link_t *link = devices->next;
     231       
    148232        while (link != devices) {
    149233                dev = list_get_instance(link, device_t, link);
    150                 if (handle == dev->handle) {
     234                if (dev->handle == handle) {
    151235                        fibril_mutex_unlock(&devices_mutex);
    152236                        return dev;
     
    154238                link = link->next;
    155239        }
     240       
    156241        fibril_mutex_unlock(&devices_mutex);
    157 
     242       
    158243        return NULL;
    159244}
     
    162247{
    163248        char *dev_name = NULL;
    164         int res = EOK;
    165        
    166         devman_handle_t dev_handle =  IPC_GET_ARG1(*icall);
     249        int res;
     250       
     251        devman_handle_t dev_handle = IPC_GET_ARG1(*icall);
    167252        devman_handle_t parent_dev_handle = IPC_GET_ARG2(*icall);
    168    
     253       
    169254        device_t *dev = create_device();
    170255        dev->handle = dev_handle;
     
    177262       
    178263        res = driver->driver_ops->add_device(dev);
    179         if (0 == res) {
     264        if (res == EOK) {
    180265                printf("%s: new device with handle=%" PRIun " was added.\n",
    181266                    driver->name, dev_handle);
     
    194279        /* Accept connection */
    195280        ipc_answer_0(iid, EOK);
    196 
     281       
    197282        bool cont = true;
    198283        while (cont) {
    199284                ipc_call_t call;
    200285                ipc_callid_t callid = async_get_call(&call);
    201 
     286               
    202287                switch (IPC_GET_IMETHOD(call)) {
    203288                case IPC_M_PHONE_HUNGUP:
     
    240325         * use the device.
    241326         */
    242 
     327       
    243328        int ret = EOK;
    244329        /* open the device */
    245         if (NULL != dev->ops && NULL != dev->ops->open)
     330        if (dev->ops != NULL && dev->ops->open != NULL)
    246331                ret = (*dev->ops->open)(dev);
    247332       
    248         ipc_answer_0(iid, ret); 
    249         if (EOK != ret)
     333        ipc_answer_0(iid, ret);
     334        if (ret != EOK)
    250335                return;
    251 
     336       
    252337        while (1) {
    253338                ipc_callid_t callid;
     
    258343               
    259344                switch  (method) {
    260                 case IPC_M_PHONE_HUNGUP:               
     345                case IPC_M_PHONE_HUNGUP:
    261346                        /* close the device */
    262                         if (NULL != dev->ops && NULL != dev->ops->close)
     347                        if (dev->ops != NULL && dev->ops->close != NULL)
    263348                                (*dev->ops->close)(dev);
    264349                        ipc_answer_0(callid, EOK);
    265350                        return;
    266                 default:               
     351                default:
    267352                        /* convert ipc interface id to interface index */
    268353                       
     
    272357                                remote_handler_t *default_handler =
    273358                                    device_get_default_handler(dev);
    274                                 if (NULL != default_handler) {
     359                                if (default_handler != NULL) {
    275360                                        (*default_handler)(dev, callid, &call);
    276361                                        break;
     
    286371                                break;
    287372                        }
    288 
     373                       
    289374                        /* calling one of the device's interfaces */
    290375                       
    291                         /* get the device interface structure */
    292                         void *iface = device_get_iface(dev, iface_idx);
    293                         if (NULL == iface) {
     376                        /* Get the interface ops structure. */
     377                        void *ops = device_get_ops(dev, iface_idx);
     378                        if (ops == NULL) {
    294379                                printf("%s: driver_connection_gen error - ",
    295380                                    driver->name);
     
    299384                                break;
    300385                        }
    301 
     386                       
    302387                        /*
    303388                         * Get the corresponding interface for remote request
    304389                         * handling ("remote interface").
    305390                         */
    306                         remote_iface_t* rem_iface = get_remote_iface(iface_idx);
    307                         assert(NULL != rem_iface);
    308 
     391                        remote_iface_t *rem_iface = get_remote_iface(iface_idx);
     392                        assert(rem_iface != NULL);
     393                       
    309394                        /* get the method of the remote interface */
    310395                        sysarg_t iface_method_idx = IPC_GET_ARG1(call);
    311396                        remote_iface_func_ptr_t iface_method_ptr =
    312397                            get_remote_method(rem_iface, iface_method_idx);
    313                         if (NULL == iface_method_ptr) {
     398                        if (iface_method_ptr == NULL) {
    314399                                // the interface has not such method
    315400                                printf("%s: driver_connection_gen error - "
     
    325410                         * associated with the device by its driver.
    326411                         */
    327                         (*iface_method_ptr)(dev, iface, callid, &call);
     412                        (*iface_method_ptr)(dev, ops, callid, &call);
    328413                        break;
    329414                }
     
    348433        switch ((sysarg_t) (IPC_GET_ARG1(*icall))) {
    349434        case DRIVER_DEVMAN:
    350                 /* handle PnP events from device manager */
     435                /* Handle request from device manager */
    351436                driver_connection_devman(iid, icall);
    352437                break;
    353438        case DRIVER_DRIVER:
    354                 /* handle request from drivers of child devices */
     439                /* Handle request from drivers of child devices */
    355440                driver_connection_driver(iid, icall);
    356441                break;
    357442        case DRIVER_CLIENT:
    358                 /* handle requests from client applications */
     443                /* Handle request from client applications */
    359444                driver_connection_client(iid, icall);
    360445                break;
    361 
    362446        default:
    363447                /* No such interface */
     
    366450}
    367451
     452/** Create new device structure.
     453 *
     454 * @return              The device structure.
     455 */
     456device_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
     465        return dev;
     466}
     467
     468/** Delete device structure.
     469 *
     470 * @param dev           The device structure.
     471 */
     472void delete_device(device_t *dev)
     473{
     474        clean_match_ids(&dev->match_ids);
     475        if (dev->name != NULL)
     476                free(dev->name);
     477        free(dev);
     478}
     479
     480void *device_get_ops(device_t *dev, dev_inferface_idx_t idx)
     481{
     482        assert(is_valid_iface_idx(idx));
     483        if (dev->ops == NULL)
     484                return NULL;
     485        return dev->ops->interfaces[idx];
     486}
     487
    368488int child_device_register(device_t *child, device_t *parent)
    369489{
    370         assert(NULL != child->name);
    371 
     490        assert(child->name != NULL);
     491       
    372492        int res;
    373493       
     
    375495        res = devman_child_device_register(child->name, &child->match_ids,
    376496            parent->handle, &child->handle);
    377         if (EOK == res)
     497        if (res != EOK) {
     498                remove_from_devices_list(child);
    378499                return res;
    379         remove_from_devices_list(child);       
     500        }
     501       
    380502        return res;
    381503}
     
    396518        match_id_t *match_id = NULL;
    397519        int rc;
    398 
     520       
    399521        child = create_device();
    400522        if (child == NULL) {
     
    402524                goto failure;
    403525        }
    404 
     526       
    405527        child->name = child_name;
    406 
     528       
    407529        match_id = create_match_id();
    408530        if (match_id == NULL) {
     
    410532                goto failure;
    411533        }
    412 
     534       
    413535        match_id->id = child_match_id;
    414536        match_id->score = child_match_score;
    415537        add_match_id(&child->match_ids, match_id);
    416 
     538       
    417539        rc = child_device_register(child, parent);
    418         if (EOK != rc)
     540        if (rc != EOK)
    419541                goto failure;
    420542
     
    422544                *child_handle = child->handle;
    423545        }
     546
    424547        return EOK;
    425 
     548       
    426549failure:
    427550        if (match_id != NULL) {
     
    429552                delete_match_id(match_id);
    430553        }
    431 
     554       
    432555        if (child != NULL) {
    433556                child->name = NULL;
    434557                delete_device(child);
    435558        }
    436 
     559       
    437560        return rc;
     561}
     562
     563/** Get default handler for client requests */
     564remote_handler_t *device_get_default_handler(device_t *dev)
     565{
     566        if (dev->ops == NULL)
     567                return NULL;
     568        return dev->ops->default_handler;
     569}
     570
     571int add_device_to_class(device_t *dev, const char *class_name)
     572{
     573        return devman_add_device_to_class(dev->handle, class_name);
    438574}
    439575
     
    445581         */
    446582        driver = drv;
    447 
     583       
    448584        /* Initialize the list of interrupt contexts. */
    449585        init_interrupt_context_list(&interrupt_contexts);
     
    457593         */
    458594        devman_driver_register(driver->name, driver_connection);
    459 
     595       
    460596        async_manager();
    461 
     597       
    462598        /* Never reached. */
    463599        return 0;
  • uspace/lib/drv/generic/remote_char_dev.c

    ra97ea0f r6610565b  
    3737#include <errno.h>
    3838
    39 #include "char.h"
     39#include "ops/char_dev.h"
    4040#include "driver.h"
    4141
     
    4646
    4747/** Remote character interface operations. */
    48 static remote_iface_func_ptr_t remote_char_iface_ops [] = {
     48static remote_iface_func_ptr_t remote_char_dev_iface_ops[] = {
    4949        &remote_char_read,
    5050        &remote_char_write
     
    5656 * character interface.
    5757 */
    58 remote_iface_t remote_char_iface = {
    59         .method_count = sizeof(remote_char_iface_ops) /
     58remote_iface_t remote_char_dev_iface = {
     59        .method_count = sizeof(remote_char_dev_iface_ops) /
    6060            sizeof(remote_iface_func_ptr_t),
    61         .methods = remote_char_iface_ops
     61        .methods = remote_char_dev_iface_ops
    6262};
    6363
     
    6969 *
    7070 * @param dev           The device from which the data are read.
    71  * @param iface         The local interface structure.
     71 * @param ops           The local ops structure.
    7272 */
    7373static void
    74 remote_char_read(device_t *dev, void *iface, ipc_callid_t callid,
     74remote_char_read(device_t *dev, void *ops, ipc_callid_t callid,
    7575    ipc_call_t *call)
    76 {       
    77         char_iface_t *char_iface = (char_iface_t *) iface;
     76{
     77        char_dev_ops_t *char_dev_ops = (char_dev_ops_t *) ops;
    7878        ipc_callid_t cid;
    7979       
     
    8585        }
    8686       
    87         if (!char_iface->read) {
     87        if (!char_dev_ops->read) {
    8888                async_data_read_finalize(cid, NULL, 0);
    8989                ipc_answer_0(callid, ENOTSUP);
     
    9595       
    9696        char buf[MAX_CHAR_RW_COUNT];
    97         int ret = (*char_iface->read)(dev, buf, len);
     97        int ret = (*char_dev_ops->read)(dev, buf, len);
    9898       
    9999        if (ret < 0) {
     
    116116 *
    117117 * @param dev           The device to which the data are written.
    118  * @param iface         The local interface structure.
     118 * @param ops           The local ops structure.
    119119 */
    120120static void
    121 remote_char_write(device_t *dev, void *iface, ipc_callid_t callid,
     121remote_char_write(device_t *dev, void *ops, ipc_callid_t callid,
    122122    ipc_call_t *call)
    123123{
    124         char_iface_t *char_iface = (char_iface_t *) iface;
     124        char_dev_ops_t *char_dev_ops = (char_dev_ops_t *) ops;
    125125        ipc_callid_t cid;
    126126        size_t len;
     
    132132        }
    133133       
    134         if (!char_iface->write) {
     134        if (!char_dev_ops->write) {
    135135                async_data_write_finalize(cid, NULL, 0);
    136136                ipc_answer_0(callid, ENOTSUP);
    137137                return;
    138         }       
     138        }
    139139       
    140140        if (len > MAX_CHAR_RW_COUNT)
     
    145145        async_data_write_finalize(cid, buf, len);
    146146       
    147         int ret = (*char_iface->write)(dev, buf, len);
     147        int ret = (*char_dev_ops->write)(dev, buf, len);
    148148        if (ret < 0) {
    149149                /* Some error occured. */
  • uspace/lib/drv/generic/remote_hw_res.c

    ra97ea0f r6610565b  
    3737#include <errno.h>
    3838
     39#include "ops/hw_res.h"
    3940#include "driver.h"
    40 #include "resource.h"
    4141
    42 static void remote_res_get_resources(device_t *, void *, ipc_callid_t,
     42static void remote_hw_res_get_resource_list(device_t *, void *, ipc_callid_t,
    4343    ipc_call_t *);
    44 static void remote_res_enable_interrupt(device_t *, void *, ipc_callid_t,
     44static void remote_hw_res_enable_interrupt(device_t *, void *, ipc_callid_t,
    4545    ipc_call_t *);
    4646
    47 static remote_iface_func_ptr_t remote_res_iface_ops [] = {
    48         &remote_res_get_resources,
    49         &remote_res_enable_interrupt
     47static remote_iface_func_ptr_t remote_hw_res_iface_ops [] = {
     48        &remote_hw_res_get_resource_list,
     49        &remote_hw_res_enable_interrupt
    5050};
    5151
    52 remote_iface_t remote_res_iface = {
    53         .method_count = sizeof(remote_res_iface_ops) /
     52remote_iface_t remote_hw_res_iface = {
     53        .method_count = sizeof(remote_hw_res_iface_ops) /
    5454            sizeof(remote_iface_func_ptr_t),
    55         .methods = remote_res_iface_ops
     55        .methods = remote_hw_res_iface_ops
    5656};
    5757
    58 static void remote_res_enable_interrupt(device_t *dev, void *iface,
     58static void remote_hw_res_enable_interrupt(device_t *dev, void *ops,
    5959    ipc_callid_t callid, ipc_call_t *call)
    6060{
    61         resource_iface_t *ires = (resource_iface_t *) iface;
     61        hw_res_ops_t *hw_res_ops = (hw_res_ops_t *) ops;
    6262       
    63         if (NULL == ires->enable_interrupt)
     63        if (hw_res_ops->enable_interrupt == NULL)
    6464                ipc_answer_0(callid, ENOTSUP);
    65         else if (ires->enable_interrupt(dev))
     65        else if (hw_res_ops->enable_interrupt(dev))
    6666                ipc_answer_0(callid, EOK);
    6767        else
     
    6969}
    7070
    71 static void remote_res_get_resources(device_t *dev, void *iface,
     71static void remote_hw_res_get_resource_list(device_t *dev, void *ops,
    7272    ipc_callid_t callid, ipc_call_t *call)
    7373{
    74         resource_iface_t *ires = (resource_iface_t *) iface;
    75         if (NULL == ires->get_resources) {
     74        hw_res_ops_t *hw_res_ops = (hw_res_ops_t *) ops;
     75
     76        if (hw_res_ops->get_resource_list == NULL) {
    7677                ipc_answer_0(callid, ENOTSUP);
    7778                return;
    7879        }
    7980       
    80         hw_resource_list_t *hw_resources = ires->get_resources(dev);
    81         if (NULL == hw_resources){
     81        hw_resource_list_t *hw_resources = hw_res_ops->get_resource_list(dev);
     82        if (hw_resources == NULL){
    8283                ipc_answer_0(callid, ENOENT);
    8384                return;
    84         }       
     85        }
    8586       
    86         ipc_answer_1(callid, EOK, hw_resources->count); 
     87        ipc_answer_1(callid, EOK, hw_resources->count);
    8788
    8889        size_t len;
    8990        if (!async_data_read_receive(&callid, &len)) {
    90                 /* protocol error - the recipient is not accepting data */
     91                /* Protocol error - the recipient is not accepting data */
    9192                return;
    9293        }
  • uspace/lib/drv/include/dev_iface.h

    ra97ea0f r6610565b  
    3636#define LIBDRV_DEV_IFACE_H_
    3737
    38 #include "driver.h"
     38#include <ipc/dev_iface.h>
    3939
    40 /* TODO declare device interface structures here */
     40/*
     41 * Device interface
     42 */
     43
     44struct device;
     45
     46/*
     47 * First two parameters: device and interface structure registered by the
     48 * devices driver.
     49 */
     50typedef void remote_iface_func_t(struct device *, void *, ipc_callid_t,
     51    ipc_call_t *);
     52typedef remote_iface_func_t *remote_iface_func_ptr_t;
     53typedef void remote_handler_t(struct device *, ipc_callid_t, ipc_call_t *);
     54
     55typedef struct {
     56        size_t method_count;
     57        remote_iface_func_ptr_t *methods;
     58} remote_iface_t;
     59
     60typedef struct {
     61        remote_iface_t *ifaces[DEV_IFACE_COUNT];
     62} iface_dipatch_table_t;
     63
     64extern remote_iface_t *get_remote_iface(int);
     65extern remote_iface_func_ptr_t get_remote_method(remote_iface_t *, sysarg_t);
     66
     67
     68extern bool is_valid_iface_idx(int);
    4169
    4270#endif
  • uspace/lib/drv/include/driver.h

    ra97ea0f r6610565b  
    4141#include <ipc/devman.h>
    4242#include <ipc/dev_iface.h>
    43 #include <device/hw_res.h>
    44 #include <device/char.h>
    4543#include <assert.h>
    4644#include <ddi.h>
     
    4947#include <malloc.h>
    5048
     49#include "dev_iface.h"
     50
    5151struct device;
    5252typedef struct device device_t;
    5353
    54 /* device interface */
     54/*
     55 * Device class
     56 */
    5557
    56 /*
    57  * First two parameters: device and interface structure registered by the
    58  * devices driver.
    59  */
    60 typedef void remote_iface_func_t(device_t *, void *, ipc_callid_t,
    61     ipc_call_t *);
    62 typedef remote_iface_func_t *remote_iface_func_ptr_t;
    63 typedef void remote_handler_t(device_t *, ipc_callid_t, ipc_call_t *);
    64 
    65 typedef struct {
    66         size_t method_count;
    67         remote_iface_func_ptr_t *methods;
    68 } remote_iface_t;
    69 
    70 typedef struct {
    71         remote_iface_t *ifaces[DEV_IFACE_COUNT];
    72 } iface_dipatch_table_t;
    73 
    74 
    75 static inline bool is_valid_iface_idx(int idx)
    76 {
    77         return 0 <= idx && idx < DEV_IFACE_MAX;
    78 }
    79 
    80 remote_iface_t *get_remote_iface(int);
    81 remote_iface_func_ptr_t get_remote_method(remote_iface_t *, sysarg_t);
    82 
    83 
    84 /* device class */
    85 
    86 /** Devices operations. */
     58/** Devices operations */
    8759typedef struct device_ops {
    8860        /**
     
    11082
    11183
    112 /* device */
     84/*
     85 * Device
     86 */
    11387
    114 /** The device. */
     88/** Device structure */
    11589struct device {
    11690        /**
     
    12195       
    12296        /**
    123          * The phone to the parent device driver (if it is different from this
    124          * driver).
     97         * Phone to the parent device driver (if it is different from this
     98         * driver)
    12599         */
    126100        int parent_phone;
    127101       
    128         /** Parent device if handled by this driver, NULL otherwise. */
     102        /** Parent device if handled by this driver, NULL otherwise */
    129103        device_t *parent;
    130         /** The device's name. */
     104        /** Device name */
    131105        const char *name;
    132         /** The list of device ids for device-to-driver matching. */
     106        /** List of device ids for device-to-driver matching */
    133107        match_id_list_t match_ids;
    134         /** The device driver's data associated with this device. */
     108        /** Driver-specific data associated with this device */
    135109        void *driver_data;
    136         /** The implementation of operations provided by this device. */
     110        /** The implementation of operations provided by this device */
    137111        device_ops_t *ops;
    138112       
    139         /**
    140          * Pointer to the previous and next device in the list of devices
    141          * handled by the driver.
    142          */
     113        /** Link in the list of devices handled by the driver */
    143114        link_t link;
    144115};
    145116
     117/*
     118 * Driver
     119 */
    146120
    147 /* driver */
    148 
    149 /** Generic device driver operations. */
     121/** Generic device driver operations */
    150122typedef struct driver_ops {
    151         /** Callback method for passing a new device to the device driver.*/
     123        /** Callback method for passing a new device to the device driver */
    152124        int (*add_device)(device_t *dev);
    153         /* TODO add other generic driver operations */
     125        /* TODO: add other generic driver operations */
    154126} driver_ops_t;
    155127
    156 /** The driver structure.*/
     128/** Driver structure */
    157129typedef struct driver {
    158         /** The name of the device driver. */
     130        /** Name of the device driver */
    159131        const char *name;
    160         /** Generic device driver operations. */
     132        /** Generic device driver operations */
    161133        driver_ops_t *driver_ops;
    162134} driver_t;
     
    168140 * @return              The device structure.
    169141 */
    170 static inline device_t *create_device(void)
    171 {
    172         device_t *dev = malloc(sizeof(device_t));
    173         if (NULL != dev) {
    174                 memset(dev, 0, sizeof(device_t));
    175                 init_match_ids(&dev->match_ids);
    176         }       
    177         return dev;
    178 }
     142extern device_t *create_device(void);
     143extern void delete_device(device_t *);
     144extern void *device_get_ops(device_t *, dev_inferface_idx_t);
    179145
    180 /** Delete device structure.
    181  *
    182  * @param dev           The device structure.
     146extern int child_device_register(device_t *, device_t *);
     147extern int child_device_register_wrapper(device_t *, const char *, const char *,
     148    int, devman_handle_t *);
     149
     150/*
     151 * Interrupts
    183152 */
    184 static inline void delete_device(device_t *dev)
    185 {
    186         clean_match_ids(&dev->match_ids);
    187         if (NULL != dev->name)
    188                 free(dev->name);
    189         free(dev);
    190 }
    191 
    192 static inline void *device_get_iface(device_t *dev, dev_inferface_idx_t idx)
    193 {
    194         assert(is_valid_iface_idx(idx));
    195         if (NULL == dev->ops)
    196                 return NULL;
    197         return dev->ops->interfaces[idx];
    198 }
    199 
    200 int child_device_register(device_t *, device_t *);
    201 int child_device_register_wrapper(device_t *, const char *, const char *, int,
    202     devman_handle_t *);
    203 
    204 
    205 /* interrupts */
    206153
    207154typedef void interrupt_handler_t(device_t *, ipc_callid_t, ipc_call_t *);
     
    221168} interrupt_context_list_t;
    222169
    223 static inline interrupt_context_t *create_interrupt_context(void)
    224 {
    225         interrupt_context_t *ctx;
    226        
    227         ctx = (interrupt_context_t *) malloc(sizeof(interrupt_context_t));
    228         if (NULL != ctx)
    229                 memset(ctx, 0, sizeof(interrupt_context_t));
    230        
    231         return ctx;
    232 }
     170extern interrupt_context_t *create_interrupt_context(void);
     171extern void delete_interrupt_context(interrupt_context_t *);
     172extern void init_interrupt_context_list(interrupt_context_list_t *);
     173extern void add_interrupt_context(interrupt_context_list_t *,
     174    interrupt_context_t *);
     175extern void remove_interrupt_context(interrupt_context_list_t *,
     176    interrupt_context_t *);
     177extern interrupt_context_t *find_interrupt_context_by_id(
     178    interrupt_context_list_t *, int);
     179extern interrupt_context_t *find_interrupt_context(
     180    interrupt_context_list_t *, device_t *, int);
    233181
    234 static inline void delete_interrupt_context(interrupt_context_t *ctx)
    235 {
    236         if (NULL != ctx)
    237                 free(ctx);
    238 }
     182extern int register_interrupt_handler(device_t *, int, interrupt_handler_t *,
     183    irq_code_t *);
     184extern int unregister_interrupt_handler(device_t *, int);
    239185
    240 static inline void init_interrupt_context_list(interrupt_context_list_t *list)
    241 {
    242         memset(list, 0, sizeof(interrupt_context_list_t));
    243         fibril_mutex_initialize(&list->mutex);
    244         list_initialize(&list->contexts);
    245 }
    246 
    247 static inline void
    248 add_interrupt_context(interrupt_context_list_t *list, interrupt_context_t *ctx)
    249 {
    250         fibril_mutex_lock(&list->mutex);
    251         ctx->id = list->curr_id++;
    252         list_append(&ctx->link, &list->contexts);
    253         fibril_mutex_unlock(&list->mutex);
    254 }
    255 
    256 static inline void
    257 remove_interrupt_context(interrupt_context_list_t *list,
    258     interrupt_context_t *ctx)
    259 {
    260         fibril_mutex_lock(&list->mutex);
    261         list_remove(&ctx->link);
    262         fibril_mutex_unlock(&list->mutex);
    263 }
    264 
    265 static inline interrupt_context_t *
    266 find_interrupt_context_by_id(interrupt_context_list_t *list, int id)
    267 {
    268         fibril_mutex_lock(&list->mutex);
    269        
    270         link_t *link = list->contexts.next;
    271         interrupt_context_t *ctx;
    272        
    273         while (link != &list->contexts) {
    274                 ctx = list_get_instance(link, interrupt_context_t, link);
    275                 if (id == ctx->id) {
    276                         fibril_mutex_unlock(&list->mutex);
    277                         return ctx;
    278                 }
    279                 link = link->next;
    280         }
    281        
    282         fibril_mutex_unlock(&list->mutex);
    283         return NULL;
    284 }
    285 
    286 static inline interrupt_context_t *
    287 find_interrupt_context(interrupt_context_list_t *list, device_t *dev, int irq)
    288 {
    289         fibril_mutex_lock(&list->mutex);
    290        
    291         link_t *link = list->contexts.next;
    292         interrupt_context_t *ctx;
    293        
    294         while (link != &list->contexts) {
    295                 ctx = list_get_instance(link, interrupt_context_t, link);
    296                 if (irq == ctx->irq && dev == ctx->dev) {
    297                         fibril_mutex_unlock(&list->mutex);
    298                         return ctx;
    299                 }
    300                 link = link->next;
    301         }
    302        
    303         fibril_mutex_unlock(&list->mutex);
    304         return NULL;
    305 }
    306 
    307 int register_interrupt_handler(device_t *, int, interrupt_handler_t *,
    308     irq_code_t *);
    309 int unregister_interrupt_handler(device_t *, int);
    310 
    311 
    312 /* default handler for client requests */
    313 
    314 static inline remote_handler_t *device_get_default_handler(device_t *dev)
    315 {
    316         if (NULL == dev->ops)
    317                 return NULL;
    318         return dev->ops->default_handler;
    319 }
    320 
    321 static inline int add_device_to_class(device_t *dev, const char *class_name)
    322 {
    323         return devman_add_device_to_class(dev->handle, class_name);
    324 }
     186extern remote_handler_t *device_get_default_handler(device_t *);
     187extern int add_device_to_class(device_t *, const char *);
    325188
    326189#endif
  • uspace/lib/drv/include/ops/char_dev.h

    ra97ea0f r6610565b  
    3333 */
    3434
    35 #ifndef LIBDRV_CHAR_H_
    36 #define LIBDRV_CHAR_H_
     35#ifndef LIBDRV_OPS_CHAR_DEV_H_
     36#define LIBDRV_OPS_CHAR_DEV_H_
    3737
    38 #include "driver.h"
     38#include "../driver.h"
    3939
    40 typedef struct char_iface {
     40typedef struct {
    4141        int (*read)(device_t *, char *, size_t);
    4242        int (*write)(device_t *, char *, size_t);
    43 } char_iface_t;
     43} char_dev_ops_t;
    4444
    4545#endif
  • uspace/lib/drv/include/ops/hw_res.h

    ra97ea0f r6610565b  
    3333 */
    3434
    35 #ifndef LIBDRV_RESOURCE_H_
    36 #define LIBDRV_RESOURCE_H_
     35#ifndef LIBDRV_OPS_HW_RES_H_
     36#define LIBDRV_OPS_HW_RES_H_
    3737
    38 #include "driver.h"
     38#include <device/hw_res.h>
     39#include <sys/types.h>
    3940
    40 typedef struct resource_iface {
    41          hw_resource_list_t *(* get_resources)(device_t *);
     41#include "../driver.h"
     42
     43typedef struct {
     44         hw_resource_list_t *(*get_resource_list)(device_t *);
    4245         bool (*enable_interrupt)(device_t *);
    43 } resource_iface_t;
    44 
     46} hw_res_ops_t;
    4547
    4648#endif
  • uspace/lib/drv/include/remote_char_dev.h

    ra97ea0f r6610565b  
    3333 */
    3434
    35 #ifndef LIBDRV_REMOTE_RES_H_
    36 #define LIBDRV_REMOTE_RES_H_
     35#ifndef LIBDRV_REMOTE_CHAR_DEV_H_
     36#define LIBDRV_REMOTE_CHAR_DEV_H_
    3737
    38 remote_iface_t remote_res_iface;
     38extern remote_iface_t remote_char_dev_iface;
    3939
    4040#endif
  • uspace/lib/drv/include/remote_hw_res.h

    ra97ea0f r6610565b  
    3333 */
    3434
    35 #ifndef LIBDRV_REMOTE_CHAR_H_
    36 #define LIBDRV_REMOTE_CHAR_H_
     35#ifndef LIBDRV_REMOTE_HW_RES_H_
     36#define LIBDRV_REMOTE_HW_RES_H_
    3737
    38 remote_iface_t remote_char_iface;
     38extern remote_iface_t remote_hw_res_iface;
    3939
    4040#endif
Note: See TracChangeset for help on using the changeset viewer.