xref: /netbsd-src/sys/dev/scsipi/atapiconf.c (revision c41a4eebefede43f6950f838a387dc18c6a431bf)
1 /*	$NetBSD: atapiconf.c,v 1.7 1998/01/06 17:03:15 bouyer Exp $	*/
2 
3 /*
4  * Copyright (c) 1996 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/types.h>
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/malloc.h>
36 #include <sys/device.h>
37 #include <sys/buf.h>
38 #include <sys/proc.h>
39 
40 #include <dev/scsipi/scsipi_all.h>
41 #include <dev/scsipi/scsi_all.h>
42 #include <dev/scsipi/scsipiconf.h>
43 #include <dev/scsipi/atapiconf.h>
44 
45 #include "locators.h"
46 
47 #define SILENT_PRINTF(flags,string) if (!(flags & A_SILENT)) printf string
48 
49 struct atapibus_softc {
50 	struct device sc_dev;
51 	struct scsipi_link *adapter_link;	/* proto supplied by adapter */
52 	struct scsipi_link **sc_link;		/* dynamically allocated */
53 };
54 
55 #ifdef __BROKEN_INDIRECT_CONFIG
56 int	atapibusmatch __P((struct device *, void *, void *));
57 int	atapibussubmatch __P((struct device *, void *, void *));
58 #else
59 int	atapibusmatch __P((struct device *, struct cfdata *, void *));
60 int	atapibussubmatch __P((struct device *, struct cfdata *, void *));
61 #endif
62 void	atapibusattach __P((struct device *, struct device *, void *));
63 int	atapiprint __P((void *, const char *));
64 
65 int	atapi_probe_bus __P((int, int));
66 void	atapi_probedev __P((struct atapibus_softc *, int ));
67 
68 struct cfattach atapibus_ca = {
69 	sizeof(struct atapibus_softc), atapibusmatch, atapibusattach
70 };
71 
72 struct cfdriver atapibus_cd = {
73 	NULL, "atapibus", DV_DULL
74 };
75 
76 int atapibusprint __P((void *, const char *));
77 
78 struct scsi_quirk_inquiry_pattern atapi_quirk_patterns[] = {
79 	{{T_CDROM, T_REMOV,
80 	 "GCD-R580B", "", "1.00"},                ADEV_LITTLETOC}, /* Goldstar */
81 	{{T_CDROM, T_REMOV,
82 	 "SANYO CRD-256P", "", "1.02"},           ADEV_NOCAPACITY},
83 	{{T_CDROM, T_REMOV,
84 	 "SANYO CRD-254P", "", "1.02"},           ADEV_NOCAPACITY},
85 	{{T_CDROM, T_REMOV,
86 	 "SANYO CRD-S54P", "", "1.08"},           ADEV_NOCAPACITY},
87 	{{T_CDROM, T_REMOV,
88 	 "CD-ROM  CDR-S1", "", "1.70"},           ADEV_NOCAPACITY}, /* Sanyo */
89 	{{T_CDROM, T_REMOV,
90 	 "CD-ROM  CDR-N16", "", "1.25"},          ADEV_NOCAPACITY}, /* Sanyo */
91 	{{T_CDROM, T_REMOV,
92 	 "UJDCD8730", "", "1.14"},                ADEV_NODOORLOCK}, /* Acer */
93 	{{T_CDROM, T_REMOV,
94 	 "ALPS ELECTRIC CO.,LTD. DC544C", "", "SW03D"}, ADEV_NOTUR},
95 	{{T_CDROM, T_REMOV,
96 	 "NEC                 CD-ROM DRIVE:273", "", "4.21"}, ADEV_NOTUR},
97 	{{T_CDROM, T_REMOV,
98 	 "MATSHITA CR-574", "", "1.06"},          ADEV_NOCAPACITY},
99 	{{T_CDROM, T_REMOV,
100 	 "MATSHITA CR-574", "", "1.02"},          ADEV_NOCAPACITY},
101 };
102 
103 int
104 #ifdef __BROKEN_INDIRECT_CONFIG
105 atapibusmatch(parent, match, aux)
106 	struct device *parent;
107 	void *match, *aux;
108 {
109 #else
110 atapibusmatch(parent, cf, aux)
111 	struct device *parent;
112 	struct cfdata *cf;
113 	void *aux;
114 {
115 #endif
116 	struct scsipi_link *sc_link = aux;
117 
118 	if (sc_link == NULL)
119 		return (0);
120 	if (sc_link->type != BUS_ATAPI)
121 		return (0);
122 	return (1);
123 }
124 
125 int
126 #ifdef __BROKEN_INDIRECT_CONFIG
127 atapibussubmatch(parent, match, aux)
128 	struct device *parent;
129 	void *match, *aux;
130 {
131 	struct cfdata *cf = match;
132 #else
133 atapibussubmatch(parent, cf, aux)
134 	struct device *parent;
135 	struct cfdata *cf;
136 	void *aux;
137 {
138 #endif
139 	struct scsipibus_attach_args *sa = aux;
140 	struct scsipi_link *sc_link = sa->sa_sc_link;
141 
142 	if (cf->cf_loc[ATAPIBUSCF_DRIVE] != ATAPIBUSCF_DRIVE_DEFAULT &&
143 	    cf->cf_loc[ATAPIBUSCF_DRIVE] != sc_link->scsipi_atapi.drive)
144 		return (0);
145 	return ((*cf->cf_attach->ca_match)(parent, cf, aux));
146 }
147 
148 #if 0
149 void
150 atapi_fixquirk(sc_link)
151 	struct scsipi_link *ad_link;
152 {
153 	struct atapi_identify *id = &ad_link->id;
154 	struct atapi_quirk_inquiry_pattern *quirk;
155 
156 	/*
157 	 * Clean up the model name, serial and revision numbers.
158 	 */
159 	btrim(id->model, sizeof(id->model));
160 	btrim(id->serial_number, sizeof(id->serial_number));
161 	btrim(id->firmware_revision, sizeof(id->firmware_revision));
162 }
163 #endif
164 
165 void
166 atapibusattach(parent, self, aux)
167 	struct device *parent, *self;
168 	void *aux;
169 {
170 	struct atapibus_softc *ab = (struct atapibus_softc *)self;
171 	struct scsipi_link *sc_link_proto = aux;
172 	int nbytes;
173 
174 	printf("\n");
175 
176 	sc_link_proto->scsipi_atapi.atapibus = ab->sc_dev.dv_unit;
177 	sc_link_proto->scsipi_cmd = atapi_scsipi_cmd;
178 	sc_link_proto->scsipi_interpret_sense = atapi_interpret_sense;
179 	sc_link_proto->sc_print_addr = atapi_print_addr;
180 
181 	ab->adapter_link = sc_link_proto;
182 
183 	nbytes = 2 * sizeof(struct scsipi_link **);
184 	ab->sc_link = (struct scsipi_link **)malloc(nbytes, M_DEVBUF,
185 	    M_NOWAIT);
186 	if (ab->sc_link == NULL)
187 		panic("scsibusattach: can't allocate target links");
188 	bzero(ab->sc_link, nbytes);
189 	atapi_probe_bus(ab->sc_dev.dv_unit, -1);
190 }
191 
192 int
193 atapi_probe_bus(bus, target)
194 	int bus, target;
195 {
196 	int maxtarget, mintarget;
197 	struct atapibus_softc *atapi;
198 
199 	if (bus < 0 || bus >= atapibus_cd.cd_ndevs)
200 		return (ENXIO);
201 	atapi = atapibus_cd.cd_devs[bus];
202 	if (atapi == NULL)
203 		return (ENXIO);
204 
205 	if (target == -1) {
206 		maxtarget = 1;
207 		mintarget = 0;
208 	} else {
209 		if (target < 0 || target > 1)
210 			return (ENXIO);
211 		maxtarget = mintarget = target;
212 	}
213 	for (target = mintarget; target <= maxtarget; target++)
214 		atapi_probedev(atapi, target);
215 	return (0);
216 }
217 
218 void
219 atapi_probedev(atapi, target)
220 	struct atapibus_softc *atapi;
221 	int target;
222 {
223 	struct scsipi_link *sc_link;
224 	struct scsipibus_attach_args sa;
225 	struct atapi_identify ids;
226 	struct atapi_identify *id = &ids;
227 	struct cfdata *cf;
228 	struct scsi_quirk_inquiry_pattern *finger;
229 	int priority;
230 	char serial_number[20], model[40], firmware_revision[8];
231 
232 	/* skip if already attached */
233 	if (atapi->sc_link[target])
234 		return;
235 
236 	if (wdc_atapi_get_params(atapi->adapter_link, target, id)) {
237 #ifdef ATAPI_DEBUG_PROBE
238 		printf("%s drive %d: cmdsz 0x%x drqtype 0x%x\n",
239 		    atapi->sc_dev.dv_xname, target,
240 		    id->config.cmd_drq_rem & ATAPI_PACKET_SIZE_MASK,
241 		    id->config.cmd_drq_rem & ATAPI_DRQ_MASK);
242 #endif
243 		/*
244 		 * Shuffle string byte order.
245 		 * Mitsumi and NEC drives don't need this.
246 		 */
247 		if (((id->model[0] == 'N' && id->model[1] == 'E') ||
248 		    (id->model[0] == 'F' && id->model[1] == 'X')) == 0)
249 			bswap(id->model, sizeof(id->model));
250 		bswap(id->serial_number, sizeof(id->serial_number));
251 		bswap(id->firmware_revision, sizeof(id->firmware_revision));
252 
253 		/*
254 		 * Allocate a device link and try and attach
255 		 * a driver to this device.  If we fail, free
256 		 * the link.
257 		 */
258 		sc_link = malloc(sizeof(*sc_link), M_DEVBUF, M_NOWAIT);
259 		if (sc_link == NULL) {
260 			printf("%s: can't allocate link for drive %d\n",
261 			    atapi->sc_dev.dv_xname, target);
262 			return;
263 		}
264 		/* Fill in link. */
265 		*sc_link = *atapi->adapter_link;
266 		sc_link->scsipi_atapi.drive = target;
267 		sc_link->device = NULL;
268 #if defined(SCSIDEBUG) && DEBUGTYPE == BUS_ATAPI
269 		if (DEBUGTARGET == -1 || target == DEBUGTARGET)
270 			sc_link->flags |= DEBUGLEVEL;
271 #endif /* SCSIDEBUG */
272 		if (id->config.cmd_drq_rem & ATAPI_PACKET_SIZE_16)
273 			sc_link->scsipi_atapi.cap |= ACAP_LEN;
274 		sc_link->scsipi_atapi.cap |=
275 		    (id->config.cmd_drq_rem & ATAPI_DRQ_MASK) << 3;
276 #if 0
277 		bcopy(id, &ad_link->id, sizeof(*id));
278 		/* Fix strings and look through the quirk table. */
279 		atapi_fixquirk(ad_link, id);
280 #endif
281 		sa.sa_sc_link = sc_link;
282 		sa.sa_inqbuf.type =  id->config.device_type & SID_TYPE;
283 		sa.sa_inqbuf.removable =
284 		    id->config.cmd_drq_rem & ATAPI_REMOVABLE ?
285 		    T_REMOV : T_FIXED;
286 		if (sa.sa_inqbuf.removable)
287 			sc_link->flags |= SDEV_REMOVABLE;
288 		scsipi_strvis(model, id->model, 40);
289 		scsipi_strvis(serial_number, id->serial_number, 20);
290 		scsipi_strvis(firmware_revision, id->firmware_revision, 8);
291 		sa.sa_inqbuf.vendor = model;
292 		sa.sa_inqbuf.product = serial_number;
293 		sa.sa_inqbuf.revision = firmware_revision;
294 
295 		finger = (struct scsi_quirk_inquiry_pattern *)scsipi_inqmatch(
296 		    &sa.sa_inqbuf, (caddr_t)atapi_quirk_patterns,
297 		    sizeof(atapi_quirk_patterns) /
298 		        sizeof(atapi_quirk_patterns[0]),
299 		    sizeof(atapi_quirk_patterns[0]), &priority);
300 		if (priority != 0)
301 			sc_link->quirks |= finger->quirks;
302 
303 		if ((cf = config_search(atapibussubmatch, &atapi->sc_dev,
304 		    &sa)) != 0) {
305 			atapi->sc_link[target] = sc_link;
306 			config_attach(&atapi->sc_dev, cf, &sa, atapibusprint);
307 			return;
308 		} else {
309 			atapibusprint(&sa, atapi->sc_dev.dv_xname);
310 			printf(" not configured\n");
311 			free(sc_link, M_DEVBUF);
312 			return;
313 		}
314 
315 #if 0 /* WAS: */
316 		/* Try to find a match. */
317 		if (config_found(self, ad_link, atapiprint) == NULL)
318 			free(ad_link, M_DEVBUF);
319 #endif
320 	}
321 }
322 
323 int
324 atapibusprint(aux, pnp)
325 	void *aux;
326 	const char *pnp;
327 {
328 	struct scsipibus_attach_args *sa = aux;
329 	struct scsipi_inquiry_pattern *inqbuf;
330 	char *dtype;
331 
332 	if (pnp != NULL)
333 		printf("%s", pnp);
334 
335 	inqbuf = &sa->sa_inqbuf;
336 
337 	dtype = scsipi_dtype(inqbuf->type & SID_TYPE);
338 	printf(" drive %d: <%s, %s, %s> type %d %s %s",
339 	    sa->sa_sc_link->scsipi_atapi.drive,inqbuf->vendor,
340 	    inqbuf->product, inqbuf->revision, inqbuf->type, dtype,
341 	    inqbuf->removable ? "removable" : "fixed");
342 	return (UNCONF);
343 }
344