Home | History | Annotate | Line # | Download | only in scsipi
uk.c revision 1.33.8.1
      1 /*	$NetBSD: uk.c,v 1.33.8.1 2002/05/16 11:41:21 gehenna 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/cdefs.h>
     45 __KERNEL_RCSID(0, "$NetBSD: uk.c,v 1.33.8.1 2002/05/16 11:41:21 gehenna Exp $");
     46 
     47 #include <sys/param.h>
     48 #include <sys/systm.h>
     49 
     50 #include <sys/errno.h>
     51 #include <sys/ioctl.h>
     52 #include <sys/device.h>
     53 #include <sys/conf.h>
     54 #include <sys/vnode.h>
     55 
     56 #include <dev/scsipi/scsi_all.h>
     57 #include <dev/scsipi/scsipi_all.h>
     58 #include <dev/scsipi/scsiconf.h>
     59 
     60 #define	UKUNIT(z)	(minor(z))
     61 
     62 struct uk_softc {
     63 	struct device sc_dev;
     64 
     65 	struct scsipi_periph *sc_periph; /* all the inter level info */
     66 };
     67 
     68 int ukmatch __P((struct device *, struct cfdata *, void *));
     69 void ukattach __P((struct device *, struct device *, void *));
     70 int ukactivate __P((struct device *, enum devact));
     71 int ukdetach __P((struct device *, int));
     72 
     73 
     74 struct cfattach uk_scsibus_ca = {
     75 	sizeof(struct uk_softc), ukmatch, ukattach, ukdetach, ukactivate,
     76 };
     77 struct cfattach uk_atapibus_ca = {
     78 	sizeof(struct uk_softc), ukmatch, ukattach, ukdetach, ukactivate,
     79 };
     80 
     81 extern struct cfdriver uk_cd;
     82 
     83 dev_type_open(ukopen);
     84 dev_type_close(ukclose);
     85 dev_type_ioctl(ukioctl);
     86 
     87 const struct cdevsw uk_cdevsw = {
     88 	ukopen, ukclose, noread, nowrite, ukioctl,
     89 	nostop, notty, nopoll, nommap,
     90 };
     91 
     92 int
     93 ukmatch(parent, match, aux)
     94 	struct device *parent;
     95 	struct cfdata *match;
     96 	void *aux;
     97 {
     98 
     99 	return (1);
    100 }
    101 
    102 /*
    103  * The routine called by the low level scsi routine when it discovers
    104  * a device suitable for this driver.
    105  */
    106 void
    107 ukattach(parent, self, aux)
    108 	struct device *parent, *self;
    109 	void *aux;
    110 {
    111 	struct uk_softc *uk = (void *)self;
    112 	struct scsipibus_attach_args *sa = aux;
    113 	struct scsipi_periph *periph = sa->sa_periph;
    114 
    115 	SC_DEBUG(periph, SCSIPI_DB2, ("ukattach: "));
    116 
    117 	/*
    118 	 * Store information needed to contact our base driver
    119 	 */
    120 	uk->sc_periph = periph;
    121 	periph->periph_dev = &uk->sc_dev;
    122 
    123 	printf("\n");
    124 }
    125 
    126 int
    127 ukactivate(self, act)
    128 	struct device *self;
    129 	enum devact act;
    130 {
    131 	int rv = 0;
    132 
    133 	switch (act) {
    134 	case DVACT_ACTIVATE:
    135 		rv = EOPNOTSUPP;
    136 		break;
    137 
    138 	case DVACT_DEACTIVATE:
    139 		/*
    140 		 * Nothing to do; we key off the device's DVF_ACTIVE.
    141 		 */
    142 		break;
    143 	}
    144 	return (rv);
    145 }
    146 
    147 int
    148 ukdetach(self, flags)
    149 	struct device *self;
    150 	int flags;
    151 {
    152 	/*struct uk_softc *uk = (struct uk_softc *) self;*/
    153 	int cmaj, mn;
    154 
    155 	/* locate the major number */
    156 	cmaj = cdevsw_lookup_major(&uk_cdevsw);
    157 
    158 	/* Nuke the vnodes for any open instances */
    159 	mn = self->dv_unit;
    160 	vdevgone(cmaj, mn, mn, VCHR);
    161 
    162 	return (0);
    163 }
    164 
    165 
    166 
    167 /*
    168  * open the device.
    169  */
    170 int
    171 ukopen(dev, flag, fmt, p)
    172 	dev_t dev;
    173 	int flag, fmt;
    174 	struct proc *p;
    175 {
    176 	int unit, error;
    177 	struct uk_softc *uk;
    178 	struct scsipi_periph *periph;
    179 	struct scsipi_adapter *adapt;
    180 
    181 	unit = UKUNIT(dev);
    182 	if (unit >= uk_cd.cd_ndevs)
    183 		return (ENXIO);
    184 	uk = uk_cd.cd_devs[unit];
    185 	if (uk == NULL)
    186 		return (ENXIO);
    187 
    188 	periph = uk->sc_periph;
    189 	adapt = periph->periph_channel->chan_adapter;
    190 
    191 	SC_DEBUG(periph, SCSIPI_DB1,
    192 	    ("ukopen: dev=0x%x (unit %d (of %d))\n", dev, unit,
    193 		uk_cd.cd_ndevs));
    194 
    195 	/*
    196 	 * Only allow one at a time
    197 	 */
    198 	if (periph->periph_flags & PERIPH_OPEN) {
    199 		printf("%s: already open\n", uk->sc_dev.dv_xname);
    200 		return (EBUSY);
    201 	}
    202 
    203 	if ((error = scsipi_adapter_addref(adapt)) != 0)
    204 		return (error);
    205 	periph->periph_flags |= PERIPH_OPEN;
    206 
    207 	SC_DEBUG(periph, SCSIPI_DB3, ("open complete\n"));
    208 	return (0);
    209 }
    210 
    211 /*
    212  * close the device.. only called if we are the LAST
    213  * occurence of an open device
    214  */
    215 int
    216 ukclose(dev, flag, fmt, p)
    217 	dev_t dev;
    218 	int flag, fmt;
    219 	struct proc *p;
    220 {
    221 	struct uk_softc *uk = uk_cd.cd_devs[UKUNIT(dev)];
    222 	struct scsipi_periph *periph = uk->sc_periph;
    223 	struct scsipi_adapter *adapt = periph->periph_channel->chan_adapter;
    224 
    225 	SC_DEBUG(uk->sc_periph, SCSIPI_DB1, ("closing\n"));
    226 
    227 	scsipi_wait_drain(periph);
    228 
    229 	scsipi_adapter_delref(adapt);
    230 	periph->periph_flags &= ~PERIPH_OPEN;
    231 
    232 	return (0);
    233 }
    234 
    235 /*
    236  * Perform special action on behalf of the user
    237  * Only does generic scsi ioctls.
    238  */
    239 int
    240 ukioctl(dev, cmd, addr, flag, p)
    241 	dev_t dev;
    242 	u_long cmd;
    243 	caddr_t addr;
    244 	int flag;
    245 	struct proc *p;
    246 {
    247 	register struct uk_softc *uk = uk_cd.cd_devs[UKUNIT(dev)];
    248 
    249 	return (scsipi_do_ioctl(uk->sc_periph, dev, cmd, addr, flag, p));
    250 }
    251