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