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