Windows NT 4.0 source code leak
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.

303 lines
6.6 KiB

4 years ago
  1. /*
  2. ************************************************************************
  3. *
  4. * RESOURCE.c
  5. *
  6. * IRMINI Infrared Serial NDIS Miniport driver.
  7. *
  8. * (C) Copyright 1996 Microsoft Corp.
  9. *
  10. *
  11. * (ep)
  12. *
  13. *************************************************************************
  14. */
  15. #include "irmini.h"
  16. /*
  17. *************************************************************************
  18. * MyMemAlloc
  19. *************************************************************************
  20. *
  21. */
  22. PVOID MyMemAlloc(UINT size)
  23. {
  24. NDIS_STATUS stat;
  25. PVOID memptr;
  26. NDIS_PHYSICAL_ADDRESS noMaxAddr = NDIS_PHYSICAL_ADDRESS_CONST(-1,-1);
  27. stat = NdisAllocateMemory(&memptr, size, 0, noMaxAddr);
  28. if (stat != NDIS_STATUS_SUCCESS){
  29. DBGERR(("Memory allocation failed"));
  30. memptr = NULL;
  31. }
  32. return memptr;
  33. }
  34. /*
  35. *************************************************************************
  36. * MyMemFree
  37. *************************************************************************
  38. *
  39. */
  40. VOID MyMemFree(PVOID memptr, UINT size)
  41. {
  42. NdisFreeMemory(memptr, size, 0);
  43. }
  44. /*
  45. *************************************************************************
  46. * NewDevice
  47. *************************************************************************
  48. *
  49. */
  50. IrDevice *NewDevice()
  51. {
  52. IrDevice *newdev;
  53. newdev = MyMemAlloc(sizeof(IrDevice));
  54. if (newdev){
  55. InitDevice(newdev);
  56. }
  57. return newdev;
  58. }
  59. /*
  60. *************************************************************************
  61. * FreeDevice
  62. *************************************************************************
  63. *
  64. */
  65. VOID FreeDevice(IrDevice *dev)
  66. {
  67. CloseDevice(dev);
  68. MyMemFree((PVOID)dev, sizeof(IrDevice));
  69. }
  70. /*
  71. *************************************************************************
  72. * InitDevice
  73. *************************************************************************
  74. *
  75. * Zero out the device object.
  76. *
  77. * Allocate the device object's spinlock, which will persist while
  78. * the device is opened and closed.
  79. *
  80. */
  81. VOID InitDevice(IrDevice *thisDev)
  82. {
  83. NdisZeroMemory((PVOID)thisDev, sizeof(IrDevice));
  84. thisDev->firstRcvBufIndex = thisDev->lastRcvBufIndex = NO_BUF_INDEX;
  85. }
  86. /*
  87. *************************************************************************
  88. * OpenDevice
  89. *************************************************************************
  90. *
  91. * Allocate resources for a single device object.
  92. *
  93. * This function should be called with device lock already held.
  94. *
  95. */
  96. BOOLEAN OpenDevice(IrDevice *thisDev)
  97. {
  98. BOOLEAN result = FALSE;
  99. NDIS_STATUS stat;
  100. UINT bufIndex;
  101. DBGOUT(("OpenDevice()"));
  102. if (!thisDev){
  103. return FALSE;
  104. }
  105. /*
  106. * Allocate the NDIS packet and NDIS buffer pools
  107. * for this device's RECEIVE buffer queue.
  108. * Our receive packets must only contain one buffer apiece,
  109. * so #buffers == #packets.
  110. */
  111. NdisAllocatePacketPool(&stat, &thisDev->packetPoolHandle, NUM_RCV_BUFS, 24);
  112. if (stat != NDIS_STATUS_SUCCESS){
  113. goto _openDone;
  114. }
  115. NdisAllocateBufferPool(&stat, &thisDev->bufferPoolHandle, NUM_RCV_BUFS);
  116. if (stat != NDIS_STATUS_SUCCESS){
  117. goto _openDone;
  118. }
  119. /*
  120. * Initialize each of the RECEIVE packet objects for this device.
  121. */
  122. for (bufIndex = 0; bufIndex < NUM_RCV_BUFS; bufIndex++){
  123. rcvBuffer *rcvBuf = &thisDev->rcvBufs[bufIndex];
  124. rcvBuf->state = STATE_FREE;
  125. /*
  126. * Allocate a data buffer
  127. *
  128. * This buffer gets swapped with the one on comPortInfo
  129. * and must be the same size.
  130. */
  131. rcvBuf->dataBuf = MyMemAlloc(RCV_BUFFER_SIZE);
  132. if (!rcvBuf->dataBuf){
  133. goto _openDone;
  134. }
  135. /*
  136. * Allocate the NDIS_PACKET.
  137. */
  138. NdisAllocatePacket(&stat, &rcvBuf->packet, thisDev->packetPoolHandle);
  139. if (stat != NDIS_STATUS_SUCCESS){
  140. goto _openDone;
  141. }
  142. /*
  143. * For future convenience, set the MiniportReserved portion of the packet
  144. * to the index of the rcv buffer that contains it.
  145. * This will be used in ReturnPacketHandler.
  146. */
  147. *(ULONG *)rcvBuf->packet->MiniportReserved = (ULONG)bufIndex;
  148. rcvBuf->dataLen = 0;
  149. }
  150. thisDev->firstRcvBufIndex = thisDev->lastRcvBufIndex = NO_BUF_INDEX;
  151. /*
  152. * Initialize each of the SEND queue objects for this device.
  153. */
  154. thisDev->firstSendPacket = thisDev->lastSendPacket = NULL;
  155. /*
  156. * Set mediaBusy to TRUE initially. That way, we won't
  157. * IndicateStatus to the protocol in the ISR unless the
  158. * protocol has expressed interest by clearing this flag
  159. * via MiniportSetInformation(OID_IRDA_MEDIA_BUSY).
  160. */
  161. thisDev->mediaBusy = FALSE;
  162. thisDev->haveIndicatedMediaBusy = FALSE;
  163. /*
  164. * Will set speed to 9600 baud initially.
  165. */
  166. thisDev->linkSpeedInfo = &supportedBaudRateTable[BAUDRATE_9600];
  167. thisDev->lastPacketAtOldSpeed = NULL;
  168. thisDev->setSpeedAfterCurrentSendPacket = FALSE;
  169. result = TRUE;
  170. _openDone:
  171. if (!result){
  172. /*
  173. * If we're failing, close the device to free up any resources
  174. * that were allocated for it.
  175. */
  176. CloseDevice(thisDev);
  177. DBGOUT(("OpenDevice() failed"));
  178. }
  179. else {
  180. DBGOUT(("OpenDevice() succeeded"));
  181. }
  182. return result;
  183. }
  184. /*
  185. *************************************************************************
  186. * CloseDevice
  187. *************************************************************************
  188. *
  189. * Free the indicated device's resources.
  190. *
  191. *
  192. * Called for shutdown and reset.
  193. * Don't clear ndisAdapterHandle, since we might just be resetting.
  194. * This function should be called with device lock held.
  195. *
  196. *
  197. */
  198. VOID CloseDevice(IrDevice *thisDev)
  199. {
  200. UINT bufIndex;
  201. DBGOUT(("CloseDevice()"));
  202. if (!thisDev){
  203. return;
  204. }
  205. /*
  206. * Free all resources for the RECEIVE buffer queue.
  207. */
  208. for (bufIndex = 0; bufIndex < NUM_RCV_BUFS; bufIndex++){
  209. rcvBuffer *rcvBuf = &thisDev->rcvBufs[bufIndex];
  210. if (rcvBuf->packet){
  211. NdisFreePacket(rcvBuf->packet);
  212. rcvBuf->packet = NULL;
  213. }
  214. if (rcvBuf->dataBuf){
  215. MyMemFree(rcvBuf->dataBuf, RCV_BUFFER_SIZE);
  216. rcvBuf->dataBuf = NULL;
  217. }
  218. rcvBuf->dataLen = 0;
  219. rcvBuf->state = STATE_FREE;
  220. }
  221. thisDev->firstRcvBufIndex = thisDev->lastRcvBufIndex = NO_BUF_INDEX;
  222. /*
  223. * Free the packet and buffer pool handles for this device.
  224. */
  225. if (thisDev->packetPoolHandle){
  226. NdisFreePacketPool(thisDev->packetPoolHandle);
  227. thisDev->packetPoolHandle = NULL;
  228. }
  229. if (thisDev->bufferPoolHandle){
  230. NdisFreeBufferPool(thisDev->bufferPoolHandle);
  231. thisDev->bufferPoolHandle = NULL;
  232. }
  233. /*
  234. * Free all resources for the SEND buffer queue.
  235. */
  236. while (thisDev->firstSendPacket){
  237. PNDIS_PACKET nextPacket = *(PNDIS_PACKET *)thisDev->firstSendPacket->MiniportReserved;
  238. NdisMSendComplete(thisDev->ndisAdapterHandle, thisDev->firstSendPacket, NDIS_STATUS_FAILURE);
  239. thisDev->firstSendPacket = nextPacket;
  240. }
  241. thisDev->mediaBusy = FALSE;
  242. thisDev->haveIndicatedMediaBusy = FALSE;
  243. thisDev->linkSpeedInfo = NULL;
  244. }