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