atapiconf.c revision 1.67.2.6 1 /* $NetBSD: atapiconf.c,v 1.67.2.6 2008/03/17 09:15:27 yamt 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.67.2.6 2008/03/17 09:15:27 yamt 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 KASSERT(scsipi_lookup_periph(chan, target, 0) == NULL);
245 }
246 return (0);
247 }
248
249 static int
250 atapi_probe_bus(struct atapibus_softc *sc, 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(struct atapibus_softc *sc, int target,
277 struct scsipi_periph *periph, struct scsipibus_attach_args *sa)
278 {
279 struct scsipi_channel *chan = sc->sc_channel;
280 const struct scsi_quirk_inquiry_pattern *finger;
281 struct cfdata *cf;
282 int priority, quirks;
283
284 finger = scsipi_inqmatch(
285 &sa->sa_inqbuf, (const void *)atapi_quirk_patterns,
286 sizeof(atapi_quirk_patterns) /
287 sizeof(atapi_quirk_patterns[0]),
288 sizeof(atapi_quirk_patterns[0]), &priority);
289
290 if (finger != NULL)
291 quirks = finger->quirks;
292 else
293 quirks = 0;
294
295 /*
296 * Now apply any quirks from the table.
297 */
298 periph->periph_quirks |= quirks;
299
300 if ((cf = config_search_ia(atapibussubmatch, &sc->sc_dev,
301 "atapibus", sa)) != 0) {
302 scsipi_insert_periph(chan, periph);
303 /*
304 * XXX Can't assign periph_dev here, because we'll
305 * XXX need it before config_attach() returns. Must
306 * XXX assign it in periph driver.
307 */
308 return config_attach(&sc->sc_dev, cf, sa,
309 atapibusprint);
310 } else {
311 atapibusprint(sa, sc->sc_dev.dv_xname);
312 printf(" not configured\n");
313 free(periph, M_DEVBUF);
314 return NULL;
315 }
316 }
317
318 static int
319 atapibusprint(void *aux, const char *pnp)
320 {
321 struct scsipibus_attach_args *sa = aux;
322 struct scsipi_inquiry_pattern *inqbuf;
323 const char *dtype;
324
325 if (pnp != NULL)
326 aprint_normal("%s", pnp);
327
328 inqbuf = &sa->sa_inqbuf;
329
330 dtype = scsipi_dtype(inqbuf->type & SID_TYPE);
331 aprint_normal(" drive %d: <%s, %s, %s> %s %s",
332 sa->sa_periph->periph_target, inqbuf->vendor,
333 inqbuf->product, inqbuf->revision, dtype,
334 inqbuf->removable ? "removable" : "fixed");
335 return (UNCONF);
336 }
337