scsipiconf.c revision 1.12 1 /* $NetBSD: scsipiconf.c,v 1.12 2000/12/31 17:54:29 ad 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 (at) 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 (at) 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 #include <sys/proc.h>
62
63 #include <uvm/uvm_extern.h>
64
65 #include <dev/scsipi/scsipi_all.h>
66 #include <dev/scsipi/scsipiconf.h>
67
68 #define STRVIS_ISWHITE(x) ((x) == ' ' || (x) == '\0' || (x) == (u_char)'\377')
69
70 int
71 scsipi_command(sc_link, cmd, cmdlen, data_addr, datalen, retries, timeout, bp,
72 flags)
73 struct scsipi_link *sc_link;
74 struct scsipi_generic *cmd;
75 int cmdlen;
76 u_char *data_addr;
77 int datalen;
78 int retries;
79 int timeout;
80 struct buf *bp;
81 int flags;
82 {
83 int error;
84
85 if ((flags & XS_CTL_DATA_ONSTACK) != 0) {
86 /*
87 * If the I/O buffer is allocated on stack, the
88 * process must NOT be swapped out, as the device will
89 * be accessing the stack.
90 */
91 PHOLD(curproc);
92 }
93 error = (*sc_link->scsipi_cmd)(sc_link, cmd, cmdlen, data_addr,
94 datalen, retries, timeout, bp, flags);
95 if ((flags & XS_CTL_DATA_ONSTACK) != 0)
96 PRELE(curproc);
97 return (error);
98 }
99
100 /*
101 * Return a priority based on how much of the inquiry data matches
102 * the patterns for the particular driver.
103 */
104 caddr_t
105 scsipi_inqmatch(inqbuf, base, nmatches, matchsize, bestpriority)
106 struct scsipi_inquiry_pattern *inqbuf;
107 caddr_t base;
108 int nmatches, matchsize;
109 int *bestpriority;
110 {
111 u_int8_t type;
112 caddr_t bestmatch;
113
114 /* Include the qualifier to catch vendor-unique types. */
115 type = inqbuf->type;
116
117 for (*bestpriority = 0, bestmatch = 0; nmatches--; base += matchsize) {
118 struct scsipi_inquiry_pattern *match = (void *)base;
119 int priority, len;
120
121 if (type != match->type)
122 continue;
123 if (inqbuf->removable != match->removable)
124 continue;
125 priority = 2;
126 len = strlen(match->vendor);
127 if (bcmp(inqbuf->vendor, match->vendor, len))
128 continue;
129 priority += len;
130 len = strlen(match->product);
131 if (bcmp(inqbuf->product, match->product, len))
132 continue;
133 priority += len;
134 len = strlen(match->revision);
135 if (bcmp(inqbuf->revision, match->revision, len))
136 continue;
137 priority += len;
138
139 #ifdef SCSIDEBUG
140 printf("scsipi_inqmatch: %d/%d/%d <%s, %s, %s>\n",
141 priority, match->type, match->removable,
142 match->vendor, match->product, match->revision);
143 #endif
144 if (priority > *bestpriority) {
145 *bestpriority = priority;
146 bestmatch = base;
147 }
148 }
149
150 return (bestmatch);
151 }
152
153 char *
154 scsipi_dtype(type)
155 int type;
156 {
157 char *dtype;
158
159 switch (type) {
160 case T_DIRECT:
161 dtype = "direct";
162 break;
163 case T_SEQUENTIAL:
164 dtype = "sequential";
165 break;
166 case T_PRINTER:
167 dtype = "printer";
168 break;
169 case T_PROCESSOR:
170 dtype = "processor";
171 break;
172 case T_WORM:
173 dtype = "worm";
174 break;
175 case T_CDROM:
176 dtype = "cdrom";
177 break;
178 case T_SCANNER:
179 dtype = "scanner";
180 break;
181 case T_OPTICAL:
182 dtype = "optical";
183 break;
184 case T_CHANGER:
185 dtype = "changer";
186 break;
187 case T_COMM:
188 dtype = "communication";
189 break;
190 case T_IT8_1:
191 case T_IT8_2:
192 dtype = "graphic arts pre-press";
193 break;
194 case T_STORARRAY:
195 dtype = "storage array";
196 break;
197 case T_ENCLOSURE:
198 dtype = "enclosure services";
199 break;
200 case T_SIMPLE_DIRECT:
201 dtype = "simplified direct";
202 break;
203 case T_OPTIC_CARD_RW:
204 dtype = "optical card r/w";
205 break;
206 case T_OBJECT_STORED:
207 dtype = "object-based storage";
208 break;
209 case T_NODEVICE:
210 panic("scsipi_dtype: impossible device type");
211 default:
212 dtype = "unknown";
213 break;
214 }
215 return (dtype);
216 }
217
218 void
219 scsipi_strvis(dst, dlen, src, slen)
220 u_char *dst, *src;
221 int dlen, slen;
222 {
223
224 /* Trim leading and trailing blanks and NULs. */
225 while (slen > 0 && STRVIS_ISWHITE(src[0]))
226 ++src, --slen;
227 while (slen > 0 && STRVIS_ISWHITE(src[slen - 1]))
228 --slen;
229
230 while (slen > 0) {
231 if (*src < 0x20 || *src >= 0x80) {
232 /* non-printable characters */
233 dlen -= 4;
234 if (dlen < 1)
235 break;
236 *dst++ = '\\';
237 *dst++ = ((*src & 0300) >> 6) + '0';
238 *dst++ = ((*src & 0070) >> 3) + '0';
239 *dst++ = ((*src & 0007) >> 0) + '0';
240 } else if (*src == '\\') {
241 /* quote characters */
242 dlen -= 2;
243 if (dlen < 1)
244 break;
245 *dst++ = '\\';
246 *dst++ = '\\';
247 } else {
248 /* normal characters */
249 if (--dlen < 1)
250 break;
251 *dst++ = *src;
252 }
253 ++src, --slen;
254 }
255
256 *dst++ = 0;
257 }
258