Storage Class Driver's ClaimDevice Routine

The ClaimDevice routine, which claims a storage device, is typically called from a Storage Class Driver's AddDevice Routine.

To claim a storage device, a class driver gets a reference to a device object by calling IoGetAttachedDeviceReference with the PDO passed to the class driver in the AddDevice call, then either calls an internal ClaimDevice routine from its AddDevice routine or implements the same functionality inline. A ClaimDevice routine sets up an SRB with the Function value SRB_FUNCTION_CLAIM_DEVICE and sends it to the device object returned by the class driver's call to IoGetAttachedDeviceReference.

The ClaimDevice routine allocates an IRP with IoBuildDeviceIoControlRequest, setting up the port driver's I/O stack location with the I/O control code IOCTL_SCSI_EXECUTE_NONE and a pointer to the SRB at Parameters.Scsi.Srb. ClaimDevice also must set up an event object with KeInitializeEvent so it can wait for the completion of the IRP. Then, it sends the IRP on to the next-lower driver with IoCallDriver.

When the IRP completes, ClaimDevice should release the reference to the device object returned by IoGetAttachedDeviceReference.

A ClaimDevice routine can serve double duty as a routine to be called from a class driver's RemoveDevice routine, or from AddDevice if the driver succeeds in claiming the device but cannot create a device object. In such cases, ClaimDevice sends an SRB with the Function value SRB_FUNCTION_RELEASE_DEVICE.