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