Home | History | Annotate | Line # | Download | only in scsipi
uk.c revision 1.28
      1 /*	$NetBSD: uk.c,v 1.28 2000/03/29 18:11:44 augustss 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_link *sc_link;	/* 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 struct cfattach uk_scsibus_ca = {
     72 	sizeof(struct uk_softc), ukmatch, ukattach, ukdetach, ukactivate,
     73 
     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 /*
     82  * This driver is so simple it uses all the default services
     83  */
     84 struct scsipi_device uk_switch = {
     85 	NULL,
     86 	NULL,
     87 	NULL,
     88 	NULL,
     89 };
     90 
     91 cdev_decl(uk);
     92 
     93 int
     94 ukmatch(parent, match, aux)
     95 	struct device *parent;
     96 	struct cfdata *match;
     97 	void *aux;
     98 {
     99 
    100 	return (1);
    101 }
    102 
    103 /*
    104  * The routine called by the low level scsi routine when it discovers
    105  * a device suitable for this driver.
    106  */
    107 void
    108 ukattach(parent, self, aux)
    109 	struct device *parent, *self;
    110 	void *aux;
    111 {
    112 	struct uk_softc *uk = (void *)self;
    113 	struct scsipibus_attach_args *sa = aux;
    114 	struct scsipi_link *sc_link = sa->sa_sc_link;
    115 
    116 	SC_DEBUG(sc_link, SDEV_DB2, ("ukattach: "));
    117 
    118 	/*
    119 	 * Store information needed to contact our base driver
    120 	 */
    121 	uk->sc_link = sc_link;
    122 	sc_link->device = &uk_switch;
    123 	sc_link->device_softc = uk;
    124 	sc_link->openings = 1;
    125 
    126 	printf("\n");
    127 	printf("%s: unknown device\n", uk->sc_dev.dv_xname);
    128 }
    129 
    130 int
    131 ukactivate(self, act)
    132 	struct device *self;
    133 	enum devact act;
    134 {
    135 	int rv = 0;
    136 
    137 	switch (act) {
    138 	case DVACT_ACTIVATE:
    139 		rv = EOPNOTSUPP;
    140 		break;
    141 
    142 	case DVACT_DEACTIVATE:
    143 		/*
    144 		 * Nothing to do; we key off the device's DVF_ACTIVE.
    145 		 */
    146 		break;
    147 	}
    148 	return (rv);
    149 }
    150 
    151 int
    152 ukdetach(self, flags)
    153 	struct device *self;
    154 	int flags;
    155 {
    156 	/*struct uk_softc *uk = (struct uk_softc *) self;*/
    157 	int cmaj, mn;
    158 
    159 	/* locate the major number */
    160 	for (cmaj = 0; cmaj <= nchrdev; cmaj++)
    161 		if (cdevsw[cmaj].d_open == ukopen)
    162 			break;
    163 
    164 	/* Nuke the vnodes for any open instances */
    165 	mn = self->dv_unit;
    166 	vdevgone(cmaj, mn, mn, VCHR);
    167 
    168 	return (0);
    169 }
    170 
    171 /*
    172  * open the device.
    173  */
    174 int
    175 ukopen(dev, flag, fmt, p)
    176 	dev_t dev;
    177 	int flag, fmt;
    178 	struct proc *p;
    179 {
    180 	int unit, error;
    181 	struct uk_softc *uk;
    182 	struct scsipi_link *sc_link;
    183 
    184 	unit = UKUNIT(dev);
    185 	if (unit >= uk_cd.cd_ndevs)
    186 		return (ENXIO);
    187 	uk = uk_cd.cd_devs[unit];
    188 	if (uk == NULL)
    189 		return (ENXIO);
    190 
    191 	sc_link = uk->sc_link;
    192 
    193 	SC_DEBUG(sc_link, SDEV_DB1,
    194 	    ("ukopen: dev=0x%x (unit %d (of %d))\n", dev, unit,
    195 		uk_cd.cd_ndevs));
    196 
    197 	/*
    198 	 * Only allow one at a time
    199 	 */
    200 	if (sc_link->flags & SDEV_OPEN) {
    201 		printf("%s: already open\n", uk->sc_dev.dv_xname);
    202 		return (EBUSY);
    203 	}
    204 
    205 	if ((error = scsipi_adapter_addref(sc_link)) != 0)
    206 		return (error);
    207 	sc_link->flags |= SDEV_OPEN;
    208 
    209 	SC_DEBUG(sc_link, SDEV_DB3, ("open complete\n"));
    210 	return (0);
    211 }
    212 
    213 /*
    214  * close the device.. only called if we are the LAST
    215  * occurence of an open device
    216  */
    217 int
    218 ukclose(dev, flag, fmt, p)
    219 	dev_t dev;
    220 	int flag, fmt;
    221 	struct proc *p;
    222 {
    223 	struct uk_softc *uk = uk_cd.cd_devs[UKUNIT(dev)];
    224 
    225 	SC_DEBUG(uk->sc_link, SDEV_DB1, ("closing\n"));
    226 
    227 	scsipi_wait_drain(uk->sc_link);
    228 
    229 	scsipi_adapter_delref(uk->sc_link);
    230 	uk->sc_link->flags &= ~SDEV_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_link, dev, cmd, addr, flag, p));
    250 }
    251