atapiconf.c revision 1.28 1 /* $NetBSD: atapiconf.c,v 1.28 1999/09/30 22:57:53 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/ata/atareg.h>
41 #include <dev/ata/atavar.h>
42 #include <dev/scsipi/scsipi_all.h>
43 #include <dev/scsipi/atapi_all.h>
44 #include <dev/scsipi/scsipiconf.h>
45 #include <dev/scsipi/atapiconf.h>
46
47 #include "locators.h"
48
49 #define SILENT_PRINTF(flags,string) if (!(flags & A_SILENT)) printf string
50 #define MAX_TARGET 1
51
52 struct atapibus_softc {
53 struct device sc_dev;
54 struct scsipi_link *adapter_link; /* proto supplied by adapter */
55 struct scsipi_link **sc_link; /* dynamically allocated */
56 struct ata_drive_datas *sc_drvs; /* array supplied by adapter */
57 };
58
59 int atapibusmatch __P((struct device *, struct cfdata *, void *));
60 int atapibussubmatch __P((struct device *, struct cfdata *, void *));
61 void atapibusattach __P((struct device *, struct device *, void *));
62 int atapibusactivate __P((struct device *, enum devact));
63 int atapibusdetach __P((struct device *, int flags));
64
65 int atapiprint __P((void *, const char *));
66
67 int atapi_probe_bus __P((int, int));
68 void atapi_probedev __P((struct atapibus_softc *, int ));
69
70 struct cfattach atapibus_ca = {
71 sizeof(struct atapibus_softc), atapibusmatch, atapibusattach,
72 atapibusdetach, atapibusactivate,
73 };
74
75 extern struct cfdriver atapibus_cd;
76
77 int atapibusprint __P((void *, const char *));
78
79 struct scsi_quirk_inquiry_pattern atapi_quirk_patterns[] = {
80 {{T_CDROM, T_REMOV,
81 "ALPS ELECTRIC CO.,LTD. DC544C", "", "SW03D"}, ADEV_NOTUR},
82 {{T_CDROM, T_REMOV,
83 "BCD-16X 1997-04-25", "", "VER 2.2"}, SDEV_NOSTARTUNIT},
84 {{T_CDROM, T_REMOV,
85 "BCD-24X 1997-06-27", "", "VER 2.0"}, SDEV_NOSTARTUNIT},
86 {{T_CDROM, T_REMOV,
87 "CR-2801TE", "", "1.07"}, ADEV_NOSENSE},
88 {{T_CDROM, T_REMOV,
89 "CREATIVECD3630E", "", "AC101"}, ADEV_NOSENSE},
90 {{T_CDROM, T_REMOV,
91 "FX320S", "", "q01"}, ADEV_NOSENSE},
92 {{T_CDROM, T_REMOV,
93 "GCD-R580B", "", "1.00"}, ADEV_LITTLETOC},
94 {{T_CDROM, T_REMOV,
95 "MATSHITA CR-574", "", "1.02"}, ADEV_NOCAPACITY},
96 {{T_CDROM, T_REMOV,
97 "MATSHITA CR-574", "", "1.06"}, ADEV_NOCAPACITY},
98 {{T_CDROM, T_REMOV,
99 "Memorex CRW-2642", "", "1.0g"}, ADEV_NOSENSE},
100 {{T_CDROM, T_REMOV,
101 "NEC CD-ROM DRIVE:273", "", "4.21"}, ADEV_NOTUR},
102 {{T_CDROM, T_REMOV,
103 "SANYO CRD-256P", "", "1.02"}, ADEV_NOCAPACITY},
104 {{T_CDROM, T_REMOV,
105 "SANYO CRD-254P", "", "1.02"}, ADEV_NOCAPACITY},
106 {{T_CDROM, T_REMOV,
107 "SANYO CRD-S54P", "", "1.08"}, ADEV_NOCAPACITY},
108 {{T_CDROM, T_REMOV,
109 "CD-ROM CDR-S1", "", "1.70"}, ADEV_NOCAPACITY}, /* Sanyo */
110 {{T_CDROM, T_REMOV,
111 "CD-ROM CDR-N16", "", "1.25"}, ADEV_NOCAPACITY}, /* Sanyo */
112 {{T_CDROM, T_REMOV,
113 "UJDCD8730", "", "1.14"}, ADEV_NODOORLOCK}, /* Acer */
114 };
115
116 int
117 atapibusmatch(parent, cf, aux)
118 struct device *parent;
119 struct cfdata *cf;
120 void *aux;
121 {
122 struct ata_atapi_attach *aa_link = aux;
123
124 if (aa_link == NULL)
125 return (0);
126 if (aa_link->aa_type != T_ATAPI)
127 return (0);
128 if (cf->cf_loc[ATAPICF_CHANNEL] != aa_link->aa_channel &&
129 cf->cf_loc[ATAPICF_CHANNEL] != ATAPICF_CHANNEL_DEFAULT)
130 return 0;
131 return (1);
132 }
133
134 int
135 atapibussubmatch(parent, cf, aux)
136 struct device *parent;
137 struct cfdata *cf;
138 void *aux;
139 {
140 struct scsipibus_attach_args *sa = aux;
141 struct scsipi_link *sc_link = sa->sa_sc_link;
142
143 if (cf->cf_loc[ATAPIBUSCF_DRIVE] != ATAPIBUSCF_DRIVE_DEFAULT &&
144 cf->cf_loc[ATAPIBUSCF_DRIVE] != sc_link->scsipi_atapi.drive)
145 return (0);
146 return ((*cf->cf_attach->ca_match)(parent, cf, aux));
147 }
148
149 #if 0
150 void
151 atapi_fixquirk(sc_link)
152 struct scsipi_link *ad_link;
153 {
154 struct ataparams *id = &ad_link->id;
155 struct atapi_quirk_inquiry_pattern *quirk;
156
157 /*
158 * Clean up the model name, serial and revision numbers.
159 */
160 btrim(id->model, sizeof(id->model));
161 btrim(id->serial_number, sizeof(id->serial_number));
162 btrim(id->firmware_revision, sizeof(id->firmware_revision));
163 }
164 #endif
165
166 void
167 atapibusattach(parent, self, aux)
168 struct device *parent, *self;
169 void *aux;
170 {
171 struct atapibus_softc *sc_ab = (struct atapibus_softc *)self;
172 struct ata_atapi_attach *aa_link = aux;
173 struct scsipi_link *sc_link_proto;
174 int nbytes;
175
176 printf("\n");
177
178 /* Initialize shared data. */
179 scsipi_init();
180
181 sc_link_proto = malloc(sizeof(struct scsipi_link),
182 M_DEVBUF, M_NOWAIT);
183 if (sc_link_proto == NULL)
184 printf("atapibusattach : can't allocate scsipi link proto\n");
185 memset(sc_link_proto, 0, sizeof(struct scsipi_link));
186
187 sc_link_proto->type = BUS_ATAPI;
188 sc_link_proto->openings = aa_link->aa_openings;
189 sc_link_proto->scsipi_atapi.channel = aa_link->aa_channel;
190 sc_link_proto->adapter_softc = parent;
191 sc_link_proto->adapter = aa_link->aa_bus_private;
192 sc_link_proto->scsipi_atapi.atapibus = sc_ab->sc_dev.dv_unit;
193 sc_link_proto->scsipi_cmd = atapi_scsipi_cmd;
194 sc_link_proto->scsipi_interpret_sense = atapi_interpret_sense;
195 sc_link_proto->sc_print_addr = atapi_print_addr;
196
197
198 sc_ab->adapter_link = sc_link_proto;
199 sc_ab->sc_drvs = aa_link->aa_drv_data;
200
201 nbytes = 2 * sizeof(struct scsipi_link **);
202 sc_ab->sc_link = (struct scsipi_link **)malloc(nbytes, M_DEVBUF,
203 M_NOWAIT);
204 if (sc_ab->sc_link == NULL)
205 panic("scsibusattach: can't allocate target links");
206 memset(sc_ab->sc_link, 0, nbytes);
207 atapi_probe_bus(sc_ab->sc_dev.dv_unit, -1);
208 }
209
210 int
211 atapibusactivate(self, act)
212 struct device *self;
213 enum devact act;
214 {
215 struct atapibus_softc *sc = (struct atapibus_softc *)self;
216 struct scsipi_link *sc_link;
217 int target, error = 0, s;
218
219 s = splbio();
220 switch (act) {
221 case DVACT_ACTIVATE:
222 error = EOPNOTSUPP;
223 break;
224
225 case DVACT_DEACTIVATE:
226 for (target = 0; target <= MAX_TARGET; target++) {
227 sc_link = sc->sc_link[target];
228 if (sc_link == NULL)
229 continue;
230 error = config_deactivate(sc_link->device_softc);
231 if (error != 0)
232 goto out;
233 }
234 break;
235 }
236 out:
237 splx(s);
238 return (error);
239 }
240
241 int
242 atapibusdetach(self, flags)
243 struct device *self;
244 int flags;
245 {
246 struct atapibus_softc *sc = (struct atapibus_softc *)self;
247 struct scsipi_link *sc_link;
248 int target, error;
249
250 for (target = 0; target <= MAX_TARGET; target++) {
251 sc_link = sc->sc_link[target];
252 if (sc_link == NULL)
253 continue;
254 error = config_detach(sc_link->device_softc, flags);
255 if (error != 0)
256 return (error);
257
258 /*
259 * We have successfully detached the child. Drop the
260 * direct reference for the child so that wdcdetach
261 * won't call detach routine twice.
262 */
263 #ifdef DIAGNOSTIC
264 if (sc_link->device_softc != sc->sc_drvs[target].drv_softc)
265 panic("softc mismatch");
266 #endif
267 sc->sc_drvs[target].drv_softc = NULL;
268
269 free(sc_link, M_DEVBUF);
270 sc->sc_link[target] = NULL;
271 }
272 return (0);
273 }
274
275 int
276 atapi_probe_bus(bus, target)
277 int bus, target;
278 {
279 int maxtarget, mintarget;
280 struct atapibus_softc *atapi;
281 int error;
282
283 if (bus < 0 || bus >= atapibus_cd.cd_ndevs)
284 return (ENXIO);
285 atapi = atapibus_cd.cd_devs[bus];
286 if (atapi == NULL)
287 return (ENXIO);
288
289 if (target == -1) {
290 maxtarget = 1;
291 mintarget = 0;
292 } else {
293 if (target < 0 || target > 1)
294 return (ENXIO);
295 maxtarget = mintarget = target;
296 }
297 if ((error = scsipi_adapter_addref(atapi->adapter_link)) != 0)
298 return (error);
299 for (target = mintarget; target <= maxtarget; target++)
300 atapi_probedev(atapi, target);
301 scsipi_adapter_delref(atapi->adapter_link);
302 return (0);
303 }
304
305 void
306 atapi_probedev(atapi, target)
307 struct atapibus_softc *atapi;
308 int target;
309 {
310 struct scsipi_link *sc_link;
311 struct scsipibus_attach_args sa;
312 struct ataparams ids;
313 struct ataparams *id = &ids;
314 struct ata_drive_datas *drvp = &atapi->sc_drvs[target];
315 struct cfdata *cf;
316 struct scsi_quirk_inquiry_pattern *finger;
317 int priority;
318 char serial_number[21], model[41], firmware_revision[9];
319
320 /* skip if already attached */
321 if (atapi->sc_link[target])
322 return;
323
324 if (wdc_atapi_get_params(atapi->adapter_link, target,
325 XS_CTL_POLL|XS_CTL_NOSLEEP, id) == COMPLETE) {
326 #ifdef ATAPI_DEBUG_PROBE
327 printf("%s drive %d: cmdsz 0x%x drqtype 0x%x\n",
328 atapi->sc_dev.dv_xname, target,
329 id->atap_config & ATAPI_CFG_CMD_MASK,
330 id->atap_config & ATAPI_CFG_DRQ_MASK);
331 #endif
332 /*
333 * Allocate a device link and try and attach
334 * a driver to this device. If we fail, free
335 * the link.
336 */
337 sc_link = malloc(sizeof(*sc_link), M_DEVBUF, M_NOWAIT);
338 if (sc_link == NULL) {
339 printf("%s: can't allocate link for drive %d\n",
340 atapi->sc_dev.dv_xname, target);
341 return;
342 }
343 /* Fill in link. */
344 *sc_link = *atapi->adapter_link;
345 sc_link->active = 0;
346 sc_link->scsipi_atapi.drive = target;
347 sc_link->device = NULL;
348 TAILQ_INIT(&sc_link->pending_xfers);
349 #if defined(SCSIDEBUG) && DEBUGTYPE == BUS_ATAPI
350 if (DEBUGTARGET == -1 || target == DEBUGTARGET)
351 sc_link->flags |= DEBUGLEVEL;
352 #endif /* SCSIDEBUG */
353 if ((id->atap_config & ATAPI_CFG_CMD_MASK) == ATAPI_CFG_CMD_16)
354 sc_link->scsipi_atapi.cap |= ACAP_LEN;
355 sc_link->scsipi_atapi.cap |=
356 (id->atap_config & ATAPI_CFG_DRQ_MASK);
357 sa.sa_sc_link = sc_link;
358 sa.sa_inqbuf.type = ATAPI_CFG_TYPE(id->atap_config);
359 sa.sa_inqbuf.removable =
360 id->atap_config & ATAPI_CFG_REMOV ? T_REMOV : T_FIXED;
361 if (sa.sa_inqbuf.removable)
362 sc_link->flags |= SDEV_REMOVABLE;
363 scsipi_strvis(model, 40, id->atap_model, 40);
364 scsipi_strvis(serial_number, 20, id->atap_serial, 20);
365 scsipi_strvis(firmware_revision, 8, id->atap_revision, 8);
366 sa.sa_inqbuf.vendor = model;
367 sa.sa_inqbuf.product = serial_number;
368 sa.sa_inqbuf.revision = firmware_revision;
369
370 finger = (struct scsi_quirk_inquiry_pattern *)scsipi_inqmatch(
371 &sa.sa_inqbuf, (caddr_t)atapi_quirk_patterns,
372 sizeof(atapi_quirk_patterns) /
373 sizeof(atapi_quirk_patterns[0]),
374 sizeof(atapi_quirk_patterns[0]), &priority);
375 if (priority != 0)
376 sc_link->quirks |= finger->quirks;
377
378 if ((cf = config_search(atapibussubmatch, &atapi->sc_dev,
379 &sa)) != 0) {
380 atapi->sc_link[target] = sc_link;
381 drvp->drv_softc = config_attach(&atapi->sc_dev, cf,
382 &sa, atapibusprint);
383 wdc_probe_caps(drvp);
384 return;
385 } else {
386 atapibusprint(&sa, atapi->sc_dev.dv_xname);
387 printf(" not configured\n");
388 free(sc_link, M_DEVBUF);
389 return;
390 }
391 }
392 }
393
394 int
395 atapibusprint(aux, pnp)
396 void *aux;
397 const char *pnp;
398 {
399 struct scsipibus_attach_args *sa = aux;
400 struct scsipi_inquiry_pattern *inqbuf;
401 char *dtype;
402
403 if (pnp != NULL)
404 printf("%s", pnp);
405
406 inqbuf = &sa->sa_inqbuf;
407
408 dtype = scsipi_dtype(inqbuf->type & SID_TYPE);
409 printf(" drive %d: <%s, %s, %s> type %d %s %s",
410 sa->sa_sc_link->scsipi_atapi.drive,inqbuf->vendor,
411 inqbuf->product, inqbuf->revision, inqbuf->type, dtype,
412 inqbuf->removable ? "removable" : "fixed");
413 return (UNCONF);
414 }
415