00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00032 #include <assert.h>
00033 #include <stdio.h>
00034 #include <errno.h>
00035 #include <str_error.h>
00036 #include <ddf/driver.h>
00037 #include <ddf/log.h>
00038
00039 #include "test1.h"
00040
00041 static int test1_add_device(ddf_dev_t *dev);
00042
00043 static driver_ops_t driver_ops = {
00044 .add_device = &test1_add_device
00045 };
00046
00047 static driver_t test1_driver = {
00048 .name = NAME,
00049 .driver_ops = &driver_ops
00050 };
00051
00060 static int register_fun_verbose(ddf_dev_t *parent, const char *message,
00061 const char *name, const char *match_id, int match_score,
00062 int expected_rc)
00063 {
00064 ddf_fun_t *fun = NULL;
00065 int rc;
00066
00067 ddf_msg(LVL_DEBUG, "Registering function `%s': %s.", name, message);
00068
00069 fun = ddf_fun_create(parent, fun_inner, name);
00070 if (fun == NULL) {
00071 ddf_msg(LVL_ERROR, "Failed creating function %s", name);
00072 rc = ENOMEM;
00073 goto leave;
00074 }
00075
00076 rc = ddf_fun_add_match_id(fun, str_dup(match_id), match_score);
00077 if (rc != EOK) {
00078 ddf_msg(LVL_ERROR, "Failed adding match IDs to function %s",
00079 name);
00080 goto leave;
00081 }
00082
00083 rc = ddf_fun_bind(fun);
00084 if (rc != EOK) {
00085 ddf_msg(LVL_ERROR, "Failed binding function %s: %s", name,
00086 str_error(rc));
00087 goto leave;
00088 }
00089
00090 ddf_msg(LVL_NOTE, "Registered child device `%s'", name);
00091 rc = EOK;
00092
00093 leave:
00094 if (rc != expected_rc) {
00095 fprintf(stderr,
00096 NAME ": Unexpected error registering function `%s'.\n"
00097 NAME ": Expected \"%s\" but got \"%s\".\n",
00098 name, str_error(expected_rc), str_error(rc));
00099 }
00100
00101 if ((rc != EOK) && (fun != NULL)) {
00102 ddf_fun_destroy(fun);
00103 }
00104
00105 return rc;
00106 }
00107
00125 static int test1_add_device(ddf_dev_t *dev)
00126 {
00127 ddf_fun_t *fun_a;
00128 int rc;
00129
00130 ddf_msg(LVL_DEBUG, "add_device(name=\"%s\", handle=%d)",
00131 dev->name, (int) dev->handle);
00132
00133 fun_a = ddf_fun_create(dev, fun_exposed, "a");
00134 if (fun_a == NULL) {
00135 ddf_msg(LVL_ERROR, "Failed creating function 'a'.");
00136 return ENOMEM;
00137 }
00138
00139 rc = ddf_fun_bind(fun_a);
00140 if (rc != EOK) {
00141 ddf_msg(LVL_ERROR, "Failed binding function 'a'.");
00142 return rc;
00143 }
00144
00145 ddf_fun_add_to_class(fun_a, "virtual");
00146
00147 if (str_cmp(dev->name, "null") == 0) {
00148 fun_a->ops = &char_device_ops;
00149 ddf_fun_add_to_class(fun_a, "virt-null");
00150 } else if (str_cmp(dev->name, "test1") == 0) {
00151 (void) register_fun_verbose(dev,
00152 "cloning myself ;-)", "clone",
00153 "virtual&test1", 10, EOK);
00154 (void) register_fun_verbose(dev,
00155 "cloning myself twice ;-)", "clone",
00156 "virtual&test1", 10, EEXISTS);
00157 } else if (str_cmp(dev->name, "clone") == 0) {
00158 (void) register_fun_verbose(dev,
00159 "run by the same task", "child",
00160 "virtual&test1&child", 10, EOK);
00161 }
00162
00163 ddf_msg(LVL_DEBUG, "Device `%s' accepted.", dev->name);
00164
00165 return EOK;
00166 }
00167
00168 int main(int argc, char *argv[])
00169 {
00170 printf(NAME ": HelenOS test1 virtual device driver\n");
00171 ddf_log_init(NAME, LVL_ERROR);
00172 return ddf_driver_main(&test1_driver);
00173 }
00174