atapiconf.c revision 1.55 1 1.55 thorpej /* $NetBSD: atapiconf.c,v 1.55 2003/01/01 00:10:25 thorpej Exp $ */
2 1.2 bouyer
3 1.2 bouyer /*
4 1.46 bouyer * Copyright (c) 1996, 2001 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.55 thorpej __KERNEL_RCSID(0, "$NetBSD: atapiconf.c,v 1.55 2003/01/01 00:10:25 thorpej 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.2 bouyer #include <dev/scsipi/scsipi_all.h>
44 1.39 bouyer #include <dev/scsipi/scsipiconf.h>
45 1.2 bouyer #include <dev/scsipi/atapiconf.h>
46 1.4 enami
47 1.2 bouyer #include "locators.h"
48 1.2 bouyer
49 1.2 bouyer #define SILENT_PRINTF(flags,string) if (!(flags & A_SILENT)) printf string
50 1.27 enami #define MAX_TARGET 1
51 1.2 bouyer
52 1.39 bouyer const struct scsipi_periphsw atapi_probe_periphsw = {
53 1.39 bouyer NULL,
54 1.39 bouyer NULL,
55 1.39 bouyer NULL,
56 1.39 bouyer NULL,
57 1.39 bouyer };
58 1.39 bouyer
59 1.2 bouyer int atapibusmatch __P((struct device *, struct cfdata *, void *));
60 1.2 bouyer void atapibusattach __P((struct device *, struct device *, void *));
61 1.27 enami int atapibusactivate __P((struct device *, enum devact));
62 1.27 enami int atapibusdetach __P((struct device *, int flags));
63 1.2 bouyer
64 1.39 bouyer int atapibussubmatch __P((struct device *, struct cfdata *, void *));
65 1.39 bouyer
66 1.39 bouyer int atapi_probe_bus __P((struct atapibus_softc *, int));
67 1.2 bouyer
68 1.52 thorpej CFATTACH_DECL(atapibus, sizeof(struct atapibus_softc),
69 1.53 thorpej atapibusmatch, atapibusattach, atapibusdetach, atapibusactivate);
70 1.2 bouyer
71 1.8 thorpej extern struct cfdriver atapibus_cd;
72 1.2 bouyer
73 1.2 bouyer int atapibusprint __P((void *, const char *));
74 1.2 bouyer
75 1.36 jdolecek const struct scsi_quirk_inquiry_pattern atapi_quirk_patterns[] = {
76 1.2 bouyer {{T_CDROM, T_REMOV,
77 1.39 bouyer "ALPS ELECTRIC CO.,LTD. DC544C", "", "SW03D"}, PQUIRK_NOTUR},
78 1.2 bouyer {{T_CDROM, T_REMOV,
79 1.39 bouyer "BCD-16X 1997-04-25", "", "VER 2.2"}, PQUIRK_NOSTARTUNIT},
80 1.26 bouyer {{T_CDROM, T_REMOV,
81 1.39 bouyer "BCD-24X 1997-06-27", "", "VER 2.0"}, PQUIRK_NOSTARTUNIT},
82 1.22 bouyer {{T_CDROM, T_REMOV,
83 1.39 bouyer "CR-2801TE", "", "1.07"}, PQUIRK_NOSENSE},
84 1.23 bouyer {{T_CDROM, T_REMOV,
85 1.39 bouyer "CREATIVECD3630E", "", "AC101"}, PQUIRK_NOSENSE},
86 1.2 bouyer {{T_CDROM, T_REMOV,
87 1.39 bouyer "FX320S", "", "q01"}, PQUIRK_NOSENSE},
88 1.21 bouyer {{T_CDROM, T_REMOV,
89 1.39 bouyer "GCD-R580B", "", "1.00"}, PQUIRK_LITTLETOC},
90 1.31 jdolecek {{T_CDROM, T_REMOV,
91 1.39 bouyer "HITACHI CDR-7730", "", "0008a"}, PQUIRK_NOSENSE},
92 1.24 bouyer {{T_CDROM, T_REMOV,
93 1.39 bouyer "MATSHITA CR-574", "", "1.02"}, PQUIRK_NOCAPACITY},
94 1.3 bouyer {{T_CDROM, T_REMOV,
95 1.39 bouyer "MATSHITA CR-574", "", "1.06"}, PQUIRK_NOCAPACITY},
96 1.7 bouyer {{T_CDROM, T_REMOV,
97 1.39 bouyer "Memorex CRW-2642", "", "1.0g"}, PQUIRK_NOSENSE},
98 1.7 bouyer {{T_CDROM, T_REMOV,
99 1.39 bouyer "NEC CD-ROM DRIVE:273", "", "4.21"}, PQUIRK_NOTUR},
100 1.2 bouyer {{T_CDROM, T_REMOV,
101 1.39 bouyer "SANYO CRD-256P", "", "1.02"}, PQUIRK_NOCAPACITY},
102 1.2 bouyer {{T_CDROM, T_REMOV,
103 1.39 bouyer "SANYO CRD-254P", "", "1.02"}, PQUIRK_NOCAPACITY},
104 1.3 bouyer {{T_CDROM, T_REMOV,
105 1.39 bouyer "SANYO CRD-S54P", "", "1.08"}, PQUIRK_NOCAPACITY},
106 1.5 bouyer {{T_CDROM, T_REMOV,
107 1.39 bouyer "CD-ROM CDR-S1", "", "1.70"}, PQUIRK_NOCAPACITY}, /* Sanyo */
108 1.18 bouyer {{T_CDROM, T_REMOV,
109 1.39 bouyer "CD-ROM CDR-N16", "", "1.25"}, PQUIRK_NOCAPACITY}, /* Sanyo */
110 1.20 bouyer {{T_CDROM, T_REMOV,
111 1.39 bouyer "UJDCD8730", "", "1.14"}, PQUIRK_NODOORLOCK}, /* Acer */
112 1.37 augustss {{T_DIRECT, T_REMOV, /* Panasonic MultiMediaCard */
113 1.39 bouyer "04DA", "1B00", "0010"}, PQUIRK_BYTE5_ZERO |
114 1.39 bouyer PQUIRK_NO_FLEX_PAGE },
115 1.38 augustss {{T_DIRECT, T_REMOV, /* ZiO! MultiMediaCard */
116 1.39 bouyer "eUSB", "MultiMediaCard", ""}, PQUIRK_NO_FLEX_PAGE },
117 1.47 gehenna {{T_DIRECT, T_REMOV,
118 1.47 gehenna "FUJIFILM", "USB-DRIVEUNIT", "1.00"}, PQUIRK_NO_FLEX_PAGE |
119 1.47 gehenna PQUIRK_NOSENSE },
120 1.2 bouyer };
121 1.2 bouyer
122 1.2 bouyer int
123 1.44 bouyer atapiprint(aux, pnp)
124 1.44 bouyer void *aux;
125 1.44 bouyer const char *pnp;
126 1.44 bouyer {
127 1.44 bouyer struct scsipi_channel *chan = aux;
128 1.44 bouyer if (pnp)
129 1.55 thorpej aprint_normal("atapibus at %s", pnp);
130 1.55 thorpej aprint_normal(" channel %d", chan->chan_channel);
131 1.44 bouyer return (UNCONF);
132 1.44 bouyer }
133 1.44 bouyer
134 1.44 bouyer int
135 1.2 bouyer atapibusmatch(parent, cf, aux)
136 1.2 bouyer struct device *parent;
137 1.2 bouyer struct cfdata *cf;
138 1.2 bouyer void *aux;
139 1.2 bouyer {
140 1.44 bouyer struct scsipi_channel *chan = aux;
141 1.2 bouyer
142 1.44 bouyer if (chan == NULL)
143 1.4 enami return (0);
144 1.39 bouyer
145 1.44 bouyer if (chan->chan_bustype->bustype_type != SCSIPI_BUSTYPE_ATAPI)
146 1.4 enami return (0);
147 1.39 bouyer
148 1.44 bouyer if (cf->cf_loc[ATAPICF_CHANNEL] != chan->chan_channel &&
149 1.13 bouyer cf->cf_loc[ATAPICF_CHANNEL] != ATAPICF_CHANNEL_DEFAULT)
150 1.39 bouyer return (0);
151 1.39 bouyer
152 1.4 enami return (1);
153 1.2 bouyer }
154 1.2 bouyer
155 1.2 bouyer int
156 1.2 bouyer atapibussubmatch(parent, cf, aux)
157 1.2 bouyer struct device *parent;
158 1.2 bouyer struct cfdata *cf;
159 1.2 bouyer void *aux;
160 1.2 bouyer {
161 1.2 bouyer struct scsipibus_attach_args *sa = aux;
162 1.39 bouyer struct scsipi_periph *periph = sa->sa_periph;
163 1.2 bouyer
164 1.2 bouyer if (cf->cf_loc[ATAPIBUSCF_DRIVE] != ATAPIBUSCF_DRIVE_DEFAULT &&
165 1.39 bouyer cf->cf_loc[ATAPIBUSCF_DRIVE] != periph->periph_target)
166 1.4 enami return (0);
167 1.50 thorpej return (config_match(parent, cf, aux));
168 1.2 bouyer }
169 1.2 bouyer
170 1.2 bouyer void
171 1.2 bouyer atapibusattach(parent, self, aux)
172 1.4 enami struct device *parent, *self;
173 1.4 enami void *aux;
174 1.2 bouyer {
175 1.39 bouyer struct atapibus_softc *sc = (void *) self;
176 1.44 bouyer struct scsipi_channel *chan = aux;
177 1.39 bouyer
178 1.39 bouyer sc->sc_channel = chan;
179 1.48 bouyer
180 1.48 bouyer chan->chan_name = sc->sc_dev.dv_xname;
181 1.39 bouyer
182 1.39 bouyer /* ATAPI has no LUNs. */
183 1.39 bouyer chan->chan_nluns = 1;
184 1.39 bouyer printf(": %d targets\n", chan->chan_ntargets);
185 1.39 bouyer
186 1.39 bouyer /* Initialize the channel. */
187 1.49 jmc chan->chan_init_cb = NULL;
188 1.49 jmc chan->chan_init_cb_arg = NULL;
189 1.39 bouyer scsipi_channel_init(chan);
190 1.39 bouyer
191 1.39 bouyer /* Probe the bus for devices. */
192 1.39 bouyer atapi_probe_bus(sc, -1);
193 1.27 enami }
194 1.27 enami
195 1.27 enami int
196 1.27 enami atapibusactivate(self, act)
197 1.27 enami struct device *self;
198 1.27 enami enum devact act;
199 1.27 enami {
200 1.39 bouyer struct atapibus_softc *sc = (void *) self;
201 1.39 bouyer struct scsipi_channel *chan = sc->sc_channel;
202 1.39 bouyer struct scsipi_periph *periph;
203 1.27 enami int target, error = 0, s;
204 1.27 enami
205 1.27 enami s = splbio();
206 1.27 enami switch (act) {
207 1.27 enami case DVACT_ACTIVATE:
208 1.27 enami error = EOPNOTSUPP;
209 1.27 enami break;
210 1.27 enami
211 1.27 enami case DVACT_DEACTIVATE:
212 1.39 bouyer for (target = 0; target < chan->chan_ntargets; target++) {
213 1.39 bouyer periph = scsipi_lookup_periph(chan, target, 0);
214 1.39 bouyer if (periph == NULL)
215 1.27 enami continue;
216 1.39 bouyer error = config_deactivate(periph->periph_dev);
217 1.39 bouyer if (error)
218 1.27 enami goto out;
219 1.27 enami }
220 1.27 enami break;
221 1.27 enami }
222 1.27 enami out:
223 1.27 enami splx(s);
224 1.27 enami return (error);
225 1.27 enami }
226 1.27 enami
227 1.27 enami int
228 1.27 enami atapibusdetach(self, flags)
229 1.27 enami struct device *self;
230 1.27 enami int flags;
231 1.27 enami {
232 1.39 bouyer struct atapibus_softc *sc = (void *)self;
233 1.39 bouyer struct scsipi_channel *chan = sc->sc_channel;
234 1.39 bouyer struct scsipi_periph *periph;
235 1.27 enami int target, error;
236 1.27 enami
237 1.39 bouyer /*
238 1.39 bouyer * Shut down the channel.
239 1.39 bouyer */
240 1.39 bouyer scsipi_channel_shutdown(chan);
241 1.39 bouyer
242 1.39 bouyer /*
243 1.39 bouyer * Now detach all of the periphs.
244 1.39 bouyer */
245 1.39 bouyer for (target = 0; target < chan->chan_ntargets; target++) {
246 1.39 bouyer periph = scsipi_lookup_periph(chan, target, 0);
247 1.39 bouyer if (periph == NULL)
248 1.27 enami continue;
249 1.39 bouyer error = config_detach(periph->periph_dev, flags);
250 1.39 bouyer if (error)
251 1.27 enami return (error);
252 1.27 enami
253 1.39 bouyer scsipi_remove_periph(chan, periph);
254 1.39 bouyer free(periph, M_DEVBUF);
255 1.27 enami }
256 1.27 enami return (0);
257 1.2 bouyer }
258 1.2 bouyer
259 1.4 enami int
260 1.39 bouyer atapi_probe_bus(sc, target)
261 1.39 bouyer struct atapibus_softc *sc;
262 1.39 bouyer int target;
263 1.2 bouyer {
264 1.39 bouyer struct scsipi_channel *chan = sc->sc_channel;
265 1.2 bouyer int maxtarget, mintarget;
266 1.15 thorpej int error;
267 1.34 bouyer struct atapi_adapter *atapi_adapter;
268 1.4 enami
269 1.2 bouyer if (target == -1) {
270 1.2 bouyer maxtarget = 1;
271 1.2 bouyer mintarget = 0;
272 1.2 bouyer } else {
273 1.39 bouyer if (target < 0 || target >= chan->chan_ntargets)
274 1.4 enami return (ENXIO);
275 1.2 bouyer maxtarget = mintarget = target;
276 1.2 bouyer }
277 1.39 bouyer
278 1.39 bouyer if ((error = scsipi_adapter_addref(chan->chan_adapter)) != 0)
279 1.15 thorpej return (error);
280 1.39 bouyer atapi_adapter = (struct atapi_adapter*)chan->chan_adapter;
281 1.4 enami for (target = mintarget; target <= maxtarget; target++)
282 1.39 bouyer atapi_adapter->atapi_probe_device(sc, target);
283 1.39 bouyer scsipi_adapter_delref(chan->chan_adapter);
284 1.4 enami return (0);
285 1.2 bouyer }
286 1.2 bouyer
287 1.34 bouyer void *
288 1.39 bouyer atapi_probe_device(sc, target, periph, sa)
289 1.39 bouyer struct atapibus_softc *sc;
290 1.2 bouyer int target;
291 1.39 bouyer struct scsipi_periph *periph;
292 1.34 bouyer struct scsipibus_attach_args *sa;
293 1.2 bouyer {
294 1.39 bouyer struct scsipi_channel *chan = sc->sc_channel;
295 1.39 bouyer struct scsi_quirk_inquiry_pattern *finger;
296 1.2 bouyer struct cfdata *cf;
297 1.39 bouyer int priority, quirks;
298 1.2 bouyer
299 1.34 bouyer finger = (struct scsi_quirk_inquiry_pattern *)scsipi_inqmatch(
300 1.34 bouyer &sa->sa_inqbuf, (caddr_t)atapi_quirk_patterns,
301 1.34 bouyer sizeof(atapi_quirk_patterns) /
302 1.34 bouyer sizeof(atapi_quirk_patterns[0]),
303 1.34 bouyer sizeof(atapi_quirk_patterns[0]), &priority);
304 1.34 bouyer
305 1.39 bouyer if (finger != NULL)
306 1.39 bouyer quirks = finger->quirks;
307 1.39 bouyer else
308 1.39 bouyer quirks = 0;
309 1.39 bouyer
310 1.39 bouyer /*
311 1.39 bouyer * Now apply any quirks from the table.
312 1.39 bouyer */
313 1.39 bouyer periph->periph_quirks |= quirks;
314 1.39 bouyer
315 1.39 bouyer if ((cf = config_search(atapibussubmatch, &sc->sc_dev,
316 1.34 bouyer sa)) != 0) {
317 1.39 bouyer scsipi_insert_periph(chan, periph);
318 1.39 bouyer /*
319 1.39 bouyer * XXX Can't assign periph_dev here, because we'll
320 1.39 bouyer * XXX need it before config_attach() returns. Must
321 1.39 bouyer * XXX assign it in periph driver.
322 1.39 bouyer */
323 1.39 bouyer return config_attach(&sc->sc_dev, cf, sa,
324 1.39 bouyer atapibusprint);
325 1.34 bouyer } else {
326 1.41 enami atapibusprint(sa, sc->sc_dev.dv_xname);
327 1.34 bouyer printf(" not configured\n");
328 1.39 bouyer free(periph, M_DEVBUF);
329 1.39 bouyer return NULL;
330 1.2 bouyer }
331 1.2 bouyer }
332 1.2 bouyer
333 1.2 bouyer int
334 1.2 bouyer atapibusprint(aux, pnp)
335 1.2 bouyer void *aux;
336 1.2 bouyer const char *pnp;
337 1.2 bouyer {
338 1.2 bouyer struct scsipibus_attach_args *sa = aux;
339 1.2 bouyer struct scsipi_inquiry_pattern *inqbuf;
340 1.2 bouyer char *dtype;
341 1.2 bouyer
342 1.2 bouyer if (pnp != NULL)
343 1.55 thorpej aprint_normal("%s", pnp);
344 1.2 bouyer
345 1.2 bouyer inqbuf = &sa->sa_inqbuf;
346 1.2 bouyer
347 1.2 bouyer dtype = scsipi_dtype(inqbuf->type & SID_TYPE);
348 1.55 thorpej aprint_normal(" drive %d: <%s, %s, %s> %s %s",
349 1.54 soren sa->sa_periph->periph_target, inqbuf->vendor,
350 1.54 soren inqbuf->product, inqbuf->revision, dtype,
351 1.2 bouyer inqbuf->removable ? "removable" : "fixed");
352 1.2 bouyer return (UNCONF);
353 1.2 bouyer }
354