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