1 /* $NetBSD: scsipiconf.c,v 1.9 2000/05/15 16:35:49 dante 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. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39 /* 40 * Originally written by Julian Elischer (julian@tfs.com) 41 * for TRW Financial Systems for use under the MACH(2.5) operating system. 42 * 43 * TRW Financial Systems, in accordance with their agreement with Carnegie 44 * Mellon University, makes this software available to CMU to distribute 45 * or use in any manner that they see fit as long as this message is kept with 46 * the software. For this reason TFS also grants any other persons or 47 * organisations permission to use or modify this software. 48 * 49 * TFS supplies this software to be publicly redistributed 50 * on the understanding that TFS is not responsible for the correct 51 * functioning of this software in any circumstances. 52 * 53 * Ported to run under 386BSD by Julian Elischer (julian@tfs.com) Sept 1992 54 */ 55 56 #include <sys/types.h> 57 #include <sys/param.h> 58 #include <sys/systm.h> 59 #include <sys/malloc.h> 60 #include <sys/device.h> 61 62 #include <dev/scsipi/scsipi_all.h> 63 #include <dev/scsipi/scsipiconf.h> 64 65 /* 66 * Return a priority based on how much of the inquiry data matches 67 * the patterns for the particular driver. 68 */ 69 caddr_t 70 scsipi_inqmatch(inqbuf, base, nmatches, matchsize, bestpriority) 71 struct scsipi_inquiry_pattern *inqbuf; 72 caddr_t base; 73 int nmatches, matchsize; 74 int *bestpriority; 75 { 76 u_int8_t type; 77 caddr_t bestmatch; 78 79 /* Include the qualifier to catch vendor-unique types. */ 80 type = inqbuf->type; 81 82 for (*bestpriority = 0, bestmatch = 0; nmatches--; base += matchsize) { 83 struct scsipi_inquiry_pattern *match = (void *)base; 84 int priority, len; 85 86 if (type != match->type) 87 continue; 88 if (inqbuf->removable != match->removable) 89 continue; 90 priority = 2; 91 len = strlen(match->vendor); 92 if (bcmp(inqbuf->vendor, match->vendor, len)) 93 continue; 94 priority += len; 95 len = strlen(match->product); 96 if (bcmp(inqbuf->product, match->product, len)) 97 continue; 98 priority += len; 99 len = strlen(match->revision); 100 if (bcmp(inqbuf->revision, match->revision, len)) 101 continue; 102 priority += len; 103 104 #ifdef SCSIDEBUG 105 printf("scsipi_inqmatch: %d/%d/%d <%s, %s, %s>\n", 106 priority, match->type, match->removable, 107 match->vendor, match->product, match->revision); 108 #endif 109 if (priority > *bestpriority) { 110 *bestpriority = priority; 111 bestmatch = base; 112 } 113 } 114 115 return (bestmatch); 116 } 117 118 char * 119 scsipi_dtype(type) 120 int type; 121 { 122 char *dtype; 123 124 switch (type) { 125 case T_DIRECT: 126 dtype = "direct"; 127 break; 128 case T_SEQUENTIAL: 129 dtype = "sequential"; 130 break; 131 case T_PRINTER: 132 dtype = "printer"; 133 break; 134 case T_PROCESSOR: 135 dtype = "processor"; 136 break; 137 case T_WORM: 138 dtype = "worm"; 139 break; 140 case T_CDROM: 141 dtype = "cdrom"; 142 break; 143 case T_SCANNER: 144 dtype = "scanner"; 145 break; 146 case T_OPTICAL: 147 dtype = "optical"; 148 break; 149 case T_CHANGER: 150 dtype = "changer"; 151 break; 152 case T_COMM: 153 dtype = "communication"; 154 break; 155 case T_IT8_1: 156 case T_IT8_2: 157 dtype = "graphic arts pre-press"; 158 break; 159 case T_STORARRAY: 160 dtype = "storage array"; 161 break; 162 case T_ENCLOSURE: 163 dtype = "enclosure services"; 164 break; 165 case T_SIMPLE_DIRECT: 166 dtype = "simplified direct"; 167 break; 168 case T_OPTIC_CARD_RW: 169 dtype = "optical card r/w"; 170 break; 171 case T_OBJECT_STORED: 172 dtype = "object-based storage"; 173 break; 174 case T_NODEVICE: 175 panic("scsipi_dtype: impossible device type"); 176 default: 177 dtype = "unknown"; 178 break; 179 } 180 return (dtype); 181 } 182 183 void 184 scsipi_strvis(dst, dlen, src, slen) 185 u_char *dst, *src; 186 int dlen, slen; 187 { 188 189 /* Trim leading and trailing blanks and NULs. */ 190 while (slen > 0 && (src[0] == ' ' || src[0] == '\0')) 191 ++src, --slen; 192 while (slen > 0 && (src[slen-1] == ' ' || src[slen-1] == '\0')) 193 --slen; 194 195 while (slen > 0) { 196 if (*src < 0x20 || *src >= 0x80) { 197 /* non-printable characters */ 198 dlen -= 4; 199 if (dlen < 1) 200 break; 201 *dst++ = '\\'; 202 *dst++ = ((*src & 0300) >> 6) + '0'; 203 *dst++ = ((*src & 0070) >> 3) + '0'; 204 *dst++ = ((*src & 0007) >> 0) + '0'; 205 } else if (*src == '\\') { 206 /* quote characters */ 207 dlen -= 2; 208 if (dlen < 1) 209 break; 210 *dst++ = '\\'; 211 *dst++ = '\\'; 212 } else { 213 /* normal characters */ 214 if (--dlen < 1) 215 break; 216 *dst++ = *src; 217 } 218 ++src, --slen; 219 } 220 221 *dst++ = 0; 222 } 223