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
Line 
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 */
37
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>
47#include <str.h>
48#include <ctype.h>
49#include <errno.h>
50#include <inttypes.h>
51
52#include <ipc/driver.h>
53
54#include "dev_iface.h"
55#include "driver.h"
56
57/** Driver structure */
58static driver_t *driver;
59
60/** Devices */
61LIST_INITIALIZE(functions);
62FIBRIL_MUTEX_INITIALIZE(functions_mutex);
63
64/** Interrupts */
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)
80{
81 int id = (int)IPC_GET_IMETHOD(*icall);
82 interrupt_context_t *ctx;
83
84 ctx = find_interrupt_context_by_id(&interrupt_contexts, id);
85 if (ctx != NULL && ctx->handler != NULL)
86 (*ctx->handler)(ctx->dev, iid, icall);
87}
88
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
173int
174register_interrupt_handler(device_t *dev, int irq, interrupt_handler_t *handler,
175 irq_code_t *pseudocode)
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
185 if (pseudocode == NULL)
186 pseudocode = &default_pseudocode;
187
188 int res = register_irq(irq, dev->handle, ctx->id, pseudocode);
189 if (res != EOK) {
190 remove_interrupt_context(&interrupt_contexts, ctx);
191 delete_interrupt_context(ctx);
192 }
193
194 return res;
195}
196
197int unregister_interrupt_handler(device_t *dev, int irq)
198{
199 interrupt_context_t *ctx = find_interrupt_context(&interrupt_contexts,
200 dev, irq);
201 int res = unregister_irq(irq, dev->handle);
202
203 if (ctx != NULL) {
204 remove_interrupt_context(&interrupt_contexts, ctx);
205 delete_interrupt_context(ctx);
206 }
207
208 return res;
209}
210
211static void add_to_functions_list(function_t *fun)
212{
213 fibril_mutex_lock(&functions_mutex);
214 list_append(&fun->link, &functions);
215 fibril_mutex_unlock(&functions_mutex);
216}
217
218static void remove_from_functions_list(function_t *fun)
219{
220 fibril_mutex_lock(&functions_mutex);
221 list_remove(&fun->link);
222 fibril_mutex_unlock(&functions_mutex);
223}
224
225static function_t *driver_get_function(link_t *functions, devman_handle_t handle)
226{
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;
239 }
240
241 link = link->next;
242 }
243
244 fibril_mutex_unlock(&functions_mutex);
245
246 return NULL;
247}
248
249static void driver_add_device(ipc_callid_t iid, ipc_call_t *icall)
250{
251 char *dev_name = NULL;
252 int res;
253
254 devman_handle_t dev_handle = IPC_GET_ARG1(*icall);
255 devman_handle_t parent_fun_handle = IPC_GET_ARG2(*icall);
256
257 device_t *dev = create_device();
258 dev->handle = dev_handle;
259
260 async_data_write_accept((void **) &dev_name, true, 0, 0, 0, 0);
261 dev->name = dev_name;
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;
268
269 res = driver->driver_ops->add_device(dev);
270 if (res == EOK) {
271 printf("%s: new device with handle=%" PRIun " was added.\n",
272 driver->name, dev_handle);
273 } else {
274 printf("%s: failed to add a new device with handle = %" PRIun ".\n",
275 driver->name, dev_handle);
276 delete_device(dev);
277 }
278
279 async_answer_0(iid, res);
280}
281
282static void driver_connection_devman(ipc_callid_t iid, ipc_call_t *icall)
283{
284 /* Accept connection */
285 async_answer_0(iid, EOK);
286
287 bool cont = true;
288 while (cont) {
289 ipc_call_t call;
290 ipc_callid_t callid = async_get_call(&call);
291
292 switch (IPC_GET_IMETHOD(call)) {
293 case IPC_M_PHONE_HUNGUP:
294 cont = false;
295 continue;
296 case DRIVER_ADD_DEVICE:
297 driver_add_device(callid, &call);
298 break;
299 default:
300 async_answer_0(callid, ENOENT);
301 }
302 }
303}
304
305/**
306 * Generic client connection handler both for applications and drivers.
307 *
308 * @param drv True for driver client, false for other clients
309 * (applications, services etc.).
310 */
311static void driver_connection_gen(ipc_callid_t iid, ipc_call_t *icall, bool drv)
312{
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 */
317 devman_handle_t handle = IPC_GET_ARG2(*icall);
318 function_t *fun = driver_get_function(&functions, handle);
319
320 if (fun == NULL) {
321 printf("%s: driver_connection_gen error - no function with handle"
322 " %" PRIun " was found.\n", driver->name, handle);
323 async_answer_0(iid, ENOENT);
324 return;
325 }
326
327
328 /*
329 * TODO - if the client is not a driver, check whether it is allowed to
330 * use the device.
331 */
332
333 int ret = EOK;
334 /* Open device function */
335 if (fun->ops != NULL && fun->ops->open != NULL)
336 ret = (*fun->ops->open)(fun);
337
338 async_answer_0(iid, ret);
339 if (ret != EOK)
340 return;
341
342 while (1) {
343 ipc_callid_t callid;
344 ipc_call_t call;
345 callid = async_get_call(&call);
346 sysarg_t method = IPC_GET_IMETHOD(call);
347 int iface_idx;
348
349 switch (method) {
350 case IPC_M_PHONE_HUNGUP:
351 /* Close device function */
352 if (fun->ops != NULL && fun->ops->close != NULL)
353 (*fun->ops->close)(fun);
354 async_answer_0(callid, EOK);
355 return;
356 default:
357 /* convert ipc interface id to interface index */
358
359 iface_idx = DEV_IFACE_IDX(method);
360
361 if (!is_valid_iface_idx(iface_idx)) {
362 remote_handler_t *default_handler =
363 function_get_default_handler(fun);
364 if (default_handler != NULL) {
365 (*default_handler)(fun, callid, &call);
366 break;
367 }
368
369 /*
370 * Function has no such interface and
371 * default handler is not provided.
372 */
373 printf("%s: driver_connection_gen error - "
374 "invalid interface id %d.",
375 driver->name, iface_idx);
376 async_answer_0(callid, ENOTSUP);
377 break;
378 }
379
380 /* calling one of the function's interfaces */
381
382 /* Get the interface ops structure. */
383 void *ops = function_get_ops(fun, iface_idx);
384 if (ops == NULL) {
385 printf("%s: driver_connection_gen error - ",
386 driver->name);
387 printf("Function with handle %" PRIun " has no interface "
388 "with id %d.\n", handle, iface_idx);
389 async_answer_0(callid, ENOTSUP);
390 break;
391 }
392
393 /*
394 * Get the corresponding interface for remote request
395 * handling ("remote interface").
396 */
397 remote_iface_t *rem_iface = get_remote_iface(iface_idx);
398 assert(rem_iface != NULL);
399
400 /* get the method of the remote interface */
401 sysarg_t iface_method_idx = IPC_GET_ARG1(call);
402 remote_iface_func_ptr_t iface_method_ptr =
403 get_remote_method(rem_iface, iface_method_idx);
404 if (iface_method_ptr == NULL) {
405 // the interface has not such method
406 printf("%s: driver_connection_gen error - "
407 "invalid interface method.", driver->name);
408 async_answer_0(callid, ENOTSUP);
409 break;
410 }
411
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
416 * associated with the function by its driver.
417 */
418 (*iface_method_ptr)(fun, ops, callid, &call);
419 break;
420 }
421 }
422}
423
424static void driver_connection_driver(ipc_callid_t iid, ipc_call_t *icall)
425{
426 driver_connection_gen(iid, icall, true);
427}
428
429static void driver_connection_client(ipc_callid_t iid, ipc_call_t *icall)
430{
431 driver_connection_gen(iid, icall, false);
432}
433
434/** Function for handling connections to device driver. */
435static void driver_connection(ipc_callid_t iid, ipc_call_t *icall)
436{
437 /* Select interface */
438 switch ((sysarg_t) (IPC_GET_ARG1(*icall))) {
439 case DRIVER_DEVMAN:
440 /* Handle request from device manager */
441 driver_connection_devman(iid, icall);
442 break;
443 case DRIVER_DRIVER:
444 /* Handle request from drivers of child devices */
445 driver_connection_driver(iid, icall);
446 break;
447 case DRIVER_CLIENT:
448 /* Handle request from client applications */
449 driver_connection_client(iid, icall);
450 break;
451 default:
452 /* No such interface */
453 async_answer_0(iid, ENOENT);
454 }
455}
456
457/** Create new device structure.
458 *
459 * @return The device structure.
460 */
461device_t *create_device(void)
462{
463 device_t *dev;
464
465 dev = malloc(sizeof(device_t));
466 if (dev == NULL)
467 return NULL;
468
469 memset(dev, 0, sizeof(device_t));
470 return dev;
471}
472
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
493/** Delete device structure.
494 *
495 * @param dev The device structure.
496 */
497void delete_device(device_t *dev)
498{
499 free(dev);
500}
501
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)
515{
516 assert(is_valid_iface_idx(idx));
517 if (fun->ops == NULL)
518 return NULL;
519 return fun->ops->interfaces[idx];
520}
521
522int register_function(function_t *fun, device_t *dev)
523{
524 assert(fun->name != NULL);
525
526 int res;
527
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);
533 if (res != EOK) {
534 remove_from_functions_list(fun);
535 return res;
536 }
537
538 return res;
539}
540
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 */
549int register_function_wrapper(device_t *dev, const char *fun_name,
550 const char *match_id, int match_score)
551{
552 function_t *fun = NULL;
553 match_id_t *m_id = NULL;
554 int rc;
555
556 fun = create_function();
557 if (fun == NULL) {
558 rc = ENOMEM;
559 goto failure;
560 }
561
562 fun->dev = dev;
563 fun->name = fun_name;
564 fun->ftype = fun_inner;
565
566 m_id = create_match_id();
567 if (m_id == NULL) {
568 rc = ENOMEM;
569 goto failure;
570 }
571
572 m_id->id = match_id;
573 m_id->score = match_score;
574 add_match_id(&fun->match_ids, m_id);
575
576 rc = register_function(fun, dev);
577 if (rc != EOK)
578 goto failure;
579
580 return EOK;
581
582failure:
583 if (m_id != NULL) {
584 m_id->id = NULL;
585 delete_match_id(m_id);
586 }
587
588 if (fun != NULL) {
589 fun->name = NULL;
590 delete_function(fun);
591 }
592
593 return rc;
594}
595
596/** Get default handler for client requests */
597remote_handler_t *function_get_default_handler(function_t *fun)
598{
599 if (fun->ops == NULL)
600 return NULL;
601 return fun->ops->default_handler;
602}
603
604int add_function_to_class(function_t *fun, const char *class_name)
605{
606 return devman_add_device_to_class(fun->handle, class_name);
607}
608
609int driver_main(driver_t *drv)
610{
611 /*
612 * Remember the driver structure - driver_ops will be called by generic
613 * handler for incoming connections.
614 */
615 driver = drv;
616
617 /* Initialize the list of interrupt contexts. */
618 init_interrupt_context_list(&interrupt_contexts);
619
620 /* Set generic interrupt handler. */
621 async_set_interrupt_received(driver_irq_handler);
622
623 /*
624 * Register driver by device manager with generic handler for incoming
625 * connections.
626 */
627 devman_driver_register(driver->name, driver_connection);
628
629 async_manager();
630
631 /* Never reached. */
632 return 0;
633}
634
635/**
636 * @}
637 */
Note: See TracBrowser for help on using the repository browser.