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

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

Finish splitting device node: devman client in C library, drv library. Update device drivers accordingly.

  • Property mode set to 100644
File size: 14.7 KB
RevLine 
[c16cf62]1/*
2 * Copyright (c) 2010 Lenka Trochtova
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/**
30 * @defgroup libdrv generic device driver support.
31 * @brief HelenOS generic device driver support.
32 * @{
33 */
34
35/** @file
36 */
[52b7b1bb]37
[c16cf62]38#include <assert.h>
39#include <ipc/services.h>
40#include <ipc/ns.h>
41#include <async.h>
42#include <stdio.h>
43#include <errno.h>
44#include <bool.h>
45#include <fibril_synch.h>
46#include <stdlib.h>
[c47e1a8]47#include <str.h>
[c16cf62]48#include <ctype.h>
[66babbd]49#include <errno.h>
[7e752b2]50#include <inttypes.h>
[c16cf62]51
[084ff99]52#include <ipc/driver.h>
[c16cf62]53
[5fdd7c3]54#include "dev_iface.h"
[c16cf62]55#include "driver.h"
56
[36f2b3e]57/** Driver structure */
[c16cf62]58static driver_t *driver;
[7f8b581]59
[36f2b3e]60/** Devices */
[8b1e15ac]61LIST_INITIALIZE(functions);
62FIBRIL_MUTEX_INITIALIZE(functions_mutex);
[084ff99]63
[36f2b3e]64/** Interrupts */
[7f8b581]65static interrupt_context_list_t interrupt_contexts;
66
67static irq_cmd_t default_cmds[] = {
68 {
69 .cmd = CMD_ACCEPT
70 }
71};
72
73static irq_code_t default_pseudocode = {
74 sizeof(default_cmds) / sizeof(irq_cmd_t),
75 default_cmds
76};
77
78
79static void driver_irq_handler(ipc_callid_t iid, ipc_call_t *icall)
[7a252ec8]80{
[228e490]81 int id = (int)IPC_GET_IMETHOD(*icall);
[7a252ec8]82 interrupt_context_t *ctx;
83
84 ctx = find_interrupt_context_by_id(&interrupt_contexts, id);
[36f2b3e]85 if (ctx != NULL && ctx->handler != NULL)
[7a252ec8]86 (*ctx->handler)(ctx->dev, iid, icall);
[7f8b581]87}
88
[5fdd7c3]89interrupt_context_t *create_interrupt_context(void)
90{
91 interrupt_context_t *ctx;
92
93 ctx = (interrupt_context_t *) malloc(sizeof(interrupt_context_t));
94 if (ctx != NULL)
95 memset(ctx, 0, sizeof(interrupt_context_t));
96
97 return ctx;
98}
99
100void delete_interrupt_context(interrupt_context_t *ctx)
101{
102 if (ctx != NULL)
103 free(ctx);
104}
105
106void init_interrupt_context_list(interrupt_context_list_t *list)
107{
108 memset(list, 0, sizeof(interrupt_context_list_t));
109 fibril_mutex_initialize(&list->mutex);
110 list_initialize(&list->contexts);
111}
112
113void
114add_interrupt_context(interrupt_context_list_t *list, interrupt_context_t *ctx)
115{
116 fibril_mutex_lock(&list->mutex);
117 ctx->id = list->curr_id++;
118 list_append(&ctx->link, &list->contexts);
119 fibril_mutex_unlock(&list->mutex);
120}
121
122void remove_interrupt_context(interrupt_context_list_t *list,
123 interrupt_context_t *ctx)
124{
125 fibril_mutex_lock(&list->mutex);
126 list_remove(&ctx->link);
127 fibril_mutex_unlock(&list->mutex);
128}
129
130interrupt_context_t *
131find_interrupt_context_by_id(interrupt_context_list_t *list, int id)
132{
133 fibril_mutex_lock(&list->mutex);
134
135 link_t *link = list->contexts.next;
136 interrupt_context_t *ctx;
137
138 while (link != &list->contexts) {
139 ctx = list_get_instance(link, interrupt_context_t, link);
140 if (ctx->id == id) {
141 fibril_mutex_unlock(&list->mutex);
142 return ctx;
143 }
144 link = link->next;
145 }
146
147 fibril_mutex_unlock(&list->mutex);
148 return NULL;
149}
150
151interrupt_context_t *
152find_interrupt_context(interrupt_context_list_t *list, device_t *dev, int irq)
153{
154 fibril_mutex_lock(&list->mutex);
155
156 link_t *link = list->contexts.next;
157 interrupt_context_t *ctx;
158
159 while (link != &list->contexts) {
160 ctx = list_get_instance(link, interrupt_context_t, link);
161 if (ctx->irq == irq && ctx->dev == dev) {
162 fibril_mutex_unlock(&list->mutex);
163 return ctx;
164 }
165 link = link->next;
166 }
167
168 fibril_mutex_unlock(&list->mutex);
169 return NULL;
170}
171
172
[7a252ec8]173int
174register_interrupt_handler(device_t *dev, int irq, interrupt_handler_t *handler,
175 irq_code_t *pseudocode)
[7f8b581]176{
177 interrupt_context_t *ctx = create_interrupt_context();
178
179 ctx->dev = dev;
180 ctx->irq = irq;
181 ctx->handler = handler;
182
183 add_interrupt_context(&interrupt_contexts, ctx);
184
[36f2b3e]185 if (pseudocode == NULL)
[7f8b581]186 pseudocode = &default_pseudocode;
187
[ffa2c8ef]188 int res = register_irq(irq, dev->handle, ctx->id, pseudocode);
[36f2b3e]189 if (res != EOK) {
[7f8b581]190 remove_interrupt_context(&interrupt_contexts, ctx);
191 delete_interrupt_context(ctx);
192 }
[7a252ec8]193
194 return res;
[7f8b581]195}
196
197int unregister_interrupt_handler(device_t *dev, int irq)
198{
[7a252ec8]199 interrupt_context_t *ctx = find_interrupt_context(&interrupt_contexts,
200 dev, irq);
[ffa2c8ef]201 int res = unregister_irq(irq, dev->handle);
[36f2b3e]202
203 if (ctx != NULL) {
[7f8b581]204 remove_interrupt_context(&interrupt_contexts, ctx);
[7a252ec8]205 delete_interrupt_context(ctx);
[7f8b581]206 }
[36f2b3e]207
[7f8b581]208 return res;
209}
210
[8b1e15ac]211static void add_to_functions_list(function_t *fun)
[9a66bc2e]212{
[8b1e15ac]213 fibril_mutex_lock(&functions_mutex);
214 list_append(&fun->link, &functions);
215 fibril_mutex_unlock(&functions_mutex);
[9a66bc2e]216}
217
[8b1e15ac]218static void remove_from_functions_list(function_t *fun)
[9a66bc2e]219{
[8b1e15ac]220 fibril_mutex_lock(&functions_mutex);
221 list_remove(&fun->link);
222 fibril_mutex_unlock(&functions_mutex);
[9a66bc2e]223}
224
[8b1e15ac]225static function_t *driver_get_function(link_t *functions, devman_handle_t handle)
[52b7b1bb]226{
[8b1e15ac]227 function_t *fun = NULL;
228 printf("driver_get_function handle=%" PRIun "\n", handle);
229
230 fibril_mutex_lock(&functions_mutex);
231 link_t *link = functions->next;
232
233 while (link != functions) {
234 fun = list_get_instance(link, function_t, link);
235 printf(" - fun handle %" PRIun "\n", fun->handle);
236 if (fun->handle == handle) {
237 fibril_mutex_unlock(&functions_mutex);
238 return fun;
[a1769ee]239 }
[8b1e15ac]240
[9a66bc2e]241 link = link->next;
[a1769ee]242 }
[36f2b3e]243
[8b1e15ac]244 fibril_mutex_unlock(&functions_mutex);
[36f2b3e]245
[a1769ee]246 return NULL;
247}
248
[52b7b1bb]249static void driver_add_device(ipc_callid_t iid, ipc_call_t *icall)
[084ff99]250{
[df747b9c]251 char *dev_name = NULL;
[36f2b3e]252 int res;
[9a66bc2e]253
[d35ac1d]254 devman_handle_t dev_handle = IPC_GET_ARG1(*icall);
[8b1e15ac]255 devman_handle_t parent_fun_handle = IPC_GET_ARG2(*icall);
[d35ac1d]256
[5af21c5]257 device_t *dev = create_device();
[084ff99]258 dev->handle = dev_handle;
[8b1e15ac]259
[7a252ec8]260 async_data_write_accept((void **) &dev_name, true, 0, 0, 0, 0);
[df747b9c]261 dev->name = dev_name;
[8b1e15ac]262
263 /*
264 * Currently not used, parent fun handle is stored in context
265 * of the connection to the parent device driver.
266 */
267 (void) parent_fun_handle;
[0d6915f]268
[df747b9c]269 res = driver->driver_ops->add_device(dev);
[36f2b3e]270 if (res == EOK) {
[7e752b2]271 printf("%s: new device with handle=%" PRIun " was added.\n",
[7a252ec8]272 driver->name, dev_handle);
[9a66bc2e]273 } else {
[7e752b2]274 printf("%s: failed to add a new device with handle = %" PRIun ".\n",
[7a252ec8]275 driver->name, dev_handle);
276 delete_device(dev);
[084ff99]277 }
[df747b9c]278
[ffa2c8ef]279 async_answer_0(iid, res);
[084ff99]280}
[c16cf62]281
282static void driver_connection_devman(ipc_callid_t iid, ipc_call_t *icall)
283{
284 /* Accept connection */
[ffa2c8ef]285 async_answer_0(iid, EOK);
[36f2b3e]286
[c16cf62]287 bool cont = true;
288 while (cont) {
289 ipc_call_t call;
290 ipc_callid_t callid = async_get_call(&call);
[36f2b3e]291
[228e490]292 switch (IPC_GET_IMETHOD(call)) {
[c16cf62]293 case IPC_M_PHONE_HUNGUP:
294 cont = false;
295 continue;
296 case DRIVER_ADD_DEVICE:
[084ff99]297 driver_add_device(callid, &call);
[c16cf62]298 break;
299 default:
[ffa2c8ef]300 async_answer_0(callid, ENOENT);
[c16cf62]301 }
[52b7b1bb]302 }
[c16cf62]303}
304
[52b7b1bb]305/**
[a1769ee]306 * Generic client connection handler both for applications and drivers.
[52b7b1bb]307 *
[7a252ec8]308 * @param drv True for driver client, false for other clients
309 * (applications, services etc.).
[a1769ee]310 */
[9a66bc2e]311static void driver_connection_gen(ipc_callid_t iid, ipc_call_t *icall, bool drv)
[52b7b1bb]312{
[7a252ec8]313 /*
314 * Answer the first IPC_M_CONNECT_ME_TO call and remember the handle of
315 * the device to which the client connected.
316 */
[0b5a4131]317 devman_handle_t handle = IPC_GET_ARG2(*icall);
[8b1e15ac]318 function_t *fun = driver_get_function(&functions, handle);
[52b7b1bb]319
[8b1e15ac]320 if (fun == NULL) {
321 printf("%s: driver_connection_gen error - no function with handle"
[7e752b2]322 " %" PRIun " was found.\n", driver->name, handle);
[ffa2c8ef]323 async_answer_0(iid, ENOENT);
[a1769ee]324 return;
325 }
[5cd136ab]326
327
[7a252ec8]328 /*
329 * TODO - if the client is not a driver, check whether it is allowed to
330 * use the device.
331 */
[36f2b3e]332
[25a7e11d]333 int ret = EOK;
[8b1e15ac]334 /* Open device function */
335 if (fun->ops != NULL && fun->ops->open != NULL)
336 ret = (*fun->ops->open)(fun);
[a1769ee]337
[ffa2c8ef]338 async_answer_0(iid, ret);
[36f2b3e]339 if (ret != EOK)
[a6e54c5d]340 return;
[36f2b3e]341
[a1769ee]342 while (1) {
343 ipc_callid_t callid;
344 ipc_call_t call;
345 callid = async_get_call(&call);
[228e490]346 sysarg_t method = IPC_GET_IMETHOD(call);
[3843ecb]347 int iface_idx;
348
[a1769ee]349 switch (method) {
[d35ac1d]350 case IPC_M_PHONE_HUNGUP:
[8b1e15ac]351 /* Close device function */
352 if (fun->ops != NULL && fun->ops->close != NULL)
353 (*fun->ops->close)(fun);
[ffa2c8ef]354 async_answer_0(callid, EOK);
[a1769ee]355 return;
[d35ac1d]356 default:
[7a252ec8]357 /* convert ipc interface id to interface index */
[3843ecb]358
359 iface_idx = DEV_IFACE_IDX(method);
360
361 if (!is_valid_iface_idx(iface_idx)) {
[7a252ec8]362 remote_handler_t *default_handler =
[8b1e15ac]363 function_get_default_handler(fun);
[36f2b3e]364 if (default_handler != NULL) {
[8b1e15ac]365 (*default_handler)(fun, callid, &call);
[08d9525a]366 break;
367 }
[8b1e15ac]368
[7a252ec8]369 /*
[8b1e15ac]370 * Function has no such interface and
[7a252ec8]371 * default handler is not provided.
372 */
373 printf("%s: driver_connection_gen error - "
374 "invalid interface id %d.",
375 driver->name, iface_idx);
[ffa2c8ef]376 async_answer_0(callid, ENOTSUP);
[52b7b1bb]377 break;
378 }
[36f2b3e]379
[8b1e15ac]380 /* calling one of the function's interfaces */
[52b7b1bb]381
[d35ac1d]382 /* Get the interface ops structure. */
[8b1e15ac]383 void *ops = function_get_ops(fun, iface_idx);
[d35ac1d]384 if (ops == NULL) {
[7a252ec8]385 printf("%s: driver_connection_gen error - ",
386 driver->name);
[8b1e15ac]387 printf("Function with handle %" PRIun " has no interface "
[7a252ec8]388 "with id %d.\n", handle, iface_idx);
[ffa2c8ef]389 async_answer_0(callid, ENOTSUP);
[52b7b1bb]390 break;
[a1769ee]391 }
[36f2b3e]392
[7a252ec8]393 /*
394 * Get the corresponding interface for remote request
395 * handling ("remote interface").
396 */
[d35ac1d]397 remote_iface_t *rem_iface = get_remote_iface(iface_idx);
[36f2b3e]398 assert(rem_iface != NULL);
399
[7a252ec8]400 /* get the method of the remote interface */
[96b02eb9]401 sysarg_t iface_method_idx = IPC_GET_ARG1(call);
[7a252ec8]402 remote_iface_func_ptr_t iface_method_ptr =
403 get_remote_method(rem_iface, iface_method_idx);
[36f2b3e]404 if (iface_method_ptr == NULL) {
[52b7b1bb]405 // the interface has not such method
[7a252ec8]406 printf("%s: driver_connection_gen error - "
407 "invalid interface method.", driver->name);
[ffa2c8ef]408 async_answer_0(callid, ENOTSUP);
[52b7b1bb]409 break;
410 }
411
[7a252ec8]412 /*
413 * Call the remote interface's method, which will
414 * receive parameters from the remote client and it will
415 * pass it to the corresponding local interface method
[8b1e15ac]416 * associated with the function by its driver.
[7a252ec8]417 */
[8b1e15ac]418 (*iface_method_ptr)(fun, ops, callid, &call);
[a1769ee]419 break;
420 }
421 }
422}
423
[c16cf62]424static void driver_connection_driver(ipc_callid_t iid, ipc_call_t *icall)
425{
[52b7b1bb]426 driver_connection_gen(iid, icall, true);
[c16cf62]427}
428
429static void driver_connection_client(ipc_callid_t iid, ipc_call_t *icall)
430{
[52b7b1bb]431 driver_connection_gen(iid, icall, false);
[c16cf62]432}
433
[7a252ec8]434/** Function for handling connections to device driver. */
[c16cf62]435static void driver_connection(ipc_callid_t iid, ipc_call_t *icall)
436{
437 /* Select interface */
[96b02eb9]438 switch ((sysarg_t) (IPC_GET_ARG1(*icall))) {
[c16cf62]439 case DRIVER_DEVMAN:
[36f2b3e]440 /* Handle request from device manager */
[c16cf62]441 driver_connection_devman(iid, icall);
442 break;
443 case DRIVER_DRIVER:
[36f2b3e]444 /* Handle request from drivers of child devices */
[c16cf62]445 driver_connection_driver(iid, icall);
446 break;
447 case DRIVER_CLIENT:
[36f2b3e]448 /* Handle request from client applications */
[c16cf62]449 driver_connection_client(iid, icall);
450 break;
451 default:
[52b7b1bb]452 /* No such interface */
[ffa2c8ef]453 async_answer_0(iid, ENOENT);
[c16cf62]454 }
455}
456
[5fdd7c3]457/** Create new device structure.
458 *
459 * @return The device structure.
460 */
461device_t *create_device(void)
462{
[8b1e15ac]463 device_t *dev;
[5fdd7c3]464
[8b1e15ac]465 dev = malloc(sizeof(device_t));
466 if (dev == NULL)
467 return NULL;
[5fdd7c3]468
[8b1e15ac]469 memset(dev, 0, sizeof(device_t));
[5fdd7c3]470 return dev;
471}
472
[8b1e15ac]473/** Create new function structure.
474 *
475 * @return The device structure.
476 */
477function_t *create_function(void)
478{
479 function_t *fun;
480
481 fun = malloc(sizeof(function_t));
482 if (fun == NULL)
483 return NULL;
484
485 memset(fun, 0, sizeof(device_t));
486
487 init_match_ids(&fun->match_ids);
488 link_initialize(&fun->link);
489
490 return fun;
491}
492
[5fdd7c3]493/** Delete device structure.
494 *
495 * @param dev The device structure.
496 */
497void delete_device(device_t *dev)
498{
499 free(dev);
500}
501
[8b1e15ac]502/** Delete device structure.
503 *
504 * @param dev The device structure.
505 */
506void delete_function(function_t *fun)
507{
508 clean_match_ids(&fun->match_ids);
509 if (fun->name != NULL)
510 free(fun->name);
511 free(fun);
512}
513
514void *function_get_ops(function_t *fun, dev_inferface_idx_t idx)
[5fdd7c3]515{
516 assert(is_valid_iface_idx(idx));
[8b1e15ac]517 if (fun->ops == NULL)
[5fdd7c3]518 return NULL;
[8b1e15ac]519 return fun->ops->interfaces[idx];
[5fdd7c3]520}
521
[8b1e15ac]522int register_function(function_t *fun, device_t *dev)
[7707954]523{
[8b1e15ac]524 assert(fun->name != NULL);
[36f2b3e]525
[df747b9c]526 int res;
527
[8b1e15ac]528 fun->dev = dev;
529
530 add_to_functions_list(fun);
531 res = devman_add_function(fun->name, fun->ftype, &fun->match_ids,
532 dev->handle, &fun->handle);
[36f2b3e]533 if (res != EOK) {
[8b1e15ac]534 remove_from_functions_list(fun);
[df747b9c]535 return res;
[36f2b3e]536 }
537
[df747b9c]538 return res;
[7707954]539}
540
[0ca16307]541/** Wrapper for child_device_register for devices with single match id.
542 *
543 * @param parent Parent device.
544 * @param child_name Child device name.
545 * @param child_match_id Child device match id.
546 * @param child_match_score Child device match score.
547 * @return Error code.
548 */
[8b1e15ac]549int register_function_wrapper(device_t *dev, const char *fun_name,
550 const char *match_id, int match_score)
[0ca16307]551{
[8b1e15ac]552 function_t *fun = NULL;
553 match_id_t *m_id = NULL;
[0ca16307]554 int rc;
[36f2b3e]555
[8b1e15ac]556 fun = create_function();
557 if (fun == NULL) {
[0ca16307]558 rc = ENOMEM;
559 goto failure;
560 }
[36f2b3e]561
[8b1e15ac]562 fun->dev = dev;
563 fun->name = fun_name;
564 fun->ftype = fun_inner;
[36f2b3e]565
[8b1e15ac]566 m_id = create_match_id();
567 if (m_id == NULL) {
[0ca16307]568 rc = ENOMEM;
569 goto failure;
570 }
[36f2b3e]571
[8b1e15ac]572 m_id->id = match_id;
573 m_id->score = match_score;
574 add_match_id(&fun->match_ids, m_id);
[36f2b3e]575
[8b1e15ac]576 rc = register_function(fun, dev);
[36f2b3e]577 if (rc != EOK)
[0ca16307]578 goto failure;
[36f2b3e]579
[4006447]580 return EOK;
[36f2b3e]581
[0ca16307]582failure:
[8b1e15ac]583 if (m_id != NULL) {
584 m_id->id = NULL;
585 delete_match_id(m_id);
[0ca16307]586 }
[36f2b3e]587
[8b1e15ac]588 if (fun != NULL) {
589 fun->name = NULL;
590 delete_function(fun);
[0ca16307]591 }
[36f2b3e]592
[0ca16307]593 return rc;
594}
595
[5fdd7c3]596/** Get default handler for client requests */
[8b1e15ac]597remote_handler_t *function_get_default_handler(function_t *fun)
[5fdd7c3]598{
[8b1e15ac]599 if (fun->ops == NULL)
[5fdd7c3]600 return NULL;
[8b1e15ac]601 return fun->ops->default_handler;
[5fdd7c3]602}
603
[8b1e15ac]604int add_function_to_class(function_t *fun, const char *class_name)
[5fdd7c3]605{
[8b1e15ac]606 return devman_add_device_to_class(fun->handle, class_name);
[5fdd7c3]607}
608
[52b7b1bb]609int driver_main(driver_t *drv)
[c16cf62]610{
[7a252ec8]611 /*
612 * Remember the driver structure - driver_ops will be called by generic
613 * handler for incoming connections.
614 */
[c16cf62]615 driver = drv;
[36f2b3e]616
[7a252ec8]617 /* Initialize the list of interrupt contexts. */
[7f8b581]618 init_interrupt_context_list(&interrupt_contexts);
619
[7a252ec8]620 /* Set generic interrupt handler. */
[7f8b581]621 async_set_interrupt_received(driver_irq_handler);
622
[7a252ec8]623 /*
624 * Register driver by device manager with generic handler for incoming
625 * connections.
626 */
[52b7b1bb]627 devman_driver_register(driver->name, driver_connection);
[36f2b3e]628
[c16cf62]629 async_manager();
[36f2b3e]630
[7a252ec8]631 /* Never reached. */
[52b7b1bb]632 return 0;
[c16cf62]633}
634
635/**
636 * @}
[52b7b1bb]637 */
Note: See TracBrowser for help on using the repository browser.