Changeset c05642d in mainline for uspace/srv/devman/devman.c
- Timestamp:
- 2011-09-07T00:03:26Z (13 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 5081276
- Parents:
- bb74dabe (diff), 038b289 (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
rbb74dabe rc05642d 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 int driver_dev_gone(dev_tree_t *tree, dev_node_t *dev) 883 { 884 async_exch_t *exch; 885 sysarg_t retval; 886 driver_t *drv; 887 devman_handle_t handle; 888 889 assert(dev != NULL); 890 891 log_msg(LVL_DEBUG, "driver_dev_gone(%p)", dev); 892 893 fibril_rwlock_read_lock(&tree->rwlock); 894 drv = dev->drv; 895 handle = dev->handle; 896 fibril_rwlock_read_unlock(&tree->rwlock); 897 898 exch = async_exchange_begin(drv->sess); 899 retval = async_req_1_0(exch, DRIVER_DEV_GONE, handle); 900 async_exchange_end(exch); 901 902 return retval; 903 } 904 905 int driver_fun_online(dev_tree_t *tree, fun_node_t *fun) 906 { 907 async_exch_t *exch; 908 sysarg_t retval; 909 driver_t *drv; 910 devman_handle_t handle; 911 912 log_msg(LVL_DEBUG, "driver_fun_online(%p)", fun); 913 914 fibril_rwlock_read_lock(&tree->rwlock); 915 916 if (fun->dev == NULL) { 917 /* XXX root function? */ 918 fibril_rwlock_read_unlock(&tree->rwlock); 919 return EINVAL; 920 } 921 922 drv = fun->dev->drv; 923 handle = fun->handle; 924 fibril_rwlock_read_unlock(&tree->rwlock); 925 926 exch = async_exchange_begin(drv->sess); 927 retval = async_req_1_0(exch, DRIVER_FUN_ONLINE, handle); 928 loc_exchange_end(exch); 929 930 return retval; 931 } 932 933 int driver_fun_offline(dev_tree_t *tree, fun_node_t *fun) 934 { 935 async_exch_t *exch; 936 sysarg_t retval; 937 driver_t *drv; 938 devman_handle_t handle; 939 940 log_msg(LVL_DEBUG, "driver_fun_offline(%p)", fun); 941 942 fibril_rwlock_read_lock(&tree->rwlock); 943 if (fun->dev == NULL) { 944 /* XXX root function? */ 945 fibril_rwlock_read_unlock(&tree->rwlock); 946 return EINVAL; 947 } 948 949 drv = fun->dev->drv; 950 handle = fun->handle; 951 fibril_rwlock_read_unlock(&tree->rwlock); 952 953 exch = async_exchange_begin(drv->sess); 954 retval = async_req_1_0(exch, DRIVER_FUN_OFFLINE, handle); 955 loc_exchange_end(exch); 956 957 return retval; 958 800 959 } 801 960 … … 826 985 if (!create_root_nodes(tree)) 827 986 return false; 828 987 829 988 /* Find suitable driver and start it. */ 830 return assign_driver(tree->root_node->child, drivers_list, tree); 989 dev_node_t *rdev = tree->root_node->child; 990 dev_add_ref(rdev); 991 int rc = assign_driver(rdev, drivers_list, tree); 992 dev_del_ref(rdev); 993 994 return rc; 831 995 } 832 996 … … 839 1003 dev_node_t *create_dev_node(void) 840 1004 { 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; 1005 dev_node_t *dev; 1006 1007 dev = calloc(1, sizeof(dev_node_t)); 1008 if (dev == NULL) 1009 return NULL; 1010 1011 atomic_set(&dev->refcnt, 0); 1012 list_initialize(&dev->functions); 1013 link_initialize(&dev->driver_devices); 1014 link_initialize(&dev->devman_dev); 1015 1016 return dev; 851 1017 } 852 1018 … … 864 1030 } 865 1031 1032 /** Increase device node reference count. 1033 * 1034 * @param dev Device node 1035 */ 1036 void dev_add_ref(dev_node_t *dev) 1037 { 1038 atomic_inc(&dev->refcnt); 1039 } 1040 1041 /** Decrease device node reference count. 1042 * 1043 * When the count drops to zero the device node is freed. 1044 * 1045 * @param dev Device node 1046 */ 1047 void dev_del_ref(dev_node_t *dev) 1048 { 1049 if (atomic_predec(&dev->refcnt) == 0) 1050 delete_dev_node(dev); 1051 } 1052 1053 866 1054 /** Find the device node structure of the device witch has the specified handle. 867 1055 * … … 893 1081 fibril_rwlock_read_lock(&tree->rwlock); 894 1082 dev = find_dev_node_no_lock(tree, handle); 1083 if (dev != NULL) 1084 dev_add_ref(dev); 1085 895 1086 fibril_rwlock_read_unlock(&tree->rwlock); 896 1087 … … 920 1111 list_get_instance(item, fun_node_t, dev_functions); 921 1112 922 if (pos < buf_cnt) 1113 if (pos < buf_cnt) { 923 1114 hdl_buf[pos] = fun->handle; 1115 } 1116 924 1117 pos++; 925 1118 } … … 937 1130 fun_node_t *create_fun_node(void) 938 1131 { 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; 1132 fun_node_t *fun; 1133 1134 fun = calloc(1, sizeof(fun_node_t)); 1135 if (fun == NULL) 1136 return NULL; 1137 1138 fun->state = FUN_INIT; 1139 atomic_set(&fun->refcnt, 0); 1140 link_initialize(&fun->dev_functions); 1141 list_initialize(&fun->match_ids.ids); 1142 link_initialize(&fun->devman_fun); 1143 link_initialize(&fun->loc_fun); 1144 1145 return fun; 950 1146 } 951 1147 … … 965 1161 } 966 1162 1163 /** Increase function node reference count. 1164 * 1165 * @param fun Function node 1166 */ 1167 void fun_add_ref(fun_node_t *fun) 1168 { 1169 atomic_inc(&fun->refcnt); 1170 } 1171 1172 /** Decrease function node reference count. 1173 * 1174 * When the count drops to zero the function node is freed. 1175 * 1176 * @param fun Function node 1177 */ 1178 void fun_del_ref(fun_node_t *fun) 1179 { 1180 if (atomic_predec(&fun->refcnt) == 0) 1181 delete_fun_node(fun); 1182 } 1183 967 1184 /** Find the function node with the specified handle. 968 1185 * … … 975 1192 unsigned long key = handle; 976 1193 link_t *link; 1194 fun_node_t *fun; 977 1195 978 1196 assert(fibril_rwlock_is_locked(&tree->rwlock)); … … 982 1200 return NULL; 983 1201 984 return hash_table_get_instance(link, fun_node_t, devman_fun); 1202 fun = hash_table_get_instance(link, fun_node_t, devman_fun); 1203 1204 return fun; 985 1205 } 986 1206 … … 996 1216 997 1217 fibril_rwlock_read_lock(&tree->rwlock); 1218 998 1219 fun = find_fun_node_no_lock(tree, handle); 1220 if (fun != NULL) 1221 fun_add_ref(fun); 1222 999 1223 fibril_rwlock_read_unlock(&tree->rwlock); 1000 1224 … … 1004 1228 /** Create and set device's full path in device tree. 1005 1229 * 1230 * @param tree Device tree 1006 1231 * @param node The device's device node. 1007 1232 * @param parent The parent device node. … … 1009 1234 * resources etc.). 1010 1235 */ 1011 static bool set_fun_path(fun_node_t *fun, fun_node_t *parent) 1012 { 1236 static bool set_fun_path(dev_tree_t *tree, fun_node_t *fun, fun_node_t *parent) 1237 { 1238 assert(fibril_rwlock_is_write_locked(&tree->rwlock)); 1013 1239 assert(fun->name != NULL); 1014 1240 … … 1037 1263 * 1038 1264 * @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. 1265 * @param dev The newly added device node. 1266 * @param pfun The parent function node. 1042 1267 * 1043 1268 * @return True on success, false otherwise (insufficient resources … … 1046 1271 bool insert_dev_node(dev_tree_t *tree, dev_node_t *dev, fun_node_t *pfun) 1047 1272 { 1048 assert(dev != NULL);1049 assert(tree != NULL);1050 1273 assert(fibril_rwlock_is_write_locked(&tree->rwlock)); 1051 1274 … … 1065 1288 } 1066 1289 1290 /** Remove device from device tree. 1291 * 1292 * @param tree Device tree 1293 * @param dev Device node 1294 */ 1295 void remove_dev_node(dev_tree_t *tree, dev_node_t *dev) 1296 { 1297 assert(fibril_rwlock_is_write_locked(&tree->rwlock)); 1298 1299 log_msg(LVL_DEBUG, "remove_dev_node(dev=%p)", dev); 1300 1301 /* Remove node from the handle-to-node map. */ 1302 unsigned long key = dev->handle; 1303 hash_table_remove(&tree->devman_devices, &key, 1); 1304 1305 /* Unlink from parent function. */ 1306 dev->pfun->child = NULL; 1307 dev->pfun = NULL; 1308 1309 dev->state = DEVICE_REMOVED; 1310 } 1311 1312 1067 1313 /** Insert new function into device tree. 1068 1314 * 1069 1315 * @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.1316 * @param fun The newly added function node. 1317 * @param fun_name The name of the newly added function. 1318 * @param dev Owning device node. 1073 1319 * 1074 1320 * @return True on success, false otherwise (insufficient resources … … 1080 1326 fun_node_t *pfun; 1081 1327 1082 assert(fun != NULL);1083 assert(tree != NULL);1084 1328 assert(fun_name != NULL); 1085 1329 assert(fibril_rwlock_is_write_locked(&tree->rwlock)); … … 1092 1336 1093 1337 fun->name = fun_name; 1094 if (!set_fun_path( fun, pfun)) {1338 if (!set_fun_path(tree, fun, pfun)) { 1095 1339 return false; 1096 1340 } … … 1116 1360 void remove_fun_node(dev_tree_t *tree, fun_node_t *fun) 1117 1361 { 1118 assert(tree != NULL);1119 assert(fun != NULL);1120 1362 assert(fibril_rwlock_is_write_locked(&tree->rwlock)); 1121 1363 … … 1127 1369 if (fun->dev != NULL) 1128 1370 list_remove(&fun->dev_functions); 1371 1372 fun->dev = NULL; 1373 fun->state = FUN_REMOVED; 1129 1374 } 1130 1375 … … 1148 1393 1149 1394 fun_node_t *fun = tree->root_node; 1395 fun_add_ref(fun); 1150 1396 /* 1151 1397 * Relative path to the function from its parent (but with '/' at the … … 1165 1411 } 1166 1412 1167 fun = find_node_child(fun, rel_path + 1); 1413 fun_node_t *cfun = find_node_child(tree, fun, rel_path + 1); 1414 fun_del_ref(fun); 1415 fun = cfun; 1168 1416 1169 1417 if (cont) { … … 1183 1431 * Device tree rwlock should be held at least for reading. 1184 1432 * 1433 * @param tree Device tree 1185 1434 * @param dev Device the function belongs to. 1186 1435 * @param name Function name (not path). … … 1188 1437 * @retval NULL No function with given name. 1189 1438 */ 1190 fun_node_t *find_fun_node_in_device(dev_ node_t *dev, const char *name)1191 { 1192 assert(dev != NULL); 1439 fun_node_t *find_fun_node_in_device(dev_tree_t *tree, dev_node_t *dev, 1440 const char *name) 1441 { 1193 1442 assert(name != NULL); 1443 assert(fibril_rwlock_is_locked(&tree->rwlock)); 1194 1444 1195 1445 fun_node_t *fun; … … 1198 1448 fun = list_get_instance(link, fun_node_t, dev_functions); 1199 1449 1200 if (str_cmp(name, fun->name) == 0) 1450 if (str_cmp(name, fun->name) == 0) { 1451 fun_add_ref(fun); 1201 1452 return fun; 1453 } 1202 1454 } 1203 1455 … … 1209 1461 * Device tree rwlock should be held at least for reading. 1210 1462 * 1463 * @param tree Device tree 1211 1464 * @param parent The parent function node. 1212 1465 * @param name The name of the child function. 1213 1466 * @return The child function node. 1214 1467 */ 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); 1468 static fun_node_t *find_node_child(dev_tree_t *tree, fun_node_t *pfun, 1469 const char *name) 1470 { 1471 return find_fun_node_in_device(tree, pfun->child, name); 1218 1472 } 1219 1473 … … 1228 1482 fibril_rwlock_read_lock(&tree->rwlock); 1229 1483 link = hash_table_find(&tree->loc_functions, &key); 1230 if (link != NULL) 1484 if (link != NULL) { 1231 1485 fun = hash_table_get_instance(link, fun_node_t, loc_fun); 1486 fun_add_ref(fun); 1487 } 1232 1488 fibril_rwlock_read_unlock(&tree->rwlock); 1233 1489 … … 1237 1493 void tree_add_loc_function(dev_tree_t *tree, fun_node_t *fun) 1238 1494 { 1495 assert(fibril_rwlock_is_write_locked(&tree->rwlock)); 1496 1239 1497 unsigned long key = (unsigned long) fun->service_id; 1240 fibril_rwlock_write_lock(&tree->rwlock);1241 1498 hash_table_insert(&tree->loc_functions, &key, &fun->loc_fun); 1242 fibril_rwlock_write_unlock(&tree->rwlock);1243 1499 } 1244 1500
Note:
See TracChangeset
for help on using the changeset viewer.