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.

387 lines
8.9 KiB

  1. /*++
  2. Copyright (c) 1999 Microsoft Corporation
  3. Module Name:
  4. device.cpp
  5. Abstract:
  6. Device driver core, initialization, etc.
  7. --*/
  8. #define KSDEBUG_INIT
  9. #include "BDATuner.h"
  10. #include "wdmdebug.h"
  11. #ifdef ALLOC_DATA_PRAGMA
  12. #pragma const_seg("PAGECONST")
  13. #endif // ALLOC_DATA_PRAGMA
  14. #ifdef ALLOC_PRAGMA
  15. #pragma code_seg("PAGE")
  16. #endif // ALLOC_PRAGMA
  17. extern "C"
  18. NTSTATUS
  19. DriverEntry(
  20. IN PDRIVER_OBJECT DriverObject,
  21. IN PUNICODE_STRING RegistryPathName
  22. )
  23. /*++
  24. Routine Description:
  25. Sets up the driver object.
  26. Arguments:
  27. DriverObject -
  28. Driver object for this instance.
  29. RegistryPathName -
  30. Contains the registry path which was used to load this instance.
  31. Return Values:
  32. Returns STATUS_SUCCESS if the driver was initialized.
  33. --*/
  34. {
  35. NTSTATUS Status = STATUS_SUCCESS;
  36. _DbgPrintF(DEBUGLVL_VERBOSE,("DriverEntry"));
  37. // DEBUG_BREAK;
  38. Status = KsInitializeDriver( DriverObject,
  39. RegistryPathName,
  40. &DeviceDescriptor);
  41. // DEBUG_BREAK;
  42. return Status;
  43. }
  44. // Driver Global Device instance #
  45. //
  46. static ULONG ulDeviceInstance = 0;
  47. STDMETHODIMP_(NTSTATUS)
  48. CDevice::
  49. Create(
  50. IN PKSDEVICE Device
  51. )
  52. {
  53. NTSTATUS Status = STATUS_SUCCESS;
  54. CDevice * pDevice = NULL;
  55. // DEBUG_BREAK;
  56. _DbgPrintF(DEBUGLVL_VERBOSE,("CDevice::Create"));
  57. ASSERT(Device);
  58. // Allocate memory for the our device class.
  59. //
  60. pDevice = new(PagedPool,MS_SAMPLE_TUNER_POOL_TAG) CDevice; // Tags the allocated memory
  61. if (pDevice)
  62. {
  63. //
  64. // Add the item to the object bag if we were successful.
  65. // Whenever the device goes away, the bag is cleaned up and
  66. // we will be freed.
  67. //
  68. // For backwards compatibility with DirectX 8.0, we must grab
  69. // the device mutex before doing this. For Windows XP, this is
  70. // not required, but it is still safe.
  71. //
  72. KsAcquireDevice (Device);
  73. Status = KsAddItemToObjectBag (
  74. Device -> Bag,
  75. reinterpret_cast <PVOID> (pDevice),
  76. NULL
  77. );
  78. KsReleaseDevice (Device);
  79. if (!NT_SUCCESS (Status)) {
  80. delete pDevice;
  81. return Status;
  82. }
  83. // Point the KSDEVICE at our device class.
  84. //
  85. Device->Context = pDevice;
  86. // Point back to the KSDEVICE.
  87. //
  88. pDevice->m_pKSDevice = Device;
  89. // Make the resource available for a filter to use.
  90. //
  91. pDevice->m_ulcResourceUsers = 0;
  92. pDevice->m_ulCurResourceID = 0;
  93. // Get the instance number of this device.
  94. //
  95. pDevice->m_ulDeviceInstance = ulDeviceInstance++;
  96. // Set the implementation GUID. For cases where a single
  97. // driver is used for multiple implementations the INF
  98. // which installs the device would write the implementation
  99. // GUID into the registery. This code would then
  100. // read the Implementation GUID from the registry.
  101. //
  102. pDevice->m_guidImplemenation = KSMEDIUMSETID_MyImplementation;
  103. } else
  104. {
  105. Status = STATUS_INSUFFICIENT_RESOURCES;
  106. }
  107. return Status;
  108. }
  109. STDMETHODIMP_(NTSTATUS)
  110. CDevice::
  111. Start(
  112. IN PKSDEVICE pKSDevice,
  113. IN PIRP pIrp,
  114. IN PCM_RESOURCE_LIST pTranslatedResourceList OPTIONAL,
  115. IN PCM_RESOURCE_LIST pUntranslatedResourceList OPTIONAL
  116. )
  117. {
  118. NTSTATUS Status = STATUS_SUCCESS;
  119. CDevice * pDevice;
  120. PKSFILTERFACTORY pKSFilterFactory = NULL;
  121. // DEBUG_BREAK;
  122. _DbgPrintF( DEBUGLVL_VERBOSE, ("CDevice::Start"));
  123. ASSERT( pKSDevice);
  124. // DEBUG_BREAK;
  125. pDevice = reinterpret_cast<CDevice *>(pKSDevice->Context);
  126. ASSERT(pDevice);
  127. // initialize private class variables in pDevice here
  128. // Initialize the hardware.
  129. //
  130. Status = pDevice->InitializeHW();
  131. if (Status == STATUS_SUCCESS)
  132. {
  133. // Create the the Filter Factory. This factory is used to
  134. // create instances of the filter.
  135. //
  136. Status = BdaCreateFilterFactoryEx( pKSDevice,
  137. &InitialFilterDescriptor,
  138. &BdaFilterTemplate,
  139. &pKSFilterFactory
  140. );
  141. }
  142. if ((Status == STATUS_SUCCESS) && pKSFilterFactory)
  143. {
  144. BdaFilterFactoryUpdateCacheData(
  145. pKSFilterFactory,
  146. BdaFilterTemplate.pFilterDescriptor
  147. );
  148. }
  149. return Status;
  150. }
  151. NTSTATUS
  152. CDevice::
  153. InitializeHW(
  154. )
  155. {
  156. NTSTATUS Status = STATUS_SUCCESS;
  157. //
  158. // Initialize the device hardware here.
  159. //
  160. return Status;
  161. }
  162. NTSTATUS
  163. CDevice::
  164. GetStatus(
  165. PBDATUNER_DEVICE_STATUS pDeviceStatus
  166. )
  167. {
  168. NTSTATUS Status = STATUS_SUCCESS;
  169. //
  170. // Get the signal status from the HW here
  171. //
  172. // Since we don't have HW we'll fake it here.
  173. //
  174. {
  175. LONGLONG llhzFrequency;
  176. // Let's pretend that Channels 10, 25, 38, and 39 have
  177. // active ATSC signals and channels 4, 5 and 7 have analog
  178. // signals present.
  179. //
  180. llhzFrequency = m_CurResource.ulCarrierFrequency;
  181. llhzFrequency *= m_CurResource.ulFrequencyMultiplier;
  182. llhzFrequency /= 1000;
  183. if ( (llhzFrequency == (LONGLONG) 193250L)
  184. || (llhzFrequency == (LONGLONG) 537250L)
  185. || (llhzFrequency == (LONGLONG) 615250L)
  186. || (llhzFrequency == (LONGLONG) 621250L)
  187. )
  188. {
  189. pDeviceStatus->fCarrierPresent = TRUE;
  190. pDeviceStatus->fSignalLocked = TRUE;
  191. }
  192. else if ( (llhzFrequency == (LONGLONG) 67250L)
  193. || (llhzFrequency == (LONGLONG) 77250L)
  194. || (llhzFrequency == (LONGLONG) 83250L)
  195. )
  196. {
  197. pDeviceStatus->fCarrierPresent = TRUE;
  198. pDeviceStatus->fSignalLocked = FALSE;
  199. }
  200. else
  201. {
  202. pDeviceStatus->fCarrierPresent = FALSE;
  203. pDeviceStatus->fSignalLocked = FALSE;
  204. }
  205. }
  206. return Status;
  207. }
  208. NTSTATUS
  209. CDevice::
  210. AcquireResources(
  211. PBDATUNER_DEVICE_RESOURCE pNewResource,
  212. PULONG pulAcquiredResourceID
  213. )
  214. {
  215. NTSTATUS Status = STATUS_SUCCESS;
  216. LONGLONG ulhzFrequency;
  217. //
  218. // Validate the requested resource here.
  219. //
  220. // Check to see if the resources are being used by another
  221. // filter instance.
  222. //
  223. if (!m_ulcResourceUsers)
  224. {
  225. m_CurResource = *pNewResource;
  226. // Generate a new resource ID and hand it back.
  227. //
  228. m_ulCurResourceID += 25;
  229. *pulAcquiredResourceID = m_ulCurResourceID;
  230. m_ulcResourceUsers += 1;
  231. //
  232. // Configure the new resource on the hardware here.
  233. //
  234. }
  235. #ifdef RESOURCE_SHARING
  236. // For resource sharing the IsEqualResource method should be
  237. // implemented
  238. //
  239. else if (IsEqualResource( pNewResource, &m_CurResource))
  240. {
  241. *pulAcquiredResourceID = m_ulCurResourceID;
  242. m_ulcResourceUsers += 1;
  243. }
  244. #endif // RESOURCE_SHARING
  245. else
  246. {
  247. // We only allow one active filter at a time in this implementation.
  248. //
  249. Status = STATUS_DEVICE_BUSY;
  250. }
  251. return Status;
  252. }
  253. NTSTATUS
  254. CDevice::
  255. UpdateResources(
  256. PBDATUNER_DEVICE_RESOURCE pNewResource,
  257. ULONG ulResourceID
  258. )
  259. {
  260. NTSTATUS Status = STATUS_SUCCESS;
  261. LONGLONG ulhzFrequency;
  262. //
  263. // Validate the requested resource here.
  264. //
  265. // Check to see if the resources are being used by another
  266. // filter instance.
  267. //
  268. if ( m_ulcResourceUsers
  269. && (ulResourceID == m_ulCurResourceID)
  270. )
  271. {
  272. m_CurResource = *pNewResource;
  273. //
  274. // Configure the updated resource on the hardware here.
  275. //
  276. }
  277. else
  278. {
  279. // We only allow one active filter at a time in this implementation.
  280. //
  281. Status = STATUS_INVALID_DEVICE_REQUEST;
  282. }
  283. return Status;
  284. }
  285. NTSTATUS
  286. CDevice::
  287. ReleaseResources(
  288. ULONG ulResourceID
  289. )
  290. {
  291. NTSTATUS Status = STATUS_SUCCESS;
  292. if ( m_ulcResourceUsers
  293. && (ulResourceID == m_ulCurResourceID)
  294. )
  295. {
  296. // Free the resource to be used by another filter.
  297. //
  298. m_ulcResourceUsers--;
  299. }
  300. else
  301. {
  302. Status = STATUS_INVALID_DEVICE_REQUEST;
  303. }
  304. return Status;
  305. }