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