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