Home | History | Annotate | Line # | Download | only in scsipi
atapiconf.c revision 1.34
      1 /*	$NetBSD: atapiconf.c,v 1.34 2000/04/01 14:32:26 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/atapiconf.h>
     45 
     46 #include "locators.h"
     47 
     48 #define SILENT_PRINTF(flags,string) if (!(flags & A_SILENT)) printf string
     49 #define MAX_TARGET 1
     50 
     51 int	atapibusmatch __P((struct device *, struct cfdata *, void *));
     52 int	atapibussubmatch __P((struct device *, struct cfdata *, void *));
     53 void	atapibusattach __P((struct device *, struct device *, void *));
     54 int	atapibusactivate __P((struct device *, enum devact));
     55 int	atapibusdetach __P((struct device *, int flags));
     56 
     57 int	atapi_probe_bus __P((int, int));
     58 
     59 struct cfattach atapibus_ca = {
     60 	sizeof(struct atapibus_softc), atapibusmatch, atapibusattach,
     61 	atapibusdetach, atapibusactivate,
     62 };
     63 
     64 extern struct cfdriver atapibus_cd;
     65 
     66 int atapibusprint __P((void *, const char *));
     67 
     68 struct scsi_quirk_inquiry_pattern atapi_quirk_patterns[] = {
     69 	{{T_CDROM, T_REMOV,
     70 	 "ALPS ELECTRIC CO.,LTD. DC544C", "", "SW03D"},	ADEV_NOTUR},
     71 	{{T_CDROM, T_REMOV,
     72 	 "BCD-16X 1997-04-25", "", "VER 2.2"},	SDEV_NOSTARTUNIT},
     73 	{{T_CDROM, T_REMOV,
     74 	 "BCD-24X 1997-06-27", "", "VER 2.0"},	SDEV_NOSTARTUNIT},
     75 	{{T_CDROM, T_REMOV,
     76 	 "CR-2801TE", "", "1.07"},		ADEV_NOSENSE},
     77 	{{T_CDROM, T_REMOV,
     78 	 "CREATIVECD3630E", "", "AC101"},	ADEV_NOSENSE},
     79 	{{T_CDROM, T_REMOV,
     80 	 "FX320S", "", "q01"},			ADEV_NOSENSE},
     81 	{{T_CDROM, T_REMOV,
     82 	 "GCD-R580B", "", "1.00"},		ADEV_LITTLETOC},
     83 	{{T_CDROM, T_REMOV,
     84 	 "HITACHI CDR-7730", "", "0008a"},	ADEV_NOSENSE},
     85 	{{T_CDROM, T_REMOV,
     86 	 "MATSHITA CR-574", "", "1.02"},	ADEV_NOCAPACITY},
     87 	{{T_CDROM, T_REMOV,
     88 	 "MATSHITA CR-574", "", "1.06"},	ADEV_NOCAPACITY},
     89 	{{T_CDROM, T_REMOV,
     90 	 "Memorex CRW-2642", "", "1.0g"},	ADEV_NOSENSE},
     91 	{{T_CDROM, T_REMOV,
     92 	 "NEC                 CD-ROM DRIVE:273", "", "4.21"}, ADEV_NOTUR},
     93 	{{T_CDROM, T_REMOV,
     94 	 "SANYO CRD-256P", "", "1.02"},		ADEV_NOCAPACITY},
     95 	{{T_CDROM, T_REMOV,
     96 	 "SANYO CRD-254P", "", "1.02"},		ADEV_NOCAPACITY},
     97 	{{T_CDROM, T_REMOV,
     98 	 "SANYO CRD-S54P", "", "1.08"},		ADEV_NOCAPACITY},
     99 	{{T_CDROM, T_REMOV,
    100 	 "CD-ROM  CDR-S1", "", "1.70"},		ADEV_NOCAPACITY}, /* Sanyo */
    101 	{{T_CDROM, T_REMOV,
    102 	 "CD-ROM  CDR-N16", "", "1.25"},	ADEV_NOCAPACITY}, /* Sanyo */
    103 	{{T_CDROM, T_REMOV,
    104 	 "UJDCD8730", "", "1.14"},		ADEV_NODOORLOCK}, /* Acer */
    105 };
    106 
    107 int
    108 atapibusmatch(parent, cf, aux)
    109 	struct device *parent;
    110 	struct cfdata *cf;
    111 	void *aux;
    112 {
    113 	struct ata_atapi_attach *aa_link = aux;
    114 
    115 	if (aa_link == NULL)
    116 		return (0);
    117 	if (aa_link->aa_type != T_ATAPI)
    118 		return (0);
    119 	if (cf->cf_loc[ATAPICF_CHANNEL] != aa_link->aa_channel &&
    120 	    cf->cf_loc[ATAPICF_CHANNEL] != ATAPICF_CHANNEL_DEFAULT)
    121 	    return 0;
    122 	return (1);
    123 }
    124 
    125 int
    126 atapibussubmatch(parent, cf, aux)
    127 	struct device *parent;
    128 	struct cfdata *cf;
    129 	void *aux;
    130 {
    131 	struct scsipibus_attach_args *sa = aux;
    132 	struct scsipi_link *sc_link = sa->sa_sc_link;
    133 
    134 	if (cf->cf_loc[ATAPIBUSCF_DRIVE] != ATAPIBUSCF_DRIVE_DEFAULT &&
    135 	    cf->cf_loc[ATAPIBUSCF_DRIVE] != sc_link->scsipi_atapi.drive)
    136 		return (0);
    137 	return ((*cf->cf_attach->ca_match)(parent, cf, aux));
    138 }
    139 
    140 void
    141 atapibusattach(parent, self, aux)
    142 	struct device *parent, *self;
    143 	void *aux;
    144 {
    145 	struct atapibus_softc *sc_ab = (struct atapibus_softc *)self;
    146 	struct ata_atapi_attach *aa_link = aux;
    147 	struct scsipi_link *sc_link_proto;
    148 	int nbytes;
    149 
    150 	printf("\n");
    151 
    152 	/* Initialize shared data. */
    153 	scsipi_init();
    154 
    155 	sc_link_proto = malloc(sizeof(struct scsipi_link),
    156 	    M_DEVBUF, M_NOWAIT);
    157 	if (sc_link_proto == NULL)
    158 		panic("atapibusattach : can't allocate scsipi link proto\n");
    159 	memset(sc_link_proto, 0, sizeof(struct scsipi_link));
    160 
    161 	sc_link_proto->type = BUS_ATAPI;
    162 	sc_link_proto->openings = aa_link->aa_openings;
    163 	sc_link_proto->scsipi_atapi.channel = aa_link->aa_channel;
    164 	sc_link_proto->adapter_softc = parent;
    165 	sc_link_proto->adapter = aa_link->aa_bus_private;
    166 	sc_link_proto->scsipi_atapi.atapibus = sc_ab->sc_dev.dv_unit;
    167 	sc_link_proto->scsipi_cmd = atapi_scsipi_cmd;
    168 	sc_link_proto->scsipi_interpret_sense = atapi_interpret_sense;
    169 	sc_link_proto->sc_print_addr = atapi_print_addr;
    170 	sc_link_proto->scsipi_kill_pending = atapi_kill_pending;
    171 
    172 
    173 	sc_ab->adapter_link = sc_link_proto;
    174 	sc_ab->sc_drvs = aa_link->aa_drv_data;
    175 
    176 	nbytes = 2 * sizeof(struct scsipi_link **);
    177 	sc_ab->sc_link = (struct scsipi_link **)malloc(nbytes, M_DEVBUF,
    178 	    M_NOWAIT);
    179 	if (sc_ab->sc_link == NULL)
    180 		panic("scsibusattach: can't allocate target links");
    181 	memset(sc_ab->sc_link, 0, nbytes);
    182 	atapi_probe_bus(sc_ab->sc_dev.dv_unit, -1);
    183 }
    184 
    185 int
    186 atapibusactivate(self, act)
    187 	struct device *self;
    188 	enum devact act;
    189 {
    190 	struct atapibus_softc *sc = (struct atapibus_softc *)self;
    191 	struct scsipi_link *sc_link;
    192 	int target, error = 0, s;
    193 
    194 	s = splbio();
    195 	switch (act) {
    196 	case DVACT_ACTIVATE:
    197 		error = EOPNOTSUPP;
    198 		break;
    199 
    200 	case DVACT_DEACTIVATE:
    201 		for (target = 0; target <= MAX_TARGET; target++) {
    202 			sc_link = sc->sc_link[target];
    203 			if (sc_link == NULL)
    204 				continue;
    205 			error = config_deactivate(sc_link->device_softc);
    206 			if (error != 0)
    207 				goto out;
    208 		}
    209 		break;
    210 	}
    211  out:
    212 	splx(s);
    213 	return (error);
    214 }
    215 
    216 int
    217 atapibusdetach(self, flags)
    218 	struct device *self;
    219 	int flags;
    220 {
    221 	struct atapibus_softc *sc = (struct atapibus_softc *)self;
    222 	struct scsipi_link *sc_link;
    223 	int target, error;
    224 
    225 	for (target = 0; target <= MAX_TARGET; target++) {
    226 		sc_link = sc->sc_link[target];
    227 		if (sc_link == NULL)
    228 			continue;
    229 		error = config_detach(sc_link->device_softc, flags);
    230 		if (error != 0)
    231 			return (error);
    232 
    233 		/*
    234 		 * We have successfully detached the child.  Drop the
    235 		 * direct reference for the child so that wdcdetach
    236 		 * won't call detach routine twice.
    237 		 */
    238 #ifdef DIAGNOSTIC
    239 		if (sc_link->device_softc != sc->sc_drvs[target].drv_softc)
    240 			panic("softc mismatch");
    241 #endif
    242 		sc->sc_drvs[target].drv_softc = NULL;
    243 
    244 		free(sc_link, M_DEVBUF);
    245 		sc->sc_link[target] = NULL;
    246 	}
    247 	return (0);
    248 }
    249 
    250 int
    251 atapi_probe_bus(bus, target)
    252 	int bus, target;
    253 {
    254 	int maxtarget, mintarget;
    255 	struct atapibus_softc *atapi;
    256 	int error;
    257 	struct atapi_adapter *atapi_adapter;
    258 
    259 	if (bus < 0 || bus >= atapibus_cd.cd_ndevs)
    260 		return (ENXIO);
    261 	atapi = atapibus_cd.cd_devs[bus];
    262 	if (atapi == NULL)
    263 		return (ENXIO);
    264 
    265 	if (target == -1) {
    266 		maxtarget = 1;
    267 		mintarget = 0;
    268 	} else {
    269 		if (target < 0 || target > 1)
    270 			return (ENXIO);
    271 		maxtarget = mintarget = target;
    272 	}
    273 	if ((error = scsipi_adapter_addref(atapi->adapter_link)) != 0)
    274 		return (error);
    275 	atapi_adapter = (struct atapi_adapter*)atapi->adapter_link->adapter;
    276 	for (target = mintarget; target <= maxtarget; target++)
    277 		atapi_adapter->atapi_probedev(atapi, target);
    278 	scsipi_adapter_delref(atapi->adapter_link);
    279 	return (0);
    280 }
    281 
    282 void *
    283 atapi_probedev(atapi, target, sc_link, sa)
    284 	struct atapibus_softc *atapi;
    285 	int target;
    286 	struct scsipi_link *sc_link;
    287 	struct scsipibus_attach_args *sa;
    288 {
    289 	struct cfdata *cf;
    290 	struct scsi_quirk_inquiry_pattern *finger;
    291 	int priority;
    292 
    293 	/* Fill generic parts of the link. */
    294 	sc_link->active = 0;
    295 	sc_link->scsipi_atapi.drive = target;
    296 	sc_link->device = NULL;
    297 	TAILQ_INIT(&sc_link->pending_xfers);
    298 #if defined(SCSIDEBUG) && DEBUGTYPE == BUS_ATAPI
    299 	if (DEBUGTARGET == -1 || target == DEBUGTARGET)
    300 		sc_link->flags |= DEBUGLEVEL;
    301 #endif /* SCSIDEBUG */
    302 	finger = (struct scsi_quirk_inquiry_pattern *)scsipi_inqmatch(
    303 	    &sa->sa_inqbuf, (caddr_t)atapi_quirk_patterns,
    304 	    sizeof(atapi_quirk_patterns) /
    305 	        sizeof(atapi_quirk_patterns[0]),
    306 	    sizeof(atapi_quirk_patterns[0]), &priority);
    307 	if (priority != 0)
    308 		sc_link->quirks |= finger->quirks;
    309 
    310 	if ((cf = config_search(atapibussubmatch, &atapi->sc_dev,
    311 	    sa)) != 0) {
    312 		atapi->sc_link[target] = sc_link;
    313 		return (config_attach(&atapi->sc_dev, cf,
    314 		    sa, atapibusprint));
    315 	} else {
    316 		atapibusprint(sa, atapi->sc_dev.dv_xname);
    317 		printf(" not configured\n");
    318 		free(sc_link, M_DEVBUF);
    319 		return (NULL);
    320 	}
    321 }
    322 
    323 int
    324 atapibusprint(aux, pnp)
    325 	void *aux;
    326 	const char *pnp;
    327 {
    328 	struct scsipibus_attach_args *sa = aux;
    329 	struct scsipi_inquiry_pattern *inqbuf;
    330 	char *dtype;
    331 
    332 	if (pnp != NULL)
    333 		printf("%s", pnp);
    334 
    335 	inqbuf = &sa->sa_inqbuf;
    336 
    337 	dtype = scsipi_dtype(inqbuf->type & SID_TYPE);
    338 	printf(" drive %d: <%s, %s, %s> type %d %s %s",
    339 	    sa->sa_sc_link->scsipi_atapi.drive,inqbuf->vendor,
    340 	    inqbuf->product, inqbuf->revision, inqbuf->type, dtype,
    341 	    inqbuf->removable ? "removable" : "fixed");
    342 	return (UNCONF);
    343 }
    344