atapiconf.c revision 1.62 1 /* $NetBSD: atapiconf.c,v 1.62 2003/10/08 10:58:13 bouyer 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.62 2003/10/08 10:58:13 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/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,
107 "FUJIFILM", "USB-DRIVEUNIT", "1.00"}, PQUIRK_NOSENSE },
108 };
109
110 int
111 atapiprint(aux, pnp)
112 void *aux;
113 const char *pnp;
114 {
115 if (pnp)
116 aprint_normal("atapibus at %s", pnp);
117 return (UNCONF);
118 }
119
120 int
121 atapibusmatch(parent, cf, aux)
122 struct device *parent;
123 struct cfdata *cf;
124 void *aux;
125 {
126 struct scsipi_channel *chan = aux;
127
128 if (chan == NULL)
129 return (0);
130
131 if (chan->chan_bustype->bustype_type != SCSIPI_BUSTYPE_ATAPI)
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 (config_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 scsipi_channel *chan = aux;
159
160 sc->sc_channel = chan;
161
162 chan->chan_name = sc->sc_dev.dv_xname;
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 chan->chan_init_cb = NULL;
170 chan->chan_init_cb_arg = NULL;
171 scsipi_channel_init(chan);
172
173 /* Probe the bus for devices. */
174 atapi_probe_bus(sc, -1);
175 }
176
177 int
178 atapibusactivate(self, act)
179 struct device *self;
180 enum devact act;
181 {
182 struct atapibus_softc *sc = (void *) self;
183 struct scsipi_channel *chan = sc->sc_channel;
184 struct scsipi_periph *periph;
185 int target, error = 0, s;
186
187 s = splbio();
188 switch (act) {
189 case DVACT_ACTIVATE:
190 error = EOPNOTSUPP;
191 break;
192
193 case DVACT_DEACTIVATE:
194 for (target = 0; target < chan->chan_ntargets; target++) {
195 periph = scsipi_lookup_periph(chan, target, 0);
196 if (periph == NULL)
197 continue;
198 error = config_deactivate(periph->periph_dev);
199 if (error)
200 goto out;
201 }
202 break;
203 }
204 out:
205 splx(s);
206 return (error);
207 }
208
209 int
210 atapibusdetach(self, flags)
211 struct device *self;
212 int flags;
213 {
214 struct atapibus_softc *sc = (void *)self;
215 struct scsipi_channel *chan = sc->sc_channel;
216 struct scsipi_periph *periph;
217 int target, error;
218
219 /*
220 * Shut down the channel.
221 */
222 scsipi_channel_shutdown(chan);
223
224 /*
225 * Now detach all of the periphs.
226 */
227 for (target = 0; target < chan->chan_ntargets; target++) {
228 periph = scsipi_lookup_periph(chan, target, 0);
229 if (periph == NULL)
230 continue;
231 error = config_detach(periph->periph_dev, flags);
232 if (error)
233 return (error);
234
235 scsipi_remove_periph(chan, periph);
236 free(periph, M_DEVBUF);
237 }
238 return (0);
239 }
240
241 int
242 atapi_probe_bus(sc, target)
243 struct atapibus_softc *sc;
244 int target;
245 {
246 struct scsipi_channel *chan = sc->sc_channel;
247 int maxtarget, mintarget;
248 int error;
249 struct atapi_adapter *atapi_adapter;
250
251 if (target == -1) {
252 maxtarget = 1;
253 mintarget = 0;
254 } else {
255 if (target < 0 || target >= chan->chan_ntargets)
256 return (ENXIO);
257 maxtarget = mintarget = target;
258 }
259
260 if ((error = scsipi_adapter_addref(chan->chan_adapter)) != 0)
261 return (error);
262 atapi_adapter = (struct atapi_adapter*)chan->chan_adapter;
263 for (target = mintarget; target <= maxtarget; target++)
264 atapi_adapter->atapi_probe_device(sc, target);
265 scsipi_adapter_delref(chan->chan_adapter);
266 return (0);
267 }
268
269 void *
270 atapi_probe_device(sc, target, periph, sa)
271 struct atapibus_softc *sc;
272 int target;
273 struct scsipi_periph *periph;
274 struct scsipibus_attach_args *sa;
275 {
276 struct scsipi_channel *chan = sc->sc_channel;
277 struct scsi_quirk_inquiry_pattern *finger;
278 struct cfdata *cf;
279 int priority, quirks;
280
281 finger = (struct scsi_quirk_inquiry_pattern *)scsipi_inqmatch(
282 &sa->sa_inqbuf, (caddr_t)atapi_quirk_patterns,
283 sizeof(atapi_quirk_patterns) /
284 sizeof(atapi_quirk_patterns[0]),
285 sizeof(atapi_quirk_patterns[0]), &priority);
286
287 if (finger != NULL)
288 quirks = finger->quirks;
289 else
290 quirks = 0;
291
292 /*
293 * Now apply any quirks from the table.
294 */
295 periph->periph_quirks |= quirks;
296
297 if ((cf = config_search(atapibussubmatch, &sc->sc_dev,
298 sa)) != 0) {
299 scsipi_insert_periph(chan, periph);
300 /*
301 * XXX Can't assign periph_dev here, because we'll
302 * XXX need it before config_attach() returns. Must
303 * XXX assign it in periph driver.
304 */
305 return config_attach(&sc->sc_dev, cf, sa,
306 atapibusprint);
307 } else {
308 atapibusprint(sa, sc->sc_dev.dv_xname);
309 printf(" not configured\n");
310 free(periph, M_DEVBUF);
311 return NULL;
312 }
313 }
314
315 int
316 atapibusprint(aux, pnp)
317 void *aux;
318 const char *pnp;
319 {
320 struct scsipibus_attach_args *sa = aux;
321 struct scsipi_inquiry_pattern *inqbuf;
322 char *dtype;
323
324 if (pnp != NULL)
325 aprint_normal("%s", pnp);
326
327 inqbuf = &sa->sa_inqbuf;
328
329 dtype = scsipi_dtype(inqbuf->type & SID_TYPE);
330 aprint_normal(" drive %d: <%s, %s, %s> %s %s",
331 sa->sa_periph->periph_target, inqbuf->vendor,
332 inqbuf->product, inqbuf->revision, dtype,
333 inqbuf->removable ? "removable" : "fixed");
334 return (UNCONF);
335 }
336