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