[USBSTOR]
[reactos.git] / drivers / usb / usbehci / hardware.cpp
1 /*
2 * PROJECT: ReactOS Universal Serial Bus Bulk Enhanced Host Controller Interface
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: drivers/usb/usbehci/hcd_controller.cpp
5 * PURPOSE: USB EHCI device driver.
6 * PROGRAMMERS:
7 * Michael Martin (michael.martin@reactos.org)
8 * Johannes Anderwald (johannes.anderwald@reactos.org)
9 */
10
11 #define INITGUID
12 #include "usbehci.h"
13 #include "hardware.h"
14
15 typedef VOID __stdcall HD_INIT_CALLBACK(IN PVOID CallBackContext);
16
17 BOOLEAN
18 NTAPI
19 InterruptServiceRoutine(
20 IN PKINTERRUPT Interrupt,
21 IN PVOID ServiceContext);
22
23 VOID
24 NTAPI
25 EhciDefferedRoutine(
26 IN PKDPC Dpc,
27 IN PVOID DeferredContext,
28 IN PVOID SystemArgument1,
29 IN PVOID SystemArgument2);
30
31 VOID
32 NTAPI
33 StatusChangeWorkItemRoutine(PVOID Context);
34
35 class CUSBHardwareDevice : public IUSBHardwareDevice
36 {
37 public:
38 STDMETHODIMP QueryInterface( REFIID InterfaceId, PVOID* Interface);
39
40 STDMETHODIMP_(ULONG) AddRef()
41 {
42 InterlockedIncrement(&m_Ref);
43 return m_Ref;
44 }
45 STDMETHODIMP_(ULONG) Release()
46 {
47 InterlockedDecrement(&m_Ref);
48
49 if (!m_Ref)
50 {
51 delete this;
52 return 0;
53 }
54 return m_Ref;
55 }
56 // com
57 NTSTATUS Initialize(PDRIVER_OBJECT DriverObject, PDEVICE_OBJECT FunctionalDeviceObject, PDEVICE_OBJECT PhysicalDeviceObject, PDEVICE_OBJECT LowerDeviceObject);
58 NTSTATUS PnpStart(PCM_RESOURCE_LIST RawResources, PCM_RESOURCE_LIST TranslatedResources);
59 NTSTATUS PnpStop(void);
60 NTSTATUS HandlePower(PIRP Irp);
61 NTSTATUS GetDeviceDetails(PUSHORT VendorId, PUSHORT DeviceId, PULONG NumberOfPorts, PULONG Speed);
62 NTSTATUS GetDMA(OUT struct IDMAMemoryManager **m_DmaManager);
63 NTSTATUS GetUSBQueue(OUT struct IUSBQueue **OutUsbQueue);
64
65 NTSTATUS StartController();
66 NTSTATUS StopController();
67 NTSTATUS ResetController();
68 NTSTATUS ResetPort(ULONG PortIndex);
69
70 NTSTATUS GetPortStatus(ULONG PortId, OUT USHORT *PortStatus, OUT USHORT *PortChange);
71 NTSTATUS ClearPortStatus(ULONG PortId, ULONG Status);
72 NTSTATUS SetPortFeature(ULONG PortId, ULONG Feature);
73
74 VOID SetAsyncListRegister(ULONG PhysicalAddress);
75 VOID SetPeriodicListRegister(ULONG PhysicalAddress);
76 struct _QUEUE_HEAD * GetAsyncListQueueHead();
77 ULONG GetPeriodicListRegister();
78
79 VOID SetStatusChangeEndpointCallBack(PVOID CallBack, PVOID Context);
80
81 KIRQL AcquireDeviceLock(void);
82 VOID ReleaseDeviceLock(KIRQL OldLevel);
83 // set command
84 VOID SetCommandRegister(PEHCI_USBCMD_CONTENT UsbCmd);
85
86 // get command
87 VOID GetCommandRegister(PEHCI_USBCMD_CONTENT UsbCmd);
88
89
90 // local
91 BOOLEAN InterruptService();
92
93 // friend function
94 friend BOOLEAN NTAPI InterruptServiceRoutine(IN PKINTERRUPT Interrupt, IN PVOID ServiceContext);
95 friend VOID NTAPI EhciDefferedRoutine(IN PKDPC Dpc, IN PVOID DeferredContext, IN PVOID SystemArgument1, IN PVOID SystemArgument2);
96 friend VOID NTAPI StatusChangeWorkItemRoutine(PVOID Context);
97 // constructor / destructor
98 CUSBHardwareDevice(IUnknown *OuterUnknown){}
99 virtual ~CUSBHardwareDevice(){}
100
101 protected:
102 LONG m_Ref; // reference count
103 PDRIVER_OBJECT m_DriverObject; // driver object
104 PDEVICE_OBJECT m_PhysicalDeviceObject; // pdo
105 PDEVICE_OBJECT m_FunctionalDeviceObject; // fdo (hcd controller)
106 PDEVICE_OBJECT m_NextDeviceObject; // lower device object
107 KSPIN_LOCK m_Lock; // hardware lock
108 PKINTERRUPT m_Interrupt; // interrupt object
109 KDPC m_IntDpcObject; // dpc object for deferred isr processing
110 PVOID VirtualBase; // virtual base for memory manager
111 PHYSICAL_ADDRESS PhysicalAddress; // physical base for memory manager
112 PULONG m_Base; // EHCI operational port base registers
113 PDMA_ADAPTER m_Adapter; // dma adapter object
114 ULONG m_MapRegisters; // map registers count
115 EHCI_CAPS m_Capabilities; // EHCI caps
116 USHORT m_VendorID; // vendor id
117 USHORT m_DeviceID; // device id
118 PQUEUE_HEAD AsyncQueueHead; // async queue head terminator
119 PUSBQUEUE m_UsbQueue; // usb request queue
120 PDMAMEMORYMANAGER m_MemoryManager; // memory manager
121 HD_INIT_CALLBACK* m_SCECallBack; // status change callback routine
122 PVOID m_SCEContext; // status change callback routine context
123 BOOLEAN m_DoorBellRingInProgress; // door bell ring in progress
124 WORK_QUEUE_ITEM m_StatusChangeWorkItem; // work item for status change callback
125 ULONG m_SyncFramePhysAddr; // periodic frame list physical address
126 BOOLEAN m_ResetInProgress[16]; // set when a reset is in progress
127 BUS_INTERFACE_STANDARD m_BusInterface; // pci bus interface
128
129 // read register
130 ULONG EHCI_READ_REGISTER_ULONG(ULONG Offset);
131
132 // write register
133 VOID EHCI_WRITE_REGISTER_ULONG(ULONG Offset, ULONG Value);
134 };
135
136 //=================================================================================================
137 // COM
138 //
139 NTSTATUS
140 STDMETHODCALLTYPE
141 CUSBHardwareDevice::QueryInterface(
142 IN REFIID refiid,
143 OUT PVOID* Output)
144 {
145 if (IsEqualGUIDAligned(refiid, IID_IUnknown))
146 {
147 *Output = PVOID(PUNKNOWN(this));
148 PUNKNOWN(*Output)->AddRef();
149 return STATUS_SUCCESS;
150 }
151
152 return STATUS_UNSUCCESSFUL;
153 }
154
155 NTSTATUS
156 CUSBHardwareDevice::Initialize(
157 PDRIVER_OBJECT DriverObject,
158 PDEVICE_OBJECT FunctionalDeviceObject,
159 PDEVICE_OBJECT PhysicalDeviceObject,
160 PDEVICE_OBJECT LowerDeviceObject)
161 {
162 PCI_COMMON_CONFIG PciConfig;
163 NTSTATUS Status;
164 ULONG BytesRead;
165
166 DPRINT1("CUSBHardwareDevice::Initialize\n");
167
168 //
169 // Create DMAMemoryManager for use with QueueHeads and Transfer Descriptors.
170 //
171 Status = CreateDMAMemoryManager(&m_MemoryManager);
172 if (!NT_SUCCESS(Status))
173 {
174 DPRINT1("Failed to create DMAMemoryManager Object\n");
175 return Status;
176 }
177
178 //
179 // Create the UsbQueue class that will handle the Asynchronous and Periodic Schedules
180 //
181 Status = CreateUSBQueue(&m_UsbQueue);
182 if (!NT_SUCCESS(Status))
183 {
184 DPRINT1("Failed to create UsbQueue!\n");
185 return Status;
186 }
187
188 //
189 // store device objects
190 //
191 m_DriverObject = DriverObject;
192 m_FunctionalDeviceObject = FunctionalDeviceObject;
193 m_PhysicalDeviceObject = PhysicalDeviceObject;
194 m_NextDeviceObject = LowerDeviceObject;
195
196 //
197 // initialize device lock
198 //
199 KeInitializeSpinLock(&m_Lock);
200
201 //
202 // intialize status change work item
203 //
204 ExInitializeWorkItem(&m_StatusChangeWorkItem, StatusChangeWorkItemRoutine, PVOID(this));
205
206 m_VendorID = 0;
207 m_DeviceID = 0;
208
209 Status = GetBusInterface(PhysicalDeviceObject, &m_BusInterface);
210 if (!NT_SUCCESS(Status))
211 {
212 DPRINT1("Failed to get BusInteface!\n");
213 return Status;
214 }
215
216 BytesRead = (*m_BusInterface.GetBusData)(m_BusInterface.Context,
217 PCI_WHICHSPACE_CONFIG,
218 &PciConfig,
219 0,
220 PCI_COMMON_HDR_LENGTH);
221
222 if (BytesRead != PCI_COMMON_HDR_LENGTH)
223 {
224 DPRINT1("Failed to get pci config information!\n");
225 return STATUS_SUCCESS;
226 }
227
228 m_VendorID = PciConfig.VendorID;
229 m_DeviceID = PciConfig.DeviceID;
230
231
232 if (PciConfig.Command & PCI_ENABLE_BUS_MASTER)
233 {
234 //
235 // master is enabled
236 //
237 return STATUS_SUCCESS;
238 }
239
240 DPRINT1("PCI Configuration shows this as a non Bus Mastering device! Enabling...\n");
241
242 PciConfig.Command |= PCI_ENABLE_BUS_MASTER;
243 m_BusInterface.SetBusData(m_BusInterface.Context, PCI_WHICHSPACE_CONFIG, &PciConfig, 0, PCI_COMMON_HDR_LENGTH);
244
245 BytesRead = (*m_BusInterface.GetBusData)(m_BusInterface.Context,
246 PCI_WHICHSPACE_CONFIG,
247 &PciConfig,
248 0,
249 PCI_COMMON_HDR_LENGTH);
250
251 if (BytesRead != PCI_COMMON_HDR_LENGTH)
252 {
253 DPRINT1("Failed to get pci config information!\n");
254 ASSERT(FALSE);
255 return STATUS_SUCCESS;
256 }
257
258 if (!(PciConfig.Command & PCI_ENABLE_BUS_MASTER))
259 {
260 PciConfig.Command |= PCI_ENABLE_BUS_MASTER;
261 DPRINT1("Failed to enable master\n");
262 return STATUS_UNSUCCESSFUL;
263 }
264 return STATUS_SUCCESS;
265 }
266
267 VOID
268 CUSBHardwareDevice::SetCommandRegister(PEHCI_USBCMD_CONTENT UsbCmd)
269 {
270 PULONG Register;
271 Register = (PULONG)UsbCmd;
272 WRITE_REGISTER_ULONG((PULONG)((ULONG)m_Base + EHCI_USBCMD), *Register);
273 }
274
275 VOID
276 CUSBHardwareDevice::GetCommandRegister(PEHCI_USBCMD_CONTENT UsbCmd)
277 {
278 PULONG Register;
279 Register = (PULONG)UsbCmd;
280 *Register = READ_REGISTER_ULONG((PULONG)((ULONG)m_Base + EHCI_USBCMD));
281 }
282
283 ULONG
284 CUSBHardwareDevice::EHCI_READ_REGISTER_ULONG(ULONG Offset)
285 {
286 return READ_REGISTER_ULONG((PULONG)((ULONG)m_Base + Offset));
287 }
288
289 VOID
290 CUSBHardwareDevice::EHCI_WRITE_REGISTER_ULONG(ULONG Offset, ULONG Value)
291 {
292 WRITE_REGISTER_ULONG((PULONG)((ULONG)m_Base + Offset), Value);
293 }
294
295 NTSTATUS
296 CUSBHardwareDevice::PnpStart(
297 PCM_RESOURCE_LIST RawResources,
298 PCM_RESOURCE_LIST TranslatedResources)
299 {
300 ULONG Index, Count;
301 PCM_PARTIAL_RESOURCE_DESCRIPTOR ResourceDescriptor;
302 DEVICE_DESCRIPTION DeviceDescription;
303 PHYSICAL_ADDRESS AsyncPhysicalAddress;
304 PVOID ResourceBase;
305 NTSTATUS Status;
306 UCHAR Value;
307 UCHAR PortCount;
308
309 DPRINT1("CUSBHardwareDevice::PnpStart\n");
310 for(Index = 0; Index < TranslatedResources->List[0].PartialResourceList.Count; Index++)
311 {
312 //
313 // get resource descriptor
314 //
315 ResourceDescriptor = &TranslatedResources->List[0].PartialResourceList.PartialDescriptors[Index];
316
317 switch(ResourceDescriptor->Type)
318 {
319 case CmResourceTypeInterrupt:
320 {
321 KeInitializeDpc(&m_IntDpcObject,
322 EhciDefferedRoutine,
323 this);
324
325 Status = IoConnectInterrupt(&m_Interrupt,
326 InterruptServiceRoutine,
327 (PVOID)this,
328 NULL,
329 ResourceDescriptor->u.Interrupt.Vector,
330 (KIRQL)ResourceDescriptor->u.Interrupt.Level,
331 (KIRQL)ResourceDescriptor->u.Interrupt.Level,
332 (KINTERRUPT_MODE)(ResourceDescriptor->Flags & CM_RESOURCE_INTERRUPT_LATCHED),
333 (ResourceDescriptor->ShareDisposition != CmResourceShareDeviceExclusive),
334 ResourceDescriptor->u.Interrupt.Affinity,
335 FALSE);
336
337 if (!NT_SUCCESS(Status))
338 {
339 //
340 // failed to register interrupt
341 //
342 DPRINT1("IoConnect Interrupt failed with %x\n", Status);
343 return Status;
344 }
345 break;
346 }
347 case CmResourceTypeMemory:
348 {
349 //
350 // get resource base
351 //
352 ResourceBase = MmMapIoSpace(ResourceDescriptor->u.Memory.Start, ResourceDescriptor->u.Memory.Length, MmNonCached);
353 if (!ResourceBase)
354 {
355 //
356 // failed to map registers
357 //
358 DPRINT1("MmMapIoSpace failed\n");
359 return STATUS_INSUFFICIENT_RESOURCES;
360 }
361
362 //
363 // Get controllers capabilities
364 //
365 m_Capabilities.Length = READ_REGISTER_UCHAR((PUCHAR)ResourceBase + EHCI_CAPLENGTH);
366 m_Capabilities.HCIVersion = READ_REGISTER_USHORT((PUSHORT)((ULONG)ResourceBase + EHCI_HCIVERSION));
367 m_Capabilities.HCSParamsLong = READ_REGISTER_ULONG((PULONG)((ULONG)ResourceBase + EHCI_HCSPARAMS));
368 m_Capabilities.HCCParamsLong = READ_REGISTER_ULONG((PULONG)((ULONG)ResourceBase + EHCI_HCCPARAMS));
369
370 DPRINT1("Controller has %d Length\n", m_Capabilities.Length);
371 DPRINT1("Controller has %d Ports\n", m_Capabilities.HCSParams.PortCount);
372 DPRINT1("Controller EHCI Version %x\n", m_Capabilities.HCIVersion);
373 DPRINT1("Controler EHCI Caps HCSParamsLong %x\n", m_Capabilities.HCSParamsLong);
374 DPRINT1("Controler EHCI Caps HCCParamsLong %x\n", m_Capabilities.HCCParamsLong);
375 DPRINT1("Controler EHCI Caps PowerControl %x\n", m_Capabilities.HCSParams.PortPowerControl);
376
377 if (m_Capabilities.HCSParams.PortRouteRules)
378 {
379 Count = 0;
380 PortCount = max(m_Capabilities.HCSParams.PortCount/2, (m_Capabilities.HCSParams.PortCount+1)/2);
381 do
382 {
383 //
384 // each entry is a 4 bit field EHCI 2.2.5
385 //
386 Value = READ_REGISTER_UCHAR((PUCHAR)(ULONG)ResourceBase + EHCI_HCSP_PORTROUTE + Count);
387 m_Capabilities.PortRoute[Count*2] = (Value & 0xF0);
388
389 if ((Count*2) + 1 < m_Capabilities.HCSParams.PortCount)
390 m_Capabilities.PortRoute[(Count*2)+1] = (Value & 0x0F);
391
392 Count++;
393 }while(Count < PortCount);
394 }
395
396 //
397 // Set m_Base to the address of Operational Register Space
398 //
399 m_Base = (PULONG)((ULONG)ResourceBase + m_Capabilities.Length);
400 break;
401 }
402 }
403 }
404
405
406 //
407 // zero device description
408 //
409 RtlZeroMemory(&DeviceDescription, sizeof(DEVICE_DESCRIPTION));
410
411 //
412 // initialize device description
413 //
414 DeviceDescription.Version = DEVICE_DESCRIPTION_VERSION;
415 DeviceDescription.Master = TRUE;
416 DeviceDescription.ScatterGather = TRUE;
417 DeviceDescription.Dma32BitAddresses = TRUE;
418 DeviceDescription.DmaWidth = Width32Bits;
419 DeviceDescription.InterfaceType = PCIBus;
420 DeviceDescription.MaximumLength = MAXULONG;
421
422 //
423 // get dma adapter
424 //
425 m_Adapter = IoGetDmaAdapter(m_PhysicalDeviceObject, &DeviceDescription, &m_MapRegisters);
426 if (!m_Adapter)
427 {
428 //
429 // failed to get dma adapter
430 //
431 DPRINT1("Failed to acquire dma adapter\n");
432 return STATUS_INSUFFICIENT_RESOURCES;
433 }
434
435 //
436 // Create Common Buffer
437 //
438 VirtualBase = m_Adapter->DmaOperations->AllocateCommonBuffer(m_Adapter,
439 PAGE_SIZE * 4,
440 &PhysicalAddress,
441 FALSE);
442 if (!VirtualBase)
443 {
444 DPRINT1("Failed to allocate a common buffer\n");
445 return STATUS_INSUFFICIENT_RESOURCES;
446 }
447
448 //
449 // Stop the controller before modifying schedules
450 //
451 Status = StopController();
452 if (!NT_SUCCESS(Status))
453 return Status;
454
455 //
456 // Initialize the DMAMemoryManager
457 //
458 Status = m_MemoryManager->Initialize(this, &m_Lock, PAGE_SIZE * 4, VirtualBase, PhysicalAddress, 32);
459 if (!NT_SUCCESS(Status))
460 {
461 DPRINT1("Failed to initialize the DMAMemoryManager\n");
462 return Status;
463 }
464
465 //
466 // Create a queuehead for the Async Register
467 //
468 m_MemoryManager->Allocate(sizeof(QUEUE_HEAD), (PVOID*)&AsyncQueueHead, &AsyncPhysicalAddress);
469
470 AsyncQueueHead->PhysicalAddr = AsyncPhysicalAddress.LowPart;
471 AsyncQueueHead->HorizontalLinkPointer = AsyncQueueHead->PhysicalAddr | QH_TYPE_QH;
472 AsyncQueueHead->EndPointCharacteristics.HeadOfReclamation = TRUE;
473 AsyncQueueHead->EndPointCharacteristics.EndPointSpeed = QH_ENDPOINT_HIGHSPEED;
474 AsyncQueueHead->Token.Bits.Halted = TRUE;
475
476 AsyncQueueHead->EndPointCapabilities.NumberOfTransactionPerFrame = 0x01;
477 AsyncQueueHead->NextPointer = TERMINATE_POINTER;
478 AsyncQueueHead->CurrentLinkPointer = TERMINATE_POINTER;
479
480 InitializeListHead(&AsyncQueueHead->LinkedQueueHeads);
481
482 //
483 // Initialize the UsbQueue now that we have an AdapterObject.
484 //
485 Status = m_UsbQueue->Initialize(PUSBHARDWAREDEVICE(this), m_Adapter, m_MemoryManager, &m_Lock);
486 if (!NT_SUCCESS(Status))
487 {
488 DPRINT1("Failed to Initialize the UsbQueue\n");
489 return Status;
490 }
491
492 //
493 // Start the controller
494 //
495 DPRINT1("Starting Controller\n");
496 Status = StartController();
497
498 //
499 // done
500 //
501 return Status;
502 }
503
504 NTSTATUS
505 CUSBHardwareDevice::PnpStop(void)
506 {
507 UNIMPLEMENTED
508 return STATUS_NOT_IMPLEMENTED;
509 }
510
511 NTSTATUS
512 CUSBHardwareDevice::HandlePower(
513 PIRP Irp)
514 {
515 UNIMPLEMENTED
516 return STATUS_NOT_IMPLEMENTED;
517 }
518
519 NTSTATUS
520 CUSBHardwareDevice::GetDeviceDetails(
521 OUT OPTIONAL PUSHORT VendorId,
522 OUT OPTIONAL PUSHORT DeviceId,
523 OUT OPTIONAL PULONG NumberOfPorts,
524 OUT OPTIONAL PULONG Speed)
525 {
526 if (VendorId)
527 *VendorId = m_VendorID;
528 if (DeviceId)
529 *DeviceId = m_DeviceID;
530 if (NumberOfPorts)
531 *NumberOfPorts = m_Capabilities.HCSParams.PortCount;
532 //FIXME: What to returned here?
533 if (Speed)
534 *Speed = 0x200;
535 return STATUS_SUCCESS;
536 }
537
538 NTSTATUS CUSBHardwareDevice::GetDMA(
539 OUT struct IDMAMemoryManager **OutDMAMemoryManager)
540 {
541 if (!m_MemoryManager)
542 return STATUS_UNSUCCESSFUL;
543 *OutDMAMemoryManager = m_MemoryManager;
544 return STATUS_SUCCESS;
545 }
546
547 NTSTATUS
548 CUSBHardwareDevice::GetUSBQueue(
549 OUT struct IUSBQueue **OutUsbQueue)
550 {
551 if (!m_UsbQueue)
552 return STATUS_UNSUCCESSFUL;
553 *OutUsbQueue = m_UsbQueue;
554 return STATUS_SUCCESS;
555 }
556
557
558 NTSTATUS
559 CUSBHardwareDevice::StartController(void)
560 {
561 EHCI_USBCMD_CONTENT UsbCmd;
562 ULONG UsbSts, FailSafe, ExtendedCapsSupport, Caps, Index;
563 UCHAR Value;
564 LARGE_INTEGER Timeout;
565
566 //
567 // are extended caps supported
568 //
569 ExtendedCapsSupport = (m_Capabilities.HCCParamsLong >> EHCI_ECP_SHIFT) & EHCI_ECP_MASK;
570 if (ExtendedCapsSupport)
571 {
572 DPRINT1("[EHCI] Extended Caps Support detected!\n");
573
574 //
575 // sanity check
576 //
577 ASSERT(ExtendedCapsSupport >= PCI_COMMON_HDR_LENGTH);
578 m_BusInterface.GetBusData(m_BusInterface.Context, PCI_WHICHSPACE_CONFIG, &Caps, ExtendedCapsSupport, sizeof(ULONG));
579
580 //
581 // OS Handoff Synchronization support capability. EHCI 5.1
582 //
583 if ((Caps & EHCI_LEGSUP_CAPID_MASK) == EHCI_LEGSUP_CAPID)
584 {
585 //
586 // is it bios owned
587 //
588 if ((Caps & EHCI_LEGSUP_BIOSOWNED))
589 {
590 DPRINT1("[EHCI] Controller is BIOS owned, acquring control\n");
591
592 //
593 // acquire ownership
594 //
595 Value = 1;
596 m_BusInterface.SetBusData(m_BusInterface.Context, PCI_WHICHSPACE_CONFIG, &Value, ExtendedCapsSupport+3, sizeof(UCHAR));
597
598 for(Index = 0; Index < 20; Index++)
599 {
600 //
601 // get status
602 //
603 m_BusInterface.GetBusData(m_BusInterface.Context, PCI_WHICHSPACE_CONFIG, &Caps, ExtendedCapsSupport, sizeof(ULONG));
604 if ((Caps & EHCI_LEGSUP_BIOSOWNED))
605 {
606 //
607 // lets wait a bit
608 //
609 Timeout.QuadPart = 50;
610 DPRINT1("Waiting %d milliseconds for port reset\n", Timeout.LowPart);
611
612 //
613 // convert to 100 ns units (absolute)
614 //
615 Timeout.QuadPart *= -10000;
616
617 //
618 // perform the wait
619 //
620 KeDelayExecutionThread(KernelMode, FALSE, &Timeout);
621 }
622 }
623 if ((Caps & EHCI_LEGSUP_BIOSOWNED))
624 {
625 //
626 // failed to aquire ownership
627 //
628 DPRINT1("[EHCI] failed to acquire ownership\n");
629 }
630 else if ((Caps & EHCI_LEGSUP_OSOWNED))
631 {
632 //
633 // HC OS Owned Semaphore EHCI 2.1.7
634 //
635 DPRINT1("[EHCI] acquired ownership\n");
636 }
637 #if 0
638 //
639 // explictly clear the bios owned flag 2.1.7
640 //
641 Value = 0;
642 m_BusInterface.SetBusData(m_BusInterface.Context, PCI_WHICHSPACE_CONFIG, &Value, ExtendedCapsSupport+2, sizeof(UCHAR));
643
644 //
645 // clear SMI interrupt EHCI 2.1.8
646 //
647 Caps = 4;
648 m_BusInterface.SetBusData(m_BusInterface.Context, PCI_WHICHSPACE_CONFIG, &Caps, ExtendedCapsSupport+4, sizeof(ULONG));
649 #endif
650 }
651 }
652 }
653
654 //
655 // get command register
656 //
657 GetCommandRegister(&UsbCmd);
658
659 //
660 // disable running schedules
661 //
662 UsbCmd.PeriodicEnable = FALSE;
663 UsbCmd.AsyncEnable = FALSE;
664 SetCommandRegister(&UsbCmd);
665
666 //
667 // Wait for execution to start
668 //
669 for (FailSafe = 100; FailSafe > 1; FailSafe--)
670 {
671 KeStallExecutionProcessor(100);
672 UsbSts = EHCI_READ_REGISTER_ULONG(EHCI_USBSTS);
673
674 if (!(UsbSts & EHCI_STS_PSS) && (UsbSts & EHCI_STS_ASS))
675 {
676 break;
677 }
678 }
679
680 if ((UsbSts & (EHCI_STS_PSS | EHCI_STS_ASS)))
681 {
682 DPRINT1("Failed to stop running schedules %x\n", UsbSts);
683 ASSERT(FALSE);
684 }
685
686
687 //
688 // Stop the controller if its running
689 //
690 UsbSts = EHCI_READ_REGISTER_ULONG(EHCI_USBSTS);
691 if (!(UsbSts & EHCI_STS_HALT))
692 {
693 DPRINT1("Stopping Controller %x\n", UsbSts);
694 StopController();
695 }
696
697 //
698 // Reset the controller
699 //
700 ResetController();
701
702 //
703 // check caps
704 //
705 if (m_Capabilities.HCCParams.CurAddrBits)
706 {
707 //
708 // disable 64-bit addressing
709 //
710 EHCI_WRITE_REGISTER_ULONG(EHCI_CTRLDSSEGMENT, 0x0);
711 }
712
713 //
714 // Enable Interrupts and start execution
715 //
716 ULONG Mask = EHCI_USBINTR_INTE | EHCI_USBINTR_ERR | EHCI_USBINTR_ASYNC | EHCI_USBINTR_HSERR | EHCI_USBINTR_PC;
717 EHCI_WRITE_REGISTER_ULONG(EHCI_USBINTR, Mask);
718
719 KeStallExecutionProcessor(10);
720
721 ULONG Status = EHCI_READ_REGISTER_ULONG(EHCI_USBINTR);
722
723 DPRINT1("Interrupt Mask %x\n", Status);
724 ASSERT((Status & Mask) == Mask);
725
726 //
727 // Assign the SyncList Register
728 //
729 EHCI_WRITE_REGISTER_ULONG(EHCI_PERIODICLISTBASE, m_SyncFramePhysAddr);
730
731 //
732 // Set Schedules to Enable and Interrupt Threshold to 1ms.
733 //
734 RtlZeroMemory(&UsbCmd, sizeof(EHCI_USBCMD_CONTENT));
735
736 UsbCmd.PeriodicEnable = TRUE;
737 UsbCmd.IntThreshold = 0x8; //1ms
738 UsbCmd.Run = TRUE;
739 UsbCmd.FrameListSize = 0x0; //1024
740 SetCommandRegister(&UsbCmd);
741
742 //
743 // Wait for execution to start
744 //
745 for (FailSafe = 100; FailSafe > 1; FailSafe--)
746 {
747 KeStallExecutionProcessor(100);
748 UsbSts = EHCI_READ_REGISTER_ULONG(EHCI_USBSTS);
749
750 if (!(UsbSts & EHCI_STS_HALT) && (UsbSts & EHCI_STS_PSS))
751 {
752 break;
753 }
754 }
755
756 if (UsbSts & EHCI_STS_HALT)
757 {
758 DPRINT1("Could not start execution on the controller\n");
759 ASSERT(FALSE);
760 return STATUS_UNSUCCESSFUL;
761 }
762
763 if (!(UsbSts & EHCI_STS_PSS))
764 {
765 DPRINT1("Could not enable periodic scheduling\n");
766 ASSERT(FALSE);
767 return STATUS_UNSUCCESSFUL;
768 }
769
770 //
771 // Assign the AsyncList Register
772 //
773 EHCI_WRITE_REGISTER_ULONG(EHCI_ASYNCLISTBASE, AsyncQueueHead->PhysicalAddr);
774
775 //
776 // get command register
777 //
778 GetCommandRegister(&UsbCmd);
779
780 //
781 // preserve bits
782 //
783 UsbCmd.AsyncEnable = TRUE;
784
785 //
786 // enable async
787 //
788 SetCommandRegister(&UsbCmd);
789
790 //
791 // Wait for execution to start
792 //
793 for (FailSafe = 100; FailSafe > 1; FailSafe--)
794 {
795 KeStallExecutionProcessor(100);
796 UsbSts = EHCI_READ_REGISTER_ULONG(EHCI_USBSTS);
797
798 if ((UsbSts & EHCI_STS_ASS))
799 {
800 break;
801 }
802 }
803
804 if (!(UsbSts & EHCI_STS_ASS))
805 {
806 DPRINT1("Failed to enable async schedule UsbSts %x\n", UsbSts);
807 ASSERT(FALSE);
808 return STATUS_UNSUCCESSFUL;
809 }
810
811 DPRINT1("UsbSts %x\n", UsbSts);
812 GetCommandRegister(&UsbCmd);
813
814 DPRINT1("UsbCmd.PeriodicEnable %x\n", UsbCmd.PeriodicEnable);
815 DPRINT1("UsbCmd.AsyncEnable %x\n", UsbCmd.AsyncEnable);
816 DPRINT1("UsbCmd.IntThreshold %x\n", UsbCmd.IntThreshold);
817 DPRINT1("UsbCmd.Run %x\n", UsbCmd.Run);
818 DPRINT1("UsbCmd.FrameListSize %x\n", UsbCmd.FrameListSize);
819
820 //
821 // Set port routing to EHCI controller
822 //
823 EHCI_WRITE_REGISTER_ULONG(EHCI_CONFIGFLAG, 1);
824
825 DPRINT1("EHCI Started!\n");
826 return STATUS_SUCCESS;
827 }
828
829 NTSTATUS
830 CUSBHardwareDevice::StopController(void)
831 {
832 EHCI_USBCMD_CONTENT UsbCmd;
833 ULONG UsbSts, FailSafe;
834
835 //
836 // Disable Interrupts and stop execution
837 //
838 EHCI_WRITE_REGISTER_ULONG (EHCI_USBINTR, 0);
839
840 GetCommandRegister(&UsbCmd);
841 UsbCmd.Run = FALSE;
842 SetCommandRegister(&UsbCmd);
843
844 for (FailSafe = 100; FailSafe > 1; FailSafe--)
845 {
846 KeStallExecutionProcessor(10);
847 UsbSts = EHCI_READ_REGISTER_ULONG(EHCI_USBSTS);
848 if (UsbSts & EHCI_STS_HALT)
849 {
850 break;
851 }
852 }
853
854 if (!(UsbSts & EHCI_STS_HALT))
855 {
856 DPRINT1("EHCI ERROR: Controller is not responding to Stop request!\n");
857 return STATUS_UNSUCCESSFUL;
858 }
859
860 return STATUS_SUCCESS;
861 }
862
863 NTSTATUS
864 CUSBHardwareDevice::ResetController(void)
865 {
866 EHCI_USBCMD_CONTENT UsbCmd;
867 ULONG FailSafe;
868
869 GetCommandRegister(&UsbCmd);
870 UsbCmd.HCReset = TRUE;
871 SetCommandRegister(&UsbCmd);
872
873 for (FailSafe = 100; FailSafe > 1; FailSafe--)
874 {
875 KeStallExecutionProcessor(100);
876 GetCommandRegister(&UsbCmd);
877 if (!UsbCmd.HCReset)
878 break;
879 }
880
881 if (UsbCmd.HCReset)
882 {
883 DPRINT1("EHCI ERROR: Controller is not responding to reset request!\n");
884 return STATUS_UNSUCCESSFUL;
885 }
886
887 return STATUS_SUCCESS;
888 }
889
890 NTSTATUS
891 CUSBHardwareDevice::ResetPort(
892 IN ULONG PortIndex)
893 {
894 ULONG PortStatus;
895 LARGE_INTEGER Timeout;
896
897 if (PortIndex > m_Capabilities.HCSParams.PortCount)
898 return STATUS_UNSUCCESSFUL;
899
900 PortStatus = EHCI_READ_REGISTER_ULONG(EHCI_PORTSC + (4 * PortIndex));
901 //
902 // check slow speed line before reset
903 //
904 if (PortStatus & EHCI_PRT_SLOWSPEEDLINE)
905 {
906 DPRINT1("Non HighSpeed device. Releasing Ownership\n");
907 EHCI_WRITE_REGISTER_ULONG(EHCI_PORTSC + (4 * PortIndex), EHCI_PRT_RELEASEOWNERSHIP);
908 return STATUS_DEVICE_NOT_CONNECTED;
909 }
910
911 ASSERT(PortStatus & EHCI_PRT_CONNECTED);
912
913 //
914 // Reset and clean enable
915 //
916 PortStatus |= EHCI_PRT_RESET;
917 PortStatus &= ~EHCI_PRT_ENABLED;
918 EHCI_WRITE_REGISTER_ULONG(EHCI_PORTSC + (4 * PortIndex), PortStatus);
919
920 //
921 // delay is 20 ms for port reset as per USB 2.0 spec
922 //
923 Timeout.QuadPart = 20;
924 DPRINT1("Waiting %d milliseconds for port reset\n", Timeout.LowPart);
925
926 //
927 // convert to 100 ns units (absolute)
928 //
929 Timeout.QuadPart *= -10000;
930
931 //
932 // perform the wait
933 //
934 KeDelayExecutionThread(KernelMode, FALSE, &Timeout);
935
936 //
937 // Clear reset
938 //
939 PortStatus = EHCI_READ_REGISTER_ULONG(EHCI_PORTSC + (4 * PortIndex));
940 PortStatus &= ~EHCI_PRT_RESET;
941 EHCI_WRITE_REGISTER_ULONG(EHCI_PORTSC + (4 * PortIndex), PortStatus);
942
943 do
944 {
945 //
946 // wait
947 //
948 KeStallExecutionProcessor(100);
949
950 //
951 // Check that the port reset
952 //
953 PortStatus = EHCI_READ_REGISTER_ULONG(EHCI_PORTSC + (4 * PortIndex));
954 if (!(PortStatus & EHCI_PRT_RESET))
955 break;
956 } while (TRUE);
957
958 //
959 // delay is 10 ms
960 //
961 Timeout.QuadPart = 10;
962 DPRINT1("Waiting %d milliseconds for port to recover after reset\n", Timeout.LowPart);
963
964 //
965 // convert to 100 ns units (absolute)
966 //
967 Timeout.QuadPart *= -10000;
968
969 //
970 // perform the wait
971 //
972 KeDelayExecutionThread(KernelMode, FALSE, &Timeout);
973
974 //
975 // check slow speed line after reset
976 //
977 PortStatus = EHCI_READ_REGISTER_ULONG(EHCI_PORTSC + (4 * PortIndex));
978 if (PortStatus & EHCI_PRT_SLOWSPEEDLINE)
979 {
980 DPRINT1("Non HighSpeed device. Releasing Ownership\n");
981 EHCI_WRITE_REGISTER_ULONG(EHCI_PORTSC + (4 * PortIndex), EHCI_PRT_RELEASEOWNERSHIP);
982 return STATUS_DEVICE_NOT_CONNECTED;
983 }
984
985 //
986 // this must be enabled now
987 //
988 ASSERT(PortStatus & EHCI_PRT_ENABLED);
989
990 return STATUS_SUCCESS;
991 }
992
993 NTSTATUS
994 CUSBHardwareDevice::GetPortStatus(
995 ULONG PortId,
996 OUT USHORT *PortStatus,
997 OUT USHORT *PortChange)
998 {
999 ULONG Value;
1000 USHORT Status = 0, Change = 0;
1001
1002 if (PortId > m_Capabilities.HCSParams.PortCount)
1003 return STATUS_UNSUCCESSFUL;
1004
1005 //
1006 // Get the value of the Port Status and Control Register
1007 //
1008 Value = EHCI_READ_REGISTER_ULONG(EHCI_PORTSC + (4 * PortId));
1009
1010 //
1011 // If the PowerPortControl is 0 then host controller does not have power control switches
1012 if (!m_Capabilities.HCSParams.PortPowerControl)
1013 {
1014 Status |= USB_PORT_STATUS_POWER;
1015 }
1016 else
1017 {
1018 // Check the value of PortPower
1019 if (Value & EHCI_PRT_POWER)
1020 {
1021 Status |= USB_PORT_STATUS_POWER;
1022 }
1023 }
1024
1025 // Get Connected Status
1026 if (Value & EHCI_PRT_CONNECTED)
1027 {
1028 Status |= USB_PORT_STATUS_CONNECT;
1029
1030 // Get Speed. If SlowSpeedLine flag is there then its a slow speed device
1031 if (Value & EHCI_PRT_SLOWSPEEDLINE)
1032 Status |= USB_PORT_STATUS_LOW_SPEED;
1033 else
1034 Status |= USB_PORT_STATUS_HIGH_SPEED;
1035 }
1036
1037 // Get Enabled Status
1038 if (Value & EHCI_PRT_ENABLED)
1039 Status |= USB_PORT_STATUS_ENABLE;
1040
1041 // Is it suspended?
1042 if (Value & EHCI_PRT_SUSPEND)
1043 Status |= USB_PORT_STATUS_SUSPEND;
1044
1045 // a overcurrent is active?
1046 if (Value & EHCI_PRT_OVERCURRENTACTIVE)
1047 Status |= USB_PORT_STATUS_OVER_CURRENT;
1048
1049 // In a reset state?
1050 if ((Value & EHCI_PRT_RESET) || m_ResetInProgress[PortId])
1051 {
1052 Status |= USB_PORT_STATUS_RESET;
1053 Change |= USB_PORT_STATUS_RESET;
1054 }
1055
1056 //
1057 // FIXME: Is the Change here correct?
1058 //
1059 if (Value & EHCI_PRT_CONNECTSTATUSCHANGE)
1060 Change |= USB_PORT_STATUS_CONNECT;
1061
1062 if (Value & EHCI_PRT_ENABLEDSTATUSCHANGE)
1063 Change |= USB_PORT_STATUS_ENABLE;
1064
1065 *PortStatus = Status;
1066 *PortChange = Change;
1067
1068 return STATUS_SUCCESS;
1069 }
1070
1071 NTSTATUS
1072 CUSBHardwareDevice::ClearPortStatus(
1073 ULONG PortId,
1074 ULONG Status)
1075 {
1076 ULONG Value;
1077
1078 DPRINT("CUSBHardwareDevice::ClearPortStatus PortId %x Feature %x\n", PortId, Status);
1079
1080 if (PortId > m_Capabilities.HCSParams.PortCount)
1081 return STATUS_UNSUCCESSFUL;
1082
1083 if (Status == C_PORT_RESET)
1084 {
1085 //
1086 // update port status
1087 //
1088 m_ResetInProgress[PortId] = FALSE;
1089 }
1090
1091 if (Status == C_PORT_CONNECTION)
1092 {
1093 LARGE_INTEGER Timeout;
1094
1095 //
1096 // reset status change bits
1097 //
1098 Value = EHCI_READ_REGISTER_ULONG(EHCI_PORTSC + (4 * PortId));
1099 Value |= EHCI_PRT_CONNECTSTATUSCHANGE | EHCI_PRT_ENABLEDSTATUSCHANGE;
1100 EHCI_WRITE_REGISTER_ULONG(EHCI_PORTSC + (4 * PortId), Value);
1101
1102 //
1103 // delay is 100 ms
1104 //
1105 Timeout.QuadPart = 100;
1106 DPRINT1("Waiting %d milliseconds for port to stabilize after connection\n", Timeout.LowPart);
1107
1108 //
1109 // convert to 100 ns units (absolute)
1110 //
1111 Timeout.QuadPart *= -10000;
1112
1113 //
1114 // perform the wait
1115 //
1116 KeDelayExecutionThread(KernelMode, FALSE, &Timeout);
1117 }
1118
1119 return STATUS_SUCCESS;
1120 }
1121
1122
1123 NTSTATUS
1124 CUSBHardwareDevice::SetPortFeature(
1125 ULONG PortId,
1126 ULONG Feature)
1127 {
1128 ULONG Value;
1129
1130 DPRINT("CUSBHardwareDevice::SetPortFeature\n");
1131
1132 if (PortId > m_Capabilities.HCSParams.PortCount)
1133 return STATUS_UNSUCCESSFUL;
1134
1135 Value = EHCI_READ_REGISTER_ULONG(EHCI_PORTSC + (4 * PortId));
1136
1137 if (Feature == PORT_ENABLE)
1138 {
1139 //
1140 // FIXME: EHCI Ports can only be disabled via reset
1141 //
1142 DPRINT1("PORT_ENABLE not supported for EHCI\n");
1143 }
1144
1145 if (Feature == PORT_RESET)
1146 {
1147 ResetPort(PortId);
1148
1149 //
1150 // update cached settings
1151 //
1152 m_ResetInProgress[PortId] = TRUE;
1153
1154 //
1155 // is there a status change callback
1156 //
1157 if (m_SCECallBack != NULL)
1158 {
1159 //
1160 // issue callback
1161 //
1162 m_SCECallBack(m_SCEContext);
1163 }
1164 }
1165
1166 if (Feature == PORT_POWER)
1167 {
1168 if (m_Capabilities.HCSParams.PortPowerControl)
1169 {
1170 ULONG Value;
1171 LARGE_INTEGER Timeout;
1172
1173 //
1174 // enable port power
1175 //
1176 Value = EHCI_READ_REGISTER_ULONG(EHCI_PORTSC + (4 * PortId)) | EHCI_PRT_POWER;
1177 EHCI_WRITE_REGISTER_ULONG(EHCI_PORTSC, Value);
1178
1179 //
1180 // delay is 20 ms
1181 //
1182 Timeout.QuadPart = 20;
1183 DPRINT1("Waiting %d milliseconds for port power up\n", Timeout.LowPart);
1184
1185 //
1186 // convert to 100 ns units (absolute)
1187 //
1188 Timeout.QuadPart *= -10000;
1189
1190 //
1191 // perform the wait
1192 //
1193 KeDelayExecutionThread(KernelMode, FALSE, &Timeout);
1194 }
1195 }
1196 return STATUS_SUCCESS;
1197 }
1198
1199 VOID
1200 CUSBHardwareDevice::SetAsyncListRegister(
1201 ULONG PhysicalAddress)
1202 {
1203 EHCI_WRITE_REGISTER_ULONG(EHCI_ASYNCLISTBASE, PhysicalAddress);
1204 }
1205
1206 VOID
1207 CUSBHardwareDevice::SetPeriodicListRegister(
1208 ULONG PhysicalAddress)
1209 {
1210 //
1211 // store physical address
1212 //
1213 m_SyncFramePhysAddr = PhysicalAddress;
1214 }
1215
1216 struct _QUEUE_HEAD *
1217 CUSBHardwareDevice::GetAsyncListQueueHead()
1218 {
1219 return AsyncQueueHead;
1220 }
1221
1222 ULONG CUSBHardwareDevice::GetPeriodicListRegister()
1223 {
1224 UNIMPLEMENTED
1225 return NULL;
1226 }
1227
1228 VOID CUSBHardwareDevice::SetStatusChangeEndpointCallBack(
1229 PVOID CallBack,
1230 PVOID Context)
1231 {
1232 m_SCECallBack = (HD_INIT_CALLBACK*)CallBack;
1233 m_SCEContext = Context;
1234 }
1235
1236 KIRQL
1237 CUSBHardwareDevice::AcquireDeviceLock(void)
1238 {
1239 KIRQL OldLevel;
1240
1241 //
1242 // acquire lock
1243 //
1244 KeAcquireSpinLock(&m_Lock, &OldLevel);
1245
1246 //
1247 // return old irql
1248 //
1249 return OldLevel;
1250 }
1251
1252
1253 VOID
1254 CUSBHardwareDevice::ReleaseDeviceLock(
1255 KIRQL OldLevel)
1256 {
1257 KeReleaseSpinLock(&m_Lock, OldLevel);
1258 }
1259
1260 BOOLEAN
1261 NTAPI
1262 InterruptServiceRoutine(
1263 IN PKINTERRUPT Interrupt,
1264 IN PVOID ServiceContext)
1265 {
1266 CUSBHardwareDevice *This;
1267 ULONG CStatus;
1268
1269 This = (CUSBHardwareDevice*) ServiceContext;
1270 CStatus = This->EHCI_READ_REGISTER_ULONG(EHCI_USBSTS);
1271
1272 CStatus &= (EHCI_ERROR_INT | EHCI_STS_INT | EHCI_STS_IAA | EHCI_STS_PCD | EHCI_STS_FLR);
1273 DPRINT1("CStatus %x\n", CStatus);
1274
1275 //
1276 // Check that it belongs to EHCI
1277 //
1278 if (!CStatus)
1279 return FALSE;
1280
1281 //
1282 // Clear the Status
1283 //
1284 This->EHCI_WRITE_REGISTER_ULONG(EHCI_USBSTS, CStatus);
1285
1286 if (CStatus & EHCI_STS_FATAL)
1287 {
1288 This->StopController();
1289 DPRINT1("EHCI: Host System Error!\n");
1290 return TRUE;
1291 }
1292
1293 if (CStatus & EHCI_ERROR_INT)
1294 {
1295 DPRINT1("EHCI Status = 0x%x\n", CStatus);
1296 }
1297
1298 if (CStatus & EHCI_STS_HALT)
1299 {
1300 DPRINT1("Host Error Unexpected Halt\n");
1301 // FIXME: Reset controller\n");
1302 return TRUE;
1303 }
1304
1305 KeInsertQueueDpc(&This->m_IntDpcObject, This, (PVOID)CStatus);
1306 return TRUE;
1307 }
1308
1309 VOID NTAPI
1310 EhciDefferedRoutine(
1311 IN PKDPC Dpc,
1312 IN PVOID DeferredContext,
1313 IN PVOID SystemArgument1,
1314 IN PVOID SystemArgument2)
1315 {
1316 CUSBHardwareDevice *This;
1317 ULONG CStatus, PortStatus, PortCount, i, ShouldRingDoorBell;
1318 NTSTATUS Status = STATUS_SUCCESS;
1319 EHCI_USBCMD_CONTENT UsbCmd;
1320
1321 This = (CUSBHardwareDevice*) SystemArgument1;
1322 CStatus = (ULONG) SystemArgument2;
1323
1324 DPRINT("CStatus %x\n", CStatus);
1325
1326 //
1327 // check for completion of async schedule
1328 //
1329 if (CStatus & (EHCI_STS_RECL| EHCI_STS_INT | EHCI_ERROR_INT))
1330 {
1331 //
1332 // check if there is a door bell ring in progress
1333 //
1334 if (This->m_DoorBellRingInProgress == FALSE)
1335 {
1336 if (CStatus & EHCI_ERROR_INT)
1337 {
1338 //
1339 // controller reported error
1340 //
1341 DPRINT1("CStatus %x\n", CStatus);
1342 ASSERT(FALSE);
1343 }
1344
1345 //
1346 // inform IUSBQueue of a completed queue head
1347 //
1348 This->m_UsbQueue->InterruptCallback(Status, &ShouldRingDoorBell);
1349
1350 //
1351 // was a queue head completed?
1352 //
1353 if (ShouldRingDoorBell)
1354 {
1355 //
1356 // set door ring bell in progress status flag
1357 //
1358 This->m_DoorBellRingInProgress = TRUE;
1359
1360 //
1361 // get command register
1362 //
1363 This->GetCommandRegister(&UsbCmd);
1364
1365 //
1366 // set door rang bell bit
1367 //
1368 UsbCmd.DoorBell = TRUE;
1369
1370 //
1371 // update command status
1372 //
1373 This->SetCommandRegister(&UsbCmd);
1374 }
1375 }
1376 }
1377
1378 //
1379 // check if the controller has acknowledged the door bell
1380 //
1381 if (CStatus & EHCI_STS_IAA)
1382 {
1383 //
1384 // controller has acknowledged, assert we rang the bell
1385 //
1386 PC_ASSERT(This->m_DoorBellRingInProgress == TRUE);
1387
1388 //
1389 // now notify IUSBQueue that it can free completed requests
1390 //
1391 This->m_UsbQueue->CompleteAsyncRequests();
1392
1393 //
1394 // door ring bell completed
1395 //
1396 This->m_DoorBellRingInProgress = FALSE;
1397 }
1398
1399 This->GetDeviceDetails(NULL, NULL, &PortCount, NULL);
1400 if (CStatus & EHCI_STS_PCD)
1401 {
1402 for (i = 0; i < PortCount; i++)
1403 {
1404 PortStatus = This->EHCI_READ_REGISTER_ULONG(EHCI_PORTSC + (4 * i));
1405
1406 //
1407 // Device connected or removed
1408 //
1409 if (PortStatus & EHCI_PRT_CONNECTSTATUSCHANGE)
1410 {
1411 if (PortStatus & EHCI_PRT_CONNECTED)
1412 {
1413 DPRINT1("Device connected on port %d\n", i);
1414
1415 //
1416 //FIXME: Determine device speed
1417 //
1418 if (This->m_Capabilities.HCSParams.CHCCount)
1419 {
1420 if (PortStatus & EHCI_PRT_ENABLED)
1421 {
1422 DPRINT1("Misbeaving controller. Port should be disabled at this point\n");
1423 }
1424
1425 if (PortStatus & EHCI_PRT_SLOWSPEEDLINE)
1426 {
1427 DPRINT1("Non HighSpeed device connected. Release ownership\n");
1428 This->EHCI_WRITE_REGISTER_ULONG(EHCI_PORTSC + (4 * i), EHCI_PRT_RELEASEOWNERSHIP);
1429 continue;
1430 }
1431 }
1432 }
1433 else
1434 {
1435 DPRINT1("Device disconnected on port %d\n", i);
1436 }
1437
1438 //
1439 // is there a status change callback
1440 //
1441 if (This->m_SCECallBack != NULL)
1442 {
1443 //
1444 // queue work item for processing
1445 //
1446 ExQueueWorkItem(&This->m_StatusChangeWorkItem, DelayedWorkQueue);
1447 }
1448
1449 //
1450 // FIXME: This needs to be saved somewhere
1451 //
1452 }
1453 }
1454 }
1455 return;
1456 }
1457
1458 VOID
1459 NTAPI
1460 StatusChangeWorkItemRoutine(
1461 PVOID Context)
1462 {
1463 //
1464 // cast to hardware object
1465 //
1466 CUSBHardwareDevice * This = (CUSBHardwareDevice*)Context;
1467
1468 //
1469 // is there a callback
1470 //
1471 if (This->m_SCECallBack)
1472 {
1473 //
1474 // issue callback
1475 //
1476 This->m_SCECallBack(This->m_SCEContext);
1477 }
1478
1479 }
1480
1481 NTSTATUS
1482 CreateUSBHardware(
1483 PUSBHARDWAREDEVICE *OutHardware)
1484 {
1485 PUSBHARDWAREDEVICE This;
1486
1487 This = new(NonPagedPool, TAG_USBEHCI) CUSBHardwareDevice(0);
1488
1489 if (!This)
1490 return STATUS_INSUFFICIENT_RESOURCES;
1491
1492 This->AddRef();
1493
1494 // return result
1495 *OutHardware = (PUSBHARDWAREDEVICE)This;
1496
1497 return STATUS_SUCCESS;
1498 }