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