336ebe74d47e2eae2c4ed30fb2c72b6e17f6feae
[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/event.h>
60 #include <sys/tty.h>
61 #include <sys/conf.h>
62 #include <sys/fcntl.h>
63 #include <sys/dkstat.h>
64 #include <sys/malloc.h>
65 #include <sys/sysctl.h>
66 #include <sys/uio.h>
67 #include <sys/proc.h>
68 #include <sys/thread2.h>
69
70 #include <bus/isa/isavar.h>
71
72 #include <bus/pci/pci_cfgreg.h>
73 #include <machine/clock.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_kqfilter_t     spickqfilter;
89
90 static void spicfilt_detach(struct knote *);
91 static int spicfilt(struct knote *, long);
92
93 static struct dev_ops spic_ops = {
94         { "spic", CDEV_MAJOR, D_KQFILTER },
95         .d_open =       spicopen,
96         .d_close =      spicclose,
97         .d_read =       spicread,
98         .d_ioctl =      spicioctl,
99         .d_kqfilter =   spickqfilter
100 };
101
102 #define SCBUFLEN 128
103
104 struct spic_softc {
105         u_short sc_port_addr;
106         u_char sc_intr;
107         struct resource *sc_port_res,*sc_intr_res;
108         int     sc_port_rid,sc_intr_rid;
109         int sc_opened;
110         int sc_sleeping;
111         int sc_buttonlast;
112         struct callout  sc_timeout_ch;
113         device_t sc_dev;
114         struct kqinfo sc_rkq;
115         u_char sc_buf[SCBUFLEN];
116         int sc_count;
117         int sc_model;
118 };
119
120 static void
121 write_port1(struct spic_softc *sc, u_char val)
122 {
123         DELAY(10);
124         outb(sc->sc_port_addr, val);
125 }
126
127 static void
128 write_port2(struct spic_softc *sc, u_char val)
129 {
130         DELAY(10);
131         outb(sc->sc_port_addr + 4, val);
132 }
133
134 static u_char
135 read_port1(struct spic_softc *sc)
136 {
137         DELAY(10);
138         return inb(sc->sc_port_addr);
139 }
140
141 static u_char
142 read_port2(struct spic_softc *sc)
143 {
144         DELAY(10);
145         return inb(sc->sc_port_addr + 4);
146 }
147
148 static u_char
149 read_port_cst(struct spic_softc *sc)
150 {
151         DELAY(10);
152         return inb(SPIC_CST_IOPORT);
153 }
154
155 static void
156 busy_wait(struct spic_softc *sc)
157 {
158         int i=0;
159
160         while(read_port2(sc) & 2) {
161                 DELAY(10);
162                 if (i++>10000) {
163                         kprintf("spic busy wait abort\n");
164                         return;
165                 }
166         }
167 }
168
169 static void
170 busy_wait_cst(struct spic_softc *sc, int mask)
171 {
172         int i=0;
173
174         while(read_port_cst(sc) & mask) {
175                 DELAY(10);
176                 if (i++>10000) {
177                         kprintf("spic busy wait abort\n");
178                         return;
179                 }
180         }
181 }
182
183 static u_char
184 spic_call1(struct spic_softc *sc, u_char dev) {
185         busy_wait(sc);
186         write_port2(sc, dev);
187         read_port2(sc);
188         return read_port1(sc);
189 }
190
191 static u_char
192 spic_call2(struct spic_softc *sc, u_char dev, u_char fn)
193 {
194         busy_wait(sc);
195         write_port2(sc, dev);
196         busy_wait(sc);
197         write_port1(sc, fn);
198         return read_port1(sc);
199 }
200
201 static void
202 spic_ecrset(struct spic_softc *sc, u_int16_t addr, u_int16_t value)
203 {
204         busy_wait_cst(sc, 3);
205         outb(SPIC_CST_IOPORT, 0x81);
206         busy_wait_cst(sc, 2);
207         outb(SPIC_DATA_IOPORT, addr);
208         busy_wait_cst(sc, 2);
209         outb(SPIC_DATA_IOPORT, value);
210         busy_wait_cst(sc, 2);
211 }
212
213 static void
214 spic_type2_srs(struct spic_softc *sc)
215 {
216         spic_ecrset(sc, SPIC_SHIB, (sc->sc_port_addr & 0xFF00) >> 8);
217         spic_ecrset(sc, SPIC_SLOB,  sc->sc_port_addr & 0x00FF);
218         spic_ecrset(sc, SPIC_SIRQ,  0x00); /* using polling mode (IRQ=0)*/
219         DELAY(10);
220 }
221
222 static int
223 spic_probe(device_t dev)
224 {
225         struct spic_softc *sc;
226         u_char t, spic_irq;
227
228         sc = device_get_softc(dev);
229
230         /*
231          * We can only have 1 of these. Attempting to probe for a unit 1
232          * will destroy the work we did for unit 0
233          */
234         if (device_get_unit(dev))
235                 return ENXIO;
236
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         callout_init(&sc->sc_timeout_ch);
345         
346         spic_pollrate = (hz/50); /* Every 50th of a second */
347
348         spic_call1(sc, 0x82);
349         spic_call2(sc, 0x81, 0xff);
350         spic_call1(sc, 0x92);
351
352         /* There can be only one */
353         make_dev(&spic_ops, device_get_unit(dev), 0, 0, 0600, "jogdial");
354
355         return 0;
356 }
357
358 static void
359 spictimeout(void *arg)
360 {
361         struct spic_softc *sc = arg;
362         u_char b, event, param;
363         int j;
364
365         if (!sc->sc_opened) {
366                 device_printf(sc->sc_dev, "timeout called while closed!\n");
367                 return;
368         }
369
370         event = read_port2(sc);
371         param = read_port1(sc);
372
373         if ((event != 4) && (!(event & 0x1)))
374                 switch(event) {
375                         case 0x10: /* jog wheel event (type1) */
376                                 if (sc->sc_model == SPIC_DEVICE_MODEL_TYPE1) {
377                                         b = !!(param & 0x40);
378                                         if (b != sc->sc_buttonlast) {
379                                                 sc->sc_buttonlast = b;
380                                                 sc->sc_buf[sc->sc_count++] =
381                                                         b?'d':'u';
382                                         }
383                                         j = (param & 0xf) | ((param & 0x10)? ~0xf:0);
384                                         if (j<0)
385                                                 while(j++!=0) {
386                                                         sc->sc_buf[sc->sc_count++] =
387                                                                 'l';
388                                                 }
389                                         else if (j>0)
390                                                 while(j--!=0) {
391                                                         sc->sc_buf[sc->sc_count++] =
392                                                                 'r';
393                                                 }
394                                 }
395                                 break;
396                         case 0x08: /* jog wheel event (type2) */
397                         case 0x00: 
398                                 /* SPIC_DEVICE_MODEL_TYPE2 returns jog wheel event=0x00 */
399                                 if (sc->sc_model == SPIC_DEVICE_MODEL_TYPE2) {
400                                         b = !!(param & 0x40);
401                                         if (b != sc->sc_buttonlast) {
402                                                 sc->sc_buttonlast = b;
403                                                 sc->sc_buf[sc->sc_count++] =
404                                                         b?'d':'u';
405                                         }
406                                         j = (param & 0xf) | ((param & 0x10)? ~0xf:0);
407                                         if (j<0)
408                                                 while(j++!=0) {
409                                                         sc->sc_buf[sc->sc_count++] =
410                                                                 'l';
411                                                 }
412                                         else if (j>0)
413                                                 while(j--!=0) {
414                                                         sc->sc_buf[sc->sc_count++] =
415                                                                 'r';
416                                                 }
417                                 }
418                                 break;
419                         case 0x60: /* Capture button */
420                                 kprintf("Capture button event: %x\n",param);
421                                 break;
422                         case 0x30: /* Lid switch */
423                                 kprintf("Lid switch event: %x\n",param);
424                                 break;
425                         default:
426                                 kprintf("Unknown event: event %02x param %02x\n", event, param);
427                                 break;
428                 }
429         else {
430                 /* No event. Wait some more */
431                 callout_reset(&sc->sc_timeout_ch, spic_pollrate,
432                                 spictimeout, sc);
433                 return;
434         }
435
436         if (sc->sc_count) {
437                 if (sc->sc_sleeping) {
438                         sc->sc_sleeping = 0;
439                         wakeup((caddr_t) sc);
440                 }
441                 KNOTE(&sc->sc_rkq.ki_note, 0);
442         }
443         spic_call2(sc, 0x81, 0xff); /* Clear event */
444
445         callout_reset(&sc->sc_timeout_ch, spic_pollrate, spictimeout, sc);
446 }
447
448 static int
449 spicopen(struct dev_open_args *ap)
450 {
451         struct spic_softc *sc;
452
453         sc = devclass_get_softc(spic_devclass, 0);
454
455         if (sc->sc_opened)
456                 return EBUSY;
457
458         sc->sc_opened++;
459         sc->sc_count=0;
460
461         /* Start the polling */
462         callout_reset(&sc->sc_timeout_ch, spic_pollrate, spictimeout, sc);
463         return 0;
464 }
465
466 static int
467 spicclose(struct dev_close_args *ap)
468 {
469         struct spic_softc *sc;
470
471         sc = devclass_get_softc(spic_devclass, 0);
472
473         /* Stop polling */
474         callout_stop(&sc->sc_timeout_ch);
475         sc->sc_opened = 0;
476         return 0;
477 }
478
479 static int
480 spicread(struct dev_read_args *ap)
481 {
482         struct uio *uio = ap->a_uio;
483         struct spic_softc *sc;
484         int l, 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         crit_enter();
493         while (!(sc->sc_count)) {
494                 sc->sc_sleeping=1;
495                 error = tsleep((caddr_t) sc, PCATCH, "jogrea", 0);
496                 sc->sc_sleeping=0;
497                 if (error) {
498                         crit_exit();
499                         return error;
500                 }
501         }
502         crit_exit();
503
504         crit_enter();
505         l = (int)szmin(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         crit_exit();
510         return uiomove(buf, (size_t)l, uio);
511
512 }
513
514 static int
515 spicioctl(struct dev_ioctl_args *ap)
516 {
517         struct spic_softc *sc;
518
519         sc = devclass_get_softc(spic_devclass, 0);
520
521         return EIO;
522 }
523
524 static struct filterops spicfiltops =
525         { FILTEROP_ISFD, NULL, spicfilt_detach, spicfilt };
526
527 static int
528 spickqfilter(struct dev_kqfilter_args *ap)
529 {
530         struct knote *kn = ap->a_kn;
531         struct klist *klist;
532         struct spic_softc *sc;
533
534         ap->a_result = 0;
535
536         switch (kn->kn_filter) {
537         case EVFILT_READ:
538                 sc = devclass_get_softc(spic_devclass, 0);
539                 kn->kn_fop = &spicfiltops;
540                 kn->kn_hook = (caddr_t)sc;
541                 break;
542         default:
543                 ap->a_result = EOPNOTSUPP;
544                 return (0);
545         }
546
547         klist = &sc->sc_rkq.ki_note;
548         knote_insert(klist, kn);
549
550         return (0);
551 }
552
553 static void
554 spicfilt_detach(struct knote *kn)
555 {
556         struct spic_softc *sc = (struct spic_softc *)kn->kn_hook;
557         struct klist *klist = &sc->sc_rkq.ki_note;
558
559         knote_remove(klist, kn);
560 }
561
562 static int
563 spicfilt(struct knote *kn, long hint)
564 {
565         struct spic_softc *sc = (struct spic_softc *)kn->kn_hook;
566         int ready = 0;
567
568         crit_enter();
569         if (sc->sc_count)
570                 ready = 1;
571         crit_exit();
572
573         return (ready);
574 }
575
576 static device_method_t spic_methods[] = {
577         DEVMETHOD(device_probe,         spic_probe),
578         DEVMETHOD(device_attach,        spic_attach),
579
580         { 0, 0 }
581 };
582
583 static driver_t spic_driver = {
584         "spic",
585         spic_methods,
586         sizeof(struct spic_softc),
587 };
588
589 DRIVER_MODULE(spic, isa, spic_driver, spic_devclass, 0, 0);
590