[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>
|
---|
[3e6a98c5] | 47 | #include <stdbool.h>
|
---|
[729fa2d6] | 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 =
|
---|
[422722e] | 91 | service_connect_blocking(EXCHANGE_PARALLEL,
|
---|
[79ae36dd] | 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 =
|
---|
[422722e] | 140 | service_connect(EXCHANGE_PARALLEL, SERVICE_DEVMAN,
|
---|
[79ae36dd] | 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. */
|
---|
[f302586] | 179 | int devman_driver_register(const char *name)
|
---|
[729fa2d6] | 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) {
|
---|
[50b581d] | 190 | async_forget(req);
|
---|
[79ae36dd] | 191 | return retval;
|
---|
[729fa2d6] | 192 | }
|
---|
| 193 |
|
---|
[79ae36dd] | 194 | exch = devman_exchange_begin(DEVMAN_DRIVER);
|
---|
[f302586] | 195 | async_connect_to_me(exch, 0, 0, 0, NULL, NULL);
|
---|
[79ae36dd] | 196 | devman_exchange_end(exch);
|
---|
[729fa2d6] | 197 |
|
---|
[79ae36dd] | 198 | async_wait_for(req, &retval);
|
---|
[729fa2d6] | 199 | return retval;
|
---|
| 200 | }
|
---|
| 201 |
|
---|
[8b1e15ac] | 202 | /** Add function to a device.
|
---|
| 203 | *
|
---|
| 204 | * Request devman to add a new function to the specified device owned by
|
---|
| 205 | * this driver task.
|
---|
| 206 | *
|
---|
[79ae36dd] | 207 | * @param name Name of the new function
|
---|
| 208 | * @param ftype Function type, fun_inner or fun_exposed
|
---|
| 209 | * @param match_ids Match IDs (should be empty for fun_exposed)
|
---|
| 210 | * @param devh Devman handle of the device
|
---|
| 211 | * @param funh Place to store handle of the new function
|
---|
| 212 | *
|
---|
| 213 | * @return EOK on success or negative error code.
|
---|
[8b1e15ac] | 214 | *
|
---|
| 215 | */
|
---|
| 216 | int devman_add_function(const char *name, fun_type_t ftype,
|
---|
| 217 | match_id_list_t *match_ids, devman_handle_t devh, devman_handle_t *funh)
|
---|
[1b367b4] | 218 | {
|
---|
[8b1e15ac] | 219 | int match_count = list_count(&match_ids->ids);
|
---|
[79ae36dd] | 220 | async_exch_t *exch = devman_exchange_begin_blocking(DEVMAN_DRIVER);
|
---|
| 221 |
|
---|
[7707954] | 222 | ipc_call_t answer;
|
---|
[79ae36dd] | 223 | aid_t req = async_send_3(exch, DEVMAN_ADD_FUNCTION, (sysarg_t) ftype,
|
---|
[8b1e15ac] | 224 | devh, match_count, &answer);
|
---|
[79ae36dd] | 225 | sysarg_t retval = async_data_write_start(exch, name, str_size(name));
|
---|
[7707954] | 226 | if (retval != EOK) {
|
---|
[79ae36dd] | 227 | devman_exchange_end(exch);
|
---|
[50b581d] | 228 | async_forget(req);
|
---|
[7707954] | 229 | return retval;
|
---|
| 230 | }
|
---|
| 231 |
|
---|
[feeac0d] | 232 | list_foreach(match_ids->ids, link, match_id_t, match_id) {
|
---|
[15e54f5] | 233 | ipc_call_t answer2;
|
---|
| 234 | aid_t req2 = async_send_1(exch, DEVMAN_ADD_MATCH_ID,
|
---|
| 235 | match_id->score, &answer2);
|
---|
[79ae36dd] | 236 | retval = async_data_write_start(exch, match_id->id,
|
---|
| 237 | str_size(match_id->id));
|
---|
| 238 | if (retval != EOK) {
|
---|
| 239 | devman_exchange_end(exch);
|
---|
[50b581d] | 240 | async_forget(req2);
|
---|
| 241 | async_forget(req);
|
---|
[79ae36dd] | 242 | return retval;
|
---|
| 243 | }
|
---|
| 244 |
|
---|
[15e54f5] | 245 | async_wait_for(req2, &retval);
|
---|
[79ae36dd] | 246 | if (retval != EOK) {
|
---|
| 247 | devman_exchange_end(exch);
|
---|
[50b581d] | 248 | async_forget(req);
|
---|
[79ae36dd] | 249 | return retval;
|
---|
| 250 | }
|
---|
[4265fd4] | 251 | }
|
---|
[7707954] | 252 |
|
---|
[79ae36dd] | 253 | devman_exchange_end(exch);
|
---|
[15e54f5] | 254 |
|
---|
| 255 | async_wait_for(req, &retval);
|
---|
[e6211f8] | 256 | if (retval == EOK) {
|
---|
[15e54f5] | 257 | if (funh != NULL)
|
---|
| 258 | *funh = (int) IPC_GET_ARG1(answer);
|
---|
| 259 | } else {
|
---|
| 260 | if (funh != NULL)
|
---|
| 261 | *funh = -1;
|
---|
| 262 | }
|
---|
| 263 |
|
---|
| 264 | return retval;
|
---|
[7707954] | 265 | }
|
---|
| 266 |
|
---|
[1dc4a5e] | 267 | int devman_add_device_to_category(devman_handle_t devman_handle,
|
---|
| 268 | const char *cat_name)
|
---|
[692c40cb] | 269 | {
|
---|
[79ae36dd] | 270 | async_exch_t *exch = devman_exchange_begin_blocking(DEVMAN_DRIVER);
|
---|
[692c40cb] | 271 |
|
---|
| 272 | ipc_call_t answer;
|
---|
[1dc4a5e] | 273 | aid_t req = async_send_1(exch, DEVMAN_ADD_DEVICE_TO_CATEGORY,
|
---|
[1b367b4] | 274 | devman_handle, &answer);
|
---|
[1dc4a5e] | 275 | sysarg_t retval = async_data_write_start(exch, cat_name,
|
---|
| 276 | str_size(cat_name));
|
---|
[79ae36dd] | 277 |
|
---|
| 278 | devman_exchange_end(exch);
|
---|
| 279 |
|
---|
[692c40cb] | 280 | if (retval != EOK) {
|
---|
[50b581d] | 281 | async_forget(req);
|
---|
[692c40cb] | 282 | return retval;
|
---|
| 283 | }
|
---|
| 284 |
|
---|
| 285 | async_wait_for(req, &retval);
|
---|
[1b367b4] | 286 | return retval;
|
---|
[692c40cb] | 287 | }
|
---|
| 288 |
|
---|
[79ae36dd] | 289 | async_sess_t *devman_device_connect(exch_mgmt_t mgmt, devman_handle_t handle,
|
---|
| 290 | unsigned int flags)
|
---|
[5cd136ab] | 291 | {
|
---|
[79ae36dd] | 292 | async_sess_t *sess;
|
---|
[5cd136ab] | 293 |
|
---|
[79ae36dd] | 294 | if (flags & IPC_FLAG_BLOCKING)
|
---|
| 295 | sess = service_connect_blocking(mgmt, SERVICE_DEVMAN,
|
---|
| 296 | DEVMAN_CONNECT_TO_DEVICE, handle);
|
---|
| 297 | else
|
---|
| 298 | sess = service_connect(mgmt, SERVICE_DEVMAN,
|
---|
| 299 | DEVMAN_CONNECT_TO_DEVICE, handle);
|
---|
| 300 |
|
---|
| 301 | return sess;
|
---|
[5cd136ab] | 302 | }
|
---|
| 303 |
|
---|
[d0dd7b5] | 304 | /** Remove function from device.
|
---|
| 305 | *
|
---|
| 306 | * Request devman to remove function owned by this driver task.
|
---|
| 307 | * @param funh Devman handle of the function
|
---|
| 308 | *
|
---|
| 309 | * @return EOK on success or negative error code.
|
---|
| 310 | */
|
---|
| 311 | int devman_remove_function(devman_handle_t funh)
|
---|
| 312 | {
|
---|
| 313 | async_exch_t *exch;
|
---|
| 314 | sysarg_t retval;
|
---|
| 315 |
|
---|
| 316 | exch = devman_exchange_begin_blocking(DEVMAN_DRIVER);
|
---|
| 317 | retval = async_req_1_0(exch, DEVMAN_REMOVE_FUNCTION, (sysarg_t) funh);
|
---|
| 318 | devman_exchange_end(exch);
|
---|
| 319 |
|
---|
| 320 | return (int) retval;
|
---|
| 321 | }
|
---|
| 322 |
|
---|
[1a5b252] | 323 | int devman_drv_fun_online(devman_handle_t funh)
|
---|
| 324 | {
|
---|
| 325 | async_exch_t *exch = devman_exchange_begin(DEVMAN_DRIVER);
|
---|
| 326 | if (exch == NULL)
|
---|
| 327 | return ENOMEM;
|
---|
| 328 |
|
---|
| 329 | sysarg_t retval = async_req_1_0(exch, DEVMAN_DRV_FUN_ONLINE, funh);
|
---|
| 330 |
|
---|
| 331 | devman_exchange_end(exch);
|
---|
| 332 | return (int) retval;
|
---|
| 333 | }
|
---|
| 334 |
|
---|
| 335 | int devman_drv_fun_offline(devman_handle_t funh)
|
---|
| 336 | {
|
---|
| 337 | async_exch_t *exch = devman_exchange_begin(DEVMAN_DRIVER);
|
---|
| 338 | if (exch == NULL)
|
---|
| 339 | return ENOMEM;
|
---|
| 340 |
|
---|
| 341 | sysarg_t retval = async_req_1_0(exch, DEVMAN_DRV_FUN_OFFLINE, funh);
|
---|
| 342 |
|
---|
| 343 | devman_exchange_end(exch);
|
---|
| 344 | return (int) retval;
|
---|
| 345 | }
|
---|
| 346 |
|
---|
[79ae36dd] | 347 | async_sess_t *devman_parent_device_connect(exch_mgmt_t mgmt,
|
---|
| 348 | devman_handle_t handle, unsigned int flags)
|
---|
[5cd136ab] | 349 | {
|
---|
[79ae36dd] | 350 | async_sess_t *sess;
|
---|
[5cd136ab] | 351 |
|
---|
[79ae36dd] | 352 | if (flags & IPC_FLAG_BLOCKING)
|
---|
| 353 | sess = service_connect_blocking(mgmt, SERVICE_DEVMAN,
|
---|
| 354 | DEVMAN_CONNECT_TO_PARENTS_DEVICE, handle);
|
---|
| 355 | else
|
---|
| 356 | sess = service_connect(mgmt, SERVICE_DEVMAN,
|
---|
| 357 | DEVMAN_CONNECT_TO_PARENTS_DEVICE, handle);
|
---|
| 358 |
|
---|
| 359 | return sess;
|
---|
[5cd136ab] | 360 | }
|
---|
| 361 |
|
---|
[7beb220] | 362 | int devman_fun_get_handle(const char *pathname, devman_handle_t *handle,
|
---|
[1b367b4] | 363 | unsigned int flags)
|
---|
[f658458] | 364 | {
|
---|
[79ae36dd] | 365 | async_exch_t *exch;
|
---|
| 366 |
|
---|
| 367 | if (flags & IPC_FLAG_BLOCKING)
|
---|
| 368 | exch = devman_exchange_begin_blocking(DEVMAN_CLIENT);
|
---|
| 369 | else {
|
---|
| 370 | exch = devman_exchange_begin(DEVMAN_CLIENT);
|
---|
| 371 | if (exch == NULL)
|
---|
[e280857] | 372 | return ENOMEM;
|
---|
[79ae36dd] | 373 | }
|
---|
[f658458] | 374 |
|
---|
| 375 | ipc_call_t answer;
|
---|
[79ae36dd] | 376 | aid_t req = async_send_2(exch, DEVMAN_DEVICE_GET_HANDLE, flags, 0,
|
---|
[f658458] | 377 | &answer);
|
---|
[79ae36dd] | 378 | sysarg_t retval = async_data_write_start(exch, pathname,
|
---|
[1b367b4] | 379 | str_size(pathname));
|
---|
[79ae36dd] | 380 |
|
---|
| 381 | devman_exchange_end(exch);
|
---|
| 382 |
|
---|
[f658458] | 383 | if (retval != EOK) {
|
---|
[50b581d] | 384 | async_forget(req);
|
---|
[f658458] | 385 | return retval;
|
---|
| 386 | }
|
---|
| 387 |
|
---|
| 388 | async_wait_for(req, &retval);
|
---|
| 389 |
|
---|
| 390 | if (retval != EOK) {
|
---|
| 391 | if (handle != NULL)
|
---|
[0b5a4131] | 392 | *handle = (devman_handle_t) -1;
|
---|
[79ae36dd] | 393 |
|
---|
[f658458] | 394 | return retval;
|
---|
| 395 | }
|
---|
| 396 |
|
---|
| 397 | if (handle != NULL)
|
---|
[0b5a4131] | 398 | *handle = (devman_handle_t) IPC_GET_ARG1(answer);
|
---|
[f658458] | 399 |
|
---|
| 400 | return retval;
|
---|
| 401 | }
|
---|
| 402 |
|
---|
[7beb220] | 403 | static int devman_get_str_internal(sysarg_t method, sysarg_t arg1, char *buf,
|
---|
| 404 | size_t buf_size)
|
---|
[431d6d6] | 405 | {
|
---|
[7beb220] | 406 | async_exch_t *exch;
|
---|
| 407 | ipc_call_t dreply;
|
---|
| 408 | size_t act_size;
|
---|
| 409 | sysarg_t dretval;
|
---|
[79ae36dd] | 410 |
|
---|
[d87561c] | 411 | exch = devman_exchange_begin_blocking(DEVMAN_CLIENT);
|
---|
[79ae36dd] | 412 |
|
---|
[7beb220] | 413 | ipc_call_t answer;
|
---|
| 414 | aid_t req = async_send_1(exch, method, arg1, &answer);
|
---|
| 415 | aid_t dreq = async_data_read(exch, buf, buf_size - 1, &dreply);
|
---|
| 416 | async_wait_for(dreq, &dretval);
|
---|
[79ae36dd] | 417 |
|
---|
| 418 | devman_exchange_end(exch);
|
---|
| 419 |
|
---|
[7beb220] | 420 | if (dretval != EOK) {
|
---|
[50b581d] | 421 | async_forget(req);
|
---|
[7beb220] | 422 | return dretval;
|
---|
[431d6d6] | 423 | }
|
---|
[79ae36dd] | 424 |
|
---|
[7beb220] | 425 | sysarg_t retval;
|
---|
| 426 | async_wait_for(req, &retval);
|
---|
| 427 |
|
---|
[3f57fb7] | 428 | if (retval != EOK) {
|
---|
[7beb220] | 429 | return retval;
|
---|
[3f57fb7] | 430 | }
|
---|
[7beb220] | 431 |
|
---|
| 432 | act_size = IPC_GET_ARG2(dreply);
|
---|
| 433 | assert(act_size <= buf_size - 1);
|
---|
| 434 | buf[act_size] = '\0';
|
---|
| 435 |
|
---|
| 436 | return EOK;
|
---|
| 437 | }
|
---|
| 438 |
|
---|
| 439 | int devman_fun_get_path(devman_handle_t handle, char *buf, size_t buf_size)
|
---|
| 440 | {
|
---|
| 441 | return devman_get_str_internal(DEVMAN_FUN_GET_PATH, handle, buf,
|
---|
| 442 | buf_size);
|
---|
| 443 | }
|
---|
| 444 |
|
---|
| 445 | int devman_fun_get_name(devman_handle_t handle, char *buf, size_t buf_size)
|
---|
| 446 | {
|
---|
| 447 | return devman_get_str_internal(DEVMAN_FUN_GET_NAME, handle, buf,
|
---|
| 448 | buf_size);
|
---|
| 449 | }
|
---|
| 450 |
|
---|
[3f57fb7] | 451 | int devman_fun_get_driver_name(devman_handle_t handle, char *buf, size_t buf_size)
|
---|
| 452 | {
|
---|
| 453 | return devman_get_str_internal(DEVMAN_FUN_GET_DRIVER_NAME, handle, buf,
|
---|
| 454 | buf_size);
|
---|
| 455 | }
|
---|
| 456 |
|
---|
[1a5b252] | 457 | int devman_fun_online(devman_handle_t funh)
|
---|
| 458 | {
|
---|
| 459 | async_exch_t *exch = devman_exchange_begin(DEVMAN_CLIENT);
|
---|
| 460 | if (exch == NULL)
|
---|
| 461 | return ENOMEM;
|
---|
| 462 |
|
---|
| 463 | sysarg_t retval = async_req_1_0(exch, DEVMAN_FUN_ONLINE, funh);
|
---|
| 464 |
|
---|
| 465 | devman_exchange_end(exch);
|
---|
| 466 | return (int) retval;
|
---|
| 467 | }
|
---|
| 468 |
|
---|
| 469 | int devman_fun_offline(devman_handle_t funh)
|
---|
| 470 | {
|
---|
| 471 | async_exch_t *exch = devman_exchange_begin(DEVMAN_CLIENT);
|
---|
| 472 | if (exch == NULL)
|
---|
| 473 | return ENOMEM;
|
---|
| 474 |
|
---|
| 475 | sysarg_t retval = async_req_1_0(exch, DEVMAN_FUN_OFFLINE, funh);
|
---|
| 476 |
|
---|
| 477 | devman_exchange_end(exch);
|
---|
| 478 | return (int) retval;
|
---|
| 479 | }
|
---|
| 480 |
|
---|
[7beb220] | 481 | static int devman_get_handles_once(sysarg_t method, sysarg_t arg1,
|
---|
| 482 | devman_handle_t *handle_buf, size_t buf_size, size_t *act_size)
|
---|
| 483 | {
|
---|
| 484 | async_exch_t *exch = devman_exchange_begin_blocking(DEVMAN_CLIENT);
|
---|
| 485 |
|
---|
| 486 | ipc_call_t answer;
|
---|
| 487 | aid_t req = async_send_1(exch, method, arg1, &answer);
|
---|
| 488 | int rc = async_data_read_start(exch, handle_buf, buf_size);
|
---|
[79ae36dd] | 489 |
|
---|
[7beb220] | 490 | devman_exchange_end(exch);
|
---|
[79ae36dd] | 491 |
|
---|
[7beb220] | 492 | if (rc != EOK) {
|
---|
[50b581d] | 493 | async_forget(req);
|
---|
[7beb220] | 494 | return rc;
|
---|
[431d6d6] | 495 | }
|
---|
[79ae36dd] | 496 |
|
---|
[7beb220] | 497 | sysarg_t retval;
|
---|
| 498 | async_wait_for(req, &retval);
|
---|
[79ae36dd] | 499 |
|
---|
[7beb220] | 500 | if (retval != EOK) {
|
---|
| 501 | return retval;
|
---|
| 502 | }
|
---|
[79ae36dd] | 503 |
|
---|
[7beb220] | 504 | *act_size = IPC_GET_ARG1(answer);
|
---|
[431d6d6] | 505 | return EOK;
|
---|
| 506 | }
|
---|
| 507 |
|
---|
[7beb220] | 508 | /** Get list of handles.
|
---|
| 509 | *
|
---|
| 510 | * Returns an allocated array of handles.
|
---|
| 511 | *
|
---|
| 512 | * @param method IPC method
|
---|
| 513 | * @param arg1 IPC argument 1
|
---|
| 514 | * @param data Place to store pointer to array of handles
|
---|
| 515 | * @param count Place to store number of handles
|
---|
| 516 | * @return EOK on success or negative error code
|
---|
| 517 | */
|
---|
| 518 | static int devman_get_handles_internal(sysarg_t method, sysarg_t arg1,
|
---|
| 519 | devman_handle_t **data, size_t *count)
|
---|
| 520 | {
|
---|
| 521 | devman_handle_t *handles;
|
---|
| 522 | size_t act_size;
|
---|
| 523 | size_t alloc_size;
|
---|
| 524 | int rc;
|
---|
| 525 |
|
---|
| 526 | *data = NULL;
|
---|
| 527 | act_size = 0; /* silence warning */
|
---|
| 528 |
|
---|
| 529 | rc = devman_get_handles_once(method, arg1, NULL, 0,
|
---|
| 530 | &act_size);
|
---|
| 531 | if (rc != EOK)
|
---|
| 532 | return rc;
|
---|
| 533 |
|
---|
| 534 | alloc_size = act_size;
|
---|
| 535 | handles = malloc(alloc_size);
|
---|
| 536 | if (handles == NULL)
|
---|
| 537 | return ENOMEM;
|
---|
| 538 |
|
---|
| 539 | while (true) {
|
---|
| 540 | rc = devman_get_handles_once(method, arg1, handles, alloc_size,
|
---|
| 541 | &act_size);
|
---|
| 542 | if (rc != EOK)
|
---|
| 543 | return rc;
|
---|
| 544 |
|
---|
| 545 | if (act_size <= alloc_size)
|
---|
| 546 | break;
|
---|
| 547 |
|
---|
| 548 | alloc_size *= 2;
|
---|
| 549 | free(handles);
|
---|
| 550 |
|
---|
| 551 | handles = malloc(alloc_size);
|
---|
| 552 | if (handles == NULL)
|
---|
| 553 | return ENOMEM;
|
---|
| 554 | }
|
---|
| 555 |
|
---|
| 556 | *count = act_size / sizeof(devman_handle_t);
|
---|
| 557 | *data = handles;
|
---|
| 558 | return EOK;
|
---|
| 559 | }
|
---|
| 560 |
|
---|
| 561 | int devman_fun_get_child(devman_handle_t funh, devman_handle_t *devh)
|
---|
| 562 | {
|
---|
| 563 | async_exch_t *exch = devman_exchange_begin(DEVMAN_CLIENT);
|
---|
| 564 | if (exch == NULL)
|
---|
| 565 | return ENOMEM;
|
---|
| 566 |
|
---|
| 567 | sysarg_t retval = async_req_1_1(exch, DEVMAN_FUN_GET_CHILD,
|
---|
| 568 | funh, devh);
|
---|
| 569 |
|
---|
| 570 | devman_exchange_end(exch);
|
---|
| 571 | return (int) retval;
|
---|
| 572 | }
|
---|
| 573 |
|
---|
| 574 | int devman_dev_get_functions(devman_handle_t devh, devman_handle_t **funcs,
|
---|
| 575 | size_t *count)
|
---|
| 576 | {
|
---|
| 577 | return devman_get_handles_internal(DEVMAN_DEV_GET_FUNCTIONS,
|
---|
| 578 | devh, funcs, count);
|
---|
| 579 | }
|
---|
| 580 |
|
---|
[e280857] | 581 | int devman_fun_sid_to_handle(service_id_t sid, devman_handle_t *handle)
|
---|
| 582 | {
|
---|
| 583 | async_exch_t *exch = devman_exchange_begin(DEVMAN_CLIENT);
|
---|
| 584 | if (exch == NULL)
|
---|
| 585 | return ENOMEM;
|
---|
| 586 |
|
---|
| 587 | sysarg_t retval = async_req_1_1(exch, DEVMAN_FUN_SID_TO_HANDLE,
|
---|
| 588 | sid, handle);
|
---|
| 589 |
|
---|
| 590 | devman_exchange_end(exch);
|
---|
| 591 | return (int) retval;
|
---|
| 592 | }
|
---|
| 593 |
|
---|
[0511549] | 594 | int devman_get_drivers(devman_handle_t **drvs,
|
---|
| 595 | size_t *count)
|
---|
| 596 | {
|
---|
| 597 | return devman_get_handles_internal(DEVMAN_GET_DRIVERS, 0, drvs, count);
|
---|
| 598 | }
|
---|
| 599 |
|
---|
[7969087] | 600 | int devman_driver_get_handle(const char *drvname, devman_handle_t *handle)
|
---|
| 601 | {
|
---|
| 602 | async_exch_t *exch;
|
---|
| 603 |
|
---|
| 604 | exch = devman_exchange_begin(DEVMAN_CLIENT);
|
---|
| 605 | if (exch == NULL)
|
---|
| 606 | return ENOMEM;
|
---|
| 607 |
|
---|
| 608 | ipc_call_t answer;
|
---|
| 609 | aid_t req = async_send_0(exch, DEVMAN_DRIVER_GET_HANDLE, &answer);
|
---|
| 610 | sysarg_t retval = async_data_write_start(exch, drvname,
|
---|
| 611 | str_size(drvname));
|
---|
| 612 |
|
---|
| 613 | devman_exchange_end(exch);
|
---|
| 614 |
|
---|
| 615 | if (retval != EOK) {
|
---|
| 616 | async_forget(req);
|
---|
| 617 | return retval;
|
---|
| 618 | }
|
---|
| 619 |
|
---|
| 620 | async_wait_for(req, &retval);
|
---|
| 621 |
|
---|
| 622 | if (retval != EOK) {
|
---|
| 623 | if (handle != NULL)
|
---|
| 624 | *handle = (devman_handle_t) -1;
|
---|
| 625 |
|
---|
| 626 | return retval;
|
---|
| 627 | }
|
---|
| 628 |
|
---|
| 629 | if (handle != NULL)
|
---|
| 630 | *handle = (devman_handle_t) IPC_GET_ARG1(answer);
|
---|
| 631 |
|
---|
| 632 | return retval;
|
---|
| 633 | }
|
---|
| 634 |
|
---|
[0511549] | 635 | int devman_driver_get_name(devman_handle_t handle, char *buf, size_t buf_size)
|
---|
| 636 | {
|
---|
| 637 | return devman_get_str_internal(DEVMAN_DRIVER_GET_NAME, handle, buf,
|
---|
| 638 | buf_size);
|
---|
| 639 | }
|
---|
| 640 |
|
---|
[e5556e4a] | 641 | int devman_driver_get_state(devman_handle_t drvh, driver_state_t *rstate)
|
---|
| 642 | {
|
---|
| 643 | sysarg_t state;
|
---|
| 644 | async_exch_t *exch = devman_exchange_begin(DEVMAN_CLIENT);
|
---|
| 645 | if (exch == NULL)
|
---|
| 646 | return ENOMEM;
|
---|
| 647 |
|
---|
| 648 | int rc = async_req_1_1(exch, DEVMAN_DRIVER_GET_STATE, drvh,
|
---|
| 649 | &state);
|
---|
| 650 |
|
---|
| 651 | devman_exchange_end(exch);
|
---|
| 652 | if (rc != EOK)
|
---|
| 653 | return rc;
|
---|
| 654 |
|
---|
| 655 | *rstate = state;
|
---|
| 656 | return rc;
|
---|
| 657 | }
|
---|
| 658 |
|
---|
[7969087] | 659 | int devman_driver_load(devman_handle_t drvh)
|
---|
| 660 | {
|
---|
| 661 | async_exch_t *exch = devman_exchange_begin(DEVMAN_CLIENT);
|
---|
| 662 | if (exch == NULL)
|
---|
| 663 | return ENOMEM;
|
---|
| 664 |
|
---|
| 665 | int rc = async_req_1_0(exch, DEVMAN_DRIVER_LOAD, drvh);
|
---|
| 666 |
|
---|
| 667 | devman_exchange_end(exch);
|
---|
| 668 | return rc;
|
---|
| 669 | }
|
---|
| 670 |
|
---|
[5cd136ab] | 671 | /** @}
|
---|
[398c4d7] | 672 | */
|
---|