1 /* $NetBSD: atapiconf.c,v 1.90 2016/11/29 03:23:00 mlelstv Exp $ */ 2 3 /* 4 * Copyright (c) 1996, 2001 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 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __KERNEL_RCSID(0, "$NetBSD: atapiconf.c,v 1.90 2016/11/29 03:23:00 mlelstv Exp $"); 29 30 #include <sys/param.h> 31 #include <sys/systm.h> 32 #include <sys/malloc.h> 33 #include <sys/device.h> 34 #include <sys/buf.h> 35 #include <sys/proc.h> 36 #include <sys/kthread.h> 37 #include <sys/atomic.h> 38 39 #include <dev/scsipi/scsipi_all.h> 40 #include <dev/scsipi/scsipiconf.h> 41 #include <dev/scsipi/atapiconf.h> 42 43 #include "locators.h" 44 45 #define SILENT_PRINTF(flags,string) if (!(flags & A_SILENT)) printf string 46 #define MAX_TARGET 1 47 48 const struct scsipi_periphsw atapi_probe_periphsw = { 49 NULL, 50 NULL, 51 NULL, 52 NULL, 53 }; 54 55 static int atapibusmatch(device_t, cfdata_t, void *); 56 static void atapibusattach(device_t, device_t, void *); 57 static int atapibusdetach(device_t, int flags); 58 static void atapibuschilddet(device_t, device_t); 59 60 static int atapibussubmatch(device_t, cfdata_t, const int *, void *); 61 62 static int atapi_probe_bus(struct atapibus_softc *, int); 63 64 static int atapibusprint(void *, const char *); 65 66 CFATTACH_DECL3_NEW(atapibus, sizeof(struct atapibus_softc), 67 atapibusmatch, atapibusattach, atapibusdetach, NULL, NULL, 68 atapibuschilddet, DVF_DETACH_SHUTDOWN); 69 70 extern struct cfdriver atapibus_cd; 71 72 static const struct scsi_quirk_inquiry_pattern atapi_quirk_patterns[] = { 73 {{T_CDROM, T_REMOV, 74 "ALPS ELECTRIC CO.,LTD. DC544C", "", "SW03D"}, PQUIRK_NOTUR}, 75 {{T_CDROM, T_REMOV, 76 "CR-2801TE", "", "1.07"}, PQUIRK_NOSENSE}, 77 {{T_CDROM, T_REMOV, 78 "CREATIVECD3630E", "", "AC101"}, PQUIRK_NOSENSE}, 79 {{T_CDROM, T_REMOV, 80 "FX320S", "", "q01"}, PQUIRK_NOSENSE}, 81 {{T_CDROM, T_REMOV, 82 "GCD-R580B", "", "1.00"}, PQUIRK_LITTLETOC}, 83 {{T_CDROM, T_REMOV, 84 "HITACHI CDR-7730", "", "0008a"}, PQUIRK_NOSENSE}, 85 {{T_CDROM, T_REMOV, 86 "MATSHITA CR-574", "", "1.02"}, PQUIRK_NOCAPACITY}, 87 {{T_CDROM, T_REMOV, 88 "MATSHITA CR-574", "", "1.06"}, PQUIRK_NOCAPACITY}, 89 {{T_CDROM, T_REMOV, 90 "Memorex CRW-2642", "", "1.0g"}, PQUIRK_NOSENSE}, 91 {{T_CDROM, T_REMOV, 92 "NEC CD-ROM DRIVE:273", "", "4.21"}, PQUIRK_NOTUR}, 93 {{T_CDROM, T_REMOV, 94 "SANYO CRD-256P", "", "1.02"}, PQUIRK_NOCAPACITY}, 95 {{T_CDROM, T_REMOV, 96 "SANYO CRD-254P", "", "1.02"}, PQUIRK_NOCAPACITY}, 97 {{T_CDROM, T_REMOV, 98 "SANYO CRD-S54P", "", "1.08"}, PQUIRK_NOCAPACITY}, 99 {{T_CDROM, T_REMOV, 100 "CD-ROM CDR-S1", "", "1.70"}, PQUIRK_NOCAPACITY}, /* Sanyo */ 101 {{T_CDROM, T_REMOV, 102 "CD-ROM CDR-N16", "", "1.25"}, PQUIRK_NOCAPACITY}, /* Sanyo */ 103 }; 104 105 int 106 atapiprint(void *aux, const char *pnp) 107 { 108 if (pnp) 109 aprint_normal("atapibus at %s", pnp); 110 return (UNCONF); 111 } 112 113 static int 114 atapibusmatch(device_t parent, cfdata_t cf, void *aux) 115 { 116 struct scsipi_channel *chan = aux; 117 118 if (chan == NULL) 119 return (0); 120 121 if (SCSIPI_BUSTYPE_TYPE(chan->chan_bustype->bustype_type) != 122 SCSIPI_BUSTYPE_ATAPI) 123 return (0); 124 125 return (1); 126 } 127 128 static int 129 atapibussubmatch(device_t parent, cfdata_t cf, const int *ldesc, void *aux) 130 { 131 struct scsipibus_attach_args *sa = aux; 132 struct scsipi_periph *periph = sa->sa_periph; 133 134 if (cf->cf_loc[ATAPIBUSCF_DRIVE] != ATAPIBUSCF_DRIVE_DEFAULT && 135 cf->cf_loc[ATAPIBUSCF_DRIVE] != periph->periph_target) 136 return (0); 137 return (config_match(parent, cf, aux)); 138 } 139 140 static void 141 atapibusattach(device_t parent, device_t self, void *aux) 142 { 143 struct atapibus_softc *sc = device_private(self); 144 struct scsipi_channel *chan = aux; 145 146 sc->sc_channel = chan; 147 sc->sc_dev = self; 148 149 chan->chan_name = device_xname(sc->sc_dev); 150 151 /* ATAPI has no LUNs. */ 152 chan->chan_nluns = 1; 153 aprint_naive("\n"); 154 aprint_normal(": %d targets\n", chan->chan_ntargets); 155 156 if (atomic_inc_uint_nv(&chan_running(chan)) == 1) 157 mutex_init(chan_mtx(chan), MUTEX_DEFAULT, IPL_BIO); 158 159 cv_init(&chan->chan_cv_thr, "scshut"); 160 cv_init(&chan->chan_cv_comp, "sccomp"); 161 cv_init(&chan->chan_cv_xs, "xscmd"); 162 163 /* Initialize the channel. */ 164 chan->chan_init_cb = NULL; 165 chan->chan_init_cb_arg = NULL; 166 scsipi_channel_init(chan); 167 168 if (!pmf_device_register(self, NULL, NULL)) 169 aprint_error_dev(self, "couldn't establish power handler\n"); 170 171 /* Probe the bus for devices. */ 172 atapi_probe_bus(sc, -1); 173 } 174 175 static void 176 atapibuschilddet(device_t self, device_t child) 177 { 178 struct atapibus_softc *sc = device_private(self); 179 struct scsipi_channel *chan = sc->sc_channel; 180 struct scsipi_periph *periph; 181 int target; 182 183 mutex_enter(chan_mtx(chan)); 184 for (target = 0; target < chan->chan_ntargets; target++) { 185 periph = scsipi_lookup_periph_locked(chan, target, 0); 186 if (periph == NULL || periph->periph_dev != child) 187 continue; 188 scsipi_remove_periph(chan, periph); 189 scsipi_free_periph(periph); 190 break; 191 } 192 mutex_exit(chan_mtx(chan)); 193 } 194 195 static int 196 atapibusdetach(device_t self, int flags) 197 { 198 struct atapibus_softc *sc = device_private(self); 199 struct scsipi_channel *chan = sc->sc_channel; 200 struct scsipi_periph *periph; 201 int target, error = 0; 202 203 /* 204 * Shut down the channel. 205 */ 206 scsipi_channel_shutdown(chan); 207 208 /* for config_detach() */ 209 KERNEL_LOCK(1, curlwp); 210 211 /* 212 * Now detach all of the periphs. 213 */ 214 mutex_enter(chan_mtx(chan)); 215 for (target = 0; target < chan->chan_ntargets; target++) { 216 periph = scsipi_lookup_periph_locked(chan, target, 0); 217 if (periph == NULL) 218 continue; 219 error = config_detach(periph->periph_dev, flags); 220 if (error) { 221 mutex_exit(chan_mtx(chan)); 222 goto out; 223 } 224 KASSERT(scsipi_lookup_periph(chan, target, 0) == NULL); 225 } 226 mutex_exit(chan_mtx(chan)); 227 228 cv_destroy(&chan->chan_cv_xs); 229 cv_destroy(&chan->chan_cv_comp); 230 cv_destroy(&chan->chan_cv_thr); 231 232 if (atomic_dec_uint_nv(&chan_running(chan)) == 0) 233 mutex_destroy(chan_mtx(chan)); 234 235 out: 236 /* XXXSMP scsipi */ 237 KERNEL_UNLOCK_ONE(curlwp); 238 return error; 239 } 240 241 static int 242 atapi_probe_bus(struct atapibus_softc *sc, int target) 243 { 244 struct scsipi_channel *chan = sc->sc_channel; 245 int maxtarget, mintarget; 246 int error; 247 struct atapi_adapter *atapi_adapter; 248 249 KASSERT(chan->chan_ntargets >= 1); 250 251 if (target == -1) { 252 maxtarget = chan->chan_ntargets - 1; 253 mintarget = 0; 254 } else { 255 if (target < 0 || target >= chan->chan_ntargets) 256 return (ENXIO); 257 maxtarget = mintarget = target; 258 } 259 260 if ((error = scsipi_adapter_addref(chan->chan_adapter)) != 0) 261 return (error); 262 atapi_adapter = (struct atapi_adapter*)chan->chan_adapter; 263 for (target = mintarget; target <= maxtarget; target++) 264 atapi_adapter->atapi_probe_device(sc, target); 265 scsipi_adapter_delref(chan->chan_adapter); 266 return (0); 267 } 268 269 void * 270 atapi_probe_device(struct atapibus_softc *sc, int target, 271 struct scsipi_periph *periph, struct scsipibus_attach_args *sa) 272 { 273 struct scsipi_channel *chan = sc->sc_channel; 274 const struct scsi_quirk_inquiry_pattern *finger; 275 cfdata_t cf; 276 int priority, quirks; 277 278 finger = scsipi_inqmatch( 279 &sa->sa_inqbuf, (const void *)atapi_quirk_patterns, 280 sizeof(atapi_quirk_patterns) / 281 sizeof(atapi_quirk_patterns[0]), 282 sizeof(atapi_quirk_patterns[0]), &priority); 283 284 if (finger != NULL) 285 quirks = finger->quirks; 286 else 287 quirks = 0; 288 289 /* 290 * Now apply any quirks from the table. 291 */ 292 periph->periph_quirks |= quirks; 293 294 if ((cf = config_search_ia(atapibussubmatch, sc->sc_dev, 295 "atapibus", sa)) != 0) { 296 scsipi_insert_periph(chan, periph); 297 /* 298 * XXX Can't assign periph_dev here, because we'll 299 * XXX need it before config_attach() returns. Must 300 * XXX assign it in periph driver. 301 */ 302 return config_attach(sc->sc_dev, cf, sa, 303 atapibusprint); 304 } else { 305 atapibusprint(sa, device_xname(sc->sc_dev)); 306 aprint_normal(" not configured\n"); 307 scsipi_free_periph(periph); 308 return NULL; 309 } 310 } 311 312 static int 313 atapibusprint(void *aux, const char *pnp) 314 { 315 struct scsipibus_attach_args *sa = aux; 316 struct scsipi_inquiry_pattern *inqbuf; 317 const char *dtype; 318 319 if (pnp != NULL) 320 aprint_normal("%s", pnp); 321 322 inqbuf = &sa->sa_inqbuf; 323 324 dtype = scsipi_dtype(inqbuf->type & SID_TYPE); 325 aprint_normal(" drive %d: <%s, %s, %s> %s %s", 326 sa->sa_periph->periph_target, inqbuf->vendor, 327 inqbuf->product, inqbuf->revision, dtype, 328 inqbuf->removable ? "removable" : "fixed"); 329 return (UNCONF); 330 } 331