1 /* $NetBSD: uk.c,v 1.31 2001/11/13 06:56:41 lukem 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.31 2001/11/13 06:56:41 lukem Exp $"); 46 47 #include <sys/types.h> 48 #include <sys/param.h> 49 #include <sys/systm.h> 50 51 #include <sys/errno.h> 52 #include <sys/ioctl.h> 53 #include <sys/device.h> 54 #include <sys/conf.h> 55 #include <sys/vnode.h> 56 57 #include <dev/scsipi/scsi_all.h> 58 #include <dev/scsipi/scsipi_all.h> 59 #include <dev/scsipi/scsiconf.h> 60 61 #define UKUNIT(z) (minor(z)) 62 63 struct uk_softc { 64 struct device sc_dev; 65 66 struct scsipi_periph *sc_periph; /* all the inter level info */ 67 }; 68 69 int ukmatch __P((struct device *, struct cfdata *, void *)); 70 void ukattach __P((struct device *, struct device *, void *)); 71 int ukactivate __P((struct device *, enum devact)); 72 int ukdetach __P((struct device *, int)); 73 74 75 struct cfattach uk_scsibus_ca = { 76 sizeof(struct uk_softc), ukmatch, ukattach, ukdetach, ukactivate, 77 }; 78 struct cfattach uk_atapibus_ca = { 79 sizeof(struct uk_softc), ukmatch, ukattach, ukdetach, ukactivate, 80 }; 81 82 extern struct cfdriver uk_cd; 83 84 cdev_decl(uk); 85 86 int 87 ukmatch(parent, match, aux) 88 struct device *parent; 89 struct cfdata *match; 90 void *aux; 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 void 101 ukattach(parent, self, aux) 102 struct device *parent, *self; 103 void *aux; 104 { 105 struct uk_softc *uk = (void *)self; 106 struct scsipibus_attach_args *sa = aux; 107 struct scsipi_periph *periph = sa->sa_periph; 108 109 SC_DEBUG(periph, SCSIPI_DB2, ("ukattach: ")); 110 111 /* 112 * Store information needed to contact our base driver 113 */ 114 uk->sc_periph = periph; 115 periph->periph_dev = &uk->sc_dev; 116 117 printf("\n"); 118 printf("%s: unknown device\n", uk->sc_dev.dv_xname); 119 } 120 121 int 122 ukactivate(self, act) 123 struct device *self; 124 enum devact act; 125 { 126 int rv = 0; 127 128 switch (act) { 129 case DVACT_ACTIVATE: 130 rv = EOPNOTSUPP; 131 break; 132 133 case DVACT_DEACTIVATE: 134 /* 135 * Nothing to do; we key off the device's DVF_ACTIVE. 136 */ 137 break; 138 } 139 return (rv); 140 } 141 142 int 143 ukdetach(self, flags) 144 struct device *self; 145 int flags; 146 { 147 /*struct uk_softc *uk = (struct uk_softc *) self;*/ 148 int cmaj, mn; 149 150 /* locate the major number */ 151 for (cmaj = 0; cmaj <= nchrdev; cmaj++) 152 if (cdevsw[cmaj].d_open == ukopen) 153 break; 154 155 /* Nuke the vnodes for any open instances */ 156 mn = self->dv_unit; 157 vdevgone(cmaj, mn, mn, VCHR); 158 159 return (0); 160 } 161 162 163 164 /* 165 * open the device. 166 */ 167 int 168 ukopen(dev, flag, fmt, p) 169 dev_t dev; 170 int flag, fmt; 171 struct proc *p; 172 { 173 int unit, error; 174 struct uk_softc *uk; 175 struct scsipi_periph *periph; 176 struct scsipi_adapter *adapt; 177 178 unit = UKUNIT(dev); 179 if (unit >= uk_cd.cd_ndevs) 180 return (ENXIO); 181 uk = uk_cd.cd_devs[unit]; 182 if (uk == NULL) 183 return (ENXIO); 184 185 periph = uk->sc_periph; 186 adapt = periph->periph_channel->chan_adapter; 187 188 SC_DEBUG(periph, SCSIPI_DB1, 189 ("ukopen: dev=0x%x (unit %d (of %d))\n", dev, unit, 190 uk_cd.cd_ndevs)); 191 192 /* 193 * Only allow one at a time 194 */ 195 if (periph->periph_flags & PERIPH_OPEN) { 196 printf("%s: already open\n", uk->sc_dev.dv_xname); 197 return (EBUSY); 198 } 199 200 if ((error = scsipi_adapter_addref(adapt)) != 0) 201 return (error); 202 periph->periph_flags |= PERIPH_OPEN; 203 204 SC_DEBUG(periph, SCSIPI_DB3, ("open complete\n")); 205 return (0); 206 } 207 208 /* 209 * close the device.. only called if we are the LAST 210 * occurence of an open device 211 */ 212 int 213 ukclose(dev, flag, fmt, p) 214 dev_t dev; 215 int flag, fmt; 216 struct proc *p; 217 { 218 struct uk_softc *uk = uk_cd.cd_devs[UKUNIT(dev)]; 219 struct scsipi_periph *periph = uk->sc_periph; 220 struct scsipi_adapter *adapt = periph->periph_channel->chan_adapter; 221 222 SC_DEBUG(uk->sc_periph, SCSIPI_DB1, ("closing\n")); 223 224 scsipi_wait_drain(periph); 225 226 scsipi_adapter_delref(adapt); 227 periph->periph_flags &= ~PERIPH_OPEN; 228 229 return (0); 230 } 231 232 /* 233 * Perform special action on behalf of the user 234 * Only does generic scsi ioctls. 235 */ 236 int 237 ukioctl(dev, cmd, addr, flag, p) 238 dev_t dev; 239 u_long cmd; 240 caddr_t addr; 241 int flag; 242 struct proc *p; 243 { 244 register struct uk_softc *uk = uk_cd.cd_devs[UKUNIT(dev)]; 245 246 return (scsipi_do_ioctl(uk->sc_periph, dev, cmd, addr, flag, p)); 247 } 248