Line data Source code
1 : /* $OpenBSD: uk.c,v 1.19 2017/09/08 05:36:53 deraadt Exp $ */
2 : /* $NetBSD: uk.c,v 1.15 1996/03/17 00:59:57 thorpej Exp $ */
3 :
4 : /*
5 : * Copyright (c) 1994 Charles Hannum. All rights reserved.
6 : *
7 : * Redistribution and use in source and binary forms, with or without
8 : * modification, are permitted provided that the following conditions
9 : * are met:
10 : * 1. Redistributions of source code must retain the above copyright
11 : * notice, this list of conditions and the following disclaimer.
12 : * 2. Redistributions in binary form must reproduce the above copyright
13 : * notice, this list of conditions and the following disclaimer in the
14 : * documentation and/or other materials provided with the distribution.
15 : * 3. All advertising materials mentioning features or use of this software
16 : * must display the following acknowledgement:
17 : * This product includes software developed by Charles Hannum.
18 : * 4. The name of the author may not be used to endorse or promote products
19 : * derived from this software without specific prior written permission.
20 : *
21 : * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 : * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 : * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 : * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 : * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 : * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 : * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 : * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 : * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 : * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 : */
32 :
33 : /*
34 : * Dummy driver for a device we can't identify.
35 : * Originally by Julian Elischer (julian@tfs.com)
36 : */
37 :
38 : #include <sys/param.h>
39 : #include <sys/systm.h>
40 : #include <sys/errno.h>
41 : #include <sys/ioctl.h>
42 : #include <sys/conf.h>
43 : #include <sys/device.h>
44 : #include <sys/vnode.h>
45 :
46 : #include <scsi/scsi_all.h>
47 : #include <scsi/scsiconf.h>
48 :
49 : #define UKUNIT(z) (minor(z))
50 :
51 : struct uk_softc {
52 : struct device sc_dev;
53 : struct scsi_link *sc_link; /* all the inter level info */
54 : };
55 :
56 : int ukmatch(struct device *, void *, void *);
57 : void ukattach(struct device *, struct device *, void *);
58 : int ukdetach(struct device *, int);
59 :
60 : struct cfattach uk_ca = {
61 : sizeof(struct uk_softc), ukmatch, ukattach, ukdetach
62 : };
63 :
64 : struct cfdriver uk_cd = {
65 : NULL, "uk", DV_DULL
66 : };
67 :
68 : #define uklookup(unit) (struct uk_softc *)device_lookup(&uk_cd, (unit))
69 :
70 : int
71 0 : ukmatch(struct device *parent, void *match, void *aux)
72 : {
73 0 : return (1);
74 : }
75 :
76 : /*
77 : * The routine called by the low level scsi routine when it discovers
78 : * a device suitable for this driver.
79 : */
80 : void
81 0 : ukattach(struct device *parent, struct device *self, void *aux)
82 : {
83 0 : struct uk_softc *sc = (void *)self;
84 0 : struct scsi_attach_args *sa = aux;
85 0 : struct scsi_link *link = sa->sa_sc_link;
86 :
87 : SC_DEBUG(link, SDEV_DB2, ("ukattach: "));
88 :
89 : /* Store information needed to contact our base driver */
90 0 : sc->sc_link = link;
91 0 : link->device_softc = sc;
92 0 : link->openings = 1;
93 :
94 0 : printf("\n");
95 0 : }
96 :
97 : int
98 0 : ukdetach(struct device *self, int flags)
99 : {
100 : int bmaj, cmaj, mn;
101 :
102 0 : mn = self->dv_unit;
103 :
104 0 : for (bmaj = 0; bmaj < nblkdev; bmaj++)
105 0 : if (bdevsw[bmaj].d_open == ukopen)
106 0 : vdevgone(bmaj, mn, mn, VBLK);
107 0 : for (cmaj = 0; cmaj < nchrdev; cmaj++)
108 0 : if (cdevsw[cmaj].d_open == ukopen)
109 0 : vdevgone(cmaj, mn, mn, VCHR);
110 :
111 0 : return (0);
112 : }
113 :
114 : /*
115 : * open the device.
116 : */
117 : int
118 0 : ukopen(dev_t dev, int flag, int fmt, struct proc *p)
119 : {
120 : int unit;
121 : struct uk_softc *sc;
122 : struct scsi_link *link;
123 :
124 0 : unit = UKUNIT(dev);
125 0 : sc = uklookup(unit);
126 0 : if (sc == NULL)
127 0 : return (ENXIO);
128 :
129 0 : link = sc->sc_link;
130 :
131 : SC_DEBUG(link, SDEV_DB1, ("ukopen: dev=0x%x (unit %d (of %d))\n",
132 : dev, unit, uk_cd.cd_ndevs));
133 :
134 : /* Only allow one at a time */
135 0 : if (link->flags & SDEV_OPEN) {
136 0 : device_unref(&sc->sc_dev);
137 0 : return (EBUSY);
138 : }
139 :
140 0 : link->flags |= SDEV_OPEN;
141 :
142 : SC_DEBUG(link, SDEV_DB3, ("open complete\n"));
143 :
144 0 : device_unref(&sc->sc_dev);
145 0 : return (0);
146 0 : }
147 :
148 : /*
149 : * close the device.. only called if we are the LAST
150 : * occurrence of an open device
151 : */
152 : int
153 0 : ukclose(dev_t dev, int flag, int fmt, struct proc *p)
154 : {
155 : struct uk_softc *sc;
156 :
157 0 : sc = uklookup(UKUNIT(dev));
158 0 : if (sc == NULL)
159 0 : return (ENXIO);
160 :
161 : SC_DEBUG(sc->sc_link, SDEV_DB1, ("closing\n"));
162 0 : sc->sc_link->flags &= ~SDEV_OPEN;
163 :
164 0 : device_unref(&sc->sc_dev);
165 0 : return (0);
166 0 : }
167 :
168 : /*
169 : * Perform special action on behalf of the user
170 : * Only does generic scsi ioctls.
171 : */
172 : int
173 0 : ukioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
174 : {
175 : int rv;
176 : struct uk_softc *sc;
177 :
178 0 : sc = uklookup(UKUNIT(dev));
179 0 : if (sc == NULL)
180 0 : return (ENXIO);
181 :
182 0 : rv = scsi_do_ioctl(sc->sc_link, cmd, addr, flag);
183 :
184 0 : device_unref(&sc->sc_dev);
185 0 : return (rv);
186 0 : }
|