Home | History | Annotate | Line # | Download | only in scsipi
atapiconf.c revision 1.37
      1  1.37  augustss /*	$NetBSD: atapiconf.c,v 1.37 2001/03/20 22:39:51 augustss Exp $	*/
      2   1.2    bouyer 
      3   1.2    bouyer /*
      4   1.2    bouyer  * Copyright (c) 1996 Manuel Bouyer.  All rights reserved.
      5   1.2    bouyer  *
      6   1.2    bouyer  * Redistribution and use in source and binary forms, with or without
      7   1.2    bouyer  * modification, are permitted provided that the following conditions
      8   1.2    bouyer  * are met:
      9   1.2    bouyer  * 1. Redistributions of source code must retain the above copyright
     10   1.2    bouyer  *    notice, this list of conditions and the following disclaimer.
     11   1.2    bouyer  * 2. Redistributions in binary form must reproduce the above copyright
     12   1.2    bouyer  *    notice, this list of conditions and the following disclaimer in the
     13   1.2    bouyer  *    documentation and/or other materials provided with the distribution.
     14   1.2    bouyer  * 3. All advertising materials mentioning features or use of this software
     15   1.2    bouyer  *    must display the following acknowledgement:
     16   1.2    bouyer  *	This product includes software developed by Manuel Bouyer.
     17   1.2    bouyer  * 4. The name of the author may not be used to endorse or promote products
     18   1.2    bouyer  *    derived from this software without specific prior written permission.
     19   1.2    bouyer  *
     20   1.2    bouyer  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21   1.2    bouyer  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22   1.2    bouyer  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23   1.2    bouyer  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24   1.2    bouyer  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25   1.2    bouyer  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26   1.2    bouyer  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27   1.2    bouyer  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28   1.2    bouyer  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29   1.2    bouyer  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30   1.2    bouyer  */
     31   1.2    bouyer 
     32   1.2    bouyer #include <sys/types.h>
     33   1.2    bouyer #include <sys/param.h>
     34   1.2    bouyer #include <sys/systm.h>
     35   1.2    bouyer #include <sys/malloc.h>
     36   1.2    bouyer #include <sys/device.h>
     37   1.2    bouyer #include <sys/buf.h>
     38   1.4     enami #include <sys/proc.h>
     39   1.2    bouyer 
     40  1.13    bouyer #include <dev/ata/atareg.h>
     41  1.13    bouyer #include <dev/ata/atavar.h>
     42   1.2    bouyer #include <dev/scsipi/scsipi_all.h>
     43   1.9       cgd #include <dev/scsipi/atapi_all.h>
     44   1.2    bouyer #include <dev/scsipi/atapiconf.h>
     45   1.4     enami 
     46   1.2    bouyer #include "locators.h"
     47   1.2    bouyer 
     48   1.2    bouyer #define SILENT_PRINTF(flags,string) if (!(flags & A_SILENT)) printf string
     49  1.27     enami #define MAX_TARGET 1
     50   1.2    bouyer 
     51   1.2    bouyer int	atapibusmatch __P((struct device *, struct cfdata *, void *));
     52   1.2    bouyer int	atapibussubmatch __P((struct device *, struct cfdata *, void *));
     53   1.2    bouyer void	atapibusattach __P((struct device *, struct device *, void *));
     54  1.27     enami int	atapibusactivate __P((struct device *, enum devact));
     55  1.27     enami int	atapibusdetach __P((struct device *, int flags));
     56   1.2    bouyer 
     57   1.2    bouyer int	atapi_probe_bus __P((int, int));
     58   1.2    bouyer 
     59   1.2    bouyer struct cfattach atapibus_ca = {
     60  1.27     enami 	sizeof(struct atapibus_softc), atapibusmatch, atapibusattach,
     61  1.27     enami 	atapibusdetach, atapibusactivate,
     62   1.2    bouyer };
     63   1.2    bouyer 
     64   1.8   thorpej extern struct cfdriver atapibus_cd;
     65   1.2    bouyer 
     66   1.2    bouyer int atapibusprint __P((void *, const char *));
     67   1.2    bouyer 
     68  1.36  jdolecek const struct scsi_quirk_inquiry_pattern atapi_quirk_patterns[] = {
     69   1.2    bouyer 	{{T_CDROM, T_REMOV,
     70  1.21    bouyer 	 "ALPS ELECTRIC CO.,LTD. DC544C", "", "SW03D"},	ADEV_NOTUR},
     71   1.2    bouyer 	{{T_CDROM, T_REMOV,
     72  1.21    bouyer 	 "BCD-16X 1997-04-25", "", "VER 2.2"},	SDEV_NOSTARTUNIT},
     73  1.26    bouyer 	{{T_CDROM, T_REMOV,
     74  1.26    bouyer 	 "BCD-24X 1997-06-27", "", "VER 2.0"},	SDEV_NOSTARTUNIT},
     75  1.22    bouyer 	{{T_CDROM, T_REMOV,
     76  1.22    bouyer 	 "CR-2801TE", "", "1.07"},		ADEV_NOSENSE},
     77  1.23    bouyer 	{{T_CDROM, T_REMOV,
     78  1.23    bouyer 	 "CREATIVECD3630E", "", "AC101"},	ADEV_NOSENSE},
     79   1.2    bouyer 	{{T_CDROM, T_REMOV,
     80  1.21    bouyer 	 "FX320S", "", "q01"},			ADEV_NOSENSE},
     81  1.21    bouyer 	{{T_CDROM, T_REMOV,
     82  1.21    bouyer 	 "GCD-R580B", "", "1.00"},		ADEV_LITTLETOC},
     83  1.31  jdolecek 	{{T_CDROM, T_REMOV,
     84  1.31  jdolecek 	 "HITACHI CDR-7730", "", "0008a"},	ADEV_NOSENSE},
     85  1.24    bouyer 	{{T_CDROM, T_REMOV,
     86  1.21    bouyer 	 "MATSHITA CR-574", "", "1.02"},	ADEV_NOCAPACITY},
     87   1.3    bouyer 	{{T_CDROM, T_REMOV,
     88  1.21    bouyer 	 "MATSHITA CR-574", "", "1.06"},	ADEV_NOCAPACITY},
     89   1.7    bouyer 	{{T_CDROM, T_REMOV,
     90  1.21    bouyer 	 "Memorex CRW-2642", "", "1.0g"},	ADEV_NOSENSE},
     91   1.7    bouyer 	{{T_CDROM, T_REMOV,
     92  1.21    bouyer 	 "NEC                 CD-ROM DRIVE:273", "", "4.21"}, ADEV_NOTUR},
     93   1.2    bouyer 	{{T_CDROM, T_REMOV,
     94  1.21    bouyer 	 "SANYO CRD-256P", "", "1.02"},		ADEV_NOCAPACITY},
     95   1.2    bouyer 	{{T_CDROM, T_REMOV,
     96  1.21    bouyer 	 "SANYO CRD-254P", "", "1.02"},		ADEV_NOCAPACITY},
     97   1.3    bouyer 	{{T_CDROM, T_REMOV,
     98  1.21    bouyer 	 "SANYO CRD-S54P", "", "1.08"},		ADEV_NOCAPACITY},
     99   1.5    bouyer 	{{T_CDROM, T_REMOV,
    100  1.21    bouyer 	 "CD-ROM  CDR-S1", "", "1.70"},		ADEV_NOCAPACITY}, /* Sanyo */
    101  1.18    bouyer 	{{T_CDROM, T_REMOV,
    102  1.21    bouyer 	 "CD-ROM  CDR-N16", "", "1.25"},	ADEV_NOCAPACITY}, /* Sanyo */
    103  1.20    bouyer 	{{T_CDROM, T_REMOV,
    104  1.21    bouyer 	 "UJDCD8730", "", "1.14"},		ADEV_NODOORLOCK}, /* Acer */
    105  1.37  augustss 	{{T_DIRECT, T_REMOV,		/* Panasonic MultiMediaCard */
    106  1.37  augustss 	  "04DA", "1B00", "0010"},		ADEV_BYTE5_ZERO |
    107  1.37  augustss 	 					ADEV_NO_FLEX_PAGE },
    108   1.2    bouyer };
    109   1.2    bouyer 
    110   1.2    bouyer int
    111   1.2    bouyer atapibusmatch(parent, cf, aux)
    112   1.2    bouyer 	struct device *parent;
    113   1.2    bouyer 	struct cfdata *cf;
    114   1.2    bouyer 	void *aux;
    115   1.2    bouyer {
    116  1.13    bouyer 	struct ata_atapi_attach *aa_link = aux;
    117   1.2    bouyer 
    118  1.13    bouyer 	if (aa_link == NULL)
    119   1.4     enami 		return (0);
    120  1.13    bouyer 	if (aa_link->aa_type != T_ATAPI)
    121   1.4     enami 		return (0);
    122  1.13    bouyer 	if (cf->cf_loc[ATAPICF_CHANNEL] != aa_link->aa_channel &&
    123  1.13    bouyer 	    cf->cf_loc[ATAPICF_CHANNEL] != ATAPICF_CHANNEL_DEFAULT)
    124  1.13    bouyer 	    return 0;
    125   1.4     enami 	return (1);
    126   1.2    bouyer }
    127   1.2    bouyer 
    128   1.2    bouyer int
    129   1.2    bouyer atapibussubmatch(parent, cf, aux)
    130   1.2    bouyer 	struct device *parent;
    131   1.2    bouyer 	struct cfdata *cf;
    132   1.2    bouyer 	void *aux;
    133   1.2    bouyer {
    134   1.2    bouyer 	struct scsipibus_attach_args *sa = aux;
    135   1.2    bouyer 	struct scsipi_link *sc_link = sa->sa_sc_link;
    136   1.2    bouyer 
    137   1.2    bouyer 	if (cf->cf_loc[ATAPIBUSCF_DRIVE] != ATAPIBUSCF_DRIVE_DEFAULT &&
    138   1.4     enami 	    cf->cf_loc[ATAPIBUSCF_DRIVE] != sc_link->scsipi_atapi.drive)
    139   1.4     enami 		return (0);
    140   1.2    bouyer 	return ((*cf->cf_attach->ca_match)(parent, cf, aux));
    141   1.2    bouyer }
    142   1.2    bouyer 
    143   1.2    bouyer void
    144   1.2    bouyer atapibusattach(parent, self, aux)
    145   1.4     enami 	struct device *parent, *self;
    146   1.4     enami 	void *aux;
    147   1.2    bouyer {
    148  1.13    bouyer 	struct atapibus_softc *sc_ab = (struct atapibus_softc *)self;
    149  1.13    bouyer 	struct ata_atapi_attach *aa_link = aux;
    150  1.13    bouyer 	struct scsipi_link *sc_link_proto;
    151   1.2    bouyer 	int nbytes;
    152   1.2    bouyer 
    153   1.2    bouyer 	printf("\n");
    154  1.10   thorpej 
    155  1.10   thorpej 	/* Initialize shared data. */
    156  1.10   thorpej 	scsipi_init();
    157   1.2    bouyer 
    158  1.13    bouyer 	sc_link_proto = malloc(sizeof(struct scsipi_link),
    159  1.13    bouyer 	    M_DEVBUF, M_NOWAIT);
    160  1.13    bouyer 	if (sc_link_proto == NULL)
    161  1.33  augustss 		panic("atapibusattach : can't allocate scsipi link proto\n");
    162  1.13    bouyer 	memset(sc_link_proto, 0, sizeof(struct scsipi_link));
    163  1.13    bouyer 
    164  1.13    bouyer 	sc_link_proto->type = BUS_ATAPI;
    165  1.13    bouyer 	sc_link_proto->openings = aa_link->aa_openings;
    166  1.13    bouyer 	sc_link_proto->scsipi_atapi.channel = aa_link->aa_channel;
    167  1.13    bouyer 	sc_link_proto->adapter_softc = parent;
    168  1.13    bouyer 	sc_link_proto->adapter = aa_link->aa_bus_private;
    169  1.13    bouyer 	sc_link_proto->scsipi_atapi.atapibus = sc_ab->sc_dev.dv_unit;
    170   1.2    bouyer 	sc_link_proto->scsipi_cmd = atapi_scsipi_cmd;
    171   1.2    bouyer 	sc_link_proto->scsipi_interpret_sense = atapi_interpret_sense;
    172   1.2    bouyer 	sc_link_proto->sc_print_addr = atapi_print_addr;
    173  1.35  augustss 	sc_link_proto->scsipi_kill_pending =
    174  1.35  augustss 	  ((struct atapi_adapter*)aa_link->aa_bus_private)->atapi_kill_pending;
    175   1.2    bouyer 
    176  1.13    bouyer 
    177  1.13    bouyer 	sc_ab->adapter_link = sc_link_proto;
    178  1.13    bouyer 	sc_ab->sc_drvs = aa_link->aa_drv_data;
    179   1.2    bouyer 
    180   1.4     enami 	nbytes = 2 * sizeof(struct scsipi_link **);
    181  1.13    bouyer 	sc_ab->sc_link = (struct scsipi_link **)malloc(nbytes, M_DEVBUF,
    182   1.2    bouyer 	    M_NOWAIT);
    183  1.13    bouyer 	if (sc_ab->sc_link == NULL)
    184   1.2    bouyer 		panic("scsibusattach: can't allocate target links");
    185  1.13    bouyer 	memset(sc_ab->sc_link, 0, nbytes);
    186  1.13    bouyer 	atapi_probe_bus(sc_ab->sc_dev.dv_unit, -1);
    187  1.27     enami }
    188  1.27     enami 
    189  1.27     enami int
    190  1.27     enami atapibusactivate(self, act)
    191  1.27     enami 	struct device *self;
    192  1.27     enami 	enum devact act;
    193  1.27     enami {
    194  1.27     enami 	struct atapibus_softc *sc = (struct atapibus_softc *)self;
    195  1.27     enami 	struct scsipi_link *sc_link;
    196  1.27     enami 	int target, error = 0, s;
    197  1.27     enami 
    198  1.27     enami 	s = splbio();
    199  1.27     enami 	switch (act) {
    200  1.27     enami 	case DVACT_ACTIVATE:
    201  1.27     enami 		error = EOPNOTSUPP;
    202  1.27     enami 		break;
    203  1.27     enami 
    204  1.27     enami 	case DVACT_DEACTIVATE:
    205  1.27     enami 		for (target = 0; target <= MAX_TARGET; target++) {
    206  1.27     enami 			sc_link = sc->sc_link[target];
    207  1.27     enami 			if (sc_link == NULL)
    208  1.27     enami 				continue;
    209  1.27     enami 			error = config_deactivate(sc_link->device_softc);
    210  1.27     enami 			if (error != 0)
    211  1.27     enami 				goto out;
    212  1.27     enami 		}
    213  1.27     enami 		break;
    214  1.27     enami 	}
    215  1.27     enami  out:
    216  1.27     enami 	splx(s);
    217  1.27     enami 	return (error);
    218  1.27     enami }
    219  1.27     enami 
    220  1.27     enami int
    221  1.27     enami atapibusdetach(self, flags)
    222  1.27     enami 	struct device *self;
    223  1.27     enami 	int flags;
    224  1.27     enami {
    225  1.27     enami 	struct atapibus_softc *sc = (struct atapibus_softc *)self;
    226  1.27     enami 	struct scsipi_link *sc_link;
    227  1.27     enami 	int target, error;
    228  1.27     enami 
    229  1.27     enami 	for (target = 0; target <= MAX_TARGET; target++) {
    230  1.27     enami 		sc_link = sc->sc_link[target];
    231  1.27     enami 		if (sc_link == NULL)
    232  1.27     enami 			continue;
    233  1.27     enami 		error = config_detach(sc_link->device_softc, flags);
    234  1.27     enami 		if (error != 0)
    235  1.27     enami 			return (error);
    236  1.27     enami 
    237  1.27     enami 		/*
    238  1.27     enami 		 * We have successfully detached the child.  Drop the
    239  1.27     enami 		 * direct reference for the child so that wdcdetach
    240  1.27     enami 		 * won't call detach routine twice.
    241  1.27     enami 		 */
    242  1.27     enami #ifdef DIAGNOSTIC
    243  1.27     enami 		if (sc_link->device_softc != sc->sc_drvs[target].drv_softc)
    244  1.27     enami 			panic("softc mismatch");
    245  1.27     enami #endif
    246  1.27     enami 		sc->sc_drvs[target].drv_softc = NULL;
    247  1.27     enami 
    248  1.27     enami 		free(sc_link, M_DEVBUF);
    249  1.27     enami 		sc->sc_link[target] = NULL;
    250  1.27     enami 	}
    251  1.27     enami 	return (0);
    252   1.2    bouyer }
    253   1.2    bouyer 
    254   1.4     enami int
    255   1.2    bouyer atapi_probe_bus(bus, target)
    256   1.4     enami 	int bus, target;
    257   1.2    bouyer {
    258   1.2    bouyer 	int maxtarget, mintarget;
    259   1.2    bouyer 	struct atapibus_softc *atapi;
    260  1.15   thorpej 	int error;
    261  1.34    bouyer 	struct atapi_adapter *atapi_adapter;
    262   1.4     enami 
    263   1.2    bouyer 	if (bus < 0 || bus >= atapibus_cd.cd_ndevs)
    264   1.4     enami 		return (ENXIO);
    265   1.2    bouyer 	atapi = atapibus_cd.cd_devs[bus];
    266   1.4     enami 	if (atapi == NULL)
    267   1.4     enami 		return (ENXIO);
    268   1.2    bouyer 
    269   1.2    bouyer 	if (target == -1) {
    270   1.2    bouyer 		maxtarget = 1;
    271   1.2    bouyer 		mintarget = 0;
    272   1.2    bouyer 	} else {
    273   1.2    bouyer 		if (target < 0 || target > 1)
    274   1.4     enami 			return (ENXIO);
    275   1.2    bouyer 		maxtarget = mintarget = target;
    276   1.2    bouyer 	}
    277  1.15   thorpej 	if ((error = scsipi_adapter_addref(atapi->adapter_link)) != 0)
    278  1.15   thorpej 		return (error);
    279  1.34    bouyer 	atapi_adapter = (struct atapi_adapter*)atapi->adapter_link->adapter;
    280   1.4     enami 	for (target = mintarget; target <= maxtarget; target++)
    281  1.34    bouyer 		atapi_adapter->atapi_probedev(atapi, target);
    282  1.15   thorpej 	scsipi_adapter_delref(atapi->adapter_link);
    283   1.4     enami 	return (0);
    284   1.2    bouyer }
    285   1.2    bouyer 
    286  1.34    bouyer void *
    287  1.34    bouyer atapi_probedev(atapi, target, sc_link, sa)
    288   1.2    bouyer 	struct atapibus_softc *atapi;
    289   1.2    bouyer 	int target;
    290  1.34    bouyer 	struct scsipi_link *sc_link;
    291  1.34    bouyer 	struct scsipibus_attach_args *sa;
    292   1.2    bouyer {
    293   1.2    bouyer 	struct cfdata *cf;
    294   1.2    bouyer 	struct scsi_quirk_inquiry_pattern *finger;
    295   1.2    bouyer 	int priority;
    296   1.2    bouyer 
    297  1.34    bouyer 	/* Fill generic parts of the link. */
    298  1.34    bouyer 	sc_link->active = 0;
    299  1.34    bouyer 	sc_link->scsipi_atapi.drive = target;
    300  1.34    bouyer 	sc_link->device = NULL;
    301  1.34    bouyer 	TAILQ_INIT(&sc_link->pending_xfers);
    302   1.2    bouyer #if defined(SCSIDEBUG) && DEBUGTYPE == BUS_ATAPI
    303  1.34    bouyer 	if (DEBUGTARGET == -1 || target == DEBUGTARGET)
    304  1.34    bouyer 		sc_link->flags |= DEBUGLEVEL;
    305   1.2    bouyer #endif /* SCSIDEBUG */
    306  1.34    bouyer 	finger = (struct scsi_quirk_inquiry_pattern *)scsipi_inqmatch(
    307  1.34    bouyer 	    &sa->sa_inqbuf, (caddr_t)atapi_quirk_patterns,
    308  1.34    bouyer 	    sizeof(atapi_quirk_patterns) /
    309  1.34    bouyer 	        sizeof(atapi_quirk_patterns[0]),
    310  1.34    bouyer 	    sizeof(atapi_quirk_patterns[0]), &priority);
    311  1.34    bouyer 	if (priority != 0)
    312  1.34    bouyer 		sc_link->quirks |= finger->quirks;
    313  1.34    bouyer 
    314  1.34    bouyer 	if ((cf = config_search(atapibussubmatch, &atapi->sc_dev,
    315  1.34    bouyer 	    sa)) != 0) {
    316  1.34    bouyer 		atapi->sc_link[target] = sc_link;
    317  1.34    bouyer 		return (config_attach(&atapi->sc_dev, cf,
    318  1.34    bouyer 		    sa, atapibusprint));
    319  1.34    bouyer 	} else {
    320  1.34    bouyer 		atapibusprint(sa, atapi->sc_dev.dv_xname);
    321  1.34    bouyer 		printf(" not configured\n");
    322  1.34    bouyer 		free(sc_link, M_DEVBUF);
    323  1.34    bouyer 		return (NULL);
    324   1.2    bouyer 	}
    325   1.2    bouyer }
    326   1.2    bouyer 
    327   1.2    bouyer int
    328   1.2    bouyer atapibusprint(aux, pnp)
    329   1.2    bouyer 	void *aux;
    330   1.2    bouyer 	const char *pnp;
    331   1.2    bouyer {
    332   1.2    bouyer 	struct scsipibus_attach_args *sa = aux;
    333   1.2    bouyer 	struct scsipi_inquiry_pattern *inqbuf;
    334   1.2    bouyer 	char *dtype;
    335   1.2    bouyer 
    336   1.2    bouyer 	if (pnp != NULL)
    337   1.2    bouyer 		printf("%s", pnp);
    338   1.2    bouyer 
    339   1.2    bouyer 	inqbuf = &sa->sa_inqbuf;
    340   1.2    bouyer 
    341   1.2    bouyer 	dtype = scsipi_dtype(inqbuf->type & SID_TYPE);
    342   1.2    bouyer 	printf(" drive %d: <%s, %s, %s> type %d %s %s",
    343   1.2    bouyer 	    sa->sa_sc_link->scsipi_atapi.drive,inqbuf->vendor,
    344   1.2    bouyer 	    inqbuf->product, inqbuf->revision, inqbuf->type, dtype,
    345   1.2    bouyer 	    inqbuf->removable ? "removable" : "fixed");
    346   1.2    bouyer 	return (UNCONF);
    347   1.2    bouyer }
    348