Changes in uspace/srv/devman/main.c [f278930:79ae36dd] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/srv/devman/main.c
rf278930 r79ae36dd 56 56 #include <ipc/driver.h> 57 57 #include <thread.h> 58 #include < loc.h>58 #include <devmap.h> 59 59 60 60 #include "devman.h" … … 64 64 static driver_list_t drivers_list; 65 65 static dev_tree_t device_tree; 66 67 static int init_running_drv(void *drv); 66 static class_list_t class_list; 68 67 69 68 /** Register running driver. */ 70 static driver_t *devman_driver_register(ipc_callid_t callid, ipc_call_t *call) 71 { 69 static driver_t *devman_driver_register(void) 70 { 71 ipc_call_t icall; 72 ipc_callid_t iid; 72 73 driver_t *driver = NULL; 74 75 log_msg(LVL_DEBUG, "devman_driver_register"); 76 77 iid = async_get_call(&icall); 78 if (IPC_GET_IMETHOD(icall) != DEVMAN_DRIVER_REGISTER) { 79 async_answer_0(iid, EREFUSED); 80 return NULL; 81 } 82 73 83 char *drv_name = NULL; 74 75 log_msg(LVL_DEBUG, "devman_driver_register");76 84 77 85 /* Get driver name. */ 78 86 int rc = async_data_write_accept((void **) &drv_name, true, 0, 0, 0, 0); 79 87 if (rc != EOK) { 80 async_answer_0( callid, rc);88 async_answer_0(iid, rc); 81 89 return NULL; 82 90 } … … 91 99 free(drv_name); 92 100 drv_name = NULL; 93 async_answer_0( callid, ENOENT);101 async_answer_0(iid, ENOENT); 94 102 return NULL; 95 103 } … … 105 113 driver->name); 106 114 fibril_mutex_unlock(&driver->driver_mutex); 107 async_answer_0( callid, EEXISTS);115 async_answer_0(iid, EEXISTS); 108 116 return NULL; 109 117 } … … 127 135 log_msg(LVL_DEBUG, "Creating connection to the `%s' driver.", 128 136 driver->name); 129 driver->sess = async_callback_receive(EXCHANGE_ PARALLEL);137 driver->sess = async_callback_receive(EXCHANGE_SERIALIZE); 130 138 if (!driver->sess) { 131 139 fibril_mutex_unlock(&driver->driver_mutex); 132 async_answer_0( callid, ENOTSUP);140 async_answer_0(iid, ENOTSUP); 133 141 return NULL; 134 142 } 135 /* FIXME: Work around problem with callback sessions */136 async_sess_args_set(driver->sess, DRIVER_DEVMAN, 0, 0);143 144 fibril_mutex_unlock(&driver->driver_mutex); 137 145 138 146 log_msg(LVL_NOTE, 139 147 "The `%s' driver was successfully registered as running.", 140 148 driver->name); 149 150 async_answer_0(iid, EOK); 151 152 return driver; 153 } 154 155 /** Receive device match ID from the device's parent driver and add it to the 156 * list of devices match ids. 157 * 158 * @param match_ids The list of the device's match ids. 159 * @return Zero on success, negative error code otherwise. 160 */ 161 static int devman_receive_match_id(match_id_list_t *match_ids) 162 { 163 match_id_t *match_id = create_match_id(); 164 ipc_callid_t callid; 165 ipc_call_t call; 166 int rc = 0; 167 168 callid = async_get_call(&call); 169 if (DEVMAN_ADD_MATCH_ID != IPC_GET_IMETHOD(call)) { 170 log_msg(LVL_ERROR, 171 "Invalid protocol when trying to receive match id."); 172 async_answer_0(callid, EINVAL); 173 delete_match_id(match_id); 174 return EINVAL; 175 } 176 177 if (match_id == NULL) { 178 log_msg(LVL_ERROR, "Failed to allocate match id."); 179 async_answer_0(callid, ENOMEM); 180 return ENOMEM; 181 } 182 183 async_answer_0(callid, EOK); 184 185 match_id->score = IPC_GET_ARG1(call); 186 187 char *match_id_str; 188 rc = async_data_write_accept((void **) &match_id_str, true, 0, 0, 0, 0); 189 match_id->id = match_id_str; 190 if (rc != EOK) { 191 delete_match_id(match_id); 192 log_msg(LVL_ERROR, "Failed to receive match id string: %s.", 193 str_error(rc)); 194 return rc; 195 } 196 197 list_append(&match_id->link, &match_ids->ids); 198 199 log_msg(LVL_DEBUG, "Received match id `%s', score %d.", 200 match_id->id, match_id->score); 201 return rc; 202 } 203 204 /** Receive device match IDs from the device's parent driver and add them to the 205 * list of devices match ids. 206 * 207 * @param match_count The number of device's match ids to be received. 208 * @param match_ids The list of the device's match ids. 209 * @return Zero on success, negative error code otherwise. 210 */ 211 static int devman_receive_match_ids(sysarg_t match_count, 212 match_id_list_t *match_ids) 213 { 214 int ret = EOK; 215 size_t i; 216 217 for (i = 0; i < match_count; i++) { 218 if (EOK != (ret = devman_receive_match_id(match_ids))) 219 return ret; 220 } 221 return ret; 222 } 223 224 static int assign_driver_fibril(void *arg) 225 { 226 dev_node_t *dev_node = (dev_node_t *) arg; 227 assign_driver(dev_node, &drivers_list, &device_tree); 228 return EOK; 229 } 230 231 /** Handle function registration. 232 * 233 * Child devices are registered by their parent's device driver. 234 */ 235 static void devman_add_function(ipc_callid_t callid, ipc_call_t *call) 236 { 237 fun_type_t ftype = (fun_type_t) IPC_GET_ARG1(*call); 238 devman_handle_t dev_handle = IPC_GET_ARG2(*call); 239 sysarg_t match_count = IPC_GET_ARG3(*call); 240 dev_tree_t *tree = &device_tree; 241 242 fibril_rwlock_write_lock(&tree->rwlock); 243 244 dev_node_t *dev = NULL; 245 dev_node_t *pdev = find_dev_node_no_lock(&device_tree, dev_handle); 246 247 if (pdev == NULL) { 248 fibril_rwlock_write_unlock(&tree->rwlock); 249 async_answer_0(callid, ENOENT); 250 return; 251 } 252 253 if (ftype != fun_inner && ftype != fun_exposed) { 254 /* Unknown function type */ 255 log_msg(LVL_ERROR, 256 "Unknown function type %d provided by driver.", 257 (int) ftype); 258 259 fibril_rwlock_write_unlock(&tree->rwlock); 260 async_answer_0(callid, EINVAL); 261 return; 262 } 263 264 char *fun_name = NULL; 265 int rc = async_data_write_accept((void **)&fun_name, true, 0, 0, 0, 0); 266 if (rc != EOK) { 267 fibril_rwlock_write_unlock(&tree->rwlock); 268 async_answer_0(callid, rc); 269 return; 270 } 271 272 /* Check that function with same name is not there already. */ 273 if (find_fun_node_in_device(pdev, fun_name) != NULL) { 274 fibril_rwlock_write_unlock(&tree->rwlock); 275 async_answer_0(callid, EEXISTS); 276 printf(NAME ": Warning, driver tried to register `%s' twice.\n", 277 fun_name); 278 free(fun_name); 279 return; 280 } 281 282 fun_node_t *fun = create_fun_node(); 283 if (!insert_fun_node(&device_tree, fun, fun_name, pdev)) { 284 fibril_rwlock_write_unlock(&tree->rwlock); 285 delete_fun_node(fun); 286 async_answer_0(callid, ENOMEM); 287 return; 288 } 289 290 if (ftype == fun_inner) { 291 dev = create_dev_node(); 292 if (dev == NULL) { 293 fibril_rwlock_write_unlock(&tree->rwlock); 294 delete_fun_node(fun); 295 async_answer_0(callid, ENOMEM); 296 return; 297 } 298 299 insert_dev_node(tree, dev, fun); 300 } 301 302 fibril_rwlock_write_unlock(&tree->rwlock); 303 304 log_msg(LVL_DEBUG, "devman_add_function(fun=\"%s\")", fun->pathname); 305 306 devman_receive_match_ids(match_count, &fun->match_ids); 307 308 if (ftype == fun_inner) { 309 assert(dev != NULL); 310 /* 311 * Try to find a suitable driver and assign it to the device. We do 312 * not want to block the current fibril that is used for processing 313 * incoming calls: we will launch a separate fibril to handle the 314 * driver assigning. That is because assign_driver can actually include 315 * task spawning which could take some time. 316 */ 317 fid_t assign_fibril = fibril_create(assign_driver_fibril, dev); 318 if (assign_fibril == 0) { 319 /* 320 * Fallback in case we are out of memory. 321 * Probably not needed as we will die soon anyway ;-). 322 */ 323 (void) assign_driver_fibril(fun); 324 } else { 325 fibril_add_ready(assign_fibril); 326 } 327 } else { 328 devmap_register_tree_function(fun, tree); 329 } 330 331 /* Return device handle to parent's driver. */ 332 async_answer_1(callid, EOK, fun->handle); 333 } 334 335 static void devmap_register_class_dev(dev_class_info_t *cli) 336 { 337 /* Create devmap path and name for the device. */ 338 char *devmap_pathname = NULL; 339 340 asprintf(&devmap_pathname, "%s/%s%c%s", DEVMAP_CLASS_NAMESPACE, 341 cli->dev_class->name, DEVMAP_SEPARATOR, cli->dev_name); 342 if (devmap_pathname == NULL) 343 return; 344 345 /* 346 * Register the device by the device mapper and remember its devmap 347 * handle. 348 */ 349 devmap_device_register_with_iface(devmap_pathname, 350 &cli->devmap_handle, DEVMAN_CONNECT_FROM_DEVMAP); 351 352 /* 353 * Add device to the hash map of class devices registered by device 354 * mapper. 355 */ 356 class_add_devmap_function(&class_list, cli); 357 358 free(devmap_pathname); 359 } 360 361 static void devman_add_function_to_class(ipc_callid_t callid, ipc_call_t *call) 362 { 363 devman_handle_t handle = IPC_GET_ARG1(*call); 364 365 /* Get class name. */ 366 char *class_name; 367 int rc = async_data_write_accept((void **) &class_name, true, 368 0, 0, 0, 0); 369 if (rc != EOK) { 370 async_answer_0(callid, rc); 371 return; 372 } 373 374 fun_node_t *fun = find_fun_node(&device_tree, handle); 375 if (fun == NULL) { 376 async_answer_0(callid, ENOENT); 377 return; 378 } 379 380 dev_class_t *cl = get_dev_class(&class_list, class_name); 381 dev_class_info_t *class_info = add_function_to_class(fun, cl, NULL); 382 383 /* Register the device's class alias by devmapper. */ 384 devmap_register_class_dev(class_info); 385 386 log_msg(LVL_NOTE, "Function `%s' added to class `%s' as `%s'.", 387 fun->pathname, class_name, class_info->dev_name); 388 389 async_answer_0(callid, EOK); 390 } 391 392 /** Initialize driver which has registered itself as running and ready. 393 * 394 * The initialization is done in a separate fibril to avoid deadlocks (if the 395 * driver needed to be served by devman during the driver's initialization). 396 */ 397 static int init_running_drv(void *drv) 398 { 399 driver_t *driver = (driver_t *) drv; 400 401 initialize_running_driver(driver, &device_tree); 402 log_msg(LVL_DEBUG, "The `%s` driver was successfully initialized.", 403 driver->name); 404 return 0; 405 } 406 407 /** Function for handling connections from a driver to the device manager. */ 408 static void devman_connection_driver(ipc_callid_t iid, ipc_call_t *icall) 409 { 410 /* Accept the connection. */ 411 async_answer_0(iid, EOK); 412 413 driver_t *driver = devman_driver_register(); 414 if (driver == NULL) 415 return; 141 416 142 417 /* … … 149 424 log_msg(LVL_ERROR, "Failed to create initialization fibril " \ 150 425 "for driver `%s'.", driver->name); 151 fibril_mutex_unlock(&driver->driver_mutex); 152 async_answer_0(callid, ENOMEM); 153 return NULL; 154 } 155 426 return; 427 } 156 428 fibril_add_ready(fid); 157 fibril_mutex_unlock(&driver->driver_mutex);158 159 async_answer_0(callid, EOK);160 return driver;161 }162 163 /** Receive device match ID from the device's parent driver and add it to the164 * list of devices match ids.165 *166 * @param match_ids The list of the device's match ids.167 * @return Zero on success, negative error code otherwise.168 */169 static int devman_receive_match_id(match_id_list_t *match_ids)170 {171 match_id_t *match_id = create_match_id();172 ipc_callid_t callid;173 ipc_call_t call;174 int rc = 0;175 176 callid = async_get_call(&call);177 if (DEVMAN_ADD_MATCH_ID != IPC_GET_IMETHOD(call)) {178 log_msg(LVL_ERROR,179 "Invalid protocol when trying to receive match id.");180 async_answer_0(callid, EINVAL);181 delete_match_id(match_id);182 return EINVAL;183 }184 185 if (match_id == NULL) {186 log_msg(LVL_ERROR, "Failed to allocate match id.");187 async_answer_0(callid, ENOMEM);188 return ENOMEM;189 }190 191 async_answer_0(callid, EOK);192 193 match_id->score = IPC_GET_ARG1(call);194 195 char *match_id_str;196 rc = async_data_write_accept((void **) &match_id_str, true, 0, 0, 0, 0);197 match_id->id = match_id_str;198 if (rc != EOK) {199 delete_match_id(match_id);200 log_msg(LVL_ERROR, "Failed to receive match id string: %s.",201 str_error(rc));202 return rc;203 }204 205 list_append(&match_id->link, &match_ids->ids);206 207 log_msg(LVL_DEBUG, "Received match id `%s', score %d.",208 match_id->id, match_id->score);209 return rc;210 }211 212 /** Receive device match IDs from the device's parent driver and add them to the213 * list of devices match ids.214 *215 * @param match_count The number of device's match ids to be received.216 * @param match_ids The list of the device's match ids.217 * @return Zero on success, negative error code otherwise.218 */219 static int devman_receive_match_ids(sysarg_t match_count,220 match_id_list_t *match_ids)221 {222 int ret = EOK;223 size_t i;224 225 for (i = 0; i < match_count; i++) {226 if (EOK != (ret = devman_receive_match_id(match_ids)))227 return ret;228 }229 return ret;230 }231 232 static int assign_driver_fibril(void *arg)233 {234 dev_node_t *dev_node = (dev_node_t *) arg;235 assign_driver(dev_node, &drivers_list, &device_tree);236 237 /* Delete one reference we got from the caller. */238 dev_del_ref(dev_node);239 return EOK;240 }241 242 static int online_function(fun_node_t *fun)243 {244 dev_node_t *dev;245 246 fibril_rwlock_write_lock(&device_tree.rwlock);247 248 if (fun->state == FUN_ON_LINE) {249 fibril_rwlock_write_unlock(&device_tree.rwlock);250 log_msg(LVL_WARN, "Function %s is already on line.",251 fun->pathname);252 return EOK;253 }254 255 if (fun->ftype == fun_inner) {256 dev = create_dev_node();257 if (dev == NULL) {258 fibril_rwlock_write_unlock(&device_tree.rwlock);259 return ENOMEM;260 }261 262 insert_dev_node(&device_tree, dev, fun);263 dev_add_ref(dev);264 }265 266 log_msg(LVL_DEBUG, "devman_add_function(fun=\"%s\")", fun->pathname);267 268 if (fun->ftype == fun_inner) {269 dev = fun->child;270 assert(dev != NULL);271 272 /* Give one reference over to assign_driver_fibril(). */273 dev_add_ref(dev);274 /*275 * Try to find a suitable driver and assign it to the device. We do276 * not want to block the current fibril that is used for processing277 * incoming calls: we will launch a separate fibril to handle the278 * driver assigning. That is because assign_driver can actually include279 * task spawning which could take some time.280 */281 fid_t assign_fibril = fibril_create(assign_driver_fibril, dev);282 if (assign_fibril == 0) {283 log_msg(LVL_ERROR, "Failed to create fibril for "284 "assigning driver.");285 /* XXX Cleanup */286 fibril_rwlock_write_unlock(&device_tree.rwlock);287 return ENOMEM;288 }289 fibril_add_ready(assign_fibril);290 } else {291 loc_register_tree_function(fun, &device_tree);292 }293 294 fibril_rwlock_write_unlock(&device_tree.rwlock);295 296 return EOK;297 }298 299 static int offline_function(fun_node_t *fun)300 {301 int rc;302 303 fibril_rwlock_write_lock(&device_tree.rwlock);304 305 if (fun->state == FUN_OFF_LINE) {306 fibril_rwlock_write_unlock(&device_tree.rwlock);307 log_msg(LVL_WARN, "Function %s is already off line.",308 fun->pathname);309 return EOK;310 }311 312 if (fun->ftype == fun_inner) {313 log_msg(LVL_DEBUG, "Offlining inner function %s.",314 fun->pathname);315 316 if (fun->child != NULL) {317 dev_node_t *dev = fun->child;318 319 dev_add_ref(dev);320 fibril_rwlock_write_unlock(&device_tree.rwlock);321 322 /* If device is owned by driver, ask driver to give it up. */323 if (dev->state == DEVICE_USABLE) {324 rc = driver_dev_remove(&device_tree, dev);325 if (rc != EOK) {326 dev_del_ref(dev);327 return ENOTSUP;328 }329 }330 331 /* Verify that driver removed all functions */332 fibril_rwlock_read_lock(&device_tree.rwlock);333 if (!list_empty(&dev->functions)) {334 fibril_rwlock_read_unlock(&device_tree.rwlock);335 return EIO;336 }337 driver_t *driver = dev->drv;338 fibril_rwlock_read_unlock(&device_tree.rwlock);339 340 if (driver)341 detach_driver(&device_tree, dev);342 343 fibril_rwlock_write_lock(&device_tree.rwlock);344 remove_dev_node(&device_tree, dev);345 346 /* Delete ref created when node was inserted */347 dev_del_ref(dev);348 /* Delete ref created by dev_add_ref(dev) above */349 dev_del_ref(dev);350 }351 } else {352 /* Unregister from location service */353 rc = loc_service_unregister(fun->service_id);354 if (rc != EOK) {355 fibril_rwlock_write_unlock(&device_tree.rwlock);356 log_msg(LVL_ERROR, "Failed unregistering tree service.");357 return EIO;358 }359 360 fun->service_id = 0;361 }362 363 fun->state = FUN_OFF_LINE;364 fibril_rwlock_write_unlock(&device_tree.rwlock);365 366 return EOK;367 }368 369 /** Handle function registration.370 *371 * Child devices are registered by their parent's device driver.372 */373 static void devman_add_function(ipc_callid_t callid, ipc_call_t *call)374 {375 fun_type_t ftype = (fun_type_t) IPC_GET_ARG1(*call);376 devman_handle_t dev_handle = IPC_GET_ARG2(*call);377 sysarg_t match_count = IPC_GET_ARG3(*call);378 dev_tree_t *tree = &device_tree;379 380 dev_node_t *pdev = find_dev_node(&device_tree, dev_handle);381 if (pdev == NULL) {382 async_answer_0(callid, ENOENT);383 return;384 }385 386 if (ftype != fun_inner && ftype != fun_exposed) {387 /* Unknown function type */388 log_msg(LVL_ERROR,389 "Unknown function type %d provided by driver.",390 (int) ftype);391 392 dev_del_ref(pdev);393 async_answer_0(callid, EINVAL);394 return;395 }396 397 char *fun_name = NULL;398 int rc = async_data_write_accept((void **)&fun_name, true, 0, 0, 0, 0);399 if (rc != EOK) {400 dev_del_ref(pdev);401 async_answer_0(callid, rc);402 return;403 }404 405 fibril_rwlock_write_lock(&tree->rwlock);406 407 /* Check device state */408 if (pdev->state == DEVICE_REMOVED) {409 fibril_rwlock_write_unlock(&tree->rwlock);410 dev_del_ref(pdev);411 async_answer_0(callid, ENOENT);412 return;413 }414 415 /* Check that function with same name is not there already. */416 if (find_fun_node_in_device(tree, pdev, fun_name) != NULL) {417 fibril_rwlock_write_unlock(&tree->rwlock);418 dev_del_ref(pdev);419 async_answer_0(callid, EEXISTS);420 printf(NAME ": Warning, driver tried to register `%s' twice.\n",421 fun_name);422 free(fun_name);423 return;424 }425 426 fun_node_t *fun = create_fun_node();427 fun_add_ref(fun);428 fun->ftype = ftype;429 430 if (!insert_fun_node(&device_tree, fun, fun_name, pdev)) {431 fibril_rwlock_write_unlock(&tree->rwlock);432 dev_del_ref(pdev);433 delete_fun_node(fun);434 async_answer_0(callid, ENOMEM);435 return;436 }437 438 fibril_rwlock_write_unlock(&tree->rwlock);439 dev_del_ref(pdev);440 441 devman_receive_match_ids(match_count, &fun->match_ids);442 443 rc = online_function(fun);444 if (rc != EOK) {445 /* XXX clean up */446 async_answer_0(callid, rc);447 return;448 }449 450 /* Return device handle to parent's driver. */451 async_answer_1(callid, EOK, fun->handle);452 }453 454 static void devman_add_function_to_cat(ipc_callid_t callid, ipc_call_t *call)455 {456 devman_handle_t handle = IPC_GET_ARG1(*call);457 category_id_t cat_id;458 int rc;459 460 /* Get category name. */461 char *cat_name;462 rc = async_data_write_accept((void **) &cat_name, true,463 0, 0, 0, 0);464 if (rc != EOK) {465 async_answer_0(callid, rc);466 return;467 }468 469 fun_node_t *fun = find_fun_node(&device_tree, handle);470 if (fun == NULL) {471 async_answer_0(callid, ENOENT);472 return;473 }474 475 fibril_rwlock_read_lock(&device_tree.rwlock);476 477 /* Check function state */478 if (fun->state == FUN_REMOVED) {479 fibril_rwlock_read_unlock(&device_tree.rwlock);480 async_answer_0(callid, ENOENT);481 return;482 }483 484 rc = loc_category_get_id(cat_name, &cat_id, IPC_FLAG_BLOCKING);485 if (rc == EOK) {486 loc_service_add_to_cat(fun->service_id, cat_id);487 } else {488 log_msg(LVL_ERROR, "Failed adding function `%s' to category "489 "`%s'.", fun->pathname, cat_name);490 }491 492 log_msg(LVL_NOTE, "Function `%s' added to category `%s'.",493 fun->pathname, cat_name);494 495 fibril_rwlock_read_unlock(&device_tree.rwlock);496 fun_del_ref(fun);497 498 async_answer_0(callid, EOK);499 }500 501 /** Online function by driver request.502 *503 */504 static void devman_drv_fun_online(ipc_callid_t iid, ipc_call_t *icall,505 driver_t *drv)506 {507 fun_node_t *fun;508 int rc;509 510 printf("devman_drv_fun_online()\n");511 fun = find_fun_node(&device_tree, IPC_GET_ARG1(*icall));512 if (fun == NULL) {513 async_answer_0(iid, ENOENT);514 return;515 }516 517 fibril_rwlock_read_lock(&device_tree.rwlock);518 if (fun->dev == NULL || fun->dev->drv != drv) {519 fibril_rwlock_read_unlock(&device_tree.rwlock);520 fun_del_ref(fun);521 async_answer_0(iid, ENOENT);522 return;523 }524 fibril_rwlock_read_unlock(&device_tree.rwlock);525 526 rc = online_function(fun);527 if (rc != EOK) {528 printf("devman_drv_fun_online() online_fun->ERROR\n");529 fun_del_ref(fun);530 async_answer_0(iid, (sysarg_t) rc);531 return;532 }533 534 fun_del_ref(fun);535 printf("devman_drv_fun_online() online_fun->OK\n");536 537 async_answer_0(iid, (sysarg_t) EOK);538 }539 540 541 /** Offline function by driver request.542 *543 */544 static void devman_drv_fun_offline(ipc_callid_t iid, ipc_call_t *icall,545 driver_t *drv)546 {547 fun_node_t *fun;548 int rc;549 550 fun = find_fun_node(&device_tree, IPC_GET_ARG1(*icall));551 if (fun == NULL) {552 async_answer_0(iid, ENOENT);553 return;554 }555 556 fibril_rwlock_write_lock(&device_tree.rwlock);557 if (fun->dev == NULL || fun->dev->drv != drv) {558 fun_del_ref(fun);559 async_answer_0(iid, ENOENT);560 return;561 }562 fibril_rwlock_write_unlock(&device_tree.rwlock);563 564 rc = offline_function(fun);565 if (rc != EOK) {566 fun_del_ref(fun);567 async_answer_0(iid, (sysarg_t) rc);568 return;569 }570 571 fun_del_ref(fun);572 async_answer_0(iid, (sysarg_t) EOK);573 }574 575 /** Remove function. */576 static void devman_remove_function(ipc_callid_t callid, ipc_call_t *call)577 {578 devman_handle_t fun_handle = IPC_GET_ARG1(*call);579 dev_tree_t *tree = &device_tree;580 int rc;581 582 583 fun_node_t *fun = find_fun_node(&device_tree, fun_handle);584 if (fun == NULL) {585 async_answer_0(callid, ENOENT);586 return;587 }588 589 fibril_rwlock_write_lock(&tree->rwlock);590 591 log_msg(LVL_DEBUG, "devman_remove_function(fun='%s')", fun->pathname);592 593 /* Check function state */594 if (fun->state == FUN_REMOVED) {595 fibril_rwlock_write_unlock(&tree->rwlock);596 async_answer_0(callid, ENOENT);597 return;598 }599 600 if (fun->ftype == fun_inner) {601 /* Handle possible descendants */602 /* TODO - This is a surprise removal */603 if (fun->child != NULL) {604 log_msg(LVL_WARN, "devman_remove_function(): not handling "605 "descendants\n");606 }607 } else {608 if (fun->service_id != 0) {609 /* Unregister from location service */610 rc = loc_service_unregister(fun->service_id);611 if (rc != EOK) {612 log_msg(LVL_ERROR, "Failed unregistering tree "613 "service.");614 fibril_rwlock_write_unlock(&tree->rwlock);615 fun_del_ref(fun);616 async_answer_0(callid, EIO);617 return;618 }619 }620 }621 622 remove_fun_node(&device_tree, fun);623 fibril_rwlock_write_unlock(&tree->rwlock);624 625 /* Delete ref added when inserting function into tree */626 fun_del_ref(fun);627 /* Delete ref added above when looking up function */628 fun_del_ref(fun);629 630 log_msg(LVL_DEBUG, "devman_remove_function() succeeded.");631 async_answer_0(callid, EOK);632 }633 634 /** Initialize driver which has registered itself as running and ready.635 *636 * The initialization is done in a separate fibril to avoid deadlocks (if the637 * driver needed to be served by devman during the driver's initialization).638 */639 static int init_running_drv(void *drv)640 {641 driver_t *driver = (driver_t *) drv;642 643 initialize_running_driver(driver, &device_tree);644 log_msg(LVL_DEBUG, "The `%s` driver was successfully initialized.",645 driver->name);646 return 0;647 }648 649 /** Function for handling connections from a driver to the device manager. */650 static void devman_connection_driver(ipc_callid_t iid, ipc_call_t *icall)651 {652 client_t *client;653 driver_t *driver;654 655 /* Accept the connection. */656 async_answer_0(iid, EOK);657 658 client = async_get_client_data();659 if (client == NULL) {660 log_msg(LVL_ERROR, "Failed to allocate client data.");661 return;662 }663 429 664 430 while (true) { … … 669 435 break; 670 436 671 if (IPC_GET_IMETHOD(call) != DEVMAN_DRIVER_REGISTER) {672 fibril_mutex_lock(&client->mutex);673 driver = client->driver;674 fibril_mutex_unlock(&client->mutex);675 if (driver == NULL) {676 /* First call must be to DEVMAN_DRIVER_REGISTER */677 async_answer_0(callid, ENOTSUP);678 continue;679 }680 }681 682 437 switch (IPC_GET_IMETHOD(call)) { 683 case DEVMAN_DRIVER_REGISTER:684 fibril_mutex_lock(&client->mutex);685 if (client->driver != NULL) {686 fibril_mutex_unlock(&client->mutex);687 async_answer_0(callid, EINVAL);688 continue;689 }690 client->driver = devman_driver_register(callid, &call);691 fibril_mutex_unlock(&client->mutex);692 break;693 438 case DEVMAN_ADD_FUNCTION: 694 439 devman_add_function(callid, &call); 695 440 break; 696 case DEVMAN_ADD_DEVICE_TO_CATEGORY: 697 devman_add_function_to_cat(callid, &call); 698 break; 699 case DEVMAN_DRV_FUN_ONLINE: 700 devman_drv_fun_online(callid, &call, driver); 701 break; 702 case DEVMAN_DRV_FUN_OFFLINE: 703 devman_drv_fun_offline(callid, &call, driver); 704 break; 705 case DEVMAN_REMOVE_FUNCTION: 706 devman_remove_function(callid, &call); 441 case DEVMAN_ADD_DEVICE_TO_CLASS: 442 devman_add_function_to_class(callid, &call); 707 443 break; 708 444 default: 709 async_answer_0(callid, EINVAL); 445 async_answer_0(callid, EINVAL); 710 446 break; 711 447 } … … 718 454 { 719 455 char *pathname; 720 devman_handle_t handle;721 456 722 457 int rc = async_data_write_accept((void **) &pathname, true, 0, 0, 0, 0); … … 735 470 } 736 471 737 fibril_rwlock_read_lock(&device_tree.rwlock); 738 739 /* Check function state */ 740 if (fun->state == FUN_REMOVED) { 741 fibril_rwlock_read_unlock(&device_tree.rwlock); 472 async_answer_1(iid, EOK, fun->handle); 473 } 474 475 /** Find handle for the device instance identified by device class name. */ 476 static void devman_function_get_handle_by_class(ipc_callid_t iid, 477 ipc_call_t *icall) 478 { 479 char *classname; 480 char *devname; 481 482 int rc = async_data_write_accept((void **) &classname, true, 0, 0, 0, 0); 483 if (rc != EOK) { 484 async_answer_0(iid, rc); 485 return; 486 } 487 rc = async_data_write_accept((void **) &devname, true, 0, 0, 0, 0); 488 if (rc != EOK) { 489 free(classname); 490 async_answer_0(iid, rc); 491 return; 492 } 493 494 495 fun_node_t *fun = find_fun_node_by_class(&class_list, 496 classname, devname); 497 498 free(classname); 499 free(devname); 500 501 if (fun == NULL) { 742 502 async_answer_0(iid, ENOENT); 743 503 return; 744 504 } 745 handle = fun->handle; 746 747 fibril_rwlock_read_unlock(&device_tree.rwlock); 748 749 /* Delete reference created above by find_fun_node_by_path() */ 750 fun_del_ref(fun); 751 752 async_answer_1(iid, EOK, handle); 753 } 754 755 /** Get device name. */ 756 static void devman_fun_get_name(ipc_callid_t iid, ipc_call_t *icall) 505 506 async_answer_1(iid, EOK, fun->handle); 507 } 508 509 /** Find device path by its handle. */ 510 static void devman_get_device_path_by_handle(ipc_callid_t iid, 511 ipc_call_t *icall) 757 512 { 758 513 devman_handle_t handle = IPC_GET_ARG1(*icall); … … 768 523 if (!async_data_read_receive(&data_callid, &data_len)) { 769 524 async_answer_0(iid, EINVAL); 770 fun_del_ref(fun);771 525 return; 772 526 } … … 776 530 async_answer_0(data_callid, ENOMEM); 777 531 async_answer_0(iid, ENOMEM); 778 fun_del_ref(fun); 779 return; 780 } 781 782 fibril_rwlock_read_lock(&device_tree.rwlock); 783 784 /* Check function state */ 785 if (fun->state == FUN_REMOVED) { 786 fibril_rwlock_read_unlock(&device_tree.rwlock); 787 free(buffer); 788 789 async_answer_0(data_callid, ENOENT); 790 async_answer_0(iid, ENOENT); 791 fun_del_ref(fun); 792 return; 793 } 794 795 size_t sent_length = str_size(fun->name); 796 if (sent_length > data_len) { 797 sent_length = data_len; 798 } 799 800 async_data_read_finalize(data_callid, fun->name, sent_length); 801 async_answer_0(iid, EOK); 802 803 fibril_rwlock_read_unlock(&device_tree.rwlock); 804 fun_del_ref(fun); 805 free(buffer); 806 } 807 808 809 /** Get device path. */ 810 static void devman_fun_get_path(ipc_callid_t iid, ipc_call_t *icall) 811 { 812 devman_handle_t handle = IPC_GET_ARG1(*icall); 813 814 fun_node_t *fun = find_fun_node(&device_tree, handle); 815 if (fun == NULL) { 816 async_answer_0(iid, ENOMEM); 817 return; 818 } 819 820 ipc_callid_t data_callid; 821 size_t data_len; 822 if (!async_data_read_receive(&data_callid, &data_len)) { 823 async_answer_0(iid, EINVAL); 824 fun_del_ref(fun); 825 return; 826 } 827 828 void *buffer = malloc(data_len); 829 if (buffer == NULL) { 830 async_answer_0(data_callid, ENOMEM); 831 async_answer_0(iid, ENOMEM); 832 fun_del_ref(fun); 833 return; 834 } 835 836 fibril_rwlock_read_lock(&device_tree.rwlock); 837 838 /* Check function state */ 839 if (fun->state == FUN_REMOVED) { 840 fibril_rwlock_read_unlock(&device_tree.rwlock); 841 free(buffer); 842 843 async_answer_0(data_callid, ENOENT); 844 async_answer_0(iid, ENOENT); 845 fun_del_ref(fun); 846 return; 847 } 848 532 return; 533 } 534 849 535 size_t sent_length = str_size(fun->pathname); 850 536 if (sent_length > data_len) { … … 855 541 async_answer_0(iid, EOK); 856 542 857 fibril_rwlock_read_unlock(&device_tree.rwlock);858 fun_del_ref(fun);859 543 free(buffer); 860 544 } 861 545 862 static void devman_dev_get_functions(ipc_callid_t iid, ipc_call_t *icall)863 {864 ipc_callid_t callid;865 size_t size;866 size_t act_size;867 int rc;868 869 if (!async_data_read_receive(&callid, &size)) {870 async_answer_0(callid, EREFUSED);871 async_answer_0(iid, EREFUSED);872 return;873 }874 875 fibril_rwlock_read_lock(&device_tree.rwlock);876 877 dev_node_t *dev = find_dev_node_no_lock(&device_tree,878 IPC_GET_ARG1(*icall));879 if (dev == NULL || dev->state == DEVICE_REMOVED) {880 fibril_rwlock_read_unlock(&device_tree.rwlock);881 async_answer_0(callid, ENOENT);882 async_answer_0(iid, ENOENT);883 return;884 }885 886 devman_handle_t *hdl_buf = (devman_handle_t *) malloc(size);887 if (hdl_buf == NULL) {888 fibril_rwlock_read_unlock(&device_tree.rwlock);889 async_answer_0(callid, ENOMEM);890 async_answer_0(iid, ENOMEM);891 return;892 }893 894 rc = dev_get_functions(&device_tree, dev, hdl_buf, size, &act_size);895 if (rc != EOK) {896 fibril_rwlock_read_unlock(&device_tree.rwlock);897 async_answer_0(callid, rc);898 async_answer_0(iid, rc);899 return;900 }901 902 fibril_rwlock_read_unlock(&device_tree.rwlock);903 904 sysarg_t retval = async_data_read_finalize(callid, hdl_buf, size);905 free(hdl_buf);906 907 async_answer_1(iid, retval, act_size);908 }909 910 911 /** Get handle for child device of a function. */912 static void devman_fun_get_child(ipc_callid_t iid, ipc_call_t *icall)913 {914 fun_node_t *fun;915 916 fibril_rwlock_read_lock(&device_tree.rwlock);917 918 fun = find_fun_node_no_lock(&device_tree, IPC_GET_ARG1(*icall));919 if (fun == NULL || fun->state == FUN_REMOVED) {920 fibril_rwlock_read_unlock(&device_tree.rwlock);921 async_answer_0(iid, ENOENT);922 return;923 }924 925 if (fun->child == NULL) {926 fibril_rwlock_read_unlock(&device_tree.rwlock);927 async_answer_0(iid, ENOENT);928 return;929 }930 931 async_answer_1(iid, EOK, fun->child->handle);932 933 fibril_rwlock_read_unlock(&device_tree.rwlock);934 }935 936 /** Online function.937 *938 * Send a request to online a function to the responsible driver.939 * The driver may offline other functions if necessary (i.e. if the state940 * of this function is linked to state of another function somehow).941 */942 static void devman_fun_online(ipc_callid_t iid, ipc_call_t *icall)943 {944 fun_node_t *fun;945 int rc;946 947 fun = find_fun_node(&device_tree, IPC_GET_ARG1(*icall));948 if (fun == NULL) {949 async_answer_0(iid, ENOENT);950 return;951 }952 953 rc = driver_fun_online(&device_tree, fun);954 fun_del_ref(fun);955 956 async_answer_0(iid, (sysarg_t) rc);957 }958 959 /** Offline function.960 *961 * Send a request to offline a function to the responsible driver. As962 * a result the subtree rooted at that function should be cleanly963 * detatched. The driver may offline other functions if necessary964 * (i.e. if the state of this function is linked to state of another965 * function somehow).966 */967 static void devman_fun_offline(ipc_callid_t iid, ipc_call_t *icall)968 {969 fun_node_t *fun;970 int rc;971 972 fun = find_fun_node(&device_tree, IPC_GET_ARG1(*icall));973 if (fun == NULL) {974 async_answer_0(iid, ENOENT);975 return;976 }977 978 rc = driver_fun_offline(&device_tree, fun);979 fun_del_ref(fun);980 981 async_answer_0(iid, (sysarg_t) rc);982 }983 984 /** Find handle for the function instance identified by its service ID. */985 static void devman_fun_sid_to_handle(ipc_callid_t iid, ipc_call_t *icall)986 {987 fun_node_t *fun;988 989 fun = find_loc_tree_function(&device_tree, IPC_GET_ARG1(*icall));990 991 if (fun == NULL) {992 async_answer_0(iid, ENOENT);993 return;994 }995 996 fibril_rwlock_read_lock(&device_tree.rwlock);997 998 /* Check function state */999 if (fun->state == FUN_REMOVED) {1000 fibril_rwlock_read_unlock(&device_tree.rwlock);1001 async_answer_0(iid, ENOENT);1002 return;1003 }1004 1005 async_answer_1(iid, EOK, fun->handle);1006 fibril_rwlock_read_unlock(&device_tree.rwlock);1007 fun_del_ref(fun);1008 }1009 546 1010 547 /** Function for handling connections from a client to the device manager. */ … … 1025 562 devman_function_get_handle(callid, &call); 1026 563 break; 1027 case DEVMAN_DEV _GET_FUNCTIONS:1028 devman_ dev_get_functions(callid, &call);564 case DEVMAN_DEVICE_GET_HANDLE_BY_CLASS: 565 devman_function_get_handle_by_class(callid, &call); 1029 566 break; 1030 case DEVMAN_FUN_GET_CHILD: 1031 devman_fun_get_child(callid, &call); 1032 break; 1033 case DEVMAN_FUN_GET_NAME: 1034 devman_fun_get_name(callid, &call); 1035 break; 1036 case DEVMAN_FUN_GET_PATH: 1037 devman_fun_get_path(callid, &call); 1038 break; 1039 case DEVMAN_FUN_ONLINE: 1040 devman_fun_online(callid, &call); 1041 break; 1042 case DEVMAN_FUN_OFFLINE: 1043 devman_fun_offline(callid, &call); 1044 break; 1045 case DEVMAN_FUN_SID_TO_HANDLE: 1046 devman_fun_sid_to_handle(callid, &call); 567 case DEVMAN_DEVICE_GET_DEVICE_PATH: 568 devman_get_device_path_by_handle(callid, &call); 1047 569 break; 1048 570 default: … … 1063 585 if (fun == NULL) 1064 586 dev = find_dev_node(&device_tree, handle); 1065 else { 1066 fibril_rwlock_read_lock(&device_tree.rwlock); 587 else 1067 588 dev = fun->dev; 1068 if (dev != NULL)1069 dev_add_ref(dev);1070 fibril_rwlock_read_unlock(&device_tree.rwlock);1071 }1072 589 1073 590 /* … … 1081 598 "function with handle %" PRIun " was found.", handle); 1082 599 async_answer_0(iid, ENOENT); 1083 goto cleanup;600 return; 1084 601 } 1085 602 … … 1089 606 handle); 1090 607 async_answer_0(iid, ENOENT); 1091 goto cleanup;608 return; 1092 609 } 1093 610 1094 611 driver_t *driver = NULL; 1095 1096 fibril_rwlock_read_lock(&device_tree.rwlock);1097 612 1098 613 if (drv_to_parent) { … … 1109 624 } 1110 625 1111 fibril_rwlock_read_unlock(&device_tree.rwlock);1112 1113 626 if (driver == NULL) { 1114 627 log_msg(LVL_ERROR, "IPC forwarding refused - " \ 1115 628 "the device %" PRIun " is not in usable state.", handle); 1116 629 async_answer_0(iid, ENOENT); 1117 goto cleanup;630 return; 1118 631 } 1119 632 … … 1128 641 "Could not forward to driver `%s'.", driver->name); 1129 642 async_answer_0(iid, EINVAL); 1130 goto cleanup;643 return; 1131 644 } 1132 645 … … 1144 657 async_forward_fast(iid, exch, method, fwd_h, 0, IPC_FF_NONE); 1145 658 async_exchange_end(exch); 1146 1147 cleanup: 1148 if (dev != NULL) 1149 dev_del_ref(dev); 1150 if (fun != NULL) 1151 fun_del_ref(fun); 1152 } 1153 1154 /** Function for handling connections from a client forwarded by the location 1155 * service to the device manager. */ 1156 static void devman_connection_loc(ipc_callid_t iid, ipc_call_t *icall) 1157 { 1158 service_id_t service_id = IPC_GET_ARG2(*icall); 659 } 660 661 /** Function for handling connections from a client forwarded by the device 662 * mapper to the device manager. */ 663 static void devman_connection_devmapper(ipc_callid_t iid, ipc_call_t *icall) 664 { 665 devmap_handle_t devmap_handle = IPC_GET_ARG2(*icall); 1159 666 fun_node_t *fun; 1160 667 dev_node_t *dev; 1161 devman_handle_t handle; 1162 driver_t *driver; 1163 1164 fun = find_loc_tree_function(&device_tree, service_id); 1165 1166 fibril_rwlock_read_lock(&device_tree.rwlock); 1167 1168 if (fun == NULL || fun->dev == NULL || fun->dev->drv == NULL) { 1169 log_msg(LVL_WARN, "devman_connection_loc(): function " 1170 "not found.\n"); 1171 fibril_rwlock_read_unlock(&device_tree.rwlock); 668 669 fun = find_devmap_tree_function(&device_tree, devmap_handle); 670 if (fun == NULL) 671 fun = find_devmap_class_function(&class_list, devmap_handle); 672 673 if (fun == NULL || fun->dev->drv == NULL) { 1172 674 async_answer_0(iid, ENOENT); 1173 675 return; … … 1175 677 1176 678 dev = fun->dev; 1177 driver = dev->drv; 1178 handle = fun->handle; 1179 1180 fibril_rwlock_read_unlock(&device_tree.rwlock); 1181 1182 async_exch_t *exch = async_exchange_begin(driver->sess); 1183 async_forward_fast(iid, exch, DRIVER_CLIENT, handle, 0, 679 680 if ((dev->state != DEVICE_USABLE) || (!dev->drv->sess)) { 681 async_answer_0(iid, EINVAL); 682 return; 683 } 684 685 async_exch_t *exch = async_exchange_begin(dev->drv->sess); 686 async_forward_fast(iid, exch, DRIVER_CLIENT, fun->handle, 0, 1184 687 IPC_FF_NONE); 1185 688 async_exchange_end(exch); 1186 689 1187 690 log_msg(LVL_DEBUG, 1188 "Forwarding loc service request for `%s' function to driver `%s'.", 1189 fun->pathname, driver->name); 1190 1191 fun_del_ref(fun); 691 "Forwarding devmapper request for `%s' function to driver `%s'.", 692 fun->pathname, dev->drv->name); 1192 693 } 1193 694 1194 695 /** Function for handling connections to device manager. */ 1195 static void devman_connection(ipc_callid_t iid, ipc_call_t *icall , void *arg)1196 { 1197 /* Select port. */696 static void devman_connection(ipc_callid_t iid, ipc_call_t *icall) 697 { 698 /* Select interface. */ 1198 699 switch ((sysarg_t) (IPC_GET_ARG1(*icall))) { 1199 700 case DEVMAN_DRIVER: … … 1207 708 devman_forward(iid, icall, false); 1208 709 break; 1209 case DEVMAN_CONNECT_FROM_ LOC:1210 /* Someone connected through locnode. */1211 devman_connection_ loc(iid, icall);710 case DEVMAN_CONNECT_FROM_DEVMAP: 711 /* Someone connected through devmap node. */ 712 devman_connection_devmapper(iid, icall); 1212 713 break; 1213 714 case DEVMAN_CONNECT_TO_PARENTS_DEVICE: … … 1221 722 } 1222 723 1223 static void *devman_client_data_create(void)1224 {1225 client_t *client;1226 1227 client = calloc(1, sizeof(client_t));1228 if (client == NULL)1229 return NULL;1230 1231 fibril_mutex_initialize(&client->mutex);1232 return client;1233 }1234 1235 static void devman_client_data_destroy(void *data)1236 {1237 free(data);1238 }1239 1240 724 /** Initialize device manager internal structures. */ 1241 725 static bool devman_init(void) … … 1259 743 } 1260 744 745 init_class_list(&class_list); 746 1261 747 /* 1262 * !!! devman_connection ... as the device manager is not a real loc748 * !!! devman_connection ... as the device manager is not a real devmap 1263 749 * driver (it uses a completely different ipc protocol than an ordinary 1264 * locdriver) forwarding a connection from client to the devman by1265 * location servicewould not work.750 * devmap driver) forwarding a connection from client to the devman by 751 * devmapper would not work. 1266 752 */ 1267 loc_server_register(NAME, devman_connection);753 devmap_driver_register(NAME, devman_connection); 1268 754 1269 755 return true; … … 1274 760 printf(NAME ": HelenOS Device Manager\n"); 1275 761 1276 if (log_init(NAME, LVL_ WARN) != EOK) {762 if (log_init(NAME, LVL_ERROR) != EOK) { 1277 763 printf(NAME ": Error initializing logging subsystem.\n"); 1278 764 return -1; … … 1284 770 } 1285 771 1286 /* Set handlers for incoming connections. */ 1287 async_set_client_data_constructor(devman_client_data_create); 1288 async_set_client_data_destructor(devman_client_data_destroy); 772 /* Set a handler of incomming connections. */ 1289 773 async_set_client_connection(devman_connection); 1290 774
Note:
See TracChangeset
for help on using the changeset viewer.