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