smartmontools SVN Rev 3317
Utility to control and monitor storage systems with "S.M.A.R.T."
dev_ata_cmd_set.cpp
Go to the documentation of this file.
00001 /*
00002  * dev_ata_cmd_set.cpp
00003  *
00004  * Home page of code is: http://smartmontools.sourceforge.net
00005  *
00006  * Copyright (C) 2008 Christian Franke <smartmontools-support@lists.sourceforge.net>
00007  *
00008  * This program is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2, or (at your option)
00011  * any later version.
00012  *
00013  * You should have received a copy of the GNU General Public License
00014  * (for example COPYING); If not, see <http://www.gnu.org/licenses/>.
00015  *
00016  */
00017 
00018 #include "config.h"
00019 #include "int64.h"
00020 #include "atacmds.h"
00021 #include "dev_ata_cmd_set.h"
00022 
00023 #include <errno.h>
00024 
00025 const char * dev_ata_cmd_set_cpp_cvsid = "$Id: dev_ata_cmd_set.cpp,v 1.4 2008/10/24 21:49:23 manfred99 Exp $"
00026   DEV_ATA_CMD_SET_H_CVSID;
00027 
00028 
00029 /////////////////////////////////////////////////////////////////////////////
00030 // ata_device_with_command_set
00031 
00032 // Adapter routine to implement new ATA pass through with old interface
00033 
00034 bool ata_device_with_command_set::ata_pass_through(const ata_cmd_in & in, ata_cmd_out & out)
00035 {
00036   if (!ata_cmd_is_ok(in, true)) // data_out_support
00037     return false;
00038 
00039   smart_command_set command = (smart_command_set)-1;
00040   int select = 0;
00041   char * data = (char *)in.buffer;
00042   char buffer[512];
00043   switch (in.in_regs.command) {
00044     case ATA_IDENTIFY_DEVICE:
00045       command = IDENTIFY;
00046       break;
00047     case ATA_IDENTIFY_PACKET_DEVICE:
00048       command = PIDENTIFY;
00049       break;
00050     case ATA_CHECK_POWER_MODE:
00051       command = CHECK_POWER_MODE;
00052       data = buffer; data[0] = 0;
00053       break;
00054     case ATA_SMART_CMD:
00055       switch (in.in_regs.features) {
00056         case ATA_SMART_ENABLE:
00057           command = ENABLE;
00058           break;
00059         case ATA_SMART_READ_VALUES:
00060           command = READ_VALUES;
00061           break;
00062         case ATA_SMART_READ_THRESHOLDS:
00063           command = READ_THRESHOLDS;
00064           break;
00065         case ATA_SMART_READ_LOG_SECTOR:
00066           command = READ_LOG;
00067           select = in.in_regs.lba_low;
00068           break;
00069         case ATA_SMART_WRITE_LOG_SECTOR:
00070           command = WRITE_LOG;
00071           select = in.in_regs.lba_low;
00072           break;
00073         case ATA_SMART_DISABLE:
00074           command = DISABLE;
00075           break;
00076         case ATA_SMART_STATUS:
00077           command = (in.out_needed.lba_high ? STATUS_CHECK : STATUS);
00078           break;
00079         case ATA_SMART_AUTO_OFFLINE:
00080           command = AUTO_OFFLINE;
00081           select = in.in_regs.sector_count;
00082           break;
00083         case ATA_SMART_AUTOSAVE:
00084           command = AUTOSAVE;
00085           select = in.in_regs.sector_count;
00086           break;
00087         case ATA_SMART_IMMEDIATE_OFFLINE:
00088           command = IMMEDIATE_OFFLINE;
00089           select = in.in_regs.lba_low;
00090           break;
00091         default:
00092           return set_err(ENOSYS, "Unknown SMART command");
00093       }
00094       break;
00095     default:
00096       return set_err(ENOSYS, "Non-SMART commands not implemented");
00097   }
00098 
00099   clear_err(); errno = 0;
00100   int rc = ata_command_interface(command, select, data);
00101   if (rc < 0) {
00102     if (!get_errno())
00103       set_err(errno);
00104     return false;
00105   }
00106 
00107   switch (command) {
00108     case CHECK_POWER_MODE:
00109       out.out_regs.sector_count = data[0];
00110       break;
00111     case STATUS_CHECK:
00112       switch (rc) {
00113         case 0: // Good SMART status
00114           out.out_regs.lba_high = 0xc2; out.out_regs.lba_mid = 0x4f;
00115           break;
00116         case 1: // Bad SMART status
00117           out.out_regs.lba_high = 0x2c; out.out_regs.lba_mid = 0xf4;
00118           break;
00119       }
00120       break;
00121     default:
00122       break;
00123   }
00124   return true;
00125 }
00126