LINT build test. Aggregated source code adjustments to bring most of the
[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 $
7b95be2a 46 * $DragonFly: src/sys/dev/misc/spigot/spigot.c,v 1.5 2003/07/21 07:57:45 dillon Exp $
984263bc
MD
47 *
48 */
49
50#include "spigot.h"
51
52#if NSPIGOT > 1
53error "Can only have 1 spigot configured."
54#endif
55
56#include "opt_spigot.h"
57
58#include <sys/param.h>
59#include <sys/systm.h>
60#include <sys/conf.h>
61#include <sys/proc.h>
62#include <sys/signalvar.h>
63#include <sys/mman.h>
64
65#include <machine/frame.h>
66#include <machine/md_var.h>
67#include <machine/spigot.h>
68#include <machine/psl.h>
69
70#include <i386/isa/isa_device.h>
71
72static struct spigot_softc {
73 u_long flags;
74 u_long maddr;
75 struct proc *p;
76 u_long signal_num;
77 u_short irq;
78} spigot_softc[NSPIGOT];
79
80/* flags in softc */
81#define OPEN 0x01
82#define ALIVE 0x02
83
84#define UNIT(dev) minor(dev)
85
86static int spigot_probe(struct isa_device *id);
87static int spigot_attach(struct isa_device *id);
88
89struct isa_driver spigotdriver = {spigot_probe, spigot_attach, "spigot"};
90
91static d_open_t spigot_open;
92static d_close_t spigot_close;
93static d_read_t spigot_read;
94static d_write_t spigot_write;
95static d_ioctl_t spigot_ioctl;
96static d_mmap_t spigot_mmap;
97
98#define CDEV_MAJOR 11
99static struct cdevsw spigot_cdevsw = {
fabb8ceb
MD
100 /* name */ "spigot",
101 /* maj */ CDEV_MAJOR,
102 /* flags */ 0,
103 /* port */ NULL,
104 /* autoq */ 0,
105
984263bc
MD
106 /* open */ spigot_open,
107 /* close */ spigot_close,
108 /* read */ spigot_read,
109 /* write */ spigot_write,
110 /* ioctl */ spigot_ioctl,
111 /* poll */ nopoll,
112 /* mmap */ spigot_mmap,
113 /* strategy */ nostrategy,
984263bc 114 /* dump */ nodump,
fabb8ceb 115 /* psize */ nopsize
984263bc
MD
116};
117
118static ointhand2_t spigintr;
119
120static int
121spigot_probe(struct isa_device *devp)
122{
123int status;
124struct spigot_softc *ss=(struct spigot_softc *)&spigot_softc[devp->id_unit];
125static int once;
126
127 if (!once++)
128 cdevsw_add(&spigot_cdevsw);
129
130 ss->flags = 0;
131 ss->maddr = 0;
132 ss->irq = 0;
133
134 if(devp->id_iobase != 0xad6 || inb(0xad9) == 0xff)
135 status = 0; /* not found */
136 else {
137 status = 1; /* found */
138 ss->flags |= ALIVE;
139 }
140
141 return(status);
142}
143
144static int
145spigot_attach(struct isa_device *devp)
146{
147 int unit;
148 struct spigot_softc *ss= &spigot_softc[unit = devp->id_unit];
149
150 devp->id_ointr = spigintr;
151 ss->maddr = kvtop(devp->id_maddr);
152 ss->irq = devp->id_irq;
153 make_dev(&spigot_cdevsw, unit, 0, 0, 0644, "spigot%d", unit);
154 return 1;
155}
156
157static int
7b95be2a 158spigot_open(dev_t dev, int flags, int fmt, struct thread *td)
984263bc
MD
159{
160int error;
161struct spigot_softc *ss = (struct spigot_softc *)&spigot_softc[UNIT(dev)];
162
163 if((ss->flags & ALIVE) == 0)
164 return ENXIO;
165
166 if(ss->flags & OPEN)
167 return EBUSY;
168
169#if !defined(SPIGOT_UNSECURE)
170 /*
171 * Don't allow open() unless the process has sufficient privileges,
172 * since mapping the i/o page and granting i/o privilege would
173 * require sufficient privilege soon and nothing much can be done
174 * without them.
175 */
dadab5e9 176 error = suser(td);
984263bc
MD
177 if (error != 0)
178 return error;
179 if (securelevel > 0)
180 return EPERM;
181#endif
182
183 ss->flags |= OPEN;
184 ss->p = 0;
185 ss->signal_num = 0;
186
187 return 0;
188}
189
190static int
7b95be2a 191spigot_close(dev_t dev, int flags, int fmt, struct thread *td)
984263bc
MD
192{
193struct spigot_softc *ss = (struct spigot_softc *)&spigot_softc[UNIT(dev)];
194
195 ss->flags &= ~OPEN;
196 ss->p = 0;
197 ss->signal_num = 0;
198
199 outb(0xad6, 0);
200
201 return 0;
202}
203
204static int
205spigot_write(dev_t dev, struct uio *uio, int ioflag)
206{
207 return ENXIO;
208}
209
210static int
211spigot_read(dev_t dev, struct uio *uio, int ioflag)
212{
213 return ENXIO;
214}
215
216
217static int
7b95be2a 218spigot_ioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct thread *td)
984263bc
MD
219{
220int error;
221struct spigot_softc *ss = (struct spigot_softc *)&spigot_softc[UNIT(dev)];
222struct spigot_info *info;
223
224 if(!data) return(EINVAL);
225 switch(cmd){
226 case SPIGOT_SETINT:
7b95be2a 227 ss->p = td->td_proc;
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)
dadab5e9 232 error = suser(td);
984263bc
MD
233 if (error != 0)
234 return error;
235 if (securelevel > 0)
236 return EPERM;
237#endif
7b95be2a 238 td->td_proc->p_md.md_regs->tf_eflags |= PSL_IOPL;
984263bc
MD
239 break;
240 case SPIGOT_IOPL_OFF: /* deny access to the IO PAGE */
7b95be2a 241 td->td_proc->p_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
260spigintr(int unit)
261{
262struct spigot_softc *ss = (struct spigot_softc *)&spigot_softc[unit];
263
264 if(ss->p && ss->signal_num)
265 psignal(ss->p, ss->signal_num);
266}
267
268static int
269spigot_mmap(dev_t dev, vm_offset_t offset, int nprot)
270{
271struct spigot_softc *ss = (struct spigot_softc *)&spigot_softc[0];
272
273 if(offset != 0) {
274 printf("spigot mmap failed, offset = 0x%x != 0x0\n", offset);
275 return -1;
276 }
277
278 if(nprot & PROT_EXEC)
279 return -1;
280
281 return i386_btop(ss->maddr);
282}