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 |
|
---|
42 | struct device;
|
---|
43 | typedef struct device device_t;
|
---|
44 |
|
---|
45 | // device interface
|
---|
46 |
|
---|
47 | // first two parameters: device and interface structure registered by the devices driver
|
---|
48 | typedef void remote_iface_func_t(device_t*, void *, ipc_callid_t, ipc_call_t *);
|
---|
49 | typedef remote_iface_func_t *remote_iface_func_ptr_t;
|
---|
50 |
|
---|
51 | typedef struct {
|
---|
52 | int method_count;
|
---|
53 | remote_iface_func_ptr_t *methods;
|
---|
54 | } remote_iface_t;
|
---|
55 |
|
---|
56 | typedef struct {
|
---|
57 | remote_iface_t * ifaces[DEV_IFACE_COUNT];
|
---|
58 | } iface_dipatch_table_t;
|
---|
59 |
|
---|
60 | static inline int get_iface_index(dev_inferface_id_t id)
|
---|
61 | {
|
---|
62 | return id - DEV_IFACE_FIRST;
|
---|
63 | }
|
---|
64 |
|
---|
65 | static inline bool is_valid_iface_id(dev_inferface_id_t id)
|
---|
66 | {
|
---|
67 | return DEV_IFACE_FIRST <= id && id < DEV_IFACE_MAX;
|
---|
68 | }
|
---|
69 |
|
---|
70 | // device
|
---|
71 |
|
---|
72 | struct device {
|
---|
73 | device_handle_t handle;
|
---|
74 | ipcarg_t parent_phone;
|
---|
75 | const char *name;
|
---|
76 | match_id_list_t match_ids;
|
---|
77 | void *driver_data;
|
---|
78 | void *interfaces[DEV_IFACE_COUNT];
|
---|
79 |
|
---|
80 | // TODO add more items
|
---|
81 |
|
---|
82 | link_t link;
|
---|
83 | };
|
---|
84 |
|
---|
85 |
|
---|
86 | // driver
|
---|
87 |
|
---|
88 | typedef struct driver_ops {
|
---|
89 | bool (*add_device)(device_t *dev);
|
---|
90 | // TODO add other generic driver operations
|
---|
91 | } driver_ops_t;
|
---|
92 |
|
---|
93 | typedef struct driver {
|
---|
94 | const char *name;
|
---|
95 | driver_ops_t *driver_ops;
|
---|
96 | } driver_t;
|
---|
97 |
|
---|
98 |
|
---|
99 |
|
---|
100 |
|
---|
101 |
|
---|
102 | int driver_main(driver_t *drv);
|
---|
103 |
|
---|
104 | static inline device_t * create_device()
|
---|
105 | {
|
---|
106 | device_t *dev = malloc(sizeof(device_t));
|
---|
107 | if (NULL != dev) {
|
---|
108 | memset(dev, 0, sizeof(device_t));
|
---|
109 | }
|
---|
110 | list_initialize(&dev->match_ids.ids);
|
---|
111 | return dev;
|
---|
112 | }
|
---|
113 |
|
---|
114 | static inline void delete_device(device_t *dev)
|
---|
115 | {
|
---|
116 | clean_match_ids(&dev->match_ids);
|
---|
117 | if (NULL != dev->name) {
|
---|
118 | free(dev->name);
|
---|
119 | }
|
---|
120 | free(dev);
|
---|
121 | }
|
---|
122 |
|
---|
123 | static inline void device_set_iface (device_t *dev, dev_inferface_id_t id, void *iface)
|
---|
124 | {
|
---|
125 | assert(is_valid_iface_id(id));
|
---|
126 |
|
---|
127 | int idx = get_iface_index(id);
|
---|
128 | dev->interfaces[idx] = iface;
|
---|
129 | }
|
---|
130 |
|
---|
131 | bool child_device_register(device_t *child, device_t *parent);
|
---|
132 |
|
---|
133 |
|
---|
134 | #endif
|
---|
135 |
|
---|
136 | /**
|
---|
137 | * @}
|
---|
138 | */
|
---|