2 * This file has been modified for the cdrkit suite.
4 * The behaviour and appearence of the program code below can differ to a major
5 * extent from the version distributed by the original author(s).
7 * For details, see Changelog file distributed with the cdrkit package. If you
8 * received this file from another source then ask the distributing person for
9 * a log of modifications.
13 /* @(#)scsi-os2.c 1.25 04/01/15 Copyright 1998 J. Schilling, C. Wohlgemuth */
15 * Interface for the OS/2 ASPI-Router ASPIROUT.SYS ((c) D. Dorau).
16 * This additional driver is a prerequisite for using cdrecord.
17 * Get it from HOBBES or LEO.
19 * Warning: you may change this source, but if you do that
20 * you need to change the _usal_version and _usal_auth* string below.
21 * You may not return "schily" for an SCG_AUTHOR request anymore.
22 * Choose your name instead of "schily" and make clear that the version
23 * string is related to a modified source.
25 * XXX it currently uses static SRB and for this reason is not reentrant
27 * Copyright (c) 1998 J. Schilling
28 * Copyright (c) 1998 C. Wohlgemuth for this interface.
31 * This program is free software; you can redistribute it and/or modify
32 * it under the terms of the GNU General Public License version 2
33 * as published by the Free Software Foundation.
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
40 * You should have received a copy of the GNU General Public License along with
41 * this program; see the file COPYING. If not, write to the Free Software
42 * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
50 #include "usal/srb_os2.h"
53 * Warning: you may change this source, but if you do that
54 * you need to change the _usal_version and _usal_auth* string below.
55 * You may not return "schily" for an SCG_AUTHOR request anymore.
56 * Choose your name instead of "schily" and make clear that the version
57 * string is related to a modified source.
59 static char _usal_trans_version[] = "scsi-os2.c-1.25"; /* The version for this transport*/
61 #define FILE_OPEN 0x0001
62 #define OPEN_SHARE_DENYREADWRITE 0x0010
63 #define OPEN_ACCESS_READWRITE 0x0002
64 #define DC_SEM_SHARED 0x01
65 #define OBJ_TILE 0x0040
66 #define PAG_READ 0x0001
67 #define PAG_WRITE 0x0002
68 #define PAG_COMMIT 0x0010
70 typedef unsigned long LHANDLE;
71 typedef unsigned long ULONG;
72 typedef unsigned char *PSZ;
73 typedef unsigned short USHORT;
74 typedef unsigned char UCHAR;
76 typedef LHANDLE HFILE;
79 #define MAX_SCG 16 /* Max # of SCSI controllers */
86 #define usallocal(p) ((struct usal_local *)((p)->local))
88 #define MAX_DMA_OS2 (63*1024) /* ASPI-Router allows up to 64k */
90 static void *buffer = NULL;
91 static HFILE driver_handle = 0;
92 static HEV postSema = 0;
94 static BOOL open_driver(SCSI *usalp);
95 static BOOL close_driver(void);
96 static ULONG wait_post(ULONG ulTimeOut);
97 static BOOL init_buffer(void* mem);
98 static void exit_func(void);
99 static void set_error(SRB *srb, struct usal_cmd *sp);
106 fprintf(stderr, "Cannot close OS/2-ASPI-Router!\n");
110 * Return version information for the low level SCSI transport code.
111 * This has been introduced to make it easier to trace down problems
115 usalo_version(SCSI *usalp, int what)
117 if (usalp != (SCSI *)0) {
121 return (_usal_trans_version);
123 * If you changed this source, you are not allowed to
124 * return "schily" for the SCG_AUTHOR request.
127 return (_usal_auth_cdrkit);
136 usalo_help(SCSI *usalp, FILE *f)
138 __usal_help(f, "ASPI", "Generic transport independent SCSI",
139 "", "bus,target,lun", "1,2,0", TRUE, FALSE);
144 usalo_open(SCSI *usalp, char *device)
146 int busno = usal_scsibus(usalp);
147 int tgt = usal_target(usalp);
148 int tlun = usal_lun(usalp);
150 if (busno >= MAX_SCG || tgt >= MAX_TGT || tlun >= MAX_LUN) {
153 snprintf(usalp->errstr, SCSI_ERRSTR_SIZE,
154 "Illegal value for busno, target or lun '%d,%d,%d'",
159 if ((device != NULL && *device != '\0') || (busno == -2 && tgt == -2)) {
162 snprintf(usalp->errstr, SCSI_ERRSTR_SIZE,
163 "Open by 'devname' not supported on this OS");
167 if (usalp->local == NULL) {
168 usalp->local = malloc(sizeof (struct usal_local));
169 if (usalp->local == NULL)
173 if (!open_driver(usalp)) /* Try to open ASPI-Router */
175 atexit(exit_func); /* Install Exit Function which closes the ASPI-Router */
184 usalo_close(SCSI *usalp)
191 usalo_maxdma(SCSI *cgp, long amt)
193 long maxdma = MAX_DMA_OS2;
198 usalo_getbuf(SCSI *usalp, long amt)
203 fprintf((FILE *)usalp->errfile, "usalo_getbuf: %ld bytes\n", amt);
205 rc = DosAllocMem(&buffer, amt, OBJ_TILE | PAG_READ | PAG_WRITE | PAG_COMMIT);
208 fprintf((FILE *)usalp->errfile, "Cannot allocate buffer.\n");
211 usalp->bufbase = buffer;
214 fprintf((FILE *)usalp->errfile, "Buffer allocated at: 0x%x\n", usalp->bufbase);
218 if (init_buffer(usalp->bufbase))
219 return (usalp->bufbase);
221 fprintf((FILE *)usalp->errfile, "Cannot lock memory buffer.\n");
222 return ((void *)0); /* Error */
226 usalo_freebuf(SCSI *usalp)
228 if (usalp->bufbase && DosFreeMem(usalp->bufbase)) {
229 fprintf((FILE *)usalp->errfile,
230 "Cannot free buffer memory for ASPI-Router!\n"); /* Free our memory buffer if not already done */
232 if (buffer == usalp->bufbase)
234 usalp->bufbase = NULL;
238 usalo_havebus(SCSI *usalp, int busno)
243 if (busno < 0 || busno >= MAX_SCG)
250 usalo_fileno(SCSI *usalp, int busno, int tgt, int tlun)
252 if (busno < 0 || busno >= MAX_SCG ||
253 tgt < 0 || tgt >= MAX_TGT ||
254 tlun < 0 || tlun >= MAX_LUN)
265 usalo_initiator_id(SCSI *usalp)
271 usalo_isatapi(SCSI *usalp)
278 usalo_reset(SCSI *usalp, int what)
280 ULONG rc; /* return value */
284 static SRB SRBlock; /* XXX makes it non reentrant */
286 if (what == SCG_RESET_NOP)
288 if (what != SCG_RESET_BUS) {
293 * XXX Does this reset TGT or BUS ???
295 SRBlock.cmd = SRB_Reset; /* reset device */
296 SRBlock.ha_num = usal_scsibus(usalp); /* host adapter number */
297 SRBlock.flags = SRB_Post; /* posting enabled */
298 SRBlock.u.res.target = usal_target(usalp); /* target id */
299 SRBlock.u.res.lun = usal_lun(usalp); /* target LUN */
301 rc = DosDevIOCtl(driver_handle, 0x92, 0x02, (void*) &SRBlock, sizeof (SRB), &cbParam,
302 (void*) &SRBlock, sizeof (SRB), &cbreturn);
304 fprintf((FILE *)usalp->errfile,
305 "DosDevIOCtl() failed in resetDevice.\n");
306 return (1); /* DosDevIOCtl failed */
308 success = wait_post(40000); /** wait for SRB being processed */
312 if (SRBlock.status != SRB_Done)
315 fprintf((FILE *)usalp->errfile,
316 "resetDevice of host: %d target: %d lun: %d successful.\n", usal_scsibus(usalp), usal_target(usalp), usal_lun(usalp));
317 fprintf((FILE *)usalp->errfile,
318 "SRBlock.ha_status: 0x%x, SRBlock.target_status: 0x%x, SRBlock.satus: 0x%x\n",
319 SRBlock.u.cmd.ha_status, SRBlock.u.cmd.target_status, SRBlock.status);
328 set_error(SRB *srb, struct usal_cmd *sp)
330 switch (srb->status) {
332 case SRB_InvalidCmd: /* 0x80 Invalid SCSI request */
333 case SRB_InvalidHA: /* 0x81 Invalid host adapter number */
334 case SRB_BadDevice: /* 0x82 SCSI device not installed */
335 sp->error = SCG_FATAL;
336 sp->ux_errno = EINVAL; /* Should we ever return != EIO */
341 case SRB_Busy: /* 0x00 SCSI request in progress */
342 case SRB_Aborted: /* 0x02 SCSI aborted by host */
343 case SRB_BadAbort: /* 0x03 Unable to abort SCSI request */
344 case SRB_Error: /* 0x04 SCSI request completed with error */
346 sp->error = SCG_RETRYABLE;
353 usalo_send(SCSI *usalp)
355 struct usal_cmd *sp = usalp->scmd;
356 ULONG rc; /* return value */
357 static SRB SRBlock; /* XXX makes it non reentrant */
362 if (usalp->fd < 0) { /* Set in usalo_open() */
363 sp->error = SCG_FATAL;
367 if (sp->cdb_len > sizeof (SRBlock.u.cmd.cdb_st)) { /* commandsize too big */
368 sp->error = SCG_FATAL;
369 sp->ux_errno = EINVAL;
370 fprintf((FILE *)usalp->errfile,
371 "sp->cdb_len > SRBlock.u.cmd.cdb_st. Fatal error in usalo_send, exiting...\n");
375 /* clear command block */
376 fillbytes((caddr_t)&SRBlock.u.cmd.cdb_st, sizeof (SRBlock.u.cmd.cdb_st), '\0');
377 /* copy cdrecord command into SRB */
378 movebytes(&sp->cdb, &SRBlock.u.cmd.cdb_st, sp->cdb_len);
380 /* Build SRB command block */
381 SRBlock.cmd = SRB_Command;
382 SRBlock.ha_num = usal_scsibus(usalp); /* host adapter number */
384 SRBlock.flags = SRB_Post; /* flags */
386 SRBlock.u.cmd.target = usal_target(usalp); /* Target SCSI ID */
387 SRBlock.u.cmd.lun = usal_lun(usalp); /* Target SCSI LUN */
388 SRBlock.u.cmd.data_len = sp->size; /* # of bytes transferred */
389 SRBlock.u.cmd.data_ptr = 0; /* pointer to data buffer */
390 SRBlock.u.cmd.sense_len = sp->sense_len; /* length of sense buffer */
392 SRBlock.u.cmd.link_ptr = 0; /* pointer to next SRB */
393 SRBlock.u.cmd.cdb_len = sp->cdb_len; /* SCSI command length */
395 /* Specify direction */
396 if (sp->flags & SCG_RECV_DATA) {
397 SRBlock.flags |= SRB_Read;
400 SRBlock.flags |= SRB_Write;
401 if (usalp->bufbase != sp->addr) { /* Copy only if data not in ASPI-Mem */
402 movebytes(sp->addr, usalp->bufbase, sp->size);
405 SRBlock.flags |= SRB_NoTransfer;
408 sp->error = SCG_NO_ERROR;
410 sp->u_scb.cmd_scb[0] = 0;
413 /* execute SCSI command */
414 rc = DosDevIOCtl(driver_handle, 0x92, 0x02,
415 (void*) &SRBlock, sizeof (SRB), &cbParam,
416 (void*) &SRBlock, sizeof (SRB), &cbreturn);
418 if (rc) { /* An error occured */
419 fprintf((FILE *)usalp->errfile,
420 "DosDevIOCtl() in sendCommand failed.\n");
421 sp->error = SCG_FATAL;
422 sp->ux_errno = EIO; /* Später vielleicht errno einsetzen */
425 /* Wait until the command is processed */
426 rc = wait_post(sp->timeout*1000);
427 if (rc) { /* An error occured */
430 sp->error = SCG_TIMEOUT;
432 fprintf((FILE *)usalp->errfile,
433 "Timeout during SCSI-Command.\n");
436 sp->error = SCG_FATAL;
438 fprintf((FILE *)usalp->errfile,
439 "Fatal Error during DosWaitEventSem().\n");
443 /* The command is processed */
444 if (SRBlock.status == SRB_Done) { /* succesful completion */
446 fprintf((FILE *)usalp->errfile,
447 "Command successful finished. SRBlock.status=0x%x\n\n", SRBlock.status);
451 if (sp->flags & SCG_RECV_DATA) { /* We read data */
452 if (sp->addr && sp->size) {
453 if (usalp->bufbase != sp->addr) /* Copy only if data not in ASPI-Mem */
454 movebytes(usalp->bufbase, sp->addr, SRBlock.u.cmd.data_len);
455 ptr = (UCHAR*)sp->addr;
456 sp->resid = sp->size - SRBlock.u.cmd.data_len; /*nicht übertragene bytes. Korrekt berechnet???*/
458 } /* end of if (sp->flags & SCG_RECV_DATA) */
459 if (SRBlock.u.cmd.target_status == SRB_CheckStatus) { /* Sense data valid */
460 sp->sense_count = (int)SRBlock.u.cmd.sense_len;
461 if (sp->sense_count > sp->sense_len)
462 sp->sense_count = sp->sense_len;
464 ptr = (UCHAR*)&SRBlock.u.cmd.cdb_st;
465 ptr += SRBlock.u.cmd.cdb_len;
467 fillbytes(&sp->u_sense.Sense, sizeof (sp->u_sense.Sense), '\0');
468 movebytes(ptr, &sp->u_sense.Sense, sp->sense_len);
470 sp->u_scb.cmd_scb[0] = SRBlock.u.cmd.target_status;
471 sp->ux_errno = EIO; /* Später differenzieren? */
475 /* SCSI-Error occured */
476 set_error(&SRBlock, sp);
478 if (SRBlock.u.cmd.target_status == SRB_CheckStatus) { /* Sense data valid */
479 sp->sense_count = (int)SRBlock.u.cmd.sense_len;
480 if (sp->sense_count > sp->sense_len)
481 sp->sense_count = sp->sense_len;
483 ptr = (UCHAR*)&SRBlock.u.cmd.cdb_st;
484 ptr += SRBlock.u.cmd.cdb_len;
486 fillbytes(&sp->u_sense.Sense, sizeof (sp->u_sense.Sense), '\0');
487 movebytes(ptr, &sp->u_sense.Sense, sp->sense_len);
489 sp->u_scb.cmd_scb[0] = SRBlock.u.cmd.target_status;
491 if (sp->flags & SCG_RECV_DATA) {
492 if (sp->addr && sp->size) {
493 if (usalp->bufbase != sp->addr) /* Copy only if data not in ASPI-Mem */
494 movebytes(usalp->bufbase, sp->addr, SRBlock.u.cmd.data_len);
498 sp->resid = SRBlock.u.cmd.data_len; /* XXXXX Got no Data ????? */
500 sp->resid = sp->size - SRBlock.u.cmd.data_len;
506 /***************************************************************************
508 * BOOL open_driver() *
510 * Opens the ASPI Router device driver and sets device_handle. *
513 * FALSE - Unsuccessful opening of device driver *
515 * Preconditions: ASPI Router driver has be loaded *
517 ***************************************************************************/
519 open_driver(SCSI *usalp)
521 ULONG rc; /* return value */
522 ULONG ActionTaken; /* return value */
523 USHORT openSemaReturn; /* return value */
527 if (driver_handle) /* ASPI-Router already opened */
530 rc = DosOpen((PSZ) "aspirou$", /* open driver*/
536 OPEN_SHARE_DENYREADWRITE | OPEN_ACCESS_READWRITE,
539 fprintf((FILE *)usalp->errfile,
540 "Cannot open ASPI-Router!\n");
542 return (FALSE); /* opening failed -> return false*/
546 if (DosCreateEventSem(NULL, &postSema, /* create event semaphore */
548 DosClose(driver_handle);
549 fprintf((FILE *)usalp->errfile,
550 "Cannot create event semaphore!\n");
554 rc = DosDevIOCtl(driver_handle, 0x92, 0x03, /* pass semaphore handle */
555 (void*) &postSema, sizeof (HEV), /* to driver */
556 &cbParam, (void*) &openSemaReturn,
557 sizeof (USHORT), &cbreturn);
559 if (rc||openSemaReturn) { /* Error */
560 DosCloseEventSem(postSema);
561 DosClose(driver_handle);
567 /***************************************************************************
569 * BOOL close_driver() *
571 * Closes the device driver *
574 * FALSE - Unsuccessful closing of device driver *
576 * Preconditions: ASPI Router driver has be opened with open_driver *
578 ***************************************************************************/
582 ULONG rc; /* return value */
585 rc = DosClose(driver_handle);
587 return (FALSE); /* closing failed -> return false */
589 if (DosCloseEventSem(postSema))
590 fprintf(stderr, "Cannot close event semaphore!\n");
591 if (buffer && DosFreeMem(buffer)) {
593 "Cannot free buffer memory for ASPI-Router!\n"); /* Free our memory buffer if not already done */
601 wait_post(ULONG ulTimeOut)
604 ULONG rc; /* return value */
606 /* rc = DosWaitEventSem(postSema, -1);*/ /* wait forever*/
607 rc = DosWaitEventSem(postSema, ulTimeOut);
608 DosResetEventSem(postSema, &count);
613 init_buffer(void *mem)
615 ULONG rc; /* return value */
616 USHORT lockSegmentReturn; /* return value */
620 rc = DosDevIOCtl(driver_handle, 0x92, 0x04, /* pass buffers pointer */
621 (void*) mem, sizeof (void*), /* to driver */
622 &cbParam, (void*) &lockSegmentReturn,
623 sizeof (USHORT), &cbreturn);
625 return (FALSE); /* DosDevIOCtl failed */
626 if (lockSegmentReturn)
627 return (FALSE); /* Driver could not lock segment */
630 #define sense u_sense.Sense