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