| Commit | Line | Data |
|---|---|---|
| 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 $ | |
| 21ce0dfa | 46 | * $DragonFly: src/sys/dev/misc/spigot/spigot.c,v 1.16 2008/08/02 01:14:42 dillon Exp $ |
| 984263bc MD |
47 | * |
| 48 | */ | |
| 49 | ||
| 1f2de5d4 | 50 | #include "use_spigot.h" |
| 984263bc MD |
51 | |
| 52 | #if NSPIGOT > 1 | |
| 53 | error "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> | |
| fef8985e | 61 | #include <sys/device.h> |
| 984263bc | 62 | #include <sys/proc.h> |
| 895c1f85 | 63 | #include <sys/priv.h> |
| 984263bc MD |
64 | #include <sys/signalvar.h> |
| 65 | #include <sys/mman.h> | |
| 66 | ||
| 67 | #include <machine/frame.h> | |
| 68 | #include <machine/md_var.h> | |
| 69 | #include <machine/spigot.h> | |
| 70 | #include <machine/psl.h> | |
| 71 | ||
| 21ce0dfa | 72 | #include <bus/isa/isa_device.h> |
| 984263bc MD |
73 | |
| 74 | static struct spigot_softc { | |
| 75 | u_long flags; | |
| 76 | u_long maddr; | |
| 77 | struct proc *p; | |
| 78 | u_long signal_num; | |
| 79 | u_short irq; | |
| 80 | } spigot_softc[NSPIGOT]; | |
| 81 | ||
| 82 | /* flags in softc */ | |
| 83 | #define OPEN 0x01 | |
| 84 | #define ALIVE 0x02 | |
| 85 | ||
| 86 | #define UNIT(dev) minor(dev) | |
| 87 | ||
| 88 | static int spigot_probe(struct isa_device *id); | |
| 89 | static int spigot_attach(struct isa_device *id); | |
| 90 | ||
| 91 | struct isa_driver spigotdriver = {spigot_probe, spigot_attach, "spigot"}; | |
| 92 | ||
| 93 | static d_open_t spigot_open; | |
| 94 | static d_close_t spigot_close; | |
| 95 | static d_read_t spigot_read; | |
| 96 | static d_write_t spigot_write; | |
| 97 | static d_ioctl_t spigot_ioctl; | |
| 98 | static d_mmap_t spigot_mmap; | |
| 99 | ||
| 100 | #define CDEV_MAJOR 11 | |
| fef8985e MD |
101 | static struct dev_ops spigot_ops = { |
| 102 | { "spigot", CDEV_MAJOR, 0 }, | |
| 103 | .d_open = spigot_open, | |
| 104 | .d_close = spigot_close, | |
| 105 | .d_read = spigot_read, | |
| 106 | .d_write = spigot_write, | |
| 107 | .d_ioctl = spigot_ioctl, | |
| 108 | .d_mmap = spigot_mmap, | |
| 984263bc MD |
109 | }; |
| 110 | ||
| 1b51b0fa | 111 | static void spigintr(void *); |
| 984263bc MD |
112 | |
| 113 | static int | |
| 114 | spigot_probe(struct isa_device *devp) | |
| 115 | { | |
| e4c9c0c8 MD |
116 | struct spigot_softc *ss; |
| 117 | int status; | |
| 984263bc | 118 | |
| e4c9c0c8 | 119 | ss = (struct spigot_softc *)&spigot_softc[devp->id_unit]; |
| 984263bc MD |
120 | ss->flags = 0; |
| 121 | ss->maddr = 0; | |
| 122 | ss->irq = 0; | |
| 123 | ||
| 124 | if(devp->id_iobase != 0xad6 || inb(0xad9) == 0xff) | |
| 125 | status = 0; /* not found */ | |
| 126 | else { | |
| 127 | status = 1; /* found */ | |
| 128 | ss->flags |= ALIVE; | |
| 129 | } | |
| 130 | ||
| 131 | return(status); | |
| 132 | } | |
| 133 | ||
| 134 | static int | |
| 135 | spigot_attach(struct isa_device *devp) | |
| 136 | { | |
| 137 | int unit; | |
| 138 | struct spigot_softc *ss= &spigot_softc[unit = devp->id_unit]; | |
| 139 | ||
| 1b51b0fa | 140 | devp->id_intr = (inthand2_t *)spigintr; |
| 984263bc MD |
141 | ss->maddr = kvtop(devp->id_maddr); |
| 142 | ss->irq = devp->id_irq; | |
| fef8985e | 143 | make_dev(&spigot_ops, unit, 0, 0, 0644, "spigot%d", unit); |
| 984263bc MD |
144 | return 1; |
| 145 | } | |
| 146 | ||
| 147 | static int | |
| fef8985e | 148 | spigot_open(struct dev_open_args *ap) |
| 984263bc | 149 | { |
| b13267a5 | 150 | cdev_t dev = ap->a_head.a_dev; |
| fef8985e MD |
151 | int error; |
| 152 | struct spigot_softc *ss; | |
| 153 | ||
| 154 | ss = (struct spigot_softc *)&spigot_softc[UNIT(dev)]; | |
| 984263bc MD |
155 | |
| 156 | if((ss->flags & ALIVE) == 0) | |
| 157 | return ENXIO; | |
| 158 | ||
| 159 | if(ss->flags & OPEN) | |
| 160 | return EBUSY; | |
| 161 | ||
| 162 | #if !defined(SPIGOT_UNSECURE) | |
| 163 | /* | |
| 164 | * Don't allow open() unless the process has sufficient privileges, | |
| 165 | * since mapping the i/o page and granting i/o privilege would | |
| 166 | * require sufficient privilege soon and nothing much can be done | |
| 167 | * without them. | |
| 168 | */ | |
| 895c1f85 | 169 | error = priv_check_cred(ap->a_cred, PRIV_ROOT, 0); |
| 984263bc MD |
170 | if (error != 0) |
| 171 | return error; | |
| 172 | if (securelevel > 0) | |
| 173 | return EPERM; | |
| 174 | #endif | |
| 175 | ||
| 176 | ss->flags |= OPEN; | |
| 177 | ss->p = 0; | |
| 178 | ss->signal_num = 0; | |
| 179 | ||
| 180 | return 0; | |
| 181 | } | |
| 182 | ||
| 183 | static int | |
| fef8985e | 184 | spigot_close(struct dev_close_args *ap) |
| 984263bc | 185 | { |
| b13267a5 | 186 | cdev_t dev = ap->a_head.a_dev; |
| fef8985e | 187 | struct spigot_softc *ss; |
| 984263bc | 188 | |
| fef8985e | 189 | ss = (struct spigot_softc *)&spigot_softc[UNIT(dev)]; |
| 984263bc MD |
190 | ss->flags &= ~OPEN; |
| 191 | ss->p = 0; | |
| 192 | ss->signal_num = 0; | |
| 193 | ||
| 194 | outb(0xad6, 0); | |
| 195 | ||
| 196 | return 0; | |
| 197 | } | |
| 198 | ||
| 199 | static int | |
| fef8985e | 200 | spigot_write(struct dev_write_args *ap) |
| 984263bc MD |
201 | { |
| 202 | return ENXIO; | |
| 203 | } | |
| 204 | ||
| 205 | static int | |
| fef8985e | 206 | spigot_read(struct dev_read_args *ap) |
| 984263bc MD |
207 | { |
| 208 | return ENXIO; | |
| 209 | } | |
| 210 | ||
| 211 | ||
| 212 | static int | |
| fef8985e | 213 | spigot_ioctl(struct dev_ioctl_args *ap) |
| 984263bc | 214 | { |
| b13267a5 | 215 | cdev_t dev = ap->a_head.a_dev; |
| fef8985e MD |
216 | caddr_t data = ap->a_data; |
| 217 | int error; | |
| 218 | struct spigot_softc *ss; | |
| 219 | struct spigot_info *info; | |
| 220 | ||
| 221 | ss = (struct spigot_softc *)&spigot_softc[UNIT(dev)]; | |
| 222 | if (data == NULL) | |
| 223 | return(EINVAL); | |
| 984263bc | 224 | |
| fef8985e | 225 | switch(ap->a_cmd){ |
| 984263bc | 226 | case SPIGOT_SETINT: |
| d82eda3e DR |
227 | if (*(int *)data < 0 || *(int *)data > _SIG_MAXSIG) |
| 228 | return (EINVAL); | |
| fef8985e | 229 | ss->p = curproc; |
| 984263bc MD |
230 | ss->signal_num = *((int *)data); |
| 231 | break; | |
| 232 | case SPIGOT_IOPL_ON: /* allow access to the IO PAGE */ | |
| 233 | #if !defined(SPIGOT_UNSECURE) | |
| 895c1f85 | 234 | error = priv_check_cred(ap->a_cred, PRIV_ROOT, 0); |
| 984263bc MD |
235 | if (error != 0) |
| 236 | return error; | |
| 237 | if (securelevel > 0) | |
| 238 | return EPERM; | |
| 239 | #endif | |
| 08f2f1bb | 240 | curthread->td_lwp->lwp_md.md_regs->tf_eflags |= PSL_IOPL; |
| 984263bc MD |
241 | break; |
| 242 | case SPIGOT_IOPL_OFF: /* deny access to the IO PAGE */ | |
| 08f2f1bb | 243 | curthread->td_lwp->lwp_md.md_regs->tf_eflags &= ~PSL_IOPL; |
| 984263bc MD |
244 | break; |
| 245 | case SPIGOT_GET_INFO: | |
| 246 | info = (struct spigot_info *)data; | |
| 247 | info->maddr = ss->maddr; | |
| 248 | info->irq = ss->irq; | |
| 249 | break; | |
| 250 | default: | |
| 251 | return ENOTTY; | |
| 252 | } | |
| 253 | ||
| 254 | return 0; | |
| 255 | } | |
| 256 | ||
| 257 | /* | |
| 258 | * Interrupt procedure. | |
| 259 | * Just call a user level interrupt routine. | |
| 260 | */ | |
| 261 | static void | |
| 477d3c1c | 262 | spigintr(void *arg) |
| 984263bc | 263 | { |
| 477d3c1c | 264 | int unit = (int)arg; |
| fef8985e | 265 | struct spigot_softc *ss; |
| 984263bc | 266 | |
| fef8985e | 267 | ss = (struct spigot_softc *)&spigot_softc[unit]; |
| 984263bc | 268 | if(ss->p && ss->signal_num) |
| 84204577 | 269 | ksignal(ss->p, ss->signal_num); |
| 984263bc MD |
270 | } |
| 271 | ||
| 272 | static int | |
| fef8985e | 273 | spigot_mmap(struct dev_mmap_args *ap) |
| 984263bc | 274 | { |
| fef8985e | 275 | struct spigot_softc *ss; |
| 984263bc | 276 | |
| fef8985e MD |
277 | ss = (struct spigot_softc *)&spigot_softc[0]; |
| 278 | if (ap->a_offset != 0) | |
| 279 | return EINVAL; | |
| 280 | if (ap->a_nprot & PROT_EXEC) | |
| 281 | return EINVAL; | |
| 282 | ap->a_result = i386_btop(ss->maddr); | |
| 283 | return(0); | |
| 984263bc | 284 | } |