atapiconf.c revision 1.37 1 /* $NetBSD: atapiconf.c,v 1.37 2001/03/20 22:39:51 augustss 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/atapiconf.h>
45
46 #include "locators.h"
47
48 #define SILENT_PRINTF(flags,string) if (!(flags & A_SILENT)) printf string
49 #define MAX_TARGET 1
50
51 int atapibusmatch __P((struct device *, struct cfdata *, void *));
52 int atapibussubmatch __P((struct device *, struct cfdata *, void *));
53 void atapibusattach __P((struct device *, struct device *, void *));
54 int atapibusactivate __P((struct device *, enum devact));
55 int atapibusdetach __P((struct device *, int flags));
56
57 int atapi_probe_bus __P((int, int));
58
59 struct cfattach atapibus_ca = {
60 sizeof(struct atapibus_softc), atapibusmatch, atapibusattach,
61 atapibusdetach, atapibusactivate,
62 };
63
64 extern struct cfdriver atapibus_cd;
65
66 int atapibusprint __P((void *, const char *));
67
68 const struct scsi_quirk_inquiry_pattern atapi_quirk_patterns[] = {
69 {{T_CDROM, T_REMOV,
70 "ALPS ELECTRIC CO.,LTD. DC544C", "", "SW03D"}, ADEV_NOTUR},
71 {{T_CDROM, T_REMOV,
72 "BCD-16X 1997-04-25", "", "VER 2.2"}, SDEV_NOSTARTUNIT},
73 {{T_CDROM, T_REMOV,
74 "BCD-24X 1997-06-27", "", "VER 2.0"}, SDEV_NOSTARTUNIT},
75 {{T_CDROM, T_REMOV,
76 "CR-2801TE", "", "1.07"}, ADEV_NOSENSE},
77 {{T_CDROM, T_REMOV,
78 "CREATIVECD3630E", "", "AC101"}, ADEV_NOSENSE},
79 {{T_CDROM, T_REMOV,
80 "FX320S", "", "q01"}, ADEV_NOSENSE},
81 {{T_CDROM, T_REMOV,
82 "GCD-R580B", "", "1.00"}, ADEV_LITTLETOC},
83 {{T_CDROM, T_REMOV,
84 "HITACHI CDR-7730", "", "0008a"}, ADEV_NOSENSE},
85 {{T_CDROM, T_REMOV,
86 "MATSHITA CR-574", "", "1.02"}, ADEV_NOCAPACITY},
87 {{T_CDROM, T_REMOV,
88 "MATSHITA CR-574", "", "1.06"}, ADEV_NOCAPACITY},
89 {{T_CDROM, T_REMOV,
90 "Memorex CRW-2642", "", "1.0g"}, ADEV_NOSENSE},
91 {{T_CDROM, T_REMOV,
92 "NEC CD-ROM DRIVE:273", "", "4.21"}, ADEV_NOTUR},
93 {{T_CDROM, T_REMOV,
94 "SANYO CRD-256P", "", "1.02"}, ADEV_NOCAPACITY},
95 {{T_CDROM, T_REMOV,
96 "SANYO CRD-254P", "", "1.02"}, ADEV_NOCAPACITY},
97 {{T_CDROM, T_REMOV,
98 "SANYO CRD-S54P", "", "1.08"}, ADEV_NOCAPACITY},
99 {{T_CDROM, T_REMOV,
100 "CD-ROM CDR-S1", "", "1.70"}, ADEV_NOCAPACITY}, /* Sanyo */
101 {{T_CDROM, T_REMOV,
102 "CD-ROM CDR-N16", "", "1.25"}, ADEV_NOCAPACITY}, /* Sanyo */
103 {{T_CDROM, T_REMOV,
104 "UJDCD8730", "", "1.14"}, ADEV_NODOORLOCK}, /* Acer */
105 {{T_DIRECT, T_REMOV, /* Panasonic MultiMediaCard */
106 "04DA", "1B00", "0010"}, ADEV_BYTE5_ZERO |
107 ADEV_NO_FLEX_PAGE },
108 };
109
110 int
111 atapibusmatch(parent, cf, aux)
112 struct device *parent;
113 struct cfdata *cf;
114 void *aux;
115 {
116 struct ata_atapi_attach *aa_link = aux;
117
118 if (aa_link == NULL)
119 return (0);
120 if (aa_link->aa_type != T_ATAPI)
121 return (0);
122 if (cf->cf_loc[ATAPICF_CHANNEL] != aa_link->aa_channel &&
123 cf->cf_loc[ATAPICF_CHANNEL] != ATAPICF_CHANNEL_DEFAULT)
124 return 0;
125 return (1);
126 }
127
128 int
129 atapibussubmatch(parent, cf, aux)
130 struct device *parent;
131 struct cfdata *cf;
132 void *aux;
133 {
134 struct scsipibus_attach_args *sa = aux;
135 struct scsipi_link *sc_link = sa->sa_sc_link;
136
137 if (cf->cf_loc[ATAPIBUSCF_DRIVE] != ATAPIBUSCF_DRIVE_DEFAULT &&
138 cf->cf_loc[ATAPIBUSCF_DRIVE] != sc_link->scsipi_atapi.drive)
139 return (0);
140 return ((*cf->cf_attach->ca_match)(parent, cf, aux));
141 }
142
143 void
144 atapibusattach(parent, self, aux)
145 struct device *parent, *self;
146 void *aux;
147 {
148 struct atapibus_softc *sc_ab = (struct atapibus_softc *)self;
149 struct ata_atapi_attach *aa_link = aux;
150 struct scsipi_link *sc_link_proto;
151 int nbytes;
152
153 printf("\n");
154
155 /* Initialize shared data. */
156 scsipi_init();
157
158 sc_link_proto = malloc(sizeof(struct scsipi_link),
159 M_DEVBUF, M_NOWAIT);
160 if (sc_link_proto == NULL)
161 panic("atapibusattach : can't allocate scsipi link proto\n");
162 memset(sc_link_proto, 0, sizeof(struct scsipi_link));
163
164 sc_link_proto->type = BUS_ATAPI;
165 sc_link_proto->openings = aa_link->aa_openings;
166 sc_link_proto->scsipi_atapi.channel = aa_link->aa_channel;
167 sc_link_proto->adapter_softc = parent;
168 sc_link_proto->adapter = aa_link->aa_bus_private;
169 sc_link_proto->scsipi_atapi.atapibus = sc_ab->sc_dev.dv_unit;
170 sc_link_proto->scsipi_cmd = atapi_scsipi_cmd;
171 sc_link_proto->scsipi_interpret_sense = atapi_interpret_sense;
172 sc_link_proto->sc_print_addr = atapi_print_addr;
173 sc_link_proto->scsipi_kill_pending =
174 ((struct atapi_adapter*)aa_link->aa_bus_private)->atapi_kill_pending;
175
176
177 sc_ab->adapter_link = sc_link_proto;
178 sc_ab->sc_drvs = aa_link->aa_drv_data;
179
180 nbytes = 2 * sizeof(struct scsipi_link **);
181 sc_ab->sc_link = (struct scsipi_link **)malloc(nbytes, M_DEVBUF,
182 M_NOWAIT);
183 if (sc_ab->sc_link == NULL)
184 panic("scsibusattach: can't allocate target links");
185 memset(sc_ab->sc_link, 0, nbytes);
186 atapi_probe_bus(sc_ab->sc_dev.dv_unit, -1);
187 }
188
189 int
190 atapibusactivate(self, act)
191 struct device *self;
192 enum devact act;
193 {
194 struct atapibus_softc *sc = (struct atapibus_softc *)self;
195 struct scsipi_link *sc_link;
196 int target, error = 0, s;
197
198 s = splbio();
199 switch (act) {
200 case DVACT_ACTIVATE:
201 error = EOPNOTSUPP;
202 break;
203
204 case DVACT_DEACTIVATE:
205 for (target = 0; target <= MAX_TARGET; target++) {
206 sc_link = sc->sc_link[target];
207 if (sc_link == NULL)
208 continue;
209 error = config_deactivate(sc_link->device_softc);
210 if (error != 0)
211 goto out;
212 }
213 break;
214 }
215 out:
216 splx(s);
217 return (error);
218 }
219
220 int
221 atapibusdetach(self, flags)
222 struct device *self;
223 int flags;
224 {
225 struct atapibus_softc *sc = (struct atapibus_softc *)self;
226 struct scsipi_link *sc_link;
227 int target, error;
228
229 for (target = 0; target <= MAX_TARGET; target++) {
230 sc_link = sc->sc_link[target];
231 if (sc_link == NULL)
232 continue;
233 error = config_detach(sc_link->device_softc, flags);
234 if (error != 0)
235 return (error);
236
237 /*
238 * We have successfully detached the child. Drop the
239 * direct reference for the child so that wdcdetach
240 * won't call detach routine twice.
241 */
242 #ifdef DIAGNOSTIC
243 if (sc_link->device_softc != sc->sc_drvs[target].drv_softc)
244 panic("softc mismatch");
245 #endif
246 sc->sc_drvs[target].drv_softc = NULL;
247
248 free(sc_link, M_DEVBUF);
249 sc->sc_link[target] = NULL;
250 }
251 return (0);
252 }
253
254 int
255 atapi_probe_bus(bus, target)
256 int bus, target;
257 {
258 int maxtarget, mintarget;
259 struct atapibus_softc *atapi;
260 int error;
261 struct atapi_adapter *atapi_adapter;
262
263 if (bus < 0 || bus >= atapibus_cd.cd_ndevs)
264 return (ENXIO);
265 atapi = atapibus_cd.cd_devs[bus];
266 if (atapi == NULL)
267 return (ENXIO);
268
269 if (target == -1) {
270 maxtarget = 1;
271 mintarget = 0;
272 } else {
273 if (target < 0 || target > 1)
274 return (ENXIO);
275 maxtarget = mintarget = target;
276 }
277 if ((error = scsipi_adapter_addref(atapi->adapter_link)) != 0)
278 return (error);
279 atapi_adapter = (struct atapi_adapter*)atapi->adapter_link->adapter;
280 for (target = mintarget; target <= maxtarget; target++)
281 atapi_adapter->atapi_probedev(atapi, target);
282 scsipi_adapter_delref(atapi->adapter_link);
283 return (0);
284 }
285
286 void *
287 atapi_probedev(atapi, target, sc_link, sa)
288 struct atapibus_softc *atapi;
289 int target;
290 struct scsipi_link *sc_link;
291 struct scsipibus_attach_args *sa;
292 {
293 struct cfdata *cf;
294 struct scsi_quirk_inquiry_pattern *finger;
295 int priority;
296
297 /* Fill generic parts of the link. */
298 sc_link->active = 0;
299 sc_link->scsipi_atapi.drive = target;
300 sc_link->device = NULL;
301 TAILQ_INIT(&sc_link->pending_xfers);
302 #if defined(SCSIDEBUG) && DEBUGTYPE == BUS_ATAPI
303 if (DEBUGTARGET == -1 || target == DEBUGTARGET)
304 sc_link->flags |= DEBUGLEVEL;
305 #endif /* SCSIDEBUG */
306 finger = (struct scsi_quirk_inquiry_pattern *)scsipi_inqmatch(
307 &sa->sa_inqbuf, (caddr_t)atapi_quirk_patterns,
308 sizeof(atapi_quirk_patterns) /
309 sizeof(atapi_quirk_patterns[0]),
310 sizeof(atapi_quirk_patterns[0]), &priority);
311 if (priority != 0)
312 sc_link->quirks |= finger->quirks;
313
314 if ((cf = config_search(atapibussubmatch, &atapi->sc_dev,
315 sa)) != 0) {
316 atapi->sc_link[target] = sc_link;
317 return (config_attach(&atapi->sc_dev, cf,
318 sa, atapibusprint));
319 } else {
320 atapibusprint(sa, atapi->sc_dev.dv_xname);
321 printf(" not configured\n");
322 free(sc_link, M_DEVBUF);
323 return (NULL);
324 }
325 }
326
327 int
328 atapibusprint(aux, pnp)
329 void *aux;
330 const char *pnp;
331 {
332 struct scsipibus_attach_args *sa = aux;
333 struct scsipi_inquiry_pattern *inqbuf;
334 char *dtype;
335
336 if (pnp != NULL)
337 printf("%s", pnp);
338
339 inqbuf = &sa->sa_inqbuf;
340
341 dtype = scsipi_dtype(inqbuf->type & SID_TYPE);
342 printf(" drive %d: <%s, %s, %s> type %d %s %s",
343 sa->sa_sc_link->scsipi_atapi.drive,inqbuf->vendor,
344 inqbuf->product, inqbuf->revision, inqbuf->type, dtype,
345 inqbuf->removable ? "removable" : "fixed");
346 return (UNCONF);
347 }
348