source: trunk/src/wnaspi32/aspilib.cpp@ 4260

Last change on this file since 4260 was 4260, checked in by sandervl, 25 years ago

bugfixes

File size: 22.4 KB
Line 
1/* $Id: aspilib.cpp,v 1.4 2000-09-14 19:09:16 sandervl Exp $ */
2/*
3 * ASPI Router Library
4 * for Odin WNASPI32.DLL
5 *
6 * This is a sample library which shows how to send SRB's to the
7 * ASPI Router device driver. USE AT YOUR OWN RISK!!
8 *
9 * Copyright (C) 1997 Daniel Dorau
10 * Copyright (C) 2000 Przemyslaw Dobrowolski <dobrawka@asua.org.pl>
11 *
12 */
13#define INCL_DOSFILEMGR
14#define INCL_DOSDEVICES
15#define INCL_DOSDEVIOCTL
16#define INCL_DOSSEMAPHORES
17#define INCL_DOSMEMMGR
18#define INCL_DOSERRORS
19#include <os2wrap.h>
20#include <string.h>
21#include <stdio.h>
22#include <misc.h>
23#include "aspilib.h"
24
25//***************************************************************************
26//* *
27//* scsiObj() *
28//* *
29//* Standard constructor *
30//* *
31//***************************************************************************
32scsiObj::scsiObj() : buffer(NULL)
33{
34 memset(&SRBlock, 0, sizeof(SRBlock));
35 memset(&AbortSRB, 0, sizeof(AbortSRB));
36}
37
38
39//***************************************************************************
40//* *
41//* ~scsiObj() *
42//* *
43//* Standard destructor *
44//* *
45//***************************************************************************
46scsiObj::~scsiObj()
47{
48}
49
50
51//***************************************************************************
52//* *
53//* BOOL openDriver() *
54//* *
55//* Opens the ASPI Router device driver and sets device_handle. *
56//* Returns: *
57//* TRUE - Success *
58//* FALSE - Unsuccessful opening of device driver *
59//* *
60//* Preconditions: ASPI Router driver has be loaded *
61//* *
62//***************************************************************************
63BOOL scsiObj::openDriver()
64{
65 ULONG rc; // return value
66 ULONG ActionTaken; // return value
67
68 rc = DosOpen((PSZ) "aspirou$", // open driver
69 &driver_handle,
70 &ActionTaken,
71 0,
72 0,
73 FILE_OPEN,
74 OPEN_SHARE_DENYREADWRITE | OPEN_ACCESS_READWRITE,
75 NULL);
76 if (rc) return FALSE; // opening failed -> return false
77 return TRUE;
78}
79
80
81//***************************************************************************
82//* *
83//* BOOL closeDriver() *
84//* *
85//* Closes the device driver *
86//* Returns: *
87//* TRUE - Success *
88//* FALSE - Unsuccessful closing of device driver *
89//* *
90//* Preconditions: ASPI Router driver has be opened with openDriver *
91//* *
92//***************************************************************************
93BOOL scsiObj::closeDriver()
94{
95 ULONG rc; // return value
96
97 rc = DosClose(driver_handle);
98 if (rc) return FALSE; // closing failed -> return false
99 return TRUE;
100}
101
102
103//***************************************************************************
104//* *
105//* BOOL initSemaphore() *
106//* *
107//* Creates a new Event Semaphore and passes its handle to ASPI Router. *
108//* Returns: *
109//* TRUE - Success *
110//* FALSE - Unsuccessful creation of event semaphore *
111//* *
112//* Preconditions: driver_handle has to be set with openDriver *
113//* *
114//***************************************************************************
115BOOL scsiObj::initSemaphore()
116{
117 ULONG rc; // return value
118 USHORT openSemaReturn; // return value
119 unsigned long cbreturn;
120 unsigned long cbParam;
121
122 rc = DosCreateEventSem(NULL, &postSema, // create event semaphore
123 DC_SEM_SHARED, 0);
124 if (rc) return FALSE; // DosCreateEventSem failed
125 rc = DosDevIOCtl(driver_handle, 0x92, 0x03, // pass semaphore handle
126 (void*) &postSema, sizeof(HEV), // to driver
127 &cbParam, (void*) &openSemaReturn,
128 sizeof(USHORT), &cbreturn);
129 if (rc) return FALSE; // DosDevIOCtl failed
130 if (openSemaReturn) return FALSE; // Driver could not open semaphore
131
132 return TRUE;
133}
134
135
136//***************************************************************************
137//* *
138//* BOOL closeSemaphore() *
139//* *
140//* Closes the Event Semaphore *
141//* Returns: *
142//* TRUE - Success *
143//* FALSE - Unsuccessful closing of event semaphore *
144//* *
145//* Preconditions: init_Semaphore has to be called successfully before *
146//* *
147//***************************************************************************
148BOOL scsiObj::closeSemaphore()
149{
150 ULONG rc; // return value
151
152 rc = DosCloseEventSem(postSema); // close event semaphore
153 if (rc) return FALSE; // DosCloseEventSem failed
154 return TRUE;
155}
156
157
158//***************************************************************************
159//* *
160//* BOOL initBuffer() *
161//* *
162//* Sends the address of the data buffer to ASPI Router so that it can *
163//* lock down the segment. *
164//* Returns: *
165//* TRUE - Success *
166//* FALSE - Unsuccessful locking of buffer segment *
167//* *
168//* Preconditions: (called from init()) *
169//* *
170//***************************************************************************
171BOOL scsiObj::initBuffer()
172{
173 ULONG rc; // return value
174 USHORT lockSegmentReturn; // return value
175 unsigned long cbreturn;
176 unsigned long cbParam;
177
178 rc = DosDevIOCtl(driver_handle, 0x92, 0x04, // pass buffer pointer
179 (void*) buffer, sizeof(PVOID), // to driver
180 &cbParam, (void*) &lockSegmentReturn,
181 sizeof(USHORT), &cbreturn);
182 if (rc) return FALSE; // DosDevIOCtl failed
183 if (lockSegmentReturn) return FALSE; // Driver could not lock segment
184
185 return TRUE;
186}
187
188
189//***************************************************************************
190//* *
191//* BOOL init(ULONG bufsize) *
192//* *
193//* This inits the ASPI library and ASPI router driver. *
194//* Allocates the data buffer and passes its address to the driver *
195//* Returns: *
196//* TRUE - Success *
197//* FALSE - Unsuccessful initialization of driver and library *
198//* *
199//* Preconditions: ASPI router device driver has to be loaded *
200//* *
201//***************************************************************************
202BOOL scsiObj::init(ULONG bufsize)
203{
204 BOOL success;
205 ULONG rc;
206
207 rc = DosAllocMem(&buffer, bufsize, OBJ_TILE | PAG_READ | PAG_WRITE | PAG_COMMIT);
208 if (rc) return FALSE;
209 success=openDriver(); // call openDriver member function
210 if (!success) return FALSE;
211 success=initSemaphore(); // call initSemaphore member function
212 if (!success) return FALSE;
213
214 success=initBuffer();
215
216 return TRUE;
217}
218
219
220//***************************************************************************
221//* *
222//* BOOL close() *
223//* *
224//* This closes the ASPI library and ASPI router driver and frees *
225//* the memory allocated for the data buffer.
226//* Returns: *
227//* TRUE - Success *
228//* FALSE - Unsuccessful closing of library and driver *
229//* *
230//* Preconditions: init() should be called successfully before *
231//* *
232//***************************************************************************
233BOOL scsiObj::close()
234{
235 BOOL success;
236 ULONG rc;
237
238 success=closeSemaphore(); // call closeSemaphore member function
239 if (!success)
240 {
241 printf("closeSemaphore() unsuccessful.\n");
242 return FALSE;
243 }
244 success=closeDriver(); // call closeDriver member function
245 if (!success)
246 {
247 return FALSE;
248 printf("closeDriver() unsucessful.\n");
249 }
250 rc = DosFreeMem(buffer);
251 if (rc)
252 {
253 printf("DosFreeMem unsuccessful. return code: %ld\n", rc);
254 return FALSE;
255 }
256 return TRUE;
257}
258
259
260//***************************************************************************
261//* *
262//* BOOL waitPost() *
263//* *
264//* Waits for postSema being posted by device driver *
265//* Returns: *
266//* TRUE - Success *
267//* FALSE - Unsuccessful access of event semaphore *
268//* *
269//* Preconditions: init() has to be called successfully before *
270//* *
271//***************************************************************************
272BOOL scsiObj::waitPost()
273{
274 ULONG count=0;
275 ULONG rc; // return value
276
277 rc = DosWaitEventSem(postSema, -1); // wait forever
278 if (rc) return FALSE; // DosWaitEventSem failed
279 rc = DosResetEventSem(postSema, &count); // reset semaphore
280 if (rc) return FALSE; // DosResetEventSem failed
281 return TRUE;
282}
283
284//***************************************************************************
285//* *
286//* ULONG HA_inquiry(UCHAR ha) *
287//* *
288//* Sends a SRB containing a Host Adapter Inquiry command *
289//* Returns: *
290//* 0 - Success *
291//* 1 - DevIOCtl failed *
292//* 2 - Host Adapter not installed *
293//* *
294//* Preconditions: driver has to be opened *
295//* *
296//***************************************************************************
297ULONG scsiObj::HA_inquiry(UCHAR ha)
298{
299 ULONG rc; // return value
300 unsigned long cbreturn;
301 unsigned long cbParam;
302
303 SRBlock.cmd=SRB_Inquiry; // host adapter inquiry
304 SRBlock.ha_num=ha; // host adapter number
305 SRBlock.flags=0; // no flags set
306
307 rc = DosDevIOCtl(driver_handle, 0x92, 0x02, (void*) &SRBlock, sizeof(SRBOS2), &cbParam,
308 (void*) &SRBlock, sizeof(SRBOS2), &cbreturn);
309 if (rc)
310 return 1; // DosDevIOCtl failed
311 if (SRBlock.status != SRB_Done) return 2;
312 return 0;
313}
314
315
316//***************************************************************************
317//* *
318//* ULONG getDeviceType(UCHAR id, UCHAR lun) *
319//* *
320//* Sends a SRB containing a Get Device Type command *
321//* Returns: *
322//* 0 - Success *
323//* 1 - DevIOCtl failed *
324//* 2 - Device not installed *
325//* *
326//* Preconditions: driver has to be opened *
327//* *
328//***************************************************************************
329ULONG scsiObj::getDeviceType(UCHAR ha_num, UCHAR id, UCHAR lun)
330{
331 ULONG rc; // return value
332 unsigned long cbreturn;
333 unsigned long cbParam;
334
335 SRBlock.cmd=SRB_Device; // get device type
336 SRBlock.ha_num=ha_num; // host adapter number
337 SRBlock.flags=0; // no flags set
338 SRBlock.u.dev.target=id; // target id
339 SRBlock.u.dev.lun=lun; // target LUN
340
341 rc = DosDevIOCtl(driver_handle, 0x92, 0x02, (void*) &SRBlock, sizeof(SRBOS2), &cbParam,
342 (void*) &SRBlock, sizeof(SRBOS2), &cbreturn);
343 if (rc)
344 return 1; // DosDevIOCtl failed
345 if (SRBlock.status != SRB_Done) return 2;
346 return 0;
347}
348
349
350//***************************************************************************
351//* *
352//* ULONG resetDevice(UCHAR id, UCHAR lun) *
353//* *
354//* Sends a SRB containing a Reset Device command *
355//* Returns: *
356//* 0 - Success *
357//* 1 - DevIOCtl failed *
358//* 2 - Semaphore access failure *
359//* 3 - SCSI command failed *
360//* *
361//* Preconditions: init() has to be called successfully before *
362//* *
363//***************************************************************************
364ULONG scsiObj::resetDevice(UCHAR ha_num,UCHAR id, UCHAR lun)
365{
366 ULONG rc; // return value
367 unsigned long cbreturn;
368 unsigned long cbParam;
369 BOOL success;
370
371 SRBlock.cmd=SRB_Reset; // reset device
372 SRBlock.ha_num=ha_num; // host adapter number
373 SRBlock.flags=SRB_Post; // posting enabled
374 SRBlock.u.res.target=id; // target id
375 SRBlock.u.res.lun=lun; // target LUN
376
377 rc = DosDevIOCtl(driver_handle, 0x92, 0x02, (void*) &SRBlock, sizeof(SRBOS2), &cbParam,
378 (void*) &SRBlock, sizeof(SRBOS2), &cbreturn);
379 if (rc)
380 return 1; // DosDevIOCtl failed
381 else
382 {
383 success=waitPost(); // wait for SRB being processed
384 if (!success) return 2; // semaphore could not be accessed
385 }
386 if (SRBlock.status != SRB_Done) return 3;
387 return 0;
388}
389
390
391//***************************************************************************
392//* *
393//* ULONG abort() *
394//* *
395//* Sends a SRB containing a Get Device Type command *
396//* Returns: *
397//* 0 - Success *
398//* 1 - DevIOCtl failed *
399//* 2 - Abort SRB not successful *
400//* *
401//* Preconditions: driver has to be opened *
402//* *
403//***************************************************************************
404ULONG scsiObj::abort()
405{
406 ULONG rc; // return value
407 unsigned long cbreturn;
408 unsigned long cbParam;
409
410 AbortSRB.cmd=SRBOS2_Abort; // abort SRB
411 AbortSRB.ha_num=0; // host adapter number
412 AbortSRB.flags=0; // no flags set
413 AbortSRB.u.abt.srb=&SRBlock; // SRB to abort
414
415 rc = DosDevIOCtl(driver_handle, 0x92, 0x02, (void*) &AbortSRB, sizeof(SRBOS2), &cbParam,
416 (void*) &AbortSRB, sizeof(SRBOS2), &cbreturn);
417 if (rc)
418 return 1; // DosDevIOCtl failed
419 if (SRBlock.status != SRB_Done) return 2;
420 return 0;
421}
422//***************************************************************************
423//***************************************************************************
424ULONG scsiObj::getNumHosts()
425{ int i,j=0;
426 ULONG rc;
427
428 for (i=0;i<15;i++)
429 {
430 rc=HA_inquiry(i);
431 if (rc==0) j++;
432 }
433 return j;
434}
435//***************************************************************************
436//***************************************************************************
437ULONG scsiObj::SendSRBlock(VOID)
438{
439 ULONG ulParam, ulReturn;
440
441 return DosDevIOCtl( (HFILE) driver_handle,
442 0x92,
443 0x02,
444 (void*) &SRBlock,
445 sizeof(SRBOS2),
446 &ulParam,
447 (void*) &SRBlock,
448 sizeof(SRBOS2),
449 &ulReturn);
450}
451//***************************************************************************
452//***************************************************************************
453BOOL fGainDrvAccess( BOOL fWait,
454 ULONG *phSem)
455{
456 ULONG rc;
457 rc = DosCreateMutexSem ( "\\SEM32\\ODIN\\ASPIROUT",
458 (HMTX*)phSem,
459 0,
460 TRUE);
461 if(NO_ERROR==rc)
462 return TRUE;
463 if((ERROR_DUPLICATE_NAME!=rc)||(!fWait))
464 return FALSE;
465 rc = DosOpenMutexSem ( "\\SEM32\\ODIN\\ASPIROUT",
466 (HMTX*)phSem);
467 if(NO_ERROR!=rc)
468 return FALSE;
469
470 rc = DosRequestMutexSem( (HMTX)(*phSem),
471 SEM_INDEFINITE_WAIT);
472
473 if(NO_ERROR!=rc)
474 return FALSE;
475
476 return TRUE;
477}
478//***************************************************************************
479//***************************************************************************
480BOOL fReleaseDrvAccess(ULONG hSem)
481{
482 ULONG rc;
483 BOOL frc = TRUE;
484
485 rc = DosReleaseMutexSem((HMTX)hSem);
486 if(NO_ERROR!=rc)
487 frc=FALSE;
488
489 rc = DosCloseMutexSem((HMTX)hSem);
490 if(NO_ERROR!=rc)
491 frc=FALSE;
492
493 return frc;
494}
Note: See TracBrowser for help on using the repository browser.