sync with trunk (r46275)
[reactos.git] / 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 DbgBreakPoint();
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 if (NdisStatus != NDIS_STATUS_SUCCESS)
1854 {
1855 NDIS_DbgPrint(MIN_TRACE, ("Failed to open configuration key\n"));
1856 ExInterlockedRemoveEntryList( &Adapter->ListEntry, &AdapterListLock );
1857 return NdisStatus;
1858 }
1859
1860 Size = sizeof(ULONG);
1861 Status = IoGetDeviceProperty(Adapter->NdisMiniportBlock.PhysicalDeviceObject,
1862 DevicePropertyLegacyBusType, Size,
1863 &Adapter->NdisMiniportBlock.BusType, &Size);
1864 if (!NT_SUCCESS(Status) || (INTERFACE_TYPE)Adapter->NdisMiniportBlock.BusType == InterfaceTypeUndefined)
1865 {
1866 NdisInitUnicodeString(&ParamName, L"BusType");
1867 NdisReadConfiguration(&NdisStatus, &ConfigParam, ConfigHandle,
1868 &ParamName, NdisParameterInteger);
1869 if (NdisStatus == NDIS_STATUS_SUCCESS)
1870 Adapter->NdisMiniportBlock.BusType = ConfigParam->ParameterData.IntegerData;
1871 else
1872 Adapter->NdisMiniportBlock.BusType = Isa;
1873 }
1874
1875 Status = IoGetDeviceProperty(Adapter->NdisMiniportBlock.PhysicalDeviceObject,
1876 DevicePropertyBusNumber, Size,
1877 &Adapter->NdisMiniportBlock.BusNumber, &Size);
1878 if (!NT_SUCCESS(Status) || Adapter->NdisMiniportBlock.BusNumber == 0xFFFFFFF0)
1879 {
1880 NdisInitUnicodeString(&ParamName, L"BusNumber");
1881 NdisReadConfiguration(&NdisStatus, &ConfigParam, ConfigHandle,
1882 &ParamName, NdisParameterInteger);
1883 if (NdisStatus == NDIS_STATUS_SUCCESS)
1884 Adapter->NdisMiniportBlock.BusNumber = ConfigParam->ParameterData.IntegerData;
1885 else
1886 Adapter->NdisMiniportBlock.BusNumber = 0;
1887 }
1888 WrapperContext.BusNumber = Adapter->NdisMiniportBlock.BusNumber;
1889
1890 Status = IoGetDeviceProperty(Adapter->NdisMiniportBlock.PhysicalDeviceObject,
1891 DevicePropertyAddress, Size,
1892 &Adapter->NdisMiniportBlock.SlotNumber, &Size);
1893 if (!NT_SUCCESS(Status) || Adapter->NdisMiniportBlock.SlotNumber == (NDIS_INTERFACE_TYPE)-1)
1894 {
1895 NdisInitUnicodeString(&ParamName, L"SlotNumber");
1896 NdisReadConfiguration(&NdisStatus, &ConfigParam, ConfigHandle,
1897 &ParamName, NdisParameterInteger);
1898 if (NdisStatus == NDIS_STATUS_SUCCESS)
1899 Adapter->NdisMiniportBlock.SlotNumber = ConfigParam->ParameterData.IntegerData;
1900 else
1901 Adapter->NdisMiniportBlock.SlotNumber = 0;
1902 }
1903 else
1904 {
1905 /* Convert slotnumber to PCI_SLOT_NUMBER */
1906 ULONG PciSlotNumber = Adapter->NdisMiniportBlock.SlotNumber;
1907 PCI_SLOT_NUMBER SlotNumber;
1908
1909 SlotNumber.u.AsULONG = 0;
1910 SlotNumber.u.bits.DeviceNumber = (PciSlotNumber >> 16) & 0xFFFF;
1911 SlotNumber.u.bits.FunctionNumber = PciSlotNumber & 0xFFFF;
1912
1913 Adapter->NdisMiniportBlock.SlotNumber = SlotNumber.u.AsULONG;
1914 }
1915 WrapperContext.SlotNumber = Adapter->NdisMiniportBlock.SlotNumber;
1916
1917 NdisCloseConfiguration(ConfigHandle);
1918
1919 /* Set handlers (some NDIS macros require these) */
1920 Adapter->NdisMiniportBlock.EthRxCompleteHandler = EthFilterDprIndicateReceiveComplete;
1921 Adapter->NdisMiniportBlock.EthRxIndicateHandler = EthFilterDprIndicateReceive;
1922 Adapter->NdisMiniportBlock.SendCompleteHandler = MiniSendComplete;
1923 Adapter->NdisMiniportBlock.SendResourcesHandler = MiniSendResourcesAvailable;
1924 Adapter->NdisMiniportBlock.ResetCompleteHandler = MiniResetComplete;
1925 Adapter->NdisMiniportBlock.TDCompleteHandler = MiniTransferDataComplete;
1926 Adapter->NdisMiniportBlock.PacketIndicateHandler= MiniIndicateReceivePacket;
1927 Adapter->NdisMiniportBlock.StatusHandler = MiniStatus;
1928 Adapter->NdisMiniportBlock.StatusCompleteHandler= MiniStatusComplete;
1929 Adapter->NdisMiniportBlock.SendPacketsHandler = ProSendPackets;
1930 Adapter->NdisMiniportBlock.QueryCompleteHandler = MiniRequestComplete;
1931 Adapter->NdisMiniportBlock.SetCompleteHandler = MiniRequestComplete;
1932
1933 /*
1934 * Call MiniportInitialize.
1935 */
1936
1937 NDIS_DbgPrint(MID_TRACE, ("calling MiniportInitialize\n"));
1938 NdisStatus = (*Adapter->NdisMiniportBlock.DriverHandle->MiniportCharacteristics.InitializeHandler)(
1939 &OpenErrorStatus, &SelectedMediumIndex, &MediaArray[0],
1940 MEDIA_ARRAY_SIZE, Adapter, (NDIS_HANDLE)&WrapperContext);
1941
1942 ZwClose(WrapperContext.RegistryHandle);
1943
1944 if (NdisStatus != NDIS_STATUS_SUCCESS)
1945 {
1946 NDIS_DbgPrint(MIN_TRACE, ("MiniportInitialize() failed for an adapter.\n"));
1947 ExInterlockedRemoveEntryList( &Adapter->ListEntry, &AdapterListLock );
1948 return NdisStatus;
1949 }
1950
1951 if (SelectedMediumIndex >= MEDIA_ARRAY_SIZE)
1952 {
1953 NDIS_DbgPrint(MIN_TRACE, ("MiniportInitialize() selected a bad index\n"));
1954 ExInterlockedRemoveEntryList( &Adapter->ListEntry, &AdapterListLock );
1955 return NDIS_STATUS_UNSUPPORTED_MEDIA;
1956 }
1957
1958 Adapter->NdisMiniportBlock.MediaType = MediaArray[SelectedMediumIndex];
1959
1960 switch (Adapter->NdisMiniportBlock.MediaType)
1961 {
1962 case NdisMedium802_3:
1963 Adapter->MediumHeaderSize = 14; /* XXX figure out what to do about LLC */
1964 AddressOID = OID_802_3_CURRENT_ADDRESS;
1965 Adapter->AddressLength = ETH_LENGTH_OF_ADDRESS;
1966 NdisStatus = DoQueries(Adapter, AddressOID);
1967 if (NdisStatus == NDIS_STATUS_SUCCESS)
1968 {
1969 NdisStatus = MiniQueryInformation(Adapter, OID_802_3_MAXIMUM_LIST_SIZE, sizeof(UINT),
1970 &MaxMulticastAddresses, &BytesWritten);
1971
1972 if (NdisStatus != NDIS_STATUS_SUCCESS)
1973 {
1974 ExInterlockedRemoveEntryList( &Adapter->ListEntry, &AdapterListLock );
1975 NDIS_DbgPrint(MIN_TRACE, ("MiniQueryInformation failed (%x)\n", NdisStatus));
1976 return NdisStatus;
1977 }
1978
1979 Success = EthCreateFilter(MaxMulticastAddresses,
1980 Adapter->Address.Type.Medium802_3,
1981 &Adapter->NdisMiniportBlock.EthDB);
1982 if (Success)
1983 ((PETHI_FILTER)Adapter->NdisMiniportBlock.EthDB)->Miniport = (PNDIS_MINIPORT_BLOCK)Adapter;
1984 else
1985 NdisStatus = NDIS_STATUS_RESOURCES;
1986 }
1987 break;
1988
1989 default:
1990 /* FIXME: Support other types of media */
1991 NDIS_DbgPrint(MIN_TRACE, ("error: unsupported media\n"));
1992 ASSERT(FALSE);
1993 ExInterlockedRemoveEntryList( &Adapter->ListEntry, &AdapterListLock );
1994 return STATUS_UNSUCCESSFUL;
1995 }
1996
1997 if (NdisStatus != NDIS_STATUS_SUCCESS)
1998 {
1999 NDIS_DbgPrint(MIN_TRACE, ("couldn't create filter (%x)\n", NdisStatus));
2000 return NdisStatus;
2001 }
2002
2003 /* Check for a hang every two seconds if it wasn't set in MiniportInitialize */
2004 if (Adapter->NdisMiniportBlock.CheckForHangSeconds == 0)
2005 Adapter->NdisMiniportBlock.CheckForHangSeconds = 2;
2006
2007 Adapter->NdisMiniportBlock.OldPnPDeviceState = Adapter->NdisMiniportBlock.PnPDeviceState;
2008 Adapter->NdisMiniportBlock.PnPDeviceState = NdisPnPDeviceStarted;
2009
2010 IoSetDeviceInterfaceState(&Adapter->NdisMiniportBlock.SymbolicLinkName, TRUE);
2011
2012 Timeout.QuadPart = Int32x32To64(Adapter->NdisMiniportBlock.CheckForHangSeconds, -1000000);
2013 KeSetTimerEx(&Adapter->NdisMiniportBlock.WakeUpDpcTimer.Timer, Timeout,
2014 Adapter->NdisMiniportBlock.CheckForHangSeconds * 1000,
2015 &Adapter->NdisMiniportBlock.WakeUpDpcTimer.Dpc);
2016
2017 /* Put adapter in adapter list for this miniport */
2018 ExInterlockedInsertTailList(&Adapter->NdisMiniportBlock.DriverHandle->DeviceList, &Adapter->MiniportListEntry, &Adapter->NdisMiniportBlock.DriverHandle->Lock);
2019
2020 return STATUS_SUCCESS;
2021 }
2022
2023 \f
2024 NTSTATUS
2025 NTAPI
2026 NdisIPnPStopDevice(
2027 IN PDEVICE_OBJECT DeviceObject,
2028 PIRP Irp)
2029 /*
2030 * FUNCTION: Handle the PnP stop device event
2031 * ARGUMENTS:
2032 * DeviceObejct = Functional Device Object
2033 * Irp = IRP_MN_STOP_DEVICE I/O request packet
2034 * RETURNS:
2035 * Status of operation
2036 */
2037 {
2038 PLOGICAL_ADAPTER Adapter = (PLOGICAL_ADAPTER)DeviceObject->DeviceExtension;
2039
2040 /* Remove adapter from adapter list for this miniport */
2041 ExInterlockedRemoveEntryList(&Adapter->MiniportListEntry, &Adapter->NdisMiniportBlock.DriverHandle->Lock);
2042
2043 /* Remove adapter from global adapter list */
2044 ExInterlockedRemoveEntryList(&Adapter->ListEntry, &AdapterListLock);
2045
2046 KeCancelTimer(&Adapter->NdisMiniportBlock.WakeUpDpcTimer.Timer);
2047
2048 (*Adapter->NdisMiniportBlock.DriverHandle->MiniportCharacteristics.HaltHandler)(Adapter);
2049
2050 IoSetDeviceInterfaceState(&Adapter->NdisMiniportBlock.SymbolicLinkName, FALSE);
2051
2052 if (Adapter->NdisMiniportBlock.AllocatedResources)
2053 {
2054 ExFreePool(Adapter->NdisMiniportBlock.AllocatedResources);
2055 Adapter->NdisMiniportBlock.AllocatedResources = NULL;
2056 }
2057 if (Adapter->NdisMiniportBlock.AllocatedResourcesTranslated)
2058 {
2059 ExFreePool(Adapter->NdisMiniportBlock.AllocatedResourcesTranslated);
2060 Adapter->NdisMiniportBlock.AllocatedResourcesTranslated = NULL;
2061 }
2062
2063 if (Adapter->NdisMiniportBlock.Resources)
2064 {
2065 ExFreePool(Adapter->NdisMiniportBlock.Resources);
2066 Adapter->NdisMiniportBlock.Resources = NULL;
2067 }
2068
2069 if (Adapter->NdisMiniportBlock.EthDB)
2070 {
2071 EthDeleteFilter(Adapter->NdisMiniportBlock.EthDB);
2072 Adapter->NdisMiniportBlock.EthDB = NULL;
2073 }
2074
2075 Adapter->NdisMiniportBlock.OldPnPDeviceState = Adapter->NdisMiniportBlock.PnPDeviceState;
2076 Adapter->NdisMiniportBlock.PnPDeviceState = NdisPnPDeviceStopped;
2077
2078 return STATUS_SUCCESS;
2079 }
2080
2081 NTSTATUS
2082 NTAPI
2083 NdisIShutdown(
2084 IN PDEVICE_OBJECT DeviceObject,
2085 PIRP Irp)
2086 {
2087 PLOGICAL_ADAPTER Adapter = DeviceObject->DeviceExtension;
2088 PMINIPORT_BUGCHECK_CONTEXT Context = Adapter->BugcheckContext;
2089 ADAPTER_SHUTDOWN_HANDLER ShutdownHandler = Context->ShutdownHandler;
2090
2091 ASSERT(ShutdownHandler);
2092
2093 ShutdownHandler(Context->DriverContext);
2094
2095 Irp->IoStatus.Status = STATUS_SUCCESS;
2096 Irp->IoStatus.Information = 0;
2097
2098 IoCompleteRequest(Irp, IO_NO_INCREMENT);
2099
2100 return STATUS_SUCCESS;
2101 }
2102
2103 NTSTATUS
2104 NTAPI
2105 NdisIDeviceIoControl(
2106 IN PDEVICE_OBJECT DeviceObject,
2107 PIRP Irp)
2108 {
2109 PLOGICAL_ADAPTER Adapter = (PLOGICAL_ADAPTER)DeviceObject->DeviceExtension;
2110 PIO_STACK_LOCATION Stack = IoGetCurrentIrpStackLocation(Irp);
2111 NDIS_STATUS Status = STATUS_NOT_SUPPORTED;
2112
2113 Irp->IoStatus.Information = 0;
2114
2115 ASSERT(Adapter);
2116
2117 switch (Stack->Parameters.DeviceIoControl.IoControlCode)
2118 {
2119 case IOCTL_NDIS_QUERY_GLOBAL_STATS:
2120 Status = MiniQueryInformation(Adapter,
2121 *(PNDIS_OID)Irp->AssociatedIrp.SystemBuffer,
2122 Stack->Parameters.DeviceIoControl.OutputBufferLength,
2123 MmGetSystemAddressForMdl(Irp->MdlAddress),
2124 &Irp->IoStatus.Information);
2125 break;
2126
2127 default:
2128 ASSERT(FALSE);
2129 break;
2130 }
2131
2132 if (Status != NDIS_STATUS_PENDING)
2133 {
2134 Irp->IoStatus.Status = Status;
2135 IoCompleteRequest(Irp, IO_NO_INCREMENT);
2136 }
2137 else
2138 IoMarkIrpPending(Irp);
2139
2140 return Status;
2141 }
2142
2143 \f
2144 NTSTATUS
2145 NTAPI
2146 NdisIDispatchPnp(
2147 IN PDEVICE_OBJECT DeviceObject,
2148 PIRP Irp)
2149 {
2150 PIO_STACK_LOCATION Stack = IoGetCurrentIrpStackLocation(Irp);
2151 PLOGICAL_ADAPTER Adapter = (PLOGICAL_ADAPTER)DeviceObject->DeviceExtension;
2152 NTSTATUS Status;
2153
2154 switch (Stack->MinorFunction)
2155 {
2156 case IRP_MN_START_DEVICE:
2157 Status = NdisIForwardIrpAndWait(Adapter, Irp);
2158 if (NT_SUCCESS(Status) && NT_SUCCESS(Irp->IoStatus.Status))
2159 {
2160 Status = NdisIPnPStartDevice(DeviceObject, Irp);
2161 }
2162 else
2163 NDIS_DbgPrint(MIN_TRACE, ("Lower driver failed device start\n"));
2164 Irp->IoStatus.Status = Status;
2165 IoCompleteRequest(Irp, IO_NO_INCREMENT);
2166 break;
2167
2168 case IRP_MN_STOP_DEVICE:
2169 Status = NdisIForwardIrpAndWait(Adapter, Irp);
2170 if (NT_SUCCESS(Status) && NT_SUCCESS(Irp->IoStatus.Status))
2171 {
2172 Status = NdisIPnPStopDevice(DeviceObject, Irp);
2173 }
2174 else
2175 NDIS_DbgPrint(MIN_TRACE, ("Lower driver failed device stop\n"));
2176 Irp->IoStatus.Status = Status;
2177 IoCompleteRequest(Irp, IO_NO_INCREMENT);
2178 break;
2179
2180 case IRP_MN_QUERY_REMOVE_DEVICE:
2181 case IRP_MN_QUERY_STOP_DEVICE:
2182 Status = NdisIPnPQueryStopDevice(DeviceObject, Irp);
2183 Irp->IoStatus.Status = Status;
2184 IoCompleteRequest(Irp, IO_NO_INCREMENT);
2185 break;
2186
2187 case IRP_MN_CANCEL_REMOVE_DEVICE:
2188 case IRP_MN_CANCEL_STOP_DEVICE:
2189 Status = NdisIPnPCancelStopDevice(DeviceObject, Irp);
2190 Irp->IoStatus.Status = Status;
2191 IoCompleteRequest(Irp, IO_NO_INCREMENT);
2192 break;
2193
2194 case IRP_MN_QUERY_PNP_DEVICE_STATE:
2195 Status = NDIS_STATUS_SUCCESS;
2196 Irp->IoStatus.Status = Status;
2197 Irp->IoStatus.Information |= Adapter->NdisMiniportBlock.PnPFlags;
2198 IoCompleteRequest(Irp, IO_NO_INCREMENT);
2199 break;
2200
2201 default:
2202 IoSkipCurrentIrpStackLocation(Irp);
2203 Status = IoCallDriver(Adapter->NdisMiniportBlock.NextDeviceObject, Irp);
2204 break;
2205 }
2206
2207 return Status;
2208 }
2209
2210 \f
2211 NTSTATUS
2212 NTAPI
2213 NdisIAddDevice(
2214 IN PDRIVER_OBJECT DriverObject,
2215 IN PDEVICE_OBJECT PhysicalDeviceObject)
2216 /*
2217 * FUNCTION: Create a device for an adapter found using PnP
2218 * ARGUMENTS:
2219 * DriverObject = Pointer to the miniport driver object
2220 * PhysicalDeviceObject = Pointer to the PDO for our adapter
2221 */
2222 {
2223 static const WCHAR ClassKeyName[] = {'C','l','a','s','s','\\'};
2224 static const WCHAR LinkageKeyName[] = {'\\','L','i','n','k','a','g','e',0};
2225 PNDIS_M_DRIVER_BLOCK Miniport;
2226 PNDIS_M_DRIVER_BLOCK *MiniportPtr;
2227 WCHAR *LinkageKeyBuffer;
2228 ULONG DriverKeyLength;
2229 RTL_QUERY_REGISTRY_TABLE QueryTable[2];
2230 UNICODE_STRING ExportName;
2231 PDEVICE_OBJECT DeviceObject;
2232 PLOGICAL_ADAPTER Adapter;
2233 NTSTATUS Status;
2234
2235 /*
2236 * Gain the access to the miniport data structure first.
2237 */
2238
2239 MiniportPtr = IoGetDriverObjectExtension(DriverObject, (PVOID)'NMID');
2240 if (MiniportPtr == NULL)
2241 {
2242 NDIS_DbgPrint(MIN_TRACE, ("Can't get driver object extension.\n"));
2243 return NDIS_STATUS_FAILURE;
2244 }
2245 Miniport = *MiniportPtr;
2246
2247 /*
2248 * Get name of the Linkage registry key for our adapter. It's located under
2249 * the driver key for our driver and so we have basicly two ways to do it.
2250 * Either we can use IoOpenDriverRegistryKey or compose it using information
2251 * gathered by IoGetDeviceProperty. I choosed the second because
2252 * IoOpenDriverRegistryKey wasn't implemented at the time of writing.
2253 */
2254
2255 Status = IoGetDeviceProperty(PhysicalDeviceObject, DevicePropertyDriverKeyName,
2256 0, NULL, &DriverKeyLength);
2257 if (Status != STATUS_BUFFER_TOO_SMALL && Status != STATUS_BUFFER_OVERFLOW && Status != STATUS_SUCCESS)
2258 {
2259 NDIS_DbgPrint(MIN_TRACE, ("Can't get miniport driver key length.\n"));
2260 return Status;
2261 }
2262
2263 LinkageKeyBuffer = ExAllocatePool(PagedPool, DriverKeyLength +
2264 sizeof(ClassKeyName) + sizeof(LinkageKeyName));
2265 if (LinkageKeyBuffer == NULL)
2266 {
2267 NDIS_DbgPrint(MIN_TRACE, ("Can't allocate memory for driver key name.\n"));
2268 return STATUS_INSUFFICIENT_RESOURCES;
2269 }
2270
2271 Status = IoGetDeviceProperty(PhysicalDeviceObject, DevicePropertyDriverKeyName,
2272 DriverKeyLength, LinkageKeyBuffer +
2273 (sizeof(ClassKeyName) / sizeof(WCHAR)),
2274 &DriverKeyLength);
2275 if (!NT_SUCCESS(Status))
2276 {
2277 NDIS_DbgPrint(MIN_TRACE, ("Can't get miniport driver key.\n"));
2278 ExFreePool(LinkageKeyBuffer);
2279 return Status;
2280 }
2281
2282 /* Compose the linkage key name. */
2283 RtlCopyMemory(LinkageKeyBuffer, ClassKeyName, sizeof(ClassKeyName));
2284 RtlCopyMemory(LinkageKeyBuffer + ((sizeof(ClassKeyName) + DriverKeyLength) /
2285 sizeof(WCHAR)) - 1, LinkageKeyName, sizeof(LinkageKeyName));
2286
2287 NDIS_DbgPrint(DEBUG_MINIPORT, ("LinkageKey: %S.\n", LinkageKeyBuffer));
2288
2289 /*
2290 * Now open the linkage key and read the "Export" and "RootDevice" values
2291 * which contains device name and root service respectively.
2292 */
2293
2294 RtlZeroMemory(QueryTable, sizeof(QueryTable));
2295 RtlInitUnicodeString(&ExportName, NULL);
2296 QueryTable[0].Flags = RTL_QUERY_REGISTRY_REQUIRED | RTL_QUERY_REGISTRY_DIRECT;
2297 QueryTable[0].Name = L"Export";
2298 QueryTable[0].EntryContext = &ExportName;
2299
2300 Status = RtlQueryRegistryValues(RTL_REGISTRY_CONTROL, LinkageKeyBuffer,
2301 QueryTable, NULL, NULL);
2302 ExFreePool(LinkageKeyBuffer);
2303 if (!NT_SUCCESS(Status))
2304 {
2305 NDIS_DbgPrint(MIN_TRACE, ("Can't get miniport device name. (%x)\n", Status));
2306 return Status;
2307 }
2308
2309 /*
2310 * Create the device object.
2311 */
2312
2313 NDIS_DbgPrint(MAX_TRACE, ("creating device %wZ\n", &ExportName));
2314
2315 Status = IoCreateDevice(Miniport->DriverObject, sizeof(LOGICAL_ADAPTER),
2316 &ExportName, FILE_DEVICE_PHYSICAL_NETCARD,
2317 0, FALSE, &DeviceObject);
2318 if (!NT_SUCCESS(Status))
2319 {
2320 NDIS_DbgPrint(MIN_TRACE, ("Could not create device object.\n"));
2321 RtlFreeUnicodeString(&ExportName);
2322 return Status;
2323 }
2324
2325 /*
2326 * Initialize the adapter structure.
2327 */
2328
2329 Adapter = (PLOGICAL_ADAPTER)DeviceObject->DeviceExtension;
2330 KeInitializeSpinLock(&Adapter->NdisMiniportBlock.Lock);
2331 InitializeListHead(&Adapter->ProtocolListHead);
2332
2333 Status = IoRegisterDeviceInterface(PhysicalDeviceObject,
2334 &GUID_DEVINTERFACE_NET,
2335 NULL,
2336 &Adapter->NdisMiniportBlock.SymbolicLinkName);
2337
2338 if (!NT_SUCCESS(Status))
2339 {
2340 NDIS_DbgPrint(MIN_TRACE, ("Could not create device interface.\n"));
2341 IoDeleteDevice(DeviceObject);
2342 RtlFreeUnicodeString(&ExportName);
2343 return Status;
2344 }
2345
2346 Adapter->NdisMiniportBlock.DriverHandle = Miniport;
2347 Adapter->NdisMiniportBlock.MiniportName = ExportName;
2348 Adapter->NdisMiniportBlock.DeviceObject = DeviceObject;
2349 Adapter->NdisMiniportBlock.PhysicalDeviceObject = PhysicalDeviceObject;
2350 Adapter->NdisMiniportBlock.NextDeviceObject =
2351 IoAttachDeviceToDeviceStack(Adapter->NdisMiniportBlock.DeviceObject,
2352 PhysicalDeviceObject);
2353
2354 Adapter->NdisMiniportBlock.OldPnPDeviceState = 0;
2355 Adapter->NdisMiniportBlock.PnPDeviceState = NdisPnPDeviceAdded;
2356
2357 KeInitializeTimer(&Adapter->NdisMiniportBlock.WakeUpDpcTimer.Timer);
2358 KeInitializeDpc(&Adapter->NdisMiniportBlock.WakeUpDpcTimer.Dpc, MiniportHangDpc, Adapter);
2359
2360 DeviceObject->Flags &= ~DO_DEVICE_INITIALIZING;
2361
2362 return STATUS_SUCCESS;
2363 }
2364
2365 \f
2366 /*
2367 * @implemented
2368 */
2369 NDIS_STATUS
2370 EXPORT
2371 NdisMRegisterMiniport(
2372 IN NDIS_HANDLE NdisWrapperHandle,
2373 IN PNDIS_MINIPORT_CHARACTERISTICS MiniportCharacteristics,
2374 IN UINT CharacteristicsLength)
2375 /*
2376 * FUNCTION: Registers a miniport's MiniportXxx entry points with the NDIS library
2377 * ARGUMENTS:
2378 * NdisWrapperHandle = Pointer to handle returned by NdisMInitializeWrapper
2379 * MiniportCharacteristics = Pointer to a buffer with miniport characteristics
2380 * CharacteristicsLength = Number of bytes in characteristics buffer
2381 * RETURNS:
2382 * Status of operation
2383 */
2384 {
2385 UINT MinSize;
2386 PNDIS_M_DRIVER_BLOCK Miniport = GET_MINIPORT_DRIVER(NdisWrapperHandle);
2387 PNDIS_M_DRIVER_BLOCK *MiniportPtr;
2388 NTSTATUS Status;
2389
2390 NDIS_DbgPrint(MAX_TRACE, ("Called.\n"));
2391
2392 switch (MiniportCharacteristics->MajorNdisVersion)
2393 {
2394 case 0x03:
2395 MinSize = sizeof(NDIS30_MINIPORT_CHARACTERISTICS);
2396 break;
2397
2398 case 0x04:
2399 MinSize = sizeof(NDIS40_MINIPORT_CHARACTERISTICS);
2400 break;
2401
2402 case 0x05:
2403 MinSize = sizeof(NDIS50_MINIPORT_CHARACTERISTICS);
2404 break;
2405
2406 default:
2407 NDIS_DbgPrint(MIN_TRACE, ("Bad miniport characteristics version.\n"));
2408 return NDIS_STATUS_BAD_VERSION;
2409 }
2410
2411 NDIS_DbgPrint(MIN_TRACE, ("Initializing an NDIS %u.%u miniport\n",
2412 MiniportCharacteristics->MajorNdisVersion,
2413 MiniportCharacteristics->MinorNdisVersion));
2414
2415 if (CharacteristicsLength < MinSize)
2416 {
2417 NDIS_DbgPrint(MIN_TRACE, ("Bad miniport characteristics length.\n"));
2418 return NDIS_STATUS_BAD_CHARACTERISTICS;
2419 }
2420
2421 /* Check if mandatory MiniportXxx functions are specified */
2422 if ((!MiniportCharacteristics->HaltHandler) ||
2423 (!MiniportCharacteristics->InitializeHandler)||
2424 (!MiniportCharacteristics->ResetHandler))
2425 {
2426 NDIS_DbgPrint(MIN_TRACE, ("Bad miniport characteristics.\n"));
2427 return NDIS_STATUS_BAD_CHARACTERISTICS;
2428 }
2429
2430 if (MiniportCharacteristics->MajorNdisVersion < 0x05)
2431 {
2432 if ((!MiniportCharacteristics->QueryInformationHandler) ||
2433 (!MiniportCharacteristics->SetInformationHandler))
2434 {
2435 NDIS_DbgPrint(MIN_TRACE, ("Bad miniport characteristics. (Set/Query)\n"));
2436 return NDIS_STATUS_BAD_CHARACTERISTICS;
2437 }
2438 }
2439 else
2440 {
2441 if (((!MiniportCharacteristics->QueryInformationHandler) ||
2442 (!MiniportCharacteristics->SetInformationHandler)) &&
2443 (!MiniportCharacteristics->CoRequestHandler))
2444 {
2445 NDIS_DbgPrint(MIN_TRACE, ("Bad miniport characteristics. (Set/Query)\n"));
2446 return NDIS_STATUS_BAD_CHARACTERISTICS;
2447 }
2448 }
2449
2450 if (MiniportCharacteristics->MajorNdisVersion == 0x03)
2451 {
2452 if (!MiniportCharacteristics->SendHandler)
2453 {
2454 NDIS_DbgPrint(MIN_TRACE, ("Bad miniport characteristics. (NDIS 3.0)\n"));
2455 return NDIS_STATUS_BAD_CHARACTERISTICS;
2456 }
2457 }
2458 else if (MiniportCharacteristics->MajorNdisVersion == 0x04)
2459 {
2460 /* NDIS 4.0 */
2461 if ((!MiniportCharacteristics->SendHandler) &&
2462 (!MiniportCharacteristics->SendPacketsHandler))
2463 {
2464 NDIS_DbgPrint(MIN_TRACE, ("Bad miniport characteristics. (NDIS 4.0)\n"));
2465 return NDIS_STATUS_BAD_CHARACTERISTICS;
2466 }
2467 }
2468 else if (MiniportCharacteristics->MajorNdisVersion == 0x05)
2469 {
2470 /* TODO: Add more checks here */
2471
2472 if ((!MiniportCharacteristics->SendHandler) &&
2473 (!MiniportCharacteristics->SendPacketsHandler) &&
2474 (!MiniportCharacteristics->CoSendPacketsHandler))
2475 {
2476 NDIS_DbgPrint(MIN_TRACE, ("Bad miniport characteristics. (NDIS 5.0)\n"));
2477 return NDIS_STATUS_BAD_CHARACTERISTICS;
2478 }
2479 }
2480
2481 /* TODO: verify NDIS5 and NDIS5.1 */
2482
2483 RtlCopyMemory(&Miniport->MiniportCharacteristics, MiniportCharacteristics, MinSize);
2484
2485 /*
2486 * NOTE: This is VERY unoptimal! Should we store the NDIS_M_DRIVER_BLOCK
2487 * structure in the driver extension or what?
2488 */
2489
2490 Status = IoAllocateDriverObjectExtension(Miniport->DriverObject, (PVOID)'NMID',
2491 sizeof(PNDIS_M_DRIVER_BLOCK), (PVOID*)&MiniportPtr);
2492 if (!NT_SUCCESS(Status))
2493 {
2494 NDIS_DbgPrint(MIN_TRACE, ("Can't allocate driver object extension.\n"));
2495 return NDIS_STATUS_RESOURCES;
2496 }
2497
2498 *MiniportPtr = Miniport;
2499
2500 Miniport->DriverObject->MajorFunction[IRP_MJ_PNP] = NdisIDispatchPnp;
2501 Miniport->DriverObject->MajorFunction[IRP_MJ_SHUTDOWN] = NdisIShutdown;
2502 Miniport->DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = NdisIDeviceIoControl;
2503 Miniport->DriverObject->DriverExtension->AddDevice = NdisIAddDevice;
2504
2505 return NDIS_STATUS_SUCCESS;
2506 }
2507
2508 \f
2509 /*
2510 * @implemented
2511 */
2512 #undef NdisMResetComplete
2513 VOID
2514 EXPORT
2515 NdisMResetComplete(
2516 IN NDIS_HANDLE MiniportAdapterHandle,
2517 IN NDIS_STATUS Status,
2518 IN BOOLEAN AddressingReset)
2519 {
2520 MiniResetComplete(MiniportAdapterHandle, Status, AddressingReset);
2521 }
2522
2523 \f
2524 /*
2525 * @implemented
2526 */
2527 #undef NdisMSendComplete
2528 VOID
2529 EXPORT
2530 NdisMSendComplete(
2531 IN NDIS_HANDLE MiniportAdapterHandle,
2532 IN PNDIS_PACKET Packet,
2533 IN NDIS_STATUS Status)
2534 /*
2535 * FUNCTION: Forwards a message to the initiating protocol saying
2536 * that a packet was handled
2537 * ARGUMENTS:
2538 * NdisAdapterHandle = Handle input to MiniportInitialize
2539 * Packet = Pointer to NDIS packet that was sent
2540 * Status = Status of send operation
2541 */
2542 {
2543 MiniSendComplete(MiniportAdapterHandle, Packet, Status);
2544 }
2545
2546 \f
2547 /*
2548 * @implemented
2549 */
2550 #undef NdisMSendResourcesAvailable
2551 VOID
2552 EXPORT
2553 NdisMSendResourcesAvailable(
2554 IN NDIS_HANDLE MiniportAdapterHandle)
2555 {
2556 MiniSendResourcesAvailable(MiniportAdapterHandle);
2557 }
2558
2559 \f
2560 /*
2561 * @implemented
2562 */
2563 #undef NdisMTransferDataComplete
2564 VOID
2565 EXPORT
2566 NdisMTransferDataComplete(
2567 IN NDIS_HANDLE MiniportAdapterHandle,
2568 IN PNDIS_PACKET Packet,
2569 IN NDIS_STATUS Status,
2570 IN UINT BytesTransferred)
2571 {
2572 MiniTransferDataComplete(MiniportAdapterHandle, Packet, Status, BytesTransferred);
2573 }
2574
2575 \f
2576 /*
2577 * @implemented
2578 */
2579 #undef NdisMSetAttributes
2580 VOID
2581 EXPORT
2582 NdisMSetAttributes(
2583 IN NDIS_HANDLE MiniportAdapterHandle,
2584 IN NDIS_HANDLE MiniportAdapterContext,
2585 IN BOOLEAN BusMaster,
2586 IN NDIS_INTERFACE_TYPE AdapterType)
2587 /*
2588 * FUNCTION: Informs the NDIS library of significant features of the caller's NIC
2589 * ARGUMENTS:
2590 * MiniportAdapterHandle = Handle input to MiniportInitialize
2591 * MiniportAdapterContext = Pointer to context information
2592 * BusMaster = Specifies TRUE if the caller's NIC is a busmaster DMA device
2593 * AdapterType = Specifies the I/O bus interface of the caller's NIC
2594 */
2595 {
2596 NDIS_DbgPrint(MAX_TRACE, ("Called.\n"));
2597 NdisMSetAttributesEx(MiniportAdapterHandle, MiniportAdapterContext, 0,
2598 BusMaster ? NDIS_ATTRIBUTE_BUS_MASTER : 0,
2599 AdapterType);
2600 }
2601
2602 \f
2603 /*
2604 * @implemented
2605 */
2606 VOID
2607 EXPORT
2608 NdisMSetAttributesEx(
2609 IN NDIS_HANDLE MiniportAdapterHandle,
2610 IN NDIS_HANDLE MiniportAdapterContext,
2611 IN UINT CheckForHangTimeInSeconds OPTIONAL,
2612 IN ULONG AttributeFlags,
2613 IN NDIS_INTERFACE_TYPE AdapterType)
2614 /*
2615 * FUNCTION: Informs the NDIS library of significant features of the caller's NIC
2616 * ARGUMENTS:
2617 * MiniportAdapterHandle = Handle input to MiniportInitialize
2618 * MiniportAdapterContext = Pointer to context information
2619 * CheckForHangTimeInSeconds = Specifies interval in seconds at which
2620 * MiniportCheckForHang should be called
2621 * AttributeFlags = Bitmask that indicates specific attributes
2622 * AdapterType = Specifies the I/O bus interface of the caller's NIC
2623 */
2624 {
2625 PLOGICAL_ADAPTER Adapter = GET_LOGICAL_ADAPTER(MiniportAdapterHandle);
2626
2627 NDIS_DbgPrint(MAX_TRACE, ("Called.\n"));
2628
2629 Adapter->NdisMiniportBlock.MiniportAdapterContext = MiniportAdapterContext;
2630 Adapter->NdisMiniportBlock.Flags = AttributeFlags;
2631 Adapter->NdisMiniportBlock.AdapterType = AdapterType;
2632 if (CheckForHangTimeInSeconds > 0)
2633 Adapter->NdisMiniportBlock.CheckForHangSeconds = CheckForHangTimeInSeconds;
2634 if (AttributeFlags & NDIS_ATTRIBUTE_INTERMEDIATE_DRIVER)
2635 NDIS_DbgPrint(MAX_TRACE, ("Intermediate drivers not supported yet.\n"));
2636
2637
2638 if (Adapter->NdisMiniportBlock.DriverHandle->MiniportCharacteristics.AdapterShutdownHandler)
2639 {
2640 NDIS_DbgPrint(MAX_TRACE, ("Miniport set AdapterShutdownHandler in MiniportCharacteristics\n"));
2641 NdisMRegisterAdapterShutdownHandler(Adapter,
2642 Adapter->NdisMiniportBlock.MiniportAdapterContext,
2643 Adapter->NdisMiniportBlock.DriverHandle->MiniportCharacteristics.AdapterShutdownHandler);
2644 }
2645 }
2646
2647 \f
2648 /*
2649 * @implemented
2650 */
2651 VOID
2652 EXPORT
2653 NdisMSleep(
2654 IN ULONG MicrosecondsToSleep)
2655 /*
2656 * FUNCTION: delay the thread's execution for MicrosecondsToSleep
2657 * ARGUMENTS:
2658 * MicrosecondsToSleep: duh...
2659 * NOTES:
2660 * - Because this is a blocking call, current IRQL must be < DISPATCH_LEVEL
2661 */
2662 {
2663 KTIMER Timer;
2664 LARGE_INTEGER DueTime;
2665
2666 PAGED_CODE();
2667
2668 DueTime.QuadPart = (-1) * 10 * MicrosecondsToSleep;
2669
2670 KeInitializeTimer(&Timer);
2671 KeSetTimer(&Timer, DueTime, 0);
2672 KeWaitForSingleObject(&Timer, Executive, KernelMode, FALSE, 0);
2673 }
2674
2675 \f
2676 /*
2677 * @implemented
2678 */
2679 BOOLEAN
2680 EXPORT
2681 NdisMSynchronizeWithInterrupt(
2682 IN PNDIS_MINIPORT_INTERRUPT Interrupt,
2683 IN PVOID SynchronizeFunction,
2684 IN PVOID SynchronizeContext)
2685 {
2686 return(KeSynchronizeExecution(Interrupt->InterruptObject,
2687 (PKSYNCHRONIZE_ROUTINE)SynchronizeFunction,
2688 SynchronizeContext));
2689 }
2690
2691 \f
2692 /*
2693 * @unimplemented
2694 */
2695 NDIS_STATUS
2696 EXPORT
2697 NdisMWriteLogData(
2698 IN NDIS_HANDLE LogHandle,
2699 IN PVOID LogBuffer,
2700 IN UINT LogBufferSize)
2701 {
2702 PUCHAR Buffer = LogBuffer;
2703 UINT i, j, idx;
2704
2705 UNIMPLEMENTED;
2706 for (i = 0; i < LogBufferSize; i += 16)
2707 {
2708 DbgPrint("%08x |", i);
2709 for (j = 0; j < 16; j++)
2710 {
2711 idx = i + j;
2712 if (idx < LogBufferSize)
2713 DbgPrint(" %02x", Buffer[idx]);
2714 else
2715 DbgPrint(" ");
2716 }
2717 DbgPrint(" | ");
2718 for (j = 0; j < 16; j++)
2719 {
2720 idx = i + j;
2721 if (idx == LogBufferSize)
2722 break;
2723 if (Buffer[idx] >= ' ') /* FIXME: not portable! replace by if (isprint(Buffer[idx])) ? */
2724 DbgPrint("%c", Buffer[idx]);
2725 else
2726 DbgPrint(".");
2727 }
2728 DbgPrint("\n");
2729 }
2730
2731 return NDIS_STATUS_FAILURE;
2732 }
2733
2734 \f
2735 /*
2736 * @implemented
2737 */
2738 VOID
2739 EXPORT
2740 NdisTerminateWrapper(
2741 IN NDIS_HANDLE NdisWrapperHandle,
2742 IN PVOID SystemSpecific)
2743 /*
2744 * FUNCTION: Releases resources allocated by a call to NdisInitializeWrapper
2745 * ARGUMENTS:
2746 * NdisWrapperHandle = Handle returned by NdisInitializeWrapper (NDIS_M_DRIVER_BLOCK)
2747 * SystemSpecific = Always NULL
2748 */
2749 {
2750 PNDIS_M_DRIVER_BLOCK Miniport = GET_MINIPORT_DRIVER(NdisWrapperHandle);
2751
2752 NDIS_DbgPrint(MAX_TRACE, ("Called.\n"));
2753
2754 ExFreePool(Miniport->RegistryPath->Buffer);
2755 ExFreePool(Miniport->RegistryPath);
2756 ExInterlockedRemoveEntryList(&Miniport->ListEntry, &MiniportListLock);
2757 ExFreePool(Miniport);
2758 }
2759
2760
2761 /*
2762 * @implemented
2763 */
2764 NDIS_STATUS
2765 EXPORT
2766 NdisMQueryAdapterInstanceName(
2767 OUT PNDIS_STRING AdapterInstanceName,
2768 IN NDIS_HANDLE MiniportAdapterHandle)
2769 /*
2770 * FUNCTION:
2771 * ARGUMENTS:
2772 * NOTES:
2773 * NDIS 5.0
2774 */
2775 {
2776 PLOGICAL_ADAPTER Adapter = (PLOGICAL_ADAPTER)MiniportAdapterHandle;
2777 UNICODE_STRING AdapterName;
2778
2779 NDIS_DbgPrint(MAX_TRACE, ("Called.\n"));
2780
2781 AdapterName.Length = 0;
2782 AdapterName.MaximumLength = Adapter->NdisMiniportBlock.MiniportName.MaximumLength;
2783 AdapterName.Buffer = ExAllocatePool(PagedPool, AdapterName.MaximumLength);
2784 if (!AdapterName.Buffer) {
2785 NDIS_DbgPrint(MIN_TRACE, ("Insufficient resources\n"));
2786 return NDIS_STATUS_RESOURCES;
2787 }
2788
2789 RtlCopyUnicodeString(&AdapterName, &Adapter->NdisMiniportBlock.MiniportName);
2790
2791 *AdapterInstanceName = AdapterName;
2792
2793 return NDIS_STATUS_SUCCESS;
2794 }
2795
2796 /*
2797 * @implemented
2798 */
2799 VOID
2800 EXPORT
2801 NdisDeregisterAdapterShutdownHandler(
2802 IN NDIS_HANDLE NdisAdapterHandle)
2803 /*
2804 * FUNCTION:
2805 * ARGUMENTS:
2806 * NOTES:
2807 * NDIS 4.0
2808 */
2809 {
2810 NdisMDeregisterAdapterShutdownHandler(NdisAdapterHandle);
2811 }
2812
2813
2814 /*
2815 * @implemented
2816 */
2817 VOID
2818 EXPORT
2819 NdisRegisterAdapterShutdownHandler(
2820 IN NDIS_HANDLE NdisAdapterHandle,
2821 IN PVOID ShutdownContext,
2822 IN ADAPTER_SHUTDOWN_HANDLER ShutdownHandler)
2823 /*
2824 * FUNCTION:
2825 * ARGUMENTS:
2826 * NOTES:
2827 * NDIS 4.0
2828 */
2829 {
2830 NdisMRegisterAdapterShutdownHandler(NdisAdapterHandle,
2831 ShutdownContext,
2832 ShutdownHandler);
2833 }
2834
2835 /*
2836 * @implemented
2837 */
2838 VOID
2839 EXPORT
2840 NdisMGetDeviceProperty(
2841 IN NDIS_HANDLE MiniportAdapterHandle,
2842 IN OUT PDEVICE_OBJECT *PhysicalDeviceObject OPTIONAL,
2843 IN OUT PDEVICE_OBJECT *FunctionalDeviceObject OPTIONAL,
2844 IN OUT PDEVICE_OBJECT *NextDeviceObject OPTIONAL,
2845 IN OUT PCM_RESOURCE_LIST *AllocatedResources OPTIONAL,
2846 IN OUT PCM_RESOURCE_LIST *AllocatedResourcesTranslated OPTIONAL)
2847 /*
2848 * FUNCTION:
2849 * ARGUMENTS:
2850 * NOTES:
2851 * NDIS 5.0
2852 */
2853 {
2854 PLOGICAL_ADAPTER Adapter = MiniportAdapterHandle;
2855
2856 NDIS_DbgPrint(MAX_TRACE, ("Called\n"));
2857
2858 if (PhysicalDeviceObject != NULL)
2859 *PhysicalDeviceObject = Adapter->NdisMiniportBlock.PhysicalDeviceObject;
2860
2861 if (FunctionalDeviceObject != NULL)
2862 *FunctionalDeviceObject = Adapter->NdisMiniportBlock.DeviceObject;
2863
2864 if (NextDeviceObject != NULL)
2865 *NextDeviceObject = Adapter->NdisMiniportBlock.NextDeviceObject;
2866
2867 if (AllocatedResources != NULL)
2868 *AllocatedResources = Adapter->NdisMiniportBlock.AllocatedResources;
2869
2870 if (AllocatedResourcesTranslated != NULL)
2871 *AllocatedResourcesTranslated = Adapter->NdisMiniportBlock.AllocatedResourcesTranslated;
2872 }
2873
2874 /*
2875 * @implemented
2876 */
2877 VOID
2878 EXPORT
2879 NdisMRegisterUnloadHandler(
2880 IN NDIS_HANDLE NdisWrapperHandle,
2881 IN PDRIVER_UNLOAD UnloadHandler)
2882 /*
2883 * FUNCTION:
2884 * ARGUMENTS:
2885 * NOTES:
2886 * NDIS 5.0
2887 */
2888 {
2889 PNDIS_M_DRIVER_BLOCK DriverBlock = NdisWrapperHandle;
2890
2891 NDIS_DbgPrint(MAX_TRACE, ("Miniport registered unload handler\n"));
2892
2893 DriverBlock->DriverObject->DriverUnload = UnloadHandler;
2894 }
2895
2896 /*
2897 * @implemented
2898 */
2899 NDIS_STATUS
2900 EXPORT
2901 NdisMRegisterDevice(
2902 IN NDIS_HANDLE NdisWrapperHandle,
2903 IN PNDIS_STRING DeviceName,
2904 IN PNDIS_STRING SymbolicName,
2905 IN PDRIVER_DISPATCH MajorFunctions[],
2906 OUT PDEVICE_OBJECT *pDeviceObject,
2907 OUT NDIS_HANDLE *NdisDeviceHandle)
2908 /*
2909 * FUNCTION:
2910 * ARGUMENTS:
2911 * NOTES:
2912 * NDIS 5.0
2913 */
2914 {
2915 PNDIS_M_DRIVER_BLOCK DriverBlock = NdisWrapperHandle;
2916 PNDIS_M_DEVICE_BLOCK DeviceBlock;
2917 PDEVICE_OBJECT DeviceObject;
2918 NDIS_STATUS Status;
2919 UINT i;
2920
2921 NDIS_DbgPrint(MAX_TRACE, ("Called\n"));
2922
2923 Status = IoCreateDevice(DriverBlock->DriverObject,
2924 0, /* This space is reserved for us. Should we use it? */
2925 DeviceName,
2926 FILE_DEVICE_NETWORK,
2927 0,
2928 FALSE,
2929 &DeviceObject);
2930
2931 if (!NT_SUCCESS(Status))
2932 {
2933 NDIS_DbgPrint(MIN_TRACE, ("IoCreateDevice failed (%x)\n", Status));
2934 return Status;
2935 }
2936
2937 Status = IoCreateSymbolicLink(SymbolicName, DeviceName);
2938
2939 if (!NT_SUCCESS(Status))
2940 {
2941 NDIS_DbgPrint(MIN_TRACE, ("IoCreateSymbolicLink failed (%x)\n", Status));
2942 IoDeleteDevice(DeviceObject);
2943 return Status;
2944 }
2945
2946 DeviceBlock = ExAllocatePool(NonPagedPool, sizeof(NDIS_M_DEVICE_BLOCK));
2947
2948 if (!DeviceBlock)
2949 {
2950 NDIS_DbgPrint(MIN_TRACE, ("Insufficient resources\n"));
2951 IoDeleteDevice(DeviceObject);
2952 IoDeleteSymbolicLink(SymbolicName);
2953 return NDIS_STATUS_RESOURCES;
2954 }
2955
2956 for (i = 0; i <= IRP_MJ_MAXIMUM_FUNCTION; i++)
2957 DriverBlock->DriverObject->MajorFunction[i] = MajorFunctions[i];
2958
2959 DriverBlock->DriverObject->MajorFunction[IRP_MJ_PNP] = NdisIDispatchPnp;
2960
2961 DeviceBlock->DeviceObject = DeviceObject;
2962 DeviceBlock->SymbolicName = SymbolicName;
2963
2964 *pDeviceObject = DeviceObject;
2965 *NdisDeviceHandle = DeviceBlock;
2966
2967 return NDIS_STATUS_SUCCESS;
2968 }
2969
2970 /*
2971 * @implemented
2972 */
2973 NDIS_STATUS
2974 EXPORT
2975 NdisMDeregisterDevice(
2976 IN NDIS_HANDLE NdisDeviceHandle)
2977 /*
2978 * FUNCTION:
2979 * ARGUMENTS:
2980 * NOTES:
2981 * NDIS 5.0
2982 */
2983 {
2984 PNDIS_M_DEVICE_BLOCK DeviceBlock = NdisDeviceHandle;
2985
2986 IoDeleteDevice(DeviceBlock->DeviceObject);
2987
2988 IoDeleteSymbolicLink(DeviceBlock->SymbolicName);
2989
2990 ExFreePool(DeviceBlock);
2991
2992 return NDIS_STATUS_SUCCESS;
2993 }
2994
2995 /*
2996 * @implemented
2997 */
2998 NDIS_STATUS
2999 EXPORT
3000 NdisQueryAdapterInstanceName(
3001 OUT PNDIS_STRING AdapterInstanceName,
3002 IN NDIS_HANDLE NdisBindingHandle)
3003 /*
3004 * FUNCTION:
3005 * ARGUMENTS:
3006 * NOTES:
3007 * NDIS 5.0
3008 */
3009 {
3010 PADAPTER_BINDING AdapterBinding = NdisBindingHandle;
3011 PLOGICAL_ADAPTER Adapter = AdapterBinding->Adapter;
3012
3013 return NdisMQueryAdapterInstanceName(AdapterInstanceName,
3014 Adapter);
3015 }
3016
3017 /*
3018 * @implemented
3019 */
3020 VOID
3021 EXPORT
3022 NdisCompletePnPEvent(
3023 IN NDIS_STATUS Status,
3024 IN NDIS_HANDLE NdisBindingHandle,
3025 IN PNET_PNP_EVENT NetPnPEvent)
3026 /*
3027 * FUNCTION:
3028 * ARGUMENTS:
3029 * NOTES:
3030 * NDIS 5.0
3031 */
3032 {
3033 PIRP Irp = (PIRP)NetPnPEvent->NdisReserved[0];
3034 PLIST_ENTRY CurrentEntry = (PLIST_ENTRY)NetPnPEvent->NdisReserved[1];
3035 PADAPTER_BINDING AdapterBinding = NdisBindingHandle;
3036 PLOGICAL_ADAPTER Adapter = AdapterBinding->Adapter;
3037 NDIS_STATUS NdisStatus;
3038
3039 if (Status != NDIS_STATUS_SUCCESS)
3040 {
3041 if (NetPnPEvent->Buffer) ExFreePool(NetPnPEvent->Buffer);
3042 ExFreePool(NetPnPEvent);
3043 Irp->IoStatus.Status = Status;
3044 IoCompleteRequest(Irp, IO_NO_INCREMENT);
3045 return;
3046 }
3047
3048 while (CurrentEntry != &Adapter->ProtocolListHead)
3049 {
3050 AdapterBinding = CONTAINING_RECORD(CurrentEntry, ADAPTER_BINDING, AdapterListEntry);
3051
3052 NdisStatus = (*AdapterBinding->ProtocolBinding->Chars.PnPEventHandler)(
3053 AdapterBinding->NdisOpenBlock.ProtocolBindingContext,
3054 NetPnPEvent);
3055
3056 if (NdisStatus == NDIS_STATUS_PENDING)
3057 {
3058 NetPnPEvent->NdisReserved[1] = (ULONG_PTR)CurrentEntry->Flink;
3059 return;
3060 }
3061 else if (NdisStatus != NDIS_STATUS_SUCCESS)
3062 {
3063 if (NetPnPEvent->Buffer) ExFreePool(NetPnPEvent->Buffer);
3064 ExFreePool(NetPnPEvent);
3065 Irp->IoStatus.Status = NdisStatus;
3066 IoCompleteRequest(Irp, IO_NO_INCREMENT);
3067 return;
3068 }
3069
3070 CurrentEntry = CurrentEntry->Flink;
3071 }
3072
3073 if (NetPnPEvent->Buffer) ExFreePool(NetPnPEvent->Buffer);
3074 ExFreePool(NetPnPEvent);
3075
3076 Irp->IoStatus.Status = NDIS_STATUS_SUCCESS;
3077 IoCompleteRequest(Irp, IO_NO_INCREMENT);
3078 }
3079
3080 /*
3081 * @implemented
3082 */
3083 VOID
3084 EXPORT
3085 NdisCancelSendPackets(
3086 IN NDIS_HANDLE NdisBindingHandle,
3087 IN PVOID CancelId)
3088 {
3089 PADAPTER_BINDING AdapterBinding = NdisBindingHandle;
3090 PLOGICAL_ADAPTER Adapter = AdapterBinding->Adapter;
3091
3092 NDIS_DbgPrint(MAX_TRACE, ("Called for ID %x.\n", CancelId));
3093
3094 if (Adapter->NdisMiniportBlock.DriverHandle->MiniportCharacteristics.CancelSendPacketsHandler)
3095 {
3096 (*Adapter->NdisMiniportBlock.DriverHandle->MiniportCharacteristics.CancelSendPacketsHandler)(
3097 Adapter->NdisMiniportBlock.MiniportAdapterContext,
3098 CancelId);
3099 }
3100 }
3101
3102
3103 /*
3104 * @implemented
3105 */
3106 NDIS_HANDLE
3107 EXPORT
3108 NdisIMGetBindingContext(
3109 IN NDIS_HANDLE NdisBindingHandle)
3110 /*
3111 * FUNCTION:
3112 * ARGUMENTS:
3113 * NOTES:
3114 * NDIS 5.0
3115 */
3116 {
3117 PADAPTER_BINDING AdapterBinding = NdisBindingHandle;
3118 PLOGICAL_ADAPTER Adapter = AdapterBinding->Adapter;
3119
3120 NDIS_DbgPrint(MAX_TRACE, ("Called.\n"));
3121
3122 return Adapter->NdisMiniportBlock.DeviceContext;
3123 }
3124
3125
3126 /*
3127 * @implemented
3128 */
3129 NDIS_HANDLE
3130 EXPORT
3131 NdisIMGetDeviceContext(
3132 IN NDIS_HANDLE MiniportAdapterHandle)
3133 /*
3134 * FUNCTION:
3135 * ARGUMENTS:
3136 * NOTES:
3137 * NDIS 5.0
3138 */
3139 {
3140 PLOGICAL_ADAPTER Adapter = MiniportAdapterHandle;
3141
3142 NDIS_DbgPrint(MAX_TRACE, ("Called.\n"));
3143
3144 return Adapter->NdisMiniportBlock.DeviceContext;
3145 }
3146
3147 /* EOF */
3148