Changeset a1b7e80 in mainline for uspace/srv/devman/devman.c
- Timestamp:
- 2011-09-02T16:54:18Z (13 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- f480d7e
- Parents:
- 7a72ce1a (diff), 224c0e7 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/srv/devman/devman.c
r7a72ce1a ra1b7e80 30 30 * @{ 31 31 */ 32 /** @file Device Manager 33 * 34 * Locking order: 35 * (1) driver_t.driver_mutex 36 * (2) dev_tree_t.rwlock 37 * 38 * Synchronization: 39 * - device_tree.rwlock protects: 40 * - tree root, complete tree topology 41 * - complete contents of device and function nodes 42 * - dev_node_t.refcnt, fun_node_t.refcnt prevent nodes from 43 * being deallocated 44 * - find_xxx() functions increase reference count of returned object 45 * - find_xxx_no_lock() do not increase reference count 46 * 47 * TODO 48 * - Track all steady and transient device/function states 49 * - Check states, wait for steady state on certain operations 50 */ 32 51 33 52 #include <errno.h> … … 43 62 #include "devman.h" 44 63 45 fun_node_t *find_node_child(fun_node_t *parent, const char *name);64 static fun_node_t *find_node_child(dev_tree_t *, fun_node_t *, const char *); 46 65 47 66 /* hash table operations */ … … 406 425 } 407 426 427 fun_add_ref(fun); 408 428 insert_fun_node(tree, fun, str_dup(""), NULL); 429 409 430 match_id_t *id = create_match_id(); 410 431 id->id = str_dup("root"); … … 422 443 } 423 444 445 dev_add_ref(dev); 424 446 insert_dev_node(tree, dev, fun); 425 447 … … 467 489 /** Assign a driver to a device. 468 490 * 491 * @param tree Device tree 469 492 * @param node The device's node in the device tree. 470 493 * @param drv The driver. 471 494 */ 472 void attach_driver(dev_ node_t *dev, driver_t *drv)495 void attach_driver(dev_tree_t *tree, dev_node_t *dev, driver_t *drv) 473 496 { 474 497 log_msg(LVL_DEBUG, "attach_driver(dev=\"%s\",drv=\"%s\")", … … 476 499 477 500 fibril_mutex_lock(&drv->driver_mutex); 501 fibril_rwlock_write_lock(&tree->rwlock); 478 502 479 503 dev->drv = drv; 480 504 list_append(&dev->driver_devices, &drv->devices); 481 505 506 fibril_rwlock_write_unlock(&tree->rwlock); 507 fibril_mutex_unlock(&drv->driver_mutex); 508 } 509 510 /** Detach driver from device. 511 * 512 * @param tree Device tree 513 * @param node The device's node in the device tree. 514 * @param drv The driver. 515 */ 516 void detach_driver(dev_tree_t *tree, dev_node_t *dev) 517 { 518 driver_t *drv = dev->drv; 519 520 assert(drv != NULL); 521 522 log_msg(LVL_DEBUG, "detach_driver(dev=\"%s\",drv=\"%s\")", 523 dev->pfun->pathname, drv->name); 524 525 fibril_mutex_lock(&drv->driver_mutex); 526 fibril_rwlock_write_lock(&tree->rwlock); 527 528 dev->drv = NULL; 529 list_remove(&dev->driver_devices); 530 531 fibril_rwlock_write_unlock(&tree->rwlock); 482 532 fibril_mutex_unlock(&drv->driver_mutex); 483 533 } … … 556 606 while (link != &driver->devices.head) { 557 607 dev = list_get_instance(link, dev_node_t, driver_devices); 608 fibril_rwlock_write_lock(&tree->rwlock); 609 558 610 if (dev->passed_to_driver) { 611 fibril_rwlock_write_unlock(&tree->rwlock); 559 612 link = link->next; 560 613 continue; 561 614 } 562 615 563 /* 564 * We remove the device from the list to allow safe adding 565 * of new devices (no one will touch our item this way). 566 */ 567 list_remove(link); 616 log_msg(LVL_DEBUG, "pass_devices_to_driver: dev->refcnt=%d\n", 617 (int)atomic_get(&dev->refcnt)); 618 dev_add_ref(dev); 568 619 569 620 /* … … 572 623 */ 573 624 fibril_mutex_unlock(&driver->driver_mutex); 625 fibril_rwlock_write_unlock(&tree->rwlock); 574 626 575 627 add_device(driver, dev, tree); 628 629 dev_del_ref(dev); 576 630 577 631 /* … … 580 634 */ 581 635 fibril_mutex_lock(&driver->driver_mutex); 582 583 /*584 * Insert the device back.585 * The order is not relevant here so no harm is done586 * (actually, the order would be preserved in most cases).587 */588 list_append(link, &driver->devices);589 636 590 637 /* … … 679 726 char *loc_name = NULL; 680 727 728 assert(fibril_rwlock_is_locked(&tree->rwlock)); 729 681 730 asprintf(&loc_name, "%s", fun->pathname); 682 731 if (loc_name == NULL) … … 726 775 727 776 ipc_call_t answer; 728 aid_t req = async_send_2(exch, DRIVER_ ADD_DEVICE, dev->handle,777 aid_t req = async_send_2(exch, DRIVER_DEV_ADD, dev->handle, 729 778 parent_handle, &answer); 730 779 … … 783 832 784 833 /* Attach the driver to the device. */ 785 attach_driver( dev, drv);834 attach_driver(tree, dev, drv); 786 835 787 836 fibril_mutex_lock(&drv->driver_mutex); … … 797 846 add_device(drv, dev, tree); 798 847 848 fibril_mutex_lock(&drv->driver_mutex); 849 fibril_mutex_unlock(&drv->driver_mutex); 850 851 fibril_rwlock_write_lock(&tree->rwlock); 852 if (dev->pfun != NULL) { 853 dev->pfun->state = FUN_ON_LINE; 854 } 855 fibril_rwlock_write_unlock(&tree->rwlock); 799 856 return true; 857 } 858 859 int driver_dev_remove(dev_tree_t *tree, dev_node_t *dev) 860 { 861 async_exch_t *exch; 862 sysarg_t retval; 863 driver_t *drv; 864 devman_handle_t handle; 865 866 assert(dev != NULL); 867 868 log_msg(LVL_DEBUG, "driver_dev_remove(%p)", dev); 869 870 fibril_rwlock_read_lock(&tree->rwlock); 871 drv = dev->drv; 872 handle = dev->handle; 873 fibril_rwlock_read_unlock(&tree->rwlock); 874 875 exch = async_exchange_begin(drv->sess); 876 retval = async_req_1_0(exch, DRIVER_DEV_REMOVE, handle); 877 async_exchange_end(exch); 878 879 return retval; 880 881 } 882 883 int driver_fun_online(dev_tree_t *tree, fun_node_t *fun) 884 { 885 async_exch_t *exch; 886 sysarg_t retval; 887 driver_t *drv; 888 devman_handle_t handle; 889 890 log_msg(LVL_DEBUG, "driver_fun_online(%p)", fun); 891 892 fibril_rwlock_read_lock(&tree->rwlock); 893 894 if (fun->dev == NULL) { 895 /* XXX root function? */ 896 fibril_rwlock_read_unlock(&tree->rwlock); 897 return EINVAL; 898 } 899 900 drv = fun->dev->drv; 901 handle = fun->handle; 902 fibril_rwlock_read_unlock(&tree->rwlock); 903 904 exch = async_exchange_begin(drv->sess); 905 retval = async_req_1_0(exch, DRIVER_FUN_ONLINE, handle); 906 loc_exchange_end(exch); 907 908 return retval; 909 } 910 911 int driver_fun_offline(dev_tree_t *tree, fun_node_t *fun) 912 { 913 async_exch_t *exch; 914 sysarg_t retval; 915 driver_t *drv; 916 devman_handle_t handle; 917 918 log_msg(LVL_DEBUG, "driver_fun_offline(%p)", fun); 919 920 fibril_rwlock_read_lock(&tree->rwlock); 921 if (fun->dev == NULL) { 922 /* XXX root function? */ 923 fibril_rwlock_read_unlock(&tree->rwlock); 924 return EINVAL; 925 } 926 927 drv = fun->dev->drv; 928 handle = fun->handle; 929 fibril_rwlock_read_unlock(&tree->rwlock); 930 931 exch = async_exchange_begin(drv->sess); 932 retval = async_req_1_0(exch, DRIVER_FUN_OFFLINE, handle); 933 loc_exchange_end(exch); 934 935 return retval; 936 800 937 } 801 938 … … 826 963 if (!create_root_nodes(tree)) 827 964 return false; 828 965 829 966 /* Find suitable driver and start it. */ 830 return assign_driver(tree->root_node->child, drivers_list, tree); 967 dev_node_t *rdev = tree->root_node->child; 968 dev_add_ref(rdev); 969 int rc = assign_driver(rdev, drivers_list, tree); 970 dev_del_ref(rdev); 971 972 return rc; 831 973 } 832 974 … … 839 981 dev_node_t *create_dev_node(void) 840 982 { 841 dev_node_t *res = malloc(sizeof(dev_node_t)); 842 843 if (res != NULL) { 844 memset(res, 0, sizeof(dev_node_t)); 845 list_initialize(&res->functions); 846 link_initialize(&res->driver_devices); 847 link_initialize(&res->devman_dev); 848 } 849 850 return res; 983 dev_node_t *dev; 984 985 dev = calloc(1, sizeof(dev_node_t)); 986 if (dev == NULL) 987 return NULL; 988 989 atomic_set(&dev->refcnt, 0); 990 list_initialize(&dev->functions); 991 link_initialize(&dev->driver_devices); 992 link_initialize(&dev->devman_dev); 993 994 return dev; 851 995 } 852 996 … … 864 1008 } 865 1009 1010 /** Increase device node reference count. 1011 * 1012 * @param dev Device node 1013 */ 1014 void dev_add_ref(dev_node_t *dev) 1015 { 1016 atomic_inc(&dev->refcnt); 1017 } 1018 1019 /** Decrease device node reference count. 1020 * 1021 * When the count drops to zero the device node is freed. 1022 * 1023 * @param dev Device node 1024 */ 1025 void dev_del_ref(dev_node_t *dev) 1026 { 1027 if (atomic_predec(&dev->refcnt) == 0) 1028 delete_dev_node(dev); 1029 } 1030 1031 866 1032 /** Find the device node structure of the device witch has the specified handle. 867 1033 * … … 893 1059 fibril_rwlock_read_lock(&tree->rwlock); 894 1060 dev = find_dev_node_no_lock(tree, handle); 1061 if (dev != NULL) 1062 dev_add_ref(dev); 1063 895 1064 fibril_rwlock_read_unlock(&tree->rwlock); 896 1065 … … 920 1089 list_get_instance(item, fun_node_t, dev_functions); 921 1090 922 if (pos < buf_cnt) 1091 if (pos < buf_cnt) { 923 1092 hdl_buf[pos] = fun->handle; 1093 } 1094 924 1095 pos++; 925 1096 } … … 937 1108 fun_node_t *create_fun_node(void) 938 1109 { 939 fun_node_t *res = malloc(sizeof(fun_node_t)); 940 941 if (res != NULL) { 942 memset(res, 0, sizeof(fun_node_t)); 943 link_initialize(&res->dev_functions); 944 list_initialize(&res->match_ids.ids); 945 link_initialize(&res->devman_fun); 946 link_initialize(&res->loc_fun); 947 } 948 949 return res; 1110 fun_node_t *fun; 1111 1112 fun = calloc(1, sizeof(fun_node_t)); 1113 if (fun == NULL) 1114 return NULL; 1115 1116 fun->state = FUN_INIT; 1117 atomic_set(&fun->refcnt, 0); 1118 link_initialize(&fun->dev_functions); 1119 list_initialize(&fun->match_ids.ids); 1120 link_initialize(&fun->devman_fun); 1121 link_initialize(&fun->loc_fun); 1122 1123 return fun; 950 1124 } 951 1125 … … 965 1139 } 966 1140 1141 /** Increase function node reference count. 1142 * 1143 * @param fun Function node 1144 */ 1145 void fun_add_ref(fun_node_t *fun) 1146 { 1147 atomic_inc(&fun->refcnt); 1148 } 1149 1150 /** Decrease function node reference count. 1151 * 1152 * When the count drops to zero the function node is freed. 1153 * 1154 * @param fun Function node 1155 */ 1156 void fun_del_ref(fun_node_t *fun) 1157 { 1158 if (atomic_predec(&fun->refcnt) == 0) 1159 delete_fun_node(fun); 1160 } 1161 967 1162 /** Find the function node with the specified handle. 968 1163 * … … 975 1170 unsigned long key = handle; 976 1171 link_t *link; 1172 fun_node_t *fun; 977 1173 978 1174 assert(fibril_rwlock_is_locked(&tree->rwlock)); … … 982 1178 return NULL; 983 1179 984 return hash_table_get_instance(link, fun_node_t, devman_fun); 1180 fun = hash_table_get_instance(link, fun_node_t, devman_fun); 1181 1182 return fun; 985 1183 } 986 1184 … … 996 1194 997 1195 fibril_rwlock_read_lock(&tree->rwlock); 1196 998 1197 fun = find_fun_node_no_lock(tree, handle); 1198 if (fun != NULL) 1199 fun_add_ref(fun); 1200 999 1201 fibril_rwlock_read_unlock(&tree->rwlock); 1000 1202 … … 1004 1206 /** Create and set device's full path in device tree. 1005 1207 * 1208 * @param tree Device tree 1006 1209 * @param node The device's device node. 1007 1210 * @param parent The parent device node. … … 1009 1212 * resources etc.). 1010 1213 */ 1011 static bool set_fun_path(fun_node_t *fun, fun_node_t *parent) 1012 { 1214 static bool set_fun_path(dev_tree_t *tree, fun_node_t *fun, fun_node_t *parent) 1215 { 1216 assert(fibril_rwlock_is_write_locked(&tree->rwlock)); 1013 1217 assert(fun->name != NULL); 1014 1218 … … 1037 1241 * 1038 1242 * @param tree The device tree. 1039 * @param node The newly added device node. 1040 * @param dev_name The name of the newly added device. 1041 * @param parent The parent device node. 1243 * @param dev The newly added device node. 1244 * @param pfun The parent function node. 1042 1245 * 1043 1246 * @return True on success, false otherwise (insufficient resources … … 1046 1249 bool insert_dev_node(dev_tree_t *tree, dev_node_t *dev, fun_node_t *pfun) 1047 1250 { 1048 assert(dev != NULL);1049 assert(tree != NULL);1050 1251 assert(fibril_rwlock_is_write_locked(&tree->rwlock)); 1051 1252 … … 1065 1266 } 1066 1267 1268 /** Remove device from device tree. 1269 * 1270 * @param tree Device tree 1271 * @param dev Device node 1272 */ 1273 void remove_dev_node(dev_tree_t *tree, dev_node_t *dev) 1274 { 1275 assert(fibril_rwlock_is_write_locked(&tree->rwlock)); 1276 1277 log_msg(LVL_DEBUG, "remove_dev_node(dev=%p)", dev); 1278 1279 /* Remove node from the handle-to-node map. */ 1280 unsigned long key = dev->handle; 1281 hash_table_remove(&tree->devman_devices, &key, 1); 1282 1283 /* Unlink from parent function. */ 1284 dev->pfun->child = NULL; 1285 dev->pfun = NULL; 1286 1287 dev->state = DEVICE_REMOVED; 1288 } 1289 1290 1067 1291 /** Insert new function into device tree. 1068 1292 * 1069 1293 * @param tree The device tree. 1070 * @param nodeThe newly added function node.1071 * @param dev_name The name of the newly added function.1072 * @param parentOwning device node.1294 * @param fun The newly added function node. 1295 * @param fun_name The name of the newly added function. 1296 * @param dev Owning device node. 1073 1297 * 1074 1298 * @return True on success, false otherwise (insufficient resources … … 1080 1304 fun_node_t *pfun; 1081 1305 1082 assert(fun != NULL);1083 assert(tree != NULL);1084 1306 assert(fun_name != NULL); 1085 1307 assert(fibril_rwlock_is_write_locked(&tree->rwlock)); … … 1092 1314 1093 1315 fun->name = fun_name; 1094 if (!set_fun_path( fun, pfun)) {1316 if (!set_fun_path(tree, fun, pfun)) { 1095 1317 return false; 1096 1318 } … … 1116 1338 void remove_fun_node(dev_tree_t *tree, fun_node_t *fun) 1117 1339 { 1118 assert(tree != NULL);1119 assert(fun != NULL);1120 1340 assert(fibril_rwlock_is_write_locked(&tree->rwlock)); 1121 1341 … … 1127 1347 if (fun->dev != NULL) 1128 1348 list_remove(&fun->dev_functions); 1349 1350 fun->dev = NULL; 1351 fun->state = FUN_REMOVED; 1129 1352 } 1130 1353 … … 1148 1371 1149 1372 fun_node_t *fun = tree->root_node; 1373 fun_add_ref(fun); 1150 1374 /* 1151 1375 * Relative path to the function from its parent (but with '/' at the … … 1165 1389 } 1166 1390 1167 fun = find_node_child(fun, rel_path + 1); 1391 fun_node_t *cfun = find_node_child(tree, fun, rel_path + 1); 1392 fun_del_ref(fun); 1393 fun = cfun; 1168 1394 1169 1395 if (cont) { … … 1183 1409 * Device tree rwlock should be held at least for reading. 1184 1410 * 1411 * @param tree Device tree 1185 1412 * @param dev Device the function belongs to. 1186 1413 * @param name Function name (not path). … … 1188 1415 * @retval NULL No function with given name. 1189 1416 */ 1190 fun_node_t *find_fun_node_in_device(dev_ node_t *dev, const char *name)1191 { 1192 assert(dev != NULL); 1417 fun_node_t *find_fun_node_in_device(dev_tree_t *tree, dev_node_t *dev, 1418 const char *name) 1419 { 1193 1420 assert(name != NULL); 1421 assert(fibril_rwlock_is_locked(&tree->rwlock)); 1194 1422 1195 1423 fun_node_t *fun; … … 1198 1426 fun = list_get_instance(link, fun_node_t, dev_functions); 1199 1427 1200 if (str_cmp(name, fun->name) == 0) 1428 if (str_cmp(name, fun->name) == 0) { 1429 fun_add_ref(fun); 1201 1430 return fun; 1431 } 1202 1432 } 1203 1433 … … 1209 1439 * Device tree rwlock should be held at least for reading. 1210 1440 * 1441 * @param tree Device tree 1211 1442 * @param parent The parent function node. 1212 1443 * @param name The name of the child function. 1213 1444 * @return The child function node. 1214 1445 */ 1215 fun_node_t *find_node_child(fun_node_t *pfun, const char *name) 1216 { 1217 return find_fun_node_in_device(pfun->child, name); 1446 static fun_node_t *find_node_child(dev_tree_t *tree, fun_node_t *pfun, 1447 const char *name) 1448 { 1449 return find_fun_node_in_device(tree, pfun->child, name); 1218 1450 } 1219 1451 … … 1228 1460 fibril_rwlock_read_lock(&tree->rwlock); 1229 1461 link = hash_table_find(&tree->loc_functions, &key); 1230 if (link != NULL) 1462 if (link != NULL) { 1231 1463 fun = hash_table_get_instance(link, fun_node_t, loc_fun); 1464 fun_add_ref(fun); 1465 } 1232 1466 fibril_rwlock_read_unlock(&tree->rwlock); 1233 1467 … … 1237 1471 void tree_add_loc_function(dev_tree_t *tree, fun_node_t *fun) 1238 1472 { 1473 assert(fibril_rwlock_is_write_locked(&tree->rwlock)); 1474 1239 1475 unsigned long key = (unsigned long) fun->service_id; 1240 fibril_rwlock_write_lock(&tree->rwlock);1241 1476 hash_table_insert(&tree->loc_functions, &key, &fun->loc_fun); 1242 fibril_rwlock_write_unlock(&tree->rwlock);1243 1477 } 1244 1478
Note:
See TracChangeset
for help on using the changeset viewer.