atapiconf.c revision 1.38 1 /* $NetBSD: atapiconf.c,v 1.38 2001/04/01 19:05:44 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 {{T_DIRECT, T_REMOV, /* ZiO! MultiMediaCard */
109 "eUSB", "MultiMediaCard", ""}, ADEV_NO_FLEX_PAGE },
110 };
111
112 int
113 atapibusmatch(parent, cf, aux)
114 struct device *parent;
115 struct cfdata *cf;
116 void *aux;
117 {
118 struct ata_atapi_attach *aa_link = aux;
119
120 if (aa_link == NULL)
121 return (0);
122 if (aa_link->aa_type != T_ATAPI)
123 return (0);
124 if (cf->cf_loc[ATAPICF_CHANNEL] != aa_link->aa_channel &&
125 cf->cf_loc[ATAPICF_CHANNEL] != ATAPICF_CHANNEL_DEFAULT)
126 return 0;
127 return (1);
128 }
129
130 int
131 atapibussubmatch(parent, cf, aux)
132 struct device *parent;
133 struct cfdata *cf;
134 void *aux;
135 {
136 struct scsipibus_attach_args *sa = aux;
137 struct scsipi_link *sc_link = sa->sa_sc_link;
138
139 if (cf->cf_loc[ATAPIBUSCF_DRIVE] != ATAPIBUSCF_DRIVE_DEFAULT &&
140 cf->cf_loc[ATAPIBUSCF_DRIVE] != sc_link->scsipi_atapi.drive)
141 return (0);
142 return ((*cf->cf_attach->ca_match)(parent, cf, aux));
143 }
144
145 void
146 atapibusattach(parent, self, aux)
147 struct device *parent, *self;
148 void *aux;
149 {
150 struct atapibus_softc *sc_ab = (struct atapibus_softc *)self;
151 struct ata_atapi_attach *aa_link = aux;
152 struct scsipi_link *sc_link_proto;
153 int nbytes;
154
155 printf("\n");
156
157 /* Initialize shared data. */
158 scsipi_init();
159
160 sc_link_proto = malloc(sizeof(struct scsipi_link),
161 M_DEVBUF, M_NOWAIT);
162 if (sc_link_proto == NULL)
163 panic("atapibusattach : can't allocate scsipi link proto\n");
164 memset(sc_link_proto, 0, sizeof(struct scsipi_link));
165
166 sc_link_proto->type = BUS_ATAPI;
167 sc_link_proto->openings = aa_link->aa_openings;
168 sc_link_proto->scsipi_atapi.channel = aa_link->aa_channel;
169 sc_link_proto->adapter_softc = parent;
170 sc_link_proto->adapter = aa_link->aa_bus_private;
171 sc_link_proto->scsipi_atapi.atapibus = sc_ab->sc_dev.dv_unit;
172 sc_link_proto->scsipi_cmd = atapi_scsipi_cmd;
173 sc_link_proto->scsipi_interpret_sense = atapi_interpret_sense;
174 sc_link_proto->sc_print_addr = atapi_print_addr;
175 sc_link_proto->scsipi_kill_pending =
176 ((struct atapi_adapter*)aa_link->aa_bus_private)->atapi_kill_pending;
177
178
179 sc_ab->adapter_link = sc_link_proto;
180 sc_ab->sc_drvs = aa_link->aa_drv_data;
181
182 nbytes = 2 * sizeof(struct scsipi_link **);
183 sc_ab->sc_link = (struct scsipi_link **)malloc(nbytes, M_DEVBUF,
184 M_NOWAIT);
185 if (sc_ab->sc_link == NULL)
186 panic("scsibusattach: can't allocate target links");
187 memset(sc_ab->sc_link, 0, nbytes);
188 atapi_probe_bus(sc_ab->sc_dev.dv_unit, -1);
189 }
190
191 int
192 atapibusactivate(self, act)
193 struct device *self;
194 enum devact act;
195 {
196 struct atapibus_softc *sc = (struct atapibus_softc *)self;
197 struct scsipi_link *sc_link;
198 int target, error = 0, s;
199
200 s = splbio();
201 switch (act) {
202 case DVACT_ACTIVATE:
203 error = EOPNOTSUPP;
204 break;
205
206 case DVACT_DEACTIVATE:
207 for (target = 0; target <= MAX_TARGET; target++) {
208 sc_link = sc->sc_link[target];
209 if (sc_link == NULL)
210 continue;
211 error = config_deactivate(sc_link->device_softc);
212 if (error != 0)
213 goto out;
214 }
215 break;
216 }
217 out:
218 splx(s);
219 return (error);
220 }
221
222 int
223 atapibusdetach(self, flags)
224 struct device *self;
225 int flags;
226 {
227 struct atapibus_softc *sc = (struct atapibus_softc *)self;
228 struct scsipi_link *sc_link;
229 int target, error;
230
231 for (target = 0; target <= MAX_TARGET; target++) {
232 sc_link = sc->sc_link[target];
233 if (sc_link == NULL)
234 continue;
235 error = config_detach(sc_link->device_softc, flags);
236 if (error != 0)
237 return (error);
238
239 /*
240 * We have successfully detached the child. Drop the
241 * direct reference for the child so that wdcdetach
242 * won't call detach routine twice.
243 */
244 #ifdef DIAGNOSTIC
245 if (sc_link->device_softc != sc->sc_drvs[target].drv_softc)
246 panic("softc mismatch");
247 #endif
248 sc->sc_drvs[target].drv_softc = NULL;
249
250 free(sc_link, M_DEVBUF);
251 sc->sc_link[target] = NULL;
252 }
253 return (0);
254 }
255
256 int
257 atapi_probe_bus(bus, target)
258 int bus, target;
259 {
260 int maxtarget, mintarget;
261 struct atapibus_softc *atapi;
262 int error;
263 struct atapi_adapter *atapi_adapter;
264
265 if (bus < 0 || bus >= atapibus_cd.cd_ndevs)
266 return (ENXIO);
267 atapi = atapibus_cd.cd_devs[bus];
268 if (atapi == NULL)
269 return (ENXIO);
270
271 if (target == -1) {
272 maxtarget = 1;
273 mintarget = 0;
274 } else {
275 if (target < 0 || target > 1)
276 return (ENXIO);
277 maxtarget = mintarget = target;
278 }
279 if ((error = scsipi_adapter_addref(atapi->adapter_link)) != 0)
280 return (error);
281 atapi_adapter = (struct atapi_adapter*)atapi->adapter_link->adapter;
282 for (target = mintarget; target <= maxtarget; target++)
283 atapi_adapter->atapi_probedev(atapi, target);
284 scsipi_adapter_delref(atapi->adapter_link);
285 return (0);
286 }
287
288 void *
289 atapi_probedev(atapi, target, sc_link, sa)
290 struct atapibus_softc *atapi;
291 int target;
292 struct scsipi_link *sc_link;
293 struct scsipibus_attach_args *sa;
294 {
295 struct cfdata *cf;
296 struct scsi_quirk_inquiry_pattern *finger;
297 int priority;
298
299 /* Fill generic parts of the link. */
300 sc_link->active = 0;
301 sc_link->scsipi_atapi.drive = target;
302 sc_link->device = NULL;
303 TAILQ_INIT(&sc_link->pending_xfers);
304 #if defined(SCSIDEBUG) && DEBUGTYPE == BUS_ATAPI
305 if (DEBUGTARGET == -1 || target == DEBUGTARGET)
306 sc_link->flags |= DEBUGLEVEL;
307 #endif /* SCSIDEBUG */
308 finger = (struct scsi_quirk_inquiry_pattern *)scsipi_inqmatch(
309 &sa->sa_inqbuf, (caddr_t)atapi_quirk_patterns,
310 sizeof(atapi_quirk_patterns) /
311 sizeof(atapi_quirk_patterns[0]),
312 sizeof(atapi_quirk_patterns[0]), &priority);
313 if (priority != 0)
314 sc_link->quirks |= finger->quirks;
315
316 if ((cf = config_search(atapibussubmatch, &atapi->sc_dev,
317 sa)) != 0) {
318 atapi->sc_link[target] = sc_link;
319 return (config_attach(&atapi->sc_dev, cf,
320 sa, atapibusprint));
321 } else {
322 atapibusprint(sa, atapi->sc_dev.dv_xname);
323 printf(" not configured\n");
324 free(sc_link, M_DEVBUF);
325 return (NULL);
326 }
327 }
328
329 int
330 atapibusprint(aux, pnp)
331 void *aux;
332 const char *pnp;
333 {
334 struct scsipibus_attach_args *sa = aux;
335 struct scsipi_inquiry_pattern *inqbuf;
336 char *dtype;
337
338 if (pnp != NULL)
339 printf("%s", pnp);
340
341 inqbuf = &sa->sa_inqbuf;
342
343 dtype = scsipi_dtype(inqbuf->type & SID_TYPE);
344 printf(" drive %d: <%s, %s, %s> type %d %s %s",
345 sa->sa_sc_link->scsipi_atapi.drive,inqbuf->vendor,
346 inqbuf->product, inqbuf->revision, inqbuf->type, dtype,
347 inqbuf->removable ? "removable" : "fixed");
348 return (UNCONF);
349 }
350