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