PageRenderTime 31ms CodeModel.GetById 16ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/packages/cdrom/src/wnaspi32.pp

https://github.com/slibre/freepascal
Puppet | 259 lines | 238 code | 21 blank | 0 comment | 2 complexity | 5f1fcbb87af9441a73479e527dc1eb34 MD5 | raw file
Possible License(s): LGPL-2.0, LGPL-2.1, LGPL-3.0
  1unit wnaspi32;
  2
  3{$mode objfpc}
  4
  5interface
  6
  7uses Windows;
  8
  9type
 10  LPSRB = Pointer;
 11
 12const
 13{ $packrecords c}
 14
 15  SENSE_LEN                 = 14;  // Default sense buffer length
 16  SRB_DIR_SCSI              = $00; // Direction determined by SCSI command
 17  SRB_DIR_IN                = $08; // Transfer from SCSI target to host
 18  SRB_DIR_OUT               = $10; // Transfer from host to SCSI target
 19  SRB_POSTING               = $01; // Enable ASPI posting
 20  SRB_EVENT_NOTIFY          = $40; // Enable ASPI event notification
 21  SRB_ENABLE_RESIDUAL_COUNT = $04; // Enable residual byte count reporting
 22  SRB_DATA_SG_LIST          = $02; // Data buffer points to scatter-gather list
 23
 24  WM_ASPIPOST               = $4D42; // ASPI Post message
 25
 26{ ---------------------------------------------------------------------
 27  ASPI Command Definitions
 28  ---------------------------------------------------------------------}
 29
 30  SC_HA_INQUIRY     = $00; // Host adapter inquiry
 31  SC_GET_DEV_TYPE   = $01; // Get device type
 32  SC_EXEC_SCSI_CMD  = $02; // Execute SCSI command
 33  SC_ABORT_SRB      = $03; // Abort an SRB
 34  SC_RESET_DEV      = $04; // SCSI bus device reset
 35  SC_GET_DISK_INFO  = $06; // Get Disk information
 36
 37{ ---------------------------------------------------------------------
 38  SRB Status
 39  ---------------------------------------------------------------------}
 40
 41  SS_PENDING        = $00; // SRB being processed
 42  SS_COMP           = $01; // SRB completed without error
 43  SS_ABORTED        = $02; // SRB aborted
 44  SS_ABORT_FAIL     = $03; // Unable to abort SRB
 45  SS_ERR            = $04; // SRB completed with error
 46
 47  SS_INVALID_CMD    = $80; // Invalid ASPI command
 48  SS_INVALID_HA     = $81; // Invalid host adapter number
 49  SS_NO_DEVICE      = $82; // SCSI device not installed
 50
 51  SS_INVALID_SRB    = $E0; // Invalid parameter set in SRB
 52  SS_FAILED_INIT    = $E4; // ASPI for windows failed init
 53  SS_ASPI_IS_BUSY   = $E5; // No resources available to execute cmd
 54  SS_BUFFER_TO_BIG  = $E6; // Buffer size to big to handle!
 55  SS_NO_ADAPTERS    = $E8; // No adapters.
 56
 57{ ---------------------------------------------------------------------
 58    Host Adapter Status
 59  ---------------------------------------------------------------------}
 60
 61  HASTAT_OK                   = $00; // Host adapter did not detect an                                                                                                                  // error
 62  HASTAT_SEL_TO               = $11; // Selection Timeout
 63  HASTAT_DO_DU                = $12; // Data overrun data underrun
 64  HASTAT_BUS_FREE             = $13; // Unexpected bus free
 65  HASTAT_PHASE_ERR            = $14; // Target bus phase sequence                                                                                                                               // failure
 66  HASTAT_TIMEOUT              = $09; // Timed out while SRB was                                                                                                                                         waiting to beprocessed.
 67  HASTAT_COMMAND_TIMEOUT      = $0B; // While processing the SRB, the adapter timed out.
 68  HASTAT_MESSAGE_REJECT       = $0D; // While processing SRB, the                                                                                                                               // adapter received a MESSAGE                                                                                                                   // REJECT.
 69  HASTAT_BUS_RESET            = $0E; // A bus reset was detected.
 70  HASTAT_PARITY_ERROR         = $0F; // A parity error was detected.
 71  HASTAT_REQUEST_SENSE_FAILED = $10; // The adapter failed in issuing
 72
 73{ ---------------------------------------------------------------------
 74    SRB - HOST ADAPTER INQUIRY - SC_HA_INQUIRY
 75  ---------------------------------------------------------------------}
 76
 77type
 78
 79  SRB_HAInquiry = record
 80    SRB_Cmd : Byte;                       // ASPI command code = SC_HA_INQUIRY
 81    SRB_Status : Byte;                    // ASPI command status byte
 82    SRB_HaId : Byte;                      // ASPI host adapter number
 83    SRB_Flags : Byte;                     // ASPI request flags
 84    SRB_Hdr_Rsvd : DWORD;                 // Reserved, MUST = 0
 85    HA_Count : Byte;                      // Number of host adapters present
 86    HA_SCSI_ID : Byte;                    // SCSI ID of host adapter
 87    HA_ManagerId : array[0..15] of Byte;  // String describing the manager
 88    HA_Identifier : array[0..15] of Byte; // String describing the host adapter
 89    HA_Unique : array[0..15] of Byte;     // Host Adapter Unique parameters
 90    HA_Rsvd1 : Word;
 91  end;
 92
 93  PSRB_HAInquiry = ^SRB_HAInquiry;
 94  TSRB_HAInquiry = SRB_HAInquiry;
 95
 96{ ---------------------------------------------------------------------
 97     SRB - GET DEVICE TYPE - SC_GET_DEV_TYPE
 98  ---------------------------------------------------------------------}
 99
