File: sata.c

package info (click to toggle)
hddtemp 0.3-beta15-52
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch, wheezy
  • size: 2,160 kB
  • ctags: 1,331
  • sloc: ansic: 10,604; sh: 4,494; makefile: 525; yacc: 288; perl: 19; sed: 16
file content (190 lines) | stat: -rw-r--r-- 4,631 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
 * Copyright (C) 2002  Emmanuel VARAGNAT <hddtemp@guzu.net>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */


// Include file generated by ./configure
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

// Gettext includes
#if ENABLE_NLS
#include <libintl.h>
#define _(String) gettext (String)
#else
#define _(String) (String)
#endif

// Standard includes
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <scsi/scsi.h>
#include <linux/hdreg.h>

// Application specific includes
#include "hddtemp.h"
#include "satacmds.h"
#include "scsicmds.h"

#define swapb(x) \
({ \
        u16 __x = (x); \
        (x) = ((u16)( \
                (((u16)(__x) & (u16)0x00ffU) << 8) | \
                (((u16)(__x) & (u16)0xff00U) >> 8) )); \
})

 
static int sata_probe(int device) {
  int bus_num;
  unsigned char cmd[4] = { WIN_IDENTIFY, 0, 0, 1 };
  unsigned char identify[512];
  char buf[36]; /* should be 36 for unsafe devices (like USB mass storage stuff)
                        otherwise they can lock up! SPC sections 7.4 and 8.6 */
 
  /* SATA disks are difficult to detect as they answer to both ATA and SCSI 
     commands */

  /* First check that the device is accessible through SCSI */
  if(ioctl(device, SCSI_IOCTL_GET_BUS_NUMBER, &bus_num))
    return 0;
  
  /* Get SCSI name and verify it starts with "ATA " */
  if (scsi_inquiry(device, buf))
    return 0;
  else if (strncmp(buf + 8, "ATA ", 4))
     return 0;

  /* Verify that it supports ATA pass thru */
  if (sata_pass_thru(device, cmd, identify) != 0)
    return 0;
  else
    return 1;
}

static const char *sata_model (int device) {
  unsigned char cmd[4] = { WIN_IDENTIFY, 0, 0, 1 };
  unsigned char identify[512];
  
  if(device == -1 || sata_pass_thru(device, cmd, identify))
    return strdup(_("unknown"));
  else
  {
    sata_fixstring(identify + 54, 24);
    return strdup(identify + 54);
  }
}

static unsigned char* sata_search_temperature(const unsigned char* smart_data, int attribute_id) {
  int i, n;

  n = 3;
  i = 0;
  while((debug || *(smart_data + n) != attribute_id) && i < 30) {
    if(debug && *(smart_data + n))
      printf(_("field(%d)\t = %d\n"), *(smart_data + n), *(smart_data + n + 3));

    n += 12;
    i++;
  }

  if(i >= 30)
    return NULL;
  else
    return (unsigned char*)(smart_data + n);
}

static enum e_gettemp sata_get_temperature(struct disk *dsk) {   
  unsigned char    values[512];
  unsigned char *  field;
  int              i;
  u16 *            p;

  if(dsk->db_entry->attribute_id == 0) {
    close(dsk->fd);
    dsk->fd = -1;
    return GETTEMP_NOSENSOR;
  }

  switch(ata_get_powermode(dsk->fd)) {
  case PWM_STANDBY:
  case PWM_SLEEPING:
    if (!wakeup)
      return GETTEMP_DRIVE_SLEEP;
  case PWM_UNKNOWN:
  case PWM_ACTIVE: /* active or idle */
  default:
    break;
  }
  
  /* get SMART values */
  if(sata_enable_smart(dsk->fd) != 0) {
    enum e_gettemp ret;
    if(errno == EIO) {
      snprintf(dsk->errormsg, MAX_ERRORMSG_SIZE, _("S.M.A.R.T. not available"));
      ret = GETTEMP_NOT_APPLICABLE;
    }
    else
    {
      snprintf(dsk->errormsg, MAX_ERRORMSG_SIZE, "%s", strerror(errno));
      ret = GETTEMP_ERROR;
    }
    close(dsk->fd);
    dsk->fd = -1;
    return ret;
  }

  if(sata_get_smart_values(dsk->fd, values)) {
    snprintf(dsk->errormsg, MAX_ERRORMSG_SIZE, "%s", strerror(errno));
    close(dsk->fd);
    dsk->fd = -1;
    return GETTEMP_ERROR;
  }

  p = (u16*)values;
  for(i = 0; i < 256; i++) {
    swapb(*(p+i));
  }

  /* temperature */
  field = sata_search_temperature(values, dsk->db_entry->attribute_id);

  if(field)
    dsk->value = *(field+3);

  if(dsk->db_entry && dsk->value != -1)
    return GETTEMP_KNOWN;
  else
    return GETTEMP_UNKNOWN;

  /* never reached */
}


/*******************************
 *******************************/

struct bustype sata_bus = {
  "SATA",
  sata_probe,
  sata_model,
  sata_get_temperature
};