[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>
|
---|
| 39 | #include <string.h>
|
---|
| 40 | #include <adt/list.h>
|
---|
| 41 | #include <ipc/ipc.h>
|
---|
[bda60d9] | 42 | #include <ipc/devman.h>
|
---|
[e85920d] | 43 | #include <fibril_synch.h>
|
---|
[084ff99] | 44 | #include <atomic.h>
|
---|
[e2b9a993] | 45 |
|
---|
[08d9c4e6] | 46 | #include "util.h"
|
---|
[e2b9a993] | 47 |
|
---|
| 48 | #define NAME "devman"
|
---|
| 49 |
|
---|
| 50 | #define MATCH_EXT ".ma"
|
---|
[bda60d9] | 51 | #define MAX_DEV 256
|
---|
[e2b9a993] | 52 |
|
---|
| 53 | struct node;
|
---|
| 54 |
|
---|
| 55 | typedef struct node node_t;
|
---|
| 56 |
|
---|
[e85920d] | 57 | typedef enum {
|
---|
| 58 | /** driver has not been started */
|
---|
| 59 | DRIVER_NOT_STARTED = 0,
|
---|
| 60 | /** driver has been started, but has not registered as running and ready to receive requests */
|
---|
| 61 | DRIVER_STARTING,
|
---|
| 62 | /** driver is running and prepared to serve incomming requests */
|
---|
| 63 | DRIVER_RUNNING
|
---|
| 64 | } driver_state_t;
|
---|
| 65 |
|
---|
[e2b9a993] | 66 | /** Representation of device driver.
|
---|
| 67 | */
|
---|
| 68 | typedef struct driver {
|
---|
| 69 | /** Pointers to previous and next drivers in a linked list */
|
---|
| 70 | link_t drivers;
|
---|
[e85920d] | 71 | /** Specifies whether the driver has been started and wheter is running and prepared to receive requests.*/
|
---|
| 72 | int state;
|
---|
[e2b9a993] | 73 | /** Phone asociated with this driver */
|
---|
| 74 | ipcarg_t phone;
|
---|
| 75 | /** Name of the device driver */
|
---|
| 76 | char *name;
|
---|
| 77 | /** Path to the driver's binary */
|
---|
| 78 | const char *binary_path;
|
---|
| 79 | /** List of device ids for device-to-driver matching.*/
|
---|
| 80 | match_id_list_t match_ids;
|
---|
| 81 | /** Pointer to the linked list of devices controlled by this driver */
|
---|
| 82 | link_t devices;
|
---|
[e85920d] | 83 | /** Fibril mutex for this driver - driver state, list of devices, phone.*/
|
---|
| 84 | fibril_mutex_t driver_mutex;
|
---|
[e2b9a993] | 85 | } driver_t;
|
---|
| 86 |
|
---|
[e85920d] | 87 | /** The list of drivers. */
|
---|
| 88 | typedef struct driver_list {
|
---|
| 89 | /** List of drivers */
|
---|
| 90 | link_t drivers;
|
---|
| 91 | /** Fibril mutex for list of drivers. */
|
---|
| 92 | fibril_mutex_t drivers_mutex;
|
---|
| 93 | } driver_list_t;
|
---|
| 94 |
|
---|
[df747b9c] | 95 | /** The state of the device. */
|
---|
| 96 | typedef enum {
|
---|
| 97 | DEVICE_NOT_INITIALIZED = 0,
|
---|
| 98 | DEVICE_USABLE,
|
---|
| 99 | DEVICE_NOT_PRESENT,
|
---|
| 100 | DEVICE_INVALID
|
---|
| 101 | } device_state_t;
|
---|
| 102 |
|
---|
[e2b9a993] | 103 | /** Representation of a node in the device tree.*/
|
---|
| 104 | struct node {
|
---|
[084ff99] | 105 | /** The global unique identifier of the device.*/
|
---|
[bda60d9] | 106 | device_handle_t handle;
|
---|
| 107 | /** The name of the device specified by its parent. */
|
---|
| 108 | char *name;
|
---|
| 109 | /** Full path and name of the device in device hierarchi (i. e. in full path in device tree).*/
|
---|
| 110 | char *pathname;
|
---|
[e2b9a993] | 111 | /** The node of the parent device. */
|
---|
| 112 | node_t *parent;
|
---|
| 113 | /** Pointers to previous and next child devices in the linked list of parent device's node.*/
|
---|
[08d9c4e6] | 114 | link_t sibling;
|
---|
[e2b9a993] | 115 | /** List of child device nodes. */
|
---|
| 116 | link_t children;
|
---|
[e85920d] | 117 | /** Fibril mutex for the list of child device nodes of this node. */
|
---|
| 118 | fibril_mutex_t children_mutex;
|
---|
[e2b9a993] | 119 | /** List of device ids for device-to-driver matching.*/
|
---|
[08d9c4e6] | 120 | match_id_list_t match_ids;
|
---|
[e2b9a993] | 121 | /** Driver of this device.*/
|
---|
[08d9c4e6] | 122 | driver_t *drv;
|
---|
[df747b9c] | 123 | /** The state of the device. */
|
---|
| 124 | device_state_t state;
|
---|
[e2b9a993] | 125 | /** Pointer to the previous and next device in the list of devices
|
---|
| 126 | owned by one driver */
|
---|
[08d9c4e6] | 127 | link_t driver_devices;
|
---|
[e2b9a993] | 128 | };
|
---|
| 129 |
|
---|
| 130 | /** Represents device tree.
|
---|
| 131 | */
|
---|
| 132 | typedef struct dev_tree {
|
---|
| 133 | /** Root device node. */
|
---|
| 134 | node_t *root_node;
|
---|
[bda60d9] | 135 | /** The next available handle - handles are assigned in a sequential manner.*/
|
---|
[084ff99] | 136 | atomic_t current_handle;
|
---|
[bda60d9] | 137 | /** Handle-to-node mapping. */
|
---|
| 138 | node_t * node_map[MAX_DEV];
|
---|
[e2b9a993] | 139 | } dev_tree_t;
|
---|
| 140 |
|
---|
| 141 |
|
---|
| 142 |
|
---|
| 143 | // Match ids and scores
|
---|
| 144 |
|
---|
| 145 | int get_match_score(driver_t *drv, node_t *dev);
|
---|
| 146 |
|
---|
| 147 | bool parse_match_ids(const char *buf, match_id_list_t *ids);
|
---|
| 148 | bool read_match_ids(const char *conf_path, match_id_list_t *ids);
|
---|
| 149 | char * read_id(const char **buf) ;
|
---|
[729fa2d6] | 150 |
|
---|
| 151 | // Drivers
|
---|
[0c3666d] | 152 |
|
---|
[d347b53] | 153 | /**
|
---|
| 154 | * Initialize the list of device driver's.
|
---|
| 155 | *
|
---|
| 156 | * @param drv_list the list of device driver's.
|
---|
| 157 | *
|
---|
| 158 | */
|
---|
[0c3666d] | 159 | static inline void init_driver_list(driver_list_t *drv_list)
|
---|
| 160 | {
|
---|
| 161 | assert(NULL != drv_list);
|
---|
| 162 |
|
---|
| 163 | list_initialize(&drv_list->drivers);
|
---|
| 164 | fibril_mutex_initialize(&drv_list->drivers_mutex);
|
---|
| 165 | }
|
---|
| 166 |
|
---|
[e2b9a993] | 167 | driver_t * create_driver();
|
---|
| 168 | bool get_driver_info(const char *base_path, const char *name, driver_t *drv);
|
---|
[0c3666d] | 169 | int lookup_available_drivers(driver_list_t *drivers_list, const char *dir_path);
|
---|
[e2b9a993] | 170 |
|
---|
[0c3666d] | 171 | driver_t * find_best_match_driver(driver_list_t *drivers_list, node_t *node);
|
---|
| 172 | bool assign_driver(node_t *node, driver_list_t *drivers_list);
|
---|
[e2b9a993] | 173 |
|
---|
[0c3666d] | 174 | void add_driver(driver_list_t *drivers_list, driver_t *drv);
|
---|
[08d9c4e6] | 175 | void attach_driver(node_t *node, driver_t *drv);
|
---|
[084ff99] | 176 | void add_device(int phone, driver_t *drv, node_t *node);
|
---|
[e2b9a993] | 177 | bool start_driver(driver_t *drv);
|
---|
| 178 |
|
---|
[729fa2d6] | 179 | driver_t * find_driver(driver_list_t *drv_list, const char *drv_name);
|
---|
[c16cf62] | 180 | void set_driver_phone(driver_t *driver, ipcarg_t phone);
|
---|
| 181 | void initialize_running_driver(driver_t *driver);
|
---|
[729fa2d6] | 182 |
|
---|
[d347b53] | 183 | /**
|
---|
| 184 | * Initialize device driver structure.
|
---|
| 185 | *
|
---|
| 186 | * @param drv the device driver structure.
|
---|
| 187 | *
|
---|
| 188 | */
|
---|
[08d9c4e6] | 189 | static inline void init_driver(driver_t *drv)
|
---|
[e2b9a993] | 190 | {
|
---|
[08d9c4e6] | 191 | assert(drv != NULL);
|
---|
| 192 |
|
---|
| 193 | memset(drv, 0, sizeof(driver_t));
|
---|
[e2b9a993] | 194 | list_initialize(&drv->match_ids.ids);
|
---|
| 195 | list_initialize(&drv->devices);
|
---|
[924c75e1] | 196 | fibril_mutex_initialize(&drv->driver_mutex);
|
---|
[e2b9a993] | 197 | }
|
---|
| 198 |
|
---|
[d347b53] | 199 | /**
|
---|
| 200 | * Device driver structure clean-up.
|
---|
| 201 | *
|
---|
| 202 | * @param drv the device driver structure.
|
---|
| 203 | */
|
---|
[08d9c4e6] | 204 | static inline void clean_driver(driver_t *drv)
|
---|
[e2b9a993] | 205 | {
|
---|
| 206 | assert(drv != NULL);
|
---|
[08d9c4e6] | 207 |
|
---|
| 208 | free_not_null(drv->name);
|
---|
| 209 | free_not_null(drv->binary_path);
|
---|
| 210 |
|
---|
[e2b9a993] | 211 | clean_match_ids(&drv->match_ids);
|
---|
[08d9c4e6] | 212 |
|
---|
| 213 | init_driver(drv);
|
---|
[e2b9a993] | 214 | }
|
---|
| 215 |
|
---|
[d347b53] | 216 | /**
|
---|
| 217 | * Delete device driver structure.
|
---|
| 218 | *
|
---|
| 219 | * @param drv the device driver structure.*
|
---|
| 220 | */
|
---|
[08d9c4e6] | 221 | static inline void delete_driver(driver_t *drv)
|
---|
[e2b9a993] | 222 | {
|
---|
[08d9c4e6] | 223 | assert(NULL != drv);
|
---|
| 224 |
|
---|
[e2b9a993] | 225 | clean_driver(drv);
|
---|
| 226 | free(drv);
|
---|
| 227 | }
|
---|
| 228 |
|
---|
| 229 | // Device nodes
|
---|
[d347b53] | 230 | /**
|
---|
| 231 | * Create a new device node.
|
---|
| 232 | *
|
---|
| 233 | * @return a device node structure.
|
---|
| 234 | *
|
---|
| 235 | */
|
---|
[e2b9a993] | 236 | static inline node_t * create_dev_node()
|
---|
| 237 | {
|
---|
| 238 | node_t *res = malloc(sizeof(node_t));
|
---|
| 239 | if (res != NULL) {
|
---|
[08d9c4e6] | 240 | memset(res, 0, sizeof(node_t));
|
---|
[e2b9a993] | 241 | }
|
---|
[084ff99] | 242 |
|
---|
| 243 | list_initialize(&res->children);
|
---|
| 244 | list_initialize(&res->match_ids.ids);
|
---|
[d347b53] | 245 | fibril_mutex_initialize(&res->children_mutex);
|
---|
[084ff99] | 246 |
|
---|
[e2b9a993] | 247 | return res;
|
---|
| 248 | }
|
---|
| 249 |
|
---|
[5cd136ab] | 250 | /**
|
---|
| 251 | * Delete a device node.
|
---|
| 252 | *
|
---|
| 253 | * @param node a device node structure.
|
---|
| 254 | *
|
---|
| 255 | */
|
---|
[d347b53] | 256 | static inline void delete_dev_node(node_t *node)
|
---|
| 257 | {
|
---|
| 258 | assert(list_empty(&node->children) && NULL == node->parent && NULL == node->drv);
|
---|
| 259 |
|
---|
| 260 | clean_match_ids(&node->match_ids);
|
---|
| 261 | free_not_null(node->name);
|
---|
| 262 | free_not_null(node->pathname);
|
---|
| 263 | free(node);
|
---|
| 264 | }
|
---|
| 265 |
|
---|
| 266 | /**
|
---|
| 267 | * Find the device node structure of the device witch has the specified handle.
|
---|
| 268 | *
|
---|
| 269 | * @param tree the device tree where we look for the device node.
|
---|
| 270 | * @param handle the handle of the device.
|
---|
| 271 | * @return the device node.
|
---|
| 272 | */
|
---|
[bda60d9] | 273 | static inline node_t * find_dev_node(dev_tree_t *tree, long handle)
|
---|
[e2b9a993] | 274 | {
|
---|
[bda60d9] | 275 | if (handle < MAX_DEV) {
|
---|
| 276 | return tree->node_map[handle];
|
---|
[e2b9a993] | 277 | }
|
---|
[bda60d9] | 278 | return NULL;
|
---|
[e2b9a993] | 279 | }
|
---|
| 280 |
|
---|
[5cd136ab] | 281 | node_t * find_dev_node_by_path(dev_tree_t *tree, char *path);
|
---|
| 282 | node_t *find_node_child(node_t *parent, const char *name);
|
---|
| 283 |
|
---|
[e2b9a993] | 284 | // Device tree
|
---|
| 285 |
|
---|
[0c3666d] | 286 | bool init_device_tree(dev_tree_t *tree, driver_list_t *drivers_list);
|
---|
[084ff99] | 287 | bool create_root_node(dev_tree_t *tree);
|
---|
[5cd136ab] | 288 | bool insert_dev_node(dev_tree_t *tree, node_t *node, char *dev_name, node_t *parent);
|
---|
[e2b9a993] | 289 |
|
---|
| 290 | #endif
|
---|
[c16cf62] | 291 |
|
---|
| 292 | /** @}
|
---|
| 293 | */ |
---|