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