[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 |
|
---|
[e2b9a993] | 58 | struct node;
|
---|
| 59 | typedef struct node node_t;
|
---|
| 60 |
|
---|
[e85920d] | 61 | typedef enum {
|
---|
[38b3baf] | 62 | /** Driver has not been started. */
|
---|
[e85920d] | 63 | DRIVER_NOT_STARTED = 0,
|
---|
[38b3baf] | 64 |
|
---|
| 65 | /**
|
---|
| 66 | * Driver has been started, but has not registered as running and ready
|
---|
| 67 | * to receive requests.
|
---|
| 68 | */
|
---|
[e85920d] | 69 | DRIVER_STARTING,
|
---|
[38b3baf] | 70 |
|
---|
| 71 | /** Driver is running and prepared to serve incomming requests. */
|
---|
[e85920d] | 72 | DRIVER_RUNNING
|
---|
| 73 | } driver_state_t;
|
---|
| 74 |
|
---|
[38b3baf] | 75 | /** Representation of device driver. */
|
---|
[e2b9a993] | 76 | typedef struct driver {
|
---|
[38b3baf] | 77 | /** Pointers to previous and next drivers in a linked list. */
|
---|
[e2b9a993] | 78 | link_t drivers;
|
---|
[38b3baf] | 79 |
|
---|
| 80 | /**
|
---|
| 81 | * Specifies whether the driver has been started and wheter is running
|
---|
| 82 | * and prepared to receive requests.
|
---|
| 83 | */
|
---|
[e85920d] | 84 | int state;
|
---|
[38b3baf] | 85 |
|
---|
| 86 | /** Phone asociated with this driver. */
|
---|
[96b02eb9] | 87 | sysarg_t phone;
|
---|
[38b3baf] | 88 | /** Name of the device driver. */
|
---|
[e2b9a993] | 89 | char *name;
|
---|
[38b3baf] | 90 | /** Path to the driver's binary. */
|
---|
[e2b9a993] | 91 | const char *binary_path;
|
---|
[38b3baf] | 92 | /** List of device ids for device-to-driver matching. */
|
---|
[e2b9a993] | 93 | match_id_list_t match_ids;
|
---|
[38b3baf] | 94 | /** Pointer to the linked list of devices controlled by this driver. */
|
---|
[e2b9a993] | 95 | link_t devices;
|
---|
[38b3baf] | 96 |
|
---|
| 97 | /**
|
---|
| 98 | * Fibril mutex for this driver - driver state, list of devices, phone.
|
---|
| 99 | */
|
---|
[e85920d] | 100 | fibril_mutex_t driver_mutex;
|
---|
[e2b9a993] | 101 | } driver_t;
|
---|
| 102 |
|
---|
[e85920d] | 103 | /** The list of drivers. */
|
---|
| 104 | typedef struct driver_list {
|
---|
| 105 | /** List of drivers */
|
---|
| 106 | link_t drivers;
|
---|
| 107 | /** Fibril mutex for list of drivers. */
|
---|
[38b3baf] | 108 | fibril_mutex_t drivers_mutex;
|
---|
[e85920d] | 109 | } driver_list_t;
|
---|
| 110 |
|
---|
[df747b9c] | 111 | /** The state of the device. */
|
---|
| 112 | typedef enum {
|
---|
| 113 | DEVICE_NOT_INITIALIZED = 0,
|
---|
| 114 | DEVICE_USABLE,
|
---|
| 115 | DEVICE_NOT_PRESENT,
|
---|
| 116 | DEVICE_INVALID
|
---|
| 117 | } device_state_t;
|
---|
| 118 |
|
---|
[38b3baf] | 119 | /** Representation of a node in the device tree. */
|
---|
[e2b9a993] | 120 | struct node {
|
---|
[38b3baf] | 121 | /** The global unique identifier of the device. */
|
---|
[0b5a4131] | 122 | devman_handle_t handle;
|
---|
[bda60d9] | 123 | /** The name of the device specified by its parent. */
|
---|
| 124 | char *name;
|
---|
[38b3baf] | 125 |
|
---|
| 126 | /**
|
---|
| 127 | * Full path and name of the device in device hierarchi (i. e. in full
|
---|
| 128 | * path in device tree).
|
---|
| 129 | */
|
---|
| 130 | char *pathname;
|
---|
| 131 |
|
---|
[e2b9a993] | 132 | /** The node of the parent device. */
|
---|
| 133 | node_t *parent;
|
---|
[38b3baf] | 134 |
|
---|
| 135 | /**
|
---|
| 136 | * Pointers to previous and next child devices in the linked list of
|
---|
| 137 | * parent device's node.
|
---|
| 138 | */
|
---|
[08d9c4e6] | 139 | link_t sibling;
|
---|
[38b3baf] | 140 |
|
---|
[e2b9a993] | 141 | /** List of child device nodes. */
|
---|
| 142 | link_t children;
|
---|
[38b3baf] | 143 | /** List of device ids for device-to-driver matching. */
|
---|
[08d9c4e6] | 144 | match_id_list_t match_ids;
|
---|
[38b3baf] | 145 | /** Driver of this device. */
|
---|
[08d9c4e6] | 146 | driver_t *drv;
|
---|
[df747b9c] | 147 | /** The state of the device. */
|
---|
| 148 | device_state_t state;
|
---|
[38b3baf] | 149 | /**
|
---|
| 150 | * Pointer to the previous and next device in the list of devices
|
---|
| 151 | * owned by one driver.
|
---|
| 152 | */
|
---|
[08d9c4e6] | 153 | link_t driver_devices;
|
---|
[38b3baf] | 154 |
|
---|
| 155 | /** The list of device classes to which this device belongs. */
|
---|
[d51ee2b] | 156 | link_t classes;
|
---|
[957cfa58] | 157 | /** Devmap handle if the device is registered by devmapper. */
|
---|
[991f645] | 158 | devmap_handle_t devmap_handle;
|
---|
[38b3baf] | 159 |
|
---|
| 160 | /**
|
---|
| 161 | * Used by the hash table of devices indexed by devman device handles.
|
---|
| 162 | */
|
---|
[957cfa58] | 163 | link_t devman_link;
|
---|
[38b3baf] | 164 |
|
---|
| 165 | /**
|
---|
| 166 | * Used by the hash table of devices indexed by devmap device handles.
|
---|
| 167 | */
|
---|
[957cfa58] | 168 | link_t devmap_link;
|
---|
[5bee897] | 169 |
|
---|
| 170 | /**
|
---|
| 171 | * Whether this device was already passed to the driver.
|
---|
| 172 | */
|
---|
| 173 | bool passed_to_driver;
|
---|
[e2b9a993] | 174 | };
|
---|
| 175 |
|
---|
[38b3baf] | 176 | /** Represents device tree. */
|
---|
[e2b9a993] | 177 | typedef struct dev_tree {
|
---|
| 178 | /** Root device node. */
|
---|
| 179 | node_t *root_node;
|
---|
[38b3baf] | 180 |
|
---|
| 181 | /**
|
---|
| 182 | * The next available handle - handles are assigned in a sequential
|
---|
| 183 | * manner.
|
---|
| 184 | */
|
---|
[0b5a4131] | 185 | devman_handle_t current_handle;
|
---|
[38b3baf] | 186 |
|
---|
| 187 | /** Synchronize access to the device tree. */
|
---|
[957cfa58] | 188 | fibril_rwlock_t rwlock;
|
---|
[38b3baf] | 189 |
|
---|
| 190 | /** Hash table of all devices indexed by devman handles. */
|
---|
[957cfa58] | 191 | hash_table_t devman_devices;
|
---|
[38b3baf] | 192 |
|
---|
| 193 | /**
|
---|
| 194 | * Hash table of devices registered by devmapper, indexed by devmap
|
---|
| 195 | * handles.
|
---|
| 196 | */
|
---|
[957cfa58] | 197 | hash_table_t devmap_devices;
|
---|
[e2b9a993] | 198 | } dev_tree_t;
|
---|
| 199 |
|
---|
[38b3baf] | 200 | typedef struct dev_class {
|
---|
| 201 | /** The name of the class. */
|
---|
[d51ee2b] | 202 | const char *name;
|
---|
[38b3baf] | 203 |
|
---|
| 204 | /**
|
---|
| 205 | * Pointer to the previous and next class in the list of registered
|
---|
| 206 | * classes.
|
---|
| 207 | */
|
---|
| 208 | link_t link;
|
---|
| 209 |
|
---|
| 210 | /**
|
---|
| 211 | * List of dev_class_info structures - one for each device registered by
|
---|
| 212 | * this class.
|
---|
| 213 | */
|
---|
[692c40cb] | 214 | link_t devices;
|
---|
[38b3baf] | 215 |
|
---|
| 216 | /**
|
---|
| 217 | * Default base name for the device within the class, might be overrided
|
---|
| 218 | * by the driver.
|
---|
| 219 | */
|
---|
[d51ee2b] | 220 | const char *base_dev_name;
|
---|
[38b3baf] | 221 |
|
---|
| 222 | /** Unique numerical identifier of the newly added device. */
|
---|
[d51ee2b] | 223 | size_t curr_dev_idx;
|
---|
| 224 | /** Synchronize access to the list of devices in this class. */
|
---|
| 225 | fibril_mutex_t mutex;
|
---|
| 226 | } dev_class_t;
|
---|
| 227 |
|
---|
[58b833c] | 228 | /**
|
---|
| 229 | * Provides n-to-m mapping between device nodes and classes - each device may
|
---|
[38b3baf] | 230 | * be register to the arbitrary number of classes and each class may contain
|
---|
| 231 | * the arbitrary number of devices.
|
---|
| 232 | */
|
---|
[d51ee2b] | 233 | typedef struct dev_class_info {
|
---|
[38b3baf] | 234 | /** The class. */
|
---|
[d51ee2b] | 235 | dev_class_t *dev_class;
|
---|
[38b3baf] | 236 | /** The device. */
|
---|
[d51ee2b] | 237 | node_t *dev;
|
---|
[38b3baf] | 238 |
|
---|
| 239 | /**
|
---|
| 240 | * Pointer to the previous and next class info in the list of devices
|
---|
| 241 | * registered by the class.
|
---|
| 242 | */
|
---|
[d51ee2b] | 243 | link_t link;
|
---|
[38b3baf] | 244 |
|
---|
| 245 | /**
|
---|
| 246 | * Pointer to the previous and next class info in the list of classes
|
---|
| 247 | * by which the device is registered.
|
---|
| 248 | */
|
---|
[d51ee2b] | 249 | link_t dev_classes;
|
---|
[38b3baf] | 250 |
|
---|
| 251 | /** The name of the device within the class. */
|
---|
| 252 | char *dev_name;
|
---|
| 253 | /** The handle of the device by device mapper in the class namespace. */
|
---|
[991f645] | 254 | devmap_handle_t devmap_handle;
|
---|
[38b3baf] | 255 |
|
---|
| 256 | /**
|
---|
| 257 | * Link in the hash table of devices registered by the devmapper using
|
---|
| 258 | * their class names.
|
---|
| 259 | */
|
---|
[ce89036b] | 260 | link_t devmap_link;
|
---|
[d51ee2b] | 261 | } dev_class_info_t;
|
---|
[e2b9a993] | 262 |
|
---|
[692c40cb] | 263 | /** The list of device classes. */
|
---|
| 264 | typedef struct class_list {
|
---|
[38b3baf] | 265 | /** List of classes. */
|
---|
[692c40cb] | 266 | link_t classes;
|
---|
[38b3baf] | 267 |
|
---|
| 268 | /**
|
---|
| 269 | * Hash table of devices registered by devmapper using their class name,
|
---|
| 270 | * indexed by devmap handles.
|
---|
| 271 | */
|
---|
[ce89036b] | 272 | hash_table_t devmap_devices;
|
---|
[38b3baf] | 273 |
|
---|
[692c40cb] | 274 | /** Fibril mutex for list of classes. */
|
---|
[38b3baf] | 275 | fibril_rwlock_t rwlock;
|
---|
[692c40cb] | 276 | } class_list_t;
|
---|
| 277 |
|
---|
[38b3baf] | 278 | /* Match ids and scores */
|
---|
[e2b9a993] | 279 |
|
---|
[38b3baf] | 280 | extern int get_match_score(driver_t *, node_t *);
|
---|
[e2b9a993] | 281 |
|
---|
[38b3baf] | 282 | extern bool parse_match_ids(char *, match_id_list_t *);
|
---|
| 283 | extern bool read_match_ids(const char *, match_id_list_t *);
|
---|
| 284 | extern char *read_match_id(char **);
|
---|
| 285 | extern char *read_id(const char **);
|
---|
[729fa2d6] | 286 |
|
---|
[38b3baf] | 287 | /* Drivers */
|
---|
[0c3666d] | 288 |
|
---|
[791f58c] | 289 | extern void init_driver_list(driver_list_t *);
|
---|
[38b3baf] | 290 | extern driver_t *create_driver(void);
|
---|
| 291 | extern bool get_driver_info(const char *, const char *, driver_t *);
|
---|
| 292 | extern int lookup_available_drivers(driver_list_t *, const char *);
|
---|
[e2b9a993] | 293 |
|
---|
[38b3baf] | 294 | extern driver_t *find_best_match_driver(driver_list_t *, node_t *);
|
---|
| 295 | extern bool assign_driver(node_t *, driver_list_t *, dev_tree_t *);
|
---|
[e2b9a993] | 296 |
|
---|
[38b3baf] | 297 | extern void add_driver(driver_list_t *, driver_t *);
|
---|
| 298 | extern void attach_driver(node_t *, driver_t *);
|
---|
| 299 | extern void add_device(int, driver_t *, node_t *, dev_tree_t *);
|
---|
| 300 | extern bool start_driver(driver_t *);
|
---|
[e2b9a993] | 301 |
|
---|
[38b3baf] | 302 | extern driver_t *find_driver(driver_list_t *, const char *);
|
---|
[96b02eb9] | 303 | extern void set_driver_phone(driver_t *, sysarg_t);
|
---|
[791f58c] | 304 | extern void initialize_running_driver(driver_t *, dev_tree_t *);
|
---|
[e2b9a993] | 305 |
|
---|
[791f58c] | 306 | extern void init_driver(driver_t *);
|
---|
| 307 | extern void clean_driver(driver_t *);
|
---|
| 308 | extern void delete_driver(driver_t *);
|
---|
[38b3baf] | 309 |
|
---|
| 310 | /* Device nodes */
|
---|
| 311 |
|
---|
[791f58c] | 312 | extern node_t *create_dev_node(void);
|
---|
| 313 | extern void delete_dev_node(node_t *node);
|
---|
| 314 | extern node_t *find_dev_node_no_lock(dev_tree_t *tree,
|
---|
[0b5a4131] | 315 | devman_handle_t handle);
|
---|
| 316 | extern node_t *find_dev_node(dev_tree_t *tree, devman_handle_t handle);
|
---|
[38b3baf] | 317 | extern node_t *find_dev_node_by_path(dev_tree_t *, char *);
|
---|
| 318 | extern node_t *find_node_child(node_t *, const char *);
|
---|
| 319 |
|
---|
| 320 | /* Device tree */
|
---|
[5cd136ab] | 321 |
|
---|
[38b3baf] | 322 | extern bool init_device_tree(dev_tree_t *, driver_list_t *);
|
---|
| 323 | extern bool create_root_node(dev_tree_t *);
|
---|
| 324 | extern bool insert_dev_node(dev_tree_t *, node_t *, char *, node_t *);
|
---|
[e2b9a993] | 325 |
|
---|
[38b3baf] | 326 | /* Device classes */
|
---|
[d51ee2b] | 327 |
|
---|
[791f58c] | 328 | extern dev_class_t *create_dev_class(void);
|
---|
| 329 | extern dev_class_info_t *create_dev_class_info(void);
|
---|
| 330 | extern size_t get_new_class_dev_idx(dev_class_t *);
|
---|
[38b3baf] | 331 | extern char *create_dev_name_for_class(dev_class_t *, const char *);
|
---|
| 332 | extern dev_class_info_t *add_device_to_class(node_t *, dev_class_t *,
|
---|
| 333 | const char *);
|
---|
[d51ee2b] | 334 |
|
---|
[38b3baf] | 335 | extern void init_class_list(class_list_t *);
|
---|
[692c40cb] | 336 |
|
---|
[38b3baf] | 337 | extern dev_class_t *get_dev_class(class_list_t *, char *);
|
---|
| 338 | extern dev_class_t *find_dev_class_no_lock(class_list_t *, const char *);
|
---|
[791f58c] | 339 | extern void add_dev_class_no_lock(class_list_t *, dev_class_t *);
|
---|
[ce89036b] | 340 |
|
---|
[38b3baf] | 341 | /* Devmap devices */
|
---|
[ce89036b] | 342 |
|
---|
[991f645] | 343 | extern node_t *find_devmap_tree_device(dev_tree_t *, devmap_handle_t);
|
---|
| 344 | extern node_t *find_devmap_class_device(class_list_t *, devmap_handle_t);
|
---|
[a32defa] | 345 |
|
---|
[791f58c] | 346 | extern void class_add_devmap_device(class_list_t *, dev_class_info_t *);
|
---|
| 347 | extern void tree_add_devmap_device(dev_tree_t *, node_t *);
|
---|
[a32defa] | 348 |
|
---|
[e2b9a993] | 349 | #endif
|
---|
[c16cf62] | 350 |
|
---|
| 351 | /** @}
|
---|
[38b3baf] | 352 | */
|
---|