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