atapiconf.c revision 1.4 1 /* $NetBSD: atapiconf.c,v 1.4 1997/10/01 01:18:41 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/scsipi/scsipi_all.h>
41 #include <dev/scsipi/scsi_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 struct cfdriver atapibus_cd = {
73 NULL, "atapibus", DV_DULL
74 };
75
76 int atapibusprint __P((void *, const char *));
77
78 struct scsi_quirk_inquiry_pattern atapi_quirk_patterns[] = {
79 {{T_CDROM, T_REMOV,
80 "GCD-R580B", "", "1.00"}, ADEV_LITTLETOC},
81 {{T_CDROM, T_REMOV,
82 "SANYO CRD-256P", "", "1.02"}, ADEV_NOCAPACITY},
83 {{T_CDROM, T_REMOV,
84 "SANYO CRD-254P", "", "1.02"}, ADEV_NOCAPACITY},
85 {{T_CDROM, T_REMOV,
86 "UJDCD8730", "", "1.14"}, ADEV_NODOORLOCK},
87 {{T_CDROM, T_REMOV,
88 "ALPS ELECTRIC CO.,LTD. DC544C", "", "SW03D"}, ADEV_NOTUR},
89 {{T_CDROM, T_REMOV,
90 "NEC CD-ROM DRIVE:273", "", "4.21"}, ADEV_NOTUR},
91 {{T_CDROM, T_REMOV,
92 "MATSHITA CR-574", "", "1.06"}, ADEV_NOCAPACITY},
93 };
94
95 int
96 #ifdef __BROKEN_INDIRECT_CONFIG
97 atapibusmatch(parent, match, aux)
98 struct device *parent;
99 void *match, *aux;
100 {
101 #else
102 atapibusmatch(parent, cf, aux)
103 struct device *parent;
104 struct cfdata *cf;
105 void *aux;
106 {
107 #endif
108 struct scsipi_link *sc_link = aux;
109
110 if (sc_link == NULL)
111 return (0);
112 if (sc_link->type != BUS_ATAPI)
113 return (0);
114 return (1);
115 }
116
117 int
118 #ifdef __BROKEN_INDIRECT_CONFIG
119 atapibussubmatch(parent, match, aux)
120 struct device *parent;
121 void *match, *aux;
122 {
123 struct cfdata *cf = match;
124 #else
125 atapibussubmatch(parent, cf, aux)
126 struct device *parent;
127 struct cfdata *cf;
128 void *aux;
129 {
130 #endif
131 struct scsipibus_attach_args *sa = aux;
132 struct scsipi_link *sc_link = sa->sa_sc_link;
133
134 if (cf->cf_loc[ATAPIBUSCF_DRIVE] != ATAPIBUSCF_DRIVE_DEFAULT &&
135 cf->cf_loc[ATAPIBUSCF_DRIVE] != sc_link->scsipi_atapi.drive)
136 return (0);
137 return ((*cf->cf_attach->ca_match)(parent, cf, aux));
138 }
139
140 #if 0
141 void
142 atapi_fixquirk(sc_link)
143 struct scsipi_link *ad_link;
144 {
145 struct atapi_identify *id = &ad_link->id;
146 struct atapi_quirk_inquiry_pattern *quirk;
147
148 /*
149 * Clean up the model name, serial and revision numbers.
150 */
151 btrim(id->model, sizeof(id->model));
152 btrim(id->serial_number, sizeof(id->serial_number));
153 btrim(id->firmware_revision, sizeof(id->firmware_revision));
154 }
155 #endif
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
191 if (bus < 0 || bus >= atapibus_cd.cd_ndevs)
192 return (ENXIO);
193 atapi = atapibus_cd.cd_devs[bus];
194 if (atapi == NULL)
195 return (ENXIO);
196
197 if (target == -1) {
198 maxtarget = 1;
199 mintarget = 0;
200 } else {
201 if (target < 0 || target > 1)
202 return (ENXIO);
203 maxtarget = mintarget = target;
204 }
205 for (target = mintarget; target <= maxtarget; target++)
206 atapi_probedev(atapi, target);
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