home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / doc / sg3-utils / examples / sg_sat_chk_power.c < prev    next >
Encoding:
C/C++ Source or Header  |  2007-01-30  |  7.6 KB  |  217 lines

  1. /*
  2.  * Copyright (c) 2006-2007 Douglas Gilbert.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. The name of the author may not be used to endorse or promote products
  14.  *    derived from this software without specific prior written permission.
  15.  *
  16.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  17.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  20.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  22.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  26.  * SUCH DAMAGE.
  27.  *
  28.  */
  29.  
  30. #include <unistd.h>
  31. #include <fcntl.h>
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <string.h>
  35. #include <errno.h>
  36. #include <sys/ioctl.h>
  37. #include <sys/types.h>
  38. #include <sys/stat.h>
  39. #include "sg_lib.h"
  40. #include "sg_io_linux.h"
  41.  
  42. /* This program performs a ATA PASS-THROUGH (16) SCSI command in order
  43.    to perform an ATA CHECK POWER MODE command. See http://www.t10.org
  44.    SAT draft at time of writing: sat-r09.pdf
  45.  
  46.    Invocation: sg_sat_chk_power [-v] [-V] <device>
  47.  
  48. */
  49.  
  50. #define SAT_ATA_PASS_THROUGH16 0x85
  51. #define SAT_ATA_PASS_THROUGH16_LEN 16
  52. #define SAT_ATA_RETURN_DESC 9  /* ATA Return (sense) Descriptor */
  53.  
  54. #define ATA_CHECK_POWER_MODE 0xe5
  55.  
  56. #define EBUFF_SZ 256
  57.  
  58. static char * version_str = "1.03 20070129";
  59.  
  60. int main(int argc, char * argv[])
  61. {
  62.     int sg_fd, k;
  63.     unsigned char aptCmdBlk[SAT_ATA_PASS_THROUGH16_LEN] =
  64.                 {SAT_ATA_PASS_THROUGH16, 0, 0, 0, 0, 0, 0, 0,
  65.                  0, 0, 0, 0, 0, 0, 0, 0};
  66.     sg_io_hdr_t io_hdr;
  67.     char * file_name = 0;
  68.     char ebuff[EBUFF_SZ];
  69.     unsigned char sense_buffer[64];
  70.     int verbose = 0;
  71.     int extend = 0;
  72.     int chk_cond = 1;   /* set to 1 to read register(s) back */
  73.     int protocol = 3;   /* non-dat data-in */
  74.     int t_dir = 1;      /* 0 -> to device, 1 -> from device */
  75.     int byte_block = 1; /* 0 -> bytes, 1 -> 512 byte blocks */
  76.     int t_length = 0;   /* 0 -> no data transferred, 2 -> sector count */
  77.     const unsigned char * ucp = NULL;
  78.  
  79.     for (k = 1; k < argc; ++k) {
  80.         if (0 == strcmp(argv[k], "-v"))
  81.             ++verbose;
  82.         else if (0 == strcmp(argv[k], "-vv"))
  83.             verbose += 2;
  84.         else if (0 == strcmp(argv[k], "-vvv"))
  85.             verbose += 3;
  86.         else if (0 == strcmp(argv[k], "-V")) {
  87.             fprintf(stderr, "version: %s\n", version_str);
  88.             exit(0);
  89.         } else if (*argv[k] == '-') {
  90.             printf("Unrecognized switch: %s\n", argv[k]);
  91.             file_name = 0;
  92.             break;
  93.         }
  94.         else if (0 == file_name)
  95.             file_name = argv[k];
  96.         else {
  97.             printf("too many arguments\n");
  98.             file_name = 0;
  99.             break;
  100.         }
  101.     }
  102.     if (0 == file_name) {
  103.         printf("Usage: 'sg_sat_chk_power [-v] [-V] <device>'\n");
  104.         return 1;
  105.     }
  106.  
  107.     if ((sg_fd = open(file_name, O_RDWR)) < 0) {
  108.         snprintf(ebuff, EBUFF_SZ,
  109.                  "sg_sat_chk_power: error opening file: %s", file_name);
  110.         perror(ebuff);
  111.         return 1;
  112.     }
  113.  
  114.     /* Prepare ATA PASS-THROUGH COMMAND (16) command */
  115.     aptCmdBlk[14] = ATA_CHECK_POWER_MODE;
  116.     aptCmdBlk[1] = (protocol << 1) | extend;
  117.     aptCmdBlk[2] = (chk_cond << 5) | (t_dir << 3) |
  118.                    (byte_block << 2) | t_length;
  119.     if (verbose) {
  120.         fprintf(stderr, "    ata pass through(16) cdb: ");
  121.         for (k = 0; k < SAT_ATA_PASS_THROUGH16_LEN; ++k)
  122.             fprintf(stderr, "%02x ", aptCmdBlk[k]);
  123.         fprintf(stderr, "\n");
  124.     }
  125.  
  126.     memset(&io_hdr, 0, sizeof(sg_io_hdr_t));
  127.     io_hdr.interface_id = 'S';
  128.     io_hdr.cmd_len = sizeof(aptCmdBlk);
  129.     /* io_hdr.iovec_count = 0; */  /* memset takes care of this */
  130.     io_hdr.mx_sb_len = sizeof(sense_buffer);
  131.     io_hdr.dxfer_direction = SG_DXFER_NONE;
  132.     io_hdr.dxfer_len = 0;
  133.     io_hdr.dxferp = NULL;
  134.     io_hdr.cmdp = aptCmdBlk;
  135.     io_hdr.sbp = sense_buffer;
  136.     io_hdr.timeout = 20000;     /* 20000 millisecs == 20 seconds */
  137.     /* io_hdr.flags = 0; */     /* take defaults: indirect IO, etc */
  138.     /* io_hdr.pack_id = 0; */
  139.     /* io_hdr.usr_ptr = NULL; */
  140.  
  141.     if (ioctl(sg_fd, SG_IO, &io_hdr) < 0) {
  142.         perror("sg_sat_chk_power: SG_IO ioctl error");
  143.         close(sg_fd);
  144.         return 1;
  145.     }
  146.  
  147.     /* error processing: N.B. expect check condition, no sense ... !! */
  148.     switch (sg_err_category3(&io_hdr)) {
  149.     case SG_LIB_CAT_CLEAN:
  150.         break;
  151.     case SG_LIB_CAT_RECOVERED:  /* sat-r09 (latest) uses this sk */
  152.     case SG_LIB_CAT_NO_SENSE:   /* earlier SAT drafts used this */
  153.         /* XXX: Until the spec decides which one to go with. 20060607 */
  154.         ucp = sg_scsi_sense_desc_find(sense_buffer, sizeof(sense_buffer),
  155.                                       SAT_ATA_RETURN_DESC);
  156.         if (NULL == ucp) {
  157.             if (verbose > 1)
  158.                 printf("ATA Return Descriptor expected in sense but not "
  159.                        "found\n");
  160.             sg_chk_n_print3("ATA_16 command error", &io_hdr, 1);
  161.         } else if (verbose)
  162.             sg_chk_n_print3("ATA Return Descriptor, as expected",
  163.                              &io_hdr, 1);
  164.         if (ucp && ucp[3]) {
  165.             if (ucp[3] & 0x4)
  166.                 printf("error in returned FIS: aborted command\n");
  167.             else
  168.                 printf("error=0x%x, status=0x%x\n", ucp[3], ucp[13]);
  169.         }
  170.         break;
  171.     default:
  172.         fprintf(stderr, "unexpected SCSI sense category\n");
  173.         ucp = sg_scsi_sense_desc_find(sense_buffer, sizeof(sense_buffer),
  174.                                       SAT_ATA_RETURN_DESC);
  175.         if (NULL == ucp)
  176.             sg_chk_n_print3("ATA_16 command error", &io_hdr, 1);
  177.         else if (verbose)
  178.             sg_chk_n_print3("ATA Return Descriptor, as expected",
  179.                              &io_hdr, 1);
  180.         if (ucp && ucp[3]) {
  181.             if (ucp[3] & 0x4)
  182.                 printf("error in returned FIS: aborted command\n");
  183.             else
  184.                 printf("error=0x%x, status=0x%x\n", ucp[3], ucp[13]);
  185.         }
  186.         break;
  187.     }
  188.  
  189.     if (ucp) {
  190.         switch (ucp[5]) {       /* sector_count (7:0) */
  191.         case 0xff:
  192.             printf("In active mode or idle mode\n");
  193.             break;
  194.         case 0x80:
  195.             printf("In idle mode\n");
  196.             break;
  197.         case 0x41:
  198.             printf("In NV power mode and spindle is spun or spinning up\n");
  199.             break;
  200.         case 0x40:
  201.             printf("In NV power mode and spindle is spun or spinning down\n");
  202.             break;
  203.         case 0x0:
  204.             printf("In standby mode\n");
  205.             break;
  206.         default:
  207.             printf("unknown power mode (sector count) value=0x%x\n", ucp[5]);
  208.             break;
  209.         }
  210.     } else
  211.         fprintf(stderr, "Expecting a ATA Return Descriptor in sense and "
  212.                 "didn't receive it\n");
  213.  
  214.     close(sg_fd);
  215.     return 0;
  216. }
  217.