1 #ifndef _USBSTOR_H_ 2 #define _USBSTOR_H_ 3 4 #include <wdm.h> 5 #include <ntstrsafe.h> 6 #include <usbdi.h> 7 #include <usbbusif.h> 8 #include <usbdlib.h> 9 #include <classpnp.h> 10 11 #define USB_STOR_TAG 'sbsu' 12 13 #ifndef BooleanFlagOn 14 #define BooleanFlagOn(Flags, SingleFlag) ((BOOLEAN)((((Flags) & (SingleFlag)) != 0))) 15 #endif 16 17 #ifndef SrbGetCdb 18 #define SrbGetCdb(srb) ((PCDB)(srb->Cdb)) 19 #endif 20 21 // Storage subclass codes 22 23 #define USB_SUBCLASS_RBC 0x01 // Typically, flash devices 24 #define USB_SUBCLASS_8020 0x02 // CD-ROM 25 #define USB_SUBCLASS_QIC 0x03 // QIC-157 Tapes 26 #define USB_SUBCLASS_UFI 0x04 // Floppy 27 #define USB_SUBCLASS_8070 0x05 // Removable media 28 #define USB_SUBCLASS_SCSI 0x06 // Transparent 29 #define USB_SUBCLASS_LOCKABLE 0x07 // Password-protected 30 31 #define USB_SUBCLASS_ISD200 0xF0 // ISD200 ATA 32 #define USB_SUBCLASS_CYP_ATACB 0xF1 // Cypress ATACB 33 #define USB_SUBCLASS_VENDOR 0xFF // Use vendor specific 34 35 // Storage protocol codes 36 37 #define USB_PROTOCOL_CBI 0x00 // Control/Bulk/Interrupt 38 #define USB_PROTOCOL_CB 0x01 // Control/Bulk w/o interrupt 39 #define USB_PROTOCOL_BULK 0x50 // bulk only 40 #define USB_PROTOCOL_UAS 0x62 // USB Attached SCSI 41 #define USB_PROTOCOL_USBAT 0x80 // SCM-ATAPI bridge 42 #define USB_PROTOCOL_EUSB_SDDR09 0x81 // SCM-SCSI bridge for SDDR-09 43 #define USB_PROTOCOL_SDDR55 0x82 // SDDR-55 (made up) 44 45 #define USB_PROTOCOL_DPCM_USB 0xF0 // Combination CB/SDDR09 46 #define USB_PROTOCOL_FREECOM 0xF1 // Freecom 47 #define USB_PROTOCOL_DATAFAB 0xF2 // Datafab chipsets 48 #define USB_PROTOCOL_JUMPSHOT 0xF3 // Lexar Jumpshot 49 #define USB_PROTOCOL_ALAUDA 0xF4 // Alauda chipsets 50 #define USB_PROTOCOL_KARMA 0xF5 // Rio Karma 51 #define USB_PROTOCOL_VENDOR 0xFF // Use vendor specific 52 53 // Mass storage class-specific commands 54 55 #define USB_BULK_GET_MAX_LUN 0xFE 56 #define USB_BULK_RESET_DEVICE 0xFF 57 58 #define USB_RECOVERABLE_ERRORS (USBD_STATUS_STALL_PID | USBD_STATUS_DEV_NOT_RESPONDING \ 59 | USBD_STATUS_ENDPOINT_HALTED | USBD_STATUS_NO_BANDWIDTH) 60 61 #define USB_MAXCHILDREN 16 62 #define MAX_LUN 0xF 63 #define USBSTOR_DEFAULT_MAX_TRANSFER_LENGTH 0x10000 64 65 #define CBW_SIGNATURE 0x43425355 66 #define CSW_SIGNATURE 0x53425355 67 68 #include <pshpack1.h> 69 70 typedef struct 71 { 72 ULONG Signature; // CBW signature 73 ULONG Tag; // CBW Tag of operation 74 ULONG DataTransferLength; // data transfer length 75 UCHAR Flags; // CBW Flags endpoint direction 76 UCHAR LUN; // lun unit 77 UCHAR CommandBlockLength; // Command block length 78 UCHAR CommandBlock[16]; 79 } CBW, *PCBW; 80 81 C_ASSERT(sizeof(CBW) == 31); 82 83 #define CSW_STATUS_COMMAND_PASSED 0x00 84 #define CSW_STATUS_COMMAND_FAILED 0x01 85 #define CSW_STATUS_PHASE_ERROR 0x02 86 87 typedef struct 88 { 89 ULONG Signature; // CSW signature 90 ULONG Tag; // CSW tag 91 ULONG DataResidue; // CSW data transfer diff 92 UCHAR Status; // CSW status 93 } CSW, *PCSW; 94 95 #include <poppack.h> 96 97 typedef struct 98 { 99 PIRP Irp; 100 ULONG ErrorIndex; 101 ULONG StallRetryCount; // the number of retries after receiving USBD_STATUS_STALL_PID status 102 union 103 { 104 CBW cbw; 105 CSW csw; 106 }; 107 URB Urb; 108 SCSI_REQUEST_BLOCK SenseSrb; 109 } IRP_CONTEXT, *PIRP_CONTEXT; 110 111 typedef struct __COMMON_DEVICE_EXTENSION__ 112 { 113 BOOLEAN IsFDO; 114 115 }USBSTOR_COMMON_DEVICE_EXTENSION, *PUSBSTOR_COMMON_DEVICE_EXTENSION; 116 117 #define USBSTOR_FDO_FLAGS_DEVICE_RESETTING 0x00000001 // hard reset is in progress 118 #define USBSTOR_FDO_FLAGS_IRP_LIST_FREEZE 0x00000002 // the irp list is freezed 119 120 typedef struct 121 { 122 USBSTOR_COMMON_DEVICE_EXTENSION Common; // common device extension 123 124 PDEVICE_OBJECT FunctionalDeviceObject; // functional device object 125 PDEVICE_OBJECT PhysicalDeviceObject; // physical device object 126 PDEVICE_OBJECT LowerDeviceObject; // lower device object 127 USB_BUS_INTERFACE_USBDI_V2 BusInterface; // bus interface of device 128 PUSB_DEVICE_DESCRIPTOR DeviceDescriptor; // usb device descriptor 129 PUSB_CONFIGURATION_DESCRIPTOR ConfigurationDescriptor; // usb configuration descriptor 130 PUSB_STRING_DESCRIPTOR SerialNumber; // usb serial number 131 PUSBD_INTERFACE_INFORMATION InterfaceInformation; // usb interface information 132 USBD_CONFIGURATION_HANDLE ConfigurationHandle; // usb configuration handle 133 UCHAR BulkInPipeIndex; // bulk in pipe index 134 UCHAR BulkOutPipeIndex; // bulk out pipe index 135 UCHAR MaxLUN; // max lun for device 136 PDEVICE_OBJECT ChildPDO[USB_MAXCHILDREN]; // max 16 child pdo devices 137 KSPIN_LOCK IrpListLock; // irp list lock 138 LIST_ENTRY IrpListHead; // irp list head 139 ULONG IrpPendingCount; // count of irp pending 140 PSCSI_REQUEST_BLOCK ActiveSrb; // stores the current active SRB 141 KEVENT NoPendingRequests; // set if no pending or in progress requests 142 PSCSI_REQUEST_BLOCK LastTimerActiveSrb; // last timer tick active srb 143 ULONG SrbErrorHandlingActive; // error handling of srb is activated 144 ULONG TimerWorkQueueEnabled; // timer work queue enabled 145 ULONG InstanceCount; // pdo instance count 146 KSPIN_LOCK CommonLock; 147 PIO_WORKITEM ResetDeviceWorkItem; 148 ULONG Flags; 149 IRP_CONTEXT CurrentIrpContext; 150 }FDO_DEVICE_EXTENSION, *PFDO_DEVICE_EXTENSION; 151 152 typedef struct 153 { 154 USBSTOR_COMMON_DEVICE_EXTENSION Common; 155 156 UCHAR LUN; // lun id 157 BOOLEAN Claimed; // indicating if it has been claimed by upper driver 158 PDEVICE_OBJECT LowerDeviceObject; // points to FDO 159 PDEVICE_OBJECT *PDODeviceObject; // entry in pdo list 160 PDEVICE_OBJECT Self; // self 161 // the whole structure is not stored 162 UCHAR InquiryData[INQUIRYDATABUFFERSIZE]; // USB SCSI inquiry data 163 }PDO_DEVICE_EXTENSION, *PPDO_DEVICE_EXTENSION; 164 165 typedef struct _ERRORHANDLER_WORKITEM_DATA 166 { 167 PDEVICE_OBJECT DeviceObject; 168 PIRP_CONTEXT Context; 169 WORK_QUEUE_ITEM WorkQueueItem; 170 PIRP Irp; 171 } ERRORHANDLER_WORKITEM_DATA, *PERRORHANDLER_WORKITEM_DATA; 172 173 // we need this to be compatible with ReactOS' classpnp (which is compiled with NTDDI_WIN8) 174 typedef struct _STORAGE_ADAPTER_DESCRIPTOR_WIN8 { 175 ULONG Version; 176 ULONG Size; 177 ULONG MaximumTransferLength; 178 ULONG MaximumPhysicalPages; 179 ULONG AlignmentMask; 180 BOOLEAN AdapterUsesPio; 181 BOOLEAN AdapterScansDown; 182 BOOLEAN CommandQueueing; 183 BOOLEAN AcceleratedTransfer; 184 UCHAR BusType; 185 USHORT BusMajorVersion; 186 USHORT BusMinorVersion; 187 UCHAR SrbType; 188 UCHAR AddressType; 189 } STORAGE_ADAPTER_DESCRIPTOR_WIN8, *PSTORAGE_ADAPTER_DESCRIPTOR_WIN8; 190 191 192 //--------------------------------------------------------------------- 193 // 194 // fdo.c routines 195 // 196 NTSTATUS 197 USBSTOR_FdoHandlePnp( 198 IN PDEVICE_OBJECT DeviceObject, 199 IN OUT PIRP Irp); 200 201 //--------------------------------------------------------------------- 202 // 203 // pdo.c routines 204 // 205 NTSTATUS 206 USBSTOR_PdoHandlePnp( 207 IN PDEVICE_OBJECT DeviceObject, 208 IN OUT PIRP Irp); 209 210 NTSTATUS 211 USBSTOR_CreatePDO( 212 IN PDEVICE_OBJECT DeviceObject, 213 IN UCHAR LUN); 214 215 //--------------------------------------------------------------------- 216 // 217 // misc.c routines 218 // 219 220 NTSTATUS 221 NTAPI 222 USBSTOR_GetBusInterface( 223 IN PDEVICE_OBJECT DeviceObject, 224 OUT PUSB_BUS_INTERFACE_USBDI_V2 BusInterface); 225 226 PVOID 227 AllocateItem( 228 IN POOL_TYPE PoolType, 229 IN ULONG ItemSize); 230 231 VOID 232 FreeItem( 233 IN PVOID Item); 234 235 NTSTATUS 236 USBSTOR_SyncUrbRequest( 237 IN PDEVICE_OBJECT DeviceObject, 238 OUT PURB UrbRequest); 239 240 NTSTATUS 241 USBSTOR_GetMaxLUN( 242 IN PDEVICE_OBJECT DeviceObject, 243 IN PFDO_DEVICE_EXTENSION DeviceExtension); 244 245 NTSTATUS 246 USBSTOR_ResetDevice( 247 IN PDEVICE_OBJECT DeviceObject, 248 IN PFDO_DEVICE_EXTENSION DeviceExtension); 249 250 //--------------------------------------------------------------------- 251 // 252 // descriptor.c routines 253 // 254 255 NTSTATUS 256 USBSTOR_GetDescriptors( 257 IN PDEVICE_OBJECT DeviceObject); 258 259 NTSTATUS 260 USBSTOR_SelectConfigurationAndInterface( 261 IN PDEVICE_OBJECT DeviceObject, 262 IN PFDO_DEVICE_EXTENSION DeviceExtension); 263 264 NTSTATUS 265 USBSTOR_GetPipeHandles( 266 IN PFDO_DEVICE_EXTENSION DeviceExtension); 267 268 //--------------------------------------------------------------------- 269 // 270 // scsi.c routines 271 // 272 NTSTATUS 273 USBSTOR_HandleExecuteSCSI( 274 IN PDEVICE_OBJECT DeviceObject, 275 IN PIRP Irp); 276 277 NTSTATUS 278 USBSTOR_SendCSWRequest( 279 PFDO_DEVICE_EXTENSION FDODeviceExtension, 280 PIRP Irp); 281 282 283 //--------------------------------------------------------------------- 284 // 285 // disk.c routines 286 // 287 NTSTATUS 288 USBSTOR_HandleInternalDeviceControl( 289 IN PDEVICE_OBJECT DeviceObject, 290 IN PIRP Irp); 291 292 NTSTATUS 293 USBSTOR_HandleDeviceControl( 294 IN PDEVICE_OBJECT DeviceObject, 295 IN PIRP Irp); 296 297 //--------------------------------------------------------------------- 298 // 299 // queue.c routines 300 // 301 VOID 302 NTAPI 303 USBSTOR_StartIo( 304 PDEVICE_OBJECT DeviceObject, 305 PIRP Irp); 306 307 VOID 308 USBSTOR_QueueWaitForPendingRequests( 309 IN PDEVICE_OBJECT DeviceObject); 310 311 VOID 312 USBSTOR_QueueRelease( 313 IN PDEVICE_OBJECT DeviceObject); 314 315 BOOLEAN 316 USBSTOR_QueueAddIrp( 317 IN PDEVICE_OBJECT DeviceObject, 318 IN PIRP Irp); 319 320 VOID 321 NTAPI 322 USBSTOR_CancelIo( 323 IN PDEVICE_OBJECT DeviceObject, 324 IN PIRP Irp); 325 326 VOID 327 USBSTOR_QueueInitialize( 328 PFDO_DEVICE_EXTENSION FDODeviceExtension); 329 330 VOID 331 USBSTOR_QueueNextRequest( 332 IN PDEVICE_OBJECT DeviceObject); 333 334 VOID 335 USBSTOR_QueueTerminateRequest( 336 IN PDEVICE_OBJECT DeviceObject, 337 IN PIRP Irp); 338 339 //--------------------------------------------------------------------- 340 // 341 // error.c routines 342 // 343 NTSTATUS 344 USBSTOR_GetEndpointStatus( 345 IN PDEVICE_OBJECT DeviceObject, 346 IN UCHAR bEndpointAddress, 347 OUT PUSHORT Value); 348 349 NTSTATUS 350 USBSTOR_ResetPipeWithHandle( 351 IN PDEVICE_OBJECT DeviceObject, 352 IN USBD_PIPE_HANDLE PipeHandle); 353 354 VOID 355 NTAPI 356 USBSTOR_TimerRoutine( 357 PDEVICE_OBJECT DeviceObject, 358 PVOID Context); 359 360 VOID 361 NTAPI 362 USBSTOR_QueueResetPipe( 363 IN PFDO_DEVICE_EXTENSION FDODeviceExtension); 364 365 VOID 366 NTAPI 367 USBSTOR_QueueResetDevice( 368 IN PFDO_DEVICE_EXTENSION FDODeviceExtension); 369 370 #endif // _USBSTOR_H_ 371