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