| 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 | /** @addtogroup libc
|
|---|
| 32 | * @{
|
|---|
| 33 | */
|
|---|
| 34 | /** @file
|
|---|
| 35 | */
|
|---|
| 36 |
|
|---|
| 37 | #include <string.h>
|
|---|
| 38 | #include <stdio.h>
|
|---|
| 39 | #include <ipc/ipc.h>
|
|---|
| 40 | #include <ipc/services.h>
|
|---|
| 41 | #include <ipc/devman.h>
|
|---|
| 42 | #include <devman.h>
|
|---|
| 43 | #include <async.h>
|
|---|
| 44 | #include <errno.h>
|
|---|
| 45 | #include <malloc.h>
|
|---|
| 46 | #include <bool.h>
|
|---|
| 47 | #include <adt/list.h>
|
|---|
| 48 |
|
|---|
| 49 | static int devman_phone_driver = -1;
|
|---|
| 50 | static int devman_phone_client = -1;
|
|---|
| 51 |
|
|---|
| 52 | int devman_get_phone(devman_interface_t iface, unsigned int flags)
|
|---|
| 53 | {
|
|---|
| 54 | switch (iface) {
|
|---|
| 55 | case DEVMAN_DRIVER:
|
|---|
| 56 | if (devman_phone_driver >= 0)
|
|---|
| 57 | return devman_phone_driver;
|
|---|
| 58 |
|
|---|
| 59 | if (flags & IPC_FLAG_BLOCKING)
|
|---|
| 60 | devman_phone_driver = ipc_connect_me_to_blocking(PHONE_NS,
|
|---|
| 61 | SERVICE_DEVMAN, DEVMAN_DRIVER, 0);
|
|---|
| 62 | else
|
|---|
| 63 | devman_phone_driver = ipc_connect_me_to(PHONE_NS,
|
|---|
| 64 | SERVICE_DEVMAN, DEVMAN_DRIVER, 0);
|
|---|
| 65 |
|
|---|
| 66 | return devman_phone_driver;
|
|---|
| 67 | case DEVMAN_CLIENT:
|
|---|
| 68 | if (devman_phone_client >= 0)
|
|---|
| 69 | return devman_phone_client;
|
|---|
| 70 |
|
|---|
| 71 | if (flags & IPC_FLAG_BLOCKING)
|
|---|
| 72 | devman_phone_client = ipc_connect_me_to_blocking(PHONE_NS,
|
|---|
| 73 | SERVICE_DEVMAN, DEVMAN_CLIENT, 0);
|
|---|
| 74 | else
|
|---|
| 75 | devman_phone_client = ipc_connect_me_to(PHONE_NS,
|
|---|
| 76 | SERVICE_DEVMAN, DEVMAN_CLIENT, 0);
|
|---|
| 77 |
|
|---|
| 78 | return devman_phone_client;
|
|---|
| 79 | default:
|
|---|
| 80 | return -1;
|
|---|
| 81 | }
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | /** Register running driver with device manager. */
|
|---|
| 85 | int devman_driver_register(const char *name, async_client_conn_t conn)
|
|---|
| 86 | {
|
|---|
| 87 | int phone = devman_get_phone(DEVMAN_DRIVER, IPC_FLAG_BLOCKING);
|
|---|
| 88 |
|
|---|
| 89 | if (phone < 0)
|
|---|
| 90 | return phone;
|
|---|
| 91 |
|
|---|
| 92 | async_serialize_start();
|
|---|
| 93 |
|
|---|
| 94 | ipc_call_t answer;
|
|---|
| 95 | aid_t req = async_send_2(phone, DEVMAN_DRIVER_REGISTER, 0, 0, &answer);
|
|---|
| 96 |
|
|---|
| 97 | ipcarg_t retval = async_data_write_start(phone, name, str_size(name));
|
|---|
| 98 | if (retval != EOK) {
|
|---|
| 99 | async_wait_for(req, NULL);
|
|---|
| 100 | async_serialize_end();
|
|---|
| 101 | return -1;
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | async_set_client_connection(conn);
|
|---|
| 105 |
|
|---|
| 106 | ipcarg_t callback_phonehash;
|
|---|
| 107 | ipc_connect_to_me(phone, 0, 0, 0, &callback_phonehash);
|
|---|
| 108 | async_wait_for(req, &retval);
|
|---|
| 109 |
|
|---|
| 110 | async_serialize_end();
|
|---|
| 111 |
|
|---|
| 112 | return retval;
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | static int devman_send_match_id(int phone, match_id_t *match_id) \
|
|---|
| 116 | {
|
|---|
| 117 | ipc_call_t answer;
|
|---|
| 118 | async_send_1(phone, DEVMAN_ADD_MATCH_ID, match_id->score, &answer);
|
|---|
| 119 | int retval = async_data_write_start(phone, match_id->id, str_size(match_id->id));
|
|---|
| 120 | return retval;
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 |
|
|---|
| 124 | static int devman_send_match_ids(int phone, match_id_list_t *match_ids)
|
|---|
| 125 | {
|
|---|
| 126 | link_t *link = match_ids->ids.next;
|
|---|
| 127 | match_id_t *match_id = NULL;
|
|---|
| 128 | int ret = EOK;
|
|---|
| 129 |
|
|---|
| 130 | while (link != &match_ids->ids) {
|
|---|
| 131 | match_id = list_get_instance(link, match_id_t, link);
|
|---|
| 132 | if (EOK != (ret = devman_send_match_id(phone, match_id)))
|
|---|
| 133 | {
|
|---|
| 134 | printf("Driver failed to send match id, error number = %d\n", ret);
|
|---|
| 135 | return ret;
|
|---|
| 136 | }
|
|---|
| 137 | link = link->next;
|
|---|
| 138 | }
|
|---|
| 139 | return ret;
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 | int devman_child_device_register(
|
|---|
| 143 | const char *name, match_id_list_t *match_ids, device_handle_t parent_handle, device_handle_t *handle)
|
|---|
| 144 | {
|
|---|
| 145 | int phone = devman_get_phone(DEVMAN_DRIVER, IPC_FLAG_BLOCKING);
|
|---|
| 146 |
|
|---|
| 147 | if (phone < 0)
|
|---|
| 148 | return phone;
|
|---|
| 149 |
|
|---|
| 150 | async_serialize_start();
|
|---|
| 151 |
|
|---|
| 152 | int match_count = list_count(&match_ids->ids);
|
|---|
| 153 | ipc_call_t answer;
|
|---|
| 154 | aid_t req = async_send_2(phone, DEVMAN_ADD_CHILD_DEVICE, parent_handle, match_count, &answer);
|
|---|
| 155 |
|
|---|
| 156 | ipcarg_t retval = async_data_write_start(phone, name, str_size(name));
|
|---|
| 157 | if (retval != EOK) {
|
|---|
| 158 | async_wait_for(req, NULL);
|
|---|
| 159 | async_serialize_end();
|
|---|
| 160 | return retval;
|
|---|
| 161 | }
|
|---|
| 162 |
|
|---|
| 163 | devman_send_match_ids(phone, match_ids);
|
|---|
| 164 |
|
|---|
| 165 | async_wait_for(req, &retval);
|
|---|
| 166 |
|
|---|
| 167 | async_serialize_end();
|
|---|
| 168 |
|
|---|
| 169 | if (retval != EOK) {
|
|---|
| 170 | if (handle != NULL) {
|
|---|
| 171 | *handle = -1;
|
|---|
| 172 | }
|
|---|
| 173 | return retval;
|
|---|
| 174 | }
|
|---|
| 175 |
|
|---|
| 176 | if (handle != NULL)
|
|---|
| 177 | *handle = (int) IPC_GET_ARG1(answer);
|
|---|
| 178 |
|
|---|
| 179 | return retval;
|
|---|
| 180 | }
|
|---|
| 181 |
|
|---|
| 182 | void devman_hangup_phone(devman_interface_t iface)
|
|---|
| 183 | {
|
|---|
| 184 | switch (iface) {
|
|---|
| 185 | case DEVMAN_DRIVER:
|
|---|
| 186 | if (devman_phone_driver >= 0) {
|
|---|
| 187 | ipc_hangup(devman_phone_driver);
|
|---|
| 188 | devman_phone_driver = -1;
|
|---|
| 189 | }
|
|---|
| 190 | break;
|
|---|
| 191 | case DEVMAN_CLIENT:
|
|---|
| 192 | if (devman_phone_client >= 0) {
|
|---|
| 193 | ipc_hangup(devman_phone_client);
|
|---|
| 194 | devman_phone_client = -1;
|
|---|
| 195 | }
|
|---|
| 196 | break;
|
|---|
| 197 | default:
|
|---|
| 198 | break;
|
|---|
| 199 | }
|
|---|
| 200 | }
|
|---|
| 201 |
|
|---|
| 202 | int devman_device_connect(device_handle_t handle, unsigned int flags)
|
|---|
| 203 | {
|
|---|
| 204 | int phone;
|
|---|
| 205 |
|
|---|
| 206 | if (flags & IPC_FLAG_BLOCKING) {
|
|---|
| 207 | phone = ipc_connect_me_to_blocking(PHONE_NS, SERVICE_DEVMAN,
|
|---|
| 208 | DEVMAN_CONNECT_TO_DEVICE, handle);
|
|---|
| 209 | } else {
|
|---|
| 210 | phone = ipc_connect_me_to(PHONE_NS, SERVICE_DEVMAN,
|
|---|
| 211 | DEVMAN_CONNECT_TO_DEVICE, handle);
|
|---|
| 212 | }
|
|---|
| 213 |
|
|---|
| 214 | return phone;
|
|---|
| 215 | }
|
|---|
| 216 |
|
|---|
| 217 | int devman_parent_device_connect(device_handle_t handle, unsigned int flags)
|
|---|
| 218 | {
|
|---|
| 219 | int phone;
|
|---|
| 220 |
|
|---|
| 221 | if (flags & IPC_FLAG_BLOCKING) {
|
|---|
| 222 | phone = ipc_connect_me_to_blocking(PHONE_NS, SERVICE_DEVMAN,
|
|---|
| 223 | DEVMAN_CONNECT_TO_PARENTS_DEVICE, handle);
|
|---|
| 224 | } else {
|
|---|
| 225 | phone = ipc_connect_me_to(PHONE_NS, SERVICE_DEVMAN,
|
|---|
| 226 | DEVMAN_CONNECT_TO_PARENTS_DEVICE, handle);
|
|---|
| 227 | }
|
|---|
| 228 |
|
|---|
| 229 | return phone;
|
|---|
| 230 | }
|
|---|
| 231 |
|
|---|
| 232 | /** @}
|
|---|
| 233 | */ |
|---|