Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

242 lines
6.4 KiB

  1. /*++
  2. Copyright (c) Microsoft Corporation. All rights reserved.
  3. Module Name:
  4. dock.c
  5. Abstract:
  6. Author:
  7. Kenneth D. Ray (kenray) Feb 1998
  8. Revision History:
  9. --*/
  10. #include "pnpmgrp.h"
  11. #undef ExAllocatePool
  12. #undef ExAllocatePoolWithQuota
  13. #include "..\config\cmp.h"
  14. #include <string.h>
  15. #include <profiles.h>
  16. #include <wdmguid.h>
  17. //
  18. // Internal functions to dockhwp.c
  19. //
  20. NTSTATUS
  21. IopExecuteHardwareProfileChange(
  22. IN HARDWARE_PROFILE_BUS_TYPE Bus,
  23. IN PWCHAR * ProfileSerialNumbers,
  24. IN ULONG SerialNumbersCount,
  25. OUT PHANDLE NewProfile,
  26. OUT PBOOLEAN ProfileChanged
  27. );
  28. NTSTATUS
  29. IopExecuteHwpDefaultSelect(
  30. IN PCM_HARDWARE_PROFILE_LIST ProfileList,
  31. OUT PULONG ProfileIndexToUse,
  32. IN PVOID Context
  33. );
  34. #ifdef ALLOC_PRAGMA
  35. #pragma alloc_text(PAGE, IopExecuteHwpDefaultSelect)
  36. #pragma alloc_text(PAGE, IopExecuteHardwareProfileChange)
  37. #endif // ALLOC_PRAGMA
  38. NTSTATUS
  39. IopExecuteHwpDefaultSelect(
  40. IN PCM_HARDWARE_PROFILE_LIST ProfileList,
  41. OUT PULONG ProfileIndexToUse,
  42. IN PVOID Context
  43. )
  44. {
  45. UNREFERENCED_PARAMETER(ProfileList);
  46. UNREFERENCED_PARAMETER(Context);
  47. * ProfileIndexToUse = 0;
  48. return STATUS_SUCCESS;
  49. }
  50. NTSTATUS
  51. IopExecuteHardwareProfileChange(
  52. IN HARDWARE_PROFILE_BUS_TYPE Bus,
  53. IN PWCHAR *ProfileSerialNumbers,
  54. IN ULONG SerialNumbersCount,
  55. OUT HANDLE *NewProfile,
  56. OUT BOOLEAN *ProfileChanged
  57. )
  58. /*++
  59. Routine Description:
  60. A docking event has occured and now, given a list of Profile Serial Numbers
  61. that describe the new docking state:
  62. Transition to the given docking state.
  63. Set the Current Hardware Profile to based on the new state.
  64. (Possibly Prompt the user if there is ambiguity)
  65. Send Removes to those devices that are turned off in this profile,
  66. Arguments:
  67. Bus - This is the bus that is supplying the hardware profile change.
  68. (currently only HardwareProfileBusTypeAcpi is supported).
  69. ProfileSerialNumbers - A list of serial numbers (a list of null terminated
  70. UCHAR lists) representing this new docking state.
  71. These can be listed in any order, and form a
  72. complete representation of the new docking state
  73. caused by a docking even on the given bus. A Serial Number string of "\0"
  74. represents an "undocked state" and should not be listed with any other
  75. strings. This list need not be sorted.
  76. SerialNumbersCount - The number of serial numbers listed.
  77. NewProfile - a handle to the registry key representing the new hardware
  78. profile (IE \CCS\HardwareProfiles\Current".)
  79. ProfileChanged - set to TRUE if new current profile (as a result of this
  80. docking event, is different that then old current profile.
  81. --*/
  82. {
  83. NTSTATUS status = STATUS_SUCCESS;
  84. ULONG len;
  85. ULONG tmplen;
  86. ULONG i, j;
  87. PWCHAR tmpStr;
  88. UNICODE_STRING tmpUStr;
  89. PUNICODE_STRING sortedSerials = NULL;
  90. PPROFILE_ACPI_DOCKING_STATE dockState = NULL;
  91. IopDbgPrint(( IOP_TRACE_LEVEL,
  92. "Execute Profile (BusType %x), (SerialNumCount %x)\n", Bus, SerialNumbersCount));
  93. //
  94. // Sort the list of serial numbers
  95. //
  96. len = sizeof(UNICODE_STRING) * SerialNumbersCount;
  97. sortedSerials = ExAllocatePool(NonPagedPool, len);
  98. if (NULL == sortedSerials) {
  99. status = STATUS_INSUFFICIENT_RESOURCES;
  100. goto Clean;
  101. }
  102. for(i=0; i < SerialNumbersCount; i++) {
  103. RtlInitUnicodeString(&sortedSerials[i], ProfileSerialNumbers[i]);
  104. }
  105. //
  106. // I do not anticipate getting more than a few serial numbers, and I am
  107. // just lazy enough to write this comment and use a bubble sort.
  108. //
  109. for(i = 0; i < SerialNumbersCount; i++) {
  110. for(j = 0; j < SerialNumbersCount - 1; j++) {
  111. if (0 < RtlCompareUnicodeString(&sortedSerials[j],
  112. &sortedSerials[j+1],
  113. FALSE)) {
  114. tmpUStr = sortedSerials[j];
  115. sortedSerials[j] = sortedSerials[j+1];
  116. sortedSerials[j+1] = tmpUStr;
  117. }
  118. }
  119. }
  120. //
  121. // Construct the DockState ID
  122. //
  123. len = 0;
  124. for(i=0; i < SerialNumbersCount; i++) {
  125. len += sortedSerials[i].Length;
  126. }
  127. len += sizeof(WCHAR); // NULL termination;
  128. dockState = (PPROFILE_ACPI_DOCKING_STATE) ExAllocatePool(
  129. NonPagedPool,
  130. len + sizeof(PROFILE_ACPI_DOCKING_STATE)
  131. );
  132. if (NULL == dockState) {
  133. status = STATUS_INSUFFICIENT_RESOURCES;
  134. goto Clean;
  135. }
  136. for(i = 0, tmpStr = dockState->SerialNumber, tmplen = 0;
  137. i < SerialNumbersCount;
  138. i++) {
  139. tmplen = sortedSerials[i].Length;
  140. ASSERT(tmplen <= len - ((PCHAR)tmpStr - (PCHAR)dockState->SerialNumber));
  141. RtlCopyMemory(tmpStr, sortedSerials[i].Buffer, tmplen);
  142. tmpStr = (PWCHAR)((PCHAR)tmpStr + tmplen);
  143. }
  144. *(tmpStr++) = L'\0';
  145. ASSERT(len == (ULONG) ((PCHAR) tmpStr - (PCHAR) dockState->SerialNumber));
  146. dockState->SerialLength = (USHORT) len;
  147. if ((SerialNumbersCount > 1) || (L'\0' != dockState->SerialNumber[0])) {
  148. dockState->DockingState = HW_PROFILE_DOCKSTATE_DOCKED;
  149. } else {
  150. dockState->DockingState = HW_PROFILE_DOCKSTATE_UNDOCKED;
  151. }
  152. //
  153. // Set the new Profile
  154. //
  155. switch(Bus) {
  156. case HardwareProfileBusTypeACPI:
  157. status = CmSetAcpiHwProfile(
  158. dockState,
  159. IopExecuteHwpDefaultSelect,
  160. NULL,
  161. NewProfile,
  162. ProfileChanged
  163. );
  164. ASSERT(NT_SUCCESS(status) || (!(*ProfileChanged)));
  165. break;
  166. default:
  167. *ProfileChanged = FALSE;
  168. status = STATUS_NOT_SUPPORTED;
  169. goto Clean;
  170. }
  171. Clean:
  172. if (NULL != sortedSerials) {
  173. ExFreePool(sortedSerials);
  174. }
  175. if (NULL != dockState) {
  176. ExFreePool(dockState);
  177. }
  178. return status;
  179. }