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

Cache object: 711fd499d182d882acbfcbcb6a037bda


[ 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.