1 /* $NetBSD: scsi_subr.c,v 1.5 2001/09/05 16:25:18 thorpej 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/scsipi_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 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.u_len.scsi.length = len; 115 116 scsi_command(fd, &cmd, sizeof(cmd), buf, len, 10000, SCCMD_READ); 117 } 118 119 void 120 scsi_mode_select(fd, byte2, buf, len) 121 int fd; 122 u_int8_t byte2; 123 void *buf; 124 size_t len; 125 { 126 struct scsipi_mode_select cmd; 127 128 memset(&cmd, 0, sizeof(cmd)); 129 130 cmd.opcode = MODE_SELECT; 131 cmd.byte2 = SMS_PF | byte2; 132 cmd.u_len.scsi.length = len; 133 134 scsi_command(fd, &cmd, sizeof(cmd), buf, len, 10000, SCCMD_WRITE); 135 } 136 137 void 138 scsi_strvis(sdst, dlen, ssrc, slen) 139 char *sdst; 140 size_t dlen; 141 const char *ssrc; 142 size_t slen; 143 { 144 u_char *dst = (u_char *)sdst; 145 const u_char *src = (const u_char *)ssrc; 146 147 /* Trim leading and trailing blanks and NULs. */ 148 while (slen > 0 && STRVIS_ISWHITE(src[0])) 149 ++src, --slen; 150 while (slen > 0 && STRVIS_ISWHITE(src[slen - 1])) 151 --slen; 152 153 while (slen > 0) { 154 if (*src < 0x20 || *src >= 0x80) { 155 /* non-printable characters */ 156 dlen -= 4; 157 if (dlen < 1) 158 break; 159 *dst++ = '\\'; 160 *dst++ = ((*src & 0300) >> 6) + '0'; 161 *dst++ = ((*src & 0070) >> 3) + '0'; 162 *dst++ = ((*src & 0007) >> 0) + '0'; 163 } else if (*src == '\\') { 164 /* quote characters */ 165 dlen -= 2; 166 if (dlen < 1) 167 break; 168 *dst++ = '\\'; 169 *dst++ = '\\'; 170 } else { 171 /* normal characters */ 172 if (--dlen < 1) 173 break; 174 *dst++ = *src; 175 } 176 ++src, --slen; 177 } 178 179 *dst++ = 0; 180 } 181