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.

404 lines
7.8 KiB

  1. #include "kernel.h"
  2. #include "usbdev.h"
  3. #include "usbreader.h"
  4. #include "lvprot.h"
  5. #include "gemcore.h"
  6. //$(WINICE)\nmsym.exe /translate:source,package,always obj\checked\grclass.sys
  7. #pragma LOCKEDCODE
  8. //The global visible instance of the kernel and system object
  9. CKernel* kernel = NULL;
  10. // Some static kernel variable required initialization
  11. LONG CKernel::systemtype = 0;
  12. LONG CKernel::refcount = 0;
  13. CDebug* CKernel::debug = NULL;
  14. CLogger* CKernel::logger = NULL;
  15. //CLinkedList<CUSBDevice> * CKernel::USBDeviceLinkHead = NULL;
  16. //CLinkedList<CUSBReader> * CKernel::DeviceLinkHead = NULL;
  17. //CLinkedList<CGBus> * CKernel::BusesLinkHead = NULL;
  18. CLinkedList<CDevice> * CKernel::DeviceLinkHead = NULL;
  19. // Instance number for the driver devices
  20. ULONG CDevice::DeviceNumber = 0;
  21. CKernel* CKernel::loadWDMKernel(VOID)
  22. {
  23. if( kernel == NULL )
  24. {
  25. kernel = new (NonPagedPool) CKernel;
  26. if (!ALLOCATED_OK(kernel))
  27. {
  28. DISPOSE_OBJECT(kernel);
  29. return NULL;
  30. }
  31. systemtype = WDM_SYSTEM;
  32. refcount++;
  33. CKernel::DeviceLinkHead = new (NonPagedPool) CLinkedList<CDevice>;
  34. if (!ALLOCATED_OK(CKernel::DeviceLinkHead))
  35. {
  36. DISPOSE_OBJECT(CKernel::DeviceLinkHead);
  37. DISPOSE_OBJECT(kernel);
  38. return NULL;
  39. }
  40. debug = createDebug();
  41. if(!ALLOCATED_OK(debug))
  42. {
  43. DISPOSE_OBJECT(debug);
  44. DISPOSE_OBJECT(CKernel::DeviceLinkHead);
  45. DISPOSE_OBJECT(kernel);
  46. return NULL;
  47. }
  48. logger = createLogger();
  49. if(!ALLOCATED_OK(logger))
  50. {
  51. DISPOSE_OBJECT(logger);
  52. DISPOSE_OBJECT(debug);
  53. DISPOSE_OBJECT(CKernel::DeviceLinkHead);
  54. DISPOSE_OBJECT(kernel);
  55. return NULL;
  56. }
  57. }
  58. return kernel;
  59. }
  60. CKernel* CKernel::loadNT4Kernel(VOID)
  61. {
  62. if( kernel == NULL )
  63. {
  64. kernel = new (NonPagedPool) CKernel;
  65. if (!ALLOCATED_OK(kernel))
  66. {
  67. DISPOSE_OBJECT(kernel);
  68. return NULL;
  69. }
  70. systemtype = NT4_SYSTEM;
  71. refcount++;
  72. CKernel::DeviceLinkHead = new (NonPagedPool) CLinkedList<CDevice>;
  73. if (!CKernel::DeviceLinkHead)
  74. {
  75. DISPOSE_OBJECT(CKernel::DeviceLinkHead);
  76. DISPOSE_OBJECT(kernel);
  77. return NULL;
  78. }
  79. debug = createDebug();
  80. if(!ALLOCATED_OK(debug))
  81. {
  82. DISPOSE_OBJECT(debug);
  83. DISPOSE_OBJECT(CKernel::DeviceLinkHead);
  84. DISPOSE_OBJECT(kernel);
  85. return NULL;
  86. }
  87. logger = createLogger();
  88. if(!ALLOCATED_OK(logger))
  89. {
  90. DISPOSE_OBJECT(logger);
  91. DISPOSE_OBJECT(debug);
  92. DISPOSE_OBJECT(CKernel::DeviceLinkHead);
  93. DISPOSE_OBJECT(kernel);
  94. return NULL;
  95. }
  96. }
  97. return kernel;
  98. }
  99. CKernel* CKernel::loadWin9xKernel(VOID)
  100. {
  101. if( kernel == NULL )
  102. {
  103. kernel = new (NonPagedPool) CKernel;
  104. if (!ALLOCATED_OK(kernel))
  105. {
  106. DISPOSE_OBJECT(kernel);
  107. return NULL;
  108. }
  109. systemtype = WIN9X_SYSTEM;
  110. refcount++;
  111. CKernel::DeviceLinkHead = new (NonPagedPool) CLinkedList<CDevice>;
  112. if (!CKernel::DeviceLinkHead)
  113. {
  114. DISPOSE_OBJECT(CKernel::DeviceLinkHead);
  115. DISPOSE_OBJECT(kernel);
  116. return NULL;
  117. }
  118. debug = createDebug();
  119. if(!ALLOCATED_OK(debug))
  120. {
  121. DISPOSE_OBJECT(debug);
  122. DISPOSE_OBJECT(CKernel::DeviceLinkHead);
  123. DISPOSE_OBJECT(kernel);
  124. return NULL;
  125. }
  126. logger = createLogger();
  127. if(!ALLOCATED_OK(logger))
  128. {
  129. DISPOSE_OBJECT(logger);
  130. DISPOSE_OBJECT(debug);
  131. DISPOSE_OBJECT(CKernel::DeviceLinkHead);
  132. DISPOSE_OBJECT(kernel);
  133. return NULL;
  134. }
  135. }
  136. return kernel;
  137. }
  138. VOID CKernel::dispose(VOID)
  139. {
  140. refcount--;
  141. if(refcount<=0)
  142. {
  143. TRACE("Unload: Removing all registered devices...\n");
  144. delete DeviceLinkHead; // this will kill all attached devices
  145. if(kernel->RegistryPath)
  146. {
  147. kernel->RegistryPath->dispose();
  148. }
  149. if(debug) debug->dispose();
  150. if(logger) logger->dispose();
  151. self_delete();
  152. kernel = NULL;
  153. }
  154. }
  155. CSystem* CKernel::createSystem(VOID)
  156. {
  157. if(systemtype==WDM_SYSTEM)
  158. {
  159. CSystem* obj = CWDMSystem::create();
  160. RETURN_VERIFIED_OBJECT(obj);
  161. }
  162. return NULL;
  163. }
  164. CMemory* CKernel::createMemory(VOID)
  165. {
  166. if(systemtype==WDM_SYSTEM)
  167. {
  168. CMemory* obj = CWDMMemory::create();
  169. RETURN_VERIFIED_OBJECT(obj);
  170. }
  171. return NULL;
  172. }
  173. CIrp* CKernel::createIrp(VOID)
  174. {
  175. if(systemtype==WDM_SYSTEM)
  176. {
  177. CIrp* obj = CWDMIrp::create();
  178. RETURN_VERIFIED_OBJECT(obj);
  179. }
  180. return NULL;
  181. }
  182. CEvent* CKernel::createEvent(VOID)
  183. {
  184. if(systemtype==WDM_SYSTEM)
  185. {
  186. CEvent* obj = CWDMEvent::create();
  187. RETURN_VERIFIED_OBJECT(obj);
  188. }
  189. return NULL;
  190. }
  191. CSemaphore* CKernel::createSemaphore(VOID)
  192. {
  193. if(systemtype==WDM_SYSTEM)
  194. {
  195. CSemaphore* obj = CWDMSemaphore::create();
  196. RETURN_VERIFIED_OBJECT(obj);
  197. }
  198. return NULL;
  199. }
  200. CInterrupt* CKernel::createInterrupt(VOID)
  201. {
  202. if(systemtype==WDM_SYSTEM)
  203. {
  204. CInterrupt* obj = CWDMInterrupt::create();
  205. RETURN_VERIFIED_OBJECT(obj);
  206. }
  207. return NULL;
  208. }
  209. CLock* CKernel::createLock(VOID)
  210. {
  211. if(systemtype==WDM_SYSTEM)
  212. {
  213. CLock* obj = CWDMLock::create();
  214. RETURN_VERIFIED_OBJECT(obj);
  215. }
  216. return NULL;
  217. }
  218. CPower* CKernel::createPower(VOID)
  219. {
  220. if(systemtype==WDM_SYSTEM)
  221. {
  222. CPower* obj = CWDMPower::create();
  223. RETURN_VERIFIED_OBJECT(obj);
  224. }
  225. return NULL;
  226. }
  227. CDebug* CKernel::createDebug(VOID)
  228. {
  229. // Only one debug module exist at system
  230. // It will be shared between all objects
  231. if(debug)
  232. {
  233. debug->incrementUsage();
  234. //TRACE(" Debug object reused %d times\n",);
  235. return debug;
  236. }
  237. if(systemtype==WDM_SYSTEM)
  238. {
  239. CDebug* d = CWDMDebug::create();
  240. if(!ALLOCATED_OK(d))
  241. {
  242. DISPOSE_OBJECT(d);
  243. return NULL;
  244. }
  245. d->incrementUsage();
  246. return d;
  247. }
  248. return NULL;
  249. }
  250. VOID CKernel::registerObject(PDEVICE_OBJECT fdo,CDevice* device)
  251. {
  252. fdo->DeviceExtension = device;
  253. if(!ALLOCATED_OK(device)) return;
  254. if(device->m_Type!=CHILD_DEVICE)
  255. {
  256. if(DeviceLinkHead) DeviceLinkHead->New(device);
  257. }
  258. }
  259. VOID CKernel::unregisterObject(PDEVICE_OBJECT fdo)
  260. {
  261. CDevice* device;
  262. device = ((CDevice*)fdo->DeviceExtension);
  263. fdo->DeviceExtension = NULL;
  264. if(!ALLOCATED_OK(device)) return;
  265. if(device->m_Type!=CHILD_DEVICE) DeviceLinkHead->remove(device);
  266. }
  267. CDevice* CKernel::getRegisteredDevice(PDEVICE_OBJECT fdo)
  268. {
  269. return getObjectPointer(fdo);
  270. }
  271. CReaderInterface* CKernel::createReaderInterface(LONG interfaceType,LONG protocolType,CDevice* device)
  272. {
  273. CProtocol* protocol = NULL;
  274. //CReaderInterface* interface = NULL;
  275. CGemCore* interface = NULL;// TOBE CHANGED
  276. switch(protocolType)
  277. {
  278. case READER_PROTOCOL_LV:
  279. default:
  280. protocol = new (NonPagedPool) CLVProtocol(device);// TO CHANGE LATER...
  281. break;
  282. }
  283. if(!ALLOCATED_OK(protocol))
  284. {
  285. DISPOSE_OBJECT(protocol);
  286. return NULL;
  287. }
  288. switch(interfaceType)
  289. {
  290. case READER_INTERFACE_GEMCORE:
  291. default:
  292. interface = new (NonPagedPool) CGemCore(protocol);
  293. break;
  294. }
  295. if(!ALLOCATED_OK(interface))
  296. {
  297. DISPOSE_OBJECT(interface);
  298. DISPOSE_OBJECT(protocol);
  299. return NULL;
  300. }
  301. return interface;
  302. };
  303. CTimer* CKernel::createTimer(TIMER_TYPE Type)
  304. {
  305. if(systemtype==WDM_SYSTEM)
  306. {
  307. CTimer* obj = CWDMTimer::create(Type);
  308. RETURN_VERIFIED_OBJECT(obj);
  309. }
  310. return NULL;
  311. }
  312. CLogger* CKernel::createLogger()
  313. {
  314. if(logger)
  315. {
  316. logger->incrementUsage();
  317. return logger;
  318. }
  319. if(systemtype==WDM_SYSTEM)
  320. {
  321. logger = CWDMLogger::create();
  322. if(!ALLOCATED_OK(logger))
  323. {
  324. DISPOSE_OBJECT(logger);
  325. return NULL;
  326. }
  327. logger->incrementUsage();
  328. return logger;
  329. }
  330. return NULL;
  331. }
  332. #ifdef USBREADER_PROJECT
  333. #ifndef USBDEVICE_PROJECT
  334. #define USBDEVICE_PROJECT
  335. #endif
  336. #endif
  337. #ifdef USBDEVICE_PROJECT
  338. CUSBDevice* CKernel::createUSBDevice(VOID)
  339. {
  340. if(systemtype==WDM_SYSTEM)
  341. {
  342. CUSBDevice* obj = new (NonPagedPool) CUSBDevice;
  343. RETURN_VERIFIED_OBJECT(obj);
  344. }
  345. return NULL;
  346. }
  347. #endif
  348. #ifdef USBREADER_PROJECT
  349. CUSBReader* CKernel::createUSBReader(VOID)
  350. {
  351. if(systemtype==WDM_SYSTEM)
  352. {
  353. CUSBReader* obj = new (NonPagedPool) CUSBReader;
  354. RETURN_VERIFIED_OBJECT(obj);
  355. }
  356. return NULL;
  357. }
  358. #endif