Home | History | Annotate | Line # | Download | only in scsipi
atapiconf.c revision 1.28.2.9
      1 /*	$NetBSD: atapiconf.c,v 1.28.2.9 2001/03/27 15:32:16 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 #include <sys/kthread.h>
     40 
     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 const struct scsipi_periphsw atapi_probe_periphsw = {
     53 	NULL,
     54 	NULL,
     55 	NULL,
     56 	NULL,
     57 };
     58 
     59 int	atapibusmatch __P((struct device *, struct cfdata *, void *));
     60 void	atapibusattach __P((struct device *, struct device *, void *));
     61 int	atapibusactivate __P((struct device *, enum devact));
     62 int	atapibusdetach __P((struct device *, int flags));
     63 
     64 int	atapibussubmatch __P((struct device *, struct cfdata *, void *));
     65 
     66 int	atapi_probe_bus __P((struct atapibus_softc *, int));
     67 
     68 struct cfattach atapibus_ca = {
     69 	sizeof(struct atapibus_softc), atapibusmatch, atapibusattach,
     70 	atapibusdetach, atapibusactivate,
     71 };
     72 
     73 extern struct cfdriver atapibus_cd;
     74 
     75 int atapibusprint __P((void *, const char *));
     76 
     77 const struct scsi_quirk_inquiry_pattern atapi_quirk_patterns[] = {
     78 	{{T_CDROM, T_REMOV,
     79 	 "ALPS ELECTRIC CO.,LTD. DC544C", "", "SW03D"},	PQUIRK_NOTUR},
     80 	{{T_CDROM, T_REMOV,
     81 	 "BCD-16X 1997-04-25", "", "VER 2.2"},	PQUIRK_NOSTARTUNIT},
     82 	{{T_CDROM, T_REMOV,
     83 	 "BCD-24X 1997-06-27", "", "VER 2.0"},	PQUIRK_NOSTARTUNIT},
     84 	{{T_CDROM, T_REMOV,
     85 	 "CR-2801TE", "", "1.07"},		PQUIRK_NOSENSE},
     86 	{{T_CDROM, T_REMOV,
     87 	 "CREATIVECD3630E", "", "AC101"},	PQUIRK_NOSENSE},
     88 	{{T_CDROM, T_REMOV,
     89 	 "FX320S", "", "q01"},			PQUIRK_NOSENSE},
     90 	{{T_CDROM, T_REMOV,
     91 	 "GCD-R580B", "", "1.00"},		PQUIRK_LITTLETOC},
     92 	{{T_CDROM, T_REMOV,
     93 	 "HITACHI CDR-7730", "", "0008a"},      PQUIRK_NOSENSE},
     94 	{{T_CDROM, T_REMOV,
     95 	 "MATSHITA CR-574", "", "1.02"},	PQUIRK_NOCAPACITY},
     96 	{{T_CDROM, T_REMOV,
     97 	 "MATSHITA CR-574", "", "1.06"},	PQUIRK_NOCAPACITY},
     98 	{{T_CDROM, T_REMOV,
     99 	 "Memorex CRW-2642", "", "1.0g"},	PQUIRK_NOSENSE},
    100 	{{T_CDROM, T_REMOV,
    101 	 "NEC                 CD-ROM DRIVE:273", "", "4.21"}, PQUIRK_NOTUR},
    102 	{{T_CDROM, T_REMOV,
    103 	 "SANYO CRD-256P", "", "1.02"},		PQUIRK_NOCAPACITY},
    104 	{{T_CDROM, T_REMOV,
    105 	 "SANYO CRD-254P", "", "1.02"},		PQUIRK_NOCAPACITY},
    106 	{{T_CDROM, T_REMOV,
    107 	 "SANYO CRD-S54P", "", "1.08"},		PQUIRK_NOCAPACITY},
    108 	{{T_CDROM, T_REMOV,
    109 	 "CD-ROM  CDR-S1", "", "1.70"},		PQUIRK_NOCAPACITY}, /* Sanyo */
    110 	{{T_CDROM, T_REMOV,
    111 	 "CD-ROM  CDR-N16", "", "1.25"},	PQUIRK_NOCAPACITY}, /* Sanyo */
    112 	{{T_CDROM, T_REMOV,
    113 	 "UJDCD8730", "", "1.14"},		PQUIRK_NODOORLOCK}, /* Acer */
    114 	{{T_DIRECT, T_REMOV,		/* Panasonic MultiMediaCard */
    115 	  "04DA", "1B00", "0010"},		PQUIRK_BYTE5_ZERO |
    116 	 					PQUIRK_NO_FLEX_PAGE },
    117 };
    118 
    119 int
    120 atapibusmatch(parent, cf, aux)
    121 	struct device *parent;
    122 	struct cfdata *cf;
    123 	void *aux;
    124 {
    125 	struct ata_atapi_attach *aa = aux;
    126 
    127 	if (aa == NULL)
    128 		return (0);
    129 
    130 	if (aa->aa_type != T_ATAPI)
    131 		return (0);
    132 
    133 	if (cf->cf_loc[ATAPICF_CHANNEL] != aa->aa_channel &&
    134 	    cf->cf_loc[ATAPICF_CHANNEL] != ATAPICF_CHANNEL_DEFAULT)
    135 		return (0);
    136 
    137 	return (1);
    138 }
    139 
    140 int
    141 atapibussubmatch(parent, cf, aux)
    142 	struct device *parent;
    143 	struct cfdata *cf;
    144 	void *aux;
    145 {
    146 	struct scsipibus_attach_args *sa = aux;
    147 	struct scsipi_periph *periph = sa->sa_periph;
    148 
    149 	if (cf->cf_loc[ATAPIBUSCF_DRIVE] != ATAPIBUSCF_DRIVE_DEFAULT &&
    150 	    cf->cf_loc[ATAPIBUSCF_DRIVE] != periph->periph_target)
    151 		return (0);
    152 	return ((*cf->cf_attach->ca_match)(parent, cf, aux));
    153 }
    154 
    155 void
    156 atapibusattach(parent, self, aux)
    157 	struct device *parent, *self;
    158 	void *aux;
    159 {
    160 	struct atapibus_softc *sc = (void *) self;
    161 	struct ata_atapi_attach *aa = aux;
    162 	struct scsipi_channel *chan = aa->aa_bus_private;
    163 
    164 	sc->sc_channel = chan;
    165 	sc->sc_drvs = aa->aa_drv_data;
    166 
    167 	/* ATAPI has no LUNs. */
    168 	chan->chan_nluns = 1;
    169 	printf(": %d targets\n", chan->chan_ntargets);
    170 
    171 	/* Initialize the channel. */
    172 	scsipi_channel_init(chan);
    173 
    174 	/* Probe the bus for devices. */
    175 	atapi_probe_bus(sc, -1);
    176 }
    177 
    178 int
    179 atapibusactivate(self, act)
    180 	struct device *self;
    181 	enum devact act;
    182 {
    183 	struct atapibus_softc *sc = (void *) self;
    184 	struct scsipi_channel *chan = sc->sc_channel;
    185 	struct scsipi_periph *periph;
    186 	int target, error = 0, s;
    187 
    188 	s = splbio();
    189 	switch (act) {
    190 	case DVACT_ACTIVATE:
    191 		error = EOPNOTSUPP;
    192 		break;
    193 
    194 	case DVACT_DEACTIVATE:
    195 		for (target = 0; target < chan->chan_ntargets; target++) {
    196 			periph = scsipi_lookup_periph(chan, target, 0);
    197 			if (periph == NULL)
    198 				continue;
    199 			error = config_deactivate(periph->periph_dev);
    200 			if (error)
    201 				goto out;
    202 		}
    203 		break;
    204 	}
    205  out:
    206 	splx(s);
    207 	return (error);
    208 }
    209 
    210 int
    211 atapibusdetach(self, flags)
    212 	struct device *self;
    213 	int flags;
    214 {
    215 	struct atapibus_softc *sc = (void *)self;
    216 	struct scsipi_channel *chan = sc->sc_channel;
    217 	struct scsipi_periph *periph;
    218 	int target, error;
    219 
    220 	/*
    221 	 * Shut down the channel.
    222 	 */
    223 	scsipi_channel_shutdown(chan);
    224 
    225 	/*
    226 	 * Now detach all of the periphs.
    227 	 */
    228 	for (target = 0; target < chan->chan_ntargets; target++) {
    229 		periph = scsipi_lookup_periph(chan, target, 0);
    230 		if (periph == NULL)
    231 			continue;
    232 		error = config_detach(periph->periph_dev, flags);
    233 		if (error)
    234 			return (error);
    235 
    236 		/*
    237 		 * We have successfully detached the child.  Drop the
    238 		 * direct reference for the child so that wdcdetach
    239 		 * won't call detach routine twice.
    240 		 */
    241 #ifdef DIAGNOSTIC
    242 		if (periph->periph_dev != sc->sc_drvs[target].drv_softc)
    243 			panic("softc mismatch");
    244 #endif
    245 		sc->sc_drvs[target].drv_softc = NULL;
    246 
    247 		scsipi_remove_periph(chan, periph);
    248 		free(periph, M_DEVBUF);
    249 	}
    250 	return (0);
    251 }
    252 
    253 int
    254 atapi_probe_bus(sc, target)
    255 	struct atapibus_softc *sc;
    256 	int target;
    257 {
    258 	struct scsipi_channel *chan = sc->sc_channel;
    259 	int maxtarget, mintarget;
    260 	int error;
    261 	struct atapi_adapter *atapi_adapter;
    262 
    263 	if (target == -1) {
    264 		maxtarget = 1;
    265 		mintarget = 0;
    266 	} else {
    267 		if (target < 0 || target >= chan->chan_ntargets)
    268 			return (ENXIO);
    269 		maxtarget = mintarget = target;
    270 	}
    271 
    272 	if ((error = scsipi_adapter_addref(chan->chan_adapter)) != 0)
    273 		return (error);
    274 	atapi_adapter = (struct atapi_adapter*)chan->chan_adapter;
    275 	for (target = mintarget; target <= maxtarget; target++)
    276 		atapi_adapter->atapi_probe_device(sc, target);
    277 	scsipi_adapter_delref(chan->chan_adapter);
    278 	return (0);
    279 }
    280 
    281 void *
    282 atapi_probe_device(sc, target, periph, sa)
    283 	struct atapibus_softc *sc;
    284 	int target;
    285 	struct scsipi_periph *periph;
    286 	struct scsipibus_attach_args *sa;
    287 {
    288 	struct scsipi_channel *chan = sc->sc_channel;
    289 	struct scsi_quirk_inquiry_pattern *finger;
    290 	struct cfdata *cf;
    291 	int priority, quirks;
    292 
    293 	finger = (struct scsi_quirk_inquiry_pattern *)scsipi_inqmatch(
    294 	    &sa->sa_inqbuf, (caddr_t)atapi_quirk_patterns,
    295 	    sizeof(atapi_quirk_patterns) /
    296 	        sizeof(atapi_quirk_patterns[0]),
    297 	    sizeof(atapi_quirk_patterns[0]), &priority);
    298 
    299 	if (finger != NULL)
    300 		quirks = finger->quirks;
    301 	else
    302 		quirks = 0;
    303 
    304 	/*
    305 	 * Now apply any quirks from the table.
    306 	 */
    307 	periph->periph_quirks |= quirks;
    308 
    309 	if ((cf = config_search(atapibussubmatch, &sc->sc_dev,
    310 	    sa)) != 0) {
    311 		scsipi_insert_periph(chan, periph);
    312 		/*
    313 		 * XXX Can't assign periph_dev here, because we'll
    314 		 * XXX need it before config_attach() returns.  Must
    315 		 * XXX assign it in periph driver.
    316 		 */
    317 		return config_attach(&sc->sc_dev, cf, sa,
    318 		    atapibusprint);
    319 	} else {
    320 		atapibusprint(&sa, sc->sc_dev.dv_xname);
    321 		printf(" not configured\n");
    322 		free(periph, M_DEVBUF);
    323 		return NULL;
    324 	}
    325 }
    326 
    327 int
    328 atapibusprint(aux, pnp)
    329 	void *aux;
    330 	const char *pnp;
    331 {
    332 	struct scsipibus_attach_args *sa = aux;
    333 	struct scsipi_inquiry_pattern *inqbuf;
    334 	char *dtype;
    335 
    336 	if (pnp != NULL)
    337 		printf("%s", pnp);
    338 
    339 	inqbuf = &sa->sa_inqbuf;
    340 
    341 	dtype = scsipi_dtype(inqbuf->type & SID_TYPE);
    342 	printf(" drive %d: <%s, %s, %s> type %d %s %s",
    343 	    sa->sa_periph->periph_target ,inqbuf->vendor,
    344 	    inqbuf->product, inqbuf->revision, inqbuf->type, dtype,
    345 	    inqbuf->removable ? "removable" : "fixed");
    346 	return (UNCONF);
    347 }
    348