scsi_subr.c revision 1.10 1 /* $NetBSD: scsi_subr.c,v 1.10 2005/02/05 13:37:39 xtraeme 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.10 2005/02/05 13:37:39 xtraeme 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(int fd, void *cmd, size_t cmdlen, void *data, size_t datalen,
70 int timeout, int flags)
71 {
72 scsireq_t req;
73
74 memset(&req, 0, sizeof(req));
75
76 memcpy(req.cmd, cmd, cmdlen);
77 req.cmdlen = cmdlen;
78 req.databuf = data;
79 req.datalen = datalen;
80 req.timeout = timeout;
81 req.flags = flags;
82 req.senselen = SENSEBUFLEN;
83
84 if (ioctl(fd, SCIOCCOMMAND, &req) == -1)
85 err(1, "SCIOCCOMMAND");
86
87 if (req.retsts == SCCMD_OK)
88 return;
89
90 /* Some problem; report it and exit. */
91 if (req.retsts == SCCMD_TIMEOUT)
92 fprintf(stderr, "%s: SCSI command timed out\n", dvname);
93 else if (req.retsts == SCCMD_BUSY)
94 fprintf(stderr, "%s: device is busy\n", dvname);
95 else if (req.retsts == SCCMD_SENSE)
96 scsi_print_sense(dvname, &req, 1);
97 else
98 fprintf(stderr, "%s: device had unknown status %x\n", dvname,
99 req.retsts);
100
101 exit(1);
102 }
103
104 void
105 scsi_mode_sense(int fd, u_int8_t pgcode, u_int8_t pctl, void *buf, size_t len)
106 {
107 struct scsipi_mode_sense cmd;
108
109 memset(&cmd, 0, sizeof(cmd));
110 memset(buf, 0, len);
111
112 cmd.opcode = 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(int fd, u_int8_t byte2, void *buf, size_t len)
121 {
122 struct scsipi_mode_select cmd;
123
124 memset(&cmd, 0, sizeof(cmd));
125
126 cmd.opcode = MODE_SELECT;
127 cmd.byte2 = SMS_PF | byte2;
128 cmd.length = len;
129
130 scsi_command(fd, &cmd, sizeof(cmd), buf, len, 10000, SCCMD_WRITE);
131 }
132
133 void
134 scsi_request_sense(int fd, void *buf, size_t len)
135 {
136 struct scsipi_sense cmd;
137
138 memset(&cmd, 0, sizeof(cmd));
139 memset(buf, 0, len);
140
141 cmd.opcode = REQUEST_SENSE;
142 cmd.length = len;
143 scsi_command(fd, &cmd, sizeof(cmd), buf, len, 10000, SCCMD_READ);
144 }
145
146 void
147 scsi_strvis(char *sdst, size_t dlen, const char *ssrc, size_t slen)
148 {
149 u_char *dst = (u_char *)sdst;
150 const u_char *src = (const u_char *)ssrc;
151
152 /* Trim leading and trailing blanks and NULs. */
153 while (slen > 0 && STRVIS_ISWHITE(src[0]))
154 ++src, --slen;
155 while (slen > 0 && STRVIS_ISWHITE(src[slen - 1]))
156 --slen;
157
158 while (slen > 0) {
159 if (*src < 0x20 || *src >= 0x80) {
160 /* non-printable characters */
161 dlen -= 4;
162 if (dlen < 1)
163 break;
164 *dst++ = '\\';
165 *dst++ = ((*src & 0300) >> 6) + '0';
166 *dst++ = ((*src & 0070) >> 3) + '0';
167 *dst++ = ((*src & 0007) >> 0) + '0';
168 } else if (*src == '\\') {
169 /* quote characters */
170 dlen -= 2;
171 if (dlen < 1)
172 break;
173 *dst++ = '\\';
174 *dst++ = '\\';
175 } else {
176 /* normal characters */
177 if (--dlen < 1)
178 break;
179 *dst++ = *src;
180 }
181 ++src, --slen;
182 }
183
184 *dst++ = 0;
185 }
186