File:  [DragonFly] / src / sys / dev / misc / mse / mse.c
Revision 1.9: download - view: text, annotated - select for diffs
Wed May 19 22:52:43 2004 UTC (10 years, 3 months ago) by dillon
Branches: MAIN
CVS tags: HEAD, DragonFly_Snap13Sep2004, 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:  * Copyright 1992 by the University of Guelph
    3:  *
    4:  * Permission to use, copy and modify this
    5:  * software and its documentation for any purpose and without
    6:  * fee is hereby granted, provided that the above copyright
    7:  * notice appear in all copies and that both that copyright
    8:  * notice and this permission notice appear in supporting
    9:  * documentation.
   10:  * University of Guelph makes no representations about the suitability of
   11:  * this software for any purpose.  It is provided "as is"
   12:  * without express or implied warranty.
   13:  *
   14:  * $FreeBSD: src/sys/i386/isa/mse.c,v 1.49.2.1 2000/03/20 13:58:47 yokota Exp $
   15:  * $DragonFly: src/sys/dev/misc/mse/mse.c,v 1.9 2004/05/19 22:52:43 dillon Exp $
   16:  */
   17: /*
   18:  * Driver for the Logitech and ATI Inport Bus mice for use with 386bsd and
   19:  * the X386 port, courtesy of
   20:  * Rick Macklem, rick@snowhite.cis.uoguelph.ca
   21:  * Caveats: The driver currently uses spltty(), but doesn't use any
   22:  * generic tty code. It could use splmse() (that only masks off the
   23:  * bus mouse interrupt, but that would require hacking in i386/isa/icu.s.
   24:  * (This may be worth the effort, since the Logitech generates 30/60
   25:  * interrupts/sec continuously while it is open.)
   26:  * NB: The ATI has NOT been tested yet!
   27:  */
   28: 
   29: /*
   30:  * Modification history:
   31:  * Sep 6, 1994 -- Lars Fredriksen(fredriks@mcs.com)
   32:  *   improved probe based on input from Logitech.
   33:  *
   34:  * Oct 19, 1992 -- E. Stark (stark@cs.sunysb.edu)
   35:  *   fixes to make it work with Microsoft InPort busmouse
   36:  *
   37:  * Jan, 1993 -- E. Stark (stark@cs.sunysb.edu)
   38:  *   added patches for new "select" interface
   39:  *
   40:  * May 4, 1993 -- E. Stark (stark@cs.sunysb.edu)
   41:  *   changed position of some spl()'s in mseread
   42:  *
   43:  * October 8, 1993 -- E. Stark (stark@cs.sunysb.edu)
   44:  *   limit maximum negative x/y value to -127 to work around XFree problem
   45:  *   that causes spurious button pushes.
   46:  */
   47: 
   48: #include <sys/param.h>
   49: #include <sys/systm.h>
   50: #include <sys/conf.h>
   51: #include <sys/kernel.h>
   52: #include <sys/bus.h>
   53: #include <sys/poll.h>
   54: #include <sys/select.h>
   55: #include <sys/uio.h>
   56: 
   57: #include <machine/bus_pio.h>
   58: #include <machine/bus.h>
   59: #include <machine/clock.h>
   60: #include <machine/mouse.h>
   61: #include <machine/resource.h>
   62: #include <sys/rman.h>
   63: 
   64: #include <bus/isa/isavar.h>
   65: 
   66: /* driver configuration flags (config) */
   67: #define MSE_CONFIG_ACCEL	0x00f0  /* acceleration factor */
   68: #define MSE_CONFIG_FLAGS	(MSE_CONFIG_ACCEL)
   69: 
   70: /*
   71:  * Software control structure for mouse. The sc_enablemouse(),
   72:  * sc_disablemouse() and sc_getmouse() routines must be called spl'd().
   73:  */
   74: typedef struct mse_softc {
   75: 	int		sc_flags;
   76: 	int		sc_mousetype;
   77: 	struct selinfo	sc_selp;
   78: 	struct resource	*sc_port;
   79: 	struct resource	*sc_intr;
   80: 	bus_space_tag_t	sc_iot;
   81: 	bus_space_handle_t sc_ioh;
   82: 	void		*sc_ih;
   83: 	void		(*sc_enablemouse) (bus_space_tag_t t,
   84: 					       bus_space_handle_t h);
   85: 	void		(*sc_disablemouse) (bus_space_tag_t t,
   86: 						bus_space_handle_t h);
   87: 	void		(*sc_getmouse) (bus_space_tag_t t,
   88: 					    bus_space_handle_t h,
   89: 					    int *dx, int *dy, int *but);
   90: 	int		sc_deltax;
   91: 	int		sc_deltay;
   92: 	int		sc_obuttons;
   93: 	int		sc_buttons;
   94: 	int		sc_bytesread;
   95: 	u_char		sc_bytes[MOUSE_SYS_PACKETSIZE];
   96: 	struct		callout_handle sc_callout;
   97: 	int		sc_watchdog;
   98: 	mousehw_t	hw;
   99: 	mousemode_t	mode;
  100: 	mousestatus_t	status;
  101: } mse_softc_t;
  102: 
  103: static	devclass_t	mse_devclass;
  104: 
  105: static	int		mse_probe (device_t dev);
  106: static	int		mse_attach (device_t dev);
  107: static	int		mse_detach (device_t dev);
  108: 
  109: static	device_method_t	mse_methods[] = {
  110: 	DEVMETHOD(device_probe,		mse_probe),
  111: 	DEVMETHOD(device_attach,	mse_attach),
  112: 	DEVMETHOD(device_detach,	mse_detach),
  113: 	{ 0, 0 }
  114: };
  115: 
  116: static	driver_t	mse_driver = {
  117: 	"mse",
  118: 	mse_methods,
  119: 	sizeof(mse_softc_t),
  120: };
  121: 
  122: DRIVER_MODULE(mse, isa, mse_driver, mse_devclass, 0, 0);
  123: 
  124: static struct isa_pnp_id mse_ids[] = {
  125: 	{ 0x000fd041, "Bus mouse" },			/* PNP0F00 */
  126: 	{ 0x020fd041, "InPort mouse" },			/* PNP0F02 */
  127: 	{ 0x0d0fd041, "InPort mouse compatible" },	/* PNP0F0D */
  128: 	{ 0x110fd041, "Bus mouse compatible" },		/* PNP0F11 */
  129: 	{ 0x150fd041, "Logitech bus mouse" },		/* PNP0F15 */
  130: 	{ 0x180fd041, "Logitech bus mouse compatible" },/* PNP0F18 */
  131: 	{ 0 }
  132: };
  133: 
  134: static	d_open_t	mseopen;
  135: static	d_close_t	mseclose;
  136: static	d_read_t	mseread;
  137: static  d_ioctl_t	mseioctl;
  138: static	d_poll_t	msepoll;
  139: 
  140: #define CDEV_MAJOR 27
  141: static struct cdevsw mse_cdevsw = {
  142: 	/* name */	"mse",
  143: 	/* maj */	CDEV_MAJOR,
  144: 	/* flags */	0,
  145: 	/* port */	NULL,
  146: 	/* clone */	NULL,
  147: 
  148: 	/* open */	mseopen,
  149: 	/* close */	mseclose,
  150: 	/* read */	mseread,
  151: 	/* write */	nowrite,
  152: 	/* ioctl */	mseioctl,
  153: 	/* poll */	msepoll,
  154: 	/* mmap */	nommap,
  155: 	/* strategy */	nostrategy,
  156: 	/* dump */	nodump,
  157: 	/* psize */	nopsize
  158: };
  159: 
  160: static	void		mseintr (void *);
  161: static	timeout_t	msetimeout;
  162: 
  163: /* Flags */
  164: #define	MSESC_OPEN	0x1
  165: #define	MSESC_WANT	0x2
  166: 
  167: /* and Mouse Types */
  168: #define	MSE_NONE	0	/* don't move this! */
  169: #define	MSE_LOGITECH	0x1
  170: #define	MSE_ATIINPORT	0x2
  171: #define	MSE_LOGI_SIG	0xA5
  172: 
  173: #define	MSE_PORTA	0
  174: #define	MSE_PORTB	1
  175: #define	MSE_PORTC	2
  176: #define	MSE_PORTD	3
  177: #define MSE_IOSIZE	4
  178: 
  179: #define	MSE_UNIT(dev)		(minor(dev) >> 1)
  180: #define	MSE_NBLOCKIO(dev)	(minor(dev) & 0x1)
  181: 
  182: /*
  183:  * Logitech bus mouse definitions
  184:  */
  185: #define	MSE_SETUP	0x91	/* What does this mean? */
  186: 				/* The definition for the control port */
  187: 				/* is as follows: */
  188: 
  189: 				/* D7 	 =  Mode set flag (1 = active) 	*/
  190: 				/* D6,D5 =  Mode selection (port A) 	*/
  191: 				/* 	    00 = Mode 0 = Basic I/O 	*/
  192: 				/* 	    01 = Mode 1 = Strobed I/O 	*/
  193: 				/* 	    10 = Mode 2 = Bi-dir bus 	*/
  194: 				/* D4	 =  Port A direction (1 = input)*/
  195: 				/* D3	 =  Port C (upper 4 bits) 	*/
  196: 				/*	    direction. (1 = input)	*/
  197: 				/* D2	 =  Mode selection (port B & C) */
  198: 				/*	    0 = Mode 0 = Basic I/O	*/
  199: 				/*	    1 = Mode 1 = Strobed I/O	*/
  200: 				/* D1	 =  Port B direction (1 = input)*/
  201: 				/* D0	 =  Port C (lower 4 bits)	*/
  202: 				/*	    direction. (1 = input)	*/
  203: 
  204: 				/* So 91 means Basic I/O on all 3 ports,*/
  205: 				/* Port A is an input port, B is an 	*/
  206: 				/* output port, C is split with upper	*/
  207: 				/* 4 bits being an output port and lower*/
  208: 				/* 4 bits an input port, and enable the */
  209: 				/* sucker.				*/
  210: 				/* Courtesy Intel 8255 databook. Lars   */
  211: #define	MSE_HOLD	0x80
  212: #define	MSE_RXLOW	0x00
  213: #define	MSE_RXHIGH	0x20
  214: #define	MSE_RYLOW	0x40
  215: #define	MSE_RYHIGH	0x60
  216: #define	MSE_DISINTR	0x10
  217: #define MSE_INTREN	0x00
  218: 
  219: static	int		mse_probelogi (device_t dev, mse_softc_t *sc);
  220: static	void		mse_disablelogi (bus_space_tag_t t,
  221: 					     bus_space_handle_t h);
  222: static	void		mse_getlogi (bus_space_tag_t t,
  223: 					 bus_space_handle_t h,
  224: 					 int *dx, int *dy, int *but);
  225: static	void		mse_enablelogi (bus_space_tag_t t,
  226: 					    bus_space_handle_t h);
  227: 
  228: /*
  229:  * ATI Inport mouse definitions
  230:  */
  231: #define	MSE_INPORT_RESET	0x80
  232: #define	MSE_INPORT_STATUS	0x00
  233: #define	MSE_INPORT_DX		0x01
  234: #define	MSE_INPORT_DY		0x02
  235: #define	MSE_INPORT_MODE		0x07
  236: #define	MSE_INPORT_HOLD		0x20
  237: #define	MSE_INPORT_INTREN	0x09
  238: 
  239: static	int		mse_probeati (device_t dev, mse_softc_t *sc);
  240: static	void		mse_enableati (bus_space_tag_t t,
  241: 					   bus_space_handle_t h);
  242: static	void		mse_disableati (bus_space_tag_t t,
  243: 					    bus_space_handle_t h);
  244: static	void		mse_getati (bus_space_tag_t t,
  245: 					bus_space_handle_t h,
  246: 					int *dx, int *dy, int *but);
  247: 
  248: /*
  249:  * Table of mouse types.
  250:  * Keep the Logitech last, since I haven't figured out how to probe it
  251:  * properly yet. (Someday I'll have the documentation.)
  252:  */
  253: static struct mse_types {
  254: 	int	m_type;		/* Type of bus mouse */
  255: 	int	(*m_probe) (device_t dev, mse_softc_t *sc);
  256: 				/* Probe routine to test for it */
  257: 	void	(*m_enable) (bus_space_tag_t t, bus_space_handle_t h);
  258: 				/* Start routine */
  259: 	void	(*m_disable) (bus_space_tag_t t, bus_space_handle_t h);
  260: 				/* Disable interrupts routine */
  261: 	void	(*m_get) (bus_space_tag_t t, bus_space_handle_t h,
  262: 			      int *dx, int *dy, int *but);
  263: 				/* and get mouse status */
  264: 	mousehw_t   m_hw;	/* buttons iftype type model hwid */
  265: 	mousemode_t m_mode;	/* proto rate res accel level size mask */
  266: } mse_types[] = {
  267: 	{ MSE_ATIINPORT, 
  268: 	  mse_probeati, mse_enableati, mse_disableati, mse_getati,
  269: 	  { 2, MOUSE_IF_INPORT, MOUSE_MOUSE, MOUSE_MODEL_GENERIC, 0, },
  270: 	  { MOUSE_PROTO_INPORT, -1, -1, 0, 0, MOUSE_MSC_PACKETSIZE, 
  271: 	    { MOUSE_MSC_SYNCMASK, MOUSE_MSC_SYNC, }, }, },
  272: 	{ MSE_LOGITECH, 
  273: 	  mse_probelogi, mse_enablelogi, mse_disablelogi, mse_getlogi,
  274: 	  { 2, MOUSE_IF_BUS, MOUSE_MOUSE, MOUSE_MODEL_GENERIC, 0, },
  275: 	  { MOUSE_PROTO_BUS, -1, -1, 0, 0, MOUSE_MSC_PACKETSIZE, 
  276: 	    { MOUSE_MSC_SYNCMASK, MOUSE_MSC_SYNC, }, }, },
  277: 	{ 0, },
  278: };
  279: 
  280: static	int
  281: mse_probe(dev)
  282: 	device_t dev;
  283: {
  284: 	mse_softc_t *sc;
  285: 	int error;
  286: 	int rid;
  287: 	int i;
  288: 
  289: 	/* check PnP IDs */
  290: 	error = ISA_PNP_PROBE(device_get_parent(dev), dev, mse_ids);
  291: 	if (error == ENXIO)
  292: 		return ENXIO;
  293: 
  294: 	sc = device_get_softc(dev);
  295: 	rid = 0;
  296: 	sc->sc_port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
  297: 					 MSE_IOSIZE, RF_ACTIVE);
  298: 	if (sc->sc_port == NULL)
  299: 		return ENXIO;
  300: 	sc->sc_iot = rman_get_bustag(sc->sc_port);
  301: 	sc->sc_ioh = rman_get_bushandle(sc->sc_port);
  302: 
  303: 	/*
  304: 	 * Check for each mouse type in the table.
  305: 	 */
  306: 	i = 0;
  307: 	while (mse_types[i].m_type) {
  308: 		if ((*mse_types[i].m_probe)(dev, sc)) {
  309: 			sc->sc_mousetype = mse_types[i].m_type;
  310: 			sc->sc_enablemouse = mse_types[i].m_enable;
  311: 			sc->sc_disablemouse = mse_types[i].m_disable;
  312: 			sc->sc_getmouse = mse_types[i].m_get;
  313: 			sc->hw = mse_types[i].m_hw;
  314: 			sc->mode = mse_types[i].m_mode;
  315: 			bus_release_resource(dev, SYS_RES_IOPORT, rid,
  316: 					     sc->sc_port);
  317: 			device_set_desc(dev, "Bus/InPort Mouse");
  318: 			return 0;
  319: 		}
  320: 		i++;
  321: 	}
  322: 	bus_release_resource(dev, SYS_RES_IOPORT, rid, sc->sc_port);
  323: 	return ENXIO;
  324: }
  325: 
  326: static	int
  327: mse_attach(dev)
  328: 	device_t dev;
  329: {
  330: 	mse_softc_t *sc;
  331: 	int flags;
  332: 	int unit;
  333: 	int rid;
  334: 
  335: 	sc = device_get_softc(dev);
  336: 	unit = device_get_unit(dev);
  337: 
  338: 	rid = 0;
  339: 	sc->sc_port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
  340: 					 MSE_IOSIZE, RF_ACTIVE);
  341: 	if (sc->sc_port == NULL)
  342: 		return ENXIO;
  343: 	sc->sc_intr = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
  344: 					 RF_ACTIVE);
  345: 	if (sc->sc_intr == NULL) {
  346: 		bus_release_resource(dev, SYS_RES_IOPORT, rid, sc->sc_port);
  347: 		return ENXIO;
  348: 	}
  349: 	sc->sc_iot = rman_get_bustag(sc->sc_port);
  350: 	sc->sc_ioh = rman_get_bushandle(sc->sc_port);
  351: 
  352: 	if (BUS_SETUP_INTR(device_get_parent(dev), dev, sc->sc_intr,
  353: 			   INTR_TYPE_TTY, mseintr, sc, &sc->sc_ih)) {
  354: 		bus_release_resource(dev, SYS_RES_IOPORT, rid, sc->sc_port);
  355: 		bus_release_resource(dev, SYS_RES_IRQ, rid, sc->sc_intr);
  356: 		return ENXIO;
  357: 	}
  358: 
  359: 	flags = device_get_flags(dev);
  360: 	sc->mode.accelfactor = (flags & MSE_CONFIG_ACCEL) >> 4;
  361: 	callout_handle_init(&sc->sc_callout);
  362: 
  363: 	cdevsw_add(&mse_cdevsw, ~1, unit << 1);
  364: 	make_dev(&mse_cdevsw, unit << 1, 0, 0, 0600, "mse%d", unit);
  365: 	make_dev(&mse_cdevsw, (unit<<1)+1, 0, 0, 0600, "nmse%d", unit);
  366: 
  367: 	return 0;
  368: }
  369: 
  370: static	int
  371: mse_detach(dev)
  372: 	device_t dev;
  373: {
  374: 	mse_softc_t *sc;
  375: 	int rid;
  376: 
  377: 	sc = device_get_softc(dev);
  378: 	if (sc->sc_flags & MSESC_OPEN)
  379: 		return EBUSY;
  380: 
  381: 	rid = 0;
  382: 	BUS_TEARDOWN_INTR(device_get_parent(dev), dev, sc->sc_intr, sc->sc_ih);
  383: 	bus_release_resource(dev, SYS_RES_IRQ, rid, sc->sc_intr);
  384: 	bus_release_resource(dev, SYS_RES_IOPORT, rid, sc->sc_port);
  385: 	cdevsw_remove(&mse_cdevsw, ~1, device_get_unit(dev) << 1);
  386: 
  387: 	return 0;
  388: }
  389: 
  390: /*
  391:  * Exclusive open the mouse, initialize it and enable interrupts.
  392:  */
  393: static	int
  394: mseopen(dev, flags, fmt, td)
  395: 	dev_t dev;
  396: 	int flags;
  397: 	int fmt;
  398: 	struct thread *td;
  399: {
  400: 	mse_softc_t *sc;
  401: 	int s;
  402: 
  403: 	sc = devclass_get_softc(mse_devclass, MSE_UNIT(dev));
  404: 	if (sc == NULL)
  405: 		return (ENXIO);
  406: 	if (sc->sc_mousetype == MSE_NONE)
  407: 		return (ENXIO);
  408: 	if (sc->sc_flags & MSESC_OPEN)
  409: 		return (EBUSY);
  410: 	sc->sc_flags |= MSESC_OPEN;
  411: 	sc->sc_obuttons = sc->sc_buttons = MOUSE_MSC_BUTTONS;
  412: 	sc->sc_deltax = sc->sc_deltay = 0;
  413: 	sc->sc_bytesread = sc->mode.packetsize = MOUSE_MSC_PACKETSIZE;
  414: 	sc->sc_watchdog = FALSE;
  415: 	sc->sc_callout = timeout(msetimeout, dev, hz*2);
  416: 	sc->mode.level = 0;
  417: 	sc->status.flags = 0;
  418: 	sc->status.button = sc->status.obutton = 0;
  419: 	sc->status.dx = sc->status.dy = sc->status.dz = 0;
  420: 
  421: 	/*
  422: 	 * Initialize mouse interface and enable interrupts.
  423: 	 */
  424: 	s = spltty();
  425: 	(*sc->sc_enablemouse)(sc->sc_iot, sc->sc_ioh);
  426: 	splx(s);
  427: 	return (0);
  428: }
  429: 
  430: /*
  431:  * mseclose: just turn off mouse innterrupts.
  432:  */
  433: static	int
  434: mseclose(dev, flags, fmt, td)
  435: 	dev_t dev;
  436: 	int flags;
  437: 	int fmt;
  438: 	struct thread *td;
  439: {
  440: 	mse_softc_t *sc = devclass_get_softc(mse_devclass, MSE_UNIT(dev));
  441: 	int s;
  442: 
  443: 	untimeout(msetimeout, dev, sc->sc_callout);
  444: 	callout_handle_init(&sc->sc_callout);
  445: 	s = spltty();
  446: 	(*sc->sc_disablemouse)(sc->sc_iot, sc->sc_ioh);
  447: 	sc->sc_flags &= ~MSESC_OPEN;
  448: 	splx(s);
  449: 	return(0);
  450: }
  451: 
  452: /*
  453:  * mseread: return mouse info using the MSC serial protocol, but without
  454:  * using bytes 4 and 5.
  455:  * (Yes this is cheesy, but it makes the X386 server happy, so...)
  456:  */
  457: static	int
  458: mseread(dev, uio, ioflag)
  459: 	dev_t dev;
  460: 	struct uio *uio;
  461: 	int ioflag;
  462: {
  463: 	mse_softc_t *sc = devclass_get_softc(mse_devclass, MSE_UNIT(dev));
  464: 	int xfer, s, error;
  465: 
  466: 	/*
  467: 	 * If there are no protocol bytes to be read, set up a new protocol
  468: 	 * packet.
  469: 	 */
  470: 	s = spltty(); /* XXX Should be its own spl, but where is imlXX() */
  471: 	if (sc->sc_bytesread >= sc->mode.packetsize) {
  472: 		while (sc->sc_deltax == 0 && sc->sc_deltay == 0 &&
  473: 		       (sc->sc_obuttons ^ sc->sc_buttons) == 0) {
  474: 			if (MSE_NBLOCKIO(dev)) {
  475: 				splx(s);
  476: 				return (0);
  477: 			}
  478: 			sc->sc_flags |= MSESC_WANT;
  479: 			error = tsleep((caddr_t)sc, PCATCH, "mseread", 0);
  480: 			if (error) {
  481: 				splx(s);
  482: 				return (error);
  483: 			}
  484: 		}
  485: 
  486: 		/*
  487: 		 * Generate protocol bytes.
  488: 		 * For some reason X386 expects 5 bytes but never uses
  489: 		 * the fourth or fifth?
  490: 		 */
  491: 		sc->sc_bytes[0] = sc->mode.syncmask[1] 
  492: 		    | (sc->sc_buttons & ~sc->mode.syncmask[0]);
  493: 		if (sc->sc_deltax > 127)
  494: 			sc->sc_deltax = 127;
  495: 		if (sc->sc_deltax < -127)
  496: 			sc->sc_deltax = -127;
  497: 		sc->sc_deltay = -sc->sc_deltay;	/* Otherwise mousey goes wrong way */
  498: 		if (sc->sc_deltay > 127)
  499: 			sc->sc_deltay = 127;
  500: 		if (sc->sc_deltay < -127)
  501: 			sc->sc_deltay = -127;
  502: 		sc->sc_bytes[1] = sc->sc_deltax;
  503: 		sc->sc_bytes[2] = sc->sc_deltay;
  504: 		sc->sc_bytes[3] = sc->sc_bytes[4] = 0;
  505: 		sc->sc_bytes[5] = sc->sc_bytes[6] = 0;
  506: 		sc->sc_bytes[7] = MOUSE_SYS_EXTBUTTONS;
  507: 		sc->sc_obuttons = sc->sc_buttons;
  508: 		sc->sc_deltax = sc->sc_deltay = 0;
  509: 		sc->sc_bytesread = 0;
  510: 	}
  511: 	splx(s);
  512: 	xfer = min(uio->uio_resid, sc->mode.packetsize - sc->sc_bytesread);
  513: 	error = uiomove(&sc->sc_bytes[sc->sc_bytesread], xfer, uio);
  514: 	if (error)
  515: 		return (error);
  516: 	sc->sc_bytesread += xfer;
  517: 	return(0);
  518: }
  519: 
  520: /*
  521:  * mseioctl: process ioctl commands.
  522:  */
  523: static int
  524: mseioctl(dev, cmd, addr, flag, td)
  525: 	dev_t dev;
  526: 	u_long cmd;
  527: 	caddr_t addr;
  528: 	int flag;
  529: 	struct thread *td;
  530: {
  531: 	mse_softc_t *sc = devclass_get_softc(mse_devclass, MSE_UNIT(dev));
  532: 	mousestatus_t status;
  533: 	int err = 0;
  534: 	int s;
  535: 
  536: 	switch (cmd) {
  537: 
  538: 	case MOUSE_GETHWINFO:
  539: 		s = spltty();
  540: 		*(mousehw_t *)addr = sc->hw;
  541: 		if (sc->mode.level == 0)
  542: 			((mousehw_t *)addr)->model = MOUSE_MODEL_GENERIC;
  543: 		splx(s);
  544: 		break;
  545: 
  546: 	case MOUSE_GETMODE:
  547: 		s = spltty();
  548: 		*(mousemode_t *)addr = sc->mode;
  549: 		switch (sc->mode.level) {
  550: 		case 0:
  551: 			break;
  552: 		case 1:
  553: 			((mousemode_t *)addr)->protocol = MOUSE_PROTO_SYSMOUSE;
  554: 	    		((mousemode_t *)addr)->syncmask[0] = MOUSE_SYS_SYNCMASK;
  555: 	    		((mousemode_t *)addr)->syncmask[1] = MOUSE_SYS_SYNC;
  556: 			break;
  557: 		}
  558: 		splx(s);
  559: 		break;
  560: 
  561: 	case MOUSE_SETMODE:
  562: 		switch (((mousemode_t *)addr)->level) {
  563: 		case 0:
  564: 		case 1:
  565: 			break;
  566: 		default:
  567: 			return (EINVAL);
  568: 		}
  569: 		if (((mousemode_t *)addr)->accelfactor < -1)
  570: 			return (EINVAL);
  571: 		else if (((mousemode_t *)addr)->accelfactor >= 0)
  572: 			sc->mode.accelfactor = 
  573: 			    ((mousemode_t *)addr)->accelfactor;
  574: 		sc->mode.level = ((mousemode_t *)addr)->level;
  575: 		switch (sc->mode.level) {
  576: 		case 0:
  577: 			sc->sc_bytesread = sc->mode.packetsize 
  578: 			    = MOUSE_MSC_PACKETSIZE;
  579: 			break;
  580: 		case 1:
  581: 			sc->sc_bytesread = sc->mode.packetsize 
  582: 			    = MOUSE_SYS_PACKETSIZE;
  583: 			break;
  584: 		}
  585: 		break;
  586: 
  587: 	case MOUSE_GETLEVEL:
  588: 		*(int *)addr = sc->mode.level;
  589: 		break;
  590: 
  591: 	case MOUSE_SETLEVEL:
  592: 		switch (*(int *)addr) {
  593: 		case 0:
  594: 			sc->mode.level = *(int *)addr;
  595: 			sc->sc_bytesread = sc->mode.packetsize 
  596: 			    = MOUSE_MSC_PACKETSIZE;
  597: 			break;
  598: 		case 1:
  599: 			sc->mode.level = *(int *)addr;
  600: 			sc->sc_bytesread = sc->mode.packetsize 
  601: 			    = MOUSE_SYS_PACKETSIZE;
  602: 			break;
  603: 		default:
  604: 			return (EINVAL);
  605: 		}
  606: 		break;
  607: 
  608: 	case MOUSE_GETSTATUS:
  609: 		s = spltty();
  610: 		status = sc->status;
  611: 		sc->status.flags = 0;
  612: 		sc->status.obutton = sc->status.button;
  613: 		sc->status.button = 0;
  614: 		sc->status.dx = 0;
  615: 		sc->status.dy = 0;
  616: 		sc->status.dz = 0;
  617: 		splx(s);
  618: 		*(mousestatus_t *)addr = status;
  619: 		break;
  620: 
  621: 	case MOUSE_READSTATE:
  622: 	case MOUSE_READDATA:
  623: 		return (ENODEV);
  624: 
  625: #if (defined(MOUSE_GETVARS))
  626: 	case MOUSE_GETVARS:
  627: 	case MOUSE_SETVARS:
  628: 		return (ENODEV);
  629: #endif
  630: 
  631: 	default:
  632: 		return (ENOTTY);
  633: 	}
  634: 	return (err);
  635: }
  636: 
  637: /*
  638:  * msepoll: check for mouse input to be processed.
  639:  */
  640: static	int
  641: msepoll(dev, events, td)
  642: 	dev_t dev;
  643: 	int events;
  644: 	struct thread *td;
  645: {
  646: 	mse_softc_t *sc = devclass_get_softc(mse_devclass, MSE_UNIT(dev));
  647: 	int s;
  648: 	int revents = 0;
  649: 
  650: 	s = spltty();
  651: 	if (events & (POLLIN | POLLRDNORM)) {
  652: 		if (sc->sc_bytesread != sc->mode.packetsize ||
  653: 		    sc->sc_deltax != 0 || sc->sc_deltay != 0 ||
  654: 		    (sc->sc_obuttons ^ sc->sc_buttons) != 0)
  655: 			revents |= events & (POLLIN | POLLRDNORM);
  656: 		else {
  657: 			/*
  658: 			 * Since this is an exclusive open device, any previous
  659: 			 * proc pointer is trash now, so we can just assign it.
  660: 			 */
  661: 			selrecord(td, &sc->sc_selp);
  662: 		}
  663: 	}
  664: 	splx(s);
  665: 	return (revents);
  666: }
  667: 
  668: /*
  669:  * msetimeout: watchdog timer routine.
  670:  */
  671: static void
  672: msetimeout(arg)
  673: 	void *arg;
  674: {
  675: 	dev_t dev;
  676: 	mse_softc_t *sc;
  677: 
  678: 	dev = (dev_t)arg;
  679: 	sc = devclass_get_softc(mse_devclass, MSE_UNIT(dev));
  680: 	if (sc->sc_watchdog) {
  681: 		if (bootverbose)
  682: 			printf("mse%d: lost interrupt?\n", MSE_UNIT(dev));
  683: 		mseintr(sc);
  684: 	}
  685: 	sc->sc_watchdog = TRUE;
  686: 	sc->sc_callout = timeout(msetimeout, dev, hz);
  687: }
  688: 
  689: /*
  690:  * mseintr: update mouse status. sc_deltax and sc_deltay are accumulative.
  691:  */
  692: static void
  693: mseintr(arg)
  694: 	void *arg;
  695: {
  696: 	/*
  697: 	 * the table to turn MouseSystem button bits (MOUSE_MSC_BUTTON?UP)
  698: 	 * into `mousestatus' button bits (MOUSE_BUTTON?DOWN).
  699: 	 */
  700: 	static int butmap[8] = {
  701: 		0, 
  702: 		MOUSE_BUTTON3DOWN, 
  703: 		MOUSE_BUTTON2DOWN, 
  704: 		MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN, 
  705: 		MOUSE_BUTTON1DOWN, 
  706: 		MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN, 
  707: 		MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN,
  708:         	MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN
  709: 	};
  710: 	mse_softc_t *sc = arg;
  711: 	int dx, dy, but;
  712: 	int sign;
  713: 
  714: #ifdef DEBUG
  715: 	static int mse_intrcnt = 0;
  716: 	if((mse_intrcnt++ % 10000) == 0)
  717: 		printf("mseintr\n");
  718: #endif /* DEBUG */
  719: 	if ((sc->sc_flags & MSESC_OPEN) == 0)
  720: 		return;
  721: 
  722: 	(*sc->sc_getmouse)(sc->sc_iot, sc->sc_ioh, &dx, &dy, &but);
  723: 	if (sc->mode.accelfactor > 0) {
  724: 		sign = (dx < 0);
  725: 		dx = dx * dx / sc->mode.accelfactor;
  726: 		if (dx == 0)
  727: 			dx = 1;
  728: 		if (sign)
  729: 			dx = -dx;
  730: 		sign = (dy < 0);
  731: 		dy = dy * dy / sc->mode.accelfactor;
  732: 		if (dy == 0)
  733: 			dy = 1;
  734: 		if (sign)
  735: 			dy = -dy;
  736: 	}
  737: 	sc->sc_deltax += dx;
  738: 	sc->sc_deltay += dy;
  739: 	sc->sc_buttons = but;
  740: 
  741: 	but = butmap[~but & MOUSE_MSC_BUTTONS];
  742: 	sc->status.dx += dx;
  743: 	sc->status.dy += dy;
  744: 	sc->status.flags |= ((dx || dy) ? MOUSE_POSCHANGED : 0)
  745: 	    | (sc->status.button ^ but);
  746: 	sc->status.button = but;
  747: 
  748: 	sc->sc_watchdog = FALSE;
  749: 
  750: 	/*
  751: 	 * If mouse state has changed, wake up anyone wanting to know.
  752: 	 */
  753: 	if (sc->sc_deltax != 0 || sc->sc_deltay != 0 ||
  754: 	    (sc->sc_obuttons ^ sc->sc_buttons) != 0) {
  755: 		if (sc->sc_flags & MSESC_WANT) {
  756: 			sc->sc_flags &= ~MSESC_WANT;
  757: 			wakeup((caddr_t)sc);
  758: 		}
  759: 		selwakeup(&sc->sc_selp);
  760: 	}
  761: }
  762: 
  763: /*
  764:  * Routines for the Logitech mouse.
  765:  */
  766: /*
  767:  * Test for a Logitech bus mouse and return 1 if it is.
  768:  * (until I know how to use the signature port properly, just disable
  769:  *  interrupts and return 1)
  770:  */
  771: static int
  772: mse_probelogi(dev, sc)
  773: 	device_t dev;
  774: 	mse_softc_t *sc;
  775: {
  776: 
  777: 	int sig;
  778: 
  779: 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, MSE_PORTD, MSE_SETUP);
  780: 		/* set the signature port */
  781: 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, MSE_PORTB, MSE_LOGI_SIG);
  782: 
  783: 	DELAY(30000); /* 30 ms delay */
  784: 	sig = bus_space_read_1(sc->sc_iot, sc->sc_ioh, MSE_PORTB) & 0xFF;
  785: 	if (sig == MSE_LOGI_SIG) {
  786: 		bus_space_write_1(sc->sc_iot, sc->sc_ioh, MSE_PORTC,
  787: 				  MSE_DISINTR);
  788: 		return(1);
  789: 	} else {
  790: 		if (bootverbose)
  791: 			device_printf(dev, "wrong signature %x\n", sig);
  792: 		return(0);
  793: 	}
  794: }
  795: 
  796: /*
  797:  * Initialize Logitech mouse and enable interrupts.
  798:  */
  799: static void
  800: mse_enablelogi(tag, handle)
  801: 	bus_space_tag_t tag;
  802: 	bus_space_handle_t handle;
  803: {
  804: 	int dx, dy, but;
  805: 
  806: 	bus_space_write_1(tag, handle, MSE_PORTD, MSE_SETUP);
  807: 	mse_getlogi(tag, handle, &dx, &dy, &but);
  808: }
  809: 
  810: /*
  811:  * Disable interrupts for Logitech mouse.
  812:  */
  813: static void
  814: mse_disablelogi(tag, handle)
  815: 	bus_space_tag_t tag;
  816: 	bus_space_handle_t handle;
  817: {
  818: 
  819: 	bus_space_write_1(tag, handle, MSE_PORTC, MSE_DISINTR);
  820: }
  821: 
  822: /*
  823:  * Get the current dx, dy and button up/down state.
  824:  */
  825: static void
  826: mse_getlogi(tag, handle, dx, dy, but)
  827: 	bus_space_tag_t tag;
  828: 	bus_space_handle_t handle;
  829: 	int *dx;
  830: 	int *dy;
  831: 	int *but;
  832: {
  833: 	register char x, y;
  834: 
  835: 	bus_space_write_1(tag, handle, MSE_PORTC, MSE_HOLD | MSE_RXLOW);
  836: 	x = bus_space_read_1(tag, handle, MSE_PORTA);
  837: 	*but = (x >> 5) & MOUSE_MSC_BUTTONS;
  838: 	x &= 0xf;
  839: 	bus_space_write_1(tag, handle, MSE_PORTC, MSE_HOLD | MSE_RXHIGH);
  840: 	x |= (bus_space_read_1(tag, handle, MSE_PORTA) << 4);
  841: 	bus_space_write_1(tag, handle, MSE_PORTC, MSE_HOLD | MSE_RYLOW);
  842: 	y = (bus_space_read_1(tag, handle, MSE_PORTA) & 0xf);
  843: 	bus_space_write_1(tag, handle, MSE_PORTC, MSE_HOLD | MSE_RYHIGH);
  844: 	y |= (bus_space_read_1(tag, handle, MSE_PORTA) << 4);
  845: 	*dx = x;
  846: 	*dy = y;
  847: 	bus_space_write_1(tag, handle, MSE_PORTC, MSE_INTREN);
  848: }
  849: 
  850: /*
  851:  * Routines for the ATI Inport bus mouse.
  852:  */
  853: /*
  854:  * Test for a ATI Inport bus mouse and return 1 if it is.
  855:  * (do not enable interrupts)
  856:  */
  857: static int
  858: mse_probeati(dev, sc)
  859: 	device_t dev;
  860: 	mse_softc_t *sc;
  861: {
  862: 	int i;
  863: 
  864: 	for (i = 0; i < 2; i++)
  865: 		if (bus_space_read_1(sc->sc_iot, sc->sc_ioh, MSE_PORTC) == 0xde)
  866: 			return (1);
  867: 	return (0);
  868: }
  869: 
  870: /*
  871:  * Initialize ATI Inport mouse and enable interrupts.
  872:  */
  873: static void
  874: mse_enableati(tag, handle)
  875: 	bus_space_tag_t tag;
  876: 	bus_space_handle_t handle;
  877: {
  878: 
  879: 	bus_space_write_1(tag, handle, MSE_PORTA, MSE_INPORT_RESET);
  880: 	bus_space_write_1(tag, handle, MSE_PORTA, MSE_INPORT_MODE);
  881: 	bus_space_write_1(tag, handle, MSE_PORTB, MSE_INPORT_INTREN);
  882: }
  883: 
  884: /*
  885:  * Disable interrupts for ATI Inport mouse.
  886:  */
  887: static void
  888: mse_disableati(tag, handle)
  889: 	bus_space_tag_t tag;
  890: 	bus_space_handle_t handle;
  891: {
  892: 
  893: 	bus_space_write_1(tag, handle, MSE_PORTA, MSE_INPORT_MODE);
  894: 	bus_space_write_1(tag, handle, MSE_PORTB, 0);
  895: }
  896: 
  897: /*
  898:  * Get current dx, dy and up/down button state.
  899:  */
  900: static void
  901: mse_getati(tag, handle, dx, dy, but)
  902: 	bus_space_tag_t tag;
  903: 	bus_space_handle_t handle;
  904: 	int *dx;
  905: 	int *dy;
  906: 	int *but;
  907: {
  908: 	register char byte;
  909: 
  910: 	bus_space_write_1(tag, handle, MSE_PORTA, MSE_INPORT_MODE);
  911: 	bus_space_write_1(tag, handle, MSE_PORTB, MSE_INPORT_HOLD);
  912: 	bus_space_write_1(tag, handle, MSE_PORTA, MSE_INPORT_STATUS);
  913: 	*but = ~bus_space_read_1(tag, handle, MSE_PORTB) & MOUSE_MSC_BUTTONS;
  914: 	bus_space_write_1(tag, handle, MSE_PORTA, MSE_INPORT_DX);
  915: 	byte = bus_space_read_1(tag, handle, MSE_PORTB);
  916: 	*dx = byte;
  917: 	bus_space_write_1(tag, handle, MSE_PORTA, MSE_INPORT_DY);
  918: 	byte = bus_space_read_1(tag, handle, MSE_PORTB);
  919: 	*dy = byte;
  920: 	bus_space_write_1(tag, handle, MSE_PORTA, MSE_INPORT_MODE);
  921: 	bus_space_write_1(tag, handle, MSE_PORTB, MSE_INPORT_INTREN);
  922: }