| 1 | /*
|
|---|
| 2 | * Copyright (c) 2007 Josef Cejka
|
|---|
| 3 | * Copyright (c) 2009 Jiri Svoboda
|
|---|
| 4 | * Copyright (c) 2010 Lenka Trochtova
|
|---|
| 5 | * All rights reserved.
|
|---|
| 6 | *
|
|---|
| 7 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 8 | * modification, are permitted provided that the following conditions
|
|---|
| 9 | * are met:
|
|---|
| 10 | *
|
|---|
| 11 | * - Redistributions of source code must retain the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 13 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 14 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 15 | * documentation and/or other materials provided with the distribution.
|
|---|
| 16 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 17 | * derived from this software without specific prior written permission.
|
|---|
| 18 | *
|
|---|
| 19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 20 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 21 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 22 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 23 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 24 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 28 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 29 | */
|
|---|
| 30 |
|
|---|
| 31 | #include <string.h>
|
|---|
| 32 | #include <stdio.h>
|
|---|
| 33 | #include <ipc/ipc.h>
|
|---|
| 34 | #include <ipc/services.h>
|
|---|
| 35 | #include <ipc/devman.h>
|
|---|
| 36 | #include <devman.h>
|
|---|
| 37 | #include <async.h>
|
|---|
| 38 | #include <errno.h>
|
|---|
| 39 | #include <malloc.h>
|
|---|
| 40 | #include <bool.h>
|
|---|
| 41 |
|
|---|
| 42 | static int devman_phone_driver = -1;
|
|---|
| 43 | static int devman_phone_client = -1;
|
|---|
| 44 |
|
|---|
| 45 | int devman_get_phone(devman_interface_t iface, unsigned int flags)
|
|---|
| 46 | {
|
|---|
| 47 | switch (iface) {
|
|---|
| 48 | case DEVMAN_DRIVER:
|
|---|
| 49 | if (devman_phone_driver >= 0)
|
|---|
| 50 | return devman_phone_driver;
|
|---|
| 51 |
|
|---|
| 52 | if (flags & IPC_FLAG_BLOCKING)
|
|---|
| 53 | devman_phone_driver = ipc_connect_me_to_blocking(PHONE_NS,
|
|---|
| 54 | SERVICE_DEVMAN, DEVMAN_DRIVER, 0);
|
|---|
| 55 | else
|
|---|
| 56 | devman_phone_driver = ipc_connect_me_to(PHONE_NS,
|
|---|
| 57 | SERVICE_DEVMAN, DEVMAN_DRIVER, 0);
|
|---|
| 58 |
|
|---|
| 59 | return devman_phone_driver;
|
|---|
| 60 | case DEVMAN_CLIENT:
|
|---|
| 61 | if (devman_phone_client >= 0)
|
|---|
| 62 | return devman_phone_client;
|
|---|
| 63 |
|
|---|
| 64 | if (flags & IPC_FLAG_BLOCKING)
|
|---|
| 65 | devman_phone_client = ipc_connect_me_to_blocking(PHONE_NS,
|
|---|
| 66 | SERVICE_DEVMAN, DEVMAN_CLIENT, 0);
|
|---|
| 67 | else
|
|---|
| 68 | devman_phone_client = ipc_connect_me_to(PHONE_NS,
|
|---|
| 69 | SERVICE_DEVMAN, DEVMAN_CLIENT, 0);
|
|---|
| 70 |
|
|---|
| 71 | return devman_phone_client;
|
|---|
| 72 | default:
|
|---|
| 73 | return -1;
|
|---|
| 74 | }
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | /** Register running driver with device manager. */
|
|---|
| 78 | int devman_driver_register(const char *name, async_client_conn_t conn)
|
|---|
| 79 | {
|
|---|
| 80 | int phone = devman_get_phone(DEVMAN_DRIVER, IPC_FLAG_BLOCKING);
|
|---|
| 81 |
|
|---|
| 82 | if (phone < 0)
|
|---|
| 83 | return phone;
|
|---|
| 84 |
|
|---|
| 85 | async_serialize_start();
|
|---|
| 86 |
|
|---|
| 87 | ipc_call_t answer;
|
|---|
| 88 | aid_t req = async_send_2(phone, DEVMAN_DRIVER_REGISTER, 0, 0, &answer);
|
|---|
| 89 |
|
|---|
| 90 | ipcarg_t retval = async_data_write_start(phone, name, str_size(name));
|
|---|
| 91 | if (retval != EOK) {
|
|---|
| 92 | async_wait_for(req, NULL);
|
|---|
| 93 | async_serialize_end();
|
|---|
| 94 | return -1;
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | async_set_client_connection(conn);
|
|---|
| 98 |
|
|---|
| 99 | ipcarg_t callback_phonehash;
|
|---|
| 100 | ipc_connect_to_me(phone, 0, 0, 0, &callback_phonehash);
|
|---|
| 101 | async_wait_for(req, &retval);
|
|---|
| 102 |
|
|---|
| 103 | async_serialize_end();
|
|---|
| 104 |
|
|---|
| 105 | return retval;
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | void devman_hangup_phone(devman_interface_t iface)
|
|---|
| 109 | {
|
|---|
| 110 | switch (iface) {
|
|---|
| 111 | case DEVMAN_DRIVER:
|
|---|
| 112 | if (devman_phone_driver >= 0) {
|
|---|
| 113 | ipc_hangup(devman_phone_driver);
|
|---|
| 114 | devman_phone_driver = -1;
|
|---|
| 115 | }
|
|---|
| 116 | break;
|
|---|
| 117 | case DEVMAN_CLIENT:
|
|---|
| 118 | if (devman_phone_client >= 0) {
|
|---|
| 119 | ipc_hangup(devman_phone_client);
|
|---|
| 120 | devman_phone_client = -1;
|
|---|
| 121 | }
|
|---|
| 122 | break;
|
|---|
| 123 | default:
|
|---|
| 124 | break;
|
|---|
| 125 | }
|
|---|
| 126 | }
|
|---|