File: sense.c

package info (click to toggle)
cdrtools 4%3A1.10-7
  • links: PTS
  • area: main
  • in suites: woody
  • size: 7,620 kB
  • ctags: 10,027
  • sloc: ansic: 79,657; sh: 3,076; makefile: 385; perl: 223; pascal: 42; sed: 4
file content (293 lines) | stat: -rw-r--r-- 9,780 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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/* @(#)sense.c	1.1 01/03/18 Copyright 2001 J. Schilling */
#ifndef lint
static	char sccsid[] =
	"@(#)sense.c	1.1 01/03/18 Copyright 2001 J. Schilling";
#endif
/*
 *	Copyright (c) 2001 J. Schilling
 */
/*
 * 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.
 *
 * 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; see the file COPYING.  If not, write to
 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <mconfig.h>
#include <stdio.h>
#include <stdxlib.h>
#include <unixstd.h>
#include <strdefs.h>
#include <schily.h>
#include <standard.h>

#include <utypes.h>
#include <btorder.h>
#include <scg/scgcmd.h>
#include <scg/scsidefs.h>
#include <scg/scsireg.h>
#include <scg/scsitransp.h>

#include "cdrecord.h"
#include "scgcheck.h"

extern	char	*buf;			/* The transfer buffer */
extern	long	bufsize;		/* The size of the transfer buffer */

extern	FILE	*logfile;
extern	char	unavail[];

LOCAL	BOOL	inq_nofail = FALSE;


EXPORT	void	sensetest	__PR((SCSI *scgp));
LOCAL	int	sensecount	__PR((SCSI *scgp, int sensecnt));
LOCAL	int	badinquiry	__PR((SCSI *scgp, caddr_t bp, int cnt, int sensecnt));
LOCAL	int	bad_unit_ready	__PR((SCSI *scgp, int sensecnt));

