source: mainline/uspace/drv/infrastructure/rootvirt/rootvirt.c@ 5203e256

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 5203e256 was 5203e256, checked in by Martin Decky <martin@…>, 14 years ago

keep the drivers source tree tidy by using logical subdirectories

  • Property mode set to 100644
File size: 3.8 KB
RevLine 
[6f9e7fea]1/*
2 * Copyright (c) 2010 Vojtech Horky
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/**
30 * @defgroup rootvirt Root device driver for virtual devices.
31 * @{
32 */
33
34/** @file
35 */
36
37#include <assert.h>
38#include <stdio.h>
39#include <errno.h>
40#include <str_error.h>
[af6b5157]41#include <ddf/driver.h>
[fc51296]42#include <ddf/log.h>
[6f9e7fea]43
44#define NAME "rootvirt"
45
[8b1e15ac]46/** Virtual function entry */
[6f9e7fea]47typedef struct {
[8b1e15ac]48 /** Function name */
[6f9e7fea]49 const char *name;
[8b1e15ac]50 /** Function match ID */
[6f9e7fea]51 const char *match_id;
[8b1e15ac]52} virtual_function_t;
[6f9e7fea]53
[8b1e15ac]54/** List of existing virtual functions */
55virtual_function_t virtual_functions[] = {
[6f9e7fea]56#include "devices.def"
[8b1e15ac]57 /* Terminating item */
[6f9e7fea]58 {
59 .name = NULL,
60 .match_id = NULL
61 }
62};
63
[83a2f43]64static int rootvirt_add_device(ddf_dev_t *dev);
[6f9e7fea]65
66static driver_ops_t rootvirt_ops = {
[68414f4a]67 .add_device = &rootvirt_add_device
[6f9e7fea]68};
69
70static driver_t rootvirt_driver = {
71 .name = NAME,
72 .driver_ops = &rootvirt_ops
73};
74
[8b1e15ac]75/** Add function to the virtual device.
[6f9e7fea]76 *
[8b1e15ac]77 * @param vdev The virtual device
78 * @param vfun Virtual function description
79 * @return EOK on success or negative error code.
[6f9e7fea]80 */
[83a2f43]81static int rootvirt_add_fun(ddf_dev_t *vdev, virtual_function_t *vfun)
[6f9e7fea]82{
[83a2f43]83 ddf_fun_t *fun;
[cd0684d]84 int rc;
85
[ebcb05a]86 ddf_msg(LVL_DEBUG, "Registering function `%s' (match \"%s\")",
[8b1e15ac]87 vfun->name, vfun->match_id);
[6f9e7fea]88
[cd0684d]89 fun = ddf_fun_create(vdev, fun_inner, vfun->name);
90 if (fun == NULL) {
[ebcb05a]91 ddf_msg(LVL_ERROR, "Failed creating function %s", vfun->name);
[cd0684d]92 return ENOMEM;
93 }
[6f9e7fea]94
[cd0684d]95 rc = ddf_fun_add_match_id(fun, vfun->match_id, 10);
96 if (rc != EOK) {
[ebcb05a]97 ddf_msg(LVL_ERROR, "Failed adding match IDs to function %s",
[8b1e15ac]98 vfun->name);
[cd0684d]99 ddf_fun_destroy(fun);
100 return rc;
[6f9e7fea]101 }
102
[cd0684d]103 rc = ddf_fun_bind(fun);
104 if (rc != EOK) {
[ebcb05a]105 ddf_msg(LVL_ERROR, "Failed binding function %s: %s",
106 vfun->name, str_error(rc));
[cd0684d]107 ddf_fun_destroy(fun);
108 return rc;
109 }
110
[ebcb05a]111 ddf_msg(LVL_NOTE, "Registered child device `%s'", vfun->name);
[cd0684d]112 return EOK;
[6f9e7fea]113}
114
[83a2f43]115static int rootvirt_add_device(ddf_dev_t *dev)
[6f9e7fea]116{
117 static int instances = 0;
118
119 /*
120 * Allow only single instance of root virtual device.
121 */
122 instances++;
123 if (instances > 1) {
124 return ELIMIT;
125 }
126
[ebcb05a]127 ddf_msg(LVL_DEBUG, "add_device(handle=%d)", (int)dev->handle);
[bab6388]128
[6f9e7fea]129 /*
[8b1e15ac]130 * Go through all virtual functions and try to add them.
[6f9e7fea]131 * We silently ignore failures.
132 */
[8b1e15ac]133 virtual_function_t *vfun = virtual_functions;
134 while (vfun->name != NULL) {
[68414f4a]135 (void) rootvirt_add_fun(dev, vfun);
[8b1e15ac]136 vfun++;
[6f9e7fea]137 }
138
139 return EOK;
140}
141
142int main(int argc, char *argv[])
143{
144 printf(NAME ": HelenOS virtual devices root driver\n");
[fc51296]145
146 ddf_log_init(NAME, LVL_ERROR);
[83a2f43]147 return ddf_driver_main(&rootvirt_driver);
[6f9e7fea]148}
149
150/**
151 * @}
152 */
153
Note: See TracBrowser for help on using the repository browser.