source: mainline/uspace/lib/libdrv/include/driver.h@ 5cd136ab

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

device interfaces - parts of code

  • Property mode set to 100644
File size: 4.5 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 libdrv
30 * @{
31 */
32/** @file
33 */
34#ifndef LIBDRV_DRIVER_H_
35#define LIBDRV_DRIVER_H_
36
37
38#include <adt/list.h>
39#include <ipc/devman.h>
40#include <ipc/dev_iface.h>
41#include <assert.h>
42
43struct device;
44typedef struct device device_t;
45
46// device interface
47
48// first two parameters: device and interface structure registered by the devices driver
49typedef void remote_iface_func_t(device_t*, void *, ipc_callid_t, ipc_call_t *);
50typedef remote_iface_func_t *remote_iface_func_ptr_t;
51
52typedef struct {
53 size_t method_count;
54 remote_iface_func_ptr_t *methods;
55} remote_iface_t;
56
57typedef struct {
58 remote_iface_t * ifaces[DEV_IFACE_COUNT];
59} iface_dipatch_table_t;
60
61static inline int get_iface_index(dev_inferface_id_t id)
62{
63 return id - DEV_IFACE_FIRST;
64}
65
66static inline bool is_valid_iface_id(dev_inferface_id_t id)
67{
68 return DEV_IFACE_FIRST <= id && id < DEV_IFACE_MAX;
69}
70
71remote_iface_t* get_remote_iface(dev_inferface_id_t id);
72remote_iface_func_ptr_t get_remote_method(remote_iface_t *rem_iface, ipcarg_t iface_method_idx);
73
74
75// device
76
77/** The device. */
78struct device {
79 /** Globally unique device identifier (assigned to the device by the device manager). */
80 device_handle_t handle;
81 /** The phone to the parent device driver.*/
82 ipcarg_t parent_phone;
83 /** The device's name.*/
84 const char *name;
85 /** The list of device ids for device-to-driver matching.*/
86 match_id_list_t match_ids;
87 /** The device driver's data associated with this device.*/
88 void *driver_data;
89 /** The table of interfaces exported by this device. */
90 void *interfaces[DEV_IFACE_COUNT];
91 /** Pointer to the previous and next device in the list of devices handled by the driver */
92 link_t link;
93};
94
95
96// driver
97
98/** Generic device driver operations. */
99typedef struct driver_ops {
100 /** Callback method for passing a new device to the device driver.*/
101 bool (*add_device)(device_t *dev);
102 // TODO add other generic driver operations
103} driver_ops_t;
104
105/** The driver structure.*/
106typedef struct driver {
107 /** The name of the device driver. */
108 const char *name;
109 /** Generic device driver operations. */
110 driver_ops_t *driver_ops;
111} driver_t;
112
113
114
115
116
117int driver_main(driver_t *drv);
118
119/** Create new device structure.
120 *
121 * @return the device structure.
122 */
123static inline device_t * create_device()
124{
125 device_t *dev = malloc(sizeof(device_t));
126 if (NULL != dev) {
127 memset(dev, 0, sizeof(device_t));
128 }
129 list_initialize(&dev->match_ids.ids);
130 return dev;
131}
132
133/** Delete device structure.
134 *
135 * @param dev the device structure.
136 */
137static inline void delete_device(device_t *dev)
138{
139 clean_match_ids(&dev->match_ids);
140 if (NULL != dev->name) {
141 free(dev->name);
142 }
143 free(dev);
144}
145
146static inline void device_set_iface (device_t *dev, dev_inferface_id_t id, void *iface)
147{
148 assert(is_valid_iface_id(id));
149
150 int idx = get_iface_index(id);
151 dev->interfaces[idx] = iface;
152}
153
154static inline void * device_get_iface(device_t *dev, dev_inferface_id_t id)
155{
156 assert(is_valid_iface_id(id));
157
158 int idx = get_iface_index(id);
159 return dev->interfaces[idx];
160}
161
162bool child_device_register(device_t *child, device_t *parent);
163
164
165#endif
166
167/**
168 * @}
169 */
Note: See TracBrowser for help on using the repository browser.