| 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 <string.h>
|
|---|
| 40 | #include <adt/list.h>
|
|---|
| 41 | #include <ipc/ipc.h>
|
|---|
| 42 |
|
|---|
| 43 | #include "util.h"
|
|---|
| 44 |
|
|---|
| 45 | #define NAME "devman"
|
|---|
| 46 |
|
|---|
| 47 | #define MATCH_EXT ".ma"
|
|---|
| 48 |
|
|---|
| 49 | struct node;
|
|---|
| 50 |
|
|---|
| 51 | typedef struct node node_t;
|
|---|
| 52 |
|
|---|
| 53 | /** Ids of device models used for device-to-driver matching.
|
|---|
| 54 | */
|
|---|
| 55 | typedef struct match_id {
|
|---|
| 56 | /** Pointers to next and previous ids.
|
|---|
| 57 | */
|
|---|
| 58 | link_t link;
|
|---|
| 59 | /** Id of device model.
|
|---|
| 60 | */
|
|---|
| 61 | const char *id;
|
|---|
| 62 | /** Relevancy of device-to-driver match.
|
|---|
| 63 | * The higher is the product of scores specified for the device by the bus driver and by the leaf driver,
|
|---|
| 64 | * the more suitable is the leaf driver for handling the device.
|
|---|
| 65 | */
|
|---|
| 66 | unsigned int score;
|
|---|
| 67 | } match_id_t;
|
|---|
| 68 |
|
|---|
| 69 | /** List of ids for matching devices to drivers sorted
|
|---|
| 70 | * according to match scores in descending order.
|
|---|
| 71 | */
|
|---|
| 72 | typedef struct match_id_list {
|
|---|
| 73 | link_t ids;
|
|---|
| 74 | } match_id_list_t;
|
|---|
| 75 |
|
|---|
| 76 | /** Representation of device driver.
|
|---|
| 77 | */
|
|---|
| 78 | typedef struct driver {
|
|---|
| 79 | /** Pointers to previous and next drivers in a linked list */
|
|---|
| 80 | link_t drivers;
|
|---|
| 81 | /** Specifies whether the driver has been started.*/
|
|---|
| 82 | bool running;
|
|---|
| 83 | /** Phone asociated with this driver */
|
|---|
| 84 | ipcarg_t phone;
|
|---|
| 85 | /** Name of the device driver */
|
|---|
| 86 | char *name;
|
|---|
| 87 | /** Path to the driver's binary */
|
|---|
| 88 | const char *binary_path;
|
|---|
| 89 | /** List of device ids for device-to-driver matching.*/
|
|---|
| 90 | match_id_list_t match_ids;
|
|---|
| 91 | /** Pointer to the linked list of devices controlled by this driver */
|
|---|
| 92 | link_t devices;
|
|---|
| 93 | } driver_t;
|
|---|
| 94 |
|
|---|
| 95 | /** Representation of a node in the device tree.*/
|
|---|
| 96 | struct node {
|
|---|
| 97 | /** The node of the parent device. */
|
|---|
| 98 | node_t *parent;
|
|---|
| 99 | /** Pointers to previous and next child devices in the linked list of parent device's node.*/
|
|---|
| 100 | link_t sibling;
|
|---|
| 101 | /** List of child device nodes. */
|
|---|
| 102 | link_t children;
|
|---|
| 103 | /** List of device ids for device-to-driver matching.*/
|
|---|
| 104 | match_id_list_t match_ids;
|
|---|
| 105 | /** Driver of this device.*/
|
|---|
| 106 | driver_t *drv;
|
|---|
| 107 | /** Pointer to the previous and next device in the list of devices
|
|---|
| 108 | owned by one driver */
|
|---|
| 109 | link_t driver_devices;
|
|---|
| 110 | };
|
|---|
| 111 |
|
|---|
| 112 | /** Represents device tree.
|
|---|
| 113 | */
|
|---|
| 114 | typedef struct dev_tree {
|
|---|
| 115 | /** Root device node. */
|
|---|
| 116 | node_t *root_node;
|
|---|
| 117 | } dev_tree_t;
|
|---|
| 118 |
|
|---|
| 119 |
|
|---|
| 120 |
|
|---|
| 121 | // Match ids and scores
|
|---|
| 122 |
|
|---|
| 123 | int get_match_score(driver_t *drv, node_t *dev);
|
|---|
| 124 |
|
|---|
| 125 | bool parse_match_ids(const char *buf, match_id_list_t *ids);
|
|---|
| 126 | bool read_match_ids(const char *conf_path, match_id_list_t *ids);
|
|---|
| 127 | char * read_id(const char **buf) ;
|
|---|
| 128 | void add_match_id(match_id_list_t *ids, match_id_t *id);
|
|---|
| 129 |
|
|---|
| 130 | void clean_match_ids(match_id_list_t *ids);
|
|---|
| 131 |
|
|---|
| 132 |
|
|---|
| 133 | static inline match_id_t * create_match_id()
|
|---|
| 134 | {
|
|---|
| 135 | match_id_t *id = malloc(sizeof(match_id_t));
|
|---|
| 136 | memset(id, 0, sizeof(match_id_t));
|
|---|
| 137 | return id;
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | static inline void delete_match_id(match_id_t *id)
|
|---|
| 141 | {
|
|---|
| 142 | if (id) {
|
|---|
| 143 | free_not_null(id->id);
|
|---|
| 144 | free(id);
|
|---|
| 145 | }
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 | // Drivers
|
|---|
| 149 |
|
|---|
| 150 | driver_t * create_driver();
|
|---|
| 151 | bool get_driver_info(const char *base_path, const char *name, driver_t *drv);
|
|---|
| 152 | int lookup_available_drivers(link_t *drivers_list, const char *dir_path);
|
|---|
| 153 |
|
|---|
| 154 | driver_t * find_best_match_driver(link_t *drivers_list, node_t *node);
|
|---|
| 155 | bool assign_driver(node_t *node, link_t *drivers_list);
|
|---|
| 156 |
|
|---|
| 157 | void attach_driver(node_t *node, driver_t *drv);
|
|---|
| 158 | bool add_device(driver_t *drv, node_t *node);
|
|---|
| 159 | bool start_driver(driver_t *drv);
|
|---|
| 160 |
|
|---|
| 161 |
|
|---|
| 162 | static inline void init_driver(driver_t *drv)
|
|---|
| 163 | {
|
|---|
| 164 | printf(NAME ": init_driver\n");
|
|---|
| 165 | assert(drv != NULL);
|
|---|
| 166 |
|
|---|
| 167 | memset(drv, 0, sizeof(driver_t));
|
|---|
| 168 | list_initialize(&drv->match_ids.ids);
|
|---|
| 169 | list_initialize(&drv->devices);
|
|---|
| 170 | }
|
|---|
| 171 |
|
|---|
| 172 | static inline void clean_driver(driver_t *drv)
|
|---|
| 173 | {
|
|---|
| 174 | printf(NAME ": clean_driver\n");
|
|---|
| 175 | assert(drv != NULL);
|
|---|
| 176 |
|
|---|
| 177 | free_not_null(drv->name);
|
|---|
| 178 | free_not_null(drv->binary_path);
|
|---|
| 179 |
|
|---|
| 180 | clean_match_ids(&drv->match_ids);
|
|---|
| 181 |
|
|---|
| 182 | init_driver(drv);
|
|---|
| 183 | }
|
|---|
| 184 |
|
|---|
| 185 | static inline void delete_driver(driver_t *drv)
|
|---|
| 186 | {
|
|---|
| 187 | printf(NAME ": delete_driver\n");
|
|---|
| 188 | assert(NULL != drv);
|
|---|
| 189 |
|
|---|
| 190 | clean_driver(drv);
|
|---|
| 191 | free(drv);
|
|---|
| 192 | }
|
|---|
| 193 |
|
|---|
| 194 | static inline void add_driver(link_t *drivers_list, driver_t *drv)
|
|---|
| 195 | {
|
|---|
| 196 | list_prepend(&drv->drivers, drivers_list);
|
|---|
| 197 | printf(NAME": the '%s' driver was added to the list of available drivers.\n", drv->name);
|
|---|
| 198 | }
|
|---|
| 199 |
|
|---|
| 200 |
|
|---|
| 201 | // Device nodes
|
|---|
| 202 | node_t * create_root_node();
|
|---|
| 203 |
|
|---|
| 204 | static inline node_t * create_dev_node()
|
|---|
| 205 | {
|
|---|
| 206 | node_t *res = malloc(sizeof(node_t));
|
|---|
| 207 | if (res != NULL) {
|
|---|
| 208 | memset(res, 0, sizeof(node_t));
|
|---|
| 209 | }
|
|---|
| 210 | return res;
|
|---|
| 211 | }
|
|---|
| 212 |
|
|---|
| 213 | static inline void init_dev_node(node_t *node, node_t *parent)
|
|---|
| 214 | {
|
|---|
| 215 | assert(NULL != node);
|
|---|
| 216 |
|
|---|
| 217 | node->parent = parent;
|
|---|
| 218 | if (NULL != parent) {
|
|---|
| 219 | list_append(&node->sibling, &parent->children);
|
|---|
| 220 | }
|
|---|
| 221 |
|
|---|
| 222 | list_initialize(&node->children);
|
|---|
| 223 |
|
|---|
| 224 | list_initialize(&node->match_ids.ids);
|
|---|
| 225 | }
|
|---|
| 226 |
|
|---|
| 227 |
|
|---|
| 228 | // Device tree
|
|---|
| 229 |
|
|---|
| 230 | bool init_device_tree(dev_tree_t *tree, link_t *drivers_list);
|
|---|
| 231 |
|
|---|
| 232 |
|
|---|
| 233 | #endif
|
|---|