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