| [e2b9a993] | 1 | /*
|
|---|
| 2 | * Copyright (c) 2010 Lenka Trochtova
|
|---|
| [d0dd7b5] | 3 | * Copyright (c) 2011 Jiri Svoboda
|
|---|
| [e2b9a993] | 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 | */
|
|---|
| [08d9c4e6] | 33 |
|
|---|
| [e2b9a993] | 34 | #ifndef DEVMAN_H_
|
|---|
| 35 | #define DEVMAN_H_
|
|---|
| 36 |
|
|---|
| 37 | #include <assert.h>
|
|---|
| 38 | #include <bool.h>
|
|---|
| 39 | #include <dirent.h>
|
|---|
| [c47e1a8] | 40 | #include <str.h>
|
|---|
| [e2b9a993] | 41 | #include <adt/list.h>
|
|---|
| [957cfa58] | 42 | #include <adt/hash_table.h>
|
|---|
| [bda60d9] | 43 | #include <ipc/devman.h>
|
|---|
| [15f3c3f] | 44 | #include <ipc/loc.h>
|
|---|
| [e85920d] | 45 | #include <fibril_synch.h>
|
|---|
| [084ff99] | 46 | #include <atomic.h>
|
|---|
| [79ae36dd] | 47 | #include <async.h>
|
|---|
| [e2b9a993] | 48 |
|
|---|
| [08d9c4e6] | 49 | #include "util.h"
|
|---|
| [e2b9a993] | 50 |
|
|---|
| 51 | #define NAME "devman"
|
|---|
| 52 |
|
|---|
| 53 | #define MATCH_EXT ".ma"
|
|---|
| 54 |
|
|---|
| [15f3c3f] | 55 | #define LOC_DEVICE_NAMESPACE "devices"
|
|---|
| 56 | #define LOC_SEPARATOR '\\'
|
|---|
| [ce89036b] | 57 |
|
|---|
| [ba38f72c] | 58 | struct dev_node;
|
|---|
| 59 | typedef struct dev_node dev_node_t;
|
|---|
| 60 |
|
|---|
| 61 | struct fun_node;
|
|---|
| 62 | typedef struct fun_node fun_node_t;
|
|---|
| [e2b9a993] | 63 |
|
|---|
| [422722e] | 64 | typedef struct {
|
|---|
| 65 | fibril_mutex_t mutex;
|
|---|
| 66 | struct driver *driver;
|
|---|
| 67 | } client_t;
|
|---|
| 68 |
|
|---|
| [e85920d] | 69 | typedef enum {
|
|---|
| [38b3baf] | 70 | /** Driver has not been started. */
|
|---|
| [e85920d] | 71 | DRIVER_NOT_STARTED = 0,
|
|---|
| [38b3baf] | 72 |
|
|---|
| 73 | /**
|
|---|
| 74 | * Driver has been started, but has not registered as running and ready
|
|---|
| 75 | * to receive requests.
|
|---|
| 76 | */
|
|---|
| [e85920d] | 77 | DRIVER_STARTING,
|
|---|
| [38b3baf] | 78 |
|
|---|
| 79 | /** Driver is running and prepared to serve incomming requests. */
|
|---|
| [e85920d] | 80 | DRIVER_RUNNING
|
|---|
| 81 | } driver_state_t;
|
|---|
| 82 |
|
|---|
| [38b3baf] | 83 | /** Representation of device driver. */
|
|---|
| [e2b9a993] | 84 | typedef struct driver {
|
|---|
| [38b3baf] | 85 | /** Pointers to previous and next drivers in a linked list. */
|
|---|
| [e2b9a993] | 86 | link_t drivers;
|
|---|
| [38b3baf] | 87 |
|
|---|
| 88 | /**
|
|---|
| 89 | * Specifies whether the driver has been started and wheter is running
|
|---|
| 90 | * and prepared to receive requests.
|
|---|
| 91 | */
|
|---|
| [e85920d] | 92 | int state;
|
|---|
| [38b3baf] | 93 |
|
|---|
| [79ae36dd] | 94 | /** Session asociated with this driver. */
|
|---|
| 95 | async_sess_t *sess;
|
|---|
| [38b3baf] | 96 | /** Name of the device driver. */
|
|---|
| [e2b9a993] | 97 | char *name;
|
|---|
| [38b3baf] | 98 | /** Path to the driver's binary. */
|
|---|
| [e2b9a993] | 99 | const char *binary_path;
|
|---|
| [38b3baf] | 100 | /** List of device ids for device-to-driver matching. */
|
|---|
| [e2b9a993] | 101 | match_id_list_t match_ids;
|
|---|
| [b72efe8] | 102 | /** List of devices controlled by this driver. */
|
|---|
| 103 | list_t devices;
|
|---|
| [38b3baf] | 104 |
|
|---|
| 105 | /**
|
|---|
| [79ae36dd] | 106 | * Fibril mutex for this driver - driver state, list of devices, session.
|
|---|
| [38b3baf] | 107 | */
|
|---|
| [e85920d] | 108 | fibril_mutex_t driver_mutex;
|
|---|
| [e2b9a993] | 109 | } driver_t;
|
|---|
| 110 |
|
|---|
| [e85920d] | 111 | /** The list of drivers. */
|
|---|
| 112 | typedef struct driver_list {
|
|---|
| 113 | /** List of drivers */
|
|---|
| [b72efe8] | 114 | list_t drivers;
|
|---|
| [e85920d] | 115 | /** Fibril mutex for list of drivers. */
|
|---|
| [38b3baf] | 116 | fibril_mutex_t drivers_mutex;
|
|---|
| [e85920d] | 117 | } driver_list_t;
|
|---|
| 118 |
|
|---|
| [c1a0488] | 119 | /** Device state */
|
|---|
| [df747b9c] | 120 | typedef enum {
|
|---|
| 121 | DEVICE_NOT_INITIALIZED = 0,
|
|---|
| 122 | DEVICE_USABLE,
|
|---|
| 123 | DEVICE_NOT_PRESENT,
|
|---|
| [c1a0488] | 124 | DEVICE_INVALID,
|
|---|
| 125 | /** Device node has been removed from the tree */
|
|---|
| 126 | DEVICE_REMOVED
|
|---|
| [df747b9c] | 127 | } device_state_t;
|
|---|
| 128 |
|
|---|
| [ba38f72c] | 129 | /** Device node in the device tree. */
|
|---|
| 130 | struct dev_node {
|
|---|
| [58cbb0c8] | 131 | /** Reference count */
|
|---|
| 132 | atomic_t refcnt;
|
|---|
| 133 |
|
|---|
| [38b3baf] | 134 | /** The global unique identifier of the device. */
|
|---|
| [0b5a4131] | 135 | devman_handle_t handle;
|
|---|
| [ba38f72c] | 136 |
|
|---|
| 137 | /** (Parent) function the device is attached to. */
|
|---|
| 138 | fun_node_t *pfun;
|
|---|
| 139 |
|
|---|
| 140 | /** List of device functions. */
|
|---|
| [b72efe8] | 141 | list_t functions;
|
|---|
| [ba38f72c] | 142 | /** Driver of this device. */
|
|---|
| 143 | driver_t *drv;
|
|---|
| 144 | /** The state of the device. */
|
|---|
| 145 | device_state_t state;
|
|---|
| 146 | /** Link to list of devices owned by driver (driver_t.devices) */
|
|---|
| 147 | link_t driver_devices;
|
|---|
| [38b3baf] | 148 |
|
|---|
| 149 | /**
|
|---|
| [ba38f72c] | 150 | * Used by the hash table of devices indexed by devman device handles.
|
|---|
| [38b3baf] | 151 | */
|
|---|
| [ba38f72c] | 152 | link_t devman_dev;
|
|---|
| [38b3baf] | 153 |
|
|---|
| 154 | /**
|
|---|
| [ba38f72c] | 155 | * Whether this device was already passed to the driver.
|
|---|
| [38b3baf] | 156 | */
|
|---|
| [ba38f72c] | 157 | bool passed_to_driver;
|
|---|
| 158 | };
|
|---|
| 159 |
|
|---|
| [c1a0488] | 160 | /** Function state */
|
|---|
| 161 | typedef enum {
|
|---|
| 162 | FUN_INIT = 0,
|
|---|
| 163 | FUN_OFF_LINE,
|
|---|
| 164 | FUN_ON_LINE,
|
|---|
| 165 | /** Function node has been removed from the tree */
|
|---|
| 166 | FUN_REMOVED
|
|---|
| 167 | } fun_state_t;
|
|---|
| 168 |
|
|---|
| [ba38f72c] | 169 | /** Function node in the device tree. */
|
|---|
| 170 | struct fun_node {
|
|---|
| [58cbb0c8] | 171 | /** Reference count */
|
|---|
| 172 | atomic_t refcnt;
|
|---|
| [c1a0488] | 173 | /** State */
|
|---|
| 174 | fun_state_t state;
|
|---|
| [58cbb0c8] | 175 |
|
|---|
| [ba38f72c] | 176 | /** The global unique identifier of the function */
|
|---|
| 177 | devman_handle_t handle;
|
|---|
| 178 | /** Name of the function, assigned by the device driver */
|
|---|
| 179 | char *name;
|
|---|
| [d0dd7b5] | 180 | /** Function type */
|
|---|
| 181 | fun_type_t ftype;
|
|---|
| [ba38f72c] | 182 |
|
|---|
| 183 | /** Full path and name of the device in device hierarchy */
|
|---|
| 184 | char *pathname;
|
|---|
| 185 |
|
|---|
| 186 | /** Device which this function belongs to */
|
|---|
| 187 | dev_node_t *dev;
|
|---|
| [38b3baf] | 188 |
|
|---|
| [83a2f43] | 189 | /** Link to list of functions in the device (ddf_dev_t.functions) */
|
|---|
| [ba38f72c] | 190 | link_t dev_functions;
|
|---|
| 191 |
|
|---|
| 192 | /** Child device node (if any attached). */
|
|---|
| 193 | dev_node_t *child;
|
|---|
| [38b3baf] | 194 | /** List of device ids for device-to-driver matching. */
|
|---|
| [08d9c4e6] | 195 | match_id_list_t match_ids;
|
|---|
| [38b3baf] | 196 |
|
|---|
| [15f3c3f] | 197 | /** Service ID if the device function is registered with loc. */
|
|---|
| 198 | service_id_t service_id;
|
|---|
| [38b3baf] | 199 |
|
|---|
| 200 | /**
|
|---|
| [ba38f72c] | 201 | * Used by the hash table of functions indexed by devman device handles.
|
|---|
| [38b3baf] | 202 | */
|
|---|
| [ba38f72c] | 203 | link_t devman_fun;
|
|---|
| [38b3baf] | 204 |
|
|---|
| 205 | /**
|
|---|
| [15f3c3f] | 206 | * Used by the hash table of functions indexed by service IDs.
|
|---|
| [5bee897] | 207 | */
|
|---|
| [15f3c3f] | 208 | link_t loc_fun;
|
|---|
| [e2b9a993] | 209 | };
|
|---|
| 210 |
|
|---|
| [38b3baf] | 211 | /** Represents device tree. */
|
|---|
| [e2b9a993] | 212 | typedef struct dev_tree {
|
|---|
| 213 | /** Root device node. */
|
|---|
| [ba38f72c] | 214 | fun_node_t *root_node;
|
|---|
| [38b3baf] | 215 |
|
|---|
| 216 | /**
|
|---|
| 217 | * The next available handle - handles are assigned in a sequential
|
|---|
| 218 | * manner.
|
|---|
| 219 | */
|
|---|
| [0b5a4131] | 220 | devman_handle_t current_handle;
|
|---|
| [38b3baf] | 221 |
|
|---|
| 222 | /** Synchronize access to the device tree. */
|
|---|
| [957cfa58] | 223 | fibril_rwlock_t rwlock;
|
|---|
| [38b3baf] | 224 |
|
|---|
| 225 | /** Hash table of all devices indexed by devman handles. */
|
|---|
| [957cfa58] | 226 | hash_table_t devman_devices;
|
|---|
| [38b3baf] | 227 |
|
|---|
| [ba38f72c] | 228 | /** Hash table of all devices indexed by devman handles. */
|
|---|
| 229 | hash_table_t devman_functions;
|
|---|
| 230 |
|
|---|
| [38b3baf] | 231 | /**
|
|---|
| [15f3c3f] | 232 | * Hash table of services registered with location service, indexed by
|
|---|
| 233 | * service IDs.
|
|---|
| [38b3baf] | 234 | */
|
|---|
| [15f3c3f] | 235 | hash_table_t loc_functions;
|
|---|
| [e2b9a993] | 236 | } dev_tree_t;
|
|---|
| 237 |
|
|---|
| [38b3baf] | 238 | /* Match ids and scores */
|
|---|
| [e2b9a993] | 239 |
|
|---|
| [ba38f72c] | 240 | extern int get_match_score(driver_t *, dev_node_t *);
|
|---|
| [e2b9a993] | 241 |
|
|---|
| [38b3baf] | 242 | extern bool parse_match_ids(char *, match_id_list_t *);
|
|---|
| 243 | extern bool read_match_ids(const char *, match_id_list_t *);
|
|---|
| 244 | extern char *read_match_id(char **);
|
|---|
| 245 | extern char *read_id(const char **);
|
|---|
| [729fa2d6] | 246 |
|
|---|
| [38b3baf] | 247 | /* Drivers */
|
|---|
| [0c3666d] | 248 |
|
|---|
| [791f58c] | 249 | extern void init_driver_list(driver_list_t *);
|
|---|
| [38b3baf] | 250 | extern driver_t *create_driver(void);
|
|---|
| 251 | extern bool get_driver_info(const char *, const char *, driver_t *);
|
|---|
| 252 | extern int lookup_available_drivers(driver_list_t *, const char *);
|
|---|
| [e2b9a993] | 253 |
|
|---|
| [ba38f72c] | 254 | extern driver_t *find_best_match_driver(driver_list_t *, dev_node_t *);
|
|---|
| 255 | extern bool assign_driver(dev_node_t *, driver_list_t *, dev_tree_t *);
|
|---|
| [e2b9a993] | 256 |
|
|---|
| [38b3baf] | 257 | extern void add_driver(driver_list_t *, driver_t *);
|
|---|
| [58cbb0c8] | 258 | extern void attach_driver(dev_tree_t *, dev_node_t *, driver_t *);
|
|---|
| 259 | extern void detach_driver(dev_tree_t *, dev_node_t *);
|
|---|
| [45059d6b] | 260 | extern void add_device(driver_t *, dev_node_t *, dev_tree_t *);
|
|---|
| [38b3baf] | 261 | extern bool start_driver(driver_t *);
|
|---|
| [58cbb0c8] | 262 | extern int driver_dev_remove(dev_tree_t *, dev_node_t *);
|
|---|
| [80a96d2] | 263 | extern int driver_dev_gone(dev_tree_t *, dev_node_t *);
|
|---|
| [58cbb0c8] | 264 | extern int driver_fun_online(dev_tree_t *, fun_node_t *);
|
|---|
| 265 | extern int driver_fun_offline(dev_tree_t *, fun_node_t *);
|
|---|
| [e2b9a993] | 266 |
|
|---|
| [38b3baf] | 267 | extern driver_t *find_driver(driver_list_t *, const char *);
|
|---|
| [791f58c] | 268 | extern void initialize_running_driver(driver_t *, dev_tree_t *);
|
|---|
| [e2b9a993] | 269 |
|
|---|
| [791f58c] | 270 | extern void init_driver(driver_t *);
|
|---|
| 271 | extern void clean_driver(driver_t *);
|
|---|
| 272 | extern void delete_driver(driver_t *);
|
|---|
| [38b3baf] | 273 |
|
|---|
| 274 | /* Device nodes */
|
|---|
| 275 |
|
|---|
| [ba38f72c] | 276 | extern dev_node_t *create_dev_node(void);
|
|---|
| 277 | extern void delete_dev_node(dev_node_t *node);
|
|---|
| [58cbb0c8] | 278 | extern void dev_add_ref(dev_node_t *);
|
|---|
| 279 | extern void dev_del_ref(dev_node_t *);
|
|---|
| [ba38f72c] | 280 | extern dev_node_t *find_dev_node_no_lock(dev_tree_t *tree,
|
|---|
| 281 | devman_handle_t handle);
|
|---|
| 282 | extern dev_node_t *find_dev_node(dev_tree_t *tree, devman_handle_t handle);
|
|---|
| 283 | extern dev_node_t *find_dev_function(dev_node_t *, const char *);
|
|---|
| [7beb220] | 284 | extern int dev_get_functions(dev_tree_t *tree, dev_node_t *, devman_handle_t *,
|
|---|
| 285 | size_t, size_t *);
|
|---|
| [ba38f72c] | 286 |
|
|---|
| 287 | extern fun_node_t *create_fun_node(void);
|
|---|
| 288 | extern void delete_fun_node(fun_node_t *);
|
|---|
| [58cbb0c8] | 289 | extern void fun_add_ref(fun_node_t *);
|
|---|
| 290 | extern void fun_del_ref(fun_node_t *);
|
|---|
| [ba38f72c] | 291 | extern fun_node_t *find_fun_node_no_lock(dev_tree_t *tree,
|
|---|
| [0b5a4131] | 292 | devman_handle_t handle);
|
|---|
| [ba38f72c] | 293 | extern fun_node_t *find_fun_node(dev_tree_t *tree, devman_handle_t handle);
|
|---|
| 294 | extern fun_node_t *find_fun_node_by_path(dev_tree_t *, char *);
|
|---|
| [58cbb0c8] | 295 | extern fun_node_t *find_fun_node_in_device(dev_tree_t *tree, dev_node_t *,
|
|---|
| 296 | const char *);
|
|---|
| [38b3baf] | 297 |
|
|---|
| 298 | /* Device tree */
|
|---|
| [5cd136ab] | 299 |
|
|---|
| [38b3baf] | 300 | extern bool init_device_tree(dev_tree_t *, driver_list_t *);
|
|---|
| [ba38f72c] | 301 | extern bool create_root_nodes(dev_tree_t *);
|
|---|
| 302 | extern bool insert_dev_node(dev_tree_t *, dev_node_t *, fun_node_t *);
|
|---|
| [1a5b252] | 303 | extern void remove_dev_node(dev_tree_t *, dev_node_t *);
|
|---|
| [ba38f72c] | 304 | extern bool insert_fun_node(dev_tree_t *, fun_node_t *, char *, dev_node_t *);
|
|---|
| [d0dd7b5] | 305 | extern void remove_fun_node(dev_tree_t *, fun_node_t *);
|
|---|
| [e2b9a993] | 306 |
|
|---|
| [15f3c3f] | 307 | /* Loc services */
|
|---|
| [ce89036b] | 308 |
|
|---|
| [15f3c3f] | 309 | extern void loc_register_tree_function(fun_node_t *, dev_tree_t *);
|
|---|
| [8b1e15ac] | 310 |
|
|---|
| [15f3c3f] | 311 | extern fun_node_t *find_loc_tree_function(dev_tree_t *, service_id_t);
|
|---|
| [a32defa] | 312 |
|
|---|
| [15f3c3f] | 313 | extern void tree_add_loc_function(dev_tree_t *, fun_node_t *);
|
|---|
| [a32defa] | 314 |
|
|---|
| [e2b9a993] | 315 | #endif
|
|---|
| [c16cf62] | 316 |
|
|---|
| 317 | /** @}
|
|---|
| [38b3baf] | 318 | */
|
|---|