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