ReactOS 0.4.16-dev-109-gf4cb10f
misc.c File Reference
#include "usbstor.h"
#include <debug.h>
Include dependency graph for misc.c:

Go to the source code of this file.

Macros

#define NDEBUG
 

Functions

NTSTATUS NTAPI USBSTOR_SyncForwardIrpCompletionRoutine (PDEVICE_OBJECT DeviceObject, PIRP Irp, PVOID Context)
 
NTSTATUS NTAPI USBSTOR_SyncForwardIrp (PDEVICE_OBJECT DeviceObject, PIRP Irp)
 
NTSTATUS NTAPI USBSTOR_GetBusInterface (IN PDEVICE_OBJECT DeviceObject, OUT PUSB_BUS_INTERFACE_USBDI_V2 BusInterface)
 
NTSTATUS USBSTOR_SyncUrbRequest (IN PDEVICE_OBJECT DeviceObject, OUT PURB UrbRequest)
 
PVOID AllocateItem (IN POOL_TYPE PoolType, IN ULONG ItemSize)
 
VOID FreeItem (IN PVOID Item)
 
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)
 
NTSTATUS USBSTOR_GetMaxLUN (IN PDEVICE_OBJECT DeviceObject, IN PFDO_DEVICE_EXTENSION DeviceExtension)
 
NTSTATUS USBSTOR_ResetDevice (IN PDEVICE_OBJECT DeviceObject, IN PFDO_DEVICE_EXTENSION DeviceExtension)
 
BOOLEAN USBSTOR_IsFloppy (IN PUCHAR Buffer, IN ULONG BufferLength, OUT PUCHAR MediumTypeCode)
 

Variables

IO_COMPLETION_ROUTINE SyncForwardIrpCompletionRoutine
 

Macro Definition Documentation

◆ NDEBUG

#define NDEBUG

Definition at line 14 of file misc.c.

Function Documentation

◆ AllocateItem()

PVOID AllocateItem ( IN POOL_TYPE  PoolType,
IN ULONG  ItemSize 
)

Definition at line 246 of file misc.c.

249{
250 //
251 // allocate, zero and return item
252 //
254}
FORCEINLINE PVOID ExAllocatePoolZero(ULONG PoolType, SIZE_T NumberOfBytes, ULONG Tag)
Definition: precomp.h:45
static UCHAR ItemSize[4]
Definition: parser.c:16
#define USB_STOR_TAG
Definition: usbstor.h:11
_Must_inspect_result_ _In_ WDFDEVICE _In_ DEVICE_REGISTRY_PROPERTY _In_ _Strict_type_match_ POOL_TYPE PoolType
Definition: wdfdevice.h:3815

◆ FreeItem()

VOID FreeItem ( IN PVOID  Item)

Definition at line 257 of file misc.c.

259{
260 //
261 // free item
262 //
264}
#define ExFreePoolWithTag(_P, _T)
Definition: module.h:1109
_In_ WDFCOLLECTION _In_ WDFOBJECT Item

◆ USBSTOR_ClassRequest()

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 at line 267 of file misc.c.

