1 /* $NetBSD: scsipiconf.c,v 1.35 2007/07/09 21:01:22 ad Exp $ */
4 * Copyright (c) 1998, 1999, 2004 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Charles M. Hannum; by Jason R. Thorpe of the Numerical Aerospace
9 * Simulation Facility, NASA Ames Research Center.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
34 * Originally written by Julian Elischer (julian@tfs.com)
35 * for TRW Financial Systems for use under the MACH(2.5) operating system.
37 * TRW Financial Systems, in accordance with their agreement with Carnegie
38 * Mellon University, makes this software available to CMU to distribute
39 * or use in any manner that they see fit as long as this message is kept with
40 * the software. For this reason TFS also grants any other persons or
41 * organisations permission to use or modify this software.
43 * TFS supplies this software to be publicly redistributed
44 * on the understanding that TFS is not responsible for the correct
45 * functioning of this software in any circumstances.
47 * Ported to run under 386BSD by Julian Elischer (julian@tfs.com) Sept 1992
50 #include <sys/cdefs.h>
51 __KERNEL_RCSID(0, "$NetBSD: scsipiconf.c,v 1.35 2007/07/09 21:01:22 ad Exp $");
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/malloc.h>
56 #include <sys/device.h>
59 #include <dev/scsipi/scsipi_all.h>
60 #include <dev/scsipi/scsipiconf.h>
61 #include <dev/scsipi/scsipi_base.h>
63 #define STRVIS_ISWHITE(x) ((x) == ' ' || (x) == '\0' || (x) == (u_char)'\377')
66 scsipi_command(struct scsipi_periph
*periph
, struct scsipi_generic
*cmd
,
67 int cmdlen
, u_char
*data_addr
, int datalen
, int retries
, int timeout
,
68 struct buf
*bp
, int flags
)
70 struct scsipi_xfer
*xs
;
72 xs
= scsipi_make_xs(periph
, cmd
, cmdlen
, data_addr
, datalen
, retries
,
77 return (scsipi_execute_xs(xs
));
81 * allocate and init a scsipi_periph structure for a new device.
83 struct scsipi_periph
*
84 scsipi_alloc_periph(int malloc_flag
)
86 struct scsipi_periph
*periph
;
89 periph
= malloc(sizeof(*periph
), M_DEVBUF
, malloc_flag
|M_ZERO
);
93 periph
->periph_dev
= NULL
;
96 * Start with one command opening. The periph driver
97 * will grow this if it knows it can take advantage of it.
99 periph
->periph_openings
= 1;
100 periph
->periph_active
= 0;
102 for (i
= 0; i
< PERIPH_NTAGWORDS
; i
++)
103 periph
->periph_freetags
[i
] = 0xffffffff;
105 TAILQ_INIT(&periph
->periph_xferq
);
106 callout_init(&periph
->periph_callout
, 0);
112 * Return a priority based on how much of the inquiry data matches
113 * the patterns for the particular driver.
116 scsipi_inqmatch(struct scsipi_inquiry_pattern
*inqbuf
, const void *base
,
117 size_t nmatches
, size_t matchsize
, int *bestpriority
)
120 const struct scsipi_inquiry_pattern
*bestmatch
;
122 /* Include the qualifier to catch vendor-unique types. */
125 for (*bestpriority
= 0, bestmatch
= 0; nmatches
--;
126 base
= (const char *)base
+ matchsize
) {
127 const struct scsipi_inquiry_pattern
*match
= base
;
130 if (type
!= match
->type
)
132 if (inqbuf
->removable
!= match
->removable
)
135 len
= strlen(match
->vendor
);
136 if (memcmp(inqbuf
->vendor
, match
->vendor
, len
))
139 len
= strlen(match
->product
);
140 if (memcmp(inqbuf
->product
, match
->product
, len
))
143 len
= strlen(match
->revision
);
144 if (memcmp(inqbuf
->revision
, match
->revision
, len
))
149 printf("scsipi_inqmatch: %d/%d/%d <%s, %s, %s>\n",
150 priority
, match
->type
, match
->removable
,
151 match
->vendor
, match
->product
, match
->revision
);
153 if (priority
> *bestpriority
) {
154 *bestpriority
= priority
;
163 scsipi_dtype(int type
)
196 dtype
= "communication";
200 dtype
= "graphic arts pre-press";
203 dtype
= "storage array";
206 dtype
= "enclosure services";
208 case T_SIMPLE_DIRECT
:
209 dtype
= "simplified direct";
211 case T_OPTIC_CARD_RW
:
212 dtype
= "optical card r/w";
214 case T_OBJECT_STORED
:
215 dtype
= "object-based storage";
218 panic("scsipi_dtype: impossible device type");
227 scsipi_strvis(u_char
*dst
, int dlen
, const u_char
*src
, int slen
)
230 /* Trim leading and trailing blanks and NULs. */
231 while (slen
> 0 && STRVIS_ISWHITE(src
[0]))
233 while (slen
> 0 && STRVIS_ISWHITE(src
[slen
- 1]))
237 if (*src
< 0x20 || *src
>= 0x80) {
238 /* non-printable characters */
243 *dst
++ = ((*src
& 0300) >> 6) + '0';
244 *dst
++ = ((*src
& 0070) >> 3) + '0';
245 *dst
++ = ((*src
& 0007) >> 0) + '0';
246 } else if (*src
== '\\') {
247 /* quote characters */
254 /* normal characters */