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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since d1bafbf was d1bafbf, checked in by Jiri Svoboda <jiri@…>, 12 years ago

Move dev-node related devman functionality to a separate module.

  • Property mode set to 100644
File size: 7.5 KB
Line 
1/*
2 * Copyright (c) 2010 Lenka Trochtova
3 * Copyright (c) 2011 Jiri Svoboda
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/** @addtogroup devman
31 * @{
32 */
33
34#ifndef DEVMAN_H_
35#define DEVMAN_H_
36
37#include <stdbool.h>
38#include <adt/list.h>
39#include <adt/hash_table.h>
40#include <ipc/devman.h>
41#include <ipc/loc.h>
42#include <fibril_synch.h>
43#include <atomic.h>
44#include <async.h>
45
46#include "util.h"
47
48#define NAME "devman"
49
50#define MATCH_EXT ".ma"
51
52#define LOC_DEVICE_NAMESPACE "devices"
53#define LOC_SEPARATOR '\\'
54
55struct dev_node;
56typedef struct dev_node dev_node_t;
57
58struct fun_node;
59typedef struct fun_node fun_node_t;
60
61typedef struct {
62 fibril_mutex_t mutex;
63 struct driver *driver;
64} client_t;
65
66typedef enum {
67 /** Driver has not been started. */
68 DRIVER_NOT_STARTED = 0,
69
70 /**
71 * Driver has been started, but has not registered as running and ready
72 * to receive requests.
73 */
74 DRIVER_STARTING,
75
76 /** Driver is running and prepared to serve incomming requests. */
77 DRIVER_RUNNING
78} driver_state_t;
79
80/** Representation of device driver. */
81typedef struct driver {
82 /** Pointers to previous and next drivers in a linked list. */
83 link_t drivers;
84
85 /**
86 * Specifies whether the driver has been started and wheter is running
87 * and prepared to receive requests.
88 */
89 int state;
90
91 /** Session asociated with this driver. */
92 async_sess_t *sess;
93 /** Name of the device driver. */
94 char *name;
95 /** Path to the driver's binary. */
96 const char *binary_path;
97 /** List of device ids for device-to-driver matching. */
98 match_id_list_t match_ids;
99 /** List of devices controlled by this driver. */
100 list_t devices;
101
102 /**
103 * Fibril mutex for this driver - driver state, list of devices, session.
104 */
105 fibril_mutex_t driver_mutex;
106} driver_t;
107
108/** The list of drivers. */
109typedef struct driver_list {
110 /** List of drivers */
111 list_t drivers;
112 /** Fibril mutex for list of drivers. */
113 fibril_mutex_t drivers_mutex;
114} driver_list_t;
115
116/** Device state */
117typedef enum {
118 DEVICE_NOT_INITIALIZED = 0,
119 DEVICE_USABLE,
120 DEVICE_NOT_PRESENT,
121 DEVICE_INVALID,
122 /** Device node has been removed from the tree */
123 DEVICE_REMOVED
124} device_state_t;
125
126/** Device node in the device tree. */
127struct dev_node {
128 /** Reference count */
129 atomic_t refcnt;
130
131 /** The global unique identifier of the device. */
132 devman_handle_t handle;
133
134 /** (Parent) function the device is attached to. */
135 fun_node_t *pfun;
136
137 /** List of device functions. */
138 list_t functions;
139 /** Driver of this device. */
140 driver_t *drv;
141 /** The state of the device. */
142 device_state_t state;
143 /** Link to list of devices owned by driver (driver_t.devices) */
144 link_t driver_devices;
145
146 /**
147 * Used by the hash table of devices indexed by devman device handles.
148 */
149 ht_link_t devman_dev;
150
151 /**
152 * Whether this device was already passed to the driver.
153 */
154 bool passed_to_driver;
155};
156
157/** Function state */
158typedef enum {
159 FUN_INIT = 0,
160 FUN_OFF_LINE,
161 FUN_ON_LINE,
162 /** Function node has been removed from the tree */
163 FUN_REMOVED
164} fun_state_t;
165
166/** Function node in the device tree. */
167struct fun_node {
168 /** Reference count */
169 atomic_t refcnt;
170 /** State */
171 fun_state_t state;
172 /** Locked while performing reconfiguration operations */
173 fibril_mutex_t busy_lock;
174
175 /** The global unique identifier of the function */
176 devman_handle_t handle;
177 /** Name of the function, assigned by the device driver */
178 char *name;
179 /** Function type */
180 fun_type_t ftype;
181
182 /** Full path and name of the device in device hierarchy */
183 char *pathname;
184
185 /** Device which this function belongs to */
186 dev_node_t *dev;
187
188 /** Link to list of functions in the device (ddf_dev_t.functions) */
189 link_t dev_functions;
190
191 /** Child device node (if any attached). */
192 dev_node_t *child;
193 /** List of device ids for device-to-driver matching. */
194 match_id_list_t match_ids;
195
196 /** Service ID if the device function is registered with loc. */
197 service_id_t service_id;
198
199 /**
200 * Used by the hash table of functions indexed by devman device handles.
201 */
202 ht_link_t devman_fun;
203
204 /**
205 * Used by the hash table of functions indexed by service IDs.
206 */
207 ht_link_t loc_fun;
208};
209
210/** Represents device tree. */
211typedef struct dev_tree {
212 /** Root device node. */
213 fun_node_t *root_node;
214
215 /**
216 * The next available handle - handles are assigned in a sequential
217 * manner.
218 */
219 devman_handle_t current_handle;
220
221 /** Synchronize access to the device tree. */
222 fibril_rwlock_t rwlock;
223
224 /** Hash table of all devices indexed by devman handles. */
225 hash_table_t devman_devices;
226
227 /** Hash table of all devices indexed by devman handles. */
228 hash_table_t devman_functions;
229
230 /**
231 * Hash table of services registered with location service, indexed by
232 * service IDs.
233 */
234 hash_table_t loc_functions;
235} dev_tree_t;
236
237/* Match ids and scores */
238
239extern int get_match_score(driver_t *, dev_node_t *);
240
241extern bool parse_match_ids(char *, match_id_list_t *);
242extern bool read_match_ids(const char *, match_id_list_t *);
243extern char *read_match_id(char **);
244extern char *read_id(const char **);
245
246/* Function nodes */
247
248extern fun_node_t *create_fun_node(void);
249extern void delete_fun_node(fun_node_t *);
250extern void fun_add_ref(fun_node_t *);
251extern void fun_del_ref(fun_node_t *);
252extern void fun_busy_lock(fun_node_t *);
253extern void fun_busy_unlock(fun_node_t *);
254extern fun_node_t *find_fun_node_no_lock(dev_tree_t *tree,
255 devman_handle_t handle);
256extern fun_node_t *find_fun_node(dev_tree_t *tree, devman_handle_t handle);
257extern fun_node_t *find_fun_node_by_path(dev_tree_t *, char *);
258extern fun_node_t *find_fun_node_in_device(dev_tree_t *tree, dev_node_t *,
259 const char *);
260
261/* Device tree */
262
263extern bool init_device_tree(dev_tree_t *, driver_list_t *);
264extern bool create_root_nodes(dev_tree_t *);
265extern bool insert_dev_node(dev_tree_t *, dev_node_t *, fun_node_t *);
266extern void remove_dev_node(dev_tree_t *, dev_node_t *);
267extern bool insert_fun_node(dev_tree_t *, fun_node_t *, char *, dev_node_t *);
268extern void remove_fun_node(dev_tree_t *, fun_node_t *);
269
270/* Loc services */
271
272extern void loc_register_tree_function(fun_node_t *, dev_tree_t *);
273
274extern fun_node_t *find_loc_tree_function(dev_tree_t *, service_id_t);
275
276extern void tree_add_loc_function(dev_tree_t *, fun_node_t *);
277
278#endif
279
280/** @}
281 */
Note: See TracBrowser for help on using the repository browser.