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
RevLine 
[c16cf62]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 */
[52b7b1bb]37
[c16cf62]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>
[66babbd]49#include <errno.h>
[c16cf62]50
51#include <devman.h>
52#include <ipc/devman.h>
[084ff99]53#include <ipc/driver.h>
[c16cf62]54
55#include "driver.h"
56
[7f8b581]57// driver structure
58
[c16cf62]59static driver_t *driver;
[7f8b581]60
61// devices
62
[084ff99]63LIST_INITIALIZE(devices);
[9a66bc2e]64FIBRIL_MUTEX_INITIALIZE(devices_mutex);
[084ff99]65
[7f8b581]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)
[2300b9d]83{
[7f8b581]84 int id = (int)IPC_GET_METHOD(*icall);
85 interrupt_context_t *ctx = find_interrupt_context_by_id(&interrupt_contexts, id);
[2300b9d]86 if (NULL != ctx && NULL != ctx->handler) {
87 (*ctx->handler)(ctx->dev, iid, icall);
88 }
[7f8b581]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
[9a66bc2e]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);
[5af21c5]134 list_remove(&dev->link);
[9a66bc2e]135 fibril_mutex_unlock(&devices_mutex);
136}
137
[52b7b1bb]138static device_t * driver_get_device(link_t *devices, device_handle_t handle)
139{
[a1769ee]140 device_t *dev = NULL;
[9a66bc2e]141
142 fibril_mutex_lock(&devices_mutex);
[a1769ee]143 link_t *link = devices->next;
144 while (link != devices) {
145 dev = list_get_instance(link, device_t, link);
146 if (handle == dev->handle) {
[9a66bc2e]147 fibril_mutex_unlock(&devices_mutex);
[a1769ee]148 return dev;
149 }
[9a66bc2e]150 link = link->next;
[a1769ee]151 }
[9a66bc2e]152 fibril_mutex_unlock(&devices_mutex);
[52b7b1bb]153
[a1769ee]154 return NULL;
155}
156
[52b7b1bb]157static void driver_add_device(ipc_callid_t iid, ipc_call_t *icall)
[084ff99]158{
[df747b9c]159 char *dev_name = NULL;
160 int res = EOK;
[9a66bc2e]161
[52b7b1bb]162 device_handle_t dev_handle = IPC_GET_ARG1(*icall);
[5af21c5]163 device_t *dev = create_device();
[084ff99]164 dev->handle = dev_handle;
[df747b9c]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) {
[9a66bc2e]172 printf("%s: new device with handle = %x was added.\n", driver->name, dev_handle);
173 } else {
[df747b9c]174 printf("%s: failed to add a new device with handle = %d.\n", driver->name, dev_handle);
[9a66bc2e]175 remove_from_devices_list(dev);
[5af21c5]176 delete_device(dev);
[084ff99]177 }
[df747b9c]178
179 ipc_answer_0(iid, res);
[084ff99]180}
[c16cf62]181
182static void driver_connection_devman(ipc_callid_t iid, ipc_call_t *icall)
183{
184 /* Accept connection */
185 ipc_answer_0(iid, EOK);
[52b7b1bb]186
[c16cf62]187 bool cont = true;
188 while (cont) {
189 ipc_call_t call;
190 ipc_callid_t callid = async_get_call(&call);
[52b7b1bb]191
[c16cf62]192 switch (IPC_GET_METHOD(call)) {
193 case IPC_M_PHONE_HUNGUP:
194 cont = false;
195 continue;
196 case DRIVER_ADD_DEVICE:
[084ff99]197 driver_add_device(callid, &call);
[c16cf62]198 break;
199 default:
200 if (!(callid & IPC_CALLID_NOTIFICATION))
201 ipc_answer_0(callid, ENOENT);
202 }
[52b7b1bb]203 }
[c16cf62]204}
205
[52b7b1bb]206/**
[a1769ee]207 * Generic client connection handler both for applications and drivers.
[52b7b1bb]208 *
[9a66bc2e]209 * @param drv true for driver client, false for other clients (applications, services etc.).
[a1769ee]210 */
[9a66bc2e]211static void driver_connection_gen(ipc_callid_t iid, ipc_call_t *icall, bool drv)
[52b7b1bb]212{
213 // Answer the first IPC_M_CONNECT_ME_TO call and remember the handle of the device to which the client connected.
[9a66bc2e]214 device_handle_t handle = IPC_GET_ARG2(*icall);
[a1769ee]215 device_t *dev = driver_get_device(&devices, handle);
[52b7b1bb]216
[a1769ee]217 if (dev == NULL) {
[9a66bc2e]218 printf("%s: driver_connection_gen error - no device with handle %x was found.\n", driver->name, handle);
[a1769ee]219 ipc_answer_0(iid, ENOENT);
220 return;
221 }
[5cd136ab]222
223
224 // TODO - if the client is not a driver, check whether it is allowed to use the device
[a1769ee]225
[25a7e11d]226 int ret = EOK;
227 // open the device
228 if (NULL != dev->class && NULL != dev->class->open) {
229 ret = (*dev->class->open)(dev);
230 }
[a1769ee]231
[25a7e11d]232 ipc_answer_0(iid, ret);
[52b7b1bb]233
[a1769ee]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);
[3843ecb]239 int iface_idx;
240
[a1769ee]241 switch (method) {
[25a7e11d]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 }
[a1769ee]247 ipc_answer_0(callid, EOK);
248 return;
[3843ecb]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)) {
[08d9525a]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
[f658458]261 printf("%s: driver_connection_gen error - invalid interface id %d.", driver->name, iface_idx);
[52b7b1bb]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
[3843ecb]269 void *iface = device_get_iface(dev, iface_idx);
[52b7b1bb]270 if (NULL == iface) {
[9a66bc2e]271 printf("%s: driver_connection_gen error - ", driver->name);
[f658458]272 printf("device with handle %d has no interface with id %d.\n", handle, iface_idx);
[a1769ee]273 ipc_answer_0(callid, ENOTSUP);
[52b7b1bb]274 break;
[a1769ee]275 }
[52b7b1bb]276
277 // get the corresponding interface for remote request handling ("remote interface")
[3843ecb]278 remote_iface_t* rem_iface = get_remote_iface(iface_idx);
[52b7b1bb]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
[9a66bc2e]286 printf("%s: driver_connection_gen error - invalid interface method.", driver->name);
[52b7b1bb]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);
[a1769ee]295 break;
296 }
297 }
298}
299
[c16cf62]300static void driver_connection_driver(ipc_callid_t iid, ipc_call_t *icall)
301{
[52b7b1bb]302 driver_connection_gen(iid, icall, true);
[c16cf62]303}
304
305static void driver_connection_client(ipc_callid_t iid, ipc_call_t *icall)
306{
[52b7b1bb]307 driver_connection_gen(iid, icall, false);
[c16cf62]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:
[52b7b1bb]327 // handle requests from client applications
[c16cf62]328 driver_connection_client(iid, icall);
329 break;
330
331 default:
[52b7b1bb]332 /* No such interface */
[c16cf62]333 ipc_answer_0(iid, ENOENT);
334 }
335}
336
[df747b9c]337int child_device_register(device_t *child, device_t *parent)
[7707954]338{
[2480e19]339 // printf("%s: child_device_register\n", driver->name);
[52b7b1bb]340
[bda60d9]341 assert(NULL != child->name);
[52b7b1bb]342
[df747b9c]343 int res;
344
[9a66bc2e]345 add_to_devices_list(child);
[df747b9c]346 if (EOK == (res = devman_child_device_register(child->name, &child->match_ids, parent->handle, &child->handle))) {
347 return res;
[7707954]348 }
[9a66bc2e]349 remove_from_devices_list(child);
[df747b9c]350 return res;
[7707954]351}
352
[52b7b1bb]353int driver_main(driver_t *drv)
[c16cf62]354{
355 // remember the driver structure - driver_ops will be called by generic handler for incoming connections
356 driver = drv;
[52b7b1bb]357
[7f8b581]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
[c16cf62]364 // register driver by device manager with generic handler for incoming connections
[52b7b1bb]365 devman_driver_register(driver->name, driver_connection);
[c16cf62]366
367 async_manager();
368
369 // Never reached
[52b7b1bb]370 return 0;
[c16cf62]371}
372
373/**
374 * @}
[52b7b1bb]375 */
Note: See TracBrowser for help on using the repository browser.