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