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