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