1139790Simp/*-
269873Snsayer * Copyright (c) 2000  Nick Sayer
369873Snsayer * All rights reserved.
469873Snsayer *
569873Snsayer * Redistribution and use in source and binary forms, with or without
669873Snsayer * modification, are permitted provided that the following conditions
769873Snsayer * are met:
869873Snsayer * 1. Redistributions of source code must retain the above copyright
969873Snsayer *    notice, this list of conditions and the following disclaimer.
1069873Snsayer * 2. Redistributions in binary form must reproduce the above copyright
1169873Snsayer *    notice, this list of conditions and the following disclaimer in the
1269873Snsayer *    documentation and/or other materials provided with the distribution.
1369873Snsayer *
1469873Snsayer * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1569873Snsayer * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1669873Snsayer * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1769873Snsayer * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1869873Snsayer * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1969873Snsayer * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2069873Snsayer * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2169873Snsayer * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2269873Snsayer * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2369873Snsayer * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2469873Snsayer * SUCH DAMAGE.
25139790Simp */
26139790Simp
27139790Simp/* spic -- the Sony Programmable I/O Controller
2869873Snsayer *
2969873Snsayer * This device exists on most recent Sony laptops. It is the means by which
3069873Snsayer * you can watch the Jog Dial and some other functions.
3169873Snsayer *
3269873Snsayer * At the moment, this driver merely tries to turn the jog dial into a
3369873Snsayer * device that moused can park on, with the intent of supplying a Z axis
3469873Snsayer * and mouse button out of the jog dial. I suspect that this device will
3569873Snsayer * end up having to support at least 2 different minor devices: One to be
3669873Snsayer * the jog wheel device for moused to camp out on and the other to perform
37249584Sgabor * all of the other miscellaneous functions of this device. But for now,
3869873Snsayer * the jog wheel is all you get.
3969873Snsayer *
4069873Snsayer * At the moment, the data sent back by the device is rather primitive.
4169873Snsayer * It sends a single character per event:
4269873Snsayer * u = up, d = down -- that's the jog button
4369873Snsayer * l = left, r = right -- that's the dial.
44249588Sgabor * "left" and "right" are rather capricious. They actually represent
4569873Snsayer * ccw and cw, respectively
4669873Snsayer *
4769873Snsayer * What documentation exists is thanks to Andrew Tridge, and his page at
4869873Snsayer * http://samba.org/picturebook/ Special thanks also to Ian Dowse, who
4969873Snsayer * also provided sample code upon which this driver was based.
5069873Snsayer */
5169873Snsayer
52115703Sobrien#include <sys/cdefs.h>
53115703Sobrien__FBSDID("$FreeBSD: stable/11/sys/i386/isa/spic.c 320921 2017-07-12 20:10:53Z jhb $");
54115703Sobrien
5569873Snsayer#include <sys/param.h>
5669873Snsayer#include <sys/systm.h>
5769873Snsayer#include <sys/kernel.h>
58129882Sphk#include <sys/module.h>
5969873Snsayer#include <sys/bus.h>
6069873Snsayer#include <machine/bus.h>
6169873Snsayer#include <sys/rman.h>
6269873Snsayer#include <machine/resource.h>
6369873Snsayer#include <isa/isavar.h>
6469873Snsayer#include <sys/poll.h>
6569873Snsayer#include <machine/pci_cfgreg.h>
6669873Snsayer#include <sys/tty.h>
6769873Snsayer#include <sys/conf.h>
6869873Snsayer#include <sys/fcntl.h>
6969873Snsayer#include <sys/malloc.h>
7069873Snsayer#include <sys/sysctl.h>
7169873Snsayer#include <sys/uio.h>
7269873Snsayer
7369873Snsayer#include <i386/isa/spicreg.h>
7469873Snsayer
7569873Snsayerstatic int spic_pollrate;
7669873Snsayer
7769873SnsayerSYSCTL_INT(_machdep, OID_AUTO, spic_pollrate, CTLFLAG_RW, &spic_pollrate, 0, "")
7869873Snsayer;
7969873Snsayer
8069873Snsayerdevclass_t spic_devclass;
8169873Snsayer
8269873Snsayerstatic d_open_t		spicopen;
8369873Snsayerstatic d_close_t	spicclose;
8469873Snsayerstatic d_read_t		spicread;
8569873Snsayerstatic d_ioctl_t	spicioctl;
8669873Snsayerstatic d_poll_t		spicpoll;
8769873Snsayer
8869873Snsayerstatic struct cdevsw spic_cdevsw = {
89126080Sphk	.d_version =	D_VERSION,
90111815Sphk	.d_open =	spicopen,
91111815Sphk	.d_close =	spicclose,
92111815Sphk	.d_read =	spicread,
93111815Sphk	.d_ioctl =	spicioctl,
94111815Sphk	.d_poll =	spicpoll,
95111815Sphk	.d_name =	"spic",
9669873Snsayer};
9769873Snsayer
9869873Snsayer#define SCBUFLEN 128
9969873Snsayer
10069873Snsayerstruct spic_softc {
101118509Sbde	u_int sc_port_addr;
10269873Snsayer	u_char sc_intr;
10369873Snsayer	struct resource *sc_port_res,*sc_intr_res;
10469873Snsayer	int	sc_port_rid,sc_intr_rid;
10569873Snsayer	int sc_opened;
10669873Snsayer	int sc_sleeping;
10769873Snsayer	int sc_buttonlast;
108274759Sjhb	struct callout sc_timeout;
109274759Sjhb	struct mtx sc_lock;
11069873Snsayer	device_t sc_dev;
111274759Sjhb	struct cdev *sc_cdev;
11269873Snsayer	struct selinfo sc_rsel;
11369873Snsayer	u_char sc_buf[SCBUFLEN];
11469873Snsayer	int sc_count;
11593071Swill	int sc_model;
11669873Snsayer};
11769873Snsayer
11869873Snsayerstatic void
11969873Snsayerwrite_port1(struct spic_softc *sc, u_char val)
12069873Snsayer{
12169873Snsayer	DELAY(10);
12269873Snsayer	outb(sc->sc_port_addr, val);
12369873Snsayer}
12469873Snsayer
12569873Snsayerstatic void
12669873Snsayerwrite_port2(struct spic_softc *sc, u_char val)
12769873Snsayer{
12869873Snsayer	DELAY(10);
12969873Snsayer	outb(sc->sc_port_addr + 4, val);
13069873Snsayer}
13169873Snsayer
13269873Snsayerstatic u_char
13369873Snsayerread_port1(struct spic_softc *sc)
13469873Snsayer{
13569873Snsayer	DELAY(10);
13669873Snsayer	return inb(sc->sc_port_addr);
13769873Snsayer}
13869873Snsayer
13969873Snsayerstatic u_char
14069873Snsayerread_port2(struct spic_softc *sc)
14169873Snsayer{
14269873Snsayer	DELAY(10);
14369873Snsayer	return inb(sc->sc_port_addr + 4);
14469873Snsayer}
14569873Snsayer
14693071Swillstatic u_char
14793071Swillread_port_cst(struct spic_softc *sc)
14893071Swill{
14993071Swill	DELAY(10);
15093071Swill	return inb(SPIC_CST_IOPORT);
15193071Swill}
15293071Swill
15369873Snsayerstatic void
15469873Snsayerbusy_wait(struct spic_softc *sc)
15569873Snsayer{
15669873Snsayer	int i=0;
15769873Snsayer
15869873Snsayer	while(read_port2(sc) & 2) {
15969873Snsayer		DELAY(10);
16069873Snsayer		if (i++>10000) {
16169873Snsayer			printf("spic busy wait abort\n");
16269873Snsayer			return;
16369873Snsayer		}
16469873Snsayer	}
16569873Snsayer}
16669873Snsayer
16793071Swillstatic void
16893071Swillbusy_wait_cst(struct spic_softc *sc, int mask)
16993071Swill{
17093071Swill	int i=0;
17193071Swill
17293071Swill	while(read_port_cst(sc) & mask) {
17393071Swill		DELAY(10);
17493071Swill		if (i++>10000) {
17593071Swill			printf("spic busy wait abort\n");
17693071Swill			return;
17793071Swill		}
17893071Swill	}
17993071Swill}
18093071Swill
18169873Snsayerstatic u_char
18269873Snsayerspic_call1(struct spic_softc *sc, u_char dev) {
18369873Snsayer	busy_wait(sc);
18469873Snsayer	write_port2(sc, dev);
18569873Snsayer	read_port2(sc);
18669873Snsayer	return read_port1(sc);
18769873Snsayer}
18869873Snsayer
18969873Snsayerstatic u_char
19069873Snsayerspic_call2(struct spic_softc *sc, u_char dev, u_char fn)
19169873Snsayer{
19269873Snsayer	busy_wait(sc);
19369873Snsayer	write_port2(sc, dev);
19469873Snsayer	busy_wait(sc);
19569873Snsayer	write_port1(sc, fn);
19669873Snsayer	return read_port1(sc);
19769873Snsayer}
19869873Snsayer
19993071Swillstatic void
20093071Swillspic_ecrset(struct spic_softc *sc, u_int16_t addr, u_int16_t value)
20193071Swill{
20293071Swill	busy_wait_cst(sc, 3);
20393071Swill	outb(SPIC_CST_IOPORT, 0x81);
20493071Swill	busy_wait_cst(sc, 2);
20593071Swill	outb(SPIC_DATA_IOPORT, addr);
20693071Swill	busy_wait_cst(sc, 2);
20793071Swill	outb(SPIC_DATA_IOPORT, value);
20893071Swill	busy_wait_cst(sc, 2);
20993071Swill}
21093071Swill
21193071Swillstatic void
21293071Swillspic_type2_srs(struct spic_softc *sc)
21393071Swill{
21493071Swill	spic_ecrset(sc, SPIC_SHIB, (sc->sc_port_addr & 0xFF00) >> 8);
21593071Swill	spic_ecrset(sc, SPIC_SLOB,  sc->sc_port_addr & 0x00FF);
21693071Swill	spic_ecrset(sc, SPIC_SIRQ,  0x00); /* using polling mode (IRQ=0)*/
21793071Swill	DELAY(10);
21893071Swill}
21993071Swill
22069873Snsayerstatic int
22169873Snsayerspic_probe(device_t dev)
22269873Snsayer{
22393071Swill	struct spic_softc *sc;
22469873Snsayer	u_char t, spic_irq;
22569873Snsayer
22693071Swill	sc = device_get_softc(dev);
22769873Snsayer
22869873Snsayer	/*
22969873Snsayer	 * We can only have 1 of these. Attempting to probe for a unit 1
23069873Snsayer	 * will destroy the work we did for unit 0
23169873Snsayer	 */
23269873Snsayer	if (device_get_unit(dev))
23369873Snsayer		return ENXIO;
23469873Snsayer
23569873Snsayer	bzero(sc, sizeof(struct spic_softc));
23669873Snsayer
237296137Sjhibbits	if (!(sc->sc_port_res = bus_alloc_resource_anywhere(dev, SYS_RES_IOPORT,
238296137Sjhibbits		&sc->sc_port_rid, 5, RF_ACTIVE))) {
23969873Snsayer		device_printf(dev,"Couldn't map I/O\n");
24069873Snsayer		return ENXIO;
24169873Snsayer	}
24269873Snsayer	sc->sc_port_addr = (u_short)rman_get_start(sc->sc_port_res);
24369873Snsayer
24469873Snsayer#ifdef notyet
245127135Snjl	if (!(sc->sc_intr_res = bus_alloc_resource_any(dev, SYS_RES_IRQ,
246127135Snjl		&sc->sc_intr_rid, RF_ACTIVE))) {
24769873Snsayer		device_printf(dev,"Couldn't map IRQ\n");
24869873Snsayer		bus_release_resource(dev, SYS_RES_IOPORT,
24969873Snsayer			sc->sc_port_rid, sc->sc_port_res);
25069873Snsayer		return ENXIO;
25169873Snsayer	}
25269873Snsayer	sc->sc_intr = (u_short)rman_get_start(sc->sc_intr_res);
25369873Snsayer
25469873Snsayer	switch (sc->sc_intr) {
25569873Snsayer		case 0: spic_irq = 3; break;
25669873Snsayer		case 5: spic_irq = 0; break;
25769873Snsayer		case 0xa: spic_irq = 1; break;
25869873Snsayer		case 0xb: spic_irq = 2; break;
25969873Snsayer		default: device_printf(dev,"Invalid IRQ\n");
26069873Snsayer			bus_release_resource(dev, SYS_RES_IOPORT,
26169873Snsayer				sc->sc_port_rid, sc->sc_port_res);
26269873Snsayer			bus_release_resource(dev, SYS_RES_IRQ,
26369873Snsayer				sc->sc_intr_rid, sc->sc_intr_res);
26469873Snsayer			return ENXIO;
26569873Snsayer	}
26669873Snsayer#else
26769873Snsayer	spic_irq = 3;
26869873Snsayer#endif
26969873Snsayer
27069873Snsayer#if 0
27169873Snsayer	if (sc->sc_port_addr != 0x10A0) {
27269873Snsayer		bus_release_resource(dev, SYS_RES_IOPORT,
27369873Snsayer			sc->sc_port_rid, sc->sc_port_res);
27469873Snsayer		bus_release_resource(dev, SYS_RES_IRQ,
27569873Snsayer			sc->sc_intr_rid, sc->sc_intr_res);
27669873Snsayer		return ENXIO;
27769873Snsayer	}
27869873Snsayer#endif
27969873Snsayer
28069873Snsayer	/* PIIX4 chipset at least? */
28193071Swill	if (pci_cfgregread(PIIX4_BUS, PIIX4_SLOT, PIIX4_FUNC, 0, 4) ==
28293071Swill		PIIX4_DEVID) {
28393071Swill		sc->sc_model = SPIC_DEVICE_MODEL_TYPE1;
28493071Swill	} else {
28593071Swill		/* For newer VAIOs (R505, SRX7, ...) */
28693071Swill		sc->sc_model = SPIC_DEVICE_MODEL_TYPE2;
28793071Swill	}
28869873Snsayer
28969873Snsayer	/*
29069873Snsayer	 * This is an ugly hack. It is necessary until ACPI works correctly.
29169873Snsayer	 *
29269873Snsayer	 * The SPIC consists of 2 registers. They are mapped onto I/O by the
29369873Snsayer	 * PIIX4's General Device 10 function. There is also an interrupt
29469873Snsayer	 * control port at a somewhat magic location, but this first pass is
29569873Snsayer	 * polled.
29669873Snsayer	 *
29769873Snsayer	 * So the first thing we need to do is map the G10 space in.
29869873Snsayer	 *
29969873Snsayer	 */
30069873Snsayer
30193071Swill	/* Enable ACPI mode to get Fn key events */
30293071Swill	/* XXX This may slow down your VAIO if ACPI is not supported in the kernel.
30393071Swill	outb(0xb2, 0xf0);
30493071Swill	*/
30569873Snsayer
30693071Swill	device_printf(dev,"device model type = %d\n", sc->sc_model);
30793071Swill
30893071Swill	if(sc->sc_model == SPIC_DEVICE_MODEL_TYPE1) {
30993071Swill		pci_cfgregwrite(PIIX4_BUS, PIIX4_SLOT, PIIX4_FUNC, G10A,
31093071Swill				sc->sc_port_addr, 2);
31193071Swill		t = pci_cfgregread(PIIX4_BUS, PIIX4_SLOT, PIIX4_FUNC, G10L, 1);
31293071Swill		t &= 0xf0;
31393071Swill		t |= 4;
31493071Swill		pci_cfgregwrite(PIIX4_BUS, PIIX4_SLOT, PIIX4_FUNC, G10L, t, 1);
31593071Swill		outw(SPIC_IRQ_PORT, (inw(SPIC_IRQ_PORT) & ~(0x3 << SPIC_IRQ_SHIFT)) | (spic_irq << SPIC_IRQ_SHIFT));
31693071Swill		t = pci_cfgregread(PIIX4_BUS, PIIX4_SLOT, PIIX4_FUNC, G10L, 1);
31793071Swill		t &= 0x1f;
31893071Swill		t |= 0xc0;
31993071Swill		pci_cfgregwrite(PIIX4_BUS, PIIX4_SLOT, PIIX4_FUNC, G10L, t, 1);
32093071Swill	} else {
32193071Swill		spic_type2_srs(sc);
32293071Swill	}
32393071Swill
32469873Snsayer	/*
32569873Snsayer	 * XXX: Should try and see if there's anything actually there.
32669873Snsayer	 */
32769873Snsayer
32869873Snsayer	device_set_desc(dev, "Sony Programmable I/O Controller");
32969873Snsayer
33069873Snsayer	return 0;
33169873Snsayer}
33269873Snsayer
33369873Snsayerstatic int
33469873Snsayerspic_attach(device_t dev)
33569873Snsayer{
33693071Swill	struct spic_softc *sc;
33769873Snsayer
33893071Swill	sc = device_get_softc(dev);
33969873Snsayer
34069873Snsayer	sc->sc_dev = dev;
341274759Sjhb	mtx_init(&sc->sc_lock, "spic", NULL, MTX_DEF);
342274759Sjhb	callout_init_mtx(&sc->sc_timeout, &sc->sc_lock, 0);
34369873Snsayer
34469873Snsayer	spic_pollrate = (hz/50); /* Every 50th of a second */
34569873Snsayer
34669873Snsayer	spic_call1(sc, 0x82);
34769873Snsayer	spic_call2(sc, 0x81, 0xff);
34869873Snsayer	spic_call1(sc, 0x92);
34969873Snsayer
35093071Swill	/* There can be only one */
351274759Sjhb	sc->sc_cdev = make_dev(&spic_cdevsw, 0, 0, 0, 0600, "jogdial");
352274759Sjhb	sc->sc_cdev->si_drv1 = sc;
353320921Sjhb	device_printf(dev,
354320921Sjhb	    "WARNING: This driver is deprecated and will be removed.\n");
35569873Snsayer
35669873Snsayer	return 0;
35769873Snsayer}
35869873Snsayer
35969873Snsayerstatic void
36069873Snsayerspictimeout(void *arg)
36169873Snsayer{
36293071Swill	struct spic_softc *sc = arg;
36369873Snsayer	u_char b, event, param;
36469873Snsayer	int j;
36569873Snsayer
366274759Sjhb	mtx_assert(&sc->sc_lock, MA_OWNED);
36793071Swill	if (!sc->sc_opened) {
36893071Swill		device_printf(sc->sc_dev, "timeout called while closed!\n");
36993071Swill		return;
37093071Swill	}
37169873Snsayer
37269873Snsayer	event = read_port2(sc);
37369873Snsayer	param = read_port1(sc);
37469873Snsayer
37569873Snsayer	if ((event != 4) && (!(event & 0x1)))
37669873Snsayer		switch(event) {
37793071Swill			case 0x10: /* jog wheel event (type1) */
37893071Swill				if (sc->sc_model == SPIC_DEVICE_MODEL_TYPE1) {
37993071Swill					b = !!(param & 0x40);
38093071Swill					if (b != sc->sc_buttonlast) {
38193071Swill						sc->sc_buttonlast = b;
38269873Snsayer						sc->sc_buf[sc->sc_count++] =
38393071Swill							b?'d':'u';
38469873Snsayer					}
38593071Swill					j = (param & 0xf) | ((param & 0x10)? ~0xf:0);
38693071Swill					if (j<0)
38793071Swill						while(j++!=0) {
38893071Swill							sc->sc_buf[sc->sc_count++] =
38993071Swill								'l';
39093071Swill						}
39193071Swill					else if (j>0)
39293071Swill						while(j--!=0) {
39393071Swill							sc->sc_buf[sc->sc_count++] =
39493071Swill								'r';
39593071Swill						}
39693071Swill				}
39793071Swill				break;
39893071Swill			case 0x08: /* jog wheel event (type2) */
39993071Swill			case 0x00:
40093071Swill				/* SPIC_DEVICE_MODEL_TYPE2 returns jog wheel event=0x00 */
40193071Swill				if (sc->sc_model == SPIC_DEVICE_MODEL_TYPE2) {
40293071Swill					b = !!(param & 0x40);
40393071Swill					if (b != sc->sc_buttonlast) {
40493071Swill						sc->sc_buttonlast = b;
40569873Snsayer						sc->sc_buf[sc->sc_count++] =
40693071Swill							b?'d':'u';
40769873Snsayer					}
40893071Swill					j = (param & 0xf) | ((param & 0x10)? ~0xf:0);
40993071Swill					if (j<0)
41093071Swill						while(j++!=0) {
41193071Swill							sc->sc_buf[sc->sc_count++] =
41293071Swill								'l';
41393071Swill						}
41493071Swill					else if (j>0)
41593071Swill						while(j--!=0) {
41693071Swill							sc->sc_buf[sc->sc_count++] =
41793071Swill								'r';
41893071Swill						}
41993071Swill				}
42069873Snsayer				break;
42169873Snsayer			case 0x60: /* Capture button */
42269873Snsayer				printf("Capture button event: %x\n",param);
42369873Snsayer				break;
42469873Snsayer			case 0x30: /* Lid switch */
42569873Snsayer				printf("Lid switch event: %x\n",param);
42669873Snsayer				break;
42793071Swill			case 0x0c: /* We must ignore these type of event for C1VP... */
42893071Swill			case 0x70: /* Closing/Opening the lid on C1VP */
42993071Swill				break;
43069873Snsayer			default:
43193071Swill				printf("Unknown event: event %02x param %02x\n", event, param);
43269873Snsayer				break;
43369873Snsayer		}
43469873Snsayer	else {
43569873Snsayer		/* No event. Wait some more */
436274759Sjhb		callout_reset(&sc->sc_timeout, spic_pollrate, spictimeout, sc);
43769873Snsayer		return;
43869873Snsayer	}
43969873Snsayer
44069873Snsayer	if (sc->sc_count) {
44169873Snsayer		if (sc->sc_sleeping) {
44269873Snsayer			sc->sc_sleeping = 0;
443111748Sdes			wakeup( sc);
44469873Snsayer		}
445122352Stanimura		selwakeuppri(&sc->sc_rsel, PZERO);
44669873Snsayer	}
44769873Snsayer	spic_call2(sc, 0x81, 0xff); /* Clear event */
44869873Snsayer
449274759Sjhb	callout_reset(&sc->sc_timeout, spic_pollrate, spictimeout, sc);
45069873Snsayer}
45169873Snsayer
45269873Snsayerstatic int
453130585Sphkspicopen(struct cdev *dev, int flag, int fmt, struct thread *td)
45469873Snsayer{
45593071Swill	struct spic_softc *sc;
45669873Snsayer
457274759Sjhb	sc = dev->si_drv1;
45869873Snsayer
459274759Sjhb	mtx_lock(&sc->sc_lock);
460274759Sjhb	if (sc->sc_opened) {
461274759Sjhb		mtx_unlock(&sc->sc_lock);
462274759Sjhb		return (EBUSY);
463274759Sjhb	}
46469873Snsayer
46569873Snsayer	sc->sc_opened++;
46669873Snsayer	sc->sc_count=0;
46769873Snsayer
46893071Swill	/* Start the polling */
469274759Sjhb	callout_reset(&sc->sc_timeout, spic_pollrate, spictimeout, sc);
470274759Sjhb	mtx_unlock(&sc->sc_lock);
471274759Sjhb	return (0);
47269873Snsayer}
47369873Snsayer
47469873Snsayerstatic int
475130585Sphkspicclose(struct cdev *dev, int flag, int fmt, struct thread *td)
47669873Snsayer{
47793071Swill	struct spic_softc *sc;
47869873Snsayer
479274759Sjhb	sc = dev->si_drv1;
480274759Sjhb	mtx_lock(&sc->sc_lock);
48169873Snsayer
48293071Swill	/* Stop polling */
483274759Sjhb	callout_stop(&sc->sc_timeout);
48493071Swill	sc->sc_opened = 0;
485274759Sjhb	mtx_unlock(&sc->sc_lock);
48693071Swill	return 0;
48769873Snsayer}
48869873Snsayer
48969873Snsayerstatic int
490130585Sphkspicread(struct cdev *dev, struct uio *uio, int flag)
49169873Snsayer{
49293071Swill	struct spic_softc *sc;
493274759Sjhb	int l, error;
49469873Snsayer	u_char buf[SCBUFLEN];
49569873Snsayer
496274759Sjhb	sc = dev->si_drv1;
49769873Snsayer
49869873Snsayer	if (uio->uio_resid <= 0) /* What kind of a read is this?! */
499274759Sjhb		return (0);
50069873Snsayer
501274759Sjhb	mtx_lock(&sc->sc_lock);
50269873Snsayer	while (!(sc->sc_count)) {
50369873Snsayer		sc->sc_sleeping=1;
504274759Sjhb		error = mtx_sleep(sc, &sc->sc_lock, PZERO | PCATCH, "jogrea", 0);
50569873Snsayer		sc->sc_sleeping=0;
50669873Snsayer		if (error) {
507274759Sjhb			mtx_unlock(&sc->sc_lock);
508274759Sjhb			return (error);
50969873Snsayer		}
51069873Snsayer	}
51169873Snsayer
51269873Snsayer	l = min(uio->uio_resid, sc->sc_count);
51369873Snsayer	bcopy(sc->sc_buf, buf, l);
51469873Snsayer	sc->sc_count -= l;
51569873Snsayer	bcopy(sc->sc_buf + l, sc->sc_buf, l);
516274759Sjhb	mtx_unlock(&sc->sc_lock);
517274759Sjhb	return (uiomove(buf, l, uio));
51869873Snsayer}
51969873Snsayer
52069873Snsayerstatic int
521130585Sphkspicioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
52269873Snsayer{
52393071Swill	struct spic_softc *sc;
52469873Snsayer
525274759Sjhb	sc = dev->si_drv1;
52669873Snsayer
527274759Sjhb	return (EIO);
52869873Snsayer}
52969873Snsayer
53069873Snsayerstatic int
531130585Sphkspicpoll(struct cdev *dev, int events, struct thread *td)
53269873Snsayer{
53393071Swill	struct spic_softc *sc;
534274759Sjhb	int revents = 0;
53569873Snsayer
536274759Sjhb	sc = dev->si_drv1;
537274759Sjhb	mtx_lock(&sc->sc_lock);
53893071Swill	if (events & (POLLIN | POLLRDNORM)) {
53993071Swill		if (sc->sc_count)
54093071Swill			revents |= events & (POLLIN | POLLRDNORM);
54193071Swill		else
54293071Swill			selrecord(td, &sc->sc_rsel); /* Who shall we wake? */
54393071Swill	}
544274759Sjhb	mtx_unlock(&sc->sc_lock);
54569873Snsayer
546274759Sjhb	return (revents);
54769873Snsayer}
54869873Snsayer
54969873Snsayer
55069873Snsayerstatic device_method_t spic_methods[] = {
55169873Snsayer	DEVMETHOD(device_probe,		spic_probe),
55269873Snsayer	DEVMETHOD(device_attach,	spic_attach),
55369873Snsayer
55469873Snsayer	{ 0, 0 }
55569873Snsayer};
55669873Snsayer
55769873Snsayerstatic driver_t spic_driver = {
55869873Snsayer	"spic",
55969873Snsayer	spic_methods,
56069873Snsayer	sizeof(struct spic_softc),
56169873Snsayer};
56269873Snsayer
56369873SnsayerDRIVER_MODULE(spic, isa, spic_driver, spic_devclass, 0, 0);
56469873Snsayer
565