]> Joshua Wise's Git repositories - netwatch.git/blob - net/net.c
46ea9cd75bb9d1ab345eccaebbede9bd20028064
[netwatch.git] / net / net.c
1 #include <pci.h>
2 #include <io.h>
3 #include <video_defines.h>
4 #include <smram.h>
5 #include <pci-bother.h>
6 #include <output.h>
7 #include <minilib.h>
8 #include "net.h"
9 #include "../aseg/keyboard.h"
10
11 static struct nic *_nic = 0x0;
12
13 extern struct pci_driver a3c90x_driver;
14
15 static char test[1024] = {0};
16
17 static char packet[4096] = {0};
18
19 typedef struct packet_t {
20         char from[6];
21         char to[6];
22         unsigned short ethertype;
23         unsigned short datalen;
24         unsigned char command;
25         char data[];
26 } packet_t;
27
28 static unsigned char vga_read(unsigned char idx)
29 {
30         outb(CRTC_IDX_REG, idx);
31         return inb(CRTC_DATA_REG);
32 }
33
34 static unsigned int vga_base()
35 {
36         return (((unsigned int) vga_read(CRTC_START_ADDR_MSB_IDX)) << 9)
37              + (((unsigned int) vga_read(CRTC_START_ADDR_LSB_IDX)) << 1);
38 }
39
40 void handle_command(packet_t * p)
41 {
42         uint16_t dl = htons(p->datalen);
43         int i;
44
45         outputf("NIC: Command: 0x%x, %d bytes", p->command, dl);
46
47         if (p->command == 0x42)
48         {
49                 for (i = 0; i < dl; i++)
50                         kbd_inject_key(p->data[i]);
51         }
52 }
53
54 void eth_poll()
55 {
56         int i;
57 //      static int c;
58         static short pos = 0x0;
59         unsigned short base = vga_base();
60         unsigned char *p = (unsigned char *)0xB8000;
61         smram_state_t old_state;
62         
63         if (!_nic)
64                 return;
65
66         if (_nic->poll(_nic, 0)) {
67                 _nic->packet = packet;
68                 _nic->poll(_nic, 1);
69
70                 packet_t * p = (packet_t *) packet;
71
72                 outputf("NIC: Packet: %d 0x%x", _nic->packetlen, htons(p->ethertype));
73                 if (htons(p->ethertype) == 0x1338) {
74                         if (htons(p->datalen) + sizeof(packet_t) > _nic->packetlen) {
75                                 outputf("NIC: Malformed packet");
76                         } else {
77                                 handle_command(p);
78                         }
79                 }
80         }
81         smram_tseg_set_state(SMRAM_TSEG_OPEN);
82         old_state = smram_save_state();
83         
84 //      if ((c++) % 2)
85 //              return;
86
87         if (((base + 80*25*2)%0x8000) < base)
88         {
89                 if ((pos > ((base + 80*25*2)%0x8000)) && (pos < base))
90                         pos = base;
91         } else if ((pos > base + 80*25*2) || (pos < base))
92                 pos = base;
93         
94         test[0] = pos >> 8;
95         test[1] = pos & 0xFF;
96         test[2] = base >> 8;
97         test[3] = base & 0xFF;
98
99         smram_aseg_set_state(SMRAM_ASEG_SMMCODE);
100         
101         for (i = 4; i < 1024; i++)
102         {
103                 test[i] = p[pos++];
104                 pos %= 0x8000;
105         }
106         smram_restore_state(old_state);
107         _nic->transmit("\x00\x03\x93\x87\x84\x8C", 0x1337, 1024, test);
108 }
109
110 int eth_register(struct nic *nic)
111 {
112         if (_nic)
113                 return -1;
114         _nic = nic;
115         return 0;
116 }
117
118 void eth_init()
119 {
120         /* Required for DMA to work. :( */
121         smram_tseg_set_state(SMRAM_TSEG_OPEN);
122         pci_probe_driver(a3c90x_driver);
123 }
This page took 0.025445 seconds and 2 git commands to generate.