atapiconf.c revision 1.65 1 /* $NetBSD: atapiconf.c,v 1.65 2004/08/21 21:30:29 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 1996, 2001 Manuel Bouyer. 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 Manuel Bouyer.
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 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: atapiconf.c,v 1.65 2004/08/21 21:30:29 thorpej Exp $");
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/malloc.h>
38 #include <sys/device.h>
39 #include <sys/buf.h>
40 #include <sys/proc.h>
41 #include <sys/kthread.h>
42
43 #include <dev/scsipi/scsipi_all.h>
44 #include <dev/scsipi/scsipiconf.h>
45 #include <dev/scsipi/atapiconf.h>
46
47 #include "locators.h"
48
49 #define SILENT_PRINTF(flags,string) if (!(flags & A_SILENT)) printf string
50 #define MAX_TARGET 1
51
52 const struct scsipi_periphsw atapi_probe_periphsw = {
53 NULL,
54 NULL,
55 NULL,
56 NULL,
57 };
58
59 static int atapibusmatch(struct device *, struct cfdata *, void *);
60 static void atapibusattach(struct device *, struct device *, void *);
61 static int atapibusactivate(struct device *, enum devact);
62 static int atapibusdetach(struct device *, int flags);
63
64 static int atapibussubmatch(struct device *, struct cfdata *, void *);
65
66 static int atapi_probe_bus(struct atapibus_softc *, int);
67
68 static int atapibusprint(void *, const char *);
69
70 CFATTACH_DECL(atapibus, sizeof(struct atapibus_softc),
71 atapibusmatch, atapibusattach, atapibusdetach, atapibusactivate);
72
73 extern struct cfdriver atapibus_cd;
74
75 static const struct scsi_quirk_inquiry_pattern atapi_quirk_patterns[] = {
76 {{T_CDROM, T_REMOV,
77 "ALPS ELECTRIC CO.,LTD. DC544C", "", "SW03D"}, PQUIRK_NOTUR},
78 {{T_CDROM, T_REMOV,
79 "CR-2801TE", "", "1.07"}, PQUIRK_NOSENSE},
80 {{T_CDROM, T_REMOV,
81 "CREATIVECD3630E", "", "AC101"}, PQUIRK_NOSENSE},
82 {{T_CDROM, T_REMOV,
83 "FX320S", "", "q01"}, PQUIRK_NOSENSE},
84 {{T_CDROM, T_REMOV,
85 "GCD-R580B", "", "1.00"}, PQUIRK_LITTLETOC},
86 {{T_CDROM, T_REMOV,
87 "HITACHI CDR-7730", "", "0008a"}, PQUIRK_NOSENSE},
88 {{T_CDROM, T_REMOV,
89 "MATSHITA CR-574", "", "1.02"}, PQUIRK_NOCAPACITY},
90 {{T_CDROM, T_REMOV,
91 "MATSHITA CR-574", "", "1.06"}, PQUIRK_NOCAPACITY},
92 {{T_CDROM, T_REMOV,
93 "Memorex CRW-2642", "", "1.0g"}, PQUIRK_NOSENSE},
94 {{T_CDROM, T_REMOV,
95 "NEC CD-ROM DRIVE:273", "", "4.21"}, PQUIRK_NOTUR},
96 {{T_CDROM, T_REMOV,
97 "SANYO CRD-256P", "", "1.02"}, PQUIRK_NOCAPACITY},
98 {{T_CDROM, T_REMOV,
99 "SANYO CRD-254P", "", "1.02"}, PQUIRK_NOCAPACITY},
100 {{T_CDROM, T_REMOV,
101 "SANYO CRD-S54P", "", "1.08"}, PQUIRK_NOCAPACITY},
102 {{T_CDROM, T_REMOV,
103 "CD-ROM CDR-S1", "", "1.70"}, PQUIRK_NOCAPACITY}, /* Sanyo */
104 {{T_CDROM, T_REMOV,
105 "CD-ROM CDR-N16", "", "1.25"}, PQUIRK_NOCAPACITY}, /* Sanyo */
106 };
107
108 int
109 atapiprint(void *aux, const char *pnp)
110 {
111 if (pnp)
112 aprint_normal("atapibus at %s", pnp);
113 return (UNCONF);
114 }
115
116 static int
117 atapibusmatch(struct device *parent, struct cfdata *cf, void *aux)
118 {
119 struct scsipi_channel *chan = aux;
120
121 if (chan == NULL)
122 return (0);
123
124 if (chan->chan_bustype->bustype_type != SCSIPI_BUSTYPE_ATAPI)
125 return (0);
126
127 return (1);
128 }
129
130 static int
131 atapibussubmatch(struct device *parent, struct cfdata *cf, void *aux)
132 {
133 struct scsipibus_attach_args *sa = aux;
134 struct scsipi_periph *periph = sa->sa_periph;
135
136 if (cf->cf_loc[ATAPIBUSCF_DRIVE] != ATAPIBUSCF_DRIVE_DEFAULT &&
137 cf->cf_loc[ATAPIBUSCF_DRIVE] != periph->periph_target)
138 return (0);
139 return (config_match(parent, cf, aux));
140 }
141
142 static void
143 atapibusattach(struct device *parent, struct device *self, void *aux)
144 {
145 struct atapibus_softc *sc = (void *) self;
146 struct scsipi_channel *chan = aux;
147
148 sc->sc_channel = chan;
149
150 chan->chan_name = sc->sc_dev.dv_xname;
151
152 /* ATAPI has no LUNs. */
153 chan->chan_nluns = 1;
154 printf(": %d targets\n", chan->chan_ntargets);
155
156 /* Initialize the channel. */
157 chan->chan_init_cb = NULL;
158 chan->chan_init_cb_arg = NULL;
159 scsipi_channel_init(chan);
160
161 /* Probe the bus for devices. */
162 atapi_probe_bus(sc, -1);
163 }
164
165 static int
166 atapibusactivate(struct device *self, enum devact act)
167 {
168 struct atapibus_softc *sc = (void *) self;
169 struct scsipi_channel *chan = sc->sc_channel;
170 struct scsipi_periph *periph;
171 int target, error = 0, s;
172
173 s = splbio();
174 switch (act) {
175 case DVACT_ACTIVATE:
176 error = EOPNOTSUPP;
177 break;
178
179 case DVACT_DEACTIVATE:
180 for (target = 0; target < chan->chan_ntargets; target++) {
181 periph = scsipi_lookup_periph(chan, target, 0);
182 if (periph == NULL)
183 continue;
184 error = config_deactivate(periph->periph_dev);
185 if (error)
186 goto out;
187 }
188 break;
189 }
190 out:
191 splx(s);
192 return (error);
193 }
194
195 static int
196 atapibusdetach(struct device *self, int flags)
197 {
198 struct atapibus_softc *sc = (void *)self;
199 struct scsipi_channel *chan = sc->sc_channel;
200 struct scsipi_periph *periph;
201 int target, error;
202
203 /*
204 * Shut down the channel.
205 */
206 scsipi_channel_shutdown(chan);
207
208 /*
209 * Now detach all of the periphs.
210 */
211 for (target = 0; target < chan->chan_ntargets; target++) {
212 periph = scsipi_lookup_periph(chan, target, 0);
213 if (periph == NULL)
214 continue;
215 error = config_detach(periph->periph_dev, flags);
216 if (error)
217 return (error);
218
219 scsipi_remove_periph(chan, periph);
220 free(periph, M_DEVBUF);
221 }
222 return (0);
223 }
224
225 static int
226 atapi_probe_bus(struct atapibus_softc *sc, int target)
227 {
228 struct scsipi_channel *chan = sc->sc_channel;
229 int maxtarget, mintarget;
230 int error;
231 struct atapi_adapter *atapi_adapter;
232
233 if (target == -1) {
234 maxtarget = 1;
235 mintarget = 0;
236 } else {
237 if (target < 0 || target >= chan->chan_ntargets)
238 return (ENXIO);
239 maxtarget = mintarget = target;
240 }
241
242 if ((error = scsipi_adapter_addref(chan->chan_adapter)) != 0)
243 return (error);
244 atapi_adapter = (struct atapi_adapter*)chan->chan_adapter;
245 for (target = mintarget; target <= maxtarget; target++)
246 atapi_adapter->atapi_probe_device(sc, target);
247 scsipi_adapter_delref(chan->chan_adapter);
248 return (0);
249 }
250
251 void *
252 atapi_probe_device(struct atapibus_softc *sc, int target,
253 struct scsipi_periph *periph, struct scsipibus_attach_args *sa)
254 {
255 struct scsipi_channel *chan = sc->sc_channel;
256 struct scsi_quirk_inquiry_pattern *finger;
257 struct cfdata *cf;
258 int priority, quirks;
259
260 finger = (struct scsi_quirk_inquiry_pattern *)scsipi_inqmatch(
261 &sa->sa_inqbuf, (caddr_t)atapi_quirk_patterns,
262 sizeof(atapi_quirk_patterns) /
263 sizeof(atapi_quirk_patterns[0]),
264 sizeof(atapi_quirk_patterns[0]), &priority);
265
266 if (finger != NULL)
267 quirks = finger->quirks;
268 else
269 quirks = 0;
270
271 /*
272 * Now apply any quirks from the table.
273 */
274 periph->periph_quirks |= quirks;
275
276 if ((cf = config_search(atapibussubmatch, &sc->sc_dev,
277 sa)) != 0) {
278 scsipi_insert_periph(chan, periph);
279 /*
280 * XXX Can't assign periph_dev here, because we'll
281 * XXX need it before config_attach() returns. Must
282 * XXX assign it in periph driver.
283 */
284 return config_attach(&sc->sc_dev, cf, sa,
285 atapibusprint);
286 } else {
287 atapibusprint(sa, sc->sc_dev.dv_xname);
288 printf(" not configured\n");
289 free(periph, M_DEVBUF);
290 return NULL;
291 }
292 }
293
294 static int
295 atapibusprint(void *aux, const char *pnp)
296 {
297 struct scsipibus_attach_args *sa = aux;
298 struct scsipi_inquiry_pattern *inqbuf;
299 const char *dtype;
300
301 if (pnp != NULL)
302 aprint_normal("%s", pnp);
303
304 inqbuf = &sa->sa_inqbuf;
305
306 dtype = scsipi_dtype(inqbuf->type & SID_TYPE);
307 aprint_normal(" drive %d: <%s, %s, %s> %s %s",
308 sa->sa_periph->periph_target, inqbuf->vendor,
309 inqbuf->product, inqbuf->revision, dtype,
310 inqbuf->removable ? "removable" : "fixed");
311 return (UNCONF);
312 }
313