File:  [DragonFly] / src / sys / dev / misc / spigot / spigot.c
Revision 1.9: download - view: text, annotated - select for diffs
Wed May 19 22:52:44 2004 UTC (9 years, 11 months ago) by dillon
Branches: MAIN
CVS tags: HEAD, DragonFly_Stable, DragonFly_Snap29Sep2004, DragonFly_Snap13Sep2004, DragonFly_RELEASE_1_2_Slip, DragonFly_RELEASE_1_2, DragonFly_1_0_REL, DragonFly_1_0_RC1, DragonFly_1_0A_REL
Device layer rollup commit.

* cdevsw_add() is now required.  cdevsw_add() and cdevsw_remove() may specify
  a mask/match indicating the range of supported minor numbers.  Multiple
  cdevsw_add()'s using the same major number, but distinctly different
  ranges, may be issued.  All devices that failed to call cdevsw_add() before
  now do.

* cdevsw_remove() now automatically marks all devices within its supported
  range as being destroyed.

* vnode->v_rdev is no longer resolved when the vnode is created.  Instead,
  only v_udev (a newly added field) is resolved.  v_rdev is resolved when
  the vnode is opened and cleared on the last close.

* A great deal of code was making rather dubious assumptions with regards
  to the validity of devices associated with vnodes, primarily due to
  the persistence of a device structure due to being indexed by (major, minor)
  instead of by (cdevsw, major, minor).  In particular, if you run a program
  which connects to a USB device and then you pull the USB device and plug
  it back in, the vnode subsystem will continue to believe that the device
  is open when, in fact, it isn't (because it was destroyed and recreated).

  In particular, note that all the VFS mount procedures now check devices
  via v_udev instead of v_rdev prior to calling VOP_OPEN(), since v_rdev
  is NULL prior to the first open.

* The disk layer's device interaction has been rewritten.  The disk layer
  (i.e. the slice and disklabel management layer) no longer overloads
  its data onto the device structure representing the underlying physical
  disk.  Instead, the disk layer uses the new cdevsw_add() functionality
  to register its own cdevsw using the underlying device's major number,
  and simply does NOT register the underlying device's cdevsw.  No
  confusion is created because the device hash is now based on
  (cdevsw,major,minor) rather then (major,minor).

  NOTE: This also means that underlying raw disk devices may use the entire
  device minor number instead of having to reserve the bits used by the disk
  layer, and also means that can we (theoretically) stack a fully
  disklabel-supported 'disk' on top of any block device.

* The new reference counting scheme prevents this by associating a device
  with a cdevsw and disconnecting the device from its cdevsw when the cdevsw
  is removed.  Additionally, all udev2dev() lookups run through the cdevsw
  mask/match and only successfully find devices still associated with an
  active cdevsw.

* Major work on MFS:  MFS no longer shortcuts vnode and device creation.  It
  now creates a real vnode and a real device and implements real open and
  close VOPs.  Additionally, due to the disk layer changes, MFS is no longer
  limited to 255 mounts.  The new limit is 16 million.  Since MFS creates a
  real device node, mount_mfs will now create a real /dev/mfs<PID> device
  that can be read from userland (e.g. so you can dump an MFS filesystem).

* BUF AND DEVICE STRATEGY changes.  The struct buf contains a b_dev field.
  In order to properly handle stacked devices we now require that the b_dev
  field be initialized before the device strategy routine is called.  This
  required some additional work in various VFS implementations.  To enforce
  this requirement, biodone() now sets b_dev to NODEV.  The new disk layer
  will adjust b_dev before forwarding a request to the actual physical
  device.

* A bug in the ISO CD boot sequence which resulted in a panic has been fixed.

