1 /* $NetBSD: scsi_subr.c,v 1.11 2005/02/21 00:29:08 thorpej Exp $ */
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
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
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>
41 __RCSID("$NetBSD: scsi_subr.c,v 1.11 2005/02/21 00:29:08 thorpej Exp $");
45 #include <sys/param.h>
46 #include <sys/ioctl.h>
47 #include <sys/scsiio.h>
55 #include <dev/scsipi/scsi_spc.h>
59 #define STRVIS_ISWHITE(x) ((x) == ' ' || (x) == '\0' || (x) == (u_char)'\377')
62 scsi_command(int fd
, void *cmd
, size_t cmdlen
, void *data
, size_t datalen
,
63 int timeout
, int flags
)
67 memset(&req
, 0, sizeof(req
));
69 memcpy(req
.cmd
, cmd
, cmdlen
);
72 req
.datalen
= datalen
;
73 req
.timeout
= timeout
;
75 req
.senselen
= SENSEBUFLEN
;
77 if (ioctl(fd
, SCIOCCOMMAND
, &req
) == -1)
78 err(1, "SCIOCCOMMAND");
80 if (req
.retsts
== SCCMD_OK
)
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);
91 fprintf(stderr
, "%s: device had unknown status %x\n", dvname
,
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
));
105 cmd
.opcode
= SCSI_MODE_SENSE_6
;
106 cmd
.page
= pgcode
| pctl
;
109 scsi_command(fd
, &cmd
, sizeof(cmd
), buf
, len
, 10000, SCCMD_READ
);
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
;
123 scsi_command(fd
, &cmd
, sizeof(cmd
), buf
, len
, 10000, SCCMD_WRITE
);
127 scsi_request_sense(int fd
, void *buf
, size_t len
)
129 struct scsi_request_sense cmd
;
131 memset(&cmd
, 0, sizeof(cmd
));
134 cmd
.opcode
= SCSI_REQUEST_SENSE
;
136 scsi_command(fd
, &cmd
, sizeof(cmd
), buf
, len
, 10000, SCCMD_READ
);
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]))
148 while (slen
> 0 && STRVIS_ISWHITE(src
[slen
- 1]))
152 if (*src
< 0x20 || *src
>= 0x80) {
153 /* non-printable characters */
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 */
169 /* normal characters */