atapiconf.c revision 1.88 1 /* $NetBSD: atapiconf.c,v 1.88 2016/03/13 09:01:04 tsutsui 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.88 2016/03/13 09:01:04 tsutsui 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 (SCSIPI_BUSTYPE_TYPE(chan->chan_bustype->bustype_type) !=
121 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 void
168 atapibuschilddet(device_t self, device_t child)
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;
174
175 /* XXXSMP scsipi */
176 KERNEL_LOCK(1, curlwp);
177
178 for (target = 0; target < chan->chan_ntargets; target++) {
179 periph = scsipi_lookup_periph(chan, target, 0);
180 if (periph == NULL || periph->periph_dev != child)
181 continue;
182 scsipi_remove_periph(chan, periph);
183 free(periph, M_DEVBUF);
184 break;
185 }
186
187 /* XXXSMP scsipi */
188 KERNEL_UNLOCK_ONE(curlwp);
189 }
190
191 static int
192 atapibusdetach(device_t self, int flags)
193 {
194 struct atapibus_softc *sc = device_private(self);
195 struct scsipi_channel *chan = sc->sc_channel;
196 struct scsipi_periph *periph;
197 int target, error = 0;
198
199 /*
200 * Shut down the channel.
201 */
202 scsipi_channel_shutdown(chan);
203
204 /* XXXSMP scsipi */
205 KERNEL_LOCK(1, curlwp);
206
207 /*
208 * Now detach all of the periphs.
209 */
210 for (target = 0; target < chan->chan_ntargets; target++) {
211 periph = scsipi_lookup_periph(chan, target, 0);
212 if (periph == NULL)
213 continue;
214 error = config_detach(periph->periph_dev, flags);
215 if (error)
216 goto out;
217 KASSERT(scsipi_lookup_periph(chan, target, 0) == NULL);
218 }
219
220 out:
221 /* XXXSMP scsipi */
222 KERNEL_UNLOCK_ONE(curlwp);
223 return error;
224 }
225
226 static int
227 atapi_probe_bus(struct atapibus_softc *sc, int target)
228 {
229 struct scsipi_channel *chan = sc->sc_channel;
230 int maxtarget, mintarget;
231 int error;
232 struct atapi_adapter *atapi_adapter;
233
234 KASSERT(chan->chan_ntargets >= 1);
235
236 if (target == -1) {
237 maxtarget = chan->chan_ntargets - 1;
238 mintarget = 0;
239 } else {
240 if (target < 0 || target >= chan->chan_ntargets)
241 return (ENXIO);
242 maxtarget = mintarget = target;
243 }
244
245 if ((error = scsipi_adapter_addref(chan->chan_adapter)) != 0)
246 return (error);
247 atapi_adapter = (struct atapi_adapter*)chan->chan_adapter;
248 for (target = mintarget; target <= maxtarget; target++)
249 atapi_adapter->atapi_probe_device(sc, target);
250 scsipi_adapter_delref(chan->chan_adapter);
251 return (0);
252 }
253
254 void *
255 atapi_probe_device(struct atapibus_softc *sc, int target,
256 struct scsipi_periph *periph, struct scsipibus_attach_args *sa)
257 {
258 struct scsipi_channel *chan = sc->sc_channel;
259 const struct scsi_quirk_inquiry_pattern *finger;
260 cfdata_t cf;
261 int priority, quirks;
262
263 finger = scsipi_inqmatch(
264 &sa->sa_inqbuf, (const void *)atapi_quirk_patterns,
265 sizeof(atapi_quirk_patterns) /
266 sizeof(atapi_quirk_patterns[0]),
267 sizeof(atapi_quirk_patterns[0]), &priority);
268
269 if (finger != NULL)
270 quirks = finger->quirks;
271 else
272 quirks = 0;
273
274 /*
275 * Now apply any quirks from the table.
276 */
277 periph->periph_quirks |= quirks;
278
279 if ((cf = config_search_ia(atapibussubmatch, sc->sc_dev,
280 "atapibus", sa)) != 0) {
281 scsipi_insert_periph(chan, periph);
282 /*
283 * XXX Can't assign periph_dev here, because we'll
284 * XXX need it before config_attach() returns. Must
285 * XXX assign it in periph driver.
286 */
287 return config_attach(sc->sc_dev, cf, sa,
288 atapibusprint);
289 } else {
290 atapibusprint(sa, device_xname(sc->sc_dev));
291 aprint_normal(" not configured\n");
292 free(periph, M_DEVBUF);
293 return NULL;
294 }
295 }
296
297 static int
298 atapibusprint(void *aux, const char *pnp)
299 {
300 struct scsipibus_attach_args *sa = aux;
301 struct scsipi_inquiry_pattern *inqbuf;
302 const char *dtype;
303
304 if (pnp != NULL)
305 aprint_normal("%s", pnp);
306
307 inqbuf = &sa->sa_inqbuf;
308
309 dtype = scsipi_dtype(inqbuf->type & SID_TYPE);
310 aprint_normal(" drive %d: <%s, %s, %s> %s %s",
311 sa->sa_periph->periph_target, inqbuf->vendor,
312 inqbuf->product, inqbuf->revision, dtype,
313 inqbuf->removable ? "removable" : "fixed");
314 return (UNCONF);
315 }
316