summaryrefslogtreecommitdiffstats
path: root/drivers/net/tap.c
blob: 8a659c125eeb926c1c0c2d627921b432dcc58cde (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// SPDX-License-Identifier: GPL-2.0-only
/*
 * tap.c - A tap ethernet driver for barebox
 *
 * Copyright (c) 2007 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
 */

#include <common.h>
#include <driver.h>
#include <malloc.h>
#include <net.h>
#include <init.h>
#include <mach/linux.h>

struct tap_priv {
	int fd;
	char *name;
	char *rx_buf;
};

static int tap_eth_send(struct eth_device *edev, void *packet, int length)
{
	struct tap_priv *priv = edev->priv;

	linux_write(priv->fd, packet, length);
	return 0;
}

static int tap_eth_rx(struct eth_device *edev)
{
	struct tap_priv *priv = edev->priv;
	int length;

	length = linux_read_nonblock(priv->fd, priv->rx_buf, PKTSIZE);

	if (length > 0)
		net_receive(edev, priv->rx_buf, length);

	return 0;
}

static int tap_eth_open(struct eth_device *edev)
{
	return 0;
}

static void tap_eth_halt(struct eth_device *edev)
{
	/* nothing to do here */
}

static int tap_get_ethaddr(struct eth_device *edev, unsigned char *adr)
{
	return -1;
}

static int tap_set_ethaddr(struct eth_device *edev, const unsigned char *adr)
{
	return 0;
}

static int tap_probe(struct device_d *dev)
{
	struct eth_device *edev;
	struct tap_priv *priv;
	int ret = 0;

	priv = xzalloc(sizeof(struct tap_priv));
	priv->name = "barebox";

	priv->fd = tap_alloc(priv->name);
	if (priv->fd < 0) {
		ret = priv->fd;
		goto out;
	}

	priv->rx_buf = xmalloc(PKTSIZE);

	edev = xzalloc(sizeof(struct eth_device));
	edev->priv = priv;
	edev->parent = dev;

	edev->init = tap_eth_open;
	edev->open = tap_eth_open;
	edev->send = tap_eth_send;
	edev->recv = tap_eth_rx;
	edev->halt = tap_eth_halt;
	edev->get_ethaddr = tap_get_ethaddr;
	edev->set_ethaddr = tap_set_ethaddr;

	eth_register(edev);

	return 0;

out:
	free(priv);
	return ret;
}

static struct driver_d tap_driver = {
	.name  = "tap",
	.probe = tap_probe,
};
device_platform_driver(tap_driver);