atapiconf.c revision 1.80 1 /* $NetBSD: atapiconf.c,v 1.80 2009/10/19 18:41:16 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 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 #include <sys/cdefs.h>
28 __KERNEL_RCSID(0, "$NetBSD: atapiconf.c,v 1.80 2009/10/19 18:41:16 bouyer Exp $");
29
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/malloc.h>
33 #include <sys/device.h>
34 #include <sys/buf.h>
35 #include <sys/proc.h>
36 #include <sys/kthread.h>
37
38 #include <dev/scsipi/scsipi_all.h>
39 #include <dev/scsipi/scsipiconf.h>
40 #include <dev/scsipi/atapiconf.h>
41
42 #include "locators.h"
43
44 #define SILENT_PRINTF(flags,string) if (!(flags & A_SILENT)) printf string
45 #define MAX_TARGET 1
46
47 const struct scsipi_periphsw atapi_probe_periphsw = {
48 NULL,
49 NULL,
50 NULL,
51 NULL,
52 };
53
54 static int atapibusmatch(device_t, cfdata_t, void *);
55 static void atapibusattach(device_t, device_t, void *);
56 static int atapibusactivate(device_t, enum devact);
57 static int atapibusdetach(device_t, int flags);
58 static void atapibuschilddet(device_t, device_t);
59
60 static int atapibussubmatch(device_t, cfdata_t, const int *, void *);
61
62 static int atapi_probe_bus(struct atapibus_softc *, int);
63
64 static int atapibusprint(void *, const char *);
65
66 CFATTACH_DECL3_NEW(atapibus, sizeof(struct atapibus_softc),
67 atapibusmatch, atapibusattach, atapibusdetach, atapibusactivate, NULL,
68 atapibuschilddet, DVF_DETACH_SHUTDOWN);
69
70 extern struct cfdriver atapibus_cd;
71
72 static const struct scsi_quirk_inquiry_pattern atapi_quirk_patterns[] = {
73 {{T_CDROM, T_REMOV,
74 "ALPS ELECTRIC CO.,LTD. DC544C", "", "SW03D"}, PQUIRK_NOTUR},
75 {{T_CDROM, T_REMOV,
76 "CR-2801TE", "", "1.07"}, PQUIRK_NOSENSE},
77 {{T_CDROM, T_REMOV,
78 "CREATIVECD3630E", "", "AC101"}, PQUIRK_NOSENSE},
79 {{T_CDROM, T_REMOV,
80 "FX320S", "", "q01"}, PQUIRK_NOSENSE},
81 {{T_CDROM, T_REMOV,
82 "GCD-R580B", "", "1.00"}, PQUIRK_LITTLETOC},
83 {{T_CDROM, T_REMOV,
84 "HITACHI CDR-7730", "", "0008a"}, PQUIRK_NOSENSE},
85 {{T_CDROM, T_REMOV,
86 "MATSHITA CR-574", "", "1.02"}, PQUIRK_NOCAPACITY},
87 {{T_CDROM, T_REMOV,
88 "MATSHITA CR-574", "", "1.06"}, PQUIRK_NOCAPACITY},
89 {{T_CDROM, T_REMOV,
90 "Memorex CRW-2642", "", "1.0g"}, PQUIRK_NOSENSE},
91 {{T_CDROM, T_REMOV,
92 "NEC CD-ROM DRIVE:273", "", "4.21"}, PQUIRK_NOTUR},
93 {{T_CDROM, T_REMOV,
94 "SANYO CRD-256P", "", "1.02"}, PQUIRK_NOCAPACITY},
95 {{T_CDROM, T_REMOV,
96 "SANYO CRD-254P", "", "1.02"}, PQUIRK_NOCAPACITY},
97 {{T_CDROM, T_REMOV,
98 "SANYO CRD-S54P", "", "1.08"}, PQUIRK_NOCAPACITY},
99 {{T_CDROM, T_REMOV,
100 "CD-ROM CDR-S1", "", "1.70"}, PQUIRK_NOCAPACITY}, /* Sanyo */
101 {{T_CDROM, T_REMOV,
102 "CD-ROM CDR-N16", "", "1.25"}, PQUIRK_NOCAPACITY}, /* Sanyo */
103 };
104
105 int
106 atapiprint(void *aux, const char *pnp)
107 {
108 if (pnp)
109 aprint_normal("atapibus at %s", pnp);
110 return (UNCONF);
111 }
112
113 static int
114 atapibusmatch(device_t parent, cfdata_t cf, void *aux)
115 {
116 struct scsipi_channel *chan = aux;
117
118 if (chan == NULL)
119 return (0);
120
121 if (chan->chan_bustype->bustype_type != SCSIPI_BUSTYPE_ATAPI)
122 return (0);
123
124 return (1);
125 }
126
127 static int
128 atapibussubmatch(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
129 {
130 struct scsipibus_attach_args *sa = aux;
131 struct scsipi_periph *periph = sa->sa_periph;
132
133 if (cf->cf_loc[ATAPIBUSCF_DRIVE] != ATAPIBUSCF_DRIVE_DEFAULT &&
134 cf->cf_loc[ATAPIBUSCF_DRIVE] != periph->periph_target)
135 return (0);
136 return (config_match(parent, cf, aux));
137 }
138
139 static void
140 atapibusattach(device_t parent, device_t self, void *aux)
141 {
142 struct atapibus_softc *sc = device_private(self);
143 struct scsipi_channel *chan = aux;
144
145 sc->sc_channel = chan;
146 sc->sc_dev = self;
147
148 chan->chan_name = device_xname(sc->sc_dev);
149
150 /* ATAPI has no LUNs. */
151 chan->chan_nluns = 1;
152 aprint_naive("\n");
153 aprint_normal(": %d targets\n", chan->chan_ntargets);
154
155 /* Initialize the channel. */
156 chan->chan_init_cb = NULL;
157 chan->chan_init_cb_arg = NULL;
158 scsipi_channel_init(chan);
159
160 if (!pmf_device_register(self, NULL, NULL))
161 aprint_error_dev(self, "couldn't establish power handler\n");
162
163 /* Probe the bus for devices. */
164 atapi_probe_bus(sc, -1);
165 }
166
167 static int
168 atapibusactivate(device_t self, enum devact act)
169 {
170 struct atapibus_softc *sc = device_private(self);
171 struct scsipi_channel *chan = sc->sc_channel;
172 struct scsipi_periph *periph;
173 int target, error = 0, s;
174
175 s = splbio();
176 switch (act) {
177 case DVACT_ACTIVATE:
178 error = EOPNOTSUPP;
179 break;
180
181 case DVACT_DEACTIVATE:
182 for (target = 0; target < chan->chan_ntargets; target++) {
183 periph = scsipi_lookup_periph(chan, target, 0);
184 if (periph == NULL)
185 continue;
186 error = config_deactivate(periph->periph_dev);
187 if (error)
188 goto out;
189 }
190 break;
191 }
192 out:
193 splx(s);
194 return (error);
195 }
196
197 static void
198 atapibuschilddet(device_t self, device_t child)
199 {
200 struct atapibus_softc *sc = device_private(self);
201 struct scsipi_channel *chan = sc->sc_channel;
202 struct scsipi_periph *periph;
203 int target;
204
205 for (target = 0; target < chan->chan_ntargets; target++) {
206 periph = scsipi_lookup_periph(chan, target, 0);
207 if (periph == NULL || periph->periph_dev != child)
208 continue;
209 scsipi_remove_periph(chan, periph);
210 free(periph, M_DEVBUF);
211 break;
212 }
213 }
214
215 static int
216 atapibusdetach(device_t self, int flags)
217 {
218 struct atapibus_softc *sc = device_private(self);
219 struct scsipi_channel *chan = sc->sc_channel;
220 struct scsipi_periph *periph;
221 int target, error;
222
223 /*
224 * Shut down the channel.
225 */
226 scsipi_channel_shutdown(chan);
227
228 /*
229 * Now detach all of the periphs.
230 */
231 for (target = 0; target < chan->chan_ntargets; target++) {
232 periph = scsipi_lookup_periph(chan, target, 0);
233 if (periph == NULL)
234 continue;
235 error = config_detach(periph->periph_dev, flags);
236 if (error)
237 return (error);
238 KASSERT(scsipi_lookup_periph(chan, target, 0) == NULL);
239 }
240 return (0);
241 }
242
243 static int
244 atapi_probe_bus(struct atapibus_softc *sc, 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(struct atapibus_softc *sc, int target,
271 struct scsipi_periph *periph, struct scsipibus_attach_args *sa)
272 {
273 struct scsipi_channel *chan = sc->sc_channel;
274 const struct scsi_quirk_inquiry_pattern *finger;
275 cfdata_t cf;
276 int priority, quirks;
277
278 finger = scsipi_inqmatch(
279 &sa->sa_inqbuf, (const void *)atapi_quirk_patterns,
280 sizeof(atapi_quirk_patterns) /
281 sizeof(atapi_quirk_patterns[0]),
282 sizeof(atapi_quirk_patterns[0]), &priority);
283
284 if (finger != NULL)
285 quirks = finger->quirks;
286 else
287 quirks = 0;
288
289 /*
290 * Now apply any quirks from the table.
291 */
292 periph->periph_quirks |= quirks;
293
294 if ((cf = config_search_ia(atapibussubmatch, sc->sc_dev,
295 "atapibus", sa)) != 0) {
296 scsipi_insert_periph(chan, periph);
297 /*
298 * XXX Can't assign periph_dev here, because we'll
299 * XXX need it before config_attach() returns. Must
300 * XXX assign it in periph driver.
301 */
302 return config_attach(sc->sc_dev, cf, sa,
303 atapibusprint);
304 } else {
305 atapibusprint(sa, device_xname(sc->sc_dev));
306 printf(" not configured\n");
307 free(periph, M_DEVBUF);
308 return NULL;
309 }
310 }
311
312 static int
313 atapibusprint(void *aux, const char *pnp)
314 {
315 struct scsipibus_attach_args *sa = aux;
316 struct scsipi_inquiry_pattern *inqbuf;
317 const char *dtype;
318
319 if (pnp != NULL)
320 aprint_normal("%s", pnp);
321
322 inqbuf = &sa->sa_inqbuf;
323
324 dtype = scsipi_dtype(inqbuf->type & SID_TYPE);
325 aprint_normal(" drive %d: <%s, %s, %s> %s %s",
326 sa->sa_periph->periph_target, inqbuf->vendor,
327 inqbuf->product, inqbuf->revision, dtype,
328 inqbuf->removable ? "removable" : "fixed");
329 return (UNCONF);
330 }
331