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