Home | History | Annotate | Line # | Download | only in scsipi
atapiconf.c revision 1.5
      1 /*	$NetBSD: atapiconf.c,v 1.5 1997/10/31 09:11:06 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/scsipi/scsipi_all.h>
     41 #include <dev/scsipi/scsi_all.h>
     42 #include <dev/scsipi/scsipiconf.h>
     43 #include <dev/scsipi/atapiconf.h>
     44 
     45 #include "locators.h"
     46 
     47 #define SILENT_PRINTF(flags,string) if (!(flags & A_SILENT)) printf string
     48 
     49 struct atapibus_softc {
     50 	struct device sc_dev;
     51 	struct scsipi_link *adapter_link;	/* proto supplied by adapter */
     52 	struct scsipi_link **sc_link;		/* dynamically allocated */
     53 };
     54 
     55 #ifdef __BROKEN_INDIRECT_CONFIG
     56 int	atapibusmatch __P((struct device *, void *, void *));
     57 int	atapibussubmatch __P((struct device *, void *, void *));
     58 #else
     59 int	atapibusmatch __P((struct device *, struct cfdata *, void *));
     60 int	atapibussubmatch __P((struct device *, struct cfdata *, void *));
     61 #endif
     62 void	atapibusattach __P((struct device *, struct device *, void *));
     63 int	atapiprint __P((void *, const char *));
     64 
     65 int	atapi_probe_bus __P((int, int));
     66 void	atapi_probedev __P((struct atapibus_softc *, int ));
     67 
     68 struct cfattach atapibus_ca = {
     69 	sizeof(struct atapibus_softc), atapibusmatch, atapibusattach
     70 };
     71 
     72 struct cfdriver atapibus_cd = {
     73 	NULL, "atapibus", DV_DULL
     74 };
     75 
     76 int atapibusprint __P((void *, const char *));
     77 
     78 struct scsi_quirk_inquiry_pattern atapi_quirk_patterns[] = {
     79 	{{T_CDROM, T_REMOV,
     80 	 "GCD-R580B", "", "1.00"},                ADEV_LITTLETOC},
     81 	{{T_CDROM, T_REMOV,
     82 	 "SANYO CRD-256P", "", "1.02"},           ADEV_NOCAPACITY},
     83 	{{T_CDROM, T_REMOV,
     84 	 "SANYO CRD-254P", "", "1.02"},           ADEV_NOCAPACITY},
     85 	{{T_CDROM, T_REMOV,
     86 	 "UJDCD8730", "", "1.14"},                ADEV_NODOORLOCK},
     87 	{{T_CDROM, T_REMOV,
     88 	 "ALPS ELECTRIC CO.,LTD. DC544C", "", "SW03D"}, ADEV_NOTUR},
     89 	{{T_CDROM, T_REMOV,
     90 	 "NEC                 CD-ROM DRIVE:273", "", "4.21"}, ADEV_NOTUR},
     91 	{{T_CDROM, T_REMOV,
     92 	 "MATSHITA CR-574", "", "1.06"},          ADEV_NOCAPACITY},
     93 	{{T_CDROM, T_REMOV,
     94 	 "MATSHITA CR-574", "", "1.02"},          ADEV_NOCAPACITY},
     95 };
     96 
     97 int
     98 #ifdef __BROKEN_INDIRECT_CONFIG
     99 atapibusmatch(parent, match, aux)
    100 	struct device *parent;
    101 	void *match, *aux;
    102 {
    103 #else
    104 atapibusmatch(parent, cf, aux)
    105 	struct device *parent;
    106 	struct cfdata *cf;
    107 	void *aux;
    108 {
    109 #endif
    110 	struct scsipi_link *sc_link = aux;
    111 
    112 	if (sc_link == NULL)
    113 		return (0);
    114 	if (sc_link->type != BUS_ATAPI)
    115 		return (0);
    116 	return (1);
    117 }
    118 
    119 int
    120 #ifdef __BROKEN_INDIRECT_CONFIG
    121 atapibussubmatch(parent, match, aux)
    122 	struct device *parent;
    123 	void *match, *aux;
    124 {
    125 	struct cfdata *cf = match;
    126 #else
    127 atapibussubmatch(parent, cf, aux)
    128 	struct device *parent;
    129 	struct cfdata *cf;
    130 	void *aux;
    131 {
    132 #endif
    133 	struct scsipibus_attach_args *sa = aux;
    134 	struct scsipi_link *sc_link = sa->sa_sc_link;
    135 
    136 	if (cf->cf_loc[ATAPIBUSCF_DRIVE] != ATAPIBUSCF_DRIVE_DEFAULT &&
    137 	    cf->cf_loc[ATAPIBUSCF_DRIVE] != sc_link->scsipi_atapi.drive)
    138 		return (0);
    139 	return ((*cf->cf_attach->ca_match)(parent, cf, aux));
    140 }
    141 
    142 #if 0
    143 void
    144 atapi_fixquirk(sc_link)
    145 	struct scsipi_link *ad_link;
    146 {
    147 	struct atapi_identify *id = &ad_link->id;
    148 	struct atapi_quirk_inquiry_pattern *quirk;
    149 
    150 	/*
    151 	 * Clean up the model name, serial and revision numbers.
    152 	 */
    153 	btrim(id->model, sizeof(id->model));
    154 	btrim(id->serial_number, sizeof(id->serial_number));
    155 	btrim(id->firmware_revision, sizeof(id->firmware_revision));
    156 }
    157 #endif
    158 
    159 void
    160 atapibusattach(parent, self, aux)
    161 	struct device *parent, *self;
    162 	void *aux;
    163 {
    164 	struct atapibus_softc *ab = (struct atapibus_softc *)self;
    165 	struct scsipi_link *sc_link_proto = aux;
    166 	int nbytes;
    167 
    168 	printf("\n");
    169 
    170 	sc_link_proto->scsipi_atapi.atapibus = ab->sc_dev.dv_unit;
    171 	sc_link_proto->scsipi_cmd = atapi_scsipi_cmd;
    172 	sc_link_proto->scsipi_interpret_sense = atapi_interpret_sense;
    173 	sc_link_proto->sc_print_addr = atapi_print_addr;
    174 
    175 	ab->adapter_link = sc_link_proto;
    176 
    177 	nbytes = 2 * sizeof(struct scsipi_link **);
    178 	ab->sc_link = (struct scsipi_link **)malloc(nbytes, M_DEVBUF,
    179 	    M_NOWAIT);
    180 	if (ab->sc_link == NULL)
    181 		panic("scsibusattach: can't allocate target links");
    182 	bzero(ab->sc_link, nbytes);
    183 	atapi_probe_bus(ab->sc_dev.dv_unit, -1);
    184 }
    185 
    186 int
    187 atapi_probe_bus(bus, target)
    188 	int bus, target;
    189 {
    190 	int maxtarget, mintarget;
    191 	struct atapibus_softc *atapi;
    192 
    193 	if (bus < 0 || bus >= atapibus_cd.cd_ndevs)
    194 		return (ENXIO);
    195 	atapi = atapibus_cd.cd_devs[bus];
    196 	if (atapi == NULL)
    197 		return (ENXIO);
    198 
    199 	if (target == -1) {
    200 		maxtarget = 1;
    201 		mintarget = 0;
    202 	} else {
    203 		if (target < 0 || target > 1)
    204 			return (ENXIO);
    205 		maxtarget = mintarget = target;
    206 	}
    207 	for (target = mintarget; target <= maxtarget; target++)
    208 		atapi_probedev(atapi, target);
    209 	return (0);
    210 }
    211 
    212 void
    213 atapi_probedev(atapi, target)
    214 	struct atapibus_softc *atapi;
    215 	int target;
    216 {
    217 	struct scsipi_link *sc_link;
    218 	struct scsipibus_attach_args sa;
    219 	struct atapi_identify ids;
    220 	struct atapi_identify *id = &ids;
    221 	struct cfdata *cf;
    222 	struct scsi_quirk_inquiry_pattern *finger;
    223 	int priority;
    224 	char serial_number[20], model[40], firmware_revision[8];
    225 
    226 	/* skip if already attached */
    227 	if (atapi->sc_link[target])
    228 		return;
    229 
    230 	if (wdc_atapi_get_params(atapi->adapter_link, target, id)) {
    231 #ifdef ATAPI_DEBUG_PROBE
    232 		printf("%s drive %d: cmdsz 0x%x drqtype 0x%x\n",
    233 		    atapi->sc_dev.dv_xname, target,
    234 		    id->config.cmd_drq_rem & ATAPI_PACKET_SIZE_MASK,
    235 		    id->config.cmd_drq_rem & ATAPI_DRQ_MASK);
    236 #endif
    237 		/*
    238 		 * Shuffle string byte order.
    239 		 * Mitsumi and NEC drives don't need this.
    240 		 */
    241 		if (((id->model[0] == 'N' && id->model[1] == 'E') ||
    242 		    (id->model[0] == 'F' && id->model[1] == 'X')) == 0)
    243 			bswap(id->model, sizeof(id->model));
    244 		bswap(id->serial_number, sizeof(id->serial_number));
    245 		bswap(id->firmware_revision, sizeof(id->firmware_revision));
    246 
    247 		/*
    248 		 * Allocate a device link and try and attach
    249 		 * a driver to this device.  If we fail, free
    250 		 * the link.
    251 		 */
    252 		sc_link = malloc(sizeof(*sc_link), M_DEVBUF, M_NOWAIT);
    253 		if (sc_link == NULL) {
    254 			printf("%s: can't allocate link for drive %d\n",
    255 			    atapi->sc_dev.dv_xname, target);
    256 			return;
    257 		}
    258 		/* Fill in link. */
    259 		*sc_link = *atapi->adapter_link;
    260 		sc_link->scsipi_atapi.drive = target;
    261 		sc_link->device = NULL;
    262 #if defined(SCSIDEBUG) && DEBUGTYPE == BUS_ATAPI
    263 		if (DEBUGTARGET == -1 || target == DEBUGTARGET)
    264 			sc_link->flags |= DEBUGLEVEL;
    265 #endif /* SCSIDEBUG */
    266 		if (id->config.cmd_drq_rem & ATAPI_PACKET_SIZE_16)
    267 			sc_link->scsipi_atapi.cap |= ACAP_LEN;
    268 		sc_link->scsipi_atapi.cap |=
    269 		    (id->config.cmd_drq_rem & ATAPI_DRQ_MASK) << 3;
    270 #if 0
    271 		bcopy(id, &ad_link->id, sizeof(*id));
    272 		/* Fix strings and look through the quirk table. */
    273 		atapi_fixquirk(ad_link, id);
    274 #endif
    275 		sa.sa_sc_link = sc_link;
    276 		sa.sa_inqbuf.type =  id->config.device_type & SID_TYPE;
    277 		sa.sa_inqbuf.removable =
    278 		    id->config.cmd_drq_rem & ATAPI_REMOVABLE ?
    279 		    T_REMOV : T_FIXED;
    280 		if (sa.sa_inqbuf.removable)
    281 			sc_link->flags |= SDEV_REMOVABLE;
    282 		scsipi_strvis(model, id->model, 40);
    283 		scsipi_strvis(serial_number, id->serial_number, 20);
    284 		scsipi_strvis(firmware_revision, id->firmware_revision, 8);
    285 		sa.sa_inqbuf.vendor = model;
    286 		sa.sa_inqbuf.product = serial_number;
    287 		sa.sa_inqbuf.revision = firmware_revision;
    288 
    289 		finger = (struct scsi_quirk_inquiry_pattern *)scsipi_inqmatch(
    290 		    &sa.sa_inqbuf, (caddr_t)atapi_quirk_patterns,
    291 		    sizeof(atapi_quirk_patterns) /
    292 		        sizeof(atapi_quirk_patterns[0]),
    293 		    sizeof(atapi_quirk_patterns[0]), &priority);
    294 		if (priority != 0)
    295 			sc_link->quirks |= finger->quirks;
    296 
    297 		if ((cf = config_search(atapibussubmatch, &atapi->sc_dev,
    298 		    &sa)) != 0) {
    299 			atapi->sc_link[target] = sc_link;
    300 			config_attach(&atapi->sc_dev, cf, &sa, atapibusprint);
    301 			return;
    302 		} else {
    303 			atapibusprint(&sa, atapi->sc_dev.dv_xname);
    304 			printf(" not configured\n");
    305 			free(sc_link, M_DEVBUF);
    306 			return;
    307 		}
    308 
    309 #if 0 /* WAS: */
    310 		/* Try to find a match. */
    311 		if (config_found(self, ad_link, atapiprint) == NULL)
    312 			free(ad_link, M_DEVBUF);
    313 #endif
    314 	}
    315 }
    316 
    317 int
    318 atapibusprint(aux, pnp)
    319 	void *aux;
    320 	const char *pnp;
    321 {
    322 	struct scsipibus_attach_args *sa = aux;
    323 	struct scsipi_inquiry_pattern *inqbuf;
    324 	char *dtype;
    325 
    326 	if (pnp != NULL)
    327 		printf("%s", pnp);
    328 
    329 	inqbuf = &sa->sa_inqbuf;
    330 
    331 	dtype = scsipi_dtype(inqbuf->type & SID_TYPE);
    332 	printf(" drive %d: <%s, %s, %s> type %d %s %s",
    333 	    sa->sa_sc_link->scsipi_atapi.drive,inqbuf->vendor,
    334 	    inqbuf->product, inqbuf->revision, inqbuf->type, dtype,
    335 	    inqbuf->removable ? "removable" : "fixed");
    336 	return (UNCONF);
    337 }
    338