The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/i386/isa/spic.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    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: releng/5.0/sys/i386/isa/spic.c 93071 2002-03-24 03:07:07Z will $
   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 
   74 static int spic_pollrate;
   75 
   76 SYSCTL_INT(_machdep, OID_AUTO, spic_pollrate, CTLFLAG_RW, &spic_pollrate, 0, "")
   77 ;
   78 
   79 devclass_t spic_devclass;
   80 
   81 static d_open_t         spicopen;
   82 static d_close_t        spicclose;
   83 static d_read_t         spicread;
   84 static d_ioctl_t        spicioctl;
   85 static d_poll_t         spicpoll;
   86 
   87 static 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 
  105 struct 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         int sc_model;
  119 };
  120 
  121 static void
  122 write_port1(struct spic_softc *sc, u_char val)
  123 {
  124         DELAY(10);
  125         outb(sc->sc_port_addr, val);
  126 }
  127 
  128 static void
  129 write_port2(struct spic_softc *sc, u_char val)
  130 {
  131         DELAY(10);
  132         outb(sc->sc_port_addr + 4, val);
  133 }
  134 
  135 static u_char
  136 read_port1(struct spic_softc *sc)
  137 {
  138         DELAY(10);
  139         return inb(sc->sc_port_addr);
  140 }
  141 
  142 static u_char
  143 read_port2(struct spic_softc *sc)
  144 {
  145         DELAY(10);
  146         return inb(sc->sc_port_addr + 4);
  147 }
  148 
  149 static u_char
  150 read_port_cst(struct spic_softc *sc)
  151 {
  152         DELAY(10);
  153         return inb(SPIC_CST_IOPORT);
  154 }
  155 
  156 static void
  157 busy_wait(struct spic_softc *sc)
  158 {
  159         int i=0;
  160 
  161         while(read_port2(sc) & 2) {
  162                 DELAY(10);
  163                 if (i++>10000) {
  164                         printf("spic busy wait abort\n");
  165                         return;
  166                 }
  167         }
  168 }
  169 
  170 static void
  171 busy_wait_cst(struct spic_softc *sc, int mask)
  172 {
  173         int i=0;
  174 
  175         while(read_port_cst(sc) & mask) {
  176                 DELAY(10);
  177                 if (i++>10000) {
  178                         printf("spic busy wait abort\n");
  179                         return;
  180                 }
  181         }
  182 }
  183 
  184 static u_char
  185 spic_call1(struct spic_softc *sc, u_char dev) {
  186         busy_wait(sc);
  187         write_port2(sc, dev);
  188         read_port2(sc);
  189         return read_port1(sc);
  190 }
  191 
  192 static u_char
  193 spic_call2(struct spic_softc *sc, u_char dev, u_char fn)
  194 {
  195         busy_wait(sc);
  196         write_port2(sc, dev);
  197         busy_wait(sc);
  198         write_port1(sc, fn);
  199         return read_port1(sc);
  200 }
  201 
  202 static void
  203 spic_ecrset(struct spic_softc *sc, u_int16_t addr, u_int16_t value)
  204 {
  205         busy_wait_cst(sc, 3);
  206         outb(SPIC_CST_IOPORT, 0x81);
  207         busy_wait_cst(sc, 2);
  208         outb(SPIC_DATA_IOPORT, addr);
  209         busy_wait_cst(sc, 2);
  210         outb(SPIC_DATA_IOPORT, value);
  211         busy_wait_cst(sc, 2);
  212 }
  213 
  214 static void
  215 spic_type2_srs(struct spic_softc *sc)
  216 {
  217         spic_ecrset(sc, SPIC_SHIB, (sc->sc_port_addr & 0xFF00) >> 8);
  218         spic_ecrset(sc, SPIC_SLOB,  sc->sc_port_addr & 0x00FF);
  219         spic_ecrset(sc, SPIC_SIRQ,  0x00); /* using polling mode (IRQ=0)*/
  220         DELAY(10);
  221 }
  222 
  223 static int
  224 spic_probe(device_t dev)
  225 {
  226         struct spic_softc *sc;
  227         u_char t, spic_irq;
  228 
  229         sc = device_get_softc(dev);
  230 
  231         /*
  232          * We can only have 1 of these. Attempting to probe for a unit 1
  233          * will destroy the work we did for unit 0
  234          */
  235         if (device_get_unit(dev))
  236                 return ENXIO;
  237 
  238         bzero(sc, sizeof(struct spic_softc));
  239 
  240         if (!(sc->sc_port_res = bus_alloc_resource(dev, SYS_RES_IOPORT,
  241                 &sc->sc_port_rid, 0, ~0, 5, RF_ACTIVE))) {
  242                 device_printf(dev,"Couldn't map I/O\n");
  243                 return ENXIO;
  244         }
  245         sc->sc_port_addr = (u_short)rman_get_start(sc->sc_port_res);
  246 
  247 #ifdef notyet
  248         if (!(sc->sc_intr_res = bus_alloc_resource(dev, SYS_RES_IRQ,
  249                 &sc->sc_intr_rid, 0, ~0, 1, RF_ACTIVE))) {
  250                 device_printf(dev,"Couldn't map IRQ\n");
  251                 bus_release_resource(dev, SYS_RES_IOPORT,
  252                         sc->sc_port_rid, sc->sc_port_res);
  253                 return ENXIO;
  254         }
  255         sc->sc_intr = (u_short)rman_get_start(sc->sc_intr_res);
  256 
  257         switch (sc->sc_intr) {
  258                 case 0: spic_irq = 3; break;
  259                 case 5: spic_irq = 0; break;
  260                 case 0xa: spic_irq = 1; break;
  261                 case 0xb: spic_irq = 2; break;
  262                 default: device_printf(dev,"Invalid IRQ\n");
  263                         bus_release_resource(dev, SYS_RES_IOPORT,
  264                                 sc->sc_port_rid, sc->sc_port_res);
  265                         bus_release_resource(dev, SYS_RES_IRQ,
  266                                 sc->sc_intr_rid, sc->sc_intr_res);
  267                         return ENXIO;
  268         }
  269 #else
  270         spic_irq = 3;
  271 #endif
  272 
  273 #if 0
  274         if (sc->sc_port_addr != 0x10A0) {
  275                 bus_release_resource(dev, SYS_RES_IOPORT,
  276                         sc->sc_port_rid, sc->sc_port_res);
  277                 bus_release_resource(dev, SYS_RES_IRQ,
  278                         sc->sc_intr_rid, sc->sc_intr_res);
  279                 return ENXIO;
  280         }
  281 #endif
  282 
  283         /* PIIX4 chipset at least? */
  284         if (pci_cfgregread(PIIX4_BUS, PIIX4_SLOT, PIIX4_FUNC, 0, 4) ==
  285                 PIIX4_DEVID) {
  286                 sc->sc_model = SPIC_DEVICE_MODEL_TYPE1;
  287         } else {
  288                 /* For newer VAIOs (R505, SRX7, ...) */
  289                 sc->sc_model = SPIC_DEVICE_MODEL_TYPE2;
  290         }
  291 
  292         /*
  293          * This is an ugly hack. It is necessary until ACPI works correctly.
  294          *
  295          * The SPIC consists of 2 registers. They are mapped onto I/O by the
  296          * PIIX4's General Device 10 function. There is also an interrupt
  297          * control port at a somewhat magic location, but this first pass is
  298          * polled.
  299          *
  300          * So the first thing we need to do is map the G10 space in.
  301          *
  302          */
  303 
  304         /* Enable ACPI mode to get Fn key events */
  305         /* XXX This may slow down your VAIO if ACPI is not supported in the kernel.
  306         outb(0xb2, 0xf0);
  307         */
  308 
  309         device_printf(dev,"device model type = %d\n", sc->sc_model);
  310         
  311         if(sc->sc_model == SPIC_DEVICE_MODEL_TYPE1) {
  312                 pci_cfgregwrite(PIIX4_BUS, PIIX4_SLOT, PIIX4_FUNC, G10A,
  313                                 sc->sc_port_addr, 2);
  314                 t = pci_cfgregread(PIIX4_BUS, PIIX4_SLOT, PIIX4_FUNC, G10L, 1);
  315                 t &= 0xf0;
  316                 t |= 4;
  317                 pci_cfgregwrite(PIIX4_BUS, PIIX4_SLOT, PIIX4_FUNC, G10L, t, 1);
  318                 outw(SPIC_IRQ_PORT, (inw(SPIC_IRQ_PORT) & ~(0x3 << SPIC_IRQ_SHIFT)) | (spic_irq << SPIC_IRQ_SHIFT));
  319                 t = pci_cfgregread(PIIX4_BUS, PIIX4_SLOT, PIIX4_FUNC, G10L, 1);
  320                 t &= 0x1f;
  321                 t |= 0xc0;
  322                 pci_cfgregwrite(PIIX4_BUS, PIIX4_SLOT, PIIX4_FUNC, G10L, t, 1);
  323         } else {
  324                 spic_type2_srs(sc);
  325         }
  326 
  327         /*
  328          * XXX: Should try and see if there's anything actually there.
  329          */
  330 
  331         device_set_desc(dev, "Sony Programmable I/O Controller");
  332 
  333         return 0;
  334 }
  335 
  336 static int
  337 spic_attach(device_t dev)
  338 {
  339         struct spic_softc *sc;
  340 
  341         sc = device_get_softc(dev);
  342 
  343         sc->sc_dev = dev;
  344         
  345         spic_pollrate = (hz/50); /* Every 50th of a second */
  346 
  347         spic_call1(sc, 0x82);
  348         spic_call2(sc, 0x81, 0xff);
  349         spic_call1(sc, 0x92);
  350 
  351         /* There can be only one */
  352         make_dev(&spic_cdevsw, 0, 0, 0, 0600, "jogdial");
  353 
  354         return 0;
  355 }
  356 
  357 static void
  358 spictimeout(void *arg)
  359 {
  360         struct spic_softc *sc = arg;
  361         u_char b, event, param;
  362         int j;
  363 
  364         if (!sc->sc_opened) {
  365                 device_printf(sc->sc_dev, "timeout called while closed!\n");
  366                 return;
  367         }
  368 
  369         event = read_port2(sc);
  370         param = read_port1(sc);
  371 
  372         if ((event != 4) && (!(event & 0x1)))
  373                 switch(event) {
  374                         case 0x10: /* jog wheel event (type1) */
  375                                 if (sc->sc_model == SPIC_DEVICE_MODEL_TYPE1) {
  376                                         b = !!(param & 0x40);
  377                                         if (b != sc->sc_buttonlast) {
  378                                                 sc->sc_buttonlast = b;
  379                                                 sc->sc_buf[sc->sc_count++] =
  380                                                         b?'d':'u';
  381                                         }
  382                                         j = (param & 0xf) | ((param & 0x10)? ~0xf:0);
  383                                         if (j<0)
  384                                                 while(j++!=0) {
  385                                                         sc->sc_buf[sc->sc_count++] =
  386                                                                 'l';
  387                                                 }
  388                                         else if (j>0)
  389                                                 while(j--!=0) {
  390                                                         sc->sc_buf[sc->sc_count++] =
  391                                                                 'r';
  392                                                 }
  393                                 }
  394                                 break;
  395                         case 0x08: /* jog wheel event (type2) */
  396                         case 0x00: 
  397                                 /* SPIC_DEVICE_MODEL_TYPE2 returns jog wheel event=0x00 */
  398                                 if (sc->sc_model == SPIC_DEVICE_MODEL_TYPE2) {
  399                                         b = !!(param & 0x40);
  400                                         if (b != sc->sc_buttonlast) {
  401                                                 sc->sc_buttonlast = b;
  402                                                 sc->sc_buf[sc->sc_count++] =
  403                                                         b?'d':'u';
  404                                         }
  405                                         j = (param & 0xf) | ((param & 0x10)? ~0xf:0);
  406                                         if (j<0)
  407                                                 while(j++!=0) {
  408                                                         sc->sc_buf[sc->sc_count++] =
  409                                                                 'l';
  410                                                 }
  411                                         else if (j>0)
  412                                                 while(j--!=0) {
  413                                                         sc->sc_buf[sc->sc_count++] =
  414                                                                 'r';
  415                                                 }
  416                                 }
  417                                 break;
  418                         case 0x60: /* Capture button */
  419                                 printf("Capture button event: %x\n",param);
  420                                 break;
  421                         case 0x30: /* Lid switch */
  422                                 printf("Lid switch event: %x\n",param);
  423                                 break;
  424                         case 0x0c: /* We must ignore these type of event for C1VP... */
  425                         case 0x70: /* Closing/Opening the lid on C1VP */
  426                                 break;
  427                         default:
  428                                 printf("Unknown event: event %02x param %02x\n", event, param);
  429                                 break;
  430                 }
  431         else {
  432                 /* No event. Wait some more */
  433                 sc->sc_timeout_ch = timeout(spictimeout, sc, spic_pollrate);
  434                 return;
  435         }
  436 
  437         if (sc->sc_count) {
  438                 if (sc->sc_sleeping) {
  439                         sc->sc_sleeping = 0;
  440                         wakeup((caddr_t) sc);
  441                 }
  442                 selwakeup(&sc->sc_rsel);
  443         }
  444         spic_call2(sc, 0x81, 0xff); /* Clear event */
  445 
  446         sc->sc_timeout_ch = timeout(spictimeout, sc, spic_pollrate);
  447 }
  448 
  449 static int
  450 spicopen(dev_t dev, int flag, int fmt, struct thread *td)
  451 {
  452         struct spic_softc *sc;
  453 
  454         sc = devclass_get_softc(spic_devclass, 0);
  455 
  456         if (sc->sc_opened)
  457                 return EBUSY;
  458 
  459         sc->sc_opened++;
  460         sc->sc_count=0;
  461 
  462         /* Start the polling */
  463         timeout(spictimeout, sc, spic_pollrate);
  464         return 0;
  465 }
  466 
  467 static int
  468 spicclose(dev_t dev, int flag, int fmt, struct thread *td)
  469 {
  470         struct spic_softc *sc;
  471 
  472         sc = devclass_get_softc(spic_devclass, 0);
  473 
  474         /* Stop polling */
  475         untimeout(spictimeout, sc, sc->sc_timeout_ch);
  476         sc->sc_opened = 0;
  477         return 0;
  478 }
  479 
  480 static int
  481 spicread(dev_t dev, struct uio *uio, int flag)
  482 {
  483         struct spic_softc *sc;
  484         int l, s, error;
  485         u_char buf[SCBUFLEN];
  486 
  487         sc = devclass_get_softc(spic_devclass, 0);
  488 
  489         if (uio->uio_resid <= 0) /* What kind of a read is this?! */
  490                 return 0;
  491 
  492         s = spltty();
  493         while (!(sc->sc_count)) {
  494                 sc->sc_sleeping=1;
  495                 error = tsleep((caddr_t) sc, PZERO | PCATCH, "jogrea", 0);
  496                 sc->sc_sleeping=0;
  497                 if (error) {
  498                         splx(s);
  499                         return error;
  500                 }
  501         }
  502         splx(s);
  503 
  504         s = spltty();
  505         l = min(uio->uio_resid, sc->sc_count);
  506         bcopy(sc->sc_buf, buf, l);
  507         sc->sc_count -= l;
  508         bcopy(sc->sc_buf + l, sc->sc_buf, l);
  509         splx(s);
  510         return uiomove(buf, l, uio);
  511 
  512 }
  513 
  514 static int
  515 spicioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
  516 {
  517         struct spic_softc *sc;
  518 
  519         sc = devclass_get_softc(spic_devclass, 0);
  520 
  521         return EIO;
  522 }
  523 
  524 static int
  525 spicpoll(dev_t dev, int events, struct thread *td)
  526 {
  527         struct spic_softc *sc;
  528         int revents = 0, s;
  529 
  530         sc = devclass_get_softc(spic_devclass, 0);
  531         s = spltty();
  532         if (events & (POLLIN | POLLRDNORM)) {
  533                 if (sc->sc_count)
  534                         revents |= events & (POLLIN | POLLRDNORM);
  535                 else
  536                         selrecord(td, &sc->sc_rsel); /* Who shall we wake? */
  537         }
  538         splx(s);
  539 
  540         return revents;
  541 }
  542 
  543 
  544 static device_method_t spic_methods[] = {
  545         DEVMETHOD(device_probe,         spic_probe),
  546         DEVMETHOD(device_attach,        spic_attach),
  547 
  548         { 0, 0 }
  549 };
  550 
  551 static driver_t spic_driver = {
  552         "spic",
  553         spic_methods,
  554         sizeof(struct spic_softc),
  555 };
  556 
  557 DRIVER_MODULE(spic, isa, spic_driver, spic_devclass, 0, 0);
  558 

Cache object: e1639ac3dc7ec955cb0c1cc715f12f43


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.