Merge branch 'master' into kq_devices
[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.17 2008/08/02 01:14:42 dillon 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 <sys/rman.h>
59 #include <sys/poll.h>
60 #include <sys/event.h>
61 #include <sys/tty.h>
62 #include <sys/conf.h>
63 #include <sys/fcntl.h>
64 #include <sys/dkstat.h>
65 #include <sys/malloc.h>
66 #include <sys/sysctl.h>
67 #include <sys/uio.h>
68 #include <sys/proc.h>
69 #include <sys/thread2.h>
70
71 #include <bus/isa/isavar.h>
72
73 #include <bus/pci/pci_cfgreg.h>
74 #include <machine/clock.h>
75
76 #include "spicreg.h"
77
78 static int spic_pollrate;
79
80 SYSCTL_INT(_machdep, OID_AUTO, spic_pollrate, CTLFLAG_RW, &spic_pollrate, 0, "")
81 ;
82
83 devclass_t spic_devclass;
84
85 static d_open_t         spicopen;
86 static d_close_t        spicclose;
87 static d_read_t         spicread;
88 static d_ioctl_t        spicioctl;
89 static d_poll_t         spicpoll;
90 static d_kqfilter_t     spickqfilter;
91
92 static void spicfilt_detach(struct knote *);
93 static int spicfilt(struct knote *, long);
94
95 static struct dev_ops spic_ops = {
96         { "spic", CDEV_MAJOR, D_KQFILTER },
97         .d_open =       spicopen,
98         .d_close =      spicclose,
99         .d_read =       spicread,
100         .d_ioctl =      spicioctl,
101         .d_poll =       spicpoll,
102         .d_kqfilter =   spickqfilter
103 };
104
105 #define SCBUFLEN 128
106
107 struct spic_softc {
108         u_short sc_port_addr;
109         u_char sc_intr;
110         struct resource *sc_port_res,*sc_intr_res;
111         int     sc_port_rid,sc_intr_rid;
112         int sc_opened;
113         int sc_sleeping;
114         int sc_buttonlast;
115         struct callout  sc_timeout_ch;
116         device_t sc_dev;
117         struct selinfo sc_rsel;
118         u_char sc_buf[SCBUFLEN];
119         int sc_count;
120         int sc_model;
121 };
122
123 static void
124 write_port1(struct spic_softc *sc, u_char val)
125 {
126         DELAY(10);
127         outb(sc->sc_port_addr, val);
128 }
129
130 static void
131 write_port2(struct spic_softc *sc, u_char val)
132 {
133         DELAY(10);
134         outb(sc->sc_port_addr + 4, val);
135 }
136
137 static u_char
138 read_port1(struct spic_softc *sc)
139 {
140         DELAY(10);
141         return inb(sc->sc_port_addr);
142 }
143
144 static u_char
145 read_port2(struct spic_softc *sc)
146 {
147         DELAY(10);
148         return inb(sc->sc_port_addr + 4);
149 }
150
151 static u_char
152 read_port_cst(struct spic_softc *sc)
153 {
154         DELAY(10);
155         return inb(SPIC_CST_IOPORT);
156 }
157
158 static void
159 busy_wait(struct spic_softc *sc)
160 {
161         int i=0;
162
163         while(read_port2(sc) & 2) {
164                 DELAY(10);
165                 if (i++>10000) {
166                         kprintf("spic busy wait abort\n");
167                         return;
168                 }
169         }
170 }
171
172 static void
173 busy_wait_cst(struct spic_softc *sc, int mask)
174 {
175         int i=0;
176
177         while(read_port_cst(sc) & mask) {
178                 DELAY(10);
179                 if (i++>10000) {
180                         kprintf("spic busy wait abort\n");
181                         return;
182                 }
183         }
184 }
185
186 static u_char
187 spic_call1(struct spic_softc *sc, u_char dev) {
188         busy_wait(sc);
189         write_port2(sc, dev);
190         read_port2(sc);
191         return read_port1(sc);
192 }
193
194 static u_char
195 spic_call2(struct spic_softc *sc, u_char dev, u_char fn)
196 {
197         busy_wait(sc);
198         write_port2(sc, dev);
199         busy_wait(sc);
200         write_port1(sc, fn);
201         return read_port1(sc);
202 }
203
204 static void
205 spic_ecrset(struct spic_softc *sc, u_int16_t addr, u_int16_t value)
206 {
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);
214 }
215
216 static void
217 spic_type2_srs(struct spic_softc *sc)
218 {
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)*/
222         DELAY(10);
223 }
224
225 static int
226 spic_probe(device_t dev)
227 {
228         struct spic_softc *sc;
229         u_char t, spic_irq;
230
231         sc = device_get_softc(dev);
232
233         /*
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
236          */
237         if (device_get_unit(dev))
238                 return ENXIO;
239
240
241         bzero(sc, sizeof(struct spic_softc));
242
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");
246                 return ENXIO;
247         }
248         sc->sc_port_addr = (u_short)rman_get_start(sc->sc_port_res);
249
250 #ifdef notyet
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);
256                 return ENXIO;
257         }
258         sc->sc_intr = (u_short)rman_get_start(sc->sc_intr_res);
259
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);
270                         return ENXIO;
271         }
272 #else
273         spic_irq = 3;
274 #endif
275
276 #if 0
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);
282                 return ENXIO;
283         }
284 #endif
285
286         /* PIIX4 chipset at least? */
287         if (pci_cfgregread(PIIX4_BUS, PIIX4_SLOT, PIIX4_FUNC, 0, 4) ==
288                 PIIX4_DEVID) {
289                 sc->sc_model = SPIC_DEVICE_MODEL_TYPE1;
290         } else {
291                 /* For newer VAIOs (R505, SRX7, ...) */
292                 sc->sc_model = SPIC_DEVICE_MODEL_TYPE2;
293         }
294
295         /*
296          * This is an ugly hack. It is necessary until ACPI works correctly.
297          *
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
301          * polled.
302          *
303          * So the first thing we need to do is map the G10 space in.
304          *
305          */
306
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.
309         outb(0xb2, 0xf0);
310          */
311
312         device_printf(dev,"device model type = %d\n", sc->sc_model);
313         
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);
318                 t &= 0xf0;
319                 t |= 4;
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);
323                 t &= 0x1f;
324                 t |= 0xc0;
325                 pci_cfgregwrite(PIIX4_BUS, PIIX4_SLOT, PIIX4_FUNC, G10L, t, 1);
326         } else {
327                 spic_type2_srs(sc);
328         }
329
330         /*
331          * XXX: Should try and see if there's anything actually there.
332          */
333
334         device_set_desc(dev, "Sony Programmable I/O Controller");
335
336         return 0;
337 }
338
339 static int
340 spic_attach(device_t dev)
341 {
342         struct spic_softc *sc;
343
344         sc = device_get_softc(dev);
345
346         sc->sc_dev = dev;
347         callout_init(&sc->sc_timeout_ch);
348         
349         spic_pollrate = (hz/50); /* Every 50th of a second */
350
351         spic_call1(sc, 0x82);
352         spic_call2(sc, 0x81, 0xff);
353         spic_call1(sc, 0x92);
354
355         /* There can be only one */
356         make_dev(&spic_ops, device_get_unit(dev), 0, 0, 0600, "jogdial");
357
358         return 0;
359 }
360
361 static void
362 spictimeout(void *arg)
363 {
364         struct spic_softc *sc = arg;
365         u_char b, event, param;
366         int j;
367
368         if (!sc->sc_opened) {
369                 device_printf(sc->sc_dev, "timeout called while closed!\n");
370                 return;
371         }
372
373         event = read_port2(sc);
374         param = read_port1(sc);
375
376         if ((event != 4) && (!(event & 0x1)))
377                 switch(event) {
378                         case 0x10: /* jog wheel event (type1) */
379                                 if (sc->sc_model == SPIC_DEVICE_MODEL_TYPE1) {
380                                         b = !!(param & 0x40);
381                                         if (b != sc->sc_buttonlast) {
382                                                 sc->sc_buttonlast = b;
383                                                 sc->sc_buf[sc->sc_count++] =
384                                                         b?'d':'u';
385                                         }
386                                         j = (param & 0xf) | ((param & 0x10)? ~0xf:0);
387                                         if (j<0)
388                                                 while(j++!=0) {
389                                                         sc->sc_buf[sc->sc_count++] =
390                                                                 'l';
391                                                 }
392                                         else if (j>0)
393                                                 while(j--!=0) {
394                                                         sc->sc_buf[sc->sc_count++] =
395                                                                 'r';
396                                                 }
397                                 }
398                                 break;
399                         case 0x08: /* jog wheel event (type2) */
400                         case 0x00: 
401                                 /* SPIC_DEVICE_MODEL_TYPE2 returns jog wheel event=0x00 */
402                                 if (sc->sc_model == SPIC_DEVICE_MODEL_TYPE2) {
403                                         b = !!(param & 0x40);
404                                         if (b != sc->sc_buttonlast) {
405                                                 sc->sc_buttonlast = b;
406                                                 sc->sc_buf[sc->sc_count++] =
407                                                         b?'d':'u';
408                                         }
409                                         j = (param & 0xf) | ((param & 0x10)? ~0xf:0);
410                                         if (j<0)
411                                                 while(j++!=0) {
412                                                         sc->sc_buf[sc->sc_count++] =
413                                                                 'l';
414                                                 }
415                                         else if (j>0)
416                                                 while(j--!=0) {
417                                                         sc->sc_buf[sc->sc_count++] =
418                                                                 'r';
419                                                 }
420                                 }
421                                 break;
422                         case 0x60: /* Capture button */
423                                 kprintf("Capture button event: %x\n",param);
424                                 break;
425                         case 0x30: /* Lid switch */
426                                 kprintf("Lid switch event: %x\n",param);
427                                 break;
428                         default:
429                                 kprintf("Unknown event: event %02x param %02x\n", event, param);
430                                 break;
431                 }
432         else {
433                 /* No event. Wait some more */
434                 callout_reset(&sc->sc_timeout_ch, spic_pollrate,
435                                 spictimeout, sc);
436                 return;
437         }
438
439         if (sc->sc_count) {
440                 if (sc->sc_sleeping) {
441                         sc->sc_sleeping = 0;
442                         wakeup((caddr_t) sc);
443                 }
444                 selwakeup(&sc->sc_rsel);
445         }
446         spic_call2(sc, 0x81, 0xff); /* Clear event */
447
448         callout_reset(&sc->sc_timeout_ch, spic_pollrate, spictimeout, sc);
449 }
450
451 static int
452 spicopen(struct dev_open_args *ap)
453 {
454         struct spic_softc *sc;
455
456         sc = devclass_get_softc(spic_devclass, 0);
457
458         if (sc->sc_opened)
459                 return EBUSY;
460
461         sc->sc_opened++;
462         sc->sc_count=0;
463
464         /* Start the polling */
465         callout_reset(&sc->sc_timeout_ch, spic_pollrate, spictimeout, sc);
466         return 0;
467 }
468
469 static int
470 spicclose(struct dev_close_args *ap)
471 {
472         struct spic_softc *sc;
473
474         sc = devclass_get_softc(spic_devclass, 0);
475
476         /* Stop polling */
477         callout_stop(&sc->sc_timeout_ch);
478         sc->sc_opened = 0;
479         return 0;
480 }
481
482 static int
483 spicread(struct dev_read_args *ap)
484 {
485         struct uio *uio = ap->a_uio;
486         struct spic_softc *sc;
487         int l, error;
488         u_char buf[SCBUFLEN];
489
490         sc = devclass_get_softc(spic_devclass, 0);
491
492         if (uio->uio_resid == 0) /* What kind of a read is this?! */
493                 return 0;
494
495         crit_enter();
496         while (!(sc->sc_count)) {
497                 sc->sc_sleeping=1;
498                 error = tsleep((caddr_t) sc, PCATCH, "jogrea", 0);
499                 sc->sc_sleeping=0;
500                 if (error) {
501                         crit_exit();
502                         return error;
503                 }
504         }
505         crit_exit();
506
507         crit_enter();
508         l = (int)szmin(uio->uio_resid, sc->sc_count);
509         bcopy(sc->sc_buf, buf, l);
510         sc->sc_count -= l;
511         bcopy(sc->sc_buf + l, sc->sc_buf, l);
512         crit_exit();
513         return uiomove(buf, (size_t)l, uio);
514
515 }
516
517 static int
518 spicioctl(struct dev_ioctl_args *ap)
519 {
520         struct spic_softc *sc;
521
522         sc = devclass_get_softc(spic_devclass, 0);
523
524         return EIO;
525 }
526
527 static int
528 spicpoll(struct dev_poll_args *ap)
529 {
530         struct spic_softc *sc;
531         int revents = 0;
532
533         sc = devclass_get_softc(spic_devclass, 0);
534         crit_enter();
535         if (ap->a_events & (POLLIN | POLLRDNORM)) {
536                 if (sc->sc_count) {
537                         revents |= ap->a_events & (POLLIN | POLLRDNORM);
538                 } else {
539                         selrecord(curthread, &sc->sc_rsel);
540                 }
541         }
542         crit_exit();
543         ap->a_events = revents;
544         return(0);
545 }
546
547 static struct filterops spicfiltops =
548         { 1, NULL, spicfilt_detach, spicfilt };
549
550 static int
551 spickqfilter(struct dev_kqfilter_args *ap)
552 {
553         struct knote *kn = ap->a_kn;
554         struct klist *klist;
555         struct spic_softc *sc;
556
557         ap->a_result = 0;
558
559         switch (kn->kn_filter) {
560         case EVFILT_READ:
561                 sc = devclass_get_softc(spic_devclass, 0);
562                 kn->kn_fop = &spicfiltops;
563                 kn->kn_hook = (caddr_t)sc;
564                 break;
565         default:
566                 ap->a_result = 1;
567                 return (0);
568         }
569
570         crit_enter();
571         klist = &sc->sc_rsel.si_note;
572         SLIST_INSERT_HEAD(klist, kn, kn_selnext);
573         crit_exit();
574
575         return (0);
576 }
577
578 static void
579 spicfilt_detach(struct knote *kn)
580 {
581         struct spic_softc *sc = (struct spic_softc *)kn->kn_hook;
582         struct klist *klist;
583
584         crit_enter();
585         klist = &sc->sc_rsel.si_note;
586         SLIST_REMOVE(klist, kn, knote, kn_selnext);
587         crit_exit();
588 }
589
590 static int
591 spicfilt(struct knote *kn, long hint)
592 {
593         struct spic_softc *sc = (struct spic_softc *)kn->kn_hook;
594         int ready = 0;
595
596         crit_enter();
597         if (sc->sc_count)
598                 ready = 1;
599         crit_exit();
600
601         return (ready);
602 }
603
604 static device_method_t spic_methods[] = {
605         DEVMETHOD(device_probe,         spic_probe),
606         DEVMETHOD(device_attach,        spic_attach),
607
608         { 0, 0 }
609 };
610
611 static driver_t spic_driver = {
612         "spic",
613         spic_methods,
614         sizeof(struct spic_softc),
615 };
616
617 DRIVER_MODULE(spic, isa, spic_driver, spic_devclass, 0, 0);
618