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