atapiconf.c revision 1.10 1 /* $NetBSD: atapiconf.c,v 1.10 1998/07/31 03:00:51 thorpej 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 /* Initialize shared data. */
175 scsipi_init();
176
177 sc_link_proto->scsipi_atapi.atapibus = ab->sc_dev.dv_unit;
178 sc_link_proto->scsipi_cmd = atapi_scsipi_cmd;
179 sc_link_proto->scsipi_interpret_sense = atapi_interpret_sense;
180 sc_link_proto->sc_print_addr = atapi_print_addr;
181
182 ab->adapter_link = sc_link_proto;
183
184 nbytes = 2 * sizeof(struct scsipi_link **);
185 ab->sc_link = (struct scsipi_link **)malloc(nbytes, M_DEVBUF,
186 M_NOWAIT);
187 if (ab->sc_link == NULL)
188 panic("scsibusattach: can't allocate target links");
189 bzero(ab->sc_link, nbytes);
190 atapi_probe_bus(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 atapi_identify ids;
227 struct atapi_identify *id = &ids;
228 struct cfdata *cf;
229 struct scsi_quirk_inquiry_pattern *finger;
230 int priority;
231 char serial_number[20], model[40], firmware_revision[8];
232
233 /* skip if already attached */
234 if (atapi->sc_link[target])
235 return;
236
237 if (wdc_atapi_get_params(atapi->adapter_link, target, id)) {
238 #ifdef ATAPI_DEBUG_PROBE
239 printf("%s drive %d: cmdsz 0x%x drqtype 0x%x\n",
240 atapi->sc_dev.dv_xname, target,
241 id->config.cmd_drq_rem & ATAPI_PACKET_SIZE_MASK,
242 id->config.cmd_drq_rem & ATAPI_DRQ_MASK);
243 #endif
244 /*
245 * Shuffle string byte order.
246 * Mitsumi and NEC drives don't need this.
247 */
248 if (((id->model[0] == 'N' && id->model[1] == 'E') ||
249 (id->model[0] == 'F' && id->model[1] == 'X')) == 0)
250 bswap(id->model, sizeof(id->model));
251 bswap(id->serial_number, sizeof(id->serial_number));
252 bswap(id->firmware_revision, sizeof(id->firmware_revision));
253
254 /*
255 * Allocate a device link and try and attach
256 * a driver to this device. If we fail, free
257 * the link.
258 */
259 sc_link = malloc(sizeof(*sc_link), M_DEVBUF, M_NOWAIT);
260 if (sc_link == NULL) {
261 printf("%s: can't allocate link for drive %d\n",
262 atapi->sc_dev.dv_xname, target);
263 return;
264 }
265 /* Fill in link. */
266 *sc_link = *atapi->adapter_link;
267 sc_link->scsipi_atapi.drive = target;
268 sc_link->device = NULL;
269 #if defined(SCSIDEBUG) && DEBUGTYPE == BUS_ATAPI
270 if (DEBUGTARGET == -1 || target == DEBUGTARGET)
271 sc_link->flags |= DEBUGLEVEL;
272 #endif /* SCSIDEBUG */
273 if (id->config.cmd_drq_rem & ATAPI_PACKET_SIZE_16)
274 sc_link->scsipi_atapi.cap |= ACAP_LEN;
275 sc_link->scsipi_atapi.cap |=
276 (id->config.cmd_drq_rem & ATAPI_DRQ_MASK) << 3;
277 #if 0
278 bcopy(id, &ad_link->id, sizeof(*id));
279 /* Fix strings and look through the quirk table. */
280 atapi_fixquirk(ad_link, id);
281 #endif
282 sa.sa_sc_link = sc_link;
283 sa.sa_inqbuf.type = id->config.device_type & SID_TYPE;
284 sa.sa_inqbuf.removable =
285 id->config.cmd_drq_rem & ATAPI_REMOVABLE ?
286 T_REMOV : T_FIXED;
287 if (sa.sa_inqbuf.removable)
288 sc_link->flags |= SDEV_REMOVABLE;
289 scsipi_strvis(model, id->model, 40);
290 scsipi_strvis(serial_number, id->serial_number, 20);
291 scsipi_strvis(firmware_revision, id->firmware_revision, 8);
292 sa.sa_inqbuf.vendor = model;
293 sa.sa_inqbuf.product = serial_number;
294 sa.sa_inqbuf.revision = firmware_revision;
295
296 finger = (struct scsi_quirk_inquiry_pattern *)scsipi_inqmatch(
297 &sa.sa_inqbuf, (caddr_t)atapi_quirk_patterns,
298 sizeof(atapi_quirk_patterns) /
299 sizeof(atapi_quirk_patterns[0]),
300 sizeof(atapi_quirk_patterns[0]), &priority);
301 if (priority != 0)
302 sc_link->quirks |= finger->quirks;
303
304 if ((cf = config_search(atapibussubmatch, &atapi->sc_dev,
305 &sa)) != 0) {
306 atapi->sc_link[target] = sc_link;
307 config_attach(&atapi->sc_dev, cf, &sa, atapibusprint);
308 return;
309 } else {
310 atapibusprint(&sa, atapi->sc_dev.dv_xname);
311 printf(" not configured\n");
312 free(sc_link, M_DEVBUF);
313 return;
314 }
315
316 #if 0 /* WAS: */
317 /* Try to find a match. */
318 if (config_found(self, ad_link, atapiprint) == NULL)
319 free(ad_link, M_DEVBUF);
320 #endif
321 }
322 }
323
324 int
325 atapibusprint(aux, pnp)
326 void *aux;
327 const char *pnp;
328 {
329 struct scsipibus_attach_args *sa = aux;
330 struct scsipi_inquiry_pattern *inqbuf;
331 char *dtype;
332
333 if (pnp != NULL)
334 printf("%s", pnp);
335
336 inqbuf = &sa->sa_inqbuf;
337
338 dtype = scsipi_dtype(inqbuf->type & SID_TYPE);
339 printf(" drive %d: <%s, %s, %s> type %d %s %s",
340 sa->sa_sc_link->scsipi_atapi.drive,inqbuf->vendor,
341 inqbuf->product, inqbuf->revision, inqbuf->type, dtype,
342 inqbuf->removable ? "removable" : "fixed");
343 return (UNCONF);
344 }
345