| 1 | /*
|
|---|
| 2 | * Copyright (c) 2010 Lenka Trochtova
|
|---|
| 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 |
|
|---|
| 35 | #ifndef LIBDRV_DRIVER_H_
|
|---|
| 36 | #define LIBDRV_DRIVER_H_
|
|---|
| 37 |
|
|---|
| 38 | #include <kernel/ddi/irq.h>
|
|---|
| 39 | #include <adt/list.h>
|
|---|
| 40 | #include <devman.h>
|
|---|
| 41 | #include <ipc/devman.h>
|
|---|
| 42 | #include <ipc/dev_iface.h>
|
|---|
| 43 | #include <assert.h>
|
|---|
| 44 | #include <ddi.h>
|
|---|
| 45 | #include <libarch/ddi.h>
|
|---|
| 46 | #include <fibril_synch.h>
|
|---|
| 47 | #include <malloc.h>
|
|---|
| 48 |
|
|---|
| 49 | #include "dev_iface.h"
|
|---|
| 50 |
|
|---|
| 51 | struct device;
|
|---|
| 52 | typedef struct device device_t;
|
|---|
| 53 |
|
|---|
| 54 | struct function;
|
|---|
| 55 | typedef struct function function_t;
|
|---|
| 56 |
|
|---|
| 57 | /*
|
|---|
| 58 | * Device class
|
|---|
| 59 | */
|
|---|
| 60 |
|
|---|
| 61 | /** Devices operations */
|
|---|
| 62 | typedef struct device_ops {
|
|---|
| 63 | /**
|
|---|
| 64 | * Optional callback function called when a client is connecting to the
|
|---|
| 65 | * device.
|
|---|
| 66 | */
|
|---|
| 67 | int (*open)(function_t *);
|
|---|
| 68 |
|
|---|
| 69 | /**
|
|---|
| 70 | * Optional callback function called when a client is disconnecting from
|
|---|
| 71 | * the device.
|
|---|
| 72 | */
|
|---|
| 73 | void (*close)(function_t *);
|
|---|
| 74 |
|
|---|
| 75 | /** The table of standard interfaces implemented by the device. */
|
|---|
| 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 | */
|
|---|
| 83 | remote_handler_t *default_handler;
|
|---|
| 84 | } device_ops_t;
|
|---|
| 85 |
|
|---|
| 86 |
|
|---|
| 87 | /*
|
|---|
| 88 | * Device
|
|---|
| 89 | */
|
|---|
| 90 |
|
|---|
| 91 | /** Device structure */
|
|---|
| 92 | struct device {
|
|---|
| 93 | /**
|
|---|
| 94 | * Globally unique device identifier (assigned to the device by the
|
|---|
| 95 | * device manager).
|
|---|
| 96 | */
|
|---|
| 97 | devman_handle_t handle;
|
|---|
| 98 |
|
|---|
| 99 | /**
|
|---|
| 100 | * Phone to the parent device driver (if it is different from this
|
|---|
| 101 | * driver)
|
|---|
| 102 | */
|
|---|
| 103 | int parent_phone;
|
|---|
| 104 |
|
|---|
| 105 | /** Device name */
|
|---|
| 106 | const char *name;
|
|---|
| 107 |
|
|---|
| 108 | /** Driver-specific data associated with this device */
|
|---|
| 109 | void *driver_data;
|
|---|
| 110 |
|
|---|
| 111 | /** Link in the list of devices handled by the driver */
|
|---|
| 112 | link_t link;
|
|---|
| 113 | };
|
|---|
| 114 |
|
|---|
| 115 | /** Function structure */
|
|---|
| 116 | struct function {
|
|---|
| 117 | /** True if bound to the device manager */
|
|---|
| 118 | bool bound;
|
|---|
| 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 |
|
|---|
| 140 | /*
|
|---|
| 141 | * Driver
|
|---|
| 142 | */
|
|---|
| 143 |
|
|---|
| 144 | /** Generic device driver operations */
|
|---|
| 145 | typedef struct driver_ops {
|
|---|
| 146 | /** Callback method for passing a new device to the device driver */
|
|---|
| 147 | int (*add_device)(device_t *dev);
|
|---|
| 148 | /* TODO: add other generic driver operations */
|
|---|
| 149 | } driver_ops_t;
|
|---|
| 150 |
|
|---|
| 151 | /** Driver structure */
|
|---|
| 152 | typedef struct driver {
|
|---|
| 153 | /** Name of the device driver */
|
|---|
| 154 | const char *name;
|
|---|
| 155 | /** Generic device driver operations */
|
|---|
| 156 | driver_ops_t *driver_ops;
|
|---|
| 157 | } driver_t;
|
|---|
| 158 |
|
|---|
| 159 | int driver_main(driver_t *);
|
|---|
| 160 |
|
|---|
| 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 *);
|
|---|
| 164 | extern int ddf_fun_add_match_id(function_t *, const char *, int);
|
|---|
| 165 |
|
|---|
| 166 | extern void *function_get_ops(function_t *, dev_inferface_idx_t);
|
|---|
| 167 |
|
|---|
| 168 | /*
|
|---|
| 169 | * Interrupts
|
|---|
| 170 | */
|
|---|
| 171 |
|
|---|
| 172 | typedef void interrupt_handler_t(device_t *, ipc_callid_t, ipc_call_t *);
|
|---|
| 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 |
|
|---|
| 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 *,
|
|---|
| 201 | irq_code_t *);
|
|---|
| 202 | extern int unregister_interrupt_handler(device_t *, int);
|
|---|
| 203 |
|
|---|
| 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);
|
|---|
| 206 |
|
|---|
| 207 | #endif
|
|---|
| 208 |
|
|---|
| 209 | /**
|
|---|
| 210 | * @}
|
|---|
| 211 | */
|
|---|