100  SRB_GDEVBlock = record
101    SRB_Cmd,              // ASPI command code = SC_GET_DEV_TYPE
102    SRB_Status,           // ASPI command status byte
103    SRB_HaId,             // ASPI host adapter number
104    SRB_Flags : BYTE;     // Reserved
105    SRB_Hdr_Rsvd : DWORD; // Reserved
106    SRB_Target,           // Target's SCSI ID
107    SRB_Lun,              // Target's LUN number
108    SRB_DeviceType,       // Target's peripheral device type
109    SRB_Rsvd1 : BYTE;
110  end;
111
112  TSRB_GDEVBlock = SRB_GDEVBlock;
113  PSRB_GDEVBlock = ^SRB_GDEVBlock;
114
115{ ---------------------------------------------------------------------
116    SRB - EXECUTE SCSI COMMAND - SC_EXEC_SCSI_CMD
117  ---------------------------------------------------------------------}
118
119  SRB_ExecSCSICmd = record
120    SRB_Cmd,                   // ASPI command code = SC_EXEC_SCSI_CMD
121    SRB_Status,                // ASPI command status byte
122    SRB_HaId,                  // ASPI host adapter number
123    SRB_Flags : BYTE;          // ASPI request flags
124    SRB_Hdr_Rsvd : DWORD;      // Reserved
125    SRB_Target,                // Target's SCSI ID
126    SRB_Lun : BYTE;            // Target's LUN number
127    SRB_Rsvd1 : WORD;          // Reserved for Alignment
128    SRB_BufLen : DWORD;        // Data Allocation Length
129    SRB_BufPointer : Pointer;  // Data Buffer Pointer
130    SRB_SenseLen,              // Sense Allocation Length
131    SRB_CDBLen,                // CDB Length
132    SRB_HaStat,                // Host Adapter Status
133    SRB_TargStat : BYTE;       // Target Status
134    SRB_PostProc,              // Post routine
135    SRB_Rsvd2 : Pointer;       // Reserved
136    SRB_Rsvd3,                      // Reserved for alignment
137    CDBByte : array[0..15] of byte; // SCSI CDB
138    SenseArea : array[0..SENSE_LEN + 1] of byte; // Request Sense buffer
139  end;
140
141  TSRB_ExecSCSICmd = SRB_ExecSCSICmd;
142  PSRB_ExecSCSICmd = ^SRB_ExecSCSICmd;
143
144
145{ ---------------------------------------------------------------------
146    SRB - ABORT AN SRB - SC_ABORT_SRB
147  ---------------------------------------------------------------------}
148
149  SRB_Abort = record
150    SRB_Cmd,               // ASPI command code = SC_EXEC_SCSI_CMD
151    SRB_Status,            // ASPI command status byte
152    SRB_HaId,              // ASPI host adapter number
153    SRB_Flags : BYTE;      // Reserved
154    SRB_Hdr_Rsvd : DWORD;  // Reserved
155    SRB_ToAbort : Pointer; // Pointer to SRB to abort
156  end;
157
158  TSRB_Abort = SRB_Abort;
159  PSRB_Abort = ^SRB_Abort;
160
161{ ---------------------------------------------------------------------
162    SRB - BUS DEVICE RESET - SC_RESET_DEV
163  ---------------------------------------------------------------------}
164
165  SRB_BusDeviceReset = record
166    SRB_Cmd,                          // ASPI command code = SC_EXEC_SCSI_CMD
167    SRB_Status,                       // ASPI command status byte
168    SRB_HaId,                         // ASPI host adapter number
169    SRB_Flags : BYTE;                 // Reserved
170    SRB_Hdr_Rsvd : DWORD;             // Reserved
171    SRB_Target,                       // Target's SCSI ID
172    SRB_Lun : BYTE;                   // Target's LUN number
173    SRB_Rsvd1 : array[0..11] of byte; // Reserved for Alignment
174    SRB_HaStat,                       // Host Adapter Status
175    SRB_TargStat : BYTE;              // Target Status
176    SRB_PostProc,                     // Post routine
177    SRB_Rsvd2 : Pointer;              // Reserved
178    SRB_Rsvd3,                        // Reserved
179    CDBByte : array[0..15] of byte;   // SCSI CDB
180  end;
181
182  TSRB_BusDeviceReset = SRB_BusDeviceReset;
183  PSRB_BusDeviceReset = ^SRB_BusDeviceReset;
184
185{ ---------------------------------------------------------------------
186     SRB - GET DISK INFORMATION - SC_GET_DISK_INFO
187  ---------------------------------------------------------------------}
188
189  SRB_GetDiskInfo = record
190    SRB_Cmd,              // ASPI command code = SC_EXEC_SCSI_CMD
191    SRB_Status,           // ASPI command status byte
192    SRB_HaId,             // ASPI host adapter number
193    SRB_Flags : BYTE;     // Reserved
194    SRB_Hdr_Rsvd : DWORD; // Reserved
195    SRB_Target,           // Target's SCSI ID
196    SRB_Lun,              // Target's LUN number
197    SRB_DriveFlags,       // Driver flags
198    SRB_Int13HDriveInfo,  // Host Adapter Status
199    SRB_Heads,            // Preferred number of heads translation
200    SRB_Sectors : BYTE;   // Preferred number of sectors translation
201    SRB_Rsvd1 : array[0..9] of byte; // Reserved
202  end;
203
204  TSRB_GetDiskInfo = SRB_GetDiskInfo;
205  PSRB_GetDiskInfo = ^SRB_GetDiskInfo;
206
207type
208  TSendASPI32Command = function( LPSRB : Pointer ) : DWORD; cdecl;
209  TGetASPI32SupportInfo = function : DWORD; cdecl;
210
211Const
212  SendASPI32Command : TSendASPI32Command = nil;
213  GetASPI32SupportInfo : TGetASPI32SupportInfo = nil;
214
215Function ASPILoaded : Boolean;
216Procedure CheckASPI;
217procedure UnloadASPI;
218
219implementation
220
221const
222  HWNASPI : THandle = 0;
223  WNASPI  : pchar = 'wnaspi32.dll';
224
225Function ASPILoaded : Boolean;
226
227begin
228  Result:=HWNASPI<>0;
229end;
230
231Procedure CheckASPI;
232
233begin
234  HWNASPI:=LoadLibrary(WNASPI);
235  if (HWNASPI<>0) then
236    begin
237    SendASPI32Command:=TSendASPI32Command(GetProcAddress(HWNASPI,'SendASPI32Command'));
238    GetASPI32SupportInfo:=TGetASPI32SupportInfo(GetProcAddress(HWNASPI,'GetASPI32SupportInfo'));
239    end
240end;
241
242procedure UnloadASPI;
243
244begin
245  if (HWNASPI<>0) then
246    begin
247    FreeLibrary(HWNASPI);
248    HWNASPI:=0;
249    SendASPI32Command:=nil;
250    GetASPI32SupportInfo:=nil;
251    end;
252end;
253
254initialization
255  CheckAspi;
256
257finalization
258  UnloadASPI;
259end.