276{
277 PURB Urb;
279
280 //
281 // first allocate urb
282 //
284 if (!Urb)
285 {
286 //
287 // no memory
288 //
290 }
291
292 //
293 // initialize vendor request
294 //
297 Urb->UrbControlVendorClassRequest.TransferFlags = TransferFlags;
298 Urb->UrbControlVendorClassRequest.TransferBufferLength = TransferBufferLength;
299 Urb->UrbControlVendorClassRequest.TransferBuffer = TransferBuffer;
302
303 //
304 // submit request
305 //
307
308 //
309 // free urb
310 //
311 FreeItem(Urb);
312
313 //
314 // done
315 //
316 return Status;
317}
LONG NTSTATUS
Definition: precomp.h:26
PVOID AllocateItem(IN POOL_TYPE PoolType, IN SIZE_T NumberOfBytes)
Definition: misc.c:29
NTSTATUS USBSTOR_SyncUrbRequest(IN PDEVICE_OBJECT DeviceObject, OUT PURB UrbRequest)
Definition: misc.c:83
VOID FreeItem(IN PVOID Item)
Definition: misc.c:132
#define NonPagedPool
Definition: env_spec_w32.h:307
Status
Definition: gdiplustypes.h:25
Definition: usb.h:529
struct _URB_CONTROL_VENDOR_OR_CLASS_REQUEST UrbControlVendorClassRequest
Definition: usb.h:548
#define STATUS_INSUFFICIENT_RESOURCES
Definition: udferr_usr.h:158
#define URB_FUNCTION_CLASS_INTERFACE
Definition: usb.h:113
struct _URB * PURB
_In_ WDFCOLLECTION _In_ ULONG Index
_In_ PDEVICE_OBJECT DeviceObject
Definition: wdfdevice.h:2055
_Must_inspect_result_ _In_ WDFDEVICE _In_ WDFQUEUE _In_ _Strict_type_match_ WDF_REQUEST_TYPE RequestType
Definition: wdfdevice.h:4233

◆ USBSTOR_GetBusInterface()

NTSTATUS NTAPI USBSTOR_GetBusInterface ( IN PDEVICE_OBJECT  DeviceObject,
OUT PUSB_BUS_INTERFACE_USBDI_V2  BusInterface 
)

Definition at line 87 of file misc.c.

90{
93 PIRP Irp;
96
97 //
98 // sanity checks
99 //
101 ASSERT(BusInterface);
102
103 //
104 // initialize event
105 //
107
108 //
109 // create irp
110 //
113 NULL,
114 0,
115 NULL,
116 &Event,
117 &IoStatus);
118
119 //
120 // was irp built
121 //
122 if (Irp == NULL)
123 {
124 //
125 // no memory
126 //
128 }
129
130 //
131 // initialize request
132 //
134 Stack->MajorFunction = IRP_MJ_PNP;
135 Stack->MinorFunction = IRP_MN_QUERY_INTERFACE;
136 Stack->Parameters.QueryInterface.Size = sizeof(BUS_INTERFACE_STANDARD);
137 Stack->Parameters.QueryInterface.InterfaceType = (LPGUID)&USB_BUS_INTERFACE_USBDI_GUID;
138 Stack->Parameters.QueryInterface.Version = 2;
139 Stack->Parameters.QueryInterface.Interface = (PINTERFACE)BusInterface;
140 Stack->Parameters.QueryInterface.InterfaceSpecificData = NULL;
141 Irp->IoStatus.Status = STATUS_NOT_SUPPORTED;
142
143 //
144 // call driver
145 //
147
148 //
149 // did operation complete
150 //
151 if (Status == STATUS_PENDING)
152 {
153 //
154 // wait for completion
155 //
157
158 //
159 // collect status
160 //
161 Status=IoStatus.Status;
162 }
163
164 return Status;
165}
#define IRP_MJ_PNP
Definition: cdrw_usr.h:52
_In_ PIRP Irp
Definition: csq.h:116
#define NULL
Definition: types.h:112
#define FALSE
Definition: types.h:117
#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 ASSERT(a)
Definition: mode.c:44
__in UCHAR __in POWER_STATE __in_opt PVOID __in PIO_STATUS_BLOCK IoStatus
Definition: mxum.h:159
#define KernelMode
Definition: asm.h:34
@ 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
#define IoCallDriver
Definition: irp.c:1225
#define STATUS_PENDING
Definition: ntstatus.h:82
#define STATUS_NOT_SUPPORTED
Definition: ntstatus.h:423
GUID * LPGUID
Definition: guiddef.h:81
struct _INTERFACE * PINTERFACE
_In_ WDFREQUEST _In_ PIO_STACK_LOCATION Stack
Definition: wdfrequest.h:639
__drv_aliasesMem FORCEINLINE PIO_STACK_LOCATION IoGetNextIrpStackLocation(_In_ PIRP Irp)
Definition: iofuncs.h:2695
#define IRP_MN_QUERY_INTERFACE
struct _BUS_INTERFACE_STANDARD BUS_INTERFACE_STANDARD
@ Executive
Definition: ketypes.h:415