Testing by: lots of people, but David Rhodus found the most aggregious bugs.

    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:  * $DragonFly: src/sys/dev/misc/spigot/spigot.c,v 1.9 2004/05/19 22:52:44 dillon Exp $
   47:  *
   48:  */
   49: 
   50: #include	"use_spigot.h"
   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>
   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	<bus/isa/i386/isa_device.h>
   71: 
   72: static 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: 
   86: static int	spigot_probe(struct isa_device *id);
   87: static int	spigot_attach(struct isa_device *id);
   88: 
   89: struct isa_driver	spigotdriver = {spigot_probe, spigot_attach, "spigot"};
   90: 
   91: static	d_open_t	spigot_open;
   92: static	d_close_t	spigot_close;
   93: static	d_read_t	spigot_read;
   94: static	d_write_t	spigot_write;
   95: static	d_ioctl_t	spigot_ioctl;
   96: static	d_mmap_t	spigot_mmap;
   97: 
   98: #define CDEV_MAJOR 11
   99: static struct cdevsw spigot_cdevsw = {
  100: 	/* name */	"spigot",
  101: 	/* maj */	CDEV_MAJOR,
  102: 	/* flags */	0,
  103: 	/* port */	NULL,
  104: 	/* clone */	NULL,
  105: 
  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,
  114: 	/* dump */	nodump,
  115: 	/* psize */	nopsize
  116: };
  117: 
  118: static ointhand2_t	spigintr;
  119: 
  120: static int
  121: spigot_probe(struct isa_device *devp)
  122: {
  123: 	struct spigot_softc *ss;
  124: 	int status;
  125: 
  126: 	ss = (struct spigot_softc *)&spigot_softc[devp->id_unit];
  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: 	cdevsw_add(&spigot_cdevsw, -1, unit);
  151: 	make_dev(&spigot_cdevsw, unit, 0, 0, 0644, "spigot%d", unit);
  152: 	return 1;
  153: }
  154: 
  155: static	int
  156: spigot_open(dev_t dev, int flags, int fmt, struct thread *td)
  157: {
  158: int			error;
  159: struct	spigot_softc	*ss = (struct spigot_softc *)&spigot_softc[UNIT(dev)];
  160: 
  161: 	if((ss->flags & ALIVE) == 0)
  162: 		return ENXIO;
  163: 
  164: 	if(ss->flags & OPEN)
  165: 		return EBUSY;
  166: 
  167: #if !defined(SPIGOT_UNSECURE)
  168: 	/*
  169: 	 * Don't allow open() unless the process has sufficient privileges,
  170: 	 * since mapping the i/o page and granting i/o privilege would
  171: 	 * require sufficient privilege soon and nothing much can be done
  172: 	 * without them.
  173: 	 */
  174: 	error = suser(td);
  175: 	if (error != 0)
  176: 		return error;
  177: 	if (securelevel > 0)
  178: 		return EPERM;
  179: #endif
  180: 
  181: 	ss->flags |= OPEN;
  182: 	ss->p = 0;
  183: 	ss->signal_num = 0;
  184: 
  185: 	return 0;
  186: }
  187: 
  188: static	int
  189: spigot_close(dev_t dev, int flags, int fmt, struct thread *td)
  190: {
  191: struct	spigot_softc	*ss = (struct spigot_softc *)&spigot_softc[UNIT(dev)];
  192: 
  193: 	ss->flags &= ~OPEN;
  194: 	ss->p = 0;
  195: 	ss->signal_num = 0;
  196: 
  197: 	outb(0xad6, 0);
  198: 
  199: 	return 0;
  200: }
  201: 
  202: static	int
  203: spigot_write(dev_t dev, struct uio *uio, int ioflag)
  204: {
  205: 	return ENXIO;
  206: }
  207: 
  208: static	int
  209: spigot_read(dev_t dev, struct uio *uio, int ioflag)
  210: {
  211: 	return ENXIO;
  212: }
  213: 
  214: 
  215: static	int
  216: spigot_ioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct thread *td)
  217: {
  218: int			error;
  219: struct	spigot_softc	*ss = (struct spigot_softc *)&spigot_softc[UNIT(dev)];
  220: struct	spigot_info	*info;
  221: 
  222: 	if(!data) return(EINVAL);
  223: 	switch(cmd){
  224: 	case	SPIGOT_SETINT:
  225: 		if (*(int *)data < 0 || *(int *)data > _SIG_MAXSIG)
  226: 			return (EINVAL);
  227: 		ss->p = td->td_proc;
  228: 		ss->signal_num = *((int *)data);
  229: 		break;
  230: 	case	SPIGOT_IOPL_ON:	/* allow access to the IO PAGE */
  231: #if !defined(SPIGOT_UNSECURE)
  232: 		error = suser(td);
  233: 		if (error != 0)
  234: 			return error;
  235: 		if (securelevel > 0)
  236: 			return EPERM;
  237: #endif
  238: 		td->td_proc->p_md.md_regs->tf_eflags |= PSL_IOPL;
  239: 		break;
  240: 	case	SPIGOT_IOPL_OFF: /* deny access to the IO PAGE */
  241: 		td->td_proc->p_md.md_regs->tf_eflags &= ~PSL_IOPL;
  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:  */
  259: static void
  260: spigintr(int unit)
  261: {
  262: struct	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: 
  268: static	int
  269: spigot_mmap(dev_t dev, vm_offset_t offset, int nprot)
  270: {
  271: struct	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: }