ReactOS 0.4.16-dev-297-gc569aee
misc.c
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS Universal Serial Bus Bulk Storage Driver
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: drivers/usb/usbstor/misc.c
5 * PURPOSE: USB block storage device driver.
6 * PROGRAMMERS:
7 * James Tabor
8 * Michael Martin (michael.martin@reactos.org)
9 * Johannes Anderwald (johannes.anderwald@reactos.org)
10 */
11
12#include "usbstor.h"
13
14#define NDEBUG
15#include <debug.h>
16
17//
18// driver verifier
19//
20IO_COMPLETION_ROUTINE SyncForwardIrpCompletionRoutine;
21
26 PIRP Irp,
28{
29 if (Irp->PendingReturned)
30 {
32 }
34}
35
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}
84
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}
166
170 OUT PURB UrbRequest)
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}
244
245PVOID
249{
250 //
251 // allocate, zero and return item
252 //
254}
255
256VOID
258 IN PVOID Item)
259{
260 //
261 // free item
262 //
264}
265
269 IN PFDO_DEVICE_EXTENSION DeviceExtension,
272 IN ULONG TransferFlags,
273 IN ULONG TransferBufferLength,
274 IN PVOID TransferBuffer)
275
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}
318
322 IN PFDO_DEVICE_EXTENSION DeviceExtension)
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}
388
392 IN PFDO_DEVICE_EXTENSION DeviceExtension)
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}
407
412 OUT PUCHAR MediumTypeCode)
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}
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 STATUS_PENDING
Definition: d3dkmdt.h:43
#define STATUS_NOT_SUPPORTED
Definition: d3dkmdt.h:48
#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:33
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
NTSTATUS NTAPI USBSTOR_SyncForwardIrp(PDEVICE_OBJECT DeviceObject, PIRP Irp)
Definition: misc.c:38
#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
FORCEINLINE PVOID ExAllocatePoolZero(ULONG PoolType, SIZE_T NumberOfBytes, ULONG Tag)
Definition: precomp.h:45
__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
#define IoCopyCurrentIrpStackLocationToNext(Irp)
Definition: ntifs_ex.h:413
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
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:73
struct _IO_STACK_LOCATION::@1579::@1580 DeviceIoControl
struct _IO_STACK_LOCATION::@3978::@4017 Others
union _IO_STACK_LOCATION::@1579 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 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