File: sg_simple2.c

package info (click to toggle)
sg-utils 0.95-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 604 kB
  • ctags: 867
  • sloc: ansic: 10,626; makefile: 159; sh: 39
file content (242 lines) | stat: -rw-r--r-- 7,726 bytes parent folder | download
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "sg_include.h"

/* This is a simple program executing a SCSI INQUIRY command and a
   TEST UNIT READY command using the SCSI generic (sg) driver.
   There is another variant of this program called "sg_simple1"
   which includes the sg_err.h header and logic and so has more
   advanced error processing.
   This version now can be compiled with the original sg driver
   (as found in the 2.0 series and <= 2.2.5 kernels).

*  Copyright (C) 1999 D. Gilbert
*  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, or (at your option)
*  any later version.

   Invocation: sg_simple2 <sg_device>

   Version 0.53 (990802)

6 byte INQUIRY command:
[0x12][   |lu][pg cde][res   ][al len][cntrl ]

6 byte TEST UNIT READY command:
[0x00][   |lu][res   ][res   ][res   ][res   ]

*/

#define OFF sizeof(struct sg_header)
#define INQ_REPLY_LEN 96        /* logic assumes >= sizeof(inqCmdBlk) */
#define INQ_CMD_LEN 6
#define TUR_CMD_LEN 6
#define INQ_PACK_ID 1234
#define TUR_PACK_ID 1235

#ifndef DRIVER_SENSE
#define DRIVER_SENSE 0x08
#endif


