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