source: mainline/uspace/lib/libdrv/generic/driver.c@ 08d9525a

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

add default handler for client requests which do not use any of the standard interfaces

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