source: mainline/uspace/lib/drv/generic/driver.c@ 3a4b3ba

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 3a4b3ba was cb819f9, checked in by Jakub Jermar <jakub@…>, 15 years ago

When using async framework, we already know the call is not a notification,
so there is no need to test it explicitly.

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