◆ USBSTOR_GetMaxLUN()

NTSTATUS USBSTOR_GetMaxLUN ( IN PDEVICE_OBJECT  DeviceObject,
IN PFDO_DEVICE_EXTENSION  DeviceExtension 
)

Definition at line 320 of file misc.c.

323{
326
327 //
328 // allocate 1-byte buffer
329 //
331 if (!Buffer)
332 {
333 //
334 // no memory
335 //
338 }
339
340 //
341 // execute request
342 //
343 Status = USBSTOR_ClassRequest(DeviceObject, DeviceExtension, USB_BULK_GET_MAX_LUN, DeviceExtension->InterfaceInformation->InterfaceNumber, USBD_TRANSFER_DIRECTION_IN, sizeof(UCHAR), Buffer);
344
345 DPRINT("MaxLUN: %x\n", *Buffer);
346
347 if (NT_SUCCESS(Status))
348 {
349 if (*Buffer > 0xF)
350 {
351 //
352 // invalid response documented in usb mass storage specification
353 //
355 }
356 else
357 {
358 //
359 // store maxlun
360 //
361 DeviceExtension->MaxLUN = *Buffer;
362 }
363 }
364 else
365 {
366 //
367 // "USB Mass Storage Class. Bulk-Only Transport. Revision 1.0"
368 // 3.2 Get Max LUN (class-specific request) :
369 // Devices that do not support multiple LUNs may STALL this command.
370 //
371 USBSTOR_ResetDevice(DeviceExtension->LowerDeviceObject, DeviceExtension);
372
373 DeviceExtension->MaxLUN = 0;
375 }
376
377 //
378 // free buffer
379 //
381
382 //
383 // done
384 //
385 return Status;
386
387}
Definition: bufpool.h:45
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:33
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
#define STATUS_SUCCESS
Definition: shellext.h:65
#define DPRINT
Definition: sndvol32.h:73
unsigned char * PUCHAR
Definition: typedefs.h:53
#define STATUS_DEVICE_DATA_ERROR
Definition: udferr_usr.h:159
#define USBD_TRANSFER_DIRECTION_IN
Definition: usb.h:160
#define USB_BULK_GET_MAX_LUN
Definition: usbstor.h:55
unsigned char UCHAR
Definition: xmlstorage.h:181

◆ USBSTOR_IsFloppy()

BOOLEAN USBSTOR_IsFloppy ( IN PUCHAR  Buffer,
IN ULONG  BufferLength,
OUT PUCHAR  MediumTypeCode 
)

Definition at line 409 of file misc.c.

