source: mainline/uspace/srv/devman/devman.h@ bda60d9

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since bda60d9 was bda60d9, checked in by Lenka Trochtova <trochtova.lenka@…>, 15 years ago

adding child device - parts of code

  • Property mode set to 100644
File size: 6.4 KB
Line 
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#include <ipc/devman.h>
43#include <fibril_synch.h>
44#include <atomic.h>
45
46#include "util.h"
47
48#define NAME "devman"
49
50#define MATCH_EXT ".ma"
51#define MAX_DEV 256
52
53struct node;
54
55typedef struct node node_t;
56
57typedef 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
66/** Representation of device driver.
67 */
68typedef struct driver {
69 /** Pointers to previous and next drivers in a linked list */
70 link_t drivers;
71 /** Specifies whether the driver has been started and wheter is running and prepared to receive requests.*/
72 int state;
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;
83 /** Fibril mutex for this driver - driver state, list of devices, phone.*/
84 fibril_mutex_t driver_mutex;
85} driver_t;
86
87/** The list of drivers. */
88typedef 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
95/** Representation of a node in the device tree.*/
96struct node {
97 /** The global unique identifier of the device.*/
98 device_handle_t handle;
99 /** The name of the device specified by its parent. */
100 char *name;
101 /** Full path and name of the device in device hierarchi (i. e. in full path in device tree).*/
102 char *pathname;
103 /** The node of the parent device. */
104 node_t *parent;
105 /** Pointers to previous and next child devices in the linked list of parent device's node.*/
106 link_t sibling;
107 /** List of child device nodes. */
108 link_t children;
109 /** Fibril mutex for the list of child device nodes of this node. */
110 fibril_mutex_t children_mutex;
111 /** List of device ids for device-to-driver matching.*/
112 match_id_list_t match_ids;
113 /** Driver of this device.*/
114 driver_t *drv;
115 /** Pointer to the previous and next device in the list of devices
116 owned by one driver */
117 link_t driver_devices;
118};
119
120/** Represents device tree.
121 */
122typedef struct dev_tree {
123 /** Root device node. */
124 node_t *root_node;
125 /** The next available handle - handles are assigned in a sequential manner.*/
126 atomic_t current_handle;
127 /** Handle-to-node mapping. */
128 node_t * node_map[MAX_DEV];
129} dev_tree_t;
130
131
132
133// Match ids and scores
134
135int get_match_score(driver_t *drv, node_t *dev);
136
137bool parse_match_ids(const char *buf, match_id_list_t *ids);
138bool read_match_ids(const char *conf_path, match_id_list_t *ids);
139char * read_id(const char **buf) ;
140
141// Drivers
142
143static inline void init_driver_list(driver_list_t *drv_list)
144{
145 assert(NULL != drv_list);
146
147 list_initialize(&drv_list->drivers);
148 fibril_mutex_initialize(&drv_list->drivers_mutex);
149}
150
151driver_t * create_driver();
152bool get_driver_info(const char *base_path, const char *name, driver_t *drv);
153int lookup_available_drivers(driver_list_t *drivers_list, const char *dir_path);
154
155driver_t * find_best_match_driver(driver_list_t *drivers_list, node_t *node);
156bool assign_driver(node_t *node, driver_list_t *drivers_list);
157
158void add_driver(driver_list_t *drivers_list, driver_t *drv);
159void attach_driver(node_t *node, driver_t *drv);
160void add_device(int phone, driver_t *drv, node_t *node);
161bool start_driver(driver_t *drv);
162
163driver_t * find_driver(driver_list_t *drv_list, const char *drv_name);
164void set_driver_phone(driver_t *driver, ipcarg_t phone);
165void initialize_running_driver(driver_t *driver);
166
167
168static inline void init_driver(driver_t *drv)
169{
170 assert(drv != NULL);
171
172 memset(drv, 0, sizeof(driver_t));
173 list_initialize(&drv->match_ids.ids);
174 list_initialize(&drv->devices);
175 fibril_mutex_initialize(&drv->driver_mutex);
176}
177
178static inline void clean_driver(driver_t *drv)
179{
180 assert(drv != NULL);
181
182 free_not_null(drv->name);
183 free_not_null(drv->binary_path);
184
185 clean_match_ids(&drv->match_ids);
186
187 init_driver(drv);
188}
189
190static inline void delete_driver(driver_t *drv)
191{
192 assert(NULL != drv);
193
194 clean_driver(drv);
195 free(drv);
196}
197
198// Device nodes
199
200static inline node_t * create_dev_node()
201{
202 node_t *res = malloc(sizeof(node_t));
203 if (res != NULL) {
204 memset(res, 0, sizeof(node_t));
205 }
206
207 list_initialize(&res->children);
208 list_initialize(&res->match_ids.ids);
209
210 return res;
211}
212
213static inline node_t * find_dev_node(dev_tree_t *tree, long handle)
214{
215 if (handle < MAX_DEV) {
216 return tree->node_map[handle];
217 }
218 return NULL;
219}
220
221// Device tree
222
223bool init_device_tree(dev_tree_t *tree, driver_list_t *drivers_list);
224bool create_root_node(dev_tree_t *tree);
225bool insert_dev_node(dev_tree_t *tree, node_t *node, const char *dev_name, node_t *parent);
226
227#endif
228
229/** @}
230 */
Note: See TracBrowser for help on using the repository browser.