ReactOS 0.4.15-dev-5666-gc548b97
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 250 of file misc.c.

253{
254 //
255 // allocate item
256 //
258
259 if (Item)
260 {
261 //
262 // zero item
263 //
265 }
266
267 //
268 // return element
269 //
270 return Item;
271}
#define ExAllocatePoolWithTag(hernya, size, tag)
Definition: env_spec_w32.h:350
static UCHAR ItemSize[4]
Definition: parser.c:16
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:262
#define USB_STOR_TAG
Definition: usbstor.h:10
_In_ WDFCOLLECTION _In_ WDFOBJECT Item
_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 274 of file misc.c.

276{
277 //
278 // free item
279 //
281}
#define ExFreePoolWithTag(_P, _T)
Definition: module.h:1099

◆ 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 284 of file misc.c.

293{
294 PURB Urb;
296
297 //
298 // first allocate urb
299 //
301 if (!Urb)
302 {
303 //
304 // no memory
305 //
307 }
308
309 //
310 // initialize vendor request
311 //
314 Urb->UrbControlVendorClassRequest.TransferFlags = TransferFlags;
315 Urb->UrbControlVendorClassRequest.TransferBufferLength = TransferBufferLength;
316 Urb->UrbControlVendorClassRequest.TransferBuffer = TransferBuffer;
319
320 //
321 // submit request
322 //
324
325 //
326 // free urb
327 //
328 FreeItem(Urb);
329
330 //
331 // done
332 //
333 return Status;
334}
LONG NTSTATUS
Definition: precomp.h:26
PVOID AllocateItem(IN POOL_TYPE PoolType, IN SIZE_T NumberOfBytes)
Definition: misc.c:30
NTSTATUS USBSTOR_SyncUrbRequest(IN PDEVICE_OBJECT DeviceObject, OUT PURB UrbRequest)
Definition: misc.c:84
VOID FreeItem(IN PVOID Item)
Definition: misc.c:140
#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 88 of file misc.c.

91{
94 PIRP Irp;
97
98 //
99 // sanity checks
100 //
102 ASSERT(BusInterface);
103
104
105 //
106 // initialize event
107 //
109
110
111 //
112 // create irp
113 //
116 NULL,
117 0,
118 NULL,
119 &Event,
120 &IoStatus);
121
122 //
123 // was irp built
124 //
125 if (Irp == NULL)
126 {
127 //
128 // no memory
129 //
131 }
132
133 //
134 // initialize request
135 //
137 Stack->MajorFunction = IRP_MJ_PNP;
138 Stack->MinorFunction = IRP_MN_QUERY_INTERFACE;
139 Stack->Parameters.QueryInterface.Size = sizeof(BUS_INTERFACE_STANDARD);
140 Stack->Parameters.QueryInterface.InterfaceType = (LPGUID)&USB_BUS_INTERFACE_USBDI_GUID;
141 Stack->Parameters.QueryInterface.Version = 2;
142 Stack->Parameters.QueryInterface.Interface = (PINTERFACE)BusInterface;
143 Stack->Parameters.QueryInterface.InterfaceSpecificData = NULL;
144 Irp->IoStatus.Status = STATUS_NOT_SUPPORTED;
145
146 //
147 // call driver
148 //
150
151 //
152 // did operation complete
153 //
154 if (Status == STATUS_PENDING)
155 {
156 //
157 // wait for completion
158 //
160
161 //
162 // collect status
163 //
164 Status=IoStatus.Status;
165 }
166
167 return Status;
168}
#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:403

◆ USBSTOR_GetMaxLUN()

NTSTATUS USBSTOR_GetMaxLUN ( IN PDEVICE_OBJECT  DeviceObject,
IN PFDO_DEVICE_EXTENSION  DeviceExtension 
)

Definition at line 338 of file misc.c.

341{
344
345 //
346 // allocate 1-byte buffer
347 //
349 if (!Buffer)
350 {
351 //
352 // no memory
353 //
356 }
357
358 //
359 // execute request
360 //
361 Status = USBSTOR_ClassRequest(DeviceObject, DeviceExtension, USB_BULK_GET_MAX_LUN, DeviceExtension->InterfaceInformation->InterfaceNumber, USBD_TRANSFER_DIRECTION_IN, sizeof(UCHAR), Buffer);
362
363 DPRINT("MaxLUN: %x\n", *Buffer);
364
365 if (NT_SUCCESS(Status))
366 {
367 if (*Buffer > 0xF)
368 {
369 //
370 // invalid response documented in usb mass storage specification
371 //
373 }
374 else
375 {
376 //
377 // store maxlun
378 //
379 DeviceExtension->MaxLUN = *Buffer;
380 }
381 }
382 else
383 {
384 //
385 // "USB Mass Storage Class. Bulk-Only Transport. Revision 1.0"
386 // 3.2 Get Max LUN (class-specific request) :
387 // Devices that do not support multiple LUNs may STALL this command.
388 //
389 USBSTOR_ResetDevice(DeviceExtension->LowerDeviceObject, DeviceExtension);
390
391 DeviceExtension->MaxLUN = 0;
393 }
394
395 //
396 // free buffer
397 //
399
400 //
401 // done
402 //
403 return Status;
404
405}
Definition: bufpool.h:45
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
NTSTATUS USBSTOR_ResetDevice(IN PDEVICE_OBJECT DeviceObject, IN PFDO_DEVICE_EXTENSION DeviceExtension)
Definition: misc.c:227
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:147
unsigned char * PUCHAR
Definition: retypes.h:3
#define STATUS_SUCCESS
Definition: shellext.h:65
#define DPRINT
Definition: sndvol32.h:71
#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:54
unsigned char UCHAR
Definition: xmlstorage.h:181

