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