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