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.

793 lines
18 KiB

  1. /*++
  2. Copyright (c) 1998-1999 Microsoft Corporation
  3. Module Name:
  4. util.c
  5. Abstract:
  6. Utility functions for ARP1394.
  7. Revision History:
  8. Who When What
  9. -------- -------- ----------------------------------------------
  10. josephj 01-05-99 Created
  11. Notes:
  12. --*/
  13. #include <precomp.h>
  14. //
  15. // File-specific debugging defaults.
  16. //
  17. #define TM_CURRENT TM_UT
  18. //=========================================================================
  19. // L O C A L P R O T O T Y P E S
  20. //=========================================================================
  21. VOID
  22. arpTaskDelete (
  23. PRM_OBJECT_HEADER pObj,
  24. PRM_STACK_RECORD psr
  25. );
  26. //
  27. // TODO: make these globals constant data.
  28. //
  29. // ArpTasks_StaticInfo contains static information about
  30. // objects of type ARP1394_TASK;
  31. //
  32. RM_STATIC_OBJECT_INFO
  33. ArpTasks_StaticInfo =
  34. {
  35. 0, // TypeUID
  36. 0, // TypeFlags
  37. "ARP1394 Task", // TypeName
  38. 0, // Timeout
  39. NULL, // pfnCreate
  40. arpTaskDelete, // pfnDelete
  41. NULL, // LockVerifier
  42. 0, // length of resource table
  43. NULL // Resource Table
  44. };
  45. VOID
  46. arpTaskDelete (
  47. PRM_OBJECT_HEADER pObj,
  48. PRM_STACK_RECORD psr
  49. )
  50. /*++
  51. Routine Description:
  52. Free an object of type ARP1394_TASK.
  53. Arguments:
  54. pHdr - Actually a pointer to the ARP1394_TASK to be deleted.
  55. --*/
  56. {
  57. TASK_BACKUP* pTask= (TASK_BACKUP*)pObj;
  58. if (CHECK_TASK_IS_BACKUP(&pTask->Hdr) == TRUE)
  59. {
  60. arpReturnBackupTask((ARP1394_TASK*)pTask);
  61. }
  62. else
  63. {
  64. ARP_FREE(pObj);
  65. }
  66. }
  67. VOID
  68. arpObjectDelete (
  69. PRM_OBJECT_HEADER pObj,
  70. PRM_STACK_RECORD psr
  71. )
  72. /*++
  73. Routine Description:
  74. Free an unspecified object owned by the ARP module.
  75. Arguments:
  76. pHdr - Object to be freed.
  77. --*/
  78. {
  79. ARP_FREE(pObj);
  80. }
  81. VOID
  82. arpAdapterDelete (
  83. PRM_OBJECT_HEADER pObj,
  84. PRM_STACK_RECORD psr
  85. )
  86. /*++
  87. Routine Description:
  88. Free an object of type ARP1394_ADAPTER.
  89. Arguments:
  90. pHdr - Actually a pointer to the ARP1394_ADAPTER to be deleted.
  91. --*/
  92. {
  93. ARP1394_ADAPTER * pA = (ARP1394_ADAPTER *) pObj;
  94. if (pA->bind.DeviceName.Buffer != NULL)
  95. {
  96. ARP_FREE(pA->bind.DeviceName.Buffer);
  97. }
  98. if (pA->bind.ConfigName.Buffer != NULL)
  99. {
  100. ARP_FREE(pA->bind.ConfigName.Buffer);
  101. }
  102. if (pA->bind.IpConfigString.Buffer != NULL)
  103. {
  104. ARP_FREE(pA->bind.IpConfigString.Buffer);
  105. }
  106. ARP_FREE(pA);
  107. }
  108. NDIS_STATUS
  109. arpAllocateTask(
  110. IN PRM_OBJECT_HEADER pParentObject,
  111. IN PFN_RM_TASK_HANDLER pfnHandler,
  112. IN UINT Timeout,
  113. IN const char * szDescription, OPTIONAL
  114. OUT PRM_TASK *ppTask,
  115. IN PRM_STACK_RECORD pSR
  116. )
  117. /*++
  118. Routine Description:
  119. Allocates and initializes a task of subtype ARP1394_TASK.
  120. Arguments:
  121. pParentObject - Object that is to be the parent of the allocated task.
  122. pfnHandler - The task handler for the task.
  123. Timeout - Unused.
  124. szDescription - Text describing this task.
  125. ppTask - Place to store pointer to the new task.
  126. Return Value:
  127. NDIS_STATUS_SUCCESS if we could allocate and initialize the task.
  128. NDIS_STATUS_RESOURCES otherwise
  129. --*/
  130. {
  131. ARP1394_TASK *pATask;
  132. NDIS_STATUS Status;
  133. BOOLEAN fBackupTask = FALSE;
  134. ARP_ALLOCSTRUCT(pATask, MTAG_TASK); // TODO use lookaside lists.
  135. if (pATask == NULL)
  136. {
  137. pATask = arpGetBackupTask(&ArpGlobals);
  138. fBackupTask = TRUE;
  139. }
  140. *ppTask = NULL;
  141. if (pATask != NULL)
  142. {
  143. ARP_ZEROSTRUCT(pATask);
  144. RmInitializeTask(
  145. &(pATask->TskHdr),
  146. pParentObject,
  147. pfnHandler,
  148. &ArpTasks_StaticInfo,
  149. szDescription,
  150. Timeout,
  151. pSR
  152. );
  153. *ppTask = &(pATask->TskHdr);
  154. Status = NDIS_STATUS_SUCCESS;
  155. if (fBackupTask == TRUE)
  156. {
  157. MARK_TASK_AS_BACKUP(&pATask->TskHdr);
  158. }
  159. }
  160. else
  161. {
  162. Status = NDIS_STATUS_RESOURCES;
  163. }
  164. return Status;
  165. }
  166. NDIS_STATUS
  167. arpCopyUnicodeString(
  168. OUT PNDIS_STRING pDest,
  169. IN PNDIS_STRING pSrc,
  170. BOOLEAN fUpCase
  171. )
  172. /*++
  173. Routine Description:
  174. Copy the contents of unicode string pSrc into pDest.
  175. pDest->Buffer is allocated using NdisAllocateMemoryWithTag; Caller is
  176. responsible for freeing it.
  177. EXTRA EXTRA EXTRA:
  178. This make sure the destination is NULL terminated.
  179. IPAddInterface expects the Unicode string passed in to be
  180. NULL terminated.
  181. NOTE: fUpCase is ignored.
  182. Return Value:
  183. NDIS_STATUS_SUCCESS on success;
  184. NDIS failure status on failure.
  185. --*/
  186. {
  187. USHORT Length = pSrc->Length;
  188. PWCHAR pwStr;
  189. NdisAllocateMemoryWithTag(&pwStr, Length+sizeof(WCHAR), MTAG_STRING);
  190. ARP_ZEROSTRUCT(pDest);
  191. if (pwStr == NULL)
  192. {
  193. return NDIS_STATUS_RESOURCES;
  194. }
  195. else
  196. {
  197. pDest->Length = Length;
  198. pDest->MaximumLength = Length+sizeof(WCHAR);
  199. pDest->Buffer = pwStr;
  200. // We-- ignore the copy flag.
  201. // For some reason, we're not in passive, and moreover
  202. // NdisUpcaseUnicode doesn't work.
  203. //
  204. if (0 && fUpCase)
  205. {
  206. #if !MILLEN
  207. ASSERT_PASSIVE();
  208. NdisUpcaseUnicodeString(pDest, pSrc);
  209. #endif // !MILLEN
  210. }
  211. else
  212. {
  213. NdisMoveMemory(pwStr, pSrc->Buffer, Length);
  214. if (Length & 0x1)
  215. {
  216. ((PUCHAR)pwStr)[Length] = 0;
  217. }
  218. else
  219. {
  220. pwStr[Length/sizeof(*pwStr)] = 0;
  221. }
  222. }
  223. return NDIS_STATUS_SUCCESS;
  224. }
  225. }
  226. VOID
  227. arpSetPrimaryIfTask(
  228. PARP1394_INTERFACE pIF, // LOCKIN LOCKOUT
  229. PRM_TASK pTask,
  230. ULONG PrimaryState,
  231. PRM_STACK_RECORD pSR
  232. )
  233. {
  234. ENTER("arpSetPrimaryIfTask", 0x535f8cd4)
  235. RM_ASSERT_OBJLOCKED(&pIF->Hdr, pSR);
  236. ASSERT(pIF->pPrimaryTask==NULL);
  237. #if DBG
  238. // Veriy that this is a valid primary task. Also verify that PrimaryState
  239. // is a valid primary state.
  240. //
  241. {
  242. PFN_RM_TASK_HANDLER pfn = pTask->pfnHandler;
  243. ASSERT(
  244. ((pfn == arpTaskInitInterface) && (PrimaryState == ARPIF_PS_INITING))
  245. || ((pfn == arpTaskDeinitInterface) && (PrimaryState == ARPIF_PS_DEINITING))
  246. || ((pfn == arpTaskReinitInterface) && (PrimaryState == ARPIF_PS_REINITING))
  247. || ((pfn == arpTaskLowPower) && (PrimaryState == ARPIF_PS_LOW_POWER))
  248. );
  249. }
  250. #endif // DBG
  251. //
  252. // Although it's tempting to put pTask as entity1 and pRask->Hdr.szDescption as
  253. // entity2 below, we specify NULL for both so that we can be sure that ONLY one
  254. // primary task can be active at any one time.
  255. //
  256. DBG_ADDASSOC(
  257. &pIF->Hdr,
  258. NULL, // Entity1
  259. NULL, // Entity2
  260. ARPASSOC_PRIMARY_IF_TASK,
  261. " Primary task\n",
  262. pSR
  263. );
  264. pIF->pPrimaryTask = pTask;
  265. SET_IF_PRIMARY_STATE(pIF, PrimaryState);
  266. EXIT()
  267. }
  268. VOID
  269. arpClearPrimaryIfTask(
  270. PARP1394_INTERFACE pIF, // LOCKIN LOCKOUT
  271. PRM_TASK pTask,
  272. ULONG PrimaryState,
  273. PRM_STACK_RECORD pSR
  274. )
  275. {
  276. ENTER("arpClearPrimaryIfTask", 0x10ebb0c3)
  277. RM_ASSERT_OBJLOCKED(&pIF->Hdr, pSR);
  278. ASSERT(pIF->pPrimaryTask==pTask);
  279. // Veriy that PrimaryState is a valid primary state.
  280. //
  281. ASSERT(
  282. (PrimaryState == ARPIF_PS_INITED)
  283. || (PrimaryState == ARPIF_PS_FAILEDINIT)
  284. || (PrimaryState == ARPIF_PS_DEINITED)
  285. );
  286. // Delete the association added when setting the primary IF task
  287. //
  288. DBG_DELASSOC(
  289. &pIF->Hdr,
  290. NULL,
  291. NULL,
  292. ARPASSOC_PRIMARY_IF_TASK,
  293. pSR
  294. );
  295. pIF->pPrimaryTask = NULL;
  296. SET_IF_PRIMARY_STATE(pIF, PrimaryState);
  297. EXIT()
  298. }
  299. VOID
  300. arpSetSecondaryIfTask(
  301. PARP1394_INTERFACE pIF, // LOCKIN LOCKOUT
  302. PRM_TASK pTask,
  303. ULONG SecondaryState,
  304. PRM_STACK_RECORD pSR
  305. )
  306. {
  307. ENTER("arpSetSecondaryIfTask", 0xf7e925d1)
  308. RM_ASSERT_OBJLOCKED(&pIF->Hdr, pSR);
  309. if (pIF->pActDeactTask != NULL)
  310. {
  311. ASSERT(FALSE);
  312. return; // EARLY RETURN
  313. }
  314. #if DBG
  315. // Veriy that this is a valid act/deact task. Also verify that SecondaryState
  316. // is a valid state.
  317. //
  318. {
  319. PFN_RM_TASK_HANDLER pfn = pTask->pfnHandler;
  320. ASSERT(
  321. ((pfn == arpTaskActivateInterface) && (SecondaryState == ARPIF_AS_ACTIVATING))
  322. || ((pfn == arpTaskDeactivateInterface) && (SecondaryState == ARPIF_AS_DEACTIVATING))
  323. );
  324. }
  325. #endif // DBG
  326. //
  327. // Although it's tempting to put pTask as entity1 and pRask->Hdr.szDescption as
  328. // entity2 below, we specify NULL for both so that we can be sure that ONLY one
  329. // primary task can be active at any one time.
  330. //
  331. DBG_ADDASSOC(
  332. &pIF->Hdr,
  333. NULL, // Entity1
  334. NULL, // Entity2
  335. ARPASSOC_ACTDEACT_IF_TASK,
  336. " ActDeact task\n",
  337. pSR
  338. );
  339. pIF->pActDeactTask = pTask;
  340. SET_IF_ACTIVE_STATE(pIF, SecondaryState);
  341. EXIT()
  342. }
  343. VOID
  344. arpClearSecondaryIfTask(
  345. PARP1394_INTERFACE pIF, // LOCKIN LOCKOUT
  346. PRM_TASK pTask,
  347. ULONG SecondaryState,
  348. PRM_STACK_RECORD pSR
  349. )
  350. {
  351. ENTER("arpClearSecondaryIfTask", 0x2068f420)
  352. RM_ASSERT_OBJLOCKED(&pIF->Hdr, pSR);
  353. if (pIF->pActDeactTask != pTask)
  354. {
  355. ASSERT(FALSE);
  356. return; // EARLY RETURN
  357. }
  358. // Veriy that SecondaryState is a valid primary state.
  359. //
  360. ASSERT(
  361. (SecondaryState == ARPIF_AS_ACTIVATED)
  362. || (SecondaryState == ARPIF_AS_FAILEDACTIVATE)
  363. || (SecondaryState == ARPIF_AS_DEACTIVATED)
  364. );
  365. // Delete the association added when setting the primary IF task
  366. //
  367. DBG_DELASSOC(
  368. &pIF->Hdr,
  369. NULL,
  370. NULL,
  371. ARPASSOC_ACTDEACT_IF_TASK,
  372. pSR
  373. );
  374. pIF->pActDeactTask = NULL;
  375. SET_IF_ACTIVE_STATE(pIF, SecondaryState);
  376. EXIT()
  377. }
  378. VOID
  379. arpSetPrimaryAdapterTask(
  380. PARP1394_ADAPTER pAdapter, // LOCKIN LOCKOUT
  381. PRM_TASK pTask,
  382. ULONG PrimaryState,
  383. PRM_STACK_RECORD pSR
  384. )
  385. {
  386. ENTER("arpSetPrimaryAdapterTask", 0x535f8cd4)
  387. RM_ASSERT_OBJLOCKED(&pAdapter->Hdr, pSR);
  388. ASSERT(pAdapter->bind.pPrimaryTask==NULL);
  389. #if DBG
  390. // Veriy that this is a valid primary task. Also verify that PrimaryState
  391. // is a valid primary state.
  392. //
  393. {
  394. PFN_RM_TASK_HANDLER pfn = pTask->pfnHandler;
  395. ASSERT(
  396. ((pfn == arpTaskInitializeAdapter) && (PrimaryState == ARPAD_PS_INITING))
  397. || ((pfn == arpTaskShutdownAdapter) && (PrimaryState == ARPAD_PS_DEINITING))
  398. || (pfn == arpTaskLowPower) || ( pfn == arpTaskOnPower)
  399. );
  400. }
  401. #endif // DBG
  402. //
  403. // Although it's tempting to put pTask as entity1 and pRask->Hdr.szDescption as
  404. // entity2 below, we specify NULL for both so that we can be sure that ONLY one
  405. // primary task can be active at any one time.
  406. //
  407. DBG_ADDASSOC(
  408. &pAdapter->Hdr,
  409. NULL, // Entity1
  410. NULL, // Entity2
  411. ARPASSOC_PRIMARY_AD_TASK,
  412. " Primary task\n",
  413. pSR
  414. );
  415. pAdapter->bind.pPrimaryTask = pTask;
  416. SET_AD_PRIMARY_STATE(pAdapter, PrimaryState);
  417. EXIT()
  418. }
  419. VOID
  420. arpClearPrimaryAdapterTask(
  421. PARP1394_ADAPTER pAdapter, // LOCKIN LOCKOUT
  422. PRM_TASK pTask,
  423. ULONG PrimaryState,
  424. PRM_STACK_RECORD pSR
  425. )
  426. {
  427. ENTER("arpClearPrimaryAdapterTask", 0x9062b2ab)
  428. RM_ASSERT_OBJLOCKED(&pAdapter->Hdr, pSR);
  429. ASSERT(pAdapter->bind.pPrimaryTask==pTask);
  430. // Veriy that PrimaryState is a valid primary state.
  431. //
  432. ASSERT(
  433. (PrimaryState == ARPAD_PS_INITED)
  434. || (PrimaryState == ARPAD_PS_FAILEDINIT)
  435. || (PrimaryState == ARPAD_PS_DEINITED)
  436. );
  437. // Delete the association added when setting the primary IF task
  438. //
  439. DBG_DELASSOC(
  440. &pAdapter->Hdr,
  441. NULL,
  442. NULL,
  443. ARPASSOC_PRIMARY_AD_TASK,
  444. pSR
  445. );
  446. pAdapter->bind.pPrimaryTask = NULL;
  447. SET_AD_PRIMARY_STATE(pAdapter, PrimaryState);
  448. EXIT()
  449. }
  450. VOID
  451. arpSetSecondaryAdapterTask(
  452. PARP1394_ADAPTER pAdapter, // LOCKIN LOCKOUT
  453. PRM_TASK pTask,
  454. ULONG SecondaryState,
  455. PRM_STACK_RECORD pSR
  456. )
  457. {
  458. ENTER("arpSetSecondaryAdapterTask", 0x95dae9ac)
  459. RM_ASSERT_OBJLOCKED(&pAdapter->Hdr, pSR);
  460. if (pAdapter->bind.pSecondaryTask != NULL)
  461. {
  462. ASSERT(FALSE);
  463. return; // EARLY RETURN
  464. }
  465. #if DBG
  466. // Veriy that this is a valid act/deact task. Also verify that SecondaryState
  467. // is a valid state.
  468. //
  469. {
  470. PFN_RM_TASK_HANDLER pfn = pTask->pfnHandler;
  471. ASSERT(
  472. ((pfn == arpTaskActivateAdapter) && (SecondaryState == ARPAD_AS_ACTIVATING))
  473. || ((pfn == arpTaskDeactivateAdapter) && (SecondaryState == ARPAD_AS_DEACTIVATING))
  474. );
  475. }
  476. #endif // DBG
  477. //
  478. // Although it's tempting to put pTask as entity1 and pRask->Hdr.szDescption as
  479. // entity2 below, we specify NULL for both so that we can be sure that ONLY one
  480. // primary task can be active at any one time.
  481. //
  482. DBG_ADDASSOC(
  483. &pAdapter->Hdr,
  484. NULL, // Entity1
  485. NULL, // Entity2
  486. ARPASSOC_ACTDEACT_AD_TASK,
  487. " Secondary task\n",
  488. pSR
  489. );
  490. pAdapter->bind.pSecondaryTask = pTask;
  491. SET_AD_ACTIVE_STATE(pAdapter, SecondaryState);
  492. EXIT()
  493. }
  494. VOID
  495. arpClearSecondaryAdapterTask(
  496. PARP1394_ADAPTER pAdapter, // LOCKIN LOCKOUT
  497. PRM_TASK pTask,
  498. ULONG SecondaryState,
  499. PRM_STACK_RECORD pSR
  500. )
  501. {
  502. ENTER("arpClearSecondaryAdapterTask", 0xc876742b)
  503. RM_ASSERT_OBJLOCKED(&pAdapter->Hdr, pSR);
  504. if (pAdapter->bind.pSecondaryTask != pTask)
  505. {
  506. ASSERT(FALSE);
  507. return; // EARLY RETURN
  508. }
  509. // Veriy that SecondaryState is a valid primary state.
  510. //
  511. ASSERT(
  512. (SecondaryState == ARPAD_AS_ACTIVATED)
  513. || (SecondaryState == ARPAD_AS_FAILEDACTIVATE)
  514. || (SecondaryState == ARPAD_AS_DEACTIVATED)
  515. );
  516. // Delete the association added when setting the primary IF task
  517. //
  518. DBG_DELASSOC(
  519. &pAdapter->Hdr,
  520. NULL,
  521. NULL,
  522. ARPASSOC_ACTDEACT_AD_TASK,
  523. pSR
  524. );
  525. pAdapter->bind.pSecondaryTask = NULL;
  526. SET_AD_ACTIVE_STATE(pAdapter, SecondaryState);
  527. EXIT()
  528. }
  529. #if 0
  530. NDIS_STATUS
  531. arpCopyAnsiStringToUnicodeString(
  532. OUT PNDIS_STRING pDest,
  533. IN PANSI_STRING pSrc
  534. )
  535. /*++
  536. Routine Description:
  537. Converts pSrc into unicode and sets up pDest with the it.
  538. pDest->Buffer is allocated using NdisAllocateMemoryWithTag; Caller is
  539. responsible for freeing it.
  540. Return Value:
  541. NDIS_STATUS_SUCCESS on success;
  542. NDIS_STATUS_RESOURCES on failure.
  543. --*/
  544. {
  545. UINT Size;
  546. PWCHAR pwStr;
  547. Size = sizeof(WCHAR) * pSrc->MaximumLength;
  548. NdisAllocateMemoryWithTag(&pwStr, Size, MTAG_STRING);
  549. ARP_ZEROSTRUCT(pDest);
  550. if (pwStr == NULL)
  551. {
  552. return NDIS_STATUS_RESOURCES;
  553. }
  554. else
  555. {
  556. pDest->MaximumLength = Size;
  557. pDest->Buffer = pwStr;
  558. NdisAnsiStringToUnicodeString(pDest, pSrc);
  559. return NDIS_STATUS_SUCCESS;
  560. }
  561. }
  562. NDIS_STATUS
  563. arpCopyUnicodeStringToAnsiString(
  564. OUT PANSI_STRING pDest,
  565. IN PNDIS_STRING pSrc
  566. )
  567. /*++
  568. Routine Description:
  569. Converts pSrc into ansi and sets up pDest with the it.
  570. pDest->Buffer is allocated using NdisAllocateMemoryWithTag; Caller is
  571. responsible for freeing it.
  572. Return Value:
  573. NDIS_STATUS_SUCCESS on success;
  574. NDIS_STATUS_RESOURCES on failure.
  575. --*/
  576. {
  577. UINT Size;
  578. PCHAR pStr;
  579. Size = pSrc->MaximumLength/sizeof(WCHAR) + sizeof(WCHAR);
  580. NdisAllocateMemoryWithTag(&pStr, Size, MTAG_STRING);
  581. ARP_ZEROSTRUCT(pDest);
  582. if (pStr == NULL)
  583. {
  584. return NDIS_STATUS_RESOURCES;
  585. }
  586. else
  587. {
  588. pDest->Buffer = pStr;
  589. NdisUnicodeStringToAnsiString(pDest, pSrc);
  590. pStr[pDest->Length] = 0;
  591. pDest->MaximumLength = Size; // Must be done AFTER call to
  592. // NdisUnicodeStringToAnsiString, which
  593. // sets MaximumLength to Length;
  594. return NDIS_STATUS_SUCCESS;
  595. }
  596. }
  597. #endif // 0
  598. UINT
  599. arpGetSystemTime(VOID)
  600. /*++
  601. Returns system time in seconds.
  602. Since it's in seconds, we won't overflow unless the system has been up for over
  603. a 100 years :-)
  604. --*/
  605. {
  606. LARGE_INTEGER Time;
  607. NdisGetCurrentSystemTime(&Time);
  608. Time.QuadPart /= 10000000; //10-nanoseconds to seconds.
  609. return Time.LowPart;
  610. }
  611. #if ARP_DO_TIMESTAMPS
  612. void
  613. arpTimeStamp(
  614. char *szFormatString,
  615. UINT Val
  616. )
  617. {
  618. UINT Minutes;
  619. UINT Seconds;
  620. UINT Milliseconds;
  621. LARGE_INTEGER Time;
  622. NdisGetCurrentSystemTime(&Time);
  623. Time.QuadPart /= 10000; //10-nanoseconds to milliseconds.
  624. Milliseconds = Time.LowPart; // don't care about highpart.
  625. Seconds = Milliseconds/1000;
  626. Milliseconds %= 1000;
  627. Minutes = Seconds/60;
  628. Seconds %= 60;
  629. DbgPrint( szFormatString, Minutes, Seconds, Milliseconds, Val);
  630. }
  631. #endif // ARP_DO_TIMESTAMPS