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