ReactOS 0.4.15-dev-7924-g5949c20
misc.c
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS Universal Serial Bus Bulk Storage Driver
3 * LICENSE: GPL-2.0-or-later (https://spdx.org/licenses/GPL-2.0-or-later)
4 * PURPOSE: USB block storage device driver.
5 * COPYRIGHT: 2005-2006 James Tabor
6 * 2011-2012 Michael Martin (michael.martin@reactos.org)
7 * 2011-2013 Johannes Anderwald (johannes.anderwald@reactos.org)
8 */
9
10#include "usbstor.h"
11
12#define NDEBUG
13#include <debug.h>
14
15IO_COMPLETION_ROUTINE SyncForwardIrpCompletionRoutine;
16
21 PIRP Irp,
23{
24 if (Irp->PendingReturned)
25 {
27 }
29}
30
36{
39 PIRP Irp;
42
44 ASSERT(BusInterface);
45
47
50 NULL,
51 0,
52 NULL,
53 &Event,
54 &IoStatus);
55 if (Irp == NULL)
56 {
58 }
59
60 // initialize request
62 Stack->MajorFunction = IRP_MJ_PNP;
63 Stack->MinorFunction = IRP_MN_QUERY_INTERFACE;
64 Stack->Parameters.QueryInterface.Size = sizeof(BUS_INTERFACE_STANDARD);
65 Stack->Parameters.QueryInterface.InterfaceType = (LPGUID)&USB_BUS_INTERFACE_USBDI_GUID;
66 Stack->Parameters.QueryInterface.Version = 2;
67 Stack->Parameters.QueryInterface.Interface = (PINTERFACE)BusInterface;
68 Stack->Parameters.QueryInterface.InterfaceSpecificData = NULL;
69 Irp->IoStatus.Status = STATUS_NOT_SUPPORTED;
70
72
74 {
76 Status = IoStatus.Status;
77 }
78
79 return Status;
80}
81
85 OUT PURB UrbRequest)
86{
87 PIRP Irp;
88 PIO_STACK_LOCATION IoStack;
91
92 Irp = IoAllocateIrp(DeviceObject->StackSize, FALSE);
93 if (!Irp)
94 {
96 }
97
99
101
102 // initialize stack location
105 IoStack->Parameters.Others.Argument1 = (PVOID)UrbRequest;
106 IoStack->Parameters.DeviceIoControl.InputBufferLength = UrbRequest->UrbHeader.Length;
107 Irp->IoStatus.Status = STATUS_SUCCESS;
108
110
112
113 if (Status == STATUS_PENDING)
114 {
116 Status = Irp->IoStatus.Status;
117 }
118
119 IoFreeIrp(Irp);
120 return Status;
121}
122
123PVOID
127{
128 return ExAllocatePoolZero(PoolType, ItemSize, USB_STOR_TAG);
129}
130
131VOID
133 IN PVOID Item)
134{
136}
137
141 IN PFDO_DEVICE_EXTENSION DeviceExtension,
144 IN ULONG TransferFlags,
145 IN ULONG TransferBufferLength,
146 IN PVOID TransferBuffer)
147
148{
149 PURB Urb;
151
153 if (!Urb)
154 {
156 }
157
160 Urb->UrbControlVendorClassRequest.TransferFlags = TransferFlags;
161 Urb->UrbControlVendorClassRequest.TransferBufferLength = TransferBufferLength;
162 Urb->UrbControlVendorClassRequest.TransferBuffer = TransferBuffer;
165
167
168 FreeItem(Urb);
169 return Status;
170}
171
175 IN PFDO_DEVICE_EXTENSION DeviceExtension)
176{
179
181 if (!Buffer)
182 {
184 }
185
186 Status = USBSTOR_ClassRequest(DeviceObject, DeviceExtension, USB_BULK_GET_MAX_LUN, DeviceExtension->InterfaceInformation->InterfaceNumber, USBD_TRANSFER_DIRECTION_IN, sizeof(UCHAR), Buffer);
187
188 DPRINT("MaxLUN: %x\n", *Buffer);
189
190 if (NT_SUCCESS(Status))
191 {
192 if (*Buffer > MAX_LUN)
193 {
194 // invalid response documented in usb mass storage specification
196 }
197 else
198 {
199 // store maxlun
200 DeviceExtension->MaxLUN = *Buffer;
201 }
202 }
203 else
204 {
205 // "USB Mass Storage Class. Bulk-Only Transport. Revision 1.0"
206 // 3.2 Get Max LUN (class-specific request) :
207 // Devices that do not support multiple LUNs may STALL this command.
208 USBSTOR_ResetDevice(DeviceExtension->LowerDeviceObject, DeviceExtension);
209
210 DeviceExtension->MaxLUN = 0;
212 }
213
215 return Status;
216}
217
221 IN PFDO_DEVICE_EXTENSION DeviceExtension)
222{
224
225 Status = USBSTOR_ClassRequest(DeviceObject, DeviceExtension, USB_BULK_RESET_DEVICE, DeviceExtension->InterfaceInformation->InterfaceNumber, USBD_TRANSFER_DIRECTION_OUT, 0, NULL);
226 return Status;
227}
228
229// if somebody wants to add UFI support, here is a useful function
230#if 0
235 OUT PUCHAR MediumTypeCode)
236{
237 PUFI_CAPACITY_FORMAT_HEADER FormatHeader;
239 ULONG Length, Index, BlockCount, BlockLength;
240
241 FormatHeader = (PUFI_CAPACITY_FORMAT_HEADER)Buffer;
242 ASSERT(FormatHeader->Reserved1 == 0x00);
243 ASSERT(FormatHeader->Reserved2 == 0x00);
244 ASSERT(FormatHeader->Reserved3 == 0x00);
245
246 // is there capacity data
247 if (!FormatHeader->CapacityLength)
248 {
249 DPRINT1("[USBSTOR] No capacity length\n");
250 return FALSE;
251 }
252
253 // the format header are always 8 bytes in length
254 ASSERT((FormatHeader->CapacityLength & 0x7) == 0);
255 DPRINT1("CapacityLength %x\n", FormatHeader->CapacityLength);
256
257 // grab length and locate first descriptor
258 Length = FormatHeader->CapacityLength;
259 Descriptor = (PUFI_CAPACITY_DESCRIPTOR)(FormatHeader + 1);
260 for (Index = 0; Index < Length / sizeof(UFI_CAPACITY_DESCRIPTOR); Index++)
261 {
262 // blocks are little endian format
263 BlockCount = NTOHL(Descriptor->BlockCount);
264 BlockLength = NTOHL((Descriptor->BlockLengthByte0 << 24 | Descriptor->BlockLengthByte1 << 16 | Descriptor->BlockLengthByte2 << 8));
265
266 DPRINT1("BlockCount %x BlockLength %x Code %x\n", BlockCount, BlockLength, Descriptor->Code);
267
268 if (BlockLength == 512 && BlockCount == 1440)
269 {
270 // 720 KB DD
271 *MediumTypeCode = 0x1E;
272 return TRUE;
273 }
274 else if (BlockLength == 1024 && BlockCount == 1232)
275 {
276 // 1,25 MB
277 *MediumTypeCode = 0x93;
278 return TRUE;
279 }
280 else if (BlockLength == 512 && BlockCount == 2880)
281 {
282 // 1,44MB KB DD
283 *MediumTypeCode = 0x94;
284 return TRUE;
285 }
286
287 Descriptor++;
288 }
289
290 return FALSE;
291}
292#endif
unsigned char BOOLEAN
LONG NTSTATUS
Definition: precomp.h:26
#define DPRINT1
Definition: precomp.h:8
#define IRP_MJ_PNP
Definition: cdrw_usr.h:52
Definition: bufpool.h:45
_In_ PIRP Irp
Definition: csq.h:116
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
PVOID AllocateItem(IN POOL_TYPE PoolType, IN SIZE_T NumberOfBytes)
Definition: misc.c:29
VOID FreeItem(IN PVOID Item)
Definition: misc.c:37
IO_COMPLETION_ROUTINE SyncForwardIrpCompletionRoutine
Definition: misc.c:18
NTSTATUS NTAPI USBSTOR_SyncForwardIrpCompletionRoutine(PDEVICE_OBJECT DeviceObject, PIRP Irp, PVOID Context)
Definition: misc.c:22
NTSTATUS USBSTOR_SyncUrbRequest(IN PDEVICE_OBJECT DeviceObject, OUT PURB UrbRequest)
Definition: misc.c:83
NTSTATUS USBSTOR_GetMaxLUN(IN PDEVICE_OBJECT DeviceObject, IN PFDO_DEVICE_EXTENSION DeviceExtension)
Definition: misc.c:173
NTSTATUS USBSTOR_ResetDevice(IN PDEVICE_OBJECT DeviceObject, IN PFDO_DEVICE_EXTENSION DeviceExtension)
Definition: misc.c:219
NTSTATUS USBSTOR_ClassRequest(IN PDEVICE_OBJECT DeviceObject, IN PFDO_DEVICE_EXTENSION DeviceExtension, IN UCHAR RequestType, IN USHORT Index, IN ULONG TransferFlags, IN ULONG TransferBufferLength, IN PVOID TransferBuffer)
Definition: misc.c:139
NTSTATUS NTAPI USBSTOR_GetBusInterface(IN PDEVICE_OBJECT DeviceObject, OUT PUSB_BUS_INTERFACE_USBDI_V2 BusInterface)
Definition: misc.c:33
BOOLEAN USBSTOR_IsFloppy(IN PUCHAR Buffer, IN ULONG BufferLength, OUT PUCHAR MediumTypeCode)
Definition: misc.c:409
#define ExAllocatePoolWithTag(hernya, size, tag)
Definition: env_spec_w32.h:350
#define KeWaitForSingleObject(pEvt, foo, a, b, c)
Definition: env_spec_w32.h:478
#define KeInitializeEvent(pEvt, foo, foo2)
Definition: env_spec_w32.h:477
#define KeSetEvent(pEvt, foo, foo2)
Definition: env_spec_w32.h:476
#define NonPagedPool
Definition: env_spec_w32.h:307
Status
Definition: gdiplustypes.h:25
#define IoSetCompletionRoutine(_Irp, _CompletionRoutine, _Context, _InvokeOnSuccess, _InvokeOnError, _InvokeOnCancel)
Definition: irp.cpp:490
#define ASSERT(a)
Definition: mode.c:44
#define ExFreePoolWithTag(_P, _T)
Definition: module.h:1109
__in UCHAR __in POWER_STATE __in_opt PVOID __in PIO_STATUS_BLOCK IoStatus
Definition: mxum.h:159
#define KernelMode
Definition: asm.h:34
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:102
@ NotificationEvent
PIRP NTAPI IoBuildSynchronousFsdRequest(IN ULONG MajorFunction, IN PDEVICE_OBJECT DeviceObject, IN PVOID Buffer, IN ULONG Length, IN PLARGE_INTEGER StartingOffset, IN PKEVENT Event, IN PIO_STATUS_BLOCK IoStatusBlock)
Definition: irp.c:1069
PIRP NTAPI IoAllocateIrp(IN CCHAR StackSize, IN BOOLEAN ChargeQuota)
Definition: irp.c:615
#define IoCallDriver
Definition: irp.c:1225
VOID NTAPI IoFreeIrp(IN PIRP Irp)
Definition: irp.c:1666
#define STATUS_PENDING
Definition: ntstatus.h:82
#define STATUS_NOT_SUPPORTED
Definition: ntstatus.h:423
unsigned short USHORT
Definition: pedump.c:61
GUID * LPGUID
Definition: guiddef.h:81
struct _INTERFACE * PINTERFACE
static UCHAR ItemSize[4]
Definition: parser.c:16
#define STATUS_MORE_PROCESSING_REQUIRED
Definition: shellext.h:68
#define STATUS_SUCCESS
Definition: shellext.h:65
#define DPRINT
Definition: sndvol32.h:71
struct _IO_STACK_LOCATION::@1564::@1565 DeviceIoControl
struct _IO_STACK_LOCATION::@3978::@4017 Others
union _IO_STACK_LOCATION::@1564 Parameters
Definition: usb.h:529
struct _URB_CONTROL_VENDOR_OR_CLASS_REQUEST UrbControlVendorClassRequest
Definition: usb.h:548
INT POOL_TYPE
Definition: typedefs.h:78
#define NTAPI
Definition: typedefs.h:36
void * PVOID
Definition: typedefs.h:50
#define IN
Definition: typedefs.h:39
unsigned char * PUCHAR
Definition: typedefs.h:53
uint32_t ULONG
Definition: typedefs.h:59
#define OUT
Definition: typedefs.h:40
#define STATUS_DEVICE_DATA_ERROR
Definition: udferr_usr.h:159
#define STATUS_INSUFFICIENT_RESOURCES
Definition: udferr_usr.h:158
#define URB_FUNCTION_CLASS_INTERFACE
Definition: usb.h:113
struct _URB * PURB
#define USBD_TRANSFER_DIRECTION_IN
Definition: usb.h:160
#define USBD_TRANSFER_DIRECTION_OUT
Definition: usb.h:159
#define IOCTL_INTERNAL_USB_SUBMIT_URB
Definition: usbioctl.h:32
#define USB_BULK_GET_MAX_LUN
Definition: usbstor.h:55
#define MAX_LUN
Definition: usbstor.h:62
#define USB_STOR_TAG
Definition: usbstor.h:11
#define USB_BULK_RESET_DEVICE
Definition: usbstor.h:56
struct UFI_CAPACITY_FORMAT_HEADER * PUFI_CAPACITY_FORMAT_HEADER
struct UFI_CAPACITY_DESCRIPTOR * PUFI_CAPACITY_DESCRIPTOR
#define NTOHL(n)
Definition: usbstor.h:22
_In_ WDFCOLLECTION _In_ ULONG Index
_In_ WDFCOLLECTION _In_ WDFOBJECT Item
_In_ PDEVICE_OBJECT DeviceObject
Definition: wdfdevice.h:2055
_Must_inspect_result_ _In_ WDFDEVICE _In_ DEVICE_REGISTRY_PROPERTY _In_ _Strict_type_match_ POOL_TYPE PoolType
Definition: wdfdevice.h:3815
_Must_inspect_result_ _In_ WDFDEVICE _In_ DEVICE_REGISTRY_PROPERTY _In_ ULONG BufferLength
Definition: wdfdevice.h:3771
_Must_inspect_result_ _In_ WDFDEVICE _In_ WDFQUEUE _In_ _Strict_type_match_ WDF_REQUEST_TYPE RequestType
Definition: wdfdevice.h:4233
_In_ WDFREQUEST _In_ PIO_STACK_LOCATION Stack
Definition: wdfrequest.h:639
_Must_inspect_result_ _In_ WDFIORESLIST _In_ PIO_RESOURCE_DESCRIPTOR Descriptor
Definition: wdfresource.h:342
__drv_aliasesMem FORCEINLINE PIO_STACK_LOCATION IoGetNextIrpStackLocation(_In_ PIRP Irp)
Definition: iofuncs.h:2695
#define IO_NO_INCREMENT
Definition: iotypes.h:598
#define IRP_MN_QUERY_INTERFACE
struct _BUS_INTERFACE_STANDARD BUS_INTERFACE_STANDARD
#define IRP_MJ_INTERNAL_DEVICE_CONTROL
@ Executive
Definition: ketypes.h:415
unsigned char UCHAR
Definition: xmlstorage.h:181