[c16cf62] | 1 | /*
|
---|
[a1769ee] | 2 | * Copyright (c) 2010 Lenka Trochtova
|
---|
[c16cf62] | 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 |
|
---|
[084ff99] | 37 |
|
---|
| 38 | #include <adt/list.h>
|
---|
[7f8b581] | 39 | #include <ipc/ipc.h>
|
---|
[bda60d9] | 40 | #include <ipc/devman.h>
|
---|
[a1769ee] | 41 | #include <ipc/dev_iface.h>
|
---|
[3843ecb] | 42 | #include <device/hw_res.h>
|
---|
[25a7e11d] | 43 | #include <device/char.h>
|
---|
[52b7b1bb] | 44 | #include <assert.h>
|
---|
[cfe7716] | 45 | #include <ddi.h>
|
---|
| 46 | #include <libarch/ddi.h>
|
---|
| 47 | #include <fibril_synch.h>
|
---|
| 48 | #include <malloc.h>
|
---|
[084ff99] | 49 |
|
---|
[a1769ee] | 50 | struct device;
|
---|
| 51 | typedef struct device device_t;
|
---|
| 52 |
|
---|
| 53 | // device interface
|
---|
| 54 |
|
---|
| 55 | // first two parameters: device and interface structure registered by the devices driver
|
---|
| 56 | typedef void remote_iface_func_t(device_t*, void *, ipc_callid_t, ipc_call_t *);
|
---|
| 57 | typedef remote_iface_func_t *remote_iface_func_ptr_t;
|
---|
[08d9525a] | 58 | typedef void remote_handler_t(device_t*, ipc_callid_t, ipc_call_t *);
|
---|
[a1769ee] | 59 |
|
---|
| 60 | typedef struct {
|
---|
[52b7b1bb] | 61 | size_t method_count;
|
---|
[a1769ee] | 62 | remote_iface_func_ptr_t *methods;
|
---|
| 63 | } remote_iface_t;
|
---|
| 64 |
|
---|
| 65 | typedef struct {
|
---|
| 66 | remote_iface_t * ifaces[DEV_IFACE_COUNT];
|
---|
| 67 | } iface_dipatch_table_t;
|
---|
| 68 |
|
---|
| 69 |
|
---|
[3843ecb] | 70 | static inline bool is_valid_iface_idx(int idx)
|
---|
[a1769ee] | 71 | {
|
---|
[3843ecb] | 72 | return 0 <= idx && idx < DEV_IFACE_MAX;
|
---|
[a1769ee] | 73 | }
|
---|
| 74 |
|
---|
[3843ecb] | 75 | remote_iface_t* get_remote_iface(int idx);
|
---|
[52b7b1bb] | 76 | remote_iface_func_ptr_t get_remote_method(remote_iface_t *rem_iface, ipcarg_t iface_method_idx);
|
---|
| 77 |
|
---|
| 78 |
|
---|
[3843ecb] | 79 | // device class
|
---|
| 80 |
|
---|
| 81 | /** Devices belonging to the same class should implement the same set of interfaces.*/
|
---|
| 82 | typedef struct device_class {
|
---|
| 83 | /** Unique identification of the class. */
|
---|
| 84 | int id;
|
---|
[25a7e11d] | 85 | /** Optional callback function called when a client is connecting to the device. */
|
---|
| 86 | int (*open)(device_t *dev);
|
---|
| 87 | /** Optional callback function called when a client is disconnecting from the device. */
|
---|
| 88 | void (*close)(device_t *dev);
|
---|
[08d9525a] | 89 | /** The table of standard interfaces implemented by the device. */
|
---|
[3843ecb] | 90 | void *interfaces[DEV_IFACE_COUNT];
|
---|
[08d9525a] | 91 | /** The default handler of remote client requests. If the client's remote request cannot be handled
|
---|
| 92 | * by any of the standard interfaces, the default handler is used.*/
|
---|
| 93 | remote_handler_t *default_handler;
|
---|
[3843ecb] | 94 | } device_class_t;
|
---|
| 95 |
|
---|
| 96 |
|
---|
[a1769ee] | 97 | // device
|
---|
| 98 |
|
---|
[52b7b1bb] | 99 | /** The device. */
|
---|
[a1769ee] | 100 | struct device {
|
---|
[52b7b1bb] | 101 | /** Globally unique device identifier (assigned to the device by the device manager). */
|
---|
[bda60d9] | 102 | device_handle_t handle;
|
---|
[5e598e0] | 103 | /** The phone to the parent device driver (if it is different from this driver).*/
|
---|
[9a66bc2e] | 104 | int parent_phone;
|
---|
[5e598e0] | 105 | /** Parent device if handled by this driver, NULL otherwise. */
|
---|
| 106 | device_t *parent;
|
---|
[52b7b1bb] | 107 | /** The device's name.*/
|
---|
[bda60d9] | 108 | const char *name;
|
---|
[52b7b1bb] | 109 | /** The list of device ids for device-to-driver matching.*/
|
---|
[bda60d9] | 110 | match_id_list_t match_ids;
|
---|
[52b7b1bb] | 111 | /** The device driver's data associated with this device.*/
|
---|
[eff1a590] | 112 | void *driver_data;
|
---|
[3843ecb] | 113 | /** Device class consist of class id and table of interfaces supported by the device.*/
|
---|
| 114 | device_class_t *class;
|
---|
[52b7b1bb] | 115 | /** Pointer to the previous and next device in the list of devices handled by the driver */
|
---|
[084ff99] | 116 | link_t link;
|
---|
[a1769ee] | 117 | };
|
---|
[c16cf62] | 118 |
|
---|
[a1769ee] | 119 |
|
---|
| 120 | // driver
|
---|
| 121 |
|
---|
[52b7b1bb] | 122 | /** Generic device driver operations. */
|
---|
[a1769ee] | 123 | typedef struct driver_ops {
|
---|
[52b7b1bb] | 124 | /** Callback method for passing a new device to the device driver.*/
|
---|
[df747b9c] | 125 | int (*add_device)(device_t *dev);
|
---|
[c16cf62] | 126 | // TODO add other generic driver operations
|
---|
| 127 | } driver_ops_t;
|
---|
| 128 |
|
---|
[52b7b1bb] | 129 | /** The driver structure.*/
|
---|
[c16cf62] | 130 | typedef struct driver {
|
---|
[52b7b1bb] | 131 | /** The name of the device driver. */
|
---|
[c16cf62] | 132 | const char *name;
|
---|
[52b7b1bb] | 133 | /** Generic device driver operations. */
|
---|
[c16cf62] | 134 | driver_ops_t *driver_ops;
|
---|
| 135 | } driver_t;
|
---|
| 136 |
|
---|
| 137 | int driver_main(driver_t *drv);
|
---|
| 138 |
|
---|
[52b7b1bb] | 139 | /** Create new device structure.
|
---|
| 140 | *
|
---|
| 141 | * @return the device structure.
|
---|
| 142 | */
|
---|
[7707954] | 143 | static inline device_t * create_device()
|
---|
| 144 | {
|
---|
| 145 | device_t *dev = malloc(sizeof(device_t));
|
---|
| 146 | if (NULL != dev) {
|
---|
| 147 | memset(dev, 0, sizeof(device_t));
|
---|
[5af21c5] | 148 | init_match_ids(&dev->match_ids);
|
---|
| 149 | }
|
---|
[7707954] | 150 | return dev;
|
---|
| 151 | }
|
---|
| 152 |
|
---|
[52b7b1bb] | 153 | /** Delete device structure.
|
---|
| 154 | *
|
---|
| 155 | * @param dev the device structure.
|
---|
| 156 | */
|
---|
[a1769ee] | 157 | static inline void delete_device(device_t *dev)
|
---|
| 158 | {
|
---|
[bda60d9] | 159 | clean_match_ids(&dev->match_ids);
|
---|
| 160 | if (NULL != dev->name) {
|
---|
| 161 | free(dev->name);
|
---|
| 162 | }
|
---|
| 163 | free(dev);
|
---|
| 164 | }
|
---|
[7707954] | 165 |
|
---|
[3843ecb] | 166 | static inline void * device_get_iface(device_t *dev, dev_inferface_idx_t idx)
|
---|
[a1769ee] | 167 | {
|
---|
[3843ecb] | 168 | assert(is_valid_iface_idx(idx));
|
---|
[f658458] | 169 | if (NULL == dev->class) {
|
---|
| 170 | return NULL;
|
---|
| 171 | }
|
---|
[3843ecb] | 172 | return dev->class->interfaces[idx];
|
---|
[52b7b1bb] | 173 | }
|
---|
| 174 |
|
---|
[df747b9c] | 175 | int child_device_register(device_t *child, device_t *parent);
|
---|
[7707954] | 176 |
|
---|
[cfe7716] | 177 | // interrupts
|
---|
| 178 |
|
---|
| 179 | typedef void interrupt_handler_t(device_t *dev, ipc_callid_t iid, ipc_call_t *icall);
|
---|
| 180 |
|
---|
| 181 | typedef struct interrupt_context {
|
---|
| 182 | int id;
|
---|
| 183 | device_t *dev;
|
---|
| 184 | int irq;
|
---|
| 185 | interrupt_handler_t *handler;
|
---|
| 186 | link_t link;
|
---|
| 187 | } interrupt_context_t;
|
---|
| 188 |
|
---|
| 189 | typedef struct interrupt_context_list {
|
---|
| 190 | int curr_id;
|
---|
| 191 | link_t contexts;
|
---|
| 192 | fibril_mutex_t mutex;
|
---|
| 193 | } interrupt_context_list_t;
|
---|
| 194 |
|
---|
| 195 | static inline interrupt_context_t * create_interrupt_context()
|
---|
| 196 | {
|
---|
| 197 | interrupt_context_t *ctx = (interrupt_context_t *)malloc(sizeof(interrupt_context_t));
|
---|
| 198 | if (NULL != ctx) {
|
---|
| 199 | memset(ctx, 0, sizeof(interrupt_context_t));
|
---|
| 200 | }
|
---|
| 201 | return ctx;
|
---|
| 202 | }
|
---|
| 203 |
|
---|
| 204 | static inline void delete_interrupt_context(interrupt_context_t *ctx)
|
---|
| 205 | {
|
---|
| 206 | if (NULL != ctx) {
|
---|
| 207 | free(ctx);
|
---|
| 208 | }
|
---|
| 209 | }
|
---|
| 210 |
|
---|
| 211 | static inline void init_interrupt_context_list(interrupt_context_list_t *list)
|
---|
| 212 | {
|
---|
| 213 | memset(list, 0, sizeof(interrupt_context_list_t));
|
---|
| 214 | fibril_mutex_initialize(&list->mutex);
|
---|
| 215 | list_initialize(&list->contexts);
|
---|
| 216 | }
|
---|
| 217 |
|
---|
| 218 | static inline void add_interrupt_context(interrupt_context_list_t *list, interrupt_context_t *ctx)
|
---|
| 219 | {
|
---|
| 220 | fibril_mutex_lock(&list->mutex);
|
---|
| 221 |
|
---|
| 222 | ctx->id = list->curr_id++;
|
---|
| 223 | list_append(&ctx->link, &list->contexts);
|
---|
| 224 |
|
---|
| 225 | fibril_mutex_unlock(&list->mutex);
|
---|
| 226 | }
|
---|
| 227 |
|
---|
| 228 | static inline void remove_interrupt_context(interrupt_context_list_t *list, interrupt_context_t *ctx)
|
---|
| 229 | {
|
---|
| 230 | fibril_mutex_lock(&list->mutex);
|
---|
| 231 |
|
---|
| 232 | list_remove(&ctx->link);
|
---|
| 233 |
|
---|
| 234 | fibril_mutex_unlock(&list->mutex);
|
---|
| 235 | }
|
---|
| 236 |
|
---|
| 237 | static inline interrupt_context_t *find_interrupt_context_by_id(interrupt_context_list_t *list, int id)
|
---|
| 238 | {
|
---|
| 239 | fibril_mutex_lock(&list->mutex);
|
---|
| 240 | link_t *link = list->contexts.next;
|
---|
| 241 | interrupt_context_t *ctx;
|
---|
| 242 |
|
---|
| 243 | while (link != &list->contexts) {
|
---|
| 244 | ctx = list_get_instance(link, interrupt_context_t, link);
|
---|
| 245 | if (id == ctx->id) {
|
---|
| 246 | fibril_mutex_unlock(&list->mutex);
|
---|
| 247 | return ctx;
|
---|
| 248 | }
|
---|
| 249 | link = link->next;
|
---|
| 250 | }
|
---|
| 251 | fibril_mutex_unlock(&list->mutex);
|
---|
| 252 | return NULL;
|
---|
| 253 | }
|
---|
| 254 |
|
---|
| 255 | static inline interrupt_context_t *find_interrupt_context(interrupt_context_list_t *list, device_t *dev, int irq)
|
---|
| 256 | {
|
---|
| 257 | fibril_mutex_lock(&list->mutex);
|
---|
| 258 | link_t *link = list->contexts.next;
|
---|
| 259 | interrupt_context_t *ctx;
|
---|
| 260 |
|
---|
| 261 | while (link != &list->contexts) {
|
---|
| 262 | ctx = list_get_instance(link, interrupt_context_t, link);
|
---|
| 263 | if (irq == ctx->irq && dev == ctx->dev) {
|
---|
| 264 | fibril_mutex_unlock(&list->mutex);
|
---|
| 265 | return ctx;
|
---|
| 266 | }
|
---|
| 267 | link = link->next;
|
---|
| 268 | }
|
---|
| 269 | fibril_mutex_unlock(&list->mutex);
|
---|
| 270 | return NULL;
|
---|
| 271 | }
|
---|
[7707954] | 272 |
|
---|
[7f8b581] | 273 | int register_interrupt_handler(device_t *dev, int irq, interrupt_handler_t *handler, irq_code_t *pseudocode);
|
---|
| 274 | int unregister_interrupt_handler(device_t *dev, int irq);
|
---|
| 275 |
|
---|
[08d9525a] | 276 |
|
---|
| 277 | // default handler for client requests
|
---|
| 278 |
|
---|
| 279 | static inline remote_handler_t * device_get_default_handler(device_t *dev)
|
---|
| 280 | {
|
---|
| 281 | if (NULL == dev->class) {
|
---|
| 282 | return NULL;
|
---|
| 283 | }
|
---|
| 284 |
|
---|
| 285 | return dev->class->default_handler;
|
---|
| 286 | }
|
---|
| 287 |
|
---|
[c16cf62] | 288 | #endif
|
---|
| 289 |
|
---|
| 290 | /**
|
---|
| 291 | * @}
|
---|
[a1769ee] | 292 | */
|
---|