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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since a78fa2a was 5cd136ab, checked in by Lenka Trochtova <trochtova.lenka@…>, 15 years ago

device interfaces and driver cooperation - parts of code

  • Property mode set to 100644
File size: 7.6 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>
39#include <string.h>
40#include <adt/list.h>
41#include <ipc/ipc.h>
[bda60d9]42#include <ipc/devman.h>
[e85920d]43#include <fibril_synch.h>
[084ff99]44#include <atomic.h>
[e2b9a993]45
[08d9c4e6]46#include "util.h"
[e2b9a993]47
48#define NAME "devman"
49
50#define MATCH_EXT ".ma"
[bda60d9]51#define MAX_DEV 256
[e2b9a993]52
53struct node;
54
55typedef struct node node_t;
56
[e85920d]57typedef enum {
58 /** driver has not been started */
59 DRIVER_NOT_STARTED = 0,
60 /** driver has been started, but has not registered as running and ready to receive requests */
61 DRIVER_STARTING,
62 /** driver is running and prepared to serve incomming requests */
63 DRIVER_RUNNING
64} driver_state_t;
65
[e2b9a993]66/** Representation of device driver.
67 */
68typedef struct driver {
69 /** Pointers to previous and next drivers in a linked list */
70 link_t drivers;
[e85920d]71 /** Specifies whether the driver has been started and wheter is running and prepared to receive requests.*/
72 int state;
[e2b9a993]73 /** Phone asociated with this driver */
74 ipcarg_t phone;
75 /** Name of the device driver */
76 char *name;
77 /** Path to the driver's binary */
78 const char *binary_path;
79 /** List of device ids for device-to-driver matching.*/
80 match_id_list_t match_ids;
81 /** Pointer to the linked list of devices controlled by this driver */
82 link_t devices;
[e85920d]83 /** Fibril mutex for this driver - driver state, list of devices, phone.*/
84 fibril_mutex_t driver_mutex;
[e2b9a993]85} driver_t;
86
[e85920d]87/** The list of drivers. */
88typedef struct driver_list {
89 /** List of drivers */
90 link_t drivers;
91 /** Fibril mutex for list of drivers. */
92 fibril_mutex_t drivers_mutex;
93} driver_list_t;
94
[e2b9a993]95/** Representation of a node in the device tree.*/
96struct node {
[084ff99]97 /** The global unique identifier of the device.*/
[bda60d9]98 device_handle_t handle;
99 /** The name of the device specified by its parent. */
100 char *name;
101 /** Full path and name of the device in device hierarchi (i. e. in full path in device tree).*/
102 char *pathname;
[e2b9a993]103 /** The node of the parent device. */
104 node_t *parent;
105 /** Pointers to previous and next child devices in the linked list of parent device's node.*/
[08d9c4e6]106 link_t sibling;
[e2b9a993]107 /** List of child device nodes. */
108 link_t children;
[e85920d]109 /** Fibril mutex for the list of child device nodes of this node. */
110 fibril_mutex_t children_mutex;
[e2b9a993]111 /** List of device ids for device-to-driver matching.*/
[08d9c4e6]112 match_id_list_t match_ids;
[e2b9a993]113 /** Driver of this device.*/
[08d9c4e6]114 driver_t *drv;
[e2b9a993]115 /** Pointer to the previous and next device in the list of devices
116 owned by one driver */
[08d9c4e6]117 link_t driver_devices;
[e2b9a993]118};
119
120/** Represents device tree.
121 */
122typedef struct dev_tree {
123 /** Root device node. */
124 node_t *root_node;
[bda60d9]125 /** The next available handle - handles are assigned in a sequential manner.*/
[084ff99]126 atomic_t current_handle;
[bda60d9]127 /** Handle-to-node mapping. */
128 node_t * node_map[MAX_DEV];
[e2b9a993]129} dev_tree_t;
130
131
132
133// Match ids and scores
134
135int get_match_score(driver_t *drv, node_t *dev);
136
137bool parse_match_ids(const char *buf, match_id_list_t *ids);
138bool read_match_ids(const char *conf_path, match_id_list_t *ids);
139char * read_id(const char **buf) ;
[729fa2d6]140
141// Drivers
[0c3666d]142
[d347b53]143/**
144 * Initialize the list of device driver's.
145 *
146 * @param drv_list the list of device driver's.
147 *
148 */
[0c3666d]149static inline void init_driver_list(driver_list_t *drv_list)
150{
151 assert(NULL != drv_list);
152
153 list_initialize(&drv_list->drivers);
154 fibril_mutex_initialize(&drv_list->drivers_mutex);
155}
156
[e2b9a993]157driver_t * create_driver();
158bool get_driver_info(const char *base_path, const char *name, driver_t *drv);
[0c3666d]159int lookup_available_drivers(driver_list_t *drivers_list, const char *dir_path);
[e2b9a993]160
[0c3666d]161driver_t * find_best_match_driver(driver_list_t *drivers_list, node_t *node);
162bool assign_driver(node_t *node, driver_list_t *drivers_list);
[e2b9a993]163
[0c3666d]164void add_driver(driver_list_t *drivers_list, driver_t *drv);
[08d9c4e6]165void attach_driver(node_t *node, driver_t *drv);
[084ff99]166void add_device(int phone, driver_t *drv, node_t *node);
[e2b9a993]167bool start_driver(driver_t *drv);
168
[729fa2d6]169driver_t * find_driver(driver_list_t *drv_list, const char *drv_name);
[c16cf62]170void set_driver_phone(driver_t *driver, ipcarg_t phone);
171void initialize_running_driver(driver_t *driver);
[729fa2d6]172
[d347b53]173/**
174 * Initialize device driver structure.
175 *
176 * @param drv the device driver structure.
177 *
178 */
[08d9c4e6]179static inline void init_driver(driver_t *drv)
[e2b9a993]180{
[08d9c4e6]181 assert(drv != NULL);
182
183 memset(drv, 0, sizeof(driver_t));
[e2b9a993]184 list_initialize(&drv->match_ids.ids);
185 list_initialize(&drv->devices);
[924c75e1]186 fibril_mutex_initialize(&drv->driver_mutex);
[e2b9a993]187}
188
[d347b53]189/**
190 * Device driver structure clean-up.
191 *
192 * @param drv the device driver structure.
193 */
[08d9c4e6]194static inline void clean_driver(driver_t *drv)
[e2b9a993]195{
196 assert(drv != NULL);
[08d9c4e6]197
198 free_not_null(drv->name);
199 free_not_null(drv->binary_path);
200
[e2b9a993]201 clean_match_ids(&drv->match_ids);
[08d9c4e6]202
203 init_driver(drv);
[e2b9a993]204}
205
[d347b53]206/**
207 * Delete device driver structure.
208 *
209 * @param drv the device driver structure.*
210 */
[08d9c4e6]211static inline void delete_driver(driver_t *drv)
[e2b9a993]212{
[08d9c4e6]213 assert(NULL != drv);
214
[e2b9a993]215 clean_driver(drv);
216 free(drv);
217}
218
219// Device nodes
[d347b53]220/**
221 * Create a new device node.
222 *
223 * @return a device node structure.
224 *
225 */
[e2b9a993]226static inline node_t * create_dev_node()
227{
228 node_t *res = malloc(sizeof(node_t));
229 if (res != NULL) {
[08d9c4e6]230 memset(res, 0, sizeof(node_t));
[e2b9a993]231 }
[084ff99]232
233 list_initialize(&res->children);
234 list_initialize(&res->match_ids.ids);
[d347b53]235 fibril_mutex_initialize(&res->children_mutex);
[084ff99]236
[e2b9a993]237 return res;
238}
239
[5cd136ab]240/**
241 * Delete a device node.
242 *
243 * @param node a device node structure.
244 *
245 */
[d347b53]246static inline void delete_dev_node(node_t *node)
247{
248 assert(list_empty(&node->children) && NULL == node->parent && NULL == node->drv);
249
250 clean_match_ids(&node->match_ids);
251 free_not_null(node->name);
252 free_not_null(node->pathname);
253 free(node);
254}
255
256/**
257 * Find the device node structure of the device witch has the specified handle.
258 *
259 * @param tree the device tree where we look for the device node.
260 * @param handle the handle of the device.
261 * @return the device node.
262 */
[bda60d9]263static inline node_t * find_dev_node(dev_tree_t *tree, long handle)
[e2b9a993]264{
[bda60d9]265 if (handle < MAX_DEV) {
266 return tree->node_map[handle];
[e2b9a993]267 }
[bda60d9]268 return NULL;
[e2b9a993]269}
270
[5cd136ab]271node_t * find_dev_node_by_path(dev_tree_t *tree, char *path);
272node_t *find_node_child(node_t *parent, const char *name);
273
[e2b9a993]274// Device tree
275
[0c3666d]276bool init_device_tree(dev_tree_t *tree, driver_list_t *drivers_list);
[084ff99]277bool create_root_node(dev_tree_t *tree);
[5cd136ab]278bool insert_dev_node(dev_tree_t *tree, node_t *node, char *dev_name, node_t *parent);
[e2b9a993]279
280#endif
[c16cf62]281
282/** @}
283 */
Note: See TracBrowser for help on using the repository browser.