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
27 /* spic -- the Sony Programmable I/O Controller
28 *
29 * This device exists on most recent Sony laptops. It is the means by which
30 * you can watch the Jog Dial and some other functions.
31 *
32 * At the moment, this driver merely tries to turn the jog dial into a
33 * device that moused can park on, with the intent of supplying a Z axis
34 * and mouse button out of the jog dial. I suspect that this device will
35 * end up having to support at least 2 different minor devices: One to be
36 * the jog wheel device for moused to camp out on and the other to perform
37 * all of the other miscelaneous functions of this device. But for now,
38 * the jog wheel is all you get.
39 *
40 * At the moment, the data sent back by the device is rather primitive.
41 * It sends a single character per event:
42 * u = up, d = down -- that's the jog button
43 * l = left, r = right -- that's the dial.
44 * "left" and "right" are rather caprecious. They actually represent
45 * ccw and cw, respectively
46 *
47 * What documentation exists is thanks to Andrew Tridge, and his page at
48 * http://samba.org/picturebook/ Special thanks also to Ian Dowse, who
49 * also provided sample code upon which this driver was based.
50 */
51
52 #include <sys/cdefs.h>
53 __FBSDID("$FreeBSD: releng/8.0/sys/i386/isa/spic.c 158651 2006-05-16 14:37:58Z phk $");
54
55 #include <sys/param.h>
56 #include <sys/systm.h>
57 #include <sys/kernel.h>
58 #include <sys/module.h>
59 #include <sys/bus.h>
60 #include <machine/bus.h>
61 #include <sys/rman.h>
62 #include <machine/resource.h>
63 #include <isa/isavar.h>
64 #include <sys/poll.h>
65 #include <machine/pci_cfgreg.h>
66 #include <sys/tty.h>
67 #include <sys/conf.h>
68 #include <sys/fcntl.h>
69 #include <sys/malloc.h>
70 #include <sys/sysctl.h>
71 #include <sys/uio.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 .d_version = D_VERSION,
90 .d_flags = D_NEEDGIANT,
91 .d_open = spicopen,
92 .d_close = spicclose,
93 .d_read = spicread,
94 .d_ioctl = spicioctl,
95 .d_poll = spicpoll,
96 .d_name = "spic",
97 };
98
99 #define SCBUFLEN 128
100
101 struct spic_softc {
102 u_int sc_port_addr;
103 u_char sc_intr;
104 struct resource *sc_port_res,*sc_intr_res;
105 int sc_port_rid,sc_intr_rid;
106 int sc_opened;
107 int sc_sleeping;
108 int sc_buttonlast;
109 struct callout_handle sc_timeout_ch;
110 device_t sc_dev;
111 struct selinfo sc_rsel;
112 u_char sc_buf[SCBUFLEN];
113 int sc_count;
114 int sc_model;
115 };
116
117 static void
118 write_port1(struct spic_softc *sc, u_char val)
119 {
120 DELAY(10);
121 outb(sc->sc_port_addr, val);
122 }
123
124 static void
125 write_port2(struct spic_softc *sc, u_char val)
126 {
127 DELAY(10);
128 outb(sc->sc_port_addr + 4, val);
129 }
130
131 static u_char
132 read_port1(struct spic_softc *sc)
133 {
134 DELAY(10);
135 return inb(sc->sc_port_addr);
136 }
137
138 static u_char
139 read_port2(struct spic_softc *sc)
140 {
141 DELAY(10);
142 return inb(sc->sc_port_addr + 4);
143 }
144
145 static u_char
146 read_port_cst(struct spic_softc *sc)
147 {
148 DELAY(10);
149 return inb(SPIC_CST_IOPORT);
150 }
151
152 static void
153 busy_wait(struct spic_softc *sc)
154 {
155 int i=0;
156
157 while(read_port2(sc) & 2) {
158 DELAY(10);
159 if (i++>10000) {
160 printf("spic busy wait abort\n");
161 return;
162 }
163 }
164 }
165
166 static void
167 busy_wait_cst(struct spic_softc *sc, int mask)
168 {
169 int i=0;
170
171 while(read_port_cst(sc) & mask) {
172 DELAY(10);
173 if (i++>10000) {
174 printf("spic busy wait abort\n");
175 return;
176 }
177 }
178 }
179
180 static u_char
181 spic_call1(struct spic_softc *sc, u_char dev) {
182 busy_wait(sc);
183 write_port2(sc, dev);
184 read_port2(sc);
185 return read_port1(sc);
186 }
187
188 static u_char
189 spic_call2(struct spic_softc *sc, u_char dev, u_char fn)
190 {
191 busy_wait(sc);
192 write_port2(sc, dev);
193 busy_wait(sc);
194 write_port1(sc, fn);
195 return read_port1(sc);
196 }
197
198 static void
199 spic_ecrset(struct spic_softc *sc, u_int16_t addr, u_int16_t value)
200 {
201 busy_wait_cst(sc, 3);
202 outb(SPIC_CST_IOPORT, 0x81);
203 busy_wait_cst(sc, 2);
204 outb(SPIC_DATA_IOPORT, addr);
205 busy_wait_cst(sc, 2);
206 outb(SPIC_DATA_IOPORT, value);
207 busy_wait_cst(sc, 2);
208 }
209
210 static void
211 spic_type2_srs(struct spic_softc *sc)
212 {
213 spic_ecrset(sc, SPIC_SHIB, (sc->sc_port_addr & 0xFF00) >> 8);
214 spic_ecrset(sc, SPIC_SLOB, sc->sc_port_addr & 0x00FF);
215 spic_ecrset(sc, SPIC_SIRQ, 0x00); /* using polling mode (IRQ=0)*/
216 DELAY(10);
217 }
218
219 static int
220 spic_probe(device_t dev)
221 {
222 struct spic_softc *sc;
223 u_char t, spic_irq;
224
225 sc = device_get_softc(dev);
226
227 /*
228 * We can only have 1 of these. Attempting to probe for a unit 1
229 * will destroy the work we did for unit 0
230 */
231 if (device_get_unit(dev))
232 return ENXIO;
233
234 bzero(sc, sizeof(struct spic_softc));
235
236 if (!(sc->sc_port_res = bus_alloc_resource(dev, SYS_RES_IOPORT,
237 &sc->sc_port_rid, 0, ~0, 5, RF_ACTIVE))) {
238 device_printf(dev,"Couldn't map I/O\n");
239 return ENXIO;
240 }
241 sc->sc_port_addr = (u_short)rman_get_start(sc->sc_port_res);
242
243 #ifdef notyet
244 if (!(sc->sc_intr_res = bus_alloc_resource_any(dev, SYS_RES_IRQ,
245 &sc->sc_intr_rid, RF_ACTIVE))) {
246 device_printf(dev,"Couldn't map IRQ\n");
247 bus_release_resource(dev, SYS_RES_IOPORT,
248 sc->sc_port_rid, sc->sc_port_res);
249 return ENXIO;
250 }
251 sc->sc_intr = (u_short)rman_get_start(sc->sc_intr_res);
252
253 switch (sc->sc_intr) {
254 case 0: spic_irq = 3; break;
255 case 5: spic_irq = 0; break;
256 case 0xa: spic_irq = 1; break;
257 case 0xb: spic_irq = 2; break;
258 default: device_printf(dev,"Invalid IRQ\n");
259 bus_release_resource(dev, SYS_RES_IOPORT,
260 sc->sc_port_rid, sc->sc_port_res);
261 bus_release_resource(dev, SYS_RES_IRQ,
262 sc->sc_intr_rid, sc->sc_intr_res);
263 return ENXIO;
264 }
265 #else
266 spic_irq = 3;
267 #endif
268
269 #if 0
270 if (sc->sc_port_addr != 0x10A0) {
271 bus_release_resource(dev, SYS_RES_IOPORT,
272 sc->sc_port_rid, sc->sc_port_res);
273 bus_release_resource(dev, SYS_RES_IRQ,
274 sc->sc_intr_rid, sc->sc_intr_res);
275 return ENXIO;
276 }
277 #endif
278
279 /* PIIX4 chipset at least? */
280 if (pci_cfgregread(PIIX4_BUS, PIIX4_SLOT, PIIX4_FUNC, 0, 4) ==
281 PIIX4_DEVID) {
282 sc->sc_model = SPIC_DEVICE_MODEL_TYPE1;
283 } else {
284 /* For newer VAIOs (R505, SRX7, ...) */
285 sc->sc_model = SPIC_DEVICE_MODEL_TYPE2;
286 }
287
288 /*
289 * This is an ugly hack. It is necessary until ACPI works correctly.
290 *
291 * The SPIC consists of 2 registers. They are mapped onto I/O by the
292 * PIIX4's General Device 10 function. There is also an interrupt
293 * control port at a somewhat magic location, but this first pass is
294 * polled.
295 *
296 * So the first thing we need to do is map the G10 space in.
297 *
298 */
299
300 /* Enable ACPI mode to get Fn key events */
301 /* XXX This may slow down your VAIO if ACPI is not supported in the kernel.
302 outb(0xb2, 0xf0);
303 */
304
305 device_printf(dev,"device model type = %d\n", sc->sc_model);
306
307 if(sc->sc_model == SPIC_DEVICE_MODEL_TYPE1) {
308 pci_cfgregwrite(PIIX4_BUS, PIIX4_SLOT, PIIX4_FUNC, G10A,
309 sc->sc_port_addr, 2);
310 t = pci_cfgregread(PIIX4_BUS, PIIX4_SLOT, PIIX4_FUNC, G10L, 1);
311 t &= 0xf0;
312 t |= 4;
313 pci_cfgregwrite(PIIX4_BUS, PIIX4_SLOT, PIIX4_FUNC, G10L, t, 1);
314 outw(SPIC_IRQ_PORT, (inw(SPIC_IRQ_PORT) & ~(0x3 << SPIC_IRQ_SHIFT)) | (spic_irq << SPIC_IRQ_SHIFT));
315 t = pci_cfgregread(PIIX4_BUS, PIIX4_SLOT, PIIX4_FUNC, G10L, 1);
316 t &= 0x1f;
317 t |= 0xc0;
318 pci_cfgregwrite(PIIX4_BUS, PIIX4_SLOT, PIIX4_FUNC, G10L, t, 1);
319 } else {
320 spic_type2_srs(sc);
321 }
322
323 /*
324 * XXX: Should try and see if there's anything actually there.
325 */
326
327 device_set_desc(dev, "Sony Programmable I/O Controller");
328
329 return 0;
330 }
331
332 static int
333 spic_attach(device_t dev)
334 {
335 struct spic_softc *sc;
336
337 sc = device_get_softc(dev);
338
339 sc->sc_dev = dev;
340
341 spic_pollrate = (hz/50); /* Every 50th of a second */
342
343 spic_call1(sc, 0x82);
344 spic_call2(sc, 0x81, 0xff);
345 spic_call1(sc, 0x92);
346
347 /* There can be only one */
348 make_dev(&spic_cdevsw, 0, 0, 0, 0600, "jogdial");
349
350 return 0;
351 }
352
353 static void
354 spictimeout(void *arg)
355 {
356 struct spic_softc *sc = arg;
357 u_char b, event, param;
358 int j;
359
360 if (!sc->sc_opened) {
361 device_printf(sc->sc_dev, "timeout called while closed!\n");
362 return;
363 }
364
365 event = read_port2(sc);
366 param = read_port1(sc);
367
368 if ((event != 4) && (!(event & 0x1)))
369 switch(event) {
370 case 0x10: /* jog wheel event (type1) */
371 if (sc->sc_model == SPIC_DEVICE_MODEL_TYPE1) {
372 b = !!(param & 0x40);
373 if (b != sc->sc_buttonlast) {
374 sc->sc_buttonlast = b;
375 sc->sc_buf[sc->sc_count++] =
376 b?'d':'u';
377 }
378 j = (param & 0xf) | ((param & 0x10)? ~0xf:0);
379 if (j<0)
380 while(j++!=0) {
381 sc->sc_buf[sc->sc_count++] =
382 'l';
383 }
384 else if (j>0)
385 while(j--!=0) {
386 sc->sc_buf[sc->sc_count++] =
387 'r';
388 }
389 }
390 break;
391 case 0x08: /* jog wheel event (type2) */
392 case 0x00:
393 /* SPIC_DEVICE_MODEL_TYPE2 returns jog wheel event=0x00 */
394 if (sc->sc_model == SPIC_DEVICE_MODEL_TYPE2) {
395 b = !!(param & 0x40);
396 if (b != sc->sc_buttonlast) {
397 sc->sc_buttonlast = b;
398 sc->sc_buf[sc->sc_count++] =
399 b?'d':'u';
400 }
401 j = (param & 0xf) | ((param & 0x10)? ~0xf:0);
402 if (j<0)
403 while(j++!=0) {
404 sc->sc_buf[sc->sc_count++] =
405 'l';
406 }
407 else if (j>0)
408 while(j--!=0) {
409 sc->sc_buf[sc->sc_count++] =
410 'r';
411 }
412 }
413 break;
414 case 0x60: /* Capture button */
415 printf("Capture button event: %x\n",param);
416 break;
417 case 0x30: /* Lid switch */
418 printf("Lid switch event: %x\n",param);
419 break;
420 case 0x0c: /* We must ignore these type of event for C1VP... */
421 case 0x70: /* Closing/Opening the lid on C1VP */
422 break;
423 default:
424 printf("Unknown event: event %02x param %02x\n", event, param);
425 break;
426 }
427 else {
428 /* No event. Wait some more */
429 sc->sc_timeout_ch = timeout(spictimeout, sc, spic_pollrate);
430 return;
431 }
432
433 if (sc->sc_count) {
434 if (sc->sc_sleeping) {
435 sc->sc_sleeping = 0;
436 wakeup( sc);
437 }
438 selwakeuppri(&sc->sc_rsel, PZERO);
439 }
440 spic_call2(sc, 0x81, 0xff); /* Clear event */
441
442 sc->sc_timeout_ch = timeout(spictimeout, sc, spic_pollrate);
443 }
444
445 static int
446 spicopen(struct cdev *dev, int flag, int fmt, struct thread *td)
447 {
448 struct spic_softc *sc;
449
450 sc = devclass_get_softc(spic_devclass, 0);
451
452 if (sc->sc_opened)
453 return EBUSY;
454
455 sc->sc_opened++;
456 sc->sc_count=0;
457
458 /* Start the polling */
459 timeout(spictimeout, sc, spic_pollrate);
460 return 0;
461 }
462
463 static int
464 spicclose(struct cdev *dev, int flag, int fmt, struct thread *td)
465 {
466 struct spic_softc *sc;
467
468 sc = devclass_get_softc(spic_devclass, 0);
469
470 /* Stop polling */
471 untimeout(spictimeout, sc, sc->sc_timeout_ch);
472 sc->sc_opened = 0;
473 return 0;
474 }
475
476 static int
477 spicread(struct cdev *dev, struct uio *uio, int flag)
478 {
479 struct spic_softc *sc;
480 int l, s, error;
481 u_char buf[SCBUFLEN];
482
483 sc = devclass_get_softc(spic_devclass, 0);
484
485 if (uio->uio_resid <= 0) /* What kind of a read is this?! */
486 return 0;
487
488 s = spltty();
489 while (!(sc->sc_count)) {
490 sc->sc_sleeping=1;
491 error = tsleep( sc, PZERO | PCATCH, "jogrea", 0);
492 sc->sc_sleeping=0;
493 if (error) {
494 splx(s);
495 return error;
496 }
497 }
498 splx(s);
499
500 s = spltty();
501 l = min(uio->uio_resid, sc->sc_count);
502 bcopy(sc->sc_buf, buf, l);
503 sc->sc_count -= l;
504 bcopy(sc->sc_buf + l, sc->sc_buf, l);
505 splx(s);
506 return uiomove(buf, l, uio);
507
508 }
509
510 static int
511 spicioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
512 {
513 struct spic_softc *sc;
514
515 sc = devclass_get_softc(spic_devclass, 0);
516
517 return EIO;
518 }
519
520 static int
521 spicpoll(struct cdev *dev, int events, struct thread *td)
522 {
523 struct spic_softc *sc;
524 int revents = 0, s;
525
526 sc = devclass_get_softc(spic_devclass, 0);
527 s = spltty();
528 if (events & (POLLIN | POLLRDNORM)) {
529 if (sc->sc_count)
530 revents |= events & (POLLIN | POLLRDNORM);
531 else
532 selrecord(td, &sc->sc_rsel); /* Who shall we wake? */
533 }
534 splx(s);
535
536 return revents;
537 }
538
539
540 static device_method_t spic_methods[] = {
541 DEVMETHOD(device_probe, spic_probe),
542 DEVMETHOD(device_attach, spic_attach),
543
544 { 0, 0 }
545 };
546
547 static driver_t spic_driver = {
548 "spic",
549 spic_methods,
550 sizeof(struct spic_softc),
551 };
552
553 DRIVER_MODULE(spic, isa, spic_driver, spic_devclass, 0, 0);
554
Cache object: 2a780db34ba1b2763c35333d35166545
|