1 | /*
|
---|
2 | * Copyright (c) 2011 Radim Vansa
|
---|
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 | * @addtogroup drv_lo
|
---|
31 | * @brief Loopback virtual device driver
|
---|
32 | * @{
|
---|
33 | */
|
---|
34 | /**
|
---|
35 | * @file
|
---|
36 | */
|
---|
37 |
|
---|
38 | #include <assert.h>
|
---|
39 | #include <stdio.h>
|
---|
40 | #include <errno.h>
|
---|
41 | #include <stdlib.h>
|
---|
42 | #include <async.h>
|
---|
43 | #include <nic.h>
|
---|
44 |
|
---|
45 | #define NAME "lo"
|
---|
46 |
|
---|
47 | static nic_address_t lo_addr = {
|
---|
48 | .address = {0, 0, 0, 0, 0, 0}
|
---|
49 | };
|
---|
50 |
|
---|
51 | static ddf_dev_ops_t lo_dev_ops;
|
---|
52 |
|
---|
53 | static nic_device_info_t lo_info = {
|
---|
54 | .vendor_name = "HelenOS",
|
---|
55 | .model_name = "loopback",
|
---|
56 | .part_number = "N/A (virtual device)",
|
---|
57 | .serial_number = "N/A (virtual device)"
|
---|
58 | };
|
---|
59 |
|
---|
60 | static void lo_send_frame(nic_t *nic_data, void *data, size_t size)
|
---|
61 | {
|
---|
62 | nic_report_send_ok(nic_data, 1, size);
|
---|
63 | nic_received_noneth_frame(nic_data, data, size);
|
---|
64 | }
|
---|
65 |
|
---|
66 | static int lo_set_address(ddf_fun_t *fun, const nic_address_t *address)
|
---|
67 | {
|
---|
68 | printf("%s: Set loopback HW to " PRIMAC "\n", NAME,
|
---|
69 | ARGSMAC(address->address));
|
---|
70 | return ENOTSUP;
|
---|
71 | }
|
---|
72 |
|
---|
73 | static int lo_get_device_info(ddf_fun_t *fun, nic_device_info_t *info)
|
---|
74 | {
|
---|
75 | assert(info);
|
---|
76 | memcpy(info, &lo_info, sizeof(nic_device_info_t));
|
---|
77 | return EOK;
|
---|
78 | }
|
---|
79 |
|
---|
80 | static int lo_dev_add(ddf_dev_t *dev)
|
---|
81 | {
|
---|
82 | ddf_fun_t *fun = NULL;
|
---|
83 | bool bound = false;
|
---|
84 |
|
---|
85 | nic_t *nic = nic_create_and_bind(dev);
|
---|
86 | if (nic == NULL) {
|
---|
87 | printf("%s: Failed to initialize\n", NAME);
|
---|
88 | return ENOMEM;
|
---|
89 | }
|
---|
90 |
|
---|
91 | dev->driver_data = nic;
|
---|
92 | nic_set_send_frame_handler(nic, lo_send_frame);
|
---|
93 |
|
---|
94 | int rc = nic_connect_to_services(nic);
|
---|
95 | if (rc != EOK) {
|
---|
96 | printf("%s: Failed to connect to services\n", NAME);
|
---|
97 | goto error;
|
---|
98 | }
|
---|
99 |
|
---|
100 | fun = ddf_fun_create(nic_get_ddf_dev(nic), fun_exposed, "port0");
|
---|
101 | if (fun == NULL) {
|
---|
102 | printf("%s: Failed creating function\n", NAME);
|
---|
103 | rc = ENOMEM;
|
---|
104 | goto error;
|
---|
105 | }
|
---|
106 | nic_set_ddf_fun(nic, fun);
|
---|
107 | fun->ops = &lo_dev_ops;
|
---|
108 | fun->driver_data = nic;
|
---|
109 |
|
---|
110 | rc = nic_report_address(nic, &lo_addr);
|
---|
111 | if (rc != EOK) {
|
---|
112 | printf("%s: Failed to setup loopback address\n", NAME);
|
---|
113 | goto error;
|
---|
114 | }
|
---|
115 |
|
---|
116 | rc = ddf_fun_bind(fun);
|
---|
117 | if (rc != EOK) {
|
---|
118 | printf("%s: Failed binding function\n", NAME);
|
---|
119 | goto error;
|
---|
120 | }
|
---|
121 | bound = true;
|
---|
122 |
|
---|
123 | rc = ddf_fun_add_to_category(fun, DEVICE_CATEGORY_NIC);
|
---|
124 | if (rc != EOK)
|
---|
125 | goto error;
|
---|
126 |
|
---|
127 | printf("%s: Adding loopback device '%s'\n", NAME, dev->name);
|
---|
128 | return EOK;
|
---|
129 |
|
---|
130 | error:
|
---|
131 | if (bound)
|
---|
132 | ddf_fun_unbind(fun);
|
---|
133 |
|
---|
134 | if (fun != NULL)
|
---|
135 | ddf_fun_destroy(fun);
|
---|
136 |
|
---|
137 | nic_unbind_and_destroy(dev);
|
---|
138 | return rc;
|
---|
139 | }
|
---|
140 |
|
---|
141 | static nic_iface_t lo_nic_iface;
|
---|
142 |
|
---|
143 | static driver_ops_t lo_driver_ops = {
|
---|
144 | .dev_add = lo_dev_add,
|
---|
145 | };
|
---|
146 |
|
---|
147 | static driver_t lo_driver = {
|
---|
148 | .name = NAME,
|
---|
149 | .driver_ops = &lo_driver_ops
|
---|
150 | };
|
---|
151 |
|
---|
152 | int main(int argc, char *argv[])
|
---|
153 | {
|
---|
154 | nic_driver_init(NAME);
|
---|
155 | nic_driver_implement(&lo_driver_ops, &lo_dev_ops, &lo_nic_iface);
|
---|
156 | lo_nic_iface.set_address = lo_set_address;
|
---|
157 | lo_nic_iface.get_device_info = lo_get_device_info;
|
---|
158 |
|
---|
159 | return ddf_driver_main(&lo_driver);
|
---|
160 | }
|
---|
161 |
|
---|
162 | /** @}
|
---|
163 | */
|
---|