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