source: mainline/uspace/lib/drv/generic/driver.c@ 04cb68f2

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

fix a little bug

  • Property mode set to 100644
File size: 9.9 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/**
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 <str.h>
48#include <ctype.h>
49#include <errno.h>
50
51#include <ipc/driver.h>
52
53#include "driver.h"
54
55// driver structure
56
57static driver_t *driver;
58
59// devices
60
61LIST_INITIALIZE(devices);
62FIBRIL_MUTEX_INITIALIZE(devices_mutex);
63
64// interrupts
65
66static interrupt_context_list_t interrupt_contexts;
67
68static irq_cmd_t default_cmds[] = {
69 {
70 .cmd = CMD_ACCEPT
71 }
72};
73
74static irq_code_t default_pseudocode = {
75 sizeof(default_cmds) / sizeof(irq_cmd_t),
76 default_cmds
77};
78
79
80static void driver_irq_handler(ipc_callid_t iid, ipc_call_t *icall)
81{
82 int id = (int)IPC_GET_METHOD(*icall);
83 interrupt_context_t *ctx = find_interrupt_context_by_id(&interrupt_contexts, id);
84 if (NULL != ctx && NULL != ctx->handler) {
85 (*ctx->handler)(ctx->dev, iid, icall);
86 }
87}
88
89int register_interrupt_handler(device_t *dev, int irq, interrupt_handler_t *handler, irq_code_t *pseudocode)
90{
91 interrupt_context_t *ctx = create_interrupt_context();
92
93 ctx->dev = dev;
94 ctx->irq = irq;
95 ctx->handler = handler;
96
97 add_interrupt_context(&interrupt_contexts, ctx);
98
99 if (NULL == pseudocode) {
100 pseudocode = &default_pseudocode;
101 }
102
103 int res = ipc_register_irq(irq, dev->handle, ctx->id, pseudocode);
104 if (0 != res) {
105 remove_interrupt_context(&interrupt_contexts, ctx);
106 delete_interrupt_context(ctx);
107 }
108 return res;
109}
110
111int unregister_interrupt_handler(device_t *dev, int irq)
112{
113 interrupt_context_t *ctx = find_interrupt_context(&interrupt_contexts, dev, irq);
114 int res = ipc_unregister_irq(irq, dev->handle);
115 if (NULL != ctx) {
116 remove_interrupt_context(&interrupt_contexts, ctx);
117 delete_interrupt_context(ctx);
118 }
119 return res;
120}
121
122static void add_to_devices_list(device_t *dev)
123{
124 fibril_mutex_lock(&devices_mutex);
125 list_append(&dev->link, &devices);
126 fibril_mutex_unlock(&devices_mutex);
127}
128
129static void remove_from_devices_list(device_t *dev)
130{
131 fibril_mutex_lock(&devices_mutex);
132 list_remove(&dev->link);
133 fibril_mutex_unlock(&devices_mutex);
134}
135
136static device_t * driver_get_device(link_t *devices, device_handle_t handle)
137{
138 device_t *dev = NULL;
139
140 fibril_mutex_lock(&devices_mutex);
141 link_t *link = devices->next;
142 while (link != devices) {
143 dev = list_get_instance(link, device_t, link);
144 if (handle == dev->handle) {
145 fibril_mutex_unlock(&devices_mutex);
146 return dev;
147 }
148 link = link->next;
149 }
150 fibril_mutex_unlock(&devices_mutex);
151
152 return NULL;
153}
154
155static void driver_add_device(ipc_callid_t iid, ipc_call_t *icall)
156{
157 char *dev_name = NULL;
158 int res = EOK;
159
160 device_handle_t dev_handle = IPC_GET_ARG1(*icall);
161 device_t *dev = create_device();
162 dev->handle = dev_handle;
163
164 async_data_write_accept((void **)&dev_name, true, 0, 0, 0, 0);
165 dev->name = dev_name;
166
167 add_to_devices_list(dev);
168 res = driver->driver_ops->add_device(dev);
169 if (0 == res) {
170 printf("%s: new device with handle = %x was added.\n", driver->name, dev_handle);
171 } else {
172 printf("%s: failed to add a new device with handle = %d.\n", driver->name, dev_handle);
173 remove_from_devices_list(dev);
174 delete_device(dev);
175 }
176
177 ipc_answer_0(iid, res);
178}
179
180static void driver_connection_devman(ipc_callid_t iid, ipc_call_t *icall)
181{
182 /* Accept connection */
183 ipc_answer_0(iid, EOK);
184
185 bool cont = true;
186 while (cont) {
187 ipc_call_t call;
188 ipc_callid_t callid = async_get_call(&call);
189
190 switch (IPC_GET_METHOD(call)) {
191 case IPC_M_PHONE_HUNGUP:
192 cont = false;
193 continue;
194 case DRIVER_ADD_DEVICE:
195 driver_add_device(callid, &call);
196 break;
197 default:
198 if (!(callid & IPC_CALLID_NOTIFICATION))
199 ipc_answer_0(callid, ENOENT);
200 }
201 }
202}
203
204/**
205 * Generic client connection handler both for applications and drivers.
206 *
207 * @param drv true for driver client, false for other clients (applications, services etc.).
208 */
209static void driver_connection_gen(ipc_callid_t iid, ipc_call_t *icall, bool drv)
210{
211 // Answer the first IPC_M_CONNECT_ME_TO call and remember the handle of the device to which the client connected.
212 device_handle_t handle = IPC_GET_ARG2(*icall);
213 device_t *dev = driver_get_device(&devices, handle);
214
215 if (dev == NULL) {
216 printf("%s: driver_connection_gen error - no device with handle %x was found.\n", driver->name, handle);
217 ipc_answer_0(iid, ENOENT);
218 return;
219 }
220
221
222 // TODO - if the client is not a driver, check whether it is allowed to use the device
223
224 int ret = EOK;
225 // open the device
226 if (NULL != dev->ops && NULL != dev->ops->open) {
227 ret = (*dev->ops->open)(dev);
228 }
229
230 ipc_answer_0(iid, ret);
231 if (EOK != ret) {
232 return;
233 }
234
235 while (1) {
236 ipc_callid_t callid;
237 ipc_call_t call;
238 callid = async_get_call(&call);
239 ipcarg_t method = IPC_GET_METHOD(call);
240 int iface_idx;
241
242 switch (method) {
243 case IPC_M_PHONE_HUNGUP:
244 // close the device
245 if (NULL != dev->ops && NULL != dev->ops->close) {
246 (*dev->ops->close)(dev);
247 }
248 ipc_answer_0(callid, EOK);
249 return;
250 default:
251 // convert ipc interface id to interface index
252
253 iface_idx = DEV_IFACE_IDX(method);
254
255 if (!is_valid_iface_idx(iface_idx)) {
256 remote_handler_t *default_handler;
257 if (NULL != (default_handler = device_get_default_handler(dev))) {
258 (*default_handler)(dev, callid, &call);
259 break;
260 }
261 // this is not device's interface and the default handler is not provided
262 printf("%s: driver_connection_gen error - invalid interface id %d.", driver->name, iface_idx);
263 ipc_answer_0(callid, ENOTSUP);
264 break;
265 }
266
267 // calling one of the device's interfaces
268
269 // get the device interface structure
270 void *iface = device_get_iface(dev, iface_idx);
271 if (NULL == iface) {
272 printf("%s: driver_connection_gen error - ", driver->name);
273 printf("device with handle %d has no interface with id %d.\n", handle, iface_idx);
274 ipc_answer_0(callid, ENOTSUP);
275 break;
276 }
277
278 // get the corresponding interface for remote request handling ("remote interface")
279 remote_iface_t* rem_iface = get_remote_iface(iface_idx);
280 assert(NULL != rem_iface);
281
282 // get the method of the remote interface
283 ipcarg_t iface_method_idx = IPC_GET_ARG1(call);
284 remote_iface_func_ptr_t iface_method_ptr = get_remote_method(rem_iface, iface_method_idx);
285 if (NULL == iface_method_ptr) {
286 // the interface has not such method
287 printf("%s: driver_connection_gen error - invalid interface method.", driver->name);
288 ipc_answer_0(callid, ENOTSUP);
289 break;
290 }
291
292 // call the remote interface's method, which will receive parameters from the remote client
293 // and it will pass it to the corresponding local interface method associated with the device
294 // by its driver
295 (*iface_method_ptr)(dev, iface, callid, &call);
296 break;
297 }
298 }
299}
300
301static void driver_connection_driver(ipc_callid_t iid, ipc_call_t *icall)
302{
303 driver_connection_gen(iid, icall, true);
304}
305
306static void driver_connection_client(ipc_callid_t iid, ipc_call_t *icall)
307{
308 driver_connection_gen(iid, icall, false);
309}
310
311
312/** Function for handling connections to device driver.
313 *
314 */
315static void driver_connection(ipc_callid_t iid, ipc_call_t *icall)
316{
317 /* Select interface */
318 switch ((ipcarg_t) (IPC_GET_ARG1(*icall))) {
319 case DRIVER_DEVMAN:
320 // handle PnP events from device manager
321 driver_connection_devman(iid, icall);
322 break;
323 case DRIVER_DRIVER:
324 // handle request from drivers of child devices
325 driver_connection_driver(iid, icall);
326 break;
327 case DRIVER_CLIENT:
328 // handle requests from client applications
329 driver_connection_client(iid, icall);
330 break;
331
332 default:
333 /* No such interface */
334 ipc_answer_0(iid, ENOENT);
335 }
336}
337
338int child_device_register(device_t *child, device_t *parent)
339{
340 // printf("%s: child_device_register\n", driver->name);
341
342 assert(NULL != child->name);
343
344 int res;
345
346 add_to_devices_list(child);
347 if (EOK == (res = devman_child_device_register(child->name, &child->match_ids, parent->handle, &child->handle))) {
348 return res;
349 }
350 remove_from_devices_list(child);
351 return res;
352}
353
354int driver_main(driver_t *drv)
355{
356 // remember the driver structure - driver_ops will be called by generic handler for incoming connections
357 driver = drv;
358
359 // initialize the list of interrupt contexts
360 init_interrupt_context_list(&interrupt_contexts);
361
362 // set generic interrupt handler
363 async_set_interrupt_received(driver_irq_handler);
364
365 // register driver by device manager with generic handler for incoming connections
366 devman_driver_register(driver->name, driver_connection);
367
368 async_manager();
369
370 // Never reached
371 return 0;
372}
373
374/**
375 * @}
376 */
Note: See TracBrowser for help on using the repository browser.