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