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.

2744 lines
76 KiB

  1. /*++
  2. Copyright (c) 1997 Microsoft Corporation
  3. Module Name:
  4. scsiscan.c
  5. Abstract:
  6. The scsi scanner class driver translates IRPs to SRBs with embedded CDBs
  7. and sends them to its devices through the port driver.
  8. Author:
  9. Ray Patrick (raypat)
  10. Environment:
  11. kernel mode only
  12. Notes:
  13. Revision History:
  14. --*/
  15. //
  16. // Include
  17. //
  18. #define INITGUID
  19. #include <stdio.h>
  20. #include "stddef.h"
  21. #include "wdm.h"
  22. #include "scsi.h"
  23. #include "ntddstor.h"
  24. #include "ntddscsi.h"
  25. #include "scsiscan.h"
  26. #include "private.h"
  27. #include "debug.h"
  28. #include <initguid.h>
  29. #include <devguid.h>
  30. #include <wiaintfc.h>
  31. #ifdef ALLOC_PRAGMA
  32. #pragma alloc_text(PAGE, DriverEntry)
  33. #pragma alloc_text(PAGE, SSPnp)
  34. #pragma alloc_text(PAGE, SSPnpAddDevice)
  35. #pragma alloc_text(PAGE, SSOpen)
  36. #pragma alloc_text(PAGE, SSClose)
  37. #pragma alloc_text(PAGE, SSReadWrite)
  38. #pragma alloc_text(PAGE, SSDeviceControl)
  39. #pragma alloc_text(PAGE, SSAdjustTransferSize)
  40. #pragma alloc_text(PAGE, SSBuildTransferContext)
  41. #pragma alloc_text(PAGE, SSCreateSymbolicLink)
  42. #pragma alloc_text(PAGE, SSDestroySymbolicLink)
  43. #pragma alloc_text(PAGE, SSUnload)
  44. #endif
  45. DEFINE_GUID(GUID_STI_DEVICE, 0xF6CBF4C0L, 0xCC61, 0x11D0, 0x84, 0xE5, 0x00, 0xA0, 0xC9, 0x27, 0x65, 0x27);
  46. //
  47. // Globals
  48. //
  49. ULONG NextDeviceInstance = 0;
  50. #if DBG
  51. ULONG SCSISCAN_DebugTraceLevel = MAX_TRACE;
  52. #endif
  53. #define DBG_DEVIOCTL 1
  54. #ifdef _WIN64
  55. BOOLEAN
  56. IoIs32bitProcess(
  57. IN PIRP Irp
  58. );
  59. #endif // _WIN64
  60. //
  61. // Function
  62. //
  63. NTSTATUS
  64. DriverEntry(
  65. IN PDRIVER_OBJECT DriverObject,
  66. IN PUNICODE_STRING RegistryPath
  67. )
  68. /*++
  69. Routine Description:
  70. This routine initializes the scanner class driver. The driver
  71. opens the port driver by name and then receives configuration
  72. information used to attach to the scanner devices.
  73. Arguments:
  74. DriverObject
  75. Return Value:
  76. NT Status
  77. --*/
  78. {
  79. PAGED_CODE();
  80. DebugTrace(TRACE_PROC_ENTER,("DriverEntry: Enter...\n"));
  81. MyDebugInit(RegistryPath);
  82. //
  83. // Set up the device driver entry points.
  84. //
  85. DriverObject->MajorFunction[IRP_MJ_READ] = SSReadWrite;
  86. DriverObject->MajorFunction[IRP_MJ_WRITE] = SSReadWrite;
  87. DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = SSDeviceControl;
  88. DriverObject->MajorFunction[IRP_MJ_CREATE] = SSOpen;
  89. DriverObject->MajorFunction[IRP_MJ_CLOSE] = SSClose;
  90. DriverObject->MajorFunction[IRP_MJ_SYSTEM_CONTROL] = SSPnp;
  91. DriverObject->MajorFunction[IRP_MJ_PNP] = SSPnp;
  92. DriverObject->MajorFunction[IRP_MJ_POWER] = SSPower;
  93. DriverObject->DriverUnload = SSUnload;
  94. DriverObject->DriverExtension->AddDevice = SSPnpAddDevice;
  95. DebugTrace(TRACE_PROC_LEAVE,("DriverEntry: Leaving... Status=STATUS_SUCCESS\n"));
  96. return STATUS_SUCCESS;
  97. } // end DriverEntry
  98. NTSTATUS
  99. SSPnpAddDevice(
  100. IN PDRIVER_OBJECT pDriverObject,
  101. IN PDEVICE_OBJECT pPhysicalDeviceObject
  102. )
  103. /*++
  104. Routine Description:
  105. This routine is called to create a new instance of the device.
  106. Arguments:
  107. pDriverObject - pointer to the driver object for this instance of SS
  108. pPhysicalDeviceObject - pointer to the device object that represents the scanner
  109. on the scsi bus.
  110. Return Value:
  111. STATUS_SUCCESS if successful,
  112. STATUS_UNSUCCESSFUL otherwise
  113. --*/
  114. {
  115. UCHAR aName[64];
  116. ANSI_STRING ansiName;
  117. UNICODE_STRING uName;
  118. PDEVICE_OBJECT pDeviceObject = NULL;
  119. NTSTATUS Status;
  120. PSCSISCAN_DEVICE_EXTENSION pde;
  121. PAGED_CODE();
  122. DebugTrace(TRACE_PROC_ENTER,("SSPnpAddDevice: Enter...\n"));
  123. //
  124. // Create the Functional Device Object (FDO) for this device.
  125. //
  126. sprintf(aName,"\\Device\\Scanner%d",NextDeviceInstance);
  127. RtlInitAnsiString(&ansiName, aName);
  128. DebugTrace(TRACE_STATUS,("SSPnpAddDevice: Create device object %s\n", aName));
  129. RtlAnsiStringToUnicodeString(&uName, &ansiName, TRUE);
  130. //
  131. // Create device object for this scanner.
  132. //
  133. Status = IoCreateDevice(pDriverObject,
  134. sizeof(SCSISCAN_DEVICE_EXTENSION),
  135. &uName,
  136. FILE_DEVICE_SCANNER,
  137. 0,
  138. FALSE,
  139. &pDeviceObject);
  140. RtlFreeUnicodeString(&uName);
  141. if (!NT_SUCCESS(Status)) {
  142. DebugTrace(TRACE_ERROR,("SSPnpAddDevice: ERROR!! Can't create device object\n"));
  143. DEBUG_BREAKPOINT();
  144. return Status;
  145. }
  146. //
  147. // Indicate that IRPs should include MDLs and it's pawer pagable.
  148. //
  149. pDeviceObject->Flags |= DO_DIRECT_IO;
  150. pDeviceObject->Flags |= DO_POWER_PAGABLE;
  151. //
  152. // Initialize Device Extention
  153. //
  154. pde = (PSCSISCAN_DEVICE_EXTENSION)(pDeviceObject -> DeviceExtension);
  155. RtlZeroMemory(pde, sizeof(SCSISCAN_DEVICE_EXTENSION));
  156. //
  157. // Attach our new FDO to the PDO (Physical Device Object).
  158. //
  159. pde -> pStackDeviceObject = IoAttachDeviceToDeviceStack(pDeviceObject,
  160. pPhysicalDeviceObject);
  161. if (NULL == pde -> pStackDeviceObject) {
  162. DebugTrace(MIN_TRACE,("Cannot attach FDO to PDO.\n"));
  163. DEBUG_BREAKPOINT();
  164. IoDeleteDevice( pDeviceObject );
  165. return STATUS_NOT_SUPPORTED;
  166. }
  167. //
  168. // Remember the PDO in our device extension.
  169. //
  170. pde -> pPhysicalDeviceObject = pPhysicalDeviceObject;
  171. //
  172. // Remember the DeviceInstance number.
  173. //
  174. pde -> DeviceInstance = NextDeviceInstance;
  175. //
  176. // Reset SRB error status
  177. //
  178. pde->LastSrbError = 0L;
  179. //
  180. // Disable synchronous transfer for scanner requests.
  181. // Disable QueueFreeze in case of any error.
  182. //
  183. pde -> SrbFlags = SRB_FLAGS_DISABLE_SYNCH_TRANSFER | SRB_FLAGS_NO_QUEUE_FREEZE ;
  184. //
  185. // Set timeout value in seconds.
  186. //
  187. pde -> TimeOutValue = SCSISCAN_TIMEOUT;
  188. //
  189. // Handle exporting interface
  190. //
  191. Status = ScsiScanHandleInterface(
  192. pPhysicalDeviceObject,
  193. &pde->InterfaceNameString,
  194. TRUE
  195. );
  196. //
  197. // Each time AddDevice gets called, we advance the global DeviceInstance variable.
  198. //
  199. NextDeviceInstance++;
  200. //
  201. // Finishing initialize.
  202. //
  203. pDeviceObject -> Flags &= ~DO_DEVICE_INITIALIZING;
  204. DebugTrace(TRACE_PROC_LEAVE,("SSPnpAddDevice: Leaving... Status=STATUS_SUCCESS\n"));
  205. return STATUS_SUCCESS;
  206. } // end SSPnpAddDevice()
  207. NTSTATUS SSPnp (
  208. IN PDEVICE_OBJECT pDeviceObject,
  209. IN PIRP pIrp
  210. )
  211. /*++
  212. Routine Description:
  213. This routine handles all PNP irps.
  214. Arguments:
  215. pDevciceObject - represents a scsi scanner device
  216. pIrp - PNP irp
  217. Return Value:
  218. STATUS_SUCCESS if successful,
  219. STATUS_UNSUCCESSFUL otherwise
  220. --*/
  221. {
  222. NTSTATUS Status;
  223. PSCSISCAN_DEVICE_EXTENSION pde;
  224. PIO_STACK_LOCATION pIrpStack;
  225. STORAGE_PROPERTY_ID PropertyId;
  226. KEVENT event;
  227. PDEVICE_CAPABILITIES pCaps;
  228. PAGED_CODE();
  229. DebugTrace(TRACE_PROC_ENTER,("SSPnp: Enter...\n"));
  230. pde = (PSCSISCAN_DEVICE_EXTENSION)pDeviceObject -> DeviceExtension;
  231. pIrpStack = IoGetCurrentIrpStackLocation( pIrp );
  232. Status = STATUS_SUCCESS;
  233. switch (pIrpStack -> MajorFunction) {
  234. case IRP_MJ_SYSTEM_CONTROL:
  235. DebugTrace(TRACE_STATUS,("SSPnp: IRP_MJ_SYSTEM_CONTROL\n"));
  236. //
  237. // Just passing down IRP to the next layer.
  238. //
  239. IoCopyCurrentIrpStackLocationToNext( pIrp );
  240. Status = IoCallDriver(pde -> pStackDeviceObject, pIrp);
  241. return Status;
  242. break;
  243. case IRP_MJ_PNP:
  244. DebugTrace(TRACE_STATUS,("SSPnp: IRP_MJ_PNP\n"));
  245. switch (pIrpStack->MinorFunction) {
  246. case IRP_MN_QUERY_CAPABILITIES:
  247. DebugTrace(TRACE_STATUS,("SSPnp: IRP_MN_QUERY_CAPABILITIES\n"));
  248. pCaps = pIrpStack -> Parameters.DeviceCapabilities.Capabilities;
  249. //
  250. // fill in the structure with non-controversial values
  251. //
  252. pCaps -> D1Latency = 10;
  253. pCaps -> D2Latency = 10;
  254. pCaps -> D3Latency = 10;
  255. //
  256. // Set SurpriseRemoval OK for SBP2 devices.
  257. //
  258. pCaps->SurpriseRemovalOK = TRUE;
  259. pCaps->Removable = TRUE;
  260. //
  261. // Call down synchronously.
  262. //
  263. pIrp -> IoStatus.Status = STATUS_SUCCESS;
  264. Status = SSCallNextDriverSynch(pde, pIrp);
  265. if(!NT_SUCCESS(Status)){
  266. DebugTrace(TRACE_ERROR,("SSPnp: ERROR!! Call down failed\n Status=0x%x", Status));
  267. }
  268. //
  269. // Complete IRP.
  270. //
  271. IoCompleteRequest( pIrp, IO_NO_INCREMENT );
  272. return Status;
  273. case IRP_MN_START_DEVICE:
  274. DebugTrace(TRACE_STATUS,("SSPnp: IRP_MN_START_DEVICE\n"));
  275. //
  276. // Initialize PendingIoEvent. Set the number of pending i/o requests for this device to 1.
  277. // When this number falls to zero, it is okay to remove, or stop the device.
  278. //
  279. pde -> PendingIoCount = 0;
  280. KeInitializeEvent(&pde -> PendingIoEvent, NotificationEvent, FALSE);
  281. SSIncrementIoCount(pDeviceObject);
  282. //
  283. // First, let the port driver start the device. Simply passing down IRP.
  284. //
  285. Status = SSCallNextDriverSynch(pde, pIrp);
  286. if(!NT_SUCCESS(Status)){
  287. DebugTrace(TRACE_ERROR,("SSPnp: ERROR!! Call down failed\n Status=0x%x", Status));
  288. break;
  289. }
  290. //
  291. // The port driver has started the device. It is time for
  292. // us to do some initialization and create symbolic links
  293. // for the device.
  294. //
  295. //
  296. // Call port driver to get adapter capabilities.
  297. //
  298. PropertyId = StorageAdapterProperty;
  299. pde -> pAdapterDescriptor = NULL;
  300. Status = ClassGetDescriptor(pde -> pStackDeviceObject,
  301. &PropertyId,
  302. &(pde -> pAdapterDescriptor));
  303. if(!NT_SUCCESS(Status)) {
  304. DebugTrace(TRACE_ERROR, ("SSPnp: ERROR!! unable to retrieve adapter descriptor.\n"
  305. "[%#08lx]\n", Status));
  306. DEBUG_BREAKPOINT();
  307. if (NULL != pde -> pAdapterDescriptor) {
  308. MyFreePool( pde -> pAdapterDescriptor);
  309. pde -> pAdapterDescriptor = NULL;
  310. }
  311. break;
  312. }
  313. //
  314. // Create the symbolic link for this device.
  315. //
  316. Status = SSCreateSymbolicLink( pde );
  317. if (!NT_SUCCESS(Status)) {
  318. DebugTrace(TRACE_ERROR, ("SSPnp: ERROR!! Can't create symbolic link.\n"));
  319. DEBUG_BREAKPOINT();
  320. if (NULL != pde -> pAdapterDescriptor) {
  321. MyFreePool( pde -> pAdapterDescriptor);
  322. pde -> pAdapterDescriptor = NULL;
  323. }
  324. break;
  325. }
  326. //
  327. // Indicate device is now ready.
  328. //
  329. pde -> DeviceLock = 0;
  330. pde -> OpenInstanceCount = 0;
  331. pde -> AcceptingRequests = TRUE;
  332. pIrp -> IoStatus.Status = Status;
  333. pIrp -> IoStatus.Information = 0;
  334. pde -> LastSrbError = 0L;
  335. IoCompleteRequest( pIrp, IO_NO_INCREMENT );
  336. return Status;
  337. break;
  338. case IRP_MN_REMOVE_DEVICE:
  339. DebugTrace(TRACE_STATUS,("SSPnp: IRP_MN_REMOVE_DEVICE\n"));
  340. //
  341. // Forward remove message to lower driver.
  342. //
  343. IoCopyCurrentIrpStackLocationToNext(pIrp);
  344. pIrp -> IoStatus.Status = STATUS_SUCCESS;
  345. Status = SSCallNextDriverSynch(pde, pIrp);
  346. if(!NT_SUCCESS(Status)){
  347. DebugTrace(TRACE_ERROR,("SSPnp: ERROR!! Call down failed\n Status=0x%x", Status));
  348. }
  349. if (pde -> AcceptingRequests) {
  350. pde -> AcceptingRequests = FALSE;
  351. SSDestroySymbolicLink( pde );
  352. }
  353. ScsiScanHandleInterface(pde-> pPhysicalDeviceObject,
  354. &pde->InterfaceNameString,
  355. FALSE);
  356. #ifndef _CHICAGO_
  357. if (pde->InterfaceNameString.Buffer != NULL) {
  358. IoSetDeviceInterfaceState(&pde->InterfaceNameString,FALSE);
  359. }
  360. #endif // !_CHICAGO_
  361. //
  362. // wait for any io requests pending in our driver to
  363. // complete before finishing the remove
  364. //
  365. SSDecrementIoCount(pDeviceObject);
  366. KeWaitForSingleObject(&pde -> PendingIoEvent, Suspended, KernelMode,
  367. FALSE,NULL);
  368. if (pde -> pAdapterDescriptor) {
  369. MyFreePool(pde -> pAdapterDescriptor);
  370. pde -> pAdapterDescriptor = NULL;
  371. }
  372. IoDetachDevice(pde -> pStackDeviceObject);
  373. IoDeleteDevice (pDeviceObject);
  374. Status = STATUS_SUCCESS;
  375. pIrp -> IoStatus.Status = Status;
  376. pIrp -> IoStatus.Information = 0;
  377. IoCompleteRequest(pIrp, IO_NO_INCREMENT);
  378. return Status;
  379. break;
  380. case IRP_MN_STOP_DEVICE:
  381. DebugTrace(TRACE_STATUS,("SSPnp: IRP_MN_STOP_DEVICE\n"));
  382. //
  383. // Indicate device is not ready.
  384. //
  385. ASSERT(pde -> AcceptingRequests);
  386. pde -> AcceptingRequests = FALSE;
  387. //
  388. // Remove symbolic link.
  389. //
  390. SSDestroySymbolicLink( pde );
  391. #ifndef _CHICAGO_
  392. if (pde->InterfaceNameString.Buffer != NULL) {
  393. IoSetDeviceInterfaceState(&pde->InterfaceNameString,FALSE);
  394. }
  395. #endif // !_CHICAGO_
  396. //
  397. // Let the port driver stop the device.
  398. //
  399. pIrp -> IoStatus.Status = STATUS_SUCCESS;
  400. Status = SSCallNextDriverSynch(pde, pIrp);
  401. if(!NT_SUCCESS(Status)){
  402. DebugTrace(TRACE_ERROR,("SSPnp: ERROR!! Call down failed\n Status=0x%x", Status));
  403. }
  404. //
  405. // wait for any io requests pending in our driver to
  406. // complete before finishing the remove
  407. //
  408. SSDecrementIoCount(pDeviceObject);
  409. KeWaitForSingleObject(&pde -> PendingIoEvent, Suspended, KernelMode,
  410. FALSE,NULL);
  411. //
  412. // Free Adapter Descriptor
  413. //
  414. if(pde -> pAdapterDescriptor){
  415. MyFreePool(pde -> pAdapterDescriptor);
  416. pde -> pAdapterDescriptor = NULL;
  417. } else {
  418. DebugTrace(TRACE_ERROR,("SSPnp: ERROR!! AdapterDescriptor doesn't exist.\n"));
  419. DEBUG_BREAKPOINT();
  420. }
  421. Status = STATUS_SUCCESS;
  422. pIrp -> IoStatus.Status = Status;
  423. pIrp -> IoStatus.Information = 0;
  424. IoCompleteRequest(pIrp, IO_NO_INCREMENT);
  425. return Status;
  426. break;
  427. case IRP_MN_QUERY_STOP_DEVICE:
  428. DebugTrace(TRACE_STATUS,("SSPnp: IRP_MN_QUERY_STOP_DEVICE\n"));
  429. pIrp->IoStatus.Status = STATUS_SUCCESS;
  430. break;
  431. case IRP_MN_QUERY_REMOVE_DEVICE:
  432. DebugTrace(TRACE_STATUS,("SSPnp: IRP_MN_QUERY_REMOVE_DEVICE\n"));
  433. pIrp->IoStatus.Status = STATUS_SUCCESS;
  434. break;
  435. case IRP_MN_CANCEL_STOP_DEVICE:
  436. DebugTrace(TRACE_STATUS,("SSPnp: IRP_MN_CANCEL_STOP_DEVICE\n"));
  437. pIrp->IoStatus.Status = STATUS_SUCCESS;
  438. break;
  439. case IRP_MN_CANCEL_REMOVE_DEVICE:
  440. DebugTrace(TRACE_STATUS,("SSPnp: IRP_MN_CANCEL_REMOVE_DEVICE\n"));
  441. pIrp->IoStatus.Status = STATUS_SUCCESS;
  442. break;
  443. case IRP_MN_SURPRISE_REMOVAL:
  444. DebugTrace(TRACE_STATUS,("SSPnp: IRP_MN_SURPRISE_REMOVAL\n"));
  445. pIrp->IoStatus.Status = STATUS_SUCCESS;
  446. break;
  447. default:
  448. DebugTrace(TRACE_STATUS,("SSPnp: Minor PNP message received, MinFunction = %x\n",
  449. pIrpStack->MinorFunction));
  450. break;
  451. } /* case MinorFunction, MajorFunction == IRP_MJ_PNP_POWER */
  452. ASSERT(Status == STATUS_SUCCESS);
  453. if (!NT_SUCCESS(Status)) {
  454. pIrp -> IoStatus.Status = Status;
  455. IoCompleteRequest( pIrp, IO_NO_INCREMENT );
  456. DebugTrace(TRACE_PROC_LEAVE,("SSPnp: Leaving(w/ Error)... Status=%x\n", Status));
  457. return Status;
  458. }
  459. //
  460. // Passing down IRP
  461. //
  462. IoCopyCurrentIrpStackLocationToNext(pIrp);
  463. Status = IoCallDriver(pde -> pStackDeviceObject, pIrp);
  464. DebugTrace(TRACE_PROC_LEAVE,("SSPnp: Leaving... Status=%x\n", Status));
  465. return Status;
  466. break; // IRP_MJ_PNP
  467. default:
  468. DebugTrace(TRACE_WARNING,("SSPnp: WARNING!! Not handled Major PNP IOCTL.\n"));
  469. pIrp -> IoStatus.Status = STATUS_INVALID_PARAMETER;
  470. IoCompleteRequest( pIrp, IO_NO_INCREMENT );
  471. DebugTrace(TRACE_PROC_LEAVE,("SSPnp: Leaving... Status=STATUS_INVALID_PARAMETER\n", Status));
  472. return Status;
  473. } /* case MajorFunction */
  474. } // end SSPnp()
  475. NTSTATUS
  476. SSOpen(
  477. IN PDEVICE_OBJECT pDeviceObject,
  478. IN PIRP pIrp
  479. )
  480. /*++
  481. Routine Description:
  482. This routine is called to establish a connection to the device
  483. class driver. It does no more than return STATUS_SUCCESS.
  484. Arguments:
  485. pDeviceObject - Device object for a device.
  486. pIrp - Open request packet
  487. Return Value:
  488. NT Status - STATUS_SUCCESS
  489. --*/
  490. {
  491. NTSTATUS Status;
  492. PSCSISCAN_DEVICE_EXTENSION pde;
  493. PIO_STACK_LOCATION pIrpStack;
  494. PAGED_CODE();
  495. DebugTrace(TRACE_PROC_ENTER,("SSOpen: Enter...\n"));
  496. pde = (PSCSISCAN_DEVICE_EXTENSION)pDeviceObject -> DeviceExtension;
  497. pIrpStack = IoGetCurrentIrpStackLocation( pIrp );
  498. //
  499. // Increment pending IO count
  500. //
  501. SSIncrementIoCount( pDeviceObject );
  502. //
  503. // Initialize IoStatus
  504. //
  505. Status = STATUS_SUCCESS;
  506. pIrp -> IoStatus.Information = 0;
  507. pIrp -> IoStatus.Status = Status;
  508. //
  509. // Save instance-count to the context in file object.
  510. //
  511. InterlockedIncrement(&pde -> OpenInstanceCount);
  512. (ULONG)(UINT_PTR)(pIrpStack -> FileObject -> FsContext) = pde -> OpenInstanceCount;
  513. //
  514. // Check if device is not going away, in which case fail open request.
  515. //
  516. if (pde -> AcceptingRequests == FALSE) {
  517. DebugTrace(TRACE_STATUS,("SSOpen: Device doesn't exist.\n"));
  518. Status = STATUS_DELETE_PENDING;
  519. pIrp -> IoStatus.Information = 0;
  520. pIrp -> IoStatus.Status = Status;
  521. IoCompleteRequest(pIrp, IO_NO_INCREMENT);
  522. SSDecrementIoCount(pDeviceObject);
  523. DebugTrace(TRACE_PROC_LEAVE,("SSOpen: Leaving... Status=STATUS_DELETE_PENDING\n"));
  524. return Status;
  525. }
  526. //
  527. // Decrement pending IO count
  528. //
  529. SSDecrementIoCount(pDeviceObject);
  530. //
  531. // Passing down IRP.
  532. //
  533. IoSkipCurrentIrpStackLocation( pIrp );
  534. Status = IoCallDriver(pde -> pStackDeviceObject, pIrp);
  535. DebugTrace(TRACE_PROC_LEAVE,("SSOpen: Leaving... Status=%x\n", Status));
  536. return Status;
  537. } // end SSOpen()
  538. NTSTATUS
  539. SSClose(
  540. IN PDEVICE_OBJECT pDeviceObject,
  541. IN PIRP pIrp
  542. )
  543. /*++
  544. Routine Description:
  545. Arguments:
  546. pDeviceObject - Device object for a device.
  547. pIrp - Open request packet
  548. Return Value:
  549. NT Status - STATUS_SUCCESS
  550. --*/
  551. {
  552. NTSTATUS Status;
  553. PSCSISCAN_DEVICE_EXTENSION pde;
  554. PIO_STACK_LOCATION pIrpStack;
  555. PAGED_CODE();
  556. DebugTrace(TRACE_PROC_ENTER,("SSClose: Enter...\n"));
  557. pde = (PSCSISCAN_DEVICE_EXTENSION)pDeviceObject -> DeviceExtension;
  558. Status = STATUS_SUCCESS;
  559. //
  560. // Increment pending IO count
  561. //
  562. SSIncrementIoCount( pDeviceObject );
  563. //
  564. // Clear instance-count in context
  565. //
  566. pIrpStack = IoGetCurrentIrpStackLocation( pIrp );
  567. pIrpStack -> FileObject -> FsContext = 0;
  568. //
  569. // Initialize IoStatus
  570. //
  571. pIrp -> IoStatus.Information = 0;
  572. pIrp -> IoStatus.Status = Status;
  573. //
  574. // Decrement pending IO count
  575. //
  576. SSDecrementIoCount(pDeviceObject);
  577. //
  578. // Passing down IRP
  579. //
  580. IoSkipCurrentIrpStackLocation( pIrp );
  581. Status = IoCallDriver(pde -> pStackDeviceObject, pIrp);
  582. DebugTrace(TRACE_PROC_LEAVE,("SSClose: Leaving... Status=%x\n", Status));
  583. return Status;
  584. } // end SSClose()
  585. NTSTATUS
  586. SSDeviceControl(
  587. IN PDEVICE_OBJECT pDeviceObject,
  588. IN PIRP pIrp
  589. )
  590. /*++
  591. Routine Description:
  592. This function allows a user mode client to send CDBs to the device.
  593. Arguments:
  594. pDeviceObject - Device object for a device.
  595. pIrp - Open request packet
  596. Return Value:
  597. NT Status - STATUS_SUCCESS
  598. --*/
  599. {
  600. PIO_STACK_LOCATION pIrpStack;
  601. PIO_STACK_LOCATION pNextIrpStack;
  602. ULONG IoControlCode;
  603. ULONG OldTimeout;
  604. PSCSISCAN_DEVICE_EXTENSION pde;
  605. PTRANSFER_CONTEXT pTransferContext = NULL;
  606. PMDL pMdl = NULL;
  607. NTSTATUS Status;
  608. PVOID Owner;
  609. PULONG pTimeOut;
  610. PCDB pCdb;
  611. PVOID pUserBuffer;
  612. BOOLEAN fLockedSenseBuffer, fLockedSRBStatus;
  613. PAGED_CODE();
  614. DebugTrace(TRACE_PROC_ENTER,("SSDeviceControl: Enter...\n"));
  615. SSIncrementIoCount( pDeviceObject );
  616. pde = (PSCSISCAN_DEVICE_EXTENSION)pDeviceObject -> DeviceExtension;
  617. //
  618. // Validate state of the device
  619. //
  620. if (pde -> AcceptingRequests == FALSE) {
  621. DebugTrace(TRACE_WARNING,("SSDeviceControl: WARNING!! Device's been stopped/removed!\n"));
  622. Status = STATUS_DELETE_PENDING;
  623. pIrp -> IoStatus.Information = 0;
  624. goto SSDeviceControl_Complete;
  625. }
  626. //
  627. // Indicate that MDLs are not locked yet
  628. //
  629. fLockedSenseBuffer = fLockedSRBStatus = FALSE;
  630. //
  631. // Get context pointers
  632. //
  633. pIrpStack = IoGetCurrentIrpStackLocation( pIrp );
  634. pNextIrpStack = IoGetNextIrpStackLocation( pIrp );
  635. IoControlCode = pIrpStack->Parameters.DeviceIoControl.IoControlCode;
  636. //
  637. // Get owner of device (0 = locked, >0 if someone has it locked)
  638. //
  639. Owner = InterlockedCompareExchangePointer(&pde -> DeviceLock,
  640. NULL,
  641. NULL);
  642. if (Owner != NULL) {
  643. if (Owner != pIrpStack -> FileObject -> FsContext) {
  644. DebugTrace(TRACE_WARNING,("SSDeviceControl: WARNING!! Device is already locked\n"));
  645. Status = STATUS_DEVICE_BUSY;
  646. pIrp -> IoStatus.Information = 0;
  647. goto SSDeviceControl_Complete;
  648. }
  649. }
  650. switch (IoControlCode) {
  651. case IOCTL_SCSISCAN_SET_TIMEOUT:
  652. DebugTrace(TRACE_STATUS,("SSDeviceControl: SCSISCAN_SET_TIMEOUT\n"));
  653. //
  654. // Get pointer of timeout buffer.
  655. //
  656. pTimeOut = pIrp -> AssociatedIrp.SystemBuffer;
  657. //
  658. // Validate size of the input parameter
  659. //
  660. if (pIrpStack->Parameters.DeviceIoControl.InputBufferLength < sizeof(pde -> TimeOutValue) ) {
  661. DebugTrace(TRACE_WARNING,("SSDeviceControl: WARNING!! Buffer too small\n"));
  662. Status = STATUS_INVALID_PARAMETER;
  663. goto SSDeviceControl_Complete;
  664. }
  665. OldTimeout = *pTimeOut;
  666. OldTimeout = InterlockedExchange(&pde -> TimeOutValue, *pTimeOut );
  667. DebugTrace(TRACE_STATUS,("SSDeviceControl: Timeout %d->%d\n",OldTimeout, *pTimeOut));
  668. pIrp -> IoStatus.Information = 0;
  669. //
  670. // If caller wanted to get old timeout value back - give it to him.
  671. // Ideally we should've require nonNULL value for output buffer, but it had not been speced
  672. // and now we can't change compatibility.
  673. //
  674. if (pIrpStack->Parameters.DeviceIoControl.OutputBufferLength >= sizeof(OldTimeout) ) {
  675. *pTimeOut = OldTimeout;
  676. pIrp -> IoStatus.Information = sizeof(OldTimeout) ;
  677. }
  678. Status = STATUS_SUCCESS;
  679. goto SSDeviceControl_Complete;
  680. case IOCTL_SCSISCAN_LOCKDEVICE:
  681. DebugTrace(TRACE_STATUS,("SSDeviceControl: IOCTL_SCSISCAN_LOCKDEVICE\n"));
  682. //
  683. // Lock device
  684. //
  685. Status = STATUS_DEVICE_BUSY;
  686. if (NULL == InterlockedCompareExchangePointer(&pde -> DeviceLock,
  687. pIrpStack -> FileObject -> FsContext,
  688. NULL)) {
  689. Status = STATUS_SUCCESS;
  690. }
  691. goto SSDeviceControl_Complete;
  692. case IOCTL_SCSISCAN_UNLOCKDEVICE:
  693. DebugTrace(TRACE_STATUS,("SSDeviceControl: IOCTL_SCSISCAN_UNLOCKDEVICE\n"));
  694. //
  695. // Unlock device
  696. //
  697. Status = STATUS_DEVICE_BUSY;
  698. if (pIrpStack -> FileObject -> FsContext ==
  699. InterlockedCompareExchangePointer(&pde -> DeviceLock,
  700. NULL,
  701. pIrpStack -> FileObject -> FsContext)) {
  702. Status = STATUS_SUCCESS;
  703. }
  704. goto SSDeviceControl_Complete;
  705. case IOCTL_SCSISCAN_CMD:
  706. {
  707. SCSISCAN_CMD LocalScsiscanCmd;
  708. PSCSISCAN_CMD pCmd;
  709. DebugTrace(TRACE_STATUS,("SSDeviceControl: IOCTL_SCSISCAN_CMD\n"));
  710. //
  711. // Check input buffer size.
  712. //
  713. #ifdef _WIN64
  714. if(IoIs32bitProcess(pIrp)){
  715. PSCSISCAN_CMD_32 pScsiscanCmd32;
  716. if (pIrpStack->Parameters.DeviceIoControl.InputBufferLength < sizeof(SCSISCAN_CMD_32) ) {
  717. DebugTrace(TRACE_WARNING,("SSDeviceControl: WARNING!! Buffer too small\n"));
  718. Status = STATUS_INVALID_PARAMETER;
  719. goto SSDeviceControl_Complete;
  720. }
  721. //
  722. // Copy parameters from 32bit IOCTL buffer.
  723. //
  724. pCmd = &LocalScsiscanCmd;
  725. RtlZeroMemory(pCmd, sizeof(SCSISCAN_CMD));
  726. pScsiscanCmd32 = pIrp -> AssociatedIrp.SystemBuffer;
  727. pCmd -> Size = pScsiscanCmd32 -> Size;
  728. pCmd -> SrbFlags = pScsiscanCmd32 -> SrbFlags;
  729. pCmd -> CdbLength = pScsiscanCmd32 -> CdbLength;
  730. pCmd -> SenseLength = pScsiscanCmd32 -> SenseLength;
  731. pCmd -> TransferLength = pScsiscanCmd32 -> TransferLength;
  732. pCmd -> pSrbStatus = (PUCHAR)pScsiscanCmd32 -> pSrbStatus;
  733. pCmd -> pSenseBuffer = (PUCHAR)pScsiscanCmd32 -> pSenseBuffer;
  734. RtlCopyMemory(pCmd -> Cdb, pScsiscanCmd32 -> Cdb, 16); // 16 = CDB buffer size.
  735. } else { // if(IoIs32bitProcess(pIrp))
  736. #endif // _WIN64
  737. if (pIrpStack->Parameters.DeviceIoControl.InputBufferLength < sizeof(SCSISCAN_CMD) ) {
  738. DebugTrace(TRACE_WARNING,("SSDeviceControl: WARNING!! Buffer too small\n"));
  739. Status = STATUS_INVALID_PARAMETER;
  740. goto SSDeviceControl_Complete;
  741. }
  742. pCmd = pIrp -> AssociatedIrp.SystemBuffer;
  743. #ifdef _WIN64
  744. } // if(IoIs32bitProcess(pIrp))
  745. #endif // _WIN64
  746. //
  747. // Issue SCSI command
  748. //
  749. #if DBG_DEVIOCTL
  750. {
  751. PCDB pCdb;
  752. pCdb = (PCDB)pCmd -> Cdb;
  753. DebugTrace(TRACE_STATUS,("SSDeviceControl: CDB->ControlCode = %d \n",pCdb->CDB6GENERIC.OperationCode));
  754. }
  755. #endif
  756. pTransferContext = SSBuildTransferContext(pde,
  757. pIrp,
  758. pCmd,
  759. pIrpStack -> Parameters.DeviceIoControl.InputBufferLength,
  760. pIrp -> MdlAddress,
  761. TRUE
  762. );
  763. if (NULL == pTransferContext) {
  764. DebugTrace(TRACE_ERROR,("SSDeviceControl: ERROR!! Can't create transfer context!\n"));
  765. DEBUG_BREAKPOINT();
  766. Status = STATUS_INVALID_PARAMETER;
  767. goto SSDeviceControl_Complete;
  768. }
  769. //
  770. // Fill in transfer length in the CDB.
  771. //
  772. if(10 == pCmd -> CdbLength){
  773. //
  774. // Currently Scsiscan only supports flagmentation of 10bytes CDB.
  775. //
  776. SSSetTransferLengthToCdb((PCDB)pCmd -> Cdb, pTransferContext -> TransferLength);
  777. } else if (6 != pCmd -> CdbLength){
  778. //
  779. // If CdbLength is not 6 or 10 and transfer size exceeds adapter limit, SCSISCAN cannot handle it.
  780. //
  781. if(pTransferContext -> TransferLength != pCmd -> TransferLength){
  782. DebugTrace(TRACE_WARNING,("SSDeviceControl: WARNING!! TransferLength (CDB !=6 or 10) exceeds limits!\n"));
  783. Status = STATUS_INVALID_PARAMETER;
  784. goto SSDeviceControl_Complete;
  785. }
  786. }
  787. //
  788. // Create system address for the user's sense buffer (if any).
  789. //
  790. if (pCmd -> SenseLength) {
  791. pTransferContext -> pSenseMdl = MmCreateMdl(NULL,
  792. pCmd -> pSenseBuffer,
  793. pCmd -> SenseLength);
  794. if (NULL == pTransferContext -> pSenseMdl) {
  795. DebugTrace(TRACE_ERROR,("SSDeviceControl: ERROR!! Can't create MDL for sense buffer!\n"));
  796. DEBUG_BREAKPOINT();
  797. Status = STATUS_INSUFFICIENT_RESOURCES;
  798. goto SSDeviceControl_Error_With_Status;
  799. }
  800. //
  801. // Probe and lock the pages associated with the
  802. // caller's buffer for write access , using processor mode of the requestor
  803. // Nb: Probing may cause an exception
  804. //
  805. try{
  806. MmProbeAndLockPages(pTransferContext -> pSenseMdl,
  807. pIrp -> RequestorMode,
  808. IoModifyAccess
  809. );
  810. } except(EXCEPTION_EXECUTE_HANDLER) {
  811. //
  812. // Invalid sense buffer pointer.
  813. //
  814. DebugTrace(TRACE_WARNING,("SSDeviceControl: WARNING!! Sense Buffer validation failed\n"));
  815. Status = GetExceptionCode();
  816. pIrp -> IoStatus.Information = 0;
  817. goto SSDeviceControl_Error_With_Status;
  818. } // except
  819. //
  820. // Indicate we succesfully locked sense buffer
  821. //
  822. fLockedSenseBuffer = TRUE;
  823. //
  824. // Get system address of sense buffer
  825. //
  826. pTransferContext -> pSenseMdl -> MdlFlags |= MDL_MAPPING_CAN_FAIL;
  827. pTransferContext -> pSenseBuffer =
  828. MmGetSystemAddressForMdl(pTransferContext -> pSenseMdl);
  829. if (NULL == pTransferContext -> pSenseBuffer) {
  830. //
  831. // Error with MmGetSystemAddressForMdl
  832. //
  833. DebugTrace(TRACE_ERROR,("SSDeviceControl: ERROR!! Can't get system address for sense buffer!\n"));
  834. DEBUG_BREAKPOINT();
  835. Status = STATUS_INSUFFICIENT_RESOURCES;
  836. goto SSDeviceControl_Error_With_Status;
  837. }
  838. }
  839. //
  840. // Create system address for the user's srb status byte.
  841. //
  842. pMdl = MmCreateMdl(NULL,
  843. pCmd -> pSrbStatus,
  844. sizeof(UCHAR)
  845. );
  846. if (NULL == pMdl) {
  847. DebugTrace(TRACE_ERROR,("SSDeviceControl: ERROR!! Can't create MDL for pSrbStatus!\n"));
  848. DEBUG_BREAKPOINT();
  849. Status = STATUS_INSUFFICIENT_RESOURCES;
  850. goto SSDeviceControl_Error_With_Status;
  851. }
  852. //
  853. // Probe and lock the pages associated with the caller's
  854. // buffer for write access , using processor mode of the requestor
  855. // Nb: Probing may cause an exception
  856. //
  857. try{
  858. MmProbeAndLockPages(pMdl,
  859. pIrp -> RequestorMode,
  860. IoModifyAccess);
  861. } except(EXCEPTION_EXECUTE_HANDLER) {
  862. //
  863. // Invalid SRB status buffer pointer.
  864. //
  865. DebugTrace(TRACE_WARNING,("SSDeviceControl: WARNING!! SRB Status Buffer validation failed\n"));
  866. Status = GetExceptionCode();
  867. pIrp -> IoStatus.Information = 0;
  868. goto SSDeviceControl_Error_With_Status;
  869. } // except
  870. //
  871. // Indicate we successfully locked SRB status
  872. //
  873. fLockedSRBStatus = TRUE;
  874. //
  875. // Replace pSrbStatus with the address gotten from MmGetSystemAddressForMdl.
  876. //
  877. pMdl -> MdlFlags |= MDL_MAPPING_CAN_FAIL;
  878. pCmd -> pSrbStatus = MmGetSystemAddressForMdl(pMdl);
  879. if (NULL == pCmd -> pSrbStatus) {
  880. //
  881. // Error with MmGetSystemAddressForMdl
  882. //
  883. DebugTrace(TRACE_ERROR,("SSDeviceControl: ERROR!! Can't get system address for pSrbStatus!\n"));
  884. DEBUG_BREAKPOINT();
  885. Status = STATUS_INSUFFICIENT_RESOURCES;
  886. goto SSDeviceControl_Error_With_Status;
  887. }
  888. //
  889. // Save Mdl for pSrbStatus
  890. //
  891. pTransferContext -> pSrbStatusMdl = pMdl;
  892. break;
  893. } // case IOCTL_SCSISCAN_CMD:
  894. case IOCTL_SCSISCAN_GET_INFO:
  895. DebugTrace(TRACE_STATUS,("SSDeviceControl: IOCTL_SCSISCAN_GET_INFO\n"));
  896. //
  897. // Get and return SCSI information block for the scanner device
  898. //
  899. if (sizeof(SCSISCAN_INFO) != pIrpStack->Parameters.DeviceIoControl.OutputBufferLength) {
  900. //
  901. // Incorrect output buffer size
  902. //
  903. DebugTrace(TRACE_WARNING,("SSDeviceControl: WARNING!! Output buffer size is wrong!\n"));
  904. Status = STATUS_INVALID_PARAMETER;
  905. goto SSDeviceControl_Error_With_Status;
  906. }
  907. if (sizeof(SCSISCAN_INFO) > MmGetMdlByteCount(pIrp->MdlAddress)) {
  908. //
  909. // buffer size is short
  910. //
  911. DebugTrace(TRACE_WARNING,("SSDeviceControl: WARNING!! Output buffer size is wrong!\n"));
  912. Status = STATUS_INVALID_PARAMETER;
  913. goto SSDeviceControl_Error_With_Status;
  914. }
  915. pIrp->MdlAddress->MdlFlags |= MDL_MAPPING_CAN_FAIL;
  916. pUserBuffer = MmGetSystemAddressForMdl(pIrp->MdlAddress);
  917. if(NULL == pUserBuffer){
  918. DebugTrace(TRACE_ERROR,("SSDeviceControl: ERROR!! MmGetSystemAddressForMdl failed!\n"));
  919. Status = STATUS_INSUFFICIENT_RESOURCES;
  920. goto SSDeviceControl_Complete;
  921. }
  922. Status = ClassGetInfo(pde -> pStackDeviceObject, pUserBuffer);
  923. goto SSDeviceControl_Complete;
  924. default:
  925. //
  926. // Unsupported IOCTL code - pass down.
  927. //
  928. DebugTrace(TRACE_STATUS,("SSDeviceControl: Passing down unsupported IOCTL(0x%x)!\n", IoControlCode));
  929. IoSkipCurrentIrpStackLocation(pIrp);
  930. Status = IoCallDriver(pde -> pStackDeviceObject, pIrp);
  931. return Status;
  932. }
  933. //
  934. // Pass request down and mark as pending
  935. //
  936. IoMarkIrpPending(pIrp);
  937. IoSetCompletionRoutine(pIrp, SSIoctlIoComplete, pTransferContext, TRUE, TRUE, FALSE);
  938. SSSendScannerRequest(pDeviceObject, pIrp, pTransferContext, FALSE);
  939. DebugTrace(TRACE_PROC_LEAVE,("SSDeviceControl: Leaving... Status=STATUS_PENDING\n"));
  940. return STATUS_PENDING;
  941. //
  942. // Cleanup
  943. //
  944. SSDeviceControl_Error_With_Status:
  945. //
  946. // Clean up if something went wrong when allocating resources
  947. //
  948. if (pMdl) {
  949. if (fLockedSRBStatus) {
  950. MmUnlockPages(pMdl);
  951. }
  952. IoFreeMdl(pMdl);
  953. if (pTransferContext) {
  954. pTransferContext -> pSrbStatusMdl = NULL;
  955. }
  956. }
  957. if (pTransferContext) {
  958. if (pTransferContext -> pSenseMdl) {
  959. if ( fLockedSenseBuffer ) {
  960. MmUnlockPages(pTransferContext -> pSenseMdl);
  961. }
  962. IoFreeMdl(pTransferContext -> pSenseMdl);
  963. pTransferContext -> pSenseMdl = NULL;
  964. pTransferContext -> pSenseBuffer = NULL;
  965. }
  966. }
  967. SSDeviceControl_Complete:
  968. //
  969. // Everything seems to be OK - complet I/O request
  970. //
  971. pIrp -> IoStatus.Status = Status;
  972. IoCompleteRequest(pIrp, IO_NO_INCREMENT);
  973. SSDecrementIoCount(pDeviceObject);
  974. DebugTrace(TRACE_PROC_LEAVE,("SSDeviceControl: Leaving... Status=%x\n",Status));
  975. return Status;
  976. } // end SSDeviceControl()
  977. NTSTATUS
  978. SSReadWrite(
  979. IN PDEVICE_OBJECT pDeviceObject,
  980. IN PIRP pIrp
  981. )
  982. /*++
  983. Routine Description:
  984. This is the entry called by the I/O system for scanner IO.
  985. Arguments:
  986. DeviceObject - the system object for the device.
  987. Irp - IRP involved.
  988. Return Value:
  989. NT Status
  990. --*/
  991. {
  992. NTSTATUS Status;
  993. PIO_STACK_LOCATION pIrpStack;
  994. PSCSISCAN_DEVICE_EXTENSION pde;
  995. PTRANSFER_CONTEXT pTransferContext;
  996. PMDL pMdl;
  997. PSCSISCAN_CMD pCmd;
  998. PCDB pCdb;
  999. PVOID Owner;
  1000. PAGED_CODE();
  1001. DebugTrace(TRACE_PROC_ENTER,("SSReadWrite: Enter...\n"));
  1002. pde = (PSCSISCAN_DEVICE_EXTENSION)pDeviceObject -> DeviceExtension;
  1003. pIrpStack = IoGetCurrentIrpStackLocation( pIrp );
  1004. pCmd = NULL;
  1005. //
  1006. // Incremet pending IO count.
  1007. //
  1008. SSIncrementIoCount( pDeviceObject );
  1009. //
  1010. // Validate state of the device
  1011. //
  1012. if (pde -> AcceptingRequests == FALSE) {
  1013. DebugTrace(TRACE_WARNING,("SSReadWrite: WARNING!! Device is already stopped/removed!\n"));
  1014. Status = STATUS_DELETE_PENDING;
  1015. pIrp -> IoStatus.Information = 0;
  1016. goto SSReadWrite_Complete;
  1017. }
  1018. #if DBG
  1019. if (pIrpStack -> MajorFunction == IRP_MJ_READ) {
  1020. DebugTrace(TRACE_STATUS,("SSReadWrite: Read request received\n"));
  1021. } else {
  1022. DebugTrace(TRACE_STATUS,("SSReadWrite: Write request received\n"));
  1023. }
  1024. #endif
  1025. //
  1026. // Check if device is locked.
  1027. //
  1028. Owner = InterlockedCompareExchangePointer(&pde -> DeviceLock,
  1029. pIrpStack -> FileObject -> FsContext,
  1030. pIrpStack -> FileObject -> FsContext);
  1031. if (Owner != 0) {
  1032. if (Owner != pIrpStack -> FileObject -> FsContext) {
  1033. DebugTrace(TRACE_WARNING,("SSReadWrite: WARNING!! Device is locked\n"));
  1034. Status = STATUS_DEVICE_BUSY;
  1035. pIrp -> IoStatus.Information = 0;
  1036. goto SSReadWrite_Complete;
  1037. }
  1038. }
  1039. pMdl = pIrp -> MdlAddress;
  1040. //
  1041. // Allocate a SCSISCAN_CMD structure and initialize it.
  1042. //
  1043. pCmd = MyAllocatePool(NonPagedPool, sizeof(SCSISCAN_CMD));
  1044. if (NULL == pCmd) {
  1045. DebugTrace(TRACE_CRITICAL, ("SSReadWrite: ERROR!! cannot allocated SCSISCAN_CMD structure\n"));
  1046. DEBUG_BREAKPOINT();
  1047. pIrp->IoStatus.Information = 0;
  1048. Status = STATUS_INSUFFICIENT_RESOURCES;
  1049. goto SSReadWrite_Complete;
  1050. }
  1051. memset(pCmd,0, sizeof(SCSISCAN_CMD));
  1052. //
  1053. // Fill out SCSISCAN_CMD structure.
  1054. //
  1055. #if DBG
  1056. pCmd -> Reserved1 = 'dmCS';
  1057. #endif
  1058. pCmd -> Size = sizeof(SCSISCAN_CMD);
  1059. pCmd -> SrbFlags = SRB_FLAGS_DATA_IN;
  1060. pCmd -> CdbLength = 6;
  1061. pCmd -> SenseLength = SENSE_BUFFER_SIZE;
  1062. pCmd -> TransferLength = pIrpStack->Parameters.Read.Length;
  1063. pCmd -> pSenseBuffer = NULL;
  1064. //
  1065. // Point pSrbStatus to a reserved field in the SCSISCAN_CMD structure.
  1066. // The ReadFile / WriteFile code path never looks at it, but BuildTransferContext
  1067. // will complain if this pointer is NULL.
  1068. //
  1069. pCmd -> pSrbStatus = &(pCmd -> Reserved2);
  1070. //
  1071. // Set READ command anyways.
  1072. //
  1073. pCdb = (PCDB)pCmd -> Cdb;
  1074. pCdb -> CDB6READWRITE.OperationCode = SCSIOP_READ6;
  1075. //
  1076. // Set WRITE command if WriteFile called this function.
  1077. //
  1078. if (pIrpStack -> MajorFunction == IRP_MJ_WRITE) {
  1079. pCmd -> SrbFlags = SRB_FLAGS_DATA_OUT;
  1080. pCdb -> CDB6READWRITE.OperationCode = SCSIOP_WRITE6;
  1081. }
  1082. //
  1083. // Allocate a sense buffer.
  1084. //
  1085. pCmd -> pSenseBuffer = MyAllocatePool(NonPagedPool, SENSE_BUFFER_SIZE);
  1086. if (NULL == pCmd -> pSenseBuffer) {
  1087. DebugTrace(TRACE_CRITICAL, ("SSReadWrite: ERROR!! Cannot allocate sense buffer\n"));
  1088. DEBUG_BREAKPOINT();
  1089. pIrp->IoStatus.Information = 0;
  1090. Status = STATUS_INSUFFICIENT_RESOURCES;
  1091. goto SSReadWrite_Complete;
  1092. }
  1093. #if DBG
  1094. *(PULONG)(pCmd ->pSenseBuffer) = 'sneS';
  1095. #endif
  1096. //
  1097. // Build a transfer context.
  1098. //
  1099. pTransferContext = SSBuildTransferContext(pde, pIrp, pCmd, sizeof(SCSISCAN_CMD), pMdl, TRUE);
  1100. if (NULL == pTransferContext) {
  1101. DebugTrace(TRACE_ERROR,("SSReadWrite: ERROR!! Can't create transfer context!\n"));
  1102. DEBUG_BREAKPOINT();
  1103. MyFreePool(pCmd -> pSenseBuffer);
  1104. MyFreePool(pCmd);
  1105. pCmd = NULL;
  1106. pIrp -> IoStatus.Information = 0;
  1107. Status = STATUS_INSUFFICIENT_RESOURCES;
  1108. goto SSReadWrite_Complete;
  1109. }
  1110. //
  1111. // Fill in transfer length in the CDB.
  1112. //
  1113. pCdb -> PRINT.TransferLength[2] = ((PFOUR_BYTE)&(pTransferContext -> TransferLength)) -> Byte0;
  1114. pCdb -> PRINT.TransferLength[1] = ((PFOUR_BYTE)&(pTransferContext -> TransferLength)) -> Byte1;
  1115. pCdb -> PRINT.TransferLength[0] = ((PFOUR_BYTE)&(pTransferContext -> TransferLength)) -> Byte2;
  1116. //
  1117. // Save retry count in transfer context.
  1118. //
  1119. pTransferContext -> RetryCount = MAXIMUM_RETRIES;
  1120. //
  1121. // Mark IRP with status pending.
  1122. //
  1123. IoMarkIrpPending(pIrp);
  1124. //
  1125. // Set the completion routine and issue scanner request.
  1126. //
  1127. IoSetCompletionRoutine(pIrp, SSReadWriteIoComplete, pTransferContext, TRUE, TRUE, FALSE);
  1128. SSSendScannerRequest(pDeviceObject, pIrp, pTransferContext, FALSE);
  1129. DebugTrace(TRACE_PROC_LEAVE,("SSReadWrite: Leaving... Status=STATUS_PENDING\n"));
  1130. return STATUS_PENDING;
  1131. SSReadWrite_Complete:
  1132. //
  1133. // Free allocated command and sense buffers
  1134. //
  1135. if (pCmd ) {
  1136. if (pCmd -> pSenseBuffer) {
  1137. MyFreePool(pCmd -> pSenseBuffer);
  1138. }
  1139. MyFreePool(pCmd);
  1140. pCmd = NULL;
  1141. }
  1142. pIrp->IoStatus.Status = Status;
  1143. IoCompleteRequest(pIrp, IO_NO_INCREMENT);
  1144. SSDecrementIoCount( pDeviceObject );
  1145. DebugTrace(TRACE_PROC_LEAVE,("SSReadWrite: Leaving... Status=%x\n",Status));
  1146. return Status;
  1147. } // end SSReadWrite()
  1148. PTRANSFER_CONTEXT
  1149. SSBuildTransferContext(
  1150. PSCSISCAN_DEVICE_EXTENSION pde,
  1151. PIRP pIrp,
  1152. PSCSISCAN_CMD pCmd,
  1153. ULONG CmdLength,
  1154. PMDL pTransferMdl,
  1155. BOOLEAN AllowMultipleTransfer
  1156. )
  1157. /*++
  1158. Routine Description:
  1159. Arguments:
  1160. Return Value:
  1161. NULL if error
  1162. --*/
  1163. {
  1164. PMDL pSenseMdl;
  1165. PTRANSFER_CONTEXT pTransferContext;
  1166. PAGED_CODE();
  1167. DebugTrace(TRACE_PROC_ENTER,("SSBuildTransferContext: Enter...\n"));
  1168. //
  1169. // Initialize pointer
  1170. //
  1171. pTransferContext = NULL;
  1172. pSenseMdl = NULL;
  1173. //
  1174. // Validate the SCSISCAN_CMD structure.
  1175. //
  1176. if ( (0 == pCmd -> CdbLength) ||
  1177. (pCmd -> CdbLength > sizeof(pCmd -> Cdb)) )
  1178. {
  1179. DebugTrace(TRACE_WARNING,("SSBuildTransferContext: WARNING!! Badly formed SCSISCAN_CMD struture!\n"));
  1180. goto BuildTransferContext_Error;
  1181. }
  1182. #ifdef _WIN64
  1183. if(IoIs32bitProcess(pIrp)){
  1184. if(pCmd -> Size != sizeof(SCSISCAN_CMD_32)) {
  1185. DebugTrace(TRACE_WARNING,("SSBuildTransferContext: WARNING!! Badly formed SCSISCAN_CMD_32 struture!\n"));
  1186. goto BuildTransferContext_Error;
  1187. }
  1188. } else { // if(IoIs32bitProcess(pIrp))
  1189. #endif // _WIN64
  1190. if(pCmd -> Size != sizeof(SCSISCAN_CMD)){
  1191. DebugTrace(TRACE_WARNING,("SSBuildTransferContext: WARNING!! Badly formed SCSISCAN_CMD struture!\n"));
  1192. goto BuildTransferContext_Error;
  1193. }
  1194. #ifdef _WIN64
  1195. } // if(IoIs32bitProcess(pIrp))
  1196. #endif // _WIN64
  1197. //
  1198. // Verify that pSrbStatus is non-zero.
  1199. //
  1200. if (NULL == pCmd -> pSrbStatus) {
  1201. DebugTrace(TRACE_WARNING,("SSBuildTransferContext: WARNING!! NULL pointer for pSrbStatus!\n"));
  1202. goto BuildTransferContext_Error;
  1203. }
  1204. #if DBG
  1205. pCmd -> Reserved1 = 'dmCS';
  1206. #endif
  1207. //
  1208. // Verify that if TransferLength is non-zero, a transfer direction has also been specified.
  1209. //
  1210. if (0 != pCmd -> TransferLength) {
  1211. if (0 == (pCmd -> SrbFlags & (SRB_FLAGS_DATA_IN | SRB_FLAGS_DATA_OUT))) {
  1212. DebugTrace(TRACE_WARNING,("SSBuildTransferContext: WARNING!! Transfer length specified w/ no direction!\n"));
  1213. goto BuildTransferContext_Error;
  1214. }
  1215. }
  1216. //
  1217. // Verify that if the direction bits have been set, a transfer length has also be specified.
  1218. //
  1219. if (0 != (pCmd -> SrbFlags & (SRB_FLAGS_DATA_IN | SRB_FLAGS_DATA_OUT))) {
  1220. if (0 == pCmd -> TransferLength) {
  1221. DebugTrace(TRACE_WARNING,("SSBuildTransferContext: WARNING!! Direction bits is set w/ 0 transfer size!\n"));
  1222. goto BuildTransferContext_Error;
  1223. }
  1224. }
  1225. //
  1226. // Verify that if TransferLength is non-zero, then an associated MDL has also been specified.
  1227. // Also, verify that the transfer length does not exceed the transfer buffer size.
  1228. //
  1229. if (0 != pCmd -> TransferLength) {
  1230. if (NULL == pTransferMdl) {
  1231. DebugTrace(TRACE_WARNING,("SSBuildTransferContext: WARNING!! Non-zero transfer length w/ NULL buffer!\n"));
  1232. goto BuildTransferContext_Error;
  1233. }
  1234. if (pCmd -> TransferLength > MmGetMdlByteCount(pTransferMdl)) {
  1235. DebugTrace(TRACE_WARNING,("SSBuildTransferContext: WARNING!! Transfer length exceeds buffer size!\n"));
  1236. goto BuildTransferContext_Error;
  1237. }
  1238. }
  1239. //
  1240. // Verify that if SenseLength is non-zero, then pSenseBuffer is non-zero as well.
  1241. //
  1242. if (pCmd -> SenseLength) {
  1243. if (NULL == pCmd -> pSenseBuffer) {
  1244. DebugTrace(TRACE_WARNING,("SSBuildTransferContext: WARNING!! Non-zero sense length w/ NULL buffer!\n"));
  1245. goto BuildTransferContext_Error;
  1246. }
  1247. if (pCmd -> SrbFlags & SRB_FLAGS_DISABLE_AUTOSENSE) {
  1248. DebugTrace(TRACE_STATUS,("SSBuildTransferContext: Autosense disabled with NON-null sense buffer.\n"));
  1249. }
  1250. }
  1251. //
  1252. // Allocate transfer context
  1253. //
  1254. pTransferContext = MyAllocatePool(NonPagedPool, sizeof(TRANSFER_CONTEXT));
  1255. if (NULL == pTransferContext) {
  1256. DebugTrace(TRACE_CRITICAL,("SSBuildTransferContext: ERROR!! Failed to allocate transfer context\n"));
  1257. DEBUG_BREAKPOINT();
  1258. return NULL;
  1259. }
  1260. memset(pTransferContext, 0, sizeof(TRANSFER_CONTEXT));
  1261. #if DBG
  1262. pTransferContext -> Signature = 'refX';
  1263. #endif
  1264. pTransferContext -> pCmd = pCmd;
  1265. if (pCmd -> TransferLength) {
  1266. #ifdef WINNT
  1267. pTransferContext -> pTransferBuffer = MmGetMdlVirtualAddress(pTransferMdl);
  1268. #else
  1269. pTransferContext -> pTransferBuffer = MmGetSystemAddressForMdl(pTransferMdl);
  1270. #endif
  1271. if(NULL == pTransferContext -> pTransferBuffer){
  1272. DebugTrace(TRACE_ERROR,("SSBuildTransferContext: ERROR!! Failed to create address for MDL.\n"));
  1273. DEBUG_BREAKPOINT();
  1274. goto BuildTransferContext_Error;
  1275. }
  1276. pTransferContext -> RemainingTransferLength = pCmd -> TransferLength;
  1277. pTransferContext -> TransferLength = pCmd -> TransferLength;
  1278. //
  1279. // Adjust the transfer size to work within the limits of the hardware. Fail if the transfer is too
  1280. // big and the caller doesn't want the transfer to be split up.
  1281. //
  1282. SSAdjustTransferSize( pde, pTransferContext );
  1283. if (pTransferContext -> RemainingTransferLength !=
  1284. (LONG)pTransferContext -> TransferLength) {
  1285. if (!AllowMultipleTransfer) {
  1286. DebugTrace(TRACE_WARNING,("SSBuildTransferContext: WARNING!! Transfer exceeds hardware limits!\n"));
  1287. goto BuildTransferContext_Error;
  1288. }
  1289. }
  1290. }
  1291. pTransferContext -> pSenseBuffer = pCmd -> pSenseBuffer;
  1292. DebugTrace(TRACE_PROC_LEAVE,("SSBuildTransferContext: Leaving... Return=%x\n",pTransferContext));
  1293. return pTransferContext;
  1294. BuildTransferContext_Error:
  1295. if (pTransferContext) {
  1296. MyFreePool( pTransferContext );
  1297. }
  1298. DebugTrace(TRACE_PROC_LEAVE,("SSBuildTransferContext: Leaving... Return=NULL\n"));
  1299. return NULL;
  1300. } // end SSBuildTransferContext()
  1301. VOID
  1302. SSAdjustTransferSize(
  1303. PSCSISCAN_DEVICE_EXTENSION pde,
  1304. PTRANSFER_CONTEXT pTransferContext
  1305. )
  1306. /*++
  1307. Routine Description:
  1308. This is the entry called by the I/O system for scanner IO.
  1309. Arguments:
  1310. Return Value:
  1311. NT Status
  1312. --*/
  1313. {
  1314. ULONG MaxTransferLength;
  1315. ULONG nTransferPages;
  1316. PAGED_CODE();
  1317. MaxTransferLength = pde -> pAdapterDescriptor -> MaximumTransferLength;
  1318. //
  1319. // Make sure the transfer size does not exceed the limitations of the underlying hardware.
  1320. // If so, we will break the transfer up into chunks.
  1321. //
  1322. if (pTransferContext -> TransferLength > MaxTransferLength) {
  1323. DebugTrace(TRACE_STATUS,("Request size (0x%x) greater than maximum (0x%x)\n",
  1324. pTransferContext -> TransferLength,
  1325. MaxTransferLength));
  1326. pTransferContext -> TransferLength = MaxTransferLength;
  1327. }
  1328. //
  1329. // Calculate number of pages in this transfer.
  1330. //
  1331. nTransferPages = ADDRESS_AND_SIZE_TO_SPAN_PAGES(
  1332. pTransferContext -> pTransferBuffer,
  1333. pTransferContext -> TransferLength);
  1334. if (nTransferPages > pde -> pAdapterDescriptor -> MaximumPhysicalPages) {
  1335. DebugTrace(TRACE_STATUS,("Request number of pages (0x%x) greater than maximum (0x%x).\n",
  1336. nTransferPages,
  1337. pde -> pAdapterDescriptor -> MaximumPhysicalPages));
  1338. //
  1339. // Calculate maximum bytes to transfer that gaurantees that
  1340. // we will not exceed the maximum number of page breaks,
  1341. // assuming that the transfer may not be page alligned.
  1342. //
  1343. pTransferContext -> TransferLength = (pde -> pAdapterDescriptor -> MaximumPhysicalPages - 1) * PAGE_SIZE;
  1344. }
  1345. } // end SSAdjustTransferSize()
  1346. VOID
  1347. SSSetTransferLengthToCdb(
  1348. PCDB pCdb,
  1349. ULONG TransferLength
  1350. )
  1351. /*++
  1352. Routine Description:
  1353. Set transfer length to CDB due to its SCSI command.
  1354. Arguments:
  1355. pCdb - pointer to CDB
  1356. TransferLength - size of data to transfer
  1357. Return Value:
  1358. none
  1359. --*/
  1360. {
  1361. switch (pCdb->SEEK.OperationCode) {
  1362. case 0x24: // Scanner SetWindow command
  1363. case SCSIOP_READ_CAPACITY: // Scanner GetWindow command
  1364. case SCSIOP_READ: // Scanner Read command
  1365. case SCSIOP_WRITE: // Scanner Send Command
  1366. default: // All other commands
  1367. {
  1368. pCdb -> SEEK.Reserved2[2] = ((PFOUR_BYTE)&TransferLength) -> Byte0;
  1369. pCdb -> SEEK.Reserved2[1] = ((PFOUR_BYTE)&TransferLength) -> Byte1;
  1370. pCdb -> SEEK.Reserved2[0] = ((PFOUR_BYTE)&TransferLength) -> Byte2;
  1371. break;
  1372. }
  1373. case 0x34 : // Scanner GetDataBufferStatus Command
  1374. {
  1375. pCdb -> SEEK.Reserved2[2] = ((PFOUR_BYTE)&TransferLength) -> Byte0;
  1376. pCdb -> SEEK.Reserved2[1] = ((PFOUR_BYTE)&TransferLength) -> Byte1;
  1377. break;
  1378. }
  1379. }
  1380. } // end SSSetTransferLengthToCdb()
  1381. VOID
  1382. SSSendScannerRequest(
  1383. PDEVICE_OBJECT pDeviceObject,
  1384. PIRP pIrp,
  1385. PTRANSFER_CONTEXT pTransferContext,
  1386. BOOLEAN Retry
  1387. )
  1388. /*++
  1389. Routine Description:
  1390. Arguments:
  1391. Return Value:
  1392. None.
  1393. --*/
  1394. {
  1395. PSCSISCAN_DEVICE_EXTENSION pde;
  1396. PIO_STACK_LOCATION pIrpStack;
  1397. PIO_STACK_LOCATION pNextIrpStack;
  1398. PSRB pSrb;
  1399. PCDB pCdb;
  1400. PSCSISCAN_CMD pCmd;
  1401. DebugTrace(TRACE_PROC_ENTER,("SendScannerRequest pirp=0x%p TransferBuffer=0x%p\n", pIrp, pTransferContext->pTransferBuffer));
  1402. pde = (PSCSISCAN_DEVICE_EXTENSION)pDeviceObject -> DeviceExtension;
  1403. pIrpStack = IoGetCurrentIrpStackLocation( pIrp );
  1404. pNextIrpStack = IoGetNextIrpStackLocation( pIrp );
  1405. ASSERT(pTransferContext);
  1406. pSrb = &(pTransferContext -> Srb);
  1407. ASSERT(pSrb);
  1408. pCmd = pTransferContext -> pCmd;
  1409. ASSERT(pCmd);
  1410. //
  1411. // Write length to SRB.
  1412. //
  1413. pSrb -> Length = SCSI_REQUEST_BLOCK_SIZE;
  1414. //
  1415. // Set up IRP Address.
  1416. //
  1417. pSrb -> OriginalRequest = pIrp;
  1418. pSrb -> Function = SRB_FUNCTION_EXECUTE_SCSI;
  1419. pSrb -> DataBuffer = pTransferContext -> pTransferBuffer;
  1420. //
  1421. // Save byte count of transfer in SRB Extension.
  1422. //
  1423. pSrb -> DataTransferLength = pTransferContext -> TransferLength;
  1424. //
  1425. // Initialize the queue actions field.
  1426. //
  1427. pSrb -> QueueAction = SRB_SIMPLE_TAG_REQUEST;
  1428. //
  1429. // Queue sort key is not used.
  1430. //
  1431. pSrb -> QueueSortKey = 0;
  1432. //
  1433. // Indicate auto request sense by specifying buffer and size.
  1434. //
  1435. pSrb -> SenseInfoBuffer = pTransferContext -> pSenseBuffer;
  1436. pSrb -> SenseInfoBufferLength = pCmd -> SenseLength;
  1437. //
  1438. // Set timeout value in seconds.
  1439. //
  1440. pSrb -> TimeOutValue = pde -> TimeOutValue;
  1441. //
  1442. // Zero status fields
  1443. //
  1444. pSrb -> SrbStatus = pSrb -> ScsiStatus = 0;
  1445. pSrb -> NextSrb = 0;
  1446. //
  1447. // Get pointer to CDB in SRB.
  1448. //
  1449. pCdb = (PCDB)(pSrb -> Cdb);
  1450. //
  1451. // Set length of CDB.
  1452. //
  1453. pSrb -> CdbLength = pCmd -> CdbLength;
  1454. //
  1455. // Copy the user's CDB into our private CDB.
  1456. //
  1457. RtlCopyMemory(pCdb, pCmd -> Cdb, pCmd -> CdbLength);
  1458. //
  1459. // Set the srb flags.
  1460. //
  1461. pSrb -> SrbFlags = pCmd -> SrbFlags;
  1462. //
  1463. // Or in the default flags from the device object.
  1464. //
  1465. pSrb -> SrbFlags |= pde -> SrbFlags;
  1466. if (Retry) {
  1467. // Disable synchronous data transfers and
  1468. // disable tagged queuing. This fixes some errors.
  1469. DebugTrace(TRACE_STATUS,("SscsiScan :: Retrying \n"));
  1470. //
  1471. // Original code also added disable disconnect flag to SRB.
  1472. // That action would lock SCSI bus and in a case when paging drive is
  1473. // located on the same bus and scanner is taking long timeouts ( for example
  1474. // when it is mechanically locked) memory manager would hit timeout and
  1475. // bugcheck.
  1476. //
  1477. // pSrb -> SrbFlags |= SRB_FLAGS_DISABLE_DISCONNECT |
  1478. //
  1479. pSrb -> SrbFlags |= SRB_FLAGS_DISABLE_SYNCH_TRANSFER;
  1480. pSrb -> SrbFlags &= ~SRB_FLAGS_QUEUE_ACTION_ENABLE;
  1481. DebugTrace(TRACE_STATUS,("SSSendScannerRequest: Retry branch .Srb flags=(0x%x) \n", pSrb -> SrbFlags));
  1482. pSrb -> QueueTag = SP_UNTAGGED;
  1483. }
  1484. //
  1485. // Set up major SCSI function.
  1486. //
  1487. pNextIrpStack -> MajorFunction = IRP_MJ_SCSI;
  1488. //
  1489. // Save SRB address in next stack for port driver.
  1490. //
  1491. pNextIrpStack -> Parameters.Scsi.Srb = pSrb;
  1492. //
  1493. // Print out SRB fields
  1494. //
  1495. // DebugTrace(MAX_TRACE,("SSSendScannerRequest: SRB ready. Flags=(%#X)Func=(%#x) DataLen=%d \nDataBuffer(16)=[%16s] \n",
  1496. DebugTrace(TRACE_STATUS,("SSSendScannerRequest: SRB ready. Flags=(%#X)Func=(%#x) DataLen=%d \nDataBuffer(16)=[%lx] \n",
  1497. pSrb -> SrbFlags ,pSrb -> Function,
  1498. pSrb -> DataTransferLength,
  1499. pSrb -> DataBuffer));
  1500. IoCallDriver(pde -> pStackDeviceObject, pIrp);
  1501. } // end SSSendScannerRequest()
  1502. NTSTATUS
  1503. SSReadWriteIoComplete(
  1504. IN PDEVICE_OBJECT pDeviceObject,
  1505. IN PIRP pIrp,
  1506. IN PTRANSFER_CONTEXT pTransferContext
  1507. )
  1508. /*++
  1509. Routine Description:
  1510. This routine executes when the port driver has completed a request.
  1511. It looks at the SRB status in the completing SRB and if not success
  1512. it checks for valid request sense buffer information. If valid, the
  1513. info is used to update status with more precise message of type of
  1514. error. This routine deallocates the SRB.
  1515. Arguments:
  1516. pDeviceObject - Supplies the device object which represents the logical
  1517. unit.
  1518. pIrp - Supplies the Irp which has completed.
  1519. Return Value:
  1520. NT status
  1521. --*/
  1522. {
  1523. PIO_STACK_LOCATION pIrpStack;
  1524. PIO_STACK_LOCATION pNextIrpStack;
  1525. NTSTATUS Status;
  1526. BOOLEAN Retry;
  1527. PSRB pSrb;
  1528. UCHAR SrbStatus;
  1529. PCDB pCdb;
  1530. PSCSISCAN_CMD pCmd;
  1531. DebugTrace(TRACE_PROC_ENTER,("ReadWriteIoComplete: Enter... IRP 0x%p.\n", pIrp));
  1532. ASSERT(NULL != pTransferContext);
  1533. //
  1534. // Initialize local.
  1535. //
  1536. Retry = FALSE;
  1537. pCdb = NULL;
  1538. pCmd = NULL;
  1539. pIrpStack = IoGetCurrentIrpStackLocation(pIrp);
  1540. pNextIrpStack = IoGetNextIrpStackLocation(pIrp);
  1541. Status = pIrp->IoStatus.Status;
  1542. pSrb = &(pTransferContext -> Srb);
  1543. SrbStatus = SRB_STATUS(pSrb -> SrbStatus);
  1544. if( (SrbStatus != SRB_STATUS_SUCCESS)
  1545. || (STATUS_SUCCESS != Status) )
  1546. {
  1547. DebugTrace(TRACE_ERROR,("ReadWriteIoComplete: ERROR!! Irp error. 0x%p SRB status:0x%p\n", Status, pSrb -> SrbStatus));
  1548. //
  1549. // Release the queue if it is frozen.
  1550. //
  1551. if (pSrb -> SrbStatus & SRB_STATUS_QUEUE_FROZEN) {
  1552. DebugTrace(TRACE_ERROR,("ReadWriteIoComplete: Release queue. IRP 0x%p.\n", pIrp));
  1553. ClassReleaseQueue(pDeviceObject);
  1554. }
  1555. Retry = ClassInterpretSenseInfo(
  1556. pDeviceObject,
  1557. pSrb,
  1558. pNextIrpStack->MajorFunction,
  1559. 0,
  1560. MAXIMUM_RETRIES - ((ULONG)(UINT_PTR)pIrpStack->Parameters.Others.Argument4),
  1561. &Status);
  1562. if (Retry && pTransferContext -> RetryCount--) {
  1563. DebugTrace(TRACE_STATUS,("ReadWriteIoComplete: Retry request 0x%p TransferBuffer=0x%p \n",
  1564. pIrp,pTransferContext->pTransferBuffer));
  1565. IoSetCompletionRoutine(pIrp, SSReadWriteIoComplete, pTransferContext, TRUE, TRUE, FALSE);
  1566. SSSendScannerRequest(pDeviceObject, pIrp, pTransferContext, TRUE);
  1567. return STATUS_MORE_PROCESSING_REQUIRED;
  1568. }
  1569. //
  1570. // If status is overrun, ignore it to support some bad devices.
  1571. //
  1572. if (SRB_STATUS_DATA_OVERRUN == SrbStatus) {
  1573. DebugTrace(TRACE_WARNING,("ReadWriteIoComplete: WARNING!! Data overrun IRP=0x%p. Ignoring...\n", pIrp));
  1574. pTransferContext -> NBytesTransferred += pSrb -> DataTransferLength;
  1575. Status = STATUS_SUCCESS;
  1576. } else {
  1577. DebugTrace(TRACE_STATUS,("ReadWriteIoComplete: Request failed. IRP 0x%p.\n", pIrp));
  1578. // DEBUG_BREAKPOINT();
  1579. pTransferContext -> NBytesTransferred = 0;
  1580. Status = STATUS_IO_DEVICE_ERROR;
  1581. }
  1582. } else {
  1583. pTransferContext -> NBytesTransferred += pSrb -> DataTransferLength;
  1584. pTransferContext -> RemainingTransferLength -= pSrb -> DataTransferLength;
  1585. pTransferContext -> pTransferBuffer += pSrb -> DataTransferLength;
  1586. if (pTransferContext -> RemainingTransferLength > 0) {
  1587. if ((LONG)(pTransferContext -> TransferLength) > pTransferContext -> RemainingTransferLength) {
  1588. pTransferContext -> TransferLength = pTransferContext -> RemainingTransferLength;
  1589. pCmd = pTransferContext -> pCmd;
  1590. pCdb = (PCDB)pCmd -> Cdb;
  1591. pCdb -> PRINT.TransferLength[2] = ((PFOUR_BYTE)&(pTransferContext -> TransferLength)) -> Byte0;
  1592. pCdb -> PRINT.TransferLength[1] = ((PFOUR_BYTE)&(pTransferContext -> TransferLength)) -> Byte1;
  1593. pCdb -> PRINT.TransferLength[0] = ((PFOUR_BYTE)&(pTransferContext -> TransferLength)) -> Byte2;
  1594. }
  1595. IoSetCompletionRoutine(pIrp, SSReadWriteIoComplete, pTransferContext, TRUE, TRUE, FALSE);
  1596. SSSendScannerRequest(pDeviceObject, pIrp, pTransferContext, FALSE);
  1597. return STATUS_MORE_PROCESSING_REQUIRED;
  1598. }
  1599. Status = STATUS_SUCCESS;
  1600. }
  1601. pIrp -> IoStatus.Information = pTransferContext -> NBytesTransferred;
  1602. MyFreePool(pTransferContext -> pCmd -> pSenseBuffer);
  1603. MyFreePool(pTransferContext -> pCmd);
  1604. MyFreePool(pTransferContext);
  1605. pIrp -> IoStatus.Status = Status;
  1606. SSDecrementIoCount( pDeviceObject );
  1607. return Status;
  1608. } // end SSReadWriteIoComplete()
  1609. NTSTATUS
  1610. SSIoctlIoComplete(
  1611. IN PDEVICE_OBJECT pDeviceObject,
  1612. IN PIRP pIrp,
  1613. IN PTRANSFER_CONTEXT pTransferContext
  1614. )
  1615. /*++
  1616. Routine Description:
  1617. This routine executes when an DevIoctl request has completed.
  1618. Arguments:
  1619. pDeviceObject - Supplies the device object which represents the logical
  1620. unit.
  1621. pIrp - Supplies the Irp which has completed.
  1622. pTransferContext - pointer to info about the request.
  1623. Return Value:
  1624. NT status
  1625. --*/
  1626. {
  1627. PIO_STACK_LOCATION pIrpStack;
  1628. NTSTATUS Status;
  1629. PSRB pSrb;
  1630. PSCSISCAN_CMD pCmd;
  1631. PCDB pCdb;
  1632. DebugTrace(TRACE_PROC_ENTER,("IoctlIoComplete: Enter... IRP=0x%p\n", pIrp));
  1633. ASSERT(NULL != pTransferContext);
  1634. pIrpStack = IoGetCurrentIrpStackLocation(pIrp);
  1635. pSrb = &(pTransferContext -> Srb);
  1636. pCmd = pTransferContext -> pCmd;
  1637. ASSERT(NULL != pCmd);
  1638. pCdb = NULL;
  1639. Status = pIrp->IoStatus.Status;
  1640. //
  1641. // Copy the SRB Status back into the user's SCSISCAN_CMD buffer.
  1642. //
  1643. *(pCmd -> pSrbStatus) = pSrb -> SrbStatus;
  1644. //
  1645. // If an error occurred on this transfer, release the frozen queue if necessary.
  1646. //
  1647. if( (SRB_STATUS(pSrb -> SrbStatus) != SRB_STATUS_SUCCESS)
  1648. || (STATUS_SUCCESS != Status) )
  1649. {
  1650. DebugTrace(TRACE_ERROR,("IoctlIoComplete: ERROR!! Irp error. Status=0x%x SRB status:0x%x\n", Status, pSrb -> SrbStatus));
  1651. if (pSrb -> SrbStatus & SRB_STATUS_QUEUE_FROZEN) {
  1652. DebugTrace(TRACE_ERROR,("IoctlIoComplete: Release queue. IRP 0x%p.\n", pIrp));
  1653. ClassReleaseQueue(pDeviceObject);
  1654. }
  1655. } else {
  1656. pTransferContext -> NBytesTransferred += pSrb -> DataTransferLength;
  1657. pTransferContext -> RemainingTransferLength -= pSrb -> DataTransferLength;
  1658. pTransferContext -> pTransferBuffer += pSrb -> DataTransferLength;
  1659. if (pTransferContext -> RemainingTransferLength > 0) {
  1660. if ((LONG)(pTransferContext -> TransferLength) > pTransferContext -> RemainingTransferLength) {
  1661. pTransferContext -> TransferLength = pTransferContext -> RemainingTransferLength;
  1662. pCmd = pTransferContext -> pCmd;
  1663. pCdb = (PCDB)pCmd -> Cdb;
  1664. //
  1665. // SCSISCAN only supports 10bytes CDB fragmentation.
  1666. //
  1667. ASSERT(pCmd->CdbLength == 10);
  1668. //
  1669. // Fill in transfer length in the CDB.
  1670. //
  1671. SSSetTransferLengthToCdb((PCDB)pCmd -> Cdb, pTransferContext -> TransferLength);
  1672. }
  1673. IoSetCompletionRoutine(pIrp, SSIoctlIoComplete, pTransferContext, TRUE, TRUE, FALSE);
  1674. SSSendScannerRequest(pDeviceObject, pIrp, pTransferContext, FALSE);
  1675. return STATUS_MORE_PROCESSING_REQUIRED;
  1676. }
  1677. Status = STATUS_SUCCESS;
  1678. }
  1679. //
  1680. // Clean up and return.
  1681. //
  1682. if (pTransferContext -> pSrbStatusMdl) {
  1683. MmUnlockPages(pTransferContext -> pSrbStatusMdl);
  1684. IoFreeMdl(pTransferContext -> pSrbStatusMdl);
  1685. //pTransferContext -> pSrbStatusMdl = NULL;
  1686. }
  1687. if (pTransferContext -> pSenseMdl) {
  1688. MmUnlockPages(pTransferContext -> pSenseMdl);
  1689. IoFreeMdl(pTransferContext -> pSenseMdl);
  1690. //pTransferContext -> pSenseMdl = NULL;
  1691. }
  1692. pIrp -> IoStatus.Information = pTransferContext -> NBytesTransferred;
  1693. pIrp -> IoStatus.Status = Status;
  1694. MyFreePool(pTransferContext);
  1695. SSDecrementIoCount( pDeviceObject );
  1696. return Status;
  1697. } // end SSIoctlIoComplete()
  1698. NTSTATUS
  1699. SSCreateSymbolicLink(
  1700. PSCSISCAN_DEVICE_EXTENSION pde
  1701. )
  1702. {
  1703. NTSTATUS Status;
  1704. UNICODE_STRING uName;
  1705. UNICODE_STRING uName2;
  1706. ANSI_STRING ansiName;
  1707. CHAR aName[32];
  1708. HANDLE hSwKey;
  1709. PAGED_CODE();
  1710. //
  1711. // Create the symbolic link for this device.
  1712. //
  1713. sprintf(aName,"\\Device\\Scanner%d",pde -> DeviceInstance);
  1714. RtlInitAnsiString(&ansiName, aName);
  1715. RtlAnsiStringToUnicodeString(&uName, &ansiName, TRUE);
  1716. sprintf(aName,"\\DosDevices\\Scanner%d",pde -> DeviceInstance);
  1717. RtlInitAnsiString(&ansiName, aName);
  1718. RtlAnsiStringToUnicodeString(&(pde -> SymbolicLinkName), &ansiName, TRUE);
  1719. Status = IoCreateSymbolicLink( &(pde -> SymbolicLinkName), &uName );
  1720. RtlFreeUnicodeString( &uName );
  1721. if (STATUS_SUCCESS != Status ) {
  1722. DebugTrace(MIN_TRACE,("Cannot create symbolic link.\n"));
  1723. DEBUG_BREAKPOINT();
  1724. Status = STATUS_NOT_SUPPORTED;
  1725. return Status;
  1726. }
  1727. //
  1728. // Now, stuff the symbolic link into the CreateFileName key so that STI can find the device.
  1729. //
  1730. IoOpenDeviceRegistryKey( pde -> pPhysicalDeviceObject,
  1731. PLUGPLAY_REGKEY_DRIVER, KEY_WRITE, &hSwKey);
  1732. RtlInitUnicodeString(&uName,L"CreateFileName");
  1733. sprintf(aName,"\\\\.\\Scanner%d",pde -> DeviceInstance);
  1734. RtlInitAnsiString(&ansiName, aName);
  1735. RtlAnsiStringToUnicodeString(&uName2, &ansiName, TRUE);
  1736. ZwSetValueKey(hSwKey,&uName,0,REG_SZ,uName2.Buffer,uName2.Length);
  1737. RtlFreeUnicodeString( &uName2 );
  1738. return STATUS_SUCCESS;
  1739. }
  1740. NTSTATUS
  1741. SSDestroySymbolicLink(
  1742. PSCSISCAN_DEVICE_EXTENSION pde
  1743. )
  1744. {
  1745. UNICODE_STRING uName;
  1746. UNICODE_STRING uName2;
  1747. ANSI_STRING ansiName;
  1748. CHAR aName[32];
  1749. HANDLE hSwKey;
  1750. PAGED_CODE();
  1751. DebugTrace(MIN_TRACE,("DestroySymbolicLink\n"));
  1752. //
  1753. // Delete the symbolic link to this device.
  1754. //
  1755. IoDeleteSymbolicLink( &(pde -> SymbolicLinkName) );
  1756. //
  1757. // Remove the CreateFile name from the s/w key.
  1758. //
  1759. IoOpenDeviceRegistryKey( pde -> pPhysicalDeviceObject,
  1760. PLUGPLAY_REGKEY_DRIVER, KEY_WRITE, &hSwKey);
  1761. RtlInitUnicodeString(&uName,L"CreateFileName");
  1762. memset(aName, 0, sizeof(aName));
  1763. RtlInitAnsiString(&ansiName, aName);
  1764. RtlAnsiStringToUnicodeString(&uName2, &ansiName, TRUE);
  1765. ZwSetValueKey(hSwKey,&uName,0,REG_SZ,uName2.Buffer,uName2.Length);
  1766. RtlFreeUnicodeString( &uName2 );
  1767. RtlFreeUnicodeString( &(pde -> SymbolicLinkName) );
  1768. ZwClose(hSwKey);
  1769. return STATUS_SUCCESS;
  1770. }
  1771. VOID
  1772. SSIncrementIoCount(
  1773. IN PDEVICE_OBJECT pDeviceObject
  1774. )
  1775. /*++
  1776. Routine Description:
  1777. Arguments:
  1778. Return Value:
  1779. --*/
  1780. {
  1781. PSCSISCAN_DEVICE_EXTENSION pde;
  1782. pde = (PSCSISCAN_DEVICE_EXTENSION)(pDeviceObject -> DeviceExtension);
  1783. InterlockedIncrement(&pde -> PendingIoCount);
  1784. }
  1785. LONG
  1786. SSDecrementIoCount(
  1787. IN PDEVICE_OBJECT pDeviceObject
  1788. )
  1789. /*++
  1790. Routine Description:
  1791. Arguments:
  1792. Return Value:
  1793. --*/
  1794. {
  1795. PSCSISCAN_DEVICE_EXTENSION pde;
  1796. LONG ioCount;
  1797. pde = (PSCSISCAN_DEVICE_EXTENSION)(pDeviceObject -> DeviceExtension);
  1798. ioCount = InterlockedDecrement(&pde -> PendingIoCount);
  1799. DebugTrace(TRACE_STATUS,("Pending io count = %x\n",ioCount));
  1800. if (0 == ioCount) {
  1801. KeSetEvent(&pde -> PendingIoEvent,
  1802. 1,
  1803. FALSE);
  1804. }
  1805. return ioCount;
  1806. }
  1807. NTSTATUS
  1808. SSDeferIrpCompletion(
  1809. IN PDEVICE_OBJECT pDeviceObject,
  1810. IN PIRP pIrp,
  1811. IN PVOID Context
  1812. )
  1813. /*++
  1814. Routine Description:
  1815. This routine is called when the port driver completes an IRP.
  1816. Arguments:
  1817. pDeviceObject - Pointer to the device object for the class device.
  1818. pIrp - Irp completed.
  1819. Context - Driver defined context.
  1820. Return Value:
  1821. The function value is the final status from the operation.
  1822. --*/
  1823. {
  1824. PKEVENT pEvent = Context;
  1825. KeSetEvent(pEvent,
  1826. 1,
  1827. FALSE);
  1828. return STATUS_MORE_PROCESSING_REQUIRED;
  1829. }
  1830. NTSTATUS
  1831. SSPower(
  1832. IN PDEVICE_OBJECT pDeviceObject,
  1833. IN PIRP pIrp
  1834. )
  1835. /*++
  1836. Routine Description:
  1837. Process the Power IRPs sent to the PDO for this device.
  1838. Arguments:
  1839. pDeviceObject - pointer to the functional device object (FDO) for this device.
  1840. pIrp - pointer to an I/O Request Packet
  1841. Return Value:
  1842. NT status code
  1843. --*/
  1844. {
  1845. NTSTATUS Status;
  1846. PSCSISCAN_DEVICE_EXTENSION pde;
  1847. PIO_STACK_LOCATION pIrpStack;
  1848. BOOLEAN hookIt = FALSE;
  1849. PAGED_CODE();
  1850. SSIncrementIoCount( pDeviceObject );
  1851. pde = (PSCSISCAN_DEVICE_EXTENSION)pDeviceObject -> DeviceExtension;
  1852. pIrpStack = IoGetCurrentIrpStackLocation( pIrp );
  1853. Status = STATUS_SUCCESS;
  1854. switch (pIrpStack -> MinorFunction) {
  1855. case IRP_MN_SET_POWER:
  1856. DebugTrace(MIN_TRACE,("IRP_MN_SET_POWER\n"));
  1857. PoStartNextPowerIrp(pIrp);
  1858. IoSkipCurrentIrpStackLocation(pIrp);
  1859. Status = PoCallDriver(pde -> pStackDeviceObject, pIrp);
  1860. SSDecrementIoCount(pDeviceObject);
  1861. break; /* IRP_MN_QUERY_POWER */
  1862. case IRP_MN_QUERY_POWER:
  1863. DebugTrace(MIN_TRACE,("IRP_MN_QUERY_POWER\n"));
  1864. PoStartNextPowerIrp(pIrp);
  1865. IoSkipCurrentIrpStackLocation(pIrp);
  1866. Status = PoCallDriver(pde -> pStackDeviceObject, pIrp);
  1867. SSDecrementIoCount(pDeviceObject);
  1868. break; /* IRP_MN_QUERY_POWER */
  1869. default:
  1870. DebugTrace(MIN_TRACE,("Unknown power message (%x)\n",pIrpStack->MinorFunction));
  1871. PoStartNextPowerIrp(pIrp);
  1872. IoSkipCurrentIrpStackLocation(pIrp);
  1873. Status = PoCallDriver(pde -> pStackDeviceObject, pIrp);
  1874. SSDecrementIoCount(pDeviceObject);
  1875. } /* irpStack->MinorFunction */
  1876. return Status;
  1877. }
  1878. VOID
  1879. SSUnload(
  1880. IN PDRIVER_OBJECT pDriverObject
  1881. )
  1882. /*++
  1883. Routine Description:
  1884. This routine is called when the driver is unloaded.
  1885. Arguments:
  1886. pDriverObject - Pointer to the driver object.evice object for the class device.
  1887. Return Value:
  1888. none.
  1889. --*/
  1890. {
  1891. PAGED_CODE();
  1892. DebugTrace(MIN_TRACE,("Driver unloaded.\n"));
  1893. }
  1894. NTSTATUS
  1895. ScsiScanHandleInterface(
  1896. PDEVICE_OBJECT DeviceObject,
  1897. PUNICODE_STRING InterfaceName,
  1898. BOOLEAN Create
  1899. )
  1900. /*++
  1901. Routine Description:
  1902. Arguments:
  1903. DeviceObject - Supplies the device object.
  1904. Return Value:
  1905. None.
  1906. --*/
  1907. {
  1908. NTSTATUS Status;
  1909. Status = STATUS_SUCCESS;
  1910. #ifndef _CHICAGO_
  1911. if (Create) {
  1912. Status=IoRegisterDeviceInterface(
  1913. DeviceObject,
  1914. &GUID_DEVINTERFACE_IMAGE,
  1915. NULL,
  1916. InterfaceName
  1917. );
  1918. DebugTrace(TRACE_STATUS,("Called IoRegisterDeviceInterface . Returned=0x%X\n",Status));
  1919. if (NT_SUCCESS(Status)) {
  1920. IoSetDeviceInterfaceState(
  1921. InterfaceName,
  1922. TRUE
  1923. );
  1924. DebugTrace(TRACE_STATUS,("Called IoSetDeviceInterfaceState(TRUE) . \n"));
  1925. }
  1926. } else {
  1927. if (InterfaceName->Buffer != NULL) {
  1928. IoSetDeviceInterfaceState(
  1929. InterfaceName,
  1930. FALSE
  1931. );
  1932. DebugTrace(TRACE_STATUS,("Called IoSetDeviceInterfaceState(FALSE) . \n"));
  1933. RtlFreeUnicodeString(
  1934. InterfaceName
  1935. );
  1936. InterfaceName->Buffer = NULL;
  1937. }
  1938. }
  1939. #endif // !_CHICAGO_
  1940. return Status;
  1941. }
  1942. NTSTATUS
  1943. SSCallNextDriverSynch(
  1944. IN PSCSISCAN_DEVICE_EXTENSION pde,
  1945. IN PIRP pIrp
  1946. )
  1947. /*++
  1948. Routine Description:
  1949. Calls lower driver and waits for result
  1950. Arguments:
  1951. DeviceExtension - pointer to device extension
  1952. Irp - pointer to IRP
  1953. Return Value:
  1954. none.
  1955. --*/
  1956. {
  1957. KEVENT Event;
  1958. PIO_STACK_LOCATION IrpStack;
  1959. NTSTATUS Status;
  1960. DebugTrace(TRACE_PROC_ENTER,("SSCallNextDriverSynch: Enter..\n"));
  1961. IrpStack = IoGetCurrentIrpStackLocation(pIrp);
  1962. //
  1963. // Copy IRP stack to the next.
  1964. //
  1965. IoCopyCurrentIrpStackLocationToNext(pIrp);
  1966. //
  1967. // Initialize synchronizing event.
  1968. //
  1969. KeInitializeEvent(&Event,
  1970. SynchronizationEvent,
  1971. FALSE);
  1972. //
  1973. // Set completion routine
  1974. //
  1975. IoSetCompletionRoutine(pIrp,
  1976. SSDeferIrpCompletion,
  1977. &Event,
  1978. TRUE,
  1979. TRUE,
  1980. TRUE);
  1981. //
  1982. // Call down
  1983. //
  1984. Status = IoCallDriver(pde -> pStackDeviceObject, pIrp);
  1985. if (Status == STATUS_PENDING) {
  1986. //
  1987. // Waiting for the completion.
  1988. //
  1989. DebugTrace(TRACE_STATUS,("SSCallNextDriverSynch: STATUS_PENDING. Wait for event.\n"));
  1990. KeWaitForSingleObject(&Event,
  1991. Executive,
  1992. KernelMode,
  1993. FALSE,
  1994. NULL);
  1995. Status = pIrp -> IoStatus.Status;
  1996. }
  1997. //
  1998. // Return
  1999. //
  2000. DebugTrace(TRACE_PROC_LEAVE,("SSCallNextDriverSynch: Leaving.. Status = %x\n", Status));
  2001. return (Status);
  2002. }