EXPORT void
sensetest(scgp)
	SCSI	*scgp;
{
	char	abuf[2];
	int	ret;
	int	sense_count = 0;
	BOOL	passed = TRUE;

	printf("Ready to start test for failing command? Enter <CR> to conitnue: ");
	fprintf(logfile, "**********> Testing for failed SCSI command.\n");
	flushit();
	(void)getline(abuf, sizeof(abuf));
/*	scgp->verbose++;*/
	fillbytes(buf, sizeof(struct scsi_inquiry), '\0');
	fillbytes((caddr_t)scgp->scmd, sizeof(*scgp->scmd), '\0');
	ret = badinquiry(scgp, buf, sizeof(struct scsi_inquiry), CCS_SENSE_LEN);
	scg_vsetup(scgp);
	scg_errfflush(scgp, logfile);
	if (ret >= 0 || !scg_cmd_err(scgp)) {
		inq_nofail = TRUE;
		printf("Inquiry did not fail.\n");
		fprintf(logfile, "Inquiry did not fail.\n");
		printf("This may be because the firmware in your drive is buggy.\n");
		printf("If the current drive is not a CD-ROM drive please restart\n");
		printf("the test utility. Otherwise remove any medium from the drive.\n");
		printf("Ready to start test for failing command? Enter <CR> to conitnue: ");
		flushit();
		(void)getline(abuf, sizeof(abuf));
		ret = test_unit_ready(scgp);
		if (ret >= 0 || !scg_cmd_err(scgp)) {
			scsi_unload(scgp);
			printf("Test Unit Ready did not fail.\n");
			printf("Ready to eject tray? Enter <CR> to conitnue: ");
			flushit();
			(void)getline(abuf, sizeof(abuf));
			ret = test_unit_ready(scgp);
		}
	}
	scg_vsetup(scgp);
	scg_errfflush(scgp, logfile);
/*	scgp->verbose--;*/
	if (ret < 0 &&
	    scgp->scmd->error == SCG_NO_ERROR &&
	    scgp->scmd->ux_errno != 0 &&
	    *(Uchar *)&scgp->scmd->scb != 0) {
		printf("----------> SCSI failed command test PASSED\n");
		fprintf(logfile, "----------> SCSI failed command test PASSED\n");
	} else {
		if (ret >= 0) {
			printf("---------->	scg_cmd() returns not -1 (%d)\n", ret);
			fprintf(logfile, "---------->	scg_cmd() returns not -1 (%d)\n", ret);
		}
		if (scgp->scmd->error != SCG_NO_ERROR) {
			printf("---------->	SCSI Transport return != SCG_NO_ERROR (%d)\n", scgp->scmd->error);
			fprintf(logfile, "---------->	SCSI Transport return != SCG_NO_ERROR (%d)\n", scgp->scmd->error);
		}
		if (scgp->scmd->ux_errno == 0) {
			printf("---------->	UNIX errno set to 0\n");
			fprintf(logfile, "---------->	UNIX errno set to 0\n");
		}
		if (*(Uchar *)&scgp->scmd->scb == 0) {
			printf("---------->	SCSI status byte set to 0 (0x%x)\n", *(Uchar *)&scgp->scmd->scb & 0xFF);
			fprintf(logfile, "---------->	SCSI status byte set to 0 (0x%x)\n", *(Uchar *)&scgp->scmd->scb & 0xFF);
		}
		printf("----------> SCSI failed command test FAILED\n");
		fprintf(logfile, "----------> SCSI failed command test FAILED\n");
	}


	printf("Ready to start test for sense data count? Enter <CR> to continue: ");
	fprintf(logfile, "**********> Testing for SCSI sense data count.\n");
	flushit();
	(void)getline(abuf, sizeof(abuf));
	printf("Testing if at least CCS_SENSE_LEN (%d) is supported...\n", CCS_SENSE_LEN);
	fprintf(logfile, "**********> Testing if at least CCS_SENSE_LEN (%d) is supported...\n", CCS_SENSE_LEN);
	ret = sensecount(scgp, CCS_SENSE_LEN);
	if (ret > sense_count)
		sense_count = ret;
	if (ret == CCS_SENSE_LEN) {
		printf("---------->	Wanted %d sense bytes got it.\n", CCS_SENSE_LEN);
		fprintf(logfile, "---------->	Wanted %d sense bytes got it.\n", CCS_SENSE_LEN);
	}
	if (ret != CCS_SENSE_LEN) {
		printf("---------->	Minimum standard (CCS) sense length failed\n");
		printf("---------->	Wanted %d sense bytes got (%d)\n", CCS_SENSE_LEN, ret);
		fprintf(logfile, "---------->	Minimum standard (CCS) sense length failed\n");
		fprintf(logfile, "---------->	Wanted %d sense bytes got (%d)\n", CCS_SENSE_LEN, ret);
	}
	if (ret != scgp->scmd->sense_count) {
		passed = FALSE;
		printf("---------->	Libscg says %d sense bytes but got (%d)\n", scgp->scmd->sense_count, ret);
		fprintf(logfile, "---------->	Libscg says %d sense bytes but got (%d)\n", scgp->scmd->sense_count, ret);
	}
	printf("Testing for %d bytes of sense data...\n", SCG_MAX_SENSE);
	fprintf(logfile, "**********> Testing for %d bytes of sense data...\n", SCG_MAX_SENSE);
	ret = sensecount(scgp, SCG_MAX_SENSE);
	if (ret > sense_count)
		sense_count = ret;
	if (ret == SCG_MAX_SENSE) {
		printf("---------->	Wanted %d sense bytes got it.\n", SCG_MAX_SENSE);
		fprintf(logfile, "---------->	Wanted %d sense bytes got it.\n", SCG_MAX_SENSE);
	}
	if (ret != SCG_MAX_SENSE) {
		printf("---------->	Wanted %d sense bytes got (%d)\n", SCG_MAX_SENSE, ret);
		fprintf(logfile, "---------->	Wanted %d sense bytes got (%d)\n", SCG_MAX_SENSE, ret);
	}
	if (ret != scgp->scmd->sense_count) {
		passed = FALSE;
		printf("---------->	Libscg says %d sense bytes but got (%d)\n", scgp->scmd->sense_count, ret);
		fprintf(logfile, "---------->	Libscg says %d sense bytes but got (%d)\n", scgp->scmd->sense_count, ret);
	}

	printf("----------> Got a maximum of %d sense bytes\n", sense_count);
	fprintf(logfile, "----------> Got a maximum of %d sense bytes\n", sense_count);
	if (passed && sense_count >= CCS_SENSE_LEN) {
		printf("----------> SCSI sense count test PASSED\n");
		fprintf(logfile, "----------> SCSI sense count test PASSED\n");
	} else {
		printf("----------> SCSI sense count test FAILED\n");
		fprintf(logfile, "----------> SCSI sense count test FAILED\n");
	}
}

