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

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

Allow multiple tasks to register for loc category change events.
Open NIC devices via location service, discover using category change events.
Eliminate device_added driver entry point.
loc_service_get_name() should return fully qualified service name.

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