413{
414 PUFI_CAPACITY_FORMAT_HEADER FormatHeader;
416 ULONG Length, Index, BlockCount, BlockLength;
417
418 //
419 // get format header
420 //
421 FormatHeader = (PUFI_CAPACITY_FORMAT_HEADER)Buffer;
422
423 //
424 // sanity checks
425 //
426 ASSERT(FormatHeader->Reserved1 == 0x00);
427 ASSERT(FormatHeader->Reserved2 == 0x00);
428 ASSERT(FormatHeader->Reserved3 == 0x00);
429
430 //
431 // is there capacity data
432 //
433 if (!FormatHeader->CapacityLength)
434 {
435 //
436 // no data provided
437 //
438 DPRINT1("[USBSTOR] No capacity length\n");
439 return FALSE;
440 }
441
442 //
443 // the format header are always 8 bytes in length
444 //
445 ASSERT((FormatHeader->CapacityLength & 0x7) == 0);
446 DPRINT1("CapacityLength %x\n", FormatHeader->CapacityLength);
447
448 //
449 // grab length and locate first descriptor
450 //
451 Length = FormatHeader->CapacityLength;
452 Descriptor = (PUFI_CAPACITY_DESCRIPTOR)(FormatHeader + 1);
453 for(Index = 0; Index < Length / sizeof(UFI_CAPACITY_DESCRIPTOR); Index++)
454 {
455 //
456 // blocks are little endian format
457 //
458 BlockCount = NTOHL(Descriptor->BlockCount);
459
460 //
461 // get block length
462 //
463 BlockLength = NTOHL((Descriptor->BlockLengthByte0 << 24 | Descriptor->BlockLengthByte1 << 16 | Descriptor->BlockLengthByte2 << 8));
464
465 DPRINT1("BlockCount %x BlockLength %x Code %x\n", BlockCount, BlockLength, Descriptor->Code);
466
467 if (BlockLength == 512 && BlockCount == 1440)
468 {
469 //
470 // 720 KB DD
471 //
472 *MediumTypeCode = 0x1E;
473 return TRUE;
474 }
475 else if (BlockLength == 1024 && BlockCount == 1232)
476 {
477 //
478 // 1,25 MB
479 //
480 *MediumTypeCode = 0x93;
481 return TRUE;
482 }
483 else if (BlockLength == 512 && BlockCount == 2880)
484 {
485 //
486 // 1,44MB KB DD
487 //
488 *MediumTypeCode = 0x94;
489 return TRUE;
490 }
491
492 //
493 // move to next descriptor
494 //
495 Descriptor = (Descriptor + 1);
496 }
497
498 //
499 // no floppy detected
500 //
501 return FALSE;
502}
#define DPRINT1
Definition: precomp.h:8
#define TRUE
Definition: types.h:120
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:102
uint32_t ULONG
Definition: typedefs.h:59
struct UFI_CAPACITY_FORMAT_HEADER * PUFI_CAPACITY_FORMAT_HEADER
struct UFI_CAPACITY_DESCRIPTOR * PUFI_CAPACITY_DESCRIPTOR
#define NTOHL(n)
Definition: usbstor.h:22
_Must_inspect_result_ _In_ WDFIORESLIST _In_ PIO_RESOURCE_DESCRIPTOR Descriptor
Definition: wdfresource.h:342

Referenced by USBSTOR_SendFormatCapacityIrp().

◆ USBSTOR_ResetDevice()

NTSTATUS USBSTOR_ResetDevice ( IN PDEVICE_OBJECT  DeviceObject,
IN PFDO_DEVICE_EXTENSION  DeviceExtension 
)

Definition at line 390 of file misc.c.

393{
395
396 //
397 // execute request
398 //
399 Status = USBSTOR_ClassRequest(DeviceObject, DeviceExtension, USB_BULK_RESET_DEVICE, DeviceExtension->InterfaceInformation->InterfaceNumber, USBD_TRANSFER_DIRECTION_OUT, 0, NULL);
400
401 //
402 // done
403 //
404 return Status;
405
406}
#define USBD_TRANSFER_DIRECTION_OUT
Definition: usb.h:159
#define USB_BULK_RESET_DEVICE
Definition: usbstor.h:56

◆ USBSTOR_SyncForwardIrp()

NTSTATUS NTAPI USBSTOR_SyncForwardIrp ( PDEVICE_OBJECT  DeviceObject,
PIRP  Irp 
)

Definition at line 38 of file misc.c.

