spic.c revision 69873
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 69873 2000-12-11 19:41:48Z nsayer $
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/dkstat.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        /* open */      spicopen,
89        /* close */     spicclose,
90        /* read */      spicread,
91        /* write */     nowrite,
92        /* ioctl */     spicioctl,
93        /* poll */      spicpoll,
94        /* mmap */      nommap,
95        /* strategy */  nostrategy,
96        /* name */      "spic",
97        /* maj */       CDEV_MAJOR,
98        /* dump */      nodump,
99        /* psize */     nopsize,
100        /* flags */     0,
101        /* bmaj */      -1
102};
103
104#define SCBUFLEN 128
105
106struct spic_softc {
107	u_short sc_port_addr;
108	u_char sc_intr;
109	struct resource *sc_port_res,*sc_intr_res;
110	int	sc_port_rid,sc_intr_rid;
111	int sc_opened;
112	int sc_sleeping;
113	int sc_buttonlast;
114	struct callout_handle sc_timeout_ch;
115	device_t sc_dev;
116	struct selinfo sc_rsel;
117	u_char sc_buf[SCBUFLEN];
118	int sc_count;
119};
120
121static void
122write_port1(struct spic_softc *sc, u_char val)
123{
124	DELAY(10);
125	outb(sc->sc_port_addr, val);
126}
127
128static void
129write_port2(struct spic_softc *sc, u_char val)
130{
131	DELAY(10);
132	outb(sc->sc_port_addr + 4, val);
133}
134
135static u_char
136read_port1(struct spic_softc *sc)
137{
138	DELAY(10);
139	return inb(sc->sc_port_addr);
140}
141
142static u_char
143read_port2(struct spic_softc *sc)
144{
145	DELAY(10);
146	return inb(sc->sc_port_addr + 4);
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 u_char
164spic_call1(struct spic_softc *sc, u_char dev) {
165	busy_wait(sc);
166	write_port2(sc, dev);
167	read_port2(sc);
168	return read_port1(sc);
169}
170
171static u_char
172spic_call2(struct spic_softc *sc, u_char dev, u_char fn)
173{
174	busy_wait(sc);
175	write_port2(sc, dev);
176	busy_wait(sc);
177	write_port1(sc, fn);
178	return read_port1(sc);
179}
180
181static int
182spic_probe(device_t dev)
183{
184        struct spic_softc *sc;
185	u_char t, spic_irq;
186
187        sc = device_get_softc(dev);
188
189	/*
190	 * We can only have 1 of these. Attempting to probe for a unit 1
191	 * will destroy the work we did for unit 0
192	 */
193	if (device_get_unit(dev))
194		return ENXIO;
195
196	bzero(sc, sizeof(struct spic_softc));
197
198	if (!(sc->sc_port_res = bus_alloc_resource(dev, SYS_RES_IOPORT,
199		&sc->sc_port_rid, 0, ~0, 5, RF_ACTIVE))) {
200		device_printf(dev,"Couldn't map I/O\n");
201		return ENXIO;
202	}
203	sc->sc_port_addr = (u_short)rman_get_start(sc->sc_port_res);
204
205#ifdef notyet
206	if (!(sc->sc_intr_res = bus_alloc_resource(dev, SYS_RES_IRQ,
207		&sc->sc_intr_rid, 0, ~0, 1, RF_ACTIVE))) {
208		device_printf(dev,"Couldn't map IRQ\n");
209		bus_release_resource(dev, SYS_RES_IOPORT,
210			sc->sc_port_rid, sc->sc_port_res);
211		return ENXIO;
212	}
213	sc->sc_intr = (u_short)rman_get_start(sc->sc_intr_res);
214
215	switch (sc->sc_intr) {
216		case 0: spic_irq = 3; break;
217		case 5: spic_irq = 0; break;
218		case 0xa: spic_irq = 1; break;
219		case 0xb: spic_irq = 2; break;
220		default: device_printf(dev,"Invalid IRQ\n");
221			bus_release_resource(dev, SYS_RES_IOPORT,
222				sc->sc_port_rid, sc->sc_port_res);
223			bus_release_resource(dev, SYS_RES_IRQ,
224				sc->sc_intr_rid, sc->sc_intr_res);
225			return ENXIO;
226	}
227#else
228	spic_irq = 3;
229#endif
230
231#if 0
232	if (sc->sc_port_addr != 0x10A0) {
233		bus_release_resource(dev, SYS_RES_IOPORT,
234			sc->sc_port_rid, sc->sc_port_res);
235		bus_release_resource(dev, SYS_RES_IRQ,
236			sc->sc_intr_rid, sc->sc_intr_res);
237		return ENXIO;
238	}
239#endif
240
241	/* PIIX4 chipset at least? */
242	if (pci_cfgregread(PIIX4_BUS, PIIX4_SLOT, PIIX4_FUNC, 0, 4) !=
243		PIIX4_DEVID)
244		return ENXIO;
245
246	/*
247	 * This is an ugly hack. It is necessary until ACPI works correctly.
248	 *
249	 * The SPIC consists of 2 registers. They are mapped onto I/O by the
250	 * PIIX4's General Device 10 function. There is also an interrupt
251	 * control port at a somewhat magic location, but this first pass is
252	 * polled.
253	 *
254	 * So the first thing we need to do is map the G10 space in.
255	 *
256	 */
257
258	pci_cfgregwrite(PIIX4_BUS, PIIX4_SLOT, PIIX4_FUNC, G10A,
259		sc->sc_port_addr, 2);
260	t = pci_cfgregread(PIIX4_BUS, PIIX4_SLOT, PIIX4_FUNC, G10L, 1);
261	t &= 0xf0;
262	t |= 4;
263	pci_cfgregwrite(PIIX4_BUS, PIIX4_SLOT, PIIX4_FUNC, G10L, t, 1);
264	outw(SPIC_IRQ_PORT, (inw(SPIC_IRQ_PORT) & ~(0x3 << SPIC_IRQ_SHIFT)) | (spic_irq << SPIC_IRQ_SHIFT));
265	t = pci_cfgregread(PIIX4_BUS, PIIX4_SLOT, PIIX4_FUNC, G10L, 1);
266	t &= 0x1f;
267	t |= 0xc0;
268	pci_cfgregwrite(PIIX4_BUS, PIIX4_SLOT, PIIX4_FUNC, G10L, t, 1);
269
270	/*
271	 * XXX: Should try and see if there's anything actually there.
272	 */
273
274	device_set_desc(dev, "Sony Programmable I/O Controller");
275
276	return 0;
277}
278
279static int
280spic_attach(device_t dev)
281{
282        struct spic_softc *sc;
283
284        sc = device_get_softc(dev);
285
286	sc->sc_dev = dev;
287
288	spic_pollrate = (hz/50); /* Every 50th of a second */
289
290	spic_call1(sc, 0x82);
291	spic_call2(sc, 0x81, 0xff);
292	spic_call1(sc, 0x92);
293
294        /* There can be only one */
295        make_dev(&spic_cdevsw, 0, 0, 0, 0600, "jogdial");
296
297	return 0;
298}
299
300static void
301spictimeout(void *arg)
302{
303        struct spic_softc *sc = arg;
304	u_char b, event, param;
305	int j;
306
307        if (!sc->sc_opened) {
308                device_printf(sc->sc_dev, "timeout called while closed!\n");
309                return;
310        }
311
312	event = read_port2(sc);
313	param = read_port1(sc);
314
315	if ((event != 4) && (!(event & 0x1)))
316		switch(event) {
317			case 0x10: /* jog wheel event */
318				b = !!(param & 0x40);
319				if (b != sc->sc_buttonlast) {
320					sc->sc_buttonlast = b;
321					sc->sc_buf[sc->sc_count++] =
322						b?'d':'u';
323				}
324				j = (param & 0xf) | ((param & 0x10)? ~0xf:0);
325				if (j<0)
326					while(j++!=0) {
327						sc->sc_buf[sc->sc_count++] =
328							'l';
329					}
330				else if (j>0)
331					while(j--!=0) {
332						sc->sc_buf[sc->sc_count++] =
333							'r';
334					}
335				break;
336			case 0x60: /* Capture button */
337				printf("Capture button event: %x\n",param);
338				break;
339			case 0x30: /* Lid switch */
340				printf("Lid switch event: %x\n",param);
341				break;
342			default:
343				printf("Unknown event: %x %x\n", event, param);
344				break;
345		}
346	else {
347		/* No event. Wait some more */
348        	sc->sc_timeout_ch = timeout(spictimeout, sc, spic_pollrate);
349		return;
350	}
351
352	if (sc->sc_count) {
353		if (sc->sc_sleeping) {
354			sc->sc_sleeping = 0;
355			wakeup((caddr_t) sc);
356		}
357		selwakeup(&sc->sc_rsel);
358	}
359
360	spic_call2(sc, 0x81, 0xff); /* Clear event */
361
362        sc->sc_timeout_ch = timeout(spictimeout, sc, spic_pollrate);
363}
364
365static int
366spicopen(dev_t dev, int flag, int fmt, struct proc *p)
367{
368        struct spic_softc *sc;
369
370        sc = devclass_get_softc(spic_devclass, 0);
371
372        if (sc->sc_opened)
373                return EBUSY;
374
375	sc->sc_opened++;
376	sc->sc_count=0;
377
378        /* Start the polling */
379        timeout(spictimeout, sc, spic_pollrate);
380        return 0;
381}
382
383static int
384spicclose(dev_t dev, int flag, int fmt, struct proc *p)
385{
386        struct spic_softc *sc;
387
388        sc = devclass_get_softc(spic_devclass, 0);
389
390        /* Stop polling */
391        untimeout(spictimeout, sc, sc->sc_timeout_ch);
392        sc->sc_opened = 0;
393        return 0;
394}
395
396static int
397spicread(dev_t dev, struct uio *uio, int flag)
398{
399        struct spic_softc *sc;
400	int l, s, error;
401	u_char buf[SCBUFLEN];
402
403        sc = devclass_get_softc(spic_devclass, 0);
404
405	if (uio->uio_resid <= 0) /* What kind of a read is this?! */
406		return 0;
407
408	s = spltty();
409	while (!(sc->sc_count)) {
410		sc->sc_sleeping=1;
411		error = tsleep((caddr_t) sc, PZERO | PCATCH, "jogrea", 0);
412		sc->sc_sleeping=0;
413		if (error) {
414			splx(s);
415			return error;
416		}
417	}
418	splx(s);
419
420	s = spltty();
421	l = min(uio->uio_resid, sc->sc_count);
422	bcopy(sc->sc_buf, buf, l);
423	sc->sc_count -= l;
424	bcopy(sc->sc_buf + l, sc->sc_buf, l);
425	splx(s);
426	return uiomove(buf, l, uio);
427
428}
429
430static int
431spicioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
432{
433        struct spic_softc *sc;
434
435        sc = devclass_get_softc(spic_devclass, 0);
436
437	return EIO;
438}
439
440static int
441spicpoll(dev_t dev, int events, struct proc *p)
442{
443        struct spic_softc *sc;
444        int revents = 0, s;
445
446        sc = devclass_get_softc(spic_devclass, 0);
447        s = spltty();
448        if (events & (POLLIN | POLLRDNORM)) {
449                if (sc->sc_count)
450                        revents |= events & (POLLIN | POLLRDNORM);
451                else
452                        selrecord(p, &sc->sc_rsel); /* Who shall we wake? */
453        }
454        splx(s);
455
456        return revents;
457}
458
459
460static device_method_t spic_methods[] = {
461	DEVMETHOD(device_probe,		spic_probe),
462	DEVMETHOD(device_attach,	spic_attach),
463
464	{ 0, 0 }
465};
466
467static driver_t spic_driver = {
468	"spic",
469	spic_methods,
470	sizeof(struct spic_softc),
471};
472
473DRIVER_MODULE(spic, isa, spic_driver, spic_devclass, 0, 0);
474
475