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