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

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

Separate list_t typedef from link_t (user-space part).

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