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

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

Set connection handler for callback session from devman to driver
(driver→sess). Now devman does not need to create a new connection
every time it actually has some request.

  • Property mode set to 100644
File size: 17.3 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 interrupt_context_t *ctx;
142
143 fibril_mutex_lock(&list->mutex);
144
145 list_foreach(list->contexts, link) {
146 ctx = list_get_instance(link, interrupt_context_t, link);
147 if (ctx->id == id) {
148 fibril_mutex_unlock(&list->mutex);
149 return ctx;
150 }
151 }
152
153 fibril_mutex_unlock(&list->mutex);
154 return NULL;
155}
156
157interrupt_context_t *
158find_interrupt_context(interrupt_context_list_t *list, ddf_dev_t *dev, int irq)
159{
160 interrupt_context_t *ctx;
161
162 fibril_mutex_lock(&list->mutex);
163
164 list_foreach(list->contexts, link) {
165 ctx = list_get_instance(link, interrupt_context_t, link);
166 if (ctx->irq == irq && ctx->dev == dev) {
167 fibril_mutex_unlock(&list->mutex);
168 return ctx;
169 }
170 }
171
172 fibril_mutex_unlock(&list->mutex);
173 return NULL;
174}
175
176
177int
178register_interrupt_handler(ddf_dev_t *dev, int irq, interrupt_handler_t *handler,
179 irq_code_t *pseudocode)
180{
181 interrupt_context_t *ctx = create_interrupt_context();
182
183 ctx->dev = dev;
184 ctx->irq = irq;
185 ctx->handler = handler;
186
187 add_interrupt_context(&interrupt_contexts, ctx);
188
189 if (pseudocode == NULL)
190 pseudocode = &default_pseudocode;
191
192 int res = register_irq(irq, dev->handle, ctx->id, pseudocode);
193 if (res != EOK) {
194 remove_interrupt_context(&interrupt_contexts, ctx);
195 delete_interrupt_context(ctx);
196 }
197
198 return res;
199}
200
201int unregister_interrupt_handler(ddf_dev_t *dev, int irq)
202{
203 interrupt_context_t *ctx = find_interrupt_context(&interrupt_contexts,
204 dev, irq);
205 int res = unregister_irq(irq, dev->handle);
206
207 if (ctx != NULL) {
208 remove_interrupt_context(&interrupt_contexts, ctx);
209 delete_interrupt_context(ctx);
210 }
211
212 return res;
213}
214
215static void add_to_functions_list(ddf_fun_t *fun)
216{
217 fibril_mutex_lock(&functions_mutex);
218 list_append(&fun->link, &functions);
219 fibril_mutex_unlock(&functions_mutex);
220}
221
222static void remove_from_functions_list(ddf_fun_t *fun)
223{
224 fibril_mutex_lock(&functions_mutex);
225 list_remove(&fun->link);
226 fibril_mutex_unlock(&functions_mutex);
227}
228
229static ddf_fun_t *driver_get_function(list_t *functions, devman_handle_t handle)
230{
231 ddf_fun_t *fun = NULL;
232
233 fibril_mutex_lock(&functions_mutex);
234
235 list_foreach(*functions, link) {
236 fun = list_get_instance(link, ddf_fun_t, link);
237 if (fun->handle == handle) {
238 fibril_mutex_unlock(&functions_mutex);
239 return fun;
240 }
241 }
242
243 fibril_mutex_unlock(&functions_mutex);
244
245 return NULL;
246}
247
248static void driver_add_device(ipc_callid_t iid, ipc_call_t *icall)
249{
250 char *dev_name = NULL;
251 int res;
252
253 devman_handle_t dev_handle = IPC_GET_ARG1(*icall);
254 devman_handle_t parent_fun_handle = IPC_GET_ARG2(*icall);
255
256 ddf_dev_t *dev = create_device();
257 dev->handle = dev_handle;
258
259 async_data_write_accept((void **) &dev_name, true, 0, 0, 0, 0);
260 dev->name = dev_name;
261
262 /*
263 * Currently not used, parent fun handle is stored in context
264 * of the connection to the parent device driver.
265 */
266 (void) parent_fun_handle;
267
268 res = driver->driver_ops->add_device(dev);
269 if (res != EOK)
270 delete_device(dev);
271
272 async_answer_0(iid, res);
273}
274
275static void driver_connection_devman(ipc_callid_t iid, ipc_call_t *icall)
276{
277 /* Accept connection */
278 async_answer_0(iid, EOK);
279
280 while (true) {
281 ipc_call_t call;
282 ipc_callid_t callid = async_get_call(&call);
283
284 if (!IPC_GET_IMETHOD(call))
285 break;
286
287 switch (IPC_GET_IMETHOD(call)) {
288 case DRIVER_ADD_DEVICE:
289 driver_add_device(callid, &call);
290 break;
291 default:
292 async_answer_0(callid, ENOENT);
293 }
294 }
295}
296
297/** Generic client connection handler both for applications and drivers.
298 *
299 * @param drv True for driver client, false for other clients
300 * (applications, services, etc.).
301 *
302 */
303static void driver_connection_gen(ipc_callid_t iid, ipc_call_t *icall, bool drv)
304{
305 /*
306 * Answer the first IPC_M_CONNECT_ME_TO call and remember the handle of
307 * the device to which the client connected.
308 */
309 devman_handle_t handle = IPC_GET_ARG2(*icall);
310 ddf_fun_t *fun = driver_get_function(&functions, handle);
311
312 if (fun == NULL) {
313 printf("%s: driver_connection_gen error - no function with handle"
314 " %" PRIun " was found.\n", driver->name, handle);
315 async_answer_0(iid, ENOENT);
316 return;
317 }
318
319 if (fun->conn_handler != NULL) {
320 /* Driver has a custom connection handler. */
321 (*fun->conn_handler)(iid, icall, (void *)fun);
322 return;
323 }
324
325 /*
326 * TODO - if the client is not a driver, check whether it is allowed to
327 * use the device.
328 */
329
330 int ret = EOK;
331 /* Open device function */
332 if (fun->ops != NULL && fun->ops->open != NULL)
333 ret = (*fun->ops->open)(fun);
334
335 async_answer_0(iid, ret);
336 if (ret != EOK)
337 return;
338
339 while (true) {
340 ipc_callid_t callid;
341 ipc_call_t call;
342 callid = async_get_call(&call);
343 sysarg_t method = IPC_GET_IMETHOD(call);
344
345 if (!method) {
346 /* Close device function */
347 if (fun->ops != NULL && fun->ops->close != NULL)
348 (*fun->ops->close)(fun);
349 async_answer_0(callid, EOK);
350 return;
351 }
352
353 /* Convert ipc interface id to interface index */
354
355 int iface_idx = DEV_IFACE_IDX(method);
356
357 if (!is_valid_iface_idx(iface_idx)) {
358 remote_handler_t *default_handler =
359 function_get_default_handler(fun);
360 if (default_handler != NULL) {
361 (*default_handler)(fun, callid, &call);
362 continue;
363 }
364
365 /*
366 * Function has no such interface and
367 * default handler is not provided.
368 */
369 printf("%s: driver_connection_gen error - "
370 "invalid interface id %d.",
371 driver->name, iface_idx);
372 async_answer_0(callid, ENOTSUP);
373 continue;
374 }
375
376 /* Calling one of the function's interfaces */
377
378 /* Get the interface ops structure. */
379 void *ops = function_get_ops(fun, iface_idx);
380 if (ops == NULL) {
381 printf("%s: driver_connection_gen error - ", driver->name);
382 printf("Function with handle %" PRIun " has no interface "
383 "with id %d.\n", handle, iface_idx);
384 async_answer_0(callid, ENOTSUP);
385 continue;
386 }
387
388 /*
389 * Get the corresponding interface for remote request
390 * handling ("remote interface").
391 */
392 remote_iface_t *rem_iface = get_remote_iface(iface_idx);
393 assert(rem_iface != NULL);
394
395 /* get the method of the remote interface */
396 sysarg_t iface_method_idx = IPC_GET_ARG1(call);
397 remote_iface_func_ptr_t iface_method_ptr =
398 get_remote_method(rem_iface, iface_method_idx);
399 if (iface_method_ptr == NULL) {
400 /* The interface has not such method */
401 printf("%s: driver_connection_gen error - "
402 "invalid interface method.", driver->name);
403 async_answer_0(callid, ENOTSUP);
404 continue;
405 }
406
407 /*
408 * Call the remote interface's method, which will
409 * receive parameters from the remote client and it will
410 * pass it to the corresponding local interface method
411 * associated with the function by its driver.
412 */
413 (*iface_method_ptr)(fun, ops, callid, &call);
414 }
415}
416
417static void driver_connection_driver(ipc_callid_t iid, ipc_call_t *icall)
418{
419 driver_connection_gen(iid, icall, true);
420}
421
422static void driver_connection_client(ipc_callid_t iid, ipc_call_t *icall)
423{
424 driver_connection_gen(iid, icall, false);
425}
426
427/** Function for handling connections to device driver. */
428static void driver_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
429{
430 sysarg_t conn_type;
431
432 if (iid == 0) {
433 /* Callback connection from devman */
434 /* XXX Use separate handler for this type of connection */
435 conn_type = DRIVER_DEVMAN;
436 } else {
437 conn_type = IPC_GET_ARG1(*icall);
438 }
439
440 /* Select interface */
441 switch (conn_type) {
442 case DRIVER_DEVMAN:
443 /* Handle request from device manager */
444 driver_connection_devman(iid, icall);
445 break;
446 case DRIVER_DRIVER:
447 /* Handle request from drivers of child devices */
448 driver_connection_driver(iid, icall);
449 break;
450 case DRIVER_CLIENT:
451 /* Handle request from client applications */
452 driver_connection_client(iid, icall);
453 break;
454 default:
455 /* No such interface */
456 async_answer_0(iid, ENOENT);
457 }
458}
459
460/** Create new device structure.
461 *
462 * @return The device structure.
463 */
464static ddf_dev_t *create_device(void)
465{
466 ddf_dev_t *dev;
467
468 dev = malloc(sizeof(ddf_dev_t));
469 if (dev == NULL)
470 return NULL;
471
472 memset(dev, 0, sizeof(ddf_dev_t));
473 return dev;
474}
475
476/** Create new function structure.
477 *
478 * @return The device structure.
479 */
480static ddf_fun_t *create_function(void)
481{
482 ddf_fun_t *fun;
483
484 fun = calloc(1, sizeof(ddf_fun_t));
485 if (fun == NULL)
486 return NULL;
487
488 init_match_ids(&fun->match_ids);
489 link_initialize(&fun->link);
490
491 return fun;
492}
493
494/** Delete device structure.
495 *
496 * @param dev The device structure.
497 */
498static void delete_device(ddf_dev_t *dev)
499{
500 free(dev);
501}
502
503/** Delete device structure.
504 *
505 * @param dev The device structure.
506 */
507static void delete_function(ddf_fun_t *fun)
508{
509 clean_match_ids(&fun->match_ids);
510 if (fun->name != NULL)
511 free(fun->name);
512 free(fun);
513}
514
515/** Create a DDF function node.
516 *
517 * Create a DDF function (in memory). Both child devices and external clients
518 * communicate with a device via its functions.
519 *
520 * The created function node is fully formed, but only exists in the memory
521 * of the client task. In order to be visible to the system, the function
522 * must be bound using ddf_fun_bind().
523 *
524 * This function should only fail if there is not enough free memory.
525 * Specifically, this function succeeds even if @a dev already has
526 * a (bound) function with the same name.
527 *
528 * Type: A function of type fun_inner indicates that DDF should attempt
529 * to attach child devices to the function. fun_exposed means that
530 * the function should be exported to external clients (applications).
531 *
532 * @param dev Device to which we are adding function
533 * @param ftype Type of function (fun_inner or fun_exposed)
534 * @param name Name of function
535 *
536 * @return New function or @c NULL if memory is not available
537 */
538ddf_fun_t *ddf_fun_create(ddf_dev_t *dev, fun_type_t ftype, const char *name)
539{
540 ddf_fun_t *fun;
541
542 fun = create_function();
543 if (fun == NULL)
544 return NULL;
545
546 fun->bound = false;
547 fun->dev = dev;
548 fun->ftype = ftype;
549
550 fun->name = str_dup(name);
551 if (fun->name == NULL) {
552 delete_function(fun);
553 return NULL;
554 }
555
556 return fun;
557}
558
559/** Destroy DDF function node.
560 *
561 * Destroy a function previously created with ddf_fun_create(). The function
562 * must not be bound.
563 *
564 * @param fun Function to destroy
565 */
566void ddf_fun_destroy(ddf_fun_t *fun)
567{
568 assert(fun->bound == false);
569 delete_function(fun);
570}
571
572static void *function_get_ops(ddf_fun_t *fun, dev_inferface_idx_t idx)
573{
574 assert(is_valid_iface_idx(idx));
575 if (fun->ops == NULL)
576 return NULL;
577 return fun->ops->interfaces[idx];
578}
579
580/** Bind a function node.
581 *
582 * Bind the specified function to the system. This effectively makes
583 * the function visible to the system (uploads it to the server).
584 *
585 * This function can fail for several reasons. Specifically,
586 * it will fail if the device already has a bound function of
587 * the same name.
588 *
589 * @param fun Function to bind
590 * @return EOK on success or negative error code
591 */
592int ddf_fun_bind(ddf_fun_t *fun)
593{
594 assert(fun->bound == false);
595 assert(fun->name != NULL);
596
597 int res;
598
599 add_to_functions_list(fun);
600 res = devman_add_function(fun->name, fun->ftype, &fun->match_ids,
601 fun->dev->handle, &fun->handle);
602 if (res != EOK) {
603 remove_from_functions_list(fun);
604 return res;
605 }
606
607 fun->bound = true;
608 return res;
609}
610
611/** Unbind a function node.
612 *
613 * Unbind the specified function from the system. This effectively makes
614 * the function invisible to the system.
615 *
616 * @param fun Function to bind
617 * @return EOK on success or negative error code
618 */
619int ddf_fun_unbind(ddf_fun_t *fun)
620{
621 int res;
622
623 assert(fun->bound == true);
624
625 add_to_functions_list(fun);
626 res = devman_remove_function(fun->handle);
627 if (res != EOK)
628 return res;
629
630 remove_from_functions_list(fun);
631
632 fun->bound = false;
633 return EOK;
634}
635
636/** Add single match ID to inner function.
637 *
638 * Construct and add a single match ID to the specified function.
639 * Cannot be called when the function node is bound.
640 *
641 * @param fun Function
642 * @param match_id_str Match string
643 * @param match_score Match score
644 * @return EOK on success, ENOMEM if out of memory.
645 */
646int ddf_fun_add_match_id(ddf_fun_t *fun, const char *match_id_str,
647 int match_score)
648{
649 match_id_t *match_id;
650
651 assert(fun->bound == false);
652 assert(fun->ftype == fun_inner);
653
654 match_id = create_match_id();
655 if (match_id == NULL)
656 return ENOMEM;
657
658 match_id->id = str_dup(match_id_str);
659 match_id->score = 90;
660
661 add_match_id(&fun->match_ids, match_id);
662 return EOK;
663}
664
665/** Get default handler for client requests */
666static remote_handler_t *function_get_default_handler(ddf_fun_t *fun)
667{
668 if (fun->ops == NULL)
669 return NULL;
670 return fun->ops->default_handler;
671}
672
673/** Add exposed function to category.
674 *
675 * Must only be called when the function is bound.
676 */
677int ddf_fun_add_to_category(ddf_fun_t *fun, const char *cat_name)
678{
679 assert(fun->bound == true);
680 assert(fun->ftype == fun_exposed);
681
682 return devman_add_device_to_category(fun->handle, cat_name);
683}
684
685int ddf_driver_main(driver_t *drv)
686{
687 int rc;
688
689 /*
690 * Remember the driver structure - driver_ops will be called by generic
691 * handler for incoming connections.
692 */
693 driver = drv;
694
695 /* Initialize the list of interrupt contexts. */
696 init_interrupt_context_list(&interrupt_contexts);
697
698 /* Set generic interrupt handler. */
699 async_set_interrupt_received(driver_irq_handler);
700
701 /*
702 * Register driver with device manager using generic handler for
703 * incoming connections.
704 */
705 rc = devman_driver_register(driver->name, driver_connection);
706 if (rc != EOK) {
707 printf("Error: Failed to register driver with device manager "
708 "(%s).\n", (rc == EEXISTS) ? "driver already started" :
709 str_error(rc));
710
711 return 1;
712 }
713
714 /* Return success from the task since server has started. */
715 rc = task_retval(0);
716 if (rc != EOK)
717 return 1;
718
719 async_manager();
720
721 /* Never reached. */
722 return 0;
723}
724
725/**
726 * @}
727 */
Note: See TracBrowser for help on using the repository browser.