| 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 | */
|
|---|
| 32 |
|
|---|
| 33 | #ifndef DEVMAN_H_
|
|---|
| 34 | #define DEVMAN_H_
|
|---|
| 35 |
|
|---|
| 36 | #include <assert.h>
|
|---|
| 37 | #include <bool.h>
|
|---|
| 38 | #include <dirent.h>
|
|---|
| 39 | #include <str.h>
|
|---|
| 40 | #include <adt/list.h>
|
|---|
| 41 | #include <adt/hash_table.h>
|
|---|
| 42 | #include <ipc/devman.h>
|
|---|
| 43 | #include <ipc/devmap.h>
|
|---|
| 44 | #include <fibril_synch.h>
|
|---|
| 45 | #include <atomic.h>
|
|---|
| 46 |
|
|---|
| 47 | #include "util.h"
|
|---|
| 48 |
|
|---|
| 49 | #define NAME "devman"
|
|---|
| 50 |
|
|---|
| 51 | #define MATCH_EXT ".ma"
|
|---|
| 52 | #define DEVICE_BUCKETS 256
|
|---|
| 53 |
|
|---|
| 54 | #define DEVMAP_CLASS_NAMESPACE "class"
|
|---|
| 55 | #define DEVMAP_DEVICE_NAMESPACE "devices"
|
|---|
| 56 | #define DEVMAP_SEPARATOR '\\'
|
|---|
| 57 |
|
|---|
| 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;
|
|---|
| 63 |
|
|---|
| 64 | typedef enum {
|
|---|
| 65 | /** Driver has not been started. */
|
|---|
| 66 | DRIVER_NOT_STARTED = 0,
|
|---|
| 67 |
|
|---|
| 68 | /**
|
|---|
| 69 | * Driver has been started, but has not registered as running and ready
|
|---|
| 70 | * to receive requests.
|
|---|
| 71 | */
|
|---|
| 72 | DRIVER_STARTING,
|
|---|
| 73 |
|
|---|
| 74 | /** Driver is running and prepared to serve incomming requests. */
|
|---|
| 75 | DRIVER_RUNNING
|
|---|
| 76 | } driver_state_t;
|
|---|
| 77 |
|
|---|
| 78 | /** Representation of device driver. */
|
|---|
| 79 | typedef struct driver {
|
|---|
| 80 | /** Pointers to previous and next drivers in a linked list. */
|
|---|
| 81 | link_t drivers;
|
|---|
| 82 |
|
|---|
| 83 | /**
|
|---|
| 84 | * Specifies whether the driver has been started and wheter is running
|
|---|
| 85 | * and prepared to receive requests.
|
|---|
| 86 | */
|
|---|
| 87 | int state;
|
|---|
| 88 |
|
|---|
| 89 | /** Phone asociated with this driver. */
|
|---|
| 90 | sysarg_t phone;
|
|---|
| 91 | /** Name of the device driver. */
|
|---|
| 92 | char *name;
|
|---|
| 93 | /** Path to the driver's binary. */
|
|---|
| 94 | const char *binary_path;
|
|---|
| 95 | /** List of device ids for device-to-driver matching. */
|
|---|
| 96 | match_id_list_t match_ids;
|
|---|
| 97 | /** Pointer to the linked list of devices controlled by this driver. */
|
|---|
| 98 | link_t devices;
|
|---|
| 99 |
|
|---|
| 100 | /**
|
|---|
| 101 | * Fibril mutex for this driver - driver state, list of devices, phone.
|
|---|
| 102 | */
|
|---|
| 103 | fibril_mutex_t driver_mutex;
|
|---|
| 104 | } driver_t;
|
|---|
| 105 |
|
|---|
| 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. */
|
|---|
| 111 | fibril_mutex_t drivers_mutex;
|
|---|
| 112 | } driver_list_t;
|
|---|
| 113 |
|
|---|
| 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 |
|
|---|
| 122 | /** Device node in the device tree. */
|
|---|
| 123 | struct dev_node {
|
|---|
| 124 | /** The global unique identifier of the device. */
|
|---|
| 125 | devman_handle_t handle;
|
|---|
| 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;
|
|---|
| 138 |
|
|---|
| 139 | /**
|
|---|
| 140 | * Used by the hash table of devices indexed by devman device handles.
|
|---|
| 141 | */
|
|---|
| 142 | link_t devman_dev;
|
|---|
| 143 |
|
|---|
| 144 | /**
|
|---|
| 145 | * Whether this device was already passed to the driver.
|
|---|
| 146 | */
|
|---|
| 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;
|
|---|
| 162 |
|
|---|
| 163 | /** Link to list of functions in the device (ddf_dev_t.functions) */
|
|---|
| 164 | link_t dev_functions;
|
|---|
| 165 |
|
|---|
| 166 | /** Child device node (if any attached). */
|
|---|
| 167 | dev_node_t *child;
|
|---|
| 168 | /** List of device ids for device-to-driver matching. */
|
|---|
| 169 | match_id_list_t match_ids;
|
|---|
| 170 |
|
|---|
| 171 | /** The list of device classes to which this device function belongs. */
|
|---|
| 172 | link_t classes;
|
|---|
| 173 | /** Devmap handle if the device function is registered by devmap. */
|
|---|
| 174 | devmap_handle_t devmap_handle;
|
|---|
| 175 |
|
|---|
| 176 | /**
|
|---|
| 177 | * Used by the hash table of functions indexed by devman device handles.
|
|---|
| 178 | */
|
|---|
| 179 | link_t devman_fun;
|
|---|
| 180 |
|
|---|
| 181 | /**
|
|---|
| 182 | * Used by the hash table of functions indexed by devmap device handles.
|
|---|
| 183 | */
|
|---|
| 184 | link_t devmap_fun;
|
|---|
| 185 | };
|
|---|
| 186 |
|
|---|
| 187 |
|
|---|
| 188 | /** Represents device tree. */
|
|---|
| 189 | typedef struct dev_tree {
|
|---|
| 190 | /** Root device node. */
|
|---|
| 191 | fun_node_t *root_node;
|
|---|
| 192 |
|
|---|
| 193 | /**
|
|---|
| 194 | * The next available handle - handles are assigned in a sequential
|
|---|
| 195 | * manner.
|
|---|
| 196 | */
|
|---|
| 197 | devman_handle_t current_handle;
|
|---|
| 198 |
|
|---|
| 199 | /** Synchronize access to the device tree. */
|
|---|
| 200 | fibril_rwlock_t rwlock;
|
|---|
| 201 |
|
|---|
| 202 | /** Hash table of all devices indexed by devman handles. */
|
|---|
| 203 | hash_table_t devman_devices;
|
|---|
| 204 |
|
|---|
| 205 | /** Hash table of all devices indexed by devman handles. */
|
|---|
| 206 | hash_table_t devman_functions;
|
|---|
| 207 |
|
|---|
| 208 | /**
|
|---|
| 209 | * Hash table of devices registered by devmapper, indexed by devmap
|
|---|
| 210 | * handles.
|
|---|
| 211 | */
|
|---|
| 212 | hash_table_t devmap_functions;
|
|---|
| 213 | } dev_tree_t;
|
|---|
| 214 |
|
|---|
| 215 | typedef struct dev_class {
|
|---|
| 216 | /** The name of the class. */
|
|---|
| 217 | const char *name;
|
|---|
| 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 | */
|
|---|
| 229 | link_t devices;
|
|---|
| 230 |
|
|---|
| 231 | /**
|
|---|
| 232 | * Default base name for the device within the class, might be overrided
|
|---|
| 233 | * by the driver.
|
|---|
| 234 | */
|
|---|
| 235 | const char *base_dev_name;
|
|---|
| 236 |
|
|---|
| 237 | /** Unique numerical identifier of the newly added device. */
|
|---|
| 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 |
|
|---|
| 243 | /**
|
|---|
| 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.
|
|---|
| 247 | */
|
|---|
| 248 | typedef struct dev_class_info {
|
|---|
| 249 | /** The class. */
|
|---|
| 250 | dev_class_t *dev_class;
|
|---|
| 251 | /** The device. */
|
|---|
| 252 | fun_node_t *fun;
|
|---|
| 253 |
|
|---|
| 254 | /**
|
|---|
| 255 | * Pointer to the previous and next class info in the list of devices
|
|---|
| 256 | * registered by the class.
|
|---|
| 257 | */
|
|---|
| 258 | link_t link;
|
|---|
| 259 |
|
|---|
| 260 | /**
|
|---|
| 261 | * Pointer to the previous and next class info in the list of classes
|
|---|
| 262 | * by which the device is registered.
|
|---|
| 263 | */
|
|---|
| 264 | link_t dev_classes;
|
|---|
| 265 |
|
|---|
| 266 | /** The name of the device function within the class. */
|
|---|
| 267 | char *dev_name;
|
|---|
| 268 | /** The handle of the device by device mapper in the class namespace. */
|
|---|
| 269 | devmap_handle_t devmap_handle;
|
|---|
| 270 |
|
|---|
| 271 | /**
|
|---|
| 272 | * Link in the hash table of devices registered by the devmapper using
|
|---|
| 273 | * their class names.
|
|---|
| 274 | */
|
|---|
| 275 | link_t devmap_link;
|
|---|
| 276 | } dev_class_info_t;
|
|---|
| 277 |
|
|---|
| 278 | /** The list of device classes. */
|
|---|
| 279 | typedef struct class_list {
|
|---|
| 280 | /** List of classes. */
|
|---|
| 281 | link_t classes;
|
|---|
| 282 |
|
|---|
| 283 | /**
|
|---|
| 284 | * Hash table of devices registered by devmapper using their class name,
|
|---|
| 285 | * indexed by devmap handles.
|
|---|
| 286 | */
|
|---|
| 287 | hash_table_t devmap_functions;
|
|---|
| 288 |
|
|---|
| 289 | /** Fibril mutex for list of classes. */
|
|---|
| 290 | fibril_rwlock_t rwlock;
|
|---|
| 291 | } class_list_t;
|
|---|
| 292 |
|
|---|
| 293 | /* Match ids and scores */
|
|---|
| 294 |
|
|---|
| 295 | extern int get_match_score(driver_t *, dev_node_t *);
|
|---|
| 296 |
|
|---|
| 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 **);
|
|---|
| 301 |
|
|---|
| 302 | /* Drivers */
|
|---|
| 303 |
|
|---|
| 304 | extern void init_driver_list(driver_list_t *);
|
|---|
| 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 *);
|
|---|
| 308 |
|
|---|
| 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 *);
|
|---|
| 311 |
|
|---|
| 312 | extern void add_driver(driver_list_t *, driver_t *);
|
|---|
| 313 | extern void attach_driver(dev_node_t *, driver_t *);
|
|---|
| 314 | extern void add_device(int, driver_t *, dev_node_t *, dev_tree_t *);
|
|---|
| 315 | extern bool start_driver(driver_t *);
|
|---|
| 316 |
|
|---|
| 317 | extern driver_t *find_driver(driver_list_t *, const char *);
|
|---|
| 318 | extern void set_driver_phone(driver_t *, sysarg_t);
|
|---|
| 319 | extern void initialize_running_driver(driver_t *, dev_tree_t *);
|
|---|
| 320 |
|
|---|
| 321 | extern void init_driver(driver_t *);
|
|---|
| 322 | extern void clean_driver(driver_t *);
|
|---|
| 323 | extern void delete_driver(driver_t *);
|
|---|
| 324 |
|
|---|
| 325 | /* Device nodes */
|
|---|
| 326 |
|
|---|
| 327 | extern dev_node_t *create_dev_node(void);
|
|---|
| 328 | extern void delete_dev_node(dev_node_t *node);
|
|---|
| 329 | extern dev_node_t *find_dev_node_no_lock(dev_tree_t *tree,
|
|---|
| 330 | devman_handle_t handle);
|
|---|
| 331 | extern dev_node_t *find_dev_node(dev_tree_t *tree, devman_handle_t handle);
|
|---|
| 332 | extern dev_node_t *find_dev_function(dev_node_t *, const char *);
|
|---|
| 333 |
|
|---|
| 334 | extern fun_node_t *create_fun_node(void);
|
|---|
| 335 | extern void delete_fun_node(fun_node_t *);
|
|---|
| 336 | extern fun_node_t *find_fun_node_no_lock(dev_tree_t *tree,
|
|---|
| 337 | devman_handle_t handle);
|
|---|
| 338 | extern fun_node_t *find_fun_node(dev_tree_t *tree, devman_handle_t handle);
|
|---|
| 339 | extern fun_node_t *find_fun_node_by_path(dev_tree_t *, char *);
|
|---|
| 340 | extern fun_node_t *find_fun_node_in_device(dev_node_t *, const char *);
|
|---|
| 341 | extern fun_node_t *find_fun_node_by_class(class_list_t *, const char *, const char *);
|
|---|
| 342 |
|
|---|
| 343 | /* Device tree */
|
|---|
| 344 |
|
|---|
| 345 | extern bool init_device_tree(dev_tree_t *, driver_list_t *);
|
|---|
| 346 | extern bool create_root_nodes(dev_tree_t *);
|
|---|
| 347 | extern bool insert_dev_node(dev_tree_t *, dev_node_t *, fun_node_t *);
|
|---|
| 348 | extern bool insert_fun_node(dev_tree_t *, fun_node_t *, char *, dev_node_t *);
|
|---|
| 349 |
|
|---|
| 350 | /* Device classes */
|
|---|
| 351 |
|
|---|
| 352 | extern dev_class_t *create_dev_class(void);
|
|---|
| 353 | extern dev_class_info_t *create_dev_class_info(void);
|
|---|
| 354 | extern size_t get_new_class_dev_idx(dev_class_t *);
|
|---|
| 355 | extern char *create_dev_name_for_class(dev_class_t *, const char *);
|
|---|
| 356 | extern dev_class_info_t *add_function_to_class(fun_node_t *, dev_class_t *,
|
|---|
| 357 | const char *);
|
|---|
| 358 |
|
|---|
| 359 | extern void init_class_list(class_list_t *);
|
|---|
| 360 |
|
|---|
| 361 | extern dev_class_t *get_dev_class(class_list_t *, char *);
|
|---|
| 362 | extern dev_class_t *find_dev_class_no_lock(class_list_t *, const char *);
|
|---|
| 363 | extern dev_class_info_t *find_dev_in_class(dev_class_t *, const char *);
|
|---|
| 364 | extern void add_dev_class_no_lock(class_list_t *, dev_class_t *);
|
|---|
| 365 |
|
|---|
| 366 | /* Devmap devices */
|
|---|
| 367 |
|
|---|
| 368 | extern void devmap_register_tree_function(fun_node_t *, dev_tree_t *);
|
|---|
| 369 |
|
|---|
| 370 | extern fun_node_t *find_devmap_tree_function(dev_tree_t *, devmap_handle_t);
|
|---|
| 371 | extern fun_node_t *find_devmap_class_function(class_list_t *, devmap_handle_t);
|
|---|
| 372 |
|
|---|
| 373 | extern void class_add_devmap_function(class_list_t *, dev_class_info_t *);
|
|---|
| 374 | extern void tree_add_devmap_function(dev_tree_t *, fun_node_t *);
|
|---|
| 375 |
|
|---|
| 376 | #endif
|
|---|
| 377 |
|
|---|
| 378 | /** @}
|
|---|
| 379 | */
|
|---|