1 /* $NetBSD: atapiconf.c,v 1.25 1999/02/15 18:43:08 bouyer 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 51 struct atapibus_softc { 52 struct device sc_dev; 53 struct scsipi_link *adapter_link; /* proto supplied by adapter */ 54 struct scsipi_link **sc_link; /* dynamically allocated */ 55 struct ata_drive_datas *sc_drvs; /* array supplied by adapter */ 56 }; 57 58 int atapibusmatch __P((struct device *, struct cfdata *, void *)); 59 int atapibussubmatch __P((struct device *, struct cfdata *, void *)); 60 void atapibusattach __P((struct device *, struct device *, void *)); 61 int atapiprint __P((void *, const char *)); 62 63 int atapi_probe_bus __P((int, int)); 64 void atapi_probedev __P((struct atapibus_softc *, int )); 65 66 struct cfattach atapibus_ca = { 67 sizeof(struct atapibus_softc), atapibusmatch, atapibusattach 68 }; 69 70 extern struct cfdriver atapibus_cd; 71 72 int atapibusprint __P((void *, const char *)); 73 74 struct scsi_quirk_inquiry_pattern atapi_quirk_patterns[] = { 75 {{T_CDROM, T_REMOV, 76 "ALPS ELECTRIC CO.,LTD. DC544C", "", "SW03D"}, ADEV_NOTUR}, 77 {{T_CDROM, T_REMOV, 78 "BCD-16X 1997-04-25", "", "VER 2.2"}, SDEV_NOSTARTUNIT}, 79 {{T_CDROM, T_REMOV, 80 "CR-2801TE", "", "1.07"}, ADEV_NOSENSE}, 81 {{T_CDROM, T_REMOV, 82 "CREATIVECD3630E", "", "AC101"}, ADEV_NOSENSE}, 83 {{T_CDROM, T_REMOV, 84 "FX320S", "", "q01"}, ADEV_NOSENSE}, 85 {{T_CDROM, T_REMOV, 86 "GCD-R580B", "", "1.00"}, ADEV_LITTLETOC}, 87 {{T_CDROM, T_REMOV, 88 "MATSHITA CR-574", "", "1.02"}, ADEV_NOCAPACITY}, 89 {{T_CDROM, T_REMOV, 90 "MATSHITA CR-574", "", "1.06"}, ADEV_NOCAPACITY}, 91 {{T_CDROM, T_REMOV, 92 "Memorex CRW-2642", "", "1.0g"}, ADEV_NOSENSE}, 93 {{T_CDROM, T_REMOV, 94 "NEC CD-ROM DRIVE:273", "", "4.21"}, ADEV_NOTUR}, 95 {{T_CDROM, T_REMOV, 96 "SANYO CRD-256P", "", "1.02"}, ADEV_NOCAPACITY}, 97 {{T_CDROM, T_REMOV, 98 "SANYO CRD-254P", "", "1.02"}, ADEV_NOCAPACITY}, 99 {{T_CDROM, T_REMOV, 100 "SANYO CRD-S54P", "", "1.08"}, ADEV_NOCAPACITY}, 101 {{T_CDROM, T_REMOV, 102 "CD-ROM CDR-S1", "", "1.70"}, ADEV_NOCAPACITY}, /* Sanyo */ 103 {{T_CDROM, T_REMOV, 104 "CD-ROM CDR-N16", "", "1.25"}, ADEV_NOCAPACITY}, /* Sanyo */ 105 {{T_CDROM, T_REMOV, 106 "UJDCD8730", "", "1.14"}, ADEV_NODOORLOCK}, /* Acer */ 107 }; 108 109 int 110 atapibusmatch(parent, cf, aux) 111 struct device *parent; 112 struct cfdata *cf; 113 void *aux; 114 { 115 struct ata_atapi_attach *aa_link = aux; 116 117 if (aa_link == NULL) 118 return (0); 119 if (aa_link->aa_type != T_ATAPI) 120 return (0); 121 if (cf->cf_loc[ATAPICF_CHANNEL] != aa_link->aa_channel && 122 cf->cf_loc[ATAPICF_CHANNEL] != ATAPICF_CHANNEL_DEFAULT) 123 return 0; 124 return (1); 125 } 126 127 int 128 atapibussubmatch(parent, cf, aux) 129 struct device *parent; 130 struct cfdata *cf; 131 void *aux; 132 { 133 struct scsipibus_attach_args *sa = aux; 134 struct scsipi_link *sc_link = sa->sa_sc_link; 135 136 if (cf->cf_loc[ATAPIBUSCF_DRIVE] != ATAPIBUSCF_DRIVE_DEFAULT && 137 cf->cf_loc[ATAPIBUSCF_DRIVE] != sc_link->scsipi_atapi.drive) 138 return (0); 139 return ((*cf->cf_attach->ca_match)(parent, cf, aux)); 140 } 141 142 #if 0 143 void 144 atapi_fixquirk(sc_link) 145 struct scsipi_link *ad_link; 146 { 147 struct ataparams *id = &ad_link->id; 148 struct atapi_quirk_inquiry_pattern *quirk; 149 150 /* 151 * Clean up the model name, serial and revision numbers. 152 */ 153 btrim(id->model, sizeof(id->model)); 154 btrim(id->serial_number, sizeof(id->serial_number)); 155 btrim(id->firmware_revision, sizeof(id->firmware_revision)); 156 } 157 #endif 158 159 void 160 atapibusattach(parent, self, aux) 161 struct device *parent, *self; 162 void *aux; 163 { 164 struct atapibus_softc *sc_ab = (struct atapibus_softc *)self; 165 struct ata_atapi_attach *aa_link = aux; 166 struct scsipi_link *sc_link_proto; 167 int nbytes; 168 169 printf("\n"); 170 171 /* Initialize shared data. */ 172 scsipi_init(); 173 174 sc_link_proto = malloc(sizeof(struct scsipi_link), 175 M_DEVBUF, M_NOWAIT); 176 if (sc_link_proto == NULL) 177 printf("atapibusattach : can't allocate scsipi link proto\n"); 178 memset(sc_link_proto, 0, sizeof(struct scsipi_link)); 179 180 sc_link_proto->type = BUS_ATAPI; 181 sc_link_proto->openings = aa_link->aa_openings; 182 sc_link_proto->scsipi_atapi.channel = aa_link->aa_channel; 183 sc_link_proto->adapter_softc = parent; 184 sc_link_proto->adapter = aa_link->aa_bus_private; 185 sc_link_proto->scsipi_atapi.atapibus = sc_ab->sc_dev.dv_unit; 186 sc_link_proto->scsipi_cmd = atapi_scsipi_cmd; 187 sc_link_proto->scsipi_interpret_sense = atapi_interpret_sense; 188 sc_link_proto->sc_print_addr = atapi_print_addr; 189 190 191 sc_ab->adapter_link = sc_link_proto; 192 sc_ab->sc_drvs = aa_link->aa_drv_data; 193 194 nbytes = 2 * sizeof(struct scsipi_link **); 195 sc_ab->sc_link = (struct scsipi_link **)malloc(nbytes, M_DEVBUF, 196 M_NOWAIT); 197 if (sc_ab->sc_link == NULL) 198 panic("scsibusattach: can't allocate target links"); 199 memset(sc_ab->sc_link, 0, nbytes); 200 atapi_probe_bus(sc_ab->sc_dev.dv_unit, -1); 201 } 202 203 int 204 atapi_probe_bus(bus, target) 205 int bus, target; 206 { 207 int maxtarget, mintarget; 208 struct atapibus_softc *atapi; 209 int error; 210 211 if (bus < 0 || bus >= atapibus_cd.cd_ndevs) 212 return (ENXIO); 213 atapi = atapibus_cd.cd_devs[bus]; 214 if (atapi == NULL) 215 return (ENXIO); 216 217 if (target == -1) { 218 maxtarget = 1; 219 mintarget = 0; 220 } else { 221 if (target < 0 || target > 1) 222 return (ENXIO); 223 maxtarget = mintarget = target; 224 } 225 if ((error = scsipi_adapter_addref(atapi->adapter_link)) != 0) 226 return (error); 227 for (target = mintarget; target <= maxtarget; target++) 228 atapi_probedev(atapi, target); 229 scsipi_adapter_delref(atapi->adapter_link); 230 return (0); 231 } 232 233 void 234 atapi_probedev(atapi, target) 235 struct atapibus_softc *atapi; 236 int target; 237 { 238 struct scsipi_link *sc_link; 239 struct scsipibus_attach_args sa; 240 struct ataparams ids; 241 struct ataparams *id = &ids; 242 struct ata_drive_datas *drvp = &atapi->sc_drvs[target]; 243 struct cfdata *cf; 244 struct scsi_quirk_inquiry_pattern *finger; 245 int priority; 246 char serial_number[21], model[41], firmware_revision[9]; 247 248 /* skip if already attached */ 249 if (atapi->sc_link[target]) 250 return; 251 252 if (wdc_atapi_get_params(atapi->adapter_link, target, 253 SCSI_POLL|SCSI_NOSLEEP, id) == COMPLETE) { 254 #ifdef ATAPI_DEBUG_PROBE 255 printf("%s drive %d: cmdsz 0x%x drqtype 0x%x\n", 256 atapi->sc_dev.dv_xname, target, 257 id->atap_config & ATAPI_CFG_CMD_MASK, 258 id->atap_config & ATAPI_CFG_DRQ_MASK); 259 #endif 260 /* 261 * Allocate a device link and try and attach 262 * a driver to this device. If we fail, free 263 * the link. 264 */ 265 sc_link = malloc(sizeof(*sc_link), M_DEVBUF, M_NOWAIT); 266 if (sc_link == NULL) { 267 printf("%s: can't allocate link for drive %d\n", 268 atapi->sc_dev.dv_xname, target); 269 return; 270 } 271 /* Fill in link. */ 272 *sc_link = *atapi->adapter_link; 273 sc_link->scsipi_atapi.drive = target; 274 sc_link->device = NULL; 275 TAILQ_INIT(&sc_link->pending_xfers); 276 #if defined(SCSIDEBUG) && DEBUGTYPE == BUS_ATAPI 277 if (DEBUGTARGET == -1 || target == DEBUGTARGET) 278 sc_link->flags |= DEBUGLEVEL; 279 #endif /* SCSIDEBUG */ 280 if ((id->atap_config & ATAPI_CFG_CMD_MASK) == ATAPI_CFG_CMD_16) 281 sc_link->scsipi_atapi.cap |= ACAP_LEN; 282 sc_link->scsipi_atapi.cap |= 283 (id->atap_config & ATAPI_CFG_DRQ_MASK); 284 sa.sa_sc_link = sc_link; 285 sa.sa_inqbuf.type = ATAPI_CFG_TYPE(id->atap_config); 286 sa.sa_inqbuf.removable = 287 id->atap_config & ATAPI_CFG_REMOV ? T_REMOV : T_FIXED; 288 if (sa.sa_inqbuf.removable) 289 sc_link->flags |= SDEV_REMOVABLE; 290 scsipi_strvis(model, 40, id->atap_model, 40); 291 scsipi_strvis(serial_number, 20, id->atap_serial, 20); 292 scsipi_strvis(firmware_revision, 8, id->atap_revision, 8); 293 sa.sa_inqbuf.vendor = model; 294 sa.sa_inqbuf.product = serial_number; 295 sa.sa_inqbuf.revision = firmware_revision; 296 297 finger = (struct scsi_quirk_inquiry_pattern *)scsipi_inqmatch( 298 &sa.sa_inqbuf, (caddr_t)atapi_quirk_patterns, 299 sizeof(atapi_quirk_patterns) / 300 sizeof(atapi_quirk_patterns[0]), 301 sizeof(atapi_quirk_patterns[0]), &priority); 302 if (priority != 0) 303 sc_link->quirks |= finger->quirks; 304 305 if ((cf = config_search(atapibussubmatch, &atapi->sc_dev, 306 &sa)) != 0) { 307 atapi->sc_link[target] = sc_link; 308 drvp->drv_softc = config_attach(&atapi->sc_dev, cf, 309 &sa, atapibusprint); 310 wdc_probe_caps(drvp); 311 return; 312 } else { 313 atapibusprint(&sa, atapi->sc_dev.dv_xname); 314 printf(" not configured\n"); 315 free(sc_link, M_DEVBUF); 316 return; 317 } 318 } 319 } 320 321 int 322 atapibusprint(aux, pnp) 323 void *aux; 324 const char *pnp; 325 { 326 struct scsipibus_attach_args *sa = aux; 327 struct scsipi_inquiry_pattern *inqbuf; 328 char *dtype; 329 330 if (pnp != NULL) 331 printf("%s", pnp); 332 333 inqbuf = &sa->sa_inqbuf; 334 335 dtype = scsipi_dtype(inqbuf->type & SID_TYPE); 336 printf(" drive %d: <%s, %s, %s> type %d %s %s", 337 sa->sa_sc_link->scsipi_atapi.drive,inqbuf->vendor, 338 inqbuf->product, inqbuf->revision, inqbuf->type, dtype, 339 inqbuf->removable ? "removable" : "fixed"); 340 return (UNCONF); 341 } 342