int main(int argc, char * argv[])
{
    int sg_fd, k, ok;
    unsigned char inqCmdBlk [INQ_CMD_LEN] =
                                {0x12, 0, 0, 0, INQ_REPLY_LEN, 0};
    unsigned char turCmdBlk [TUR_CMD_LEN] =
                                {0x00, 0, 0, 0, 0, 0};
    unsigned char inqBuff[OFF + INQ_REPLY_LEN];
    unsigned char * turBuff = inqBuff;  /* Use same buffer */
    int inqInLen = OFF + sizeof(inqCmdBlk);
    int turInLen = OFF + sizeof(turCmdBlk);
    int inqOutLen = OFF + INQ_REPLY_LEN;
    unsigned char * buffp = inqBuff + OFF;
    struct sg_header * sghp = (struct sg_header *)inqBuff;
    char * file_name = 0;
    char ebuff[128];

    for (k = 1; k < argc; ++k) {
        if (*argv[k] == '-') {
            printf("Unrecognized switch: %s\n", argv[k]);
            file_name = 0;
            break;
        }
        else if (0 == file_name)
            file_name = argv[k];
        else {
            printf("too many arguments\n");
            file_name = 0;
            break;
        }
    }
    if (0 == file_name) {
        printf("Usage: 'sg_simple2 <sg_device>'\n");
        return 1;
    }
    
    if ((sg_fd = open(file_name, O_RDWR)) < 0) {
        sprintf(ebuff, "sg_simple2: error opening file: %s", file_name);
        perror(ebuff);
        return 1;
    }
    /* Just to be safe, check we have a sg device by trying an ioctl */
    if (ioctl(sg_fd, SG_GET_TIMEOUT, 0) < 0) {
        printf("sg_simple2: %s doesn't seem to be an sg device\n", file_name);
        close(sg_fd);
        return 1;
    }
    
    /* Prepare INQUIRY command */
    sghp->reply_len = inqOutLen;
    sghp->pack_id = INQ_PACK_ID;
    sghp->twelve_byte = 0;
    sghp->other_flags = 0;     /* some apps assume this is done ?!? */
    sghp->sense_buffer[0] = 0; /* only needed for old sg driver */
    memcpy(inqBuff + OFF, inqCmdBlk, INQ_CMD_LEN);

    /* Send INQUIRY command */
    if (write(sg_fd, inqBuff, inqInLen) < 0) {
        perror("sg_simple2: Inquiry write error");
        close(sg_fd);
        return 1;
    }
    
    /* Read back status (sense_buffer) and data */
    if (read(sg_fd, inqBuff, inqOutLen) < 0) {
        perror("sg_simple2: Inquiry read error");
        close(sg_fd);
        return 1;
    }
    if (INQ_PACK_ID != sghp->pack_id) /* this shouldn't happen */
        printf("Inquiry pack_id mismatch, wanted=%d, got=%d\n", 
               INQ_PACK_ID, sghp->pack_id);

    /* now for the error processing */
    ok = 0;
#ifdef SG_RESERVED_BUFFER
    if ((0 == sghp->target_status) &
        (0 == sghp->host_status) &
        (0 == sghp->driver_status))
        ok = 1;
    else {
        if ((CHECK_CONDITION == sghp->target_status) ||
            (COMMAND_TERMINATED == sghp->target_status) ||
            (DRIVER_SENSE & sghp->driver_status)) {
            printf("INQUIRY sense data: ");
            for (k = 0; k < SG_MAX_SENSE; ++k) {
                if ((k > 0) && (0 == (k % 10)))
                    printf("\n  ");
                printf("0x%02x ", sghp->sense_buffer[k]);
            }
            printf("\n");
        }
        else if (sghp->host_status)
            printf("INQUIRY host_status=0x%x\n", sghp->host_status);
        else if (sghp->driver_status)
            printf("INQUIRY driver_status=0x%x\n", sghp->driver_status);
    }
#else
    /* Old sg driver ... */
    if (0 != sghp->sense_buffer[0]) {
        printf("INQUIRY sense data: ");
        for (k = 0; k < 16; ++k) {
            if ((k > 0) && (0 == (k % 10)))
                printf("\n  ");
            printf("0x%02x ", sghp->sense_buffer[k]);
        }
        printf("\n");

    }
    else
        ok = 1;
#endif

    if (ok) { /* output result if it is available */
        int f = (int)*(buffp + 7);
        printf("Some of the INQUIRY command's results:\n");
        printf("    %.8s  %.16s  %.4s  ", buffp + 8, buffp + 16, buffp + 32);
        printf("[wide=%d sync=%d cmdque=%d sftre=%d]\n",
               !!(f & 0x20), !!(f & 0x10), !!(f & 2), !!(f & 1));
    }
    
    /* Prepare TEST UNIT READY command */
    sghp->reply_len = OFF;
    sghp->pack_id = TUR_PACK_ID;
    sghp->twelve_byte = 0;
    sghp->other_flags = 0;     /* some apps assume this is done ?!? */
    sghp->sense_buffer[0] = 0; /* only needed for old sg driver */
    memcpy(turBuff + OFF, turCmdBlk, TUR_CMD_LEN);

    /* Send TEST UNIT READY command */
    if (write(sg_fd, turBuff, turInLen) < 0) {
        perror("sg_simple2: Test Unit Ready write error");
        close(sg_fd);
        return 1;
    }
    
    /* Read back status (sense_buffer) */
    if (read(sg_fd, turBuff, OFF) < 0) {
        perror("sg_simple2: Test Unit Ready read error");
        close(sg_fd);
        return 1;
    }
    if (TUR_PACK_ID != sghp->pack_id) /* this shouldn't happen */
        printf("Test Unit Ready pack_id mismatch, wanted=%d, got=%d\n", 
               TUR_PACK_ID, sghp->pack_id);

    /* now for the error processing */
    ok = 0;
#ifdef SG_RESERVED_BUFFER
    if ((0 == sghp->target_status) &
        (0 == sghp->host_status) &
        (0 == sghp->driver_status))
        ok = 1;
    else {
        if ((CHECK_CONDITION == sghp->target_status) ||
            (COMMAND_TERMINATED == sghp->target_status) ||
            (DRIVER_SENSE & sghp->driver_status)) {
            printf("TEST UNIT READY sense data: ");
            for (k = 0; k < SG_MAX_SENSE; ++k) {
                if ((k > 0) && (0 == (k % 10)))
                    printf("\n  ");
                printf("0x%02x ", sghp->sense_buffer[k]);
            }
            printf("\n");
        }
        else if (sghp->host_status)
            printf("TEST UNIT READY host_status=0x%x\n", sghp->host_status);
        else if (sghp->driver_status)
            printf("TEST UNIT READY driver_status=0x%x\n", sghp->driver_status);
    }
#else
    /* Old sg driver ... */
    if (0 != sghp->sense_buffer[0]) {
        printf("TEST UNIT READY sense data: ");
        for (k = 0; k < 16; ++k) {
            if ((k > 0) && (0 == (k % 10)))
                printf("\n  ");
            printf("0x%02x ", sghp->sense_buffer[k]);
        }
        printf("\n");
    }
    else
        ok = 1;
#endif

    if (ok)
        printf("Test Unit Ready successful so unit is ready!\n");
    else
        printf("Test Unit Ready failed so unit may _not_ be ready!\n");

    close(sg_fd);
    return 0;
}