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

Last change on this file was c3d9aaf5, checked in by Jiri Svoboda <jiri@…>, 5 months ago

Determine when device (sub)tree is stable.

Devman will only return value when the entire device tree is stable.

  • Property mode set to 100644
File size: 5.9 KB
Line 
1/*
2 * Copyright (c) 2025 Jiri Svoboda
3 * Copyright (c) 2010 Lenka Trochtova
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 <refcount.h>
44#include <async.h>
45
46#include "util.h"
47
48#define NAME "devman"
49
50#define LOC_DEVICE_NAMESPACE "devices"
51#define LOC_SEPARATOR '\\'
52
53struct dev_node;
54typedef struct dev_node dev_node_t;
55
56struct fun_node;
57typedef struct fun_node fun_node_t;
58
59typedef struct {
60 fibril_mutex_t mutex;
61 struct driver *driver;
62} client_t;
63
64/** Representation of device driver. */
65typedef struct driver {
66 /** Pointers to previous and next drivers in a linked list. */
67 link_t drivers;
68 /** Handle */
69 devman_handle_t handle;
70
71 /**
72 * Specifies whether the driver has been started and wheter is running
73 * and prepared to receive requests.
74 */
75 driver_state_t state;
76
77 /** Session asociated with this driver. */
78 async_sess_t *sess;
79 /** Name of the device driver. */
80 char *name;
81 /** Path to the driver's binary. */
82 char *binary_path;
83 /** List of device ids for device-to-driver matching. */
84 match_id_list_t match_ids;
85 /** List of devices controlled by this driver. */
86 list_t devices;
87
88 /**
89 * Fibril mutex for this driver - driver state, list of devices, session.
90 */
91 fibril_mutex_t driver_mutex;
92} driver_t;
93
94/** The list of drivers. */
95typedef struct driver_list {
96 /** List of drivers */
97 list_t drivers;
98 /** Fibril mutex for list of drivers. */
99 fibril_mutex_t drivers_mutex;
100 /** Next free handle */
101 devman_handle_t next_handle;
102} driver_list_t;
103
104/** Device state */
105typedef enum {
106 DEVICE_NOT_INITIALIZED = 0,
107 DEVICE_ATTACHING,
108 DEVICE_USABLE,
109 DEVICE_NOT_PRESENT,
110 DEVICE_INVALID,
111 /** Device node has been removed from the tree */
112 DEVICE_REMOVED
113} device_state_t;
114
115/** Device node in the device tree. */
116struct dev_node {
117 /** Reference count */
118 atomic_refcount_t refcnt;
119
120 /** The global unique identifier of the device. */
121 devman_handle_t handle;
122
123 /** (Parent) function the device is attached to. */
124 fun_node_t *pfun;
125
126 /** List of device functions. */
127 list_t functions;
128 /** Driver of this device. */
129 driver_t *drv;
130 /** The state of the device. */
131 device_state_t state;
132 /** Protect device state field */
133 fibril_mutex_t state_lock;
134 /** Signalled when device state changes */
135 fibril_condvar_t state_cv;
136 /** Link to list of devices owned by driver (driver_t.devices) */
137 link_t driver_devices;
138
139 /**
140 * Used by the hash table of devices indexed by devman device handles.
141 */
142 ht_link_t devman_dev;
143
144 /**
145 * Whether this device was already passed to the driver.
146 */
147 bool passed_to_driver;
148};
149
150/** Function state */
151typedef enum {
152 FUN_INIT = 0,
153 FUN_OFF_LINE,
154 FUN_ON_LINE,
155 /** Function node has been removed from the tree */
156 FUN_REMOVED
157} fun_state_t;
158
159/** Function node in the device tree. */
160struct fun_node {
161 /** Reference count */
162 atomic_refcount_t refcnt;
163 /** State */
164 fun_state_t state;
165 /** Locked while performing reconfiguration operations */
166 fibril_mutex_t busy_lock;
167
168 /** The global unique identifier of the function */
169 devman_handle_t handle;
170 /** Name of the function, assigned by the device driver */
171 char *name;
172 /** Function type */
173 fun_type_t ftype;
174
175 /** Full path and name of the device in device hierarchy */
176 char *pathname;
177
178 /** Device which this function belongs to */
179 dev_node_t *dev;
180
181 /** Link to list of functions in the device (ddf_dev_t.functions) */
182 link_t dev_functions;
183
184 /** Child device node (if any attached). */
185 dev_node_t *child;
186 /** List of device ids for device-to-driver matching. */
187 match_id_list_t match_ids;
188
189 /** Service ID if the device function is registered with loc. */
190 service_id_t service_id;
191
192 /**
193 * Used by the hash table of functions indexed by devman device handles.
194 */
195 ht_link_t devman_fun;
196
197 /**
198 * Used by the hash table of functions indexed by service IDs.
199 */
200 ht_link_t loc_fun;
201};
202
203/** Represents device tree. */
204typedef struct dev_tree {
205 /** Root device node. */
206 fun_node_t *root_node;
207
208 /**
209 * The next available handle - handles are assigned in a sequential
210 * manner.
211 */
212 devman_handle_t current_handle;
213
214 /** Synchronize access to the device tree. */
215 fibril_rwlock_t rwlock;
216
217 /** Hash table of all devices indexed by devman handles. */
218 hash_table_t devman_devices;
219
220 /** Hash table of all devices indexed by devman handles. */
221 hash_table_t devman_functions;
222
223 /**
224 * Hash table of services registered with location service, indexed by
225 * service IDs.
226 */
227 hash_table_t loc_functions;
228} dev_tree_t;
229
230#endif
231
232/** @}
233 */
Note: See TracBrowser for help on using the repository browser.