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