rootvirt.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2010 Vojtech Horky
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  *
00009  * - Redistributions of source code must retain the above copyright
00010  *   notice, this list of conditions and the following disclaimer.
00011  * - Redistributions in binary form must reproduce the above copyright
00012  *   notice, this list of conditions and the following disclaimer in the
00013  *   documentation and/or other materials provided with the distribution.
00014  * - The name of the author may not be used to endorse or promote products
00015  *   derived from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00018  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00019  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00020  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00021  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00022  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00023  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00024  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00026  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027  */
00028 
00037 #include <assert.h>
00038 #include <stdio.h>
00039 #include <errno.h>
00040 #include <str_error.h>
00041 #include <ddf/driver.h>
00042 #include <ddf/log.h>
00043 
00044 #define NAME "rootvirt"
00045 
00047 typedef struct {
00049         const char *name;
00051         const char *match_id;
00052 } virtual_function_t;
00053 
00055 virtual_function_t virtual_functions[] = {
00056 #include "devices.def"
00057         /* Terminating item */
00058         {
00059                 .name = NULL,
00060                 .match_id = NULL
00061         }
00062 };
00063 
00064 static int rootvirt_add_device(ddf_dev_t *dev);
00065 
00066 static driver_ops_t rootvirt_ops = {
00067         .add_device = &rootvirt_add_device
00068 };
00069 
00070 static driver_t rootvirt_driver = {
00071         .name = NAME,
00072         .driver_ops = &rootvirt_ops
00073 };
00074 
00081 static int rootvirt_add_fun(ddf_dev_t *vdev, virtual_function_t *vfun)
00082 {
00083         ddf_fun_t *fun;
00084         int rc;
00085 
00086         ddf_msg(LVL_DEBUG, "Registering function `%s' (match \"%s\")",
00087             vfun->name, vfun->match_id);
00088 
00089         fun = ddf_fun_create(vdev, fun_inner, vfun->name);
00090         if (fun == NULL) {
00091                 ddf_msg(LVL_ERROR, "Failed creating function %s", vfun->name);
00092                 return ENOMEM;
00093         }
00094 
00095         rc = ddf_fun_add_match_id(fun, vfun->match_id, 10);
00096         if (rc != EOK) {
00097                 ddf_msg(LVL_ERROR, "Failed adding match IDs to function %s",
00098                     vfun->name);
00099                 ddf_fun_destroy(fun);
00100                 return rc;
00101         }
00102 
00103         rc = ddf_fun_bind(fun);
00104         if (rc != EOK) {
00105                 ddf_msg(LVL_ERROR, "Failed binding function %s: %s",
00106                     vfun->name, str_error(rc));
00107                 ddf_fun_destroy(fun);
00108                 return rc;
00109         }
00110 
00111         ddf_msg(LVL_NOTE, "Registered child device `%s'", vfun->name);
00112         return EOK;
00113 }
00114 
00115 static int rootvirt_add_device(ddf_dev_t *dev)
00116 {
00117         static int instances = 0;
00118 
00119         /*
00120          * Allow only single instance of root virtual device.
00121          */
00122         instances++;
00123         if (instances > 1) {
00124                 return ELIMIT;
00125         }
00126 
00127         ddf_msg(LVL_DEBUG, "add_device(handle=%d)", (int)dev->handle);
00128 
00129         /*
00130          * Go through all virtual functions and try to add them.
00131          * We silently ignore failures.
00132          */
00133         virtual_function_t *vfun = virtual_functions;
00134         while (vfun->name != NULL) {
00135                 (void) rootvirt_add_fun(dev, vfun);
00136                 vfun++;
00137         }
00138 
00139         return EOK;
00140 }
00141 
00142 int main(int argc, char *argv[])
00143 {
00144         printf(NAME ": HelenOS virtual devices root driver\n");
00145 
00146         ddf_log_init(NAME, LVL_ERROR);
00147         return ddf_driver_main(&rootvirt_driver);
00148 }
00149 

Generated on Thu Jun 2 07:45:44 2011 for HelenOS/USB by  doxygen 1.4.7