source: mainline/uspace/lib/libdrv/include/driver.h@ 3a5909f

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

parts of pci enumeration

  • Property mode set to 100644
File size: 4.6 KB
RevLine 
[c16cf62]1/*
[a1769ee]2 * Copyright (c) 2010 Lenka Trochtova
[c16cf62]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
[084ff99]37
38#include <adt/list.h>
[bda60d9]39#include <ipc/devman.h>
[a1769ee]40#include <ipc/dev_iface.h>
[52b7b1bb]41#include <assert.h>
[084ff99]42
[a1769ee]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 {
[52b7b1bb]53 size_t method_count;
[a1769ee]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
[52b7b1bb]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
[a1769ee]75// device
76
[52b7b1bb]77/** The device. */
[a1769ee]78struct device {
[52b7b1bb]79 /** Globally unique device identifier (assigned to the device by the device manager). */
[bda60d9]80 device_handle_t handle;
[5e598e0]81 /** The phone to the parent device driver (if it is different from this driver).*/
[9a66bc2e]82 int parent_phone;
[5e598e0]83 /** Parent device if handled by this driver, NULL otherwise. */
84 device_t *parent;
[52b7b1bb]85 /** The device's name.*/
[bda60d9]86 const char *name;
[52b7b1bb]87 /** The list of device ids for device-to-driver matching.*/
[bda60d9]88 match_id_list_t match_ids;
[52b7b1bb]89 /** The device driver's data associated with this device.*/
[eff1a590]90 void *driver_data;
[52b7b1bb]91 /** The table of interfaces exported by this device. */
[a1769ee]92 void *interfaces[DEV_IFACE_COUNT];
[52b7b1bb]93 /** Pointer to the previous and next device in the list of devices handled by the driver */
[084ff99]94 link_t link;
[a1769ee]95};
[c16cf62]96
[a1769ee]97
98// driver
99
[52b7b1bb]100/** Generic device driver operations. */
[a1769ee]101typedef struct driver_ops {
[52b7b1bb]102 /** Callback method for passing a new device to the device driver.*/
[c16cf62]103 bool (*add_device)(device_t *dev);
104 // TODO add other generic driver operations
105} driver_ops_t;
106
[52b7b1bb]107/** The driver structure.*/
[c16cf62]108typedef struct driver {
[52b7b1bb]109 /** The name of the device driver. */
[c16cf62]110 const char *name;
[52b7b1bb]111 /** Generic device driver operations. */
[c16cf62]112 driver_ops_t *driver_ops;
113} driver_t;
114
[a1769ee]115
116
117
118
[c16cf62]119int driver_main(driver_t *drv);
120
[52b7b1bb]121/** Create new device structure.
122 *
123 * @return the device structure.
124 */
[7707954]125static inline device_t * create_device()
126{
127 device_t *dev = malloc(sizeof(device_t));
128 if (NULL != dev) {
129 memset(dev, 0, sizeof(device_t));
[a1769ee]130 }
[bda60d9]131 list_initialize(&dev->match_ids.ids);
[7707954]132 return dev;
133}
134
[52b7b1bb]135/** Delete device structure.
136 *
137 * @param dev the device structure.
138 */
[a1769ee]139static inline void delete_device(device_t *dev)
140{
[bda60d9]141 clean_match_ids(&dev->match_ids);
142 if (NULL != dev->name) {
143 free(dev->name);
144 }
145 free(dev);
146}
[7707954]147
[a1769ee]148static inline void device_set_iface (device_t *dev, dev_inferface_id_t id, void *iface)
149{
150 assert(is_valid_iface_id(id));
151
152 int idx = get_iface_index(id);
153 dev->interfaces[idx] = iface;
154}
155
[52b7b1bb]156static inline void * device_get_iface(device_t *dev, dev_inferface_id_t id)
157{
158 assert(is_valid_iface_id(id));
159
160 int idx = get_iface_index(id);
161 return dev->interfaces[idx];
162}
163
[bda60d9]164bool child_device_register(device_t *child, device_t *parent);
[7707954]165
166
[c16cf62]167#endif
168
169/**
170 * @}
[a1769ee]171 */
Note: See TracBrowser for help on using the repository browser.