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