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