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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since d7c72db was 4e78236, checked in by Lubos Slovak <lubos.slovak@…>, 15 years ago

Fixed mkbd and corresponding library functions.

  • Fixed linking remote_usbhid.c
  • Added some temporary debug output.
  • Fix in hiddev.c.
  • Property mode set to 100644
File size: 16.9 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 bool cont = true;
288 while (cont) {
289 ipc_call_t call;
290 ipc_callid_t callid = async_get_call(&call);
291
292 switch (IPC_GET_IMETHOD(call)) {
293 case IPC_M_PHONE_HUNGUP:
294 cont = false;
295 continue;
296 case DRIVER_ADD_DEVICE:
297 driver_add_device(callid, &call);
298 break;
299 default:
300 async_answer_0(callid, ENOENT);
301 }
302 }
303}
304
305/**
306 * Generic client connection handler both for applications and drivers.
307 *
308 * @param drv True for driver client, false for other clients
309 * (applications, services etc.).
310 */
311static void driver_connection_gen(ipc_callid_t iid, ipc_call_t *icall, bool drv)
312{
313 /*
314 * Answer the first IPC_M_CONNECT_ME_TO call and remember the handle of
315 * the device to which the client connected.
316 */
317 devman_handle_t handle = IPC_GET_ARG2(*icall);
318 ddf_fun_t *fun = driver_get_function(&functions, handle);
319
320 if (fun == NULL) {
321 printf("%s: driver_connection_gen error - no function with handle"
322 " %" PRIun " was found.\n", driver->name, handle);
323 async_answer_0(iid, ENOENT);
324 return;
325 }
326
327
328 /*
329 * TODO - if the client is not a driver, check whether it is allowed to
330 * use the device.
331 */
332
333 int ret = EOK;
334 /* Open device function */
335 if (fun->ops != NULL && fun->ops->open != NULL)
336 ret = (*fun->ops->open)(fun);
337
338 async_answer_0(iid, ret);
339 if (ret != EOK)
340 return;
341
342 while (1) {
343 ipc_callid_t callid;
344 ipc_call_t call;
345 callid = async_get_call(&call);
346 sysarg_t method = IPC_GET_IMETHOD(call);
347 int iface_idx;
348
349 printf("driver_connection_gen(): method: %d\n", method);
350
351 switch (method) {
352 case IPC_M_PHONE_HUNGUP:
353 /* Close device function */
354 if (fun->ops != NULL && fun->ops->close != NULL)
355 (*fun->ops->close)(fun);
356 async_answer_0(callid, EOK);
357 return;
358 default:
359 /* convert ipc interface id to interface index */
360
361 iface_idx = DEV_IFACE_IDX(method);
362
363 if (!is_valid_iface_idx(iface_idx)) {
364 remote_handler_t *default_handler =
365 function_get_default_handler(fun);
366 if (default_handler != NULL) {
367 (*default_handler)(fun, callid, &call);
368 break;
369 }
370
371 /*
372 * Function has no such interface and
373 * default handler is not provided.
374 */
375 printf("%s: driver_connection_gen error - "
376 "invalid interface id %d.",
377 driver->name, iface_idx);
378 async_answer_0(callid, ENOTSUP);
379 break;
380 }
381
382 /* calling one of the function's interfaces */
383
384 /* Get the interface ops structure. */
385 void *ops = function_get_ops(fun, iface_idx);
386 if (ops == NULL) {
387 printf("%s: driver_connection_gen error - ",
388 driver->name);
389 printf("Function with handle %" PRIun " has no interface "
390 "with id %d.\n", handle, iface_idx);
391 async_answer_0(callid, ENOTSUP);
392 break;
393 }
394
395 /*
396 * Get the corresponding interface for remote request
397 * handling ("remote interface").
398 */
399 printf("iface_idx: %d\n", iface_idx);
400 remote_iface_t *rem_iface = get_remote_iface(iface_idx);
401 assert(rem_iface != NULL);
402
403 /* get the method of the remote interface */
404 sysarg_t iface_method_idx = IPC_GET_ARG1(call);
405 remote_iface_func_ptr_t iface_method_ptr =
406 get_remote_method(rem_iface, iface_method_idx);
407 if (iface_method_ptr == NULL) {
408 /* The interface has not such method */
409 printf("%s: driver_connection_gen error - "
410 "invalid interface method (%d).\n",
411 driver->name, iface_method_idx);
412 async_answer_0(callid, ENOTSUP);
413 break;
414 }
415
416 /*
417 * Call the remote interface's method, which will
418 * receive parameters from the remote client and it will
419 * pass it to the corresponding local interface method
420 * associated with the function by its driver.
421 */
422 (*iface_method_ptr)(fun, ops, callid, &call);
423 break;
424 }
425 }
426}
427
428static void driver_connection_driver(ipc_callid_t iid, ipc_call_t *icall)
429{
430 driver_connection_gen(iid, icall, true);
431}
432
433static void driver_connection_client(ipc_callid_t iid, ipc_call_t *icall)
434{
435 driver_connection_gen(iid, icall, false);
436}
437
438/** Function for handling connections to device driver. */
439static void driver_connection(ipc_callid_t iid, ipc_call_t *icall)
440{
441 /* Select interface */
442 switch ((sysarg_t) (IPC_GET_ARG1(*icall))) {
443 case DRIVER_DEVMAN:
444 /* Handle request from device manager */
445 driver_connection_devman(iid, icall);
446 break;
447 case DRIVER_DRIVER:
448 /* Handle request from drivers of child devices */
449 driver_connection_driver(iid, icall);
450 break;
451 case DRIVER_CLIENT:
452 /* Handle request from client applications */
453 driver_connection_client(iid, icall);
454 break;
455 default:
456 /* No such interface */
457 async_answer_0(iid, ENOENT);
458 }
459}
460
461/** Create new device structure.
462 *
463 * @return The device structure.
464 */
465static ddf_dev_t *create_device(void)
466{
467 ddf_dev_t *dev;
468
469 dev = malloc(sizeof(ddf_dev_t));
470 if (dev == NULL)
471 return NULL;
472
473 memset(dev, 0, sizeof(ddf_dev_t));
474 return dev;
475}
476
477/** Create new function structure.
478 *
479 * @return The device structure.
480 */
481static ddf_fun_t *create_function(void)
482{
483 ddf_fun_t *fun;
484
485 fun = calloc(1, sizeof(ddf_fun_t));
486 if (fun == NULL)
487 return NULL;
488
489 init_match_ids(&fun->match_ids);
490 link_initialize(&fun->link);
491
492 return fun;
493}
494
495/** Delete device structure.
496 *
497 * @param dev The device structure.
498 */
499static void delete_device(ddf_dev_t *dev)
500{
501 free(dev);
502}
503
504/** Delete device structure.
505 *
506 * @param dev The device structure.
507 */
508static void delete_function(ddf_fun_t *fun)
509{
510 clean_match_ids(&fun->match_ids);
511 if (fun->name != NULL)
512 free(fun->name);
513 free(fun);
514}
515
516/** Create a DDF function node.
517 *
518 * Create a DDF function (in memory). Both child devices and external clients
519 * communicate with a device via its functions.
520 *
521 * The created function node is fully formed, but only exists in the memory
522 * of the client task. In order to be visible to the system, the function
523 * must be bound using ddf_fun_bind().
524 *
525 * This function should only fail if there is not enough free memory.
526 * Specifically, this function succeeds even if @a dev already has
527 * a (bound) function with the same name.
528 *
529 * Type: A function of type fun_inner indicates that DDF should attempt
530 * to attach child devices to the function. fun_exposed means that
531 * the function should be exported to external clients (applications).
532 *
533 * @param dev Device to which we are adding function
534 * @param ftype Type of function (fun_inner or fun_exposed)
535 * @param name Name of function
536 *
537 * @return New function or @c NULL if memory is not available
538 */
539ddf_fun_t *ddf_fun_create(ddf_dev_t *dev, fun_type_t ftype, const char *name)
540{
541 ddf_fun_t *fun;
542
543 fun = create_function();
544 if (fun == NULL)
545 return NULL;
546
547 fun->bound = false;
548 fun->dev = dev;
549 fun->ftype = ftype;
550
551 fun->name = str_dup(name);
552 if (fun->name == NULL) {
553 delete_function(fun);
554 return NULL;
555 }
556
557 return fun;
558}
559
560/** Destroy DDF function node.
561 *
562 * Destroy a function previously created with ddf_fun_create(). The function
563 * must not be bound.
564 *
565 * @param fun Function to destroy
566 */
567void ddf_fun_destroy(ddf_fun_t *fun)
568{
569 assert(fun->bound == false);
570 delete_function(fun);
571}
572
573static void *function_get_ops(ddf_fun_t *fun, dev_inferface_idx_t idx)
574{
575 assert(is_valid_iface_idx(idx));
576 if (fun->ops == NULL)
577 return NULL;
578 return fun->ops->interfaces[idx];
579}
580
581/** Bind a function node.
582 *
583 * Bind the specified function to the system. This effectively makes
584 * the function visible to the system (uploads it to the server).
585 *
586 * This function can fail for several reasons. Specifically,
587 * it will fail if the device already has a bound function of
588 * the same name.
589 *
590 * @param fun Function to bind
591 * @return EOK on success or negative error code
592 */
593int ddf_fun_bind(ddf_fun_t *fun)
594{
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/** Add single match ID to inner function.
612 *
613 * Construct and add a single match ID to the specified function.
614 * Cannot be called when the function node is bound.
615 *
616 * @param fun Function
617 * @param match_id_str Match string
618 * @param match_score Match score
619 * @return EOK on success, ENOMEM if out of memory.
620 */
621int ddf_fun_add_match_id(ddf_fun_t *fun, const char *match_id_str,
622 int match_score)
623{
624 match_id_t *match_id;
625
626 assert(fun->bound == false);
627 assert(fun->ftype == fun_inner);
628
629 match_id = create_match_id();
630 if (match_id == NULL)
631 return ENOMEM;
632
633 match_id->id = match_id_str;
634 match_id->score = 90;
635
636 add_match_id(&fun->match_ids, match_id);
637 return EOK;
638}
639
640/** Get default handler for client requests */
641static remote_handler_t *function_get_default_handler(ddf_fun_t *fun)
642{
643 if (fun->ops == NULL)
644 return NULL;
645 return fun->ops->default_handler;
646}
647
648/** Add exposed function to class.
649 *
650 * Must only be called when the function is bound.
651 */
652int ddf_fun_add_to_class(ddf_fun_t *fun, const char *class_name)
653{
654 assert(fun->bound == true);
655 assert(fun->ftype == fun_exposed);
656
657 return devman_add_device_to_class(fun->handle, class_name);
658}
659
660int ddf_driver_main(driver_t *drv)
661{
662 int rc;
663
664 /*
665 * Remember the driver structure - driver_ops will be called by generic
666 * handler for incoming connections.
667 */
668 driver = drv;
669
670 /* Initialize the list of interrupt contexts. */
671 init_interrupt_context_list(&interrupt_contexts);
672
673 /* Set generic interrupt handler. */
674 async_set_interrupt_received(driver_irq_handler);
675
676 /*
677 * Register driver with device manager using generic handler for
678 * incoming connections.
679 */
680 rc = devman_driver_register(driver->name, driver_connection);
681 if (rc != EOK) {
682 printf("Error: Failed to register driver with device manager "
683 "(%s).\n", (rc == EEXISTS) ? "driver already started" :
684 str_error(rc));
685
686 return 1;
687 }
688
689 /* Return success from the task since server has started. */
690 rc = task_retval(0);
691 if (rc != EOK)
692 return 1;
693
694 async_manager();
695
696 /* Never reached. */
697 return 0;
698}
699
700/**
701 * @}
702 */
Note: See TracBrowser for help on using the repository browser.