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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since ffa2c8ef was ffa2c8ef, checked in by Martin Decky <martin@…>, 14 years ago

do not intermix low-level IPC methods with async framework methods

  • Property mode set to 100644
File size: 9.6 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 <str.h>
40#include <adt/list.h>
41#include <adt/hash_table.h>
42#include <ipc/devman.h>
43#include <ipc/devmap.h>
44#include <fibril_synch.h>
45#include <atomic.h>
46
47#include "util.h"
48
49#define NAME "devman"
50
51#define MATCH_EXT ".ma"
52#define DEVICE_BUCKETS 256
53
54#define DEVMAP_CLASS_NAMESPACE "class"
55#define DEVMAP_DEVICE_NAMESPACE "devices"
56#define DEVMAP_SEPARATOR '\\'
57
58struct node;
59typedef struct node node_t;
60
61typedef enum {
62 /** Driver has not been started. */
63 DRIVER_NOT_STARTED = 0,
64
65 /**
66 * Driver has been started, but has not registered as running and ready
67 * to receive requests.
68 */
69 DRIVER_STARTING,
70
71 /** Driver is running and prepared to serve incomming requests. */
72 DRIVER_RUNNING
73} driver_state_t;
74
75/** Representation of device driver. */
76typedef struct driver {
77 /** Pointers to previous and next drivers in a linked list. */
78 link_t drivers;
79
80 /**
81 * Specifies whether the driver has been started and wheter is running
82 * and prepared to receive requests.
83 */
84 int state;
85
86 /** Phone asociated with this driver. */
87 sysarg_t phone;
88 /** Name of the device driver. */
89 char *name;
90 /** Path to the driver's binary. */
91 const char *binary_path;
92 /** List of device ids for device-to-driver matching. */
93 match_id_list_t match_ids;
94 /** Pointer to the linked list of devices controlled by this driver. */
95 link_t devices;
96
97 /**
98 * Fibril mutex for this driver - driver state, list of devices, phone.
99 */
100 fibril_mutex_t driver_mutex;
101} driver_t;
102
103/** The list of drivers. */
104typedef struct driver_list {
105 /** List of drivers */
106 link_t drivers;
107 /** Fibril mutex for list of drivers. */
108 fibril_mutex_t drivers_mutex;
109} driver_list_t;
110
111/** The state of the device. */
112typedef enum {
113 DEVICE_NOT_INITIALIZED = 0,
114 DEVICE_USABLE,
115 DEVICE_NOT_PRESENT,
116 DEVICE_INVALID
117} device_state_t;
118
119/** Representation of a node in the device tree. */
120struct node {
121 /** The global unique identifier of the device. */
122 devman_handle_t handle;
123 /** The name of the device specified by its parent. */
124 char *name;
125
126 /**
127 * Full path and name of the device in device hierarchi (i. e. in full
128 * path in device tree).
129 */
130 char *pathname;
131
132 /** The node of the parent device. */
133 node_t *parent;
134
135 /**
136 * Pointers to previous and next child devices in the linked list of
137 * parent device's node.
138 */
139 link_t sibling;
140
141 /** List of child device nodes. */
142 link_t children;
143 /** List of device ids for device-to-driver matching. */
144 match_id_list_t match_ids;
145 /** Driver of this device. */
146 driver_t *drv;
147 /** The state of the device. */
148 device_state_t state;
149 /**
150 * Pointer to the previous and next device in the list of devices
151 * owned by one driver.
152 */
153 link_t driver_devices;
154
155 /** The list of device classes to which this device belongs. */
156 link_t classes;
157 /** Devmap handle if the device is registered by devmapper. */
158 devmap_handle_t devmap_handle;
159
160 /**
161 * Used by the hash table of devices indexed by devman device handles.
162 */
163 link_t devman_link;
164
165 /**
166 * Used by the hash table of devices indexed by devmap device handles.
167 */
168 link_t devmap_link;
169
170 /**
171 * Whether this device was already passed to the driver.
172 */
173 bool passed_to_driver;
174};
175
176/** Represents device tree. */
177typedef struct dev_tree {
178 /** Root device node. */
179 node_t *root_node;
180
181 /**
182 * The next available handle - handles are assigned in a sequential
183 * manner.
184 */
185 devman_handle_t current_handle;
186
187 /** Synchronize access to the device tree. */
188 fibril_rwlock_t rwlock;
189
190 /** Hash table of all devices indexed by devman handles. */
191 hash_table_t devman_devices;
192
193 /**
194 * Hash table of devices registered by devmapper, indexed by devmap
195 * handles.
196 */
197 hash_table_t devmap_devices;
198} dev_tree_t;
199
200typedef struct dev_class {
201 /** The name of the class. */
202 const char *name;
203
204 /**
205 * Pointer to the previous and next class in the list of registered
206 * classes.
207 */
208 link_t link;
209
210 /**
211 * List of dev_class_info structures - one for each device registered by
212 * this class.
213 */
214 link_t devices;
215
216 /**
217 * Default base name for the device within the class, might be overrided
218 * by the driver.
219 */
220 const char *base_dev_name;
221
222 /** Unique numerical identifier of the newly added device. */
223 size_t curr_dev_idx;
224 /** Synchronize access to the list of devices in this class. */
225 fibril_mutex_t mutex;
226} dev_class_t;
227
228/**
229 * Provides n-to-m mapping between device nodes and classes - each device may
230 * be register to the arbitrary number of classes and each class may contain
231 * the arbitrary number of devices.
232 */
233typedef struct dev_class_info {
234 /** The class. */
235 dev_class_t *dev_class;
236 /** The device. */
237 node_t *dev;
238
239 /**
240 * Pointer to the previous and next class info in the list of devices
241 * registered by the class.
242 */
243 link_t link;
244
245 /**
246 * Pointer to the previous and next class info in the list of classes
247 * by which the device is registered.
248 */
249 link_t dev_classes;
250
251 /** The name of the device within the class. */
252 char *dev_name;
253 /** The handle of the device by device mapper in the class namespace. */
254 devmap_handle_t devmap_handle;
255
256 /**
257 * Link in the hash table of devices registered by the devmapper using
258 * their class names.
259 */
260 link_t devmap_link;
261} dev_class_info_t;
262
263/** The list of device classes. */
264typedef struct class_list {
265 /** List of classes. */
266 link_t classes;
267
268 /**
269 * Hash table of devices registered by devmapper using their class name,
270 * indexed by devmap handles.
271 */
272 hash_table_t devmap_devices;
273
274 /** Fibril mutex for list of classes. */
275 fibril_rwlock_t rwlock;
276} class_list_t;
277
278/* Match ids and scores */
279
280extern int get_match_score(driver_t *, node_t *);
281
282extern bool parse_match_ids(char *, match_id_list_t *);
283extern bool read_match_ids(const char *, match_id_list_t *);
284extern char *read_match_id(char **);
285extern char *read_id(const char **);
286
287/* Drivers */
288
289extern void init_driver_list(driver_list_t *);
290extern driver_t *create_driver(void);
291extern bool get_driver_info(const char *, const char *, driver_t *);
292extern int lookup_available_drivers(driver_list_t *, const char *);
293
294extern driver_t *find_best_match_driver(driver_list_t *, node_t *);
295extern bool assign_driver(node_t *, driver_list_t *, dev_tree_t *);
296
297extern void add_driver(driver_list_t *, driver_t *);
298extern void attach_driver(node_t *, driver_t *);
299extern void add_device(int, driver_t *, node_t *, dev_tree_t *);
300extern bool start_driver(driver_t *);
301
302extern driver_t *find_driver(driver_list_t *, const char *);
303extern void set_driver_phone(driver_t *, sysarg_t);
304extern void initialize_running_driver(driver_t *, dev_tree_t *);
305
306extern void init_driver(driver_t *);
307extern void clean_driver(driver_t *);
308extern void delete_driver(driver_t *);
309
310/* Device nodes */
311
312extern node_t *create_dev_node(void);
313extern void delete_dev_node(node_t *node);
314extern node_t *find_dev_node_no_lock(dev_tree_t *tree,
315 devman_handle_t handle);
316extern node_t *find_dev_node(dev_tree_t *tree, devman_handle_t handle);
317extern node_t *find_dev_node_by_path(dev_tree_t *, char *);
318extern node_t *find_node_child(node_t *, const char *);
319
320/* Device tree */
321
322extern bool init_device_tree(dev_tree_t *, driver_list_t *);
323extern bool create_root_node(dev_tree_t *);
324extern bool insert_dev_node(dev_tree_t *, node_t *, char *, node_t *);
325
326/* Device classes */
327
328extern dev_class_t *create_dev_class(void);
329extern dev_class_info_t *create_dev_class_info(void);
330extern size_t get_new_class_dev_idx(dev_class_t *);
331extern char *create_dev_name_for_class(dev_class_t *, const char *);
332extern dev_class_info_t *add_device_to_class(node_t *, dev_class_t *,
333 const char *);
334
335extern void init_class_list(class_list_t *);
336
337extern dev_class_t *get_dev_class(class_list_t *, char *);
338extern dev_class_t *find_dev_class_no_lock(class_list_t *, const char *);
339extern void add_dev_class_no_lock(class_list_t *, dev_class_t *);
340
341/* Devmap devices */
342
343extern node_t *find_devmap_tree_device(dev_tree_t *, devmap_handle_t);
344extern node_t *find_devmap_class_device(class_list_t *, devmap_handle_t);
345
346extern void class_add_devmap_device(class_list_t *, dev_class_info_t *);
347extern void tree_add_devmap_device(dev_tree_t *, node_t *);
348
349#endif
350
351/** @}
352 */
Note: See TracBrowser for help on using the repository browser.