| 1 | /*
|
|---|
| 2 | * Copyright (c) 2010 Lenka Trochtova
|
|---|
| 3 | * Copyright (c) 2011 Jiri Svoboda
|
|---|
| 4 | * All rights reserved.
|
|---|
| 5 | *
|
|---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 7 | * modification, are permitted provided that the following conditions
|
|---|
| 8 | * are met:
|
|---|
| 9 | *
|
|---|
| 10 | * - Redistributions of source code must retain the above copyright
|
|---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 14 | * documentation and/or other materials provided with the distribution.
|
|---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 16 | * derived from this software without specific prior written permission.
|
|---|
| 17 | *
|
|---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 28 | */
|
|---|
| 29 |
|
|---|
| 30 | /** @addtogroup devman
|
|---|
| 31 | * @{
|
|---|
| 32 | */
|
|---|
| 33 |
|
|---|
| 34 | #ifndef DEVMAN_H_
|
|---|
| 35 | #define DEVMAN_H_
|
|---|
| 36 |
|
|---|
| 37 | #include <stdbool.h>
|
|---|
| 38 | #include <adt/list.h>
|
|---|
| 39 | #include <adt/hash_table.h>
|
|---|
| 40 | #include <ipc/devman.h>
|
|---|
| 41 | #include <ipc/loc.h>
|
|---|
| 42 | #include <fibril_synch.h>
|
|---|
| 43 | #include <refcount.h>
|
|---|
| 44 | #include <async.h>
|
|---|
| 45 |
|
|---|
| 46 | #include "util.h"
|
|---|
| 47 |
|
|---|
| 48 | #define NAME "devman"
|
|---|
| 49 |
|
|---|
| 50 | #define LOC_SEPARATOR '\\'
|
|---|
| 51 |
|
|---|
| 52 | struct dev_node;
|
|---|
| 53 | typedef struct dev_node dev_node_t;
|
|---|
| 54 |
|
|---|
| 55 | struct fun_node;
|
|---|
| 56 | typedef struct fun_node fun_node_t;
|
|---|
| 57 |
|
|---|
| 58 | typedef struct {
|
|---|
| 59 | fibril_mutex_t mutex;
|
|---|
| 60 | struct driver *driver;
|
|---|
| 61 | } client_t;
|
|---|
| 62 |
|
|---|
| 63 | /** Representation of device driver. */
|
|---|
| 64 | typedef struct driver {
|
|---|
| 65 | /** Pointers to previous and next drivers in a linked list. */
|
|---|
| 66 | link_t drivers;
|
|---|
| 67 | /** Handle */
|
|---|
| 68 | devman_handle_t handle;
|
|---|
| 69 |
|
|---|
| 70 | /**
|
|---|
| 71 | * Specifies whether the driver has been started and wheter is running
|
|---|
| 72 | * and prepared to receive requests.
|
|---|
| 73 | */
|
|---|
| 74 | driver_state_t state;
|
|---|
| 75 |
|
|---|
| 76 | /** Session asociated with this driver. */
|
|---|
| 77 | async_sess_t *sess;
|
|---|
| 78 | /** Name of the device driver. */
|
|---|
| 79 | char *name;
|
|---|
| 80 | /** List of device ids for device-to-driver matching. */
|
|---|
| 81 | match_id_list_t match_ids;
|
|---|
| 82 | /** List of devices controlled by this driver. */
|
|---|
| 83 | list_t devices;
|
|---|
| 84 |
|
|---|
| 85 | /**
|
|---|
| 86 | * Fibril mutex for this driver - driver state, list of devices, session.
|
|---|
| 87 | */
|
|---|
| 88 | fibril_mutex_t driver_mutex;
|
|---|
| 89 | } driver_t;
|
|---|
| 90 |
|
|---|
| 91 | /** The list of drivers. */
|
|---|
| 92 | typedef struct driver_list {
|
|---|
| 93 | /** List of drivers */
|
|---|
| 94 | list_t drivers;
|
|---|
| 95 | /** Fibril mutex for list of drivers. */
|
|---|
| 96 | fibril_mutex_t drivers_mutex;
|
|---|
| 97 | /** Next free handle */
|
|---|
| 98 | devman_handle_t next_handle;
|
|---|
| 99 | } driver_list_t;
|
|---|
| 100 |
|
|---|
| 101 | /** Device state */
|
|---|
| 102 | typedef enum {
|
|---|
| 103 | DEVICE_NOT_INITIALIZED = 0,
|
|---|
| 104 | DEVICE_USABLE,
|
|---|
| 105 | DEVICE_NOT_PRESENT,
|
|---|
| 106 | DEVICE_INVALID,
|
|---|
| 107 | /** Device node has been removed from the tree */
|
|---|
| 108 | DEVICE_REMOVED
|
|---|
| 109 | } device_state_t;
|
|---|
| 110 |
|
|---|
| 111 | /** Device node in the device tree. */
|
|---|
| 112 | struct dev_node {
|
|---|
| 113 | /** Reference count */
|
|---|
| 114 | atomic_refcount_t refcnt;
|
|---|
| 115 |
|
|---|
| 116 | /** The global unique identifier of the device. */
|
|---|
| 117 | devman_handle_t handle;
|
|---|
| 118 |
|
|---|
| 119 | /** (Parent) function the device is attached to. */
|
|---|
| 120 | fun_node_t *pfun;
|
|---|
| 121 |
|
|---|
| 122 | /** List of device functions. */
|
|---|
| 123 | list_t functions;
|
|---|
| 124 | /** Driver of this device. */
|
|---|
| 125 | driver_t *drv;
|
|---|
| 126 | /** The state of the device. */
|
|---|
| 127 | device_state_t state;
|
|---|
| 128 | /** Link to list of devices owned by driver (driver_t.devices) */
|
|---|
| 129 | link_t driver_devices;
|
|---|
| 130 |
|
|---|
| 131 | /**
|
|---|
| 132 | * Used by the hash table of devices indexed by devman device handles.
|
|---|
| 133 | */
|
|---|
| 134 | ht_link_t devman_dev;
|
|---|
| 135 |
|
|---|
| 136 | /**
|
|---|
| 137 | * Whether this device was already passed to the driver.
|
|---|
| 138 | */
|
|---|
| 139 | bool passed_to_driver;
|
|---|
| 140 | };
|
|---|
| 141 |
|
|---|
| 142 | /** Function state */
|
|---|
| 143 | typedef enum {
|
|---|
| 144 | FUN_INIT = 0,
|
|---|
| 145 | FUN_OFF_LINE,
|
|---|
| 146 | FUN_ON_LINE,
|
|---|
| 147 | /** Function node has been removed from the tree */
|
|---|
| 148 | FUN_REMOVED
|
|---|
| 149 | } fun_state_t;
|
|---|
| 150 |
|
|---|
| 151 | /** Function node in the device tree. */
|
|---|
| 152 | struct fun_node {
|
|---|
| 153 | /** Reference count */
|
|---|
| 154 | atomic_refcount_t refcnt;
|
|---|
| 155 | /** State */
|
|---|
| 156 | fun_state_t state;
|
|---|
| 157 | /** Locked while performing reconfiguration operations */
|
|---|
| 158 | fibril_mutex_t busy_lock;
|
|---|
| 159 |
|
|---|
| 160 | /** The global unique identifier of the function */
|
|---|
| 161 | devman_handle_t handle;
|
|---|
| 162 | /** Name of the function, assigned by the device driver */
|
|---|
| 163 | char *name;
|
|---|
| 164 | /** Function type */
|
|---|
| 165 | fun_type_t ftype;
|
|---|
| 166 |
|
|---|
| 167 | /** Full path and name of the device in device hierarchy */
|
|---|
| 168 | char *pathname;
|
|---|
| 169 |
|
|---|
| 170 | /** Device which this function belongs to */
|
|---|
| 171 | dev_node_t *dev;
|
|---|
| 172 |
|
|---|
| 173 | /** Link to list of functions in the device (ddf_dev_t.functions) */
|
|---|
| 174 | link_t dev_functions;
|
|---|
| 175 |
|
|---|
| 176 | /** Child device node (if any attached). */
|
|---|
| 177 | dev_node_t *child;
|
|---|
| 178 | /** List of device ids for device-to-driver matching. */
|
|---|
| 179 | match_id_list_t match_ids;
|
|---|
| 180 |
|
|---|
| 181 | /** Service ID if the device function is registered with loc. */
|
|---|
| 182 | service_id_t service_id;
|
|---|
| 183 |
|
|---|
| 184 | /**
|
|---|
| 185 | * Used by the hash table of functions indexed by devman device handles.
|
|---|
| 186 | */
|
|---|
| 187 | ht_link_t devman_fun;
|
|---|
| 188 |
|
|---|
| 189 | /**
|
|---|
| 190 | * Used by the hash table of functions indexed by service IDs.
|
|---|
| 191 | */
|
|---|
| 192 | ht_link_t loc_fun;
|
|---|
| 193 | };
|
|---|
| 194 |
|
|---|
| 195 | /** Represents device tree. */
|
|---|
| 196 | typedef struct dev_tree {
|
|---|
| 197 | /** Root device node. */
|
|---|
| 198 | fun_node_t *root_node;
|
|---|
| 199 |
|
|---|
| 200 | /**
|
|---|
| 201 | * The next available handle - handles are assigned in a sequential
|
|---|
| 202 | * manner.
|
|---|
| 203 | */
|
|---|
| 204 | devman_handle_t current_handle;
|
|---|
| 205 |
|
|---|
| 206 | /** Synchronize access to the device tree. */
|
|---|
| 207 | fibril_rwlock_t rwlock;
|
|---|
| 208 |
|
|---|
| 209 | /** Hash table of all devices indexed by devman handles. */
|
|---|
| 210 | hash_table_t devman_devices;
|
|---|
| 211 |
|
|---|
| 212 | /** Hash table of all devices indexed by devman handles. */
|
|---|
| 213 | hash_table_t devman_functions;
|
|---|
| 214 |
|
|---|
| 215 | /**
|
|---|
| 216 | * Hash table of services registered with location service, indexed by
|
|---|
| 217 | * service IDs.
|
|---|
| 218 | */
|
|---|
| 219 | hash_table_t loc_functions;
|
|---|
| 220 | } dev_tree_t;
|
|---|
| 221 |
|
|---|
| 222 | #endif
|
|---|
| 223 |
|
|---|
| 224 | /** @}
|
|---|
| 225 | */
|
|---|