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.

351 lines
8.7 KiB

  1. /********************************************************************/
  2. /** Copyright(c) 1995 Microsoft Corporation. **/
  3. /********************************************************************/
  4. //***
  5. //
  6. // Filename: mediaobj.c
  7. //
  8. // Description:
  9. //
  10. // History: May 11,1995 NarenG Created original version.
  11. //
  12. #include <nt.h>
  13. #include <ntrtl.h>
  14. #include <nturtl.h>
  15. #include <windows.h>
  16. #include <winsvc.h>
  17. #include "ddm.h"
  18. #include "objects.h"
  19. #include <raserror.h>
  20. #include "rasmanif.h"
  21. #include <stdlib.h>
  22. //**
  23. //
  24. // Call: MediaObjInitializeTable
  25. //
  26. // Returns: NO_ERROR - success
  27. // else - failure
  28. //
  29. // Description: Allocates and initializes the media table
  30. //
  31. DWORD
  32. MediaObjInitializeTable(
  33. VOID
  34. )
  35. {
  36. gblMediaTable.cMediaListSize = 5;
  37. gblMediaTable.pMediaList = (MEDIA_OBJECT *)LOCAL_ALLOC(
  38. LPTR,
  39. gblMediaTable.cMediaListSize
  40. * sizeof( MEDIA_OBJECT ));
  41. if ( gblMediaTable.pMediaList == (MEDIA_OBJECT *)NULL )
  42. {
  43. return( GetLastError() );
  44. }
  45. return( NO_ERROR );
  46. }
  47. //**
  48. //
  49. // Call: MediaObjAddToTable
  50. //
  51. // Returns: NO_ERROR - success
  52. // ERROR_NOT_ENOUGH_MEMORY - Failure
  53. //
  54. // Description: Will increment the number of available resources for the
  55. // specified media.
  56. //
  57. DWORD
  58. MediaObjAddToTable(
  59. IN LPWSTR lpwsMedia
  60. )
  61. {
  62. DWORD dwIndex;
  63. MEDIA_OBJECT * pFreeEntry = NULL;
  64. //
  65. // Iterate through the media table
  66. //
  67. EnterCriticalSection( &(gblMediaTable.CriticalSection) );
  68. for ( dwIndex = 0; dwIndex < gblMediaTable.cMediaListSize; dwIndex++ )
  69. {
  70. if ( _wcsicmp( gblMediaTable.pMediaList[dwIndex].wchMediaName,
  71. lpwsMedia ) == 0 )
  72. {
  73. //
  74. // If there was no device available and there is now we need to
  75. // notify the interfaces.
  76. //
  77. if ( gblMediaTable.pMediaList[dwIndex].dwNumAvailable == 0 )
  78. {
  79. gblMediaTable.fCheckInterfaces = TRUE;
  80. }
  81. gblMediaTable.pMediaList[dwIndex].dwNumAvailable++;
  82. DDMTRACE1( "Added instance of %ws media to media table",
  83. lpwsMedia );
  84. LeaveCriticalSection( &(gblMediaTable.CriticalSection) );
  85. return( NO_ERROR );
  86. }
  87. else if ( gblMediaTable.pMediaList[dwIndex].wchMediaName[0]==(WCHAR)0 )
  88. {
  89. if ( pFreeEntry == (MEDIA_OBJECT *)NULL )
  90. {
  91. pFreeEntry = &(gblMediaTable.pMediaList[dwIndex]);
  92. }
  93. }
  94. }
  95. //
  96. // If we couldn't find this media in the table we need to add it to the
  97. // table. Check if there is space for it
  98. //
  99. if ( dwIndex == gblMediaTable.cMediaListSize )
  100. {
  101. if ( pFreeEntry == (MEDIA_OBJECT *)NULL )
  102. {
  103. PVOID pTemp;
  104. //
  105. // We need to expand the table
  106. //
  107. gblMediaTable.cMediaListSize += 5;
  108. pTemp = LOCAL_REALLOC( gblMediaTable.pMediaList,
  109. gblMediaTable.cMediaListSize
  110. * sizeof( MEDIA_OBJECT ) );
  111. if ( pTemp == NULL )
  112. {
  113. LOCAL_FREE( gblMediaTable.pMediaList );
  114. gblMediaTable.pMediaList = NULL;
  115. LeaveCriticalSection( &(gblMediaTable.CriticalSection) );
  116. return( GetLastError() );
  117. }
  118. else
  119. {
  120. gblMediaTable.pMediaList = pTemp;
  121. }
  122. pFreeEntry =
  123. &(gblMediaTable.pMediaList[gblMediaTable.cMediaListSize-5]);
  124. }
  125. }
  126. //
  127. // Add the new media
  128. //
  129. wcscpy( pFreeEntry->wchMediaName, lpwsMedia );
  130. pFreeEntry->dwNumAvailable++;
  131. gblMediaTable.fCheckInterfaces = TRUE;
  132. DDMTRACE1( "Added %ws to available media table", lpwsMedia );
  133. LeaveCriticalSection( &(gblMediaTable.CriticalSection) );
  134. return( NO_ERROR );
  135. }
  136. //**
  137. //
  138. // Call: MediaObjRemoveFromTable
  139. //
  140. // Returns: None
  141. //
  142. // Description: Will decrement the number of avaialble resources of this
  143. // media type.
  144. //
  145. VOID
  146. MediaObjRemoveFromTable(
  147. IN LPWSTR lpwsMedia
  148. )
  149. {
  150. DWORD dwIndex;
  151. EnterCriticalSection( &(gblMediaTable.CriticalSection) );
  152. //
  153. // Iterate through the media table
  154. //
  155. for ( dwIndex = 0; dwIndex < gblMediaTable.cMediaListSize; dwIndex++ )
  156. {
  157. if ( _wcsicmp( gblMediaTable.pMediaList[dwIndex].wchMediaName,
  158. lpwsMedia ) == 0 )
  159. {
  160. //
  161. // If there was device available and there are none now we need to
  162. // notify the interfaces.
  163. //
  164. if ( gblMediaTable.pMediaList[dwIndex].dwNumAvailable > 0 )
  165. {
  166. gblMediaTable.pMediaList[dwIndex].dwNumAvailable--;
  167. DDMTRACE1( "Removed instance of %ws media from media table",
  168. lpwsMedia );
  169. if ( gblMediaTable.pMediaList[dwIndex].dwNumAvailable == 0 )
  170. {
  171. gblMediaTable.fCheckInterfaces = TRUE;
  172. DDMTRACE1( "Removed %ws from available media table",
  173. lpwsMedia );
  174. }
  175. }
  176. }
  177. }
  178. LeaveCriticalSection( &(gblMediaTable.CriticalSection) );
  179. }
  180. //**
  181. //
  182. // Call: MediaObjGetAvailableMediaBits
  183. //
  184. // Returns: None
  185. //
  186. // Description: Will retrieve a DWORD of bits, each of which represent a
  187. // media od which resources are still available.
  188. //
  189. VOID
  190. MediaObjGetAvailableMediaBits(
  191. DWORD * pfAvailableMedia
  192. )
  193. {
  194. DWORD dwIndex;
  195. *pfAvailableMedia = 0;
  196. EnterCriticalSection( &(gblMediaTable.CriticalSection) );
  197. //
  198. // Iterate through the media table
  199. //
  200. if (gblMediaTable.pMediaList != NULL)
  201. {
  202. for ( dwIndex = 0; dwIndex < gblMediaTable.cMediaListSize; dwIndex++ )
  203. {
  204. if ( gblMediaTable.pMediaList[dwIndex].wchMediaName[0] != (WCHAR)0 )
  205. {
  206. if ( gblMediaTable.pMediaList[dwIndex].dwNumAvailable > 0 )
  207. {
  208. *pfAvailableMedia |= ( 1 << dwIndex );
  209. }
  210. }
  211. }
  212. }
  213. LeaveCriticalSection( &(gblMediaTable.CriticalSection) );
  214. }
  215. //**
  216. //
  217. // Call: MediaObjSetMediaBit
  218. //
  219. // Returns: NO_ERROR - Success
  220. // Non-zero returns - Failure
  221. //
  222. // Description: Sets the appropriate bit for the given media
  223. //
  224. DWORD
  225. MediaObjSetMediaBit(
  226. IN LPWSTR lpwsMedia,
  227. IN DWORD * pfMedia
  228. )
  229. {
  230. DWORD dwIndex;
  231. DWORD dwRetCode = NO_ERROR;
  232. //
  233. // Iterate through the media table
  234. //
  235. EnterCriticalSection( &(gblMediaTable.CriticalSection) );
  236. for ( dwIndex = 0; dwIndex < gblMediaTable.cMediaListSize; dwIndex++ )
  237. {
  238. if ( _wcsicmp( gblMediaTable.pMediaList[dwIndex].wchMediaName, lpwsMedia ) == 0 )
  239. {
  240. *pfMedia |= ( 1 << dwIndex );
  241. LeaveCriticalSection( &(gblMediaTable.CriticalSection) );
  242. return( NO_ERROR );
  243. }
  244. }
  245. //
  246. // If we get here that means that we do not have this media in the table so add it
  247. //
  248. MediaObjAddToTable( lpwsMedia );
  249. //
  250. // Now set the correct bit.
  251. //
  252. for ( dwIndex = 0; dwIndex < gblMediaTable.cMediaListSize; dwIndex++ )
  253. {
  254. if ( _wcsicmp( gblMediaTable.pMediaList[dwIndex].wchMediaName, lpwsMedia ) == 0 )
  255. {
  256. *pfMedia |= ( 1 << dwIndex );
  257. LeaveCriticalSection( &(gblMediaTable.CriticalSection) );
  258. //
  259. // Let the caller know that there is no such device
  260. //
  261. return( ERROR_DEVICETYPE_DOES_NOT_EXIST );
  262. }
  263. }
  264. LeaveCriticalSection( &(gblMediaTable.CriticalSection) );
  265. return( ERROR_DEVICETYPE_DOES_NOT_EXIST );
  266. }
  267. //**
  268. //
  269. // Call: MediaObjFreeTable
  270. //
  271. // Returns: NO_ERROR - Success
  272. // Non-zero returns - Failure
  273. //
  274. // Description: Cleans up the media object table
  275. //
  276. VOID
  277. MediaObjFreeTable(
  278. VOID
  279. )
  280. {
  281. DeleteCriticalSection( &(gblMediaTable.CriticalSection) );
  282. if ( gblMediaTable.pMediaList != NULL )
  283. {
  284. LOCAL_FREE( gblMediaTable.pMediaList );
  285. gblMediaTable.pMediaList = NULL;
  286. }
  287. }