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.

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