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