xref: /netbsd-src/sys/dev/scsipi/scsipiconf.c (revision 93f9db1b75d415b78f73ed629beeb86235153473)
1 /*	$NetBSD: scsipiconf.c,v 1.7 1998/08/17 00:49:02 mycroft 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/scsi_all.h>
64 #include <dev/scsipi/scsipiconf.h>
65 
66 /*
67  * Return a priority based on how much of the inquiry data matches
68  * the patterns for the particular driver.
69  */
70 caddr_t
71 scsipi_inqmatch(inqbuf, base, nmatches, matchsize, bestpriority)
72 	struct scsipi_inquiry_pattern *inqbuf;
73 	caddr_t base;
74 	int nmatches, matchsize;
75 	int *bestpriority;
76 {
77 	u_int8_t type;
78 	caddr_t bestmatch;
79 
80 	/* Include the qualifier to catch vendor-unique types. */
81 	type = inqbuf->type;
82 
83 	for (*bestpriority = 0, bestmatch = 0; nmatches--; base += matchsize) {
84 		struct scsipi_inquiry_pattern *match = (void *)base;
85 		int priority, len;
86 
87 		if (type != match->type)
88 			continue;
89 		if (inqbuf->removable != match->removable)
90 			continue;
91 		priority = 2;
92 		len = strlen(match->vendor);
93 		if (bcmp(inqbuf->vendor, match->vendor, len))
94 			continue;
95 		priority += len;
96 		len = strlen(match->product);
97 		if (bcmp(inqbuf->product, match->product, len))
98 			continue;
99 		priority += len;
100 		len = strlen(match->revision);
101 		if (bcmp(inqbuf->revision, match->revision, len))
102 			continue;
103 		priority += len;
104 
105 #ifdef SCSIDEBUG
106 		printf("scsipi_inqmatch: %d/%d/%d <%s, %s, %s>\n",
107 		    priority, match->type, match->removable,
108 		    match->vendor, match->product, match->revision);
109 #endif
110 		if (priority > *bestpriority) {
111 			*bestpriority = priority;
112 			bestmatch = base;
113 		}
114 	}
115 
116 	return (bestmatch);
117 }
118 
119 char *
120 scsipi_dtype(type)
121 	int type;
122 {
123 	char *dtype;
124 
125 	switch (type) {
126 	case T_DIRECT:
127 		dtype = "direct";
128 		break;
129 	case T_SEQUENTIAL:
130 		dtype = "sequential";
131 		break;
132 	case T_PRINTER:
133 		dtype = "printer";
134 		break;
135 	case T_PROCESSOR:
136 		dtype = "processor";
137 		break;
138 	case T_WORM:
139 		dtype = "worm";
140 		break;
141 	case T_CDROM:
142 		dtype = "cdrom";
143 		break;
144 	case T_SCANNER:
145 		dtype = "scanner";
146 		break;
147 	case T_OPTICAL:
148 		dtype = "optical";
149 		break;
150 	case T_CHANGER:
151 		dtype = "changer";
152 		break;
153 	case T_COMM:
154 		dtype = "communication";
155 		break;
156 	case T_IT8_1:
157 	case T_IT8_2:
158 		dtype = "it8";		/* ??? */
159 		break;
160 	case T_STORARRAY:
161 		dtype = "storage array";
162 		break;
163 	case T_ENCLOSURE:
164 		dtype = "enclosure services";
165 		break;
166 	case T_NODEVICE:
167 		panic("scsipi_dtype: impossible device type");
168 	default:
169 		dtype = "unknown";
170 		break;
171 	}
172 	return (dtype);
173 }
174 
175 void
176 scsipi_strvis(dst, dlen, src, slen)
177 	u_char *dst, *src;
178 	int dlen, slen;
179 {
180 
181 	/* Trim leading and trailing blanks and NULs. */
182 	while (slen > 0 && (src[0] == ' ' || src[0] == '\0'))
183 		++src, --slen;
184 	while (slen > 0 && (src[slen-1] == ' ' || src[slen-1] == '\0'))
185 		--slen;
186 
187 	while (slen > 0) {
188 		if (*src < 0x20 || *src >= 0x80) {
189 			/* non-printable characters */
190 			dlen -= 4;
191 			if (dlen < 1)
192 				break;
193 			*dst++ = '\\';
194 			*dst++ = ((*src & 0300) >> 6) + '0';
195 			*dst++ = ((*src & 0070) >> 3) + '0';
196 			*dst++ = ((*src & 0007) >> 0) + '0';
197 		} else if (*src == '\\') {
198 			/* quote characters */
199 			dlen -= 2;
200 			if (dlen < 1)
201 				break;
202 			*dst++ = '\\';
203 			*dst++ = '\\';
204 		} else {
205 			/* normal characters */
206 			if (--dlen < 1)
207 				break;
208 			*dst++ = *src;
209 		}
210 		++src, --slen;
211 	}
212 
213 	*dst++ = 0;
214 }
215