atapiconf.c revision 1.44 1 /* $NetBSD: atapiconf.c,v 1.44 2001/12/02 22:44:33 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/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: atapiconf.c,v 1.44 2001/12/02 22:44:33 bouyer 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 atapiprint(aux, pnp)
124 void *aux;
125 const char *pnp;
126 {
127 struct scsipi_channel *chan = aux;
128 if (pnp)
129 printf("atapibus at %s", pnp);
130 printf(" channel %d", chan->chan_channel);
131 return (UNCONF);
132 }
133
134 int
135 atapibusmatch(parent, cf, aux)
136 struct device *parent;
137 struct cfdata *cf;
138 void *aux;
139 {
140 struct scsipi_channel *chan = aux;
141
142 if (chan == NULL)
143 return (0);
144
145 if (chan->chan_bustype->bustype_type != SCSIPI_BUSTYPE_ATAPI)
146 return (0);
147
148 if (cf->cf_loc[ATAPICF_CHANNEL] != chan->chan_channel &&
149 cf->cf_loc[ATAPICF_CHANNEL] != ATAPICF_CHANNEL_DEFAULT)
150 return (0);
151
152 return (1);
153 }
154
155 int
156 atapibussubmatch(parent, cf, aux)
157 struct device *parent;
158 struct cfdata *cf;
159 void *aux;
160 {
161 struct scsipibus_attach_args *sa = aux;
162 struct scsipi_periph *periph = sa->sa_periph;
163
164 if (cf->cf_loc[ATAPIBUSCF_DRIVE] != ATAPIBUSCF_DRIVE_DEFAULT &&
165 cf->cf_loc[ATAPIBUSCF_DRIVE] != periph->periph_target)
166 return (0);
167 return ((*cf->cf_attach->ca_match)(parent, cf, aux));
168 }
169
170 void
171 atapibusattach(parent, self, aux)
172 struct device *parent, *self;
173 void *aux;
174 {
175 struct atapibus_softc *sc = (void *) self;
176 struct scsipi_channel *chan = aux;
177
178 sc->sc_channel = chan;
179
180 /* ATAPI has no LUNs. */
181 chan->chan_nluns = 1;
182 printf(": %d targets\n", chan->chan_ntargets);
183
184 /* Initialize the channel. */
185 scsipi_channel_init(chan);
186
187 /* Probe the bus for devices. */
188 atapi_probe_bus(sc, -1);
189 }
190
191 int
192 atapibusactivate(self, act)
193 struct device *self;
194 enum devact act;
195 {
196 struct atapibus_softc *sc = (void *) self;
197 struct scsipi_channel *chan = sc->sc_channel;
198 struct scsipi_periph *periph;
199 int target, error = 0, s;
200
201 s = splbio();
202 switch (act) {
203 case DVACT_ACTIVATE:
204 error = EOPNOTSUPP;
205 break;
206
207 case DVACT_DEACTIVATE:
208 for (target = 0; target < chan->chan_ntargets; target++) {
209 periph = scsipi_lookup_periph(chan, target, 0);
210 if (periph == NULL)
211 continue;
212 error = config_deactivate(periph->periph_dev);
213 if (error)
214 goto out;
215 }
216 break;
217 }
218 out:
219 splx(s);
220 return (error);
221 }
222
223 int
224 atapibusdetach(self, flags)
225 struct device *self;
226 int flags;
227 {
228 struct atapibus_softc *sc = (void *)self;
229 struct scsipi_channel *chan = sc->sc_channel;
230 struct scsipi_periph *periph;
231 int target, error;
232
233 /*
234 * Shut down the channel.
235 */
236 scsipi_channel_shutdown(chan);
237
238 /*
239 * Now detach all of the periphs.
240 */
241 for (target = 0; target < chan->chan_ntargets; target++) {
242 periph = scsipi_lookup_periph(chan, target, 0);
243 if (periph == NULL)
244 continue;
245 error = config_detach(periph->periph_dev, flags);
246 if (error)
247 return (error);
248
249 scsipi_remove_periph(chan, periph);
250 free(periph, M_DEVBUF);
251 }
252 return (0);
253 }
254
255 int
256 atapi_probe_bus(sc, target)
257 struct atapibus_softc *sc;
258 int target;
259 {
260 struct scsipi_channel *chan = sc->sc_channel;
261 int maxtarget, mintarget;
262 int error;
263 struct atapi_adapter *atapi_adapter;
264
265 if (target == -1) {
266 maxtarget = 1;
267 mintarget = 0;
268 } else {
269 if (target < 0 || target >= chan->chan_ntargets)
270 return (ENXIO);
271 maxtarget = mintarget = target;
272 }
273
274 if ((error = scsipi_adapter_addref(chan->chan_adapter)) != 0)
275 return (error);
276 atapi_adapter = (struct atapi_adapter*)chan->chan_adapter;
277 for (target = mintarget; target <= maxtarget; target++)
278 atapi_adapter->atapi_probe_device(sc, target);
279 scsipi_adapter_delref(chan->chan_adapter);
280 return (0);
281 }
282
283 void *
284 atapi_probe_device(sc, target, periph, sa)
285 struct atapibus_softc *sc;
286 int target;
287 struct scsipi_periph *periph;
288 struct scsipibus_attach_args *sa;
289 {
290 struct scsipi_channel *chan = sc->sc_channel;
291 struct scsi_quirk_inquiry_pattern *finger;
292 struct cfdata *cf;
293 int priority, quirks;
294
295 finger = (struct scsi_quirk_inquiry_pattern *)scsipi_inqmatch(
296 &sa->sa_inqbuf, (caddr_t)atapi_quirk_patterns,
297 sizeof(atapi_quirk_patterns) /
298 sizeof(atapi_quirk_patterns[0]),
299 sizeof(atapi_quirk_patterns[0]), &priority);
300
301 if (finger != NULL)
302 quirks = finger->quirks;
303 else
304 quirks = 0;
305
306 /*
307 * Now apply any quirks from the table.
308 */
309 periph->periph_quirks |= quirks;
310
311 if ((cf = config_search(atapibussubmatch, &sc->sc_dev,
312 sa)) != 0) {
313 scsipi_insert_periph(chan, periph);
314 /*
315 * XXX Can't assign periph_dev here, because we'll
316 * XXX need it before config_attach() returns. Must
317 * XXX assign it in periph driver.
318 */
319 return config_attach(&sc->sc_dev, cf, sa,
320 atapibusprint);
321 } else {
322 atapibusprint(sa, sc->sc_dev.dv_xname);
323 printf(" not configured\n");
324 free(periph, M_DEVBUF);
325 return NULL;
326 }
327 }
328
329 int
330 atapibusprint(aux, pnp)
331 void *aux;
332 const char *pnp;
333 {
334 struct scsipibus_attach_args *sa = aux;
335 struct scsipi_inquiry_pattern *inqbuf;
336 char *dtype;
337
338 if (pnp != NULL)
339 printf("%s", pnp);
340
341 inqbuf = &sa->sa_inqbuf;
342
343 dtype = scsipi_dtype(inqbuf->type & SID_TYPE);
344 printf(" drive %d: <%s, %s, %s> type %d %s %s",
345 sa->sa_periph->periph_target ,inqbuf->vendor,
346 inqbuf->product, inqbuf->revision, inqbuf->type, dtype,
347 inqbuf->removable ? "removable" : "fixed");
348 return (UNCONF);
349 }
350