scsi_subr.c revision 1.3 1 /* $NetBSD: scsi_subr.c,v 1.3 2001/04/17 13:31:00 ad 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
46 #include <sys/param.h>
47 #include <sys/ioctl.h>
48 #include <sys/scsiio.h>
49 #include <err.h>
50 #include <errno.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <unistd.h>
55
56 #include <dev/scsipi/scsi_all.h>
57
58 #include "extern.h"
59
60 #define STRVIS_ISWHITE(x) ((x) == ' ' || (x) == '\0' || (x) == (u_char)'\377')
61
62 void
63 scsi_command(fd, cmd, cmdlen, data, datalen, timeout, flags)
64 int fd;
65 void *cmd;
66 size_t cmdlen;
67 void *data;
68 size_t datalen;
69 int timeout, flags;
70 {
71 scsireq_t req;
72
73 memset(&req, 0, sizeof(req));
74
75 memcpy(req.cmd, cmd, cmdlen);
76 req.cmdlen = cmdlen;
77 req.databuf = data;
78 req.datalen = datalen;
79 req.timeout = timeout;
80 req.flags = flags;
81 req.senselen = SENSEBUFLEN;
82
83 if (ioctl(fd, SCIOCCOMMAND, &req) == -1)
84 err(1, "SCIOCCOMMAND");
85
86 if (req.retsts == SCCMD_OK)
87 return;
88
89 /* Some problem; report it and exit. */
90 if (req.retsts & SCCMD_TIMEOUT)
91 fprintf(stderr, "%s: SCSI command timed out\n", dvname);
92 if (req.retsts & SCCMD_BUSY)
93 fprintf(stderr, "%s: device is busy\n", dvname);
94 if (req.retsts & SCCMD_SENSE)
95 scsi_print_sense(dvname, &req, 1);
96
97 exit(1);
98 }
99
100 void
101 scsi_mode_sense(fd, pgcode, pctl, buf, len)
102 int fd;
103 u_int8_t pgcode, pctl;
104 void *buf;
105 size_t len;
106 {
107 struct scsi_mode_sense cmd;
108
109 memset(&cmd, 0, sizeof(cmd));
110 memset(buf, 0, len);
111
112 cmd.opcode = SCSI_MODE_SENSE;
113 cmd.page = pgcode | pctl;
114 cmd.length = len;
115
116 scsi_command(fd, &cmd, sizeof(cmd), buf, len, 10000, SCCMD_READ);
117 }
118
119 void
120 scsi_mode_select(fd, pgcode, pctl, buf, len)
121 int fd;
122 u_int8_t pgcode, pctl;
123 void *buf;
124 size_t len;
125 {
126 struct scsi_mode_select cmd;
127
128 memset(&cmd, 0, sizeof(cmd));
129
130 cmd.opcode = SCSI_MODE_SELECT;
131 cmd.length = len;
132
133 scsi_command(fd, &cmd, sizeof(cmd), buf, len, 10000, SCCMD_WRITE);
134 }
135
136 void
137 scsi_strvis(sdst, dlen, ssrc, slen)
138 char *sdst;
139 size_t dlen;
140 const char *ssrc;
141 size_t slen;
142 {
143 u_char *dst = (u_char *)sdst;
144 const u_char *src = (const u_char *)ssrc;
145
146 /* Trim leading and trailing blanks and NULs. */
147 while (slen > 0 && STRVIS_ISWHITE(src[0]))
148 ++src, --slen;
149 while (slen > 0 && STRVIS_ISWHITE(src[slen - 1]))
150 --slen;
151
152 while (slen > 0) {
153 if (*src < 0x20 || *src >= 0x80) {
154 /* non-printable characters */
155 dlen -= 4;
156 if (dlen < 1)
157 break;
158 *dst++ = '\\';
159 *dst++ = ((*src & 0300) >> 6) + '0';
160 *dst++ = ((*src & 0070) >> 3) + '0';
161 *dst++ = ((*src & 0007) >> 0) + '0';
162 } else if (*src == '\\') {
163 /* quote characters */
164 dlen -= 2;
165 if (dlen < 1)
166 break;
167 *dst++ = '\\';
168 *dst++ = '\\';
169 } else {
170 /* normal characters */
171 if (--dlen < 1)
172 break;
173 *dst++ = *src;
174 }
175 ++src, --slen;
176 }
177
178 *dst++ = 0;
179 }
180