ReactOS  0.4.15-dev-499-g1f31905
hardware.c
Go to the documentation of this file.
1 /*
2  * ReactOS Realtek 8139 Driver
3  *
4  * Copyright (C) 2013 Cameron Gutman
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  *
20  */
21 
22 #include "nic.h"
23 
24 #define NDEBUG
25 #include <debug.h>
26 
28 NTAPI
30  IN PRTL_ADAPTER Adapter
31  )
32 {
33  //
34  // Send 0x00 to the CONFIG_1 register (0x52) to set the LWAKE + LWPTN to active high.
35  // This should essentially *power on* the device.
36  // -- OSDev Wiki
37  //
38  NdisRawWritePortUchar(Adapter->IoBase + R_CFG1, 0x00);
39  return NDIS_STATUS_SUCCESS;
40 }
41 
43 NTAPI
45  IN PRTL_ADAPTER Adapter
46  )
47 {
48  UCHAR commandReg;
49  UINT resetAttempts;
50 
51  //
52  // Sending 0x10 to the Command register (0x37) will send the RTL8139 into a software reset.
53  // Once that byte is sent, the RST bit must be checked to make sure that the chip has finished the reset.
54  // If the RST bit is high (1), then the reset is still in operation.
55  // -- OSDev Wiki
56  NdisRawWritePortUchar(Adapter->IoBase + R_CMD, B_CMD_RST);
57 
58  for (resetAttempts = 0; resetAttempts < MAX_RESET_ATTEMPTS; resetAttempts++)
59  {
60  NdisRawReadPortUchar(Adapter->IoBase + R_CMD, &commandReg);
61 
62  if (!(commandReg & B_CMD_RST))
63  {
64  return NDIS_STATUS_SUCCESS;
65  }
66 
67  NdisMSleep(100);
68  }
69 
70  return NDIS_STATUS_FAILURE;
71 }
72 
74 NTAPI
76  IN PRTL_ADAPTER Adapter
77  )
78 {
79  ASSERT(NdisGetPhysicalAddressHigh(Adapter->ReceiveBufferPa) == 0);
80 
81  NdisRawWritePortUlong(Adapter->IoBase + R_RXSA, Adapter->ReceiveBufferPa.LowPart);
82 
83  return NDIS_STATUS_SUCCESS;
84 }
85 
87 NTAPI
89  IN PRTL_ADAPTER Adapter
90  )
91 {
92  NdisRawWritePortUlong(Adapter->IoBase + R_RXSA, 0);
93  return NDIS_STATUS_SUCCESS;
94 }
95 
97 NTAPI
99  IN PRTL_ADAPTER Adapter
100  )
101 {
102  NdisRawWritePortUchar(Adapter->IoBase + R_CMD, B_CMD_TXE | B_CMD_RXE);
103 
104  //
105  // TX and RX must be enabled before setting these
106  //
107  NdisRawWritePortUlong(Adapter->IoBase + R_RC, RC_VAL);
108  NdisRawWritePortUlong(Adapter->IoBase + R_TC, TC_VAL);
109  return NDIS_STATUS_SUCCESS;
110 }
111 
113 NTAPI
115  IN PRTL_ADAPTER Adapter,
116  OUT PUCHAR MacAddress
117  )
118 {
119  UINT i;
120 
121  for (i = 0; i < IEEE_802_ADDR_LENGTH; i++)
122  {
123  NdisRawReadPortUchar(Adapter->IoBase + R_MAC + i, &MacAddress[i]);
124  }
125 
126  return NDIS_STATUS_SUCCESS;
127 }
128 
130 NTAPI
132  IN PRTL_ADAPTER Adapter
133  )
134 {
135  NdisRawWritePortUshort(Adapter->IoBase + R_IM, Adapter->InterruptMask);
136  return NDIS_STATUS_SUCCESS;
137 }
138 
140 NTAPI
142  IN PRTL_ADAPTER Adapter
143  )
144 {
145  NdisRawWritePortUshort(Adapter->IoBase + R_IM, 0);
146  return NDIS_STATUS_SUCCESS;
147 }
148 
149 USHORT
150 NTAPI
152  IN PRTL_ADAPTER Adapter,
153  OUT PBOOLEAN InterruptRecognized
154  )
155 {
156  USHORT interruptStatus;
157 
158  NdisRawReadPortUshort(Adapter->IoBase + R_IS, &interruptStatus);
159 
160  *InterruptRecognized = (interruptStatus & Adapter->InterruptMask) != 0;
161 
162  return (interruptStatus & Adapter->InterruptMask);
163 }
164 
165 VOID
166 NTAPI
168  IN PRTL_ADAPTER Adapter
169  )
170 {
171  NdisRawWritePortUshort(Adapter->IoBase + R_IS, Adapter->InterruptPending);
172 }
173 
174 VOID
175 NTAPI
177  IN PRTL_ADAPTER Adapter
178  )
179 {
180  UCHAR mediaState;
181 
182  NdisRawReadPortUchar(Adapter->IoBase + R_MS, &mediaState);
183  Adapter->MediaState = (mediaState & R_MS_LINKDWN) ? NdisMediaStateDisconnected :
185  Adapter->LinkSpeedMbps = (mediaState & R_MS_SPEED_10) ? 10 : 100;
186 }
187 
189 NTAPI
191  IN PRTL_ADAPTER Adapter
192  )
193 {
194  ULONG filterMask;
195 
196  filterMask = RC_VAL;
197 
198  if (Adapter->PacketFilter & NDIS_PACKET_TYPE_DIRECTED)
199  {
200  filterMask |= B_RC_APM;
201  }
202 
203  if (Adapter->PacketFilter & NDIS_PACKET_TYPE_MULTICAST)
204  {
205  filterMask |= B_RC_AM;
206  }
207 
208  if (Adapter->PacketFilter & NDIS_PACKET_TYPE_BROADCAST)
209  {
210  filterMask |= B_RC_AB;
211  }
212 
213  if (Adapter->PacketFilter & NDIS_PACKET_TYPE_PROMISCUOUS)
214  {
215  filterMask |= B_RC_AAP;
216  }
217 
218  NdisRawWritePortUlong(Adapter->IoBase + R_RC, filterMask);
219 
220  return NDIS_STATUS_SUCCESS;
221 }
222 
224 NTAPI
226  IN PRTL_ADAPTER Adapter,
227  IN UCHAR TxDesc,
229  IN ULONG Length
230  )
231 {
232  NdisRawWritePortUlong(Adapter->IoBase + R_TXSAD0 + (TxDesc * sizeof(ULONG)), PhysicalAddress);
233  NdisRawWritePortUlong(Adapter->IoBase + R_TXSTS0 + (TxDesc * sizeof(ULONG)), Length);
234  return NDIS_STATUS_SUCCESS;
235 }
#define B_CMD_RXE
Definition: rtlhw.h:48
NDIS_STATUS NTAPI NICSoftReset(IN PE1000_ADAPTER Adapter)
Definition: hardware.c:562
#define IN
Definition: typedefs.h:39
#define R_IS
Definition: rtlhw.h:54
#define B_CMD_RST
Definition: rtlhw.h:49
#define NdisRawWritePortUchar(Port, Data)
Definition: ndis.h:4230
NDIS_STATUS NTAPI NICApplyPacketFilter(IN PE1000_ADAPTER Adapter)
Definition: hardware.c:752
unsigned char * PUCHAR
Definition: retypes.h:3
ULONG NTAPI NICInterruptRecognized(IN PE1000_ADAPTER Adapter, OUT PBOOLEAN InterruptRecognized)
Definition: hardware.c:790
NDIS_STATUS NTAPI NICEnableTxRx(IN PE1000_ADAPTER Adapter)
Definition: hardware.c:605
#define NdisRawReadPortUshort(Port, Data)
Definition: ndis.h:4191
#define R_TXSTS0
Definition: rtlhw.h:26
#define R_IM
Definition: rtlhw.h:53
int NDIS_STATUS
Definition: ntddndis.h:471
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
NTSTATUS(* NTAPI)(IN PFILE_FULL_EA_INFORMATION EaBuffer, IN ULONG EaLength, OUT PULONG ErrorOffset)
Definition: IoEaTest.cpp:117
#define NdisGetPhysicalAddressHigh(PhysicalAddress)
Definition: ndis.h:3830
#define NdisRawWritePortUshort(Port, Data)
Definition: ndis.h:4248
#define R_TC
Definition: rtlhw.h:55
VOID NTAPI NICUpdateLinkStatus(IN PE1000_ADAPTER Adapter)
Definition: hardware.c:808
#define B_RC_AM
Definition: rtlhw.h:70
NDIS_STATUS NTAPI NICRemoveReceiveBuffer(IN PRTL_ADAPTER Adapter)
Definition: hardware.c:88
#define IEEE_802_ADDR_LENGTH
Definition: e1000hw.h:11
#define B_RC_AB
Definition: rtlhw.h:71
#define NDIS_PACKET_TYPE_MULTICAST
Definition: ndis.h:664
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:101
#define R_RC
Definition: rtlhw.h:67
#define R_MS_SPEED_10
Definition: rtlhw.h:83
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel) ?(CompletionRoutine !=NULL) :TRUE)
#define NDIS_STATUS_SUCCESS
Definition: ndis.h:346
_Must_inspect_result_ typedef _In_ PHYSICAL_ADDRESS PhysicalAddress
Definition: iotypes.h:1063
unsigned char UCHAR
Definition: xmlstorage.h:181
NDIS_STATUS NTAPI NICPowerOn(IN PE1000_ADAPTER Adapter)
Definition: hardware.c:540
char * PBOOLEAN
Definition: retypes.h:11
#define B_RC_AAP
Definition: rtlhw.h:68
#define R_MS_LINKDWN
Definition: rtlhw.h:82
NDIS_STATUS NTAPI NICApplyInterruptMask(IN PE1000_ADAPTER Adapter)
Definition: hardware.c:768
VOID NTAPI NICAcknowledgeInterrupts(IN PRTL_ADAPTER Adapter)
Definition: hardware.c:167
NDIS_STATUS NTAPI NICRegisterReceiveBuffer(IN PRTL_ADAPTER Adapter)
Definition: hardware.c:75
#define B_CMD_TXE
Definition: rtlhw.h:47
#define R_RXSA
Definition: rtlhw.h:34
NDIS_STATUS NTAPI NICTransmitPacket(IN PE1000_ADAPTER Adapter, IN PHYSICAL_ADDRESS PhysicalAddress, IN ULONG Length)
Definition: hardware.c:825
#define MAX_RESET_ATTEMPTS
Definition: e1000hw.h:15
VOID EXPORT NdisMSleep(IN ULONG MicrosecondsToSleep)
Definition: miniport.c:2928
#define R_CMD
Definition: rtlhw.h:45
unsigned short USHORT
Definition: pedump.c:61
#define R_MS
Definition: rtlhw.h:80
unsigned int UINT
Definition: ndis.h:50
#define NDIS_PACKET_TYPE_PROMISCUOUS
Definition: ndis.h:668
#define TC_VAL
Definition: nic.h:34
#define R_MAC
Definition: rtlhw.h:17
NDIS_STATUS NTAPI NICDisableInterrupts(IN PE1000_ADAPTER Adapter)
Definition: hardware.c:779
#define OUT
Definition: typedefs.h:40
unsigned int ULONG
Definition: retypes.h:1
#define RC_VAL
Definition: nic.h:31
#define NdisRawReadPortUchar(Port, Data)
Definition: ndis.h:4173
#define R_CFG1
Definition: rtlhw.h:78
NDIS_STATUS NTAPI NICGetPermanentMacAddress(IN PE1000_ADAPTER Adapter, OUT PUCHAR MacAddress)
Definition: hardware.c:692
#define B_RC_APM
Definition: rtlhw.h:69
#define NDIS_STATUS_FAILURE
Definition: ndis.h:465
#define NDIS_PACKET_TYPE_DIRECTED
Definition: ndis.h:663
#define NDIS_PACKET_TYPE_BROADCAST
Definition: ndis.h:666
#define R_TXSAD0
Definition: rtlhw.h:30
#define NdisRawWritePortUlong(Port, Data)
Definition: ndis.h:4239