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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 9934f7d was 9934f7d, checked in by Jiri Svoboda <jiri@…>, 14 years ago

Add extra argument to async connection handlers that can be used for passing
information from async_connect_to_me() to the handler.

  • Property mode set to 100644
File size: 16.6 KB
Line 
1/*
2 * Copyright (c) 2010 Lenka Trochtova
3 * Copyright (c) 2011 Jiri Svoboda
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/**
31 * @defgroup libdrv generic device driver support.
32 * @brief HelenOS generic device driver support.
33 * @{
34 */
35
36/** @file
37 */
38
39#include <assert.h>
40#include <ipc/services.h>
41#include <ipc/ns.h>
42#include <async.h>
43#include <stdio.h>
44#include <errno.h>
45#include <bool.h>
46#include <fibril_synch.h>
47#include <stdlib.h>
48#include <str.h>
49#include <str_error.h>
50#include <ctype.h>
51#include <errno.h>
52#include <inttypes.h>
53#include <devman.h>
54
55#include <ipc/driver.h>
56
57#include "dev_iface.h"
58#include "ddf/driver.h"
59#include "ddf/interrupt.h"
60
61/** Driver structure */
62static driver_t *driver;
63
64/** Devices */
65LIST_INITIALIZE(functions);
66FIBRIL_MUTEX_INITIALIZE(functions_mutex);
67
68/** Interrupts */
69static interrupt_context_list_t interrupt_contexts;
70
71static irq_cmd_t default_cmds[] = {
72 {
73 .cmd = CMD_ACCEPT
74 }
75};
76
77static irq_code_t default_pseudocode = {
78 sizeof(default_cmds) / sizeof(irq_cmd_t),
79 default_cmds
80};
81
82static ddf_dev_t *create_device(void);
83static void delete_device(ddf_dev_t *);
84static remote_handler_t *function_get_default_handler(ddf_fun_t *);
85static void *function_get_ops(ddf_fun_t *, dev_inferface_idx_t);
86
87static void driver_irq_handler(ipc_callid_t iid, ipc_call_t *icall)
88{
89 int id = (int)IPC_GET_IMETHOD(*icall);
90 interrupt_context_t *ctx;
91
92 ctx = find_interrupt_context_by_id(&interrupt_contexts, id);
93 if (ctx != NULL && ctx->handler != NULL)
94 (*ctx->handler)(ctx->dev, iid, icall);
95}
96
97interrupt_context_t *create_interrupt_context(void)
98{
99 interrupt_context_t *ctx;
100
101 ctx = (interrupt_context_t *) malloc(sizeof(interrupt_context_t));
102 if (ctx != NULL)
103 memset(ctx, 0, sizeof(interrupt_context_t));
104
105 return ctx;
106}
107
108void delete_interrupt_context(interrupt_context_t *ctx)
109{
110 if (ctx != NULL)
111 free(ctx);
112}
113
114void init_interrupt_context_list(interrupt_context_list_t *list)
115{
116 memset(list, 0, sizeof(interrupt_context_list_t));
117 fibril_mutex_initialize(&list->mutex);
118 list_initialize(&list->contexts);
119}
120
121void
122add_interrupt_context(interrupt_context_list_t *list, interrupt_context_t *ctx)
123{
124 fibril_mutex_lock(&list->mutex);
125 ctx->id = list->curr_id++;
126 list_append(&ctx->link, &list->contexts);
127 fibril_mutex_unlock(&list->mutex);
128}
129
130void remove_interrupt_context(interrupt_context_list_t *list,
131 interrupt_context_t *ctx)
132{
133 fibril_mutex_lock(&list->mutex);
134 list_remove(&ctx->link);
135 fibril_mutex_unlock(&list->mutex);
136}
137
138interrupt_context_t *
139find_interrupt_context_by_id(interrupt_context_list_t *list, int id)
140{
141 fibril_mutex_lock(&list->mutex);
142
143 link_t *link = list->contexts.next;
144 interrupt_context_t *ctx;
145
146 while (link != &list->contexts) {
147 ctx = list_get_instance(link, interrupt_context_t, link);
148 if (ctx->id == id) {
149 fibril_mutex_unlock(&list->mutex);
150 return ctx;
151 }
152 link = link->next;
153 }
154
155 fibril_mutex_unlock(&list->mutex);
156 return NULL;
157}
158
159interrupt_context_t *
160find_interrupt_context(interrupt_context_list_t *list, ddf_dev_t *dev, int irq)
161{
162 fibril_mutex_lock(&list->mutex);
163
164 link_t *link = list->contexts.next;
165 interrupt_context_t *ctx;
166
167 while (link != &list->contexts) {
168 ctx = list_get_instance(link, interrupt_context_t, link);
169 if (ctx->irq == irq && ctx->dev == dev) {
170 fibril_mutex_unlock(&list->mutex);
171 return ctx;
172 }
173 link = link->next;
174 }
175
176 fibril_mutex_unlock(&list->mutex);
177 return NULL;
178}
179
180
181int
182register_interrupt_handler(ddf_dev_t *dev, int irq, interrupt_handler_t *handler,
183 irq_code_t *pseudocode)
184{
185 interrupt_context_t *ctx = create_interrupt_context();
186
187 ctx->dev = dev;
188 ctx->irq = irq;
189 ctx->handler = handler;
190
191 add_interrupt_context(&interrupt_contexts, ctx);
192
193 if (pseudocode == NULL)
194 pseudocode = &default_pseudocode;
195
196 int res = register_irq(irq, dev->handle, ctx->id, pseudocode);
197 if (res != EOK) {
198 remove_interrupt_context(&interrupt_contexts, ctx);
199 delete_interrupt_context(ctx);
200 }
201
202 return res;
203}
204
205int unregister_interrupt_handler(ddf_dev_t *dev, int irq)
206{
207 interrupt_context_t *ctx = find_interrupt_context(&interrupt_contexts,
208 dev, irq);
209 int res = unregister_irq(irq, dev->handle);
210
211 if (ctx != NULL) {
212 remove_interrupt_context(&interrupt_contexts, ctx);
213 delete_interrupt_context(ctx);
214 }
215
216 return res;
217}
218
219static void add_to_functions_list(ddf_fun_t *fun)
220{
221 fibril_mutex_lock(&functions_mutex);
222 list_append(&fun->link, &functions);
223 fibril_mutex_unlock(&functions_mutex);
224}
225
226static void remove_from_functions_list(ddf_fun_t *fun)
227{
228 fibril_mutex_lock(&functions_mutex);
229 list_remove(&fun->link);
230 fibril_mutex_unlock(&functions_mutex);
231}
232
233static ddf_fun_t *driver_get_function(link_t *functions, devman_handle_t handle)
234{
235 ddf_fun_t *fun = NULL;
236
237 fibril_mutex_lock(&functions_mutex);
238 link_t *link = functions->next;
239
240 while (link != functions) {
241 fun = list_get_instance(link, ddf_fun_t, link);
242 if (fun->handle == handle) {
243 fibril_mutex_unlock(&functions_mutex);
244 return fun;
245 }
246
247 link = link->next;
248 }
249
250 fibril_mutex_unlock(&functions_mutex);
251
252 return NULL;
253}
254
255static void driver_add_device(ipc_callid_t iid, ipc_call_t *icall)
256{
257 char *dev_name = NULL;
258 int res;
259
260 devman_handle_t dev_handle = IPC_GET_ARG1(*icall);
261 devman_handle_t parent_fun_handle = IPC_GET_ARG2(*icall);
262
263 ddf_dev_t *dev = create_device();
264 dev->handle = dev_handle;
265
266 async_data_write_accept((void **) &dev_name, true, 0, 0, 0, 0);
267 dev->name = dev_name;
268
269 /*
270 * Currently not used, parent fun handle is stored in context
271 * of the connection to the parent device driver.
272 */
273 (void) parent_fun_handle;
274
275 res = driver->driver_ops->add_device(dev);
276 if (res != EOK)
277 delete_device(dev);
278
279 async_answer_0(iid, res);
280}
281
282static void driver_connection_devman(ipc_callid_t iid, ipc_call_t *icall)
283{
284 /* Accept connection */
285 async_answer_0(iid, EOK);
286
287 while (true) {
288 ipc_call_t call;
289 ipc_callid_t callid = async_get_call(&call);
290
291 if (!IPC_GET_IMETHOD(call))
292 break;
293
294 switch (IPC_GET_IMETHOD(call)) {
295 case DRIVER_ADD_DEVICE:
296 driver_add_device(callid, &call);
297 break;
298 default:
299 async_answer_0(callid, ENOENT);
300 }
301 }
302}
303
304/** Generic client connection handler both for applications and drivers.
305 *
306 * @param drv True for driver client, false for other clients
307 * (applications, services, etc.).
308 *
309 */
310static void driver_connection_gen(ipc_callid_t iid, ipc_call_t *icall, bool drv)
311{
312 /*
313 * Answer the first IPC_M_CONNECT_ME_TO call and remember the handle of
314 * the device to which the client connected.
315 */
316 devman_handle_t handle = IPC_GET_ARG2(*icall);
317 ddf_fun_t *fun = driver_get_function(&functions, handle);
318
319 if (fun == NULL) {
320 printf("%s: driver_connection_gen error - no function with handle"
321 " %" PRIun " was found.\n", driver->name, handle);
322 async_answer_0(iid, ENOENT);
323 return;
324 }
325
326 /*
327 * TODO - if the client is not a driver, check whether it is allowed to
328 * use the device.
329 */
330
331 int ret = EOK;
332 /* Open device function */
333 if (fun->ops != NULL && fun->ops->open != NULL)
334 ret = (*fun->ops->open)(fun);
335
336 async_answer_0(iid, ret);
337 if (ret != EOK)
338 return;
339
340 while (true) {
341 ipc_callid_t callid;
342 ipc_call_t call;
343 callid = async_get_call(&call);
344 sysarg_t method = IPC_GET_IMETHOD(call);
345
346 if (!method) {
347 /* Close device function */
348 if (fun->ops != NULL && fun->ops->close != NULL)
349 (*fun->ops->close)(fun);
350 async_answer_0(callid, EOK);
351 return;
352 }
353
354 /* Convert ipc interface id to interface index */
355
356 int iface_idx = DEV_IFACE_IDX(method);
357
358 if (!is_valid_iface_idx(iface_idx)) {
359 remote_handler_t *default_handler =
360 function_get_default_handler(fun);
361 if (default_handler != NULL) {
362 (*default_handler)(fun, callid, &call);
363 continue;
364 }
365
366 /*
367 * Function has no such interface and
368 * default handler is not provided.
369 */
370 printf("%s: driver_connection_gen error - "
371 "invalid interface id %d.",
372 driver->name, iface_idx);
373 async_answer_0(callid, ENOTSUP);
374 continue;
375 }
376
377 /* Calling one of the function's interfaces */
378
379 /* Get the interface ops structure. */
380 void *ops = function_get_ops(fun, iface_idx);
381 if (ops == NULL) {
382 printf("%s: driver_connection_gen error - ", driver->name);
383 printf("Function with handle %" PRIun " has no interface "
384 "with id %d.\n", handle, iface_idx);
385 async_answer_0(callid, ENOTSUP);
386 continue;
387 }
388
389 /*
390 * Get the corresponding interface for remote request
391 * handling ("remote interface").
392 */
393 remote_iface_t *rem_iface = get_remote_iface(iface_idx);
394 assert(rem_iface != NULL);
395
396 /* get the method of the remote interface */
397 sysarg_t iface_method_idx = IPC_GET_ARG1(call);
398 remote_iface_func_ptr_t iface_method_ptr =
399 get_remote_method(rem_iface, iface_method_idx);
400 if (iface_method_ptr == NULL) {
401 /* The interface has not such method */
402 printf("%s: driver_connection_gen error - "
403 "invalid interface method.", driver->name);
404 async_answer_0(callid, ENOTSUP);
405 continue;
406 }
407
408 /*
409 * Call the remote interface's method, which will
410 * receive parameters from the remote client and it will
411 * pass it to the corresponding local interface method
412 * associated with the function by its driver.
413 */
414 (*iface_method_ptr)(fun, ops, callid, &call);
415 }
416}
417
418static void driver_connection_driver(ipc_callid_t iid, ipc_call_t *icall)
419{
420 driver_connection_gen(iid, icall, true);
421}
422
423static void driver_connection_client(ipc_callid_t iid, ipc_call_t *icall)
424{
425 driver_connection_gen(iid, icall, false);
426}
427
428/** Function for handling connections to device driver. */
429static void driver_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
430{
431 /* Select interface */
432 switch ((sysarg_t) (IPC_GET_ARG1(*icall))) {
433 case DRIVER_DEVMAN:
434 /* Handle request from device manager */
435 driver_connection_devman(iid, icall);
436 break;
437 case DRIVER_DRIVER:
438 /* Handle request from drivers of child devices */
439 driver_connection_driver(iid, icall);
440 break;
441 case DRIVER_CLIENT:
442 /* Handle request from client applications */
443 driver_connection_client(iid, icall);
444 break;
445 default:
446 /* No such interface */
447 async_answer_0(iid, ENOENT);
448 }
449}
450
451/** Create new device structure.
452 *
453 * @return The device structure.
454 */
455static ddf_dev_t *create_device(void)
456{
457 ddf_dev_t *dev;
458
459 dev = malloc(sizeof(ddf_dev_t));
460 if (dev == NULL)
461 return NULL;
462
463 memset(dev, 0, sizeof(ddf_dev_t));
464 return dev;
465}
466
467/** Create new function structure.
468 *
469 * @return The device structure.
470 */
471static ddf_fun_t *create_function(void)
472{
473 ddf_fun_t *fun;
474
475 fun = calloc(1, sizeof(ddf_fun_t));
476 if (fun == NULL)
477 return NULL;
478
479 init_match_ids(&fun->match_ids);
480 link_initialize(&fun->link);
481
482 return fun;
483}
484
485/** Delete device structure.
486 *
487 * @param dev The device structure.
488 */
489static void delete_device(ddf_dev_t *dev)
490{
491 free(dev);
492}
493
494/** Delete device structure.
495 *
496 * @param dev The device structure.
497 */
498static void delete_function(ddf_fun_t *fun)
499{
500 clean_match_ids(&fun->match_ids);
501 if (fun->name != NULL)
502 free(fun->name);
503 free(fun);
504}
505
506/** Create a DDF function node.
507 *
508 * Create a DDF function (in memory). Both child devices and external clients
509 * communicate with a device via its functions.
510 *
511 * The created function node is fully formed, but only exists in the memory
512 * of the client task. In order to be visible to the system, the function
513 * must be bound using ddf_fun_bind().
514 *
515 * This function should only fail if there is not enough free memory.
516 * Specifically, this function succeeds even if @a dev already has
517 * a (bound) function with the same name.
518 *
519 * Type: A function of type fun_inner indicates that DDF should attempt
520 * to attach child devices to the function. fun_exposed means that
521 * the function should be exported to external clients (applications).
522 *
523 * @param dev Device to which we are adding function
524 * @param ftype Type of function (fun_inner or fun_exposed)
525 * @param name Name of function
526 *
527 * @return New function or @c NULL if memory is not available
528 */
529ddf_fun_t *ddf_fun_create(ddf_dev_t *dev, fun_type_t ftype, const char *name)
530{
531 ddf_fun_t *fun;
532
533 fun = create_function();
534 if (fun == NULL)
535 return NULL;
536
537 fun->bound = false;
538 fun->dev = dev;
539 fun->ftype = ftype;
540
541 fun->name = str_dup(name);
542 if (fun->name == NULL) {
543 delete_function(fun);
544 return NULL;
545 }
546
547 return fun;
548}
549
550/** Destroy DDF function node.
551 *
552 * Destroy a function previously created with ddf_fun_create(). The function
553 * must not be bound.
554 *
555 * @param fun Function to destroy
556 */
557void ddf_fun_destroy(ddf_fun_t *fun)
558{
559 assert(fun->bound == false);
560 delete_function(fun);
561}
562
563static void *function_get_ops(ddf_fun_t *fun, dev_inferface_idx_t idx)
564{
565 assert(is_valid_iface_idx(idx));
566 if (fun->ops == NULL)
567 return NULL;
568 return fun->ops->interfaces[idx];
569}
570
571/** Bind a function node.
572 *
573 * Bind the specified function to the system. This effectively makes
574 * the function visible to the system (uploads it to the server).
575 *
576 * This function can fail for several reasons. Specifically,
577 * it will fail if the device already has a bound function of
578 * the same name.
579 *
580 * @param fun Function to bind
581 * @return EOK on success or negative error code
582 */
583int ddf_fun_bind(ddf_fun_t *fun)
584{
585 assert(fun->name != NULL);
586
587 int res;
588
589 add_to_functions_list(fun);
590 res = devman_add_function(fun->name, fun->ftype, &fun->match_ids,
591 fun->dev->handle, &fun->handle);
592 if (res != EOK) {
593 remove_from_functions_list(fun);
594 return res;
595 }
596
597 fun->bound = true;
598 return res;
599}
600
601/** Add single match ID to inner function.
602 *
603 * Construct and add a single match ID to the specified function.
604 * Cannot be called when the function node is bound.
605 *
606 * @param fun Function
607 * @param match_id_str Match string
608 * @param match_score Match score
609 * @return EOK on success, ENOMEM if out of memory.
610 */
611int ddf_fun_add_match_id(ddf_fun_t *fun, const char *match_id_str,
612 int match_score)
613{
614 match_id_t *match_id;
615
616 assert(fun->bound == false);
617 assert(fun->ftype == fun_inner);
618
619 match_id = create_match_id();
620 if (match_id == NULL)
621 return ENOMEM;
622
623 match_id->id = match_id_str;
624 match_id->score = 90;
625
626 add_match_id(&fun->match_ids, match_id);
627 return EOK;
628}
629
630/** Get default handler for client requests */
631static remote_handler_t *function_get_default_handler(ddf_fun_t *fun)
632{
633 if (fun->ops == NULL)
634 return NULL;
635 return fun->ops->default_handler;
636}
637
638/** Add exposed function to class.
639 *
640 * Must only be called when the function is bound.
641 */
642int ddf_fun_add_to_class(ddf_fun_t *fun, const char *class_name)
643{
644 assert(fun->bound == true);
645 assert(fun->ftype == fun_exposed);
646
647 return devman_add_device_to_class(fun->handle, class_name);
648}
649
650int ddf_driver_main(driver_t *drv)
651{
652 int rc;
653
654 /*
655 * Remember the driver structure - driver_ops will be called by generic
656 * handler for incoming connections.
657 */
658 driver = drv;
659
660 /* Initialize the list of interrupt contexts. */
661 init_interrupt_context_list(&interrupt_contexts);
662
663 /* Set generic interrupt handler. */
664 async_set_interrupt_received(driver_irq_handler);
665
666 /*
667 * Register driver with device manager using generic handler for
668 * incoming connections.
669 */
670 rc = devman_driver_register(driver->name, driver_connection);
671 if (rc != EOK) {
672 printf("Error: Failed to register driver with device manager "
673 "(%s).\n", (rc == EEXISTS) ? "driver already started" :
674 str_error(rc));
675
676 return 1;
677 }
678
679 /* Return success from the task since server has started. */
680 rc = task_retval(0);
681 if (rc != EOK)
682 return 1;
683
684 async_manager();
685
686 /* Never reached. */
687 return 0;
688}
689
690/**
691 * @}
692 */
Note: See TracBrowser for help on using the repository browser.