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

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

ISA bridge remove support.

  • 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);
[f278930]315 if (dev != NULL)
316 dev_add_ref(dev);
[1a5b252]317 fibril_mutex_unlock(&devices_mutex);
318
319 if (dev == NULL) {
320 async_answer_0(iid, ENOENT);
321 return;
322 }
323
324 if (driver->driver_ops->dev_remove != NULL)
325 rc = driver->driver_ops->dev_remove(dev);
326 else
327 rc = ENOTSUP;
328
[0f0f8bc]329 if (rc == EOK)
330 dev_del_ref(dev);
331
[1a5b252]332 async_answer_0(iid, (sysarg_t) rc);
333}
334
335static void driver_fun_online(ipc_callid_t iid, ipc_call_t *icall)
336{
337 devman_handle_t funh;
338 ddf_fun_t *fun;
339 int rc;
340
341 funh = IPC_GET_ARG1(*icall);
[0f0f8bc]342
343 /*
[4d94002d]344 * Look the function up. Bump reference count so that
[0f0f8bc]345 * the function continues to exist until we return
346 * from the driver.
347 */
[1a5b252]348 fibril_mutex_lock(&functions_mutex);
[0f0f8bc]349
[1a5b252]350 fun = driver_get_function(funh);
[0f0f8bc]351 if (fun != NULL)
352 fun_add_ref(fun);
353
[1a5b252]354 fibril_mutex_unlock(&functions_mutex);
355
356 if (fun == NULL) {
357 async_answer_0(iid, ENOENT);
358 return;
359 }
360
[0f0f8bc]361 /* Call driver entry point */
[1a5b252]362 if (driver->driver_ops->fun_online != NULL)
363 rc = driver->driver_ops->fun_online(fun);
364 else
365 rc = ENOTSUP;
366
[0f0f8bc]367 fun_del_ref(fun);
368
[1a5b252]369 async_answer_0(iid, (sysarg_t) rc);
370}
371
372static void driver_fun_offline(ipc_callid_t iid, ipc_call_t *icall)
373{
374 devman_handle_t funh;
375 ddf_fun_t *fun;
376 int rc;
377
378 funh = IPC_GET_ARG1(*icall);
[0f0f8bc]379
380 /*
[4d94002d]381 * Look the function up. Bump reference count so that
[0f0f8bc]382 * the function continues to exist until we return
383 * from the driver.
384 */
[1a5b252]385 fibril_mutex_lock(&functions_mutex);
[0f0f8bc]386
[1a5b252]387 fun = driver_get_function(funh);
[0f0f8bc]388 if (fun != NULL)
389 fun_add_ref(fun);
390
[1a5b252]391 fibril_mutex_unlock(&functions_mutex);
392
393 if (fun == NULL) {
394 async_answer_0(iid, ENOENT);
395 return;
396 }
397
[0f0f8bc]398 /* Call driver entry point */
[1a5b252]399 if (driver->driver_ops->fun_offline != NULL)
400 rc = driver->driver_ops->fun_offline(fun);
401 else
402 rc = ENOTSUP;
403
404 async_answer_0(iid, (sysarg_t) rc);
405}
406
[c16cf62]407static void driver_connection_devman(ipc_callid_t iid, ipc_call_t *icall)
408{
409 /* Accept connection */
[ffa2c8ef]410 async_answer_0(iid, EOK);
[36f2b3e]411
[79ae36dd]412 while (true) {
[c16cf62]413 ipc_call_t call;
414 ipc_callid_t callid = async_get_call(&call);
[36f2b3e]415
[79ae36dd]416 if (!IPC_GET_IMETHOD(call))
417 break;
418
[228e490]419 switch (IPC_GET_IMETHOD(call)) {
[1a5b252]420 case DRIVER_DEV_ADD:
[0f0f8bc]421 driver_dev_add(callid, &call);
[c16cf62]422 break;
[1a5b252]423 case DRIVER_DEV_REMOVE:
424 driver_dev_remove(callid, &call);
425 break;
426 case DRIVER_FUN_ONLINE:
427 driver_fun_online(callid, &call);
428 break;
429 case DRIVER_FUN_OFFLINE:
430 driver_fun_offline(callid, &call);
431 break;
[c16cf62]432 default:
[1a5b252]433 async_answer_0(callid, ENOTSUP);
[c16cf62]434 }
[52b7b1bb]435 }
[c16cf62]436}
437
[79ae36dd]438/** Generic client connection handler both for applications and drivers.
439 *
440 * @param drv True for driver client, false for other clients
441 * (applications, services, etc.).
[52b7b1bb]442 *
[a1769ee]443 */
[9a66bc2e]444static void driver_connection_gen(ipc_callid_t iid, ipc_call_t *icall, bool drv)
[52b7b1bb]445{
[7a252ec8]446 /*
447 * Answer the first IPC_M_CONNECT_ME_TO call and remember the handle of
448 * the device to which the client connected.
449 */
[0b5a4131]450 devman_handle_t handle = IPC_GET_ARG2(*icall);
[1a5b252]451
452 fibril_mutex_lock(&functions_mutex);
453 ddf_fun_t *fun = driver_get_function(handle);
454 fibril_mutex_unlock(&functions_mutex);
455 /* XXX Need a lock on fun */
[79ae36dd]456
[8b1e15ac]457 if (fun == NULL) {
458 printf("%s: driver_connection_gen error - no function with handle"
[7e752b2]459 " %" PRIun " was found.\n", driver->name, handle);
[ffa2c8ef]460 async_answer_0(iid, ENOENT);
[a1769ee]461 return;
462 }
[5cd136ab]463
[ff65e91]464 if (fun->conn_handler != NULL) {
465 /* Driver has a custom connection handler. */
466 (*fun->conn_handler)(iid, icall, (void *)fun);
467 return;
468 }
469
[7a252ec8]470 /*
471 * TODO - if the client is not a driver, check whether it is allowed to
472 * use the device.
473 */
[36f2b3e]474
[25a7e11d]475 int ret = EOK;
[8b1e15ac]476 /* Open device function */
477 if (fun->ops != NULL && fun->ops->open != NULL)
478 ret = (*fun->ops->open)(fun);
[a1769ee]479
[ffa2c8ef]480 async_answer_0(iid, ret);
[36f2b3e]481 if (ret != EOK)
[a6e54c5d]482 return;
[36f2b3e]483
[79ae36dd]484 while (true) {
[a1769ee]485 ipc_callid_t callid;
486 ipc_call_t call;
487 callid = async_get_call(&call);
[228e490]488 sysarg_t method = IPC_GET_IMETHOD(call);
[3843ecb]489
[79ae36dd]490 if (!method) {
[8b1e15ac]491 /* Close device function */
492 if (fun->ops != NULL && fun->ops->close != NULL)
493 (*fun->ops->close)(fun);
[ffa2c8ef]494 async_answer_0(callid, EOK);
[a1769ee]495 return;
[79ae36dd]496 }
497
498 /* Convert ipc interface id to interface index */
499
500 int iface_idx = DEV_IFACE_IDX(method);
501
502 if (!is_valid_iface_idx(iface_idx)) {
503 remote_handler_t *default_handler =
504 function_get_default_handler(fun);
505 if (default_handler != NULL) {
506 (*default_handler)(fun, callid, &call);
[79a141a]507 continue;
[52b7b1bb]508 }
509
[7a252ec8]510 /*
[79ae36dd]511 * Function has no such interface and
512 * default handler is not provided.
[7a252ec8]513 */
[79ae36dd]514 printf("%s: driver_connection_gen error - "
515 "invalid interface id %d.",
516 driver->name, iface_idx);
517 async_answer_0(callid, ENOTSUP);
[79a141a]518 continue;
[79ae36dd]519 }
520
521 /* Calling one of the function's interfaces */
522
523 /* Get the interface ops structure. */
524 void *ops = function_get_ops(fun, iface_idx);
525 if (ops == NULL) {
526 printf("%s: driver_connection_gen error - ", driver->name);
527 printf("Function with handle %" PRIun " has no interface "
528 "with id %d.\n", handle, iface_idx);
529 async_answer_0(callid, ENOTSUP);
[79a141a]530 continue;
[79ae36dd]531 }
532
533 /*
534 * Get the corresponding interface for remote request
535 * handling ("remote interface").
536 */
537 remote_iface_t *rem_iface = get_remote_iface(iface_idx);
538 assert(rem_iface != NULL);
539
540 /* get the method of the remote interface */
541 sysarg_t iface_method_idx = IPC_GET_ARG1(call);
542 remote_iface_func_ptr_t iface_method_ptr =
543 get_remote_method(rem_iface, iface_method_idx);
544 if (iface_method_ptr == NULL) {
545 /* The interface has not such method */
546 printf("%s: driver_connection_gen error - "
547 "invalid interface method.", driver->name);
548 async_answer_0(callid, ENOTSUP);
[79a141a]549 continue;
[a1769ee]550 }
[79ae36dd]551
552 /*
553 * Call the remote interface's method, which will
554 * receive parameters from the remote client and it will
555 * pass it to the corresponding local interface method
556 * associated with the function by its driver.
557 */
558 (*iface_method_ptr)(fun, ops, callid, &call);
[a1769ee]559 }
560}
561
[c16cf62]562static void driver_connection_driver(ipc_callid_t iid, ipc_call_t *icall)
563{
[52b7b1bb]564 driver_connection_gen(iid, icall, true);
[c16cf62]565}
566
567static void driver_connection_client(ipc_callid_t iid, ipc_call_t *icall)
568{
[52b7b1bb]569 driver_connection_gen(iid, icall, false);
[c16cf62]570}
571
[7a252ec8]572/** Function for handling connections to device driver. */
[9934f7d]573static void driver_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
[c16cf62]574{
[45059d6b]575 sysarg_t conn_type;
576
577 if (iid == 0) {
578 /* Callback connection from devman */
579 /* XXX Use separate handler for this type of connection */
580 conn_type = DRIVER_DEVMAN;
581 } else {
582 conn_type = IPC_GET_ARG1(*icall);
583 }
584
[c16cf62]585 /* Select interface */
[45059d6b]586 switch (conn_type) {
[c16cf62]587 case DRIVER_DEVMAN:
[36f2b3e]588 /* Handle request from device manager */
[c16cf62]589 driver_connection_devman(iid, icall);
590 break;
591 case DRIVER_DRIVER:
[36f2b3e]592 /* Handle request from drivers of child devices */
[c16cf62]593 driver_connection_driver(iid, icall);
594 break;
595 case DRIVER_CLIENT:
[36f2b3e]596 /* Handle request from client applications */
[c16cf62]597 driver_connection_client(iid, icall);
598 break;
599 default:
[52b7b1bb]600 /* No such interface */
[ffa2c8ef]601 async_answer_0(iid, ENOENT);
[c16cf62]602 }
603}
604
[5fdd7c3]605/** Create new device structure.
606 *
607 * @return The device structure.
608 */
[83a2f43]609static ddf_dev_t *create_device(void)
[5fdd7c3]610{
[83a2f43]611 ddf_dev_t *dev;
[5fdd7c3]612
[0f0f8bc]613 dev = calloc(1, sizeof(ddf_dev_t));
[8b1e15ac]614 if (dev == NULL)
615 return NULL;
[5fdd7c3]616
617 return dev;
618}
619
[8b1e15ac]620/** Create new function structure.
621 *
622 * @return The device structure.
623 */
[83a2f43]624static ddf_fun_t *create_function(void)
[8b1e15ac]625{
[83a2f43]626 ddf_fun_t *fun;
[8b1e15ac]627
[83a2f43]628 fun = calloc(1, sizeof(ddf_fun_t));
[8b1e15ac]629 if (fun == NULL)
630 return NULL;
631
632 init_match_ids(&fun->match_ids);
633 link_initialize(&fun->link);
634
635 return fun;
636}
637
[5fdd7c3]638/** Delete device structure.
639 *
640 * @param dev The device structure.
641 */
[83a2f43]642static void delete_device(ddf_dev_t *dev)
[5fdd7c3]643{
[5f6e25e]644 if (dev->driver_data != NULL)
645 free(dev->driver_data);
[5fdd7c3]646 free(dev);
647}
648
[0f0f8bc]649/** Delete function structure.
[8b1e15ac]650 *
651 * @param dev The device structure.
652 */
[83a2f43]653static void delete_function(ddf_fun_t *fun)
[8b1e15ac]654{
655 clean_match_ids(&fun->match_ids);
[5f6e25e]656 if (fun->driver_data != NULL)
657 free(fun->driver_data);
[8b1e15ac]658 if (fun->name != NULL)
659 free(fun->name);
660 free(fun);
661}
662
[0f0f8bc]663/** Increase device reference count. */
664static void dev_add_ref(ddf_dev_t *dev)
665{
666 atomic_inc(&dev->refcnt);
667}
668
669/** Decrease device reference count.
670 *
671 * Free the device structure if the reference count drops to zero.
672 */
673static void dev_del_ref(ddf_dev_t *dev)
674{
675 if (atomic_predec(&dev->refcnt) == 0)
676 delete_device(dev);
677}
678
[bfe7867]679/** Increase function reference count.
680 *
681 * This also increases reference count on the device. The device structure
682 * will thus not be deallocated while there are some associated function
683 * structures.
684 */
[0f0f8bc]685static void fun_add_ref(ddf_fun_t *fun)
686{
[bfe7867]687 dev_add_ref(fun->dev);
[0f0f8bc]688 atomic_inc(&fun->refcnt);
689}
690
691/** Decrease function reference count.
692 *
693 * Free the function structure if the reference count drops to zero.
694 */
695static void fun_del_ref(ddf_fun_t *fun)
696{
[bfe7867]697 ddf_dev_t *dev = fun->dev;
698
[0f0f8bc]699 if (atomic_predec(&fun->refcnt) == 0)
700 delete_function(fun);
[bfe7867]701
702 dev_del_ref(dev);
[0f0f8bc]703}
704
[c5be39b]705/** Allocate driver-specific device data. */
706extern void *ddf_dev_data_alloc(ddf_dev_t *dev, size_t size)
707{
708 void *data;
709
710 assert(dev->driver_data == NULL);
711
712 data = calloc(1, size);
713 if (data == NULL)
714 return NULL;
715
716 dev->driver_data = data;
717 return data;
718}
719
[97a62fe]720/** Create a DDF function node.
721 *
722 * Create a DDF function (in memory). Both child devices and external clients
723 * communicate with a device via its functions.
724 *
725 * The created function node is fully formed, but only exists in the memory
726 * of the client task. In order to be visible to the system, the function
727 * must be bound using ddf_fun_bind().
728 *
729 * This function should only fail if there is not enough free memory.
730 * Specifically, this function succeeds even if @a dev already has
731 * a (bound) function with the same name.
732 *
733 * Type: A function of type fun_inner indicates that DDF should attempt
734 * to attach child devices to the function. fun_exposed means that
735 * the function should be exported to external clients (applications).
736 *
737 * @param dev Device to which we are adding function
738 * @param ftype Type of function (fun_inner or fun_exposed)
739 * @param name Name of function
740 *
741 * @return New function or @c NULL if memory is not available
742 */
[83a2f43]743ddf_fun_t *ddf_fun_create(ddf_dev_t *dev, fun_type_t ftype, const char *name)
[97a62fe]744{
[83a2f43]745 ddf_fun_t *fun;
[97a62fe]746
747 fun = create_function();
748 if (fun == NULL)
749 return NULL;
750
[0f0f8bc]751 /* Add one reference that will be dropped by ddf_fun_destroy() */
[bfe7867]752 fun->dev = dev;
[0f0f8bc]753 fun_add_ref(fun);
754
[97a62fe]755 fun->bound = false;
756 fun->ftype = ftype;
757
758 fun->name = str_dup(name);
759 if (fun->name == NULL) {
760 delete_function(fun);
761 return NULL;
762 }
763
764 return fun;
765}
766
[c5be39b]767/** Allocate driver-specific function data. */
768extern void *ddf_fun_data_alloc(ddf_fun_t *fun, size_t size)
769{
770 void *data;
771
772 assert(fun->bound == false);
773 assert(fun->driver_data == NULL);
774
775 data = calloc(1, size);
776 if (data == NULL)
777 return NULL;
778
779 fun->driver_data = data;
780 return data;
781}
782
[97a62fe]783/** Destroy DDF function node.
784 *
785 * Destroy a function previously created with ddf_fun_create(). The function
786 * must not be bound.
787 *
788 * @param fun Function to destroy
789 */
[83a2f43]790void ddf_fun_destroy(ddf_fun_t *fun)
[97a62fe]791{
792 assert(fun->bound == false);
[0f0f8bc]793
794 /*
795 * Drop the reference added by ddf_fun_create(). This will deallocate
796 * the function as soon as all other references are dropped (i.e.
797 * as soon control leaves all driver entry points called in context
798 * of this function.
799 */
800 fun_del_ref(fun);
[97a62fe]801}
802
[af6b5157]803static void *function_get_ops(ddf_fun_t *fun, dev_inferface_idx_t idx)
[5fdd7c3]804{
805 assert(is_valid_iface_idx(idx));
[8b1e15ac]806 if (fun->ops == NULL)
[5fdd7c3]807 return NULL;
[8b1e15ac]808 return fun->ops->interfaces[idx];
[5fdd7c3]809}
810
[97a62fe]811/** Bind a function node.
812 *
813 * Bind the specified function to the system. This effectively makes
814 * the function visible to the system (uploads it to the server).
815 *
816 * This function can fail for several reasons. Specifically,
817 * it will fail if the device already has a bound function of
818 * the same name.
819 *
820 * @param fun Function to bind
821 * @return EOK on success or negative error code
822 */
[83a2f43]823int ddf_fun_bind(ddf_fun_t *fun)
[7707954]824{
[d0dd7b5]825 assert(fun->bound == false);
[8b1e15ac]826 assert(fun->name != NULL);
[36f2b3e]827
[df747b9c]828 int res;
829
[8b1e15ac]830 add_to_functions_list(fun);
831 res = devman_add_function(fun->name, fun->ftype, &fun->match_ids,
[97a62fe]832 fun->dev->handle, &fun->handle);
[36f2b3e]833 if (res != EOK) {
[8b1e15ac]834 remove_from_functions_list(fun);
[df747b9c]835 return res;
[36f2b3e]836 }
837
[97a62fe]838 fun->bound = true;
[df747b9c]839 return res;
[7707954]840}
841
[d0dd7b5]842/** Unbind a function node.
843 *
844 * Unbind the specified function from the system. This effectively makes
845 * the function invisible to the system.
846 *
[1a5b252]847 * @param fun Function to unbind
[d0dd7b5]848 * @return EOK on success or negative error code
849 */
850int ddf_fun_unbind(ddf_fun_t *fun)
851{
852 int res;
853
854 assert(fun->bound == true);
855
856 res = devman_remove_function(fun->handle);
857 if (res != EOK)
858 return res;
859
860 remove_from_functions_list(fun);
861
862 fun->bound = false;
863 return EOK;
864}
865
[1a5b252]866/** Online function.
867 *
868 * @param fun Function to online
869 * @return EOK on success or negative error code
870 */
871int ddf_fun_online(ddf_fun_t *fun)
872{
873 int res;
874
875 assert(fun->bound == true);
876
877 res = devman_drv_fun_online(fun->handle);
878 if (res != EOK)
879 return res;
880
881 return EOK;
882}
883
884/** Offline function.
885 *
886 * @param fun Function to offline
887 * @return EOK on success or negative error code
888 */
889int ddf_fun_offline(ddf_fun_t *fun)
890{
891 int res;
892
893 assert(fun->bound == true);
894
895 res = devman_drv_fun_offline(fun->handle);
896 if (res != EOK)
897 return res;
898
899 return EOK;
900}
901
[34588a80]902/** Add single match ID to inner function.
[cd0684d]903 *
904 * Construct and add a single match ID to the specified function.
[34588a80]905 * Cannot be called when the function node is bound.
[cd0684d]906 *
907 * @param fun Function
908 * @param match_id_str Match string
909 * @param match_score Match score
910 * @return EOK on success, ENOMEM if out of memory.
911 */
[83a2f43]912int ddf_fun_add_match_id(ddf_fun_t *fun, const char *match_id_str,
[cd0684d]913 int match_score)
914{
915 match_id_t *match_id;
916
[34588a80]917 assert(fun->bound == false);
918 assert(fun->ftype == fun_inner);
919
[cd0684d]920 match_id = create_match_id();
921 if (match_id == NULL)
922 return ENOMEM;
923
[ef9460b]924 match_id->id = str_dup(match_id_str);
[cd0684d]925 match_id->score = 90;
926
927 add_match_id(&fun->match_ids, match_id);
928 return EOK;
929}
930
[5fdd7c3]931/** Get default handler for client requests */
[af6b5157]932static remote_handler_t *function_get_default_handler(ddf_fun_t *fun)
[5fdd7c3]933{
[8b1e15ac]934 if (fun->ops == NULL)
[5fdd7c3]935 return NULL;
[8b1e15ac]936 return fun->ops->default_handler;
[5fdd7c3]937}
938
[1dc4a5e]939/** Add exposed function to category.
[34588a80]940 *
941 * Must only be called when the function is bound.
942 */
[1dc4a5e]943int ddf_fun_add_to_category(ddf_fun_t *fun, const char *cat_name)
[5fdd7c3]944{
[34588a80]945 assert(fun->bound == true);
946 assert(fun->ftype == fun_exposed);
947
[1dc4a5e]948 return devman_add_device_to_category(fun->handle, cat_name);
[5fdd7c3]949}
950
[83a2f43]951int ddf_driver_main(driver_t *drv)
[c16cf62]952{
[033cbf82]953 int rc;
954
[7a252ec8]955 /*
956 * Remember the driver structure - driver_ops will be called by generic
957 * handler for incoming connections.
958 */
[c16cf62]959 driver = drv;
[36f2b3e]960
[7a252ec8]961 /* Initialize the list of interrupt contexts. */
[7f8b581]962 init_interrupt_context_list(&interrupt_contexts);
963
[7a252ec8]964 /* Set generic interrupt handler. */
[7f8b581]965 async_set_interrupt_received(driver_irq_handler);
966
[7a252ec8]967 /*
[033cbf82]968 * Register driver with device manager using generic handler for
969 * incoming connections.
[7a252ec8]970 */
[033cbf82]971 rc = devman_driver_register(driver->name, driver_connection);
972 if (rc != EOK) {
[3ad7b1c]973 printf("Error: Failed to register driver with device manager "
974 "(%s).\n", (rc == EEXISTS) ? "driver already started" :
975 str_error(rc));
976
[033cbf82]977 return 1;
978 }
[36f2b3e]979
[26fa82bc]980 /* Return success from the task since server has started. */
981 rc = task_retval(0);
982 if (rc != EOK)
983 return 1;
984
[c16cf62]985 async_manager();
[36f2b3e]986
[7a252ec8]987 /* Never reached. */
[52b7b1bb]988 return 0;
[c16cf62]989}
990
991/**
992 * @}
[52b7b1bb]993 */
Note: See TracBrowser for help on using the repository browser.