source: mainline/uspace/drv/nic/lo/lo.c@ d57122c

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since d57122c was 6d8455d, checked in by Jiri Svoboda <jiri@…>, 14 years ago

Eliminate packet_t from sending direction of NIC interface.

  • Property mode set to 100644
File size: 3.9 KB
Line 
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
48static nic_address_t lo_addr = {
49 .address = {0, 0, 0, 0, 0, 0}
50};
51
52static ddf_dev_ops_t lo_dev_ops;
53
54static 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
61static void lo_send_frame(nic_t *nic_data, void *data, size_t size)
62{
63 packet_t *packet;
64 int rc;
65
66 packet = nic_alloc_packet(nic_data, size);
67 if (packet == NULL)
68 return;
69
70 rc = packet_copy_data(packet, data, size);
71 if (rc != EOK)
72 return;
73
74 nic_report_send_ok(nic_data, 1, size);
75 nic_received_noneth_packet(nic_data, packet);
76}
77
78static int lo_set_address(ddf_fun_t *fun, const nic_address_t *address)
79{
80 printf("%s: Set loopback HW to " PRIMAC "\n", NAME,
81 ARGSMAC(address->address));
82 return ENOTSUP;
83}
84
85static int lo_get_device_info(ddf_fun_t *fun, nic_device_info_t *info)
86{
87 assert(info);
88 memcpy(info, &lo_info, sizeof(nic_device_info_t));
89 return EOK;
90}
91
92static int lo_dev_add(ddf_dev_t *dev)
93{
94 nic_t *nic_data = nic_create_and_bind(dev);
95 if (nic_data == NULL) {
96 printf("%s: Failed to initialize\n", NAME);
97 return ENOMEM;
98 }
99
100 dev->driver_data = nic_data;
101 nic_set_send_frame_handler(nic_data, lo_send_frame);
102
103 int rc = nic_connect_to_services(nic_data);
104 if (rc != EOK) {
105 printf("%s: Failed to connect to services\n", NAME);
106 nic_unbind_and_destroy(dev);
107 return rc;
108 }
109
110 rc = nic_register_as_ddf_fun(nic_data, &lo_dev_ops);
111 if (rc != EOK) {
112 printf("%s: Failed to register as DDF function\n", NAME);
113 nic_unbind_and_destroy(dev);
114 return rc;
115 }
116
117 rc = nic_report_address(nic_data, &lo_addr);
118 if (rc != EOK) {
119 printf("%s: Failed to setup loopback address\n", NAME);
120 nic_unbind_and_destroy(dev);
121 return rc;
122 }
123
124 printf("%s: Adding loopback device '%s'\n", NAME, dev->name);
125 return EOK;
126}
127
128static nic_iface_t lo_nic_iface;
129
130static driver_ops_t lo_driver_ops = {
131 .dev_add = lo_dev_add,
132};
133
134static driver_t lo_driver = {
135 .name = NAME,
136 .driver_ops = &lo_driver_ops
137};
138
139int main(int argc, char *argv[])
140{
141 nic_driver_init(NAME);
142 nic_driver_implement(&lo_driver_ops, &lo_dev_ops, &lo_nic_iface);
143 lo_nic_iface.set_address = lo_set_address;
144 lo_nic_iface.get_device_info = lo_get_device_info;
145
146 return ddf_driver_main(&lo_driver);
147}
148
149/** @}
150 */
Note: See TracBrowser for help on using the repository browser.