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