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
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 // this is not device's interface
256 printf("%s: driver_connection_gen error - invalid interface id %d.", driver->name, iface_idx);
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
264 void *iface = device_get_iface(dev, iface_idx);
265 if (NULL == iface) {
266 printf("%s: driver_connection_gen error - ", driver->name);
267 printf("device with handle %d has no interface with id %d.\n", handle, iface_idx);
268 ipc_answer_0(callid, ENOTSUP);
269 break;
270 }
271
272 // get the corresponding interface for remote request handling ("remote interface")
273 remote_iface_t* rem_iface = get_remote_iface(iface_idx);
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
281 printf("%s: driver_connection_gen error - invalid interface method.", driver->name);
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);
290 break;
291 }
292 }
293}
294
295static void driver_connection_driver(ipc_callid_t iid, ipc_call_t *icall)
296{
297 driver_connection_gen(iid, icall, true);
298}
299
300static void driver_connection_client(ipc_callid_t iid, ipc_call_t *icall)
301{
302 driver_connection_gen(iid, icall, false);
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:
322 // handle requests from client applications
323 driver_connection_client(iid, icall);
324 break;
325
326 default:
327 /* No such interface */
328 ipc_answer_0(iid, ENOENT);
329 }
330}
331
332int child_device_register(device_t *child, device_t *parent)
333{
334 // printf("%s: child_device_register\n", driver->name);
335
336 assert(NULL != child->name);
337
338 int res;
339
340 add_to_devices_list(child);
341 if (EOK == (res = devman_child_device_register(child->name, &child->match_ids, parent->handle, &child->handle))) {
342 return res;
343 }
344 remove_from_devices_list(child);
345 return res;
346}
347
348int driver_main(driver_t *drv)
349{
350 // remember the driver structure - driver_ops will be called by generic handler for incoming connections
351 driver = drv;
352
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
359 // register driver by device manager with generic handler for incoming connections
360 devman_driver_register(driver->name, driver_connection);
361
362 async_manager();
363
364 // Never reached
365 return 0;
366}
367
368/**
369 * @}
370 */
Note: See TracBrowser for help on using the repository browser.