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