~ubuntu-branches/ubuntu/lucid/cdrdao/lucid

« back to all changes in this revision

Viewing changes to scsilib/libscg/scsi-os2.c

  • Committer: Bazaar Package Importer
  • Author(s): Andrew Suffield
  • Date: 2004-06-24 22:33:16 UTC
  • Revision ID: james.westby@ubuntu.com-20040624223316-534onzugaeeyq61j
Tags: upstream-1.1.9
Import upstream version 1.1.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* @(#)scsi-os2.c       1.25 04/01/15 Copyright 1998 J. Schilling, C. Wohlgemuth */
 
2
#ifndef lint
 
3
static  char __sccsid[] =
 
4
        "@(#)scsi-os2.c 1.25 04/01/15 Copyright 1998 J. Schilling, C. Wohlgemuth";
 
5
#endif
 
6
/*
 
7
 *      Interface for the OS/2 ASPI-Router ASPIROUT.SYS ((c) D. Dorau).
 
8
 *              This additional driver is a prerequisite for using cdrecord.
 
9
 *              Get it from HOBBES or LEO.
 
10
 *
 
11
 *      Warning: you may change this source, but if you do that
 
12
 *      you need to change the _scg_version and _scg_auth* string below.
 
13
 *      You may not return "schily" for an SCG_AUTHOR request anymore.
 
14
 *      Choose your name instead of "schily" and make clear that the version
 
15
 *      string is related to a modified source.
 
16
 *
 
17
 *      XXX it currently uses static SRB and for this reason is not reentrant
 
18
 *
 
19
 *      Copyright (c) 1998 J. Schilling
 
20
 *      Copyright (c) 1998 C. Wohlgemuth for this interface.
 
21
 */
 
22
/*
 
23
 * This program is free software; you can redistribute it and/or modify
 
24
 * it under the terms of the GNU General Public License as published by
 
25
 * the Free Software Foundation; either version 2, or (at your option)
 
26
 * any later version.
 
27
 *
 
28
 * This program is distributed in the hope that it will be useful,
 
29
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
30
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
31
 * GNU General Public License for more details.
 
32
 *
 
33
 * You should have received a copy of the GNU General Public License along with
 
34
 * this program; see the file COPYING.  If not, write to the Free Software
 
35
 * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
36
 */
 
37
 
 
38
#undef  sense
 
39
 
 
40
/*#define       DEBUG*/
 
41
 
 
42
/* For AspiRouter */
 
43
#include "scg/srb_os2.h"
 
44
 
 
45
/*
 
46
 *      Warning: you may change this source, but if you do that
 
47
 *      you need to change the _scg_version and _scg_auth* string below.
 
48
 *      You may not return "schily" for an SCG_AUTHOR request anymore.
 
49
 *      Choose your name instead of "schily" and make clear that the version
 
50
 *      string is related to a modified source.
 
51
 */
 
52
LOCAL   char    _scg_trans_version[] = "scsi-os2.c-1.25";       /* The version for this transport*/
 
53
 
 
54
#define FILE_OPEN                       0x0001
 
55
#define OPEN_SHARE_DENYREADWRITE        0x0010
 
56
#define OPEN_ACCESS_READWRITE           0x0002
 
57
#define DC_SEM_SHARED                   0x01
 
58
#define OBJ_TILE                        0x0040
 
59
#define PAG_READ                        0x0001
 
60
#define PAG_WRITE                       0x0002
 
61
#define PAG_COMMIT                      0x0010
 
62
 
 
63
typedef unsigned long LHANDLE;
 
64
typedef unsigned long ULONG;
 
65
typedef unsigned char *PSZ;
 
66
typedef unsigned short USHORT;
 
67
typedef unsigned char UCHAR;
 
68
 
 
69
typedef LHANDLE HFILE;
 
70
typedef ULONG   HEV;
 
71
 
 
72
#define MAX_SCG         16      /* Max # of SCSI controllers */
 
73
#define MAX_TGT         16
 
74
#define MAX_LUN         8
 
75
 
 
76
struct scg_local {
 
77
        int     dummy;
 
78
};
 
79
#define scglocal(p)     ((struct scg_local *)((p)->local))
 
80
 
 
81
#define MAX_DMA_OS2     (63*1024) /* ASPI-Router allows up to 64k */
 
82
 
 
83
LOCAL   void    *buffer         = NULL;
 
84
LOCAL   HFILE   driver_handle   = 0;
 
85
LOCAL   HEV     postSema        = 0;
 
86
 
 
87
LOCAL   BOOL    open_driver     __PR((SCSI *scgp));
 
88
LOCAL   BOOL    close_driver    __PR((void));
 
89
LOCAL   ULONG   wait_post       __PR((ULONG ulTimeOut));
 
90
LOCAL   BOOL    init_buffer     __PR((void* mem));
 
91
LOCAL   void    exit_func       __PR((void));
 
92
LOCAL   void    set_error       __PR((SRB *srb, struct scg_cmd *sp));
 
93
 
 
94
 
 
95
LOCAL void
 
96
exit_func()
 
97
{
 
98
        if (!close_driver())
 
99
                js_fprintf(stderr, "Cannot close OS/2-ASPI-Router!\n");
 
100
}
 
101
 
 
102
/*
 
103
 * Return version information for the low level SCSI transport code.
 
104
 * This has been introduced to make it easier to trace down problems
 
105
 * in applications.
 
106
 */
 
107
LOCAL char *
 
108
scgo_version(scgp, what)
 
109
        SCSI    *scgp;
 
110
        int     what;
 
111
{
 
112
        if (scgp != (SCSI *)0) {
 
113
                switch (what) {
 
114
 
 
115
                case SCG_VERSION:
 
116
                        return (_scg_trans_version);
 
117
                /*
 
118
                 * If you changed this source, you are not allowed to
 
119
                 * return "schily" for the SCG_AUTHOR request.
 
120
                 */
 
121
                case SCG_AUTHOR:
 
122
                        return (_scg_auth_schily);
 
123
                case SCG_SCCS_ID:
 
124
                        return (__sccsid);
 
125
                }
 
126
        }
 
127
        return ((char *)0);
 
128
}
 
129
 
 
130
LOCAL int
 
131
scgo_help(scgp, f)
 
132
        SCSI    *scgp;
 
133
        FILE    *f;
 
134
{
 
135
        __scg_help(f, "ASPI", "Generic transport independent SCSI",
 
136
                "", "bus,target,lun", "1,2,0", TRUE, FALSE);
 
137
        return (0);
 
138
}
 
139
 
 
140
LOCAL int
 
141
scgo_open(scgp, device)
 
142
        SCSI    *scgp;
 
143
        char    *device;
 
144
{
 
145
        int     busno   = scg_scsibus(scgp);
 
146
        int     tgt     = scg_target(scgp);
 
147
        int     tlun    = scg_lun(scgp);
 
148
 
 
149
        if (busno >= MAX_SCG || tgt >= MAX_TGT || tlun >= MAX_LUN) {
 
150
                errno = EINVAL;
 
151
                if (scgp->errstr)
 
152
                        js_snprintf(scgp->errstr, SCSI_ERRSTR_SIZE,
 
153
                                "Illegal value for busno, target or lun '%d,%d,%d'",
 
154
                                busno, tgt, tlun);
 
155
                return (-1);
 
156
        }
 
157
 
 
158
        if ((device != NULL && *device != '\0') || (busno == -2 && tgt == -2)) {
 
159
                errno = EINVAL;
 
160
                if (scgp->errstr)
 
161
                        js_snprintf(scgp->errstr, SCSI_ERRSTR_SIZE,
 
162
                                "Open by 'devname' not supported on this OS");
 
163
                return (-1);
 
164
        }
 
165
 
 
166
        if (scgp->local == NULL) {
 
167
                scgp->local = malloc(sizeof (struct scg_local));
 
168
                if (scgp->local == NULL)
 
169
                        return (0);
 
170
        }
 
171
 
 
172
        if (!open_driver(scgp)) /* Try to open ASPI-Router */
 
173
                return (-1);
 
174
        atexit(exit_func);      /* Install Exit Function which closes the ASPI-Router */
 
175
 
 
176
        /*
 
177
         * Success after all
 
178
         */
 
179
        return (1);
 
180
}
 
181
 
 
182
LOCAL int
 
183
scgo_close(scgp)
 
184
        SCSI    *scgp;
 
185
{
 
186
        exit_func();
 
187
        return (0);
 
188
}
 
189
 
 
190
LOCAL long
 
191
scgo_maxdma(scgp, amt)
 
192
        SCSI    *scgp;
 
193
        long    amt;
 
194
{
 
195
        long maxdma = MAX_DMA_OS2;
 
196
        return (maxdma);
 
197
}
 
198
 
 
199
LOCAL void *
 
200
scgo_getbuf(scgp, amt)
 
201
        SCSI    *scgp;
 
202
        long    amt;
 
203
{
 
204
        ULONG rc;
 
205
 
 
206
#ifdef DEBUG
 
207
        js_fprintf((FILE *)scgp->errfile, "scgo_getbuf: %ld bytes\n", amt);
 
208
#endif
 
209
        rc = DosAllocMem(&buffer, amt, OBJ_TILE | PAG_READ | PAG_WRITE | PAG_COMMIT);
 
210
 
 
211
        if (rc) {
 
212
                js_fprintf((FILE *)scgp->errfile, "Cannot allocate buffer.\n");
 
213
                return ((void *)0);
 
214
        }
 
215
        scgp->bufbase = buffer;
 
216
 
 
217
#ifdef DEBUG
 
218
        js_fprintf((FILE *)scgp->errfile, "Buffer allocated at: 0x%x\n", scgp->bufbase);
 
219
#endif
 
220
 
 
221
        /* Lock memory */
 
222
        if (init_buffer(scgp->bufbase))
 
223
                return (scgp->bufbase);
 
224
 
 
225
        js_fprintf((FILE *)scgp->errfile, "Cannot lock memory buffer.\n");
 
226
        return ((void *)0); /* Error */
 
227
}
 
228
 
 
229
LOCAL void
 
230
scgo_freebuf(scgp)
 
231
        SCSI    *scgp;
 
232
{
 
233
        if (scgp->bufbase && DosFreeMem(scgp->bufbase)) {
 
234
                js_fprintf((FILE *)scgp->errfile,
 
235
                "Cannot free buffer memory for ASPI-Router!\n"); /* Free our memory buffer if not already done */
 
236
        }
 
237
        if (buffer == scgp->bufbase)
 
238
                buffer = NULL;
 
239
        scgp->bufbase = NULL;
 
240
}
 
241
 
 
242
LOCAL BOOL
 
243
scgo_havebus(scgp, busno)
 
244
        SCSI    *scgp;
 
245
        int     busno;
 
246
{
 
247
        register int    t;
 
248
        register int    l;
 
249
 
 
250
        if (busno < 0 || busno >= MAX_SCG)
 
251
                return (FALSE);
 
252
 
 
253
        return (TRUE);
 
254
}
 
255
 
 
256
LOCAL int
 
257
scgo_fileno(scgp, busno, tgt, tlun)
 
258
        SCSI    *scgp;
 
259
        int     busno;
 
260
        int     tgt;
 
261
        int     tlun;
 
262
{
 
263
        if (busno < 0 || busno >= MAX_SCG ||
 
264
            tgt < 0 || tgt >= MAX_TGT ||
 
265
            tlun < 0 || tlun >= MAX_LUN)
 
266
                return (-1);
 
267
 
 
268
        /*
 
269
         * Return fake
 
270
         */
 
271
        return (1);
 
272
}
 
273
 
 
274
 
 
275
LOCAL int
 
276
scgo_initiator_id(scgp)
 
277
        SCSI    *scgp;
 
278
{
 
279
        return (-1);
 
280
}
 
281
 
 
282
LOCAL int
 
283
scgo_isatapi(scgp)
 
284
        SCSI    *scgp;
 
285
{
 
286
        return (FALSE);
 
287
}
 
288
 
 
289
 
 
290
LOCAL int
 
291
scgo_reset(scgp, what)
 
292
        SCSI    *scgp;
 
293
        int     what;
 
294
{
 
295
        ULONG   rc;                             /* return value */
 
296
        ULONG   cbreturn;
 
297
        ULONG   cbParam;
 
298
        BOOL    success;
 
299
static  SRB     SRBlock;                        /* XXX makes it non reentrant */
 
300
 
 
301
        if (what == SCG_RESET_NOP)
 
302
                return (0);
 
303
        if (what != SCG_RESET_BUS) {
 
304
                errno = EINVAL;
 
305
                return (-1);
 
306
        }
 
307
        /*
 
308
         * XXX Does this reset TGT or BUS ???
 
309
         */
 
310
        SRBlock.cmd             = SRB_Reset;            /* reset device         */
 
311
        SRBlock.ha_num          = scg_scsibus(scgp);    /* host adapter number  */
 
312
        SRBlock.flags           = SRB_Post;             /* posting enabled      */
 
313
        SRBlock.u.res.target    = scg_target(scgp);     /* target id            */
 
314
        SRBlock.u.res.lun       = scg_lun(scgp);        /* target LUN           */
 
315
 
 
316
        rc = DosDevIOCtl(driver_handle, 0x92, 0x02, (void*) &SRBlock, sizeof (SRB), &cbParam,
 
317
                        (void*) &SRBlock, sizeof (SRB), &cbreturn);
 
318
        if (rc) {
 
319
                js_fprintf((FILE *)scgp->errfile,
 
320
                                "DosDevIOCtl() failed in resetDevice.\n");
 
321
                return (1);                     /* DosDevIOCtl failed */
 
322
        } else {
 
323
                success = wait_post(40000);     /** wait for SRB being processed */
 
324
                if (success)
 
325
                        return (2);
 
326
        }
 
327
        if (SRBlock.status != SRB_Done)
 
328
                return (3);
 
329
#ifdef DEBUG
 
330
        js_fprintf((FILE *)scgp->errfile,
 
331
                "resetDevice of host: %d target: %d lun: %d successful.\n", scg_scsibus(scgp), scg_target(scgp), scg_lun(scgp));
 
332
        js_fprintf((FILE *)scgp->errfile,
 
333
                "SRBlock.ha_status: 0x%x, SRBlock.target_status: 0x%x, SRBlock.satus: 0x%x\n",
 
334
                                SRBlock.u.cmd.ha_status, SRBlock.u.cmd.target_status, SRBlock.status);
 
335
#endif
 
336
        return (0);
 
337
}
 
338
 
 
339
/*
 
340
 * Set error flags
 
341
 */
 
342
LOCAL void
 
343
set_error(srb, sp)
 
344
        SRB     *srb;
 
345
        struct scg_cmd  *sp;
 
346
{
 
347
        switch (srb->status) {
 
348
 
 
349
        case SRB_InvalidCmd:            /* 0x80 Invalid SCSI request        */
 
350
        case SRB_InvalidHA:             /* 0x81 Invalid host adapter number */
 
351
        case SRB_BadDevice:             /* 0x82 SCSI device not installed   */
 
352
                sp->error = SCG_FATAL;
 
353
                sp->ux_errno = EINVAL;  /* Should we ever return != EIO     */
 
354
                sp->ux_errno = EIO;
 
355
                break;
 
356
 
 
357
 
 
358
        case SRB_Busy:                  /* 0x00 SCSI request in progress    */
 
359
        case SRB_Aborted:               /* 0x02 SCSI aborted by host        */
 
360
        case SRB_BadAbort:              /* 0x03 Unable to abort SCSI request */
 
361
        case SRB_Error:                 /* 0x04 SCSI request completed with error */
 
362
        default:
 
363
                sp->error = SCG_RETRYABLE;
 
364
                sp->ux_errno = EIO;
 
365
                break;
 
366
        }
 
367
}
 
368
 
 
369
LOCAL int
 
370
scgo_send(scgp)
 
371
        SCSI    *scgp;
 
372
{
 
373
        struct scg_cmd  *sp = scgp->scmd;
 
374
        ULONG   rc;                             /* return value */
 
375
static  SRB     SRBlock;                        /* XXX makes it non reentrant */
 
376
        Ulong   cbreturn;
 
377
        Ulong   cbParam;
 
378
        UCHAR*  ptr;
 
379
 
 
380
        if (scgp->fd < 0) {                     /* Set in scgo_open() */
 
381
                sp->error = SCG_FATAL;
 
382
                return (0);
 
383
        }
 
384
 
 
385
        if (sp->cdb_len > sizeof (SRBlock.u.cmd.cdb_st)) { /* commandsize too big */
 
386
                sp->error = SCG_FATAL;
 
387
                sp->ux_errno = EINVAL;
 
388
                js_fprintf((FILE *)scgp->errfile,
 
389
                        "sp->cdb_len > SRBlock.u.cmd.cdb_st. Fatal error in scgo_send, exiting...\n");
 
390
                return (-1);
 
391
        }
 
392
 
 
393
        /* clear command block */
 
394
        fillbytes((caddr_t)&SRBlock.u.cmd.cdb_st, sizeof (SRBlock.u.cmd.cdb_st), '\0');
 
395
        /* copy cdrecord command into SRB */
 
396
        movebytes(&sp->cdb, &SRBlock.u.cmd.cdb_st, sp->cdb_len);
 
397
 
 
398
        /* Build SRB command block */
 
399
        SRBlock.cmd = SRB_Command;
 
400
        SRBlock.ha_num = scg_scsibus(scgp);     /* host adapter number */
 
401
 
 
402
        SRBlock.flags = SRB_Post;               /* flags */
 
403
 
 
404
        SRBlock.u.cmd.target    = scg_target(scgp); /* Target SCSI ID */
 
405
        SRBlock.u.cmd.lun       = scg_lun(scgp); /* Target SCSI LUN */
 
406
        SRBlock.u.cmd.data_len  = sp->size;     /* # of bytes transferred */
 
407
        SRBlock.u.cmd.data_ptr  = 0;            /* pointer to data buffer */
 
408
        SRBlock.u.cmd.sense_len = sp->sense_len; /* length of sense buffer */
 
409
 
 
410
        SRBlock.u.cmd.link_ptr  = 0;            /* pointer to next SRB */
 
411
        SRBlock.u.cmd.cdb_len   = sp->cdb_len;  /* SCSI command length */
 
412
 
 
413
        /* Specify direction */
 
414
        if (sp->flags & SCG_RECV_DATA) {
 
415
                SRBlock.flags |= SRB_Read;
 
416
        } else {
 
417
                if (sp->size > 0) {
 
418
                        SRBlock.flags |= SRB_Write;
 
419
                        if (scgp->bufbase != sp->addr) { /* Copy only if data not in ASPI-Mem */
 
420
                                movebytes(sp->addr, scgp->bufbase, sp->size);
 
421
                        }
 
422
                } else {
 
423
                        SRBlock.flags |= SRB_NoTransfer;
 
424
                }
 
425
        }
 
426
        sp->error       = SCG_NO_ERROR;
 
427
        sp->sense_count = 0;
 
428
        sp->u_scb.cmd_scb[0] = 0;
 
429
        sp->resid       = 0;
 
430
 
 
431
        /* execute SCSI command */
 
432
        rc = DosDevIOCtl(driver_handle, 0x92, 0x02,
 
433
                        (void*) &SRBlock, sizeof (SRB), &cbParam,
 
434
                        (void*) &SRBlock, sizeof (SRB), &cbreturn);
 
435
 
 
436
        if (rc) {               /* An error occured */
 
437
                js_fprintf((FILE *)scgp->errfile,
 
438
                                "DosDevIOCtl() in sendCommand failed.\n");
 
439
                sp->error = SCG_FATAL;
 
440
                sp->ux_errno = EIO;     /* Sp�ter vielleicht errno einsetzen */
 
441
                return (rc);
 
442
        } else {
 
443
                /* Wait until the command is processed */
 
444
                rc = wait_post(sp->timeout*1000);
 
445
                if (rc) {       /* An error occured */
 
446
                        if (rc == 640) {
 
447
                                /* Timeout */
 
448
                                sp->error = SCG_TIMEOUT;
 
449
                                sp->ux_errno = EIO;
 
450
                                js_fprintf((FILE *)scgp->errfile,
 
451
                                                "Timeout during SCSI-Command.\n");
 
452
                                return (1);
 
453
                        }
 
454
                        sp->error = SCG_FATAL;
 
455
                        sp->ux_errno = EIO;
 
456
                        js_fprintf((FILE *)scgp->errfile,
 
457
                                        "Fatal Error during DosWaitEventSem().\n");
 
458
                        return (1);
 
459
                }
 
460
 
 
461
                /* The command is processed */
 
462
                if (SRBlock.status == SRB_Done) {       /* succesful completion */
 
463
#ifdef DEBUG
 
464
                        js_fprintf((FILE *)scgp->errfile,
 
465
                                "Command successful finished. SRBlock.status=0x%x\n\n", SRBlock.status);
 
466
#endif
 
467
                        sp->sense_count = 0;
 
468
                        sp->resid = 0;
 
469
                        if (sp->flags & SCG_RECV_DATA) {        /* We read data */
 
470
                                if (sp->addr && sp->size) {
 
471
                                        if (scgp->bufbase != sp->addr)  /* Copy only if data not in ASPI-Mem */
 
472
                                                movebytes(scgp->bufbase, sp->addr, SRBlock.u.cmd.data_len);
 
473
                                        ptr = (UCHAR*)sp->addr;
 
474
                                        sp->resid = sp->size - SRBlock.u.cmd.data_len; /*nicht �bertragene bytes. Korrekt berechnet???*/
 
475
                                }
 
476
                        }       /* end of if (sp->flags & SCG_RECV_DATA) */
 
477
                        if (SRBlock.u.cmd.target_status == SRB_CheckStatus) { /* Sense data valid */
 
478
                                sp->sense_count = (int)SRBlock.u.cmd.sense_len;
 
479
                                if (sp->sense_count > sp->sense_len)
 
480
                                        sp->sense_count = sp->sense_len;
 
481
 
 
482
                                ptr = (UCHAR*)&SRBlock.u.cmd.cdb_st;
 
483
                                ptr += SRBlock.u.cmd.cdb_len;
 
484
 
 
485
                                fillbytes(&sp->u_sense.Sense, sizeof (sp->u_sense.Sense), '\0');
 
486
                                movebytes(ptr, &sp->u_sense.Sense, sp->sense_len);
 
487
 
 
488
                                sp->u_scb.cmd_scb[0] = SRBlock.u.cmd.target_status;
 
489
                                sp->ux_errno = EIO;     /* Sp�ter differenzieren? */
 
490
                        }
 
491
                        return (0);
 
492
                }
 
493
                /* SCSI-Error occured */
 
494
                set_error(&SRBlock, sp);
 
495
 
 
496
                if (SRBlock.u.cmd.target_status == SRB_CheckStatus) {   /* Sense data valid */
 
497
                        sp->sense_count = (int)SRBlock.u.cmd.sense_len;
 
498
                        if (sp->sense_count > sp->sense_len)
 
499
                                sp->sense_count = sp->sense_len;
 
500
 
 
501
                        ptr = (UCHAR*)&SRBlock.u.cmd.cdb_st;
 
502
                        ptr += SRBlock.u.cmd.cdb_len;
 
503
 
 
504
                        fillbytes(&sp->u_sense.Sense, sizeof (sp->u_sense.Sense), '\0');
 
505
                        movebytes(ptr, &sp->u_sense.Sense, sp->sense_len);
 
506
 
 
507
                        sp->u_scb.cmd_scb[0] = SRBlock.u.cmd.target_status;
 
508
                }
 
509
                if (sp->flags & SCG_RECV_DATA) {
 
510
                        if (sp->addr && sp->size) {
 
511
                                if (scgp->bufbase != sp->addr)  /* Copy only if data not in ASPI-Mem */
 
512
                                        movebytes(scgp->bufbase, sp->addr, SRBlock.u.cmd.data_len);
 
513
                        }
 
514
                }
 
515
#ifdef  really
 
516
                sp->resid       = SRBlock.u.cmd.data_len; /* XXXXX Got no Data ????? */
 
517
#else
 
518
                sp->resid       = sp->size - SRBlock.u.cmd.data_len;
 
519
#endif
 
520
                return (1);
 
521
        }
 
522
}
 
523
 
 
524
/***************************************************************************
 
525
 *                                                                         *
 
526
 *  BOOL open_driver()                                                     *
 
527
 *                                                                         *
 
528
 *  Opens the ASPI Router device driver and sets device_handle.            *
 
529
 *  Returns:                                                               *
 
530
 *    TRUE - Success                                                       *
 
531
 *    FALSE - Unsuccessful opening of device driver                        *
 
532
 *                                                                         *
 
533
 *  Preconditions: ASPI Router driver has be loaded                        *
 
534
 *                                                                         *
 
535
 ***************************************************************************/
 
536
LOCAL BOOL
 
537
open_driver(scgp)
 
538
        SCSI    *scgp;
 
539
{
 
540
        ULONG   rc;                     /* return value */
 
541
        ULONG   ActionTaken;            /* return value */
 
542
        USHORT  openSemaReturn;         /* return value */
 
543
        ULONG   cbreturn;
 
544
        ULONG   cbParam;
 
545
 
 
546
        if (driver_handle)              /* ASPI-Router already opened   */
 
547
                return (TRUE);
 
548
 
 
549
        rc = DosOpen((PSZ) "aspirou$",  /* open driver*/
 
550
                        &driver_handle,
 
551
                        &ActionTaken,
 
552
                        0,
 
553
                        0,
 
554
                        FILE_OPEN,
 
555
                        OPEN_SHARE_DENYREADWRITE | OPEN_ACCESS_READWRITE,
 
556
                        NULL);
 
557
        if (rc) {
 
558
                js_fprintf((FILE *)scgp->errfile,
 
559
                                "Cannot open ASPI-Router!\n");
 
560
 
 
561
                return (FALSE);         /* opening failed -> return false*/
 
562
        }
 
563
 
 
564
        /* Init semaphore */
 
565
        if (DosCreateEventSem(NULL, &postSema,  /* create event semaphore */
 
566
                                DC_SEM_SHARED, 0)) {
 
567
                DosClose(driver_handle);
 
568
                js_fprintf((FILE *)scgp->errfile,
 
569
                                "Cannot create event semaphore!\n");
 
570
 
 
571
                return (FALSE);
 
572
        }
 
573
        rc = DosDevIOCtl(driver_handle, 0x92, 0x03,     /* pass semaphore handle */
 
574
                        (void*) &postSema, sizeof (HEV), /* to driver            */
 
575
                        &cbParam, (void*) &openSemaReturn,
 
576
                        sizeof (USHORT), &cbreturn);
 
577
 
 
578
        if (rc||openSemaReturn) {                       /* Error */
 
579
                DosCloseEventSem(postSema);
 
580
                DosClose(driver_handle);
 
581
                return (FALSE);
 
582
        }
 
583
        return (TRUE);
 
584
}
 
585
 
 
586
/***************************************************************************
 
587
 *                                                                         *
 
588
 *  BOOL close_driver()                                                    *
 
589
 *                                                                         *
 
590
 *  Closes the device driver                                               *
 
591
 *  Returns:                                                               *
 
592
 *    TRUE - Success                                                       *
 
593
 *    FALSE - Unsuccessful closing of device driver                        *
 
594
 *                                                                         *
 
595
 *  Preconditions: ASPI Router driver has be opened with open_driver       *
 
596
 *                                                                         *
 
597
 ***************************************************************************/
 
598
LOCAL BOOL
 
599
close_driver()
 
600
{
 
601
        ULONG rc;                               /* return value */
 
602
 
 
603
        if (driver_handle) {
 
604
                rc = DosClose(driver_handle);
 
605
                if (rc)
 
606
                        return (FALSE);         /* closing failed -> return false */
 
607
                driver_handle = 0;
 
608
                if (DosCloseEventSem(postSema))
 
609
                        js_fprintf(stderr, "Cannot close event semaphore!\n");
 
610
                if (buffer && DosFreeMem(buffer)) {
 
611
                        js_fprintf(stderr,
 
612
                        "Cannot free buffer memory for ASPI-Router!\n"); /* Free our memory buffer if not already done */
 
613
                }
 
614
                buffer = NULL;
 
615
        }
 
616
        return (TRUE);
 
617
}
 
618
 
 
619
LOCAL ULONG
 
620
wait_post(ULONG ulTimeOut)
 
621
{
 
622
        ULONG count = 0;
 
623
        ULONG rc;                                       /* return value */
 
624
 
 
625
/*      rc = DosWaitEventSem(postSema, -1);*/           /* wait forever*/
 
626
        rc = DosWaitEventSem(postSema, ulTimeOut);
 
627
        DosResetEventSem(postSema, &count);
 
628
        return (rc);
 
629
}
 
630
 
 
631
LOCAL BOOL
 
632
init_buffer(mem)
 
633
        void    *mem;
 
634
{
 
635
        ULONG   rc;                                     /* return value */
 
636
        USHORT lockSegmentReturn;                       /* return value */
 
637
        Ulong   cbreturn;
 
638
        Ulong   cbParam;
 
639
 
 
640
        rc = DosDevIOCtl(driver_handle, 0x92, 0x04,     /* pass buffers pointer */
 
641
                        (void*) mem, sizeof (void*),    /* to driver */
 
642
                        &cbParam, (void*) &lockSegmentReturn,
 
643
                        sizeof (USHORT), &cbreturn);
 
644
        if (rc)
 
645
                return (FALSE);                         /* DosDevIOCtl failed */
 
646
        if (lockSegmentReturn)
 
647
                return (FALSE);                         /* Driver could not lock segment */
 
648
        return (TRUE);
 
649
}
 
650
#define sense   u_sense.Sense