File:  [DragonFly] / src / sys / dev / misc / spic / spic.c
Revision 1.7: download - view: text, annotated - select for diffs
Thu May 13 23:49:17 2004 UTC (10 years, 6 months ago) by dillon
Branches: MAIN
CVS tags: HEAD
device switch 1/many: Remove d_autoq, add d_clone (where d_autoq was).

d_autoq was used to allow the device port dispatch to mix old-style synchronous
calls with new style messaging calls within a particular device.  It was never
used for that purpose.

d_clone will be more fully implemented as work continues.  We are going to
install d_port in the dev_t (struct specinfo) structure itself and d_clone
will be needed to allow devices to 'revector' the port on a minor-number
by minor-number basis, in particular allowing minor numbers to be directly
dispatched to distinct threads.  This is something we will be needing later
on.

    1: /*
    2:  * Copyright (c) 2000  Nick Sayer
    3:  * All rights reserved.
    4:  *
    5:  * Redistribution and use in source and binary forms, with or without
    6:  * modification, are permitted provided that the following conditions
    7:  * are met:
    8:  * 1. Redistributions of source code must retain the above copyright
    9:  *    notice, this list of conditions and the following disclaimer.
   10:  * 2. Redistributions in binary form must reproduce the above copyright
   11:  *    notice, this list of conditions and the following disclaimer in the
   12:  *    documentation and/or other materials provided with the distribution.
   13:  *
   14:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   15:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   16:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   17:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   18:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   19:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   20:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   21:  * HOWEVER 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:  * spic -- the Sony Programmable I/O Controller
   27:  *
   28:  * This device exists on most recent Sony laptops. It is the means by which
   29:  * you can watch the Jog Dial and some other functions.
   30:  *
   31:  * At the moment, this driver merely tries to turn the jog dial into a
   32:  * device that moused can park on, with the intent of supplying a Z axis
   33:  * and mouse button out of the jog dial. I suspect that this device will
   34:  * end up having to support at least 2 different minor devices: One to be
   35:  * the jog wheel device for moused to camp out on and the other to perform
   36:  * all of the other miscelaneous functions of this device. But for now,
   37:  * the jog wheel is all you get.
   38:  *
   39:  * At the moment, the data sent back by the device is rather primitive.
   40:  * It sends a single character per event:
   41:  * u = up, d = down -- that's the jog button
   42:  * l = left, r = right -- that's the dial.
   43:  * "left" and "right" are rather caprecious. They actually represent
   44:  * ccw and cw, respectively
   45:  *
   46:  * What documentation exists is thanks to Andrew Tridge, and his page at
   47:  * http://samba.org/picturebook/ Special thanks also to Ian Dowse, who
   48:  * also provided sample code upon which this driver was based.
   49:  *
   50:  * $FreeBSD: src/sys/i386/isa/spic.c,v 1.4.2.1 2002/04/15 00:52:12 will Exp $
   51:  * $DragonFly: src/sys/dev/misc/spic/spic.c,v 1.7 2004/05/13 23:49:17 dillon Exp $
   52:  */
   53: 
   54: #include <sys/param.h>
   55: #include <sys/systm.h>
   56: #include <sys/kernel.h>
   57: #include <sys/bus.h>
   58: #include <machine/bus.h>
   59: #include <sys/rman.h>
   60: #include <machine/resource.h>
   61: #include <bus/isa/isavar.h>
   62: #include <sys/poll.h>
   63: #include <machine/pci_cfgreg.h>
   64: #include <machine/clock.h>
   65: #include <sys/tty.h>
   66: #include <sys/conf.h>
   67: #include <sys/fcntl.h>
   68: #include <sys/dkstat.h>
   69: #include <sys/malloc.h>
   70: #include <sys/sysctl.h>
   71: #include <sys/uio.h>
   72: #include <sys/proc.h>
   73: 
   74: #include "spicreg.h"
   75: 
   76: static int spic_pollrate;
   77: 
   78: SYSCTL_INT(_machdep, OID_AUTO, spic_pollrate, CTLFLAG_RW, &spic_pollrate, 0, "")
   79: ;
   80: 
   81: devclass_t spic_devclass;
   82: 
   83: static d_open_t		spicopen;
   84: static d_close_t	spicclose;
   85: static d_read_t		spicread;
   86: static d_ioctl_t	spicioctl;
   87: static d_poll_t		spicpoll;
   88: 
   89: static struct cdevsw spic_cdevsw = {
   90:         /* name */      "spic",
   91:         /* maj */       CDEV_MAJOR,
   92:         /* flags */     0,
   93: 	/* port */	NULL,
   94: 	/* clone */	NULL,
   95: 
   96:         /* open */      spicopen,
   97:         /* close */     spicclose,
   98:         /* read */      spicread,
   99:         /* write */     nowrite,
  100:         /* ioctl */     spicioctl,
  101:         /* poll */      spicpoll,
  102:         /* mmap */      nommap,
  103:         /* strategy */  nostrategy,
  104:         /* dump */      nodump,
  105:         /* psize */     nopsize
  106: };
  107: 
  108: #define SCBUFLEN 128
  109: 
  110: struct spic_softc {
  111: 	u_short sc_port_addr;
  112: 	u_char sc_intr;
  113: 	struct resource *sc_port_res,*sc_intr_res;
  114: 	int	sc_port_rid,sc_intr_rid;
  115: 	int sc_opened;
  116: 	int sc_sleeping;
  117: 	int sc_buttonlast;
  118: 	struct callout_handle sc_timeout_ch;
  119: 	device_t sc_dev;
  120: 	struct selinfo sc_rsel;
  121: 	u_char sc_buf[SCBUFLEN];
  122: 	int sc_count;
  123: 	int sc_model;
  124: };
  125: 
  126: static void
  127: write_port1(struct spic_softc *sc, u_char val)
  128: {
  129: 	DELAY(10);
  130: 	outb(sc->sc_port_addr, val);
  131: }
  132: 
  133: static void
  134: write_port2(struct spic_softc *sc, u_char val)
  135: {
  136: 	DELAY(10);
  137: 	outb(sc->sc_port_addr + 4, val);
  138: }
  139: 
  140: static u_char
  141: read_port1(struct spic_softc *sc)
  142: {
  143: 	DELAY(10);
  144: 	return inb(sc->sc_port_addr);
  145: }
  146: 
  147: static u_char
  148: read_port2(struct spic_softc *sc)
  149: {
  150: 	DELAY(10);
  151: 	return inb(sc->sc_port_addr + 4);
  152: }
  153: 
  154: static u_char
  155: read_port_cst(struct spic_softc *sc)
  156: {
  157: 	DELAY(10);
  158: 	return inb(SPIC_CST_IOPORT);
  159: }
  160: 
  161: static void
  162: busy_wait(struct spic_softc *sc)
  163: {
  164: 	int i=0;
  165: 
  166: 	while(read_port2(sc) & 2) {
  167: 		DELAY(10);
  168: 		if (i++>10000) {
  169: 			printf("spic busy wait abort\n");
  170: 			return;
  171: 		}
  172: 	}
  173: }
  174: 
  175: static void
  176: busy_wait_cst(struct spic_softc *sc, int mask)
  177: {
  178: 	int i=0;
  179: 
  180: 	while(read_port_cst(sc) & mask) {
  181: 		DELAY(10);
  182: 		if (i++>10000) {
  183: 			printf("spic busy wait abort\n");
  184: 			return;
  185: 		}
  186: 	}
  187: }
  188: 
  189: static u_char
  190: spic_call1(struct spic_softc *sc, u_char dev) {
  191: 	busy_wait(sc);
  192: 	write_port2(sc, dev);
  193: 	read_port2(sc);
  194: 	return read_port1(sc);
  195: }
  196: 
  197: static u_char
  198: spic_call2(struct spic_softc *sc, u_char dev, u_char fn)
  199: {
  200: 	busy_wait(sc);
  201: 	write_port2(sc, dev);
  202: 	busy_wait(sc);
  203: 	write_port1(sc, fn);
  204: 	return read_port1(sc);
  205: }
  206: 
  207: static void
  208: spic_ecrset(struct spic_softc *sc, u_int16_t addr, u_int16_t value)
  209: {
  210: 	busy_wait_cst(sc, 3);
  211: 	outb(SPIC_CST_IOPORT, 0x81);
  212: 	busy_wait_cst(sc, 2);
  213: 	outb(SPIC_DATA_IOPORT, addr);
  214: 	busy_wait_cst(sc, 2);
  215: 	outb(SPIC_DATA_IOPORT, value);
  216: 	busy_wait_cst(sc, 2);
  217: }
  218: 
  219: static void
  220: spic_type2_srs(struct spic_softc *sc)
  221: {
  222: 	spic_ecrset(sc, SPIC_SHIB, (sc->sc_port_addr & 0xFF00) >> 8);
  223: 	spic_ecrset(sc, SPIC_SLOB,  sc->sc_port_addr & 0x00FF);
  224: 	spic_ecrset(sc, SPIC_SIRQ,  0x00); /* using polling mode (IRQ=0)*/
  225: 	DELAY(10);
  226: }
  227: 
  228: static int
  229: spic_probe(device_t dev)
  230: {
  231: 	struct spic_softc *sc;
  232: 	u_char t, spic_irq;
  233: 
  234: 	sc = device_get_softc(dev);
  235: 
  236: 	/*
  237: 	 * We can only have 1 of these. Attempting to probe for a unit 1
  238: 	 * will destroy the work we did for unit 0
  239: 	 */
  240: 	if (device_get_unit(dev))
  241: 		return ENXIO;
  242: 
  243: 
  244: 	bzero(sc, sizeof(struct spic_softc));
  245: 
  246: 	if (!(sc->sc_port_res = bus_alloc_resource(dev, SYS_RES_IOPORT,
  247: 		&sc->sc_port_rid, 0, ~0, 5, RF_ACTIVE))) {
  248: 		device_printf(dev,"Couldn't map I/O\n");
  249: 		return ENXIO;
  250: 	}
  251: 	sc->sc_port_addr = (u_short)rman_get_start(sc->sc_port_res);
  252: 
  253: #ifdef notyet
  254: 	if (!(sc->sc_intr_res = bus_alloc_resource(dev, SYS_RES_IRQ,
  255: 		&sc->sc_intr_rid, 0, ~0, 1, RF_ACTIVE))) {
  256: 		device_printf(dev,"Couldn't map IRQ\n");
  257: 		bus_release_resource(dev, SYS_RES_IOPORT,
  258: 			sc->sc_port_rid, sc->sc_port_res);
  259: 		return ENXIO;
  260: 	}
  261: 	sc->sc_intr = (u_short)rman_get_start(sc->sc_intr_res);
  262: 
  263: 	switch (sc->sc_intr) {
  264: 		case 0: spic_irq = 3; break;
  265: 		case 5: spic_irq = 0; break;
  266: 		case 0xa: spic_irq = 1; break;
  267: 		case 0xb: spic_irq = 2; break;
  268: 		default: device_printf(dev,"Invalid IRQ\n");
  269: 			bus_release_resource(dev, SYS_RES_IOPORT,
  270: 				sc->sc_port_rid, sc->sc_port_res);
  271: 			bus_release_resource(dev, SYS_RES_IRQ,
  272: 				sc->sc_intr_rid, sc->sc_intr_res);
  273: 			return ENXIO;
  274: 	}
  275: #else
  276: 	spic_irq = 3;
  277: #endif
  278: 
  279: #if 0
  280: 	if (sc->sc_port_addr != 0x10A0) {
  281: 		bus_release_resource(dev, SYS_RES_IOPORT,
  282: 			sc->sc_port_rid, sc->sc_port_res);
  283: 		bus_release_resource(dev, SYS_RES_IRQ,
  284: 			sc->sc_intr_rid, sc->sc_intr_res);
  285: 		return ENXIO;
  286: 	}
  287: #endif
  288: 
  289: 	/* PIIX4 chipset at least? */
  290: 	if (pci_cfgregread(PIIX4_BUS, PIIX4_SLOT, PIIX4_FUNC, 0, 4) ==
  291: 		PIIX4_DEVID) {
  292: 		sc->sc_model = SPIC_DEVICE_MODEL_TYPE1;
  293: 	} else {
  294: 		/* For newer VAIOs (R505, SRX7, ...) */
  295: 		sc->sc_model = SPIC_DEVICE_MODEL_TYPE2;
  296: 	}
  297: 
  298: 	/*
  299: 	 * This is an ugly hack. It is necessary until ACPI works correctly.
  300: 	 *
  301: 	 * The SPIC consists of 2 registers. They are mapped onto I/O by the
  302: 	 * PIIX4's General Device 10 function. There is also an interrupt
  303: 	 * control port at a somewhat magic location, but this first pass is
  304: 	 * polled.
  305: 	 *
  306: 	 * So the first thing we need to do is map the G10 space in.
  307: 	 *
  308: 	 */
  309: 
  310: 	/* Enable ACPI mode to get Fn key events */
  311: 	/* XXX This may slow down your VAIO if ACPI is not supported in the kernel.
  312: 	outb(0xb2, 0xf0);
  313: 	 */
  314: 
  315: 	device_printf(dev,"device model type = %d\n", sc->sc_model);
  316: 	
  317: 	if(sc->sc_model == SPIC_DEVICE_MODEL_TYPE1) {
  318: 		pci_cfgregwrite(PIIX4_BUS, PIIX4_SLOT, PIIX4_FUNC, G10A,
  319: 				sc->sc_port_addr, 2);
  320: 		t = pci_cfgregread(PIIX4_BUS, PIIX4_SLOT, PIIX4_FUNC, G10L, 1);
  321: 		t &= 0xf0;
  322: 		t |= 4;
  323: 		pci_cfgregwrite(PIIX4_BUS, PIIX4_SLOT, PIIX4_FUNC, G10L, t, 1);
  324: 		outw(SPIC_IRQ_PORT, (inw(SPIC_IRQ_PORT) & ~(0x3 << SPIC_IRQ_SHIFT)) | (spic_irq << SPIC_IRQ_SHIFT));
  325: 		t = pci_cfgregread(PIIX4_BUS, PIIX4_SLOT, PIIX4_FUNC, G10L, 1);
  326: 		t &= 0x1f;
  327: 		t |= 0xc0;
  328: 		pci_cfgregwrite(PIIX4_BUS, PIIX4_SLOT, PIIX4_FUNC, G10L, t, 1);
  329: 	} else {
  330: 		spic_type2_srs(sc);
  331: 	}
  332: 
  333: 	/*
  334: 	 * XXX: Should try and see if there's anything actually there.
  335: 	 */
  336: 
  337: 	device_set_desc(dev, "Sony Programmable I/O Controller");
  338: 
  339: 	return 0;
  340: }
  341: 
  342: static int
  343: spic_attach(device_t dev)
  344: {
  345: 	struct spic_softc *sc;
  346: 
  347: 	sc = device_get_softc(dev);
  348: 
  349: 	sc->sc_dev = dev;
  350: 	
  351: 	spic_pollrate = (hz/50); /* Every 50th of a second */
  352: 
  353: 	spic_call1(sc, 0x82);
  354: 	spic_call2(sc, 0x81, 0xff);
  355: 	spic_call1(sc, 0x92);
  356: 
  357: 	/* There can be only one */
  358: 	make_dev(&spic_cdevsw, 0, 0, 0, 0600, "jogdial");
  359: 
  360: 	return 0;
  361: }
  362: 
  363: static void
  364: spictimeout(void *arg)
  365: {
  366: 	struct spic_softc *sc = arg;
  367: 	u_char b, event, param;
  368: 	int j;
  369: 
  370: 	if (!sc->sc_opened) {
  371: 		device_printf(sc->sc_dev, "timeout called while closed!\n");
  372: 		return;
  373: 	}
  374: 
  375: 	event = read_port2(sc);
  376: 	param = read_port1(sc);
  377: 
  378: 	if ((event != 4) && (!(event & 0x1)))
  379: 		switch(event) {
  380: 			case 0x10: /* jog wheel event (type1) */
  381: 				if (sc->sc_model == SPIC_DEVICE_MODEL_TYPE1) {
  382: 					b = !!(param & 0x40);
  383: 					if (b != sc->sc_buttonlast) {
  384: 						sc->sc_buttonlast = b;
  385: 						sc->sc_buf[sc->sc_count++] =
  386: 							b?'d':'u';
  387: 					}
  388: 					j = (param & 0xf) | ((param & 0x10)? ~0xf:0);
  389: 					if (j<0)
  390: 						while(j++!=0) {
  391: 							sc->sc_buf[sc->sc_count++] =
  392: 								'l';
  393: 						}
  394: 					else if (j>0)
  395: 						while(j--!=0) {
  396: 							sc->sc_buf[sc->sc_count++] =
  397: 								'r';
  398: 						}
  399: 				}
  400: 				break;
  401: 			case 0x08: /* jog wheel event (type2) */
  402: 			case 0x00: 
  403: 				/* SPIC_DEVICE_MODEL_TYPE2 returns jog wheel event=0x00 */
  404: 				if (sc->sc_model == SPIC_DEVICE_MODEL_TYPE2) {
  405: 					b = !!(param & 0x40);
  406: 					if (b != sc->sc_buttonlast) {
  407: 						sc->sc_buttonlast = b;
  408: 						sc->sc_buf[sc->sc_count++] =
  409: 							b?'d':'u';
  410: 					}
  411: 					j = (param & 0xf) | ((param & 0x10)? ~0xf:0);
  412: 					if (j<0)
  413: 						while(j++!=0) {
  414: 							sc->sc_buf[sc->sc_count++] =
  415: 								'l';
  416: 						}
  417: 					else if (j>0)
  418: 						while(j--!=0) {
  419: 							sc->sc_buf[sc->sc_count++] =
  420: 								'r';
  421: 						}
  422: 				}
  423: 				break;
  424: 			case 0x60: /* Capture button */
  425: 				printf("Capture button event: %x\n",param);
  426: 				break;
  427: 			case 0x30: /* Lid switch */
  428: 				printf("Lid switch event: %x\n",param);
  429: 				break;
  430: 			default:
  431: 				printf("Unknown event: event %02x param %02x\n", event, param);
  432: 				break;
  433: 		}
  434: 	else {
  435: 		/* No event. Wait some more */
  436: 		sc->sc_timeout_ch = timeout(spictimeout, sc, spic_pollrate);
  437: 		return;
  438: 	}
  439: 
  440: 	if (sc->sc_count) {
  441: 		if (sc->sc_sleeping) {
  442: 			sc->sc_sleeping = 0;
  443: 			wakeup((caddr_t) sc);
  444: 		}
  445: 		selwakeup(&sc->sc_rsel);
  446: 	}
  447: 	spic_call2(sc, 0x81, 0xff); /* Clear event */
  448: 
  449: 	sc->sc_timeout_ch = timeout(spictimeout, sc, spic_pollrate);
  450: }
  451: 
  452: static int
  453: spicopen(dev_t dev, int flag, int fmt, struct thread *td)
  454: {
  455: 	struct spic_softc *sc;
  456: 
  457: 	sc = devclass_get_softc(spic_devclass, 0);
  458: 
  459: 	if (sc->sc_opened)
  460: 		return EBUSY;
  461: 
  462: 	sc->sc_opened++;
  463: 	sc->sc_count=0;
  464: 
  465: 	/* Start the polling */
  466: 	timeout(spictimeout, sc, spic_pollrate);
  467: 	return 0;
  468: }
  469: 
  470: static int
  471: spicclose(dev_t dev, int flag, int fmt, struct thread *td)
  472: {
  473: 	struct spic_softc *sc;
  474: 
  475: 	sc = devclass_get_softc(spic_devclass, 0);
  476: 
  477: 	/* Stop polling */
  478: 	untimeout(spictimeout, sc, sc->sc_timeout_ch);
  479: 	sc->sc_opened = 0;
  480: 	return 0;
  481: }
  482: 
  483: static int
  484: spicread(dev_t dev, struct uio *uio, int flag)
  485: {
  486: 	struct spic_softc *sc;
  487: 	int l, s, error;
  488: 	u_char buf[SCBUFLEN];
  489: 
  490: 	sc = devclass_get_softc(spic_devclass, 0);
  491: 
  492: 	if (uio->uio_resid <= 0) /* What kind of a read is this?! */
  493: 		return 0;
  494: 
  495: 	s = spltty();
  496: 	while (!(sc->sc_count)) {
  497: 		sc->sc_sleeping=1;
  498: 		error = tsleep((caddr_t) sc, PCATCH, "jogrea", 0);
  499: 		sc->sc_sleeping=0;
  500: 		if (error) {
  501: 			splx(s);
  502: 			return error;
  503: 		}
  504: 	}
  505: 	splx(s);
  506: 
  507: 	s = spltty();
  508: 	l = min(uio->uio_resid, sc->sc_count);
  509: 	bcopy(sc->sc_buf, buf, l);
  510: 	sc->sc_count -= l;
  511: 	bcopy(sc->sc_buf + l, sc->sc_buf, l);
  512: 	splx(s);
  513: 	return uiomove(buf, l, uio);
  514: 
  515: }
  516: 
  517: static int
  518: spicioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
  519: {
  520: 	struct spic_softc *sc;
  521: 
  522: 	sc = devclass_get_softc(spic_devclass, 0);
  523: 
  524: 	return EIO;
  525: }
  526: 
  527: static int
  528: spicpoll(dev_t dev, int events, struct thread *td)
  529: {
  530: 	struct spic_softc *sc;
  531: 	struct proc *p;
  532: 	struct proc *p1;
  533: 	int revents = 0, s;
  534: 
  535: 	p = td->td_proc;
  536: 	KKASSERT(p);
  537: 
  538: 	sc = devclass_get_softc(spic_devclass, 0);
  539: 	s = spltty();
  540: 	if (events & (POLLIN | POLLRDNORM)) {
  541: 		if (sc->sc_count)
  542: 			revents |= events & (POLLIN | POLLRDNORM);
  543: 		else {
  544: 			if (sc->sc_rsel.si_pid && (p1=pfind(sc->sc_rsel.si_pid))
  545: 					&& p1->p_wchan == (caddr_t)&selwait)
  546: 				sc->sc_rsel.si_flags = SI_COLL;
  547: 			else
  548: 				sc->sc_rsel.si_pid = p->p_pid;
  549: 		}
  550: 	}
  551: 	splx(s);
  552: 
  553: 	return revents;
  554: }
  555: 
  556: 
  557: static device_method_t spic_methods[] = {
  558: 	DEVMETHOD(device_probe,		spic_probe),
  559: 	DEVMETHOD(device_attach,	spic_attach),
  560: 
  561: 	{ 0, 0 }
  562: };
  563: 
  564: static driver_t spic_driver = {
  565: 	"spic",
  566: 	spic_methods,
  567: 	sizeof(struct spic_softc),
  568: };
  569: 
  570: DRIVER_MODULE(spic, isa, spic_driver, spic_devclass, 0, 0);
  571: