1 /* $NetBSD: atapiconf.c,v 1.29 1999/10/20 15:22:27 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/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 sc_link_proto->scsipi_kill_pending = atapi_kill_pending; 197 198 199 sc_ab->adapter_link = sc_link_proto; 200 sc_ab->sc_drvs = aa_link->aa_drv_data; 201 202 nbytes = 2 * sizeof(struct scsipi_link **); 203 sc_ab->sc_link = (struct scsipi_link **)malloc(nbytes, M_DEVBUF, 204 M_NOWAIT); 205 if (sc_ab->sc_link == NULL) 206 panic("scsibusattach: can't allocate target links"); 207 memset(sc_ab->sc_link, 0, nbytes); 208 atapi_probe_bus(sc_ab->sc_dev.dv_unit, -1); 209 } 210 211 int 212 atapibusactivate(self, act) 213 struct device *self; 214 enum devact act; 215 { 216 struct atapibus_softc *sc = (struct atapibus_softc *)self; 217 struct scsipi_link *sc_link; 218 int target, error = 0, s; 219 220 s = splbio(); 221 switch (act) { 222 case DVACT_ACTIVATE: 223 error = EOPNOTSUPP; 224 break; 225 226 case DVACT_DEACTIVATE: 227 for (target = 0; target <= MAX_TARGET; target++) { 228 sc_link = sc->sc_link[target]; 229 if (sc_link == NULL) 230 continue; 231 error = config_deactivate(sc_link->device_softc); 232 if (error != 0) 233 goto out; 234 } 235 break; 236 } 237 out: 238 splx(s); 239 return (error); 240 } 241 242 int 243 atapibusdetach(self, flags) 244 struct device *self; 245 int flags; 246 { 247 struct atapibus_softc *sc = (struct atapibus_softc *)self; 248 struct scsipi_link *sc_link; 249 int target, error; 250 251 for (target = 0; target <= MAX_TARGET; target++) { 252 sc_link = sc->sc_link[target]; 253 if (sc_link == NULL) 254 continue; 255 error = config_detach(sc_link->device_softc, flags); 256 if (error != 0) 257 return (error); 258 259 /* 260 * We have successfully detached the child. Drop the 261 * direct reference for the child so that wdcdetach 262 * won't call detach routine twice. 263 */ 264 #ifdef DIAGNOSTIC 265 if (sc_link->device_softc != sc->sc_drvs[target].drv_softc) 266 panic("softc mismatch"); 267 #endif 268 sc->sc_drvs[target].drv_softc = NULL; 269 270 free(sc_link, M_DEVBUF); 271 sc->sc_link[target] = NULL; 272 } 273 return (0); 274 } 275 276 int 277 atapi_probe_bus(bus, target) 278 int bus, target; 279 { 280 int maxtarget, mintarget; 281 struct atapibus_softc *atapi; 282 int error; 283 284 if (bus < 0 || bus >= atapibus_cd.cd_ndevs) 285 return (ENXIO); 286 atapi = atapibus_cd.cd_devs[bus]; 287 if (atapi == NULL) 288 return (ENXIO); 289 290 if (target == -1) { 291 maxtarget = 1; 292 mintarget = 0; 293 } else { 294 if (target < 0 || target > 1) 295 return (ENXIO); 296 maxtarget = mintarget = target; 297 } 298 if ((error = scsipi_adapter_addref(atapi->adapter_link)) != 0) 299 return (error); 300 for (target = mintarget; target <= maxtarget; target++) 301 atapi_probedev(atapi, target); 302 scsipi_adapter_delref(atapi->adapter_link); 303 return (0); 304 } 305 306 void 307 atapi_probedev(atapi, target) 308 struct atapibus_softc *atapi; 309 int target; 310 { 311 struct scsipi_link *sc_link; 312 struct scsipibus_attach_args sa; 313 struct ataparams ids; 314 struct ataparams *id = &ids; 315 struct ata_drive_datas *drvp = &atapi->sc_drvs[target]; 316 struct cfdata *cf; 317 struct scsi_quirk_inquiry_pattern *finger; 318 int priority; 319 char serial_number[21], model[41], firmware_revision[9]; 320 321 /* skip if already attached */ 322 if (atapi->sc_link[target]) 323 return; 324 325 if (wdc_atapi_get_params(atapi->adapter_link, target, 326 XS_CTL_POLL|XS_CTL_NOSLEEP, id) == COMPLETE) { 327 #ifdef ATAPI_DEBUG_PROBE 328 printf("%s drive %d: cmdsz 0x%x drqtype 0x%x\n", 329 atapi->sc_dev.dv_xname, target, 330 id->atap_config & ATAPI_CFG_CMD_MASK, 331 id->atap_config & ATAPI_CFG_DRQ_MASK); 332 #endif 333 /* 334 * Allocate a device link and try and attach 335 * a driver to this device. If we fail, free 336 * the link. 337 */ 338 sc_link = malloc(sizeof(*sc_link), M_DEVBUF, M_NOWAIT); 339 if (sc_link == NULL) { 340 printf("%s: can't allocate link for drive %d\n", 341 atapi->sc_dev.dv_xname, target); 342 return; 343 } 344 /* Fill in link. */ 345 *sc_link = *atapi->adapter_link; 346 sc_link->active = 0; 347 sc_link->scsipi_atapi.drive = target; 348 sc_link->device = NULL; 349 TAILQ_INIT(&sc_link->pending_xfers); 350 #if defined(SCSIDEBUG) && DEBUGTYPE == BUS_ATAPI 351 if (DEBUGTARGET == -1 || target == DEBUGTARGET) 352 sc_link->flags |= DEBUGLEVEL; 353 #endif /* SCSIDEBUG */ 354 if ((id->atap_config & ATAPI_CFG_CMD_MASK) == ATAPI_CFG_CMD_16) 355 sc_link->scsipi_atapi.cap |= ACAP_LEN; 356 sc_link->scsipi_atapi.cap |= 357 (id->atap_config & ATAPI_CFG_DRQ_MASK); 358 sa.sa_sc_link = sc_link; 359 sa.sa_inqbuf.type = ATAPI_CFG_TYPE(id->atap_config); 360 sa.sa_inqbuf.removable = 361 id->atap_config & ATAPI_CFG_REMOV ? T_REMOV : T_FIXED; 362 if (sa.sa_inqbuf.removable) 363 sc_link->flags |= SDEV_REMOVABLE; 364 scsipi_strvis(model, 40, id->atap_model, 40); 365 scsipi_strvis(serial_number, 20, id->atap_serial, 20); 366 scsipi_strvis(firmware_revision, 8, id->atap_revision, 8); 367 sa.sa_inqbuf.vendor = model; 368 sa.sa_inqbuf.product = serial_number; 369 sa.sa_inqbuf.revision = firmware_revision; 370 371 finger = (struct scsi_quirk_inquiry_pattern *)scsipi_inqmatch( 372 &sa.sa_inqbuf, (caddr_t)atapi_quirk_patterns, 373 sizeof(atapi_quirk_patterns) / 374 sizeof(atapi_quirk_patterns[0]), 375 sizeof(atapi_quirk_patterns[0]), &priority); 376 if (priority != 0) 377 sc_link->quirks |= finger->quirks; 378 379 if ((cf = config_search(atapibussubmatch, &atapi->sc_dev, 380 &sa)) != 0) { 381 atapi->sc_link[target] = sc_link; 382 drvp->drv_softc = config_attach(&atapi->sc_dev, cf, 383 &sa, atapibusprint); 384 wdc_probe_caps(drvp); 385 return; 386 } else { 387 atapibusprint(&sa, atapi->sc_dev.dv_xname); 388 printf(" not configured\n"); 389 free(sc_link, M_DEVBUF); 390 return; 391 } 392 } 393 } 394 395 int 396 atapibusprint(aux, pnp) 397 void *aux; 398 const char *pnp; 399 { 400 struct scsipibus_attach_args *sa = aux; 401 struct scsipi_inquiry_pattern *inqbuf; 402 char *dtype; 403 404 if (pnp != NULL) 405 printf("%s", pnp); 406 407 inqbuf = &sa->sa_inqbuf; 408 409 dtype = scsipi_dtype(inqbuf->type & SID_TYPE); 410 printf(" drive %d: <%s, %s, %s> type %d %s %s", 411 sa->sa_sc_link->scsipi_atapi.drive,inqbuf->vendor, 412 inqbuf->product, inqbuf->revision, inqbuf->type, dtype, 413 inqbuf->removable ? "removable" : "fixed"); 414 return (UNCONF); 415 } 416