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

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

Merge from lp:~vojtech-horky/helenos/ddf-fixes.

  • Property mode set to 100644
File size: 11.3 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#include <inttypes.h>
51
52#include <ipc/driver.h>
53
54#include "driver.h"
55
56/* driver structure */
57
58static driver_t *driver;
59
60/* devices */
61
62LIST_INITIALIZE(devices);
63FIBRIL_MUTEX_INITIALIZE(devices_mutex);
64
65/* interrupts */
66
67static interrupt_context_list_t interrupt_contexts;
68
69static irq_cmd_t default_cmds[] = {
70 {
71 .cmd = CMD_ACCEPT
72 }
73};
74
75static irq_code_t default_pseudocode = {
76 sizeof(default_cmds) / sizeof(irq_cmd_t),
77 default_cmds
78};
79
80
81static void driver_irq_handler(ipc_callid_t iid, ipc_call_t *icall)
82{
83 int id = (int)IPC_GET_METHOD(*icall);
84 interrupt_context_t *ctx;
85
86 ctx = find_interrupt_context_by_id(&interrupt_contexts, id);
87 if (NULL != ctx && NULL != ctx->handler)
88 (*ctx->handler)(ctx->dev, iid, icall);
89}
90
91int
92register_interrupt_handler(device_t *dev, int irq, interrupt_handler_t *handler,
93 irq_code_t *pseudocode)
94{
95 interrupt_context_t *ctx = create_interrupt_context();
96
97 ctx->dev = dev;
98 ctx->irq = irq;
99 ctx->handler = handler;
100
101 add_interrupt_context(&interrupt_contexts, ctx);
102
103 if (NULL == pseudocode)
104 pseudocode = &default_pseudocode;
105
106 int res = ipc_register_irq(irq, dev->handle, ctx->id, pseudocode);
107 if (0 != res) {
108 remove_interrupt_context(&interrupt_contexts, ctx);
109 delete_interrupt_context(ctx);
110 }
111
112 return res;
113}
114
115int unregister_interrupt_handler(device_t *dev, int irq)
116{
117 interrupt_context_t *ctx = find_interrupt_context(&interrupt_contexts,
118 dev, irq);
119 int res = ipc_unregister_irq(irq, dev->handle);
120
121 if (NULL != ctx) {
122 remove_interrupt_context(&interrupt_contexts, ctx);
123 delete_interrupt_context(ctx);
124 }
125 return res;
126}
127
128static void add_to_devices_list(device_t *dev)
129{
130 fibril_mutex_lock(&devices_mutex);
131 list_append(&dev->link, &devices);
132 fibril_mutex_unlock(&devices_mutex);
133}
134
135static void remove_from_devices_list(device_t *dev)
136{
137 fibril_mutex_lock(&devices_mutex);
138 list_remove(&dev->link);
139 fibril_mutex_unlock(&devices_mutex);
140}
141
142static device_t * driver_get_device(link_t *devices, devman_handle_t handle)
143{
144 device_t *dev = NULL;
145
146 fibril_mutex_lock(&devices_mutex);
147 link_t *link = devices->next;
148 while (link != devices) {
149 dev = list_get_instance(link, device_t, link);
150 if (handle == dev->handle) {
151 fibril_mutex_unlock(&devices_mutex);
152 return dev;
153 }
154 link = link->next;
155 }
156 fibril_mutex_unlock(&devices_mutex);
157
158 return NULL;
159}
160
161static void driver_add_device(ipc_callid_t iid, ipc_call_t *icall)
162{
163 char *dev_name = NULL;
164 int res = EOK;
165
166 devman_handle_t dev_handle = IPC_GET_ARG1(*icall);
167 devman_handle_t parent_dev_handle = IPC_GET_ARG2(*icall);
168
169 device_t *dev = create_device();
170 dev->handle = dev_handle;
171
172 async_data_write_accept((void **) &dev_name, true, 0, 0, 0, 0);
173 dev->name = dev_name;
174
175 add_to_devices_list(dev);
176 dev->parent = driver_get_device(&devices, parent_dev_handle);
177
178 res = driver->driver_ops->add_device(dev);
179 if (0 == res) {
180 printf("%s: new device with handle=%" PRIun " was added.\n",
181 driver->name, dev_handle);
182 } else {
183 printf("%s: failed to add a new device with handle = %" PRIun ".\n",
184 driver->name, dev_handle);
185 remove_from_devices_list(dev);
186 delete_device(dev);
187 }
188
189 ipc_answer_0(iid, res);
190}
191
192static void driver_connection_devman(ipc_callid_t iid, ipc_call_t *icall)
193{
194 /* Accept connection */
195 ipc_answer_0(iid, EOK);
196
197 bool cont = true;
198 while (cont) {
199 ipc_call_t call;
200 ipc_callid_t callid = async_get_call(&call);
201
202 switch (IPC_GET_METHOD(call)) {
203 case IPC_M_PHONE_HUNGUP:
204 cont = false;
205 continue;
206 case DRIVER_ADD_DEVICE:
207 driver_add_device(callid, &call);
208 break;
209 default:
210 ipc_answer_0(callid, ENOENT);
211 }
212 }
213}
214
215/**
216 * Generic client connection handler both for applications and drivers.
217 *
218 * @param drv True for driver client, false for other clients
219 * (applications, services etc.).
220 */
221static void driver_connection_gen(ipc_callid_t iid, ipc_call_t *icall, bool drv)
222{
223 /*
224 * Answer the first IPC_M_CONNECT_ME_TO call and remember the handle of
225 * the device to which the client connected.
226 */
227 devman_handle_t handle = IPC_GET_ARG2(*icall);
228 device_t *dev = driver_get_device(&devices, handle);
229
230 if (dev == NULL) {
231 printf("%s: driver_connection_gen error - no device with handle"
232 " %" PRIun " was found.\n", driver->name, handle);
233 ipc_answer_0(iid, ENOENT);
234 return;
235 }
236
237
238 /*
239 * TODO - if the client is not a driver, check whether it is allowed to
240 * use the device.
241 */
242
243 int ret = EOK;
244 /* open the device */
245 if (NULL != dev->ops && NULL != dev->ops->open)
246 ret = (*dev->ops->open)(dev);
247
248 ipc_answer_0(iid, ret);
249 if (EOK != ret)
250 return;
251
252 while (1) {
253 ipc_callid_t callid;
254 ipc_call_t call;
255 callid = async_get_call(&call);
256 ipcarg_t method = IPC_GET_METHOD(call);
257 int iface_idx;
258
259 switch (method) {
260 case IPC_M_PHONE_HUNGUP:
261 /* close the device */
262 if (NULL != dev->ops && NULL != dev->ops->close)
263 (*dev->ops->close)(dev);
264 ipc_answer_0(callid, EOK);
265 return;
266 default:
267 /* convert ipc interface id to interface index */
268
269 iface_idx = DEV_IFACE_IDX(method);
270
271 if (!is_valid_iface_idx(iface_idx)) {
272 remote_handler_t *default_handler =
273 device_get_default_handler(dev);
274 if (NULL != default_handler) {
275 (*default_handler)(dev, callid, &call);
276 break;
277 }
278 /*
279 * This is not device's interface and the
280 * default handler is not provided.
281 */
282 printf("%s: driver_connection_gen error - "
283 "invalid interface id %d.",
284 driver->name, iface_idx);
285 ipc_answer_0(callid, ENOTSUP);
286 break;
287 }
288
289 /* calling one of the device's interfaces */
290
291 /* get the device interface structure */
292 void *iface = device_get_iface(dev, iface_idx);
293 if (NULL == iface) {
294 printf("%s: driver_connection_gen error - ",
295 driver->name);
296 printf("device with handle %" PRIun " has no interface "
297 "with id %d.\n", handle, iface_idx);
298 ipc_answer_0(callid, ENOTSUP);
299 break;
300 }
301
302 /*
303 * Get the corresponding interface for remote request
304 * handling ("remote interface").
305 */
306 remote_iface_t* rem_iface = get_remote_iface(iface_idx);
307 assert(NULL != rem_iface);
308
309 /* get the method of the remote interface */
310 ipcarg_t iface_method_idx = IPC_GET_ARG1(call);
311 remote_iface_func_ptr_t iface_method_ptr =
312 get_remote_method(rem_iface, iface_method_idx);
313 if (NULL == iface_method_ptr) {
314 // the interface has not such method
315 printf("%s: driver_connection_gen error - "
316 "invalid interface method.", driver->name);
317 ipc_answer_0(callid, ENOTSUP);
318 break;
319 }
320
321 /*
322 * Call the remote interface's method, which will
323 * receive parameters from the remote client and it will
324 * pass it to the corresponding local interface method
325 * associated with the device by its driver.
326 */
327 (*iface_method_ptr)(dev, iface, callid, &call);
328 break;
329 }
330 }
331}
332
333static void driver_connection_driver(ipc_callid_t iid, ipc_call_t *icall)
334{
335 driver_connection_gen(iid, icall, true);
336}
337
338static void driver_connection_client(ipc_callid_t iid, ipc_call_t *icall)
339{
340 driver_connection_gen(iid, icall, false);
341}
342
343
344/** Function for handling connections to device driver. */
345static void driver_connection(ipc_callid_t iid, ipc_call_t *icall)
346{
347 /* Select interface */
348 switch ((ipcarg_t) (IPC_GET_ARG1(*icall))) {
349 case DRIVER_DEVMAN:
350 /* handle PnP events from device manager */
351 driver_connection_devman(iid, icall);
352 break;
353 case DRIVER_DRIVER:
354 /* handle request from drivers of child devices */
355 driver_connection_driver(iid, icall);
356 break;
357 case DRIVER_CLIENT:
358 /* handle requests from client applications */
359 driver_connection_client(iid, icall);
360 break;
361
362 default:
363 /* No such interface */
364 ipc_answer_0(iid, ENOENT);
365 }
366}
367
368int child_device_register(device_t *child, device_t *parent)
369{
370 assert(NULL != child->name);
371
372 int res;
373
374 add_to_devices_list(child);
375 res = devman_child_device_register(child->name, &child->match_ids,
376 parent->handle, &child->handle);
377 if (EOK == res)
378 return res;
379 remove_from_devices_list(child);
380 return res;
381}
382
383/** Wrapper for child_device_register for devices with single match id.
384 *
385 * @param parent Parent device.
386 * @param child_name Child device name.
387 * @param child_match_id Child device match id.
388 * @param child_match_score Child device match score.
389 * @return Error code.
390 */
391int child_device_register_wrapper(device_t *parent, const char *child_name,
392 const char *child_match_id, int child_match_score)
393{
394 device_t *child = NULL;
395 match_id_t *match_id = NULL;
396 int rc;
397
398 child = create_device();
399 if (child == NULL) {
400 rc = ENOMEM;
401 goto failure;
402 }
403
404 child->name = child_name;
405
406 match_id = create_match_id();
407 if (match_id == NULL) {
408 rc = ENOMEM;
409 goto failure;
410 }
411
412 match_id->id = child_match_id;
413 match_id->score = child_match_score;
414 add_match_id(&child->match_ids, match_id);
415
416 rc = child_device_register(child, parent);
417 if (EOK != rc)
418 goto failure;
419
420 return EOK;
421
422failure:
423 if (match_id != NULL) {
424 match_id->id = NULL;
425 delete_match_id(match_id);
426 }
427
428 if (child != NULL) {
429 child->name = NULL;
430 delete_device(child);
431 }
432
433 return rc;
434}
435
436int driver_main(driver_t *drv)
437{
438 /*
439 * Remember the driver structure - driver_ops will be called by generic
440 * handler for incoming connections.
441 */
442 driver = drv;
443
444 /* Initialize the list of interrupt contexts. */
445 init_interrupt_context_list(&interrupt_contexts);
446
447 /* Set generic interrupt handler. */
448 async_set_interrupt_received(driver_irq_handler);
449
450 /*
451 * Register driver by device manager with generic handler for incoming
452 * connections.
453 */
454 devman_driver_register(driver->name, driver_connection);
455
456 async_manager();
457
458 /* Never reached. */
459 return 0;
460}
461
462/**
463 * @}
464 */
Note: See TracBrowser for help on using the repository browser.