atapiconf.c revision 1.1.2.2 1 1.1.2.2 thorpej /* $NetBSD: atapiconf.c,v 1.1.2.2 1997/07/01 22:37:08 thorpej Exp $ */
2 1.1.2.1 bouyer
3 1.1.2.1 bouyer /*
4 1.1.2.1 bouyer * Copyright (c) 1996 Manuel Bouyer. All rights reserved.
5 1.1.2.1 bouyer *
6 1.1.2.1 bouyer * Redistribution and use in source and binary forms, with or without
7 1.1.2.1 bouyer * modification, are permitted provided that the following conditions
8 1.1.2.1 bouyer * are met:
9 1.1.2.1 bouyer * 1. Redistributions of source code must retain the above copyright
10 1.1.2.1 bouyer * notice, this list of conditions and the following disclaimer.
11 1.1.2.1 bouyer * 2. Redistributions in binary form must reproduce the above copyright
12 1.1.2.1 bouyer * notice, this list of conditions and the following disclaimer in the
13 1.1.2.1 bouyer * documentation and/or other materials provided with the distribution.
14 1.1.2.1 bouyer * 3. All advertising materials mentioning features or use of this software
15 1.1.2.1 bouyer * must display the following acknowledgement:
16 1.1.2.2 thorpej * This product includes software developed by Manuel Bouyer.
17 1.1.2.1 bouyer * 4. The name of the author may not be used to endorse or promote products
18 1.1.2.1 bouyer * derived from this software without specific prior written permission.
19 1.1.2.1 bouyer *
20 1.1.2.1 bouyer * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 1.1.2.1 bouyer * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 1.1.2.1 bouyer * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 1.1.2.1 bouyer * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 1.1.2.1 bouyer * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 1.1.2.1 bouyer * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 1.1.2.1 bouyer * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 1.1.2.1 bouyer * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 1.1.2.1 bouyer * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 1.1.2.1 bouyer * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 1.1.2.1 bouyer */
31 1.1.2.1 bouyer
32 1.1.2.1 bouyer #include <sys/types.h>
33 1.1.2.1 bouyer #include <sys/param.h>
34 1.1.2.1 bouyer #include <sys/systm.h>
35 1.1.2.1 bouyer #include <sys/malloc.h>
36 1.1.2.1 bouyer #include <sys/device.h>
37 1.1.2.1 bouyer #include <sys/buf.h>
38 1.1.2.1 bouyer #include <sys/proc.h>
39 1.1.2.1 bouyer
40 1.1.2.1 bouyer #include <dev/scsipi/scsipi_all.h>
41 1.1.2.1 bouyer #include <dev/scsipi/scsi_all.h>
42 1.1.2.1 bouyer #include <dev/scsipi/scsipiconf.h>
43 1.1.2.1 bouyer #include <dev/scsipi/atapiconf.h>
44 1.1.2.1 bouyer
45 1.1.2.1 bouyer #define SILENT_PRINTF(flags,string) if (!(flags & A_SILENT)) printf string
46 1.1.2.1 bouyer
47 1.1.2.1 bouyer struct atapibus_softc {
48 1.1.2.1 bouyer struct device sc_dev;
49 1.1.2.1 bouyer struct scsipi_link *adapter_link; /* proto supplied by adapter */
50 1.1.2.1 bouyer struct scsipi_link **sc_link; /* dynamically allocated */
51 1.1.2.1 bouyer };
52 1.1.2.1 bouyer
53 1.1.2.1 bouyer #ifdef __BROKEN_INDIRECT_CONFIG
54 1.1.2.2 thorpej int atapibusmatch __P((struct device *, void *, void *));
55 1.1.2.2 thorpej int atapibussubmatch __P((struct device *, void *, void *));
56 1.1.2.1 bouyer #else
57 1.1.2.2 thorpej int atapibusmatch __P((struct device *, struct cfdata *, void *));
58 1.1.2.2 thorpej int atapibussubmatch __P((struct device *, struct cfdata *, void *));
59 1.1.2.1 bouyer #endif
60 1.1.2.2 thorpej void atapibusattach __P((struct device *, struct device *, void *));
61 1.1.2.2 thorpej int atapiprint __P((void *, const char *));
62 1.1.2.2 thorpej
63 1.1.2.2 thorpej int atapi_probe_bus __P((int, int));
64 1.1.2.2 thorpej void atapi_probedev __P((struct atapibus_softc *, int ));
65 1.1.2.2 thorpej
66 1.1.2.1 bouyer struct cfattach atapibus_ca = {
67 1.1.2.1 bouyer sizeof(struct atapibus_softc), atapibusmatch, atapibusattach
68 1.1.2.1 bouyer };
69 1.1.2.1 bouyer
70 1.1.2.1 bouyer struct cfdriver atapibus_cd = {
71 1.1.2.1 bouyer NULL, "atapibus", DV_DULL
72 1.1.2.1 bouyer };
73 1.1.2.1 bouyer
74 1.1.2.1 bouyer int atapibusprint __P((void *, const char *));
75 1.1.2.1 bouyer
76 1.1.2.1 bouyer struct scsi_quirk_inquiry_pattern atapi_quirk_patterns[] = {
77 1.1.2.1 bouyer {{T_CDROM, T_REMOV,
78 1.1.2.1 bouyer "GCD-R580B", "", "1.00"}, ADEV_LITTLETOC},
79 1.1.2.1 bouyer {{T_CDROM, T_REMOV,
80 1.1.2.1 bouyer "SANYO CRD-256P", "", "1.02"}, ADEV_NOCAPACITY},
81 1.1.2.1 bouyer {{T_CDROM, T_REMOV,
82 1.1.2.1 bouyer "UJDCD8730", "", "1.14"}, ADEV_NODOORLOCK},
83 1.1.2.1 bouyer {{T_CDROM, T_REMOV,
84 1.1.2.1 bouyer "ALPS ELECTRIC CO.,LTD. DC544C", "", "SW03D"}, ADEV_NOTUR},
85 1.1.2.1 bouyer {{T_CDROM, T_REMOV,
86 1.1.2.1 bouyer "NEC CD-ROM DRIVE:273", "", "4.21"}, ADEV_NOTUR},
87 1.1.2.1 bouyer };
88 1.1.2.1 bouyer
89 1.1.2.1 bouyer int
90 1.1.2.1 bouyer #ifdef __BROKEN_INDIRECT_CONFIG
91 1.1.2.1 bouyer atapibusmatch(parent, match, aux)
92 1.1.2.1 bouyer struct device *parent;
93 1.1.2.1 bouyer void *match, *aux;
94 1.1.2.1 bouyer {
95 1.1.2.1 bouyer #else
96 1.1.2.1 bouyer atapibusmatch(parent, cf, aux)
97 1.1.2.1 bouyer struct device *parent;
98 1.1.2.1 bouyer struct cfdata *cf;
99 1.1.2.1 bouyer void *aux;
100 1.1.2.1 bouyer {
101 1.1.2.1 bouyer #endif
102 1.1.2.1 bouyer struct scsipi_link *sc_link = aux;
103 1.1.2.1 bouyer
104 1.1.2.1 bouyer if (sc_link == NULL)
105 1.1.2.1 bouyer return 0;
106 1.1.2.1 bouyer if (sc_link->type != BUS_ATAPI)
107 1.1.2.1 bouyer return 0;
108 1.1.2.1 bouyer return 1;
109 1.1.2.1 bouyer }
110 1.1.2.1 bouyer
111 1.1.2.1 bouyer int
112 1.1.2.1 bouyer #ifdef __BROKEN_INDIRECT_CONFIG
113 1.1.2.1 bouyer atapibussubmatch(parent, match, aux)
114 1.1.2.1 bouyer struct device *parent;
115 1.1.2.1 bouyer void *match, *aux;
116 1.1.2.1 bouyer {
117 1.1.2.1 bouyer struct cfdata *cf = match;
118 1.1.2.1 bouyer #else
119 1.1.2.1 bouyer atapibussubmatch(parent, cf, aux)
120 1.1.2.1 bouyer struct device *parent;
121 1.1.2.1 bouyer struct cfdata *cf;
122 1.1.2.1 bouyer void *aux;
123 1.1.2.1 bouyer {
124 1.1.2.1 bouyer #endif
125 1.1.2.1 bouyer struct scsipibus_attach_args *sa = aux;
126 1.1.2.1 bouyer struct scsipi_link *sc_link = sa->sa_sc_link;
127 1.1.2.1 bouyer
128 1.1.2.1 bouyer if (cf->cf_loc[0] != -1 && cf->cf_loc[0] != sc_link->scsipi_atapi.drive)
129 1.1.2.1 bouyer return 0;
130 1.1.2.1 bouyer return ((*cf->cf_attach->ca_match)(parent, cf, aux));
131 1.1.2.1 bouyer }
132 1.1.2.1 bouyer
133 1.1.2.1 bouyer
134 1.1.2.1 bouyer #if 0
135 1.1.2.1 bouyer void
136 1.1.2.1 bouyer atapi_fixquirk(sc_link)
137 1.1.2.1 bouyer struct scsipi_link *ad_link;
138 1.1.2.1 bouyer {
139 1.1.2.1 bouyer struct atapi_identify *id = &ad_link->id;
140 1.1.2.1 bouyer struct atapi_quirk_inquiry_pattern *quirk;
141 1.1.2.1 bouyer
142 1.1.2.1 bouyer
143 1.1.2.1 bouyer /*
144 1.1.2.1 bouyer * Clean up the model name, serial and
145 1.1.2.1 bouyer * revision numbers.
146 1.1.2.1 bouyer */
147 1.1.2.1 bouyer btrim(id->model, sizeof(id->model));
148 1.1.2.1 bouyer btrim(id->serial_number, sizeof(id->serial_number));
149 1.1.2.1 bouyer btrim(id->firmware_revision, sizeof(id->firmware_revision));
150 1.1.2.1 bouyer
151 1.1.2.1 bouyer }
152 1.1.2.1 bouyer #endif
153 1.1.2.1 bouyer
154 1.1.2.1 bouyer
155 1.1.2.1 bouyer void
156 1.1.2.1 bouyer atapibusattach(parent, self, aux)
157 1.1.2.1 bouyer struct device *parent, *self;
158 1.1.2.1 bouyer void *aux;
159 1.1.2.1 bouyer {
160 1.1.2.1 bouyer struct atapibus_softc *ab = (struct atapibus_softc *)self;
161 1.1.2.1 bouyer struct scsipi_link *sc_link_proto = aux;
162 1.1.2.1 bouyer int nbytes;
163 1.1.2.1 bouyer
164 1.1.2.1 bouyer printf("\n");
165 1.1.2.1 bouyer
166 1.1.2.1 bouyer sc_link_proto->scsipi_atapi.atapibus = ab->sc_dev.dv_unit;
167 1.1.2.1 bouyer sc_link_proto->scsipi_cmd = atapi_scsipi_cmd;
168 1.1.2.1 bouyer sc_link_proto->scsipi_interpret_sense = atapi_interpret_sense;
169 1.1.2.1 bouyer sc_link_proto->sc_print_addr = atapi_print_addr;
170 1.1.2.1 bouyer
171 1.1.2.1 bouyer ab->adapter_link = sc_link_proto;
172 1.1.2.1 bouyer
173 1.1.2.1 bouyer nbytes = 2 * sizeof(struct scsipi_link **);
174 1.1.2.1 bouyer ab->sc_link = (struct scsipi_link **)malloc(nbytes, M_DEVBUF,
175 1.1.2.2 thorpej M_NOWAIT);
176 1.1.2.1 bouyer if (ab->sc_link == NULL)
177 1.1.2.1 bouyer panic("scsibusattach: can't allocate target links");
178 1.1.2.1 bouyer bzero(ab->sc_link, nbytes);
179 1.1.2.1 bouyer atapi_probe_bus(ab->sc_dev.dv_unit, -1);
180 1.1.2.1 bouyer }
181 1.1.2.1 bouyer
182 1.1.2.1 bouyer int
183 1.1.2.1 bouyer atapi_probe_bus(bus, target)
184 1.1.2.1 bouyer int bus, target;
185 1.1.2.1 bouyer {
186 1.1.2.1 bouyer int maxtarget, mintarget;
187 1.1.2.1 bouyer struct atapibus_softc *atapi;
188 1.1.2.1 bouyer if (bus < 0 || bus >= atapibus_cd.cd_ndevs)
189 1.1.2.1 bouyer return ENXIO;
190 1.1.2.1 bouyer atapi = atapibus_cd.cd_devs[bus];
191 1.1.2.1 bouyer if (!atapi)
192 1.1.2.1 bouyer return ENXIO;
193 1.1.2.1 bouyer
194 1.1.2.1 bouyer if (target == -1) {
195 1.1.2.1 bouyer maxtarget = 1;
196 1.1.2.1 bouyer mintarget = 0;
197 1.1.2.1 bouyer } else {
198 1.1.2.1 bouyer if (target < 0 || target > 1)
199 1.1.2.1 bouyer return ENXIO;
200 1.1.2.1 bouyer maxtarget = mintarget = target;
201 1.1.2.1 bouyer }
202 1.1.2.1 bouyer for (target = mintarget; target <= maxtarget; target++) {
203 1.1.2.1 bouyer atapi_probedev(atapi, target);
204 1.1.2.1 bouyer }
205 1.1.2.1 bouyer return 0;
206 1.1.2.1 bouyer }
207 1.1.2.1 bouyer
208 1.1.2.1 bouyer void
209 1.1.2.1 bouyer atapi_probedev(atapi, target)
210 1.1.2.1 bouyer struct atapibus_softc *atapi;
211 1.1.2.1 bouyer int target;
212 1.1.2.1 bouyer {
213 1.1.2.1 bouyer struct scsipi_link *sc_link;
214 1.1.2.1 bouyer struct scsipibus_attach_args sa;
215 1.1.2.1 bouyer struct atapi_identify ids;
216 1.1.2.1 bouyer struct atapi_identify *id = &ids;
217 1.1.2.1 bouyer struct cfdata *cf;
218 1.1.2.1 bouyer struct scsi_quirk_inquiry_pattern *finger;
219 1.1.2.1 bouyer int priority;
220 1.1.2.1 bouyer char serial_number[20], model[40], firmware_revision[8];
221 1.1.2.2 thorpej
222 1.1.2.1 bouyer /* skip if already attached */
223 1.1.2.1 bouyer if (atapi->sc_link[target])
224 1.1.2.1 bouyer return;
225 1.1.2.1 bouyer
226 1.1.2.1 bouyer if (wdc_atapi_get_params(atapi->adapter_link, target, id)) {
227 1.1.2.1 bouyer #ifdef ATAPI_DEBUG_PROBE
228 1.1.2.1 bouyer printf("%s drive %d: cmdsz 0x%x drqtype 0x%x\n",
229 1.1.2.1 bouyer atapi->sc_dev.dv_xname, target,
230 1.1.2.1 bouyer id->config.cmd_drq_rem & ATAPI_PACKET_SIZE_MASK,
231 1.1.2.1 bouyer id->config.cmd_drq_rem & ATAPI_DRQ_MASK);
232 1.1.2.1 bouyer #endif
233 1.1.2.2 thorpej /*
234 1.1.2.2 thorpej * Shuffle string byte order.
235 1.1.2.2 thorpej * Mitsumi and NEC drives don't need this.
236 1.1.2.2 thorpej */
237 1.1.2.2 thorpej if (((id->model[0] == 'N' && id->model[1] == 'E') ||
238 1.1.2.2 thorpej (id->model[0] == 'F' && id->model[1] == 'X')) == 0)
239 1.1.2.2 thorpej bswap(id->model, sizeof(id->model));
240 1.1.2.2 thorpej bswap(id->serial_number, sizeof(id->serial_number));
241 1.1.2.2 thorpej bswap(id->firmware_revision, sizeof(id->firmware_revision));
242 1.1.2.1 bouyer
243 1.1.2.1 bouyer /*
244 1.1.2.1 bouyer * Allocate a device link and try and attach
245 1.1.2.1 bouyer * a driver to this device. If we fail, free
246 1.1.2.1 bouyer * the link.
247 1.1.2.1 bouyer */
248 1.1.2.1 bouyer sc_link = malloc(sizeof(*sc_link), M_DEVBUF, M_NOWAIT);
249 1.1.2.1 bouyer if (sc_link == NULL) {
250 1.1.2.1 bouyer printf("%s: can't allocate link for drive %d\n",
251 1.1.2.1 bouyer atapi->sc_dev.dv_xname, target);
252 1.1.2.1 bouyer return;
253 1.1.2.1 bouyer }
254 1.1.2.1 bouyer /* Fill in link. */
255 1.1.2.1 bouyer *sc_link = *atapi->adapter_link;
256 1.1.2.1 bouyer sc_link->scsipi_atapi.drive = target;
257 1.1.2.1 bouyer sc_link->device = NULL;
258 1.1.2.1 bouyer #if defined(SCSIDEBUG) && DEBUGTYPE == BUS_ATAPI
259 1.1.2.2 thorpej if (DEBUGTARGET == -1 || target == DEBUGTARGET)
260 1.1.2.2 thorpej sc_link->flags |= DEBUGLEVEL;
261 1.1.2.1 bouyer #endif /* SCSIDEBUG */
262 1.1.2.1 bouyer if (id->config.cmd_drq_rem & ATAPI_PACKET_SIZE_16)
263 1.1.2.1 bouyer sc_link->scsipi_atapi.cap |= ACAP_LEN;
264 1.1.2.1 bouyer sc_link->scsipi_atapi.cap |=
265 1.1.2.1 bouyer (id->config.cmd_drq_rem & ATAPI_DRQ_MASK) << 3;
266 1.1.2.1 bouyer #if 0
267 1.1.2.1 bouyer bcopy(id, &ad_link->id, sizeof(*id));
268 1.1.2.2 thorpej /* Fix strings and look through the quirk table. */
269 1.1.2.2 thorpej atapi_fixquirk(ad_link, id);
270 1.1.2.1 bouyer #endif
271 1.1.2.1 bouyer sa.sa_sc_link = sc_link;
272 1.1.2.1 bouyer sa.sa_inqbuf.type = id->config.device_type & SID_TYPE;
273 1.1.2.2 thorpej sa.sa_inqbuf.removable =
274 1.1.2.2 thorpej id->config.cmd_drq_rem & ATAPI_REMOVABLE ?
275 1.1.2.2 thorpej T_REMOV : T_FIXED;
276 1.1.2.1 bouyer if (sa.sa_inqbuf.removable)
277 1.1.2.1 bouyer sc_link->flags |= SDEV_REMOVABLE;
278 1.1.2.1 bouyer scsipi_strvis(model, id->model, 40);
279 1.1.2.1 bouyer scsipi_strvis(serial_number, id->serial_number, 20);
280 1.1.2.1 bouyer scsipi_strvis(firmware_revision, id->firmware_revision, 8);
281 1.1.2.1 bouyer sa.sa_inqbuf.vendor = model;
282 1.1.2.1 bouyer sa.sa_inqbuf.product = serial_number;
283 1.1.2.1 bouyer sa.sa_inqbuf.revision = firmware_revision;
284 1.1.2.1 bouyer
285 1.1.2.1 bouyer finger = (struct scsi_quirk_inquiry_pattern *)scsipi_inqmatch(
286 1.1.2.2 thorpej &sa.sa_inqbuf, (caddr_t)atapi_quirk_patterns,
287 1.1.2.2 thorpej sizeof(atapi_quirk_patterns) /
288 1.1.2.2 thorpej sizeof(atapi_quirk_patterns[0]),
289 1.1.2.2 thorpej sizeof(atapi_quirk_patterns[0]), &priority);
290 1.1.2.1 bouyer if (priority != 0)
291 1.1.2.1 bouyer sc_link->quirks |= finger->quirks;
292 1.1.2.1 bouyer
293 1.1.2.2 thorpej if ((cf = config_search(atapibussubmatch, &atapi->sc_dev,
294 1.1.2.2 thorpej &sa)) != 0) {
295 1.1.2.1 bouyer atapi->sc_link[target] = sc_link;
296 1.1.2.2 thorpej config_attach(&atapi->sc_dev, cf, &sa, atapibusprint);
297 1.1.2.1 bouyer return;
298 1.1.2.1 bouyer } else {
299 1.1.2.1 bouyer atapibusprint(&sa, atapi->sc_dev.dv_xname);
300 1.1.2.1 bouyer printf(" not configured\n");
301 1.1.2.1 bouyer free(sc_link, M_DEVBUF);
302 1.1.2.1 bouyer return;
303 1.1.2.1 bouyer }
304 1.1.2.1 bouyer
305 1.1.2.1 bouyer #if 0 /* WAS: */
306 1.1.2.1 bouyer /* Try to find a match. */
307 1.1.2.1 bouyer if (config_found(self, ad_link, atapiprint) == NULL)
308 1.1.2.1 bouyer free(ad_link, M_DEVBUF);
309 1.1.2.1 bouyer #endif
310 1.1.2.1 bouyer }
311 1.1.2.1 bouyer }
312 1.1.2.1 bouyer
313 1.1.2.1 bouyer int
314 1.1.2.1 bouyer atapibusprint(aux, pnp)
315 1.1.2.2 thorpej void *aux;
316 1.1.2.1 bouyer const char *pnp;
317 1.1.2.1 bouyer {
318 1.1.2.1 bouyer struct scsipibus_attach_args *sa = aux;
319 1.1.2.1 bouyer struct scsipi_inquiry_pattern *inqbuf;
320 1.1.2.1 bouyer char *dtype;
321 1.1.2.1 bouyer
322 1.1.2.1 bouyer if (pnp != NULL)
323 1.1.2.1 bouyer printf("%s", pnp);
324 1.1.2.2 thorpej
325 1.1.2.1 bouyer inqbuf = &sa->sa_inqbuf;
326 1.1.2.1 bouyer
327 1.1.2.1 bouyer dtype = scsipi_dtype(inqbuf->type & SID_TYPE);
328 1.1.2.1 bouyer printf(" drive %d: <%s, %s, %s> type %d %s %s",
329 1.1.2.2 thorpej sa->sa_sc_link->scsipi_atapi.drive,inqbuf->vendor,
330 1.1.2.2 thorpej inqbuf->product, inqbuf->revision, inqbuf->type, dtype,
331 1.1.2.2 thorpej inqbuf->removable ? "removable" : "fixed");
332 1.1.2.1 bouyer return (UNCONF);
333 1.1.2.1 bouyer }
334