2 * Copyright (c) 2000 Nick Sayer
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
26 * spic -- the Sony Programmable I/O Controller
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.
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.
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
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.
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.3 2003/07/19 21:14:34 dillon Exp $
54 #include <sys/param.h>
55 #include <sys/systm.h>
56 #include <sys/kernel.h>
58 #include <machine/bus.h>
60 #include <machine/resource.h>
61 #include <isa/isavar.h>
63 #include <machine/pci_cfgreg.h>
64 #include <machine/clock.h>
67 #include <sys/fcntl.h>
68 #include <sys/dkstat.h>
69 #include <sys/malloc.h>
70 #include <sys/sysctl.h>
74 #include <i386/isa/spicreg.h>
76 static int spic_pollrate;
78 SYSCTL_INT(_machdep, OID_AUTO, spic_pollrate, CTLFLAG_RW, &spic_pollrate, 0, "")
81 devclass_t spic_devclass;
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;
89 static struct cdevsw spic_cdevsw = {
91 /* close */ spicclose,
94 /* ioctl */ spicioctl,
97 /* strategy */ nostrategy,
108 u_short sc_port_addr;
110 struct resource *sc_port_res,*sc_intr_res;
111 int sc_port_rid,sc_intr_rid;
115 struct callout_handle sc_timeout_ch;
117 struct selinfo sc_rsel;
118 u_char sc_buf[SCBUFLEN];
124 write_port1(struct spic_softc *sc, u_char val)
127 outb(sc->sc_port_addr, val);
131 write_port2(struct spic_softc *sc, u_char val)
134 outb(sc->sc_port_addr + 4, val);
138 read_port1(struct spic_softc *sc)
141 return inb(sc->sc_port_addr);
145 read_port2(struct spic_softc *sc)
148 return inb(sc->sc_port_addr + 4);
152 read_port_cst(struct spic_softc *sc)
155 return inb(SPIC_CST_IOPORT);
159 busy_wait(struct spic_softc *sc)
163 while(read_port2(sc) & 2) {
166 printf("spic busy wait abort\n");
173 busy_wait_cst(struct spic_softc *sc, int mask)
177 while(read_port_cst(sc) & mask) {
180 printf("spic busy wait abort\n");
187 spic_call1(struct spic_softc *sc, u_char dev) {
189 write_port2(sc, dev);
191 return read_port1(sc);
195 spic_call2(struct spic_softc *sc, u_char dev, u_char fn)
198 write_port2(sc, dev);
201 return read_port1(sc);
205 spic_ecrset(struct spic_softc *sc, u_int16_t addr, u_int16_t value)
207 busy_wait_cst(sc, 3);
208 outb(SPIC_CST_IOPORT, 0x81);
209 busy_wait_cst(sc, 2);
210 outb(SPIC_DATA_IOPORT, addr);
211 busy_wait_cst(sc, 2);
212 outb(SPIC_DATA_IOPORT, value);
213 busy_wait_cst(sc, 2);
217 spic_type2_srs(struct spic_softc *sc)
219 spic_ecrset(sc, SPIC_SHIB, (sc->sc_port_addr & 0xFF00) >> 8);
220 spic_ecrset(sc, SPIC_SLOB, sc->sc_port_addr & 0x00FF);
221 spic_ecrset(sc, SPIC_SIRQ, 0x00); /* using polling mode (IRQ=0)*/
226 spic_probe(device_t dev)
228 struct spic_softc *sc;
231 sc = device_get_softc(dev);
234 * We can only have 1 of these. Attempting to probe for a unit 1
235 * will destroy the work we did for unit 0
237 if (device_get_unit(dev))
241 bzero(sc, sizeof(struct spic_softc));
243 if (!(sc->sc_port_res = bus_alloc_resource(dev, SYS_RES_IOPORT,
244 &sc->sc_port_rid, 0, ~0, 5, RF_ACTIVE))) {
245 device_printf(dev,"Couldn't map I/O\n");
248 sc->sc_port_addr = (u_short)rman_get_start(sc->sc_port_res);
251 if (!(sc->sc_intr_res = bus_alloc_resource(dev, SYS_RES_IRQ,
252 &sc->sc_intr_rid, 0, ~0, 1, RF_ACTIVE))) {
253 device_printf(dev,"Couldn't map IRQ\n");
254 bus_release_resource(dev, SYS_RES_IOPORT,
255 sc->sc_port_rid, sc->sc_port_res);
258 sc->sc_intr = (u_short)rman_get_start(sc->sc_intr_res);
260 switch (sc->sc_intr) {
261 case 0: spic_irq = 3; break;
262 case 5: spic_irq = 0; break;
263 case 0xa: spic_irq = 1; break;
264 case 0xb: spic_irq = 2; break;
265 default: device_printf(dev,"Invalid IRQ\n");
266 bus_release_resource(dev, SYS_RES_IOPORT,
267 sc->sc_port_rid, sc->sc_port_res);
268 bus_release_resource(dev, SYS_RES_IRQ,
269 sc->sc_intr_rid, sc->sc_intr_res);
277 if (sc->sc_port_addr != 0x10A0) {
278 bus_release_resource(dev, SYS_RES_IOPORT,
279 sc->sc_port_rid, sc->sc_port_res);
280 bus_release_resource(dev, SYS_RES_IRQ,
281 sc->sc_intr_rid, sc->sc_intr_res);
286 /* PIIX4 chipset at least? */
287 if (pci_cfgregread(PIIX4_BUS, PIIX4_SLOT, PIIX4_FUNC, 0, 4) ==
289 sc->sc_model = SPIC_DEVICE_MODEL_TYPE1;
291 /* For newer VAIOs (R505, SRX7, ...) */
292 sc->sc_model = SPIC_DEVICE_MODEL_TYPE2;
296 * This is an ugly hack. It is necessary until ACPI works correctly.
298 * The SPIC consists of 2 registers. They are mapped onto I/O by the
299 * PIIX4's General Device 10 function. There is also an interrupt
300 * control port at a somewhat magic location, but this first pass is
303 * So the first thing we need to do is map the G10 space in.
307 /* Enable ACPI mode to get Fn key events */
308 /* XXX This may slow down your VAIO if ACPI is not supported in the kernel.
312 device_printf(dev,"device model type = %d\n", sc->sc_model);
314 if(sc->sc_model == SPIC_DEVICE_MODEL_TYPE1) {
315 pci_cfgregwrite(PIIX4_BUS, PIIX4_SLOT, PIIX4_FUNC, G10A,
316 sc->sc_port_addr, 2);
317 t = pci_cfgregread(PIIX4_BUS, PIIX4_SLOT, PIIX4_FUNC, G10L, 1);
320 pci_cfgregwrite(PIIX4_BUS, PIIX4_SLOT, PIIX4_FUNC, G10L, t, 1);
321 outw(SPIC_IRQ_PORT, (inw(SPIC_IRQ_PORT) & ~(0x3 << SPIC_IRQ_SHIFT)) | (spic_irq << SPIC_IRQ_SHIFT));
322 t = pci_cfgregread(PIIX4_BUS, PIIX4_SLOT, PIIX4_FUNC, G10L, 1);
325 pci_cfgregwrite(PIIX4_BUS, PIIX4_SLOT, PIIX4_FUNC, G10L, t, 1);
331 * XXX: Should try and see if there's anything actually there.
334 device_set_desc(dev, "Sony Programmable I/O Controller");
340 spic_attach(device_t dev)
342 struct spic_softc *sc;
344 sc = device_get_softc(dev);
348 spic_pollrate = (hz/50); /* Every 50th of a second */
350 spic_call1(sc, 0x82);
351 spic_call2(sc, 0x81, 0xff);
352 spic_call1(sc, 0x92);
354 /* There can be only one */
355 make_dev(&spic_cdevsw, 0, 0, 0, 0600, "jogdial");
361 spictimeout(void *arg)
363 struct spic_softc *sc = arg;
364 u_char b, event, param;
367 if (!sc->sc_opened) {
368 device_printf(sc->sc_dev, "timeout called while closed!\n");
372 event = read_port2(sc);
373 param = read_port1(sc);
375 if ((event != 4) && (!(event & 0x1)))
377 case 0x10: /* jog wheel event (type1) */
378 if (sc->sc_model == SPIC_DEVICE_MODEL_TYPE1) {
379 b = !!(param & 0x40);
380 if (b != sc->sc_buttonlast) {
381 sc->sc_buttonlast = b;
382 sc->sc_buf[sc->sc_count++] =
385 j = (param & 0xf) | ((param & 0x10)? ~0xf:0);
388 sc->sc_buf[sc->sc_count++] =
393 sc->sc_buf[sc->sc_count++] =
398 case 0x08: /* jog wheel event (type2) */
400 /* SPIC_DEVICE_MODEL_TYPE2 returns jog wheel event=0x00 */
401 if (sc->sc_model == SPIC_DEVICE_MODEL_TYPE2) {
402 b = !!(param & 0x40);
403 if (b != sc->sc_buttonlast) {
404 sc->sc_buttonlast = b;
405 sc->sc_buf[sc->sc_count++] =
408 j = (param & 0xf) | ((param & 0x10)? ~0xf:0);
411 sc->sc_buf[sc->sc_count++] =
416 sc->sc_buf[sc->sc_count++] =
421 case 0x60: /* Capture button */
422 printf("Capture button event: %x\n",param);
424 case 0x30: /* Lid switch */
425 printf("Lid switch event: %x\n",param);
428 printf("Unknown event: event %02x param %02x\n", event, param);
432 /* No event. Wait some more */
433 sc->sc_timeout_ch = timeout(spictimeout, sc, spic_pollrate);
438 if (sc->sc_sleeping) {
440 wakeup((caddr_t) sc);
442 selwakeup(&sc->sc_rsel);
444 spic_call2(sc, 0x81, 0xff); /* Clear event */
446 sc->sc_timeout_ch = timeout(spictimeout, sc, spic_pollrate);
450 spicopen(dev_t dev, int flag, int fmt, struct proc *p)
452 struct spic_softc *sc;
454 sc = devclass_get_softc(spic_devclass, 0);
462 /* Start the polling */
463 timeout(spictimeout, sc, spic_pollrate);
468 spicclose(dev_t dev, int flag, int fmt, struct proc *p)
470 struct spic_softc *sc;
472 sc = devclass_get_softc(spic_devclass, 0);
475 untimeout(spictimeout, sc, sc->sc_timeout_ch);
481 spicread(dev_t dev, struct uio *uio, int flag)
483 struct spic_softc *sc;
485 u_char buf[SCBUFLEN];
487 sc = devclass_get_softc(spic_devclass, 0);
489 if (uio->uio_resid <= 0) /* What kind of a read is this?! */
493 while (!(sc->sc_count)) {
495 error = tsleep((caddr_t) sc, PCATCH, "jogrea", 0);
505 l = min(uio->uio_resid, sc->sc_count);
506 bcopy(sc->sc_buf, buf, l);
508 bcopy(sc->sc_buf + l, sc->sc_buf, l);
510 return uiomove(buf, l, uio);
515 spicioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
517 struct spic_softc *sc;
519 sc = devclass_get_softc(spic_devclass, 0);
525 spicpoll(dev_t dev, int events, struct proc *p)
527 struct spic_softc *sc;
531 sc = devclass_get_softc(spic_devclass, 0);
533 if (events & (POLLIN | POLLRDNORM)) {
535 revents |= events & (POLLIN | POLLRDNORM);
537 if (sc->sc_rsel.si_pid && (p1=pfind(sc->sc_rsel.si_pid))
538 && p1->p_wchan == (caddr_t)&selwait)
539 sc->sc_rsel.si_flags = SI_COLL;
541 sc->sc_rsel.si_pid = p->p_pid;
550 static device_method_t spic_methods[] = {
551 DEVMETHOD(device_probe, spic_probe),
552 DEVMETHOD(device_attach, spic_attach),
557 static driver_t spic_driver = {
560 sizeof(struct spic_softc),
563 DRIVER_MODULE(spic, isa, spic_driver, spic_devclass, 0, 0);