spic.c revision 111002
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 111002 2003-02-16 14:13:23Z 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        /* open */      spicopen,
88        /* close */     spicclose,
89        /* read */      spicread,
90        /* write */     nowrite,
91        /* ioctl */     spicioctl,
92        /* poll */      spicpoll,
93        /* mmap */      nommap,
94        /* strategy */  nostrategy,
95        /* name */      "spic",
96        /* maj */       CDEV_MAJOR,
97        /* dump */      nodump,
98        /* psize */     nopsize,
99        /* flags */     0,
100};
101
102#define SCBUFLEN 128
103
104struct spic_softc {
105	u_short sc_port_addr;
106	u_char sc_intr;
107	struct resource *sc_port_res,*sc_intr_res;
108	int	sc_port_rid,sc_intr_rid;
109	int sc_opened;
110	int sc_sleeping;
111	int sc_buttonlast;
112	struct callout_handle sc_timeout_ch;
113	device_t sc_dev;
114	struct selinfo sc_rsel;
115	u_char sc_buf[SCBUFLEN];
116	int sc_count;
117	int sc_model;
118};
119
120static void
121write_port1(struct spic_softc *sc, u_char val)
122{
123	DELAY(10);
124	outb(sc->sc_port_addr, val);
125}
126
127static void
128write_port2(struct spic_softc *sc, u_char val)
129{
130	DELAY(10);
131	outb(sc->sc_port_addr + 4, val);
132}
133
134static u_char
135read_port1(struct spic_softc *sc)
136{
137	DELAY(10);
138	return inb(sc->sc_port_addr);
139}
140
141static u_char
142read_port2(struct spic_softc *sc)
143{
144	DELAY(10);
145	return inb(sc->sc_port_addr + 4);
146}
147
148static u_char
149read_port_cst(struct spic_softc *sc)
150{
151	DELAY(10);
152	return inb(SPIC_CST_IOPORT);
153}
154
155static void
156busy_wait(struct spic_softc *sc)
157{
158	int i=0;
159
160	while(read_port2(sc) & 2) {
161		DELAY(10);
162		if (i++>10000) {
163			printf("spic busy wait abort\n");
164			return;
165		}
166	}
167}
168
169static void
170busy_wait_cst(struct spic_softc *sc, int mask)
171{
172	int i=0;
173
174	while(read_port_cst(sc) & mask) {
175		DELAY(10);
176		if (i++>10000) {
177			printf("spic busy wait abort\n");
178			return;
179		}
180	}
181}
182
183static u_char
184spic_call1(struct spic_softc *sc, u_char dev) {
185	busy_wait(sc);
186	write_port2(sc, dev);
187	read_port2(sc);
188	return read_port1(sc);
189}
190
191static u_char
192spic_call2(struct spic_softc *sc, u_char dev, u_char fn)
193{
194	busy_wait(sc);
195	write_port2(sc, dev);
196	busy_wait(sc);
197	write_port1(sc, fn);
198	return read_port1(sc);
199}
200
201static void
202spic_ecrset(struct spic_softc *sc, u_int16_t addr, u_int16_t value)
203{
204	busy_wait_cst(sc, 3);
205	outb(SPIC_CST_IOPORT, 0x81);
206	busy_wait_cst(sc, 2);
207	outb(SPIC_DATA_IOPORT, addr);
208	busy_wait_cst(sc, 2);
209	outb(SPIC_DATA_IOPORT, value);
210	busy_wait_cst(sc, 2);
211}
212
213static void
214spic_type2_srs(struct spic_softc *sc)
215{
216	spic_ecrset(sc, SPIC_SHIB, (sc->sc_port_addr & 0xFF00) >> 8);
217	spic_ecrset(sc, SPIC_SLOB,  sc->sc_port_addr & 0x00FF);
218	spic_ecrset(sc, SPIC_SIRQ,  0x00); /* using polling mode (IRQ=0)*/
219	DELAY(10);
220}
221
222static int
223spic_probe(device_t dev)
224{
225	struct spic_softc *sc;
226	u_char t, spic_irq;
227
228	sc = device_get_softc(dev);
229
230	/*
231	 * We can only have 1 of these. Attempting to probe for a unit 1
232	 * will destroy the work we did for unit 0
233	 */
234	if (device_get_unit(dev))
235		return ENXIO;
236
237	bzero(sc, sizeof(struct spic_softc));
238
239	if (!(sc->sc_port_res = bus_alloc_resource(dev, SYS_RES_IOPORT,
240		&sc->sc_port_rid, 0, ~0, 5, RF_ACTIVE))) {
241		device_printf(dev,"Couldn't map I/O\n");
242		return ENXIO;
243	}
244	sc->sc_port_addr = (u_short)rman_get_start(sc->sc_port_res);
245
246#ifdef notyet
247	if (!(sc->sc_intr_res = bus_alloc_resource(dev, SYS_RES_IRQ,
248		&sc->sc_intr_rid, 0, ~0, 1, RF_ACTIVE))) {
249		device_printf(dev,"Couldn't map IRQ\n");
250		bus_release_resource(dev, SYS_RES_IOPORT,
251			sc->sc_port_rid, sc->sc_port_res);
252		return ENXIO;
253	}
254	sc->sc_intr = (u_short)rman_get_start(sc->sc_intr_res);
255
256	switch (sc->sc_intr) {
257		case 0: spic_irq = 3; break;
258		case 5: spic_irq = 0; break;
259		case 0xa: spic_irq = 1; break;
260		case 0xb: spic_irq = 2; break;
261		default: device_printf(dev,"Invalid IRQ\n");
262			bus_release_resource(dev, SYS_RES_IOPORT,
263				sc->sc_port_rid, sc->sc_port_res);
264			bus_release_resource(dev, SYS_RES_IRQ,
265				sc->sc_intr_rid, sc->sc_intr_res);
266			return ENXIO;
267	}
268#else
269	spic_irq = 3;
270#endif
271
272#if 0
273	if (sc->sc_port_addr != 0x10A0) {
274		bus_release_resource(dev, SYS_RES_IOPORT,
275			sc->sc_port_rid, sc->sc_port_res);
276		bus_release_resource(dev, SYS_RES_IRQ,
277			sc->sc_intr_rid, sc->sc_intr_res);
278		return ENXIO;
279	}
280#endif
281
282	/* PIIX4 chipset at least? */
283	if (pci_cfgregread(PIIX4_BUS, PIIX4_SLOT, PIIX4_FUNC, 0, 4) ==
284		PIIX4_DEVID) {
285		sc->sc_model = SPIC_DEVICE_MODEL_TYPE1;
286	} else {
287		/* For newer VAIOs (R505, SRX7, ...) */
288		sc->sc_model = SPIC_DEVICE_MODEL_TYPE2;
289	}
290
291	/*
292	 * This is an ugly hack. It is necessary until ACPI works correctly.
293	 *
294	 * The SPIC consists of 2 registers. They are mapped onto I/O by the
295	 * PIIX4's General Device 10 function. There is also an interrupt
296	 * control port at a somewhat magic location, but this first pass is
297	 * polled.
298	 *
299	 * So the first thing we need to do is map the G10 space in.
300	 *
301	 */
302
303	/* Enable ACPI mode to get Fn key events */
304	/* XXX This may slow down your VAIO if ACPI is not supported in the kernel.
305	outb(0xb2, 0xf0);
306	*/
307
308	device_printf(dev,"device model type = %d\n", sc->sc_model);
309
310	if(sc->sc_model == SPIC_DEVICE_MODEL_TYPE1) {
311		pci_cfgregwrite(PIIX4_BUS, PIIX4_SLOT, PIIX4_FUNC, G10A,
312				sc->sc_port_addr, 2);
313		t = pci_cfgregread(PIIX4_BUS, PIIX4_SLOT, PIIX4_FUNC, G10L, 1);
314		t &= 0xf0;
315		t |= 4;
316		pci_cfgregwrite(PIIX4_BUS, PIIX4_SLOT, PIIX4_FUNC, G10L, t, 1);
317		outw(SPIC_IRQ_PORT, (inw(SPIC_IRQ_PORT) & ~(0x3 << SPIC_IRQ_SHIFT)) | (spic_irq << SPIC_IRQ_SHIFT));
318		t = pci_cfgregread(PIIX4_BUS, PIIX4_SLOT, PIIX4_FUNC, G10L, 1);
319		t &= 0x1f;
320		t |= 0xc0;
321		pci_cfgregwrite(PIIX4_BUS, PIIX4_SLOT, PIIX4_FUNC, G10L, t, 1);
322	} else {
323		spic_type2_srs(sc);
324	}
325
326	/*
327	 * XXX: Should try and see if there's anything actually there.
328	 */
329
330	device_set_desc(dev, "Sony Programmable I/O Controller");
331
332	return 0;
333}
334
335static int
336spic_attach(device_t dev)
337{
338	struct spic_softc *sc;
339
340	sc = device_get_softc(dev);
341
342	sc->sc_dev = dev;
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_cdevsw, 0, 0, 0, 0600, "jogdial");
352
353	return 0;
354}
355
356static void
357spictimeout(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				printf("Capture button event: %x\n",param);
419				break;
420			case 0x30: /* Lid switch */
421				printf("Lid switch event: %x\n",param);
422				break;
423			case 0x0c: /* We must ignore these type of event for C1VP... */
424			case 0x70: /* Closing/Opening the lid on C1VP */
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
448static int
449spicopen(dev_t dev, int flag, int fmt, struct thread *td)
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
466static int
467spicclose(dev_t dev, int flag, int fmt, struct thread *td)
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
479static int
480spicread(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
513static int
514spicioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
515{
516	struct spic_softc *sc;
517
518	sc = devclass_get_softc(spic_devclass, 0);
519
520	return EIO;
521}
522
523static int
524spicpoll(dev_t dev, int events, struct thread *td)
525{
526	struct spic_softc *sc;
527	int revents = 0, s;
528
529	sc = devclass_get_softc(spic_devclass, 0);
530	s = spltty();
531	if (events & (POLLIN | POLLRDNORM)) {
532		if (sc->sc_count)
533			revents |= events & (POLLIN | POLLRDNORM);
534		else
535			selrecord(td, &sc->sc_rsel); /* Who shall we wake? */
536	}
537	splx(s);
538
539	return revents;
540}
541
542
543static device_method_t spic_methods[] = {
544	DEVMETHOD(device_probe,		spic_probe),
545	DEVMETHOD(device_attach,	spic_attach),
546
547	{ 0, 0 }
548};
549
550static driver_t spic_driver = {
551	"spic",
552	spic_methods,
553	sizeof(struct spic_softc),
554};
555
556DRIVER_MODULE(spic, isa, spic_driver, spic_devclass, 0, 0);
557
558