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

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

parts of generic char interface, fixed some bugs

  • Property mode set to 100644
File size: 9.7 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)) {
[52b7b1bb]255 // this is not device's interface
[f658458]256 printf("%s: driver_connection_gen error - invalid interface id %d.", driver->name, iface_idx);
[52b7b1bb]257 ipc_answer_0(callid, ENOTSUP);
258 break;
259 }
260
261 // calling one of the device's interfaces
262
263 // get the device interface structure
[3843ecb]264 void *iface = device_get_iface(dev, iface_idx);
[52b7b1bb]265 if (NULL == iface) {
[9a66bc2e]266 printf("%s: driver_connection_gen error - ", driver->name);
[f658458]267 printf("device with handle %d has no interface with id %d.\n", handle, iface_idx);
[a1769ee]268 ipc_answer_0(callid, ENOTSUP);
[52b7b1bb]269 break;
[a1769ee]270 }
[52b7b1bb]271
272 // get the corresponding interface for remote request handling ("remote interface")
[3843ecb]273 remote_iface_t* rem_iface = get_remote_iface(iface_idx);
[52b7b1bb]274 assert(NULL != rem_iface);
275
276 // get the method of the remote interface
277 ipcarg_t iface_method_idx = IPC_GET_ARG1(call);
278 remote_iface_func_ptr_t iface_method_ptr = get_remote_method(rem_iface, iface_method_idx);
279 if (NULL == iface_method_ptr) {
280 // the interface has not such method
[9a66bc2e]281 printf("%s: driver_connection_gen error - invalid interface method.", driver->name);
[52b7b1bb]282 ipc_answer_0(callid, ENOTSUP);
283 break;
284 }
285
286 // call the remote interface's method, which will receive parameters from the remote client
287 // and it will pass it to the corresponding local interface method associated with the device
288 // by its driver
289 (*iface_method_ptr)(dev, iface, callid, &call);
[a1769ee]290 break;
291 }
292 }
293}
294
[c16cf62]295static void driver_connection_driver(ipc_callid_t iid, ipc_call_t *icall)
296{
[52b7b1bb]297 driver_connection_gen(iid, icall, true);
[c16cf62]298}
299
300static void driver_connection_client(ipc_callid_t iid, ipc_call_t *icall)
301{
[52b7b1bb]302 driver_connection_gen(iid, icall, false);
[c16cf62]303}
304
305
306/** Function for handling connections to device driver.
307 *
308 */
309static void driver_connection(ipc_callid_t iid, ipc_call_t *icall)
310{
311 /* Select interface */
312 switch ((ipcarg_t) (IPC_GET_ARG1(*icall))) {
313 case DRIVER_DEVMAN:
314 // handle PnP events from device manager
315 driver_connection_devman(iid, icall);
316 break;
317 case DRIVER_DRIVER:
318 // handle request from drivers of child devices
319 driver_connection_driver(iid, icall);
320 break;
321 case DRIVER_CLIENT:
[52b7b1bb]322 // handle requests from client applications
[c16cf62]323 driver_connection_client(iid, icall);
324 break;
325
326 default:
[52b7b1bb]327 /* No such interface */
[c16cf62]328 ipc_answer_0(iid, ENOENT);
329 }
330}
331
[df747b9c]332int child_device_register(device_t *child, device_t *parent)
[7707954]333{
[2480e19]334 // printf("%s: child_device_register\n", driver->name);
[52b7b1bb]335
[bda60d9]336 assert(NULL != child->name);
[52b7b1bb]337
[df747b9c]338 int res;
339
[9a66bc2e]340 add_to_devices_list(child);
[df747b9c]341 if (EOK == (res = devman_child_device_register(child->name, &child->match_ids, parent->handle, &child->handle))) {
342 return res;
[7707954]343 }
[9a66bc2e]344 remove_from_devices_list(child);
[df747b9c]345 return res;
[7707954]346}
347
[52b7b1bb]348int driver_main(driver_t *drv)
[c16cf62]349{
350 // remember the driver structure - driver_ops will be called by generic handler for incoming connections
351 driver = drv;
[52b7b1bb]352
[7f8b581]353 // initialize the list of interrupt contexts
354 init_interrupt_context_list(&interrupt_contexts);
355
356 // set generic interrupt handler
357 async_set_interrupt_received(driver_irq_handler);
358
[c16cf62]359 // register driver by device manager with generic handler for incoming connections
[52b7b1bb]360 devman_driver_register(driver->name, driver_connection);
[c16cf62]361
362 async_manager();
363
364 // Never reached
[52b7b1bb]365 return 0;
[c16cf62]366}
367
368/**
369 * @}
[52b7b1bb]370 */
Note: See TracBrowser for help on using the repository browser.