Home | History | Annotate | Line # | Download | only in scsipi
atapiconf.c revision 1.72.24.2
      1  1.72.24.2      matt /*	atapiconf.c,v 1.72.24.1 2008/01/09 01:54:29 matt Exp	*/
      2        1.2    bouyer 
      3        1.2    bouyer /*
      4       1.46    bouyer  * Copyright (c) 1996, 2001 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.42     lukem 
     32       1.42     lukem #include <sys/cdefs.h>
     33  1.72.24.2      matt __KERNEL_RCSID(0, "atapiconf.c,v 1.72.24.1 2008/01/09 01:54:29 matt Exp");
     34        1.2    bouyer 
     35        1.2    bouyer #include <sys/param.h>
     36        1.2    bouyer #include <sys/systm.h>
     37        1.2    bouyer #include <sys/malloc.h>
     38        1.2    bouyer #include <sys/device.h>
     39        1.2    bouyer #include <sys/buf.h>
     40        1.4     enami #include <sys/proc.h>
     41       1.39    bouyer #include <sys/kthread.h>
     42        1.2    bouyer 
     43        1.2    bouyer #include <dev/scsipi/scsipi_all.h>
     44       1.39    bouyer #include <dev/scsipi/scsipiconf.h>
     45        1.2    bouyer #include <dev/scsipi/atapiconf.h>
     46        1.4     enami 
     47        1.2    bouyer #include "locators.h"
     48        1.2    bouyer 
     49        1.2    bouyer #define SILENT_PRINTF(flags,string) if (!(flags & A_SILENT)) printf string
     50       1.27     enami #define MAX_TARGET 1
     51        1.2    bouyer 
     52       1.39    bouyer const struct scsipi_periphsw atapi_probe_periphsw = {
     53       1.39    bouyer 	NULL,
     54       1.39    bouyer 	NULL,
     55       1.39    bouyer 	NULL,
     56       1.39    bouyer 	NULL,
     57       1.39    bouyer };
     58       1.39    bouyer 
     59  1.72.24.2      matt static int	atapibusmatch(device_t, struct cfdata *, void *);
     60  1.72.24.2      matt static void	atapibusattach(device_t, device_t, void *);
     61  1.72.24.2      matt static int	atapibusactivate(device_t, enum devact);
     62  1.72.24.2      matt static int	atapibusdetach(device_t, int flags);
     63  1.72.24.2      matt static void	atapibuschilddet(device_t, device_t);
     64        1.2    bouyer 
     65  1.72.24.2      matt static int	atapibussubmatch(device_t, struct cfdata *,
     66       1.68  drochner 				 const int *, void *);
     67       1.39    bouyer 
     68       1.64   thorpej static int	atapi_probe_bus(struct atapibus_softc *, int);
     69       1.64   thorpej 
     70       1.64   thorpej static int	atapibusprint(void *, const char *);
     71        1.2    bouyer 
     72  1.72.24.2      matt CFATTACH_DECL2(atapibus, sizeof(struct atapibus_softc),
     73  1.72.24.2      matt     atapibusmatch, atapibusattach, atapibusdetach, atapibusactivate, NULL,
     74  1.72.24.2      matt     atapibuschilddet);
     75        1.2    bouyer 
     76        1.8   thorpej extern struct cfdriver atapibus_cd;
     77        1.2    bouyer 
     78       1.64   thorpej static const struct scsi_quirk_inquiry_pattern atapi_quirk_patterns[] = {
     79        1.2    bouyer 	{{T_CDROM, T_REMOV,
     80       1.39    bouyer 	 "ALPS ELECTRIC CO.,LTD. DC544C", "", "SW03D"},	PQUIRK_NOTUR},
     81       1.22    bouyer 	{{T_CDROM, T_REMOV,
     82       1.39    bouyer 	 "CR-2801TE", "", "1.07"},		PQUIRK_NOSENSE},
     83       1.23    bouyer 	{{T_CDROM, T_REMOV,
     84       1.39    bouyer 	 "CREATIVECD3630E", "", "AC101"},	PQUIRK_NOSENSE},
     85        1.2    bouyer 	{{T_CDROM, T_REMOV,
     86       1.39    bouyer 	 "FX320S", "", "q01"},			PQUIRK_NOSENSE},
     87       1.21    bouyer 	{{T_CDROM, T_REMOV,
     88       1.39    bouyer 	 "GCD-R580B", "", "1.00"},		PQUIRK_LITTLETOC},
     89       1.31  jdolecek 	{{T_CDROM, T_REMOV,
     90       1.39    bouyer 	 "HITACHI CDR-7730", "", "0008a"},      PQUIRK_NOSENSE},
     91       1.24    bouyer 	{{T_CDROM, T_REMOV,
     92       1.39    bouyer 	 "MATSHITA CR-574", "", "1.02"},	PQUIRK_NOCAPACITY},
     93        1.3    bouyer 	{{T_CDROM, T_REMOV,
     94       1.39    bouyer 	 "MATSHITA CR-574", "", "1.06"},	PQUIRK_NOCAPACITY},
     95        1.7    bouyer 	{{T_CDROM, T_REMOV,
     96       1.39    bouyer 	 "Memorex CRW-2642", "", "1.0g"},	PQUIRK_NOSENSE},
     97        1.7    bouyer 	{{T_CDROM, T_REMOV,
     98       1.39    bouyer 	 "NEC                 CD-ROM DRIVE:273", "", "4.21"}, PQUIRK_NOTUR},
     99        1.2    bouyer 	{{T_CDROM, T_REMOV,
    100       1.39    bouyer 	 "SANYO CRD-256P", "", "1.02"},		PQUIRK_NOCAPACITY},
    101        1.2    bouyer 	{{T_CDROM, T_REMOV,
    102       1.39    bouyer 	 "SANYO CRD-254P", "", "1.02"},		PQUIRK_NOCAPACITY},
    103        1.3    bouyer 	{{T_CDROM, T_REMOV,
    104       1.39    bouyer 	 "SANYO CRD-S54P", "", "1.08"},		PQUIRK_NOCAPACITY},
    105        1.5    bouyer 	{{T_CDROM, T_REMOV,
    106       1.39    bouyer 	 "CD-ROM  CDR-S1", "", "1.70"},		PQUIRK_NOCAPACITY}, /* Sanyo */
    107       1.18    bouyer 	{{T_CDROM, T_REMOV,
    108       1.39    bouyer 	 "CD-ROM  CDR-N16", "", "1.25"},	PQUIRK_NOCAPACITY}, /* Sanyo */
    109        1.2    bouyer };
    110        1.2    bouyer 
    111        1.2    bouyer int
    112       1.72  christos atapiprint(void *aux, const char *pnp)
    113       1.44    bouyer {
    114       1.44    bouyer 	if (pnp)
    115       1.55   thorpej 		aprint_normal("atapibus at %s", pnp);
    116       1.44    bouyer 	return (UNCONF);
    117       1.44    bouyer }
    118       1.44    bouyer 
    119       1.64   thorpej static int
    120  1.72.24.2      matt atapibusmatch(device_t parent, struct cfdata *cf, void *aux)
    121        1.2    bouyer {
    122       1.44    bouyer 	struct scsipi_channel *chan = aux;
    123        1.2    bouyer 
    124       1.44    bouyer 	if (chan == NULL)
    125        1.4     enami 		return (0);
    126       1.39    bouyer 
    127       1.44    bouyer 	if (chan->chan_bustype->bustype_type != SCSIPI_BUSTYPE_ATAPI)
    128       1.39    bouyer 		return (0);
    129       1.39    bouyer 
    130        1.4     enami 	return (1);
    131        1.2    bouyer }
    132        1.2    bouyer 
    133       1.64   thorpej static int
    134  1.72.24.2      matt atapibussubmatch(device_t parent, struct cfdata *cf,
    135       1.72  christos     const int *ldesc, void *aux)
    136        1.2    bouyer {
    137        1.2    bouyer 	struct scsipibus_attach_args *sa = aux;
    138       1.39    bouyer 	struct scsipi_periph *periph = sa->sa_periph;
    139        1.2    bouyer 
    140        1.2    bouyer 	if (cf->cf_loc[ATAPIBUSCF_DRIVE] != ATAPIBUSCF_DRIVE_DEFAULT &&
    141       1.39    bouyer 	    cf->cf_loc[ATAPIBUSCF_DRIVE] != periph->periph_target)
    142        1.4     enami 		return (0);
    143       1.50   thorpej 	return (config_match(parent, cf, aux));
    144        1.2    bouyer }
    145        1.2    bouyer 
    146       1.64   thorpej static void
    147  1.72.24.2      matt atapibusattach(device_t parent, device_t self, void *aux)
    148        1.2    bouyer {
    149       1.70   thorpej 	struct atapibus_softc *sc = device_private(self);
    150       1.44    bouyer 	struct scsipi_channel *chan = aux;
    151       1.39    bouyer 
    152       1.39    bouyer 	sc->sc_channel = chan;
    153       1.48    bouyer 
    154       1.48    bouyer 	chan->chan_name = sc->sc_dev.dv_xname;
    155       1.39    bouyer 
    156       1.39    bouyer 	/* ATAPI has no LUNs. */
    157       1.39    bouyer 	chan->chan_nluns = 1;
    158  1.72.24.1      matt 	aprint_naive("\n");
    159  1.72.24.1      matt 	aprint_normal(": %d targets\n", chan->chan_ntargets);
    160       1.39    bouyer 
    161       1.39    bouyer 	/* Initialize the channel. */
    162       1.49       jmc 	chan->chan_init_cb = NULL;
    163       1.49       jmc 	chan->chan_init_cb_arg = NULL;
    164       1.39    bouyer 	scsipi_channel_init(chan);
    165       1.39    bouyer 
    166  1.72.24.1      matt 	if (!pmf_device_register(self, NULL, NULL))
    167  1.72.24.1      matt 		aprint_error_dev(self, "couldn't establish power handler\n");
    168  1.72.24.1      matt 
    169       1.39    bouyer 	/* Probe the bus for devices. */
    170       1.39    bouyer 	atapi_probe_bus(sc, -1);
    171       1.27     enami }
    172       1.27     enami 
    173       1.64   thorpej static int
    174  1.72.24.2      matt atapibusactivate(device_t self, enum devact act)
    175       1.27     enami {
    176       1.70   thorpej 	struct atapibus_softc *sc = device_private(self);
    177       1.39    bouyer 	struct scsipi_channel *chan = sc->sc_channel;
    178       1.39    bouyer 	struct scsipi_periph *periph;
    179       1.27     enami 	int target, error = 0, s;
    180       1.27     enami 
    181       1.27     enami 	s = splbio();
    182       1.27     enami 	switch (act) {
    183       1.27     enami 	case DVACT_ACTIVATE:
    184       1.27     enami 		error = EOPNOTSUPP;
    185       1.27     enami 		break;
    186       1.27     enami 
    187       1.27     enami 	case DVACT_DEACTIVATE:
    188       1.39    bouyer 		for (target = 0; target < chan->chan_ntargets; target++) {
    189       1.39    bouyer 			periph = scsipi_lookup_periph(chan, target, 0);
    190       1.39    bouyer 			if (periph == NULL)
    191       1.27     enami 				continue;
    192       1.39    bouyer 			error = config_deactivate(periph->periph_dev);
    193       1.39    bouyer 			if (error)
    194       1.27     enami 				goto out;
    195       1.27     enami 		}
    196       1.27     enami 		break;
    197       1.27     enami 	}
    198       1.27     enami  out:
    199       1.27     enami 	splx(s);
    200       1.27     enami 	return (error);
    201       1.27     enami }
    202       1.27     enami 
    203  1.72.24.2      matt static void
    204  1.72.24.2      matt atapibuschilddet(device_t self, device_t child)
    205  1.72.24.2      matt {
    206  1.72.24.2      matt 	struct atapibus_softc *sc = device_private(self);
    207  1.72.24.2      matt 	struct scsipi_channel *chan = sc->sc_channel;
    208  1.72.24.2      matt 	struct scsipi_periph *periph;
    209  1.72.24.2      matt 	int target;
    210  1.72.24.2      matt 
    211  1.72.24.2      matt 	for (target = 0; target < chan->chan_ntargets; target++) {
    212  1.72.24.2      matt 		periph = scsipi_lookup_periph(chan, target, 0);
    213  1.72.24.2      matt 		if (periph == NULL || periph->periph_dev != child)
    214  1.72.24.2      matt 			continue;
    215  1.72.24.2      matt 		scsipi_remove_periph(chan, periph);
    216  1.72.24.2      matt 		free(periph, M_DEVBUF);
    217  1.72.24.2      matt 		break;
    218  1.72.24.2      matt 	}
    219  1.72.24.2      matt }
    220  1.72.24.2      matt 
    221       1.64   thorpej static int
    222  1.72.24.2      matt atapibusdetach(device_t self, int flags)
    223       1.27     enami {
    224       1.70   thorpej 	struct atapibus_softc *sc = device_private(self);
    225       1.39    bouyer 	struct scsipi_channel *chan = sc->sc_channel;
    226       1.39    bouyer 	struct scsipi_periph *periph;
    227       1.27     enami 	int target, error;
    228       1.27     enami 
    229       1.39    bouyer 	/*
    230       1.39    bouyer 	 * Shut down the channel.
    231       1.39    bouyer 	 */
    232       1.39    bouyer 	scsipi_channel_shutdown(chan);
    233       1.39    bouyer 
    234       1.39    bouyer 	/*
    235       1.39    bouyer 	 * Now detach all of the periphs.
    236       1.39    bouyer 	 */
    237       1.39    bouyer 	for (target = 0; target < chan->chan_ntargets; target++) {
    238       1.39    bouyer 		periph = scsipi_lookup_periph(chan, target, 0);
    239       1.39    bouyer 		if (periph == NULL)
    240       1.27     enami 			continue;
    241       1.39    bouyer 		error = config_detach(periph->periph_dev, flags);
    242       1.39    bouyer 		if (error)
    243       1.27     enami 			return (error);
    244  1.72.24.2      matt 		KASSERT(scsipi_lookup_periph(chan, target, 0) == NULL);
    245       1.27     enami 	}
    246       1.27     enami 	return (0);
    247        1.2    bouyer }
    248        1.2    bouyer 
    249       1.64   thorpej static int
    250       1.64   thorpej atapi_probe_bus(struct atapibus_softc *sc, int target)
    251        1.2    bouyer {
    252       1.39    bouyer 	struct scsipi_channel *chan = sc->sc_channel;
    253        1.2    bouyer 	int maxtarget, mintarget;
    254       1.15   thorpej 	int error;
    255       1.34    bouyer 	struct atapi_adapter *atapi_adapter;
    256        1.4     enami 
    257        1.2    bouyer 	if (target == -1) {
    258        1.2    bouyer 		maxtarget = 1;
    259        1.2    bouyer 		mintarget = 0;
    260        1.2    bouyer 	} else {
    261       1.39    bouyer 		if (target < 0 || target >= chan->chan_ntargets)
    262        1.4     enami 			return (ENXIO);
    263        1.2    bouyer 		maxtarget = mintarget = target;
    264        1.2    bouyer 	}
    265       1.39    bouyer 
    266       1.39    bouyer 	if ((error = scsipi_adapter_addref(chan->chan_adapter)) != 0)
    267       1.15   thorpej 		return (error);
    268       1.39    bouyer 	atapi_adapter = (struct atapi_adapter*)chan->chan_adapter;
    269        1.4     enami 	for (target = mintarget; target <= maxtarget; target++)
    270       1.39    bouyer 		atapi_adapter->atapi_probe_device(sc, target);
    271       1.39    bouyer 	scsipi_adapter_delref(chan->chan_adapter);
    272        1.4     enami 	return (0);
    273        1.2    bouyer }
    274        1.2    bouyer 
    275       1.34    bouyer void *
    276       1.72  christos atapi_probe_device(struct atapibus_softc *sc, int target,
    277       1.64   thorpej     struct scsipi_periph *periph, struct scsipibus_attach_args *sa)
    278        1.2    bouyer {
    279       1.39    bouyer 	struct scsipi_channel *chan = sc->sc_channel;
    280       1.67  christos 	const struct scsi_quirk_inquiry_pattern *finger;
    281        1.2    bouyer 	struct cfdata *cf;
    282       1.39    bouyer 	int priority, quirks;
    283        1.2    bouyer 
    284       1.67  christos 	finger = scsipi_inqmatch(
    285       1.67  christos 	    &sa->sa_inqbuf, (const void *)atapi_quirk_patterns,
    286       1.34    bouyer 	    sizeof(atapi_quirk_patterns) /
    287       1.34    bouyer 	        sizeof(atapi_quirk_patterns[0]),
    288       1.34    bouyer 	    sizeof(atapi_quirk_patterns[0]), &priority);
    289       1.34    bouyer 
    290       1.39    bouyer 	if (finger != NULL)
    291       1.39    bouyer 		quirks = finger->quirks;
    292       1.39    bouyer 	else
    293       1.39    bouyer 		quirks = 0;
    294       1.39    bouyer 
    295       1.39    bouyer 	/*
    296       1.39    bouyer 	 * Now apply any quirks from the table.
    297       1.39    bouyer 	 */
    298       1.39    bouyer 	periph->periph_quirks |= quirks;
    299       1.39    bouyer 
    300       1.66  drochner 	if ((cf = config_search_ia(atapibussubmatch, &sc->sc_dev,
    301       1.66  drochner 	    "atapibus", sa)) != 0) {
    302       1.39    bouyer 		scsipi_insert_periph(chan, periph);
    303       1.39    bouyer 		/*
    304       1.39    bouyer 		 * XXX Can't assign periph_dev here, because we'll
    305       1.39    bouyer 		 * XXX need it before config_attach() returns.  Must
    306       1.39    bouyer 		 * XXX assign it in periph driver.
    307       1.39    bouyer 		 */
    308       1.39    bouyer 		return config_attach(&sc->sc_dev, cf, sa,
    309       1.39    bouyer 		    atapibusprint);
    310       1.34    bouyer 	} else {
    311       1.41     enami 		atapibusprint(sa, sc->sc_dev.dv_xname);
    312       1.34    bouyer 		printf(" not configured\n");
    313       1.39    bouyer 		free(periph, M_DEVBUF);
    314       1.39    bouyer 		return NULL;
    315        1.2    bouyer 	}
    316        1.2    bouyer }
    317        1.2    bouyer 
    318       1.64   thorpej static int
    319       1.64   thorpej atapibusprint(void *aux, const char *pnp)
    320        1.2    bouyer {
    321        1.2    bouyer 	struct scsipibus_attach_args *sa = aux;
    322        1.2    bouyer 	struct scsipi_inquiry_pattern *inqbuf;
    323       1.65   thorpej 	const char *dtype;
    324        1.2    bouyer 
    325        1.2    bouyer 	if (pnp != NULL)
    326       1.55   thorpej 		aprint_normal("%s", pnp);
    327        1.2    bouyer 
    328        1.2    bouyer 	inqbuf = &sa->sa_inqbuf;
    329        1.2    bouyer 
    330        1.2    bouyer 	dtype = scsipi_dtype(inqbuf->type & SID_TYPE);
    331       1.55   thorpej 	aprint_normal(" drive %d: <%s, %s, %s> %s %s",
    332       1.54     soren 	    sa->sa_periph->periph_target, inqbuf->vendor,
    333       1.54     soren 	    inqbuf->product, inqbuf->revision, dtype,
    334        1.2    bouyer 	    inqbuf->removable ? "removable" : "fixed");
    335        1.2    bouyer 	return (UNCONF);
    336        1.2    bouyer }
    337