The Pedigree Project 0.1
modules/drivers/x86/ne2k/main.cc
1/*
2 * Copyright (c) 2008-2014, Pedigree Developers
3 *
4 * Please see the CONTRIB file in the root of the source tree for a full
5 * list of contributors.
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20#include "pedigree/kernel/Log.h"
21#include "pedigree/kernel/machine/Device.h"
22#include "pedigree/kernel/utilities/List.h"
23#include "pedigree/kernel/utilities/new"
24#include "pedigree/kernel/utilities/utility.h"
25
26#include "Ne2k.h"
27#include "modules/Module.h"
28#include "modules/system/network-stack/NetworkStack.h"
29
30class Network;
31
32static bool bFound = false;
33static List<Ne2k*> g_Cards;
34
35static void probeDevice(Device* pDev) {
36 NOTICE("NE2K found");
37
38 // Create a new NE2K node
39 Ne2k* pNe2k = new Ne2k(reinterpret_cast<Network*>(pDev));
40
41 // Replace pDev with pNe2k.
42 pNe2k->setParent(pDev->getParent());
43 pDev->getParent()->replaceChild(pDev, pNe2k);
44
45 g_Cards.pushBack(pNe2k);
46 bFound = true;
47
48 // Device(pDev) transfers the live I/O mappings into this object. Keep the
49 // inert, source-masked device owned by the module if IRQ admission failed;
50 // deleting it here would leave the original tree node without mappings.
51 if (!pNe2k->isValid()) {
52 ERROR("NE2K: device initialisation failed; device left disabled");
53 }
54}
55
56static bool entry() {
57 Device::searchByVendorIdAndDeviceId(NE2K_VENDOR_ID, NE2K_DEVICE_ID, probeDevice);
58
59 return bFound;
60}
61
62static void exit() {
63 auto removeCard = [](Device* device, Device* target) {
64 return device == target ? nullptr : device;
65 };
66 auto callback = pedigree_std::make_callable(removeCard);
67 while (g_Cards.count()) {
68 Ne2k* card = g_Cards.popFront();
70 Device::foreach (callback, 0, card);
71 }
72 bFound = false;
73}
74
75MODULE_NAME("ne2k");
76MODULE_ENTRY(&entry);
77MODULE_EXIT(&exit);
78MODULE_DEPENDS("network-stack", "pci");
static void foreach(Callback callback, Device *root=0)
Definition Device.cc:107
Device * getParent() const
Definition Device.h:166
void setParent(Device *p)
Definition Device.h:170
void replaceChild(Device *src, Device *dest)
Definition Device.cc:166
Definition List.h:61
Definition Ne2k.h:42
void deRegisterDevice(Network *pDevice)
static NetworkStack & instance()