atapiconf.c revision 1.43 1 1.43 lukem /* $NetBSD: atapiconf.c,v 1.43 2001/11/15 09:48:16 lukem Exp $ */
2 1.2 bouyer
3 1.2 bouyer /*
4 1.2 bouyer * Copyright (c) 1996 Manuel Bouyer. All rights reserved.
5 1.2 bouyer *
6 1.2 bouyer * Redistribution and use in source and binary forms, with or without
7 1.2 bouyer * modification, are permitted provided that the following conditions
8 1.2 bouyer * are met:
9 1.2 bouyer * 1. Redistributions of source code must retain the above copyright
10 1.2 bouyer * notice, this list of conditions and the following disclaimer.
11 1.2 bouyer * 2. Redistributions in binary form must reproduce the above copyright
12 1.2 bouyer * notice, this list of conditions and the following disclaimer in the
13 1.2 bouyer * documentation and/or other materials provided with the distribution.
14 1.2 bouyer * 3. All advertising materials mentioning features or use of this software
15 1.2 bouyer * must display the following acknowledgement:
16 1.2 bouyer * This product includes software developed by Manuel Bouyer.
17 1.2 bouyer * 4. The name of the author may not be used to endorse or promote products
18 1.2 bouyer * derived from this software without specific prior written permission.
19 1.2 bouyer *
20 1.2 bouyer * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 1.2 bouyer * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 1.2 bouyer * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 1.2 bouyer * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 1.2 bouyer * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 1.2 bouyer * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 1.2 bouyer * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 1.2 bouyer * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 1.2 bouyer * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 1.2 bouyer * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 1.2 bouyer */
31 1.42 lukem
32 1.42 lukem #include <sys/cdefs.h>
33 1.43 lukem __KERNEL_RCSID(0, "$NetBSD: atapiconf.c,v 1.43 2001/11/15 09:48:16 lukem Exp $");
34 1.2 bouyer
35 1.2 bouyer #include <sys/param.h>
36 1.2 bouyer #include <sys/systm.h>
37 1.2 bouyer #include <sys/malloc.h>
38 1.2 bouyer #include <sys/device.h>
39 1.2 bouyer #include <sys/buf.h>
40 1.4 enami #include <sys/proc.h>
41 1.39 bouyer #include <sys/kthread.h>
42 1.2 bouyer
43 1.13 bouyer #include <dev/ata/atavar.h>
44 1.2 bouyer #include <dev/scsipi/scsipi_all.h>
45 1.39 bouyer #include <dev/scsipi/scsipiconf.h>
46 1.2 bouyer #include <dev/scsipi/atapiconf.h>
47 1.4 enami
48 1.2 bouyer #include "locators.h"
49 1.2 bouyer
50 1.2 bouyer #define SILENT_PRINTF(flags,string) if (!(flags & A_SILENT)) printf string
51 1.27 enami #define MAX_TARGET 1
52 1.2 bouyer
53 1.39 bouyer const struct scsipi_periphsw atapi_probe_periphsw = {
54 1.39 bouyer NULL,
55 1.39 bouyer NULL,
56 1.39 bouyer NULL,
57 1.39 bouyer NULL,
58 1.39 bouyer };
59 1.39 bouyer
60 1.2 bouyer int atapibusmatch __P((struct device *, struct cfdata *, void *));
61 1.2 bouyer void atapibusattach __P((struct device *, struct device *, void *));
62 1.27 enami int atapibusactivate __P((struct device *, enum devact));
63 1.27 enami int atapibusdetach __P((struct device *, int flags));
64 1.2 bouyer
65 1.39 bouyer int atapibussubmatch __P((struct device *, struct cfdata *, void *));
66 1.39 bouyer
67 1.39 bouyer int atapi_probe_bus __P((struct atapibus_softc *, int));
68 1.2 bouyer
69 1.2 bouyer struct cfattach atapibus_ca = {
70 1.27 enami sizeof(struct atapibus_softc), atapibusmatch, atapibusattach,
71 1.27 enami atapibusdetach, atapibusactivate,
72 1.2 bouyer };
73 1.2 bouyer
74 1.8 thorpej extern struct cfdriver atapibus_cd;
75 1.2 bouyer
76 1.2 bouyer int atapibusprint __P((void *, const char *));
77 1.2 bouyer
78 1.36 jdolecek const struct scsi_quirk_inquiry_pattern atapi_quirk_patterns[] = {
79 1.2 bouyer {{T_CDROM, T_REMOV,
80 1.39 bouyer "ALPS ELECTRIC CO.,LTD. DC544C", "", "SW03D"}, PQUIRK_NOTUR},
81 1.2 bouyer {{T_CDROM, T_REMOV,
82 1.39 bouyer "BCD-16X 1997-04-25", "", "VER 2.2"}, PQUIRK_NOSTARTUNIT},
83 1.26 bouyer {{T_CDROM, T_REMOV,
84 1.39 bouyer "BCD-24X 1997-06-27", "", "VER 2.0"}, PQUIRK_NOSTARTUNIT},
85 1.22 bouyer {{T_CDROM, T_REMOV,
86 1.39 bouyer "CR-2801TE", "", "1.07"}, PQUIRK_NOSENSE},
87 1.23 bouyer {{T_CDROM, T_REMOV,
88 1.39 bouyer "CREATIVECD3630E", "", "AC101"}, PQUIRK_NOSENSE},
89 1.2 bouyer {{T_CDROM, T_REMOV,
90 1.39 bouyer "FX320S", "", "q01"}, PQUIRK_NOSENSE},
91 1.21 bouyer {{T_CDROM, T_REMOV,
92 1.39 bouyer "GCD-R580B", "", "1.00"}, PQUIRK_LITTLETOC},
93 1.31 jdolecek {{T_CDROM, T_REMOV,
94 1.39 bouyer "HITACHI CDR-7730", "", "0008a"}, PQUIRK_NOSENSE},
95 1.24 bouyer {{T_CDROM, T_REMOV,
96 1.39 bouyer "MATSHITA CR-574", "", "1.02"}, PQUIRK_NOCAPACITY},
97 1.3 bouyer {{T_CDROM, T_REMOV,
98 1.39 bouyer "MATSHITA CR-574", "", "1.06"}, PQUIRK_NOCAPACITY},
99 1.7 bouyer {{T_CDROM, T_REMOV,
100 1.39 bouyer "Memorex CRW-2642", "", "1.0g"}, PQUIRK_NOSENSE},
101 1.7 bouyer {{T_CDROM, T_REMOV,
102 1.39 bouyer "NEC CD-ROM DRIVE:273", "", "4.21"}, PQUIRK_NOTUR},
103 1.2 bouyer {{T_CDROM, T_REMOV,
104 1.39 bouyer "SANYO CRD-256P", "", "1.02"}, PQUIRK_NOCAPACITY},
105 1.2 bouyer {{T_CDROM, T_REMOV,
106 1.39 bouyer "SANYO CRD-254P", "", "1.02"}, PQUIRK_NOCAPACITY},
107 1.3 bouyer {{T_CDROM, T_REMOV,
108 1.39 bouyer "SANYO CRD-S54P", "", "1.08"}, PQUIRK_NOCAPACITY},
109 1.5 bouyer {{T_CDROM, T_REMOV,
110 1.39 bouyer "CD-ROM CDR-S1", "", "1.70"}, PQUIRK_NOCAPACITY}, /* Sanyo */
111 1.18 bouyer {{T_CDROM, T_REMOV,
112 1.39 bouyer "CD-ROM CDR-N16", "", "1.25"}, PQUIRK_NOCAPACITY}, /* Sanyo */
113 1.20 bouyer {{T_CDROM, T_REMOV,
114 1.39 bouyer "UJDCD8730", "", "1.14"}, PQUIRK_NODOORLOCK}, /* Acer */
115 1.37 augustss {{T_DIRECT, T_REMOV, /* Panasonic MultiMediaCard */
116 1.39 bouyer "04DA", "1B00", "0010"}, PQUIRK_BYTE5_ZERO |
117 1.39 bouyer PQUIRK_NO_FLEX_PAGE },
118 1.38 augustss {{T_DIRECT, T_REMOV, /* ZiO! MultiMediaCard */
119 1.39 bouyer "eUSB", "MultiMediaCard", ""}, PQUIRK_NO_FLEX_PAGE },
120 1.2 bouyer };
121 1.2 bouyer
122 1.2 bouyer int
123 1.2 bouyer atapibusmatch(parent, cf, aux)
124 1.2 bouyer struct device *parent;
125 1.2 bouyer struct cfdata *cf;
126 1.2 bouyer void *aux;
127 1.2 bouyer {
128 1.39 bouyer struct ata_atapi_attach *aa = aux;
129 1.2 bouyer
130 1.39 bouyer if (aa == NULL)
131 1.4 enami return (0);
132 1.39 bouyer
133 1.39 bouyer if (aa->aa_type != T_ATAPI)
134 1.4 enami return (0);
135 1.39 bouyer
136 1.39 bouyer if (cf->cf_loc[ATAPICF_CHANNEL] != aa->aa_channel &&
137 1.13 bouyer cf->cf_loc[ATAPICF_CHANNEL] != ATAPICF_CHANNEL_DEFAULT)
138 1.39 bouyer return (0);
139 1.39 bouyer
140 1.4 enami return (1);
141 1.2 bouyer }
142 1.2 bouyer
143 1.2 bouyer int
144 1.2 bouyer atapibussubmatch(parent, cf, aux)
145 1.2 bouyer struct device *parent;
146 1.2 bouyer struct cfdata *cf;
147 1.2 bouyer void *aux;
148 1.2 bouyer {
149 1.2 bouyer struct scsipibus_attach_args *sa = aux;
150 1.39 bouyer struct scsipi_periph *periph = sa->sa_periph;
151 1.2 bouyer
152 1.2 bouyer if (cf->cf_loc[ATAPIBUSCF_DRIVE] != ATAPIBUSCF_DRIVE_DEFAULT &&
153 1.39 bouyer cf->cf_loc[ATAPIBUSCF_DRIVE] != periph->periph_target)
154 1.4 enami return (0);
155 1.2 bouyer return ((*cf->cf_attach->ca_match)(parent, cf, aux));
156 1.2 bouyer }
157 1.2 bouyer
158 1.2 bouyer void
159 1.2 bouyer atapibusattach(parent, self, aux)
160 1.4 enami struct device *parent, *self;
161 1.4 enami void *aux;
162 1.2 bouyer {
163 1.39 bouyer struct atapibus_softc *sc = (void *) self;
164 1.39 bouyer struct ata_atapi_attach *aa = aux;
165 1.39 bouyer struct scsipi_channel *chan = aa->aa_bus_private;
166 1.39 bouyer
167 1.39 bouyer sc->sc_channel = chan;
168 1.39 bouyer sc->sc_drvs = aa->aa_drv_data;
169 1.39 bouyer
170 1.39 bouyer /* ATAPI has no LUNs. */
171 1.39 bouyer chan->chan_nluns = 1;
172 1.39 bouyer printf(": %d targets\n", chan->chan_ntargets);
173 1.39 bouyer
174 1.39 bouyer /* Initialize the channel. */
175 1.39 bouyer scsipi_channel_init(chan);
176 1.39 bouyer
177 1.39 bouyer /* Probe the bus for devices. */
178 1.39 bouyer atapi_probe_bus(sc, -1);
179 1.27 enami }
180 1.27 enami
181 1.27 enami int
182 1.27 enami atapibusactivate(self, act)
183 1.27 enami struct device *self;
184 1.27 enami enum devact act;
185 1.27 enami {
186 1.39 bouyer struct atapibus_softc *sc = (void *) self;
187 1.39 bouyer struct scsipi_channel *chan = sc->sc_channel;
188 1.39 bouyer struct scsipi_periph *periph;
189 1.27 enami int target, error = 0, s;
190 1.27 enami
191 1.27 enami s = splbio();
192 1.27 enami switch (act) {
193 1.27 enami case DVACT_ACTIVATE:
194 1.27 enami error = EOPNOTSUPP;
195 1.27 enami break;
196 1.27 enami
197 1.27 enami case DVACT_DEACTIVATE:
198 1.39 bouyer for (target = 0; target < chan->chan_ntargets; target++) {
199 1.39 bouyer periph = scsipi_lookup_periph(chan, target, 0);
200 1.39 bouyer if (periph == NULL)
201 1.27 enami continue;
202 1.39 bouyer error = config_deactivate(periph->periph_dev);
203 1.39 bouyer if (error)
204 1.27 enami goto out;
205 1.27 enami }
206 1.27 enami break;
207 1.27 enami }
208 1.27 enami out:
209 1.27 enami splx(s);
210 1.27 enami return (error);
211 1.27 enami }
212 1.27 enami
213 1.27 enami int
214 1.27 enami atapibusdetach(self, flags)
215 1.27 enami struct device *self;
216 1.27 enami int flags;
217 1.27 enami {
218 1.39 bouyer struct atapibus_softc *sc = (void *)self;
219 1.39 bouyer struct scsipi_channel *chan = sc->sc_channel;
220 1.39 bouyer struct scsipi_periph *periph;
221 1.27 enami int target, error;
222 1.27 enami
223 1.39 bouyer /*
224 1.39 bouyer * Shut down the channel.
225 1.39 bouyer */
226 1.39 bouyer scsipi_channel_shutdown(chan);
227 1.39 bouyer
228 1.39 bouyer /*
229 1.39 bouyer * Now detach all of the periphs.
230 1.39 bouyer */
231 1.39 bouyer for (target = 0; target < chan->chan_ntargets; target++) {
232 1.39 bouyer periph = scsipi_lookup_periph(chan, target, 0);
233 1.39 bouyer if (periph == NULL)
234 1.27 enami continue;
235 1.39 bouyer error = config_detach(periph->periph_dev, flags);
236 1.39 bouyer if (error)
237 1.27 enami return (error);
238 1.27 enami
239 1.27 enami /*
240 1.27 enami * We have successfully detached the child. Drop the
241 1.27 enami * direct reference for the child so that wdcdetach
242 1.27 enami * won't call detach routine twice.
243 1.27 enami */
244 1.27 enami #ifdef DIAGNOSTIC
245 1.39 bouyer if (periph->periph_dev != sc->sc_drvs[target].drv_softc)
246 1.27 enami panic("softc mismatch");
247 1.27 enami #endif
248 1.27 enami sc->sc_drvs[target].drv_softc = NULL;
249 1.27 enami
250 1.39 bouyer scsipi_remove_periph(chan, periph);
251 1.39 bouyer free(periph, M_DEVBUF);
252 1.27 enami }
253 1.27 enami return (0);
254 1.2 bouyer }
255 1.2 bouyer
256 1.4 enami int
257 1.39 bouyer atapi_probe_bus(sc, target)
258 1.39 bouyer struct atapibus_softc *sc;
259 1.39 bouyer int target;
260 1.2 bouyer {
261 1.39 bouyer struct scsipi_channel *chan = sc->sc_channel;
262 1.2 bouyer int maxtarget, mintarget;
263 1.15 thorpej int error;
264 1.34 bouyer struct atapi_adapter *atapi_adapter;
265 1.4 enami
266 1.2 bouyer if (target == -1) {
267 1.2 bouyer maxtarget = 1;
268 1.2 bouyer mintarget = 0;
269 1.2 bouyer } else {
270 1.39 bouyer if (target < 0 || target >= chan->chan_ntargets)
271 1.4 enami return (ENXIO);
272 1.2 bouyer maxtarget = mintarget = target;
273 1.2 bouyer }
274 1.39 bouyer
275 1.39 bouyer if ((error = scsipi_adapter_addref(chan->chan_adapter)) != 0)
276 1.15 thorpej return (error);
277 1.39 bouyer atapi_adapter = (struct atapi_adapter*)chan->chan_adapter;
278 1.4 enami for (target = mintarget; target <= maxtarget; target++)
279 1.39 bouyer atapi_adapter->atapi_probe_device(sc, target);
280 1.39 bouyer scsipi_adapter_delref(chan->chan_adapter);
281 1.4 enami return (0);
282 1.2 bouyer }
283 1.2 bouyer
284 1.34 bouyer void *
285 1.39 bouyer atapi_probe_device(sc, target, periph, sa)
286 1.39 bouyer struct atapibus_softc *sc;
287 1.2 bouyer int target;
288 1.39 bouyer struct scsipi_periph *periph;
289 1.34 bouyer struct scsipibus_attach_args *sa;
290 1.2 bouyer {
291 1.39 bouyer struct scsipi_channel *chan = sc->sc_channel;
292 1.39 bouyer struct scsi_quirk_inquiry_pattern *finger;
293 1.2 bouyer struct cfdata *cf;
294 1.39 bouyer int priority, quirks;
295 1.2 bouyer
296 1.34 bouyer finger = (struct scsi_quirk_inquiry_pattern *)scsipi_inqmatch(
297 1.34 bouyer &sa->sa_inqbuf, (caddr_t)atapi_quirk_patterns,
298 1.34 bouyer sizeof(atapi_quirk_patterns) /
299 1.34 bouyer sizeof(atapi_quirk_patterns[0]),
300 1.34 bouyer sizeof(atapi_quirk_patterns[0]), &priority);
301 1.34 bouyer
302 1.39 bouyer if (finger != NULL)
303 1.39 bouyer quirks = finger->quirks;
304 1.39 bouyer else
305 1.39 bouyer quirks = 0;
306 1.39 bouyer
307 1.39 bouyer /*
308 1.39 bouyer * Now apply any quirks from the table.
309 1.39 bouyer */
310 1.39 bouyer periph->periph_quirks |= quirks;
311 1.39 bouyer
312 1.39 bouyer if ((cf = config_search(atapibussubmatch, &sc->sc_dev,
313 1.34 bouyer sa)) != 0) {
314 1.39 bouyer scsipi_insert_periph(chan, periph);
315 1.39 bouyer /*
316 1.39 bouyer * XXX Can't assign periph_dev here, because we'll
317 1.39 bouyer * XXX need it before config_attach() returns. Must
318 1.39 bouyer * XXX assign it in periph driver.
319 1.39 bouyer */
320 1.39 bouyer return config_attach(&sc->sc_dev, cf, sa,
321 1.39 bouyer atapibusprint);
322 1.34 bouyer } else {
323 1.41 enami atapibusprint(sa, sc->sc_dev.dv_xname);
324 1.34 bouyer printf(" not configured\n");
325 1.39 bouyer free(periph, M_DEVBUF);
326 1.39 bouyer return NULL;
327 1.2 bouyer }
328 1.2 bouyer }
329 1.2 bouyer
330 1.2 bouyer int
331 1.2 bouyer atapibusprint(aux, pnp)
332 1.2 bouyer void *aux;
333 1.2 bouyer const char *pnp;
334 1.2 bouyer {
335 1.2 bouyer struct scsipibus_attach_args *sa = aux;
336 1.2 bouyer struct scsipi_inquiry_pattern *inqbuf;
337 1.2 bouyer char *dtype;
338 1.2 bouyer
339 1.2 bouyer if (pnp != NULL)
340 1.2 bouyer printf("%s", pnp);
341 1.2 bouyer
342 1.2 bouyer inqbuf = &sa->sa_inqbuf;
343 1.2 bouyer
344 1.2 bouyer dtype = scsipi_dtype(inqbuf->type & SID_TYPE);
345 1.2 bouyer printf(" drive %d: <%s, %s, %s> type %d %s %s",
346 1.39 bouyer sa->sa_periph->periph_target ,inqbuf->vendor,
347 1.2 bouyer inqbuf->product, inqbuf->revision, inqbuf->type, dtype,
348 1.2 bouyer inqbuf->removable ? "removable" : "fixed");
349 1.2 bouyer return (UNCONF);
350 1.2 bouyer }
351