xref: /netbsd-src/sbin/scsictl/scsi_subr.c (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /*	$NetBSD: scsi_subr.c,v 1.9 2003/09/08 09:05:08 agc Exp $	*/
2 
3 /*-
4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Charles M. Hannum; Jason R. Thorpe of the Numerical Aerospace
9  * Simulation Facility, NASA Ames Research Center.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *        This product includes software developed by the NetBSD
22  *        Foundation, Inc. and its contributors.
23  * 4. Neither the name of The NetBSD Foundation nor the names of its
24  *    contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39 
40 /*
41  * SCSI support subroutines.
42  *
43  * XXX THESE SHOULD BE IN A LIBRARY!
44  */
45 #include <sys/cdefs.h>
46 
47 #ifndef lint
48 __RCSID("$NetBSD: scsi_subr.c,v 1.9 2003/09/08 09:05:08 agc Exp $");
49 #endif
50 
51 
52 #include <sys/param.h>
53 #include <sys/ioctl.h>
54 #include <sys/scsiio.h>
55 #include <err.h>
56 #include <errno.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #include <unistd.h>
61 
62 #include <dev/scsipi/scsipi_all.h>
63 
64 #include "extern.h"
65 
66 #define	STRVIS_ISWHITE(x) ((x) == ' ' || (x) == '\0' || (x) == (u_char)'\377')
67 
68 void
69 scsi_command(fd, cmd, cmdlen, data, datalen, timeout, flags)
70 	int fd;
71 	void *cmd;
72 	size_t cmdlen;
73 	void *data;
74 	size_t datalen;
75 	int timeout, flags;
76 {
77 	scsireq_t req;
78 
79 	memset(&req, 0, sizeof(req));
80 
81 	memcpy(req.cmd, cmd, cmdlen);
82 	req.cmdlen = cmdlen;
83 	req.databuf = data;
84 	req.datalen = datalen;
85 	req.timeout = timeout;
86 	req.flags = flags;
87 	req.senselen = SENSEBUFLEN;
88 
89 	if (ioctl(fd, SCIOCCOMMAND, &req) == -1)
90 		err(1, "SCIOCCOMMAND");
91 
92 	if (req.retsts == SCCMD_OK)
93 		return;
94 
95 	/* Some problem; report it and exit. */
96 	if (req.retsts == SCCMD_TIMEOUT)
97 		fprintf(stderr, "%s: SCSI command timed out\n", dvname);
98 	else if (req.retsts == SCCMD_BUSY)
99 		fprintf(stderr, "%s: device is busy\n", dvname);
100 	else if (req.retsts == SCCMD_SENSE)
101 		scsi_print_sense(dvname, &req, 1);
102 	else
103 		fprintf(stderr, "%s: device had unknown status %x\n", dvname,
104 		    req.retsts);
105 
106 	exit(1);
107 }
108 
109 void
110 scsi_mode_sense(fd, pgcode, pctl, buf, len)
111 	int fd;
112 	u_int8_t pgcode, pctl;
113 	void *buf;
114 	size_t len;
115 {
116 	struct scsipi_mode_sense cmd;
117 
118 	memset(&cmd, 0, sizeof(cmd));
119 	memset(buf, 0, len);
120 
121 	cmd.opcode = MODE_SENSE;
122 	cmd.page = pgcode | pctl;
123 	cmd.length = len;
124 
125 	scsi_command(fd, &cmd, sizeof(cmd), buf, len, 10000, SCCMD_READ);
126 }
127 
128 void
129 scsi_mode_select(fd, byte2, buf, len)
130 	int fd;
131 	u_int8_t byte2;
132 	void *buf;
133 	size_t len;
134 {
135 	struct scsipi_mode_select cmd;
136 
137 	memset(&cmd, 0, sizeof(cmd));
138 
139 	cmd.opcode = MODE_SELECT;
140 	cmd.byte2 = SMS_PF | byte2;
141 	cmd.length = len;
142 
143 	scsi_command(fd, &cmd, sizeof(cmd), buf, len, 10000, SCCMD_WRITE);
144 }
145 
146 void
147 scsi_request_sense(fd, buf, len)
148 	int fd;
149 	void *buf;
150 	size_t len;
151 {
152 	struct scsipi_sense cmd;
153 
154 	memset(&cmd, 0, sizeof(cmd));
155 	memset(buf, 0, len);
156 
157 	cmd.opcode = REQUEST_SENSE;
158 	cmd.length = len;
159  	scsi_command(fd, &cmd, sizeof(cmd), buf, len, 10000, SCCMD_READ);
160 }
161 
162 void
163 scsi_strvis(sdst, dlen, ssrc, slen)
164 	char *sdst;
165 	size_t dlen;
166 	const char *ssrc;
167 	size_t slen;
168 {
169 	u_char *dst = (u_char *)sdst;
170 	const u_char *src = (const u_char *)ssrc;
171 
172 	/* Trim leading and trailing blanks and NULs. */
173 	while (slen > 0 && STRVIS_ISWHITE(src[0]))
174 		++src, --slen;
175 	while (slen > 0 && STRVIS_ISWHITE(src[slen - 1]))
176 		--slen;
177 
178 	while (slen > 0) {
179 		if (*src < 0x20 || *src >= 0x80) {
180 			/* non-printable characters */
181 			dlen -= 4;
182 			if (dlen < 1)
183 				break;
184 			*dst++ = '\\';
185 			*dst++ = ((*src & 0300) >> 6) + '0';
186 			*dst++ = ((*src & 0070) >> 3) + '0';
187 			*dst++ = ((*src & 0007) >> 0) + '0';
188 		} else if (*src == '\\') {
189 			/* quote characters */
190 			dlen -= 2;
191 			if (dlen < 1)
192 				break;
193 			*dst++ = '\\';
194 			*dst++ = '\\';
195 		} else {
196 			/* normal characters */
197 			if (--dlen < 1)
198 				break;
199 			*dst++ = *src;
200 		}
201 		++src, --slen;
202 	}
203 
204 	*dst++ = 0;
205 }
206