gold: Fix hardcoded library search path
[dragonfly.git] / sys / dev / misc / spigot / spigot.c
CommitLineData
984263bc
MD
1/*
2 * Video spigot capture driver.
3 *
4 * Copyright (c) 1995, Jim Lowe. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met: 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 2.
10 * Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
18 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
21 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * This is the minimum driver code required to make a spigot work.
27 * Unfortunatly, I can't include a real driver since the information
28 * on the spigot is under non-disclosure. You can pick up a library
29 * that will work with this driver from
30 * ftp://ftp.cs.uwm.edu/pub/FreeBSD-UWM. The library contains the
31 * source that I can release as well as several object modules and
32 * functions that allows one to read spigot data. See the code for
33 * spigot_grab.c that is included with the library data.
34 *
35 * The vendor will not allow me to release the spigot library code.
36 * Please don't ask me for it.
37 *
38 * To use this driver you will need the spigot library. The library is
39 * available from:
40 *
41 * ftp.cs.uwm.edu://pub/FreeBSD-UWM/spigot/spigot.tar.gz
42 *
43 * Version 1.7, December 1995.
44 *
45 * $FreeBSD: src/sys/i386/isa/spigot.c,v 1.44 2000/01/29 16:17:36 peter Exp $
46 *
47 */
48
1f2de5d4 49#include "use_spigot.h"
984263bc
MD
50
51#if NSPIGOT > 1
52error "Can only have 1 spigot configured."
53#endif
54
55#include "opt_spigot.h"
56
57#include <sys/param.h>
58#include <sys/systm.h>
59#include <sys/conf.h>
fef8985e 60#include <sys/device.h>
984263bc 61#include <sys/proc.h>
895c1f85 62#include <sys/priv.h>
984263bc
MD
63#include <sys/signalvar.h>
64#include <sys/mman.h>
65
66#include <machine/frame.h>
67#include <machine/md_var.h>
68#include <machine/spigot.h>
69#include <machine/psl.h>
70
21ce0dfa 71#include <bus/isa/isa_device.h>
984263bc
MD
72
73static struct spigot_softc {
74 u_long flags;
75 u_long maddr;
76 struct proc *p;
77 u_long signal_num;
78 u_short irq;
79} spigot_softc[NSPIGOT];
80
81/* flags in softc */
82#define OPEN 0x01
83#define ALIVE 0x02
84
85#define UNIT(dev) minor(dev)
86
87static int spigot_probe(struct isa_device *id);
88static int spigot_attach(struct isa_device *id);
89
90struct isa_driver spigotdriver = {spigot_probe, spigot_attach, "spigot"};
91
92static d_open_t spigot_open;
93static d_close_t spigot_close;
94static d_read_t spigot_read;
95static d_write_t spigot_write;
96static d_ioctl_t spigot_ioctl;
97static d_mmap_t spigot_mmap;
98
fef8985e 99static struct dev_ops spigot_ops = {
88abd8b5 100 { "spigot", 0, 0 },
fef8985e
MD
101 .d_open = spigot_open,
102 .d_close = spigot_close,
103 .d_read = spigot_read,
104 .d_write = spigot_write,
105 .d_ioctl = spigot_ioctl,
106 .d_mmap = spigot_mmap,
984263bc
MD
107};
108
1b51b0fa 109static void spigintr(void *);
984263bc
MD
110
111static int
112spigot_probe(struct isa_device *devp)
113{
e4c9c0c8
MD
114 struct spigot_softc *ss;
115 int status;
984263bc 116
e4c9c0c8 117 ss = (struct spigot_softc *)&spigot_softc[devp->id_unit];
984263bc
MD
118 ss->flags = 0;
119 ss->maddr = 0;
120 ss->irq = 0;
121
122 if(devp->id_iobase != 0xad6 || inb(0xad9) == 0xff)
123 status = 0; /* not found */
124 else {
125 status = 1; /* found */
126 ss->flags |= ALIVE;
127 }
128
129 return(status);
130}
131
132static int
133spigot_attach(struct isa_device *devp)
134{
135 int unit;
136 struct spigot_softc *ss= &spigot_softc[unit = devp->id_unit];
137
1b51b0fa 138 devp->id_intr = (inthand2_t *)spigintr;
984263bc
MD
139 ss->maddr = kvtop(devp->id_maddr);
140 ss->irq = devp->id_irq;
fef8985e 141 make_dev(&spigot_ops, unit, 0, 0, 0644, "spigot%d", unit);
984263bc
MD
142 return 1;
143}
144
145static int
fef8985e 146spigot_open(struct dev_open_args *ap)
984263bc 147{
b13267a5 148 cdev_t dev = ap->a_head.a_dev;
fef8985e
MD
149 int error;
150 struct spigot_softc *ss;
151
152 ss = (struct spigot_softc *)&spigot_softc[UNIT(dev)];
984263bc
MD
153
154 if((ss->flags & ALIVE) == 0)
155 return ENXIO;
156
157 if(ss->flags & OPEN)
158 return EBUSY;
159
160#if !defined(SPIGOT_UNSECURE)
161 /*
162 * Don't allow open() unless the process has sufficient privileges,
163 * since mapping the i/o page and granting i/o privilege would
164 * require sufficient privilege soon and nothing much can be done
165 * without them.
166 */
895c1f85 167 error = priv_check_cred(ap->a_cred, PRIV_ROOT, 0);
984263bc
MD
168 if (error != 0)
169 return error;
170 if (securelevel > 0)
171 return EPERM;
172#endif
173
174 ss->flags |= OPEN;
175 ss->p = 0;
176 ss->signal_num = 0;
177
178 return 0;
179}
180
181static int
fef8985e 182spigot_close(struct dev_close_args *ap)
984263bc 183{
b13267a5 184 cdev_t dev = ap->a_head.a_dev;
fef8985e 185 struct spigot_softc *ss;
984263bc 186
fef8985e 187 ss = (struct spigot_softc *)&spigot_softc[UNIT(dev)];
984263bc
MD
188 ss->flags &= ~OPEN;
189 ss->p = 0;
190 ss->signal_num = 0;
191
192 outb(0xad6, 0);
193
194 return 0;
195}
196
197static int
fef8985e 198spigot_write(struct dev_write_args *ap)
984263bc
MD
199{
200 return ENXIO;
201}
202
203static int
fef8985e 204spigot_read(struct dev_read_args *ap)
984263bc
MD
205{
206 return ENXIO;
207}
208
209
210static int
fef8985e 211spigot_ioctl(struct dev_ioctl_args *ap)
984263bc 212{
b13267a5 213 cdev_t dev = ap->a_head.a_dev;
fef8985e
MD
214 caddr_t data = ap->a_data;
215 int error;
216 struct spigot_softc *ss;
217 struct spigot_info *info;
218
219 ss = (struct spigot_softc *)&spigot_softc[UNIT(dev)];
220 if (data == NULL)
221 return(EINVAL);
984263bc 222
fef8985e 223 switch(ap->a_cmd){
984263bc 224 case SPIGOT_SETINT:
d82eda3e
DR
225 if (*(int *)data < 0 || *(int *)data > _SIG_MAXSIG)
226 return (EINVAL);
fef8985e 227 ss->p = curproc;
984263bc
MD
228 ss->signal_num = *((int *)data);
229 break;
230 case SPIGOT_IOPL_ON: /* allow access to the IO PAGE */
231#if !defined(SPIGOT_UNSECURE)
895c1f85 232 error = priv_check_cred(ap->a_cred, PRIV_ROOT, 0);
984263bc
MD
233 if (error != 0)
234 return error;
235 if (securelevel > 0)
236 return EPERM;
237#endif
08f2f1bb 238 curthread->td_lwp->lwp_md.md_regs->tf_eflags |= PSL_IOPL;
984263bc
MD
239 break;
240 case SPIGOT_IOPL_OFF: /* deny access to the IO PAGE */
08f2f1bb 241 curthread->td_lwp->lwp_md.md_regs->tf_eflags &= ~PSL_IOPL;
984263bc
MD
242 break;
243 case SPIGOT_GET_INFO:
244 info = (struct spigot_info *)data;
245 info->maddr = ss->maddr;
246 info->irq = ss->irq;
247 break;
248 default:
249 return ENOTTY;
250 }
251
252 return 0;
253}
254
255/*
256 * Interrupt procedure.
257 * Just call a user level interrupt routine.
258 */
259static void
477d3c1c 260spigintr(void *arg)
984263bc 261{
477d3c1c 262 int unit = (int)arg;
fef8985e 263 struct spigot_softc *ss;
984263bc 264
fef8985e 265 ss = (struct spigot_softc *)&spigot_softc[unit];
984263bc 266 if(ss->p && ss->signal_num)
84204577 267 ksignal(ss->p, ss->signal_num);
984263bc
MD
268}
269
270static int
fef8985e 271spigot_mmap(struct dev_mmap_args *ap)
984263bc 272{
fef8985e 273 struct spigot_softc *ss;
984263bc 274
fef8985e
MD
275 ss = (struct spigot_softc *)&spigot_softc[0];
276 if (ap->a_offset != 0)
277 return EINVAL;
278 if (ap->a_nprot & PROT_EXEC)
279 return EINVAL;
280 ap->a_result = i386_btop(ss->maddr);
281 return(0);
984263bc 282}