39{
42
43 //
44 // initialize event
45 //
47
48 //
49 // copy irp stack location
50 //
52
53 //
54 // set completion routine
55 //
57
58 //
59 // call driver
60 //
62
63 //
64 // check if pending
65 //
67 {
68 //
69 // wait for the request to finish
70 //
72
73 //
74 // copy status code
75 //
76 Status = Irp->IoStatus.Status;
77 }
78
79 //
80 // done
81 //
82 return Status;
83}
NTSTATUS NTAPI USBSTOR_SyncForwardIrpCompletionRoutine(PDEVICE_OBJECT DeviceObject, PIRP Irp, PVOID Context)
Definition: misc.c:22
#define IoSetCompletionRoutine(_Irp, _CompletionRoutine, _Context, _InvokeOnSuccess, _InvokeOnError, _InvokeOnCancel)
Definition: irp.cpp:490
#define IoCopyCurrentIrpStackLocationToNext(Irp)
Definition: ntifs_ex.h:413

Referenced by USBSTOR_FdoHandleDeviceRelations(), USBSTOR_FdoHandleStartDevice(), and USBSTOR_PdoHandlePnp().

◆ USBSTOR_SyncForwardIrpCompletionRoutine()

NTSTATUS NTAPI USBSTOR_SyncForwardIrpCompletionRoutine ( PDEVICE_OBJECT  DeviceObject,
PIRP  Irp,
PVOID  Context 
)

Definition at line 24 of file misc.c.

28{
29 if (Irp->PendingReturned)
30 {
32 }
34}
#define KeSetEvent(pEvt, foo, foo2)
Definition: env_spec_w32.h:476
#define STATUS_MORE_PROCESSING_REQUIRED
Definition: shellext.h:68
#define IO_NO_INCREMENT
Definition: iotypes.h:598

◆ USBSTOR_SyncUrbRequest()

NTSTATUS USBSTOR_SyncUrbRequest ( IN PDEVICE_OBJECT  DeviceObject,
OUT PURB  UrbRequest 
)

Definition at line 168 of file misc.c.

171{
172 PIRP Irp;
173 PIO_STACK_LOCATION IoStack;
176
177 //
178 // allocate irp
179 //
180 Irp = IoAllocateIrp(DeviceObject->StackSize, FALSE);
181 if (!Irp)
182 {
183 //
184 // no memory
185 //
187 }
188
189 //
190 // initialize event
191 //
193
194 //
195 // get next stack location
196 //
198
199 //
200 // initialize stack location
201 //
204 IoStack->Parameters.Others.Argument1 = (PVOID)UrbRequest;
205 IoStack->Parameters.DeviceIoControl.InputBufferLength = UrbRequest->UrbHeader.Length;
206 Irp->IoStatus.Status = STATUS_SUCCESS;
207
208 //
209 // setup completion routine
210 //
212
213 //
214 // call driver
215 //
217
218 //
219 // check if request is pending
220 //
221 if (Status == STATUS_PENDING)
222 {
223 //
224 // wait for completion
225 //
227
228 //
229 // update status
230 //
231 Status = Irp->IoStatus.Status;
232 }
233
234 //
235 // free irp
236 //
237 IoFreeIrp(Irp);
238
239 //
240 // done
241 //
242 return Status;
243}
PIRP NTAPI IoAllocateIrp(IN CCHAR StackSize, IN BOOLEAN ChargeQuota)
Definition: irp.c:615
VOID NTAPI IoFreeIrp(IN PIRP Irp)
Definition: irp.c:1666
struct _IO_STACK_LOCATION::@1575::@1576 DeviceIoControl
union _IO_STACK_LOCATION::@1575 Parameters
struct _IO_STACK_LOCATION::@3974::@4013 Others
void * PVOID
Definition: typedefs.h:50
#define IOCTL_INTERNAL_USB_SUBMIT_URB
Definition: usbioctl.h:32
#define IRP_MJ_INTERNAL_DEVICE_CONTROL

Variable Documentation

◆ SyncForwardIrpCompletionRoutine

IO_COMPLETION_ROUTINE SyncForwardIrpCompletionRoutine

Definition at line 20 of file misc.c.