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