LOCAL int
sensecount(scgp, sensecnt)
	SCSI	*scgp;
	int	sensecnt;
{
	int	maxcnt;
	int	i;
	Uchar	*p;

	if (sensecnt > SCG_MAX_SENSE)
		sensecnt = SCG_MAX_SENSE;
		
/*	scgp->verbose++;*/
	scgp->silent++;
	fillbytes(buf, sizeof(struct scsi_inquiry), '\0');
	fillbytes((caddr_t)scgp->scmd, sizeof(*scgp->scmd), '\0');
	fillbytes((caddr_t)scgp->scmd->u_sense.cmd_sense, sensecnt, 0x00);
	if (inq_nofail)
		bad_unit_ready(scgp, sensecnt);
	else
		badinquiry(scgp, buf, sizeof(struct scsi_inquiry), sensecnt);
	scg_prbytes("Sense Data:", (Uchar *)scgp->scmd->u_sense.cmd_sense, sensecnt);
	scg_fprbytes(logfile, "Sense Data:", (Uchar *)scgp->scmd->u_sense.cmd_sense, sensecnt);
	p = (Uchar *)scgp->scmd->u_sense.cmd_sense;
	for (i=sensecnt-1; i >= 0; i--) {
		if (p[i] != 0x00) {
			break;
		}
	}
	i++;
	maxcnt = i;
printf("%d %d %d\n", sensecnt, scgp->scmd->sense_count, maxcnt);

	fillbytes(buf, sizeof(struct scsi_inquiry), '\0');
	fillbytes((caddr_t)scgp->scmd, sizeof(*scgp->scmd), '\0');
	fillbytes((caddr_t)scgp->scmd->u_sense.cmd_sense, sensecnt, 0xFF);
	if (inq_nofail)
		bad_unit_ready(scgp, sensecnt);
	else
		badinquiry(scgp, buf, sizeof(struct scsi_inquiry), sensecnt);
	scg_prbytes("Sense Data:", (Uchar *)scgp->scmd->u_sense.cmd_sense, sensecnt);
	scg_fprbytes(logfile, "Sense Data:", (Uchar *)scgp->scmd->u_sense.cmd_sense, sensecnt);
	p = (Uchar *)scgp->scmd->u_sense.cmd_sense;
	for (i=sensecnt-1; i >= 0; i--) {
		if (p[i] != 0xFF) {
			break;
		}
	}
	i++;
	if (i > maxcnt)
		maxcnt = i;
printf("%d %d %d\n", sensecnt, scgp->scmd->sense_count, maxcnt);

/*	scgp->verbose--;*/
	scgp->silent--;
/*	scg_vsetup(scgp);*/
/*	scg_errfflush(scgp, logfile);*/

	return (i);
}

LOCAL int
badinquiry(scgp, bp, cnt, sensecnt)
	SCSI	*scgp;
	caddr_t	bp;
	int	cnt;
	int	sensecnt;
{
	register struct	scg_cmd	*scmd = scgp->scmd;

/*	fillbytes(bp, cnt, '\0');*/
/*	fillbytes((caddr_t)scmd, sizeof(*scmd), '\0');*/
	scmd->addr = bp;
	scmd->size = cnt;
	scmd->flags = SCG_RECV_DATA|SCG_DISRE_ENA;
	scmd->cdb_len = SC_G0_CDBLEN;
/*	scmd->sense_len = CCS_SENSE_LEN;*/
	scmd->sense_len = sensecnt;
	scmd->cdb.g0_cdb.cmd = SC_INQUIRY;
	scmd->cdb.g0_cdb.lun = scg_lun(scgp);
	scmd->cdb.g0_cdb.count = cnt;

scmd->cdb.cmd_cdb[3] = 0xFF;
	
	scgp->cmdname = "inquiry";

	if (scg_cmd(scgp) < 0)
		return (-1);
	if (scgp->verbose)
		scg_prbytes("Inquiry Data   :", (Uchar *)bp, cnt - scg_getresid(scgp));
	return (0);
}

LOCAL int
bad_unit_ready(scgp, sensecnt)
	SCSI	*scgp;
	int	sensecnt;
{
	register struct	scg_cmd	*scmd = scgp->scmd;

/*	fillbytes((caddr_t)scmd, sizeof(*scmd), '\0');*/
	scmd->addr = (caddr_t)0;
	scmd->size = 0;
	scmd->flags = SCG_DISRE_ENA | (scgp->silent ? SCG_SILENT:0);
	scmd->cdb_len = SC_G0_CDBLEN;
	scmd->sense_len = sensecnt;
	scmd->cdb.g0_cdb.cmd = SC_TEST_UNIT_READY;
	scmd->cdb.g0_cdb.lun = scg_lun(scgp);
	
	scgp->cmdname = "test unit ready";

	return (scg_cmd(scgp));
}