[729fa2d6] | 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 | */
|
---|
[5cd136ab] | 30 |
|
---|
| 31 | /** @addtogroup libc
|
---|
| 32 | * @{
|
---|
| 33 | */
|
---|
| 34 | /** @file
|
---|
| 35 | */
|
---|
[729fa2d6] | 36 |
|
---|
[c47e1a8] | 37 | #include <str.h>
|
---|
[729fa2d6] | 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>
|
---|
[622dea8] | 44 | #include <fibril_synch.h>
|
---|
[729fa2d6] | 45 | #include <errno.h>
|
---|
| 46 | #include <malloc.h>
|
---|
| 47 | #include <bool.h>
|
---|
[66babbd] | 48 | #include <adt/list.h>
|
---|
[729fa2d6] | 49 |
|
---|
| 50 | static int devman_phone_driver = -1;
|
---|
| 51 | static int devman_phone_client = -1;
|
---|
| 52 |
|
---|
[622dea8] | 53 | static FIBRIL_MUTEX_INITIALIZE(devman_phone_mutex);
|
---|
| 54 |
|
---|
[729fa2d6] | 55 | int devman_get_phone(devman_interface_t iface, unsigned int flags)
|
---|
| 56 | {
|
---|
| 57 | switch (iface) {
|
---|
| 58 | case DEVMAN_DRIVER:
|
---|
[622dea8] | 59 | fibril_mutex_lock(&devman_phone_mutex);
|
---|
| 60 | if (devman_phone_driver >= 0) {
|
---|
| 61 | fibril_mutex_unlock(&devman_phone_mutex);
|
---|
[729fa2d6] | 62 | return devman_phone_driver;
|
---|
[622dea8] | 63 | }
|
---|
[729fa2d6] | 64 |
|
---|
| 65 | if (flags & IPC_FLAG_BLOCKING)
|
---|
[8983273] | 66 | devman_phone_driver = async_connect_me_to_blocking(
|
---|
| 67 | PHONE_NS, SERVICE_DEVMAN, DEVMAN_DRIVER, 0);
|
---|
[729fa2d6] | 68 | else
|
---|
[8983273] | 69 | devman_phone_driver = async_connect_me_to(PHONE_NS,
|
---|
[729fa2d6] | 70 | SERVICE_DEVMAN, DEVMAN_DRIVER, 0);
|
---|
| 71 |
|
---|
[622dea8] | 72 | fibril_mutex_unlock(&devman_phone_mutex);
|
---|
[729fa2d6] | 73 | return devman_phone_driver;
|
---|
| 74 | case DEVMAN_CLIENT:
|
---|
[622dea8] | 75 | fibril_mutex_lock(&devman_phone_mutex);
|
---|
| 76 | if (devman_phone_client >= 0) {
|
---|
| 77 | fibril_mutex_unlock(&devman_phone_mutex);
|
---|
[729fa2d6] | 78 | return devman_phone_client;
|
---|
[622dea8] | 79 | }
|
---|
[729fa2d6] | 80 |
|
---|
[4db43721] | 81 | if (flags & IPC_FLAG_BLOCKING) {
|
---|
[8983273] | 82 | devman_phone_client = async_connect_me_to_blocking(
|
---|
| 83 | PHONE_NS, SERVICE_DEVMAN, DEVMAN_CLIENT, 0);
|
---|
[4db43721] | 84 | } else {
|
---|
[8983273] | 85 | devman_phone_client = async_connect_me_to(PHONE_NS,
|
---|
[729fa2d6] | 86 | SERVICE_DEVMAN, DEVMAN_CLIENT, 0);
|
---|
[4db43721] | 87 | }
|
---|
[729fa2d6] | 88 |
|
---|
[622dea8] | 89 | fibril_mutex_unlock(&devman_phone_mutex);
|
---|
[729fa2d6] | 90 | return devman_phone_client;
|
---|
| 91 | default:
|
---|
| 92 | return -1;
|
---|
| 93 | }
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | /** Register running driver with device manager. */
|
---|
| 97 | int devman_driver_register(const char *name, async_client_conn_t conn)
|
---|
| 98 | {
|
---|
| 99 | int phone = devman_get_phone(DEVMAN_DRIVER, IPC_FLAG_BLOCKING);
|
---|
| 100 |
|
---|
| 101 | if (phone < 0)
|
---|
| 102 | return phone;
|
---|
| 103 |
|
---|
| 104 | async_serialize_start();
|
---|
| 105 |
|
---|
| 106 | ipc_call_t answer;
|
---|
| 107 | aid_t req = async_send_2(phone, DEVMAN_DRIVER_REGISTER, 0, 0, &answer);
|
---|
| 108 |
|
---|
[96b02eb9] | 109 | sysarg_t retval = async_data_write_start(phone, name, str_size(name));
|
---|
[729fa2d6] | 110 | if (retval != EOK) {
|
---|
| 111 | async_wait_for(req, NULL);
|
---|
| 112 | async_serialize_end();
|
---|
| 113 | return -1;
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | async_set_client_connection(conn);
|
---|
| 117 |
|
---|
[124c061] | 118 | ipc_connect_to_me(phone, 0, 0, 0, NULL, NULL);
|
---|
[729fa2d6] | 119 | async_wait_for(req, &retval);
|
---|
| 120 |
|
---|
| 121 | async_serialize_end();
|
---|
| 122 |
|
---|
| 123 | return retval;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
[66babbd] | 126 | static int devman_send_match_id(int phone, match_id_t *match_id) \
|
---|
| 127 | {
|
---|
| 128 | ipc_call_t answer;
|
---|
[398c4d7] | 129 | aid_t req = async_send_1(phone, DEVMAN_ADD_MATCH_ID, match_id->score, &answer);
|
---|
[66babbd] | 130 | int retval = async_data_write_start(phone, match_id->id, str_size(match_id->id));
|
---|
[398c4d7] | 131 | async_wait_for(req, NULL);
|
---|
| 132 | return retval;
|
---|
[66babbd] | 133 | }
|
---|
| 134 |
|
---|
| 135 |
|
---|
| 136 | static int devman_send_match_ids(int phone, match_id_list_t *match_ids)
|
---|
| 137 | {
|
---|
| 138 | link_t *link = match_ids->ids.next;
|
---|
| 139 | match_id_t *match_id = NULL;
|
---|
| 140 | int ret = EOK;
|
---|
| 141 |
|
---|
| 142 | while (link != &match_ids->ids) {
|
---|
| 143 | match_id = list_get_instance(link, match_id_t, link);
|
---|
| 144 | if (EOK != (ret = devman_send_match_id(phone, match_id)))
|
---|
| 145 | {
|
---|
| 146 | printf("Driver failed to send match id, error number = %d\n", ret);
|
---|
| 147 | return ret;
|
---|
| 148 | }
|
---|
| 149 | link = link->next;
|
---|
| 150 | }
|
---|
| 151 | return ret;
|
---|
| 152 | }
|
---|
| 153 |
|
---|
[bda60d9] | 154 | int devman_child_device_register(
|
---|
[0b5a4131] | 155 | const char *name, match_id_list_t *match_ids, devman_handle_t parent_handle, devman_handle_t *handle)
|
---|
[d347b53] | 156 | {
|
---|
[7707954] | 157 | int phone = devman_get_phone(DEVMAN_DRIVER, IPC_FLAG_BLOCKING);
|
---|
| 158 |
|
---|
| 159 | if (phone < 0)
|
---|
| 160 | return phone;
|
---|
| 161 |
|
---|
| 162 | async_serialize_start();
|
---|
| 163 |
|
---|
[66babbd] | 164 | int match_count = list_count(&match_ids->ids);
|
---|
[7707954] | 165 | ipc_call_t answer;
|
---|
[66babbd] | 166 | aid_t req = async_send_2(phone, DEVMAN_ADD_CHILD_DEVICE, parent_handle, match_count, &answer);
|
---|
[7707954] | 167 |
|
---|
[96b02eb9] | 168 | sysarg_t retval = async_data_write_start(phone, name, str_size(name));
|
---|
[7707954] | 169 | if (retval != EOK) {
|
---|
| 170 | async_wait_for(req, NULL);
|
---|
| 171 | async_serialize_end();
|
---|
| 172 | return retval;
|
---|
| 173 | }
|
---|
| 174 |
|
---|
[66babbd] | 175 | devman_send_match_ids(phone, match_ids);
|
---|
[bda60d9] | 176 |
|
---|
[7707954] | 177 | async_wait_for(req, &retval);
|
---|
| 178 |
|
---|
| 179 | async_serialize_end();
|
---|
| 180 |
|
---|
| 181 | if (retval != EOK) {
|
---|
| 182 | if (handle != NULL) {
|
---|
| 183 | *handle = -1;
|
---|
| 184 | }
|
---|
| 185 | return retval;
|
---|
| 186 | }
|
---|
| 187 |
|
---|
| 188 | if (handle != NULL)
|
---|
| 189 | *handle = (int) IPC_GET_ARG1(answer);
|
---|
[66babbd] | 190 |
|
---|
| 191 | return retval;
|
---|
[7707954] | 192 | }
|
---|
| 193 |
|
---|
[0b5a4131] | 194 | int devman_add_device_to_class(devman_handle_t devman_handle, const char *class_name)
|
---|
[692c40cb] | 195 | {
|
---|
| 196 | int phone = devman_get_phone(DEVMAN_DRIVER, IPC_FLAG_BLOCKING);
|
---|
| 197 |
|
---|
| 198 | if (phone < 0)
|
---|
| 199 | return phone;
|
---|
| 200 |
|
---|
| 201 | async_serialize_start();
|
---|
| 202 | ipc_call_t answer;
|
---|
[0b5a4131] | 203 | aid_t req = async_send_1(phone, DEVMAN_ADD_DEVICE_TO_CLASS, devman_handle, &answer);
|
---|
[692c40cb] | 204 |
|
---|
[96b02eb9] | 205 | sysarg_t retval = async_data_write_start(phone, class_name, str_size(class_name));
|
---|
[692c40cb] | 206 | if (retval != EOK) {
|
---|
| 207 | async_wait_for(req, NULL);
|
---|
| 208 | async_serialize_end();
|
---|
| 209 | return retval;
|
---|
| 210 | }
|
---|
| 211 |
|
---|
| 212 | async_wait_for(req, &retval);
|
---|
| 213 | async_serialize_end();
|
---|
| 214 |
|
---|
| 215 | return retval;
|
---|
| 216 | }
|
---|
| 217 |
|
---|
[729fa2d6] | 218 | void devman_hangup_phone(devman_interface_t iface)
|
---|
| 219 | {
|
---|
| 220 | switch (iface) {
|
---|
| 221 | case DEVMAN_DRIVER:
|
---|
| 222 | if (devman_phone_driver >= 0) {
|
---|
| 223 | ipc_hangup(devman_phone_driver);
|
---|
| 224 | devman_phone_driver = -1;
|
---|
| 225 | }
|
---|
| 226 | break;
|
---|
| 227 | case DEVMAN_CLIENT:
|
---|
| 228 | if (devman_phone_client >= 0) {
|
---|
| 229 | ipc_hangup(devman_phone_client);
|
---|
| 230 | devman_phone_client = -1;
|
---|
| 231 | }
|
---|
| 232 | break;
|
---|
| 233 | default:
|
---|
| 234 | break;
|
---|
| 235 | }
|
---|
| 236 | }
|
---|
[5cd136ab] | 237 |
|
---|
[0b5a4131] | 238 | int devman_device_connect(devman_handle_t handle, unsigned int flags)
|
---|
[5cd136ab] | 239 | {
|
---|
| 240 | int phone;
|
---|
| 241 |
|
---|
| 242 | if (flags & IPC_FLAG_BLOCKING) {
|
---|
[8983273] | 243 | phone = async_connect_me_to_blocking(PHONE_NS, SERVICE_DEVMAN,
|
---|
[5cd136ab] | 244 | DEVMAN_CONNECT_TO_DEVICE, handle);
|
---|
| 245 | } else {
|
---|
[8983273] | 246 | phone = async_connect_me_to(PHONE_NS, SERVICE_DEVMAN,
|
---|
[5cd136ab] | 247 | DEVMAN_CONNECT_TO_DEVICE, handle);
|
---|
| 248 | }
|
---|
| 249 |
|
---|
| 250 | return phone;
|
---|
| 251 | }
|
---|
| 252 |
|
---|
[0b5a4131] | 253 | int devman_parent_device_connect(devman_handle_t handle, unsigned int flags)
|
---|
[5cd136ab] | 254 | {
|
---|
| 255 | int phone;
|
---|
| 256 |
|
---|
| 257 | if (flags & IPC_FLAG_BLOCKING) {
|
---|
[8983273] | 258 | phone = async_connect_me_to_blocking(PHONE_NS, SERVICE_DEVMAN,
|
---|
[5cd136ab] | 259 | DEVMAN_CONNECT_TO_PARENTS_DEVICE, handle);
|
---|
| 260 | } else {
|
---|
[8983273] | 261 | phone = async_connect_me_to(PHONE_NS, SERVICE_DEVMAN,
|
---|
[5cd136ab] | 262 | DEVMAN_CONNECT_TO_PARENTS_DEVICE, handle);
|
---|
| 263 | }
|
---|
| 264 |
|
---|
| 265 | return phone;
|
---|
| 266 | }
|
---|
| 267 |
|
---|
[0b5a4131] | 268 | int devman_device_get_handle(const char *pathname, devman_handle_t *handle, unsigned int flags)
|
---|
[f658458] | 269 | {
|
---|
| 270 | int phone = devman_get_phone(DEVMAN_CLIENT, flags);
|
---|
| 271 |
|
---|
| 272 | if (phone < 0)
|
---|
| 273 | return phone;
|
---|
| 274 |
|
---|
| 275 | async_serialize_start();
|
---|
| 276 |
|
---|
| 277 | ipc_call_t answer;
|
---|
| 278 | aid_t req = async_send_2(phone, DEVMAN_DEVICE_GET_HANDLE, flags, 0,
|
---|
| 279 | &answer);
|
---|
| 280 |
|
---|
[96b02eb9] | 281 | sysarg_t retval = async_data_write_start(phone, pathname, str_size(pathname));
|
---|
[f658458] | 282 | if (retval != EOK) {
|
---|
| 283 | async_wait_for(req, NULL);
|
---|
| 284 | async_serialize_end();
|
---|
| 285 | return retval;
|
---|
| 286 | }
|
---|
| 287 |
|
---|
| 288 | async_wait_for(req, &retval);
|
---|
| 289 |
|
---|
| 290 | async_serialize_end();
|
---|
| 291 |
|
---|
| 292 | if (retval != EOK) {
|
---|
| 293 | if (handle != NULL)
|
---|
[0b5a4131] | 294 | *handle = (devman_handle_t) -1;
|
---|
[f658458] | 295 | return retval;
|
---|
| 296 | }
|
---|
| 297 |
|
---|
| 298 | if (handle != NULL)
|
---|
[0b5a4131] | 299 | *handle = (devman_handle_t) IPC_GET_ARG1(answer);
|
---|
[f658458] | 300 |
|
---|
| 301 | return retval;
|
---|
| 302 | }
|
---|
| 303 |
|
---|
| 304 |
|
---|
[5cd136ab] | 305 | /** @}
|
---|
[398c4d7] | 306 | */
|
---|