[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>
|
---|
[d51ee2b] | 43 | #include <ipc/devmap.h>
|
---|
[e85920d] | 44 | #include <fibril_synch.h>
|
---|
[084ff99] | 45 | #include <atomic.h>
|
---|
[e2b9a993] | 46 |
|
---|
[08d9c4e6] | 47 | #include "util.h"
|
---|
[e2b9a993] | 48 |
|
---|
| 49 | #define NAME "devman"
|
---|
| 50 |
|
---|
| 51 | #define MATCH_EXT ".ma"
|
---|
[957cfa58] | 52 | #define DEVICE_BUCKETS 256
|
---|
[e2b9a993] | 53 |
|
---|
[ce89036b] | 54 | #define DEVMAP_CLASS_NAMESPACE "class"
|
---|
| 55 | #define DEVMAP_DEVICE_NAMESPACE "devices"
|
---|
[a32defa] | 56 | #define DEVMAP_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 |
|
---|
| 89 | /** Phone asociated with this driver. */
|
---|
[3ad7b1c] | 90 | int phone;
|
---|
[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;
|
---|
[38b3baf] | 97 | /** Pointer to the linked list of devices controlled by this driver. */
|
---|
[e2b9a993] | 98 | link_t devices;
|
---|
[38b3baf] | 99 |
|
---|
| 100 | /**
|
---|
| 101 | * Fibril mutex for this driver - driver state, list of devices, phone.
|
---|
| 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 */
|
---|
| 109 | link_t drivers;
|
---|
| 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. */
|
---|
| 131 | link_t functions;
|
---|
| 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 |
|
---|
[ba38f72c] | 171 | /** The list of device classes to which this device function belongs. */
|
---|
[d51ee2b] | 172 | link_t classes;
|
---|
[ba38f72c] | 173 | /** Devmap handle if the device function is registered by devmap. */
|
---|
[991f645] | 174 | devmap_handle_t devmap_handle;
|
---|
[38b3baf] | 175 |
|
---|
| 176 | /**
|
---|
[ba38f72c] | 177 | * Used by the hash table of functions indexed by devman device handles.
|
---|
[38b3baf] | 178 | */
|
---|
[ba38f72c] | 179 | link_t devman_fun;
|
---|
[38b3baf] | 180 |
|
---|
| 181 | /**
|
---|
[ba38f72c] | 182 | * Used by the hash table of functions indexed by devmap device handles.
|
---|
[5bee897] | 183 | */
|
---|
[ba38f72c] | 184 | link_t devmap_fun;
|
---|
[e2b9a993] | 185 | };
|
---|
| 186 |
|
---|
[ba38f72c] | 187 |
|
---|
[38b3baf] | 188 | /** Represents device tree. */
|
---|
[e2b9a993] | 189 | typedef struct dev_tree {
|
---|
| 190 | /** Root device node. */
|
---|
[ba38f72c] | 191 | fun_node_t *root_node;
|
---|
[38b3baf] | 192 |
|
---|
| 193 | /**
|
---|
| 194 | * The next available handle - handles are assigned in a sequential
|
---|
| 195 | * manner.
|
---|
| 196 | */
|
---|
[0b5a4131] | 197 | devman_handle_t current_handle;
|
---|
[38b3baf] | 198 |
|
---|
| 199 | /** Synchronize access to the device tree. */
|
---|
[957cfa58] | 200 | fibril_rwlock_t rwlock;
|
---|
[38b3baf] | 201 |
|
---|
| 202 | /** Hash table of all devices indexed by devman handles. */
|
---|
[957cfa58] | 203 | hash_table_t devman_devices;
|
---|
[38b3baf] | 204 |
|
---|
[ba38f72c] | 205 | /** Hash table of all devices indexed by devman handles. */
|
---|
| 206 | hash_table_t devman_functions;
|
---|
| 207 |
|
---|
[38b3baf] | 208 | /**
|
---|
| 209 | * Hash table of devices registered by devmapper, indexed by devmap
|
---|
| 210 | * handles.
|
---|
| 211 | */
|
---|
[ba38f72c] | 212 | hash_table_t devmap_functions;
|
---|
[e2b9a993] | 213 | } dev_tree_t;
|
---|
| 214 |
|
---|
[38b3baf] | 215 | typedef struct dev_class {
|
---|
| 216 | /** The name of the class. */
|
---|
[d51ee2b] | 217 | const char *name;
|
---|
[38b3baf] | 218 |
|
---|
| 219 | /**
|
---|
| 220 | * Pointer to the previous and next class in the list of registered
|
---|
| 221 | * classes.
|
---|
| 222 | */
|
---|
| 223 | link_t link;
|
---|
| 224 |
|
---|
| 225 | /**
|
---|
| 226 | * List of dev_class_info structures - one for each device registered by
|
---|
| 227 | * this class.
|
---|
| 228 | */
|
---|
[692c40cb] | 229 | link_t devices;
|
---|
[38b3baf] | 230 |
|
---|
| 231 | /**
|
---|
| 232 | * Default base name for the device within the class, might be overrided
|
---|
| 233 | * by the driver.
|
---|
| 234 | */
|
---|
[d51ee2b] | 235 | const char *base_dev_name;
|
---|
[38b3baf] | 236 |
|
---|
| 237 | /** Unique numerical identifier of the newly added device. */
|
---|
[d51ee2b] | 238 | size_t curr_dev_idx;
|
---|
| 239 | /** Synchronize access to the list of devices in this class. */
|
---|
| 240 | fibril_mutex_t mutex;
|
---|
| 241 | } dev_class_t;
|
---|
| 242 |
|
---|
[58b833c] | 243 | /**
|
---|
[ba38f72c] | 244 | * Provides n-to-m mapping between function nodes and classes - each function
|
---|
| 245 | * can register in an arbitrary number of classes and each class can contain
|
---|
| 246 | * an arbitrary number of device functions.
|
---|
[38b3baf] | 247 | */
|
---|
[d51ee2b] | 248 | typedef struct dev_class_info {
|
---|
[38b3baf] | 249 | /** The class. */
|
---|
[d51ee2b] | 250 | dev_class_t *dev_class;
|
---|
[38b3baf] | 251 | /** The device. */
|
---|
[ba38f72c] | 252 | fun_node_t *fun;
|
---|
[38b3baf] | 253 |
|
---|
| 254 | /**
|
---|
| 255 | * Pointer to the previous and next class info in the list of devices
|
---|
| 256 | * registered by the class.
|
---|
| 257 | */
|
---|
[d51ee2b] | 258 | link_t link;
|
---|
[38b3baf] | 259 |
|
---|
| 260 | /**
|
---|
| 261 | * Pointer to the previous and next class info in the list of classes
|
---|
| 262 | * by which the device is registered.
|
---|
| 263 | */
|
---|
[d51ee2b] | 264 | link_t dev_classes;
|
---|
[38b3baf] | 265 |
|
---|
[ba38f72c] | 266 | /** The name of the device function within the class. */
|
---|
[38b3baf] | 267 | char *dev_name;
|
---|
| 268 | /** The handle of the device by device mapper in the class namespace. */
|
---|
[991f645] | 269 | devmap_handle_t devmap_handle;
|
---|
[38b3baf] | 270 |
|
---|
| 271 | /**
|
---|
| 272 | * Link in the hash table of devices registered by the devmapper using
|
---|
| 273 | * their class names.
|
---|
| 274 | */
|
---|
[ce89036b] | 275 | link_t devmap_link;
|
---|
[d51ee2b] | 276 | } dev_class_info_t;
|
---|
[e2b9a993] | 277 |
|
---|
[692c40cb] | 278 | /** The list of device classes. */
|
---|
| 279 | typedef struct class_list {
|
---|
[38b3baf] | 280 | /** List of classes. */
|
---|
[692c40cb] | 281 | link_t classes;
|
---|
[38b3baf] | 282 |
|
---|
| 283 | /**
|
---|
| 284 | * Hash table of devices registered by devmapper using their class name,
|
---|
| 285 | * indexed by devmap handles.
|
---|
| 286 | */
|
---|
[ba38f72c] | 287 | hash_table_t devmap_functions;
|
---|
[38b3baf] | 288 |
|
---|
[692c40cb] | 289 | /** Fibril mutex for list of classes. */
|
---|
[38b3baf] | 290 | fibril_rwlock_t rwlock;
|
---|
[692c40cb] | 291 | } class_list_t;
|
---|
| 292 |
|
---|
[38b3baf] | 293 | /* Match ids and scores */
|
---|
[e2b9a993] | 294 |
|
---|
[ba38f72c] | 295 | extern int get_match_score(driver_t *, dev_node_t *);
|
---|
[e2b9a993] | 296 |
|
---|
[38b3baf] | 297 | extern bool parse_match_ids(char *, match_id_list_t *);
|
---|
| 298 | extern bool read_match_ids(const char *, match_id_list_t *);
|
---|
| 299 | extern char *read_match_id(char **);
|
---|
| 300 | extern char *read_id(const char **);
|
---|
[729fa2d6] | 301 |
|
---|
[38b3baf] | 302 | /* Drivers */
|
---|
[0c3666d] | 303 |
|
---|
[791f58c] | 304 | extern void init_driver_list(driver_list_t *);
|
---|
[38b3baf] | 305 | extern driver_t *create_driver(void);
|
---|
| 306 | extern bool get_driver_info(const char *, const char *, driver_t *);
|
---|
| 307 | extern int lookup_available_drivers(driver_list_t *, const char *);
|
---|
[e2b9a993] | 308 |
|
---|
[ba38f72c] | 309 | extern driver_t *find_best_match_driver(driver_list_t *, dev_node_t *);
|
---|
| 310 | extern bool assign_driver(dev_node_t *, driver_list_t *, dev_tree_t *);
|
---|
[e2b9a993] | 311 |
|
---|
[38b3baf] | 312 | extern void add_driver(driver_list_t *, driver_t *);
|
---|
[ba38f72c] | 313 | extern void attach_driver(dev_node_t *, driver_t *);
|
---|
| 314 | extern void add_device(int, driver_t *, dev_node_t *, dev_tree_t *);
|
---|
[38b3baf] | 315 | extern bool start_driver(driver_t *);
|
---|
[e2b9a993] | 316 |
|
---|
[38b3baf] | 317 | extern driver_t *find_driver(driver_list_t *, const char *);
|
---|
[791f58c] | 318 | extern void initialize_running_driver(driver_t *, dev_tree_t *);
|
---|
[e2b9a993] | 319 |
|
---|
[791f58c] | 320 | extern void init_driver(driver_t *);
|
---|
| 321 | extern void clean_driver(driver_t *);
|
---|
| 322 | extern void delete_driver(driver_t *);
|
---|
[38b3baf] | 323 |
|
---|
| 324 | /* Device nodes */
|
---|
| 325 |
|
---|
[ba38f72c] | 326 | extern dev_node_t *create_dev_node(void);
|
---|
| 327 | extern void delete_dev_node(dev_node_t *node);
|
---|
| 328 | extern dev_node_t *find_dev_node_no_lock(dev_tree_t *tree,
|
---|
| 329 | devman_handle_t handle);
|
---|
| 330 | extern dev_node_t *find_dev_node(dev_tree_t *tree, devman_handle_t handle);
|
---|
| 331 | extern dev_node_t *find_dev_function(dev_node_t *, const char *);
|
---|
| 332 |
|
---|
| 333 | extern fun_node_t *create_fun_node(void);
|
---|
| 334 | extern void delete_fun_node(fun_node_t *);
|
---|
| 335 | extern fun_node_t *find_fun_node_no_lock(dev_tree_t *tree,
|
---|
[0b5a4131] | 336 | devman_handle_t handle);
|
---|
[ba38f72c] | 337 | extern fun_node_t *find_fun_node(dev_tree_t *tree, devman_handle_t handle);
|
---|
| 338 | extern fun_node_t *find_fun_node_by_path(dev_tree_t *, char *);
|
---|
[0876062] | 339 | extern fun_node_t *find_fun_node_in_device(dev_node_t *, const char *);
|
---|
[fc8c2b6] | 340 | extern fun_node_t *find_fun_node_by_class(class_list_t *, const char *, const char *);
|
---|
[38b3baf] | 341 |
|
---|
| 342 | /* Device tree */
|
---|
[5cd136ab] | 343 |
|
---|
[38b3baf] | 344 | extern bool init_device_tree(dev_tree_t *, driver_list_t *);
|
---|
[ba38f72c] | 345 | extern bool create_root_nodes(dev_tree_t *);
|
---|
| 346 | extern bool insert_dev_node(dev_tree_t *, dev_node_t *, fun_node_t *);
|
---|
| 347 | extern bool insert_fun_node(dev_tree_t *, fun_node_t *, char *, dev_node_t *);
|
---|
[e2b9a993] | 348 |
|
---|
[38b3baf] | 349 | /* Device classes */
|
---|
[d51ee2b] | 350 |
|
---|
[791f58c] | 351 | extern dev_class_t *create_dev_class(void);
|
---|
| 352 | extern dev_class_info_t *create_dev_class_info(void);
|
---|
| 353 | extern size_t get_new_class_dev_idx(dev_class_t *);
|
---|
[38b3baf] | 354 | extern char *create_dev_name_for_class(dev_class_t *, const char *);
|
---|
[ba38f72c] | 355 | extern dev_class_info_t *add_function_to_class(fun_node_t *, dev_class_t *,
|
---|
[38b3baf] | 356 | const char *);
|
---|
[d51ee2b] | 357 |
|
---|
[38b3baf] | 358 | extern void init_class_list(class_list_t *);
|
---|
[692c40cb] | 359 |
|
---|
[38b3baf] | 360 | extern dev_class_t *get_dev_class(class_list_t *, char *);
|
---|
| 361 | extern dev_class_t *find_dev_class_no_lock(class_list_t *, const char *);
|
---|
[fc8c2b6] | 362 | extern dev_class_info_t *find_dev_in_class(dev_class_t *, const char *);
|
---|
[791f58c] | 363 | extern void add_dev_class_no_lock(class_list_t *, dev_class_t *);
|
---|
[ce89036b] | 364 |
|
---|
[38b3baf] | 365 | /* Devmap devices */
|
---|
[ce89036b] | 366 |
|
---|
[8b1e15ac] | 367 | extern void devmap_register_tree_function(fun_node_t *, dev_tree_t *);
|
---|
| 368 |
|
---|
[ba38f72c] | 369 | extern fun_node_t *find_devmap_tree_function(dev_tree_t *, devmap_handle_t);
|
---|
| 370 | extern fun_node_t *find_devmap_class_function(class_list_t *, devmap_handle_t);
|
---|
[a32defa] | 371 |
|
---|
[ba38f72c] | 372 | extern void class_add_devmap_function(class_list_t *, dev_class_info_t *);
|
---|
| 373 | extern void tree_add_devmap_function(dev_tree_t *, fun_node_t *);
|
---|
[a32defa] | 374 |
|
---|
[e2b9a993] | 375 | #endif
|
---|
[c16cf62] | 376 |
|
---|
| 377 | /** @}
|
---|
[38b3baf] | 378 | */
|
---|