xref: /netbsd-src/sys/dev/scsipi/scsipiconf.c (revision dc306354b0b29af51801a7632f1e95265a68cd81)
1 /*	$NetBSD: scsipiconf.c,v 1.8 1998/11/17 14:38:43 bouyer 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 = "it8";		/* ??? */
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_NODEVICE:
166 		panic("scsipi_dtype: impossible device type");
167 	default:
168 		dtype = "unknown";
169 		break;
170 	}
171 	return (dtype);
172 }
173 
174 void
175 scsipi_strvis(dst, dlen, src, slen)
176 	u_char *dst, *src;
177 	int dlen, slen;
178 {
179 
180 	/* Trim leading and trailing blanks and NULs. */
181 	while (slen > 0 && (src[0] == ' ' || src[0] == '\0'))
182 		++src, --slen;
183 	while (slen > 0 && (src[slen-1] == ' ' || src[slen-1] == '\0'))
184 		--slen;
185 
186 	while (slen > 0) {
187 		if (*src < 0x20 || *src >= 0x80) {
188 			/* non-printable characters */
189 			dlen -= 4;
190 			if (dlen < 1)
191 				break;
192 			*dst++ = '\\';
193 			*dst++ = ((*src & 0300) >> 6) + '0';
194 			*dst++ = ((*src & 0070) >> 3) + '0';
195 			*dst++ = ((*src & 0007) >> 0) + '0';
196 		} else if (*src == '\\') {
197 			/* quote characters */
198 			dlen -= 2;
199 			if (dlen < 1)
200 				break;
201 			*dst++ = '\\';
202 			*dst++ = '\\';
203 		} else {
204 			/* normal characters */
205 			if (--dlen < 1)
206 				break;
207 			*dst++ = *src;
208 		}
209 		++src, --slen;
210 	}
211 
212 	*dst++ = 0;
213 }
214