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