Home | History | Annotate | Line # | Download | only in scsipi
      1 /*	$NetBSD: uk.c,v 1.65 2016/11/20 15:37:19 mlelstv 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  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Dummy driver for a device we can't identify.
     34  * Originally by Julian Elischer (julian (at) tfs.com)
     35  */
     36 
     37 #include <sys/cdefs.h>
     38 __KERNEL_RCSID(0, "$NetBSD: uk.c,v 1.65 2016/11/20 15:37:19 mlelstv Exp $");
     39 
     40 #include <sys/param.h>
     41 #include <sys/systm.h>
     42 
     43 #include <sys/errno.h>
     44 #include <sys/ioctl.h>
     45 #include <sys/device.h>
     46 #include <sys/conf.h>
     47 #include <sys/vnode.h>
     48 
     49 #include <dev/scsipi/scsi_all.h>
     50 #include <dev/scsipi/scsipi_all.h>
     51 #include <dev/scsipi/scsiconf.h>
     52 
     53 struct uk_softc {
     54 	device_t sc_dev;
     55 
     56 	struct scsipi_periph *sc_periph; /* all the inter level info */
     57 };
     58 
     59 static int	ukmatch(device_t, cfdata_t, void *);
     60 static void	ukattach(device_t, device_t, void *);
     61 static int	ukdetach(device_t, int);
     62 
     63 CFATTACH_DECL_NEW(
     64     uk,
     65     sizeof(struct uk_softc),
     66     ukmatch,
     67     ukattach,
     68     ukdetach,
     69     NULL
     70 );
     71 
     72 extern struct cfdriver uk_cd;
     73 
     74 static dev_type_open(ukopen);
     75 static dev_type_close(ukclose);
     76 static dev_type_ioctl(ukioctl);
     77 
     78 const struct cdevsw uk_cdevsw = {
     79 	.d_open = ukopen,
     80 	.d_close = ukclose,
     81 	.d_read = noread,
     82 	.d_write = nowrite,
     83 	.d_ioctl = ukioctl,
     84 	.d_stop = nostop,
     85 	.d_tty = notty,
     86 	.d_poll = nopoll,
     87 	.d_mmap = nommap,
     88 	.d_kqfilter = nokqfilter,
     89 	.d_discard = nodiscard,
     90 	.d_flag = D_OTHER | D_MPSAFE
     91 };
     92 
     93 static int
     94 ukmatch(device_t parent, cfdata_t match, void *aux)
     95 {
     96 	return 1;
     97 }
     98 
     99 /*
    100  * The routine called by the low level scsi routine when it discovers
    101  * a device suitable for this driver.
    102  */
    103 static void
    104 ukattach(device_t parent, device_t self, void *aux)
    105 {
    106 	struct uk_softc *uk = device_private(self);
    107 	struct scsipibus_attach_args *sa = aux;
    108 	struct scsipi_periph *periph = sa->sa_periph;
    109 
    110 	SC_DEBUG(periph, SCSIPI_DB2, ("ukattach: "));
    111 	uk->sc_dev = self;
    112 
    113 	/* Store information needed to contact our base driver */
    114 	uk->sc_periph = periph;
    115 	periph->periph_dev = uk->sc_dev;
    116 
    117 	aprint_naive("\n");
    118 	aprint_normal("\n");
    119 
    120 	if (!pmf_device_register(self, NULL, NULL))
    121 		aprint_error_dev(self, "couldn't establish power handler\n");
    122 }
    123 
    124 static int
    125 ukdetach(device_t self, int flags)
    126 {
    127 	int cmaj, mn;
    128 
    129 	/* locate the major number */
    130 	cmaj = cdevsw_lookup_major(&uk_cdevsw);
    131 
    132 	/* Nuke the vnodes for any open instances */
    133 	mn = device_unit(self);
    134 	vdevgone(cmaj, mn, mn, VCHR);
    135 
    136 	return 0;
    137 }
    138 
    139 static int
    140 ukopen(dev_t dev, int flag, int fmt, struct lwp *l)
    141 {
    142 	int unit, error;
    143 	struct uk_softc *uk;
    144 	struct scsipi_periph *periph;
    145 	struct scsipi_adapter *adapt;
    146 
    147 	unit = minor(dev);
    148 	uk = device_lookup_private(&uk_cd, unit);
    149 	if (uk == NULL)
    150 		return ENXIO;
    151 
    152 	periph = uk->sc_periph;
    153 	adapt = periph->periph_channel->chan_adapter;
    154 
    155 	SC_DEBUG(periph, SCSIPI_DB1,
    156 	    ("ukopen: dev=0x%"PRIx64" (unit %d (of %d))\n", dev, unit,
    157 		uk_cd.cd_ndevs));
    158 
    159 	/* Only allow one at a time */
    160 	if (periph->periph_flags & PERIPH_OPEN) {
    161 		aprint_error_dev(uk->sc_dev, "already open\n");
    162 		return EBUSY;
    163 	}
    164 
    165 	if ((error = scsipi_adapter_addref(adapt)) != 0)
    166 		return error;
    167 	periph->periph_flags |= PERIPH_OPEN;
    168 
    169 	SC_DEBUG(periph, SCSIPI_DB3, ("open complete\n"));
    170 	return 0;
    171 }
    172 
    173 static int
    174 ukclose(dev_t dev, int flag, int fmt, struct lwp *l)
    175 {
    176 	struct uk_softc *uk = device_lookup_private(&uk_cd, minor(dev));
    177 	struct scsipi_periph *periph = uk->sc_periph;
    178 	struct scsipi_adapter *adapt = periph->periph_channel->chan_adapter;
    179 
    180 	SC_DEBUG(uk->sc_periph, SCSIPI_DB1, ("closing\n"));
    181 
    182 	scsipi_wait_drain(periph);
    183 
    184 	scsipi_adapter_delref(adapt);
    185 	periph->periph_flags &= ~PERIPH_OPEN;
    186 
    187 	return 0;
    188 }
    189 
    190 /*
    191  * Perform special action on behalf of the user.
    192  * Only does generic scsi ioctls.
    193  */
    194 static int
    195 ukioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
    196 {
    197 	struct uk_softc *uk = device_lookup_private(&uk_cd, minor(dev));
    198 
    199 	return scsipi_do_ioctl(uk->sc_periph, dev, cmd, addr, flag, l);
    200 }
    201