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