source: mainline/uspace/srv/devman/devman.h@ 5f6e25e

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

DDF support for function offlining and onlining. This allows
(anticipated) hot removal — support needs to be added in individual
drivers, currently there is support in test1 and partially in rootvirt.
Surprise removal is not supported. TODO synchronization.

  • Property mode set to 100644
File size: 8.3 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 <assert.h>
38#include <bool.h>
39#include <dirent.h>
40#include <str.h>
41#include <adt/list.h>
42#include <adt/hash_table.h>
43#include <ipc/devman.h>
44#include <ipc/loc.h>
45#include <fibril_synch.h>
46#include <atomic.h>
47#include <async.h>
48
49#include "util.h"
50
51#define NAME "devman"
52
53#define MATCH_EXT ".ma"
54#define DEVICE_BUCKETS 256
55
56#define LOC_DEVICE_NAMESPACE "devices"
57#define LOC_SEPARATOR '\\'
58
59struct dev_node;
60typedef struct dev_node dev_node_t;
61
62struct fun_node;
63typedef struct fun_node fun_node_t;
64
65typedef struct {
66 fibril_mutex_t mutex;
67 struct driver *driver;
68} client_t;
69
70typedef enum {
71 /** Driver has not been started. */
72 DRIVER_NOT_STARTED = 0,
73
74 /**
75 * Driver has been started, but has not registered as running and ready
76 * to receive requests.
77 */
78 DRIVER_STARTING,
79
80 /** Driver is running and prepared to serve incomming requests. */
81 DRIVER_RUNNING
82} driver_state_t;
83
84/** Representation of device driver. */
85typedef struct driver {
86 /** Pointers to previous and next drivers in a linked list. */
87 link_t drivers;
88
89 /**
90 * Specifies whether the driver has been started and wheter is running
91 * and prepared to receive requests.
92 */
93 int state;
94
95 /** Session asociated with this driver. */
96 async_sess_t *sess;
97 /** Name of the device driver. */
98 char *name;
99 /** Path to the driver's binary. */
100 const char *binary_path;
101 /** List of device ids for device-to-driver matching. */
102 match_id_list_t match_ids;
103 /** List of devices controlled by this driver. */
104 list_t devices;
105
106 /**
107 * Fibril mutex for this driver - driver state, list of devices, session.
108 */
109 fibril_mutex_t driver_mutex;
110} driver_t;
111
112/** The list of drivers. */
113typedef struct driver_list {
114 /** List of drivers */
115 list_t drivers;
116 /** Fibril mutex for list of drivers. */
117 fibril_mutex_t drivers_mutex;
118} driver_list_t;
119
120/** The state of the device. */
121typedef enum {
122 DEVICE_NOT_INITIALIZED = 0,
123 DEVICE_USABLE,
124 DEVICE_NOT_PRESENT,
125 DEVICE_INVALID
126} device_state_t;
127
128/** Device node in the device tree. */
129struct dev_node {
130 /** The global unique identifier of the device. */
131 devman_handle_t handle;
132
133 /** (Parent) function the device is attached to. */
134 fun_node_t *pfun;
135
136 /** List of device functions. */
137 list_t functions;
138 /** Driver of this device. */
139 driver_t *drv;
140 /** The state of the device. */
141 device_state_t state;
142 /** Link to list of devices owned by driver (driver_t.devices) */
143 link_t driver_devices;
144
145 /**
146 * Used by the hash table of devices indexed by devman device handles.
147 */
148 link_t devman_dev;
149
150 /**
151 * Whether this device was already passed to the driver.
152 */
153 bool passed_to_driver;
154};
155
156/** Function node in the device tree. */
157struct fun_node {
158 /** The global unique identifier of the function */
159 devman_handle_t handle;
160 /** Name of the function, assigned by the device driver */
161 char *name;
162 /** Function type */
163 fun_type_t ftype;
164
165 /** Full path and name of the device in device hierarchy */
166 char *pathname;
167
168 /** Device which this function belongs to */
169 dev_node_t *dev;
170
171 /** Link to list of functions in the device (ddf_dev_t.functions) */
172 link_t dev_functions;
173
174 /** Child device node (if any attached). */
175 dev_node_t *child;
176 /** List of device ids for device-to-driver matching. */
177 match_id_list_t match_ids;
178
179 /** Service ID if the device function is registered with loc. */
180 service_id_t service_id;
181
182 /**
183 * Used by the hash table of functions indexed by devman device handles.
184 */
185 link_t devman_fun;
186
187 /**
188 * Used by the hash table of functions indexed by service IDs.
189 */
190 link_t loc_fun;
191};
192
193
194/** Represents device tree. */
195typedef struct dev_tree {
196 /** Root device node. */
197 fun_node_t *root_node;
198
199 /**
200 * The next available handle - handles are assigned in a sequential
201 * manner.
202 */
203 devman_handle_t current_handle;
204
205 /** Synchronize access to the device tree. */
206 fibril_rwlock_t rwlock;
207
208 /** Hash table of all devices indexed by devman handles. */
209 hash_table_t devman_devices;
210
211 /** Hash table of all devices indexed by devman handles. */
212 hash_table_t devman_functions;
213
214 /**
215 * Hash table of services registered with location service, indexed by
216 * service IDs.
217 */
218 hash_table_t loc_functions;
219} dev_tree_t;
220
221/* Match ids and scores */
222
223extern int get_match_score(driver_t *, dev_node_t *);
224
225extern bool parse_match_ids(char *, match_id_list_t *);
226extern bool read_match_ids(const char *, match_id_list_t *);
227extern char *read_match_id(char **);
228extern char *read_id(const char **);
229
230/* Drivers */
231
232extern void init_driver_list(driver_list_t *);
233extern driver_t *create_driver(void);
234extern bool get_driver_info(const char *, const char *, driver_t *);
235extern int lookup_available_drivers(driver_list_t *, const char *);
236
237extern driver_t *find_best_match_driver(driver_list_t *, dev_node_t *);
238extern bool assign_driver(dev_node_t *, driver_list_t *, dev_tree_t *);
239
240extern void add_driver(driver_list_t *, driver_t *);
241extern void attach_driver(dev_node_t *, driver_t *);
242extern void detach_driver(dev_node_t *);
243extern void add_device(driver_t *, dev_node_t *, dev_tree_t *);
244extern bool start_driver(driver_t *);
245extern int driver_dev_remove(dev_node_t *);
246extern int driver_fun_online(fun_node_t *);
247extern int driver_fun_offline(fun_node_t *);
248
249extern driver_t *find_driver(driver_list_t *, const char *);
250extern void initialize_running_driver(driver_t *, dev_tree_t *);
251
252extern void init_driver(driver_t *);
253extern void clean_driver(driver_t *);
254extern void delete_driver(driver_t *);
255
256/* Device nodes */
257
258extern dev_node_t *create_dev_node(void);
259extern void delete_dev_node(dev_node_t *node);
260extern dev_node_t *find_dev_node_no_lock(dev_tree_t *tree,
261 devman_handle_t handle);
262extern dev_node_t *find_dev_node(dev_tree_t *tree, devman_handle_t handle);
263extern dev_node_t *find_dev_function(dev_node_t *, const char *);
264extern int dev_get_functions(dev_tree_t *tree, dev_node_t *, devman_handle_t *,
265 size_t, size_t *);
266
267extern fun_node_t *create_fun_node(void);
268extern void delete_fun_node(fun_node_t *);
269extern fun_node_t *find_fun_node_no_lock(dev_tree_t *tree,
270 devman_handle_t handle);
271extern fun_node_t *find_fun_node(dev_tree_t *tree, devman_handle_t handle);
272extern fun_node_t *find_fun_node_by_path(dev_tree_t *, char *);
273extern fun_node_t *find_fun_node_in_device(dev_node_t *, const char *);
274
275/* Device tree */
276
277extern bool init_device_tree(dev_tree_t *, driver_list_t *);
278extern bool create_root_nodes(dev_tree_t *);
279extern bool insert_dev_node(dev_tree_t *, dev_node_t *, fun_node_t *);
280extern void remove_dev_node(dev_tree_t *, dev_node_t *);
281extern bool insert_fun_node(dev_tree_t *, fun_node_t *, char *, dev_node_t *);
282extern void remove_fun_node(dev_tree_t *, fun_node_t *);
283
284/* Loc services */
285
286extern void loc_register_tree_function(fun_node_t *, dev_tree_t *);
287
288extern fun_node_t *find_loc_tree_function(dev_tree_t *, service_id_t);
289
290extern void tree_add_loc_function(dev_tree_t *, fun_node_t *);
291
292#endif
293
294/** @}
295 */
Note: See TracBrowser for help on using the repository browser.