home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 5 / FreshFish_July-August1994.bin / bbs / dev / rkrm.lha / RKRM / SCSI / SCSI_Direct.c next >
Encoding:
C/C++ Source or Header  |  1992-09-03  |  10.7 KB  |  263 lines

  1. /*
  2.  * Copyright (c) 1992 Commodore-Amiga, Inc.
  3.  * 
  4.  * This example is provided in electronic form by Commodore-Amiga, Inc. for 
  5.  * use with the "Amiga ROM Kernel Reference Manual: Devices", 3rd Edition, 
  6.  * published by Addison-Wesley (ISBN 0-201-56775-X).
  7.  * 
  8.  * The "Amiga ROM Kernel Reference Manual: Devices" contains additional 
  9.  * information on the correct usage of the techniques and operating system 
  10.  * functions presented in these examples.  The source and executable code 
  11.  * of these examples may only be distributed in free electronic form, via 
  12.  * bulletin board or as part of a fully non-commercial and freely 
  13.  * redistributable diskette.  Both the source and executable code (including 
  14.  * comments) must be included, without modification, in any copy.  This 
  15.  * example may not be published in printed form or distributed with any
  16.  * commercial product.  However, the programming techniques and support
  17.  * routines set forth in these examples may be used in the development
  18.  * of original executable software products for Commodore Amiga computers.
  19.  * 
  20.  * All other rights reserved.
  21.  * 
  22.  * This example is provided "as-is" and is subject to change; no
  23.  * warranties are made.  All use is at your own risk. No liability or
  24.  * responsibility is assumed.
  25.  *
  26.  *****************************************************************************
  27.  *
  28.  *
  29.  * SCSI_Direct.c
  30.  *
  31.  * The following program demonstrates the use of the HD_SCSICmd to send a
  32.  * MODE SENSE to a unit on the requested device (default scsi.device).  This
  33.  * code can be easily modified to send other commands to the drive.
  34.  *
  35.  * Compile with SAS C 5.10  lc -b1 -cfistq -v -y -L
  36.  *
  37.  * Run from CLI only
  38.  */
  39.  
  40. #include <exec/types.h>
  41. #include <exec/memory.h>
  42. #include <exec/io.h>
  43. #include <devices/scsidisk.h>
  44. #include <dos/dosextens.h>
  45.  
  46. #include <clib/exec_protos.h>
  47. #include <clib/alib_protos.h>
  48.  
  49. #include <stdlib.h>
  50. #include <stdio.h>
  51.  
  52. #ifdef LATTICE
  53. int CXBRK(void) { return(0); }  /* Disable SAS CTRL/C handling */
  54. int chkabort(void) { return(0); }  /* really */
  55. #endif
  56.  
  57. #define BUFSIZE 256
  58.  
  59. UBYTE *buffer;                  /* a data buffer used for mode sense data */
  60. struct IOStdReq SCSIReq;        /* a standard IORequest structure */
  61. struct SCSICmd Cmd;             /* where the actual SCSI command goes */
  62. UBYTE  Sense[20];               /* buffer for request sense data */
  63. struct MsgPort Port;            /* our ReplyPort */
  64.  
  65. void ShowSenseData(void);
  66.  
  67. static UBYTE TestReady[] = { 0,0,0,0,0,0 };     /* not used but here for  */
  68. static UBYTE StartUnit[] = { 0x1b,0,0,0,1,0 };  /* illustration of other  */
  69. static UBYTE StopUnit[] =  { 0x1b,0,0,0,0,0 };  /* commands.              */
  70.  
  71. static UBYTE ModeSense[]={ 0x1a,0,0xff,0,254,0 }; /* the command being sent */
  72.  
  73. void main(int argc, char **argv)
  74. {
  75. int unit,tval,i;
  76. char *dname = "scsi.device";
  77. UBYTE *tbuf;
  78.  
  79. if ((argc < 2) || (argc > 3))
  80.     {
  81.     printf("Usage: %s SCSIunit# [xxxx.device]\n",argv[0]);
  82.     exit(100);
  83.     }
  84.  
  85. unit = atoi( argv[1] );
  86. if (argc == 3)
  87.     dname = argv[2];
  88.  
  89. buffer = (UBYTE *) AllocMem(BUFSIZE, MEMF_PUBLIC|MEMF_CLEAR);
  90.  
  91. if (!buffer)
  92.     {
  93.     printf("Couldn't get memory\n");
  94.     exit(100);
  95.     }
  96.  
  97. Port.mp_Node.ln_Pri = 0;                        /* setup the ReplyPort */
  98. Port.mp_SigBit      = AllocSignal(-1);
  99. Port.mp_SigTask     = (struct Task *)FindTask(0);
  100. NewList( &(Port.mp_MsgList) );
  101.  
  102. SCSIReq.io_Message.mn_ReplyPort = &Port;
  103.  
  104. if (OpenDevice( dname, unit, &SCSIReq, 0))
  105.     {
  106.     printf("Couldn't open unit %ld on %s\n",unit,dname);
  107.     FreeMem( buffer,BUFSIZE );
  108.     exit(100);
  109.     }
  110.  
  111. SCSIReq.io_Length  = sizeof(struct SCSICmd);
  112. SCSIReq.io_Data    = (APTR)&Cmd;
  113. SCSIReq.io_Command = HD_SCSICMD;        /* the command we are sending   */
  114.  
  115. Cmd.scsi_Data = (UWORD *)buffer;        /* where we put mode sense data */
  116. Cmd.scsi_Length = 254;          /* how much we will accept      */
  117. Cmd.scsi_CmdLength = 6;         /* length of the command        */
  118. Cmd.scsi_Flags = SCSIF_AUTOSENSE|SCSIF_READ;
  119.                                 /* do automatic REQUEST_SENSE   */
  120.                                 /* set expected data direction  */
  121. Cmd.scsi_SenseData =(UBYTE *)Sense;     /* where sense data will go     */
  122. Cmd.scsi_SenseLength = 18;              /* how much we will accept      */
  123. Cmd.scsi_SenseActual = 0;               /* how much has been received   */
  124.  
  125. Cmd.scsi_Command=(UBYTE *)ModeSense;/* issuing a MODE_SENSE command     */
  126. DoIO( &SCSIReq );                       /* send it to the device driver */
  127.  
  128. if (Cmd.scsi_Status)
  129.     ShowSenseData();      /* if bad status then show it */
  130.  
  131. else
  132.     {
  133.     printf("\nBlock descriptor header\n");
  134.     printf("=======================\n");
  135.     printf("Mode Sense data length  = %d\n",(short)buffer[0]);
  136.     printf("Block descriptor length = %d\n",(short)buffer[3]);
  137.     tbuf = &buffer[4];
  138.     printf("Density code            = %d\n",(short)tbuf[0]);
  139.     tval = (tbuf[1]<<16) + (tbuf[2]<<8) + tbuf[3];
  140.     printf("Number of blocks        = %ld\n",tval);
  141.     tval = (tbuf[5]<<16) + (tbuf[6]<<8) + tbuf[7];
  142.     printf("Block size              = %ld\n",tval);
  143.  
  144.     tbuf += buffer[3];          /* move to page descriptors */
  145.  
  146.     while ((tbuf - buffer) < buffer[0])
  147.            {
  148.  
  149.            switch (tbuf[0] & 0x7f)
  150.                    {
  151.                    case 1:
  152.                           printf("\nError Recovery Parameters\n");
  153.                           printf("=========================\n");
  154.                           printf("Page length             = %d\n",(short)tbuf[1]);
  155.                           printf("DISABLE CORRECTION      = %d\n",(short)tbuf[2]&1);
  156.                           printf("DISABLE XFER ON ERROR   = %d\n",(short)(tbuf[2]>>1)&1);
  157.                           printf("POST ERROR              = %d\n",(short)(tbuf[2]>>2)&1);
  158.                           printf("ENABLE EARLY CORRECTION = %d\n",(short)(tbuf[2]>>3)&1);
  159.                           printf("READ CONTINUOUS         = %d\n",(short)(tbuf[2]>>4)&1);
  160.                           printf("TRANSFER BLOCK          = %d\n",(short)(tbuf[2]>>5)&1);
  161.                           printf("AUTO READ REALLOCATION  = %d\n",(short)(tbuf[2]>>6)&1);
  162.                           printf("AUTO WRITE REALLOCATION = %d\n",(short)(tbuf[2]>>7)&1);
  163.                           printf("Retry count             = %d\n",(short)tbuf[3]);
  164.                           printf("Correction span         = %d\n",(short)tbuf[4]);
  165.                           printf("Head offset count       = %d\n",(short)tbuf[5]);
  166.                           printf("Data strobe offset count= %d\n",(short)tbuf[6]);
  167.                           printf("Recovery time limit     = %d\n",(short)tbuf[7]);
  168.  
  169.                           tbuf += tbuf[1]+2;
  170.                           break;
  171.  
  172.                    case 2:
  173.                           printf("\nDisconnect/Reconnect Control\n");
  174.                           printf("============================\n");
  175.                           printf("Page length             = %d\n",(short)tbuf[1]);
  176.                           printf("Buffer full ratio       = %d\n",(short)tbuf[2]);
  177.                           printf("Buffer empty ratio      = %d\n",(short)tbuf[3]);
  178.                           tval = (tbuf[4]<<8)+tbuf[5];
  179.                           printf("Bus inactivity limit    = %d\n",tval);
  180.                           tval = (tbuf[6]<<8)+tbuf[7];
  181.                           printf("Disconnect time limit   = %d\n",tval);
  182.                           tval = (tbuf[8]<<8)+tbuf[9];
  183.                           printf("Connect time limit      = %d\n",tval);
  184.                           tval = (tbuf[10]<<8)+tbuf[11];
  185.                           printf("Maximum burst size      = %d\n",tval);
  186.                           printf("Disable disconnection   = %d\n",(short)tbuf[12]&1);
  187.  
  188.                           tbuf += tbuf[1]+2;
  189.                           break;
  190.  
  191.                    case 3:
  192.                           printf("\nDevice Format Parameters\n");
  193.                           printf("========================\n");
  194.                           printf("Page length             = %d\n",(short)tbuf[1]);
  195.                           tval = (tbuf[2]<<8)+tbuf[3];
  196.                           printf("Tracks per zone         = %d\n",tval);
  197.                           tval = (tbuf[4]<<8)+tbuf[5];
  198.                           printf("Alternate sectors/zone  = %d\n",tval);
  199.                           tval = (tbuf[6]<<8)+tbuf[7];
  200.                           printf("Alternate tracks/zone   = %d\n",tval);
  201.                           tval = (tbuf[8]<<8)+tbuf[9];
  202.                           printf("Alternate tracks/volume = %d\n",tval);
  203.                           tval = (tbuf[10]<<8)+tbuf[11];
  204.                           printf("Sectors per track       = %d\n",tval);
  205.                           tval = (tbuf[12]<<8)+tbuf[13];
  206.                           printf("Bytes per sector        = %d\n",tval);
  207.                           tval = (tbuf[14]<<8)+tbuf[15];
  208.                           printf("Interleave              = %d\n",tval);
  209.                           tval = (tbuf[16]<<8)+tbuf[17];
  210.                           printf("Track skew factor       = %d\n",tval);
  211.                           tval = (tbuf[18]<<8)+tbuf[19];
  212.                           printf("Cylinder skew factor    = %d\n",tval);
  213.  
  214.                           tbuf += tbuf[1]+2;
  215.                           break;
  216.  
  217.                    case 4:
  218.                           printf("\nDrive Geometry Parameters\n");
  219.                           printf("=========================\n");
  220.                           printf("Page length             = %d\n",(short)tbuf[1]);
  221.                           tval = (tbuf[2]<<16)+(tbuf[3]<<8)+tbuf[4];
  222.                           printf("Number of cylinders     = %ld\n",tval);
  223.                           printf("Number of heads         = %d\n",(short)tbuf[5]);
  224.                           tval = (tbuf[6]<<16)+(tbuf[7]<<8)+tbuf[8];
  225.                           printf("Start write precomp     = %ld\n",tval);
  226.                           tval = (tbuf[9]<<16)+(tbuf[10]<<8)+tbuf[11];
  227.                           printf("Start reduced write     = %ld\n",tval);
  228.                           tval = (tbuf[12]<<8)+tbuf[13];
  229.                           printf("Drive step rate         = %d\n",tval);
  230.                           tval = (tbuf[14]<<16)+(tbuf[15]<<8)+tbuf[16];
  231.                           printf("Landing zone cylinder   = %ld\n",tval);
  232.  
  233.                           tbuf += tbuf[1]+2;
  234.                           break;
  235.  
  236.                    default:
  237.                            printf("\nVendor Unique Page Code %2x\n",(short)tbuf[0]);
  238.                            printf("==========================\n");
  239.                            for (i=0; i<=tbuf[1]+1; i++ )
  240.                                 printf("%x ",(short)tbuf[i]);
  241.  
  242.                            printf("\n");
  243.                            tbuf += tbuf[1]+2;
  244.                    }
  245.            }
  246.     }
  247.  
  248. CloseDevice( &SCSIReq );
  249. FreeMem( buffer, BUFSIZE );
  250. FreeSignal(Port.mp_SigBit);
  251. }
  252.  
  253. void ShowSenseData(void)
  254. {
  255. int i;
  256.  
  257. for (i=0; i<18; i++)
  258.      printf("%x ",(int)Sense[i]);
  259.  
  260. printf("\n");
  261. }
  262.  
  263.