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