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 | /**
|
---|
30 | * @defgroup libdrv generic device driver support.
|
---|
31 | * @brief HelenOS generic device driver support.
|
---|
32 | * @{
|
---|
33 | */
|
---|
34 |
|
---|
35 | /** @file
|
---|
36 | */
|
---|
37 |
|
---|
38 | #include <assert.h>
|
---|
39 | #include <ipc/services.h>
|
---|
40 | #include <ipc/ns.h>
|
---|
41 | #include <async.h>
|
---|
42 | #include <stdio.h>
|
---|
43 | #include <errno.h>
|
---|
44 | #include <bool.h>
|
---|
45 | #include <fibril_synch.h>
|
---|
46 | #include <stdlib.h>
|
---|
47 | #include <string.h>
|
---|
48 | #include <ctype.h>
|
---|
49 | #include <errno.h>
|
---|
50 |
|
---|
51 | #include <devman.h>
|
---|
52 | #include <ipc/devman.h>
|
---|
53 | #include <ipc/driver.h>
|
---|
54 |
|
---|
55 | #include "driver.h"
|
---|
56 |
|
---|
57 | static driver_t *driver;
|
---|
58 | LIST_INITIALIZE(devices);
|
---|
59 | FIBRIL_MUTEX_INITIALIZE(devices_mutex);
|
---|
60 |
|
---|
61 | static void add_to_devices_list(device_t *dev)
|
---|
62 | {
|
---|
63 | fibril_mutex_lock(&devices_mutex);
|
---|
64 | list_append(&dev->link, &devices);
|
---|
65 | fibril_mutex_unlock(&devices_mutex);
|
---|
66 | }
|
---|
67 |
|
---|
68 | static void remove_from_devices_list(device_t *dev)
|
---|
69 | {
|
---|
70 | fibril_mutex_lock(&devices_mutex);
|
---|
71 | list_remove(&dev->link);
|
---|
72 | fibril_mutex_unlock(&devices_mutex);
|
---|
73 | }
|
---|
74 |
|
---|
75 | static device_t * driver_get_device(link_t *devices, device_handle_t handle)
|
---|
76 | {
|
---|
77 | device_t *dev = NULL;
|
---|
78 |
|
---|
79 | fibril_mutex_lock(&devices_mutex);
|
---|
80 | link_t *link = devices->next;
|
---|
81 | while (link != devices) {
|
---|
82 | dev = list_get_instance(link, device_t, link);
|
---|
83 | if (handle == dev->handle) {
|
---|
84 | fibril_mutex_unlock(&devices_mutex);
|
---|
85 | return dev;
|
---|
86 | }
|
---|
87 | link = link->next;
|
---|
88 | }
|
---|
89 | fibril_mutex_unlock(&devices_mutex);
|
---|
90 |
|
---|
91 | return NULL;
|
---|
92 | }
|
---|
93 |
|
---|
94 | static void driver_add_device(ipc_callid_t iid, ipc_call_t *icall)
|
---|
95 | {
|
---|
96 | char *dev_name = NULL;
|
---|
97 | int res = EOK;
|
---|
98 |
|
---|
99 | device_handle_t dev_handle = IPC_GET_ARG1(*icall);
|
---|
100 | device_t *dev = create_device();
|
---|
101 | dev->handle = dev_handle;
|
---|
102 |
|
---|
103 | async_string_receive(&dev_name, 0, NULL);
|
---|
104 | dev->name = dev_name;
|
---|
105 |
|
---|
106 | add_to_devices_list(dev);
|
---|
107 | res = driver->driver_ops->add_device(dev);
|
---|
108 | if (0 == res) {
|
---|
109 | printf("%s: new device with handle = %x was added.\n", driver->name, dev_handle);
|
---|
110 | } else {
|
---|
111 | printf("%s: failed to add a new device with handle = %d.\n", driver->name, dev_handle);
|
---|
112 | remove_from_devices_list(dev);
|
---|
113 | delete_device(dev);
|
---|
114 | }
|
---|
115 |
|
---|
116 | ipc_answer_0(iid, res);
|
---|
117 | }
|
---|
118 |
|
---|
119 | static void driver_connection_devman(ipc_callid_t iid, ipc_call_t *icall)
|
---|
120 | {
|
---|
121 | /* Accept connection */
|
---|
122 | ipc_answer_0(iid, EOK);
|
---|
123 |
|
---|
124 | bool cont = true;
|
---|
125 | while (cont) {
|
---|
126 | ipc_call_t call;
|
---|
127 | ipc_callid_t callid = async_get_call(&call);
|
---|
128 |
|
---|
129 | switch (IPC_GET_METHOD(call)) {
|
---|
130 | case IPC_M_PHONE_HUNGUP:
|
---|
131 | cont = false;
|
---|
132 | continue;
|
---|
133 | case DRIVER_ADD_DEVICE:
|
---|
134 | driver_add_device(callid, &call);
|
---|
135 | break;
|
---|
136 | default:
|
---|
137 | if (!(callid & IPC_CALLID_NOTIFICATION))
|
---|
138 | ipc_answer_0(callid, ENOENT);
|
---|
139 | }
|
---|
140 | }
|
---|
141 | }
|
---|
142 |
|
---|
143 | /**
|
---|
144 | * Generic client connection handler both for applications and drivers.
|
---|
145 | *
|
---|
146 | * @param drv true for driver client, false for other clients (applications, services etc.).
|
---|
147 | */
|
---|
148 | static void driver_connection_gen(ipc_callid_t iid, ipc_call_t *icall, bool drv)
|
---|
149 | {
|
---|
150 | // Answer the first IPC_M_CONNECT_ME_TO call and remember the handle of the device to which the client connected.
|
---|
151 | device_handle_t handle = IPC_GET_ARG2(*icall);
|
---|
152 | device_t *dev = driver_get_device(&devices, handle);
|
---|
153 |
|
---|
154 | if (dev == NULL) {
|
---|
155 | printf("%s: driver_connection_gen error - no device with handle %x was found.\n", driver->name, handle);
|
---|
156 | ipc_answer_0(iid, ENOENT);
|
---|
157 | return;
|
---|
158 | }
|
---|
159 |
|
---|
160 |
|
---|
161 | // TODO - if the client is not a driver, check whether it is allowed to use the device
|
---|
162 |
|
---|
163 | // TODO open the device (introduce some callbacks for opening and closing devices registered by the driver)
|
---|
164 |
|
---|
165 | ipc_answer_0(iid, EOK);
|
---|
166 |
|
---|
167 | while (1) {
|
---|
168 | ipc_callid_t callid;
|
---|
169 | ipc_call_t call;
|
---|
170 | callid = async_get_call(&call);
|
---|
171 | ipcarg_t method = IPC_GET_METHOD(call);
|
---|
172 | int iface_idx;
|
---|
173 |
|
---|
174 | switch (method) {
|
---|
175 | case IPC_M_PHONE_HUNGUP:
|
---|
176 |
|
---|
177 | // TODO close the device
|
---|
178 |
|
---|
179 | ipc_answer_0(callid, EOK);
|
---|
180 | return;
|
---|
181 | default:
|
---|
182 | // convert ipc interface id to interface index
|
---|
183 |
|
---|
184 | iface_idx = DEV_IFACE_IDX(method);
|
---|
185 |
|
---|
186 | if (!is_valid_iface_idx(iface_idx)) {
|
---|
187 | // this is not device's interface
|
---|
188 | printf("%s: driver_connection_gen error - invalid interface id %x.", driver->name, method);
|
---|
189 | ipc_answer_0(callid, ENOTSUP);
|
---|
190 | break;
|
---|
191 | }
|
---|
192 |
|
---|
193 | // calling one of the device's interfaces
|
---|
194 |
|
---|
195 | // get the device interface structure
|
---|
196 | void *iface = device_get_iface(dev, iface_idx);
|
---|
197 | if (NULL == iface) {
|
---|
198 | printf("%s: driver_connection_gen error - ", driver->name);
|
---|
199 | printf("device with handle %x has no interface with id %x.\n", handle, method);
|
---|
200 | ipc_answer_0(callid, ENOTSUP);
|
---|
201 | break;
|
---|
202 | }
|
---|
203 |
|
---|
204 | // get the corresponding interface for remote request handling ("remote interface")
|
---|
205 | remote_iface_t* rem_iface = get_remote_iface(iface_idx);
|
---|
206 | assert(NULL != rem_iface);
|
---|
207 |
|
---|
208 | // get the method of the remote interface
|
---|
209 | ipcarg_t iface_method_idx = IPC_GET_ARG1(call);
|
---|
210 | remote_iface_func_ptr_t iface_method_ptr = get_remote_method(rem_iface, iface_method_idx);
|
---|
211 | if (NULL == iface_method_ptr) {
|
---|
212 | // the interface has not such method
|
---|
213 | printf("%s: driver_connection_gen error - invalid interface method.", driver->name);
|
---|
214 | ipc_answer_0(callid, ENOTSUP);
|
---|
215 | break;
|
---|
216 | }
|
---|
217 |
|
---|
218 | // call the remote interface's method, which will receive parameters from the remote client
|
---|
219 | // and it will pass it to the corresponding local interface method associated with the device
|
---|
220 | // by its driver
|
---|
221 | (*iface_method_ptr)(dev, iface, callid, &call);
|
---|
222 | break;
|
---|
223 | }
|
---|
224 | }
|
---|
225 | }
|
---|
226 |
|
---|
227 | static void driver_connection_driver(ipc_callid_t iid, ipc_call_t *icall)
|
---|
228 | {
|
---|
229 | driver_connection_gen(iid, icall, true);
|
---|
230 | }
|
---|
231 |
|
---|
232 | static void driver_connection_client(ipc_callid_t iid, ipc_call_t *icall)
|
---|
233 | {
|
---|
234 | driver_connection_gen(iid, icall, false);
|
---|
235 | }
|
---|
236 |
|
---|
237 |
|
---|
238 | /** Function for handling connections to device driver.
|
---|
239 | *
|
---|
240 | */
|
---|
241 | static void driver_connection(ipc_callid_t iid, ipc_call_t *icall)
|
---|
242 | {
|
---|
243 | /* Select interface */
|
---|
244 | switch ((ipcarg_t) (IPC_GET_ARG1(*icall))) {
|
---|
245 | case DRIVER_DEVMAN:
|
---|
246 | // handle PnP events from device manager
|
---|
247 | driver_connection_devman(iid, icall);
|
---|
248 | break;
|
---|
249 | case DRIVER_DRIVER:
|
---|
250 | // handle request from drivers of child devices
|
---|
251 | driver_connection_driver(iid, icall);
|
---|
252 | break;
|
---|
253 | case DRIVER_CLIENT:
|
---|
254 | // handle requests from client applications
|
---|
255 | driver_connection_client(iid, icall);
|
---|
256 | break;
|
---|
257 |
|
---|
258 | default:
|
---|
259 | /* No such interface */
|
---|
260 | ipc_answer_0(iid, ENOENT);
|
---|
261 | }
|
---|
262 | }
|
---|
263 |
|
---|
264 | int child_device_register(device_t *child, device_t *parent)
|
---|
265 | {
|
---|
266 | // printf("%s: child_device_register\n", driver->name);
|
---|
267 |
|
---|
268 | assert(NULL != child->name);
|
---|
269 |
|
---|
270 | int res;
|
---|
271 |
|
---|
272 | add_to_devices_list(child);
|
---|
273 | if (EOK == (res = devman_child_device_register(child->name, &child->match_ids, parent->handle, &child->handle))) {
|
---|
274 | return res;
|
---|
275 | }
|
---|
276 | remove_from_devices_list(child);
|
---|
277 | return res;
|
---|
278 | }
|
---|
279 |
|
---|
280 | int driver_main(driver_t *drv)
|
---|
281 | {
|
---|
282 | // remember the driver structure - driver_ops will be called by generic handler for incoming connections
|
---|
283 | driver = drv;
|
---|
284 |
|
---|
285 | // register driver by device manager with generic handler for incoming connections
|
---|
286 | devman_driver_register(driver->name, driver_connection);
|
---|
287 |
|
---|
288 | async_manager();
|
---|
289 |
|
---|
290 | // Never reached
|
---|
291 | return 0;
|
---|
292 | }
|
---|
293 |
|
---|
294 | /**
|
---|
295 | * @}
|
---|
296 | */
|
---|