ndisuio.h
Upload User: yanxuanwei
Upload Date: 2008-05-17
Package Size: 53k
Code Size: 15k
Category:

Driver Develop

Development Platform:

Visual C++

  1. /*++
  2. Copyright (c) 2000  Microsoft Corporation
  3. Module Name:
  4.     ndisuio.h
  5. Abstract:
  6.     Data structures, defines and function prototypes for NDISUIO.
  7. Environment:
  8.     Kernel mode only.
  9. Revision History:
  10.     arvindm     4/5/2000    Created
  11. --*/
  12. #ifndef __NDISUIO__H
  13. #define __NDISUIO__H
  14. #define NT_DEVICE_NAME          L"\Device\Ndisuio2"
  15. #define DOS_DEVICE_NAME         L"\DosDevices\Ndisuio2"
  16. //
  17. //  Abstract types
  18. //
  19. typedef NDIS_EVENT              NUIO_EVENT;
  20. //
  21. //  The Open Context represents an open of our device object.
  22. //  We allocate this on processing a BindAdapter from NDIS,
  23. //  and free it when all references (see below) to it are gone.
  24. //
  25. //  Binding/unbinding to an NDIS device:
  26. //
  27. //  On processing a BindAdapter call from NDIS, we set up a binding
  28. //  to the specified NDIS device (miniport). This binding is
  29. //  torn down when NDIS asks us to Unbind by calling
  30. //  our UnbindAdapter handler.
  31. //
  32. //  Receiving data:
  33. //
  34. //  While an NDIS binding exists, read IRPs are queued on this
  35. //  structure, to be processed when packets are received.
  36. //  If data arrives in the absense of a pended read IRP, we
  37. //  queue it, to the extent of one packet, i.e. we save the
  38. //  contents of the latest packet received. We fail read IRPs
  39. //  received when no NDIS binding exists (or is in the process
  40. //  of being torn down).
  41. //
  42. //  Sending data:
  43. //
  44. //  Write IRPs are used to send data. Each write IRP maps to
  45. //  a single NDIS packet. Packet send-completion is mapped to
  46. //  write IRP completion. We use NDIS 5.1 CancelSend to support
  47. //  write IRP cancellation. Write IRPs that arrive when we don't
  48. //  have an active NDIS binding are failed.
  49. //
  50. //  Reference count:
  51. //
  52. //  The following are long-lived references:
  53. //  OPEN_DEVICE ioctl (goes away on processing a Close IRP)
  54. //  Pended read IRPs
  55. //  Queued received packets
  56. //  Uncompleted write IRPs (outstanding sends)
  57. //  Existence of NDIS binding
  58. //
  59. typedef struct _NDISUIO_OPEN_CONTEXT
  60. {
  61.     LIST_ENTRY              Link;           // Link into global list
  62.     ULONG                   Flags;          // State information
  63.     ULONG                   RefCount;
  64.     NUIO_LOCK               Lock;
  65.     PFILE_OBJECT            pFileObject;    // Set on OPEN_DEVICE
  66.     NDIS_HANDLE             BindingHandle;
  67.     NDIS_HANDLE             SendPacketPool;
  68.     NDIS_HANDLE             SendBufferPool;
  69.     NDIS_HANDLE             RecvPacketPool;
  70.     NDIS_HANDLE             RecvBufferPool;
  71.     ULONG                   MacOptions;
  72.     ULONG                   MaxFrameSize;
  73.     LIST_ENTRY              PendedWrites;   // pended Write IRPs
  74.     ULONG                   PendedSendCount;
  75.     LIST_ENTRY              PendedReads;    // pended Read IRPs
  76.     ULONG                   PendedReadCount;
  77.     LIST_ENTRY              RecvPktQueue;   // queued rcv packets
  78.     ULONG                   RecvPktCount;
  79.     NET_DEVICE_POWER_STATE  PowerState;
  80.     NDIS_EVENT              PoweredUpEvent; // signalled iff PowerState is D0
  81.     NDIS_STRING             DeviceName;     // used in NdisOpenAdapter
  82.     NDIS_STRING DeviceDescr; // friendly name
  83.     NDIS_STATUS             BindStatus;     // for Open/CloseAdapter
  84.     NUIO_EVENT              BindEvent;      // for Open/CloseAdapter
  85.     BOOLEAN                 bRunningOnWin9x;// TRUE if Win98/SE/ME, FALSE if NT
  86.     ULONG                   oc_sig;         // Signature for sanity
  87. } NDISUIO_OPEN_CONTEXT, *PNDISUIO_OPEN_CONTEXT;
  88. #define oc_signature        'OiuN'
  89. //
  90. //  Definitions for Flags above.
  91. //
  92. #define NUIOO_BIND_IDLE             0x00000000
  93. #define NUIOO_BIND_OPENING          0x00000001
  94. #define NUIOO_BIND_FAILED           0x00000002
  95. #define NUIOO_BIND_ACTIVE           0x00000004
  96. #define NUIOO_BIND_CLOSING          0x00000008
  97. #define NUIOO_BIND_FLAGS            0x0000000F  // State of the binding
  98. #define NUIOO_OPEN_IDLE             0x00000000
  99. #define NUIOO_OPEN_ACTIVE           0x00000010
  100. #define NUIOO_OPEN_FLAGS            0x000000F0  // State of the I/O open
  101. #define NUIOO_RESET_IN_PROGRESS     0x00000100
  102. #define NUIOO_NOT_RESETTING         0x00000000
  103. #define NUIOO_RESET_FLAGS           0x00000100
  104. #define NUIOO_MEDIA_CONNECTED       0x00000000
  105. #define NUIOO_MEDIA_DISCONNECTED    0x00000200
  106. #define NUIOO_MEDIA_FLAGS           0x00000200
  107. #define NUIOO_READ_SERVICING        0x00100000  // Is the read service
  108.                                                 // routine running?
  109. #define NUIOO_READ_FLAGS            0x00100000
  110. #define NUIOO_UNBIND_RECEIVED       0x10000000  // Seen NDIS Unbind?
  111. #define NUIOO_UNBIND_FLAGS          0x10000000
  112. //
  113. //  Globals:
  114. //
  115. typedef struct _NDISUIO_GLOBALS
  116. {
  117.     PDRIVER_OBJECT          pDriverObject;
  118.     PDEVICE_OBJECT          ControlDeviceObject;
  119.     NDIS_HANDLE             NdisProtocolHandle;
  120.     USHORT                  EthType;            // frame type we are interested in
  121.     UCHAR                   PartialCancelId;    // for cancelling sends
  122.     ULONG                   LocalCancelId;
  123.     LIST_ENTRY              OpenList;           // of OPEN_CONTEXT structures
  124.     NUIO_LOCK               GlobalLock;         // to protect the above
  125.     NUIO_EVENT              BindsComplete;      // have we seen NetEventBindsComplete?
  126. } NDISUIO_GLOBALS, *PNDISUIO_GLOBALS;
  127. //
  128. //  The following are arranged in the way a little-endian processor
  129. //  would read 2 bytes off the wire.
  130. //
  131. #define NUIO_ETH_TYPE               0x8e88
  132. #define NUIO_8021P_TAG_TYPE         0x0081
  133. //
  134. //  NDIS Request context structure
  135. //
  136. typedef struct _NDISUIO_REQUEST
  137. {
  138.     NDIS_REQUEST            Request;
  139.     NUIO_EVENT              ReqEvent;
  140.     ULONG                   Status;
  141. } NDISUIO_REQUEST, *PNDISUIO_REQUEST;
  142. #define NUIOO_PACKET_FILTER  (NDIS_PACKET_TYPE_DIRECTED|    
  143.                               NDIS_PACKET_TYPE_MULTICAST|   
  144.                               NDIS_PACKET_TYPE_BROADCAST)
  145. //
  146. //  Send packet pool bounds
  147. //
  148. #define MIN_SEND_PACKET_POOL_SIZE    20
  149. #define MAX_SEND_PACKET_POOL_SIZE    400
  150. //
  151. //  ProtocolReserved in sent packets. We save a pointer to the IRP
  152. //  that generated the send.
  153. //
  154. //  The RefCount is used to determine when to free the packet back
  155. //  to its pool. It is used to synchronize between a thread completing
  156. //  a send and a thread attempting to cancel a send.
  157. //
  158. typedef struct _NUIO_SEND_PACKET_RSVD
  159. {
  160.     PIRP                    pIrp;
  161.     ULONG                   RefCount;
  162. } NUIO_SEND_PACKET_RSVD, *PNUIO_SEND_PACKET_RSVD;
  163. //
  164. //  Receive packet pool bounds
  165. //
  166. #define MIN_RECV_PACKET_POOL_SIZE    4
  167. #define MAX_RECV_PACKET_POOL_SIZE    20
  168. //
  169. //  Max receive packets we allow to be queued up
  170. //
  171. #define MAX_RECV_QUEUE_SIZE          4
  172. //
  173. //  ProtocolReserved in received packets: we link these
  174. //  packets up in a queue waiting for Read IRPs.
  175. //
  176. typedef struct _NUIO_RECV_PACKET_RSVD
  177. {
  178.     LIST_ENTRY              Link;
  179.     PNDIS_BUFFER            pOriginalBuffer;    // used if we had to partial-map
  180. } NUIO_RECV_PACKET_RSVD, *PNUIO_RECV_PACKET_RSVD;
  181. #define NUIO_MAC_ADDR_LEN            6
  182. #include <pshpack1.h>
  183. typedef struct _NDISUIO_ETH_HEADER
  184. {
  185.     UCHAR       DstAddr[NUIO_MAC_ADDR_LEN];
  186.     UCHAR       SrcAddr[NUIO_MAC_ADDR_LEN];
  187.     USHORT      EthType;
  188. } NDISUIO_ETH_HEADER;
  189. typedef struct _NDISUIO_ETH_HEADER UNALIGNED * PNDISUIO_ETH_HEADER;
  190. #include <poppack.h>
  191. extern NDISUIO_GLOBALS      Globals;
  192. #define NUIO_ALLOC_TAG      'oiuN'
  193. #ifndef NDIS51
  194. #define NdisGetPoolFromPacket(_Pkt) (_Pkt->Private.Pool)
  195. #endif
  196. //
  197. //  Prototypes.
  198. //
  199. NTSTATUS
  200. DriverEntry(
  201.     IN PDRIVER_OBJECT   pDriverObject,
  202.     IN PUNICODE_STRING  pRegistryPath
  203.     );
  204. VOID
  205. NdisuioUnload(
  206.     IN PDRIVER_OBJECT DriverObject
  207.     );
  208. NTSTATUS
  209. NdisuioOpen(
  210.     IN PDEVICE_OBJECT   pDeviceObject,
  211.     IN PIRP             pIrp
  212.     );
  213. NTSTATUS
  214. NdisuioClose(
  215.     IN PDEVICE_OBJECT   pDeviceObject,
  216.     IN PIRP             pIrp
  217.     );
  218. NTSTATUS
  219. NdisuioCleanup(
  220.     IN PDEVICE_OBJECT   pDeviceObject,
  221.     IN PIRP             pIrp
  222.     );
  223. NTSTATUS
  224. NdisuioIoControl(
  225.     IN PDEVICE_OBJECT   pDeviceObject,
  226.     IN PIRP             pIrp
  227.     );
  228. NTSTATUS
  229. ndisuioOpenDevice(
  230.     IN PUCHAR                   pDeviceName,
  231.     IN ULONG                    DeviceNameLength,
  232.     IN PFILE_OBJECT             pFileObject,
  233.     OUT PNDISUIO_OPEN_CONTEXT * ppOpenContext
  234.     );
  235. VOID
  236. ndisuioRefOpen(
  237.     IN PNDISUIO_OPEN_CONTEXT        pOpenContext
  238.     );
  239. VOID
  240. ndisuioDerefOpen(
  241.     IN PNDISUIO_OPEN_CONTEXT        pOpenContext
  242.     );
  243. #if DBG
  244. VOID
  245. ndisuioDbgRefOpen(
  246.     IN PNDISUIO_OPEN_CONTEXT        pOpenContext,
  247.     IN ULONG                        FileNumber,
  248.     IN ULONG                        LineNumber
  249.     );
  250. VOID
  251. ndisuioDbgDerefOpen(
  252.     IN PNDISUIO_OPEN_CONTEXT        pOpenContext,
  253.     IN ULONG                        FileNumber,
  254.     IN ULONG                        LineNumber
  255.     );
  256. #endif // DBG
  257. VOID
  258. NdisuioBindAdapter(
  259.     OUT PNDIS_STATUS                pStatus,
  260.     IN NDIS_HANDLE                  BindContext,
  261.     IN PNDIS_STRING                 DeviceName,
  262.     IN PVOID                        SystemSpecific1,
  263.     IN PVOID                        SystemSpecific2
  264.     );
  265. VOID
  266. NdisuioOpenAdapterComplete(
  267.     IN NDIS_HANDLE                  ProtocolBindingContext,
  268.     IN NDIS_STATUS                  Status,
  269.     IN NDIS_STATUS                  OpenErrorCode
  270.     );
  271. VOID
  272. NdisuioUnbindAdapter(
  273.     OUT PNDIS_STATUS                pStatus,
  274.     IN NDIS_HANDLE                  ProtocolBindingContext,
  275.     IN NDIS_HANDLE                  UnbindContext
  276.     );
  277. VOID
  278. NdisuioCloseAdapterComplete(
  279.     IN NDIS_HANDLE                  ProtocolBindingContext,
  280.     IN NDIS_STATUS                  Status
  281.     );
  282. NDIS_STATUS
  283. NdisuioPnPEventHandler(
  284.     IN NDIS_HANDLE                  ProtocolBindingContext,
  285.     IN PNET_PNP_EVENT               pNetPnPEvent
  286.     );
  287. VOID
  288. NdisuioProtocolUnloadHandler(
  289.     VOID
  290.     );
  291. NDIS_STATUS
  292. ndisuioCreateBinding(
  293.     IN PNDISUIO_OPEN_CONTEXT        pOpenContext,
  294.     IN PUCHAR                       pBindingInfo,
  295.     IN ULONG                        BindingInfoLength
  296.     );
  297. VOID
  298. ndisuioShutdownBinding(
  299.     IN PNDISUIO_OPEN_CONTEXT        pOpenContext
  300.     );
  301. VOID
  302. ndisuioFreeBindResources(
  303.     IN PNDISUIO_OPEN_CONTEXT        pOpenContext
  304.     );
  305. VOID
  306. ndisuioWaitForPendingIO(
  307.     IN PNDISUIO_OPEN_CONTEXT        pOpenContext,
  308.     IN BOOLEAN                      DoCancelReads
  309.     );
  310. VOID
  311. ndisuioDoProtocolUnload(
  312.     VOID
  313.     );
  314. NDIS_STATUS
  315. ndisuioDoRequest(
  316.     IN PNDISUIO_OPEN_CONTEXT        pOpenContext,
  317.     IN NDIS_REQUEST_TYPE            RequestType,
  318.     IN NDIS_OID                     Oid,
  319.     IN PVOID                        InformationBuffer,
  320.     IN UINT                         InformationBufferLength,
  321.     OUT PUINT                       pBytesProcessed
  322.     );
  323. NDIS_STATUS
  324. ndisuioValidateOpenAndDoRequest(
  325.     IN PNDISUIO_OPEN_CONTEXT        pOpenContext,
  326.     IN NDIS_REQUEST_TYPE            RequestType,
  327.     IN NDIS_OID                     Oid,
  328.     IN PVOID                        InformationBuffer,
  329.     IN UINT                         InformationBufferLength,
  330.     OUT PUINT                       pBytesProcessed,
  331.     IN BOOLEAN                      bWaitForPowerOn
  332.     );
  333. VOID
  334. NdisuioResetComplete(
  335.     IN NDIS_HANDLE                  ProtocolBindingContext,
  336.     IN NDIS_STATUS                  Status
  337.     );
  338. VOID
  339. NdisuioRequestComplete(
  340.     IN NDIS_HANDLE                  ProtocolBindingContext,
  341.     IN PNDIS_REQUEST                pNdisRequest,
  342.     IN NDIS_STATUS                  Status
  343.     );
  344. VOID
  345. NdisuioStatus(
  346.     IN NDIS_HANDLE                  ProtocolBindingContext,
  347.     IN NDIS_STATUS                  GeneralStatus,
  348.     IN PVOID                        StatusBuffer,
  349.     IN UINT                         StatusBufferSize
  350.     );
  351. VOID
  352. NdisuioStatusComplete(
  353.     IN NDIS_HANDLE                  ProtocolBindingContext
  354.     );
  355. NDIS_STATUS
  356. ndisuioQueryBinding(
  357.     IN PUCHAR                       pBuffer,
  358.     IN ULONG                        InputLength,
  359.     IN ULONG                        OutputLength,
  360.     OUT PULONG                      pBytesReturned
  361.     );
  362. PNDISUIO_OPEN_CONTEXT
  363. ndisuioLookupDevice(
  364.     IN PUCHAR                       pBindingInfo,
  365.     IN ULONG                        BindingInfoLength
  366.     );
  367. NDIS_STATUS
  368. ndisuioQueryOidValue(
  369.     IN  PNDISUIO_OPEN_CONTEXT       pOpenContext,
  370.     OUT PVOID                       pDataBuffer,
  371.     IN  ULONG                       BufferLength,
  372.     OUT PULONG                      pBytesWritten
  373.     );
  374. NDIS_STATUS
  375. ndisuioSetOidValue(
  376.     IN  PNDISUIO_OPEN_CONTEXT       pOpenContext,
  377.     OUT PVOID                       pDataBuffer,
  378.     IN  ULONG                       BufferLength
  379.     );
  380. NTSTATUS
  381. NdisuioRead(
  382.     IN PDEVICE_OBJECT               pDeviceObject,
  383.     IN PIRP                         pIrp
  384.     );
  385. VOID
  386. NdisuioCancelRead(
  387.     IN PDEVICE_OBJECT               pDeviceObject,
  388.     IN PIRP                         pIrp
  389.     );
  390. VOID
  391. ndisuioServiceReads(
  392.     IN PNDISUIO_OPEN_CONTEXT        pOpenContext
  393.     );
  394. NDIS_STATUS
  395. NdisuioReceive(
  396.     IN NDIS_HANDLE                  ProtocolBindingContext,
  397.     IN NDIS_HANDLE                  MacReceiveContext,
  398.     IN PVOID                        pHeaderBuffer,
  399.     IN UINT                         HeaderBufferSize,
  400.     IN PVOID                        pLookaheadBuffer,
  401.     IN UINT                         LookaheadBufferSize,
  402.     IN UINT                         PacketSize
  403.     );
  404. VOID
  405. NdisuioTransferDataComplete(
  406.     IN NDIS_HANDLE                  ProtocolBindingContext,
  407.     IN PNDIS_PACKET                 pNdisPacket,
  408.     IN NDIS_STATUS                  TransferStatus,
  409.     IN UINT                         BytesTransferred
  410.     );
  411. VOID
  412. NdisuioReceiveComplete(
  413.     IN NDIS_HANDLE                  ProtocolBindingContext
  414.     );
  415. INT
  416. NdisuioReceivePacket(
  417.     IN NDIS_HANDLE                  ProtocolBindingContext,
  418.     IN PNDIS_PACKET                 pNdisPacket
  419.     );
  420. VOID
  421. ndisuioShutdownBinding(
  422.     IN PNDISUIO_OPEN_CONTEXT        pOpenContext
  423.     );
  424. VOID
  425. ndisuioQueueReceivePacket(
  426.     IN PNDISUIO_OPEN_CONTEXT        pOpenContext,
  427.     IN PNDIS_PACKET                 pRcvPacket
  428.     );
  429. PNDIS_PACKET
  430. ndisuioAllocateReceivePacket(
  431.     IN PNDISUIO_OPEN_CONTEXT        pOpenContext,
  432.     IN UINT                         DataLength,
  433.     OUT PUCHAR *                    ppDataBuffer
  434.     );
  435. VOID
  436. ndisuioFreeReceivePacket(
  437.     IN PNDISUIO_OPEN_CONTEXT        pOpenContext,
  438.     IN PNDIS_PACKET                 pNdisPacket
  439.     );
  440. VOID
  441. ndisuioCancelPendingReads(
  442.     IN PNDISUIO_OPEN_CONTEXT        pOpenContext
  443.     );
  444. VOID
  445. ndisuioFlushReceiveQueue(
  446.     IN PNDISUIO_OPEN_CONTEXT            pOpenContext
  447.     );
  448. NTSTATUS
  449. NdisuioWrite(
  450.     IN PDEVICE_OBJECT       pDeviceObject,
  451.     IN PIRP                 pIrp
  452.     );
  453. VOID
  454. NdisuioCancelWrite(
  455.     IN PDEVICE_OBJECT               pDeviceObject,
  456.     IN PIRP                         pIrp
  457.     );
  458. VOID
  459. NdisuioSendComplete(
  460.     IN NDIS_HANDLE                  ProtocolBindingContext,
  461.     IN PNDIS_PACKET                 pNdisPacket,
  462.     IN NDIS_STATUS                  Status
  463.     );
  464. #endif // __NDISUIO__H