Sophie

Sophie

distrib > Mandriva > 2010.1 > x86_64 > by-pkgid > 965e33040dd61030a94f0eb89877aee8 > files > 5349

howto-html-en-20080722-2mdv2010.1.noarch.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
<HEAD>
 <META NAME="GENERATOR" CONTENT="SGML-Tools 1.0.9">
 <TITLE>The Linux SCSI programming HOWTO: Example Using Sense Buffer</TITLE>
 <LINK HREF="SCSI-Programming-HOWTO-12.html" REL=next>
 <LINK HREF="SCSI-Programming-HOWTO-10.html" REL=previous>
 <LINK HREF="SCSI-Programming-HOWTO.html#toc11" REL=contents>
</HEAD>
<BODY>
<A HREF="SCSI-Programming-HOWTO-12.html">Next</A>
<A HREF="SCSI-Programming-HOWTO-10.html">Previous</A>
<A HREF="SCSI-Programming-HOWTO.html#toc11">Contents</A>
<HR>
<H2><A NAME="s11">11. Example Using Sense Buffer</A></H2>

<P>Here we will use the TEST UNIT READY command to check whether media is
loaded into our device. The header declarations and function
<CODE>handle_SCSI_cmd</CODE> from the inquiry example will be needed as well.
<P>
<P>
<BLOCKQUOTE><CODE>
<PRE>
                        Table 73: TEST UNIT READY Command
+=====-========-========-========-========-========-========-========-========+
|  Bit|   7    |   6    |   5    |   4    |   3    |   2    |   1    |   0    |
|Byte |        |        |        |        |        |        |        |        |
|=====+=======================================================================|
| 0   |                           Operation Code (00h)                        |
|-----+-----------------------------------------------------------------------|
| 1   | Logical Unit Number      |                  Reserved                  |
|-----+-----------------------------------------------------------------------|
| 2   |                           Reserved                                    |
|-----+-----------------------------------------------------------------------|
| 3   |                           Reserved                                    |
|-----+-----------------------------------------------------------------------|
| 4   |                           Reserved                                    |
|-----+-----------------------------------------------------------------------|
| 5   |                           Control                                     |
+=============================================================================+
</PRE>
</CODE></BLOCKQUOTE>
<P>
<P>
<P>Here is the function which implements it:
<P>
<BLOCKQUOTE><CODE>
<PRE>
#define TESTUNITREADY_CMD 0
#define TESTUNITREADY_CMDLEN 6

#define ADD_SENSECODE 12
#define ADD_SC_QUALIFIER 13
#define NO_MEDIA_SC 0x3a
#define NO_MEDIA_SCQ 0x00

int TestForMedium ( void )
{
  /* request READY status */
  static unsigned char cmdblk [TESTUNITREADY_CMDLEN] = {
      TESTUNITREADY_CMD, /* command */
                      0, /* lun/reserved */
                      0, /* reserved */
                      0, /* reserved */
                      0, /* reserved */
                      0};/* control */

  memcpy( cmd + SCSI_OFF, cmdblk, sizeof(cmdblk) );

  /*
   * +------------------+
   * | struct sg_header | &lt;- cmd
   * +------------------+
   * | copy of cmdblk   | &lt;- cmd + SCSI_OFF
   * +------------------+
   */

  if (handle_SCSI_cmd(sizeof(cmdblk), 0, cmd, 
                            0, NULL)) {
      fprintf (stderr, "Test unit ready failed\n");
      exit(2);
  }

  return
   *(((struct sg_header*)cmd)->sense_buffer +ADD_SENSECODE) != 
                                                        NO_MEDIA_SC ||
   *(((struct sg_header*)cmd)->sense_buffer +ADD_SC_QUALIFIER) != 
                                                        NO_MEDIA_SCQ;
}
</PRE>
</CODE></BLOCKQUOTE>
<P>Combined with this <CODE>main</CODE> function we can do the check.
<P>
<BLOCKQUOTE><CODE>
<PRE>
void main( void )
{
  fd = open(DEVICE, O_RDWR);
  if (fd &lt; 0) {
    fprintf( stderr, "Need read/write permissions for "DEVICE".\n" );
    exit(1);
  }

  /* look if medium is loaded */

  if (!TestForMedium()) {
    printf("device is unloaded\n");
  } else {
    printf("device is loaded\n");
  }
}
</PRE>
</CODE></BLOCKQUOTE>
<P>
<P>The file <CODE>generic_demo.c</CODE> from the appendix contains both examples.
<P>
<HR>
<A HREF="SCSI-Programming-HOWTO-12.html">Next</A>
<A HREF="SCSI-Programming-HOWTO-10.html">Previous</A>
<A HREF="SCSI-Programming-HOWTO.html#toc11">Contents</A>
</BODY>
</HTML>