Home | History | Annotate | Line # | Download | only in scsipi
uk.c revision 1.30
      1 /*	$NetBSD: uk.c,v 1.30 2001/04/25 17:53:42 bouyer Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Charles M. Hannum.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * Dummy driver for a device we can't identify.
     41  * Originally by Julian Elischer (julian (at) tfs.com)
     42  */
     43 
     44 #include <sys/types.h>
     45 #include <sys/param.h>
     46 #include <sys/systm.h>
     47 
     48 #include <sys/errno.h>
     49 #include <sys/ioctl.h>
     50 #include <sys/device.h>
     51 #include <sys/conf.h>
     52 #include <sys/vnode.h>
     53 
     54 #include <dev/scsipi/scsi_all.h>
     55 #include <dev/scsipi/scsipi_all.h>
     56 #include <dev/scsipi/scsiconf.h>
     57 
     58 #define	UKUNIT(z)	(minor(z))
     59 
     60 struct uk_softc {
     61 	struct device sc_dev;
     62 
     63 	struct scsipi_periph *sc_periph; /* all the inter level info */
     64 };
     65 
     66 int ukmatch __P((struct device *, struct cfdata *, void *));
     67 void ukattach __P((struct device *, struct device *, void *));
     68 int ukactivate __P((struct device *, enum devact));
     69 int ukdetach __P((struct device *, int));
     70 
     71 
     72 struct cfattach uk_scsibus_ca = {
     73 	sizeof(struct uk_softc), ukmatch, ukattach, ukdetach, ukactivate,
     74 };
     75 struct cfattach uk_atapibus_ca = {
     76 	sizeof(struct uk_softc), ukmatch, ukattach, ukdetach, ukactivate,
     77 };
     78 
     79 extern struct cfdriver uk_cd;
     80 
     81 cdev_decl(uk);
     82 
     83 int
     84 ukmatch(parent, match, aux)
     85 	struct device *parent;
     86 	struct cfdata *match;
     87 	void *aux;
     88 {
     89 
     90 	return (1);
     91 }
     92 
     93 /*
     94  * The routine called by the low level scsi routine when it discovers
     95  * a device suitable for this driver.
     96  */
     97 void
     98 ukattach(parent, self, aux)
     99 	struct device *parent, *self;
    100 	void *aux;
    101 {
    102 	struct uk_softc *uk = (void *)self;
    103 	struct scsipibus_attach_args *sa = aux;
    104 	struct scsipi_periph *periph = sa->sa_periph;
    105 
    106 	SC_DEBUG(periph, SCSIPI_DB2, ("ukattach: "));
    107 
    108 	/*
    109 	 * Store information needed to contact our base driver
    110 	 */
    111 	uk->sc_periph = periph;
    112 	periph->periph_dev = &uk->sc_dev;
    113 
    114 	printf("\n");
    115 	printf("%s: unknown device\n", uk->sc_dev.dv_xname);
    116 }
    117 
    118 int
    119 ukactivate(self, act)
    120 	struct device *self;
    121 	enum devact act;
    122 {
    123 	int rv = 0;
    124 
    125 	switch (act) {
    126 	case DVACT_ACTIVATE:
    127 		rv = EOPNOTSUPP;
    128 		break;
    129 
    130 	case DVACT_DEACTIVATE:
    131 		/*
    132 		 * Nothing to do; we key off the device's DVF_ACTIVE.
    133 		 */
    134 		break;
    135 	}
    136 	return (rv);
    137 }
    138 
    139 int
    140 ukdetach(self, flags)
    141 	struct device *self;
    142 	int flags;
    143 {
    144 	/*struct uk_softc *uk = (struct uk_softc *) self;*/
    145 	int cmaj, mn;
    146 
    147 	/* locate the major number */
    148 	for (cmaj = 0; cmaj <= nchrdev; cmaj++)
    149 		if (cdevsw[cmaj].d_open == ukopen)
    150 			break;
    151 
    152 	/* Nuke the vnodes for any open instances */
    153 	mn = self->dv_unit;
    154 	vdevgone(cmaj, mn, mn, VCHR);
    155 
    156 	return (0);
    157 }
    158 
    159 
    160 
    161 /*
    162  * open the device.
    163  */
    164 int
    165 ukopen(dev, flag, fmt, p)
    166 	dev_t dev;
    167 	int flag, fmt;
    168 	struct proc *p;
    169 {
    170 	int unit, error;
    171 	struct uk_softc *uk;
    172 	struct scsipi_periph *periph;
    173 	struct scsipi_adapter *adapt;
    174 
    175 	unit = UKUNIT(dev);
    176 	if (unit >= uk_cd.cd_ndevs)
    177 		return (ENXIO);
    178 	uk = uk_cd.cd_devs[unit];
    179 	if (uk == NULL)
    180 		return (ENXIO);
    181 
    182 	periph = uk->sc_periph;
    183 	adapt = periph->periph_channel->chan_adapter;
    184 
    185 	SC_DEBUG(periph, SCSIPI_DB1,
    186 	    ("ukopen: dev=0x%x (unit %d (of %d))\n", dev, unit,
    187 		uk_cd.cd_ndevs));
    188 
    189 	/*
    190 	 * Only allow one at a time
    191 	 */
    192 	if (periph->periph_flags & PERIPH_OPEN) {
    193 		printf("%s: already open\n", uk->sc_dev.dv_xname);
    194 		return (EBUSY);
    195 	}
    196 
    197 	if ((error = scsipi_adapter_addref(adapt)) != 0)
    198 		return (error);
    199 	periph->periph_flags |= PERIPH_OPEN;
    200 
    201 	SC_DEBUG(periph, SCSIPI_DB3, ("open complete\n"));
    202 	return (0);
    203 }
    204 
    205 /*
    206  * close the device.. only called if we are the LAST
    207  * occurence of an open device
    208  */
    209 int
    210 ukclose(dev, flag, fmt, p)
    211 	dev_t dev;
    212 	int flag, fmt;
    213 	struct proc *p;
    214 {
    215 	struct uk_softc *uk = uk_cd.cd_devs[UKUNIT(dev)];
    216 	struct scsipi_periph *periph = uk->sc_periph;
    217 	struct scsipi_adapter *adapt = periph->periph_channel->chan_adapter;
    218 
    219 	SC_DEBUG(uk->sc_periph, SCSIPI_DB1, ("closing\n"));
    220 
    221 	scsipi_wait_drain(periph);
    222 
    223 	scsipi_adapter_delref(adapt);
    224 	periph->periph_flags &= ~PERIPH_OPEN;
    225 
    226 	return (0);
    227 }
    228 
    229 /*
    230  * Perform special action on behalf of the user
    231  * Only does generic scsi ioctls.
    232  */
    233 int
    234 ukioctl(dev, cmd, addr, flag, p)
    235 	dev_t dev;
    236 	u_long cmd;
    237 	caddr_t addr;
    238 	int flag;
    239 	struct proc *p;
    240 {
    241 	register struct uk_softc *uk = uk_cd.cd_devs[UKUNIT(dev)];
    242 
    243 	return (scsipi_do_ioctl(uk->sc_periph, dev, cmd, addr, flag, p));
    244 }
    245