source: mainline/uspace/srv/devman/devman.h@ 79ae36dd

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

new async framework with integrated exchange tracking

  • strict isolation between low-level IPC and high-level async framework with integrated exchange tracking
    • each IPC connection is represented by an async_sess_t structure
    • each IPC exchange is represented by an async_exch_t structure
    • exchange management is either based on atomic messages (EXCHANGE_ATOMIC), locking (EXCHANGE_SERIALIZE) or connection cloning (EXCHANGE_CLONE)
  • async_obsolete: temporary compatibility layer to keep old async clients working (several pieces of code are currently broken, but only non-essential functionality)
  • IPC_M_PHONE_HANGUP is now method no. 0 (for elegant boolean evaluation)
  • IPC_M_DEBUG_ALL has been renamed to IPC_M_DEBUG
  • IPC_M_PING has been removed (VFS protocol now has VFS_IN_PING)
  • console routines in libc have been rewritten for better abstraction
  • additional use for libc-private header files (FILE structure opaque to the client)
  • various cstyle changes (typos, indentation, missing externs in header files, improved comments, etc.)
  • Property mode set to 100644
File size: 10.7 KB
RevLine 
[e2b9a993]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 */
[08d9c4e6]32
[e2b9a993]33#ifndef DEVMAN_H_
34#define DEVMAN_H_
35
36#include <assert.h>
37#include <bool.h>
38#include <dirent.h>
[c47e1a8]39#include <str.h>
[e2b9a993]40#include <adt/list.h>
[957cfa58]41#include <adt/hash_table.h>
[bda60d9]42#include <ipc/devman.h>
[d51ee2b]43#include <ipc/devmap.h>
[e85920d]44#include <fibril_synch.h>
[084ff99]45#include <atomic.h>
[79ae36dd]46#include <async.h>
[e2b9a993]47
[08d9c4e6]48#include "util.h"
[e2b9a993]49
50#define NAME "devman"
51
52#define MATCH_EXT ".ma"
[957cfa58]53#define DEVICE_BUCKETS 256
[e2b9a993]54
[ce89036b]55#define DEVMAP_CLASS_NAMESPACE "class"
56#define DEVMAP_DEVICE_NAMESPACE "devices"
[a32defa]57#define DEVMAP_SEPARATOR '\\'
[ce89036b]58
[ba38f72c]59struct dev_node;
60typedef struct dev_node dev_node_t;
61
62struct fun_node;
63typedef struct fun_node fun_node_t;
[e2b9a993]64
[e85920d]65typedef enum {
[38b3baf]66 /** Driver has not been started. */
[e85920d]67 DRIVER_NOT_STARTED = 0,
[38b3baf]68
69 /**
70 * Driver has been started, but has not registered as running and ready
71 * to receive requests.
72 */
[e85920d]73 DRIVER_STARTING,
[38b3baf]74
75 /** Driver is running and prepared to serve incomming requests. */
[e85920d]76 DRIVER_RUNNING
77} driver_state_t;
78
[38b3baf]79/** Representation of device driver. */
[e2b9a993]80typedef struct driver {
[38b3baf]81 /** Pointers to previous and next drivers in a linked list. */
[e2b9a993]82 link_t drivers;
[38b3baf]83
84 /**
85 * Specifies whether the driver has been started and wheter is running
86 * and prepared to receive requests.
87 */
[e85920d]88 int state;
[38b3baf]89
[79ae36dd]90 /** Session asociated with this driver. */
91 async_sess_t *sess;
[38b3baf]92 /** Name of the device driver. */
[e2b9a993]93 char *name;
[38b3baf]94 /** Path to the driver's binary. */
[e2b9a993]95 const char *binary_path;
[38b3baf]96 /** List of device ids for device-to-driver matching. */
[e2b9a993]97 match_id_list_t match_ids;
[38b3baf]98 /** Pointer to the linked list of devices controlled by this driver. */
[e2b9a993]99 link_t devices;
[38b3baf]100
101 /**
[79ae36dd]102 * Fibril mutex for this driver - driver state, list of devices, session.
[38b3baf]103 */
[e85920d]104 fibril_mutex_t driver_mutex;
[e2b9a993]105} driver_t;
106
[e85920d]107/** The list of drivers. */
108typedef struct driver_list {
109 /** List of drivers */
110 link_t drivers;
111 /** Fibril mutex for list of drivers. */
[38b3baf]112 fibril_mutex_t drivers_mutex;
[e85920d]113} driver_list_t;
114
[df747b9c]115/** The state of the device. */
116typedef enum {
117 DEVICE_NOT_INITIALIZED = 0,
118 DEVICE_USABLE,
119 DEVICE_NOT_PRESENT,
120 DEVICE_INVALID
121} device_state_t;
122
[ba38f72c]123/** Device node in the device tree. */
124struct dev_node {
[38b3baf]125 /** The global unique identifier of the device. */
[0b5a4131]126 devman_handle_t handle;
[ba38f72c]127
128 /** (Parent) function the device is attached to. */
129 fun_node_t *pfun;
130
131 /** List of device functions. */
132 link_t functions;
133 /** Driver of this device. */
134 driver_t *drv;
135 /** The state of the device. */
136 device_state_t state;
137 /** Link to list of devices owned by driver (driver_t.devices) */
138 link_t driver_devices;
[38b3baf]139
140 /**
[ba38f72c]141 * Used by the hash table of devices indexed by devman device handles.
[38b3baf]142 */
[ba38f72c]143 link_t devman_dev;
[38b3baf]144
145 /**
[ba38f72c]146 * Whether this device was already passed to the driver.
[38b3baf]147 */
[ba38f72c]148 bool passed_to_driver;
149};
150
151/** Function node in the device tree. */
152struct fun_node {
153 /** The global unique identifier of the function */
154 devman_handle_t handle;
155 /** Name of the function, assigned by the device driver */
156 char *name;
157
158 /** Full path and name of the device in device hierarchy */
159 char *pathname;
160
161 /** Device which this function belongs to */
162 dev_node_t *dev;
[38b3baf]163
[83a2f43]164 /** Link to list of functions in the device (ddf_dev_t.functions) */
[ba38f72c]165 link_t dev_functions;
166
167 /** Child device node (if any attached). */
168 dev_node_t *child;
[38b3baf]169 /** List of device ids for device-to-driver matching. */
[08d9c4e6]170 match_id_list_t match_ids;
[38b3baf]171
[ba38f72c]172 /** The list of device classes to which this device function belongs. */
[d51ee2b]173 link_t classes;
[ba38f72c]174 /** Devmap handle if the device function is registered by devmap. */
[991f645]175 devmap_handle_t devmap_handle;
[38b3baf]176
177 /**
[ba38f72c]178 * Used by the hash table of functions indexed by devman device handles.
[38b3baf]179 */
[ba38f72c]180 link_t devman_fun;
[38b3baf]181
182 /**
[ba38f72c]183 * Used by the hash table of functions indexed by devmap device handles.
[5bee897]184 */
[ba38f72c]185 link_t devmap_fun;
[e2b9a993]186};
187
[ba38f72c]188
[38b3baf]189/** Represents device tree. */
[e2b9a993]190typedef struct dev_tree {
191 /** Root device node. */
[ba38f72c]192 fun_node_t *root_node;
[38b3baf]193
194 /**
195 * The next available handle - handles are assigned in a sequential
196 * manner.
197 */
[0b5a4131]198 devman_handle_t current_handle;
[38b3baf]199
200 /** Synchronize access to the device tree. */
[957cfa58]201 fibril_rwlock_t rwlock;
[38b3baf]202
203 /** Hash table of all devices indexed by devman handles. */
[957cfa58]204 hash_table_t devman_devices;
[38b3baf]205
[ba38f72c]206 /** Hash table of all devices indexed by devman handles. */
207 hash_table_t devman_functions;
208
[38b3baf]209 /**
210 * Hash table of devices registered by devmapper, indexed by devmap
211 * handles.
212 */
[ba38f72c]213 hash_table_t devmap_functions;
[e2b9a993]214} dev_tree_t;
215
[38b3baf]216typedef struct dev_class {
217 /** The name of the class. */
[d51ee2b]218 const char *name;
[38b3baf]219
220 /**
221 * Pointer to the previous and next class in the list of registered
222 * classes.
223 */
224 link_t link;
225
226 /**
227 * List of dev_class_info structures - one for each device registered by
228 * this class.
229 */
[692c40cb]230 link_t devices;
[38b3baf]231
232 /**
233 * Default base name for the device within the class, might be overrided
234 * by the driver.
235 */
[d51ee2b]236 const char *base_dev_name;
[38b3baf]237
238 /** Unique numerical identifier of the newly added device. */
[d51ee2b]239 size_t curr_dev_idx;
240 /** Synchronize access to the list of devices in this class. */
241 fibril_mutex_t mutex;
242} dev_class_t;
243
[58b833c]244/**
[ba38f72c]245 * Provides n-to-m mapping between function nodes and classes - each function
246 * can register in an arbitrary number of classes and each class can contain
247 * an arbitrary number of device functions.
[38b3baf]248 */
[d51ee2b]249typedef struct dev_class_info {
[38b3baf]250 /** The class. */
[d51ee2b]251 dev_class_t *dev_class;
[38b3baf]252 /** The device. */
[ba38f72c]253 fun_node_t *fun;
[38b3baf]254
255 /**
256 * Pointer to the previous and next class info in the list of devices
257 * registered by the class.
258 */
[d51ee2b]259 link_t link;
[38b3baf]260
261 /**
262 * Pointer to the previous and next class info in the list of classes
263 * by which the device is registered.
264 */
[d51ee2b]265 link_t dev_classes;
[38b3baf]266
[ba38f72c]267 /** The name of the device function within the class. */
[38b3baf]268 char *dev_name;
269 /** The handle of the device by device mapper in the class namespace. */
[991f645]270 devmap_handle_t devmap_handle;
[38b3baf]271
272 /**
273 * Link in the hash table of devices registered by the devmapper using
274 * their class names.
275 */
[ce89036b]276 link_t devmap_link;
[d51ee2b]277} dev_class_info_t;
[e2b9a993]278
[692c40cb]279/** The list of device classes. */
280typedef struct class_list {
[38b3baf]281 /** List of classes. */
[692c40cb]282 link_t classes;
[38b3baf]283
284 /**
285 * Hash table of devices registered by devmapper using their class name,
286 * indexed by devmap handles.
287 */
[ba38f72c]288 hash_table_t devmap_functions;
[38b3baf]289
[692c40cb]290 /** Fibril mutex for list of classes. */
[38b3baf]291 fibril_rwlock_t rwlock;
[692c40cb]292} class_list_t;
293
[38b3baf]294/* Match ids and scores */
[e2b9a993]295
[ba38f72c]296extern int get_match_score(driver_t *, dev_node_t *);
[e2b9a993]297
[38b3baf]298extern bool parse_match_ids(char *, match_id_list_t *);
299extern bool read_match_ids(const char *, match_id_list_t *);
300extern char *read_match_id(char **);
301extern char *read_id(const char **);
[729fa2d6]302
[38b3baf]303/* Drivers */
[0c3666d]304
[791f58c]305extern void init_driver_list(driver_list_t *);
[38b3baf]306extern driver_t *create_driver(void);
307extern bool get_driver_info(const char *, const char *, driver_t *);
308extern int lookup_available_drivers(driver_list_t *, const char *);
[e2b9a993]309
[ba38f72c]310extern driver_t *find_best_match_driver(driver_list_t *, dev_node_t *);
311extern bool assign_driver(dev_node_t *, driver_list_t *, dev_tree_t *);
[e2b9a993]312
[38b3baf]313extern void add_driver(driver_list_t *, driver_t *);
[ba38f72c]314extern void attach_driver(dev_node_t *, driver_t *);
[79ae36dd]315extern void add_device(async_sess_t *, driver_t *, dev_node_t *, dev_tree_t *);
[38b3baf]316extern bool start_driver(driver_t *);
[e2b9a993]317
[38b3baf]318extern driver_t *find_driver(driver_list_t *, const char *);
[791f58c]319extern void initialize_running_driver(driver_t *, dev_tree_t *);
[e2b9a993]320
[791f58c]321extern void init_driver(driver_t *);
322extern void clean_driver(driver_t *);
323extern void delete_driver(driver_t *);
[38b3baf]324
325/* Device nodes */
326
[ba38f72c]327extern dev_node_t *create_dev_node(void);
328extern void delete_dev_node(dev_node_t *node);
329extern dev_node_t *find_dev_node_no_lock(dev_tree_t *tree,
330 devman_handle_t handle);
331extern dev_node_t *find_dev_node(dev_tree_t *tree, devman_handle_t handle);
332extern dev_node_t *find_dev_function(dev_node_t *, const char *);
333
334extern fun_node_t *create_fun_node(void);
335extern void delete_fun_node(fun_node_t *);
336extern fun_node_t *find_fun_node_no_lock(dev_tree_t *tree,
[0b5a4131]337 devman_handle_t handle);
[ba38f72c]338extern fun_node_t *find_fun_node(dev_tree_t *tree, devman_handle_t handle);
339extern fun_node_t *find_fun_node_by_path(dev_tree_t *, char *);
[0876062]340extern fun_node_t *find_fun_node_in_device(dev_node_t *, const char *);
[fc8c2b6]341extern fun_node_t *find_fun_node_by_class(class_list_t *, const char *, const char *);
[38b3baf]342
343/* Device tree */
[5cd136ab]344
[38b3baf]345extern bool init_device_tree(dev_tree_t *, driver_list_t *);
[ba38f72c]346extern bool create_root_nodes(dev_tree_t *);
347extern bool insert_dev_node(dev_tree_t *, dev_node_t *, fun_node_t *);
348extern bool insert_fun_node(dev_tree_t *, fun_node_t *, char *, dev_node_t *);
[e2b9a993]349
[38b3baf]350/* Device classes */
[d51ee2b]351
[791f58c]352extern dev_class_t *create_dev_class(void);
353extern dev_class_info_t *create_dev_class_info(void);
354extern size_t get_new_class_dev_idx(dev_class_t *);
[38b3baf]355extern char *create_dev_name_for_class(dev_class_t *, const char *);
[ba38f72c]356extern dev_class_info_t *add_function_to_class(fun_node_t *, dev_class_t *,
[38b3baf]357 const char *);
[d51ee2b]358
[38b3baf]359extern void init_class_list(class_list_t *);
[692c40cb]360
[38b3baf]361extern dev_class_t *get_dev_class(class_list_t *, char *);
362extern dev_class_t *find_dev_class_no_lock(class_list_t *, const char *);
[fc8c2b6]363extern dev_class_info_t *find_dev_in_class(dev_class_t *, const char *);
[791f58c]364extern void add_dev_class_no_lock(class_list_t *, dev_class_t *);
[ce89036b]365
[38b3baf]366/* Devmap devices */
[ce89036b]367
[8b1e15ac]368extern void devmap_register_tree_function(fun_node_t *, dev_tree_t *);
369
[ba38f72c]370extern fun_node_t *find_devmap_tree_function(dev_tree_t *, devmap_handle_t);
371extern fun_node_t *find_devmap_class_function(class_list_t *, devmap_handle_t);
[a32defa]372
[ba38f72c]373extern void class_add_devmap_function(class_list_t *, dev_class_info_t *);
374extern void tree_add_devmap_function(dev_tree_t *, fun_node_t *);
[a32defa]375
[e2b9a993]376#endif
[c16cf62]377
378/** @}
[38b3baf]379 */
Note: See TracBrowser for help on using the repository browser.