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 | #include <packet_client.h>
|
---|
45 |
|
---|
46 | #define NAME "lo"
|
---|
47 |
|
---|
48 | static nic_address_t lo_addr = {
|
---|
49 | .address = {0, 0, 0, 0, 0, 0}
|
---|
50 | };
|
---|
51 |
|
---|
52 | static ddf_dev_ops_t lo_dev_ops;
|
---|
53 |
|
---|
54 | static nic_device_info_t lo_info = {
|
---|
55 | .vendor_name = "HelenOS",
|
---|
56 | .model_name = "loopback",
|
---|
57 | .part_number = "N/A (virtual device)",
|
---|
58 | .serial_number = "N/A (virtual device)"
|
---|
59 | };
|
---|
60 |
|
---|
61 | static void lo_write_packet(nic_t *nic_data, packet_t *packet)
|
---|
62 | {
|
---|
63 | nic_report_send_ok(nic_data, 1, packet_get_data_length(packet));
|
---|
64 | nic_received_noneth_packet(nic_data, packet);
|
---|
65 | }
|
---|
66 |
|
---|
67 | static int lo_set_address(ddf_fun_t *fun, const nic_address_t *address)
|
---|
68 | {
|
---|
69 | printf("%s: Set loopback HW to " PRIMAC "\n", NAME,
|
---|
70 | ARGSMAC(address->address));
|
---|
71 | return ENOTSUP;
|
---|
72 | }
|
---|
73 |
|
---|
74 | static int lo_get_device_info(ddf_fun_t *fun, nic_device_info_t *info)
|
---|
75 | {
|
---|
76 | assert(info);
|
---|
77 | memcpy(info, &lo_info, sizeof(nic_device_info_t));
|
---|
78 | return EOK;
|
---|
79 | }
|
---|
80 |
|
---|
81 | static int lo_dev_add(ddf_dev_t *dev)
|
---|
82 | {
|
---|
83 | nic_t *nic_data = nic_create_and_bind(dev);
|
---|
84 | if (nic_data == NULL) {
|
---|
85 | printf("%s: Failed to initialize\n", NAME);
|
---|
86 | return ENOMEM;
|
---|
87 | }
|
---|
88 |
|
---|
89 | dev->driver_data = nic_data;
|
---|
90 | nic_set_write_packet_handler(nic_data, lo_write_packet);
|
---|
91 |
|
---|
92 | int rc = nic_connect_to_services(nic_data);
|
---|
93 | if (rc != EOK) {
|
---|
94 | printf("%s: Failed to connect to services\n", NAME);
|
---|
95 | nic_unbind_and_destroy(dev);
|
---|
96 | return rc;
|
---|
97 | }
|
---|
98 |
|
---|
99 | rc = nic_register_as_ddf_fun(nic_data, &lo_dev_ops);
|
---|
100 | if (rc != EOK) {
|
---|
101 | printf("%s: Failed to register as DDF function\n", NAME);
|
---|
102 | nic_unbind_and_destroy(dev);
|
---|
103 | return rc;
|
---|
104 | }
|
---|
105 |
|
---|
106 | rc = nic_report_address(nic_data, &lo_addr);
|
---|
107 | if (rc != EOK) {
|
---|
108 | printf("%s: Failed to setup loopback address\n", NAME);
|
---|
109 | nic_unbind_and_destroy(dev);
|
---|
110 | return rc;
|
---|
111 | }
|
---|
112 |
|
---|
113 | printf("%s: Adding loopback device '%s'\n", NAME, dev->name);
|
---|
114 | return EOK;
|
---|
115 | }
|
---|
116 |
|
---|
117 | static nic_iface_t lo_nic_iface;
|
---|
118 |
|
---|
119 | static driver_ops_t lo_driver_ops = {
|
---|
120 | .dev_add = lo_dev_add,
|
---|
121 | };
|
---|
122 |
|
---|
123 | static driver_t lo_driver = {
|
---|
124 | .name = NAME,
|
---|
125 | .driver_ops = &lo_driver_ops
|
---|
126 | };
|
---|
127 |
|
---|
128 | int main(int argc, char *argv[])
|
---|
129 | {
|
---|
130 | nic_driver_init(NAME);
|
---|
131 | nic_driver_implement(&lo_driver_ops, &lo_dev_ops, &lo_nic_iface);
|
---|
132 | lo_nic_iface.set_address = lo_set_address;
|
---|
133 | lo_nic_iface.get_device_info = lo_get_device_info;
|
---|
134 |
|
---|
135 | return ddf_driver_main(&lo_driver);
|
---|
136 | }
|
---|
137 |
|
---|
138 | /** @}
|
---|
139 | */
|
---|