root / Whitix / branches / network / net / device.c

Revision 639, 2.5 kB (checked in by mwhitworth, 7 months ago)

Add to net device API, add NetDeviceSend function.

Line 
1/*  This file is part of Whitix.
2 *
3 *  Whitix is free software; you can redistribute it and/or modify
4 *  it under the terms of the GNU General Public License as published by
5 *  the Free Software Foundation; either version 2 of the License, or
6 *  (at your option) any later version.
7 *
8 *  Whitix is distributed in the hope that it will be useful,
9 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
10 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 *  GNU General Public License for more details.
12 *
13 *  You should have received a copy of the GNU General Public License
14 *  along with Whitix; if not, write to the Free Software
15 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
16 *
17 */
18
19#include <fs/devfs.h>
20#include <fs/vfs.h>
21#include <init.h>
22#include <net/network.h>
23#include <net/socket.h>
24#include <slab.h>
25
26struct Cache* netDeviceCache;
27
28#define NETWORK_MAJOR   128
29
30/* Only one device for now! TODO: Add support for multiple devices. */
31struct NetDevice* currDevice;
32
33struct NetDevice* NetDeviceAlloc()
34{
35        return (struct NetDevice*)MemCacheAlloc(netDeviceCache);
36}
37
38/* Public API */
39
40int NetDeviceSend(struct NetDevice* device, struct SocketBuffer* buffer)
41{
42        if (!device->ops || !device->ops->send)
43                return -ENOTIMPL;
44
45        if (device-> state & NET_DEVICE_QUEUE_RUNNING)
46                return device->ops->send(device, buffer);
47        else{
48                /* Add the packet onto the queue, and wait for the device to pick
49                 * it up once it has finished an event like a send interrupt.
50                 */
51                ListAddTail(&buffer->next, &device->sendList);
52        }
53}
54
55int NetDeviceIoCtl(struct File* file, unsigned long code, BYTE* data)
56{
57        struct NetDevice* netDevice=currDevice; /* For now! */
58
59        if (code >= 0x40000000)
60        {
61                if (netDevice->protoOps->ioctl)
62                        return netDevice->protoOps->ioctl(netDevice, code, data);
63                else
64                        return -ENOTIMPL;
65        }
66
67        printf("NetDeviceIoCtl\n");
68        return 0;
69}
70
71struct FileOps netFileOps=
72{
73        .ioctl = NetDeviceIoCtl,
74};
75
76int NetDeviceRegister(struct NetDevice* device)
77{
78        char buf[32];
79
80        currDevice=device;
81
82        /* Add to the device filesystem. TODO: Check for already existing devices. */
83        sprintf(buf, "Network/%s%d", device->name, 0);
84
85        DevAddDevice(buf, NETWORK_MAJOR, 0, DEVICE_CHAR, &netFileOps);
86        INIT_LIST_HEAD(&device->sendList);
87        device->state = NET_DEVICE_UP | NET_DEVICE_QUEUE_RUNNING;
88
89        return 0;
90}
91
92int NetDeviceInit()
93{
94        netDeviceCache=MemCacheCreate("Network device cache", sizeof(struct NetDevice), NULL, NULL, 0);
95        return 0;
96}
97
98NetInit(NetDeviceInit);
Note: See TracBrowser for help on using the browser.