| [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 |
|
|---|
| [ffa2c8ef] | 38 | #include <kernel/ddi/irq.h>
|
|---|
| [084ff99] | 39 | #include <adt/list.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 |
|
|---|
| [22027b6e] | 49 | #include "dev_iface.h"
|
|---|
| 50 |
|
|---|
| [a1769ee] | 51 | struct device;
|
|---|
| 52 | typedef struct device device_t;
|
|---|
| 53 |
|
|---|
| [8b1e15ac] | 54 | struct function;
|
|---|
| 55 | typedef struct function function_t;
|
|---|
| 56 |
|
|---|
| [97adec8] | 57 | /*
|
|---|
| 58 | * Device class
|
|---|
| 59 | */
|
|---|
| [3843ecb] | 60 |
|
|---|
| [97adec8] | 61 | /** Devices operations */
|
|---|
| [5159ae9] | 62 | typedef struct device_ops {
|
|---|
| [7a252ec8] | 63 | /**
|
|---|
| 64 | * Optional callback function called when a client is connecting to the
|
|---|
| 65 | * device.
|
|---|
| 66 | */
|
|---|
| [8b1e15ac] | 67 | int (*open)(function_t *);
|
|---|
| [7a252ec8] | 68 |
|
|---|
| 69 | /**
|
|---|
| 70 | * Optional callback function called when a client is disconnecting from
|
|---|
| 71 | * the device.
|
|---|
| 72 | */
|
|---|
| [8b1e15ac] | 73 | void (*close)(function_t *);
|
|---|
| [7a252ec8] | 74 |
|
|---|
| [08d9525a] | 75 | /** The table of standard interfaces implemented by the device. */
|
|---|
| [7a252ec8] | 76 | void *interfaces[DEV_IFACE_COUNT];
|
|---|
| 77 |
|
|---|
| 78 | /**
|
|---|
| 79 | * The default handler of remote client requests. If the client's remote
|
|---|
| 80 | * request cannot be handled by any of the standard interfaces, the
|
|---|
| 81 | * default handler is used.
|
|---|
| 82 | */
|
|---|
| [08d9525a] | 83 | remote_handler_t *default_handler;
|
|---|
| [5159ae9] | 84 | } device_ops_t;
|
|---|
| [3843ecb] | 85 |
|
|---|
| 86 |
|
|---|
| [97adec8] | 87 | /*
|
|---|
| 88 | * Device
|
|---|
| 89 | */
|
|---|
| [a1769ee] | 90 |
|
|---|
| [97adec8] | 91 | /** Device structure */
|
|---|
| [a1769ee] | 92 | struct device {
|
|---|
| [7a252ec8] | 93 | /**
|
|---|
| 94 | * Globally unique device identifier (assigned to the device by the
|
|---|
| 95 | * device manager).
|
|---|
| 96 | */
|
|---|
| [0b5a4131] | 97 | devman_handle_t handle;
|
|---|
| [7a252ec8] | 98 |
|
|---|
| 99 | /**
|
|---|
| [97adec8] | 100 | * Phone to the parent device driver (if it is different from this
|
|---|
| 101 | * driver)
|
|---|
| [7a252ec8] | 102 | */
|
|---|
| [9a66bc2e] | 103 | int parent_phone;
|
|---|
| [7a252ec8] | 104 |
|
|---|
| [97adec8] | 105 | /** Device name */
|
|---|
| [bda60d9] | 106 | const char *name;
|
|---|
| [8b1e15ac] | 107 |
|
|---|
| [97adec8] | 108 | /** Driver-specific data associated with this device */
|
|---|
| [eff1a590] | 109 | void *driver_data;
|
|---|
| [7a252ec8] | 110 |
|
|---|
| [97adec8] | 111 | /** Link in the list of devices handled by the driver */
|
|---|
| [084ff99] | 112 | link_t link;
|
|---|
| [a1769ee] | 113 | };
|
|---|
| [c16cf62] | 114 |
|
|---|
| [8b1e15ac] | 115 | /** Function structure */
|
|---|
| 116 | struct function {
|
|---|
| [97a62fe] | 117 | /** True if bound to the device manager */
|
|---|
| 118 | bool bound;
|
|---|
| [8b1e15ac] | 119 | /** Function indentifier (asigned by device manager) */
|
|---|
| 120 | devman_handle_t handle;
|
|---|
| 121 |
|
|---|
| 122 | /** Device which this function belogs to */
|
|---|
| 123 | device_t *dev;
|
|---|
| 124 |
|
|---|
| 125 | /** Function type */
|
|---|
| 126 | fun_type_t ftype;
|
|---|
| 127 | /** Function name */
|
|---|
| 128 | const char *name;
|
|---|
| 129 | /** List of device ids for driver matching */
|
|---|
| 130 | match_id_list_t match_ids;
|
|---|
| 131 | /** Driver-specific data associated with this function */
|
|---|
| 132 | void *driver_data;
|
|---|
| 133 | /** Implementation of operations provided by this function */
|
|---|
| 134 | device_ops_t *ops;
|
|---|
| 135 |
|
|---|
| 136 | /** Link in the list of functions handled by the driver */
|
|---|
| 137 | link_t link;
|
|---|
| 138 | };
|
|---|
| 139 |
|
|---|
| [97adec8] | 140 | /*
|
|---|
| 141 | * Driver
|
|---|
| 142 | */
|
|---|
| [a1769ee] | 143 |
|
|---|
| [97adec8] | 144 | /** Generic device driver operations */
|
|---|
| [a1769ee] | 145 | typedef struct driver_ops {
|
|---|
| [97adec8] | 146 | /** Callback method for passing a new device to the device driver */
|
|---|
| [df747b9c] | 147 | int (*add_device)(device_t *dev);
|
|---|
| [97adec8] | 148 | /* TODO: add other generic driver operations */
|
|---|
| [c16cf62] | 149 | } driver_ops_t;
|
|---|
| 150 |
|
|---|
| [97adec8] | 151 | /** Driver structure */
|
|---|
| [c16cf62] | 152 | typedef struct driver {
|
|---|
| [97adec8] | 153 | /** Name of the device driver */
|
|---|
| [c16cf62] | 154 | const char *name;
|
|---|
| [97adec8] | 155 | /** Generic device driver operations */
|
|---|
| [c16cf62] | 156 | driver_ops_t *driver_ops;
|
|---|
| 157 | } driver_t;
|
|---|
| 158 |
|
|---|
| [7a252ec8] | 159 | int driver_main(driver_t *);
|
|---|
| [c16cf62] | 160 |
|
|---|
| [97a62fe] | 161 | extern function_t *ddf_fun_create(device_t *, fun_type_t, const char *);
|
|---|
| 162 | extern void ddf_fun_destroy(function_t *);
|
|---|
| 163 | extern int ddf_fun_bind(function_t *);
|
|---|
| [cd0684d] | 164 | extern int ddf_fun_add_match_id(function_t *, const char *, int);
|
|---|
| [97a62fe] | 165 |
|
|---|
| [8b1e15ac] | 166 | extern void *function_get_ops(function_t *, dev_inferface_idx_t);
|
|---|
| [97adec8] | 167 |
|
|---|
| 168 | /*
|
|---|
| 169 | * Interrupts
|
|---|
| 170 | */
|
|---|
| [cfe7716] | 171 |
|
|---|
| [7a252ec8] | 172 | typedef void interrupt_handler_t(device_t *, ipc_callid_t, ipc_call_t *);
|
|---|
| [cfe7716] | 173 |
|
|---|
| 174 | typedef struct interrupt_context {
|
|---|
| 175 | int id;
|
|---|
| 176 | device_t *dev;
|
|---|
| 177 | int irq;
|
|---|
| 178 | interrupt_handler_t *handler;
|
|---|
| 179 | link_t link;
|
|---|
| 180 | } interrupt_context_t;
|
|---|
| 181 |
|
|---|
| 182 | typedef struct interrupt_context_list {
|
|---|
| 183 | int curr_id;
|
|---|
| 184 | link_t contexts;
|
|---|
| 185 | fibril_mutex_t mutex;
|
|---|
| 186 | } interrupt_context_list_t;
|
|---|
| 187 |
|
|---|
| [5fdd7c3] | 188 | extern interrupt_context_t *create_interrupt_context(void);
|
|---|
| 189 | extern void delete_interrupt_context(interrupt_context_t *);
|
|---|
| 190 | extern void init_interrupt_context_list(interrupt_context_list_t *);
|
|---|
| 191 | extern void add_interrupt_context(interrupt_context_list_t *,
|
|---|
| 192 | interrupt_context_t *);
|
|---|
| 193 | extern void remove_interrupt_context(interrupt_context_list_t *,
|
|---|
| 194 | interrupt_context_t *);
|
|---|
| 195 | extern interrupt_context_t *find_interrupt_context_by_id(
|
|---|
| 196 | interrupt_context_list_t *, int);
|
|---|
| 197 | extern interrupt_context_t *find_interrupt_context(
|
|---|
| 198 | interrupt_context_list_t *, device_t *, int);
|
|---|
| 199 |
|
|---|
| 200 | extern int register_interrupt_handler(device_t *, int, interrupt_handler_t *,
|
|---|
| [7a252ec8] | 201 | irq_code_t *);
|
|---|
| [5fdd7c3] | 202 | extern int unregister_interrupt_handler(device_t *, int);
|
|---|
| [08d9525a] | 203 |
|
|---|
| [8b1e15ac] | 204 | extern remote_handler_t *function_get_default_handler(function_t *);
|
|---|
| 205 | extern int add_function_to_class(function_t *fun, const char *class_name);
|
|---|
| [692c40cb] | 206 |
|
|---|
| [c16cf62] | 207 | #endif
|
|---|
| 208 |
|
|---|
| 209 | /**
|
|---|
| 210 | * @}
|
|---|
| [a1769ee] | 211 | */
|
|---|