Home | History | Annotate | Line # | Download | only in scsipi
uk.c revision 1.30.4.2
      1 /*	$NetBSD: uk.c,v 1.30.4.2 2001/09/26 15:28:19 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/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 <miscfs/specfs/specdev.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 cdev_decl(uk);
     84 
     85 int
     86 ukmatch(parent, match, aux)
     87 	struct device *parent;
     88 	struct cfdata *match;
     89 	void *aux;
     90 {
     91 
     92 	return (1);
     93 }
     94 
     95 /*
     96  * The routine called by the low level scsi routine when it discovers
     97  * a device suitable for this driver.
     98  */
     99 void
    100 ukattach(parent, self, aux)
    101 	struct device *parent, *self;
    102 	void *aux;
    103 {
    104 	struct uk_softc *uk = (void *)self;
    105 	struct scsipibus_attach_args *sa = aux;
    106 	struct scsipi_periph *periph = sa->sa_periph;
    107 
    108 	SC_DEBUG(periph, SCSIPI_DB2, ("ukattach: "));
    109 
    110 	/*
    111 	 * Store information needed to contact our base driver
    112 	 */
    113 	uk->sc_periph = periph;
    114 	periph->periph_dev = &uk->sc_dev;
    115 
    116 	printf("\n");
    117 	printf("%s: unknown device\n", uk->sc_dev.dv_xname);
    118 }
    119 
    120 int
    121 ukactivate(self, act)
    122 	struct device *self;
    123 	enum devact act;
    124 {
    125 	int rv = 0;
    126 
    127 	switch (act) {
    128 	case DVACT_ACTIVATE:
    129 		rv = EOPNOTSUPP;
    130 		break;
    131 
    132 	case DVACT_DEACTIVATE:
    133 		/*
    134 		 * Nothing to do; we key off the device's DVF_ACTIVE.
    135 		 */
    136 		break;
    137 	}
    138 	return (rv);
    139 }
    140 
    141 int
    142 ukdetach(self, flags)
    143 	struct device *self;
    144 	int flags;
    145 {
    146 	/*struct uk_softc *uk = (struct uk_softc *) self;*/
    147 	int cmaj, mn;
    148 
    149 	/* locate the major number */
    150 	for (cmaj = 0; cmaj <= nchrdev; cmaj++)
    151 		if (cdevsw[cmaj].d_open == ukopen)
    152 			break;
    153 
    154 	/* Nuke the vnodes for any open instances */
    155 	mn = self->dv_unit;
    156 	vdevgone(cmaj, mn, mn, VCHR);
    157 
    158 	return (0);
    159 }
    160 
    161 
    162 
    163 /*
    164  * open the device.
    165  */
    166 int
    167 ukopen(devvp, flag, fmt, p)
    168 	struct vnode *devvp;
    169 	int flag, fmt;
    170 	struct proc *p;
    171 {
    172 	int unit, error;
    173 	struct uk_softc *uk;
    174 	struct scsipi_periph *periph;
    175 	struct scsipi_adapter *adapt;
    176 	dev_t rdev;
    177 
    178 	rdev = vdev_rdev(devvp);
    179 	unit = UKUNIT(rdev);
    180 	if (unit >= uk_cd.cd_ndevs)
    181 		return (ENXIO);
    182 	uk = uk_cd.cd_devs[unit];
    183 	if (uk == NULL)
    184 		return (ENXIO);
    185 
    186 	vdev_setprivdata(devvp, uk);
    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", rdev, 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(devvp, flag, fmt, p)
    217 	struct vnode *devvp;
    218 	int flag, fmt;
    219 	struct proc *p;
    220 {
    221 	struct uk_softc *uk;
    222 	struct scsipi_periph *periph;
    223 	struct scsipi_adapter *adapt;
    224 
    225 	uk = vdev_privdata(devvp);
    226 	periph = uk->sc_periph;
    227 	adapt = periph->periph_channel->chan_adapter;
    228 
    229 	SC_DEBUG(uk->sc_periph, SCSIPI_DB1, ("closing\n"));
    230 
    231 	scsipi_wait_drain(periph);
    232 
    233 	scsipi_adapter_delref(adapt);
    234 	periph->periph_flags &= ~PERIPH_OPEN;
    235 
    236 	return (0);
    237 }
    238 
    239 /*
    240  * Perform special action on behalf of the user
    241  * Only does generic scsi ioctls.
    242  */
    243 int
    244 ukioctl(devvp, cmd, addr, flag, p)
    245 	struct vnode *devvp;
    246 	u_long cmd;
    247 	caddr_t addr;
    248 	int flag;
    249 	struct proc *p;
    250 {
    251 	struct uk_softc *uk;
    252 
    253 	uk = vdev_privdata(devvp);
    254 
    255 	return (scsipi_do_ioctl(uk->sc_periph, devvp, cmd, addr, flag, p));
    256 }
    257