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

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

Supress debugging messages.

  • Property mode set to 100644
File size: 22.5 KB
RevLine 
[c16cf62]1/*
2 * Copyright (c) 2010 Lenka Trochtova
[83a2f43]3 * Copyright (c) 2011 Jiri Svoboda
[c16cf62]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 */
[52b7b1bb]38
[c16cf62]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>
[c47e1a8]48#include <str.h>
[3ad7b1c]49#include <str_error.h>
[c16cf62]50#include <ctype.h>
[66babbd]51#include <errno.h>
[7e752b2]52#include <inttypes.h>
[af6b5157]53#include <devman.h>
[c16cf62]54
[084ff99]55#include <ipc/driver.h>
[c16cf62]56
[5fdd7c3]57#include "dev_iface.h"
[af6b5157]58#include "ddf/driver.h"
59#include "ddf/interrupt.h"
[c16cf62]60
[36f2b3e]61/** Driver structure */
[c16cf62]62static driver_t *driver;
[7f8b581]63
[36f2b3e]64/** Devices */
[1a5b252]65LIST_INITIALIZE(devices);
66FIBRIL_MUTEX_INITIALIZE(devices_mutex);
67
68/** Functions */
[8b1e15ac]69LIST_INITIALIZE(functions);
70FIBRIL_MUTEX_INITIALIZE(functions_mutex);
[084ff99]71
[36f2b3e]72/** Interrupts */
[7f8b581]73static interrupt_context_list_t interrupt_contexts;
74
75static irq_cmd_t default_cmds[] = {
76 {
77 .cmd = CMD_ACCEPT
78 }
79};
80
81static irq_code_t default_pseudocode = {
82 sizeof(default_cmds) / sizeof(irq_cmd_t),
83 default_cmds
84};
85
[83a2f43]86static ddf_dev_t *create_device(void);
87static void delete_device(ddf_dev_t *);
[0f0f8bc]88static void dev_add_ref(ddf_dev_t *);
89static void dev_del_ref(ddf_dev_t *);
90static void fun_add_ref(ddf_fun_t *);
91static void fun_del_ref(ddf_fun_t *);
[af6b5157]92static remote_handler_t *function_get_default_handler(ddf_fun_t *);
93static void *function_get_ops(ddf_fun_t *, dev_inferface_idx_t);
[7f8b581]94
95static void driver_irq_handler(ipc_callid_t iid, ipc_call_t *icall)
[7a252ec8]96{
[228e490]97 int id = (int)IPC_GET_IMETHOD(*icall);
[7a252ec8]98 interrupt_context_t *ctx;
99
100 ctx = find_interrupt_context_by_id(&interrupt_contexts, id);
[36f2b3e]101 if (ctx != NULL && ctx->handler != NULL)
[7a252ec8]102 (*ctx->handler)(ctx->dev, iid, icall);
[7f8b581]103}
104
[5fdd7c3]105interrupt_context_t *create_interrupt_context(void)
106{
107 interrupt_context_t *ctx;
108
109 ctx = (interrupt_context_t *) malloc(sizeof(interrupt_context_t));
110 if (ctx != NULL)
111 memset(ctx, 0, sizeof(interrupt_context_t));
112
113 return ctx;
114}
115
116void delete_interrupt_context(interrupt_context_t *ctx)
117{
118 if (ctx != NULL)
119 free(ctx);
120}
121
122void init_interrupt_context_list(interrupt_context_list_t *list)
123{
124 memset(list, 0, sizeof(interrupt_context_list_t));
125 fibril_mutex_initialize(&list->mutex);
126 list_initialize(&list->contexts);
127}
128
129void
130add_interrupt_context(interrupt_context_list_t *list, interrupt_context_t *ctx)
131{
132 fibril_mutex_lock(&list->mutex);
133 ctx->id = list->curr_id++;
134 list_append(&ctx->link, &list->contexts);
135 fibril_mutex_unlock(&list->mutex);
136}
137
138void remove_interrupt_context(interrupt_context_list_t *list,
139 interrupt_context_t *ctx)
140{
141 fibril_mutex_lock(&list->mutex);
142 list_remove(&ctx->link);
143 fibril_mutex_unlock(&list->mutex);
144}
145
146interrupt_context_t *
147find_interrupt_context_by_id(interrupt_context_list_t *list, int id)
148{
149 interrupt_context_t *ctx;
150
[b72efe8]151 fibril_mutex_lock(&list->mutex);
152
153 list_foreach(list->contexts, link) {
[5fdd7c3]154 ctx = list_get_instance(link, interrupt_context_t, link);
155 if (ctx->id == id) {
156 fibril_mutex_unlock(&list->mutex);
157 return ctx;
158 }
159 }
160
161 fibril_mutex_unlock(&list->mutex);
162 return NULL;
163}
164
165interrupt_context_t *
[83a2f43]166find_interrupt_context(interrupt_context_list_t *list, ddf_dev_t *dev, int irq)
[5fdd7c3]167{
168 interrupt_context_t *ctx;
169
[b72efe8]170 fibril_mutex_lock(&list->mutex);
171
172 list_foreach(list->contexts, link) {
[5fdd7c3]173 ctx = list_get_instance(link, interrupt_context_t, link);
174 if (ctx->irq == irq && ctx->dev == dev) {
175 fibril_mutex_unlock(&list->mutex);
176 return ctx;
177 }
178 }
179
180 fibril_mutex_unlock(&list->mutex);
181 return NULL;
182}
183
184
[7a252ec8]185int
[83a2f43]186register_interrupt_handler(ddf_dev_t *dev, int irq, interrupt_handler_t *handler,
[7a252ec8]187 irq_code_t *pseudocode)
[7f8b581]188{
189 interrupt_context_t *ctx = create_interrupt_context();
190
191 ctx->dev = dev;
192 ctx->irq = irq;
193 ctx->handler = handler;
194
195 add_interrupt_context(&interrupt_contexts, ctx);
196
[36f2b3e]197 if (pseudocode == NULL)
[7f8b581]198 pseudocode = &default_pseudocode;
199
[ffa2c8ef]200 int res = register_irq(irq, dev->handle, ctx->id, pseudocode);
[36f2b3e]201 if (res != EOK) {
[7f8b581]202 remove_interrupt_context(&interrupt_contexts, ctx);
203 delete_interrupt_context(ctx);
204 }
[7a252ec8]205
206 return res;
[7f8b581]207}
208
[83a2f43]209int unregister_interrupt_handler(ddf_dev_t *dev, int irq)
[7f8b581]210{
[7a252ec8]211 interrupt_context_t *ctx = find_interrupt_context(&interrupt_contexts,
212 dev, irq);
[ffa2c8ef]213 int res = unregister_irq(irq, dev->handle);
[36f2b3e]214
215 if (ctx != NULL) {
[7f8b581]216 remove_interrupt_context(&interrupt_contexts, ctx);
[7a252ec8]217 delete_interrupt_context(ctx);
[7f8b581]218 }
[36f2b3e]219
[7f8b581]220 return res;
221}
222
[83a2f43]223static void add_to_functions_list(ddf_fun_t *fun)
[9a66bc2e]224{
[8b1e15ac]225 fibril_mutex_lock(&functions_mutex);
226 list_append(&fun->link, &functions);
227 fibril_mutex_unlock(&functions_mutex);
[9a66bc2e]228}
229
[83a2f43]230static void remove_from_functions_list(ddf_fun_t *fun)
[9a66bc2e]231{
[8b1e15ac]232 fibril_mutex_lock(&functions_mutex);
233 list_remove(&fun->link);
234 fibril_mutex_unlock(&functions_mutex);
[9a66bc2e]235}
236
[1a5b252]237static ddf_dev_t *driver_get_device(devman_handle_t handle)
238{
239 ddf_dev_t *dev = NULL;
240
241 assert(fibril_mutex_is_locked(&devices_mutex));
242
243 list_foreach(devices, link) {
244 dev = list_get_instance(link, ddf_dev_t, link);
245 if (dev->handle == handle)
246 return dev;
247 }
248
249 return NULL;
250}
251
252static ddf_fun_t *driver_get_function(devman_handle_t handle)
[52b7b1bb]253{
[83a2f43]254 ddf_fun_t *fun = NULL;
[8b1e15ac]255
[1a5b252]256 assert(fibril_mutex_is_locked(&functions_mutex));
[8b1e15ac]257
[1a5b252]258 list_foreach(functions, link) {
[83a2f43]259 fun = list_get_instance(link, ddf_fun_t, link);
[1a5b252]260 if (fun->handle == handle)
[8b1e15ac]261 return fun;
[a1769ee]262 }
[36f2b3e]263
[a1769ee]264 return NULL;
265}
266
[0f0f8bc]267static void driver_dev_add(ipc_callid_t iid, ipc_call_t *icall)
[084ff99]268{
[df747b9c]269 char *dev_name = NULL;
[36f2b3e]270 int res;
[9a66bc2e]271
[d35ac1d]272 devman_handle_t dev_handle = IPC_GET_ARG1(*icall);
[8b1e15ac]273 devman_handle_t parent_fun_handle = IPC_GET_ARG2(*icall);
[d35ac1d]274
[83a2f43]275 ddf_dev_t *dev = create_device();
[0f0f8bc]276
277 /* Add one reference that will be dropped by driver_dev_remove() */
278 dev_add_ref(dev);
[084ff99]279 dev->handle = dev_handle;
[8b1e15ac]280
[7a252ec8]281 async_data_write_accept((void **) &dev_name, true, 0, 0, 0, 0);
[df747b9c]282 dev->name = dev_name;
[8b1e15ac]283
284 /*
285 * Currently not used, parent fun handle is stored in context
286 * of the connection to the parent device driver.
287 */
288 (void) parent_fun_handle;
[0d6915f]289
[df747b9c]290 res = driver->driver_ops->add_device(dev);
[0f0f8bc]291
292 if (res != EOK) {
293 dev_del_ref(dev);
294 async_answer_0(iid, res);
295 return;
296 }
[df747b9c]297
[1a5b252]298 fibril_mutex_lock(&devices_mutex);
299 list_append(&dev->link, &devices);
300 fibril_mutex_unlock(&devices_mutex);
301
[ffa2c8ef]302 async_answer_0(iid, res);
[084ff99]303}
[c16cf62]304
[1a5b252]305static void driver_dev_remove(ipc_callid_t iid, ipc_call_t *icall)
306{
307 devman_handle_t devh;
308 ddf_dev_t *dev;
309 int rc;
310
311 devh = IPC_GET_ARG1(*icall);
312
313 fibril_mutex_lock(&devices_mutex);
314 dev = driver_get_device(devh);
[0f0f8bc]315 dev_add_ref(dev);
[1a5b252]316 fibril_mutex_unlock(&devices_mutex);
317
318 if (dev == NULL) {
319 async_answer_0(iid, ENOENT);
320 return;
321 }
322
323 if (driver->driver_ops->dev_remove != NULL)
324 rc = driver->driver_ops->dev_remove(dev);
325 else
326 rc = ENOTSUP;
327
[0f0f8bc]328 if (rc == EOK)
329 dev_del_ref(dev);
330
[1a5b252]331 async_answer_0(iid, (sysarg_t) rc);
332}
333
334static void driver_fun_online(ipc_callid_t iid, ipc_call_t *icall)
335{
336 devman_handle_t funh;
337 ddf_fun_t *fun;
338 int rc;
339
340 funh = IPC_GET_ARG1(*icall);
[0f0f8bc]341
342 /*
[4d94002d]343 * Look the function up. Bump reference count so that
[0f0f8bc]344 * the function continues to exist until we return
345 * from the driver.
346 */
[1a5b252]347 fibril_mutex_lock(&functions_mutex);
[0f0f8bc]348
[1a5b252]349 fun = driver_get_function(funh);
[0f0f8bc]350 if (fun != NULL)
351 fun_add_ref(fun);
352
[1a5b252]353 fibril_mutex_unlock(&functions_mutex);
354
355 if (fun == NULL) {
356 async_answer_0(iid, ENOENT);
357 return;
358 }
359
[0f0f8bc]360 /* Call driver entry point */
[1a5b252]361 if (driver->driver_ops->fun_online != NULL)
362 rc = driver->driver_ops->fun_online(fun);
363 else
364 rc = ENOTSUP;
365
[0f0f8bc]366 fun_del_ref(fun);
367
[1a5b252]368 async_answer_0(iid, (sysarg_t) rc);
369}
370
371static void driver_fun_offline(ipc_callid_t iid, ipc_call_t *icall)
372{
373 devman_handle_t funh;
374 ddf_fun_t *fun;
375 int rc;
376
377 funh = IPC_GET_ARG1(*icall);
[0f0f8bc]378
379 /*
[4d94002d]380 * Look the function up. Bump reference count so that
[0f0f8bc]381 * the function continues to exist until we return
382 * from the driver.
383 */
[1a5b252]384 fibril_mutex_lock(&functions_mutex);
[0f0f8bc]385
[1a5b252]386 fun = driver_get_function(funh);
[0f0f8bc]387 if (fun != NULL)
388 fun_add_ref(fun);
389
[1a5b252]390 fibril_mutex_unlock(&functions_mutex);
391
392 if (fun == NULL) {
393 async_answer_0(iid, ENOENT);
394 return;
395 }
396
[0f0f8bc]397 /* Call driver entry point */
[1a5b252]398 if (driver->driver_ops->fun_offline != NULL)
399 rc = driver->driver_ops->fun_offline(fun);
400 else
401 rc = ENOTSUP;
402
403 async_answer_0(iid, (sysarg_t) rc);
404}
405
[c16cf62]406static void driver_connection_devman(ipc_callid_t iid, ipc_call_t *icall)
407{
408 /* Accept connection */
[ffa2c8ef]409 async_answer_0(iid, EOK);
[36f2b3e]410
[79ae36dd]411 while (true) {
[c16cf62]412 ipc_call_t call;
413 ipc_callid_t callid = async_get_call(&call);
[36f2b3e]414
[79ae36dd]415 if (!IPC_GET_IMETHOD(call))
416 break;
417
[228e490]418 switch (IPC_GET_IMETHOD(call)) {
[1a5b252]419 case DRIVER_DEV_ADD:
[0f0f8bc]420 driver_dev_add(callid, &call);
[c16cf62]421 break;
[1a5b252]422 case DRIVER_DEV_REMOVE:
423 driver_dev_remove(callid, &call);
424 break;
425 case DRIVER_FUN_ONLINE:
426 driver_fun_online(callid, &call);
427 break;
428 case DRIVER_FUN_OFFLINE:
429 driver_fun_offline(callid, &call);
430 break;
[c16cf62]431 default:
[1a5b252]432 async_answer_0(callid, ENOTSUP);
[c16cf62]433 }
[52b7b1bb]434 }
[c16cf62]435}
436
[79ae36dd]437/** Generic client connection handler both for applications and drivers.
438 *
439 * @param drv True for driver client, false for other clients
440 * (applications, services, etc.).
[52b7b1bb]441 *
[a1769ee]442 */
[9a66bc2e]443static void driver_connection_gen(ipc_callid_t iid, ipc_call_t *icall, bool drv)
[52b7b1bb]444{
[7a252ec8]445 /*
446 * Answer the first IPC_M_CONNECT_ME_TO call and remember the handle of
447 * the device to which the client connected.
448 */
[0b5a4131]449 devman_handle_t handle = IPC_GET_ARG2(*icall);
[1a5b252]450
451 fibril_mutex_lock(&functions_mutex);
452 ddf_fun_t *fun = driver_get_function(handle);
453 fibril_mutex_unlock(&functions_mutex);
454 /* XXX Need a lock on fun */
[79ae36dd]455
[8b1e15ac]456 if (fun == NULL) {
457 printf("%s: driver_connection_gen error - no function with handle"
[7e752b2]458 " %" PRIun " was found.\n", driver->name, handle);
[ffa2c8ef]459 async_answer_0(iid, ENOENT);
[a1769ee]460 return;
461 }
[5cd136ab]462
[ff65e91]463 if (fun->conn_handler != NULL) {
464 /* Driver has a custom connection handler. */
465 (*fun->conn_handler)(iid, icall, (void *)fun);
466 return;
467 }
468
[7a252ec8]469 /*
470 * TODO - if the client is not a driver, check whether it is allowed to
471 * use the device.
472 */
[36f2b3e]473
[25a7e11d]474 int ret = EOK;
[8b1e15ac]475 /* Open device function */
476 if (fun->ops != NULL && fun->ops->open != NULL)
477 ret = (*fun->ops->open)(fun);
[a1769ee]478
[ffa2c8ef]479 async_answer_0(iid, ret);
[36f2b3e]480 if (ret != EOK)
[a6e54c5d]481 return;
[36f2b3e]482
[79ae36dd]483 while (true) {
[a1769ee]484 ipc_callid_t callid;
485 ipc_call_t call;
486 callid = async_get_call(&call);
[228e490]487 sysarg_t method = IPC_GET_IMETHOD(call);
[3843ecb]488
[79ae36dd]489 if (!method) {
[8b1e15ac]490 /* Close device function */
491 if (fun->ops != NULL && fun->ops->close != NULL)
492 (*fun->ops->close)(fun);
[ffa2c8ef]493 async_answer_0(callid, EOK);
[a1769ee]494 return;
[79ae36dd]495 }
496
497 /* Convert ipc interface id to interface index */
498
499 int iface_idx = DEV_IFACE_IDX(method);
500
501 if (!is_valid_iface_idx(iface_idx)) {
502 remote_handler_t *default_handler =
503 function_get_default_handler(fun);
504 if (default_handler != NULL) {
505 (*default_handler)(fun, callid, &call);
[79a141a]506 continue;
[52b7b1bb]507 }
508
[7a252ec8]509 /*
[79ae36dd]510 * Function has no such interface and
511 * default handler is not provided.
[7a252ec8]512 */
[79ae36dd]513 printf("%s: driver_connection_gen error - "
514 "invalid interface id %d.",
515 driver->name, iface_idx);
516 async_answer_0(callid, ENOTSUP);
[79a141a]517 continue;
[79ae36dd]518 }
519
520 /* Calling one of the function's interfaces */
521
522 /* Get the interface ops structure. */
523 void *ops = function_get_ops(fun, iface_idx);
524 if (ops == NULL) {
525 printf("%s: driver_connection_gen error - ", driver->name);
526 printf("Function with handle %" PRIun " has no interface "
527 "with id %d.\n", handle, iface_idx);
528 async_answer_0(callid, ENOTSUP);
[79a141a]529 continue;
[79ae36dd]530 }
531
532 /*
533 * Get the corresponding interface for remote request
534 * handling ("remote interface").
535 */
536 remote_iface_t *rem_iface = get_remote_iface(iface_idx);
537 assert(rem_iface != NULL);
538
539 /* get the method of the remote interface */
540 sysarg_t iface_method_idx = IPC_GET_ARG1(call);
541 remote_iface_func_ptr_t iface_method_ptr =
542 get_remote_method(rem_iface, iface_method_idx);
543 if (iface_method_ptr == NULL) {
544 /* The interface has not such method */
545 printf("%s: driver_connection_gen error - "
546 "invalid interface method.", driver->name);
547 async_answer_0(callid, ENOTSUP);
[79a141a]548 continue;
[a1769ee]549 }
[79ae36dd]550
551 /*
552 * Call the remote interface's method, which will
553 * receive parameters from the remote client and it will
554 * pass it to the corresponding local interface method
555 * associated with the function by its driver.
556 */
557 (*iface_method_ptr)(fun, ops, callid, &call);
[a1769ee]558 }
559}
560
[c16cf62]561static void driver_connection_driver(ipc_callid_t iid, ipc_call_t *icall)
562{
[52b7b1bb]563 driver_connection_gen(iid, icall, true);
[c16cf62]564}
565
566static void driver_connection_client(ipc_callid_t iid, ipc_call_t *icall)
567{
[52b7b1bb]568 driver_connection_gen(iid, icall, false);
[c16cf62]569}
570
[7a252ec8]571/** Function for handling connections to device driver. */
[9934f7d]572static void driver_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
[c16cf62]573{
[45059d6b]574 sysarg_t conn_type;
575
576 if (iid == 0) {
577 /* Callback connection from devman */
578 /* XXX Use separate handler for this type of connection */
579 conn_type = DRIVER_DEVMAN;
580 } else {
581 conn_type = IPC_GET_ARG1(*icall);
582 }
583
[c16cf62]584 /* Select interface */
[45059d6b]585 switch (conn_type) {
[c16cf62]586 case DRIVER_DEVMAN:
[36f2b3e]587 /* Handle request from device manager */
[c16cf62]588 driver_connection_devman(iid, icall);
589 break;
590 case DRIVER_DRIVER:
[36f2b3e]591 /* Handle request from drivers of child devices */
[c16cf62]592 driver_connection_driver(iid, icall);
593 break;
594 case DRIVER_CLIENT:
[36f2b3e]595 /* Handle request from client applications */
[c16cf62]596 driver_connection_client(iid, icall);
597 break;
598 default:
[52b7b1bb]599 /* No such interface */
[ffa2c8ef]600 async_answer_0(iid, ENOENT);
[c16cf62]601 }
602}
603
[5fdd7c3]604/** Create new device structure.
605 *
606 * @return The device structure.
607 */
[83a2f43]608static ddf_dev_t *create_device(void)
[5fdd7c3]609{
[83a2f43]610 ddf_dev_t *dev;
[5fdd7c3]611
[0f0f8bc]612 dev = calloc(1, sizeof(ddf_dev_t));
[8b1e15ac]613 if (dev == NULL)
614 return NULL;
[5fdd7c3]615
616 return dev;
617}
618
[8b1e15ac]619/** Create new function structure.
620 *
621 * @return The device structure.
622 */
[83a2f43]623static ddf_fun_t *create_function(void)
[8b1e15ac]624{
[83a2f43]625 ddf_fun_t *fun;
[8b1e15ac]626
[83a2f43]627 fun = calloc(1, sizeof(ddf_fun_t));
[8b1e15ac]628 if (fun == NULL)
629 return NULL;
630
631 init_match_ids(&fun->match_ids);
632 link_initialize(&fun->link);
633
634 return fun;
635}
636
[5fdd7c3]637/** Delete device structure.
638 *
639 * @param dev The device structure.
640 */
[83a2f43]641static void delete_device(ddf_dev_t *dev)
[5fdd7c3]642{
[5f6e25e]643 if (dev->driver_data != NULL)
644 free(dev->driver_data);
[5fdd7c3]645 free(dev);
646}
647
[0f0f8bc]648/** Delete function structure.
[8b1e15ac]649 *
650 * @param dev The device structure.
651 */
[83a2f43]652static void delete_function(ddf_fun_t *fun)
[8b1e15ac]653{
654 clean_match_ids(&fun->match_ids);
[5f6e25e]655 if (fun->driver_data != NULL)
656 free(fun->driver_data);
[8b1e15ac]657 if (fun->name != NULL)
658 free(fun->name);
659 free(fun);
660}
661
[0f0f8bc]662/** Increase device reference count. */
663static void dev_add_ref(ddf_dev_t *dev)
664{
665 atomic_inc(&dev->refcnt);
666}
667
668/** Decrease device reference count.
669 *
670 * Free the device structure if the reference count drops to zero.
671 */
672static void dev_del_ref(ddf_dev_t *dev)
673{
674 if (atomic_predec(&dev->refcnt) == 0)
675 delete_device(dev);
676}
677
[bfe7867]678/** Increase function reference count.
679 *
680 * This also increases reference count on the device. The device structure
681 * will thus not be deallocated while there are some associated function
682 * structures.
683 */
[0f0f8bc]684static void fun_add_ref(ddf_fun_t *fun)
685{
[bfe7867]686 dev_add_ref(fun->dev);
[0f0f8bc]687 atomic_inc(&fun->refcnt);
688}
689
690/** Decrease function reference count.
691 *
692 * Free the function structure if the reference count drops to zero.
693 */
694static void fun_del_ref(ddf_fun_t *fun)
695{
[bfe7867]696 ddf_dev_t *dev = fun->dev;
697
[0f0f8bc]698 if (atomic_predec(&fun->refcnt) == 0)
699 delete_function(fun);
[bfe7867]700
701 dev_del_ref(dev);
[0f0f8bc]702}
703
[c5be39b]704/** Allocate driver-specific device data. */
705extern void *ddf_dev_data_alloc(ddf_dev_t *dev, size_t size)
706{
707 void *data;
708
709 assert(dev->driver_data == NULL);
710
711 data = calloc(1, size);
712 if (data == NULL)
713 return NULL;
714
715 dev->driver_data = data;
716 return data;
717}
718
[97a62fe]719/** Create a DDF function node.
720 *
721 * Create a DDF function (in memory). Both child devices and external clients
722 * communicate with a device via its functions.
723 *
724 * The created function node is fully formed, but only exists in the memory
725 * of the client task. In order to be visible to the system, the function
726 * must be bound using ddf_fun_bind().
727 *
728 * This function should only fail if there is not enough free memory.
729 * Specifically, this function succeeds even if @a dev already has
730 * a (bound) function with the same name.
731 *
732 * Type: A function of type fun_inner indicates that DDF should attempt
733 * to attach child devices to the function. fun_exposed means that
734 * the function should be exported to external clients (applications).
735 *
736 * @param dev Device to which we are adding function
737 * @param ftype Type of function (fun_inner or fun_exposed)
738 * @param name Name of function
739 *
740 * @return New function or @c NULL if memory is not available
741 */
[83a2f43]742ddf_fun_t *ddf_fun_create(ddf_dev_t *dev, fun_type_t ftype, const char *name)
[97a62fe]743{
[83a2f43]744 ddf_fun_t *fun;
[97a62fe]745
746 fun = create_function();
747 if (fun == NULL)
748 return NULL;
749
[0f0f8bc]750 /* Add one reference that will be dropped by ddf_fun_destroy() */
[bfe7867]751 fun->dev = dev;
[0f0f8bc]752 fun_add_ref(fun);
753
[97a62fe]754 fun->bound = false;
755 fun->ftype = ftype;
756
757 fun->name = str_dup(name);
758 if (fun->name == NULL) {
759 delete_function(fun);
760 return NULL;
761 }
762
763 return fun;
764}
765
[c5be39b]766/** Allocate driver-specific function data. */
767extern void *ddf_fun_data_alloc(ddf_fun_t *fun, size_t size)
768{
769 void *data;
770
771 assert(fun->bound == false);
772 assert(fun->driver_data == NULL);
773
774 data = calloc(1, size);
775 if (data == NULL)
776 return NULL;
777
778 fun->driver_data = data;
779 return data;
780}
781
[97a62fe]782/** Destroy DDF function node.
783 *
784 * Destroy a function previously created with ddf_fun_create(). The function
785 * must not be bound.
786 *
787 * @param fun Function to destroy
788 */
[83a2f43]789void ddf_fun_destroy(ddf_fun_t *fun)
[97a62fe]790{
791 assert(fun->bound == false);
[0f0f8bc]792
793 /*
794 * Drop the reference added by ddf_fun_create(). This will deallocate
795 * the function as soon as all other references are dropped (i.e.
796 * as soon control leaves all driver entry points called in context
797 * of this function.
798 */
799 fun_del_ref(fun);
[97a62fe]800}
801
[af6b5157]802static void *function_get_ops(ddf_fun_t *fun, dev_inferface_idx_t idx)
[5fdd7c3]803{
804 assert(is_valid_iface_idx(idx));
[8b1e15ac]805 if (fun->ops == NULL)
[5fdd7c3]806 return NULL;
[8b1e15ac]807 return fun->ops->interfaces[idx];
[5fdd7c3]808}
809
[97a62fe]810/** Bind a function node.
811 *
812 * Bind the specified function to the system. This effectively makes
813 * the function visible to the system (uploads it to the server).
814 *
815 * This function can fail for several reasons. Specifically,
816 * it will fail if the device already has a bound function of
817 * the same name.
818 *
819 * @param fun Function to bind
820 * @return EOK on success or negative error code
821 */
[83a2f43]822int ddf_fun_bind(ddf_fun_t *fun)
[7707954]823{
[d0dd7b5]824 assert(fun->bound == false);
[8b1e15ac]825 assert(fun->name != NULL);
[36f2b3e]826
[df747b9c]827 int res;
828
[8b1e15ac]829 add_to_functions_list(fun);
830 res = devman_add_function(fun->name, fun->ftype, &fun->match_ids,
[97a62fe]831 fun->dev->handle, &fun->handle);
[36f2b3e]832 if (res != EOK) {
[8b1e15ac]833 remove_from_functions_list(fun);
[df747b9c]834 return res;
[36f2b3e]835 }
836
[97a62fe]837 fun->bound = true;
[df747b9c]838 return res;
[7707954]839}
840
[d0dd7b5]841/** Unbind a function node.
842 *
843 * Unbind the specified function from the system. This effectively makes
844 * the function invisible to the system.
845 *
[1a5b252]846 * @param fun Function to unbind
[d0dd7b5]847 * @return EOK on success or negative error code
848 */
849int ddf_fun_unbind(ddf_fun_t *fun)
850{
851 int res;
852
853 assert(fun->bound == true);
854
855 res = devman_remove_function(fun->handle);
856 if (res != EOK)
857 return res;
858
859 remove_from_functions_list(fun);
860
861 fun->bound = false;
862 return EOK;
863}
864
[1a5b252]865/** Online function.
866 *
867 * @param fun Function to online
868 * @return EOK on success or negative error code
869 */
870int ddf_fun_online(ddf_fun_t *fun)
871{
872 int res;
873
874 assert(fun->bound == true);
875
876 res = devman_drv_fun_online(fun->handle);
877 if (res != EOK)
878 return res;
879
880 return EOK;
881}
882
883/** Offline function.
884 *
885 * @param fun Function to offline
886 * @return EOK on success or negative error code
887 */
888int ddf_fun_offline(ddf_fun_t *fun)
889{
890 int res;
891
892 assert(fun->bound == true);
893
894 res = devman_drv_fun_offline(fun->handle);
895 if (res != EOK)
896 return res;
897
898 return EOK;
899}
900
[34588a80]901/** Add single match ID to inner function.
[cd0684d]902 *
903 * Construct and add a single match ID to the specified function.
[34588a80]904 * Cannot be called when the function node is bound.
[cd0684d]905 *
906 * @param fun Function
907 * @param match_id_str Match string
908 * @param match_score Match score
909 * @return EOK on success, ENOMEM if out of memory.
910 */
[83a2f43]911int ddf_fun_add_match_id(ddf_fun_t *fun, const char *match_id_str,
[cd0684d]912 int match_score)
913{
914 match_id_t *match_id;
915
[34588a80]916 assert(fun->bound == false);
917 assert(fun->ftype == fun_inner);
918
[cd0684d]919 match_id = create_match_id();
920 if (match_id == NULL)
921 return ENOMEM;
922
[ef9460b]923 match_id->id = str_dup(match_id_str);
[cd0684d]924 match_id->score = 90;
925
926 add_match_id(&fun->match_ids, match_id);
927 return EOK;
928}
929
[5fdd7c3]930/** Get default handler for client requests */
[af6b5157]931static remote_handler_t *function_get_default_handler(ddf_fun_t *fun)
[5fdd7c3]932{
[8b1e15ac]933 if (fun->ops == NULL)
[5fdd7c3]934 return NULL;
[8b1e15ac]935 return fun->ops->default_handler;
[5fdd7c3]936}
937
[1dc4a5e]938/** Add exposed function to category.
[34588a80]939 *
940 * Must only be called when the function is bound.
941 */
[1dc4a5e]942int ddf_fun_add_to_category(ddf_fun_t *fun, const char *cat_name)
[5fdd7c3]943{
[34588a80]944 assert(fun->bound == true);
945 assert(fun->ftype == fun_exposed);
946
[1dc4a5e]947 return devman_add_device_to_category(fun->handle, cat_name);
[5fdd7c3]948}
949
[83a2f43]950int ddf_driver_main(driver_t *drv)
[c16cf62]951{
[033cbf82]952 int rc;
953
[7a252ec8]954 /*
955 * Remember the driver structure - driver_ops will be called by generic
956 * handler for incoming connections.
957 */
[c16cf62]958 driver = drv;
[36f2b3e]959
[7a252ec8]960 /* Initialize the list of interrupt contexts. */
[7f8b581]961 init_interrupt_context_list(&interrupt_contexts);
962
[7a252ec8]963 /* Set generic interrupt handler. */
[7f8b581]964 async_set_interrupt_received(driver_irq_handler);
965
[7a252ec8]966 /*
[033cbf82]967 * Register driver with device manager using generic handler for
968 * incoming connections.
[7a252ec8]969 */
[033cbf82]970 rc = devman_driver_register(driver->name, driver_connection);
971 if (rc != EOK) {
[3ad7b1c]972 printf("Error: Failed to register driver with device manager "
973 "(%s).\n", (rc == EEXISTS) ? "driver already started" :
974 str_error(rc));
975
[033cbf82]976 return 1;
977 }
[36f2b3e]978
[26fa82bc]979 /* Return success from the task since server has started. */
980 rc = task_retval(0);
981 if (rc != EOK)
982 return 1;
983
[c16cf62]984 async_manager();
[36f2b3e]985
[7a252ec8]986 /* Never reached. */
[52b7b1bb]987 return 0;
[c16cf62]988}
989
990/**
991 * @}
[52b7b1bb]992 */
Note: See TracBrowser for help on using the repository browser.