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