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