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