Fork us on GitHub Follow us on Facebook Follow us on Twitter

Changeset a1769ee in mainline


Ignore:
Timestamp:
2010-03-31T20:30:54Z (12 years ago)
Author:
Lenka Trochtova <trochtova.lenka@…>
Branches:
lfn, master
Children:
52b7b1bb
Parents:
eff1a590
Message:

device interfaces - parts of code

Location:
uspace/lib
Files:
3 added
2 edited

Legend:

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

    reff1a590 ra1769ee  
    5858LIST_INITIALIZE(devices);
    5959
    60 static device_t* driver_create_device()
     60static device_t * driver_create_device()
    6161{
    6262        device_t *dev = (device_t *)malloc(sizeof(device_t));
     
    6565        }       
    6666        return dev;     
     67}
     68
     69static device_t * driver_get_device(link_t *devices, device_handle_t handle)
     70{       
     71        device_t *dev = NULL;
     72        link_t *link = devices->next;
     73       
     74        while (link != devices) {
     75                dev = list_get_instance(link, device_t, link);
     76                if (handle == dev->handle) {
     77                        return dev;
     78                }
     79        }
     80       
     81        return NULL;
    6782}
    6883
     
    8297        printf("%s: new device with handle = %x was added.\n", driver->name, dev_handle);
    8398       
    84         ipcarg_t r = ipc_answer_1(iid, EOK, ret);
     99        ipc_answer_1(iid, EOK, ret);
    85100}
    86101
     
    111126}
    112127
     128/**
     129 * Generic client connection handler both for applications and drivers.
     130 *
     131 * @param driver true for driver client, false for other clients (applications, services etc.).
     132 */
     133static void driver_connection_gen(ipc_callid_t iid, ipc_call_t *icall, bool driver) {
     134        /*
     135         * Answer the first IPC_M_CONNECT_ME_TO call and remember the handle of the device to which the client connected.
     136         */
     137        device_handle_t handle = IPC_GET_ARG1(*icall);
     138        device_t *dev = driver_get_device(&devices, handle);
     139       
     140        if (dev == NULL) {
     141                ipc_answer_0(iid, ENOENT);
     142                return;
     143        }
     144       
     145        // TODO introduce generic device interface for opening and closing devices
     146        // and call its open callback here to find out wheter the device can be used by the connecting client
     147               
     148
     149        ipc_answer_0(iid, EOK);
     150       
     151        while (1) {
     152                ipc_callid_t callid;
     153                ipc_call_t call;
     154       
     155                callid = async_get_call(&call);
     156                ipcarg_t method = IPC_GET_METHOD(call);
     157                switch  (method) {
     158                case IPC_M_PHONE_HUNGUP:
     159                        // TODO close the device
     160                        ipc_answer_0(callid, EOK);
     161                        return;
     162                default:
     163                        if (DEV_IFACE_FIRST <= method && method < DEV_IFACE_MAX) {
     164                                // TODO - try to find interface, if supported
     165                                // otherwise return  ENOTSUP                           
     166                        } else {
     167                                ipc_answer_0(callid, ENOTSUP);
     168                        }
     169                        break;
     170                }
     171        }
     172}
     173
    113174static void driver_connection_driver(ipc_callid_t iid, ipc_call_t *icall)
    114175{
    115         // TODO later
     176        driver_connection_gen(iid, icall, true);
    116177}
    117178
    118179static void driver_connection_client(ipc_callid_t iid, ipc_call_t *icall)
    119180{
    120         // TODO later
     181        driver_connection_gen(iid, icall, false);
    121182}
    122183
  • uspace/lib/libdrv/include/driver.h

    reff1a590 ra1769ee  
    11/*
    2  * Copyright (c) 2010 Lenka Trochtova 
     2 * Copyright (c) 2010 Lenka Trochtova
    33 * All rights reserved.
    44 *
     
    3838#include <adt/list.h>
    3939#include <ipc/devman.h>
     40#include <ipc/dev_iface.h>
    4041
    41 typedef struct device {
     42struct device;
     43typedef struct device device_t;
     44
     45// device interface
     46
     47// first two parameters: device and interface structure registered by the devices driver
     48typedef void remote_iface_func_t(device_t*, void *, ipc_callid_t, ipc_call_t *);
     49typedef remote_iface_func_t *remote_iface_func_ptr_t;
     50
     51typedef struct {
     52        int method_count;
     53        remote_iface_func_ptr_t *methods;
     54} remote_iface_t;
     55
     56typedef struct {
     57        remote_iface_t * ifaces[DEV_IFACE_COUNT];
     58} iface_dipatch_table_t;
     59
     60static inline int get_iface_index(dev_inferface_id_t id)
     61{
     62        return id - DEV_IFACE_FIRST;
     63}
     64
     65static inline bool is_valid_iface_id(dev_inferface_id_t id)
     66{
     67        return DEV_IFACE_FIRST <= id && id < DEV_IFACE_MAX;
     68}
     69
     70// device
     71
     72struct device {
    4273        device_handle_t handle;
    43         ipcarg_t parent_phone; 
     74        ipcarg_t parent_phone;
    4475        const char *name;
    4576        match_id_list_t match_ids;
    4677        void *driver_data;
    47        
     78        void *interfaces[DEV_IFACE_COUNT];
     79
    4880        // TODO add more items
    49        
     81
    5082        link_t link;
    51 } device_t;
     83};
    5284
    53 typedef struct driver_ops {     
     85
     86// driver
     87
     88typedef struct driver_ops {
    5489        bool (*add_device)(device_t *dev);
    5590        // TODO add other generic driver operations
     
    6196} driver_t;
    6297
     98
     99
     100
     101
    63102int driver_main(driver_t *drv);
    64103
     
    68107        if (NULL != dev) {
    69108                memset(dev, 0, sizeof(device_t));
    70         }       
     109        }
    71110        list_initialize(&dev->match_ids.ids);
    72111        return dev;
    73112}
    74113
    75 static inline void delete_device(device_t *dev) {
     114static inline void delete_device(device_t *dev)
     115{
    76116        clean_match_ids(&dev->match_ids);
    77117        if (NULL != dev->name) {
     
    79119        }
    80120        free(dev);
     121}
     122
     123static inline void device_set_iface (device_t *dev, dev_inferface_id_t id, void *iface)
     124{
     125        assert(is_valid_iface_id(id));
     126
     127        int idx = get_iface_index(id);
     128        dev->interfaces[idx] = iface;
    81129}
    82130
Note: See TracChangeset for help on using the changeset viewer.