The Linux SCSI programming HOWTO: Example Using Sense Buffer

Next Previous Contents

## 11. Example Using Sense Buffer

Here we will use the TEST UNIT READY command to check whether media is loaded into our device. The header declarations and function `handle_SCSI_cmd` from the inquiry example will be needed as well.

> `

>                             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                                     |
>     +=============================================================================+

> `

Here is the function which implements it:

> `

>     #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 | <- cmd
>        * +------------------+
>        * | copy of cmdblk   | <- 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;
>     }

> `

Combined with this `main` function we can do the check.

> `

>     void main( void )
>     {
>       fd = open(DEVICE, O_RDWR);
>       if (fd < 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");
>       }
>     }

> `

The file `generic_demo.c` from the appendix contains both examples.

Next Previous Contents