- Restore a value that is lost in MiniportReset when AddressingReset is TRUE
[reactos.git] / reactos / drivers / network / ndis / ndis / miniport.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS NDIS library
4 * FILE: ndis/miniport.c
5 * PURPOSE: Routines used by NDIS miniport drivers
6 * PROGRAMMERS: Casper S. Hornstrup (chorns@users.sourceforge.net)
7 * Vizzini (vizzini@plasmic.com)
8 * REVISIONS:
9 * CSH 01/08-2000 Created
10 * 20 Aug 2003 vizzini - DMA support
11 * 3 Oct 2003 vizzini - SendPackets support
12 */
13
14 #include "ndissys.h"
15 #include "efilter.h"
16
17 #include <buffer.h>
18
19 /*
20 * Define to 1 to get a debugger breakpoint at the end of NdisInitializeWrapper
21 * for each new miniport starting up
22 */
23 #define BREAK_ON_MINIPORT_INIT 0
24
25 /*
26 * This has to be big enough to hold the results of querying the Route value
27 * from the Linkage key. Please re-code me to determine this dynamically.
28 */
29 #define ROUTE_DATA_SIZE 256
30
31 /* Number of media we know */
32 #define MEDIA_ARRAY_SIZE 15
33
34 static NDIS_MEDIUM MediaArray[MEDIA_ARRAY_SIZE] =
35 {
36 NdisMedium802_3,
37 NdisMedium802_5,
38 NdisMediumFddi,
39 NdisMediumWan,
40 NdisMediumLocalTalk,
41 NdisMediumDix,
42 NdisMediumArcnetRaw,
43 NdisMediumArcnet878_2,
44 NdisMediumAtm,
45 NdisMediumWirelessWan,
46 NdisMediumIrda,
47 NdisMediumBpc,
48 NdisMediumCoWan,
49 NdisMedium1394,
50 NdisMediumMax
51 };
52
53 /* global list and lock of Miniports NDIS has registered */
54 LIST_ENTRY MiniportListHead;
55 KSPIN_LOCK MiniportListLock;
56
57 /* global list and lock of adapters NDIS has registered */
58 LIST_ENTRY AdapterListHead;
59 KSPIN_LOCK AdapterListLock;
60
61 VOID
62 MiniDisplayPacket(
63 PNDIS_PACKET Packet)
64 {
65 #if DBG
66 ULONG i, Length;
67 UCHAR Buffer[64];
68 if ((DebugTraceLevel & DEBUG_PACKET) > 0) {
69 Length = CopyPacketToBuffer(
70 Buffer,
71 Packet,
72 0,
73 64);
74
75 DbgPrint("*** PACKET START ***");
76
77 for (i = 0; i < Length; i++) {
78 if (i % 12 == 0)
79 DbgPrint("\n%04X ", i);
80 DbgPrint("%02X ", Buffer[i]);
81 }
82
83 DbgPrint("*** PACKET STOP ***\n");
84 }
85 #endif /* DBG */
86 }
87
88 VOID
89 MiniDisplayPacket2(
90 PVOID HeaderBuffer,
91 UINT HeaderBufferSize,
92 PVOID LookaheadBuffer,
93 UINT LookaheadBufferSize)
94 {
95 #if DBG
96 if ((DebugTraceLevel & DEBUG_PACKET) > 0) {
97 ULONG i, Length;
98 PUCHAR p;
99
100 DbgPrint("*** RECEIVE PACKET START ***\n");
101 DbgPrint("HEADER:");
102 p = HeaderBuffer;
103 for (i = 0; i < HeaderBufferSize; i++) {
104 if (i % 16 == 0)
105 DbgPrint("\n%04X ", i);
106 DbgPrint("%02X ", *p++);
107 }
108
109 DbgPrint("\nFRAME:");
110
111 p = LookaheadBuffer;
112 Length = (LookaheadBufferSize < 64)? LookaheadBufferSize : 64;
113 for (i = 0; i < Length; i++) {
114 if (i % 16 == 0)
115 DbgPrint("\n%04X ", i);
116 DbgPrint("%02X ", *p++);
117 }
118
119 DbgPrint("\n*** RECEIVE PACKET STOP ***\n");
120 }
121 #endif /* DBG */
122 }
123
124 PNDIS_MINIPORT_WORK_ITEM
125 MiniGetFirstWorkItem(
126 PLOGICAL_ADAPTER Adapter,
127 NDIS_WORK_ITEM_TYPE Type)
128 {
129 PNDIS_MINIPORT_WORK_ITEM CurrentEntry = Adapter->WorkQueueHead;
130
131 while (CurrentEntry)
132 {
133 if (CurrentEntry->WorkItemType == Type)
134 return CurrentEntry;
135
136 CurrentEntry = (PNDIS_MINIPORT_WORK_ITEM)CurrentEntry->Link.Next;
137 }
138
139 return NULL;
140 }
141
142 BOOLEAN
143 MiniIsBusy(
144 PLOGICAL_ADAPTER Adapter,
145 NDIS_WORK_ITEM_TYPE Type)
146 {
147 BOOLEAN Busy = FALSE;
148 KIRQL OldIrql;
149
150 KeAcquireSpinLock(&Adapter->NdisMiniportBlock.Lock, &OldIrql);
151
152 if (Type == NdisWorkItemRequest &&
153 (Adapter->NdisMiniportBlock.PendingRequest || MiniGetFirstWorkItem(Adapter, NdisWorkItemRequest)))
154 {
155 Busy = TRUE;
156 }
157 else if (Type == NdisWorkItemSend &&
158 (Adapter->NdisMiniportBlock.FirstPendingPacket || MiniGetFirstWorkItem(Adapter, NdisWorkItemSend)))
159 {
160 Busy = TRUE;
161 }
162 else if (Type == NdisWorkItemResetRequested &&
163 (Adapter->NdisMiniportBlock.ResetStatus == NDIS_STATUS_PENDING || MiniGetFirstWorkItem(Adapter, NdisWorkItemResetRequested)))
164 {
165 Busy = TRUE;
166 }
167
168 KeReleaseSpinLock(&Adapter->NdisMiniportBlock.Lock, OldIrql);
169
170 return Busy;
171 }
172
173 \f
174 VOID
175 MiniIndicateData(
176 PLOGICAL_ADAPTER Adapter,
177 NDIS_HANDLE MacReceiveContext,
178 PVOID HeaderBuffer,
179 UINT HeaderBufferSize,
180 PVOID LookaheadBuffer,
181 UINT LookaheadBufferSize,
182 UINT PacketSize)
183 /*
184 * FUNCTION: Indicate received data to bound protocols
185 * ARGUMENTS:
186 * Adapter = Pointer to logical adapter
187 * MacReceiveContext = MAC receive context handle
188 * HeaderBuffer = Pointer to header buffer
189 * HeaderBufferSize = Size of header buffer
190 * LookaheadBuffer = Pointer to lookahead buffer
191 * LookaheadBufferSize = Size of lookahead buffer
192 * PacketSize = Total size of received packet
193 */
194 {
195 KIRQL OldIrql;
196 PLIST_ENTRY CurrentEntry;
197 PADAPTER_BINDING AdapterBinding;
198
199 NDIS_DbgPrint(DEBUG_MINIPORT, ("Called. Adapter (0x%X) HeaderBuffer (0x%X) "
200 "HeaderBufferSize (0x%X) LookaheadBuffer (0x%X) LookaheadBufferSize (0x%X).\n",
201 Adapter, HeaderBuffer, HeaderBufferSize, LookaheadBuffer, LookaheadBufferSize));
202
203 MiniDisplayPacket2(HeaderBuffer, HeaderBufferSize, LookaheadBuffer, LookaheadBufferSize);
204
205 NDIS_DbgPrint(MAX_TRACE, ("acquiring miniport block lock\n"));
206 KeAcquireSpinLock(&Adapter->NdisMiniportBlock.Lock, &OldIrql);
207 {
208 CurrentEntry = Adapter->ProtocolListHead.Flink;
209 NDIS_DbgPrint(DEBUG_MINIPORT, ("CurrentEntry = %x\n", CurrentEntry));
210
211 if (CurrentEntry == &Adapter->ProtocolListHead)
212 {
213 NDIS_DbgPrint(MIN_TRACE, ("WARNING: No upper protocol layer.\n"));
214 }
215
216 while (CurrentEntry != &Adapter->ProtocolListHead)
217 {
218 AdapterBinding = CONTAINING_RECORD(CurrentEntry, ADAPTER_BINDING, AdapterListEntry);
219 NDIS_DbgPrint(DEBUG_MINIPORT, ("AdapterBinding = %x\n", AdapterBinding));
220
221 NDIS_DbgPrint
222 (MID_TRACE,
223 ("XXX (%x) %x %x %x %x %x %x %x XXX\n",
224 *AdapterBinding->ProtocolBinding->Chars.ReceiveHandler,
225 AdapterBinding->NdisOpenBlock.ProtocolBindingContext,
226 MacReceiveContext,
227 HeaderBuffer,
228 HeaderBufferSize,
229 LookaheadBuffer,
230 LookaheadBufferSize,
231 PacketSize));
232
233 /* call the receive handler */
234 (*AdapterBinding->ProtocolBinding->Chars.ReceiveHandler)(
235 AdapterBinding->NdisOpenBlock.ProtocolBindingContext,
236 MacReceiveContext,
237 HeaderBuffer,
238 HeaderBufferSize,
239 LookaheadBuffer,
240 LookaheadBufferSize,
241 PacketSize);
242
243 CurrentEntry = CurrentEntry->Flink;
244 }
245 }
246 KeReleaseSpinLock(&Adapter->NdisMiniportBlock.Lock, OldIrql);
247
248 NDIS_DbgPrint(MAX_TRACE, ("Leaving.\n"));
249 }
250
251 \f
252 VOID NTAPI
253 MiniIndicateReceivePacket(
254 IN NDIS_HANDLE MiniportAdapterHandle,
255 IN PPNDIS_PACKET PacketArray,
256 IN UINT NumberOfPackets)
257 /*
258 * FUNCTION: receives miniport packet array indications
259 * ARGUMENTS:
260 * MiniportAdapterHandle: Miniport handle for the adapter
261 * PacketArray: pointer to a list of packet pointers to indicate
262 * NumberOfPackets: number of packets to indicate
263 *
264 */
265 {
266 PLOGICAL_ADAPTER Adapter = MiniportAdapterHandle;
267 PLIST_ENTRY CurrentEntry;
268 PADAPTER_BINDING AdapterBinding;
269 KIRQL OldIrql;
270 UINT i;
271
272 KeAcquireSpinLock(&Adapter->NdisMiniportBlock.Lock, &OldIrql);
273
274 CurrentEntry = Adapter->ProtocolListHead.Flink;
275
276 while (CurrentEntry != &Adapter->ProtocolListHead)
277 {
278 AdapterBinding = CONTAINING_RECORD(CurrentEntry, ADAPTER_BINDING, AdapterListEntry);
279
280 for (i = 0; i < NumberOfPackets; i++)
281 {
282 if (AdapterBinding->ProtocolBinding->Chars.ReceivePacketHandler &&
283 NDIS_GET_PACKET_STATUS(PacketArray[i]) != NDIS_STATUS_RESOURCES)
284 {
285 (*AdapterBinding->ProtocolBinding->Chars.ReceivePacketHandler)(
286 AdapterBinding->NdisOpenBlock.ProtocolBindingContext,
287 PacketArray[i]);
288 }
289 else
290 {
291 UINT FirstBufferLength, TotalBufferLength, LookAheadSize, HeaderSize;
292 PNDIS_BUFFER NdisBuffer;
293 PVOID NdisBufferVA, LookAheadBuffer;
294 NDIS_STATUS NdisStatus;
295
296
297 NdisGetFirstBufferFromPacket(PacketArray[i],
298 &NdisBuffer,
299 &NdisBufferVA,
300 &FirstBufferLength,
301 &TotalBufferLength);
302
303 HeaderSize = NDIS_GET_PACKET_HEADER_SIZE(PacketArray[i]);
304
305 if (Adapter->NdisMiniportBlock.CurrentLookahead < (TotalBufferLength - HeaderSize))
306 {
307 LookAheadSize = Adapter->NdisMiniportBlock.CurrentLookahead;
308 }
309 else
310 {
311 LookAheadSize = TotalBufferLength - HeaderSize;
312 }
313
314
315 LookAheadBuffer = ExAllocatePool(NonPagedPool, LookAheadSize);
316 if (!LookAheadBuffer)
317 {
318 NDIS_DbgPrint(MIN_TRACE, ("Failed to allocate lookahead buffer!\n"));
319 return;
320 }
321
322 CopyBufferChainToBuffer(LookAheadBuffer,
323 NdisBuffer,
324 HeaderSize,
325 LookAheadSize);
326
327 NdisStatus = (*AdapterBinding->ProtocolBinding->Chars.ReceiveHandler)(
328 AdapterBinding->NdisOpenBlock.ProtocolBindingContext,
329 AdapterBinding->NdisOpenBlock.MacHandle,
330 NdisBufferVA,
331 HeaderSize,
332 LookAheadBuffer,
333 LookAheadSize,
334 TotalBufferLength - HeaderSize);
335
336 NDIS_SET_PACKET_STATUS(PacketArray[i], NdisStatus);
337
338 ExFreePool(LookAheadBuffer);
339 }
340 }
341
342 CurrentEntry = CurrentEntry->Flink;
343 }
344
345 KeReleaseSpinLock(&Adapter->NdisMiniportBlock.Lock, OldIrql);
346 }
347
348 \f
349 VOID NTAPI
350 MiniResetComplete(
351 IN NDIS_HANDLE MiniportAdapterHandle,
352 IN NDIS_STATUS Status,
353 IN BOOLEAN AddressingReset)
354 {
355 PLOGICAL_ADAPTER Adapter = MiniportAdapterHandle;
356 PLIST_ENTRY CurrentEntry;
357 PADAPTER_BINDING AdapterBinding;
358 KIRQL OldIrql;
359
360 if (AddressingReset)
361 MiniDoAddressingReset(Adapter);
362
363 NdisMIndicateStatus(Adapter, NDIS_STATUS_RESET_END, NULL, 0);
364 NdisMIndicateStatusComplete(Adapter);
365
366 KeAcquireSpinLock(&Adapter->NdisMiniportBlock.Lock, &OldIrql);
367
368 Adapter->NdisMiniportBlock.ResetStatus = Status;
369
370 CurrentEntry = Adapter->ProtocolListHead.Flink;
371
372 while (CurrentEntry != &Adapter->ProtocolListHead)
373 {
374 AdapterBinding = CONTAINING_RECORD(CurrentEntry, ADAPTER_BINDING, AdapterListEntry);
375
376 (*AdapterBinding->ProtocolBinding->Chars.ResetCompleteHandler)(
377 AdapterBinding->NdisOpenBlock.ProtocolBindingContext,
378 Status);
379
380 CurrentEntry = CurrentEntry->Flink;
381 }
382
383 KeReleaseSpinLock(&Adapter->NdisMiniportBlock.Lock, OldIrql);
384 }
385
386 VOID NTAPI
387 MiniRequestComplete(
388 IN NDIS_HANDLE MiniportAdapterHandle,
389 IN NDIS_STATUS Status)
390 {
391 PLOGICAL_ADAPTER Adapter = (PLOGICAL_ADAPTER)MiniportAdapterHandle;
392 PNDIS_REQUEST Request;
393 PNDIS_REQUEST_MAC_BLOCK MacBlock;
394 KIRQL OldIrql;
395
396 NDIS_DbgPrint(DEBUG_MINIPORT, ("Called.\n"));
397
398 KeRaiseIrql(DISPATCH_LEVEL, &OldIrql);
399
400 KeAcquireSpinLockAtDpcLevel(&Adapter->NdisMiniportBlock.Lock);
401 Request = Adapter->NdisMiniportBlock.PendingRequest;
402 KeReleaseSpinLockFromDpcLevel(&Adapter->NdisMiniportBlock.Lock);
403
404 MacBlock = (PNDIS_REQUEST_MAC_BLOCK)Request->MacReserved;
405
406 if( MacBlock->Binding->RequestCompleteHandler ) {
407 (*MacBlock->Binding->RequestCompleteHandler)(
408 MacBlock->Binding->ProtocolBindingContext,
409 Request,
410 Status);
411 }
412
413 KeAcquireSpinLockAtDpcLevel(&Adapter->NdisMiniportBlock.Lock);
414 Adapter->NdisMiniportBlock.PendingRequest = NULL;
415 KeReleaseSpinLockFromDpcLevel(&Adapter->NdisMiniportBlock.Lock);
416
417 KeLowerIrql(OldIrql);
418 }
419
420 VOID NTAPI
421 MiniSendComplete(
422 IN NDIS_HANDLE MiniportAdapterHandle,
423 IN PNDIS_PACKET Packet,
424 IN NDIS_STATUS Status)
425 /*
426 * FUNCTION: Forwards a message to the initiating protocol saying
427 * that a packet was handled
428 * ARGUMENTS:
429 * NdisAdapterHandle = Handle input to MiniportInitialize
430 * Packet = Pointer to NDIS packet that was sent
431 * Status = Status of send operation
432 */
433 {
434 PLOGICAL_ADAPTER Adapter = MiniportAdapterHandle;
435 PADAPTER_BINDING AdapterBinding;
436 KIRQL OldIrql;
437 PSCATTER_GATHER_LIST SGList;
438
439 NDIS_DbgPrint(DEBUG_MINIPORT, ("Called.\n"));
440
441 AdapterBinding = (PADAPTER_BINDING)Packet->Reserved[1];
442
443 KeRaiseIrql(DISPATCH_LEVEL, &OldIrql);
444
445 if (Adapter->NdisMiniportBlock.ScatterGatherListSize != 0)
446 {
447 NDIS_DbgPrint(MAX_TRACE, ("Freeing Scatter/Gather list\n"));
448
449 SGList = NDIS_PER_PACKET_INFO_FROM_PACKET(Packet,
450 ScatterGatherListPacketInfo);
451
452 Adapter->NdisMiniportBlock.SystemAdapterObject->
453 DmaOperations->PutScatterGatherList(
454 Adapter->NdisMiniportBlock.SystemAdapterObject,
455 SGList,
456 TRUE);
457
458 NDIS_PER_PACKET_INFO_FROM_PACKET(Packet,
459 ScatterGatherListPacketInfo) = NULL;
460 }
461
462 (*AdapterBinding->ProtocolBinding->Chars.SendCompleteHandler)(
463 AdapterBinding->NdisOpenBlock.ProtocolBindingContext,
464 Packet,
465 Status);
466
467 KeLowerIrql(OldIrql);
468 }
469
470
471 VOID NTAPI
472 MiniSendResourcesAvailable(
473 IN NDIS_HANDLE MiniportAdapterHandle)
474 {
475 /*
476 UNIMPLEMENTED
477 */
478 }
479
480
481 VOID NTAPI
482 MiniTransferDataComplete(
483 IN NDIS_HANDLE MiniportAdapterHandle,
484 IN PNDIS_PACKET Packet,
485 IN NDIS_STATUS Status,
486 IN UINT BytesTransferred)
487 {
488 PADAPTER_BINDING AdapterBinding;
489 KIRQL OldIrql;
490
491 NDIS_DbgPrint(DEBUG_MINIPORT, ("Called.\n"));
492
493 AdapterBinding = (PADAPTER_BINDING)Packet->Reserved[1];
494
495 KeRaiseIrql(DISPATCH_LEVEL, &OldIrql);
496 (*AdapterBinding->ProtocolBinding->Chars.TransferDataCompleteHandler)(
497 AdapterBinding->NdisOpenBlock.ProtocolBindingContext,
498 Packet,
499 Status,
500 BytesTransferred);
501 KeLowerIrql(OldIrql);
502 }
503
504 \f
505 BOOLEAN
506 MiniAdapterHasAddress(
507 PLOGICAL_ADAPTER Adapter,
508 PNDIS_PACKET Packet)
509 /*
510 * FUNCTION: Determines whether a packet has the same destination address as an adapter
511 * ARGUMENTS:
512 * Adapter = Pointer to logical adapter object
513 * Packet = Pointer to NDIS packet
514 * RETURNS:
515 * TRUE if the destination address is that of the adapter, FALSE if not
516 */
517 {
518 UINT Length;
519 PUCHAR Start1;
520 PUCHAR Start2;
521 PNDIS_BUFFER NdisBuffer;
522 UINT BufferLength;
523
524 NDIS_DbgPrint(DEBUG_MINIPORT, ("Called.\n"));
525
526 #if DBG
527 if(!Adapter)
528 {
529 NDIS_DbgPrint(MIN_TRACE, ("Adapter object was null\n"));
530 return FALSE;
531 }
532
533 if(!Packet)
534 {
535 NDIS_DbgPrint(MIN_TRACE, ("Packet was null\n"));
536 return FALSE;
537 }
538 #endif
539
540 NdisQueryPacket(Packet, NULL, NULL, &NdisBuffer, NULL);
541
542 if (!NdisBuffer)
543 {
544 NDIS_DbgPrint(MIN_TRACE, ("Packet contains no buffers.\n"));
545 return FALSE;
546 }
547
548 NdisQueryBuffer(NdisBuffer, (PVOID)&Start2, &BufferLength);
549
550 /* FIXME: Should handle fragmented packets */
551
552 switch (Adapter->NdisMiniportBlock.MediaType)
553 {
554 case NdisMedium802_3:
555 Length = ETH_LENGTH_OF_ADDRESS;
556 /* Destination address is the first field */
557 break;
558
559 default:
560 NDIS_DbgPrint(MIN_TRACE, ("Adapter has unsupported media type (0x%X).\n", Adapter->NdisMiniportBlock.MediaType));
561 return FALSE;
562 }
563
564 if (BufferLength < Length)
565 {
566 NDIS_DbgPrint(MIN_TRACE, ("Buffer is too small.\n"));
567 return FALSE;
568 }
569
570 Start1 = (PUCHAR)&Adapter->Address;
571 NDIS_DbgPrint(MAX_TRACE, ("packet address: %x:%x:%x:%x:%x:%x adapter address: %x:%x:%x:%x:%x:%x\n",
572 *((char *)Start1), *(((char *)Start1)+1), *(((char *)Start1)+2), *(((char *)Start1)+3), *(((char *)Start1)+4), *(((char *)Start1)+5),
573 *((char *)Start2), *(((char *)Start2)+1), *(((char *)Start2)+2), *(((char *)Start2)+3), *(((char *)Start2)+4), *(((char *)Start2)+5))
574 );
575
576 return (RtlCompareMemory((PVOID)Start1, (PVOID)Start2, Length) == Length);
577 }
578
579 \f
580 PLOGICAL_ADAPTER
581 MiniLocateDevice(
582 PNDIS_STRING AdapterName)
583 /*
584 * FUNCTION: Finds an adapter object by name
585 * ARGUMENTS:
586 * AdapterName = Pointer to name of adapter
587 * RETURNS:
588 * Pointer to logical adapter object, or NULL if none was found.
589 * If found, the adapter is referenced for the caller. The caller
590 * is responsible for dereferencing after use
591 */
592 {
593 KIRQL OldIrql;
594 PLIST_ENTRY CurrentEntry;
595 PLOGICAL_ADAPTER Adapter = 0;
596
597 ASSERT(AdapterName);
598
599 NDIS_DbgPrint(DEBUG_MINIPORT, ("Called.\n"));
600
601 if(IsListEmpty(&AdapterListHead))
602 {
603 NDIS_DbgPrint(MIN_TRACE, ("No registered miniports for protocol to bind to\n"));
604 return NULL;
605 }
606
607 KeAcquireSpinLock(&AdapterListLock, &OldIrql);
608 {
609 CurrentEntry = AdapterListHead.Flink;
610
611 while (CurrentEntry != &AdapterListHead)
612 {
613 Adapter = CONTAINING_RECORD(CurrentEntry, LOGICAL_ADAPTER, ListEntry);
614
615 ASSERT(Adapter);
616
617 NDIS_DbgPrint(DEBUG_MINIPORT, ("Examining adapter 0x%lx\n", Adapter));
618 NDIS_DbgPrint(DEBUG_MINIPORT, ("AdapterName = %wZ\n", AdapterName));
619 NDIS_DbgPrint(DEBUG_MINIPORT, ("DeviceName = %wZ\n", &Adapter->NdisMiniportBlock.MiniportName));
620
621 if (RtlCompareUnicodeString(AdapterName, &Adapter->NdisMiniportBlock.MiniportName, TRUE) == 0)
622 {
623 break;
624 }
625
626 Adapter = NULL;
627 CurrentEntry = CurrentEntry->Flink;
628 }
629 }
630 KeReleaseSpinLock(&AdapterListLock, OldIrql);
631
632 if(Adapter)
633 {
634 NDIS_DbgPrint(DEBUG_MINIPORT, ("Leaving. Adapter found at 0x%x\n", Adapter));
635 }
636 else
637 {
638 NDIS_DbgPrint(MIN_TRACE, ("Leaving (adapter not found for %wZ).\n", AdapterName));
639 }
640
641 return Adapter;
642 }
643
644 NDIS_STATUS
645 MiniSetInformation(
646 PLOGICAL_ADAPTER Adapter,
647 NDIS_OID Oid,
648 ULONG Size,
649 PVOID Buffer,
650 PULONG BytesRead)
651 {
652 NDIS_STATUS NdisStatus;
653 PNDIS_REQUEST NdisRequest;
654
655 NDIS_DbgPrint(DEBUG_MINIPORT, ("Called.\n"));
656
657 NdisRequest = ExAllocatePool(NonPagedPool, sizeof(NDIS_REQUEST));
658 if (!NdisRequest) {
659 NDIS_DbgPrint(MIN_TRACE, ("Insufficient resources\n"));
660 return NDIS_STATUS_RESOURCES;
661 }
662
663 RtlZeroMemory(NdisRequest, sizeof(NDIS_REQUEST));
664
665 NdisRequest->RequestType = NdisRequestSetInformation;
666 NdisRequest->DATA.SET_INFORMATION.Oid = Oid;
667 NdisRequest->DATA.SET_INFORMATION.InformationBuffer = Buffer;
668 NdisRequest->DATA.SET_INFORMATION.InformationBufferLength = Size;
669
670 NdisStatus = MiniDoRequest(Adapter, NdisRequest);
671
672 /* FIXME: Wait in pending case! */
673
674 ASSERT(NdisStatus != NDIS_STATUS_PENDING);
675
676 *BytesRead = NdisRequest->DATA.SET_INFORMATION.BytesRead;
677
678 ExFreePool(NdisRequest);
679
680 return NdisStatus;
681 }
682
683 NDIS_STATUS
684 MiniQueryInformation(
685 PLOGICAL_ADAPTER Adapter,
686 NDIS_OID Oid,
687 ULONG Size,
688 PVOID Buffer,
689 PULONG BytesWritten)
690 /*
691 * FUNCTION: Queries a logical adapter for properties
692 * ARGUMENTS:
693 * Adapter = Pointer to the logical adapter object to query
694 * Oid = Specifies the Object ID to query for
695 * Size = Size of the passed buffer
696 * Buffer = Buffer for the output
697 * BytesWritten = Address of buffer to place number of bytes written
698 * RETURNS:
699 * Status of operation
700 */
701 {
702 NDIS_STATUS NdisStatus;
703 PNDIS_REQUEST NdisRequest;
704
705 NDIS_DbgPrint(DEBUG_MINIPORT, ("Called.\n"));
706
707 NdisRequest = ExAllocatePool(NonPagedPool, sizeof(NDIS_REQUEST));
708 if (!NdisRequest) {
709 NDIS_DbgPrint(MIN_TRACE, ("Insufficient resources\n"));
710 return NDIS_STATUS_RESOURCES;
711 }
712
713 RtlZeroMemory(NdisRequest, sizeof(NDIS_REQUEST));
714
715 NdisRequest->RequestType = NdisRequestQueryInformation;
716 NdisRequest->DATA.QUERY_INFORMATION.Oid = Oid;
717 NdisRequest->DATA.QUERY_INFORMATION.InformationBuffer = Buffer;
718 NdisRequest->DATA.QUERY_INFORMATION.InformationBufferLength = Size;
719
720 NdisStatus = MiniDoRequest(Adapter, NdisRequest);
721
722 /* FIXME: Wait in pending case! */
723
724 ASSERT(NdisStatus != NDIS_STATUS_PENDING);
725
726 *BytesWritten = NdisRequest->DATA.QUERY_INFORMATION.BytesWritten;
727
728 ExFreePool(NdisRequest);
729
730 return NdisStatus;
731 }
732
733 BOOLEAN
734 MiniCheckForHang( PLOGICAL_ADAPTER Adapter )
735 /*
736 * FUNCTION: Checks to see if the miniport is hung
737 * ARGUMENTS:
738 * Adapter = Pointer to the logical adapter object
739 * RETURNS:
740 * TRUE if the miniport is hung
741 * FALSE if the miniport is not hung
742 */
743 {
744 BOOLEAN Ret = FALSE;
745 KIRQL OldIrql;
746
747 KeRaiseIrql(DISPATCH_LEVEL, &OldIrql);
748 if (Adapter->NdisMiniportBlock.DriverHandle->MiniportCharacteristics.CheckForHangHandler)
749 Ret = (*Adapter->NdisMiniportBlock.DriverHandle->MiniportCharacteristics.CheckForHangHandler)(
750 Adapter->NdisMiniportBlock.MiniportAdapterContext);
751 KeLowerIrql(OldIrql);
752
753 return Ret;
754 }
755
756 VOID
757 MiniDoAddressingReset(PLOGICAL_ADAPTER Adapter)
758 {
759 ULONG BytesRead;
760
761 MiniSetInformation(Adapter,
762 OID_GEN_CURRENT_LOOKAHEAD,
763 sizeof(ULONG),
764 &Adapter->NdisMiniportBlock.CurrentLookahead,
765 &BytesRead);
766
767 /* FIXME: Set more stuff */
768 }
769
770 NDIS_STATUS
771 MiniReset(
772 PLOGICAL_ADAPTER Adapter)
773 /*
774 * FUNCTION: Resets the miniport
775 * ARGUMENTS:
776 * Adapter = Pointer to the logical adapter object
777 * RETURNS:
778 * Status of the operation
779 */
780 {
781 NDIS_STATUS Status;
782 KIRQL OldIrql;
783 BOOLEAN AddressingReset = TRUE;
784
785 if (MiniIsBusy(Adapter, NdisWorkItemResetRequested)) {
786 MiniQueueWorkItem(Adapter, NdisWorkItemResetRequested, NULL, FALSE);
787 return NDIS_STATUS_PENDING;
788 }
789
790 NdisMIndicateStatus(Adapter, NDIS_STATUS_RESET_START, NULL, 0);
791 NdisMIndicateStatusComplete(Adapter);
792
793 KeRaiseIrql(DISPATCH_LEVEL, &OldIrql);
794 Status = (*Adapter->NdisMiniportBlock.DriverHandle->MiniportCharacteristics.ResetHandler)(
795 Adapter->NdisMiniportBlock.MiniportAdapterContext,
796 &AddressingReset);
797
798 KeAcquireSpinLockAtDpcLevel(&Adapter->NdisMiniportBlock.Lock);
799 Adapter->NdisMiniportBlock.ResetStatus = Status;
800 KeReleaseSpinLockFromDpcLevel(&Adapter->NdisMiniportBlock.Lock);
801
802 KeLowerIrql(OldIrql);
803
804 if (Status != NDIS_STATUS_PENDING) {
805 if (AddressingReset)
806 MiniDoAddressingReset(Adapter);
807
808 NdisMIndicateStatus(Adapter, NDIS_STATUS_RESET_END, NULL, 0);
809 NdisMIndicateStatusComplete(Adapter);
810 }
811
812 return Status;
813 }
814
815 VOID NTAPI
816 MiniportHangDpc(
817 PKDPC Dpc,
818 PVOID DeferredContext,
819 PVOID SystemArgument1,
820 PVOID SystemArgument2)
821 {
822 PLOGICAL_ADAPTER Adapter = DeferredContext;
823
824 if (MiniCheckForHang(Adapter)) {
825 NDIS_DbgPrint(MIN_TRACE, ("Miniport detected adapter hang\n"));
826 MiniReset(Adapter);
827 }
828 }
829
830 \f
831 VOID
832 FASTCALL
833 MiniQueueWorkItem(
834 PLOGICAL_ADAPTER Adapter,
835 NDIS_WORK_ITEM_TYPE WorkItemType,
836 PVOID WorkItemContext,
837 BOOLEAN Top)
838 /*
839 * FUNCTION: Queues a work item for execution at a later time
840 * ARGUMENTS:
841 * Adapter = Pointer to the logical adapter object to queue work item on
842 * WorkItemType = Type of work item to queue
843 * WorkItemContext = Pointer to context information for work item
844 * RETURNS:
845 * Status of operation
846 */
847 {
848 PNDIS_MINIPORT_WORK_ITEM MiniportWorkItem;
849 PIO_WORKITEM IoWorkItem;
850 KIRQL OldIrql;
851
852 NDIS_DbgPrint(MAX_TRACE, ("Called.\n"));
853
854 ASSERT(Adapter);
855
856 KeAcquireSpinLock(&Adapter->NdisMiniportBlock.Lock, &OldIrql);
857 if (Top)
858 {
859 if (WorkItemType == NdisWorkItemSend)
860 {
861 NDIS_DbgPrint(MIN_TRACE, ("Requeuing failed packet (%x).\n", WorkItemContext));
862 Adapter->NdisMiniportBlock.FirstPendingPacket = WorkItemContext;
863 }
864 else
865 {
866 //This should never happen
867 ASSERT(FALSE);
868 }
869 }
870 else
871 {
872 MiniportWorkItem = ExAllocatePool(NonPagedPool, sizeof(NDIS_MINIPORT_WORK_ITEM));
873 if (!MiniportWorkItem)
874 {
875 KeReleaseSpinLock(&Adapter->NdisMiniportBlock.Lock, OldIrql);
876 NDIS_DbgPrint(MIN_TRACE, ("Insufficient resources.\n"));
877 return;
878 }
879
880 MiniportWorkItem->WorkItemType = WorkItemType;
881 MiniportWorkItem->WorkItemContext = WorkItemContext;
882
883 /* safe due to adapter lock held */
884 MiniportWorkItem->Link.Next = NULL;
885 if (!Adapter->WorkQueueHead)
886 {
887 Adapter->WorkQueueHead = MiniportWorkItem;
888 Adapter->WorkQueueTail = MiniportWorkItem;
889 }
890 else
891 {
892 Adapter->WorkQueueTail->Link.Next = (PSINGLE_LIST_ENTRY)MiniportWorkItem;
893 Adapter->WorkQueueTail = MiniportWorkItem;
894 }
895 }
896
897 IoWorkItem = IoAllocateWorkItem(Adapter->NdisMiniportBlock.DeviceObject);
898 if (IoWorkItem)
899 IoQueueWorkItem(IoWorkItem, MiniportWorker, DelayedWorkQueue, IoWorkItem);
900
901 KeReleaseSpinLock(&Adapter->NdisMiniportBlock.Lock, OldIrql);
902 }
903
904 \f
905 NDIS_STATUS
906 FASTCALL
907 MiniDequeueWorkItem(
908 PLOGICAL_ADAPTER Adapter,
909 NDIS_WORK_ITEM_TYPE *WorkItemType,
910 PVOID *WorkItemContext)
911 /*
912 * FUNCTION: Dequeues a work item from the work queue of a logical adapter
913 * ARGUMENTS:
914 * Adapter = Pointer to the logical adapter object to dequeue work item from
915 * AdapterBinding = Address of buffer for adapter binding for this request
916 * WorkItemType = Address of buffer for work item type
917 * WorkItemContext = Address of buffer for pointer to context information
918 * NOTES:
919 * Adapter lock must be held when called
920 * RETURNS:
921 * Status of operation
922 */
923 {
924 PNDIS_MINIPORT_WORK_ITEM MiniportWorkItem;
925 PNDIS_PACKET Packet;
926
927 NDIS_DbgPrint(MAX_TRACE, ("Called.\n"));
928
929 MiniportWorkItem = Adapter->WorkQueueHead;
930
931 if ((Packet = Adapter->NdisMiniportBlock.FirstPendingPacket))
932 {
933 Adapter->NdisMiniportBlock.FirstPendingPacket = NULL;
934
935 *WorkItemType = NdisWorkItemSend;
936 *WorkItemContext = Packet;
937
938 return NDIS_STATUS_SUCCESS;
939 }
940 else if (MiniportWorkItem)
941 {
942 /* safe due to adapter lock held */
943 Adapter->WorkQueueHead = (PNDIS_MINIPORT_WORK_ITEM)MiniportWorkItem->Link.Next;
944
945 if (MiniportWorkItem == Adapter->WorkQueueTail)
946 Adapter->WorkQueueTail = NULL;
947
948 *WorkItemType = MiniportWorkItem->WorkItemType;
949 *WorkItemContext = MiniportWorkItem->WorkItemContext;
950
951 ExFreePool(MiniportWorkItem);
952
953 return NDIS_STATUS_SUCCESS;
954 }
955 else
956 {
957 NDIS_DbgPrint(MIN_TRACE, ("No work item to dequeue\n"));
958
959 return NDIS_STATUS_FAILURE;
960 }
961 }
962
963 \f
964 NDIS_STATUS
965 MiniDoRequest(
966 PLOGICAL_ADAPTER Adapter,
967 PNDIS_REQUEST NdisRequest)
968 /*
969 * FUNCTION: Sends a request to a miniport
970 * ARGUMENTS:
971 * AdapterBinding = Pointer to binding used in the request
972 * NdisRequest = Pointer to NDIS request structure describing request
973 * RETURNS:
974 * Status of operation
975 */
976 {
977 NDIS_STATUS Status;
978 KIRQL OldIrql;
979 NDIS_DbgPrint(DEBUG_MINIPORT, ("Called.\n"));
980
981 KeRaiseIrql(DISPATCH_LEVEL, &OldIrql);
982
983 KeAcquireSpinLockAtDpcLevel(&Adapter->NdisMiniportBlock.Lock);
984 Adapter->NdisMiniportBlock.PendingRequest = NdisRequest;
985 KeReleaseSpinLockFromDpcLevel(&Adapter->NdisMiniportBlock.Lock);
986
987 if (!Adapter->NdisMiniportBlock.DriverHandle->MiniportCharacteristics.CoRequestHandler)
988 {
989 switch (NdisRequest->RequestType)
990 {
991 case NdisRequestQueryInformation:
992 Status = (*Adapter->NdisMiniportBlock.DriverHandle->MiniportCharacteristics.QueryInformationHandler)(
993 Adapter->NdisMiniportBlock.MiniportAdapterContext,
994 NdisRequest->DATA.QUERY_INFORMATION.Oid,
995 NdisRequest->DATA.QUERY_INFORMATION.InformationBuffer,
996 NdisRequest->DATA.QUERY_INFORMATION.InformationBufferLength,
997 (PULONG)&NdisRequest->DATA.QUERY_INFORMATION.BytesWritten,
998 (PULONG)&NdisRequest->DATA.QUERY_INFORMATION.BytesNeeded);
999 break;
1000
1001 case NdisRequestSetInformation:
1002 Status = (*Adapter->NdisMiniportBlock.DriverHandle->MiniportCharacteristics.SetInformationHandler)(
1003 Adapter->NdisMiniportBlock.MiniportAdapterContext,
1004 NdisRequest->DATA.SET_INFORMATION.Oid,
1005 NdisRequest->DATA.SET_INFORMATION.InformationBuffer,
1006 NdisRequest->DATA.SET_INFORMATION.InformationBufferLength,
1007 (PULONG)&NdisRequest->DATA.SET_INFORMATION.BytesRead,
1008 (PULONG)&NdisRequest->DATA.SET_INFORMATION.BytesNeeded);
1009 break;
1010
1011 default:
1012 NDIS_DbgPrint(MIN_TRACE, ("Bad request type\n"));
1013 Status = NDIS_STATUS_FAILURE;
1014 }
1015 }
1016 else
1017 {
1018 Status = (*Adapter->NdisMiniportBlock.DriverHandle->MiniportCharacteristics.CoRequestHandler)(
1019 Adapter->NdisMiniportBlock.MiniportAdapterContext,
1020 NULL, /* FIXME */
1021 NdisRequest);
1022 }
1023
1024 if (Status != NDIS_STATUS_PENDING) {
1025 KeAcquireSpinLockAtDpcLevel(&Adapter->NdisMiniportBlock.Lock);
1026 Adapter->NdisMiniportBlock.PendingRequest = NULL;
1027 KeReleaseSpinLockFromDpcLevel(&Adapter->NdisMiniportBlock.Lock);
1028 }
1029
1030 KeLowerIrql(OldIrql);
1031 return Status;
1032 }
1033
1034 \f
1035 /*
1036 * @implemented
1037 */
1038 #undef NdisMSetInformationComplete
1039 VOID
1040 EXPORT
1041 NdisMSetInformationComplete(
1042 IN NDIS_HANDLE MiniportAdapterHandle,
1043 IN NDIS_STATUS Status)
1044 {
1045 PLOGICAL_ADAPTER Adapter =
1046 (PLOGICAL_ADAPTER)MiniportAdapterHandle;
1047 KIRQL OldIrql;
1048 ASSERT(Adapter);
1049 KeRaiseIrql(DISPATCH_LEVEL, &OldIrql);
1050 if (Adapter->NdisMiniportBlock.SetCompleteHandler)
1051 (Adapter->NdisMiniportBlock.SetCompleteHandler)(MiniportAdapterHandle, Status);
1052 KeLowerIrql(OldIrql);
1053 }
1054
1055 \f
1056 /*
1057 * @implemented
1058 */
1059 #undef NdisMQueryInformationComplete
1060 VOID
1061 EXPORT
1062 NdisMQueryInformationComplete(
1063 IN NDIS_HANDLE MiniportAdapterHandle,
1064 IN NDIS_STATUS Status)
1065 {
1066 PLOGICAL_ADAPTER Adapter =
1067 (PLOGICAL_ADAPTER)MiniportAdapterHandle;
1068 KIRQL OldIrql;
1069 ASSERT(Adapter);
1070 KeRaiseIrql(DISPATCH_LEVEL, &OldIrql);
1071 if( Adapter->NdisMiniportBlock.QueryCompleteHandler )
1072 (Adapter->NdisMiniportBlock.QueryCompleteHandler)(MiniportAdapterHandle, Status);
1073 KeLowerIrql(OldIrql);
1074 }
1075
1076 VOID
1077 NTAPI
1078 MiniportWorker(IN PDEVICE_OBJECT DeviceObject, IN PVOID Context)
1079 {
1080 PLOGICAL_ADAPTER Adapter = DeviceObject->DeviceExtension;
1081 KIRQL OldIrql, RaiseOldIrql;
1082 NDIS_STATUS NdisStatus;
1083 PVOID WorkItemContext;
1084 NDIS_WORK_ITEM_TYPE WorkItemType;
1085 BOOLEAN AddressingReset;
1086
1087 IoFreeWorkItem((PIO_WORKITEM)Context);
1088
1089 KeAcquireSpinLock(&Adapter->NdisMiniportBlock.Lock, &OldIrql);
1090
1091 NdisStatus =
1092 MiniDequeueWorkItem
1093 (Adapter, &WorkItemType, &WorkItemContext);
1094
1095 KeReleaseSpinLock(&Adapter->NdisMiniportBlock.Lock, OldIrql);
1096
1097 if (NdisStatus == NDIS_STATUS_SUCCESS)
1098 {
1099 switch (WorkItemType)
1100 {
1101 case NdisWorkItemSend:
1102 /*
1103 * called by ProSend when protocols want to send packets to the miniport
1104 */
1105
1106 if(Adapter->NdisMiniportBlock.DriverHandle->MiniportCharacteristics.SendPacketsHandler)
1107 {
1108 if(Adapter->NdisMiniportBlock.Flags & NDIS_ATTRIBUTE_DESERIALIZE)
1109 {
1110 NDIS_DbgPrint(MAX_TRACE, ("Calling miniport's SendPackets handler\n"));
1111 (*Adapter->NdisMiniportBlock.DriverHandle->MiniportCharacteristics.SendPacketsHandler)(
1112 Adapter->NdisMiniportBlock.MiniportAdapterContext, (PPNDIS_PACKET)&WorkItemContext, 1);
1113 NdisStatus = NDIS_STATUS_PENDING;
1114 }
1115 else
1116 {
1117 /* SendPackets is called at DISPATCH_LEVEL for all serialized miniports */
1118 KeRaiseIrql(DISPATCH_LEVEL, &RaiseOldIrql);
1119 {
1120 NDIS_DbgPrint(MAX_TRACE, ("Calling miniport's SendPackets handler\n"));
1121 (*Adapter->NdisMiniportBlock.DriverHandle->MiniportCharacteristics.SendPacketsHandler)(
1122 Adapter->NdisMiniportBlock.MiniportAdapterContext, (PPNDIS_PACKET)&WorkItemContext, 1);
1123 }
1124 KeLowerIrql(RaiseOldIrql);
1125
1126 NdisStatus = NDIS_GET_PACKET_STATUS((PNDIS_PACKET)WorkItemContext);
1127 if( NdisStatus == NDIS_STATUS_RESOURCES ) {
1128 MiniQueueWorkItem(Adapter, WorkItemType, WorkItemContext, TRUE);
1129 break;
1130 }
1131 }
1132 }
1133 else
1134 {
1135 if(Adapter->NdisMiniportBlock.Flags & NDIS_ATTRIBUTE_DESERIALIZE)
1136 {
1137 NDIS_DbgPrint(MAX_TRACE, ("Calling miniport's Send handler\n"));
1138 NdisStatus = (*Adapter->NdisMiniportBlock.DriverHandle->MiniportCharacteristics.SendHandler)(
1139 Adapter->NdisMiniportBlock.MiniportAdapterContext, (PNDIS_PACKET)WorkItemContext,
1140 ((PNDIS_PACKET)WorkItemContext)->Private.Flags);
1141 NDIS_DbgPrint(MAX_TRACE, ("back from miniport's send handler\n"));
1142 }
1143 else
1144 {
1145 /* Send is called at DISPATCH_LEVEL for all serialized miniports */
1146 KeRaiseIrql(DISPATCH_LEVEL, &RaiseOldIrql);
1147 NDIS_DbgPrint(MAX_TRACE, ("Calling miniport's Send handler\n"));
1148 NdisStatus = (*Adapter->NdisMiniportBlock.DriverHandle->MiniportCharacteristics.SendHandler)(
1149 Adapter->NdisMiniportBlock.MiniportAdapterContext, (PNDIS_PACKET)WorkItemContext,
1150 ((PNDIS_PACKET)WorkItemContext)->Private.Flags);
1151 NDIS_DbgPrint(MAX_TRACE, ("back from miniport's send handler\n"));
1152 KeLowerIrql(RaiseOldIrql);
1153 if( NdisStatus == NDIS_STATUS_RESOURCES ) {
1154 MiniQueueWorkItem(Adapter, WorkItemType, WorkItemContext, TRUE);
1155 break;
1156 }
1157 }
1158 }
1159
1160 if( NdisStatus != NDIS_STATUS_PENDING ) {
1161 MiniSendComplete
1162 ( Adapter, (PNDIS_PACKET)WorkItemContext, NdisStatus );
1163 }
1164 break;
1165
1166 case NdisWorkItemSendLoopback:
1167 /*
1168 * called by ProSend when protocols want to send loopback packets
1169 */
1170 /* XXX atm ProIndicatePacket sends a packet up via the loopback adapter only */
1171 NdisStatus = ProIndicatePacket(Adapter, (PNDIS_PACKET)WorkItemContext);
1172
1173 if( NdisStatus != NDIS_STATUS_PENDING )
1174 MiniSendComplete((NDIS_HANDLE)Adapter, (PNDIS_PACKET)WorkItemContext, NdisStatus);
1175 break;
1176
1177 case NdisWorkItemReturnPackets:
1178 break;
1179
1180 case NdisWorkItemResetRequested:
1181 NdisMIndicateStatus(Adapter, NDIS_STATUS_RESET_START, NULL, 0);
1182 NdisMIndicateStatusComplete(Adapter);
1183
1184 KeRaiseIrql(DISPATCH_LEVEL, &OldIrql);
1185 NdisStatus = (*Adapter->NdisMiniportBlock.DriverHandle->MiniportCharacteristics.ResetHandler)(
1186 Adapter->NdisMiniportBlock.MiniportAdapterContext,
1187 &AddressingReset);
1188
1189 KeAcquireSpinLockAtDpcLevel(&Adapter->NdisMiniportBlock.Lock);
1190 Adapter->NdisMiniportBlock.ResetStatus = NdisStatus;
1191 KeReleaseSpinLockFromDpcLevel(&Adapter->NdisMiniportBlock.Lock);
1192
1193 KeLowerIrql(OldIrql);
1194
1195 if (NdisStatus != NDIS_STATUS_PENDING)
1196 MiniResetComplete(Adapter, NdisStatus, AddressingReset);
1197 break;
1198
1199 case NdisWorkItemResetInProgress:
1200 break;
1201
1202 case NdisWorkItemMiniportCallback:
1203 break;
1204
1205 case NdisWorkItemRequest:
1206 NdisStatus = MiniDoRequest(Adapter, (PNDIS_REQUEST)WorkItemContext);
1207
1208 if (NdisStatus == NDIS_STATUS_PENDING)
1209 break;
1210
1211 switch (((PNDIS_REQUEST)WorkItemContext)->RequestType)
1212 {
1213 case NdisRequestQueryInformation:
1214 NdisMQueryInformationComplete((NDIS_HANDLE)Adapter, NdisStatus);
1215 break;
1216
1217 case NdisRequestSetInformation:
1218 NdisMSetInformationComplete((NDIS_HANDLE)Adapter, NdisStatus);
1219 break;
1220
1221 default:
1222 NDIS_DbgPrint(MIN_TRACE, ("Unknown NDIS request type.\n"));
1223 break;
1224 }
1225 break;
1226
1227 default:
1228 NDIS_DbgPrint(MIN_TRACE, ("Unknown NDIS work item type (%d).\n", WorkItemType));
1229 break;
1230 }
1231 }
1232 }
1233
1234
1235 \f
1236 VOID
1237 NTAPI
1238 MiniStatus(
1239 IN NDIS_HANDLE MiniportHandle,
1240 IN NDIS_STATUS GeneralStatus,
1241 IN PVOID StatusBuffer,
1242 IN UINT StatusBufferSize)
1243 {
1244 PLOGICAL_ADAPTER Adapter = MiniportHandle;
1245 PLIST_ENTRY CurrentEntry;
1246 PADAPTER_BINDING AdapterBinding;
1247 KIRQL OldIrql;
1248
1249 KeAcquireSpinLock(&Adapter->NdisMiniportBlock.Lock, &OldIrql);
1250
1251 CurrentEntry = Adapter->ProtocolListHead.Flink;
1252
1253 while (CurrentEntry != &Adapter->ProtocolListHead)
1254 {
1255 AdapterBinding = CONTAINING_RECORD(CurrentEntry, ADAPTER_BINDING, AdapterListEntry);
1256
1257 (*AdapterBinding->ProtocolBinding->Chars.StatusHandler)(
1258 AdapterBinding->NdisOpenBlock.ProtocolBindingContext,
1259 GeneralStatus,
1260 StatusBuffer,
1261 StatusBufferSize);
1262
1263 CurrentEntry = CurrentEntry->Flink;
1264 }
1265
1266 KeReleaseSpinLock(&Adapter->NdisMiniportBlock.Lock, OldIrql);
1267 }
1268
1269 \f
1270 VOID
1271 NTAPI
1272 MiniStatusComplete(
1273 IN NDIS_HANDLE MiniportAdapterHandle)
1274 {
1275 PLOGICAL_ADAPTER Adapter = MiniportAdapterHandle;
1276 PLIST_ENTRY CurrentEntry;
1277 PADAPTER_BINDING AdapterBinding;
1278 KIRQL OldIrql;
1279
1280 KeAcquireSpinLock(&Adapter->NdisMiniportBlock.Lock, &OldIrql);
1281
1282 CurrentEntry = Adapter->ProtocolListHead.Flink;
1283
1284 while (CurrentEntry != &Adapter->ProtocolListHead)
1285 {
1286 AdapterBinding = CONTAINING_RECORD(CurrentEntry, ADAPTER_BINDING, AdapterListEntry);
1287
1288 (*AdapterBinding->ProtocolBinding->Chars.StatusCompleteHandler)(
1289 AdapterBinding->NdisOpenBlock.ProtocolBindingContext);
1290
1291 CurrentEntry = CurrentEntry->Flink;
1292 }
1293
1294 KeReleaseSpinLock(&Adapter->NdisMiniportBlock.Lock, OldIrql);
1295 }
1296
1297 \f
1298 /*
1299 * @implemented
1300 */
1301 VOID
1302 EXPORT
1303 NdisMCloseLog(
1304 IN NDIS_HANDLE LogHandle)
1305 {
1306 PNDIS_LOG Log = (PNDIS_LOG)LogHandle;
1307 PNDIS_MINIPORT_BLOCK Miniport = Log->Miniport;
1308 KIRQL OldIrql;
1309
1310 NDIS_DbgPrint(MAX_TRACE, ("called: LogHandle 0x%x\n", LogHandle));
1311
1312 KeAcquireSpinLock(&(Miniport)->Lock, &OldIrql);
1313 Miniport->Log = NULL;
1314 KeReleaseSpinLock(&(Miniport)->Lock, OldIrql);
1315
1316 ExFreePool(Log);
1317 }
1318
1319 /*
1320 * @implemented
1321 */
1322 NDIS_STATUS
1323 EXPORT
1324 NdisMCreateLog(
1325 IN NDIS_HANDLE MiniportAdapterHandle,
1326 IN UINT Size,
1327 OUT PNDIS_HANDLE LogHandle)
1328 {
1329 PLOGICAL_ADAPTER Adapter = MiniportAdapterHandle;
1330 PNDIS_LOG Log;
1331 KIRQL OldIrql;
1332
1333 NDIS_DbgPrint(MAX_TRACE, ("called: MiniportAdapterHandle 0x%x, Size %ld\n", MiniportAdapterHandle, Size));
1334
1335 KeAcquireSpinLock(&Adapter->NdisMiniportBlock.Lock, &OldIrql);
1336
1337 if (Adapter->NdisMiniportBlock.Log)
1338 {
1339 *LogHandle = NULL;
1340 return NDIS_STATUS_FAILURE;
1341 }
1342
1343 Log = ExAllocatePool(NonPagedPool, Size + sizeof(NDIS_LOG));
1344 if (!Log)
1345 {
1346 *LogHandle = NULL;
1347 return NDIS_STATUS_RESOURCES;
1348 }
1349
1350 Adapter->NdisMiniportBlock.Log = Log;
1351
1352 KeInitializeSpinLock(&Log->LogLock);
1353
1354 Log->Miniport = &Adapter->NdisMiniportBlock;
1355 Log->TotalSize = Size;
1356 Log->CurrentSize = 0;
1357 Log->OutPtr = 0;
1358 Log->InPtr = 0;
1359 Log->Irp = NULL;
1360
1361 *LogHandle = Log;
1362
1363 KeReleaseSpinLock(&Adapter->NdisMiniportBlock.Lock, OldIrql);
1364
1365 return NDIS_STATUS_SUCCESS;
1366 }
1367
1368 /*
1369 * @implemented
1370 */
1371 VOID
1372 EXPORT
1373 NdisMDeregisterAdapterShutdownHandler(
1374 IN NDIS_HANDLE MiniportHandle)
1375 /*
1376 * FUNCTION: de-registers a shutdown handler
1377 * ARGUMENTS: MiniportHandle: Handle passed into MiniportInitialize
1378 */
1379 {
1380 PLOGICAL_ADAPTER Adapter = (PLOGICAL_ADAPTER)MiniportHandle;
1381
1382 NDIS_DbgPrint(DEBUG_MINIPORT, ("Called.\n"));
1383
1384 if(Adapter->BugcheckContext->ShutdownHandler) {
1385 KeDeregisterBugCheckCallback(Adapter->BugcheckContext->CallbackRecord);
1386 IoUnregisterShutdownNotification(Adapter->NdisMiniportBlock.DeviceObject);
1387 }
1388 }
1389
1390 /*
1391 * @implemented
1392 */
1393 VOID
1394 EXPORT
1395 NdisMFlushLog(
1396 IN NDIS_HANDLE LogHandle)
1397 {
1398 PNDIS_LOG Log = (PNDIS_LOG) LogHandle;
1399 KIRQL OldIrql;
1400
1401 NDIS_DbgPrint(MAX_TRACE, ("called: LogHandle 0x%x\n", LogHandle));
1402
1403 /* Lock object */
1404 KeAcquireSpinLock(&Log->LogLock, &OldIrql);
1405
1406 /* Set buffers size */
1407 Log->CurrentSize = 0;
1408 Log->OutPtr = 0;
1409 Log->InPtr = 0;
1410
1411 /* Unlock object */
1412 KeReleaseSpinLock(&Log->LogLock, OldIrql);
1413 }
1414
1415 /*
1416 * @implemented
1417 */
1418 #undef NdisMIndicateStatus
1419 VOID
1420 EXPORT
1421 NdisMIndicateStatus(
1422 IN NDIS_HANDLE MiniportAdapterHandle,
1423 IN NDIS_STATUS GeneralStatus,
1424 IN PVOID StatusBuffer,
1425 IN UINT StatusBufferSize)
1426 {
1427 MiniStatus(MiniportAdapterHandle, GeneralStatus, StatusBuffer, StatusBufferSize);
1428 }
1429
1430 /*
1431 * @implemented
1432 */
1433 #undef NdisMIndicateStatusComplete
1434 VOID
1435 EXPORT
1436 NdisMIndicateStatusComplete(
1437 IN NDIS_HANDLE MiniportAdapterHandle)
1438 {
1439 MiniStatusComplete(MiniportAdapterHandle);
1440 }
1441
1442 \f
1443 /*
1444 * @implemented
1445 */
1446 VOID
1447 EXPORT
1448 NdisInitializeWrapper(
1449 OUT PNDIS_HANDLE NdisWrapperHandle,
1450 IN PVOID SystemSpecific1,
1451 IN PVOID SystemSpecific2,
1452 IN PVOID SystemSpecific3)
1453 /*
1454 * FUNCTION: Notifies the NDIS library that a new miniport is initializing
1455 * ARGUMENTS:
1456 * NdisWrapperHandle = Address of buffer to place NDIS wrapper handle
1457 * SystemSpecific1 = Pointer to the driver's driver object
1458 * SystemSpecific2 = Pointer to the driver's registry path
1459 * SystemSpecific3 = Always NULL
1460 * NOTES:
1461 * - SystemSpecific2 goes invalid so we copy it
1462 */
1463 {
1464 PNDIS_M_DRIVER_BLOCK Miniport;
1465 PUNICODE_STRING RegistryPath;
1466 WCHAR *RegistryBuffer;
1467
1468 NDIS_DbgPrint(MAX_TRACE, ("Called.\n"));
1469
1470 ASSERT(NdisWrapperHandle);
1471
1472 *NdisWrapperHandle = NULL;
1473
1474 #if BREAK_ON_MINIPORT_INIT
1475 __asm__ ("int $3\n");
1476 #endif
1477
1478 Miniport = ExAllocatePool(NonPagedPool, sizeof(NDIS_M_DRIVER_BLOCK));
1479
1480 if (!Miniport)
1481 {
1482 NDIS_DbgPrint(MIN_TRACE, ("Insufficient resources.\n"));
1483 return;
1484 }
1485
1486 RtlZeroMemory(Miniport, sizeof(NDIS_M_DRIVER_BLOCK));
1487
1488 KeInitializeSpinLock(&Miniport->Lock);
1489
1490 Miniport->DriverObject = (PDRIVER_OBJECT)SystemSpecific1;
1491
1492 /* set the miniport's driver registry path */
1493 RegistryPath = ExAllocatePool(PagedPool, sizeof(UNICODE_STRING));
1494 if(!RegistryPath)
1495 {
1496 ExFreePool(Miniport);
1497 NDIS_DbgPrint(MIN_TRACE, ("Insufficient resources.\n"));
1498 return;
1499 }
1500
1501 RegistryPath->Length = ((PUNICODE_STRING)SystemSpecific2)->Length;
1502 RegistryPath->MaximumLength = RegistryPath->Length + sizeof(WCHAR); /* room for 0-term */
1503
1504 RegistryBuffer = ExAllocatePool(PagedPool, RegistryPath->MaximumLength);
1505 if(!RegistryBuffer)
1506 {
1507 NDIS_DbgPrint(MIN_TRACE, ("Insufficient resources.\n"));
1508 ExFreePool(Miniport);
1509 ExFreePool(RegistryPath);
1510 return;
1511 }
1512
1513 RtlCopyMemory(RegistryBuffer, ((PUNICODE_STRING)SystemSpecific2)->Buffer, RegistryPath->Length);
1514 RegistryBuffer[RegistryPath->Length/sizeof(WCHAR)] = 0;
1515
1516 RegistryPath->Buffer = RegistryBuffer;
1517 Miniport->RegistryPath = RegistryPath;
1518
1519 InitializeListHead(&Miniport->DeviceList);
1520
1521 /* Put miniport in global miniport list */
1522 ExInterlockedInsertTailList(&MiniportListHead, &Miniport->ListEntry, &MiniportListLock);
1523
1524 *NdisWrapperHandle = Miniport;
1525 }
1526
1527 \f
1528 VOID NTAPI NdisIBugcheckCallback(
1529 IN PVOID Buffer,
1530 IN ULONG Length)
1531 /*
1532 * FUNCTION: Internal callback for handling bugchecks - calls adapter's shutdown handler
1533 * ARGUMENTS:
1534 * Buffer: Pointer to a bugcheck callback context
1535 * Length: Unused
1536 */
1537 {
1538 PMINIPORT_BUGCHECK_CONTEXT Context = (PMINIPORT_BUGCHECK_CONTEXT)Buffer;
1539 ADAPTER_SHUTDOWN_HANDLER sh = (ADAPTER_SHUTDOWN_HANDLER)Context->ShutdownHandler;
1540
1541 NDIS_DbgPrint(DEBUG_MINIPORT, ("Called.\n"));
1542
1543 if(sh)
1544 sh(Context->DriverContext);
1545 }
1546
1547 \f
1548 /*
1549 * @implemented
1550 */
1551 VOID
1552 EXPORT
1553 NdisMRegisterAdapterShutdownHandler(
1554 IN NDIS_HANDLE MiniportHandle,
1555 IN PVOID ShutdownContext,
1556 IN ADAPTER_SHUTDOWN_HANDLER ShutdownHandler)
1557 /*
1558 * FUNCTION: Register a shutdown handler for an adapter
1559 * ARGUMENTS:
1560 * MiniportHandle: Handle originally passed into MiniportInitialize
1561 * ShutdownContext: Pre-initialized bugcheck context
1562 * ShutdownHandler: Function to call to handle the bugcheck
1563 * NOTES:
1564 * - I'm not sure about ShutdownContext
1565 */
1566 {
1567 PLOGICAL_ADAPTER Adapter = (PLOGICAL_ADAPTER)MiniportHandle;
1568 PMINIPORT_BUGCHECK_CONTEXT BugcheckContext;
1569
1570 NDIS_DbgPrint(DEBUG_MINIPORT, ("Called.\n"));
1571
1572 BugcheckContext = ExAllocatePool(NonPagedPool, sizeof(MINIPORT_BUGCHECK_CONTEXT));
1573 if(!BugcheckContext)
1574 {
1575 NDIS_DbgPrint(MIN_TRACE, ("Insufficient resources.\n"));
1576 return;
1577 }
1578
1579 BugcheckContext->ShutdownHandler = ShutdownHandler;
1580 BugcheckContext->DriverContext = ShutdownContext;
1581
1582 BugcheckContext->CallbackRecord = ExAllocatePool(NonPagedPool, sizeof(KBUGCHECK_CALLBACK_RECORD));
1583 if (!BugcheckContext->CallbackRecord) {
1584 ExFreePool(BugcheckContext);
1585 return;
1586 }
1587
1588 Adapter->BugcheckContext = BugcheckContext;
1589
1590 KeInitializeCallbackRecord(BugcheckContext->CallbackRecord);
1591
1592 KeRegisterBugCheckCallback(BugcheckContext->CallbackRecord, NdisIBugcheckCallback,
1593 BugcheckContext, sizeof(BugcheckContext), (PUCHAR)"Ndis Miniport");
1594
1595 IoRegisterShutdownNotification(Adapter->NdisMiniportBlock.DeviceObject);
1596 }
1597
1598 \f
1599 NDIS_STATUS
1600 DoQueries(
1601 PLOGICAL_ADAPTER Adapter,
1602 NDIS_OID AddressOID)
1603 /*
1604 * FUNCTION: Queries miniport for information
1605 * ARGUMENTS:
1606 * Adapter = Pointer to logical adapter
1607 * AddressOID = OID to use to query for current address
1608 * RETURNS:
1609 * Status of operation
1610 */
1611 {
1612 ULONG BytesWritten;
1613 NDIS_STATUS NdisStatus;
1614
1615 NDIS_DbgPrint(MAX_TRACE, ("Called.\n"));
1616
1617 /* Get MAC options for adapter */
1618 NdisStatus = MiniQueryInformation(Adapter, OID_GEN_MAC_OPTIONS, sizeof(UINT),
1619 &Adapter->NdisMiniportBlock.MacOptions,
1620 &BytesWritten);
1621
1622 if (NdisStatus != NDIS_STATUS_SUCCESS)
1623 {
1624 NDIS_DbgPrint(MIN_TRACE, ("OID_GEN_MAC_OPTIONS failed. NdisStatus (0x%X).\n", NdisStatus));
1625 return NdisStatus;
1626 }
1627
1628 NDIS_DbgPrint(DEBUG_MINIPORT, ("MacOptions (0x%X).\n", Adapter->NdisMiniportBlock.MacOptions));
1629
1630 /* Get current hardware address of adapter */
1631 NdisStatus = MiniQueryInformation(Adapter, AddressOID, Adapter->AddressLength,
1632 &Adapter->Address, &BytesWritten);
1633
1634 if (NdisStatus != NDIS_STATUS_SUCCESS)
1635 {
1636 NDIS_DbgPrint(MIN_TRACE, ("Address OID (0x%X) failed. NdisStatus (0x%X).\n", AddressOID, NdisStatus));
1637 return NdisStatus;
1638 }
1639
1640 #if DBG
1641 {
1642 /* 802.3 only */
1643
1644 PUCHAR A = (PUCHAR)&Adapter->Address.Type.Medium802_3;
1645
1646 NDIS_DbgPrint(MAX_TRACE, ("Adapter address is (%02X %02X %02X %02X %02X %02X).\n", A[0], A[1], A[2], A[3], A[4], A[5]));
1647 }
1648 #endif /* DBG */
1649
1650 /* Get maximum lookahead buffer size of adapter */
1651 NdisStatus = MiniQueryInformation(Adapter, OID_GEN_MAXIMUM_LOOKAHEAD, sizeof(ULONG),
1652 &Adapter->NdisMiniportBlock.MaximumLookahead, &BytesWritten);
1653
1654 if (NdisStatus != NDIS_STATUS_SUCCESS)
1655 {
1656 NDIS_DbgPrint(MIN_TRACE, ("OID_GEN_MAXIMUM_LOOKAHEAD failed. NdisStatus (0x%X).\n", NdisStatus));
1657 return NdisStatus;
1658 }
1659
1660 NDIS_DbgPrint(DEBUG_MINIPORT, ("MaxLookaheadLength (0x%X).\n", Adapter->NdisMiniportBlock.MaximumLookahead));
1661
1662 /* Get current lookahead buffer size of adapter */
1663 NdisStatus = MiniQueryInformation(Adapter, OID_GEN_CURRENT_LOOKAHEAD, sizeof(ULONG),
1664 &Adapter->NdisMiniportBlock.CurrentLookahead, &BytesWritten);
1665
1666 if (NdisStatus != NDIS_STATUS_SUCCESS)
1667 {
1668 NDIS_DbgPrint(MIN_TRACE, ("OID_GEN_CURRENT_LOOKAHEAD failed. NdisStatus (0x%X).\n", NdisStatus));
1669 return NdisStatus;
1670 }
1671
1672 NdisStatus = MiniQueryInformation(Adapter, OID_GEN_MAXIMUM_SEND_PACKETS, sizeof(ULONG),
1673 &Adapter->NdisMiniportBlock.MaxSendPackets, &BytesWritten);
1674
1675 if (NdisStatus != NDIS_STATUS_SUCCESS)
1676 {
1677 NDIS_DbgPrint(MIN_TRACE, ("OID_GEN_MAXIMUM_SEND_PACKETS failed. NdisStatus (0x%X).\n", NdisStatus));
1678
1679 /* Set it to 1 if it fails because some drivers don't support this (?)*/
1680 Adapter->NdisMiniportBlock.MaxSendPackets = 1;
1681 }
1682
1683 NDIS_DbgPrint(DEBUG_MINIPORT, ("CurLookaheadLength (0x%X).\n", Adapter->NdisMiniportBlock.CurrentLookahead));
1684
1685 return STATUS_SUCCESS;
1686 }
1687
1688 \f
1689 NTSTATUS
1690 NTAPI
1691 NdisIForwardIrpAndWaitCompletionRoutine(
1692 PDEVICE_OBJECT Fdo,
1693 PIRP Irp,
1694 PVOID Context)
1695 {
1696 PKEVENT Event = Context;
1697
1698 if (Irp->PendingReturned)
1699 KeSetEvent(Event, IO_NO_INCREMENT, FALSE);
1700
1701 return STATUS_MORE_PROCESSING_REQUIRED;
1702 }
1703
1704 \f
1705 NTSTATUS
1706 NTAPI
1707 NdisIForwardIrpAndWait(PLOGICAL_ADAPTER Adapter, PIRP Irp)
1708 {
1709 KEVENT Event;
1710 NTSTATUS Status;
1711
1712 KeInitializeEvent(&Event, NotificationEvent, FALSE);
1713 IoCopyCurrentIrpStackLocationToNext(Irp);
1714 IoSetCompletionRoutine(Irp, NdisIForwardIrpAndWaitCompletionRoutine, &Event,
1715 TRUE, TRUE, TRUE);
1716 Status = IoCallDriver(Adapter->NdisMiniportBlock.NextDeviceObject, Irp);
1717 if (Status == STATUS_PENDING)
1718 {
1719 KeWaitForSingleObject(&Event, Executive, KernelMode, FALSE, NULL);
1720 Status = Irp->IoStatus.Status;
1721 }
1722 return Status;
1723 }
1724
1725 \f
1726 NTSTATUS
1727 NTAPI
1728 NdisIPnPStartDevice(
1729 IN PDEVICE_OBJECT DeviceObject,
1730 PIRP Irp)
1731 /*
1732 * FUNCTION: Handle the PnP start device event
1733 * ARGUMENTS:
1734 * DeviceObejct = Functional Device Object
1735 * Irp = IRP_MN_START_DEVICE I/O request packet
1736 * RETURNS:
1737 * Status of operation
1738 */
1739 {
1740 PIO_STACK_LOCATION Stack = IoGetCurrentIrpStackLocation(Irp);
1741 PLOGICAL_ADAPTER Adapter = (PLOGICAL_ADAPTER)DeviceObject->DeviceExtension;
1742 NDIS_WRAPPER_CONTEXT WrapperContext;
1743 NDIS_STATUS NdisStatus;
1744 NDIS_STATUS OpenErrorStatus;
1745 NTSTATUS Status;
1746 UINT SelectedMediumIndex = 0;
1747 NDIS_OID AddressOID;
1748 BOOLEAN Success = FALSE;
1749 ULONG ResourceCount;
1750 ULONG ResourceListSize;
1751 UNICODE_STRING ParamName;
1752 PNDIS_CONFIGURATION_PARAMETER ConfigParam;
1753 NDIS_HANDLE ConfigHandle;
1754 ULONG Size;
1755 LARGE_INTEGER Timeout;
1756 UINT MaxMulticastAddresses;
1757 ULONG BytesWritten;
1758
1759 /*
1760 * Prepare wrapper context used by HW and configuration routines.
1761 */
1762
1763 NDIS_DbgPrint(DEBUG_MINIPORT, ("Start Device %wZ\n", &Adapter->NdisMiniportBlock.MiniportName));
1764
1765 NDIS_DbgPrint(MAX_TRACE, ("Inserting adapter 0x%x into adapter list\n", Adapter));
1766
1767 /* Put adapter in global adapter list */
1768 ExInterlockedInsertTailList(&AdapterListHead, &Adapter->ListEntry, &AdapterListLock);
1769
1770 Status = IoOpenDeviceRegistryKey(
1771 Adapter->NdisMiniportBlock.PhysicalDeviceObject, PLUGPLAY_REGKEY_DRIVER,
1772 KEY_ALL_ACCESS, &WrapperContext.RegistryHandle);
1773 if (!NT_SUCCESS(Status))
1774 {
1775 NDIS_DbgPrint(MIN_TRACE,("failed to open adapter-specific reg key\n"));
1776 ExInterlockedRemoveEntryList( &Adapter->ListEntry, &AdapterListLock );
1777 return Status;
1778 }
1779
1780 NDIS_DbgPrint(MAX_TRACE, ("opened device reg key\n"));
1781
1782 WrapperContext.DeviceObject = Adapter->NdisMiniportBlock.DeviceObject;
1783
1784 /*
1785 * Store the adapter resources used by HW routines such as
1786 * NdisMQueryAdapterResources.
1787 */
1788
1789 if (Stack->Parameters.StartDevice.AllocatedResources != NULL)
1790 {
1791 ResourceCount = Stack->Parameters.StartDevice.AllocatedResources->List[0].
1792 PartialResourceList.Count;
1793 ResourceListSize =
1794 FIELD_OFFSET(CM_RESOURCE_LIST, List[0].PartialResourceList.
1795 PartialDescriptors[ResourceCount]);
1796
1797 Adapter->NdisMiniportBlock.AllocatedResources =
1798 ExAllocatePool(PagedPool, ResourceListSize);
1799 if (Adapter->NdisMiniportBlock.AllocatedResources == NULL)
1800 {
1801 NDIS_DbgPrint(MIN_TRACE, ("Insufficient resources\n"));
1802 ExInterlockedRemoveEntryList( &Adapter->ListEntry, &AdapterListLock );
1803 return STATUS_INSUFFICIENT_RESOURCES;
1804 }
1805
1806 Adapter->NdisMiniportBlock.Resources =
1807 ExAllocatePool(PagedPool, ResourceListSize);
1808 if (!Adapter->NdisMiniportBlock.Resources)
1809 {
1810 NDIS_DbgPrint(MIN_TRACE, ("Insufficient resources\n"));
1811 ExFreePool(Adapter->NdisMiniportBlock.AllocatedResources);
1812 ExInterlockedRemoveEntryList(&Adapter->ListEntry, &AdapterListLock);
1813 return STATUS_INSUFFICIENT_RESOURCES;
1814 }
1815
1816 RtlCopyMemory(Adapter->NdisMiniportBlock.Resources,
1817 Stack->Parameters.StartDevice.AllocatedResources,
1818 ResourceListSize);
1819
1820 RtlCopyMemory(Adapter->NdisMiniportBlock.AllocatedResources,
1821 Stack->Parameters.StartDevice.AllocatedResources,
1822 ResourceListSize);
1823 }
1824
1825 if (Stack->Parameters.StartDevice.AllocatedResourcesTranslated != NULL)
1826 {
1827 ResourceCount = Stack->Parameters.StartDevice.AllocatedResourcesTranslated->List[0].
1828 PartialResourceList.Count;
1829 ResourceListSize =
1830 FIELD_OFFSET(CM_RESOURCE_LIST, List[0].PartialResourceList.
1831 PartialDescriptors[ResourceCount]);
1832
1833 Adapter->NdisMiniportBlock.AllocatedResourcesTranslated =
1834 ExAllocatePool(PagedPool, ResourceListSize);
1835 if (Adapter->NdisMiniportBlock.AllocatedResourcesTranslated == NULL)
1836 {
1837 NDIS_DbgPrint(MIN_TRACE, ("Insufficient resources\n"));
1838 ExInterlockedRemoveEntryList( &Adapter->ListEntry, &AdapterListLock );
1839 return STATUS_INSUFFICIENT_RESOURCES;
1840 }
1841
1842 RtlCopyMemory(Adapter->NdisMiniportBlock.AllocatedResourcesTranslated,
1843 Stack->Parameters.StartDevice.AllocatedResourcesTranslated,
1844 ResourceListSize);
1845 }
1846
1847 /*
1848 * Store the Bus Type, Bus Number and Slot information. It's used by
1849 * the hardware routines then.
1850 */
1851
1852 NdisOpenConfiguration(&NdisStatus, &ConfigHandle, (NDIS_HANDLE)&WrapperContext);
1853
1854 Size = sizeof(ULONG);
1855 Status = IoGetDeviceProperty(Adapter->NdisMiniportBlock.PhysicalDeviceObject,
1856 DevicePropertyLegacyBusType, Size,
1857 &Adapter->NdisMiniportBlock.BusType, &Size);
1858 if (!NT_SUCCESS(Status) || (INTERFACE_TYPE)Adapter->NdisMiniportBlock.BusType == InterfaceTypeUndefined)
1859 {
1860 NdisInitUnicodeString(&ParamName, L"BusType");
1861 NdisReadConfiguration(&NdisStatus, &ConfigParam, ConfigHandle,
1862 &ParamName, NdisParameterInteger);
1863 if (NdisStatus == NDIS_STATUS_SUCCESS)
1864 Adapter->NdisMiniportBlock.BusType = ConfigParam->ParameterData.IntegerData;
1865 else
1866 Adapter->NdisMiniportBlock.BusType = Isa;
1867 }
1868
1869 Status = IoGetDeviceProperty(Adapter->NdisMiniportBlock.PhysicalDeviceObject,
1870 DevicePropertyBusNumber, Size,
1871 &Adapter->NdisMiniportBlock.BusNumber, &Size);
1872 if (!NT_SUCCESS(Status) || Adapter->NdisMiniportBlock.BusNumber == 0xFFFFFFF0)
1873 {
1874 NdisInitUnicodeString(&ParamName, L"BusNumber");
1875 NdisReadConfiguration(&NdisStatus, &ConfigParam, ConfigHandle,
1876 &ParamName, NdisParameterInteger);
1877 if (NdisStatus == NDIS_STATUS_SUCCESS)
1878 Adapter->NdisMiniportBlock.BusNumber = ConfigParam->ParameterData.IntegerData;
1879 else
1880 Adapter->NdisMiniportBlock.BusNumber = 0;
1881 }
1882 WrapperContext.BusNumber = Adapter->NdisMiniportBlock.BusNumber;
1883
1884 Status = IoGetDeviceProperty(Adapter->NdisMiniportBlock.PhysicalDeviceObject,
1885 DevicePropertyAddress, Size,
1886 &Adapter->NdisMiniportBlock.SlotNumber, &Size);
1887 if (!NT_SUCCESS(Status) || Adapter->NdisMiniportBlock.SlotNumber == (NDIS_INTERFACE_TYPE)-1)
1888 {
1889 NdisInitUnicodeString(&ParamName, L"SlotNumber");
1890 NdisReadConfiguration(&NdisStatus, &ConfigParam, ConfigHandle,
1891 &ParamName, NdisParameterInteger);
1892 if (NdisStatus == NDIS_STATUS_SUCCESS)
1893 Adapter->NdisMiniportBlock.SlotNumber = ConfigParam->ParameterData.IntegerData;
1894 else
1895 Adapter->NdisMiniportBlock.SlotNumber = 0;
1896 }
1897 else
1898 {
1899 /* Convert slotnumber to PCI_SLOT_NUMBER */
1900 ULONG PciSlotNumber = Adapter->NdisMiniportBlock.SlotNumber;
1901 PCI_SLOT_NUMBER SlotNumber;
1902
1903 SlotNumber.u.AsULONG = 0;
1904 SlotNumber.u.bits.DeviceNumber = (PciSlotNumber >> 16) & 0xFFFF;
1905 SlotNumber.u.bits.FunctionNumber = PciSlotNumber & 0xFFFF;
1906
1907 Adapter->NdisMiniportBlock.SlotNumber = SlotNumber.u.AsULONG;
1908 }
1909 WrapperContext.SlotNumber = Adapter->NdisMiniportBlock.SlotNumber;
1910
1911 NdisCloseConfiguration(ConfigHandle);
1912
1913 /* Set handlers (some NDIS macros require these) */
1914 Adapter->NdisMiniportBlock.EthRxCompleteHandler = EthFilterDprIndicateReceiveComplete;
1915 Adapter->NdisMiniportBlock.EthRxIndicateHandler = EthFilterDprIndicateReceive;
1916 Adapter->NdisMiniportBlock.SendCompleteHandler = MiniSendComplete;
1917 Adapter->NdisMiniportBlock.SendResourcesHandler = MiniSendResourcesAvailable;
1918 Adapter->NdisMiniportBlock.ResetCompleteHandler = MiniResetComplete;
1919 Adapter->NdisMiniportBlock.TDCompleteHandler = MiniTransferDataComplete;
1920 Adapter->NdisMiniportBlock.PacketIndicateHandler= MiniIndicateReceivePacket;
1921 Adapter->NdisMiniportBlock.StatusHandler = MiniStatus;
1922 Adapter->NdisMiniportBlock.StatusCompleteHandler= MiniStatusComplete;
1923 Adapter->NdisMiniportBlock.SendPacketsHandler = ProSendPackets;
1924 Adapter->NdisMiniportBlock.QueryCompleteHandler = MiniRequestComplete;
1925 Adapter->NdisMiniportBlock.SetCompleteHandler = MiniRequestComplete;
1926
1927 /*
1928 * Call MiniportInitialize.
1929 */
1930
1931 NDIS_DbgPrint(MID_TRACE, ("calling MiniportInitialize\n"));
1932 NdisStatus = (*Adapter->NdisMiniportBlock.DriverHandle->MiniportCharacteristics.InitializeHandler)(
1933 &OpenErrorStatus, &SelectedMediumIndex, &MediaArray[0],
1934 MEDIA_ARRAY_SIZE, Adapter, (NDIS_HANDLE)&WrapperContext);
1935
1936 ZwClose(WrapperContext.RegistryHandle);
1937
1938 if (NdisStatus != NDIS_STATUS_SUCCESS)
1939 {
1940 NDIS_DbgPrint(MIN_TRACE, ("MiniportInitialize() failed for an adapter.\n"));
1941 ExInterlockedRemoveEntryList( &Adapter->ListEntry, &AdapterListLock );
1942 return NdisStatus;
1943 }
1944
1945 if (SelectedMediumIndex >= MEDIA_ARRAY_SIZE)
1946 {
1947 NDIS_DbgPrint(MIN_TRACE, ("MiniportInitialize() selected a bad index\n"));
1948 ExInterlockedRemoveEntryList( &Adapter->ListEntry, &AdapterListLock );
1949 return NDIS_STATUS_UNSUPPORTED_MEDIA;
1950 }
1951
1952 Adapter->NdisMiniportBlock.MediaType = MediaArray[SelectedMediumIndex];
1953
1954 switch (Adapter->NdisMiniportBlock.MediaType)
1955 {
1956 case NdisMedium802_3:
1957 Adapter->MediumHeaderSize = 14; /* XXX figure out what to do about LLC */
1958 AddressOID = OID_802_3_CURRENT_ADDRESS;
1959 Adapter->AddressLength = ETH_LENGTH_OF_ADDRESS;
1960 NdisStatus = DoQueries(Adapter, AddressOID);
1961 if (NdisStatus == NDIS_STATUS_SUCCESS)
1962 {
1963 NdisStatus = MiniQueryInformation(Adapter, OID_802_3_MAXIMUM_LIST_SIZE, sizeof(UINT),
1964 &MaxMulticastAddresses, &BytesWritten);
1965
1966 if (NdisStatus != NDIS_STATUS_SUCCESS)
1967 {
1968 ExInterlockedRemoveEntryList( &Adapter->ListEntry, &AdapterListLock );
1969 NDIS_DbgPrint(MIN_TRACE, ("MiniQueryInformation failed (%x)\n", NdisStatus));
1970 return NdisStatus;
1971 }
1972
1973 Success = EthCreateFilter(MaxMulticastAddresses,
1974 Adapter->Address.Type.Medium802_3,
1975 &Adapter->NdisMiniportBlock.EthDB);
1976 if (Success)
1977 ((PETHI_FILTER)Adapter->NdisMiniportBlock.EthDB)->Miniport = (PNDIS_MINIPORT_BLOCK)Adapter;
1978 else
1979 NdisStatus = NDIS_STATUS_RESOURCES;
1980 }
1981 break;
1982
1983 default:
1984 /* FIXME: Support other types of media */
1985 NDIS_DbgPrint(MIN_TRACE, ("error: unsupported media\n"));
1986 ASSERT(FALSE);
1987 ExInterlockedRemoveEntryList( &Adapter->ListEntry, &AdapterListLock );
1988 return STATUS_UNSUCCESSFUL;
1989 }
1990
1991 if (NdisStatus != NDIS_STATUS_SUCCESS)
1992 {
1993 NDIS_DbgPrint(MIN_TRACE, ("couldn't create filter (%x)\n", NdisStatus));
1994 return NdisStatus;
1995 }
1996
1997 /* Check for a hang every two seconds if it wasn't set in MiniportInitialize */
1998 if (Adapter->NdisMiniportBlock.CheckForHangSeconds == 0)
1999 Adapter->NdisMiniportBlock.CheckForHangSeconds = 2;
2000
2001 Adapter->NdisMiniportBlock.OldPnPDeviceState = Adapter->NdisMiniportBlock.PnPDeviceState;
2002 Adapter->NdisMiniportBlock.PnPDeviceState = NdisPnPDeviceStarted;
2003
2004 IoSetDeviceInterfaceState(&Adapter->NdisMiniportBlock.SymbolicLinkName, TRUE);
2005
2006 Timeout.QuadPart = Int32x32To64(Adapter->NdisMiniportBlock.CheckForHangSeconds, -1000000);
2007 KeSetTimerEx(&Adapter->NdisMiniportBlock.WakeUpDpcTimer.Timer, Timeout,
2008 Adapter->NdisMiniportBlock.CheckForHangSeconds * 1000,
2009 &Adapter->NdisMiniportBlock.WakeUpDpcTimer.Dpc);
2010
2011 /* Put adapter in adapter list for this miniport */
2012 ExInterlockedInsertTailList(&Adapter->NdisMiniportBlock.DriverHandle->DeviceList, &Adapter->MiniportListEntry, &Adapter->NdisMiniportBlock.DriverHandle->Lock);
2013
2014 return STATUS_SUCCESS;
2015 }
2016
2017 \f
2018 NTSTATUS
2019 NTAPI
2020 NdisIPnPStopDevice(
2021 IN PDEVICE_OBJECT DeviceObject,
2022 PIRP Irp)
2023 /*
2024 * FUNCTION: Handle the PnP stop device event
2025 * ARGUMENTS:
2026 * DeviceObejct = Functional Device Object
2027 * Irp = IRP_MN_STOP_DEVICE I/O request packet
2028 * RETURNS:
2029 * Status of operation
2030 */
2031 {
2032 PLOGICAL_ADAPTER Adapter = (PLOGICAL_ADAPTER)DeviceObject->DeviceExtension;
2033
2034 /* Remove adapter from adapter list for this miniport */
2035 ExInterlockedRemoveEntryList(&Adapter->MiniportListEntry, &Adapter->NdisMiniportBlock.DriverHandle->Lock);
2036
2037 /* Remove adapter from global adapter list */
2038 ExInterlockedRemoveEntryList(&Adapter->ListEntry, &AdapterListLock);
2039
2040 KeCancelTimer(&Adapter->NdisMiniportBlock.WakeUpDpcTimer.Timer);
2041
2042 (*Adapter->NdisMiniportBlock.DriverHandle->MiniportCharacteristics.HaltHandler)(Adapter);
2043
2044 IoSetDeviceInterfaceState(&Adapter->NdisMiniportBlock.SymbolicLinkName, FALSE);
2045
2046 if (Adapter->NdisMiniportBlock.AllocatedResources)
2047 {
2048 ExFreePool(Adapter->NdisMiniportBlock.AllocatedResources);
2049 Adapter->NdisMiniportBlock.AllocatedResources = NULL;
2050 }
2051 if (Adapter->NdisMiniportBlock.AllocatedResourcesTranslated)
2052 {
2053 ExFreePool(Adapter->NdisMiniportBlock.AllocatedResourcesTranslated);
2054 Adapter->NdisMiniportBlock.AllocatedResourcesTranslated = NULL;
2055 }
2056
2057 if (Adapter->NdisMiniportBlock.Resources)
2058 {
2059 ExFreePool(Adapter->NdisMiniportBlock.Resources);
2060 Adapter->NdisMiniportBlock.Resources = NULL;
2061 }
2062
2063 if (Adapter->NdisMiniportBlock.EthDB)
2064 {
2065 EthDeleteFilter(Adapter->NdisMiniportBlock.EthDB);
2066 Adapter->NdisMiniportBlock.EthDB = NULL;
2067 }
2068
2069 Adapter->NdisMiniportBlock.OldPnPDeviceState = Adapter->NdisMiniportBlock.PnPDeviceState;
2070 Adapter->NdisMiniportBlock.PnPDeviceState = NdisPnPDeviceStopped;
2071
2072 return STATUS_SUCCESS;
2073 }
2074
2075 NTSTATUS
2076 NTAPI
2077 NdisIShutdown(
2078 IN PDEVICE_OBJECT DeviceObject,
2079 PIRP Irp)
2080 {
2081 PLOGICAL_ADAPTER Adapter = DeviceObject->DeviceExtension;
2082 PMINIPORT_BUGCHECK_CONTEXT Context = Adapter->BugcheckContext;
2083 ADAPTER_SHUTDOWN_HANDLER ShutdownHandler = Context->ShutdownHandler;
2084
2085 ASSERT(ShutdownHandler);
2086
2087 ShutdownHandler(Context->DriverContext);
2088
2089 Irp->IoStatus.Status = STATUS_SUCCESS;
2090 Irp->IoStatus.Information = 0;
2091
2092 IoCompleteRequest(Irp, IO_NO_INCREMENT);
2093
2094 return STATUS_SUCCESS;
2095 }
2096
2097 NTSTATUS
2098 NTAPI
2099 NdisIDeviceIoControl(
2100 IN PDEVICE_OBJECT DeviceObject,
2101 PIRP Irp)
2102 {
2103 PLOGICAL_ADAPTER Adapter = (PLOGICAL_ADAPTER)DeviceObject->DeviceExtension;
2104 PIO_STACK_LOCATION Stack = IoGetCurrentIrpStackLocation(Irp);
2105 NDIS_STATUS Status = STATUS_NOT_SUPPORTED;
2106
2107 Irp->IoStatus.Information = 0;
2108
2109 ASSERT(Adapter);
2110
2111 switch (Stack->Parameters.DeviceIoControl.IoControlCode)
2112 {
2113 case IOCTL_NDIS_QUERY_GLOBAL_STATS:
2114 Status = MiniQueryInformation(Adapter,
2115 *(PNDIS_OID)Irp->AssociatedIrp.SystemBuffer,
2116 Stack->Parameters.DeviceIoControl.OutputBufferLength,
2117 MmGetSystemAddressForMdl(Irp->MdlAddress),
2118 &Irp->IoStatus.Information);
2119 break;
2120
2121 default:
2122 ASSERT(FALSE);
2123 break;
2124 }
2125
2126 if (Status != NDIS_STATUS_PENDING)
2127 {
2128 Irp->IoStatus.Status = Status;
2129 IoCompleteRequest(Irp, IO_NO_INCREMENT);
2130 }
2131 else
2132 IoMarkIrpPending(Irp);
2133
2134 return Status;
2135 }
2136
2137 \f
2138 NTSTATUS
2139 NTAPI
2140 NdisIDispatchPnp(
2141 IN PDEVICE_OBJECT DeviceObject,
2142 PIRP Irp)
2143 {
2144 PIO_STACK_LOCATION Stack = IoGetCurrentIrpStackLocation(Irp);
2145 PLOGICAL_ADAPTER Adapter = (PLOGICAL_ADAPTER)DeviceObject->DeviceExtension;
2146 NTSTATUS Status;
2147
2148 switch (Stack->MinorFunction)
2149 {
2150 case IRP_MN_START_DEVICE:
2151 Status = NdisIForwardIrpAndWait(Adapter, Irp);
2152 if (NT_SUCCESS(Status) && NT_SUCCESS(Irp->IoStatus.Status))
2153 {
2154 Status = NdisIPnPStartDevice(DeviceObject, Irp);
2155 }
2156 else
2157 NDIS_DbgPrint(MIN_TRACE, ("Lower driver failed device start\n"));
2158 Irp->IoStatus.Status = Status;
2159 IoCompleteRequest(Irp, IO_NO_INCREMENT);
2160 break;
2161
2162 case IRP_MN_STOP_DEVICE:
2163 Status = NdisIForwardIrpAndWait(Adapter, Irp);
2164 if (NT_SUCCESS(Status) && NT_SUCCESS(Irp->IoStatus.Status))
2165 {
2166 Status = NdisIPnPStopDevice(DeviceObject, Irp);
2167 }
2168 else
2169 NDIS_DbgPrint(MIN_TRACE, ("Lower driver failed device stop\n"));
2170 Irp->IoStatus.Status = Status;
2171 IoCompleteRequest(Irp, IO_NO_INCREMENT);
2172 break;
2173
2174 case IRP_MN_QUERY_REMOVE_DEVICE:
2175 case IRP_MN_QUERY_STOP_DEVICE:
2176 Status = NdisIPnPQueryStopDevice(DeviceObject, Irp);
2177 Irp->IoStatus.Status = Status;
2178 IoCompleteRequest(Irp, IO_NO_INCREMENT);
2179 break;
2180
2181 case IRP_MN_CANCEL_REMOVE_DEVICE:
2182 case IRP_MN_CANCEL_STOP_DEVICE:
2183 Status = NdisIPnPCancelStopDevice(DeviceObject, Irp);
2184 Irp->IoStatus.Status = Status;
2185 IoCompleteRequest(Irp, IO_NO_INCREMENT);
2186 break;
2187
2188 case IRP_MN_QUERY_PNP_DEVICE_STATE:
2189 Status = NDIS_STATUS_SUCCESS;
2190 Irp->IoStatus.Status = Status;
2191 Irp->IoStatus.Information |= Adapter->NdisMiniportBlock.PnPFlags;
2192 IoCompleteRequest(Irp, IO_NO_INCREMENT);
2193 break;
2194
2195 default:
2196 IoSkipCurrentIrpStackLocation(Irp);
2197 Status = IoCallDriver(Adapter->NdisMiniportBlock.NextDeviceObject, Irp);
2198 break;
2199 }
2200
2201 return Status;
2202 }
2203
2204 \f
2205 NTSTATUS
2206 NTAPI
2207 NdisIAddDevice(
2208 IN PDRIVER_OBJECT DriverObject,
2209 IN PDEVICE_OBJECT PhysicalDeviceObject)
2210 /*
2211 * FUNCTION: Create a device for an adapter found using PnP
2212 * ARGUMENTS:
2213 * DriverObject = Pointer to the miniport driver object
2214 * PhysicalDeviceObject = Pointer to the PDO for our adapter
2215 */
2216 {
2217 static const WCHAR ClassKeyName[] = {'C','l','a','s','s','\\'};
2218 static const WCHAR LinkageKeyName[] = {'\\','L','i','n','k','a','g','e',0};
2219 PNDIS_M_DRIVER_BLOCK Miniport;
2220 PNDIS_M_DRIVER_BLOCK *MiniportPtr;
2221 WCHAR *LinkageKeyBuffer;
2222 ULONG DriverKeyLength;
2223 RTL_QUERY_REGISTRY_TABLE QueryTable[2];
2224 UNICODE_STRING ExportName;
2225 PDEVICE_OBJECT DeviceObject;
2226 PLOGICAL_ADAPTER Adapter;
2227 NTSTATUS Status;
2228
2229 /*
2230 * Gain the access to the miniport data structure first.
2231 */
2232
2233 MiniportPtr = IoGetDriverObjectExtension(DriverObject, (PVOID)TAG('D','I','M','N'));
2234 if (MiniportPtr == NULL)
2235 {
2236 NDIS_DbgPrint(MIN_TRACE, ("Can't get driver object extension.\n"));
2237 return NDIS_STATUS_FAILURE;
2238 }
2239 Miniport = *MiniportPtr;
2240
2241 /*
2242 * Get name of the Linkage registry key for our adapter. It's located under
2243 * the driver key for our driver and so we have basicly two ways to do it.
2244 * Either we can use IoOpenDriverRegistryKey or compose it using information
2245 * gathered by IoGetDeviceProperty. I choosed the second because
2246 * IoOpenDriverRegistryKey wasn't implemented at the time of writing.
2247 */
2248
2249 Status = IoGetDeviceProperty(PhysicalDeviceObject, DevicePropertyDriverKeyName,
2250 0, NULL, &DriverKeyLength);
2251 if (Status != STATUS_BUFFER_TOO_SMALL && Status != STATUS_BUFFER_OVERFLOW && Status != STATUS_SUCCESS)
2252 {
2253 NDIS_DbgPrint(MIN_TRACE, ("Can't get miniport driver key length.\n"));
2254 return Status;
2255 }
2256
2257 LinkageKeyBuffer = ExAllocatePool(PagedPool, DriverKeyLength +
2258 sizeof(ClassKeyName) + sizeof(LinkageKeyName));
2259 if (LinkageKeyBuffer == NULL)
2260 {
2261 NDIS_DbgPrint(MIN_TRACE, ("Can't allocate memory for driver key name.\n"));
2262 return STATUS_INSUFFICIENT_RESOURCES;
2263 }
2264
2265 Status = IoGetDeviceProperty(PhysicalDeviceObject, DevicePropertyDriverKeyName,
2266 DriverKeyLength, LinkageKeyBuffer +
2267 (sizeof(ClassKeyName) / sizeof(WCHAR)),
2268 &DriverKeyLength);
2269 if (!NT_SUCCESS(Status))
2270 {
2271 NDIS_DbgPrint(MIN_TRACE, ("Can't get miniport driver key.\n"));
2272 ExFreePool(LinkageKeyBuffer);
2273 return Status;
2274 }
2275
2276 /* Compose the linkage key name. */
2277 RtlCopyMemory(LinkageKeyBuffer, ClassKeyName, sizeof(ClassKeyName));
2278 RtlCopyMemory(LinkageKeyBuffer + ((sizeof(ClassKeyName) + DriverKeyLength) /
2279 sizeof(WCHAR)) - 1, LinkageKeyName, sizeof(LinkageKeyName));
2280
2281 NDIS_DbgPrint(DEBUG_MINIPORT, ("LinkageKey: %S.\n", LinkageKeyBuffer));
2282
2283 /*
2284 * Now open the linkage key and read the "Export" and "RootDevice" values
2285 * which contains device name and root service respectively.
2286 */
2287
2288 RtlZeroMemory(QueryTable, sizeof(QueryTable));
2289 RtlInitUnicodeString(&ExportName, NULL);
2290 QueryTable[0].Flags = RTL_QUERY_REGISTRY_REQUIRED | RTL_QUERY_REGISTRY_DIRECT;
2291 QueryTable[0].Name = L"Export";
2292 QueryTable[0].EntryContext = &ExportName;
2293
2294 Status = RtlQueryRegistryValues(RTL_REGISTRY_CONTROL, LinkageKeyBuffer,
2295 QueryTable, NULL, NULL);
2296 ExFreePool(LinkageKeyBuffer);
2297 if (!NT_SUCCESS(Status))
2298 {
2299 NDIS_DbgPrint(MIN_TRACE, ("Can't get miniport device name. (%x)\n", Status));
2300 return Status;
2301 }
2302
2303 /*
2304 * Create the device object.
2305 */
2306
2307 NDIS_DbgPrint(MAX_TRACE, ("creating device %wZ\n", &ExportName));
2308
2309 Status = IoCreateDevice(Miniport->DriverObject, sizeof(LOGICAL_ADAPTER),
2310 &ExportName, FILE_DEVICE_PHYSICAL_NETCARD,
2311 0, FALSE, &DeviceObject);
2312 if (!NT_SUCCESS(Status))
2313 {
2314 NDIS_DbgPrint(MIN_TRACE, ("Could not create device object.\n"));
2315 RtlFreeUnicodeString(&ExportName);
2316 return Status;
2317 }
2318
2319 /*
2320 * Initialize the adapter structure.
2321 */
2322
2323 Adapter = (PLOGICAL_ADAPTER)DeviceObject->DeviceExtension;
2324 KeInitializeSpinLock(&Adapter->NdisMiniportBlock.Lock);
2325 InitializeListHead(&Adapter->ProtocolListHead);
2326
2327 Status = IoRegisterDeviceInterface(PhysicalDeviceObject,
2328 &GUID_DEVINTERFACE_NET,
2329 NULL,
2330 &Adapter->NdisMiniportBlock.SymbolicLinkName);
2331
2332 if (!NT_SUCCESS(Status))
2333 {
2334 NDIS_DbgPrint(MIN_TRACE, ("Could not create device interface.\n"));
2335 IoDeleteDevice(DeviceObject);
2336 RtlFreeUnicodeString(&ExportName);
2337 return Status;
2338 }
2339
2340 Adapter->NdisMiniportBlock.DriverHandle = Miniport;
2341 Adapter->NdisMiniportBlock.MiniportName = ExportName;
2342 Adapter->NdisMiniportBlock.DeviceObject = DeviceObject;
2343 Adapter->NdisMiniportBlock.PhysicalDeviceObject = PhysicalDeviceObject;
2344 Adapter->NdisMiniportBlock.NextDeviceObject =
2345 IoAttachDeviceToDeviceStack(Adapter->NdisMiniportBlock.DeviceObject,
2346 PhysicalDeviceObject);
2347
2348 Adapter->NdisMiniportBlock.OldPnPDeviceState = 0;
2349 Adapter->NdisMiniportBlock.PnPDeviceState = NdisPnPDeviceAdded;
2350
2351 KeInitializeTimer(&Adapter->NdisMiniportBlock.WakeUpDpcTimer.Timer);
2352 KeInitializeDpc(&Adapter->NdisMiniportBlock.WakeUpDpcTimer.Dpc, MiniportHangDpc, Adapter);
2353
2354 DeviceObject->Flags &= ~DO_DEVICE_INITIALIZING;
2355
2356 return STATUS_SUCCESS;
2357 }
2358
2359 \f
2360 /*
2361 * @implemented
2362 */
2363 NDIS_STATUS
2364 EXPORT
2365 NdisMRegisterMiniport(
2366 IN NDIS_HANDLE NdisWrapperHandle,
2367 IN PNDIS_MINIPORT_CHARACTERISTICS MiniportCharacteristics,
2368 IN UINT CharacteristicsLength)
2369 /*
2370 * FUNCTION: Registers a miniport's MiniportXxx entry points with the NDIS library
2371 * ARGUMENTS:
2372 * NdisWrapperHandle = Pointer to handle returned by NdisMInitializeWrapper
2373 * MiniportCharacteristics = Pointer to a buffer with miniport characteristics
2374 * CharacteristicsLength = Number of bytes in characteristics buffer
2375 * RETURNS:
2376 * Status of operation
2377 */
2378 {
2379 UINT MinSize;
2380 PNDIS_M_DRIVER_BLOCK Miniport = GET_MINIPORT_DRIVER(NdisWrapperHandle);
2381 PNDIS_M_DRIVER_BLOCK *MiniportPtr;
2382 NTSTATUS Status;
2383
2384 NDIS_DbgPrint(MAX_TRACE, ("Called.\n"));
2385
2386 switch (MiniportCharacteristics->MajorNdisVersion)
2387 {
2388 case 0x03:
2389 MinSize = sizeof(NDIS30_MINIPORT_CHARACTERISTICS);
2390 break;
2391
2392 case 0x04:
2393 MinSize = sizeof(NDIS40_MINIPORT_CHARACTERISTICS);
2394 break;
2395
2396 case 0x05:
2397 MinSize = sizeof(NDIS50_MINIPORT_CHARACTERISTICS);
2398 break;
2399
2400 default:
2401 NDIS_DbgPrint(MIN_TRACE, ("Bad miniport characteristics version.\n"));
2402 return NDIS_STATUS_BAD_VERSION;
2403 }
2404
2405 NDIS_DbgPrint(MIN_TRACE, ("Initializing an NDIS %u.%u miniport\n",
2406 MiniportCharacteristics->MajorNdisVersion,
2407 MiniportCharacteristics->MinorNdisVersion));
2408
2409 if (CharacteristicsLength < MinSize)
2410 {
2411 NDIS_DbgPrint(MIN_TRACE, ("Bad miniport characteristics length.\n"));
2412 return NDIS_STATUS_BAD_CHARACTERISTICS;
2413 }
2414
2415 /* Check if mandatory MiniportXxx functions are specified */
2416 if ((!MiniportCharacteristics->HaltHandler) ||
2417 (!MiniportCharacteristics->InitializeHandler)||
2418 (!MiniportCharacteristics->ResetHandler))
2419 {
2420 NDIS_DbgPrint(MIN_TRACE, ("Bad miniport characteristics.\n"));
2421 return NDIS_STATUS_BAD_CHARACTERISTICS;
2422 }
2423
2424 if (MiniportCharacteristics->MajorNdisVersion < 0x05)
2425 {
2426 if ((!MiniportCharacteristics->QueryInformationHandler) ||
2427 (!MiniportCharacteristics->SetInformationHandler))
2428 {
2429 NDIS_DbgPrint(MIN_TRACE, ("Bad miniport characteristics. (Set/Query)\n"));
2430 return NDIS_STATUS_BAD_CHARACTERISTICS;
2431 }
2432 }
2433 else
2434 {
2435 if (((!MiniportCharacteristics->QueryInformationHandler) ||
2436 (!MiniportCharacteristics->SetInformationHandler)) &&
2437 (!MiniportCharacteristics->CoRequestHandler))
2438 {
2439 NDIS_DbgPrint(MIN_TRACE, ("Bad miniport characteristics. (Set/Query)\n"));
2440 return NDIS_STATUS_BAD_CHARACTERISTICS;
2441 }
2442 }
2443
2444 if (MiniportCharacteristics->MajorNdisVersion == 0x03)
2445 {
2446 if (!MiniportCharacteristics->SendHandler)
2447 {
2448 NDIS_DbgPrint(MIN_TRACE, ("Bad miniport characteristics. (NDIS 3.0)\n"));
2449 return NDIS_STATUS_BAD_CHARACTERISTICS;
2450 }
2451 }
2452 else if (MiniportCharacteristics->MajorNdisVersion == 0x04)
2453 {
2454 /* NDIS 4.0 */
2455 if ((!MiniportCharacteristics->SendHandler) &&
2456 (!MiniportCharacteristics->SendPacketsHandler))
2457 {
2458 NDIS_DbgPrint(MIN_TRACE, ("Bad miniport characteristics. (NDIS 4.0)\n"));
2459 return NDIS_STATUS_BAD_CHARACTERISTICS;
2460 }
2461 }
2462 else if (MiniportCharacteristics->MajorNdisVersion == 0x05)
2463 {
2464 /* TODO: Add more checks here */
2465
2466 if ((!MiniportCharacteristics->SendHandler) &&
2467 (!MiniportCharacteristics->SendPacketsHandler) &&
2468 (!MiniportCharacteristics->CoSendPacketsHandler))
2469 {
2470 NDIS_DbgPrint(MIN_TRACE, ("Bad miniport characteristics. (NDIS 5.0)\n"));
2471 return NDIS_STATUS_BAD_CHARACTERISTICS;
2472 }
2473 }
2474
2475 /* TODO: verify NDIS5 and NDIS5.1 */
2476
2477 RtlCopyMemory(&Miniport->MiniportCharacteristics, MiniportCharacteristics, MinSize);
2478
2479 /*
2480 * NOTE: This is VERY unoptimal! Should we store the NDIS_M_DRIVER_BLOCK
2481 * structure in the driver extension or what?
2482 */
2483
2484 Status = IoAllocateDriverObjectExtension(Miniport->DriverObject, (PVOID)TAG('D','I','M','N'),
2485 sizeof(PNDIS_M_DRIVER_BLOCK), (PVOID*)&MiniportPtr);
2486 if (!NT_SUCCESS(Status))
2487 {
2488 NDIS_DbgPrint(MIN_TRACE, ("Can't allocate driver object extension.\n"));
2489 return NDIS_STATUS_RESOURCES;
2490 }
2491
2492 *MiniportPtr = Miniport;
2493
2494 Miniport->DriverObject->MajorFunction[IRP_MJ_PNP] = NdisIDispatchPnp;
2495 Miniport->DriverObject->MajorFunction[IRP_MJ_SHUTDOWN] = NdisIShutdown;
2496 Miniport->DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = NdisIDeviceIoControl;
2497 Miniport->DriverObject->DriverExtension->AddDevice = NdisIAddDevice;
2498
2499 return NDIS_STATUS_SUCCESS;
2500 }
2501
2502 \f
2503 /*
2504 * @implemented
2505 */
2506 #undef NdisMResetComplete
2507 VOID
2508 EXPORT
2509 NdisMResetComplete(
2510 IN NDIS_HANDLE MiniportAdapterHandle,
2511 IN NDIS_STATUS Status,
2512 IN BOOLEAN AddressingReset)
2513 {
2514 MiniResetComplete(MiniportAdapterHandle, Status, AddressingReset);
2515 }
2516
2517 \f
2518 /*
2519 * @implemented
2520 */
2521 #undef NdisMSendComplete
2522 VOID
2523 EXPORT
2524 NdisMSendComplete(
2525 IN NDIS_HANDLE MiniportAdapterHandle,
2526 IN PNDIS_PACKET Packet,
2527 IN NDIS_STATUS Status)
2528 /*
2529 * FUNCTION: Forwards a message to the initiating protocol saying
2530 * that a packet was handled
2531 * ARGUMENTS:
2532 * NdisAdapterHandle = Handle input to MiniportInitialize
2533 * Packet = Pointer to NDIS packet that was sent
2534 * Status = Status of send operation
2535 */
2536 {
2537 MiniSendComplete(MiniportAdapterHandle, Packet, Status);
2538 }
2539
2540 \f
2541 /*
2542 * @implemented
2543 */
2544 #undef NdisMSendResourcesAvailable
2545 VOID
2546 EXPORT
2547 NdisMSendResourcesAvailable(
2548 IN NDIS_HANDLE MiniportAdapterHandle)
2549 {
2550 MiniSendResourcesAvailable(MiniportAdapterHandle);
2551 }
2552
2553 \f
2554 /*
2555 * @implemented
2556 */
2557 #undef NdisMTransferDataComplete
2558 VOID
2559 EXPORT
2560 NdisMTransferDataComplete(
2561 IN NDIS_HANDLE MiniportAdapterHandle,
2562 IN PNDIS_PACKET Packet,
2563 IN NDIS_STATUS Status,
2564 IN UINT BytesTransferred)
2565 {
2566 MiniTransferDataComplete(MiniportAdapterHandle, Packet, Status, BytesTransferred);
2567 }
2568
2569 \f
2570 /*
2571 * @implemented
2572 */
2573 #undef NdisMSetAttributes
2574 VOID
2575 EXPORT
2576 NdisMSetAttributes(
2577 IN NDIS_HANDLE MiniportAdapterHandle,
2578 IN NDIS_HANDLE MiniportAdapterContext,
2579 IN BOOLEAN BusMaster,
2580 IN NDIS_INTERFACE_TYPE AdapterType)
2581 /*
2582 * FUNCTION: Informs the NDIS library of significant features of the caller's NIC
2583 * ARGUMENTS:
2584 * MiniportAdapterHandle = Handle input to MiniportInitialize
2585 * MiniportAdapterContext = Pointer to context information
2586 * BusMaster = Specifies TRUE if the caller's NIC is a busmaster DMA device
2587 * AdapterType = Specifies the I/O bus interface of the caller's NIC
2588 */
2589 {
2590 NDIS_DbgPrint(MAX_TRACE, ("Called.\n"));
2591 NdisMSetAttributesEx(MiniportAdapterHandle, MiniportAdapterContext, 0,
2592 BusMaster ? NDIS_ATTRIBUTE_BUS_MASTER : 0,
2593 AdapterType);
2594 }
2595
2596 \f
2597 /*
2598 * @implemented
2599 */
2600 VOID
2601 EXPORT
2602 NdisMSetAttributesEx(
2603 IN NDIS_HANDLE MiniportAdapterHandle,
2604 IN NDIS_HANDLE MiniportAdapterContext,
2605 IN UINT CheckForHangTimeInSeconds OPTIONAL,
2606 IN ULONG AttributeFlags,
2607 IN NDIS_INTERFACE_TYPE AdapterType)
2608 /*
2609 * FUNCTION: Informs the NDIS library of significant features of the caller's NIC
2610 * ARGUMENTS:
2611 * MiniportAdapterHandle = Handle input to MiniportInitialize
2612 * MiniportAdapterContext = Pointer to context information
2613 * CheckForHangTimeInSeconds = Specifies interval in seconds at which
2614 * MiniportCheckForHang should be called
2615 * AttributeFlags = Bitmask that indicates specific attributes
2616 * AdapterType = Specifies the I/O bus interface of the caller's NIC
2617 */
2618 {
2619 PLOGICAL_ADAPTER Adapter = GET_LOGICAL_ADAPTER(MiniportAdapterHandle);
2620
2621 NDIS_DbgPrint(MAX_TRACE, ("Called.\n"));
2622
2623 Adapter->NdisMiniportBlock.MiniportAdapterContext = MiniportAdapterContext;
2624 Adapter->NdisMiniportBlock.Flags = AttributeFlags;
2625 Adapter->NdisMiniportBlock.AdapterType = AdapterType;
2626 if (CheckForHangTimeInSeconds > 0)
2627 Adapter->NdisMiniportBlock.CheckForHangSeconds = CheckForHangTimeInSeconds;
2628 if (AttributeFlags & NDIS_ATTRIBUTE_INTERMEDIATE_DRIVER)
2629 NDIS_DbgPrint(MAX_TRACE, ("Intermediate drivers not supported yet.\n"));
2630
2631
2632 if (Adapter->NdisMiniportBlock.DriverHandle->MiniportCharacteristics.AdapterShutdownHandler)
2633 {
2634 NDIS_DbgPrint(MAX_TRACE, ("Miniport set AdapterShutdownHandler in MiniportCharacteristics\n"));
2635 NdisMRegisterAdapterShutdownHandler(Adapter,
2636 Adapter->NdisMiniportBlock.MiniportAdapterContext,
2637 Adapter->NdisMiniportBlock.DriverHandle->MiniportCharacteristics.AdapterShutdownHandler);
2638 }
2639 }
2640
2641 \f
2642 /*
2643 * @implemented
2644 */
2645 VOID
2646 EXPORT
2647 NdisMSleep(
2648 IN ULONG MicrosecondsToSleep)
2649 /*
2650 * FUNCTION: delay the thread's execution for MicrosecondsToSleep
2651 * ARGUMENTS:
2652 * MicrosecondsToSleep: duh...
2653 * NOTES:
2654 * - Because this is a blocking call, current IRQL must be < DISPATCH_LEVEL
2655 */
2656 {
2657 KTIMER Timer;
2658 LARGE_INTEGER DueTime;
2659
2660 PAGED_CODE();
2661
2662 DueTime.QuadPart = (-1) * 10 * MicrosecondsToSleep;
2663
2664 KeInitializeTimer(&Timer);
2665 KeSetTimer(&Timer, DueTime, 0);
2666 KeWaitForSingleObject(&Timer, Executive, KernelMode, FALSE, 0);
2667 }
2668
2669 \f
2670 /*
2671 * @implemented
2672 */
2673 BOOLEAN
2674 EXPORT
2675 NdisMSynchronizeWithInterrupt(
2676 IN PNDIS_MINIPORT_INTERRUPT Interrupt,
2677 IN PVOID SynchronizeFunction,
2678 IN PVOID SynchronizeContext)
2679 {
2680 return(KeSynchronizeExecution(Interrupt->InterruptObject,
2681 (PKSYNCHRONIZE_ROUTINE)SynchronizeFunction,
2682 SynchronizeContext));
2683 }
2684
2685 \f
2686 /*
2687 * @unimplemented
2688 */
2689 NDIS_STATUS
2690 EXPORT
2691 NdisMWriteLogData(
2692 IN NDIS_HANDLE LogHandle,
2693 IN PVOID LogBuffer,
2694 IN UINT LogBufferSize)
2695 {
2696 PUCHAR Buffer = LogBuffer;
2697 UINT i, j, idx;
2698
2699 UNIMPLEMENTED;
2700 for (i = 0; i < LogBufferSize; i += 16)
2701 {
2702 DbgPrint("%08x |", i);
2703 for (j = 0; j < 16; j++)
2704 {
2705 idx = i + j;
2706 if (idx < LogBufferSize)
2707 DbgPrint(" %02x", Buffer[idx]);
2708 else
2709 DbgPrint(" ");
2710 }
2711 DbgPrint(" | ");
2712 for (j = 0; j < 16; j++)
2713 {
2714 idx = i + j;
2715 if (idx == LogBufferSize)
2716 break;
2717 if (Buffer[idx] >= ' ') /* FIXME: not portable! replace by if (isprint(Buffer[idx])) ? */
2718 DbgPrint("%c", Buffer[idx]);
2719 else
2720 DbgPrint(".");
2721 }
2722 DbgPrint("\n");
2723 }
2724
2725 return NDIS_STATUS_FAILURE;
2726 }
2727
2728 \f
2729 /*
2730 * @implemented
2731 */
2732 VOID
2733 EXPORT
2734 NdisTerminateWrapper(
2735 IN NDIS_HANDLE NdisWrapperHandle,
2736 IN PVOID SystemSpecific)
2737 /*
2738 * FUNCTION: Releases resources allocated by a call to NdisInitializeWrapper
2739 * ARGUMENTS:
2740 * NdisWrapperHandle = Handle returned by NdisInitializeWrapper (NDIS_M_DRIVER_BLOCK)
2741 * SystemSpecific = Always NULL
2742 */
2743 {
2744 PNDIS_M_DRIVER_BLOCK Miniport = GET_MINIPORT_DRIVER(NdisWrapperHandle);
2745
2746 NDIS_DbgPrint(MAX_TRACE, ("Called.\n"));
2747
2748 ExFreePool(Miniport->RegistryPath->Buffer);
2749 ExFreePool(Miniport->RegistryPath);
2750 ExInterlockedRemoveEntryList(&Miniport->ListEntry, &MiniportListLock);
2751 ExFreePool(Miniport);
2752 }
2753
2754
2755 /*
2756 * @implemented
2757 */
2758 NDIS_STATUS
2759 EXPORT
2760 NdisMQueryAdapterInstanceName(
2761 OUT PNDIS_STRING AdapterInstanceName,
2762 IN NDIS_HANDLE MiniportAdapterHandle)
2763 /*
2764 * FUNCTION:
2765 * ARGUMENTS:
2766 * NOTES:
2767 * NDIS 5.0
2768 */
2769 {
2770 PLOGICAL_ADAPTER Adapter = (PLOGICAL_ADAPTER)MiniportAdapterHandle;
2771 UNICODE_STRING AdapterName;
2772
2773 NDIS_DbgPrint(MAX_TRACE, ("Called.\n"));
2774
2775 AdapterName.Length = 0;
2776 AdapterName.MaximumLength = Adapter->NdisMiniportBlock.MiniportName.MaximumLength;
2777 AdapterName.Buffer = ExAllocatePool(PagedPool, AdapterName.MaximumLength);
2778 if (!AdapterName.Buffer) {
2779 NDIS_DbgPrint(MIN_TRACE, ("Insufficient resources\n"));
2780 return NDIS_STATUS_RESOURCES;
2781 }
2782
2783 RtlCopyUnicodeString(&AdapterName, &Adapter->NdisMiniportBlock.MiniportName);
2784
2785 *AdapterInstanceName = AdapterName;
2786
2787 return NDIS_STATUS_SUCCESS;
2788 }
2789
2790 /*
2791 * @implemented
2792 */
2793 VOID
2794 EXPORT
2795 NdisDeregisterAdapterShutdownHandler(
2796 IN NDIS_HANDLE NdisAdapterHandle)
2797 /*
2798 * FUNCTION:
2799 * ARGUMENTS:
2800 * NOTES:
2801 * NDIS 4.0
2802 */
2803 {
2804 NdisMDeregisterAdapterShutdownHandler(NdisAdapterHandle);
2805 }
2806
2807
2808 /*
2809 * @implemented
2810 */
2811 VOID
2812 EXPORT
2813 NdisRegisterAdapterShutdownHandler(
2814 IN NDIS_HANDLE NdisAdapterHandle,
2815 IN PVOID ShutdownContext,
2816 IN ADAPTER_SHUTDOWN_HANDLER ShutdownHandler)
2817 /*
2818 * FUNCTION:
2819 * ARGUMENTS:
2820 * NOTES:
2821 * NDIS 4.0
2822 */
2823 {
2824 NdisMRegisterAdapterShutdownHandler(NdisAdapterHandle,
2825 ShutdownContext,
2826 ShutdownHandler);
2827 }
2828
2829 /*
2830 * @implemented
2831 */
2832 VOID
2833 EXPORT
2834 NdisMGetDeviceProperty(
2835 IN NDIS_HANDLE MiniportAdapterHandle,
2836 IN OUT PDEVICE_OBJECT *PhysicalDeviceObject OPTIONAL,
2837 IN OUT PDEVICE_OBJECT *FunctionalDeviceObject OPTIONAL,
2838 IN OUT PDEVICE_OBJECT *NextDeviceObject OPTIONAL,
2839 IN OUT PCM_RESOURCE_LIST *AllocatedResources OPTIONAL,
2840 IN OUT PCM_RESOURCE_LIST *AllocatedResourcesTranslated OPTIONAL)
2841 /*
2842 * FUNCTION:
2843 * ARGUMENTS:
2844 * NOTES:
2845 * NDIS 5.0
2846 */
2847 {
2848 PLOGICAL_ADAPTER Adapter = MiniportAdapterHandle;
2849
2850 NDIS_DbgPrint(MAX_TRACE, ("Called\n"));
2851
2852 if (PhysicalDeviceObject != NULL)
2853 *PhysicalDeviceObject = Adapter->NdisMiniportBlock.PhysicalDeviceObject;
2854
2855 if (FunctionalDeviceObject != NULL)
2856 *FunctionalDeviceObject = Adapter->NdisMiniportBlock.DeviceObject;
2857
2858 if (NextDeviceObject != NULL)
2859 *NextDeviceObject = Adapter->NdisMiniportBlock.NextDeviceObject;
2860
2861 if (AllocatedResources != NULL)
2862 *AllocatedResources = Adapter->NdisMiniportBlock.AllocatedResources;
2863
2864 if (AllocatedResourcesTranslated != NULL)
2865 *AllocatedResourcesTranslated = Adapter->NdisMiniportBlock.AllocatedResourcesTranslated;
2866 }
2867
2868 /*
2869 * @implemented
2870 */
2871 VOID
2872 EXPORT
2873 NdisMRegisterUnloadHandler(
2874 IN NDIS_HANDLE NdisWrapperHandle,
2875 IN PDRIVER_UNLOAD UnloadHandler)
2876 /*
2877 * FUNCTION:
2878 * ARGUMENTS:
2879 * NOTES:
2880 * NDIS 5.0
2881 */
2882 {
2883 PNDIS_M_DRIVER_BLOCK DriverBlock = NdisWrapperHandle;
2884
2885 NDIS_DbgPrint(MAX_TRACE, ("Miniport registered unload handler\n"));
2886
2887 DriverBlock->DriverObject->DriverUnload = UnloadHandler;
2888 }
2889
2890 /*
2891 * @implemented
2892 */
2893 NDIS_STATUS
2894 EXPORT
2895 NdisMRegisterDevice(
2896 IN NDIS_HANDLE NdisWrapperHandle,
2897 IN PNDIS_STRING DeviceName,
2898 IN PNDIS_STRING SymbolicName,
2899 IN PDRIVER_DISPATCH MajorFunctions[],
2900 OUT PDEVICE_OBJECT *pDeviceObject,
2901 OUT NDIS_HANDLE *NdisDeviceHandle)
2902 /*
2903 * FUNCTION:
2904 * ARGUMENTS:
2905 * NOTES:
2906 * NDIS 5.0
2907 */
2908 {
2909 PNDIS_M_DRIVER_BLOCK DriverBlock = NdisWrapperHandle;
2910 PNDIS_M_DEVICE_BLOCK DeviceBlock;
2911 PDEVICE_OBJECT DeviceObject;
2912 NDIS_STATUS Status;
2913 UINT i;
2914
2915 NDIS_DbgPrint(MAX_TRACE, ("Called\n"));
2916
2917 Status = IoCreateDevice(DriverBlock->DriverObject,
2918 0, /* This space is reserved for us. Should we use it? */
2919 DeviceName,
2920 FILE_DEVICE_NETWORK,
2921 0,
2922 FALSE,
2923 &DeviceObject);
2924
2925 if (!NT_SUCCESS(Status))
2926 {
2927 NDIS_DbgPrint(MIN_TRACE, ("IoCreateDevice failed (%x)\n", Status));
2928 return Status;
2929 }
2930
2931 Status = IoCreateSymbolicLink(SymbolicName, DeviceName);
2932
2933 if (!NT_SUCCESS(Status))
2934 {
2935 NDIS_DbgPrint(MIN_TRACE, ("IoCreateSymbolicLink failed (%x)\n", Status));
2936 IoDeleteDevice(DeviceObject);
2937 return Status;
2938 }
2939
2940 DeviceBlock = ExAllocatePool(NonPagedPool, sizeof(NDIS_M_DEVICE_BLOCK));
2941
2942 if (!DeviceBlock)
2943 {
2944 NDIS_DbgPrint(MIN_TRACE, ("Insufficient resources\n"));
2945 IoDeleteDevice(DeviceObject);
2946 IoDeleteSymbolicLink(SymbolicName);
2947 return NDIS_STATUS_RESOURCES;
2948 }
2949
2950 for (i = 0; i <= IRP_MJ_MAXIMUM_FUNCTION; i++)
2951 DriverBlock->DriverObject->MajorFunction[i] = MajorFunctions[i];
2952
2953 DriverBlock->DriverObject->MajorFunction[IRP_MJ_PNP] = NdisIDispatchPnp;
2954
2955 DeviceBlock->DeviceObject = DeviceObject;
2956 DeviceBlock->SymbolicName = SymbolicName;
2957
2958 *pDeviceObject = DeviceObject;
2959 *NdisDeviceHandle = DeviceBlock;
2960
2961 return NDIS_STATUS_SUCCESS;
2962 }
2963
2964 /*
2965 * @implemented
2966 */
2967 NDIS_STATUS
2968 EXPORT
2969 NdisMDeregisterDevice(
2970 IN NDIS_HANDLE NdisDeviceHandle)
2971 /*
2972 * FUNCTION:
2973 * ARGUMENTS:
2974 * NOTES:
2975 * NDIS 5.0
2976 */
2977 {
2978 PNDIS_M_DEVICE_BLOCK DeviceBlock = NdisDeviceHandle;
2979
2980 IoDeleteDevice(DeviceBlock->DeviceObject);
2981
2982 IoDeleteSymbolicLink(DeviceBlock->SymbolicName);
2983
2984 ExFreePool(DeviceBlock);
2985
2986 return NDIS_STATUS_SUCCESS;
2987 }
2988
2989 /*
2990 * @implemented
2991 */
2992 NDIS_STATUS
2993 EXPORT
2994 NdisQueryAdapterInstanceName(
2995 OUT PNDIS_STRING AdapterInstanceName,
2996 IN NDIS_HANDLE NdisBindingHandle)
2997 /*
2998 * FUNCTION:
2999 * ARGUMENTS:
3000 * NOTES:
3001 * NDIS 5.0
3002 */
3003 {
3004 PADAPTER_BINDING AdapterBinding = NdisBindingHandle;
3005 PLOGICAL_ADAPTER Adapter = AdapterBinding->Adapter;
3006
3007 return NdisMQueryAdapterInstanceName(AdapterInstanceName,
3008 Adapter);
3009 }
3010
3011 /*
3012 * @implemented
3013 */
3014 VOID
3015 EXPORT
3016 NdisCompletePnPEvent(
3017 IN NDIS_STATUS Status,
3018 IN NDIS_HANDLE NdisBindingHandle,
3019 IN PNET_PNP_EVENT NetPnPEvent)
3020 /*
3021 * FUNCTION:
3022 * ARGUMENTS:
3023 * NOTES:
3024 * NDIS 5.0
3025 */
3026 {
3027 PIRP Irp = (PIRP)NetPnPEvent->NdisReserved[0];
3028 PLIST_ENTRY CurrentEntry = (PLIST_ENTRY)NetPnPEvent->NdisReserved[1];
3029 PADAPTER_BINDING AdapterBinding = NdisBindingHandle;
3030 PLOGICAL_ADAPTER Adapter = AdapterBinding->Adapter;
3031 NDIS_STATUS NdisStatus;
3032
3033 if (Status != NDIS_STATUS_SUCCESS)
3034 {
3035 if (NetPnPEvent->Buffer) ExFreePool(NetPnPEvent->Buffer);
3036 ExFreePool(NetPnPEvent);
3037 Irp->IoStatus.Status = Status;
3038 IoCompleteRequest(Irp, IO_NO_INCREMENT);
3039 return;
3040 }
3041
3042 while (CurrentEntry != &Adapter->ProtocolListHead)
3043 {
3044 AdapterBinding = CONTAINING_RECORD(CurrentEntry, ADAPTER_BINDING, AdapterListEntry);
3045
3046 NdisStatus = (*AdapterBinding->ProtocolBinding->Chars.PnPEventHandler)(
3047 AdapterBinding->NdisOpenBlock.ProtocolBindingContext,
3048 NetPnPEvent);
3049
3050 if (NdisStatus == NDIS_STATUS_PENDING)
3051 {
3052 NetPnPEvent->NdisReserved[1] = (ULONG_PTR)CurrentEntry->Flink;
3053 return;
3054 }
3055 else if (NdisStatus != NDIS_STATUS_SUCCESS)
3056 {
3057 if (NetPnPEvent->Buffer) ExFreePool(NetPnPEvent->Buffer);
3058 ExFreePool(NetPnPEvent);
3059 Irp->IoStatus.Status = NdisStatus;
3060 IoCompleteRequest(Irp, IO_NO_INCREMENT);
3061 return;
3062 }
3063
3064 CurrentEntry = CurrentEntry->Flink;
3065 }
3066
3067 if (NetPnPEvent->Buffer) ExFreePool(NetPnPEvent->Buffer);
3068 ExFreePool(NetPnPEvent);
3069
3070 Irp->IoStatus.Status = NDIS_STATUS_SUCCESS;
3071 IoCompleteRequest(Irp, IO_NO_INCREMENT);
3072 }
3073
3074 /*
3075 * @implemented
3076 */
3077 VOID
3078 EXPORT
3079 NdisCancelSendPackets(
3080 IN NDIS_HANDLE NdisBindingHandle,
3081 IN PVOID CancelId)
3082 {
3083 PADAPTER_BINDING AdapterBinding = NdisBindingHandle;
3084 PLOGICAL_ADAPTER Adapter = AdapterBinding->Adapter;
3085
3086 NDIS_DbgPrint(MAX_TRACE, ("Called for ID %x.\n", CancelId));
3087
3088 if (Adapter->NdisMiniportBlock.DriverHandle->MiniportCharacteristics.CancelSendPacketsHandler)
3089 {
3090 (*Adapter->NdisMiniportBlock.DriverHandle->MiniportCharacteristics.CancelSendPacketsHandler)(
3091 Adapter->NdisMiniportBlock.MiniportAdapterContext,
3092 CancelId);
3093 }
3094 }
3095
3096
3097 /*
3098 * @implemented
3099 */
3100 NDIS_HANDLE
3101 EXPORT
3102 NdisIMGetBindingContext(
3103 IN NDIS_HANDLE NdisBindingHandle)
3104 /*
3105 * FUNCTION:
3106 * ARGUMENTS:
3107 * NOTES:
3108 * NDIS 5.0
3109 */
3110 {
3111 PADAPTER_BINDING AdapterBinding = NdisBindingHandle;
3112 PLOGICAL_ADAPTER Adapter = AdapterBinding->Adapter;
3113
3114 NDIS_DbgPrint(MAX_TRACE, ("Called.\n"));
3115
3116 return Adapter->NdisMiniportBlock.DeviceContext;
3117 }
3118
3119
3120 /*
3121 * @implemented
3122 */
3123 NDIS_HANDLE
3124 EXPORT
3125 NdisIMGetDeviceContext(
3126 IN NDIS_HANDLE MiniportAdapterHandle)
3127 /*
3128 * FUNCTION:
3129 * ARGUMENTS:
3130 * NOTES:
3131 * NDIS 5.0
3132 */
3133 {
3134 PLOGICAL_ADAPTER Adapter = MiniportAdapterHandle;
3135
3136 NDIS_DbgPrint(MAX_TRACE, ("Called.\n"));
3137
3138 return Adapter->NdisMiniportBlock.DeviceContext;
3139 }
3140
3141 /* EOF */
3142