Home | History | Annotate | Line # | Download | only in scsictl
scsi_subr.c revision 1.11.26.1
      1 /*	$NetBSD: scsi_subr.c,v 1.11.26.1 2008/06/02 13:21:24 mjf 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  *
     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.
     31  */
     32 
     33 /*
     34  * SCSI support subroutines.
     35  *
     36  * XXX THESE SHOULD BE IN A LIBRARY!
     37  */
     38 #include <sys/cdefs.h>
     39 
     40 #ifndef lint
     41 __RCSID("$NetBSD: scsi_subr.c,v 1.11.26.1 2008/06/02 13:21:24 mjf Exp $");
     42 #endif
     43 
     44 
     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>
     54 
     55 #include <dev/scsipi/scsi_spc.h>
     56 
     57 #include "extern.h"
     58 
     59 #define	STRVIS_ISWHITE(x) ((x) == ' ' || (x) == '\0' || (x) == (u_char)'\377')
     60 
     61 void
     62 scsi_command(int fd, void *cmd, size_t cmdlen, void *data, size_t datalen,
     63 	int timeout, int flags)
     64 {
     65 	scsireq_t req;
     66 
     67 	memset(&req, 0, sizeof(req));
     68 
     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;
     76 
     77 	if (ioctl(fd, SCIOCCOMMAND, &req) == -1)
     78 		err(1, "SCIOCCOMMAND");
     79 
     80 	if (req.retsts == SCCMD_OK)
     81 		return;
     82 
     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);
     93 
     94 	exit(1);
     95 }
     96 
     97 void
     98 scsi_mode_sense(int fd, u_int8_t pgcode, u_int8_t pctl, void *buf, size_t len)
     99 {
    100 	struct scsi_mode_sense_6 cmd;
    101 
    102 	memset(&cmd, 0, sizeof(cmd));
    103 	memset(buf, 0, len);
    104 
    105 	cmd.opcode = SCSI_MODE_SENSE_6;
    106 	cmd.page = pgcode | pctl;
    107 	cmd.length = len;
    108 
    109 	scsi_command(fd, &cmd, sizeof(cmd), buf, len, 10000, SCCMD_READ);
    110 }
    111 
    112 void
    113 scsi_mode_select(int fd, u_int8_t byte2, void *buf, size_t len)
    114 {
    115 	struct scsi_mode_select_6 cmd;
    116 
    117 	memset(&cmd, 0, sizeof(cmd));
    118 
    119 	cmd.opcode = SCSI_MODE_SELECT_6;
    120 	cmd.byte2 = SMS_PF | byte2;
    121 	cmd.length = len;
    122 
    123 	scsi_command(fd, &cmd, sizeof(cmd), buf, len, 10000, SCCMD_WRITE);
    124 }
    125 
    126 void
    127 scsi_request_sense(int fd, void *buf, size_t len)
    128 {
    129 	struct scsi_request_sense cmd;
    130 
    131 	memset(&cmd, 0, sizeof(cmd));
    132 	memset(buf, 0, len);
    133 
    134 	cmd.opcode = SCSI_REQUEST_SENSE;
    135 	cmd.length = len;
    136  	scsi_command(fd, &cmd, sizeof(cmd), buf, len, 10000, SCCMD_READ);
    137 }
    138 
    139 void
    140 scsi_strvis(char *sdst, size_t dlen, const char *ssrc, size_t slen)
    141 {
    142 	u_char *dst = (u_char *)sdst;
    143 	const u_char *src = (const u_char *)ssrc;
    144 
    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;
    150 
    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;
    173 		}
    174 		++src, --slen;
    175 	}
    176 
    177 	*dst++ = 0;
    178 }
    179