| 1 | /*
|
|---|
| 2 | * Copyright (c) 2010 Lenka Trochtova
|
|---|
| 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 13 | * documentation and/or other materials provided with the distribution.
|
|---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | /** @addtogroup libdrv
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | */
|
|---|
| 34 | #ifndef LIBDRV_DRIVER_H_
|
|---|
| 35 | #define LIBDRV_DRIVER_H_
|
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 | #include <adt/list.h>
|
|---|
| 39 | #include <ipc/devman.h>
|
|---|
| 40 | #include <ipc/dev_iface.h>
|
|---|
| 41 | #include <assert.h>
|
|---|
| 42 |
|
|---|
| 43 | struct device;
|
|---|
| 44 | typedef struct device device_t;
|
|---|
| 45 |
|
|---|
| 46 | // device interface
|
|---|
| 47 |
|
|---|
| 48 | // first two parameters: device and interface structure registered by the devices driver
|
|---|
| 49 | typedef void remote_iface_func_t(device_t*, void *, ipc_callid_t, ipc_call_t *);
|
|---|
| 50 | typedef remote_iface_func_t *remote_iface_func_ptr_t;
|
|---|
| 51 |
|
|---|
| 52 | typedef struct {
|
|---|
| 53 | size_t method_count;
|
|---|
| 54 | remote_iface_func_ptr_t *methods;
|
|---|
| 55 | } remote_iface_t;
|
|---|
| 56 |
|
|---|
| 57 | typedef struct {
|
|---|
| 58 | remote_iface_t * ifaces[DEV_IFACE_COUNT];
|
|---|
| 59 | } iface_dipatch_table_t;
|
|---|
| 60 |
|
|---|
| 61 | static inline int get_iface_index(dev_inferface_id_t id)
|
|---|
| 62 | {
|
|---|
| 63 | return id - DEV_IFACE_FIRST;
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | static inline bool is_valid_iface_id(dev_inferface_id_t id)
|
|---|
| 67 | {
|
|---|
| 68 | return DEV_IFACE_FIRST <= id && id < DEV_IFACE_MAX;
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | remote_iface_t* get_remote_iface(dev_inferface_id_t id);
|
|---|
| 72 | remote_iface_func_ptr_t get_remote_method(remote_iface_t *rem_iface, ipcarg_t iface_method_idx);
|
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 | // device
|
|---|
| 76 |
|
|---|
| 77 | /** The device. */
|
|---|
| 78 | struct device {
|
|---|
| 79 | /** Globally unique device identifier (assigned to the device by the device manager). */
|
|---|
| 80 | device_handle_t handle;
|
|---|
| 81 | /** The phone to the parent device driver (if it is different from this driver).*/
|
|---|
| 82 | int parent_phone;
|
|---|
| 83 | /** Parent device if handled by this driver, NULL otherwise. */
|
|---|
| 84 | device_t *parent;
|
|---|
| 85 | /** The device's name.*/
|
|---|
| 86 | const char *name;
|
|---|
| 87 | /** The list of device ids for device-to-driver matching.*/
|
|---|
| 88 | match_id_list_t match_ids;
|
|---|
| 89 | /** The device driver's data associated with this device.*/
|
|---|
| 90 | void *driver_data;
|
|---|
| 91 | /** The table of interfaces exported by this device. */
|
|---|
| 92 | void *interfaces[DEV_IFACE_COUNT];
|
|---|
| 93 | /** Pointer to the previous and next device in the list of devices handled by the driver */
|
|---|
| 94 | link_t link;
|
|---|
| 95 | };
|
|---|
| 96 |
|
|---|
| 97 |
|
|---|
| 98 | // driver
|
|---|
| 99 |
|
|---|
| 100 | /** Generic device driver operations. */
|
|---|
| 101 | typedef struct driver_ops {
|
|---|
| 102 | /** Callback method for passing a new device to the device driver.*/
|
|---|
| 103 | bool (*add_device)(device_t *dev);
|
|---|
| 104 | // TODO add other generic driver operations
|
|---|
| 105 | } driver_ops_t;
|
|---|
| 106 |
|
|---|
| 107 | /** The driver structure.*/
|
|---|
| 108 | typedef struct driver {
|
|---|
| 109 | /** The name of the device driver. */
|
|---|
| 110 | const char *name;
|
|---|
| 111 | /** Generic device driver operations. */
|
|---|
| 112 | driver_ops_t *driver_ops;
|
|---|
| 113 | } driver_t;
|
|---|
| 114 |
|
|---|
| 115 |
|
|---|
| 116 |
|
|---|
| 117 |
|
|---|
| 118 |
|
|---|
| 119 | int driver_main(driver_t *drv);
|
|---|
| 120 |
|
|---|
| 121 | /** Create new device structure.
|
|---|
| 122 | *
|
|---|
| 123 | * @return the device structure.
|
|---|
| 124 | */
|
|---|
| 125 | static inline device_t * create_device()
|
|---|
| 126 | {
|
|---|
| 127 | device_t *dev = malloc(sizeof(device_t));
|
|---|
| 128 | if (NULL != dev) {
|
|---|
| 129 | memset(dev, 0, sizeof(device_t));
|
|---|
| 130 | }
|
|---|
| 131 | list_initialize(&dev->match_ids.ids);
|
|---|
| 132 | return dev;
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | /** Delete device structure.
|
|---|
| 136 | *
|
|---|
| 137 | * @param dev the device structure.
|
|---|
| 138 | */
|
|---|
| 139 | static inline void delete_device(device_t *dev)
|
|---|
| 140 | {
|
|---|
| 141 | clean_match_ids(&dev->match_ids);
|
|---|
| 142 | if (NULL != dev->name) {
|
|---|
| 143 | free(dev->name);
|
|---|
| 144 | }
|
|---|
| 145 | free(dev);
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 | static inline void device_set_iface (device_t *dev, dev_inferface_id_t id, void *iface)
|
|---|
| 149 | {
|
|---|
| 150 | assert(is_valid_iface_id(id));
|
|---|
| 151 |
|
|---|
| 152 | int idx = get_iface_index(id);
|
|---|
| 153 | dev->interfaces[idx] = iface;
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| 156 | static inline void * device_get_iface(device_t *dev, dev_inferface_id_t id)
|
|---|
| 157 | {
|
|---|
| 158 | assert(is_valid_iface_id(id));
|
|---|
| 159 |
|
|---|
| 160 | int idx = get_iface_index(id);
|
|---|
| 161 | return dev->interfaces[idx];
|
|---|
| 162 | }
|
|---|
| 163 |
|
|---|
| 164 | bool child_device_register(device_t *child, device_t *parent);
|
|---|
| 165 |
|
|---|
| 166 |
|
|---|
| 167 | #endif
|
|---|
| 168 |
|
|---|
| 169 | /**
|
|---|
| 170 | * @}
|
|---|
| 171 | */
|
|---|