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