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