atapiconf.c revision 1.60 1 /* $NetBSD: atapiconf.c,v 1.60 2003/09/17 19:14:57 mycroft 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.60 2003/09/17 19:14:57 mycroft 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 "CR-2801TE", "", "1.07"}, PQUIRK_NOSENSE},
80 {{T_CDROM, T_REMOV,
81 "CREATIVECD3630E", "", "AC101"}, PQUIRK_NOSENSE},
82 {{T_CDROM, T_REMOV,
83 "FX320S", "", "q01"}, PQUIRK_NOSENSE},
84 {{T_CDROM, T_REMOV,
85 "GCD-R580B", "", "1.00"}, PQUIRK_LITTLETOC},
86 {{T_CDROM, T_REMOV,
87 "HITACHI CDR-7730", "", "0008a"}, PQUIRK_NOSENSE},
88 {{T_CDROM, T_REMOV,
89 "MATSHITA CR-574", "", "1.02"}, PQUIRK_NOCAPACITY},
90 {{T_CDROM, T_REMOV,
91 "MATSHITA CR-574", "", "1.06"}, PQUIRK_NOCAPACITY},
92 {{T_CDROM, T_REMOV,
93 "Memorex CRW-2642", "", "1.0g"}, PQUIRK_NOSENSE},
94 {{T_CDROM, T_REMOV,
95 "NEC CD-ROM DRIVE:273", "", "4.21"}, PQUIRK_NOTUR},
96 {{T_CDROM, T_REMOV,
97 "SANYO CRD-256P", "", "1.02"}, PQUIRK_NOCAPACITY},
98 {{T_CDROM, T_REMOV,
99 "SANYO CRD-254P", "", "1.02"}, PQUIRK_NOCAPACITY},
100 {{T_CDROM, T_REMOV,
101 "SANYO CRD-S54P", "", "1.08"}, PQUIRK_NOCAPACITY},
102 {{T_CDROM, T_REMOV,
103 "CD-ROM CDR-S1", "", "1.70"}, PQUIRK_NOCAPACITY}, /* Sanyo */
104 {{T_CDROM, T_REMOV,
105 "CD-ROM CDR-N16", "", "1.25"}, PQUIRK_NOCAPACITY}, /* Sanyo */
106 {{T_DIRECT, T_REMOV, /* Panasonic MultiMediaCard */
107 "04DA", "1B00", "0010"}, PQUIRK_NO_FLEX_PAGE },
108 {{T_DIRECT, T_REMOV, /* ZiO! MultiMediaCard */
109 "eUSB", "MultiMediaCard", ""}, PQUIRK_NO_FLEX_PAGE },
110 {{T_DIRECT, T_REMOV,
111 "FUJIFILM", "USB-DRIVEUNIT", "1.00"}, PQUIRK_NO_FLEX_PAGE |
112 PQUIRK_NOSENSE },
113 {{T_OPTICAL, T_REMOV,
114 "FUJITSU MCJ3230AP", "", ""}, PQUIRK_NO_FLEX_PAGE },
115 };
116
117 int
118 atapiprint(aux, pnp)
119 void *aux;
120 const char *pnp;
121 {
122 struct scsipi_channel *chan = aux;
123 if (pnp)
124 aprint_normal("atapibus at %s", pnp);
125 aprint_normal(" channel %d", chan->chan_channel);
126 return (UNCONF);
127 }
128
129 int
130 atapibusmatch(parent, cf, aux)
131 struct device *parent;
132 struct cfdata *cf;
133 void *aux;
134 {
135 struct scsipi_channel *chan = aux;
136
137 if (chan == NULL)
138 return (0);
139
140 if (chan->chan_bustype->bustype_type != SCSIPI_BUSTYPE_ATAPI)
141 return (0);
142
143 if (cf->cf_loc[ATAPICF_CHANNEL] != chan->chan_channel &&
144 cf->cf_loc[ATAPICF_CHANNEL] != ATAPICF_CHANNEL_DEFAULT)
145 return (0);
146
147 return (1);
148 }
149
150 int
151 atapibussubmatch(parent, cf, aux)
152 struct device *parent;
153 struct cfdata *cf;
154 void *aux;
155 {
156 struct scsipibus_attach_args *sa = aux;
157 struct scsipi_periph *periph = sa->sa_periph;
158
159 if (cf->cf_loc[ATAPIBUSCF_DRIVE] != ATAPIBUSCF_DRIVE_DEFAULT &&
160 cf->cf_loc[ATAPIBUSCF_DRIVE] != periph->periph_target)
161 return (0);
162 return (config_match(parent, cf, aux));
163 }
164
165 void
166 atapibusattach(parent, self, aux)
167 struct device *parent, *self;
168 void *aux;
169 {
170 struct atapibus_softc *sc = (void *) self;
171 struct scsipi_channel *chan = aux;
172
173 sc->sc_channel = chan;
174
175 chan->chan_name = sc->sc_dev.dv_xname;
176
177 /* ATAPI has no LUNs. */
178 chan->chan_nluns = 1;
179 printf(": %d targets\n", chan->chan_ntargets);
180
181 /* Initialize the channel. */
182 chan->chan_init_cb = NULL;
183 chan->chan_init_cb_arg = NULL;
184 scsipi_channel_init(chan);
185
186 /* Probe the bus for devices. */
187 atapi_probe_bus(sc, -1);
188 }
189
190 int
191 atapibusactivate(self, act)
192 struct device *self;
193 enum devact act;
194 {
195 struct atapibus_softc *sc = (void *) self;
196 struct scsipi_channel *chan = sc->sc_channel;
197 struct scsipi_periph *periph;
198 int target, error = 0, s;
199
200 s = splbio();
201 switch (act) {
202 case DVACT_ACTIVATE:
203 error = EOPNOTSUPP;
204 break;
205
206 case DVACT_DEACTIVATE:
207 for (target = 0; target < chan->chan_ntargets; target++) {
208 periph = scsipi_lookup_periph(chan, target, 0);
209 if (periph == NULL)
210 continue;
211 error = config_deactivate(periph->periph_dev);
212 if (error)
213 goto out;
214 }
215 break;
216 }
217 out:
218 splx(s);
219 return (error);
220 }
221
222 int
223 atapibusdetach(self, flags)
224 struct device *self;
225 int flags;
226 {
227 struct atapibus_softc *sc = (void *)self;
228 struct scsipi_channel *chan = sc->sc_channel;
229 struct scsipi_periph *periph;
230 int target, error;
231
232 /*
233 * Shut down the channel.
234 */
235 scsipi_channel_shutdown(chan);
236
237 /*
238 * Now detach all of the periphs.
239 */
240 for (target = 0; target < chan->chan_ntargets; target++) {
241 periph = scsipi_lookup_periph(chan, target, 0);
242 if (periph == NULL)
243 continue;
244 error = config_detach(periph->periph_dev, flags);
245 if (error)
246 return (error);
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 aprint_normal("%s", pnp);
339
340 inqbuf = &sa->sa_inqbuf;
341
342 dtype = scsipi_dtype(inqbuf->type & SID_TYPE);
343 aprint_normal(" drive %d: <%s, %s, %s> %s %s",
344 sa->sa_periph->periph_target, inqbuf->vendor,
345 inqbuf->product, inqbuf->revision, dtype,
346 inqbuf->removable ? "removable" : "fixed");
347 return (UNCONF);
348 }
349