[729fa2d6] | 1 | /*
|
---|
| 2 | * Copyright (c) 2007 Josef Cejka
|
---|
[7beb220] | 3 | * Copyright (c) 2011 Jiri Svoboda
|
---|
[729fa2d6] | 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 | */
|
---|
[64d2b10] | 30 |
|
---|
| 31 | /** @addtogroup libc
|
---|
[5cd136ab] | 32 | * @{
|
---|
| 33 | */
|
---|
| 34 | /** @file
|
---|
| 35 | */
|
---|
[729fa2d6] | 36 |
|
---|
[b72efe8] | 37 | #include <adt/list.h>
|
---|
[c47e1a8] | 38 | #include <str.h>
|
---|
[729fa2d6] | 39 | #include <ipc/services.h>
|
---|
[79ae36dd] | 40 | #include <ns.h>
|
---|
[729fa2d6] | 41 | #include <ipc/devman.h>
|
---|
| 42 | #include <devman.h>
|
---|
[622dea8] | 43 | #include <fibril_synch.h>
|
---|
[79ae36dd] | 44 | #include <async.h>
|
---|
[729fa2d6] | 45 | #include <errno.h>
|
---|
| 46 | #include <malloc.h>
|
---|
| 47 | #include <bool.h>
|
---|
| 48 |
|
---|
[79ae36dd] | 49 | static FIBRIL_MUTEX_INITIALIZE(devman_driver_block_mutex);
|
---|
| 50 | static FIBRIL_MUTEX_INITIALIZE(devman_client_block_mutex);
|
---|
| 51 |
|
---|
| 52 | static FIBRIL_MUTEX_INITIALIZE(devman_driver_mutex);
|
---|
| 53 | static FIBRIL_MUTEX_INITIALIZE(devman_client_mutex);
|
---|
[729fa2d6] | 54 |
|
---|
[79ae36dd] | 55 | static async_sess_t *devman_driver_block_sess = NULL;
|
---|
| 56 | static async_sess_t *devman_client_block_sess = NULL;
|
---|
[622dea8] | 57 |
|
---|
[79ae36dd] | 58 | static async_sess_t *devman_driver_sess = NULL;
|
---|
| 59 | static async_sess_t *devman_client_sess = NULL;
|
---|
| 60 |
|
---|
| 61 | static void clone_session(fibril_mutex_t *mtx, async_sess_t *src,
|
---|
| 62 | async_sess_t **dst)
|
---|
| 63 | {
|
---|
| 64 | fibril_mutex_lock(mtx);
|
---|
| 65 |
|
---|
| 66 | if ((*dst == NULL) && (src != NULL))
|
---|
| 67 | *dst = src;
|
---|
| 68 |
|
---|
| 69 | fibril_mutex_unlock(mtx);
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | /** Start an async exchange on the devman session (blocking).
|
---|
| 73 | *
|
---|
| 74 | * @param iface Device manager interface to choose
|
---|
| 75 | *
|
---|
| 76 | * @return New exchange.
|
---|
| 77 | *
|
---|
| 78 | */
|
---|
| 79 | async_exch_t *devman_exchange_begin_blocking(devman_interface_t iface)
|
---|
[729fa2d6] | 80 | {
|
---|
| 81 | switch (iface) {
|
---|
| 82 | case DEVMAN_DRIVER:
|
---|
[79ae36dd] | 83 | fibril_mutex_lock(&devman_driver_block_mutex);
|
---|
| 84 |
|
---|
| 85 | while (devman_driver_block_sess == NULL) {
|
---|
| 86 | clone_session(&devman_driver_mutex, devman_driver_sess,
|
---|
| 87 | &devman_driver_block_sess);
|
---|
| 88 |
|
---|
| 89 | if (devman_driver_block_sess == NULL)
|
---|
| 90 | devman_driver_block_sess =
|
---|
| 91 | service_connect_blocking(EXCHANGE_SERIALIZE,
|
---|
| 92 | SERVICE_DEVMAN, DEVMAN_DRIVER, 0);
|
---|
[622dea8] | 93 | }
|
---|
[729fa2d6] | 94 |
|
---|
[79ae36dd] | 95 | fibril_mutex_unlock(&devman_driver_block_mutex);
|
---|
[729fa2d6] | 96 |
|
---|
[79ae36dd] | 97 | clone_session(&devman_driver_mutex, devman_driver_block_sess,
|
---|
| 98 | &devman_driver_sess);
|
---|
| 99 |
|
---|
| 100 | return async_exchange_begin(devman_driver_block_sess);
|
---|
[729fa2d6] | 101 | case DEVMAN_CLIENT:
|
---|
[79ae36dd] | 102 | fibril_mutex_lock(&devman_client_block_mutex);
|
---|
[729fa2d6] | 103 |
|
---|
[79ae36dd] | 104 | while (devman_client_block_sess == NULL) {
|
---|
| 105 | clone_session(&devman_client_mutex, devman_client_sess,
|
---|
| 106 | &devman_client_block_sess);
|
---|
| 107 |
|
---|
| 108 | if (devman_client_block_sess == NULL)
|
---|
| 109 | devman_client_block_sess =
|
---|
| 110 | service_connect_blocking(EXCHANGE_SERIALIZE,
|
---|
| 111 | SERVICE_DEVMAN, DEVMAN_CLIENT, 0);
|
---|
[4db43721] | 112 | }
|
---|
[729fa2d6] | 113 |
|
---|
[79ae36dd] | 114 | fibril_mutex_unlock(&devman_client_block_mutex);
|
---|
| 115 |
|
---|
| 116 | clone_session(&devman_client_mutex, devman_client_block_sess,
|
---|
| 117 | &devman_client_sess);
|
---|
| 118 |
|
---|
| 119 | return async_exchange_begin(devman_client_block_sess);
|
---|
[729fa2d6] | 120 | default:
|
---|
[79ae36dd] | 121 | return NULL;
|
---|
[729fa2d6] | 122 | }
|
---|
| 123 | }
|
---|
| 124 |
|
---|
[79ae36dd] | 125 | /** Start an async exchange on the devman session.
|
---|
| 126 | *
|
---|
| 127 | * @param iface Device manager interface to choose
|
---|
| 128 | *
|
---|
| 129 | * @return New exchange.
|
---|
| 130 | *
|
---|
| 131 | */
|
---|
| 132 | async_exch_t *devman_exchange_begin(devman_interface_t iface)
|
---|
| 133 | {
|
---|
| 134 | switch (iface) {
|
---|
| 135 | case DEVMAN_DRIVER:
|
---|
| 136 | fibril_mutex_lock(&devman_driver_mutex);
|
---|
| 137 |
|
---|
| 138 | if (devman_driver_sess == NULL)
|
---|
| 139 | devman_driver_sess =
|
---|
| 140 | service_connect(EXCHANGE_SERIALIZE, SERVICE_DEVMAN,
|
---|
| 141 | DEVMAN_DRIVER, 0);
|
---|
| 142 |
|
---|
| 143 | fibril_mutex_unlock(&devman_driver_mutex);
|
---|
| 144 |
|
---|
| 145 | if (devman_driver_sess == NULL)
|
---|
| 146 | return NULL;
|
---|
| 147 |
|
---|
| 148 | return async_exchange_begin(devman_driver_sess);
|
---|
| 149 | case DEVMAN_CLIENT:
|
---|
| 150 | fibril_mutex_lock(&devman_client_mutex);
|
---|
| 151 |
|
---|
| 152 | if (devman_client_sess == NULL)
|
---|
| 153 | devman_client_sess =
|
---|
| 154 | service_connect(EXCHANGE_SERIALIZE, SERVICE_DEVMAN,
|
---|
| 155 | DEVMAN_CLIENT, 0);
|
---|
| 156 |
|
---|
| 157 | fibril_mutex_unlock(&devman_client_mutex);
|
---|
| 158 |
|
---|
| 159 | if (devman_client_sess == NULL)
|
---|
| 160 | return NULL;
|
---|
| 161 |
|
---|
| 162 | return async_exchange_begin(devman_client_sess);
|
---|
| 163 | default:
|
---|
| 164 | return NULL;
|
---|
| 165 | }
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | /** Finish an async exchange on the devman session.
|
---|
| 169 | *
|
---|
| 170 | * @param exch Exchange to be finished.
|
---|
| 171 | *
|
---|
| 172 | */
|
---|
| 173 | void devman_exchange_end(async_exch_t *exch)
|
---|
| 174 | {
|
---|
| 175 | async_exchange_end(exch);
|
---|
| 176 | }
|
---|
| 177 |
|
---|
[729fa2d6] | 178 | /** Register running driver with device manager. */
|
---|
| 179 | int devman_driver_register(const char *name, async_client_conn_t conn)
|
---|
| 180 | {
|
---|
[79ae36dd] | 181 | async_exch_t *exch = devman_exchange_begin_blocking(DEVMAN_DRIVER);
|
---|
[729fa2d6] | 182 |
|
---|
| 183 | ipc_call_t answer;
|
---|
[79ae36dd] | 184 | aid_t req = async_send_2(exch, DEVMAN_DRIVER_REGISTER, 0, 0, &answer);
|
---|
| 185 | sysarg_t retval = async_data_write_start(exch, name, str_size(name));
|
---|
| 186 |
|
---|
| 187 | devman_exchange_end(exch);
|
---|
[729fa2d6] | 188 |
|
---|
| 189 | if (retval != EOK) {
|
---|
| 190 | async_wait_for(req, NULL);
|
---|
[79ae36dd] | 191 | return retval;
|
---|
[729fa2d6] | 192 | }
|
---|
| 193 |
|
---|
| 194 | async_set_client_connection(conn);
|
---|
| 195 |
|
---|
[79ae36dd] | 196 | exch = devman_exchange_begin(DEVMAN_DRIVER);
|
---|
[45059d6b] | 197 | async_connect_to_me(exch, 0, 0, 0, conn, NULL);
|
---|
[79ae36dd] | 198 | devman_exchange_end(exch);
|
---|
[729fa2d6] | 199 |
|
---|
[79ae36dd] | 200 | async_wait_for(req, &retval);
|
---|
[729fa2d6] | 201 | return retval;
|
---|
| 202 | }
|
---|
| 203 |
|
---|
[8b1e15ac] | 204 | /** Add function to a device.
|
---|
| 205 | *
|
---|
| 206 | * Request devman to add a new function to the specified device owned by
|
---|
| 207 | * this driver task.
|
---|
| 208 | *
|
---|
[79ae36dd] | 209 | * @param name Name of the new function
|
---|
| 210 | * @param ftype Function type, fun_inner or fun_exposed
|
---|
| 211 | * @param match_ids Match IDs (should be empty for fun_exposed)
|
---|
| 212 | * @param devh Devman handle of the device
|
---|
| 213 | * @param funh Place to store handle of the new function
|
---|
| 214 | *
|
---|
| 215 | * @return EOK on success or negative error code.
|
---|
[8b1e15ac] | 216 | *
|
---|
| 217 | */
|
---|
| 218 | int devman_add_function(const char *name, fun_type_t ftype,
|
---|
| 219 | match_id_list_t *match_ids, devman_handle_t devh, devman_handle_t *funh)
|
---|
[1b367b4] | 220 | {
|
---|
[8b1e15ac] | 221 | int match_count = list_count(&match_ids->ids);
|
---|
[79ae36dd] | 222 | async_exch_t *exch = devman_exchange_begin_blocking(DEVMAN_DRIVER);
|
---|
| 223 |
|
---|
[7707954] | 224 | ipc_call_t answer;
|
---|
[79ae36dd] | 225 | aid_t req = async_send_3(exch, DEVMAN_ADD_FUNCTION, (sysarg_t) ftype,
|
---|
[8b1e15ac] | 226 | devh, match_count, &answer);
|
---|
[79ae36dd] | 227 | sysarg_t retval = async_data_write_start(exch, name, str_size(name));
|
---|
[7707954] | 228 | if (retval != EOK) {
|
---|
[79ae36dd] | 229 | devman_exchange_end(exch);
|
---|
[7707954] | 230 | async_wait_for(req, NULL);
|
---|
| 231 | return retval;
|
---|
| 232 | }
|
---|
| 233 |
|
---|
[79ae36dd] | 234 | match_id_t *match_id = NULL;
|
---|
| 235 |
|
---|
[b72efe8] | 236 | list_foreach(match_ids->ids, link) {
|
---|
[79ae36dd] | 237 | match_id = list_get_instance(link, match_id_t, link);
|
---|
| 238 |
|
---|
[15e54f5] | 239 | ipc_call_t answer2;
|
---|
| 240 | aid_t req2 = async_send_1(exch, DEVMAN_ADD_MATCH_ID,
|
---|
| 241 | match_id->score, &answer2);
|
---|
[79ae36dd] | 242 | retval = async_data_write_start(exch, match_id->id,
|
---|
| 243 | str_size(match_id->id));
|
---|
| 244 | if (retval != EOK) {
|
---|
| 245 | devman_exchange_end(exch);
|
---|
[15e54f5] | 246 | async_wait_for(req2, NULL);
|
---|
[79ae36dd] | 247 | async_wait_for(req, NULL);
|
---|
| 248 | return retval;
|
---|
| 249 | }
|
---|
| 250 |
|
---|
[15e54f5] | 251 | async_wait_for(req2, &retval);
|
---|
[79ae36dd] | 252 | if (retval != EOK) {
|
---|
| 253 | devman_exchange_end(exch);
|
---|
[15e54f5] | 254 | async_wait_for(req, NULL);
|
---|
[79ae36dd] | 255 | return retval;
|
---|
| 256 | }
|
---|
[4265fd4] | 257 | }
|
---|
[7707954] | 258 |
|
---|
[79ae36dd] | 259 | devman_exchange_end(exch);
|
---|
[15e54f5] | 260 |
|
---|
| 261 | async_wait_for(req, &retval);
|
---|
[e6211f8] | 262 | if (retval == EOK) {
|
---|
[15e54f5] | 263 | if (funh != NULL)
|
---|
| 264 | *funh = (int) IPC_GET_ARG1(answer);
|
---|
| 265 | } else {
|
---|
| 266 | if (funh != NULL)
|
---|
| 267 | *funh = -1;
|
---|
| 268 | }
|
---|
| 269 |
|
---|
| 270 | return retval;
|
---|
[7707954] | 271 | }
|
---|
| 272 |
|
---|
[1dc4a5e] | 273 | int devman_add_device_to_category(devman_handle_t devman_handle,
|
---|
| 274 | const char *cat_name)
|
---|
[692c40cb] | 275 | {
|
---|
[79ae36dd] | 276 | async_exch_t *exch = devman_exchange_begin_blocking(DEVMAN_DRIVER);
|
---|
[692c40cb] | 277 |
|
---|
| 278 | ipc_call_t answer;
|
---|
[1dc4a5e] | 279 | aid_t req = async_send_1(exch, DEVMAN_ADD_DEVICE_TO_CATEGORY,
|
---|
[1b367b4] | 280 | devman_handle, &answer);
|
---|
[1dc4a5e] | 281 | sysarg_t retval = async_data_write_start(exch, cat_name,
|
---|
| 282 | str_size(cat_name));
|
---|
[79ae36dd] | 283 |
|
---|
| 284 | devman_exchange_end(exch);
|
---|
| 285 |
|
---|
[692c40cb] | 286 | if (retval != EOK) {
|
---|
| 287 | async_wait_for(req, NULL);
|
---|
| 288 | return retval;
|
---|
| 289 | }
|
---|
| 290 |
|
---|
| 291 | async_wait_for(req, &retval);
|
---|
[1b367b4] | 292 | return retval;
|
---|
[692c40cb] | 293 | }
|
---|
| 294 |
|
---|
[79ae36dd] | 295 | async_sess_t *devman_device_connect(exch_mgmt_t mgmt, devman_handle_t handle,
|
---|
| 296 | unsigned int flags)
|
---|
[5cd136ab] | 297 | {
|
---|
[79ae36dd] | 298 | async_sess_t *sess;
|
---|
[5cd136ab] | 299 |
|
---|
[79ae36dd] | 300 | if (flags & IPC_FLAG_BLOCKING)
|
---|
| 301 | sess = service_connect_blocking(mgmt, SERVICE_DEVMAN,
|
---|
| 302 | DEVMAN_CONNECT_TO_DEVICE, handle);
|
---|
| 303 | else
|
---|
| 304 | sess = service_connect(mgmt, SERVICE_DEVMAN,
|
---|
| 305 | DEVMAN_CONNECT_TO_DEVICE, handle);
|
---|
| 306 |
|
---|
| 307 | return sess;
|
---|
[5cd136ab] | 308 | }
|
---|
| 309 |
|
---|
[d0dd7b5] | 310 | /** Remove function from device.
|
---|
| 311 | *
|
---|
| 312 | * Request devman to remove function owned by this driver task.
|
---|
| 313 | * @param funh Devman handle of the function
|
---|
| 314 | *
|
---|
| 315 | * @return EOK on success or negative error code.
|
---|
| 316 | */
|
---|
| 317 | int devman_remove_function(devman_handle_t funh)
|
---|
| 318 | {
|
---|
| 319 | async_exch_t *exch;
|
---|
| 320 | sysarg_t retval;
|
---|
| 321 |
|
---|
| 322 | exch = devman_exchange_begin_blocking(DEVMAN_DRIVER);
|
---|
| 323 | retval = async_req_1_0(exch, DEVMAN_REMOVE_FUNCTION, (sysarg_t) funh);
|
---|
| 324 | devman_exchange_end(exch);
|
---|
| 325 |
|
---|
| 326 | return (int) retval;
|
---|
| 327 | }
|
---|
| 328 |
|
---|
[79ae36dd] | 329 | async_sess_t *devman_parent_device_connect(exch_mgmt_t mgmt,
|
---|
| 330 | devman_handle_t handle, unsigned int flags)
|
---|
[5cd136ab] | 331 | {
|
---|
[79ae36dd] | 332 | async_sess_t *sess;
|
---|
[5cd136ab] | 333 |
|
---|
[79ae36dd] | 334 | if (flags & IPC_FLAG_BLOCKING)
|
---|
| 335 | sess = service_connect_blocking(mgmt, SERVICE_DEVMAN,
|
---|
| 336 | DEVMAN_CONNECT_TO_PARENTS_DEVICE, handle);
|
---|
| 337 | else
|
---|
| 338 | sess = service_connect(mgmt, SERVICE_DEVMAN,
|
---|
| 339 | DEVMAN_CONNECT_TO_PARENTS_DEVICE, handle);
|
---|
| 340 |
|
---|
| 341 | return sess;
|
---|
[5cd136ab] | 342 | }
|
---|
| 343 |
|
---|
[7beb220] | 344 | int devman_fun_get_handle(const char *pathname, devman_handle_t *handle,
|
---|
[1b367b4] | 345 | unsigned int flags)
|
---|
[f658458] | 346 | {
|
---|
[79ae36dd] | 347 | async_exch_t *exch;
|
---|
| 348 |
|
---|
| 349 | if (flags & IPC_FLAG_BLOCKING)
|
---|
| 350 | exch = devman_exchange_begin_blocking(DEVMAN_CLIENT);
|
---|
| 351 | else {
|
---|
| 352 | exch = devman_exchange_begin(DEVMAN_CLIENT);
|
---|
| 353 | if (exch == NULL)
|
---|
[e280857] | 354 | return ENOMEM;
|
---|
[79ae36dd] | 355 | }
|
---|
[f658458] | 356 |
|
---|
| 357 | ipc_call_t answer;
|
---|
[79ae36dd] | 358 | aid_t req = async_send_2(exch, DEVMAN_DEVICE_GET_HANDLE, flags, 0,
|
---|
[f658458] | 359 | &answer);
|
---|
[79ae36dd] | 360 | sysarg_t retval = async_data_write_start(exch, pathname,
|
---|
[1b367b4] | 361 | str_size(pathname));
|
---|
[79ae36dd] | 362 |
|
---|
| 363 | devman_exchange_end(exch);
|
---|
| 364 |
|
---|
[f658458] | 365 | if (retval != EOK) {
|
---|
| 366 | async_wait_for(req, NULL);
|
---|
| 367 | return retval;
|
---|
| 368 | }
|
---|
| 369 |
|
---|
| 370 | async_wait_for(req, &retval);
|
---|
| 371 |
|
---|
| 372 | if (retval != EOK) {
|
---|
| 373 | if (handle != NULL)
|
---|
[0b5a4131] | 374 | *handle = (devman_handle_t) -1;
|
---|
[79ae36dd] | 375 |
|
---|
[f658458] | 376 | return retval;
|
---|
| 377 | }
|
---|
| 378 |
|
---|
| 379 | if (handle != NULL)
|
---|
[0b5a4131] | 380 | *handle = (devman_handle_t) IPC_GET_ARG1(answer);
|
---|
[f658458] | 381 |
|
---|
| 382 | return retval;
|
---|
| 383 | }
|
---|
| 384 |
|
---|
[7beb220] | 385 | static int devman_get_str_internal(sysarg_t method, sysarg_t arg1, char *buf,
|
---|
| 386 | size_t buf_size)
|
---|
[431d6d6] | 387 | {
|
---|
[7beb220] | 388 | async_exch_t *exch;
|
---|
| 389 | ipc_call_t dreply;
|
---|
| 390 | size_t act_size;
|
---|
| 391 | sysarg_t dretval;
|
---|
[79ae36dd] | 392 |
|
---|
[7beb220] | 393 | exch = devman_exchange_begin_blocking(LOC_PORT_CONSUMER);
|
---|
[79ae36dd] | 394 |
|
---|
[7beb220] | 395 | ipc_call_t answer;
|
---|
| 396 | aid_t req = async_send_1(exch, method, arg1, &answer);
|
---|
| 397 | aid_t dreq = async_data_read(exch, buf, buf_size - 1, &dreply);
|
---|
| 398 | async_wait_for(dreq, &dretval);
|
---|
[79ae36dd] | 399 |
|
---|
| 400 | devman_exchange_end(exch);
|
---|
| 401 |
|
---|
[7beb220] | 402 | if (dretval != EOK) {
|
---|
[431d6d6] | 403 | async_wait_for(req, NULL);
|
---|
[7beb220] | 404 | return dretval;
|
---|
[431d6d6] | 405 | }
|
---|
[79ae36dd] | 406 |
|
---|
[7beb220] | 407 | sysarg_t retval;
|
---|
| 408 | async_wait_for(req, &retval);
|
---|
| 409 |
|
---|
| 410 | if (retval != EOK)
|
---|
| 411 | return retval;
|
---|
| 412 |
|
---|
| 413 | act_size = IPC_GET_ARG2(dreply);
|
---|
| 414 | assert(act_size <= buf_size - 1);
|
---|
| 415 | buf[act_size] = '\0';
|
---|
| 416 |
|
---|
| 417 | return EOK;
|
---|
| 418 | }
|
---|
| 419 |
|
---|
| 420 | int devman_fun_get_path(devman_handle_t handle, char *buf, size_t buf_size)
|
---|
| 421 | {
|
---|
| 422 | return devman_get_str_internal(DEVMAN_FUN_GET_PATH, handle, buf,
|
---|
| 423 | buf_size);
|
---|
| 424 | }
|
---|
| 425 |
|
---|
| 426 | int devman_fun_get_name(devman_handle_t handle, char *buf, size_t buf_size)
|
---|
| 427 | {
|
---|
| 428 | return devman_get_str_internal(DEVMAN_FUN_GET_NAME, handle, buf,
|
---|
| 429 | buf_size);
|
---|
| 430 | }
|
---|
| 431 |
|
---|
| 432 | static int devman_get_handles_once(sysarg_t method, sysarg_t arg1,
|
---|
| 433 | devman_handle_t *handle_buf, size_t buf_size, size_t *act_size)
|
---|
| 434 | {
|
---|
| 435 | async_exch_t *exch = devman_exchange_begin_blocking(DEVMAN_CLIENT);
|
---|
| 436 |
|
---|
| 437 | ipc_call_t answer;
|
---|
| 438 | aid_t req = async_send_1(exch, method, arg1, &answer);
|
---|
| 439 | int rc = async_data_read_start(exch, handle_buf, buf_size);
|
---|
[79ae36dd] | 440 |
|
---|
[7beb220] | 441 | devman_exchange_end(exch);
|
---|
[79ae36dd] | 442 |
|
---|
[7beb220] | 443 | if (rc != EOK) {
|
---|
| 444 | async_wait_for(req, NULL);
|
---|
| 445 | return rc;
|
---|
[431d6d6] | 446 | }
|
---|
[79ae36dd] | 447 |
|
---|
[7beb220] | 448 | sysarg_t retval;
|
---|
| 449 | async_wait_for(req, &retval);
|
---|
[79ae36dd] | 450 |
|
---|
[7beb220] | 451 | if (retval != EOK) {
|
---|
| 452 | return retval;
|
---|
| 453 | }
|
---|
[79ae36dd] | 454 |
|
---|
[7beb220] | 455 | *act_size = IPC_GET_ARG1(answer);
|
---|
[431d6d6] | 456 | return EOK;
|
---|
| 457 | }
|
---|
| 458 |
|
---|
[7beb220] | 459 | /** Get list of handles.
|
---|
| 460 | *
|
---|
| 461 | * Returns an allocated array of handles.
|
---|
| 462 | *
|
---|
| 463 | * @param method IPC method
|
---|
| 464 | * @param arg1 IPC argument 1
|
---|
| 465 | * @param data Place to store pointer to array of handles
|
---|
| 466 | * @param count Place to store number of handles
|
---|
| 467 | * @return EOK on success or negative error code
|
---|
| 468 | */
|
---|
| 469 | static int devman_get_handles_internal(sysarg_t method, sysarg_t arg1,
|
---|
| 470 | devman_handle_t **data, size_t *count)
|
---|
| 471 | {
|
---|
| 472 | devman_handle_t *handles;
|
---|
| 473 | size_t act_size;
|
---|
| 474 | size_t alloc_size;
|
---|
| 475 | int rc;
|
---|
| 476 |
|
---|
| 477 | *data = NULL;
|
---|
| 478 | act_size = 0; /* silence warning */
|
---|
| 479 |
|
---|
| 480 | rc = devman_get_handles_once(method, arg1, NULL, 0,
|
---|
| 481 | &act_size);
|
---|
| 482 | if (rc != EOK)
|
---|
| 483 | return rc;
|
---|
| 484 |
|
---|
| 485 | alloc_size = act_size;
|
---|
| 486 | handles = malloc(alloc_size);
|
---|
| 487 | if (handles == NULL)
|
---|
| 488 | return ENOMEM;
|
---|
| 489 |
|
---|
| 490 | while (true) {
|
---|
| 491 | rc = devman_get_handles_once(method, arg1, handles, alloc_size,
|
---|
| 492 | &act_size);
|
---|
| 493 | if (rc != EOK)
|
---|
| 494 | return rc;
|
---|
| 495 |
|
---|
| 496 | if (act_size <= alloc_size)
|
---|
| 497 | break;
|
---|
| 498 |
|
---|
| 499 | alloc_size *= 2;
|
---|
| 500 | free(handles);
|
---|
| 501 |
|
---|
| 502 | handles = malloc(alloc_size);
|
---|
| 503 | if (handles == NULL)
|
---|
| 504 | return ENOMEM;
|
---|
| 505 | }
|
---|
| 506 |
|
---|
| 507 | *count = act_size / sizeof(devman_handle_t);
|
---|
| 508 | *data = handles;
|
---|
| 509 | return EOK;
|
---|
| 510 | }
|
---|
| 511 |
|
---|
| 512 | int devman_fun_get_child(devman_handle_t funh, devman_handle_t *devh)
|
---|
| 513 | {
|
---|
| 514 | async_exch_t *exch = devman_exchange_begin(DEVMAN_CLIENT);
|
---|
| 515 | if (exch == NULL)
|
---|
| 516 | return ENOMEM;
|
---|
| 517 |
|
---|
| 518 | sysarg_t retval = async_req_1_1(exch, DEVMAN_FUN_GET_CHILD,
|
---|
| 519 | funh, devh);
|
---|
| 520 |
|
---|
| 521 | devman_exchange_end(exch);
|
---|
| 522 | return (int) retval;
|
---|
| 523 | }
|
---|
| 524 |
|
---|
| 525 | int devman_dev_get_functions(devman_handle_t devh, devman_handle_t **funcs,
|
---|
| 526 | size_t *count)
|
---|
| 527 | {
|
---|
| 528 | return devman_get_handles_internal(DEVMAN_DEV_GET_FUNCTIONS,
|
---|
| 529 | devh, funcs, count);
|
---|
| 530 | }
|
---|
| 531 |
|
---|
[e280857] | 532 | int devman_fun_sid_to_handle(service_id_t sid, devman_handle_t *handle)
|
---|
| 533 | {
|
---|
| 534 | async_exch_t *exch = devman_exchange_begin(DEVMAN_CLIENT);
|
---|
| 535 | if (exch == NULL)
|
---|
| 536 | return ENOMEM;
|
---|
| 537 |
|
---|
| 538 | sysarg_t retval = async_req_1_1(exch, DEVMAN_FUN_SID_TO_HANDLE,
|
---|
| 539 | sid, handle);
|
---|
| 540 |
|
---|
| 541 | devman_exchange_end(exch);
|
---|
| 542 | return (int) retval;
|
---|
| 543 | }
|
---|
| 544 |
|
---|
[5cd136ab] | 545 | /** @}
|
---|
[398c4d7] | 546 | */
|
---|