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

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

Fix comment style.

  • Property mode set to 100644
File size: 16.4 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 <ctype.h>
50#include <errno.h>
51#include <inttypes.h>
52#include <devman.h>
53
54#include <ipc/driver.h>
55
56#include "dev_iface.h"
57#include "ddf/driver.h"
58#include "ddf/interrupt.h"
59
60/** Driver structure */
61static driver_t *driver;
62
63/** Devices */
64LIST_INITIALIZE(functions);
65FIBRIL_MUTEX_INITIALIZE(functions_mutex);
66
67/** Interrupts */
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
81static ddf_dev_t *create_device(void);
82static void delete_device(ddf_dev_t *);
83static remote_handler_t *function_get_default_handler(ddf_fun_t *);
84static void *function_get_ops(ddf_fun_t *, dev_inferface_idx_t);
85
86static void driver_irq_handler(ipc_callid_t iid, ipc_call_t *icall)
87{
88 int id = (int)IPC_GET_IMETHOD(*icall);
89 interrupt_context_t *ctx;
90
91 ctx = find_interrupt_context_by_id(&interrupt_contexts, id);
92 if (ctx != NULL && ctx->handler != NULL)
93 (*ctx->handler)(ctx->dev, iid, icall);
94}
95
96interrupt_context_t *create_interrupt_context(void)
97{
98 interrupt_context_t *ctx;
99
100 ctx = (interrupt_context_t *) malloc(sizeof(interrupt_context_t));
101 if (ctx != NULL)
102 memset(ctx, 0, sizeof(interrupt_context_t));
103
104 return ctx;
105}
106
107void delete_interrupt_context(interrupt_context_t *ctx)
108{
109 if (ctx != NULL)
110 free(ctx);
111}
112
113void init_interrupt_context_list(interrupt_context_list_t *list)
114{
115 memset(list, 0, sizeof(interrupt_context_list_t));
116 fibril_mutex_initialize(&list->mutex);
117 list_initialize(&list->contexts);
118}
119
120void
121add_interrupt_context(interrupt_context_list_t *list, interrupt_context_t *ctx)
122{
123 fibril_mutex_lock(&list->mutex);
124 ctx->id = list->curr_id++;
125 list_append(&ctx->link, &list->contexts);
126 fibril_mutex_unlock(&list->mutex);
127}
128
129void remove_interrupt_context(interrupt_context_list_t *list,
130 interrupt_context_t *ctx)
131{
132 fibril_mutex_lock(&list->mutex);
133 list_remove(&ctx->link);
134 fibril_mutex_unlock(&list->mutex);
135}
136
137interrupt_context_t *
138find_interrupt_context_by_id(interrupt_context_list_t *list, int id)
139{
140 fibril_mutex_lock(&list->mutex);
141
142 link_t *link = list->contexts.next;
143 interrupt_context_t *ctx;
144
145 while (link != &list->contexts) {
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 link = link->next;
152 }
153
154 fibril_mutex_unlock(&list->mutex);
155 return NULL;
156}
157
158interrupt_context_t *
159find_interrupt_context(interrupt_context_list_t *list, ddf_dev_t *dev, int irq)
160{
161 fibril_mutex_lock(&list->mutex);
162
163 link_t *link = list->contexts.next;
164 interrupt_context_t *ctx;
165
166 while (link != &list->contexts) {
167 ctx = list_get_instance(link, interrupt_context_t, link);
168 if (ctx->irq == irq && ctx->dev == dev) {
169 fibril_mutex_unlock(&list->mutex);
170 return ctx;
171 }
172 link = link->next;
173 }
174
175 fibril_mutex_unlock(&list->mutex);
176 return NULL;
177}
178
179
180int
181register_interrupt_handler(ddf_dev_t *dev, int irq, interrupt_handler_t *handler,
182 irq_code_t *pseudocode)
183{
184 interrupt_context_t *ctx = create_interrupt_context();
185
186 ctx->dev = dev;
187 ctx->irq = irq;
188 ctx->handler = handler;
189
190 add_interrupt_context(&interrupt_contexts, ctx);
191
192 if (pseudocode == NULL)
193 pseudocode = &default_pseudocode;
194
195 int res = register_irq(irq, dev->handle, ctx->id, pseudocode);
196 if (res != EOK) {
197 remove_interrupt_context(&interrupt_contexts, ctx);
198 delete_interrupt_context(ctx);
199 }
200
201 return res;
202}
203
204int unregister_interrupt_handler(ddf_dev_t *dev, int irq)
205{
206 interrupt_context_t *ctx = find_interrupt_context(&interrupt_contexts,
207 dev, irq);
208 int res = unregister_irq(irq, dev->handle);
209
210 if (ctx != NULL) {
211 remove_interrupt_context(&interrupt_contexts, ctx);
212 delete_interrupt_context(ctx);
213 }
214
215 return res;
216}
217
218static void add_to_functions_list(ddf_fun_t *fun)
219{
220 fibril_mutex_lock(&functions_mutex);
221 list_append(&fun->link, &functions);
222 fibril_mutex_unlock(&functions_mutex);
223}
224
225static void remove_from_functions_list(ddf_fun_t *fun)
226{
227 fibril_mutex_lock(&functions_mutex);
228 list_remove(&fun->link);
229 fibril_mutex_unlock(&functions_mutex);
230}
231
232static ddf_fun_t *driver_get_function(link_t *functions, devman_handle_t handle)
233{
234 ddf_fun_t *fun = NULL;
235
236 fibril_mutex_lock(&functions_mutex);
237 link_t *link = functions->next;
238
239 while (link != functions) {
240 fun = list_get_instance(link, ddf_fun_t, link);
241 if (fun->handle == handle) {
242 fibril_mutex_unlock(&functions_mutex);
243 return fun;
244 }
245
246 link = link->next;
247 }
248
249 fibril_mutex_unlock(&functions_mutex);
250
251 return NULL;
252}
253
254static void driver_add_device(ipc_callid_t iid, ipc_call_t *icall)
255{
256 char *dev_name = NULL;
257 int res;
258
259 devman_handle_t dev_handle = IPC_GET_ARG1(*icall);
260 devman_handle_t parent_fun_handle = IPC_GET_ARG2(*icall);
261
262 ddf_dev_t *dev = create_device();
263 dev->handle = dev_handle;
264
265 async_data_write_accept((void **) &dev_name, true, 0, 0, 0, 0);
266 dev->name = dev_name;
267
268 /*
269 * Currently not used, parent fun handle is stored in context
270 * of the connection to the parent device driver.
271 */
272 (void) parent_fun_handle;
273
274 res = driver->driver_ops->add_device(dev);
275 if (res != EOK)
276 delete_device(dev);
277
278 async_answer_0(iid, res);
279}
280
281static void driver_connection_devman(ipc_callid_t iid, ipc_call_t *icall)
282{
283 /* Accept connection */
284 async_answer_0(iid, EOK);
285
286 bool cont = true;
287 while (cont) {
288 ipc_call_t call;
289 ipc_callid_t callid = async_get_call(&call);
290
291 switch (IPC_GET_IMETHOD(call)) {
292 case IPC_M_PHONE_HUNGUP:
293 cont = false;
294 continue;
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/**
305 * Generic client connection handler both for applications and drivers.
306 *
307 * @param drv True for driver client, false for other clients
308 * (applications, services etc.).
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 /*
328 * TODO - if the client is not a driver, check whether it is allowed to
329 * use the device.
330 */
331
332 int ret = EOK;
333 /* Open device function */
334 if (fun->ops != NULL && fun->ops->open != NULL)
335 ret = (*fun->ops->open)(fun);
336
337 async_answer_0(iid, ret);
338 if (ret != EOK)
339 return;
340
341 while (1) {
342 ipc_callid_t callid;
343 ipc_call_t call;
344 callid = async_get_call(&call);
345 sysarg_t method = IPC_GET_IMETHOD(call);
346 int iface_idx;
347
348 switch (method) {
349 case IPC_M_PHONE_HUNGUP:
350 /* Close device function */
351 if (fun->ops != NULL && fun->ops->close != NULL)
352 (*fun->ops->close)(fun);
353 async_answer_0(callid, EOK);
354 return;
355 default:
356 /* convert ipc interface id to interface index */
357
358 iface_idx = DEV_IFACE_IDX(method);
359
360 if (!is_valid_iface_idx(iface_idx)) {
361 remote_handler_t *default_handler =
362 function_get_default_handler(fun);
363 if (default_handler != NULL) {
364 (*default_handler)(fun, callid, &call);
365 break;
366 }
367
368 /*
369 * Function has no such interface and
370 * default handler is not provided.
371 */
372 printf("%s: driver_connection_gen error - "
373 "invalid interface id %d.",
374 driver->name, iface_idx);
375 async_answer_0(callid, ENOTSUP);
376 break;
377 }
378
379 /* calling one of the function's interfaces */
380
381 /* Get the interface ops structure. */
382 void *ops = function_get_ops(fun, iface_idx);
383 if (ops == NULL) {
384 printf("%s: driver_connection_gen error - ",
385 driver->name);
386 printf("Function with handle %" PRIun " has no interface "
387 "with id %d.\n", handle, iface_idx);
388 async_answer_0(callid, ENOTSUP);
389 break;
390 }
391
392 /*
393 * Get the corresponding interface for remote request
394 * handling ("remote interface").
395 */
396 remote_iface_t *rem_iface = get_remote_iface(iface_idx);
397 assert(rem_iface != NULL);
398
399 /* get the method of the remote interface */
400 sysarg_t iface_method_idx = IPC_GET_ARG1(call);
401 remote_iface_func_ptr_t iface_method_ptr =
402 get_remote_method(rem_iface, iface_method_idx);
403 if (iface_method_ptr == NULL) {
404 /* The interface has not such method */
405 printf("%s: driver_connection_gen error - "
406 "invalid interface method.", driver->name);
407 async_answer_0(callid, ENOTSUP);
408 break;
409 }
410
411 /*
412 * Call the remote interface's method, which will
413 * receive parameters from the remote client and it will
414 * pass it to the corresponding local interface method
415 * associated with the function by its driver.
416 */
417 (*iface_method_ptr)(fun, ops, callid, &call);
418 break;
419 }
420 }
421}
422
423static void driver_connection_driver(ipc_callid_t iid, ipc_call_t *icall)
424{
425 driver_connection_gen(iid, icall, true);
426}
427
428static void driver_connection_client(ipc_callid_t iid, ipc_call_t *icall)
429{
430 driver_connection_gen(iid, icall, false);
431}
432
433/** Function for handling connections to device driver. */
434static void driver_connection(ipc_callid_t iid, ipc_call_t *icall)
435{
436 /* Select interface */
437 switch ((sysarg_t) (IPC_GET_ARG1(*icall))) {
438 case DRIVER_DEVMAN:
439 /* Handle request from device manager */
440 driver_connection_devman(iid, icall);
441 break;
442 case DRIVER_DRIVER:
443 /* Handle request from drivers of child devices */
444 driver_connection_driver(iid, icall);
445 break;
446 case DRIVER_CLIENT:
447 /* Handle request from client applications */
448 driver_connection_client(iid, icall);
449 break;
450 default:
451 /* No such interface */
452 async_answer_0(iid, ENOENT);
453 }
454}
455
456/** Create new device structure.
457 *
458 * @return The device structure.
459 */
460static ddf_dev_t *create_device(void)
461{
462 ddf_dev_t *dev;
463
464 dev = malloc(sizeof(ddf_dev_t));
465 if (dev == NULL)
466 return NULL;
467
468 memset(dev, 0, sizeof(ddf_dev_t));
469 return dev;
470}
471
472/** Create new function structure.
473 *
474 * @return The device structure.
475 */
476static ddf_fun_t *create_function(void)
477{
478 ddf_fun_t *fun;
479
480 fun = calloc(1, sizeof(ddf_fun_t));
481 if (fun == NULL)
482 return NULL;
483
484 init_match_ids(&fun->match_ids);
485 link_initialize(&fun->link);
486
487 return fun;
488}
489
490/** Delete device structure.
491 *
492 * @param dev The device structure.
493 */
494static void delete_device(ddf_dev_t *dev)
495{
496 free(dev);
497}
498
499/** Delete device structure.
500 *
501 * @param dev The device structure.
502 */
503static void delete_function(ddf_fun_t *fun)
504{
505 clean_match_ids(&fun->match_ids);
506 if (fun->name != NULL)
507 free(fun->name);
508 free(fun);
509}
510
511/** Create a DDF function node.
512 *
513 * Create a DDF function (in memory). Both child devices and external clients
514 * communicate with a device via its functions.
515 *
516 * The created function node is fully formed, but only exists in the memory
517 * of the client task. In order to be visible to the system, the function
518 * must be bound using ddf_fun_bind().
519 *
520 * This function should only fail if there is not enough free memory.
521 * Specifically, this function succeeds even if @a dev already has
522 * a (bound) function with the same name.
523 *
524 * Type: A function of type fun_inner indicates that DDF should attempt
525 * to attach child devices to the function. fun_exposed means that
526 * the function should be exported to external clients (applications).
527 *
528 * @param dev Device to which we are adding function
529 * @param ftype Type of function (fun_inner or fun_exposed)
530 * @param name Name of function
531 *
532 * @return New function or @c NULL if memory is not available
533 */
534ddf_fun_t *ddf_fun_create(ddf_dev_t *dev, fun_type_t ftype, const char *name)
535{
536 ddf_fun_t *fun;
537
538 fun = create_function();
539 if (fun == NULL)
540 return NULL;
541
542 fun->bound = false;
543 fun->dev = dev;
544 fun->ftype = ftype;
545
546 fun->name = str_dup(name);
547 if (fun->name == NULL) {
548 delete_function(fun);
549 return NULL;
550 }
551
552 return fun;
553}
554
555/** Destroy DDF function node.
556 *
557 * Destroy a function previously created with ddf_fun_create(). The function
558 * must not be bound.
559 *
560 * @param fun Function to destroy
561 */
562void ddf_fun_destroy(ddf_fun_t *fun)
563{
564 assert(fun->bound == false);
565 delete_function(fun);
566}
567
568static void *function_get_ops(ddf_fun_t *fun, dev_inferface_idx_t idx)
569{
570 assert(is_valid_iface_idx(idx));
571 if (fun->ops == NULL)
572 return NULL;
573 return fun->ops->interfaces[idx];
574}
575
576/** Bind a function node.
577 *
578 * Bind the specified function to the system. This effectively makes
579 * the function visible to the system (uploads it to the server).
580 *
581 * This function can fail for several reasons. Specifically,
582 * it will fail if the device already has a bound function of
583 * the same name.
584 *
585 * @param fun Function to bind
586 * @return EOK on success or negative error code
587 */
588int ddf_fun_bind(ddf_fun_t *fun)
589{
590 assert(fun->name != NULL);
591
592 int res;
593
594 add_to_functions_list(fun);
595 res = devman_add_function(fun->name, fun->ftype, &fun->match_ids,
596 fun->dev->handle, &fun->handle);
597 if (res != EOK) {
598 remove_from_functions_list(fun);
599 return res;
600 }
601
602 fun->bound = true;
603 return res;
604}
605
606/** Add single match ID to inner function.
607 *
608 * Construct and add a single match ID to the specified function.
609 * Cannot be called when the function node is bound.
610 *
611 * @param fun Function
612 * @param match_id_str Match string
613 * @param match_score Match score
614 * @return EOK on success, ENOMEM if out of memory.
615 */
616int ddf_fun_add_match_id(ddf_fun_t *fun, const char *match_id_str,
617 int match_score)
618{
619 match_id_t *match_id;
620
621 assert(fun->bound == false);
622 assert(fun->ftype == fun_inner);
623
624 match_id = create_match_id();
625 if (match_id == NULL)
626 return ENOMEM;
627
628 match_id->id = match_id_str;
629 match_id->score = 90;
630
631 add_match_id(&fun->match_ids, match_id);
632 return EOK;
633}
634
635/** Get default handler for client requests */
636static remote_handler_t *function_get_default_handler(ddf_fun_t *fun)
637{
638 if (fun->ops == NULL)
639 return NULL;
640 return fun->ops->default_handler;
641}
642
643/** Add exposed function to class.
644 *
645 * Must only be called when the function is bound.
646 */
647int ddf_fun_add_to_class(ddf_fun_t *fun, const char *class_name)
648{
649 assert(fun->bound == true);
650 assert(fun->ftype == fun_exposed);
651
652 return devman_add_device_to_class(fun->handle, class_name);
653}
654
655int ddf_driver_main(driver_t *drv)
656{
657 /*
658 * Remember the driver structure - driver_ops will be called by generic
659 * handler for incoming connections.
660 */
661 driver = drv;
662
663 /* Initialize the list of interrupt contexts. */
664 init_interrupt_context_list(&interrupt_contexts);
665
666 /* Set generic interrupt handler. */
667 async_set_interrupt_received(driver_irq_handler);
668
669 /*
670 * Register driver by device manager with generic handler for incoming
671 * connections.
672 */
673 devman_driver_register(driver->name, driver_connection);
674
675 async_manager();
676
677 /* Never reached. */
678 return 0;
679}
680
681/**
682 * @}
683 */
Note: See TracBrowser for help on using the repository browser.