uk.c revision 1.52.36.1 1 /* $NetBSD: uk.c,v 1.52.36.1 2008/04/05 23:33:22 mjf 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/cdefs.h>
45 __KERNEL_RCSID(0, "$NetBSD: uk.c,v 1.52.36.1 2008/04/05 23:33:22 mjf 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, 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 static void
101 ukattach(struct device *parent, 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 int maj;
107
108 SC_DEBUG(periph, SCSIPI_DB2, ("ukattach: "));
109
110 /*
111 * Store information needed to contact our base driver
112 */
113 uk->sc_periph = periph;
114 periph->periph_dev = &uk->sc_dev;
115
116 printf("\n");
117
118 maj = cdevsw_lookup_major(&uk_cdevsw);
119 device_register_name(makedev(maj, device_unit(self)), self, true,
120 DEV_OTHER, device_xname(self));
121 }
122
123 static int
124 ukactivate(struct device *self, 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 static int
143 ukdetach(struct device *self, int flags)
144 {
145 /*struct uk_softc *uk = device_private(self);*/
146 int cmaj, mn;
147
148 device_unregister_all(self);
149
150 /* locate the major number */
151 cmaj = cdevsw_lookup_major(&uk_cdevsw);
152
153 /* Nuke the vnodes for any open instances */
154 mn = device_unit(self);
155 vdevgone(cmaj, mn, mn, VCHR);
156
157 return (0);
158 }
159
160 /*
161 * open the device.
162 */
163 static int
164 ukopen(dev_t dev, int flag, int fmt, struct lwp *l)
165 {
166 int unit, error;
167 struct uk_softc *uk;
168 struct scsipi_periph *periph;
169 struct scsipi_adapter *adapt;
170
171 unit = UKUNIT(dev);
172 if (unit >= uk_cd.cd_ndevs)
173 return (ENXIO);
174 uk = uk_cd.cd_devs[unit];
175 if (uk == NULL)
176 return (ENXIO);
177
178 periph = uk->sc_periph;
179 adapt = periph->periph_channel->chan_adapter;
180
181 SC_DEBUG(periph, SCSIPI_DB1,
182 ("ukopen: dev=0x%x (unit %d (of %d))\n", dev, unit,
183 uk_cd.cd_ndevs));
184
185 /*
186 * Only allow one at a time
187 */
188 if (periph->periph_flags & PERIPH_OPEN) {
189 printf("%s: already open\n", uk->sc_dev.dv_xname);
190 return (EBUSY);
191 }
192
193 if ((error = scsipi_adapter_addref(adapt)) != 0)
194 return (error);
195 periph->periph_flags |= PERIPH_OPEN;
196
197 SC_DEBUG(periph, SCSIPI_DB3, ("open complete\n"));
198 return (0);
199 }
200
201 /*
202 * close the device.. only called if we are the LAST
203 * occurence of an open device
204 */
205 static int
206 ukclose(dev_t dev, int flag, int fmt, struct lwp *l)
207 {
208 struct uk_softc *uk = uk_cd.cd_devs[UKUNIT(dev)];
209 struct scsipi_periph *periph = uk->sc_periph;
210 struct scsipi_adapter *adapt = periph->periph_channel->chan_adapter;
211
212 SC_DEBUG(uk->sc_periph, SCSIPI_DB1, ("closing\n"));
213
214 scsipi_wait_drain(periph);
215
216 scsipi_adapter_delref(adapt);
217 periph->periph_flags &= ~PERIPH_OPEN;
218
219 return (0);
220 }
221
222 /*
223 * Perform special action on behalf of the user
224 * Only does generic scsi ioctls.
225 */
226 static int
227 ukioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
228 {
229 register struct uk_softc *uk = uk_cd.cd_devs[UKUNIT(dev)];
230
231 return (scsipi_do_ioctl(uk->sc_periph, dev, cmd, addr, flag, l));
232 }
233