Source code of Windows XP (NT5)
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.

194 lines
3.7 KiB

  1. /*++
  2. Copyright (c) 1995,1996 Microsoft Corporation
  3. :ts=4
  4. Module Name:
  5. bandwdth.c
  6. Abstract:
  7. This module contains code to calculate
  8. bandwidth usage.
  9. Environment:
  10. kernel mode only
  11. Notes:
  12. Revision History:
  13. 2-15-95 : created
  14. --*/
  15. #include "wdm.h"
  16. #include "stdarg.h"
  17. #include "stdio.h"
  18. #include "usbdi.h"
  19. #include "hcdi.h"
  20. #include "uhcd.h"
  21. VOID
  22. UHCD_InitBandwidthTable(
  23. IN PDEVICE_OBJECT DeviceObject
  24. )
  25. /*++
  26. Routine Description:
  27. Initialize our array that tracks the bw in use.
  28. Arguments:
  29. DeviceObject - pointer to a device object
  30. Return Value:
  31. None.
  32. --*/
  33. {
  34. ULONG i;
  35. PDEVICE_EXTENSION deviceExtension;
  36. deviceExtension = (PDEVICE_EXTENSION) DeviceObject->DeviceExtension;
  37. //
  38. // Note: BwTable stores bytes per frame
  39. //
  40. for (i=0; i<MAX_INTERVAL; i++) {
  41. //
  42. // max bytes per frame - bw reaerved for bulk and control
  43. //
  44. deviceExtension->BwTable[i] =
  45. UHCD_TOTAL_USB_BW - UHCD_BULK_CONTROL_BW;
  46. }
  47. }
  48. #if DBG
  49. ULONG UHCD_PnpTest = 0;
  50. #endif
  51. BOOLEAN
  52. UHCD_ManageBandwidth(
  53. IN PDEVICE_OBJECT DeviceObject,
  54. IN PUHCD_ENDPOINT Endpoint,
  55. IN ULONG Offset,
  56. IN BOOLEAN AllocateFlag
  57. )
  58. /*++
  59. Routine Description:
  60. Calculate bandwidth required to open an endpoint
  61. with the given input characteristics.
  62. Arguments:
  63. DeviceObject - pointer to a device object
  64. Offset - offset in to table to start, allows load balancing
  65. for interrupt endpoints.
  66. Interval - interval for endoint (1 for iso).
  67. AllocateFlag - TRUE if we are requesting BW, false if we
  68. are releasing it.
  69. Return Value:
  70. TRUE if enough bandwidth available open the
  71. endpoint.
  72. --*/
  73. {
  74. ULONG i;
  75. PDEVICE_EXTENSION deviceExtension;
  76. BOOLEAN ok=TRUE;
  77. ULONG interval, need;
  78. ASSERT_ENDPOINT(Endpoint);
  79. deviceExtension = (PDEVICE_EXTENSION) DeviceObject->DeviceExtension;
  80. //UHCD_KdPrint((2, "BWTable = %x\n", &deviceExtension->BwTable[0]));
  81. //UHCD_KdBreak();
  82. //
  83. // claculate what we'll need based on endpoint
  84. // type and max packet size
  85. //
  86. need =
  87. USBD_CalculateUsbBandwidth(Endpoint->MaxPacketSize,
  88. Endpoint->Type,
  89. (BOOLEAN) (Endpoint->EndpointFlags & EPFLAG_LOWSPEED));
  90. interval = Endpoint->Interval;
  91. LOGENTRY(LOG_MISC, 'aBW1',
  92. interval,
  93. Offset,
  94. need);
  95. UHCD_ASSERT(interval<=MAX_INTERVAL);
  96. UHCD_ASSERT(Offset<interval);
  97. UHCD_ASSERT(MAX_INTERVAL % interval == 0);
  98. if (AllocateFlag) {
  99. for (i=Offset; i<MAX_INTERVAL; i+=interval) {
  100. if (deviceExtension->BwTable[i] < need) {
  101. ok = FALSE;
  102. break;
  103. }
  104. }
  105. }
  106. #if DBG
  107. if (UHCD_PnpTest == 1) {
  108. ok = FALSE;
  109. }
  110. #endif
  111. if (ok) {
  112. //
  113. // there is enough, go ahead and allocate
  114. //
  115. Endpoint->Offset = (USHORT) Offset;
  116. for (i=Offset; i<MAX_INTERVAL; i+=interval) {
  117. if (AllocateFlag) {
  118. deviceExtension->BwTable[i] -= need;
  119. } else {
  120. deviceExtension->BwTable[i] += need;
  121. }
  122. UHCD_ASSERT(deviceExtension->BwTable[i] <=
  123. UHCD_TOTAL_USB_BW - UHCD_BULK_CONTROL_BW);
  124. }
  125. }
  126. LOGENTRY(LOG_MISC, 'aBW2',
  127. AllocateFlag,
  128. ok,
  129. &deviceExtension->BwTable[0]);
  130. #ifdef MAX_DEBUG
  131. if (!ok) {
  132. UHCD_KdBreak((2, "'no bandwidth\n"));
  133. }
  134. #endif
  135. return ok;
  136. }