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