No empty .Rs/.Re
[netbsd-mini2440.git] / sbin / scsictl / scsi_subr.c
blob3fe65ef8d5f48aa1f2675aa29350100ac4b44f43
1 /* $NetBSD: scsi_subr.c,v 1.11 2005/02/21 00:29:08 thorpej Exp $ */
3 /*-
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
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.
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.
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
34 * SCSI support subroutines.
36 * XXX THESE SHOULD BE IN A LIBRARY!
38 #include <sys/cdefs.h>
40 #ifndef lint
41 __RCSID("$NetBSD: scsi_subr.c,v 1.11 2005/02/21 00:29:08 thorpej Exp $");
42 #endif
45 #include <sys/param.h>
46 #include <sys/ioctl.h>
47 #include <sys/scsiio.h>
48 #include <err.h>
49 #include <errno.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <unistd.h>
55 #include <dev/scsipi/scsi_spc.h>
57 #include "extern.h"
59 #define STRVIS_ISWHITE(x) ((x) == ' ' || (x) == '\0' || (x) == (u_char)'\377')
61 void
62 scsi_command(int fd, void *cmd, size_t cmdlen, void *data, size_t datalen,
63 int timeout, int flags)
65 scsireq_t req;
67 memset(&req, 0, sizeof(req));
69 memcpy(req.cmd, cmd, cmdlen);
70 req.cmdlen = cmdlen;
71 req.databuf = data;
72 req.datalen = datalen;
73 req.timeout = timeout;
74 req.flags = flags;
75 req.senselen = SENSEBUFLEN;
77 if (ioctl(fd, SCIOCCOMMAND, &req) == -1)
78 err(1, "SCIOCCOMMAND");
80 if (req.retsts == SCCMD_OK)
81 return;
83 /* Some problem; report it and exit. */
84 if (req.retsts == SCCMD_TIMEOUT)
85 fprintf(stderr, "%s: SCSI command timed out\n", dvname);
86 else if (req.retsts == SCCMD_BUSY)
87 fprintf(stderr, "%s: device is busy\n", dvname);
88 else if (req.retsts == SCCMD_SENSE)
89 scsi_print_sense(dvname, &req, 1);
90 else
91 fprintf(stderr, "%s: device had unknown status %x\n", dvname,
92 req.retsts);
94 exit(1);
97 void
98 scsi_mode_sense(int fd, u_int8_t pgcode, u_int8_t pctl, void *buf, size_t len)
100 struct scsi_mode_sense_6 cmd;
102 memset(&cmd, 0, sizeof(cmd));
103 memset(buf, 0, len);
105 cmd.opcode = SCSI_MODE_SENSE_6;
106 cmd.page = pgcode | pctl;
107 cmd.length = len;
109 scsi_command(fd, &cmd, sizeof(cmd), buf, len, 10000, SCCMD_READ);
112 void
113 scsi_mode_select(int fd, u_int8_t byte2, void *buf, size_t len)
115 struct scsi_mode_select_6 cmd;
117 memset(&cmd, 0, sizeof(cmd));
119 cmd.opcode = SCSI_MODE_SELECT_6;
120 cmd.byte2 = SMS_PF | byte2;
121 cmd.length = len;
123 scsi_command(fd, &cmd, sizeof(cmd), buf, len, 10000, SCCMD_WRITE);
126 void
127 scsi_request_sense(int fd, void *buf, size_t len)
129 struct scsi_request_sense cmd;
131 memset(&cmd, 0, sizeof(cmd));
132 memset(buf, 0, len);
134 cmd.opcode = SCSI_REQUEST_SENSE;
135 cmd.length = len;
136 scsi_command(fd, &cmd, sizeof(cmd), buf, len, 10000, SCCMD_READ);
139 void
140 scsi_strvis(char *sdst, size_t dlen, const char *ssrc, size_t slen)
142 u_char *dst = (u_char *)sdst;
143 const u_char *src = (const u_char *)ssrc;
145 /* Trim leading and trailing blanks and NULs. */
146 while (slen > 0 && STRVIS_ISWHITE(src[0]))
147 ++src, --slen;
148 while (slen > 0 && STRVIS_ISWHITE(src[slen - 1]))
149 --slen;
151 while (slen > 0) {
152 if (*src < 0x20 || *src >= 0x80) {
153 /* non-printable characters */
154 dlen -= 4;
155 if (dlen < 1)
156 break;
157 *dst++ = '\\';
158 *dst++ = ((*src & 0300) >> 6) + '0';
159 *dst++ = ((*src & 0070) >> 3) + '0';
160 *dst++ = ((*src & 0007) >> 0) + '0';
161 } else if (*src == '\\') {
162 /* quote characters */
163 dlen -= 2;
164 if (dlen < 1)
165 break;
166 *dst++ = '\\';
167 *dst++ = '\\';
168 } else {
169 /* normal characters */
170 if (--dlen < 1)
171 break;
172 *dst++ = *src;
174 ++src, --slen;
177 *dst++ = 0;