spic.c revision 83366
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: head/sys/i386/isa/spic.c 83366 2001-09-12 08:38:13Z julian $ 51 */ 52 53#include <sys/param.h> 54#include <sys/systm.h> 55#include <sys/kernel.h> 56#include <sys/bus.h> 57#include <machine/bus.h> 58#include <sys/rman.h> 59#include <machine/resource.h> 60#include <isa/isavar.h> 61#include <sys/poll.h> 62#include <machine/pci_cfgreg.h> 63#include <machine/clock.h> 64#include <sys/tty.h> 65#include <sys/conf.h> 66#include <sys/fcntl.h> 67#include <sys/dkstat.h> 68#include <sys/malloc.h> 69#include <sys/sysctl.h> 70#include <sys/uio.h> 71 72#include <i386/isa/spicreg.h> 73 74static int spic_pollrate; 75 76SYSCTL_INT(_machdep, OID_AUTO, spic_pollrate, CTLFLAG_RW, &spic_pollrate, 0, "") 77; 78 79devclass_t spic_devclass; 80 81static d_open_t spicopen; 82static d_close_t spicclose; 83static d_read_t spicread; 84static d_ioctl_t spicioctl; 85static d_poll_t spicpoll; 86 87static struct cdevsw spic_cdevsw = { 88 /* open */ spicopen, 89 /* close */ spicclose, 90 /* read */ spicread, 91 /* write */ nowrite, 92 /* ioctl */ spicioctl, 93 /* poll */ spicpoll, 94 /* mmap */ nommap, 95 /* strategy */ nostrategy, 96 /* name */ "spic", 97 /* maj */ CDEV_MAJOR, 98 /* dump */ nodump, 99 /* psize */ nopsize, 100 /* flags */ 0, 101}; 102 103#define SCBUFLEN 128 104 105struct spic_softc { 106 u_short sc_port_addr; 107 u_char sc_intr; 108 struct resource *sc_port_res,*sc_intr_res; 109 int sc_port_rid,sc_intr_rid; 110 int sc_opened; 111 int sc_sleeping; 112 int sc_buttonlast; 113 struct callout_handle sc_timeout_ch; 114 device_t sc_dev; 115 struct selinfo sc_rsel; 116 u_char sc_buf[SCBUFLEN]; 117 int sc_count; 118}; 119 120static void 121write_port1(struct spic_softc *sc, u_char val) 122{ 123 DELAY(10); 124 outb(sc->sc_port_addr, val); 125} 126 127static void 128write_port2(struct spic_softc *sc, u_char val) 129{ 130 DELAY(10); 131 outb(sc->sc_port_addr + 4, val); 132} 133 134static u_char 135read_port1(struct spic_softc *sc) 136{ 137 DELAY(10); 138 return inb(sc->sc_port_addr); 139} 140 141static u_char 142read_port2(struct spic_softc *sc) 143{ 144 DELAY(10); 145 return inb(sc->sc_port_addr + 4); 146} 147 148static void 149busy_wait(struct spic_softc *sc) 150{ 151 int i=0; 152 153 while(read_port2(sc) & 2) { 154 DELAY(10); 155 if (i++>10000) { 156 printf("spic busy wait abort\n"); 157 return; 158 } 159 } 160} 161 162static u_char 163spic_call1(struct spic_softc *sc, u_char dev) { 164 busy_wait(sc); 165 write_port2(sc, dev); 166 read_port2(sc); 167 return read_port1(sc); 168} 169 170static u_char 171spic_call2(struct spic_softc *sc, u_char dev, u_char fn) 172{ 173 busy_wait(sc); 174 write_port2(sc, dev); 175 busy_wait(sc); 176 write_port1(sc, fn); 177 return read_port1(sc); 178} 179 180static int 181spic_probe(device_t dev) 182{ 183 struct spic_softc *sc; 184 u_char t, spic_irq; 185 186 sc = device_get_softc(dev); 187 188 /* 189 * We can only have 1 of these. Attempting to probe for a unit 1 190 * will destroy the work we did for unit 0 191 */ 192 if (device_get_unit(dev)) 193 return ENXIO; 194 195 bzero(sc, sizeof(struct spic_softc)); 196 197 if (!(sc->sc_port_res = bus_alloc_resource(dev, SYS_RES_IOPORT, 198 &sc->sc_port_rid, 0, ~0, 5, RF_ACTIVE))) { 199 device_printf(dev,"Couldn't map I/O\n"); 200 return ENXIO; 201 } 202 sc->sc_port_addr = (u_short)rman_get_start(sc->sc_port_res); 203 204#ifdef notyet 205 if (!(sc->sc_intr_res = bus_alloc_resource(dev, SYS_RES_IRQ, 206 &sc->sc_intr_rid, 0, ~0, 1, RF_ACTIVE))) { 207 device_printf(dev,"Couldn't map IRQ\n"); 208 bus_release_resource(dev, SYS_RES_IOPORT, 209 sc->sc_port_rid, sc->sc_port_res); 210 return ENXIO; 211 } 212 sc->sc_intr = (u_short)rman_get_start(sc->sc_intr_res); 213 214 switch (sc->sc_intr) { 215 case 0: spic_irq = 3; break; 216 case 5: spic_irq = 0; break; 217 case 0xa: spic_irq = 1; break; 218 case 0xb: spic_irq = 2; break; 219 default: device_printf(dev,"Invalid IRQ\n"); 220 bus_release_resource(dev, SYS_RES_IOPORT, 221 sc->sc_port_rid, sc->sc_port_res); 222 bus_release_resource(dev, SYS_RES_IRQ, 223 sc->sc_intr_rid, sc->sc_intr_res); 224 return ENXIO; 225 } 226#else 227 spic_irq = 3; 228#endif 229 230#if 0 231 if (sc->sc_port_addr != 0x10A0) { 232 bus_release_resource(dev, SYS_RES_IOPORT, 233 sc->sc_port_rid, sc->sc_port_res); 234 bus_release_resource(dev, SYS_RES_IRQ, 235 sc->sc_intr_rid, sc->sc_intr_res); 236 return ENXIO; 237 } 238#endif 239 240 /* PIIX4 chipset at least? */ 241 if (pci_cfgregread(PIIX4_BUS, PIIX4_SLOT, PIIX4_FUNC, 0, 4) != 242 PIIX4_DEVID) 243 return ENXIO; 244 245 /* 246 * This is an ugly hack. It is necessary until ACPI works correctly. 247 * 248 * The SPIC consists of 2 registers. They are mapped onto I/O by the 249 * PIIX4's General Device 10 function. There is also an interrupt 250 * control port at a somewhat magic location, but this first pass is 251 * polled. 252 * 253 * So the first thing we need to do is map the G10 space in. 254 * 255 */ 256 257 pci_cfgregwrite(PIIX4_BUS, PIIX4_SLOT, PIIX4_FUNC, G10A, 258 sc->sc_port_addr, 2); 259 t = pci_cfgregread(PIIX4_BUS, PIIX4_SLOT, PIIX4_FUNC, G10L, 1); 260 t &= 0xf0; 261 t |= 4; 262 pci_cfgregwrite(PIIX4_BUS, PIIX4_SLOT, PIIX4_FUNC, G10L, t, 1); 263 outw(SPIC_IRQ_PORT, (inw(SPIC_IRQ_PORT) & ~(0x3 << SPIC_IRQ_SHIFT)) | (spic_irq << SPIC_IRQ_SHIFT)); 264 t = pci_cfgregread(PIIX4_BUS, PIIX4_SLOT, PIIX4_FUNC, G10L, 1); 265 t &= 0x1f; 266 t |= 0xc0; 267 pci_cfgregwrite(PIIX4_BUS, PIIX4_SLOT, PIIX4_FUNC, G10L, t, 1); 268 269 /* 270 * XXX: Should try and see if there's anything actually there. 271 */ 272 273 device_set_desc(dev, "Sony Programmable I/O Controller"); 274 275 return 0; 276} 277 278static int 279spic_attach(device_t dev) 280{ 281 struct spic_softc *sc; 282 283 sc = device_get_softc(dev); 284 285 sc->sc_dev = dev; 286 287 spic_pollrate = (hz/50); /* Every 50th of a second */ 288 289 spic_call1(sc, 0x82); 290 spic_call2(sc, 0x81, 0xff); 291 spic_call1(sc, 0x92); 292 293 /* There can be only one */ 294 make_dev(&spic_cdevsw, 0, 0, 0, 0600, "jogdial"); 295 296 return 0; 297} 298 299static void 300spictimeout(void *arg) 301{ 302 struct spic_softc *sc = arg; 303 u_char b, event, param; 304 int j; 305 306 if (!sc->sc_opened) { 307 device_printf(sc->sc_dev, "timeout called while closed!\n"); 308 return; 309 } 310 311 event = read_port2(sc); 312 param = read_port1(sc); 313 314 if ((event != 4) && (!(event & 0x1))) 315 switch(event) { 316 case 0x10: /* jog wheel event */ 317 b = !!(param & 0x40); 318 if (b != sc->sc_buttonlast) { 319 sc->sc_buttonlast = b; 320 sc->sc_buf[sc->sc_count++] = 321 b?'d':'u'; 322 } 323 j = (param & 0xf) | ((param & 0x10)? ~0xf:0); 324 if (j<0) 325 while(j++!=0) { 326 sc->sc_buf[sc->sc_count++] = 327 'l'; 328 } 329 else if (j>0) 330 while(j--!=0) { 331 sc->sc_buf[sc->sc_count++] = 332 'r'; 333 } 334 break; 335 case 0x60: /* Capture button */ 336 printf("Capture button event: %x\n",param); 337 break; 338 case 0x30: /* Lid switch */ 339 printf("Lid switch event: %x\n",param); 340 break; 341 default: 342 printf("Unknown event: %x %x\n", event, param); 343 break; 344 } 345 else { 346 /* No event. Wait some more */ 347 sc->sc_timeout_ch = timeout(spictimeout, sc, spic_pollrate); 348 return; 349 } 350 351 if (sc->sc_count) { 352 if (sc->sc_sleeping) { 353 sc->sc_sleeping = 0; 354 wakeup((caddr_t) sc); 355 } 356 selwakeup(&sc->sc_rsel); 357 } 358 359 spic_call2(sc, 0x81, 0xff); /* Clear event */ 360 361 sc->sc_timeout_ch = timeout(spictimeout, sc, spic_pollrate); 362} 363 364static int 365spicopen(dev_t dev, int flag, int fmt, struct thread *td) 366{ 367 struct spic_softc *sc; 368 369 sc = devclass_get_softc(spic_devclass, 0); 370 371 if (sc->sc_opened) 372 return EBUSY; 373 374 sc->sc_opened++; 375 sc->sc_count=0; 376 377 /* Start the polling */ 378 timeout(spictimeout, sc, spic_pollrate); 379 return 0; 380} 381 382static int 383spicclose(dev_t dev, int flag, int fmt, struct thread *td) 384{ 385 struct spic_softc *sc; 386 387 sc = devclass_get_softc(spic_devclass, 0); 388 389 /* Stop polling */ 390 untimeout(spictimeout, sc, sc->sc_timeout_ch); 391 sc->sc_opened = 0; 392 return 0; 393} 394 395static int 396spicread(dev_t dev, struct uio *uio, int flag) 397{ 398 struct spic_softc *sc; 399 int l, s, error; 400 u_char buf[SCBUFLEN]; 401 402 sc = devclass_get_softc(spic_devclass, 0); 403 404 if (uio->uio_resid <= 0) /* What kind of a read is this?! */ 405 return 0; 406 407 s = spltty(); 408 while (!(sc->sc_count)) { 409 sc->sc_sleeping=1; 410 error = tsleep((caddr_t) sc, PZERO | PCATCH, "jogrea", 0); 411 sc->sc_sleeping=0; 412 if (error) { 413 splx(s); 414 return error; 415 } 416 } 417 splx(s); 418 419 s = spltty(); 420 l = min(uio->uio_resid, sc->sc_count); 421 bcopy(sc->sc_buf, buf, l); 422 sc->sc_count -= l; 423 bcopy(sc->sc_buf + l, sc->sc_buf, l); 424 splx(s); 425 return uiomove(buf, l, uio); 426 427} 428 429static int 430spicioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td) 431{ 432 struct spic_softc *sc; 433 434 sc = devclass_get_softc(spic_devclass, 0); 435 436 return EIO; 437} 438 439static int 440spicpoll(dev_t dev, int events, struct thread *td) 441{ 442 struct spic_softc *sc; 443 int revents = 0, s; 444 445 sc = devclass_get_softc(spic_devclass, 0); 446 s = spltty(); 447 if (events & (POLLIN | POLLRDNORM)) { 448 if (sc->sc_count) 449 revents |= events & (POLLIN | POLLRDNORM); 450 else 451 selrecord(td, &sc->sc_rsel); /* Who shall we wake? */ 452 } 453 splx(s); 454 455 return revents; 456} 457 458 459static device_method_t spic_methods[] = { 460 DEVMETHOD(device_probe, spic_probe), 461 DEVMETHOD(device_attach, spic_attach), 462 463 { 0, 0 } 464}; 465 466static driver_t spic_driver = { 467 "spic", 468 spic_methods, 469 sizeof(struct spic_softc), 470}; 471 472DRIVER_MODULE(spic, isa, spic_driver, spic_devclass, 0, 0); 473 474