◆ USBSTOR_IsFloppy()

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

Definition at line 427 of file misc.c.

431{
432 PUFI_CAPACITY_FORMAT_HEADER FormatHeader;
434 ULONG Length, Index, BlockCount, BlockLength;
435
436 //
437 // get format header
438 //
439 FormatHeader = (PUFI_CAPACITY_FORMAT_HEADER)Buffer;
440
441 //
442 // sanity checks
443 //
444 ASSERT(FormatHeader->Reserved1 == 0x00);
445 ASSERT(FormatHeader->Reserved2 == 0x00);
446 ASSERT(FormatHeader->Reserved3 == 0x00);
447
448 //
449 // is there capacity data
450 //
451 if (!FormatHeader->CapacityLength)
452 {
453 //
454 // no data provided
455 //
456 DPRINT1("[USBSTOR] No capacity length\n");
457 return FALSE;
458 }
459
460 //
461 // the format header are always 8 bytes in length
462 //
463 ASSERT((FormatHeader->CapacityLength & 0x7) == 0);
464 DPRINT1("CapacityLength %x\n", FormatHeader->CapacityLength);
465
466 //
467 // grab length and locate first descriptor
468 //
469 Length = FormatHeader->CapacityLength;
470 Descriptor = (PUFI_CAPACITY_DESCRIPTOR)(FormatHeader + 1);
471 for(Index = 0; Index < Length / sizeof(UFI_CAPACITY_DESCRIPTOR); Index++)
472 {
473 //
474 // blocks are little endian format
475 //
476 BlockCount = NTOHL(Descriptor->BlockCount);
477
478 //
479 // get block length
480 //
481 BlockLength = NTOHL((Descriptor->BlockLengthByte0 << 24 | Descriptor->BlockLengthByte1 << 16 | Descriptor->BlockLengthByte2 << 8));
482
483 DPRINT1("BlockCount %x BlockLength %x Code %x\n", BlockCount, BlockLength, Descriptor->Code);
484
485 if (BlockLength == 512 && BlockCount == 1440)
486 {
487 //
488 // 720 KB DD
489 //
490 *MediumTypeCode = 0x1E;
491 return TRUE;
492 }
493 else if (BlockLength == 1024 && BlockCount == 1232)
494 {
495 //
496 // 1,25 MB
497 //
498 *MediumTypeCode = 0x93;
499 return TRUE;
500 }
501 else if (BlockLength == 512 && BlockCount == 2880)
502 {
503 //
504 // 1,44MB KB DD
505 //
506 *MediumTypeCode = 0x94;
507 return TRUE;
508 }
509
510 //
511 // move to next descriptor
512 //
513 Descriptor = (Descriptor + 1);
514 }
515
516 //
517 // no floppy detected
518 //
519 return FALSE;
520}
#define DPRINT1
Definition: precomp.h:8
#define TRUE
Definition: types.h:120
unsigned int ULONG
Definition: retypes.h:1
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:102
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 408 of file misc.c.

411{
413
414 //
415 // execute request
416 //
417 Status = USBSTOR_ClassRequest(DeviceObject, DeviceExtension, USB_BULK_RESET_DEVICE, DeviceExtension->InterfaceInformation->InterfaceNumber, USBD_TRANSFER_DIRECTION_OUT, 0, NULL);
418
419 //
420 // done
421 //
422 return Status;
423
424}
#define USBD_TRANSFER_DIRECTION_OUT
Definition: usb.h:159
#define USB_BULK_RESET_DEVICE
Definition: usbstor.h:55

◆ 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 //
60 // call driver
61 //
63
64 //
65 // check if pending
66 //
68 {
69 //
70 // wait for the request to finish
71 //
73
74 //
75 // copy status code
76 //
77 Status = Irp->IoStatus.Status;
78 }
79
80 //
81 // done
82 //
83 return Status;
84}
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:498
#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 171 of file misc.c.

174{
175 PIRP Irp;
176 PIO_STACK_LOCATION IoStack;
179
180 //
181 // allocate irp
182 //
183 Irp = IoAllocateIrp(DeviceObject->StackSize, FALSE);
184 if (!Irp)
185 {
186 //
187 // no memory
188 //
190 }
191
192 //
193 // initialize event
194 //
196
197
198 //
199 // get next stack location
200 //
202
203 //
204 // initialize stack location
205 //
207 IoStack->Parameters.DeviceIoControl.IoControlCode = IOCTL_INTERNAL_USB_SUBMIT_URB;
208 IoStack->Parameters.Others.Argument1 = (PVOID)UrbRequest;
209 IoStack->Parameters.DeviceIoControl.InputBufferLength = UrbRequest->UrbHeader.Length;
210 Irp->IoStatus.Status = STATUS_SUCCESS;
211
212 //
213 // setup completion routine
214 //
216
217 //
218 // call driver
219 //
221
222 //
223 // check if request is pending
224 //
225 if (Status == STATUS_PENDING)
226 {
227 //
228 // wait for completion
229 //
231
232 //
233 // update status
234 //
235 Status = Irp->IoStatus.Status;
236 }
237
238 //
239 // free irp
240 //
241 IoFreeIrp(Irp);
242
243 //
244 // done
245 //
246 return Status;
247}
void * PVOID
Definition: retypes.h:9
PIRP NTAPI IoAllocateIrp(IN CCHAR StackSize, IN BOOLEAN ChargeQuota)
Definition: irp.c:615
VOID NTAPI IoFreeIrp(IN PIRP Irp)
Definition: irp.c:1666
struct _NAMED_PIPE_CREATE_PARAMETERS * Parameters
Definition: iotypes.h:3128
#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.